* as.h: Remove #if 0'd code.
[binutils.git] / gas / write.c
blob320e359fb9a9df8f25aa5cb599c95bda1c781229
1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This thing should be set up to do byteordering correctly. But... */
25 #include "as.h"
26 #include "subsegs.h"
27 #include "obstack.h"
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
33 #endif
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX) \
37 (generic_force_reloc (FIX))
38 #endif
40 #ifndef TC_FORCE_RELOCATION_ABS
41 #define TC_FORCE_RELOCATION_ABS(FIX) \
42 (TC_FORCE_RELOCATION (FIX))
43 #endif
45 #ifndef TC_FORCE_RELOCATION_LOCAL
46 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
47 (!(FIX)->fx_pcrel \
48 || (FIX)->fx_plt \
49 || TC_FORCE_RELOCATION (FIX))
50 #endif
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
54 (! SEG_NORMAL (SEG))
55 #endif
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX) 0
59 #endif
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
62 #ifdef DIFF_EXPR_OK
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 0
64 #else
65 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1
66 #endif
67 #endif
69 #ifndef TC_VALIDATE_FIX_SUB
70 #ifdef UNDEFINED_DIFFERENCE_OK
71 /* The PA needs this for PIC code generation. */
72 #define TC_VALIDATE_FIX_SUB(FIX) 1
73 #else
74 #ifdef BFD_ASSEMBLER
75 #define TC_VALIDATE_FIX_SUB(FIX) \
76 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
77 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
78 #else
79 #define TC_VALIDATE_FIX_SUB(FIX) 0
80 #endif
81 #endif
82 #endif
84 #ifndef TC_LINKRELAX_FIXUP
85 #define TC_LINKRELAX_FIXUP(SEG) 1
86 #endif
88 #ifndef MD_APPLY_SYM_VALUE
89 #define MD_APPLY_SYM_VALUE(FIX) 1
90 #endif
92 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
93 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
94 #endif
96 #ifndef MD_PCREL_FROM_SECTION
97 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
98 #endif
100 /* Used to control final evaluation of expressions. */
101 int finalize_syms = 0;
103 int symbol_table_frozen;
105 symbolS *abs_section_sym;
107 /* Remember the value of dot when parsing expressions. */
108 addressT dot_value;
110 void print_fixup (fixS *);
112 #ifdef BFD_ASSEMBLER
113 static void renumber_sections (bfd *, asection *, PTR);
115 /* We generally attach relocs to frag chains. However, after we have
116 chained these all together into a segment, any relocs we add after
117 that must be attached to a segment. This will include relocs added
118 in md_estimate_size_for_relax, for example. */
119 static int frags_chained = 0;
120 #endif
122 #ifndef BFD_ASSEMBLER
124 #ifndef MANY_SEGMENTS
125 struct frag *text_frag_root;
126 struct frag *data_frag_root;
127 struct frag *bss_frag_root;
129 struct frag *text_last_frag; /* Last frag in segment. */
130 struct frag *data_last_frag; /* Last frag in segment. */
131 static struct frag *bss_last_frag; /* Last frag in segment. */
132 #endif
134 #ifndef BFD
135 static object_headers headers;
136 #endif
138 long string_byte_count;
139 char *next_object_file_charP; /* Tracks object file bytes. */
141 #ifndef OBJ_VMS
142 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
143 #endif
145 #endif /* BFD_ASSEMBLER */
147 static int n_fixups;
149 #ifdef BFD_ASSEMBLER
150 #define RELOC_ENUM enum bfd_reloc_code_real
151 #else
152 #define RELOC_ENUM int
153 #endif
155 static fixS *fix_new_internal (fragS *, int where, int size,
156 symbolS *add, symbolS *sub,
157 offsetT offset, int pcrel,
158 RELOC_ENUM r_type);
159 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
160 static long fixup_segment (fixS *, segT);
161 #endif
162 static relax_addressT relax_align (relax_addressT addr, int align);
163 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
164 static fragS *chain_frchains_together_1 (segT, struct frchain *);
165 #endif
166 #ifdef BFD_ASSEMBLER
167 static void chain_frchains_together (bfd *, segT, PTR);
168 static void cvt_frag_to_fill (segT, fragS *);
169 static void adjust_reloc_syms (bfd *, asection *, PTR);
170 static void fix_segment (bfd *, asection *, PTR);
171 static void write_relocs (bfd *, asection *, PTR);
172 static void write_contents (bfd *, asection *, PTR);
173 static void set_symtab (void);
174 #endif
175 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
176 static void merge_data_into_text (void);
177 #endif
178 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
179 static void cvt_frag_to_fill (object_headers *, segT, fragS *);
180 static void remove_subsegs (frchainS *, int, fragS **, fragS **);
181 static void relax_and_size_all_segments (void);
182 #endif
184 /* Create a fixS in obstack 'notes'. */
186 static fixS *
187 fix_new_internal (fragS *frag, /* Which frag? */
188 int where, /* Where in that frag? */
189 int size, /* 1, 2, or 4 usually. */
190 symbolS *add_symbol, /* X_add_symbol. */
191 symbolS *sub_symbol, /* X_op_symbol. */
192 offsetT offset, /* X_add_number. */
193 int pcrel, /* TRUE if PC-relative relocation. */
194 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
196 fixS *fixP;
198 n_fixups++;
200 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
202 fixP->fx_frag = frag;
203 fixP->fx_where = where;
204 fixP->fx_size = size;
205 /* We've made fx_size a narrow field; check that it's wide enough. */
206 if (fixP->fx_size != size)
208 as_bad (_("field fx_size too small to hold %d"), size);
209 abort ();
211 fixP->fx_addsy = add_symbol;
212 fixP->fx_subsy = sub_symbol;
213 fixP->fx_offset = offset;
214 fixP->fx_dot_value = dot_value;
215 fixP->fx_pcrel = pcrel;
216 fixP->fx_plt = 0;
217 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
218 fixP->fx_r_type = r_type;
219 #endif
220 fixP->fx_im_disp = 0;
221 fixP->fx_pcrel_adjust = 0;
222 fixP->fx_bit_fixP = 0;
223 fixP->fx_addnumber = 0;
224 fixP->fx_tcbit = 0;
225 fixP->fx_done = 0;
226 fixP->fx_no_overflow = 0;
227 fixP->fx_signed = 0;
229 #ifdef USING_CGEN
230 fixP->fx_cgen.insn = NULL;
231 fixP->fx_cgen.opinfo = 0;
232 #endif
234 #ifdef TC_FIX_TYPE
235 TC_INIT_FIX_DATA (fixP);
236 #endif
238 as_where (&fixP->fx_file, &fixP->fx_line);
240 /* Usually, we want relocs sorted numerically, but while
241 comparing to older versions of gas that have relocs
242 reverse sorted, it is convenient to have this compile
243 time option. xoxorich. */
246 #ifdef BFD_ASSEMBLER
247 fixS **seg_fix_rootP = (frags_chained
248 ? &seg_info (now_seg)->fix_root
249 : &frchain_now->fix_root);
250 fixS **seg_fix_tailP = (frags_chained
251 ? &seg_info (now_seg)->fix_tail
252 : &frchain_now->fix_tail);
253 #endif
255 #ifdef REVERSE_SORT_RELOCS
257 fixP->fx_next = *seg_fix_rootP;
258 *seg_fix_rootP = fixP;
260 #else /* REVERSE_SORT_RELOCS */
262 fixP->fx_next = NULL;
264 if (*seg_fix_tailP)
265 (*seg_fix_tailP)->fx_next = fixP;
266 else
267 *seg_fix_rootP = fixP;
268 *seg_fix_tailP = fixP;
270 #endif /* REVERSE_SORT_RELOCS */
273 return fixP;
276 /* Create a fixup relative to a symbol (plus a constant). */
278 fixS *
279 fix_new (fragS *frag, /* Which frag? */
280 int where, /* Where in that frag? */
281 int size, /* 1, 2, or 4 usually. */
282 symbolS *add_symbol, /* X_add_symbol. */
283 offsetT offset, /* X_add_number. */
284 int pcrel, /* TRUE if PC-relative relocation. */
285 RELOC_ENUM r_type /* Relocation type. */)
287 return fix_new_internal (frag, where, size, add_symbol,
288 (symbolS *) NULL, offset, pcrel, r_type);
291 /* Create a fixup for an expression. Currently we only support fixups
292 for difference expressions. That is itself more than most object
293 file formats support anyhow. */
295 fixS *
296 fix_new_exp (fragS *frag, /* Which frag? */
297 int where, /* Where in that frag? */
298 int size, /* 1, 2, or 4 usually. */
299 expressionS *exp, /* Expression. */
300 int pcrel, /* TRUE if PC-relative relocation. */
301 RELOC_ENUM r_type /* Relocation type. */)
303 symbolS *add = NULL;
304 symbolS *sub = NULL;
305 offsetT off = 0;
307 switch (exp->X_op)
309 case O_absent:
310 break;
312 case O_register:
313 as_bad (_("register value used as expression"));
314 break;
316 case O_add:
317 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
318 the difference expression cannot immediately be reduced. */
320 symbolS *stmp = make_expr_symbol (exp);
322 exp->X_op = O_symbol;
323 exp->X_op_symbol = 0;
324 exp->X_add_symbol = stmp;
325 exp->X_add_number = 0;
327 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
330 case O_symbol_rva:
331 add = exp->X_add_symbol;
332 off = exp->X_add_number;
334 #if defined(BFD_ASSEMBLER)
335 r_type = BFD_RELOC_RVA;
336 #else
337 #if defined(TC_RVA_RELOC)
338 r_type = TC_RVA_RELOC;
339 #else
340 as_fatal (_("rva not supported"));
341 #endif
342 #endif
343 break;
345 case O_uminus:
346 sub = exp->X_add_symbol;
347 off = exp->X_add_number;
348 break;
350 case O_subtract:
351 sub = exp->X_op_symbol;
352 /* Fall through. */
353 case O_symbol:
354 add = exp->X_add_symbol;
355 /* Fall through. */
356 case O_constant:
357 off = exp->X_add_number;
358 break;
360 default:
361 add = make_expr_symbol (exp);
362 break;
365 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
368 /* Generic function to determine whether a fixup requires a relocation. */
370 generic_force_reloc (fixS *fix)
372 #ifdef BFD_ASSEMBLER
373 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
374 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
375 return 1;
376 #endif
377 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
380 /* Append a string onto another string, bumping the pointer along. */
381 void
382 append (char **charPP, char *fromP, unsigned long length)
384 /* Don't trust memcpy() of 0 chars. */
385 if (length == 0)
386 return;
388 memcpy (*charPP, fromP, length);
389 *charPP += length;
392 #ifndef BFD_ASSEMBLER
393 int section_alignment[SEG_MAXIMUM_ORDINAL];
394 #endif
396 /* This routine records the largest alignment seen for each segment.
397 If the beginning of the segment is aligned on the worst-case
398 boundary, all of the other alignments within it will work. At
399 least one object format really uses this info. */
401 void
402 record_alignment (/* Segment to which alignment pertains. */
403 segT seg,
404 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
405 boundary, 2 => 4-byte boundary, etc.) */
406 int align)
408 if (seg == absolute_section)
409 return;
410 #ifdef BFD_ASSEMBLER
411 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
412 bfd_set_section_alignment (stdoutput, seg, align);
413 #else
414 if (align > section_alignment[(int) seg])
415 section_alignment[(int) seg] = align;
416 #endif
420 get_recorded_alignment (segT seg)
422 if (seg == absolute_section)
423 return 0;
424 #ifdef BFD_ASSEMBLER
425 return bfd_get_section_alignment (stdoutput, seg);
426 #else
427 return section_alignment[(int) seg];
428 #endif
431 #ifdef BFD_ASSEMBLER
433 /* Reset the section indices after removing the gas created sections. */
435 static void
436 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR countparg)
438 int *countp = (int *) countparg;
440 sec->index = *countp;
441 ++*countp;
444 #endif /* defined (BFD_ASSEMBLER) */
446 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
448 static fragS *
449 chain_frchains_together_1 (segT section, struct frchain *frchp)
451 fragS dummy, *prev_frag = &dummy;
452 #ifdef BFD_ASSEMBLER
453 fixS fix_dummy, *prev_fix = &fix_dummy;
454 #endif
456 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
458 prev_frag->fr_next = frchp->frch_root;
459 prev_frag = frchp->frch_last;
460 assert (prev_frag->fr_type != 0);
461 #ifdef BFD_ASSEMBLER
462 if (frchp->fix_root != (fixS *) NULL)
464 if (seg_info (section)->fix_root == (fixS *) NULL)
465 seg_info (section)->fix_root = frchp->fix_root;
466 prev_fix->fx_next = frchp->fix_root;
467 seg_info (section)->fix_tail = frchp->fix_tail;
468 prev_fix = frchp->fix_tail;
470 #endif
472 assert (prev_frag->fr_type != 0);
473 prev_frag->fr_next = 0;
474 return prev_frag;
477 #endif
479 #ifdef BFD_ASSEMBLER
481 static void
482 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
483 segT section,
484 PTR xxx ATTRIBUTE_UNUSED)
486 segment_info_type *info;
488 /* BFD may have introduced its own sections without using
489 subseg_new, so it is possible that seg_info is NULL. */
490 info = seg_info (section);
491 if (info != (segment_info_type *) NULL)
492 info->frchainP->frch_last
493 = chain_frchains_together_1 (section, info->frchainP);
495 /* Now that we've chained the frags together, we must add new fixups
496 to the segment, not to the frag chain. */
497 frags_chained = 1;
500 #endif
502 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
504 static void
505 remove_subsegs (frchainS *head, int seg, fragS **root, fragS **last)
507 *root = head->frch_root;
508 *last = chain_frchains_together_1 (seg, head);
511 #endif /* BFD */
513 #if defined (BFD_ASSEMBLER) || !defined (BFD)
515 #ifdef BFD_ASSEMBLER
516 static void
517 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
518 #else
519 static void
520 cvt_frag_to_fill (object_headers *headersP, segT sec, fragS *fragP)
521 #endif
523 switch (fragP->fr_type)
525 case rs_align:
526 case rs_align_code:
527 case rs_align_test:
528 case rs_org:
529 case rs_space:
530 #ifdef HANDLE_ALIGN
531 HANDLE_ALIGN (fragP);
532 #endif
533 know (fragP->fr_next != NULL);
534 fragP->fr_offset = (fragP->fr_next->fr_address
535 - fragP->fr_address
536 - fragP->fr_fix) / fragP->fr_var;
537 if (fragP->fr_offset < 0)
539 as_bad_where (fragP->fr_file, fragP->fr_line,
540 _("attempt to .org/.space backwards? (%ld)"),
541 (long) fragP->fr_offset);
542 fragP->fr_offset = 0;
544 fragP->fr_type = rs_fill;
545 break;
547 case rs_fill:
548 break;
550 case rs_leb128:
552 valueT value = S_GET_VALUE (fragP->fr_symbol);
553 int size;
555 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
556 fragP->fr_subtype);
558 fragP->fr_fix += size;
559 fragP->fr_type = rs_fill;
560 fragP->fr_var = 0;
561 fragP->fr_offset = 0;
562 fragP->fr_symbol = NULL;
564 break;
566 case rs_cfa:
567 eh_frame_convert_frag (fragP);
568 break;
570 case rs_dwarf2dbg:
571 dwarf2dbg_convert_frag (fragP);
572 break;
574 case rs_machine_dependent:
575 #ifdef BFD_ASSEMBLER
576 md_convert_frag (stdoutput, sec, fragP);
577 #else
578 md_convert_frag (headersP, sec, fragP);
579 #endif
581 assert (fragP->fr_next == NULL
582 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
583 == fragP->fr_fix));
585 /* After md_convert_frag, we make the frag into a ".space 0".
586 md_convert_frag() should set up any fixSs and constants
587 required. */
588 frag_wane (fragP);
589 break;
591 #ifndef WORKING_DOT_WORD
592 case rs_broken_word:
594 struct broken_word *lie;
596 if (fragP->fr_subtype)
598 fragP->fr_fix += md_short_jump_size;
599 for (lie = (struct broken_word *) (fragP->fr_symbol);
600 lie && lie->dispfrag == fragP;
601 lie = lie->next_broken_word)
602 if (lie->added == 1)
603 fragP->fr_fix += md_long_jump_size;
605 frag_wane (fragP);
607 break;
608 #endif
610 default:
611 BAD_CASE (fragP->fr_type);
612 break;
614 #ifdef md_frag_check
615 md_frag_check (fragP);
616 #endif
619 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
621 #ifdef BFD_ASSEMBLER
622 static void relax_seg (bfd *, asection *, PTR);
624 static void
625 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR xxx)
627 segment_info_type *seginfo = seg_info (sec);
629 if (seginfo && seginfo->frchainP
630 && relax_segment (seginfo->frchainP->frch_root, sec))
632 int *result = (int *) xxx;
633 *result = 1;
637 static void size_seg (bfd *, asection *, PTR);
639 static void
640 size_seg (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
642 flagword flags;
643 fragS *fragp;
644 segment_info_type *seginfo;
645 int x;
646 valueT size, newsize;
648 subseg_change (sec, 0);
650 seginfo = seg_info (sec);
651 if (seginfo && seginfo->frchainP)
653 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
654 cvt_frag_to_fill (sec, fragp);
655 for (fragp = seginfo->frchainP->frch_root;
656 fragp->fr_next;
657 fragp = fragp->fr_next)
658 /* Walk to last elt. */
660 size = fragp->fr_address + fragp->fr_fix;
662 else
663 size = 0;
665 flags = bfd_get_section_flags (abfd, sec);
667 if (size > 0 && ! seginfo->bss)
668 flags |= SEC_HAS_CONTENTS;
670 /* @@ This is just an approximation. */
671 if (seginfo && seginfo->fix_root)
672 flags |= SEC_RELOC;
673 else
674 flags &= ~SEC_RELOC;
675 x = bfd_set_section_flags (abfd, sec, flags);
676 assert (x);
678 newsize = md_section_align (sec, size);
679 x = bfd_set_section_size (abfd, sec, newsize);
680 assert (x);
682 /* If the size had to be rounded up, add some padding in the last
683 non-empty frag. */
684 assert (newsize >= size);
685 if (size != newsize)
687 fragS *last = seginfo->frchainP->frch_last;
688 fragp = seginfo->frchainP->frch_root;
689 while (fragp->fr_next != last)
690 fragp = fragp->fr_next;
691 last->fr_address = size;
692 if ((newsize - size) % fragp->fr_var == 0)
693 fragp->fr_offset += (newsize - size) / fragp->fr_var;
694 else
695 /* If we hit this abort, it's likely due to subsegs_finish not
696 providing sufficient alignment on the last frag, and the
697 machine dependent code using alignment frags with fr_var
698 greater than 1. */
699 abort ();
702 #ifdef tc_frob_section
703 tc_frob_section (sec);
704 #endif
705 #ifdef obj_frob_section
706 obj_frob_section (sec);
707 #endif
710 #ifdef DEBUG2
711 static void
712 dump_section_relocs (abfd, sec, stream_)
713 bfd *abfd ATTRIBUTE_UNUSED;
714 asection *sec;
715 char *stream_;
717 FILE *stream = (FILE *) stream_;
718 segment_info_type *seginfo = seg_info (sec);
719 fixS *fixp = seginfo->fix_root;
721 if (!fixp)
722 return;
724 fprintf (stream, "sec %s relocs:\n", sec->name);
725 while (fixp)
727 symbolS *s = fixp->fx_addsy;
729 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
730 (int) fixp->fx_r_type);
731 if (s == NULL)
732 fprintf (stream, "no sym\n");
733 else
735 print_symbol_value_1 (stream, s);
736 fprintf (stream, "\n");
738 fixp = fixp->fx_next;
741 #else
742 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
743 #endif
745 #ifndef EMIT_SECTION_SYMBOLS
746 #define EMIT_SECTION_SYMBOLS 1
747 #endif
749 /* This pass over fixups decides whether symbols can be replaced with
750 section symbols. */
752 static void
753 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
754 asection *sec,
755 PTR xxx ATTRIBUTE_UNUSED)
757 segment_info_type *seginfo = seg_info (sec);
758 fixS *fixp;
760 if (seginfo == NULL)
761 return;
763 dump_section_relocs (abfd, sec, stderr);
765 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
766 if (fixp->fx_done)
767 /* Ignore it. */
769 else if (fixp->fx_addsy)
771 symbolS *sym;
772 asection *symsec;
774 #ifdef DEBUG5
775 fprintf (stderr, "\n\nadjusting fixup:\n");
776 print_fixup (fixp);
777 #endif
779 sym = fixp->fx_addsy;
781 /* All symbols should have already been resolved at this
782 point. It is possible to see unresolved expression
783 symbols, though, since they are not in the regular symbol
784 table. */
785 resolve_symbol_value (sym);
787 if (fixp->fx_subsy != NULL)
788 resolve_symbol_value (fixp->fx_subsy);
790 /* If this symbol is equated to an undefined symbol, convert
791 the fixup to being against that symbol. */
792 if (symbol_equated_reloc_p (sym))
794 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
795 sym = symbol_get_value_expression (sym)->X_add_symbol;
796 fixp->fx_addsy = sym;
799 if (symbol_mri_common_p (sym))
801 /* These symbols are handled specially in fixup_segment. */
802 continue;
805 /* If the symbol is undefined, common, weak, or global (ELF
806 shared libs), we can't replace it with the section symbol. */
807 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
808 continue;
810 /* Is there some other (target cpu dependent) reason we can't adjust
811 this one? (E.g. relocations involving function addresses on
812 the PA. */
813 #ifdef tc_fix_adjustable
814 if (! tc_fix_adjustable (fixp))
815 continue;
816 #endif
818 /* Since we're reducing to section symbols, don't attempt to reduce
819 anything that's already using one. */
820 if (symbol_section_p (sym))
821 continue;
823 symsec = S_GET_SEGMENT (sym);
824 if (symsec == NULL)
825 abort ();
827 if (bfd_is_abs_section (symsec))
829 /* The fixup_segment routine normally will not use this
830 symbol in a relocation. */
831 continue;
834 /* Don't try to reduce relocs which refer to non-local symbols
835 in .linkonce sections. It can lead to confusion when a
836 debugging section refers to a .linkonce section. I hope
837 this will always be correct. */
838 if (symsec != sec && ! S_IS_LOCAL (sym))
840 if ((symsec->flags & SEC_LINK_ONCE) != 0
841 || (IS_ELF
842 /* The GNU toolchain uses an extension for ELF: a
843 section beginning with the magic string
844 .gnu.linkonce is a linkonce section. */
845 && strncmp (segment_name (symsec), ".gnu.linkonce",
846 sizeof ".gnu.linkonce" - 1) == 0))
847 continue;
850 /* Never adjust a reloc against local symbol in a merge section
851 with non-zero addend. */
852 if ((symsec->flags & SEC_MERGE) != 0
853 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
854 continue;
856 /* Never adjust a reloc against TLS local symbol. */
857 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
858 continue;
860 /* We refetch the segment when calling section_symbol, rather
861 than using symsec, because S_GET_VALUE may wind up changing
862 the section when it calls resolve_symbol_value. */
863 fixp->fx_offset += S_GET_VALUE (sym);
864 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
865 #ifdef DEBUG5
866 fprintf (stderr, "\nadjusted fixup:\n");
867 print_fixup (fixp);
868 #endif
871 dump_section_relocs (abfd, sec, stderr);
874 static void
875 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
876 asection *sec,
877 PTR xxx ATTRIBUTE_UNUSED)
879 segment_info_type *seginfo = seg_info (sec);
881 fixup_segment (seginfo->fix_root, sec);
884 static void
885 write_relocs (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
887 segment_info_type *seginfo = seg_info (sec);
888 unsigned int i;
889 unsigned int n;
890 arelent **relocs;
891 fixS *fixp;
892 char *err;
894 /* If seginfo is NULL, we did not create this section; don't do
895 anything with it. */
896 if (seginfo == NULL)
897 return;
899 n = 0;
900 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
901 n++;
903 #ifndef RELOC_EXPANSION_POSSIBLE
904 /* Set up reloc information as well. */
905 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
907 i = 0;
908 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
910 arelent *reloc;
911 bfd_reloc_status_type s;
912 symbolS *sym;
914 if (fixp->fx_done)
916 n--;
917 continue;
920 /* If this is an undefined symbol which was equated to another
921 symbol, then generate the reloc against the latter symbol
922 rather than the former. */
923 sym = fixp->fx_addsy;
924 while (symbol_equated_reloc_p (sym))
926 symbolS *n;
928 /* We must avoid looping, as that can occur with a badly
929 written program. */
930 n = symbol_get_value_expression (sym)->X_add_symbol;
931 if (n == sym)
932 break;
933 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
934 sym = n;
936 fixp->fx_addsy = sym;
938 reloc = tc_gen_reloc (sec, fixp);
939 if (!reloc)
941 n--;
942 continue;
946 This test is triggered inappropriately for the SH:
947 if (fixp->fx_where + fixp->fx_size
948 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
949 abort ();
952 s = bfd_install_relocation (stdoutput, reloc,
953 fixp->fx_frag->fr_literal,
954 fixp->fx_frag->fr_address,
955 sec, &err);
956 switch (s)
958 case bfd_reloc_ok:
959 break;
960 case bfd_reloc_overflow:
961 as_bad_where (fixp->fx_file, fixp->fx_line,
962 _("relocation overflow"));
963 break;
964 case bfd_reloc_outofrange:
965 as_bad_where (fixp->fx_file, fixp->fx_line,
966 _("relocation out of range"));
967 break;
968 default:
969 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
970 fixp->fx_file, fixp->fx_line, s);
972 relocs[i++] = reloc;
974 #else
975 n = n * MAX_RELOC_EXPANSION;
976 /* Set up reloc information as well. */
977 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
979 i = 0;
980 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
982 arelent **reloc;
983 bfd_reloc_status_type s;
984 symbolS *sym;
985 int j;
987 if (fixp->fx_done)
989 n--;
990 continue;
993 /* If this is an undefined symbol which was equated to another
994 symbol, then generate the reloc against the latter symbol
995 rather than the former. */
996 sym = fixp->fx_addsy;
997 while (symbol_equated_reloc_p (sym))
999 symbolS *n;
1001 /* We must avoid looping, as that can occur with a badly
1002 written program. */
1003 n = symbol_get_value_expression (sym)->X_add_symbol;
1004 if (n == sym)
1005 break;
1006 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1007 sym = n;
1009 fixp->fx_addsy = sym;
1011 reloc = tc_gen_reloc (sec, fixp);
1013 for (j = 0; reloc[j]; j++)
1015 relocs[i++] = reloc[j];
1016 assert (i <= n);
1018 if (fixp->fx_where + fixp->fx_size
1019 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1020 as_bad_where (fixp->fx_file, fixp->fx_line,
1021 _("internal error: fixup not contained within frag"));
1022 for (j = 0; reloc[j]; j++)
1024 s = bfd_install_relocation (stdoutput, reloc[j],
1025 fixp->fx_frag->fr_literal,
1026 fixp->fx_frag->fr_address,
1027 sec, &err);
1028 switch (s)
1030 case bfd_reloc_ok:
1031 break;
1032 case bfd_reloc_overflow:
1033 as_bad_where (fixp->fx_file, fixp->fx_line,
1034 _("relocation overflow"));
1035 break;
1036 case bfd_reloc_outofrange:
1037 as_bad_where (fixp->fx_file, fixp->fx_line,
1038 _("relocation out of range"));
1039 break;
1040 default:
1041 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1042 fixp->fx_file, fixp->fx_line, s);
1046 n = i;
1047 #endif
1049 #ifdef DEBUG4
1051 int i, j, nsyms;
1052 asymbol **sympp;
1053 sympp = bfd_get_outsymbols (stdoutput);
1054 nsyms = bfd_get_symcount (stdoutput);
1055 for (i = 0; i < n; i++)
1056 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1058 for (j = 0; j < nsyms; j++)
1059 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1060 break;
1061 if (j == nsyms)
1062 abort ();
1065 #endif
1067 if (n)
1068 bfd_set_reloc (stdoutput, sec, relocs, n);
1069 else
1070 bfd_set_section_flags (abfd, sec,
1071 (bfd_get_section_flags (abfd, sec)
1072 & (flagword) ~SEC_RELOC));
1074 #ifdef SET_SECTION_RELOCS
1075 SET_SECTION_RELOCS (sec, relocs, n);
1076 #endif
1078 #ifdef DEBUG3
1080 int i;
1081 arelent *r;
1082 asymbol *s;
1083 fprintf (stderr, "relocs for sec %s\n", sec->name);
1084 for (i = 0; i < n; i++)
1086 r = relocs[i];
1087 s = *r->sym_ptr_ptr;
1088 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1089 i, r, r->address, s->name, r->addend);
1092 #endif
1095 static void
1096 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1097 asection *sec,
1098 PTR xxx ATTRIBUTE_UNUSED)
1100 segment_info_type *seginfo = seg_info (sec);
1101 addressT offset = 0;
1102 fragS *f;
1104 /* Write out the frags. */
1105 if (seginfo == NULL
1106 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1107 return;
1109 for (f = seginfo->frchainP->frch_root;
1111 f = f->fr_next)
1113 int x;
1114 addressT fill_size;
1115 char *fill_literal;
1116 offsetT count;
1118 assert (f->fr_type == rs_fill);
1119 if (f->fr_fix)
1121 x = bfd_set_section_contents (stdoutput, sec,
1122 f->fr_literal, (file_ptr) offset,
1123 (bfd_size_type) f->fr_fix);
1124 if (!x)
1126 bfd_perror (stdoutput->filename);
1127 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1128 exit (EXIT_FAILURE);
1130 offset += f->fr_fix;
1132 fill_literal = f->fr_literal + f->fr_fix;
1133 fill_size = f->fr_var;
1134 count = f->fr_offset;
1135 assert (count >= 0);
1136 if (fill_size && count)
1138 char buf[256];
1139 if (fill_size > sizeof (buf))
1141 /* Do it the old way. Can this ever happen? */
1142 while (count--)
1144 x = bfd_set_section_contents (stdoutput, sec,
1145 fill_literal,
1146 (file_ptr) offset,
1147 (bfd_size_type) fill_size);
1148 if (!x)
1150 bfd_perror (stdoutput->filename);
1151 as_perror (_("FATAL: Can't write %s"),
1152 stdoutput->filename);
1153 exit (EXIT_FAILURE);
1155 offset += fill_size;
1158 else
1160 /* Build a buffer full of fill objects and output it as
1161 often as necessary. This saves on the overhead of
1162 potentially lots of bfd_set_section_contents calls. */
1163 int n_per_buf, i;
1164 if (fill_size == 1)
1166 n_per_buf = sizeof (buf);
1167 memset (buf, *fill_literal, n_per_buf);
1169 else
1171 char *bufp;
1172 n_per_buf = sizeof (buf) / fill_size;
1173 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1174 memcpy (bufp, fill_literal, fill_size);
1176 for (; count > 0; count -= n_per_buf)
1178 n_per_buf = n_per_buf > count ? count : n_per_buf;
1179 x = bfd_set_section_contents
1180 (stdoutput, sec, buf, (file_ptr) offset,
1181 (bfd_size_type) n_per_buf * fill_size);
1182 if (!x)
1183 as_fatal (_("cannot write to output file"));
1184 offset += n_per_buf * fill_size;
1190 #endif
1192 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1193 static void
1194 merge_data_into_text (void)
1196 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1197 seg_info (text_section)->frchainP->frch_last->fr_next =
1198 seg_info (data_section)->frchainP->frch_root;
1199 seg_info (text_section)->frchainP->frch_last =
1200 seg_info (data_section)->frchainP->frch_last;
1201 seg_info (data_section)->frchainP = 0;
1202 #else
1203 fixS *tmp;
1205 text_last_frag->fr_next = data_frag_root;
1206 text_last_frag = data_last_frag;
1207 data_last_frag = NULL;
1208 data_frag_root = NULL;
1209 if (text_fix_root)
1211 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1212 tmp->fx_next = data_fix_root;
1213 text_fix_tail = data_fix_tail;
1215 else
1216 text_fix_root = data_fix_root;
1217 data_fix_root = NULL;
1218 #endif
1220 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1222 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1223 static void
1224 relax_and_size_all_segments ()
1226 fragS *fragP;
1228 relax_segment (text_frag_root, SEG_TEXT);
1229 relax_segment (data_frag_root, SEG_DATA);
1230 relax_segment (bss_frag_root, SEG_BSS);
1232 /* Now the addresses of frags are correct within the segment. */
1233 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1234 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1235 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1237 /* Join the 2 segments into 1 huge segment.
1238 To do this, re-compute every rn_address in the SEG_DATA frags.
1239 Then join the data frags after the text frags.
1241 Determine a_data [length of data segment]. */
1242 if (data_frag_root)
1244 register relax_addressT slide;
1246 know ((text_last_frag->fr_type == rs_fill)
1247 && (text_last_frag->fr_offset == 0));
1249 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1250 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1251 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1252 #ifdef OBJ_BOUT
1253 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1254 /* For b.out: If the data section has a strict alignment
1255 requirement, its load address in the .o file will be
1256 rounded up from the size of the text section. These
1257 two values are *not* the same! Similarly for the bss
1258 section.... */
1259 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1260 #endif
1262 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1263 fragP->fr_address += slide;
1265 know (text_last_frag != 0);
1266 text_last_frag->fr_next = data_frag_root;
1268 else
1270 H_SET_DATA_SIZE (&headers, 0);
1273 #ifdef OBJ_BOUT
1274 /* See above comments on b.out data section address. */
1276 addressT bss_vma;
1277 if (data_last_frag == 0)
1278 bss_vma = H_GET_TEXT_SIZE (&headers);
1279 else
1280 bss_vma = data_last_frag->fr_address;
1281 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1282 bss_address_frag.fr_address = bss_vma;
1284 #else /* ! OBJ_BOUT */
1285 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1286 H_GET_DATA_SIZE (&headers));
1288 #endif /* ! OBJ_BOUT */
1290 /* Slide all the frags. */
1291 if (bss_frag_root)
1293 relax_addressT slide = bss_address_frag.fr_address;
1295 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1296 fragP->fr_address += slide;
1299 if (bss_last_frag)
1300 H_SET_BSS_SIZE (&headers,
1301 bss_last_frag->fr_address - bss_frag_root->fr_address);
1302 else
1303 H_SET_BSS_SIZE (&headers, 0);
1305 #endif /* ! BFD_ASSEMBLER && ! BFD */
1307 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1309 #ifdef BFD_ASSEMBLER
1310 static void
1311 set_symtab (void)
1313 int nsyms;
1314 asymbol **asympp;
1315 symbolS *symp;
1316 bfd_boolean result;
1317 extern PTR bfd_alloc (bfd *, bfd_size_type);
1319 /* Count symbols. We can't rely on a count made by the loop in
1320 write_object_file, because *_frob_file may add a new symbol or
1321 two. */
1322 nsyms = 0;
1323 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1324 nsyms++;
1326 if (nsyms)
1328 int i;
1329 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1331 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1332 symp = symbol_rootP;
1333 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1335 asympp[i] = symbol_get_bfdsym (symp);
1336 symbol_mark_written (symp);
1339 else
1340 asympp = 0;
1341 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1342 assert (result);
1343 symbol_table_frozen = 1;
1345 #endif
1347 /* Finish the subsegments. After every sub-segment, we fake an
1348 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1349 ".fill 0" because that is the kind of frag that requires least
1350 thought. ".align" frags like to have a following frag since that
1351 makes calculating their intended length trivial. */
1353 #ifndef SUB_SEGMENT_ALIGN
1354 #ifdef HANDLE_ALIGN
1355 /* The last subsegment gets an alignment corresponding to the alignment
1356 of the section. This allows proper nop-filling at the end of
1357 code-bearing sections. */
1358 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1359 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1360 ? get_recorded_alignment (SEG) : 0)
1361 #else
1362 #ifdef BFD_ASSEMBLER
1363 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1364 #else
1365 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1366 #endif
1367 #endif
1368 #endif
1370 void
1371 subsegs_finish (void)
1373 struct frchain *frchainP;
1375 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1377 int alignment = 0;
1379 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1381 /* This now gets called even if we had errors. In that case,
1382 any alignment is meaningless, and, moreover, will look weird
1383 if we are generating a listing. */
1384 if (!had_errors ())
1386 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1387 #ifdef BFD_ASSEMBLER
1388 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1389 && now_seg->entsize)
1391 unsigned int entsize = now_seg->entsize;
1392 int entalign = 0;
1394 while ((entsize & 1) == 0)
1396 ++entalign;
1397 entsize >>= 1;
1399 if (entalign > alignment)
1400 alignment = entalign;
1402 #endif
1405 if (subseg_text_p (now_seg))
1406 frag_align_code (alignment, 0);
1407 else
1408 frag_align (alignment, 0, 0);
1410 /* frag_align will have left a new frag.
1411 Use this last frag for an empty ".fill".
1413 For this segment ...
1414 Create a last frag. Do not leave a "being filled in frag". */
1415 frag_wane (frag_now);
1416 frag_now->fr_fix = 0;
1417 know (frag_now->fr_next == NULL);
1421 /* Write the object file. */
1423 void
1424 write_object_file (void)
1426 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1427 fragS *fragP; /* Track along all frags. */
1428 #endif
1430 /* Do we really want to write it? */
1432 int n_warns, n_errs;
1433 n_warns = had_warnings ();
1434 n_errs = had_errors ();
1435 /* The -Z flag indicates that an object file should be generated,
1436 regardless of warnings and errors. */
1437 if (flag_always_generate_output)
1439 if (n_warns || n_errs)
1440 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1441 n_errs, n_errs == 1 ? "" : "s",
1442 n_warns, n_warns == 1 ? "" : "s");
1444 else
1446 if (n_errs)
1447 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1448 n_errs, n_errs == 1 ? "" : "s",
1449 n_warns, n_warns == 1 ? "" : "s");
1453 #ifdef OBJ_VMS
1454 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1455 a routine to check for the definition of the procedure "_main",
1456 and if so -- fix it up so that it can be program entry point. */
1457 vms_check_for_main ();
1458 #endif /* OBJ_VMS */
1460 /* From now on, we don't care about sub-segments. Build one frag chain
1461 for each segment. Linked thru fr_next. */
1463 #ifdef BFD_ASSEMBLER
1464 /* Remove the sections created by gas for its own purposes. */
1466 asection **seclist;
1467 int i;
1469 seclist = &stdoutput->sections;
1470 while (*seclist)
1472 if (*seclist == reg_section || *seclist == expr_section)
1474 bfd_section_list_remove (stdoutput, seclist);
1475 stdoutput->section_count--;
1477 else
1478 seclist = &(*seclist)->next;
1480 i = 0;
1481 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1484 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1485 #else
1486 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1487 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1488 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1489 #endif
1491 /* We have two segments. If user gave -R flag, then we must put the
1492 data frags into the text segment. Do this before relaxing so
1493 we know to take advantage of -R and make shorter addresses. */
1494 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1495 if (flag_readonly_data_in_text)
1497 merge_data_into_text ();
1499 #endif
1501 #ifdef BFD_ASSEMBLER
1502 while (1)
1504 int changed;
1506 #ifndef WORKING_DOT_WORD
1507 /* We need to reset the markers in the broken word list and
1508 associated frags between calls to relax_segment (via
1509 relax_seg). Since the broken word list is global, we do it
1510 once per round, rather than locally in relax_segment for each
1511 segment. */
1512 struct broken_word *brokp;
1514 for (brokp = broken_words;
1515 brokp != (struct broken_word *) NULL;
1516 brokp = brokp->next_broken_word)
1518 brokp->added = 0;
1520 if (brokp->dispfrag != (fragS *) NULL
1521 && brokp->dispfrag->fr_type == rs_broken_word)
1522 brokp->dispfrag->fr_subtype = 0;
1524 #endif
1526 changed = 0;
1527 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1528 if (!changed)
1529 break;
1532 /* Note - Most ports will use the default value of
1533 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1534 local symbols to be resolved, removing their frag information.
1535 Some ports however, will not have finished relaxing all of
1536 their frags and will still need the local symbol frag
1537 information. These ports can set
1538 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1539 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1541 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1542 #else
1543 relax_and_size_all_segments ();
1544 #endif /* BFD_ASSEMBLER */
1546 /* Relaxation has completed. Freeze all syms. */
1547 finalize_syms = 1;
1549 #ifdef md_post_relax_hook
1550 md_post_relax_hook;
1551 #endif
1553 #ifndef BFD_ASSEMBLER
1554 /* Crawl the symbol chain.
1556 For each symbol whose value depends on a frag, take the address of
1557 that frag and subsume it into the value of the symbol.
1558 After this, there is just one way to lookup a symbol value.
1559 Values are left in their final state for object file emission.
1560 We adjust the values of 'L' local symbols, even if we do
1561 not intend to emit them to the object file, because their values
1562 are needed for fix-ups.
1564 Unless we saw a -L flag, remove all symbols that begin with 'L'
1565 from the symbol chain. (They are still pointed to by the fixes.)
1567 Count the remaining symbols.
1568 Assign a symbol number to each symbol.
1569 Count the number of string-table chars we will emit.
1570 Put this info into the headers as appropriate. */
1571 know (zero_address_frag.fr_address == 0);
1572 string_byte_count = sizeof (string_byte_count);
1574 obj_crawl_symbol_chain (&headers);
1576 if (string_byte_count == sizeof (string_byte_count))
1577 string_byte_count = 0;
1579 H_SET_STRING_SIZE (&headers, string_byte_count);
1581 /* Addresses of frags now reflect addresses we use in the object file.
1582 Symbol values are correct.
1583 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1584 Also converting any machine-dependent frags using md_convert_frag(); */
1585 subseg_change (SEG_TEXT, 0);
1587 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1589 /* At this point we have linked all the frags into a single
1590 chain. However, cvt_frag_to_fill may call md_convert_frag
1591 which may call fix_new. We need to ensure that fix_new adds
1592 the fixup to the right section. */
1593 if (fragP == data_frag_root)
1594 subseg_change (SEG_DATA, 0);
1596 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1598 /* Some assert macros don't work with # directives mixed in. */
1599 #ifndef NDEBUG
1600 if (!(fragP->fr_next == NULL
1601 #ifdef OBJ_BOUT
1602 || fragP->fr_next == data_frag_root
1603 #endif
1604 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
1605 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1606 abort ();
1607 #endif
1609 #endif /* ! BFD_ASSEMBLER */
1611 #ifndef WORKING_DOT_WORD
1613 struct broken_word *lie;
1614 struct broken_word **prevP;
1616 prevP = &broken_words;
1617 for (lie = broken_words; lie; lie = lie->next_broken_word)
1618 if (!lie->added)
1620 expressionS exp;
1622 subseg_change (lie->seg, lie->subseg);
1623 exp.X_op = O_subtract;
1624 exp.X_add_symbol = lie->add;
1625 exp.X_op_symbol = lie->sub;
1626 exp.X_add_number = lie->addnum;
1627 #ifdef BFD_ASSEMBLER
1628 #ifdef TC_CONS_FIX_NEW
1629 TC_CONS_FIX_NEW (lie->frag,
1630 lie->word_goes_here - lie->frag->fr_literal,
1631 2, &exp);
1632 #else
1633 fix_new_exp (lie->frag,
1634 lie->word_goes_here - lie->frag->fr_literal,
1635 2, &exp, 0, BFD_RELOC_16);
1636 #endif
1637 #else
1638 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1639 fix_new_exp (lie->frag,
1640 lie->word_goes_here - lie->frag->fr_literal,
1641 2, &exp, 0, NO_RELOC);
1642 #else
1643 #ifdef TC_NS32K
1644 fix_new_ns32k_exp (lie->frag,
1645 lie->word_goes_here - lie->frag->fr_literal,
1646 2, &exp, 0, 0, 2, 0, 0);
1647 #else
1648 fix_new_exp (lie->frag,
1649 lie->word_goes_here - lie->frag->fr_literal,
1650 2, &exp, 0, 0);
1651 #endif /* TC_NS32K */
1652 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1653 #endif /* BFD_ASSEMBLER */
1654 *prevP = lie->next_broken_word;
1656 else
1657 prevP = &(lie->next_broken_word);
1659 for (lie = broken_words; lie;)
1661 struct broken_word *untruth;
1662 char *table_ptr;
1663 addressT table_addr;
1664 addressT from_addr, to_addr;
1665 int n, m;
1667 subseg_change (lie->seg, lie->subseg);
1668 fragP = lie->dispfrag;
1670 /* Find out how many broken_words go here. */
1671 n = 0;
1672 for (untruth = lie;
1673 untruth && untruth->dispfrag == fragP;
1674 untruth = untruth->next_broken_word)
1675 if (untruth->added == 1)
1676 n++;
1678 table_ptr = lie->dispfrag->fr_opcode;
1679 table_addr = (lie->dispfrag->fr_address
1680 + (table_ptr - lie->dispfrag->fr_literal));
1681 /* Create the jump around the long jumps. This is a short
1682 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1683 from_addr = table_addr;
1684 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1685 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1686 lie->add);
1687 table_ptr += md_short_jump_size;
1688 table_addr += md_short_jump_size;
1690 for (m = 0;
1691 lie && lie->dispfrag == fragP;
1692 m++, lie = lie->next_broken_word)
1694 if (lie->added == 2)
1695 continue;
1696 /* Patch the jump table. */
1697 /* This is the offset from ??? to table_ptr+0. */
1698 to_addr = table_addr - S_GET_VALUE (lie->sub);
1699 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1700 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1701 #endif
1702 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1703 for (untruth = lie->next_broken_word;
1704 untruth && untruth->dispfrag == fragP;
1705 untruth = untruth->next_broken_word)
1707 if (untruth->use_jump == lie)
1708 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1711 /* Install the long jump. */
1712 /* This is a long jump from table_ptr+0 to the final target. */
1713 from_addr = table_addr;
1714 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1715 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1716 lie->add);
1717 table_ptr += md_long_jump_size;
1718 table_addr += md_long_jump_size;
1722 #endif /* not WORKING_DOT_WORD */
1724 #ifndef BFD_ASSEMBLER
1725 #ifndef OBJ_VMS
1726 { /* not vms */
1727 char *the_object_file;
1728 long object_file_size;
1729 /* Scan every FixS performing fixups. We had to wait until now to
1730 do this because md_convert_frag() may have made some fixSs. */
1731 int trsize, drsize;
1733 subseg_change (SEG_TEXT, 0);
1734 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1735 subseg_change (SEG_DATA, 0);
1736 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1737 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1739 /* FIXME: Move this stuff into the pre-write-hook. */
1740 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1741 H_SET_ENTRY_POINT (&headers, 0);
1743 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1745 object_file_size = H_GET_FILE_SIZE (&headers);
1746 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1748 output_file_create (out_file_name);
1750 obj_header_append (&next_object_file_charP, &headers);
1752 know ((next_object_file_charP - the_object_file)
1753 == H_GET_HEADER_SIZE (&headers));
1755 /* Emit code. */
1756 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1758 register long count;
1759 register char *fill_literal;
1760 register long fill_size;
1762 PROGRESS (1);
1763 know (fragP->fr_type == rs_fill);
1764 append (&next_object_file_charP, fragP->fr_literal,
1765 (unsigned long) fragP->fr_fix);
1766 fill_literal = fragP->fr_literal + fragP->fr_fix;
1767 fill_size = fragP->fr_var;
1768 know (fragP->fr_offset >= 0);
1770 for (count = fragP->fr_offset; count; count--)
1771 append (&next_object_file_charP, fill_literal,
1772 (unsigned long) fill_size);
1775 know ((next_object_file_charP - the_object_file)
1776 == (H_GET_HEADER_SIZE (&headers)
1777 + H_GET_TEXT_SIZE (&headers)
1778 + H_GET_DATA_SIZE (&headers)));
1780 /* Emit relocations. */
1781 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1782 (relax_addressT) 0);
1783 know ((next_object_file_charP - the_object_file)
1784 == (H_GET_HEADER_SIZE (&headers)
1785 + H_GET_TEXT_SIZE (&headers)
1786 + H_GET_DATA_SIZE (&headers)
1787 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1788 #ifdef TC_I960
1789 /* Make addresses in data relocation directives relative to beginning of
1790 first data fragment, not end of last text fragment: alignment of the
1791 start of the data segment may place a gap between the segments. */
1792 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1793 data0_frchainP->frch_root->fr_address);
1794 #else /* TC_I960 */
1795 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1796 text_last_frag->fr_address);
1797 #endif /* TC_I960 */
1799 know ((next_object_file_charP - the_object_file)
1800 == (H_GET_HEADER_SIZE (&headers)
1801 + H_GET_TEXT_SIZE (&headers)
1802 + H_GET_DATA_SIZE (&headers)
1803 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1804 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1806 /* Emit line number entries. */
1807 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1808 know ((next_object_file_charP - the_object_file)
1809 == (H_GET_HEADER_SIZE (&headers)
1810 + H_GET_TEXT_SIZE (&headers)
1811 + H_GET_DATA_SIZE (&headers)
1812 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1813 + H_GET_DATA_RELOCATION_SIZE (&headers)
1814 + H_GET_LINENO_SIZE (&headers)));
1816 /* Emit symbols. */
1817 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1818 know ((next_object_file_charP - the_object_file)
1819 == (H_GET_HEADER_SIZE (&headers)
1820 + H_GET_TEXT_SIZE (&headers)
1821 + H_GET_DATA_SIZE (&headers)
1822 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1823 + H_GET_DATA_RELOCATION_SIZE (&headers)
1824 + H_GET_LINENO_SIZE (&headers)
1825 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1827 /* Emit strings. */
1828 if (string_byte_count > 0)
1829 obj_emit_strings (&next_object_file_charP);
1831 #ifdef BFD_HEADERS
1832 bfd_seek (stdoutput, (file_ptr) 0, 0);
1833 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1834 #else
1836 /* Write the data to the file. */
1837 output_file_append (the_object_file, object_file_size, out_file_name);
1838 free (the_object_file);
1839 #endif
1841 #else /* OBJ_VMS */
1842 /* Now do the VMS-dependent part of writing the object file. */
1843 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1844 H_GET_DATA_SIZE (&headers),
1845 H_GET_BSS_SIZE (&headers),
1846 text_frag_root, data_frag_root);
1847 #endif /* OBJ_VMS */
1848 #else /* BFD_ASSEMBLER */
1850 /* Resolve symbol values. This needs to be done before processing
1851 the relocations. */
1852 if (symbol_rootP)
1854 symbolS *symp;
1856 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1857 resolve_symbol_value (symp);
1859 resolve_local_symbol_values ();
1861 PROGRESS (1);
1863 #ifdef tc_frob_file_before_adjust
1864 tc_frob_file_before_adjust ();
1865 #endif
1866 #ifdef obj_frob_file_before_adjust
1867 obj_frob_file_before_adjust ();
1868 #endif
1870 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1872 #ifdef tc_frob_file_before_fix
1873 tc_frob_file_before_fix ();
1874 #endif
1875 #ifdef obj_frob_file_before_fix
1876 obj_frob_file_before_fix ();
1877 #endif
1879 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1881 /* Set up symbol table, and write it out. */
1882 if (symbol_rootP)
1884 symbolS *symp;
1886 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1888 int punt = 0;
1889 const char *name;
1891 if (symbol_mri_common_p (symp))
1893 if (S_IS_EXTERNAL (symp))
1894 as_bad (_("%s: global symbols not supported in common sections"),
1895 S_GET_NAME (symp));
1896 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1897 continue;
1900 name = S_GET_NAME (symp);
1901 if (name)
1903 const char *name2 =
1904 decode_local_label_name ((char *) S_GET_NAME (symp));
1905 /* They only differ if `name' is a fb or dollar local
1906 label name. */
1907 if (name2 != name && ! S_IS_DEFINED (symp))
1908 as_bad (_("local label `%s' is not defined"), name2);
1911 /* Do it again, because adjust_reloc_syms might introduce
1912 more symbols. They'll probably only be section symbols,
1913 but they'll still need to have the values computed. */
1914 resolve_symbol_value (symp);
1916 /* Skip symbols which were equated to undefined or common
1917 symbols. */
1918 if (symbol_equated_reloc_p (symp))
1920 if (S_IS_COMMON (symp))
1921 as_bad (_("`%s' can't be equated to common symbol"),
1922 S_GET_NAME (symp));
1923 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1924 continue;
1927 /* So far, common symbols have been treated like undefined symbols.
1928 Put them in the common section now. */
1929 if (S_IS_DEFINED (symp) == 0
1930 && S_GET_VALUE (symp) != 0)
1931 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1933 #ifdef obj_frob_symbol
1934 obj_frob_symbol (symp, punt);
1935 #endif
1936 #ifdef tc_frob_symbol
1937 if (! punt || symbol_used_in_reloc_p (symp))
1938 tc_frob_symbol (symp, punt);
1939 #endif
1941 /* If we don't want to keep this symbol, splice it out of
1942 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1943 want section symbols. Otherwise, we skip local symbols
1944 and symbols that the frob_symbol macros told us to punt,
1945 but we keep such symbols if they are used in relocs. */
1946 if (symp == abs_section_sym
1947 || (! EMIT_SECTION_SYMBOLS
1948 && symbol_section_p (symp))
1949 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1950 opposites. Sometimes the former checks flags and the
1951 latter examines the name... */
1952 || (!S_IS_EXTERN (symp)
1953 && (punt || S_IS_LOCAL (symp))
1954 && ! symbol_used_in_reloc_p (symp)))
1956 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1958 /* After symbol_remove, symbol_next(symp) still returns
1959 the one that came after it in the chain. So we don't
1960 need to do any extra cleanup work here. */
1961 continue;
1964 /* Make sure we really got a value for the symbol. */
1965 if (! symbol_resolved_p (symp))
1967 as_bad (_("can't resolve value for symbol `%s'"),
1968 S_GET_NAME (symp));
1969 symbol_mark_resolved (symp);
1972 /* Set the value into the BFD symbol. Up til now the value
1973 has only been kept in the gas symbolS struct. */
1974 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1978 PROGRESS (1);
1980 /* Now do any format-specific adjustments to the symbol table, such
1981 as adding file symbols. */
1982 #ifdef tc_adjust_symtab
1983 tc_adjust_symtab ();
1984 #endif
1985 #ifdef obj_adjust_symtab
1986 obj_adjust_symtab ();
1987 #endif
1989 /* Now that all the sizes are known, and contents correct, we can
1990 start writing to the file. */
1991 set_symtab ();
1993 /* If *_frob_file changes the symbol value at this point, it is
1994 responsible for moving the changed value into symp->bsym->value
1995 as well. Hopefully all symbol value changing can be done in
1996 *_frob_symbol. */
1997 #ifdef tc_frob_file
1998 tc_frob_file ();
1999 #endif
2000 #ifdef obj_frob_file
2001 obj_frob_file ();
2002 #endif
2004 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2006 #ifdef tc_frob_file_after_relocs
2007 tc_frob_file_after_relocs ();
2008 #endif
2009 #ifdef obj_frob_file_after_relocs
2010 obj_frob_file_after_relocs ();
2011 #endif
2013 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2014 #endif /* BFD_ASSEMBLER */
2016 #endif /* ! BFD */
2018 #ifdef TC_GENERIC_RELAX_TABLE
2020 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2022 long
2023 relax_frag (segT segment, fragS *fragP, long stretch)
2025 const relax_typeS *this_type;
2026 const relax_typeS *start_type;
2027 relax_substateT next_state;
2028 relax_substateT this_state;
2029 offsetT growth;
2030 offsetT aim;
2031 addressT target;
2032 addressT address;
2033 symbolS *symbolP;
2034 const relax_typeS *table;
2036 target = fragP->fr_offset;
2037 address = fragP->fr_address;
2038 table = TC_GENERIC_RELAX_TABLE;
2039 this_state = fragP->fr_subtype;
2040 start_type = this_type = table + this_state;
2041 symbolP = fragP->fr_symbol;
2043 if (symbolP)
2045 fragS *sym_frag;
2047 sym_frag = symbol_get_frag (symbolP);
2049 #ifndef DIFF_EXPR_OK
2050 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2051 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2052 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2053 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2054 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2055 #endif
2056 know (sym_frag != NULL);
2057 #endif
2058 know (S_GET_SEGMENT (symbolP) != absolute_section
2059 || sym_frag == &zero_address_frag);
2060 target += S_GET_VALUE (symbolP);
2062 /* If frag has yet to be reached on this pass,
2063 assume it will move by STRETCH just as we did.
2064 If this is not so, it will be because some frag
2065 between grows, and that will force another pass. */
2067 if (stretch != 0
2068 && sym_frag->relax_marker != fragP->relax_marker
2069 && S_GET_SEGMENT (symbolP) == segment)
2071 target += stretch;
2075 aim = target - address - fragP->fr_fix;
2076 #ifdef TC_PCREL_ADJUST
2077 /* Currently only the ns32k family needs this. */
2078 aim += TC_PCREL_ADJUST (fragP);
2079 #endif
2081 #ifdef md_prepare_relax_scan
2082 /* Formerly called M68K_AIM_KLUDGE. */
2083 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2084 #endif
2086 if (aim < 0)
2088 /* Look backwards. */
2089 for (next_state = this_type->rlx_more; next_state;)
2090 if (aim >= this_type->rlx_backward)
2091 next_state = 0;
2092 else
2094 /* Grow to next state. */
2095 this_state = next_state;
2096 this_type = table + this_state;
2097 next_state = this_type->rlx_more;
2100 else
2102 /* Look forwards. */
2103 for (next_state = this_type->rlx_more; next_state;)
2104 if (aim <= this_type->rlx_forward)
2105 next_state = 0;
2106 else
2108 /* Grow to next state. */
2109 this_state = next_state;
2110 this_type = table + this_state;
2111 next_state = this_type->rlx_more;
2115 growth = this_type->rlx_length - start_type->rlx_length;
2116 if (growth != 0)
2117 fragP->fr_subtype = this_state;
2118 return growth;
2121 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2123 /* Relax_align. Advance location counter to next address that has 'alignment'
2124 lowest order bits all 0s, return size of adjustment made. */
2125 static relax_addressT
2126 relax_align (register relax_addressT address, /* Address now. */
2127 register int alignment /* Alignment (binary). */)
2129 relax_addressT mask;
2130 relax_addressT new_address;
2132 mask = ~((~0) << alignment);
2133 new_address = (address + mask) & (~mask);
2134 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2135 if (linkrelax)
2136 /* We must provide lots of padding, so the linker can discard it
2137 when needed. The linker will not add extra space, ever. */
2138 new_address += (1 << alignment);
2139 #endif
2140 return (new_address - address);
2143 /* Now we have a segment, not a crowd of sub-segments, we can make
2144 fr_address values.
2146 Relax the frags.
2148 After this, all frags in this segment have addresses that are correct
2149 within the segment. Since segments live in different file addresses,
2150 these frag addresses may not be the same as final object-file
2151 addresses. */
2154 relax_segment (struct frag *segment_frag_root, segT segment)
2156 register struct frag *fragP;
2157 register relax_addressT address;
2158 int ret;
2160 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2161 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2162 #endif
2163 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2164 subseg_change (segment, 0);
2166 /* For each frag in segment: count and store (a 1st guess of)
2167 fr_address. */
2168 address = 0;
2169 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2171 fragP->relax_marker = 0;
2172 fragP->fr_address = address;
2173 address += fragP->fr_fix;
2175 switch (fragP->fr_type)
2177 case rs_fill:
2178 address += fragP->fr_offset * fragP->fr_var;
2179 break;
2181 case rs_align:
2182 case rs_align_code:
2183 case rs_align_test:
2185 addressT offset = relax_align (address, (int) fragP->fr_offset);
2187 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2188 offset = 0;
2190 if (offset % fragP->fr_var != 0)
2192 as_bad_where (fragP->fr_file, fragP->fr_line,
2193 _("alignment padding (%lu bytes) not a multiple of %ld"),
2194 (unsigned long) offset, (long) fragP->fr_var);
2195 offset -= (offset % fragP->fr_var);
2198 address += offset;
2200 break;
2202 case rs_org:
2203 case rs_space:
2204 /* Assume .org is nugatory. It will grow with 1st relax. */
2205 break;
2207 case rs_machine_dependent:
2208 /* If fr_symbol is an expression, this call to
2209 resolve_symbol_value sets up the correct segment, which will
2210 likely be needed in md_estimate_size_before_relax. */
2211 if (fragP->fr_symbol)
2212 resolve_symbol_value (fragP->fr_symbol);
2214 address += md_estimate_size_before_relax (fragP, segment);
2215 break;
2217 #ifndef WORKING_DOT_WORD
2218 /* Broken words don't concern us yet. */
2219 case rs_broken_word:
2220 break;
2221 #endif
2223 case rs_leb128:
2224 /* Initial guess is always 1; doing otherwise can result in
2225 stable solutions that are larger than the minimum. */
2226 address += fragP->fr_offset = 1;
2227 break;
2229 case rs_cfa:
2230 address += eh_frame_estimate_size_before_relax (fragP);
2231 break;
2233 case rs_dwarf2dbg:
2234 address += dwarf2dbg_estimate_size_before_relax (fragP);
2235 break;
2237 default:
2238 BAD_CASE (fragP->fr_type);
2239 break;
2243 /* Do relax(). */
2245 offsetT stretch; /* May be any size, 0 or negative. */
2246 /* Cumulative number of addresses we have relaxed this pass.
2247 We may have relaxed more than one address. */
2248 int stretched; /* Have we stretched on this pass? */
2249 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2250 grew, and another shrank. If a branch instruction doesn't fit anymore,
2251 we could be scrod. */
2255 stretch = 0;
2256 stretched = 0;
2258 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2260 offsetT growth = 0;
2261 addressT was_address;
2262 offsetT offset;
2263 symbolS *symbolP;
2265 fragP->relax_marker ^= 1;
2266 was_address = fragP->fr_address;
2267 address = fragP->fr_address += stretch;
2268 symbolP = fragP->fr_symbol;
2269 offset = fragP->fr_offset;
2271 switch (fragP->fr_type)
2273 case rs_fill: /* .fill never relaxes. */
2274 growth = 0;
2275 break;
2277 #ifndef WORKING_DOT_WORD
2278 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2279 for it I do not want to write it. I do not want to have
2280 anything to do with it. This is not the proper way to
2281 implement this misfeature. */
2282 case rs_broken_word:
2284 struct broken_word *lie;
2285 struct broken_word *untruth;
2287 /* Yes this is ugly (storing the broken_word pointer
2288 in the symbol slot). Still, this whole chunk of
2289 code is ugly, and I don't feel like doing anything
2290 about it. Think of it as stubbornness in action. */
2291 growth = 0;
2292 for (lie = (struct broken_word *) (fragP->fr_symbol);
2293 lie && lie->dispfrag == fragP;
2294 lie = lie->next_broken_word)
2297 if (lie->added)
2298 continue;
2300 offset = (S_GET_VALUE (lie->add)
2301 + lie->addnum
2302 - S_GET_VALUE (lie->sub));
2303 if (offset <= -32768 || offset >= 32767)
2305 if (flag_warn_displacement)
2307 char buf[50];
2308 sprint_value (buf, (addressT) lie->addnum);
2309 as_warn_where (fragP->fr_file, fragP->fr_line,
2310 _(".word %s-%s+%s didn't fit"),
2311 S_GET_NAME (lie->add),
2312 S_GET_NAME (lie->sub),
2313 buf);
2315 lie->added = 1;
2316 if (fragP->fr_subtype == 0)
2318 fragP->fr_subtype++;
2319 growth += md_short_jump_size;
2321 for (untruth = lie->next_broken_word;
2322 untruth && untruth->dispfrag == lie->dispfrag;
2323 untruth = untruth->next_broken_word)
2324 if ((symbol_get_frag (untruth->add)
2325 == symbol_get_frag (lie->add))
2326 && (S_GET_VALUE (untruth->add)
2327 == S_GET_VALUE (lie->add)))
2329 untruth->added = 2;
2330 untruth->use_jump = lie;
2332 growth += md_long_jump_size;
2336 break;
2337 } /* case rs_broken_word */
2338 #endif
2339 case rs_align:
2340 case rs_align_code:
2341 case rs_align_test:
2343 addressT oldoff, newoff;
2345 oldoff = relax_align (was_address + fragP->fr_fix,
2346 (int) offset);
2347 newoff = relax_align (address + fragP->fr_fix,
2348 (int) offset);
2350 if (fragP->fr_subtype != 0)
2352 if (oldoff > fragP->fr_subtype)
2353 oldoff = 0;
2354 if (newoff > fragP->fr_subtype)
2355 newoff = 0;
2358 growth = newoff - oldoff;
2360 break;
2362 case rs_org:
2364 addressT target = offset;
2365 addressT after;
2367 if (symbolP)
2369 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2370 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2371 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2372 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2373 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2374 know (symbolP->sy_frag);
2375 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2376 || (symbolP->sy_frag == &zero_address_frag));
2377 #endif
2378 /* Convert from an actual address to an octet offset
2379 into the section. Here it is assumed that the
2380 section's VMA is zero, and can omit subtracting it
2381 from the symbol's value to get the address offset. */
2382 know (S_GET_SECTION (symbolP)->vma == 0);
2383 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2386 know (fragP->fr_next);
2387 after = fragP->fr_next->fr_address;
2388 growth = target - after;
2389 if (growth < 0)
2391 /* Growth may be negative, but variable part of frag
2392 cannot have fewer than 0 chars. That is, we can't
2393 .org backwards. */
2394 as_bad_where (fragP->fr_file, fragP->fr_line,
2395 _("attempt to move .org backwards"));
2397 /* We've issued an error message. Change the
2398 frag to avoid cascading errors. */
2399 fragP->fr_type = rs_align;
2400 fragP->fr_subtype = 0;
2401 fragP->fr_offset = 0;
2402 fragP->fr_fix = after - was_address;
2403 growth = stretch;
2406 /* This is an absolute growth factor */
2407 growth -= stretch;
2408 break;
2411 case rs_space:
2412 growth = 0;
2413 if (symbolP)
2415 offsetT amount;
2417 amount = S_GET_VALUE (symbolP);
2418 if (S_GET_SEGMENT (symbolP) != absolute_section
2419 || S_IS_COMMON (symbolP)
2420 || ! S_IS_DEFINED (symbolP))
2422 as_bad_where (fragP->fr_file, fragP->fr_line,
2423 _(".space specifies non-absolute value"));
2424 /* Prevent repeat of this error message. */
2425 fragP->fr_symbol = 0;
2427 else if (amount < 0)
2429 as_warn_where (fragP->fr_file, fragP->fr_line,
2430 _(".space or .fill with negative value, ignored"));
2431 fragP->fr_symbol = 0;
2433 else
2434 growth = (was_address + fragP->fr_fix + amount
2435 - fragP->fr_next->fr_address);
2437 break;
2439 case rs_machine_dependent:
2440 #ifdef md_relax_frag
2441 growth = md_relax_frag (segment, fragP, stretch);
2442 #else
2443 #ifdef TC_GENERIC_RELAX_TABLE
2444 /* The default way to relax a frag is to look through
2445 TC_GENERIC_RELAX_TABLE. */
2446 growth = relax_frag (segment, fragP, stretch);
2447 #endif /* TC_GENERIC_RELAX_TABLE */
2448 #endif
2449 break;
2451 case rs_leb128:
2453 valueT value;
2454 offsetT size;
2456 value = resolve_symbol_value (fragP->fr_symbol);
2457 size = sizeof_leb128 (value, fragP->fr_subtype);
2458 growth = size - fragP->fr_offset;
2459 fragP->fr_offset = size;
2461 break;
2463 case rs_cfa:
2464 growth = eh_frame_relax_frag (fragP);
2465 break;
2467 case rs_dwarf2dbg:
2468 growth = dwarf2dbg_relax_frag (fragP);
2469 break;
2471 default:
2472 BAD_CASE (fragP->fr_type);
2473 break;
2475 if (growth)
2477 stretch += growth;
2478 stretched = 1;
2480 } /* For each frag in the segment. */
2482 while (stretched); /* Until nothing further to relax. */
2483 } /* do_relax */
2485 ret = 0;
2486 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2487 if (fragP->last_fr_address != fragP->fr_address)
2489 fragP->last_fr_address = fragP->fr_address;
2490 ret = 1;
2492 return ret;
2495 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2497 /* fixup_segment()
2499 Go through all the fixS's in a segment and see which ones can be
2500 handled now. (These consist of fixS where we have since discovered
2501 the value of a symbol, or the address of the frag involved.)
2502 For each one, call md_apply_fix3 to put the fix into the frag data.
2504 Result is a count of how many relocation structs will be needed to
2505 handle the remaining fixS's that we couldn't completely handle here.
2506 These will be output later by emit_relocations(). */
2508 static long
2509 fixup_segment (fixS *fixP, segT this_segment)
2511 long seg_reloc_count = 0;
2512 valueT add_number;
2513 fragS *fragP;
2514 segT add_symbol_segment = absolute_section;
2516 if (fixP != NULL && abs_section_sym == NULL)
2518 #ifndef BFD_ASSEMBLER
2519 abs_section_sym = &abs_symbol;
2520 #else
2521 abs_section_sym = section_symbol (absolute_section);
2522 #endif
2525 /* If the linker is doing the relaxing, we must not do any fixups.
2527 Well, strictly speaking that's not true -- we could do any that
2528 are PC-relative and don't cross regions that could change size.
2529 And for the i960 we might be able to turn callx/callj into bal
2530 anyways in cases where we know the maximum displacement. */
2531 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2533 for (; fixP; fixP = fixP->fx_next)
2534 if (!fixP->fx_done)
2536 if (fixP->fx_addsy == NULL)
2538 /* There was no symbol required by this relocation.
2539 However, BFD doesn't really handle relocations
2540 without symbols well. So fake up a local symbol in
2541 the absolute section. */
2542 fixP->fx_addsy = abs_section_sym;
2544 symbol_mark_used_in_reloc (fixP->fx_addsy);
2545 if (fixP->fx_subsy != NULL)
2546 symbol_mark_used_in_reloc (fixP->fx_subsy);
2547 seg_reloc_count++;
2549 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2550 return seg_reloc_count;
2553 for (; fixP; fixP = fixP->fx_next)
2555 #ifdef DEBUG5
2556 fprintf (stderr, "\nprocessing fixup:\n");
2557 print_fixup (fixP);
2558 #endif
2560 fragP = fixP->fx_frag;
2561 know (fragP);
2562 #ifdef TC_VALIDATE_FIX
2563 TC_VALIDATE_FIX (fixP, this_segment, skip);
2564 #endif
2565 add_number = fixP->fx_offset;
2567 if (fixP->fx_addsy != NULL
2568 && symbol_mri_common_p (fixP->fx_addsy))
2570 know (fixP->fx_addsy->sy_value.X_op == O_symbol);
2571 add_number += S_GET_VALUE (fixP->fx_addsy);
2572 fixP->fx_offset = add_number;
2573 fixP->fx_addsy
2574 = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2577 if (fixP->fx_addsy != NULL)
2578 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2580 if (fixP->fx_subsy != NULL)
2582 segT sub_symbol_segment;
2583 resolve_symbol_value (fixP->fx_subsy);
2584 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2585 if (fixP->fx_addsy != NULL
2586 && sub_symbol_segment == add_symbol_segment
2587 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2589 add_number += S_GET_VALUE (fixP->fx_addsy);
2590 add_number -= S_GET_VALUE (fixP->fx_subsy);
2591 fixP->fx_offset = add_number;
2592 fixP->fx_addsy = NULL;
2593 fixP->fx_subsy = NULL;
2594 #ifdef TC_M68K
2595 /* See the comment below about 68k weirdness. */
2596 fixP->fx_pcrel = 0;
2597 #endif
2599 else if (sub_symbol_segment == absolute_section
2600 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2602 add_number -= S_GET_VALUE (fixP->fx_subsy);
2603 fixP->fx_offset = add_number;
2604 fixP->fx_subsy = NULL;
2606 else if (sub_symbol_segment == this_segment
2607 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2609 add_number -= S_GET_VALUE (fixP->fx_subsy);
2610 fixP->fx_offset = (add_number + fixP->fx_dot_value
2611 + fixP->fx_frag->fr_address);
2613 /* Make it pc-relative. If the back-end code has not
2614 selected a pc-relative reloc, cancel the adjustment
2615 we do later on all pc-relative relocs. */
2616 if (0
2617 #ifdef TC_M68K
2618 /* Do this for m68k even if it's already described
2619 as pc-relative. On the m68k, an operand of
2620 "pc@(foo-.-2)" should address "foo" in a
2621 pc-relative mode. */
2622 || 1
2623 #endif
2624 || !fixP->fx_pcrel)
2625 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2626 fixP->fx_subsy = NULL;
2627 fixP->fx_pcrel = 1;
2629 else if (!TC_VALIDATE_FIX_SUB (fixP))
2631 as_bad_where (fixP->fx_file, fixP->fx_line,
2632 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2633 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2634 segment_name (add_symbol_segment),
2635 S_GET_NAME (fixP->fx_subsy),
2636 segment_name (sub_symbol_segment));
2640 if (fixP->fx_addsy)
2642 if (add_symbol_segment == this_segment
2643 && !TC_FORCE_RELOCATION_LOCAL (fixP))
2645 /* This fixup was made when the symbol's segment was
2646 SEG_UNKNOWN, but it is now in the local segment.
2647 So we know how to do the address without relocation. */
2648 add_number += S_GET_VALUE (fixP->fx_addsy);
2649 fixP->fx_offset = add_number;
2650 if (fixP->fx_pcrel)
2651 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2652 fixP->fx_addsy = NULL;
2653 fixP->fx_pcrel = 0;
2655 else if (add_symbol_segment == absolute_section
2656 && !TC_FORCE_RELOCATION_ABS (fixP))
2658 add_number += S_GET_VALUE (fixP->fx_addsy);
2659 fixP->fx_offset = add_number;
2660 fixP->fx_addsy = NULL;
2662 else if (add_symbol_segment != undefined_section
2663 #ifdef BFD_ASSEMBLER
2664 && ! bfd_is_com_section (add_symbol_segment)
2665 #endif
2666 && MD_APPLY_SYM_VALUE (fixP))
2667 add_number += S_GET_VALUE (fixP->fx_addsy);
2670 if (fixP->fx_pcrel)
2672 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2673 if (!fixP->fx_done && fixP->fx_addsy == NULL)
2675 /* There was no symbol required by this relocation.
2676 However, BFD doesn't really handle relocations
2677 without symbols well. So fake up a local symbol in
2678 the absolute section. */
2679 fixP->fx_addsy = abs_section_sym;
2683 if (!fixP->fx_done)
2684 md_apply_fix3 (fixP, &add_number, this_segment);
2686 if (!fixP->fx_done)
2688 ++seg_reloc_count;
2689 if (fixP->fx_addsy == NULL)
2690 fixP->fx_addsy = abs_section_sym;
2691 symbol_mark_used_in_reloc (fixP->fx_addsy);
2692 if (fixP->fx_subsy != NULL)
2693 symbol_mark_used_in_reloc (fixP->fx_subsy);
2696 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2698 if (fixP->fx_size < sizeof (valueT))
2700 valueT mask;
2702 mask = 0;
2703 mask--; /* Set all bits to one. */
2704 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2705 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2707 char buf[50], buf2[50];
2708 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2709 if (add_number > 1000)
2710 sprint_value (buf2, add_number);
2711 else
2712 sprintf (buf2, "%ld", (long) add_number);
2713 as_bad_where (fixP->fx_file, fixP->fx_line,
2714 _("value of %s too large for field of %d bytes at %s"),
2715 buf2, fixP->fx_size, buf);
2716 } /* Generic error checking. */
2718 #ifdef WARN_SIGNED_OVERFLOW_WORD
2719 /* Warn if a .word value is too large when treated as a signed
2720 number. We already know it is not too negative. This is to
2721 catch over-large switches generated by gcc on the 68k. */
2722 if (!flag_signed_overflow_ok
2723 && fixP->fx_size == 2
2724 && add_number > 0x7fff)
2725 as_bad_where (fixP->fx_file, fixP->fx_line,
2726 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2727 (long) add_number,
2728 (long) (fragP->fr_address + fixP->fx_where));
2729 #endif
2730 } /* Not a bit fix. */
2732 #ifdef TC_VALIDATE_FIX
2733 skip: ATTRIBUTE_UNUSED_LABEL
2735 #endif
2736 #ifdef DEBUG5
2737 fprintf (stderr, "result:\n");
2738 print_fixup (fixP);
2739 #endif
2740 } /* For each fixS in this segment. */
2742 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2743 return seg_reloc_count;
2746 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2748 void
2749 number_to_chars_bigendian (char *buf, valueT val, int n)
2751 if (n <= 0)
2752 abort ();
2753 while (n--)
2755 buf[n] = val & 0xff;
2756 val >>= 8;
2760 void
2761 number_to_chars_littleendian (char *buf, valueT val, int n)
2763 if (n <= 0)
2764 abort ();
2765 while (n--)
2767 *buf++ = val & 0xff;
2768 val >>= 8;
2772 void
2773 write_print_statistics (FILE *file)
2775 fprintf (file, "fixups: %d\n", n_fixups);
2778 /* For debugging. */
2779 extern int indent_level;
2781 void
2782 print_fixup (fixS *fixp)
2784 indent_level = 1;
2785 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2786 if (fixp->fx_pcrel)
2787 fprintf (stderr, " pcrel");
2788 if (fixp->fx_pcrel_adjust)
2789 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2790 if (fixp->fx_im_disp)
2792 #ifdef TC_NS32K
2793 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2794 #else
2795 fprintf (stderr, " im_disp");
2796 #endif
2798 if (fixp->fx_tcbit)
2799 fprintf (stderr, " tcbit");
2800 if (fixp->fx_done)
2801 fprintf (stderr, " done");
2802 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2803 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2804 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2805 #ifdef BFD_ASSEMBLER
2806 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2807 fixp->fx_r_type);
2808 #else
2809 #ifdef NEED_FX_R_TYPE
2810 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2811 #endif
2812 #endif
2813 if (fixp->fx_addsy)
2815 fprintf (stderr, "\n +<");
2816 print_symbol_value_1 (stderr, fixp->fx_addsy);
2817 fprintf (stderr, ">");
2819 if (fixp->fx_subsy)
2821 fprintf (stderr, "\n -<");
2822 print_symbol_value_1 (stderr, fixp->fx_subsy);
2823 fprintf (stderr, ">");
2825 fprintf (stderr, "\n");
2826 #ifdef TC_FIX_DATA_PRINT
2827 TC_FIX_DATA_PRINT (stderr, fixp);
2828 #endif