2000-08-22 Eric Christopher <echristo@cygnus.com>
[deliverable/binutils-gdb.git] / gas / write.c
1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This thing should be set up to do byteordering correctly. But... */
23
24 #include "as.h"
25 #include "subsegs.h"
26 #include "obstack.h"
27 #include "output-file.h"
28
29 /* This looks like a good idea. Let's try turning it on always, for now. */
30 #undef BFD_FAST_SECTION_FILL
31 #define BFD_FAST_SECTION_FILL
32
33 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
34 instruction so that the disassembler does not choke on it. */
35 #ifndef NOP_OPCODE
36 #define NOP_OPCODE 0x00
37 #endif
38
39 #ifndef TC_ADJUST_RELOC_COUNT
40 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
41 #endif
42
43 #ifndef TC_FORCE_RELOCATION
44 #define TC_FORCE_RELOCATION(FIXP) 0
45 #endif
46
47 #ifndef TC_FORCE_RELOCATION_SECTION
48 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
49 #endif
50
51 #ifndef TC_LINKRELAX_FIXUP
52 #define TC_LINKRELAX_FIXUP(SEG) 1
53 #endif
54
55 #ifndef MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
57 #endif
58
59 #ifndef WORKING_DOT_WORD
60 extern CONST int md_short_jump_size;
61 extern CONST int md_long_jump_size;
62 #endif
63
64 int symbol_table_frozen;
65 void print_fixup PARAMS ((fixS *));
66
67 #ifdef BFD_ASSEMBLER
68 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
69
70 /* We generally attach relocs to frag chains. However, after we have
71 chained these all together into a segment, any relocs we add after
72 that must be attached to a segment. This will include relocs added
73 in md_estimate_size_for_relax, for example. */
74 static int frags_chained = 0;
75 #endif
76
77 #ifndef BFD_ASSEMBLER
78
79 #ifndef MANY_SEGMENTS
80 struct frag *text_frag_root;
81 struct frag *data_frag_root;
82 struct frag *bss_frag_root;
83
84 struct frag *text_last_frag; /* Last frag in segment. */
85 struct frag *data_last_frag; /* Last frag in segment. */
86 static struct frag *bss_last_frag; /* Last frag in segment. */
87 #endif
88
89 #ifndef BFD
90 static object_headers headers;
91 #endif
92
93 long string_byte_count;
94 char *next_object_file_charP; /* Tracks object file bytes. */
95
96 #ifndef OBJ_VMS
97 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
98 #endif
99
100 #endif /* BFD_ASSEMBLER */
101
102 static int n_fixups;
103
104 #ifdef BFD_ASSEMBLER
105 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
106 symbolS *add, symbolS *sub,
107 offsetT offset, int pcrel,
108 bfd_reloc_code_real_type r_type));
109 #else
110 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
111 symbolS *add, symbolS *sub,
112 offsetT offset, int pcrel,
113 int r_type));
114 #endif
115 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
116 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
117 #endif
118 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
119 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
120 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
121 #endif
122 #ifdef BFD_ASSEMBLER
123 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
124 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
125 static void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
126 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
127 static void write_relocs PARAMS ((bfd *, asection *, PTR));
128 static void write_contents PARAMS ((bfd *, asection *, PTR));
129 static void set_symtab PARAMS ((void));
130 #endif
131 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
132 static void merge_data_into_text PARAMS ((void));
133 #endif
134 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
135 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
136 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
137 static void relax_and_size_all_segments PARAMS ((void));
138 #endif
139 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
140 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
141 #endif
142
143 /* Create a fixS in obstack 'notes'. */
144
145 static fixS *
146 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
147 r_type)
148 fragS *frag; /* Which frag? */
149 int where; /* Where in that frag? */
150 int size; /* 1, 2, or 4 usually. */
151 symbolS *add_symbol; /* X_add_symbol. */
152 symbolS *sub_symbol; /* X_op_symbol. */
153 offsetT offset; /* X_add_number. */
154 int pcrel; /* TRUE if PC-relative relocation. */
155 #ifdef BFD_ASSEMBLER
156 bfd_reloc_code_real_type r_type; /* Relocation type. */
157 #else
158 int r_type; /* Relocation type. */
159 #endif
160 {
161 fixS *fixP;
162
163 n_fixups++;
164
165 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
166
167 fixP->fx_frag = frag;
168 fixP->fx_where = where;
169 fixP->fx_size = size;
170 /* We've made fx_size a narrow field; check that it's wide enough. */
171 if (fixP->fx_size != size)
172 {
173 as_bad (_("field fx_size too small to hold %d"), size);
174 abort ();
175 }
176 fixP->fx_addsy = add_symbol;
177 fixP->fx_subsy = sub_symbol;
178 fixP->fx_offset = offset;
179 fixP->fx_pcrel = pcrel;
180 fixP->fx_plt = 0;
181 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
182 fixP->fx_r_type = r_type;
183 #endif
184 fixP->fx_im_disp = 0;
185 fixP->fx_pcrel_adjust = 0;
186 fixP->fx_bit_fixP = 0;
187 fixP->fx_addnumber = 0;
188 fixP->fx_tcbit = 0;
189 fixP->fx_done = 0;
190 fixP->fx_no_overflow = 0;
191 fixP->fx_signed = 0;
192
193 #ifdef USING_CGEN
194 fixP->fx_cgen.insn = NULL;
195 fixP->fx_cgen.opinfo = 0;
196 #endif
197
198 #ifdef TC_FIX_TYPE
199 TC_INIT_FIX_DATA (fixP);
200 #endif
201
202 as_where (&fixP->fx_file, &fixP->fx_line);
203
204 /* Usually, we want relocs sorted numerically, but while
205 comparing to older versions of gas that have relocs
206 reverse sorted, it is convenient to have this compile
207 time option. xoxorich. */
208 {
209
210 #ifdef BFD_ASSEMBLER
211 fixS **seg_fix_rootP = (frags_chained
212 ? &seg_info (now_seg)->fix_root
213 : &frchain_now->fix_root);
214 fixS **seg_fix_tailP = (frags_chained
215 ? &seg_info (now_seg)->fix_tail
216 : &frchain_now->fix_tail);
217 #endif
218
219 #ifdef REVERSE_SORT_RELOCS
220
221 fixP->fx_next = *seg_fix_rootP;
222 *seg_fix_rootP = fixP;
223
224 #else /* REVERSE_SORT_RELOCS */
225
226 fixP->fx_next = NULL;
227
228 if (*seg_fix_tailP)
229 (*seg_fix_tailP)->fx_next = fixP;
230 else
231 *seg_fix_rootP = fixP;
232 *seg_fix_tailP = fixP;
233
234 #endif /* REVERSE_SORT_RELOCS */
235 }
236
237 return fixP;
238 }
239
240 /* Create a fixup relative to a symbol (plus a constant). */
241
242 fixS *
243 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
244 fragS *frag; /* Which frag? */
245 int where; /* Where in that frag? */
246 int size; /* 1, 2, or 4 usually. */
247 symbolS *add_symbol; /* X_add_symbol. */
248 offsetT offset; /* X_add_number. */
249 int pcrel; /* TRUE if PC-relative relocation. */
250 #ifdef BFD_ASSEMBLER
251 bfd_reloc_code_real_type r_type; /* Relocation type. */
252 #else
253 int r_type; /* Relocation type. */
254 #endif
255 {
256 return fix_new_internal (frag, where, size, add_symbol,
257 (symbolS *) NULL, offset, pcrel, r_type);
258 }
259
260 /* Create a fixup for an expression. Currently we only support fixups
261 for difference expressions. That is itself more than most object
262 file formats support anyhow. */
263
264 fixS *
265 fix_new_exp (frag, where, size, exp, pcrel, r_type)
266 fragS *frag; /* Which frag? */
267 int where; /* Where in that frag? */
268 int size; /* 1, 2, or 4 usually. */
269 expressionS *exp; /* Expression. */
270 int pcrel; /* TRUE if PC-relative relocation. */
271 #ifdef BFD_ASSEMBLER
272 bfd_reloc_code_real_type r_type; /* Relocation type. */
273 #else
274 int r_type; /* Relocation type. */
275 #endif
276 {
277 symbolS *add = NULL;
278 symbolS *sub = NULL;
279 offsetT off = 0;
280
281 switch (exp->X_op)
282 {
283 case O_absent:
284 break;
285
286 case O_add:
287 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
288 the difference expression cannot immediately be reduced. */
289 {
290 symbolS *stmp = make_expr_symbol (exp);
291
292 exp->X_op = O_symbol;
293 exp->X_op_symbol = 0;
294 exp->X_add_symbol = stmp;
295 exp->X_add_number = 0;
296
297 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
298 }
299
300 case O_symbol_rva:
301 add = exp->X_add_symbol;
302 off = exp->X_add_number;
303
304 #if defined(BFD_ASSEMBLER)
305 r_type = BFD_RELOC_RVA;
306 #else
307 #if defined(TC_RVA_RELOC)
308 r_type = TC_RVA_RELOC;
309 #else
310 as_fatal (_("rva not supported"));
311 #endif
312 #endif
313 break;
314
315 case O_uminus:
316 sub = exp->X_add_symbol;
317 off = exp->X_add_number;
318 break;
319
320 case O_subtract:
321 sub = exp->X_op_symbol;
322 /* Fall through. */
323 case O_symbol:
324 add = exp->X_add_symbol;
325 /* Fall through. */
326 case O_constant:
327 off = exp->X_add_number;
328 break;
329
330 default:
331 add = make_expr_symbol (exp);
332 break;
333 }
334
335 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
336 }
337
338 /* Append a string onto another string, bumping the pointer along. */
339 void
340 append (charPP, fromP, length)
341 char **charPP;
342 char *fromP;
343 unsigned long length;
344 {
345 /* Don't trust memcpy() of 0 chars. */
346 if (length == 0)
347 return;
348
349 memcpy (*charPP, fromP, length);
350 *charPP += length;
351 }
352
353 #ifndef BFD_ASSEMBLER
354 int section_alignment[SEG_MAXIMUM_ORDINAL];
355 #endif
356
357 /* This routine records the largest alignment seen for each segment.
358 If the beginning of the segment is aligned on the worst-case
359 boundary, all of the other alignments within it will work. At
360 least one object format really uses this info. */
361
362 void
363 record_alignment (seg, align)
364 /* Segment to which alignment pertains. */
365 segT seg;
366 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
367 boundary, etc.) */
368 int align;
369 {
370 if (seg == absolute_section)
371 return;
372 #ifdef BFD_ASSEMBLER
373 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
374 bfd_set_section_alignment (stdoutput, seg, align);
375 #else
376 if (align > section_alignment[(int) seg])
377 section_alignment[(int) seg] = align;
378 #endif
379 }
380
381 #ifdef BFD_ASSEMBLER
382
383 /* Reset the section indices after removing the gas created sections. */
384
385 static void
386 renumber_sections (abfd, sec, countparg)
387 bfd *abfd ATTRIBUTE_UNUSED;
388 asection *sec;
389 PTR countparg;
390 {
391 int *countp = (int *) countparg;
392
393 sec->index = *countp;
394 ++*countp;
395 }
396
397 #endif /* defined (BFD_ASSEMBLER) */
398
399 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
400
401 static fragS *
402 chain_frchains_together_1 (section, frchp)
403 segT section;
404 struct frchain *frchp;
405 {
406 fragS dummy, *prev_frag = &dummy;
407 #ifdef BFD_ASSEMBLER
408 fixS fix_dummy, *prev_fix = &fix_dummy;
409 #endif
410
411 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
412 {
413 prev_frag->fr_next = frchp->frch_root;
414 prev_frag = frchp->frch_last;
415 assert (prev_frag->fr_type != 0);
416 #ifdef BFD_ASSEMBLER
417 if (frchp->fix_root != (fixS *) NULL)
418 {
419 if (seg_info (section)->fix_root == (fixS *) NULL)
420 seg_info (section)->fix_root = frchp->fix_root;
421 prev_fix->fx_next = frchp->fix_root;
422 seg_info (section)->fix_tail = frchp->fix_tail;
423 prev_fix = frchp->fix_tail;
424 }
425 #endif
426 }
427 assert (prev_frag->fr_type != 0);
428 prev_frag->fr_next = 0;
429 return prev_frag;
430 }
431
432 #endif
433
434 #ifdef BFD_ASSEMBLER
435
436 static void
437 chain_frchains_together (abfd, section, xxx)
438 bfd *abfd ATTRIBUTE_UNUSED;
439 segT section;
440 PTR xxx ATTRIBUTE_UNUSED;
441 {
442 segment_info_type *info;
443
444 /* BFD may have introduced its own sections without using
445 subseg_new, so it is possible that seg_info is NULL. */
446 info = seg_info (section);
447 if (info != (segment_info_type *) NULL)
448 info->frchainP->frch_last
449 = chain_frchains_together_1 (section, info->frchainP);
450
451 /* Now that we've chained the frags together, we must add new fixups
452 to the segment, not to the frag chain. */
453 frags_chained = 1;
454 }
455
456 #endif
457
458 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
459
460 static void
461 remove_subsegs (head, seg, root, last)
462 frchainS *head;
463 int seg;
464 fragS **root;
465 fragS **last;
466 {
467 *root = head->frch_root;
468 *last = chain_frchains_together_1 (seg, head);
469 }
470
471 #endif /* BFD */
472
473 #if defined (BFD_ASSEMBLER) || !defined (BFD)
474
475 #ifdef BFD_ASSEMBLER
476 static void
477 cvt_frag_to_fill (sec, fragP)
478 segT sec ATTRIBUTE_UNUSED;
479 fragS *fragP;
480 #else
481 static void
482 cvt_frag_to_fill (headersP, sec, fragP)
483 object_headers *headersP;
484 segT sec;
485 fragS *fragP;
486 #endif
487 {
488 switch (fragP->fr_type)
489 {
490 case rs_align:
491 case rs_align_code:
492 case rs_org:
493 case rs_space:
494 #ifdef HANDLE_ALIGN
495 HANDLE_ALIGN (fragP);
496 #endif
497 know (fragP->fr_next != NULL);
498 fragP->fr_offset = (fragP->fr_next->fr_address
499 - fragP->fr_address
500 - fragP->fr_fix) / fragP->fr_var;
501 if (fragP->fr_offset < 0)
502 {
503 as_bad_where (fragP->fr_file, fragP->fr_line,
504 _("attempt to .org/.space backwards? (%ld)"),
505 (long) fragP->fr_offset);
506 }
507 fragP->fr_type = rs_fill;
508 break;
509
510 case rs_fill:
511 break;
512
513 case rs_leb128:
514 {
515 valueT value = S_GET_VALUE (fragP->fr_symbol);
516 int size;
517
518 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
519 fragP->fr_subtype);
520
521 fragP->fr_fix += size;
522 fragP->fr_type = rs_fill;
523 fragP->fr_var = 0;
524 fragP->fr_offset = 0;
525 fragP->fr_symbol = NULL;
526 }
527 break;
528
529 case rs_cfa:
530 eh_frame_convert_frag (fragP);
531 break;
532
533 case rs_machine_dependent:
534 #ifdef BFD_ASSEMBLER
535 md_convert_frag (stdoutput, sec, fragP);
536 #else
537 md_convert_frag (headersP, sec, fragP);
538 #endif
539
540 assert (fragP->fr_next == NULL
541 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
542 == fragP->fr_fix));
543
544 /* After md_convert_frag, we make the frag into a ".space 0".
545 md_convert_frag() should set up any fixSs and constants
546 required. */
547 frag_wane (fragP);
548 break;
549
550 #ifndef WORKING_DOT_WORD
551 case rs_broken_word:
552 {
553 struct broken_word *lie;
554
555 if (fragP->fr_subtype)
556 {
557 fragP->fr_fix += md_short_jump_size;
558 for (lie = (struct broken_word *) (fragP->fr_symbol);
559 lie && lie->dispfrag == fragP;
560 lie = lie->next_broken_word)
561 if (lie->added == 1)
562 fragP->fr_fix += md_long_jump_size;
563 }
564 frag_wane (fragP);
565 }
566 break;
567 #endif
568
569 default:
570 BAD_CASE (fragP->fr_type);
571 break;
572 }
573 }
574
575 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
576
577 #ifdef BFD_ASSEMBLER
578 static void
579 relax_and_size_seg (abfd, sec, xxx)
580 bfd *abfd;
581 asection *sec;
582 PTR xxx ATTRIBUTE_UNUSED;
583 {
584 flagword flags;
585 fragS *fragp;
586 segment_info_type *seginfo;
587 int x;
588 valueT size, newsize;
589
590 subseg_change (sec, 0);
591
592 flags = bfd_get_section_flags (abfd, sec);
593
594 seginfo = seg_info (sec);
595 if (seginfo && seginfo->frchainP)
596 {
597 relax_segment (seginfo->frchainP->frch_root, sec);
598 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
599 cvt_frag_to_fill (sec, fragp);
600 for (fragp = seginfo->frchainP->frch_root;
601 fragp->fr_next;
602 fragp = fragp->fr_next)
603 /* Walk to last elt. */
604 ;
605 size = fragp->fr_address + fragp->fr_fix;
606 }
607 else
608 size = 0;
609
610 if (size > 0 && ! seginfo->bss)
611 flags |= SEC_HAS_CONTENTS;
612
613 /* @@ This is just an approximation. */
614 if (seginfo && seginfo->fix_root)
615 flags |= SEC_RELOC;
616 else
617 flags &= ~SEC_RELOC;
618 x = bfd_set_section_flags (abfd, sec, flags);
619 assert (x == true);
620
621 newsize = md_section_align (sec, size);
622 x = bfd_set_section_size (abfd, sec, newsize);
623 assert (x == true);
624
625 /* If the size had to be rounded up, add some padding in the last
626 non-empty frag. */
627 assert (newsize >= size);
628 if (size != newsize)
629 {
630 fragS *last = seginfo->frchainP->frch_last;
631 fragp = seginfo->frchainP->frch_root;
632 while (fragp->fr_next != last)
633 fragp = fragp->fr_next;
634 last->fr_address = size;
635 fragp->fr_offset += newsize - size;
636 }
637
638 #ifdef tc_frob_section
639 tc_frob_section (sec);
640 #endif
641 #ifdef obj_frob_section
642 obj_frob_section (sec);
643 #endif
644 }
645
646 #ifdef DEBUG2
647 static void
648 dump_section_relocs (abfd, sec, stream_)
649 bfd *abfd ATTRIBUTE_UNUSED;
650 asection *sec;
651 char *stream_;
652 {
653 FILE *stream = (FILE *) stream_;
654 segment_info_type *seginfo = seg_info (sec);
655 fixS *fixp = seginfo->fix_root;
656
657 if (!fixp)
658 return;
659
660 fprintf (stream, "sec %s relocs:\n", sec->name);
661 while (fixp)
662 {
663 symbolS *s = fixp->fx_addsy;
664
665 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
666 (int) fixp->fx_r_type);
667 if (s == NULL)
668 fprintf (stream, "no sym\n");
669 else
670 {
671 print_symbol_value_1 (stream, s);
672 fprintf (stream, "\n");
673 }
674 fixp = fixp->fx_next;
675 }
676 }
677 #else
678 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
679 #endif
680
681 #ifndef EMIT_SECTION_SYMBOLS
682 #define EMIT_SECTION_SYMBOLS 1
683 #endif
684
685 static void
686 adjust_reloc_syms (abfd, sec, xxx)
687 bfd *abfd ATTRIBUTE_UNUSED;
688 asection *sec;
689 PTR xxx ATTRIBUTE_UNUSED;
690 {
691 segment_info_type *seginfo = seg_info (sec);
692 fixS *fixp;
693
694 if (seginfo == NULL)
695 return;
696
697 dump_section_relocs (abfd, sec, stderr);
698
699 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
700 if (fixp->fx_done)
701 /* Ignore it. */
702 ;
703 else if (fixp->fx_addsy)
704 {
705 symbolS *sym;
706 asection *symsec;
707
708 #ifdef DEBUG5
709 fprintf (stderr, "\n\nadjusting fixup:\n");
710 print_fixup (fixp);
711 #endif
712
713 sym = fixp->fx_addsy;
714
715 /* All symbols should have already been resolved at this
716 point. It is possible to see unresolved expression
717 symbols, though, since they are not in the regular symbol
718 table. */
719 if (sym != NULL)
720 resolve_symbol_value (sym, 1);
721
722 if (fixp->fx_subsy != NULL)
723 resolve_symbol_value (fixp->fx_subsy, 1);
724
725 /* If this symbol is equated to an undefined symbol, convert
726 the fixup to being against that symbol. */
727 if (sym != NULL && symbol_equated_p (sym)
728 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
729 {
730 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
731 sym = symbol_get_value_expression (sym)->X_add_symbol;
732 fixp->fx_addsy = sym;
733 }
734
735 if (sym != NULL && symbol_mri_common_p (sym))
736 {
737 /* These symbols are handled specially in fixup_segment. */
738 goto done;
739 }
740
741 symsec = S_GET_SEGMENT (sym);
742
743 if (symsec == NULL)
744 abort ();
745
746 if (bfd_is_abs_section (symsec))
747 {
748 /* The fixup_segment routine will not use this symbol in a
749 relocation unless TC_FORCE_RELOCATION returns 1. */
750 if (TC_FORCE_RELOCATION (fixp))
751 {
752 symbol_mark_used_in_reloc (fixp->fx_addsy);
753 #ifdef UNDEFINED_DIFFERENCE_OK
754 if (fixp->fx_subsy != NULL)
755 symbol_mark_used_in_reloc (fixp->fx_subsy);
756 #endif
757 }
758 goto done;
759 }
760
761 /* If it's one of these sections, assume the symbol is
762 definitely going to be output. The code in
763 md_estimate_size_before_relax in tc-mips.c uses this test
764 as well, so if you change this code you should look at that
765 code. */
766 if (bfd_is_und_section (symsec)
767 || bfd_is_com_section (symsec))
768 {
769 symbol_mark_used_in_reloc (fixp->fx_addsy);
770 #ifdef UNDEFINED_DIFFERENCE_OK
771 /* We have the difference of an undefined symbol and some
772 other symbol. Make sure to mark the other symbol as used
773 in a relocation so that it will always be output. */
774 if (fixp->fx_subsy)
775 symbol_mark_used_in_reloc (fixp->fx_subsy);
776 #endif
777 goto done;
778 }
779
780 /* Don't try to reduce relocs which refer to non-local symbols
781 in .linkonce sections. It can lead to confusion when a
782 debugging section refers to a .linkonce section. I hope
783 this will always be correct. */
784 if (symsec != sec && ! S_IS_LOCAL (sym))
785 {
786 boolean linkonce;
787
788 linkonce = false;
789 #ifdef BFD_ASSEMBLER
790 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
791 != 0)
792 linkonce = true;
793 #endif
794 #ifdef OBJ_ELF
795 /* The GNU toolchain uses an extension for ELF: a section
796 beginning with the magic string .gnu.linkonce is a
797 linkonce section. */
798 if (strncmp (segment_name (symsec), ".gnu.linkonce",
799 sizeof ".gnu.linkonce" - 1) == 0)
800 linkonce = true;
801 #endif
802
803 if (linkonce)
804 {
805 symbol_mark_used_in_reloc (fixp->fx_addsy);
806 #ifdef UNDEFINED_DIFFERENCE_OK
807 if (fixp->fx_subsy != NULL)
808 symbol_mark_used_in_reloc (fixp->fx_subsy);
809 #endif
810 goto done;
811 }
812 }
813
814 /* Since we're reducing to section symbols, don't attempt to reduce
815 anything that's already using one. */
816 if (symbol_section_p (sym))
817 {
818 symbol_mark_used_in_reloc (fixp->fx_addsy);
819 goto done;
820 }
821
822 #ifdef BFD_ASSEMBLER
823 /* We can never adjust a reloc against a weak symbol. If we
824 did, and the weak symbol was overridden by a real symbol
825 somewhere else, then our relocation would be pointing at
826 the wrong area of memory. */
827 if (S_IS_WEAK (sym))
828 {
829 symbol_mark_used_in_reloc (fixp->fx_addsy);
830 goto done;
831 }
832 #endif
833
834 /* Is there some other reason we can't adjust this one? (E.g.,
835 call/bal links in i960-bout symbols.) */
836 #ifdef obj_fix_adjustable
837 if (! obj_fix_adjustable (fixp))
838 {
839 symbol_mark_used_in_reloc (fixp->fx_addsy);
840 goto done;
841 }
842 #endif
843
844 /* Is there some other (target cpu dependent) reason we can't adjust
845 this one? (E.g. relocations involving function addresses on
846 the PA. */
847 #ifdef tc_fix_adjustable
848 if (! tc_fix_adjustable (fixp))
849 {
850 symbol_mark_used_in_reloc (fixp->fx_addsy);
851 goto done;
852 }
853 #endif
854
855 /* If the section symbol isn't going to be output, the relocs
856 at least should still work. If not, figure out what to do
857 when we run into that case.
858
859 We refetch the segment when calling section_symbol, rather
860 than using symsec, because S_GET_VALUE may wind up changing
861 the section when it calls resolve_symbol_value. */
862 fixp->fx_offset += S_GET_VALUE (sym);
863 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
864 symbol_mark_used_in_reloc (fixp->fx_addsy);
865 #ifdef DEBUG5
866 fprintf (stderr, "\nadjusted fixup:\n");
867 print_fixup (fixp);
868 #endif
869
870 done:
871 ;
872 }
873 #if 1 /* def RELOC_REQUIRES_SYMBOL */
874 else
875 {
876 /* There was no symbol required by this relocation. However,
877 BFD doesn't really handle relocations without symbols well.
878 (At least, the COFF support doesn't.) So for now we fake up
879 a local symbol in the absolute section. */
880
881 fixp->fx_addsy = section_symbol (absolute_section);
882 #if 0
883 fixp->fx_addsy->sy_used_in_reloc = 1;
884 #endif
885 }
886 #endif
887
888 dump_section_relocs (abfd, sec, stderr);
889 }
890
891 static void
892 write_relocs (abfd, sec, xxx)
893 bfd *abfd;
894 asection *sec;
895 PTR xxx ATTRIBUTE_UNUSED;
896 {
897 segment_info_type *seginfo = seg_info (sec);
898 int i;
899 unsigned int n;
900 arelent **relocs;
901 fixS *fixp;
902 char *err;
903
904 /* If seginfo is NULL, we did not create this section; don't do
905 anything with it. */
906 if (seginfo == NULL)
907 return;
908
909 fixup_segment (seginfo->fix_root, sec);
910
911 n = 0;
912 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
913 n++;
914
915 #ifndef RELOC_EXPANSION_POSSIBLE
916 /* Set up reloc information as well. */
917 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
918 memset ((char *) relocs, 0, n * sizeof (arelent *));
919
920 i = 0;
921 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
922 {
923 arelent *reloc;
924 bfd_reloc_status_type s;
925 symbolS *sym;
926
927 if (fixp->fx_done)
928 {
929 n--;
930 continue;
931 }
932
933 /* If this is an undefined symbol which was equated to another
934 symbol, then use generate the reloc against the latter symbol
935 rather than the former. */
936 sym = fixp->fx_addsy;
937 while (symbol_equated_p (sym)
938 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
939 {
940 symbolS *n;
941
942 /* We must avoid looping, as that can occur with a badly
943 written program. */
944 n = symbol_get_value_expression (sym)->X_add_symbol;
945 if (n == sym)
946 break;
947 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
948 sym = n;
949 }
950 fixp->fx_addsy = sym;
951
952 reloc = tc_gen_reloc (sec, fixp);
953 if (!reloc)
954 {
955 n--;
956 continue;
957 }
958
959 #if 0
960 /* This test is triggered inappropriately for the SH. */
961 if (fixp->fx_where + fixp->fx_size
962 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
963 abort ();
964 #endif
965
966 s = bfd_install_relocation (stdoutput, reloc,
967 fixp->fx_frag->fr_literal,
968 fixp->fx_frag->fr_address,
969 sec, &err);
970 switch (s)
971 {
972 case bfd_reloc_ok:
973 break;
974 case bfd_reloc_overflow:
975 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
976 break;
977 case bfd_reloc_outofrange:
978 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
979 break;
980 default:
981 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
982 fixp->fx_file, fixp->fx_line, s);
983 }
984 relocs[i++] = reloc;
985 }
986 #else
987 n = n * MAX_RELOC_EXPANSION;
988 /* Set up reloc information as well. */
989 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
990
991 i = 0;
992 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
993 {
994 arelent **reloc;
995 char *data;
996 bfd_reloc_status_type s;
997 symbolS *sym;
998 int j;
999
1000 if (fixp->fx_done)
1001 {
1002 n--;
1003 continue;
1004 }
1005
1006 /* If this is an undefined symbol which was equated to another
1007 symbol, then use generate the reloc against the latter symbol
1008 rather than the former. */
1009 sym = fixp->fx_addsy;
1010 while (symbol_equated_p (sym)
1011 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
1012 sym = symbol_get_value_expression (sym)->X_add_symbol;
1013 fixp->fx_addsy = sym;
1014
1015 reloc = tc_gen_reloc (sec, fixp);
1016
1017 for (j = 0; reloc[j]; j++)
1018 {
1019 relocs[i++] = reloc[j];
1020 assert (i <= n);
1021 }
1022 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1023 if (fixp->fx_where + fixp->fx_size
1024 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1025 as_bad_where (fixp->fx_file, fixp->fx_line,
1026 _("internal error: fixup not contained within frag"));
1027 for (j = 0; reloc[j]; j++)
1028 {
1029 s = bfd_install_relocation (stdoutput, reloc[j],
1030 fixp->fx_frag->fr_literal,
1031 fixp->fx_frag->fr_address,
1032 sec, &err);
1033 switch (s)
1034 {
1035 case bfd_reloc_ok:
1036 break;
1037 case bfd_reloc_overflow:
1038 as_bad_where (fixp->fx_file, fixp->fx_line,
1039 _("relocation overflow"));
1040 break;
1041 default:
1042 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1043 fixp->fx_file, fixp->fx_line);
1044 }
1045 }
1046 }
1047 n = i;
1048 #endif
1049
1050 #ifdef DEBUG4
1051 {
1052 int i, j, nsyms;
1053 asymbol **sympp;
1054 sympp = bfd_get_outsymbols (stdoutput);
1055 nsyms = bfd_get_symcount (stdoutput);
1056 for (i = 0; i < n; i++)
1057 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1058 {
1059 for (j = 0; j < nsyms; j++)
1060 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1061 break;
1062 if (j == nsyms)
1063 abort ();
1064 }
1065 }
1066 #endif
1067
1068 if (n)
1069 bfd_set_reloc (stdoutput, sec, relocs, n);
1070 else
1071 bfd_set_section_flags (abfd, sec,
1072 (bfd_get_section_flags (abfd, sec)
1073 & (flagword) ~SEC_RELOC));
1074
1075 #ifdef SET_SECTION_RELOCS
1076 SET_SECTION_RELOCS (sec, relocs, n);
1077 #endif
1078
1079 #ifdef DEBUG3
1080 {
1081 int i;
1082 arelent *r;
1083 asymbol *s;
1084 fprintf (stderr, "relocs for sec %s\n", sec->name);
1085 for (i = 0; i < n; i++)
1086 {
1087 r = relocs[i];
1088 s = *r->sym_ptr_ptr;
1089 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1090 i, r, r->address, s->name, r->addend);
1091 }
1092 }
1093 #endif
1094 }
1095
1096 static void
1097 write_contents (abfd, sec, xxx)
1098 bfd *abfd ATTRIBUTE_UNUSED;
1099 asection *sec;
1100 PTR xxx ATTRIBUTE_UNUSED;
1101 {
1102 segment_info_type *seginfo = seg_info (sec);
1103 unsigned long offset = 0;
1104 fragS *f;
1105
1106 /* Write out the frags. */
1107 if (seginfo == NULL
1108 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1109 return;
1110
1111 for (f = seginfo->frchainP->frch_root;
1112 f;
1113 f = f->fr_next)
1114 {
1115 int x;
1116 unsigned long fill_size;
1117 char *fill_literal;
1118 long count;
1119
1120 assert (f->fr_type == rs_fill);
1121 if (f->fr_fix)
1122 {
1123 x = bfd_set_section_contents (stdoutput, sec,
1124 f->fr_literal, (file_ptr) offset,
1125 (bfd_size_type) f->fr_fix);
1126 if (x == false)
1127 {
1128 bfd_perror (stdoutput->filename);
1129 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1130 exit (EXIT_FAILURE);
1131 }
1132 offset += f->fr_fix;
1133 }
1134 fill_literal = f->fr_literal + f->fr_fix;
1135 fill_size = f->fr_var;
1136 count = f->fr_offset;
1137 assert (count >= 0);
1138 if (fill_size && count)
1139 {
1140 char buf[256];
1141 if (fill_size > sizeof (buf))
1142 {
1143 /* Do it the old way. Can this ever happen? */
1144 while (count--)
1145 {
1146 x = bfd_set_section_contents (stdoutput, sec,
1147 fill_literal,
1148 (file_ptr) offset,
1149 (bfd_size_type) fill_size);
1150 if (x == false)
1151 {
1152 bfd_perror (stdoutput->filename);
1153 as_perror (_("FATAL: Can't write %s"),
1154 stdoutput->filename);
1155 exit (EXIT_FAILURE);
1156 }
1157 offset += fill_size;
1158 }
1159 }
1160 else
1161 {
1162 /* Build a buffer full of fill objects and output it as
1163 often as necessary. This saves on the overhead of
1164 potentially lots of bfd_set_section_contents calls. */
1165 int n_per_buf, i;
1166 if (fill_size == 1)
1167 {
1168 n_per_buf = sizeof (buf);
1169 memset (buf, *fill_literal, n_per_buf);
1170 }
1171 else
1172 {
1173 char *bufp;
1174 n_per_buf = sizeof (buf) / fill_size;
1175 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1176 memcpy (bufp, fill_literal, fill_size);
1177 }
1178 for (; count > 0; count -= n_per_buf)
1179 {
1180 n_per_buf = n_per_buf > count ? count : n_per_buf;
1181 x = bfd_set_section_contents
1182 (stdoutput, sec, buf, (file_ptr) offset,
1183 (bfd_size_type) n_per_buf * fill_size);
1184 if (x != true)
1185 as_fatal (_("Cannot write to output file."));
1186 offset += n_per_buf * fill_size;
1187 }
1188 }
1189 }
1190 }
1191 }
1192 #endif
1193
1194 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1195 static void
1196 merge_data_into_text ()
1197 {
1198 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1199 seg_info (text_section)->frchainP->frch_last->fr_next =
1200 seg_info (data_section)->frchainP->frch_root;
1201 seg_info (text_section)->frchainP->frch_last =
1202 seg_info (data_section)->frchainP->frch_last;
1203 seg_info (data_section)->frchainP = 0;
1204 #else
1205 fixS *tmp;
1206
1207 text_last_frag->fr_next = data_frag_root;
1208 text_last_frag = data_last_frag;
1209 data_last_frag = NULL;
1210 data_frag_root = NULL;
1211 if (text_fix_root)
1212 {
1213 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1214 tmp->fx_next = data_fix_root;
1215 text_fix_tail = data_fix_tail;
1216 }
1217 else
1218 text_fix_root = data_fix_root;
1219 data_fix_root = NULL;
1220 #endif
1221 }
1222 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1223
1224 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1225 static void
1226 relax_and_size_all_segments ()
1227 {
1228 fragS *fragP;
1229
1230 relax_segment (text_frag_root, SEG_TEXT);
1231 relax_segment (data_frag_root, SEG_DATA);
1232 relax_segment (bss_frag_root, SEG_BSS);
1233
1234 /* Now the addresses of frags are correct within the segment. */
1235 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1236 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1237 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1238
1239 /* Join the 2 segments into 1 huge segment.
1240 To do this, re-compute every rn_address in the SEG_DATA frags.
1241 Then join the data frags after the text frags.
1242
1243 Determine a_data [length of data segment]. */
1244 if (data_frag_root)
1245 {
1246 register relax_addressT slide;
1247
1248 know ((text_last_frag->fr_type == rs_fill)
1249 && (text_last_frag->fr_offset == 0));
1250
1251 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1252 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1253 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1254 #ifdef OBJ_BOUT
1255 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1256 /* For b.out: If the data section has a strict alignment
1257 requirement, its load address in the .o file will be
1258 rounded up from the size of the text section. These
1259 two values are *not* the same! Similarly for the bss
1260 section.... */
1261 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1262 #endif
1263
1264 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1265 fragP->fr_address += slide;
1266
1267 know (text_last_frag != 0);
1268 text_last_frag->fr_next = data_frag_root;
1269 }
1270 else
1271 {
1272 H_SET_DATA_SIZE (&headers, 0);
1273 }
1274
1275 #ifdef OBJ_BOUT
1276 /* See above comments on b.out data section address. */
1277 {
1278 long bss_vma;
1279 if (data_last_frag == 0)
1280 bss_vma = H_GET_TEXT_SIZE (&headers);
1281 else
1282 bss_vma = data_last_frag->fr_address;
1283 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1284 bss_address_frag.fr_address = bss_vma;
1285 }
1286 #else /* ! OBJ_BOUT */
1287 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1288 H_GET_DATA_SIZE (&headers));
1289
1290 #endif /* ! OBJ_BOUT */
1291
1292 /* Slide all the frags. */
1293 if (bss_frag_root)
1294 {
1295 relax_addressT slide = bss_address_frag.fr_address;
1296
1297 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1298 fragP->fr_address += slide;
1299 }
1300
1301 if (bss_last_frag)
1302 H_SET_BSS_SIZE (&headers,
1303 bss_last_frag->fr_address - bss_frag_root->fr_address);
1304 else
1305 H_SET_BSS_SIZE (&headers, 0);
1306 }
1307 #endif /* ! BFD_ASSEMBLER && ! BFD */
1308
1309 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1310
1311 #ifdef BFD_ASSEMBLER
1312 static void
1313 set_symtab ()
1314 {
1315 int nsyms;
1316 asymbol **asympp;
1317 symbolS *symp;
1318 boolean result;
1319 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1320
1321 /* Count symbols. We can't rely on a count made by the loop in
1322 write_object_file, because *_frob_file may add a new symbol or
1323 two. */
1324 nsyms = 0;
1325 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1326 nsyms++;
1327
1328 if (nsyms)
1329 {
1330 int i;
1331
1332 asympp = (asymbol **) bfd_alloc (stdoutput,
1333 nsyms * sizeof (asymbol *));
1334 symp = symbol_rootP;
1335 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1336 {
1337 asympp[i] = symbol_get_bfdsym (symp);
1338 symbol_mark_written (symp);
1339 }
1340 }
1341 else
1342 asympp = 0;
1343 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1344 assert (result == true);
1345 symbol_table_frozen = 1;
1346 }
1347 #endif
1348
1349 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1350 static void
1351 set_segment_vma (abfd, sec, xxx)
1352 bfd *abfd;
1353 asection *sec;
1354 PTR xxx ATTRIBUTE_UNUSED;
1355 {
1356 static bfd_vma addr = 0;
1357
1358 bfd_set_section_vma (abfd, sec, addr);
1359 addr += bfd_section_size (abfd, sec);
1360 }
1361 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1362
1363 /* Finish the subsegments. After every sub-segment, we fake an
1364 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1365 ".fill 0" because that is the kind of frag that requires least
1366 thought. ".align" frags like to have a following frag since that
1367 makes calculating their intended length trivial. */
1368
1369 #ifndef SUB_SEGMENT_ALIGN
1370 #ifdef BFD_ASSEMBLER
1371 #define SUB_SEGMENT_ALIGN(SEG) (0)
1372 #else
1373 #define SUB_SEGMENT_ALIGN(SEG) (2)
1374 #endif
1375 #endif
1376
1377 void
1378 subsegs_finish ()
1379 {
1380 struct frchain *frchainP;
1381
1382 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1383 {
1384 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1385
1386 /* This now gets called even if we had errors. In that case,
1387 any alignment is meaningless, and, moreover, will look weird
1388 if we are generating a listing. */
1389 frag_align (had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg),
1390 subseg_text_p (now_seg) ? NOP_OPCODE : 0,
1391 0);
1392
1393 /* frag_align will have left a new frag.
1394 Use this last frag for an empty ".fill".
1395
1396 For this segment ...
1397 Create a last frag. Do not leave a "being filled in frag". */
1398 frag_wane (frag_now);
1399 frag_now->fr_fix = 0;
1400 know (frag_now->fr_next == NULL);
1401 }
1402 }
1403
1404 /* Write the object file. */
1405
1406 void
1407 write_object_file ()
1408 {
1409 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1410 fragS *fragP; /* Track along all frags. */
1411 #endif
1412
1413 /* Do we really want to write it? */
1414 {
1415 int n_warns, n_errs;
1416 n_warns = had_warnings ();
1417 n_errs = had_errors ();
1418 /* The -Z flag indicates that an object file should be generated,
1419 regardless of warnings and errors. */
1420 if (flag_always_generate_output)
1421 {
1422 if (n_warns || n_errs)
1423 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1424 n_errs, n_errs == 1 ? "" : "s",
1425 n_warns, n_warns == 1 ? "" : "s");
1426 }
1427 else
1428 {
1429 if (n_errs)
1430 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1431 n_errs, n_errs == 1 ? "" : "s",
1432 n_warns, n_warns == 1 ? "" : "s");
1433 }
1434 }
1435
1436 #ifdef OBJ_VMS
1437 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1438 a routine to check for the definition of the procedure "_main",
1439 and if so -- fix it up so that it can be program entry point. */
1440 vms_check_for_main ();
1441 #endif /* OBJ_VMS */
1442
1443 /* From now on, we don't care about sub-segments. Build one frag chain
1444 for each segment. Linked thru fr_next. */
1445
1446 #ifdef BFD_ASSEMBLER
1447 /* Remove the sections created by gas for its own purposes. */
1448 {
1449 asection **seclist, *sec;
1450 int i;
1451
1452 seclist = &stdoutput->sections;
1453 while (seclist && *seclist)
1454 {
1455 sec = *seclist;
1456 while (sec == reg_section || sec == expr_section)
1457 {
1458 sec = sec->next;
1459 *seclist = sec;
1460 stdoutput->section_count--;
1461 if (!sec)
1462 break;
1463 }
1464 if (*seclist)
1465 seclist = &(*seclist)->next;
1466 }
1467 i = 0;
1468 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1469 }
1470
1471 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1472 #else
1473 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1474 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1475 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1476 #endif
1477
1478 /* We have two segments. If user gave -R flag, then we must put the
1479 data frags into the text segment. Do this before relaxing so
1480 we know to take advantage of -R and make shorter addresses. */
1481 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1482 if (flag_readonly_data_in_text)
1483 {
1484 merge_data_into_text ();
1485 }
1486 #endif
1487
1488 #ifdef BFD_ASSEMBLER
1489 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1490 #else
1491 relax_and_size_all_segments ();
1492 #endif /* BFD_ASSEMBLER */
1493
1494 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1495 /* Now that the segments have their final sizes, run through the
1496 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1497 should too. Currently, only DJGPP uses this code, but other
1498 COFF targets may need to execute this too. */
1499 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1500 #endif
1501
1502 #ifndef BFD_ASSEMBLER
1503 /* Crawl the symbol chain.
1504
1505 For each symbol whose value depends on a frag, take the address of
1506 that frag and subsume it into the value of the symbol.
1507 After this, there is just one way to lookup a symbol value.
1508 Values are left in their final state for object file emission.
1509 We adjust the values of 'L' local symbols, even if we do
1510 not intend to emit them to the object file, because their values
1511 are needed for fix-ups.
1512
1513 Unless we saw a -L flag, remove all symbols that begin with 'L'
1514 from the symbol chain. (They are still pointed to by the fixes.)
1515
1516 Count the remaining symbols.
1517 Assign a symbol number to each symbol.
1518 Count the number of string-table chars we will emit.
1519 Put this info into the headers as appropriate. */
1520 know (zero_address_frag.fr_address == 0);
1521 string_byte_count = sizeof (string_byte_count);
1522
1523 obj_crawl_symbol_chain (&headers);
1524
1525 if (string_byte_count == sizeof (string_byte_count))
1526 string_byte_count = 0;
1527
1528 H_SET_STRING_SIZE (&headers, string_byte_count);
1529
1530 /* Addresses of frags now reflect addresses we use in the object file.
1531 Symbol values are correct.
1532 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1533 Also converting any machine-dependent frags using md_convert_frag(); */
1534 subseg_change (SEG_TEXT, 0);
1535
1536 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1537 {
1538 /* At this point we have linked all the frags into a single
1539 chain. However, cvt_frag_to_fill may call md_convert_frag
1540 which may call fix_new. We need to ensure that fix_new adds
1541 the fixup to the right section. */
1542 if (fragP == data_frag_root)
1543 subseg_change (SEG_DATA, 0);
1544
1545 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1546
1547 /* Some assert macros don't work with # directives mixed in. */
1548 #ifndef NDEBUG
1549 if (!(fragP->fr_next == NULL
1550 #ifdef OBJ_BOUT
1551 || fragP->fr_next == data_frag_root
1552 #endif
1553 || ((fragP->fr_next->fr_address - fragP->fr_address)
1554 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1555 abort ();
1556 #endif
1557 }
1558 #endif /* ! BFD_ASSEMBLER */
1559
1560 #ifndef WORKING_DOT_WORD
1561 {
1562 struct broken_word *lie;
1563 struct broken_word **prevP;
1564
1565 prevP = &broken_words;
1566 for (lie = broken_words; lie; lie = lie->next_broken_word)
1567 if (!lie->added)
1568 {
1569 expressionS exp;
1570
1571 subseg_change (lie->seg, lie->subseg);
1572 exp.X_op = O_subtract;
1573 exp.X_add_symbol = lie->add;
1574 exp.X_op_symbol = lie->sub;
1575 exp.X_add_number = lie->addnum;
1576 #ifdef BFD_ASSEMBLER
1577 #ifdef TC_CONS_FIX_NEW
1578 TC_CONS_FIX_NEW (lie->frag,
1579 lie->word_goes_here - lie->frag->fr_literal,
1580 2, &exp);
1581 #else
1582 fix_new_exp (lie->frag,
1583 lie->word_goes_here - lie->frag->fr_literal,
1584 2, &exp, 0, BFD_RELOC_16);
1585 #endif
1586 #else
1587 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1588 fix_new_exp (lie->frag,
1589 lie->word_goes_here - lie->frag->fr_literal,
1590 2, &exp, 0, NO_RELOC);
1591 #else
1592 #ifdef TC_NS32K
1593 fix_new_ns32k_exp (lie->frag,
1594 lie->word_goes_here - lie->frag->fr_literal,
1595 2, &exp, 0, 0, 2, 0, 0);
1596 #else
1597 fix_new_exp (lie->frag,
1598 lie->word_goes_here - lie->frag->fr_literal,
1599 2, &exp, 0, 0);
1600 #endif /* TC_NS32K */
1601 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1602 #endif /* BFD_ASSEMBLER */
1603 *prevP = lie->next_broken_word;
1604 }
1605 else
1606 prevP = &(lie->next_broken_word);
1607
1608 for (lie = broken_words; lie;)
1609 {
1610 struct broken_word *untruth;
1611 char *table_ptr;
1612 addressT table_addr;
1613 addressT from_addr, to_addr;
1614 int n, m;
1615
1616 subseg_change (lie->seg, lie->subseg);
1617 fragP = lie->dispfrag;
1618
1619 /* Find out how many broken_words go here. */
1620 n = 0;
1621 for (untruth = lie;
1622 untruth && untruth->dispfrag == fragP;
1623 untruth = untruth->next_broken_word)
1624 if (untruth->added == 1)
1625 n++;
1626
1627 table_ptr = lie->dispfrag->fr_opcode;
1628 table_addr = (lie->dispfrag->fr_address
1629 + (table_ptr - lie->dispfrag->fr_literal));
1630 /* Create the jump around the long jumps. This is a short
1631 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1632 from_addr = table_addr;
1633 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1634 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1635 lie->add);
1636 table_ptr += md_short_jump_size;
1637 table_addr += md_short_jump_size;
1638
1639 for (m = 0;
1640 lie && lie->dispfrag == fragP;
1641 m++, lie = lie->next_broken_word)
1642 {
1643 if (lie->added == 2)
1644 continue;
1645 /* Patch the jump table. */
1646 /* This is the offset from ??? to table_ptr+0. */
1647 to_addr = table_addr - S_GET_VALUE (lie->sub);
1648 #ifdef BFD_ASSEMBLER
1649 to_addr -= symbol_get_frag (lie->sub)->fr_address;
1650 #endif
1651 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1652 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1653 #endif
1654 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1655 for (untruth = lie->next_broken_word;
1656 untruth && untruth->dispfrag == fragP;
1657 untruth = untruth->next_broken_word)
1658 {
1659 if (untruth->use_jump == lie)
1660 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1661 }
1662
1663 /* Install the long jump. */
1664 /* This is a long jump from table_ptr+0 to the final target. */
1665 from_addr = table_addr;
1666 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1667 #ifdef BFD_ASSEMBLER
1668 to_addr += symbol_get_frag (lie->add)->fr_address;
1669 #endif
1670 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1671 lie->add);
1672 table_ptr += md_long_jump_size;
1673 table_addr += md_long_jump_size;
1674 }
1675 }
1676 }
1677 #endif /* not WORKING_DOT_WORD */
1678
1679 #ifndef BFD_ASSEMBLER
1680 #ifndef OBJ_VMS
1681 { /* not vms */
1682 char *the_object_file;
1683 long object_file_size;
1684 /* Scan every FixS performing fixups. We had to wait until now to
1685 do this because md_convert_frag() may have made some fixSs. */
1686 int trsize, drsize;
1687
1688 subseg_change (SEG_TEXT, 0);
1689 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1690 subseg_change (SEG_DATA, 0);
1691 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1692 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1693
1694 /* FIXME: Move this stuff into the pre-write-hook. */
1695 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1696 H_SET_ENTRY_POINT (&headers, 0);
1697
1698 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1699
1700 object_file_size = H_GET_FILE_SIZE (&headers);
1701 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1702
1703 output_file_create (out_file_name);
1704
1705 obj_header_append (&next_object_file_charP, &headers);
1706
1707 know ((next_object_file_charP - the_object_file)
1708 == H_GET_HEADER_SIZE (&headers));
1709
1710 /* Emit code. */
1711 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1712 {
1713 register long count;
1714 register char *fill_literal;
1715 register long fill_size;
1716
1717 PROGRESS (1);
1718 know (fragP->fr_type == rs_fill);
1719 append (&next_object_file_charP, fragP->fr_literal,
1720 (unsigned long) fragP->fr_fix);
1721 fill_literal = fragP->fr_literal + fragP->fr_fix;
1722 fill_size = fragP->fr_var;
1723 know (fragP->fr_offset >= 0);
1724
1725 for (count = fragP->fr_offset; count; count--)
1726 append (&next_object_file_charP, fill_literal,
1727 (unsigned long) fill_size);
1728 }
1729
1730 know ((next_object_file_charP - the_object_file)
1731 == (H_GET_HEADER_SIZE (&headers)
1732 + H_GET_TEXT_SIZE (&headers)
1733 + H_GET_DATA_SIZE (&headers)));
1734
1735 /* Emit relocations. */
1736 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1737 (relax_addressT) 0);
1738 know ((next_object_file_charP - the_object_file)
1739 == (H_GET_HEADER_SIZE (&headers)
1740 + H_GET_TEXT_SIZE (&headers)
1741 + H_GET_DATA_SIZE (&headers)
1742 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1743 #ifdef TC_I960
1744 /* Make addresses in data relocation directives relative to beginning of
1745 first data fragment, not end of last text fragment: alignment of the
1746 start of the data segment may place a gap between the segments. */
1747 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1748 data0_frchainP->frch_root->fr_address);
1749 #else /* TC_I960 */
1750 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1751 text_last_frag->fr_address);
1752 #endif /* TC_I960 */
1753
1754 know ((next_object_file_charP - the_object_file)
1755 == (H_GET_HEADER_SIZE (&headers)
1756 + H_GET_TEXT_SIZE (&headers)
1757 + H_GET_DATA_SIZE (&headers)
1758 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1759 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1760
1761 /* Emit line number entries. */
1762 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1763 know ((next_object_file_charP - the_object_file)
1764 == (H_GET_HEADER_SIZE (&headers)
1765 + H_GET_TEXT_SIZE (&headers)
1766 + H_GET_DATA_SIZE (&headers)
1767 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1768 + H_GET_DATA_RELOCATION_SIZE (&headers)
1769 + H_GET_LINENO_SIZE (&headers)));
1770
1771 /* Emit symbols. */
1772 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1773 know ((next_object_file_charP - the_object_file)
1774 == (H_GET_HEADER_SIZE (&headers)
1775 + H_GET_TEXT_SIZE (&headers)
1776 + H_GET_DATA_SIZE (&headers)
1777 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1778 + H_GET_DATA_RELOCATION_SIZE (&headers)
1779 + H_GET_LINENO_SIZE (&headers)
1780 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1781
1782 /* Emit strings. */
1783 if (string_byte_count > 0)
1784 obj_emit_strings (&next_object_file_charP);
1785
1786 #ifdef BFD_HEADERS
1787 bfd_seek (stdoutput, 0, 0);
1788 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1789 #else
1790
1791 /* Write the data to the file. */
1792 output_file_append (the_object_file, object_file_size, out_file_name);
1793 free (the_object_file);
1794 #endif
1795 }
1796 #else /* OBJ_VMS */
1797 /* Now do the VMS-dependent part of writing the object file. */
1798 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1799 H_GET_DATA_SIZE (&headers),
1800 H_GET_BSS_SIZE (&headers),
1801 text_frag_root, data_frag_root);
1802 #endif /* OBJ_VMS */
1803 #else /* BFD_ASSEMBLER */
1804
1805 /* Resolve symbol values. This needs to be done before processing
1806 the relocations. */
1807 if (symbol_rootP)
1808 {
1809 symbolS *symp;
1810
1811 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1812 resolve_symbol_value (symp, 1);
1813 }
1814 resolve_local_symbol_values ();
1815
1816 PROGRESS (1);
1817
1818 #ifdef tc_frob_file_before_adjust
1819 tc_frob_file_before_adjust ();
1820 #endif
1821 #ifdef obj_frob_file_before_adjust
1822 obj_frob_file_before_adjust ();
1823 #endif
1824
1825 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1826
1827 /* Set up symbol table, and write it out. */
1828 if (symbol_rootP)
1829 {
1830 symbolS *symp;
1831
1832 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1833 {
1834 int punt = 0;
1835 const char *name;
1836
1837 if (symbol_mri_common_p (symp))
1838 {
1839 if (S_IS_EXTERNAL (symp))
1840 as_bad (_("%s: global symbols not supported in common sections"),
1841 S_GET_NAME (symp));
1842 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1843 continue;
1844 }
1845
1846 name = S_GET_NAME (symp);
1847 if (name)
1848 {
1849 const char *name2 =
1850 decode_local_label_name ((char *) S_GET_NAME (symp));
1851 /* They only differ if `name' is a fb or dollar local
1852 label name. */
1853 if (name2 != name && ! S_IS_DEFINED (symp))
1854 as_bad (_("local label %s is not defined"), name2);
1855 }
1856
1857 /* Do it again, because adjust_reloc_syms might introduce
1858 more symbols. They'll probably only be section symbols,
1859 but they'll still need to have the values computed. */
1860 resolve_symbol_value (symp, 1);
1861
1862 /* Skip symbols which were equated to undefined or common
1863 symbols. */
1864 if (symbol_equated_p (symp)
1865 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1866 {
1867 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1868 continue;
1869 }
1870
1871 /* So far, common symbols have been treated like undefined symbols.
1872 Put them in the common section now. */
1873 if (S_IS_DEFINED (symp) == 0
1874 && S_GET_VALUE (symp) != 0)
1875 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1876 #if 0
1877 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1878 S_GET_NAME (symp), symp,
1879 S_GET_VALUE (symp),
1880 symbol_get_bfdsym (symp)->flags,
1881 segment_name (S_GET_SEGMENT (symp)));
1882 #endif
1883
1884 #ifdef obj_frob_symbol
1885 obj_frob_symbol (symp, punt);
1886 #endif
1887 #ifdef tc_frob_symbol
1888 if (! punt || symbol_used_in_reloc_p (symp))
1889 tc_frob_symbol (symp, punt);
1890 #endif
1891
1892 /* If we don't want to keep this symbol, splice it out of
1893 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1894 want section symbols. Otherwise, we skip local symbols
1895 and symbols that the frob_symbol macros told us to punt,
1896 but we keep such symbols if they are used in relocs. */
1897 if ((! EMIT_SECTION_SYMBOLS
1898 && symbol_section_p (symp))
1899 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1900 opposites. Sometimes the former checks flags and the
1901 latter examines the name... */
1902 || (!S_IS_EXTERN (symp)
1903 && (S_IS_LOCAL (symp) || punt)
1904 && ! symbol_used_in_reloc_p (symp)))
1905 {
1906 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1907
1908 /* After symbol_remove, symbol_next(symp) still returns
1909 the one that came after it in the chain. So we don't
1910 need to do any extra cleanup work here. */
1911 continue;
1912 }
1913
1914 /* Make sure we really got a value for the symbol. */
1915 if (! symbol_resolved_p (symp))
1916 {
1917 as_bad (_("can't resolve value for symbol \"%s\""),
1918 S_GET_NAME (symp));
1919 symbol_mark_resolved (symp);
1920 }
1921
1922 /* Set the value into the BFD symbol. Up til now the value
1923 has only been kept in the gas symbolS struct. */
1924 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1925 }
1926 }
1927
1928 PROGRESS (1);
1929
1930 /* Now do any format-specific adjustments to the symbol table, such
1931 as adding file symbols. */
1932 #ifdef tc_adjust_symtab
1933 tc_adjust_symtab ();
1934 #endif
1935 #ifdef obj_adjust_symtab
1936 obj_adjust_symtab ();
1937 #endif
1938
1939 /* Now that all the sizes are known, and contents correct, we can
1940 start writing to the file. */
1941 set_symtab ();
1942
1943 /* If *_frob_file changes the symbol value at this point, it is
1944 responsible for moving the changed value into symp->bsym->value
1945 as well. Hopefully all symbol value changing can be done in
1946 *_frob_symbol. */
1947 #ifdef tc_frob_file
1948 tc_frob_file ();
1949 #endif
1950 #ifdef obj_frob_file
1951 obj_frob_file ();
1952 #endif
1953
1954 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1955
1956 #ifdef tc_frob_file_after_relocs
1957 tc_frob_file_after_relocs ();
1958 #endif
1959 #ifdef obj_frob_file_after_relocs
1960 obj_frob_file_after_relocs ();
1961 #endif
1962
1963 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1964 #endif /* BFD_ASSEMBLER */
1965 }
1966 #endif /* ! BFD */
1967
1968 #ifdef TC_GENERIC_RELAX_TABLE
1969
1970 static int is_dnrange PARAMS ((fragS *, fragS *));
1971
1972 /* Subroutines of relax_segment. */
1973 static int
1974 is_dnrange (f1, f2)
1975 fragS *f1;
1976 fragS *f2;
1977 {
1978 for (; f1; f1 = f1->fr_next)
1979 if (f1->fr_next == f2)
1980 return 1;
1981 return 0;
1982 }
1983
1984 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1985
1986 long
1987 relax_frag (fragP, stretch)
1988 fragS *fragP;
1989 long stretch;
1990 {
1991 const relax_typeS *this_type;
1992 const relax_typeS *start_type;
1993 relax_substateT next_state;
1994 relax_substateT this_state;
1995 long aim, target, growth;
1996 symbolS *symbolP = fragP->fr_symbol;
1997 long offset = fragP->fr_offset;
1998 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
1999 unsigned long was_address = fragP->fr_address - stretch;
2000 unsigned long address = fragP->fr_address;
2001 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2002
2003 this_state = fragP->fr_subtype;
2004 start_type = this_type = table + this_state;
2005 target = offset;
2006
2007 if (symbolP)
2008 {
2009 #ifndef DIFF_EXPR_OK
2010 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2011 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2012 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2013 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2014 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2015 #endif
2016 know (symbolP->sy_frag);
2017 #endif
2018 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2019 || symbolP->sy_frag == &zero_address_frag);
2020 target += S_GET_VALUE (symbolP) + symbol_get_frag (symbolP)->fr_address;
2021
2022 /* If frag has yet to be reached on this pass,
2023 assume it will move by STRETCH just as we did.
2024 If this is not so, it will be because some frag
2025 between grows, and that will force another pass.
2026
2027 Beware zero-length frags.
2028
2029 There should be a faster way to do this. */
2030
2031 if (symbol_get_frag (symbolP)->fr_address >= was_address
2032 && is_dnrange (fragP, symbol_get_frag (symbolP)))
2033 {
2034 target += stretch;
2035 }
2036 }
2037
2038 aim = target - address - fragP->fr_fix;
2039 #ifdef TC_PCREL_ADJUST
2040 /* Currently only the ns32k family needs this. */
2041 aim += TC_PCREL_ADJUST (fragP);
2042 /* #else */
2043 /* This machine doesn't want to use pcrel_adjust.
2044 In that case, pcrel_adjust should be zero. */
2045 #if 0
2046 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2047 #endif
2048 #endif
2049 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2050 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2051 #endif
2052
2053 if (aim < 0)
2054 {
2055 /* Look backwards. */
2056 for (next_state = this_type->rlx_more; next_state;)
2057 if (aim >= this_type->rlx_backward)
2058 next_state = 0;
2059 else
2060 {
2061 /* Grow to next state. */
2062 this_state = next_state;
2063 this_type = table + this_state;
2064 next_state = this_type->rlx_more;
2065 }
2066 }
2067 else
2068 {
2069 /* Look forwards. */
2070 for (next_state = this_type->rlx_more; next_state;)
2071 if (aim <= this_type->rlx_forward)
2072 next_state = 0;
2073 else
2074 {
2075 /* Grow to next state. */
2076 this_state = next_state;
2077 this_type = table + this_state;
2078 next_state = this_type->rlx_more;
2079 }
2080 }
2081
2082 growth = this_type->rlx_length - start_type->rlx_length;
2083 if (growth != 0)
2084 fragP->fr_subtype = this_state;
2085 return growth;
2086 }
2087
2088 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2089
2090 /* Relax_align. Advance location counter to next address that has 'alignment'
2091 lowest order bits all 0s, return size of adjustment made. */
2092 static relax_addressT
2093 relax_align (address, alignment)
2094 register relax_addressT address; /* Address now. */
2095 register int alignment; /* Alignment (binary). */
2096 {
2097 relax_addressT mask;
2098 relax_addressT new_address;
2099
2100 mask = ~((~0) << alignment);
2101 new_address = (address + mask) & (~mask);
2102 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2103 if (linkrelax)
2104 /* We must provide lots of padding, so the linker can discard it
2105 when needed. The linker will not add extra space, ever. */
2106 new_address += (1 << alignment);
2107 #endif
2108 return (new_address - address);
2109 }
2110
2111 /* Now we have a segment, not a crowd of sub-segments, we can make
2112 fr_address values.
2113
2114 Relax the frags.
2115
2116 After this, all frags in this segment have addresses that are correct
2117 within the segment. Since segments live in different file addresses,
2118 these frag addresses may not be the same as final object-file
2119 addresses. */
2120
2121 void
2122 relax_segment (segment_frag_root, segment)
2123 struct frag *segment_frag_root;
2124 segT segment;
2125 {
2126 register struct frag *fragP;
2127 register relax_addressT address;
2128 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2129 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2130 #endif
2131 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2132 subseg_change (segment, 0);
2133
2134 /* For each frag in segment: count and store (a 1st guess of)
2135 fr_address. */
2136 address = 0;
2137 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2138 {
2139 fragP->fr_address = address;
2140 address += fragP->fr_fix;
2141
2142 switch (fragP->fr_type)
2143 {
2144 case rs_fill:
2145 address += fragP->fr_offset * fragP->fr_var;
2146 break;
2147
2148 case rs_align:
2149 case rs_align_code:
2150 {
2151 addressT offset = relax_align (address, (int) fragP->fr_offset);
2152
2153 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2154 offset = 0;
2155
2156 if (offset % fragP->fr_var != 0)
2157 {
2158 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2159 (unsigned long) offset, (long) fragP->fr_var);
2160 offset -= (offset % fragP->fr_var);
2161 }
2162
2163 address += offset;
2164 }
2165 break;
2166
2167 case rs_org:
2168 case rs_space:
2169 /* Assume .org is nugatory. It will grow with 1st relax. */
2170 break;
2171
2172 case rs_machine_dependent:
2173 address += md_estimate_size_before_relax (fragP, segment);
2174 break;
2175
2176 #ifndef WORKING_DOT_WORD
2177 /* Broken words don't concern us yet. */
2178 case rs_broken_word:
2179 break;
2180 #endif
2181
2182 case rs_leb128:
2183 /* Initial guess is always 1; doing otherwise can result in
2184 stable solutions that are larger than the minimum. */
2185 address += fragP->fr_offset = 1;
2186 break;
2187
2188 case rs_cfa:
2189 address += eh_frame_estimate_size_before_relax (fragP);
2190 break;
2191
2192 default:
2193 BAD_CASE (fragP->fr_type);
2194 break;
2195 }
2196 }
2197
2198 /* Do relax(). */
2199 {
2200 long stretch; /* May be any size, 0 or negative. */
2201 /* Cumulative number of addresses we have relaxed this pass.
2202 We may have relaxed more than one address. */
2203 long stretched; /* Have we stretched on this pass? */
2204 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2205 grew, and another shrank. If a branch instruction doesn't fit anymore,
2206 we could be scrod. */
2207
2208 do
2209 {
2210 stretch = stretched = 0;
2211
2212 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2213 {
2214 long growth = 0;
2215 addressT was_address;
2216 offsetT offset;
2217 symbolS *symbolP;
2218
2219 was_address = fragP->fr_address;
2220 address = fragP->fr_address += stretch;
2221 symbolP = fragP->fr_symbol;
2222 offset = fragP->fr_offset;
2223
2224 switch (fragP->fr_type)
2225 {
2226 case rs_fill: /* .fill never relaxes. */
2227 growth = 0;
2228 break;
2229
2230 #ifndef WORKING_DOT_WORD
2231 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2232 for it I do not want to write it. I do not want to have
2233 anything to do with it. This is not the proper way to
2234 implement this misfeature. */
2235 case rs_broken_word:
2236 {
2237 struct broken_word *lie;
2238 struct broken_word *untruth;
2239
2240 /* Yes this is ugly (storing the broken_word pointer
2241 in the symbol slot). Still, this whole chunk of
2242 code is ugly, and I don't feel like doing anything
2243 about it. Think of it as stubbornness in action. */
2244 growth = 0;
2245 for (lie = (struct broken_word *) (fragP->fr_symbol);
2246 lie && lie->dispfrag == fragP;
2247 lie = lie->next_broken_word)
2248 {
2249
2250 if (lie->added)
2251 continue;
2252
2253 offset = (symbol_get_frag (lie->add)->fr_address
2254 + S_GET_VALUE (lie->add)
2255 + lie->addnum
2256 - (symbol_get_frag (lie->sub)->fr_address
2257 + S_GET_VALUE (lie->sub)));
2258 if (offset <= -32768 || offset >= 32767)
2259 {
2260 if (flag_warn_displacement)
2261 {
2262 char buf[50];
2263 sprint_value (buf, (addressT) lie->addnum);
2264 as_warn (_(".word %s-%s+%s didn't fit"),
2265 S_GET_NAME (lie->add),
2266 S_GET_NAME (lie->sub),
2267 buf);
2268 }
2269 lie->added = 1;
2270 if (fragP->fr_subtype == 0)
2271 {
2272 fragP->fr_subtype++;
2273 growth += md_short_jump_size;
2274 }
2275 for (untruth = lie->next_broken_word;
2276 untruth && untruth->dispfrag == lie->dispfrag;
2277 untruth = untruth->next_broken_word)
2278 if ((symbol_get_frag (untruth->add)
2279 == symbol_get_frag (lie->add))
2280 && (S_GET_VALUE (untruth->add)
2281 == S_GET_VALUE (lie->add)))
2282 {
2283 untruth->added = 2;
2284 untruth->use_jump = lie;
2285 }
2286 growth += md_long_jump_size;
2287 }
2288 }
2289
2290 break;
2291 } /* case rs_broken_word */
2292 #endif
2293 case rs_align:
2294 case rs_align_code:
2295 {
2296 addressT oldoff, newoff;
2297
2298 oldoff = relax_align (was_address + fragP->fr_fix,
2299 (int) offset);
2300 newoff = relax_align (address + fragP->fr_fix,
2301 (int) offset);
2302
2303 if (fragP->fr_subtype != 0)
2304 {
2305 if (oldoff > fragP->fr_subtype)
2306 oldoff = 0;
2307 if (newoff > fragP->fr_subtype)
2308 newoff = 0;
2309 }
2310
2311 growth = newoff - oldoff;
2312 }
2313 break;
2314
2315 case rs_org:
2316 {
2317 long target = offset;
2318 long after;
2319
2320 if (symbolP)
2321 {
2322 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2323 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2324 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2325 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2326 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2327 know (symbolP->sy_frag);
2328 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2329 || (symbolP->sy_frag == &zero_address_frag));
2330 #endif
2331 target += (S_GET_VALUE (symbolP)
2332 + symbol_get_frag (symbolP)->fr_address);
2333 } /* if we have a symbol */
2334
2335 know (fragP->fr_next);
2336 after = fragP->fr_next->fr_address;
2337 growth = target - after;
2338 if (growth < 0)
2339 {
2340 /* Growth may be negative, but variable part of frag
2341 cannot have fewer than 0 chars. That is, we can't
2342 .org backwards. */
2343 as_bad_where (fragP->fr_file, fragP->fr_line,
2344 _("attempt to .org backwards ignored"));
2345
2346 /* We've issued an error message. Change the
2347 frag to avoid cascading errors. */
2348 fragP->fr_type = rs_align;
2349 fragP->fr_subtype = 0;
2350 fragP->fr_offset = 0;
2351 fragP->fr_fix = after - address;
2352 growth = stretch;
2353 }
2354
2355 /* This is an absolute growth factor */
2356 growth -= stretch;
2357 break;
2358 }
2359
2360 case rs_space:
2361 if (symbolP)
2362 {
2363 growth = S_GET_VALUE (symbolP);
2364 if (symbol_get_frag (symbolP) != &zero_address_frag
2365 || S_IS_COMMON (symbolP)
2366 || ! S_IS_DEFINED (symbolP))
2367 as_bad_where (fragP->fr_file, fragP->fr_line,
2368 _(".space specifies non-absolute value"));
2369 fragP->fr_symbol = 0;
2370 if (growth < 0)
2371 {
2372 as_warn (_(".space or .fill with negative value, ignored"));
2373 growth = 0;
2374 }
2375 }
2376 else
2377 growth = 0;
2378 break;
2379
2380 case rs_machine_dependent:
2381 #ifdef md_relax_frag
2382 growth = md_relax_frag (fragP, stretch);
2383 #else
2384 #ifdef TC_GENERIC_RELAX_TABLE
2385 /* The default way to relax a frag is to look through
2386 TC_GENERIC_RELAX_TABLE. */
2387 growth = relax_frag (fragP, stretch);
2388 #endif /* TC_GENERIC_RELAX_TABLE */
2389 #endif
2390 break;
2391
2392 case rs_leb128:
2393 {
2394 valueT value;
2395 int size;
2396
2397 value = resolve_symbol_value (fragP->fr_symbol, 0);
2398 size = sizeof_leb128 (value, fragP->fr_subtype);
2399 growth = size - fragP->fr_offset;
2400 fragP->fr_offset = size;
2401 }
2402 break;
2403
2404 case rs_cfa:
2405 growth = eh_frame_relax_frag (fragP);
2406 break;
2407
2408 default:
2409 BAD_CASE (fragP->fr_type);
2410 break;
2411 }
2412 if (growth)
2413 {
2414 stretch += growth;
2415 stretched++;
2416 }
2417 } /* For each frag in the segment. */
2418 }
2419 while (stretched); /* Until nothing further to relax. */
2420 } /* do_relax */
2421
2422 /* We now have valid fr_address'es for each frag. */
2423
2424 /* All fr_address's are correct, relative to their own segment.
2425 We have made all the fixS we will ever make. */
2426 }
2427
2428 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2429
2430 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2431 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2432 #endif
2433
2434 /* fixup_segment()
2435
2436 Go through all the fixS's in a segment and see which ones can be
2437 handled now. (These consist of fixS where we have since discovered
2438 the value of a symbol, or the address of the frag involved.)
2439 For each one, call md_apply_fix to put the fix into the frag data.
2440
2441 Result is a count of how many relocation structs will be needed to
2442 handle the remaining fixS's that we couldn't completely handle here.
2443 These will be output later by emit_relocations(). */
2444
2445 static long
2446 fixup_segment (fixP, this_segment_type)
2447 register fixS *fixP;
2448 segT this_segment_type; /* N_TYPE bits for segment. */
2449 {
2450 long seg_reloc_count = 0;
2451 symbolS *add_symbolP;
2452 symbolS *sub_symbolP;
2453 valueT add_number;
2454 int size;
2455 char *place;
2456 long where;
2457 int pcrel, plt;
2458 fragS *fragP;
2459 segT add_symbol_segment = absolute_section;
2460
2461 /* If the linker is doing the relaxing, we must not do any fixups.
2462
2463 Well, strictly speaking that's not true -- we could do any that are
2464 PC-relative and don't cross regions that could change size. And for the
2465 i960 (the only machine for which we've got a relaxing linker right now),
2466 we might be able to turn callx/callj into bal anyways in cases where we
2467 know the maximum displacement. */
2468 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2469 {
2470 for (; fixP; fixP = fixP->fx_next)
2471 seg_reloc_count++;
2472 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2473 return seg_reloc_count;
2474 }
2475
2476 for (; fixP; fixP = fixP->fx_next)
2477 {
2478 #ifdef DEBUG5
2479 fprintf (stderr, "\nprocessing fixup:\n");
2480 print_fixup (fixP);
2481 #endif
2482
2483 fragP = fixP->fx_frag;
2484 know (fragP);
2485 where = fixP->fx_where;
2486 place = fragP->fr_literal + where;
2487 size = fixP->fx_size;
2488 add_symbolP = fixP->fx_addsy;
2489 #ifdef TC_VALIDATE_FIX
2490 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2491 #endif
2492 sub_symbolP = fixP->fx_subsy;
2493 add_number = fixP->fx_offset;
2494 pcrel = fixP->fx_pcrel;
2495 plt = fixP->fx_plt;
2496
2497 if (add_symbolP != NULL
2498 && symbol_mri_common_p (add_symbolP))
2499 {
2500 know (add_symbolP->sy_value.X_op == O_symbol);
2501 add_number += S_GET_VALUE (add_symbolP);
2502 fixP->fx_offset = add_number;
2503 add_symbolP = fixP->fx_addsy =
2504 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2505 }
2506
2507 if (add_symbolP)
2508 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2509
2510 if (sub_symbolP)
2511 {
2512 resolve_symbol_value (sub_symbolP, 1);
2513 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2514 {
2515 if (add_symbolP != NULL)
2516 {
2517 add_number += S_GET_VALUE (add_symbolP);
2518 add_symbolP = NULL;
2519 fixP->fx_addsy = NULL;
2520 }
2521
2522 /* It's just -sym. */
2523 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2524 {
2525 add_number -= S_GET_VALUE (sub_symbolP);
2526 fixP->fx_subsy = NULL;
2527 }
2528 else if (pcrel
2529 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2530 {
2531 /* Should try converting to a constant. */
2532 goto bad_sub_reloc;
2533 }
2534 else
2535 bad_sub_reloc:
2536 as_bad_where (fixP->fx_file, fixP->fx_line,
2537 _("Negative of non-absolute symbol %s"),
2538 S_GET_NAME (sub_symbolP));
2539 }
2540 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2541 && SEG_NORMAL (add_symbol_segment))
2542 {
2543 /* Difference of 2 symbols from same segment.
2544 Can't make difference of 2 undefineds: 'value' means
2545 something different for N_UNDF. */
2546 #ifdef TC_I960
2547 /* Makes no sense to use the difference of 2 arbitrary symbols
2548 as the target of a call instruction. */
2549 if (fixP->fx_tcbit)
2550 as_bad_where (fixP->fx_file, fixP->fx_line,
2551 _("callj to difference of 2 symbols"));
2552 #endif /* TC_I960 */
2553 add_number += S_GET_VALUE (add_symbolP) -
2554 S_GET_VALUE (sub_symbolP);
2555
2556 add_symbolP = NULL;
2557 pcrel = 0; /* No further pcrel processing. */
2558
2559 /* Let the target machine make the final determination
2560 as to whether or not a relocation will be needed to
2561 handle this fixup. */
2562 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2563 {
2564 fixP->fx_pcrel = 0;
2565 fixP->fx_addsy = NULL;
2566 fixP->fx_subsy = NULL;
2567 }
2568 }
2569 else
2570 {
2571 /* Different segments in subtraction. */
2572 know (!(S_IS_EXTERNAL (sub_symbolP)
2573 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2574
2575 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2576 add_number -= S_GET_VALUE (sub_symbolP);
2577
2578 #ifdef DIFF_EXPR_OK
2579 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2580 #if 0
2581 /* Do this even if it's already described as
2582 pc-relative. For example, on the m68k, an
2583 operand of "pc@(foo-.-2)" should address
2584 "foo" in a pc-relative mode. */
2585 && pcrel
2586 #endif
2587 )
2588 {
2589 /* Make it pc-relative. */
2590 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2591 - S_GET_VALUE (sub_symbolP));
2592 pcrel = 1;
2593 fixP->fx_pcrel = 1;
2594 sub_symbolP = 0;
2595 fixP->fx_subsy = 0;
2596 }
2597 #endif
2598 #ifdef UNDEFINED_DIFFERENCE_OK
2599 /* The PA needs this for PIC code generation. We basically
2600 don't want to do anything if we have the difference of two
2601 symbols at this point. */
2602 else if (1)
2603 {
2604 /* Leave it alone. */
2605 }
2606 #endif
2607 #ifdef BFD_ASSEMBLER
2608 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2609 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2610 {
2611 /* Leave it alone. */
2612 }
2613 #endif
2614 else
2615 {
2616 char buf[50];
2617 sprint_value (buf, fragP->fr_address + where);
2618 as_bad_where (fixP->fx_file, fixP->fx_line,
2619 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2620 S_GET_NAME (add_symbolP),
2621 segment_name (S_GET_SEGMENT (add_symbolP)),
2622 S_GET_NAME (sub_symbolP),
2623 segment_name (S_GET_SEGMENT (sub_symbolP)),
2624 buf);
2625 }
2626 }
2627 }
2628
2629 if (add_symbolP)
2630 {
2631 if (add_symbol_segment == this_segment_type && pcrel && !plt
2632 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2633 {
2634 /* This fixup was made when the symbol's segment was
2635 SEG_UNKNOWN, but it is now in the local segment.
2636 So we know how to do the address without relocation. */
2637 #ifdef TC_I960
2638 /* reloc_callj() may replace a 'call' with a 'calls' or a
2639 'bal', in which cases it modifies *fixP as appropriate.
2640 In the case of a 'calls', no further work is required,
2641 and *fixP has been set up to make the rest of the code
2642 below a no-op. */
2643 reloc_callj (fixP);
2644 #endif /* TC_I960 */
2645
2646 add_number += S_GET_VALUE (add_symbolP);
2647 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2648 /* Lie. Don't want further pcrel processing. */
2649 pcrel = 0;
2650
2651 /* Let the target machine make the final determination
2652 as to whether or not a relocation will be needed to
2653 handle this fixup. */
2654 if (!TC_FORCE_RELOCATION (fixP))
2655 {
2656 fixP->fx_pcrel = 0;
2657 fixP->fx_addsy = NULL;
2658 }
2659 }
2660 else
2661 {
2662 if (add_symbol_segment == absolute_section
2663 && ! pcrel)
2664 {
2665 #ifdef TC_I960
2666 /* See comment about reloc_callj() above. */
2667 reloc_callj (fixP);
2668 #endif /* TC_I960 */
2669 add_number += S_GET_VALUE (add_symbolP);
2670
2671 /* Let the target machine make the final determination
2672 as to whether or not a relocation will be needed to
2673 handle this fixup. */
2674
2675 if (!TC_FORCE_RELOCATION (fixP))
2676 {
2677 fixP->fx_addsy = NULL;
2678 add_symbolP = NULL;
2679 }
2680 }
2681 else if (add_symbol_segment == undefined_section
2682 #ifdef BFD_ASSEMBLER
2683 || bfd_is_com_section (add_symbol_segment)
2684 #endif
2685 )
2686 {
2687 #ifdef TC_I960
2688 if ((int) fixP->fx_bit_fixP == 13)
2689 {
2690 /* This is a COBR instruction. They have only a
2691 13-bit displacement and are only to be used
2692 for local branches: flag as error, don't generate
2693 relocation. */
2694 as_bad_where (fixP->fx_file, fixP->fx_line,
2695 _("can't use COBR format with external label"));
2696 fixP->fx_addsy = NULL;
2697 fixP->fx_done = 1;
2698 continue;
2699 } /* COBR. */
2700 #endif /* TC_I960 */
2701
2702 #ifdef OBJ_COFF
2703 #ifdef TE_I386AIX
2704 if (S_IS_COMMON (add_symbolP))
2705 add_number += S_GET_VALUE (add_symbolP);
2706 #endif /* TE_I386AIX */
2707 #endif /* OBJ_COFF */
2708 ++seg_reloc_count;
2709 }
2710 else
2711 {
2712 seg_reloc_count++;
2713 #ifdef TC_DONT_FIX_NON_ADJUSTABLE
2714 if (1
2715 #ifdef obj_fix_adjustable
2716 && obj_fix_adjustable (fixP)
2717 #endif
2718 #ifdef tc_fix_adjustable
2719 && tc_fix_adjustable (fixP)
2720 #endif
2721 )
2722 #endif
2723 add_number += S_GET_VALUE (add_symbolP);
2724 }
2725 }
2726 }
2727
2728 if (pcrel)
2729 {
2730 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2731 if (add_symbolP == 0)
2732 {
2733 #ifndef BFD_ASSEMBLER
2734 fixP->fx_addsy = &abs_symbol;
2735 #else
2736 fixP->fx_addsy = section_symbol (absolute_section);
2737 #endif
2738 symbol_mark_used_in_reloc (fixP->fx_addsy);
2739 ++seg_reloc_count;
2740 }
2741 }
2742
2743 if (!fixP->fx_done)
2744 {
2745 #ifdef MD_APPLY_FIX3
2746 md_apply_fix3 (fixP, &add_number, this_segment_type);
2747 #else
2748 #ifdef BFD_ASSEMBLER
2749 md_apply_fix (fixP, &add_number);
2750 #else
2751 md_apply_fix (fixP, add_number);
2752 #endif
2753 #endif
2754
2755 #ifndef TC_HANDLES_FX_DONE
2756 /* If the tc-* files haven't been converted, assume it's handling
2757 it the old way, where a null fx_addsy means that the fix has
2758 been applied completely, and no further work is needed. */
2759 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2760 fixP->fx_done = 1;
2761 #endif
2762 }
2763
2764 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2765 {
2766 if ((size_t) size < sizeof (valueT))
2767 {
2768 valueT mask;
2769
2770 mask = 0;
2771 mask--; /* Set all bits to one. */
2772 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2773 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2774 {
2775 char buf[50], buf2[50];
2776 sprint_value (buf, fragP->fr_address + where);
2777 if (add_number > 1000)
2778 sprint_value (buf2, add_number);
2779 else
2780 sprintf (buf2, "%ld", (long) add_number);
2781 as_bad_where (fixP->fx_file, fixP->fx_line,
2782 _("Value of %s too large for field of %d bytes at %s"),
2783 buf2, size, buf);
2784 } /* Generic error checking. */
2785 }
2786 #ifdef WARN_SIGNED_OVERFLOW_WORD
2787 /* Warn if a .word value is too large when treated as a signed
2788 number. We already know it is not too negative. This is to
2789 catch over-large switches generated by gcc on the 68k. */
2790 if (!flag_signed_overflow_ok
2791 && size == 2
2792 && add_number > 0x7fff)
2793 as_bad_where (fixP->fx_file, fixP->fx_line,
2794 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2795 (long) add_number,
2796 (unsigned long) (fragP->fr_address + where));
2797 #endif
2798 } /* Not a bit fix. */
2799
2800 #ifdef TC_VALIDATE_FIX
2801 skip: ATTRIBUTE_UNUSED_LABEL
2802 ;
2803 #endif
2804 #ifdef DEBUG5
2805 fprintf (stderr, "result:\n");
2806 print_fixup (fixP);
2807 #endif
2808 } /* For each fixS in this segment. */
2809
2810 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2811 return seg_reloc_count;
2812 }
2813
2814 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2815
2816 void
2817 number_to_chars_bigendian (buf, val, n)
2818 char *buf;
2819 valueT val;
2820 int n;
2821 {
2822 if ((size_t) n > sizeof (val) || n <= 0)
2823 abort ();
2824 while (n--)
2825 {
2826 buf[n] = val & 0xff;
2827 val >>= 8;
2828 }
2829 }
2830
2831 void
2832 number_to_chars_littleendian (buf, val, n)
2833 char *buf;
2834 valueT val;
2835 int n;
2836 {
2837 if ((size_t) n > sizeof (val) || n <= 0)
2838 abort ();
2839 while (n--)
2840 {
2841 *buf++ = val & 0xff;
2842 val >>= 8;
2843 }
2844 }
2845
2846 void
2847 write_print_statistics (file)
2848 FILE *file;
2849 {
2850 fprintf (file, "fixups: %d\n", n_fixups);
2851 }
2852
2853 /* For debugging. */
2854 extern int indent_level;
2855
2856 void
2857 print_fixup (fixp)
2858 fixS *fixp;
2859 {
2860 indent_level = 1;
2861 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2862 if (fixp->fx_pcrel)
2863 fprintf (stderr, " pcrel");
2864 if (fixp->fx_pcrel_adjust)
2865 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2866 if (fixp->fx_im_disp)
2867 {
2868 #ifdef TC_NS32K
2869 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2870 #else
2871 fprintf (stderr, " im_disp");
2872 #endif
2873 }
2874 if (fixp->fx_tcbit)
2875 fprintf (stderr, " tcbit");
2876 if (fixp->fx_done)
2877 fprintf (stderr, " done");
2878 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2879 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2880 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2881 #ifdef BFD_ASSEMBLER
2882 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2883 fixp->fx_r_type);
2884 #else
2885 #ifdef NEED_FX_R_TYPE
2886 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2887 #endif
2888 #endif
2889 if (fixp->fx_addsy)
2890 {
2891 fprintf (stderr, "\n +<");
2892 print_symbol_value_1 (stderr, fixp->fx_addsy);
2893 fprintf (stderr, ">");
2894 }
2895 if (fixp->fx_subsy)
2896 {
2897 fprintf (stderr, "\n -<");
2898 print_symbol_value_1 (stderr, fixp->fx_subsy);
2899 fprintf (stderr, ">");
2900 }
2901 fprintf (stderr, "\n");
2902 #ifdef TC_FIX_DATA_PRINT
2903 TC_FIX_DATA_PRINT (stderr, fixp);
2904 #endif
2905 }
This page took 0.086923 seconds and 5 git commands to generate.