* elf64-alpha.c (elf64_alpha_relax_section): Ignore non-allocated
[binutils.git] / gas / write.c
blob7f4e2d57c782cea7637a376596dc476abf3a26d6
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, 2005
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, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, 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 #ifndef TC_FAKE_LABEL
101 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
102 #endif
104 /* Used to control final evaluation of expressions. */
105 int finalize_syms = 0;
107 int symbol_table_frozen;
109 symbolS *abs_section_sym;
111 /* Remember the value of dot when parsing expressions. */
112 addressT dot_value;
114 void print_fixup (fixS *);
116 #ifdef BFD_ASSEMBLER
117 static void renumber_sections (bfd *, asection *, PTR);
119 /* We generally attach relocs to frag chains. However, after we have
120 chained these all together into a segment, any relocs we add after
121 that must be attached to a segment. This will include relocs added
122 in md_estimate_size_for_relax, for example. */
123 static int frags_chained = 0;
124 #endif
126 #ifndef BFD_ASSEMBLER
128 #ifndef MANY_SEGMENTS
129 struct frag *text_frag_root;
130 struct frag *data_frag_root;
131 struct frag *bss_frag_root;
133 struct frag *text_last_frag; /* Last frag in segment. */
134 struct frag *data_last_frag; /* Last frag in segment. */
135 static struct frag *bss_last_frag; /* Last frag in segment. */
136 #endif
138 #ifndef BFD
139 static object_headers headers;
140 #endif
142 long string_byte_count;
143 char *next_object_file_charP; /* Tracks object file bytes. */
145 #ifndef OBJ_VMS
146 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
147 #endif
149 #endif /* BFD_ASSEMBLER */
151 static int n_fixups;
153 #ifdef BFD_ASSEMBLER
154 #define RELOC_ENUM enum bfd_reloc_code_real
155 #else
156 #define RELOC_ENUM int
157 #endif
159 static fixS *fix_new_internal (fragS *, int where, int size,
160 symbolS *add, symbolS *sub,
161 offsetT offset, int pcrel,
162 RELOC_ENUM r_type);
163 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
164 static long fixup_segment (fixS *, segT);
165 #endif
166 static relax_addressT relax_align (relax_addressT addr, int align);
167 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
168 static fragS *chain_frchains_together_1 (segT, struct frchain *);
169 #endif
170 #ifdef BFD_ASSEMBLER
171 static void chain_frchains_together (bfd *, segT, PTR);
172 static void cvt_frag_to_fill (segT, fragS *);
173 static void adjust_reloc_syms (bfd *, asection *, PTR);
174 static void fix_segment (bfd *, asection *, PTR);
175 static void write_relocs (bfd *, asection *, PTR);
176 static void write_contents (bfd *, asection *, PTR);
177 static void set_symtab (void);
178 #endif
179 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
180 static void merge_data_into_text (void);
181 #endif
182 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
183 static void cvt_frag_to_fill (object_headers *, segT, fragS *);
184 static void remove_subsegs (frchainS *, int, fragS **, fragS **);
185 static void relax_and_size_all_segments (void);
186 #endif
188 /* Create a fixS in obstack 'notes'. */
190 static fixS *
191 fix_new_internal (fragS *frag, /* Which frag? */
192 int where, /* Where in that frag? */
193 int size, /* 1, 2, or 4 usually. */
194 symbolS *add_symbol, /* X_add_symbol. */
195 symbolS *sub_symbol, /* X_op_symbol. */
196 offsetT offset, /* X_add_number. */
197 int pcrel, /* TRUE if PC-relative relocation. */
198 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
200 fixS *fixP;
202 n_fixups++;
204 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
206 fixP->fx_frag = frag;
207 fixP->fx_where = where;
208 fixP->fx_size = size;
209 /* We've made fx_size a narrow field; check that it's wide enough. */
210 if (fixP->fx_size != size)
212 as_bad (_("field fx_size too small to hold %d"), size);
213 abort ();
215 fixP->fx_addsy = add_symbol;
216 fixP->fx_subsy = sub_symbol;
217 fixP->fx_offset = offset;
218 fixP->fx_dot_value = dot_value;
219 fixP->fx_pcrel = pcrel;
220 fixP->fx_plt = 0;
221 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
222 fixP->fx_r_type = r_type;
223 #endif
224 fixP->fx_im_disp = 0;
225 fixP->fx_pcrel_adjust = 0;
226 fixP->fx_bit_fixP = 0;
227 fixP->fx_addnumber = 0;
228 fixP->fx_tcbit = 0;
229 fixP->fx_done = 0;
230 fixP->fx_no_overflow = 0;
231 fixP->fx_signed = 0;
233 #ifdef USING_CGEN
234 fixP->fx_cgen.insn = NULL;
235 fixP->fx_cgen.opinfo = 0;
236 #endif
238 #ifdef TC_FIX_TYPE
239 TC_INIT_FIX_DATA (fixP);
240 #endif
242 as_where (&fixP->fx_file, &fixP->fx_line);
244 /* Usually, we want relocs sorted numerically, but while
245 comparing to older versions of gas that have relocs
246 reverse sorted, it is convenient to have this compile
247 time option. xoxorich. */
250 #ifdef BFD_ASSEMBLER
251 fixS **seg_fix_rootP = (frags_chained
252 ? &seg_info (now_seg)->fix_root
253 : &frchain_now->fix_root);
254 fixS **seg_fix_tailP = (frags_chained
255 ? &seg_info (now_seg)->fix_tail
256 : &frchain_now->fix_tail);
257 #endif
259 #ifdef REVERSE_SORT_RELOCS
261 fixP->fx_next = *seg_fix_rootP;
262 *seg_fix_rootP = fixP;
264 #else /* REVERSE_SORT_RELOCS */
266 fixP->fx_next = NULL;
268 if (*seg_fix_tailP)
269 (*seg_fix_tailP)->fx_next = fixP;
270 else
271 *seg_fix_rootP = fixP;
272 *seg_fix_tailP = fixP;
274 #endif /* REVERSE_SORT_RELOCS */
277 return fixP;
280 /* Create a fixup relative to a symbol (plus a constant). */
282 fixS *
283 fix_new (fragS *frag, /* Which frag? */
284 int where, /* Where in that frag? */
285 int size, /* 1, 2, or 4 usually. */
286 symbolS *add_symbol, /* X_add_symbol. */
287 offsetT offset, /* X_add_number. */
288 int pcrel, /* TRUE if PC-relative relocation. */
289 RELOC_ENUM r_type /* Relocation type. */)
291 return fix_new_internal (frag, where, size, add_symbol,
292 (symbolS *) NULL, offset, pcrel, r_type);
295 /* Create a fixup for an expression. Currently we only support fixups
296 for difference expressions. That is itself more than most object
297 file formats support anyhow. */
299 fixS *
300 fix_new_exp (fragS *frag, /* Which frag? */
301 int where, /* Where in that frag? */
302 int size, /* 1, 2, or 4 usually. */
303 expressionS *exp, /* Expression. */
304 int pcrel, /* TRUE if PC-relative relocation. */
305 RELOC_ENUM r_type /* Relocation type. */)
307 symbolS *add = NULL;
308 symbolS *sub = NULL;
309 offsetT off = 0;
311 switch (exp->X_op)
313 case O_absent:
314 break;
316 case O_register:
317 as_bad (_("register value used as expression"));
318 break;
320 case O_add:
321 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
322 the difference expression cannot immediately be reduced. */
324 symbolS *stmp = make_expr_symbol (exp);
326 exp->X_op = O_symbol;
327 exp->X_op_symbol = 0;
328 exp->X_add_symbol = stmp;
329 exp->X_add_number = 0;
331 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
334 case O_symbol_rva:
335 add = exp->X_add_symbol;
336 off = exp->X_add_number;
338 #if defined(BFD_ASSEMBLER)
339 r_type = BFD_RELOC_RVA;
340 #else
341 #if defined(TC_RVA_RELOC)
342 r_type = TC_RVA_RELOC;
343 #else
344 as_fatal (_("rva not supported"));
345 #endif
346 #endif
347 break;
349 case O_uminus:
350 sub = exp->X_add_symbol;
351 off = exp->X_add_number;
352 break;
354 case O_subtract:
355 sub = exp->X_op_symbol;
356 /* Fall through. */
357 case O_symbol:
358 add = exp->X_add_symbol;
359 /* Fall through. */
360 case O_constant:
361 off = exp->X_add_number;
362 break;
364 default:
365 add = make_expr_symbol (exp);
366 break;
369 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
372 /* Generic function to determine whether a fixup requires a relocation. */
374 generic_force_reloc (fixS *fix)
376 #ifdef BFD_ASSEMBLER
377 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
378 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
379 return 1;
380 #endif
381 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
384 /* Append a string onto another string, bumping the pointer along. */
385 void
386 append (char **charPP, char *fromP, unsigned long length)
388 /* Don't trust memcpy() of 0 chars. */
389 if (length == 0)
390 return;
392 memcpy (*charPP, fromP, length);
393 *charPP += length;
396 #ifndef BFD_ASSEMBLER
397 int section_alignment[SEG_MAXIMUM_ORDINAL];
398 #endif
400 /* This routine records the largest alignment seen for each segment.
401 If the beginning of the segment is aligned on the worst-case
402 boundary, all of the other alignments within it will work. At
403 least one object format really uses this info. */
405 void
406 record_alignment (/* Segment to which alignment pertains. */
407 segT seg,
408 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
409 boundary, 2 => 4-byte boundary, etc.) */
410 int align)
412 if (seg == absolute_section)
413 return;
414 #ifdef BFD_ASSEMBLER
415 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
416 bfd_set_section_alignment (stdoutput, seg, align);
417 #else
418 if (align > section_alignment[(int) seg])
419 section_alignment[(int) seg] = align;
420 #endif
424 get_recorded_alignment (segT seg)
426 if (seg == absolute_section)
427 return 0;
428 #ifdef BFD_ASSEMBLER
429 return bfd_get_section_alignment (stdoutput, seg);
430 #else
431 return section_alignment[(int) seg];
432 #endif
435 #ifdef BFD_ASSEMBLER
437 /* Reset the section indices after removing the gas created sections. */
439 static void
440 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR countparg)
442 int *countp = (int *) countparg;
444 sec->index = *countp;
445 ++*countp;
448 #endif /* defined (BFD_ASSEMBLER) */
450 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
452 static fragS *
453 chain_frchains_together_1 (segT section, struct frchain *frchp)
455 fragS dummy, *prev_frag = &dummy;
456 #ifdef BFD_ASSEMBLER
457 fixS fix_dummy, *prev_fix = &fix_dummy;
458 #endif
460 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
462 prev_frag->fr_next = frchp->frch_root;
463 prev_frag = frchp->frch_last;
464 assert (prev_frag->fr_type != 0);
465 #ifdef BFD_ASSEMBLER
466 if (frchp->fix_root != (fixS *) NULL)
468 if (seg_info (section)->fix_root == (fixS *) NULL)
469 seg_info (section)->fix_root = frchp->fix_root;
470 prev_fix->fx_next = frchp->fix_root;
471 seg_info (section)->fix_tail = frchp->fix_tail;
472 prev_fix = frchp->fix_tail;
474 #endif
476 assert (prev_frag->fr_type != 0);
477 prev_frag->fr_next = 0;
478 return prev_frag;
481 #endif
483 #ifdef BFD_ASSEMBLER
485 static void
486 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
487 segT section,
488 PTR xxx ATTRIBUTE_UNUSED)
490 segment_info_type *info;
492 /* BFD may have introduced its own sections without using
493 subseg_new, so it is possible that seg_info is NULL. */
494 info = seg_info (section);
495 if (info != (segment_info_type *) NULL)
496 info->frchainP->frch_last
497 = chain_frchains_together_1 (section, info->frchainP);
499 /* Now that we've chained the frags together, we must add new fixups
500 to the segment, not to the frag chain. */
501 frags_chained = 1;
504 #endif
506 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
508 static void
509 remove_subsegs (frchainS *head, int seg, fragS **root, fragS **last)
511 *root = head->frch_root;
512 *last = chain_frchains_together_1 (seg, head);
515 #endif /* BFD */
517 #if defined (BFD_ASSEMBLER) || !defined (BFD)
519 #ifdef BFD_ASSEMBLER
520 static void
521 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
522 #else
523 static void
524 cvt_frag_to_fill (object_headers *headersP, segT sec, fragS *fragP)
525 #endif
527 switch (fragP->fr_type)
529 case rs_align:
530 case rs_align_code:
531 case rs_align_test:
532 case rs_org:
533 case rs_space:
534 #ifdef HANDLE_ALIGN
535 HANDLE_ALIGN (fragP);
536 #endif
537 know (fragP->fr_next != NULL);
538 fragP->fr_offset = (fragP->fr_next->fr_address
539 - fragP->fr_address
540 - fragP->fr_fix) / fragP->fr_var;
541 if (fragP->fr_offset < 0)
543 as_bad_where (fragP->fr_file, fragP->fr_line,
544 _("attempt to .org/.space backwards? (%ld)"),
545 (long) fragP->fr_offset);
546 fragP->fr_offset = 0;
548 fragP->fr_type = rs_fill;
549 break;
551 case rs_fill:
552 break;
554 case rs_leb128:
556 valueT value = S_GET_VALUE (fragP->fr_symbol);
557 int size;
559 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
560 fragP->fr_subtype);
562 fragP->fr_fix += size;
563 fragP->fr_type = rs_fill;
564 fragP->fr_var = 0;
565 fragP->fr_offset = 0;
566 fragP->fr_symbol = NULL;
568 break;
570 case rs_cfa:
571 eh_frame_convert_frag (fragP);
572 break;
574 case rs_dwarf2dbg:
575 dwarf2dbg_convert_frag (fragP);
576 break;
578 case rs_machine_dependent:
579 #ifdef BFD_ASSEMBLER
580 md_convert_frag (stdoutput, sec, fragP);
581 #else
582 md_convert_frag (headersP, sec, fragP);
583 #endif
585 assert (fragP->fr_next == NULL
586 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
587 == fragP->fr_fix));
589 /* After md_convert_frag, we make the frag into a ".space 0".
590 md_convert_frag() should set up any fixSs and constants
591 required. */
592 frag_wane (fragP);
593 break;
595 #ifndef WORKING_DOT_WORD
596 case rs_broken_word:
598 struct broken_word *lie;
600 if (fragP->fr_subtype)
602 fragP->fr_fix += md_short_jump_size;
603 for (lie = (struct broken_word *) (fragP->fr_symbol);
604 lie && lie->dispfrag == fragP;
605 lie = lie->next_broken_word)
606 if (lie->added == 1)
607 fragP->fr_fix += md_long_jump_size;
609 frag_wane (fragP);
611 break;
612 #endif
614 default:
615 BAD_CASE (fragP->fr_type);
616 break;
618 #ifdef md_frag_check
619 md_frag_check (fragP);
620 #endif
623 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
625 #ifdef BFD_ASSEMBLER
626 static void relax_seg (bfd *, asection *, PTR);
628 static void
629 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR xxx)
631 segment_info_type *seginfo = seg_info (sec);
633 if (seginfo && seginfo->frchainP
634 && relax_segment (seginfo->frchainP->frch_root, sec))
636 int *result = (int *) xxx;
637 *result = 1;
641 static void size_seg (bfd *, asection *, PTR);
643 static void
644 size_seg (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
646 flagword flags;
647 fragS *fragp;
648 segment_info_type *seginfo;
649 int x;
650 valueT size, newsize;
652 subseg_change (sec, 0);
654 seginfo = seg_info (sec);
655 if (seginfo && seginfo->frchainP)
657 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
658 cvt_frag_to_fill (sec, fragp);
659 for (fragp = seginfo->frchainP->frch_root;
660 fragp->fr_next;
661 fragp = fragp->fr_next)
662 /* Walk to last elt. */
664 size = fragp->fr_address + fragp->fr_fix;
666 else
667 size = 0;
669 flags = bfd_get_section_flags (abfd, sec);
671 if (size > 0 && ! seginfo->bss)
672 flags |= SEC_HAS_CONTENTS;
674 /* @@ This is just an approximation. */
675 if (seginfo && seginfo->fix_root)
676 flags |= SEC_RELOC;
677 else
678 flags &= ~SEC_RELOC;
679 x = bfd_set_section_flags (abfd, sec, flags);
680 assert (x);
682 newsize = md_section_align (sec, size);
683 x = bfd_set_section_size (abfd, sec, newsize);
684 assert (x);
686 /* If the size had to be rounded up, add some padding in the last
687 non-empty frag. */
688 assert (newsize >= size);
689 if (size != newsize)
691 fragS *last = seginfo->frchainP->frch_last;
692 fragp = seginfo->frchainP->frch_root;
693 while (fragp->fr_next != last)
694 fragp = fragp->fr_next;
695 last->fr_address = size;
696 if ((newsize - size) % fragp->fr_var == 0)
697 fragp->fr_offset += (newsize - size) / fragp->fr_var;
698 else
699 /* If we hit this abort, it's likely due to subsegs_finish not
700 providing sufficient alignment on the last frag, and the
701 machine dependent code using alignment frags with fr_var
702 greater than 1. */
703 abort ();
706 #ifdef tc_frob_section
707 tc_frob_section (sec);
708 #endif
709 #ifdef obj_frob_section
710 obj_frob_section (sec);
711 #endif
714 #ifdef DEBUG2
715 static void
716 dump_section_relocs (abfd, sec, stream_)
717 bfd *abfd ATTRIBUTE_UNUSED;
718 asection *sec;
719 char *stream_;
721 FILE *stream = (FILE *) stream_;
722 segment_info_type *seginfo = seg_info (sec);
723 fixS *fixp = seginfo->fix_root;
725 if (!fixp)
726 return;
728 fprintf (stream, "sec %s relocs:\n", sec->name);
729 while (fixp)
731 symbolS *s = fixp->fx_addsy;
733 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
734 (int) fixp->fx_r_type);
735 if (s == NULL)
736 fprintf (stream, "no sym\n");
737 else
739 print_symbol_value_1 (stream, s);
740 fprintf (stream, "\n");
742 fixp = fixp->fx_next;
745 #else
746 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
747 #endif
749 #ifndef EMIT_SECTION_SYMBOLS
750 #define EMIT_SECTION_SYMBOLS 1
751 #endif
753 /* This pass over fixups decides whether symbols can be replaced with
754 section symbols. */
756 static void
757 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
758 asection *sec,
759 PTR xxx ATTRIBUTE_UNUSED)
761 segment_info_type *seginfo = seg_info (sec);
762 fixS *fixp;
764 if (seginfo == NULL)
765 return;
767 dump_section_relocs (abfd, sec, stderr);
769 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
770 if (fixp->fx_done)
771 /* Ignore it. */
773 else if (fixp->fx_addsy)
775 symbolS *sym;
776 asection *symsec;
778 #ifdef DEBUG5
779 fprintf (stderr, "\n\nadjusting fixup:\n");
780 print_fixup (fixp);
781 #endif
783 sym = fixp->fx_addsy;
785 /* All symbols should have already been resolved at this
786 point. It is possible to see unresolved expression
787 symbols, though, since they are not in the regular symbol
788 table. */
789 resolve_symbol_value (sym);
791 if (fixp->fx_subsy != NULL)
792 resolve_symbol_value (fixp->fx_subsy);
794 /* If this symbol is equated to an undefined or common symbol,
795 convert the fixup to being against that symbol. */
796 if (symbol_equated_reloc_p (sym))
798 symbolS *new_sym
799 = symbol_get_value_expression (sym)->X_add_symbol;
800 const char *name = S_GET_NAME (sym);
801 if (!S_IS_COMMON (new_sym)
802 && !TC_FAKE_LABEL (name)
803 && (!S_IS_EXTERNAL (sym) || S_IS_LOCAL (sym)))
804 as_bad (_("Local symbol `%s' can't be equated to undefined symbol `%s'"),
805 name, S_GET_NAME (new_sym));
806 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
807 sym = new_sym;
808 fixp->fx_addsy = sym;
811 if (symbol_mri_common_p (sym))
813 /* These symbols are handled specially in fixup_segment. */
814 continue;
817 /* If the symbol is undefined, common, weak, or global (ELF
818 shared libs), we can't replace it with the section symbol. */
819 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
820 continue;
822 /* Is there some other (target cpu dependent) reason we can't adjust
823 this one? (E.g. relocations involving function addresses on
824 the PA. */
825 #ifdef tc_fix_adjustable
826 if (! tc_fix_adjustable (fixp))
827 continue;
828 #endif
830 /* Since we're reducing to section symbols, don't attempt to reduce
831 anything that's already using one. */
832 if (symbol_section_p (sym))
833 continue;
835 symsec = S_GET_SEGMENT (sym);
836 if (symsec == NULL)
837 abort ();
839 if (bfd_is_abs_section (symsec))
841 /* The fixup_segment routine normally will not use this
842 symbol in a relocation. */
843 continue;
846 /* Don't try to reduce relocs which refer to non-local symbols
847 in .linkonce sections. It can lead to confusion when a
848 debugging section refers to a .linkonce section. I hope
849 this will always be correct. */
850 if (symsec != sec && ! S_IS_LOCAL (sym))
852 if ((symsec->flags & SEC_LINK_ONCE) != 0
853 || (IS_ELF
854 /* The GNU toolchain uses an extension for ELF: a
855 section beginning with the magic string
856 .gnu.linkonce is a linkonce section. */
857 && strncmp (segment_name (symsec), ".gnu.linkonce",
858 sizeof ".gnu.linkonce" - 1) == 0))
859 continue;
862 /* Never adjust a reloc against local symbol in a merge section
863 with non-zero addend. */
864 if ((symsec->flags & SEC_MERGE) != 0
865 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
866 continue;
868 /* Never adjust a reloc against TLS local symbol. */
869 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
870 continue;
872 /* We refetch the segment when calling section_symbol, rather
873 than using symsec, because S_GET_VALUE may wind up changing
874 the section when it calls resolve_symbol_value. */
875 fixp->fx_offset += S_GET_VALUE (sym);
876 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
877 #ifdef DEBUG5
878 fprintf (stderr, "\nadjusted fixup:\n");
879 print_fixup (fixp);
880 #endif
883 dump_section_relocs (abfd, sec, stderr);
886 static void
887 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
888 asection *sec,
889 PTR xxx ATTRIBUTE_UNUSED)
891 segment_info_type *seginfo = seg_info (sec);
893 fixup_segment (seginfo->fix_root, sec);
896 static void
897 write_relocs (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
899 segment_info_type *seginfo = seg_info (sec);
900 unsigned int i;
901 unsigned int n;
902 arelent **relocs;
903 fixS *fixp;
904 char *err;
906 /* If seginfo is NULL, we did not create this section; don't do
907 anything with it. */
908 if (seginfo == NULL)
909 return;
911 n = 0;
912 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
913 n++;
915 #ifndef RELOC_EXPANSION_POSSIBLE
916 /* Set up reloc information as well. */
917 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
919 i = 0;
920 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
922 arelent *reloc;
923 bfd_reloc_status_type s;
924 symbolS *sym;
926 if (fixp->fx_done)
928 n--;
929 continue;
932 /* If this is an undefined symbol which was equated to another
933 symbol, then generate the reloc against the latter symbol
934 rather than the former. */
935 sym = fixp->fx_addsy;
936 while (symbol_equated_reloc_p (sym))
938 symbolS *n;
940 /* We must avoid looping, as that can occur with a badly
941 written program. */
942 n = symbol_get_value_expression (sym)->X_add_symbol;
943 if (n == sym)
944 break;
945 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
946 sym = n;
948 fixp->fx_addsy = sym;
950 reloc = tc_gen_reloc (sec, fixp);
951 if (!reloc)
953 n--;
954 continue;
958 This test is triggered inappropriately for the SH:
959 if (fixp->fx_where + fixp->fx_size
960 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
961 abort ();
964 s = bfd_install_relocation (stdoutput, reloc,
965 fixp->fx_frag->fr_literal,
966 fixp->fx_frag->fr_address,
967 sec, &err);
968 switch (s)
970 case bfd_reloc_ok:
971 break;
972 case bfd_reloc_overflow:
973 as_bad_where (fixp->fx_file, fixp->fx_line,
974 _("relocation overflow"));
975 break;
976 case bfd_reloc_outofrange:
977 as_bad_where (fixp->fx_file, fixp->fx_line,
978 _("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);
984 relocs[i++] = reloc;
986 #else
987 n = n * MAX_RELOC_EXPANSION;
988 /* Set up reloc information as well. */
989 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
991 i = 0;
992 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
994 arelent **reloc;
995 bfd_reloc_status_type s;
996 symbolS *sym;
997 int j;
999 if (fixp->fx_done)
1001 n--;
1002 continue;
1005 /* If this is an undefined symbol which was equated to another
1006 symbol, then generate the reloc against the latter symbol
1007 rather than the former. */
1008 sym = fixp->fx_addsy;
1009 while (symbol_equated_reloc_p (sym))
1011 symbolS *n;
1013 /* We must avoid looping, as that can occur with a badly
1014 written program. */
1015 n = symbol_get_value_expression (sym)->X_add_symbol;
1016 if (n == sym)
1017 break;
1018 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1019 sym = n;
1021 fixp->fx_addsy = sym;
1023 reloc = tc_gen_reloc (sec, fixp);
1025 for (j = 0; reloc[j]; j++)
1027 relocs[i++] = reloc[j];
1028 assert (i <= n);
1030 if (fixp->fx_where + fixp->fx_size
1031 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1032 as_bad_where (fixp->fx_file, fixp->fx_line,
1033 _("internal error: fixup not contained within frag"));
1034 for (j = 0; reloc[j]; j++)
1036 s = bfd_install_relocation (stdoutput, reloc[j],
1037 fixp->fx_frag->fr_literal,
1038 fixp->fx_frag->fr_address,
1039 sec, &err);
1040 switch (s)
1042 case bfd_reloc_ok:
1043 break;
1044 case bfd_reloc_overflow:
1045 as_bad_where (fixp->fx_file, fixp->fx_line,
1046 _("relocation overflow"));
1047 break;
1048 case bfd_reloc_outofrange:
1049 as_bad_where (fixp->fx_file, fixp->fx_line,
1050 _("relocation out of range"));
1051 break;
1052 default:
1053 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1054 fixp->fx_file, fixp->fx_line, s);
1058 n = i;
1059 #endif
1061 #ifdef DEBUG4
1063 int i, j, nsyms;
1064 asymbol **sympp;
1065 sympp = bfd_get_outsymbols (stdoutput);
1066 nsyms = bfd_get_symcount (stdoutput);
1067 for (i = 0; i < n; i++)
1068 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1070 for (j = 0; j < nsyms; j++)
1071 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1072 break;
1073 if (j == nsyms)
1074 abort ();
1077 #endif
1079 if (n)
1080 bfd_set_reloc (stdoutput, sec, relocs, n);
1081 else
1082 bfd_set_section_flags (abfd, sec,
1083 (bfd_get_section_flags (abfd, sec)
1084 & (flagword) ~SEC_RELOC));
1086 #ifdef SET_SECTION_RELOCS
1087 SET_SECTION_RELOCS (sec, relocs, n);
1088 #endif
1090 #ifdef DEBUG3
1092 int i;
1093 arelent *r;
1094 asymbol *s;
1095 fprintf (stderr, "relocs for sec %s\n", sec->name);
1096 for (i = 0; i < n; i++)
1098 r = relocs[i];
1099 s = *r->sym_ptr_ptr;
1100 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1101 i, r, r->address, s->name, r->addend);
1104 #endif
1107 static void
1108 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1109 asection *sec,
1110 PTR xxx ATTRIBUTE_UNUSED)
1112 segment_info_type *seginfo = seg_info (sec);
1113 addressT offset = 0;
1114 fragS *f;
1116 /* Write out the frags. */
1117 if (seginfo == NULL
1118 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1119 return;
1121 for (f = seginfo->frchainP->frch_root;
1123 f = f->fr_next)
1125 int x;
1126 addressT fill_size;
1127 char *fill_literal;
1128 offsetT count;
1130 assert (f->fr_type == rs_fill);
1131 if (f->fr_fix)
1133 x = bfd_set_section_contents (stdoutput, sec,
1134 f->fr_literal, (file_ptr) offset,
1135 (bfd_size_type) f->fr_fix);
1136 if (!x)
1138 bfd_perror (stdoutput->filename);
1139 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1140 exit (EXIT_FAILURE);
1142 offset += f->fr_fix;
1144 fill_literal = f->fr_literal + f->fr_fix;
1145 fill_size = f->fr_var;
1146 count = f->fr_offset;
1147 assert (count >= 0);
1148 if (fill_size && count)
1150 char buf[256];
1151 if (fill_size > sizeof (buf))
1153 /* Do it the old way. Can this ever happen? */
1154 while (count--)
1156 x = bfd_set_section_contents (stdoutput, sec,
1157 fill_literal,
1158 (file_ptr) offset,
1159 (bfd_size_type) fill_size);
1160 if (!x)
1162 bfd_perror (stdoutput->filename);
1163 as_perror (_("FATAL: Can't write %s"),
1164 stdoutput->filename);
1165 exit (EXIT_FAILURE);
1167 offset += fill_size;
1170 else
1172 /* Build a buffer full of fill objects and output it as
1173 often as necessary. This saves on the overhead of
1174 potentially lots of bfd_set_section_contents calls. */
1175 int n_per_buf, i;
1176 if (fill_size == 1)
1178 n_per_buf = sizeof (buf);
1179 memset (buf, *fill_literal, n_per_buf);
1181 else
1183 char *bufp;
1184 n_per_buf = sizeof (buf) / fill_size;
1185 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1186 memcpy (bufp, fill_literal, fill_size);
1188 for (; count > 0; count -= n_per_buf)
1190 n_per_buf = n_per_buf > count ? count : n_per_buf;
1191 x = bfd_set_section_contents
1192 (stdoutput, sec, buf, (file_ptr) offset,
1193 (bfd_size_type) n_per_buf * fill_size);
1194 if (!x)
1195 as_fatal (_("cannot write to output file"));
1196 offset += n_per_buf * fill_size;
1202 #endif
1204 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1205 static void
1206 merge_data_into_text (void)
1208 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1209 seg_info (text_section)->frchainP->frch_last->fr_next =
1210 seg_info (data_section)->frchainP->frch_root;
1211 seg_info (text_section)->frchainP->frch_last =
1212 seg_info (data_section)->frchainP->frch_last;
1213 seg_info (data_section)->frchainP = 0;
1214 #else
1215 fixS *tmp;
1217 text_last_frag->fr_next = data_frag_root;
1218 text_last_frag = data_last_frag;
1219 data_last_frag = NULL;
1220 data_frag_root = NULL;
1221 if (text_fix_root)
1223 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1224 tmp->fx_next = data_fix_root;
1225 text_fix_tail = data_fix_tail;
1227 else
1228 text_fix_root = data_fix_root;
1229 data_fix_root = NULL;
1230 #endif
1232 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1234 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1235 static void
1236 relax_and_size_all_segments ()
1238 fragS *fragP;
1240 relax_segment (text_frag_root, SEG_TEXT);
1241 relax_segment (data_frag_root, SEG_DATA);
1242 relax_segment (bss_frag_root, SEG_BSS);
1244 /* Now the addresses of frags are correct within the segment. */
1245 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1246 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1247 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1249 /* Join the 2 segments into 1 huge segment.
1250 To do this, re-compute every rn_address in the SEG_DATA frags.
1251 Then join the data frags after the text frags.
1253 Determine a_data [length of data segment]. */
1254 if (data_frag_root)
1256 register relax_addressT slide;
1258 know ((text_last_frag->fr_type == rs_fill)
1259 && (text_last_frag->fr_offset == 0));
1261 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1262 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1263 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1264 #ifdef OBJ_BOUT
1265 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1266 /* For b.out: If the data section has a strict alignment
1267 requirement, its load address in the .o file will be
1268 rounded up from the size of the text section. These
1269 two values are *not* the same! Similarly for the bss
1270 section.... */
1271 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1272 #endif
1274 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1275 fragP->fr_address += slide;
1277 know (text_last_frag != 0);
1278 text_last_frag->fr_next = data_frag_root;
1280 else
1282 H_SET_DATA_SIZE (&headers, 0);
1285 #ifdef OBJ_BOUT
1286 /* See above comments on b.out data section address. */
1288 addressT bss_vma;
1289 if (data_last_frag == 0)
1290 bss_vma = H_GET_TEXT_SIZE (&headers);
1291 else
1292 bss_vma = data_last_frag->fr_address;
1293 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1294 bss_address_frag.fr_address = bss_vma;
1296 #else /* ! OBJ_BOUT */
1297 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1298 H_GET_DATA_SIZE (&headers));
1300 #endif /* ! OBJ_BOUT */
1302 /* Slide all the frags. */
1303 if (bss_frag_root)
1305 relax_addressT slide = bss_address_frag.fr_address;
1307 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1308 fragP->fr_address += slide;
1311 if (bss_last_frag)
1312 H_SET_BSS_SIZE (&headers,
1313 bss_last_frag->fr_address - bss_frag_root->fr_address);
1314 else
1315 H_SET_BSS_SIZE (&headers, 0);
1317 #endif /* ! BFD_ASSEMBLER && ! BFD */
1319 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1321 #ifdef BFD_ASSEMBLER
1322 static void
1323 set_symtab (void)
1325 int nsyms;
1326 asymbol **asympp;
1327 symbolS *symp;
1328 bfd_boolean result;
1329 extern PTR bfd_alloc (bfd *, bfd_size_type);
1331 /* Count symbols. We can't rely on a count made by the loop in
1332 write_object_file, because *_frob_file may add a new symbol or
1333 two. */
1334 nsyms = 0;
1335 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1336 nsyms++;
1338 if (nsyms)
1340 int i;
1341 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1343 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1344 symp = symbol_rootP;
1345 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1347 asympp[i] = symbol_get_bfdsym (symp);
1348 symbol_mark_written (symp);
1351 else
1352 asympp = 0;
1353 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1354 assert (result);
1355 symbol_table_frozen = 1;
1357 #endif
1359 /* Finish the subsegments. After every sub-segment, we fake an
1360 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1361 ".fill 0" because that is the kind of frag that requires least
1362 thought. ".align" frags like to have a following frag since that
1363 makes calculating their intended length trivial. */
1365 #ifndef SUB_SEGMENT_ALIGN
1366 #ifdef HANDLE_ALIGN
1367 /* The last subsegment gets an alignment corresponding to the alignment
1368 of the section. This allows proper nop-filling at the end of
1369 code-bearing sections. */
1370 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1371 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1372 ? get_recorded_alignment (SEG) : 0)
1373 #else
1374 #ifdef BFD_ASSEMBLER
1375 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1376 #else
1377 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1378 #endif
1379 #endif
1380 #endif
1382 void
1383 subsegs_finish (void)
1385 struct frchain *frchainP;
1387 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1389 int alignment = 0;
1391 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1393 /* This now gets called even if we had errors. In that case,
1394 any alignment is meaningless, and, moreover, will look weird
1395 if we are generating a listing. */
1396 if (!had_errors ())
1398 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1399 #ifdef BFD_ASSEMBLER
1400 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1401 && now_seg->entsize)
1403 unsigned int entsize = now_seg->entsize;
1404 int entalign = 0;
1406 while ((entsize & 1) == 0)
1408 ++entalign;
1409 entsize >>= 1;
1411 if (entalign > alignment)
1412 alignment = entalign;
1414 #endif
1417 if (subseg_text_p (now_seg))
1418 frag_align_code (alignment, 0);
1419 else
1420 frag_align (alignment, 0, 0);
1422 /* frag_align will have left a new frag.
1423 Use this last frag for an empty ".fill".
1425 For this segment ...
1426 Create a last frag. Do not leave a "being filled in frag". */
1427 frag_wane (frag_now);
1428 frag_now->fr_fix = 0;
1429 know (frag_now->fr_next == NULL);
1433 /* Write the object file. */
1435 void
1436 write_object_file (void)
1438 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1439 fragS *fragP; /* Track along all frags. */
1440 #endif
1442 /* Do we really want to write it? */
1444 int n_warns, n_errs;
1445 n_warns = had_warnings ();
1446 n_errs = had_errors ();
1447 /* The -Z flag indicates that an object file should be generated,
1448 regardless of warnings and errors. */
1449 if (flag_always_generate_output)
1451 if (n_warns || n_errs)
1452 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1453 n_errs, n_errs == 1 ? "" : "s",
1454 n_warns, n_warns == 1 ? "" : "s");
1456 else
1458 if (n_errs)
1459 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1460 n_errs, n_errs == 1 ? "" : "s",
1461 n_warns, n_warns == 1 ? "" : "s");
1465 #ifdef OBJ_VMS
1466 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1467 a routine to check for the definition of the procedure "_main",
1468 and if so -- fix it up so that it can be program entry point. */
1469 vms_check_for_main ();
1470 #endif /* OBJ_VMS */
1472 /* From now on, we don't care about sub-segments. Build one frag chain
1473 for each segment. Linked thru fr_next. */
1475 #ifdef BFD_ASSEMBLER
1476 /* Remove the sections created by gas for its own purposes. */
1478 int i;
1480 bfd_section_list_remove (stdoutput, reg_section);
1481 bfd_section_list_remove (stdoutput, expr_section);
1482 stdoutput->section_count -= 2;
1483 i = 0;
1484 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1487 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1488 #else
1489 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1490 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1491 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1492 #endif
1494 /* We have two segments. If user gave -R flag, then we must put the
1495 data frags into the text segment. Do this before relaxing so
1496 we know to take advantage of -R and make shorter addresses. */
1497 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1498 if (flag_readonly_data_in_text)
1500 merge_data_into_text ();
1502 #endif
1504 #ifdef BFD_ASSEMBLER
1505 while (1)
1507 int changed;
1509 #ifndef WORKING_DOT_WORD
1510 /* We need to reset the markers in the broken word list and
1511 associated frags between calls to relax_segment (via
1512 relax_seg). Since the broken word list is global, we do it
1513 once per round, rather than locally in relax_segment for each
1514 segment. */
1515 struct broken_word *brokp;
1517 for (brokp = broken_words;
1518 brokp != (struct broken_word *) NULL;
1519 brokp = brokp->next_broken_word)
1521 brokp->added = 0;
1523 if (brokp->dispfrag != (fragS *) NULL
1524 && brokp->dispfrag->fr_type == rs_broken_word)
1525 brokp->dispfrag->fr_subtype = 0;
1527 #endif
1529 changed = 0;
1530 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1531 if (!changed)
1532 break;
1535 /* Note - Most ports will use the default value of
1536 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1537 local symbols to be resolved, removing their frag information.
1538 Some ports however, will not have finished relaxing all of
1539 their frags and will still need the local symbol frag
1540 information. These ports can set
1541 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1542 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1544 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1545 #else
1546 relax_and_size_all_segments ();
1547 #endif /* BFD_ASSEMBLER */
1549 /* Relaxation has completed. Freeze all syms. */
1550 finalize_syms = 1;
1552 #ifdef md_post_relax_hook
1553 md_post_relax_hook;
1554 #endif
1556 #ifndef BFD_ASSEMBLER
1557 /* Crawl the symbol chain.
1559 For each symbol whose value depends on a frag, take the address of
1560 that frag and subsume it into the value of the symbol.
1561 After this, there is just one way to lookup a symbol value.
1562 Values are left in their final state for object file emission.
1563 We adjust the values of 'L' local symbols, even if we do
1564 not intend to emit them to the object file, because their values
1565 are needed for fix-ups.
1567 Unless we saw a -L flag, remove all symbols that begin with 'L'
1568 from the symbol chain. (They are still pointed to by the fixes.)
1570 Count the remaining symbols.
1571 Assign a symbol number to each symbol.
1572 Count the number of string-table chars we will emit.
1573 Put this info into the headers as appropriate. */
1574 know (zero_address_frag.fr_address == 0);
1575 string_byte_count = 4;
1577 obj_crawl_symbol_chain (&headers);
1579 if (string_byte_count == 4)
1580 string_byte_count = 0;
1582 H_SET_STRING_SIZE (&headers, string_byte_count);
1584 /* Addresses of frags now reflect addresses we use in the object file.
1585 Symbol values are correct.
1586 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1587 Also converting any machine-dependent frags using md_convert_frag(); */
1588 subseg_change (SEG_TEXT, 0);
1590 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1592 /* At this point we have linked all the frags into a single
1593 chain. However, cvt_frag_to_fill may call md_convert_frag
1594 which may call fix_new. We need to ensure that fix_new adds
1595 the fixup to the right section. */
1596 if (fragP == data_frag_root)
1597 subseg_change (SEG_DATA, 0);
1599 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1601 /* Some assert macros don't work with # directives mixed in. */
1602 #ifndef NDEBUG
1603 if (!(fragP->fr_next == NULL
1604 #ifdef OBJ_BOUT
1605 || fragP->fr_next == data_frag_root
1606 #endif
1607 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
1608 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1609 abort ();
1610 #endif
1612 #endif /* ! BFD_ASSEMBLER */
1614 #ifndef WORKING_DOT_WORD
1616 struct broken_word *lie;
1617 struct broken_word **prevP;
1619 prevP = &broken_words;
1620 for (lie = broken_words; lie; lie = lie->next_broken_word)
1621 if (!lie->added)
1623 expressionS exp;
1625 subseg_change (lie->seg, lie->subseg);
1626 exp.X_op = O_subtract;
1627 exp.X_add_symbol = lie->add;
1628 exp.X_op_symbol = lie->sub;
1629 exp.X_add_number = lie->addnum;
1630 #ifdef BFD_ASSEMBLER
1631 #ifdef TC_CONS_FIX_NEW
1632 TC_CONS_FIX_NEW (lie->frag,
1633 lie->word_goes_here - lie->frag->fr_literal,
1634 2, &exp);
1635 #else
1636 fix_new_exp (lie->frag,
1637 lie->word_goes_here - lie->frag->fr_literal,
1638 2, &exp, 0, BFD_RELOC_16);
1639 #endif
1640 #else
1641 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1642 fix_new_exp (lie->frag,
1643 lie->word_goes_here - lie->frag->fr_literal,
1644 2, &exp, 0, NO_RELOC);
1645 #else
1646 #ifdef TC_NS32K
1647 fix_new_ns32k_exp (lie->frag,
1648 lie->word_goes_here - lie->frag->fr_literal,
1649 2, &exp, 0, 0, 2, 0, 0);
1650 #else
1651 fix_new_exp (lie->frag,
1652 lie->word_goes_here - lie->frag->fr_literal,
1653 2, &exp, 0, 0);
1654 #endif /* TC_NS32K */
1655 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1656 #endif /* BFD_ASSEMBLER */
1657 *prevP = lie->next_broken_word;
1659 else
1660 prevP = &(lie->next_broken_word);
1662 for (lie = broken_words; lie;)
1664 struct broken_word *untruth;
1665 char *table_ptr;
1666 addressT table_addr;
1667 addressT from_addr, to_addr;
1668 int n, m;
1670 subseg_change (lie->seg, lie->subseg);
1671 fragP = lie->dispfrag;
1673 /* Find out how many broken_words go here. */
1674 n = 0;
1675 for (untruth = lie;
1676 untruth && untruth->dispfrag == fragP;
1677 untruth = untruth->next_broken_word)
1678 if (untruth->added == 1)
1679 n++;
1681 table_ptr = lie->dispfrag->fr_opcode;
1682 table_addr = (lie->dispfrag->fr_address
1683 + (table_ptr - lie->dispfrag->fr_literal));
1684 /* Create the jump around the long jumps. This is a short
1685 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1686 from_addr = table_addr;
1687 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1688 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1689 lie->add);
1690 table_ptr += md_short_jump_size;
1691 table_addr += md_short_jump_size;
1693 for (m = 0;
1694 lie && lie->dispfrag == fragP;
1695 m++, lie = lie->next_broken_word)
1697 if (lie->added == 2)
1698 continue;
1699 /* Patch the jump table. */
1700 /* This is the offset from ??? to table_ptr+0. */
1701 to_addr = table_addr - S_GET_VALUE (lie->sub);
1702 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1703 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1704 #endif
1705 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1706 for (untruth = lie->next_broken_word;
1707 untruth && untruth->dispfrag == fragP;
1708 untruth = untruth->next_broken_word)
1710 if (untruth->use_jump == lie)
1711 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1714 /* Install the long jump. */
1715 /* This is a long jump from table_ptr+0 to the final target. */
1716 from_addr = table_addr;
1717 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1718 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1719 lie->add);
1720 table_ptr += md_long_jump_size;
1721 table_addr += md_long_jump_size;
1725 #endif /* not WORKING_DOT_WORD */
1727 #ifndef BFD_ASSEMBLER
1728 #ifndef OBJ_VMS
1729 { /* not vms */
1730 char *the_object_file;
1731 long object_file_size;
1732 /* Scan every FixS performing fixups. We had to wait until now to
1733 do this because md_convert_frag() may have made some fixSs. */
1734 int trsize, drsize;
1736 subseg_change (SEG_TEXT, 0);
1737 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1738 subseg_change (SEG_DATA, 0);
1739 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1740 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1742 /* FIXME: Move this stuff into the pre-write-hook. */
1743 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1744 H_SET_ENTRY_POINT (&headers, 0);
1746 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1748 object_file_size = H_GET_FILE_SIZE (&headers);
1749 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1751 output_file_create (out_file_name);
1753 obj_header_append (&next_object_file_charP, &headers);
1755 know ((next_object_file_charP - the_object_file)
1756 == H_GET_HEADER_SIZE (&headers));
1758 /* Emit code. */
1759 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1761 register long count;
1762 register char *fill_literal;
1763 register long fill_size;
1765 PROGRESS (1);
1766 know (fragP->fr_type == rs_fill);
1767 append (&next_object_file_charP, fragP->fr_literal,
1768 (unsigned long) fragP->fr_fix);
1769 fill_literal = fragP->fr_literal + fragP->fr_fix;
1770 fill_size = fragP->fr_var;
1771 know (fragP->fr_offset >= 0);
1773 for (count = fragP->fr_offset; count; count--)
1774 append (&next_object_file_charP, fill_literal,
1775 (unsigned long) fill_size);
1778 know ((next_object_file_charP - the_object_file)
1779 == (H_GET_HEADER_SIZE (&headers)
1780 + H_GET_TEXT_SIZE (&headers)
1781 + H_GET_DATA_SIZE (&headers)));
1783 /* Emit relocations. */
1784 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1785 (relax_addressT) 0);
1786 know ((next_object_file_charP - the_object_file)
1787 == (H_GET_HEADER_SIZE (&headers)
1788 + H_GET_TEXT_SIZE (&headers)
1789 + H_GET_DATA_SIZE (&headers)
1790 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1791 #ifdef TC_I960
1792 /* Make addresses in data relocation directives relative to beginning of
1793 first data fragment, not end of last text fragment: alignment of the
1794 start of the data segment may place a gap between the segments. */
1795 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1796 data0_frchainP->frch_root->fr_address);
1797 #else /* TC_I960 */
1798 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1799 text_last_frag->fr_address);
1800 #endif /* TC_I960 */
1802 know ((next_object_file_charP - the_object_file)
1803 == (H_GET_HEADER_SIZE (&headers)
1804 + H_GET_TEXT_SIZE (&headers)
1805 + H_GET_DATA_SIZE (&headers)
1806 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1807 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1809 /* Emit line number entries. */
1810 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1811 know ((next_object_file_charP - the_object_file)
1812 == (H_GET_HEADER_SIZE (&headers)
1813 + H_GET_TEXT_SIZE (&headers)
1814 + H_GET_DATA_SIZE (&headers)
1815 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1816 + H_GET_DATA_RELOCATION_SIZE (&headers)
1817 + H_GET_LINENO_SIZE (&headers)));
1819 /* Emit symbols. */
1820 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1821 know ((next_object_file_charP - the_object_file)
1822 == (H_GET_HEADER_SIZE (&headers)
1823 + H_GET_TEXT_SIZE (&headers)
1824 + H_GET_DATA_SIZE (&headers)
1825 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1826 + H_GET_DATA_RELOCATION_SIZE (&headers)
1827 + H_GET_LINENO_SIZE (&headers)
1828 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1830 /* Emit strings. */
1831 if (string_byte_count > 0)
1832 obj_emit_strings (&next_object_file_charP);
1834 #ifdef BFD_HEADERS
1835 bfd_seek (stdoutput, (file_ptr) 0, 0);
1836 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1837 #else
1839 /* Write the data to the file. */
1840 output_file_append (the_object_file, object_file_size, out_file_name);
1841 free (the_object_file);
1842 #endif
1844 #else /* OBJ_VMS */
1845 /* Now do the VMS-dependent part of writing the object file. */
1846 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1847 H_GET_DATA_SIZE (&headers),
1848 H_GET_BSS_SIZE (&headers),
1849 text_frag_root, data_frag_root);
1850 #endif /* OBJ_VMS */
1851 #else /* BFD_ASSEMBLER */
1853 /* Resolve symbol values. This needs to be done before processing
1854 the relocations. */
1855 if (symbol_rootP)
1857 symbolS *symp;
1859 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1860 resolve_symbol_value (symp);
1862 resolve_local_symbol_values ();
1864 PROGRESS (1);
1866 #ifdef tc_frob_file_before_adjust
1867 tc_frob_file_before_adjust ();
1868 #endif
1869 #ifdef obj_frob_file_before_adjust
1870 obj_frob_file_before_adjust ();
1871 #endif
1873 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1875 #ifdef tc_frob_file_before_fix
1876 tc_frob_file_before_fix ();
1877 #endif
1878 #ifdef obj_frob_file_before_fix
1879 obj_frob_file_before_fix ();
1880 #endif
1882 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1884 /* Set up symbol table, and write it out. */
1885 if (symbol_rootP)
1887 symbolS *symp;
1888 bfd_boolean skip_next_symbol = FALSE;
1890 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1892 int punt = 0;
1893 const char *name;
1895 if (skip_next_symbol)
1897 /* Don't do anything besides moving the value of the
1898 symbol from the GAS value-field to the BFD value-field. */
1899 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1900 skip_next_symbol = FALSE;
1901 continue;
1904 if (symbol_mri_common_p (symp))
1906 if (S_IS_EXTERNAL (symp))
1907 as_bad (_("%s: global symbols not supported in common sections"),
1908 S_GET_NAME (symp));
1909 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1910 continue;
1913 name = S_GET_NAME (symp);
1914 if (name)
1916 const char *name2 =
1917 decode_local_label_name ((char *) S_GET_NAME (symp));
1918 /* They only differ if `name' is a fb or dollar local
1919 label name. */
1920 if (name2 != name && ! S_IS_DEFINED (symp))
1921 as_bad (_("local label `%s' is not defined"), name2);
1924 /* Do it again, because adjust_reloc_syms might introduce
1925 more symbols. They'll probably only be section symbols,
1926 but they'll still need to have the values computed. */
1927 resolve_symbol_value (symp);
1929 /* Skip symbols which were equated to undefined or common
1930 symbols. */
1931 if (symbol_equated_reloc_p (symp))
1933 const char *name = S_GET_NAME (symp);
1934 if (S_IS_COMMON (symp)
1935 && !TC_FAKE_LABEL (name)
1936 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1938 expressionS *e = symbol_get_value_expression (symp);
1939 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1940 name, S_GET_NAME (e->X_add_symbol));
1942 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1943 continue;
1946 /* So far, common symbols have been treated like undefined symbols.
1947 Put them in the common section now. */
1948 if (S_IS_DEFINED (symp) == 0
1949 && S_GET_VALUE (symp) != 0)
1950 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1952 #ifdef obj_frob_symbol
1953 obj_frob_symbol (symp, punt);
1954 #endif
1955 #ifdef tc_frob_symbol
1956 if (! punt || symbol_used_in_reloc_p (symp))
1957 tc_frob_symbol (symp, punt);
1958 #endif
1960 /* If we don't want to keep this symbol, splice it out of
1961 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1962 want section symbols. Otherwise, we skip local symbols
1963 and symbols that the frob_symbol macros told us to punt,
1964 but we keep such symbols if they are used in relocs. */
1965 if (symp == abs_section_sym
1966 || (! EMIT_SECTION_SYMBOLS
1967 && symbol_section_p (symp))
1968 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1969 opposites. Sometimes the former checks flags and the
1970 latter examines the name... */
1971 || (!S_IS_EXTERNAL (symp)
1972 && (punt || S_IS_LOCAL (symp))
1973 && ! symbol_used_in_reloc_p (symp)))
1975 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1977 /* After symbol_remove, symbol_next(symp) still returns
1978 the one that came after it in the chain. So we don't
1979 need to do any extra cleanup work here. */
1980 continue;
1983 /* Make sure we really got a value for the symbol. */
1984 if (! symbol_resolved_p (symp))
1986 as_bad (_("can't resolve value for symbol `%s'"),
1987 S_GET_NAME (symp));
1988 symbol_mark_resolved (symp);
1991 /* Set the value into the BFD symbol. Up til now the value
1992 has only been kept in the gas symbolS struct. */
1993 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1995 /* A warning construct is a warning symbol followed by the
1996 symbol warned about. Don't let anything object-format or
1997 target-specific muck with it; it's ready for output. */
1998 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1999 skip_next_symbol = TRUE;
2003 PROGRESS (1);
2005 /* Now do any format-specific adjustments to the symbol table, such
2006 as adding file symbols. */
2007 #ifdef tc_adjust_symtab
2008 tc_adjust_symtab ();
2009 #endif
2010 #ifdef obj_adjust_symtab
2011 obj_adjust_symtab ();
2012 #endif
2014 /* Now that all the sizes are known, and contents correct, we can
2015 start writing to the file. */
2016 set_symtab ();
2018 /* If *_frob_file changes the symbol value at this point, it is
2019 responsible for moving the changed value into symp->bsym->value
2020 as well. Hopefully all symbol value changing can be done in
2021 *_frob_symbol. */
2022 #ifdef tc_frob_file
2023 tc_frob_file ();
2024 #endif
2025 #ifdef obj_frob_file
2026 obj_frob_file ();
2027 #endif
2029 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2031 #ifdef tc_frob_file_after_relocs
2032 tc_frob_file_after_relocs ();
2033 #endif
2034 #ifdef obj_frob_file_after_relocs
2035 obj_frob_file_after_relocs ();
2036 #endif
2038 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2039 #endif /* BFD_ASSEMBLER */
2041 #endif /* ! BFD */
2043 #ifdef TC_GENERIC_RELAX_TABLE
2045 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2047 long
2048 relax_frag (segT segment, fragS *fragP, long stretch)
2050 const relax_typeS *this_type;
2051 const relax_typeS *start_type;
2052 relax_substateT next_state;
2053 relax_substateT this_state;
2054 offsetT growth;
2055 offsetT aim;
2056 addressT target;
2057 addressT address;
2058 symbolS *symbolP;
2059 const relax_typeS *table;
2061 target = fragP->fr_offset;
2062 address = fragP->fr_address;
2063 table = TC_GENERIC_RELAX_TABLE;
2064 this_state = fragP->fr_subtype;
2065 start_type = this_type = table + this_state;
2066 symbolP = fragP->fr_symbol;
2068 if (symbolP)
2070 fragS *sym_frag;
2072 sym_frag = symbol_get_frag (symbolP);
2074 #ifndef DIFF_EXPR_OK
2075 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2076 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2077 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2078 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2079 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2080 #endif
2081 know (sym_frag != NULL);
2082 #endif
2083 know (S_GET_SEGMENT (symbolP) != absolute_section
2084 || sym_frag == &zero_address_frag);
2085 target += S_GET_VALUE (symbolP);
2087 /* If frag has yet to be reached on this pass,
2088 assume it will move by STRETCH just as we did.
2089 If this is not so, it will be because some frag
2090 between grows, and that will force another pass. */
2092 if (stretch != 0
2093 && sym_frag->relax_marker != fragP->relax_marker
2094 && S_GET_SEGMENT (symbolP) == segment)
2096 target += stretch;
2100 aim = target - address - fragP->fr_fix;
2101 #ifdef TC_PCREL_ADJUST
2102 /* Currently only the ns32k family needs this. */
2103 aim += TC_PCREL_ADJUST (fragP);
2104 #endif
2106 #ifdef md_prepare_relax_scan
2107 /* Formerly called M68K_AIM_KLUDGE. */
2108 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2109 #endif
2111 if (aim < 0)
2113 /* Look backwards. */
2114 for (next_state = this_type->rlx_more; next_state;)
2115 if (aim >= this_type->rlx_backward)
2116 next_state = 0;
2117 else
2119 /* Grow to next state. */
2120 this_state = next_state;
2121 this_type = table + this_state;
2122 next_state = this_type->rlx_more;
2125 else
2127 /* Look forwards. */
2128 for (next_state = this_type->rlx_more; next_state;)
2129 if (aim <= this_type->rlx_forward)
2130 next_state = 0;
2131 else
2133 /* Grow to next state. */
2134 this_state = next_state;
2135 this_type = table + this_state;
2136 next_state = this_type->rlx_more;
2140 growth = this_type->rlx_length - start_type->rlx_length;
2141 if (growth != 0)
2142 fragP->fr_subtype = this_state;
2143 return growth;
2146 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2148 /* Relax_align. Advance location counter to next address that has 'alignment'
2149 lowest order bits all 0s, return size of adjustment made. */
2150 static relax_addressT
2151 relax_align (register relax_addressT address, /* Address now. */
2152 register int alignment /* Alignment (binary). */)
2154 relax_addressT mask;
2155 relax_addressT new_address;
2157 mask = ~((~0) << alignment);
2158 new_address = (address + mask) & (~mask);
2159 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2160 if (linkrelax)
2161 /* We must provide lots of padding, so the linker can discard it
2162 when needed. The linker will not add extra space, ever. */
2163 new_address += (1 << alignment);
2164 #endif
2165 return (new_address - address);
2168 /* Now we have a segment, not a crowd of sub-segments, we can make
2169 fr_address values.
2171 Relax the frags.
2173 After this, all frags in this segment have addresses that are correct
2174 within the segment. Since segments live in different file addresses,
2175 these frag addresses may not be the same as final object-file
2176 addresses. */
2179 relax_segment (struct frag *segment_frag_root, segT segment)
2181 register struct frag *fragP;
2182 register relax_addressT address;
2183 int ret;
2185 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2186 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2187 #endif
2188 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2189 subseg_change (segment, 0);
2191 /* For each frag in segment: count and store (a 1st guess of)
2192 fr_address. */
2193 address = 0;
2194 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2196 fragP->relax_marker = 0;
2197 fragP->fr_address = address;
2198 address += fragP->fr_fix;
2200 switch (fragP->fr_type)
2202 case rs_fill:
2203 address += fragP->fr_offset * fragP->fr_var;
2204 break;
2206 case rs_align:
2207 case rs_align_code:
2208 case rs_align_test:
2210 addressT offset = relax_align (address, (int) fragP->fr_offset);
2212 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2213 offset = 0;
2215 if (offset % fragP->fr_var != 0)
2217 as_bad_where (fragP->fr_file, fragP->fr_line,
2218 _("alignment padding (%lu bytes) not a multiple of %ld"),
2219 (unsigned long) offset, (long) fragP->fr_var);
2220 offset -= (offset % fragP->fr_var);
2223 address += offset;
2225 break;
2227 case rs_org:
2228 case rs_space:
2229 /* Assume .org is nugatory. It will grow with 1st relax. */
2230 break;
2232 case rs_machine_dependent:
2233 /* If fr_symbol is an expression, this call to
2234 resolve_symbol_value sets up the correct segment, which will
2235 likely be needed in md_estimate_size_before_relax. */
2236 if (fragP->fr_symbol)
2237 resolve_symbol_value (fragP->fr_symbol);
2239 address += md_estimate_size_before_relax (fragP, segment);
2240 break;
2242 #ifndef WORKING_DOT_WORD
2243 /* Broken words don't concern us yet. */
2244 case rs_broken_word:
2245 break;
2246 #endif
2248 case rs_leb128:
2249 /* Initial guess is always 1; doing otherwise can result in
2250 stable solutions that are larger than the minimum. */
2251 address += fragP->fr_offset = 1;
2252 break;
2254 case rs_cfa:
2255 address += eh_frame_estimate_size_before_relax (fragP);
2256 break;
2258 case rs_dwarf2dbg:
2259 address += dwarf2dbg_estimate_size_before_relax (fragP);
2260 break;
2262 default:
2263 BAD_CASE (fragP->fr_type);
2264 break;
2268 /* Do relax(). */
2270 offsetT stretch; /* May be any size, 0 or negative. */
2271 /* Cumulative number of addresses we have relaxed this pass.
2272 We may have relaxed more than one address. */
2273 int stretched; /* Have we stretched on this pass? */
2274 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2275 grew, and another shrank. If a branch instruction doesn't fit anymore,
2276 we could be scrod. */
2280 stretch = 0;
2281 stretched = 0;
2283 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2285 offsetT growth = 0;
2286 addressT was_address;
2287 offsetT offset;
2288 symbolS *symbolP;
2290 fragP->relax_marker ^= 1;
2291 was_address = fragP->fr_address;
2292 address = fragP->fr_address += stretch;
2293 symbolP = fragP->fr_symbol;
2294 offset = fragP->fr_offset;
2296 switch (fragP->fr_type)
2298 case rs_fill: /* .fill never relaxes. */
2299 growth = 0;
2300 break;
2302 #ifndef WORKING_DOT_WORD
2303 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2304 for it I do not want to write it. I do not want to have
2305 anything to do with it. This is not the proper way to
2306 implement this misfeature. */
2307 case rs_broken_word:
2309 struct broken_word *lie;
2310 struct broken_word *untruth;
2312 /* Yes this is ugly (storing the broken_word pointer
2313 in the symbol slot). Still, this whole chunk of
2314 code is ugly, and I don't feel like doing anything
2315 about it. Think of it as stubbornness in action. */
2316 growth = 0;
2317 for (lie = (struct broken_word *) (fragP->fr_symbol);
2318 lie && lie->dispfrag == fragP;
2319 lie = lie->next_broken_word)
2322 if (lie->added)
2323 continue;
2325 offset = (S_GET_VALUE (lie->add)
2326 + lie->addnum
2327 - S_GET_VALUE (lie->sub));
2328 if (offset <= -32768 || offset >= 32767)
2330 if (flag_warn_displacement)
2332 char buf[50];
2333 sprint_value (buf, (addressT) lie->addnum);
2334 as_warn_where (fragP->fr_file, fragP->fr_line,
2335 _(".word %s-%s+%s didn't fit"),
2336 S_GET_NAME (lie->add),
2337 S_GET_NAME (lie->sub),
2338 buf);
2340 lie->added = 1;
2341 if (fragP->fr_subtype == 0)
2343 fragP->fr_subtype++;
2344 growth += md_short_jump_size;
2346 for (untruth = lie->next_broken_word;
2347 untruth && untruth->dispfrag == lie->dispfrag;
2348 untruth = untruth->next_broken_word)
2349 if ((symbol_get_frag (untruth->add)
2350 == symbol_get_frag (lie->add))
2351 && (S_GET_VALUE (untruth->add)
2352 == S_GET_VALUE (lie->add)))
2354 untruth->added = 2;
2355 untruth->use_jump = lie;
2357 growth += md_long_jump_size;
2361 break;
2362 } /* case rs_broken_word */
2363 #endif
2364 case rs_align:
2365 case rs_align_code:
2366 case rs_align_test:
2368 addressT oldoff, newoff;
2370 oldoff = relax_align (was_address + fragP->fr_fix,
2371 (int) offset);
2372 newoff = relax_align (address + fragP->fr_fix,
2373 (int) offset);
2375 if (fragP->fr_subtype != 0)
2377 if (oldoff > fragP->fr_subtype)
2378 oldoff = 0;
2379 if (newoff > fragP->fr_subtype)
2380 newoff = 0;
2383 growth = newoff - oldoff;
2385 break;
2387 case rs_org:
2389 addressT target = offset;
2390 addressT after;
2392 if (symbolP)
2394 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2395 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2396 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2397 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2398 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2399 know (symbolP->sy_frag);
2400 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2401 || (symbolP->sy_frag == &zero_address_frag));
2402 #endif
2403 /* Convert from an actual address to an octet offset
2404 into the section. Here it is assumed that the
2405 section's VMA is zero, and can omit subtracting it
2406 from the symbol's value to get the address offset. */
2407 #ifdef BFD_ASSEMBLER
2408 know (S_GET_SEGMENT (symbolP)->vma == 0);
2409 #endif
2410 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2413 know (fragP->fr_next);
2414 after = fragP->fr_next->fr_address;
2415 growth = target - after;
2416 if (growth < 0)
2418 /* Growth may be negative, but variable part of frag
2419 cannot have fewer than 0 chars. That is, we can't
2420 .org backwards. */
2421 as_bad_where (fragP->fr_file, fragP->fr_line,
2422 _("attempt to move .org backwards"));
2424 /* We've issued an error message. Change the
2425 frag to avoid cascading errors. */
2426 fragP->fr_type = rs_align;
2427 fragP->fr_subtype = 0;
2428 fragP->fr_offset = 0;
2429 fragP->fr_fix = after - was_address;
2430 growth = stretch;
2433 /* This is an absolute growth factor */
2434 growth -= stretch;
2435 break;
2438 case rs_space:
2439 growth = 0;
2440 if (symbolP)
2442 offsetT amount;
2444 amount = S_GET_VALUE (symbolP);
2445 if (S_GET_SEGMENT (symbolP) != absolute_section
2446 || S_IS_COMMON (symbolP)
2447 || ! S_IS_DEFINED (symbolP))
2449 as_bad_where (fragP->fr_file, fragP->fr_line,
2450 _(".space specifies non-absolute value"));
2451 /* Prevent repeat of this error message. */
2452 fragP->fr_symbol = 0;
2454 else if (amount < 0)
2456 as_warn_where (fragP->fr_file, fragP->fr_line,
2457 _(".space or .fill with negative value, ignored"));
2458 fragP->fr_symbol = 0;
2460 else
2461 growth = (was_address + fragP->fr_fix + amount
2462 - fragP->fr_next->fr_address);
2464 break;
2466 case rs_machine_dependent:
2467 #ifdef md_relax_frag
2468 growth = md_relax_frag (segment, fragP, stretch);
2469 #else
2470 #ifdef TC_GENERIC_RELAX_TABLE
2471 /* The default way to relax a frag is to look through
2472 TC_GENERIC_RELAX_TABLE. */
2473 growth = relax_frag (segment, fragP, stretch);
2474 #endif /* TC_GENERIC_RELAX_TABLE */
2475 #endif
2476 break;
2478 case rs_leb128:
2480 valueT value;
2481 offsetT size;
2483 value = resolve_symbol_value (fragP->fr_symbol);
2484 size = sizeof_leb128 (value, fragP->fr_subtype);
2485 growth = size - fragP->fr_offset;
2486 fragP->fr_offset = size;
2488 break;
2490 case rs_cfa:
2491 growth = eh_frame_relax_frag (fragP);
2492 break;
2494 case rs_dwarf2dbg:
2495 growth = dwarf2dbg_relax_frag (fragP);
2496 break;
2498 default:
2499 BAD_CASE (fragP->fr_type);
2500 break;
2502 if (growth)
2504 stretch += growth;
2505 stretched = 1;
2507 } /* For each frag in the segment. */
2509 while (stretched); /* Until nothing further to relax. */
2510 } /* do_relax */
2512 ret = 0;
2513 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2514 if (fragP->last_fr_address != fragP->fr_address)
2516 fragP->last_fr_address = fragP->fr_address;
2517 ret = 1;
2519 return ret;
2522 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2524 /* fixup_segment()
2526 Go through all the fixS's in a segment and see which ones can be
2527 handled now. (These consist of fixS where we have since discovered
2528 the value of a symbol, or the address of the frag involved.)
2529 For each one, call md_apply_fix3 to put the fix into the frag data.
2531 Result is a count of how many relocation structs will be needed to
2532 handle the remaining fixS's that we couldn't completely handle here.
2533 These will be output later by emit_relocations(). */
2535 static long
2536 fixup_segment (fixS *fixP, segT this_segment)
2538 long seg_reloc_count = 0;
2539 valueT add_number;
2540 fragS *fragP;
2541 segT add_symbol_segment = absolute_section;
2543 if (fixP != NULL && abs_section_sym == NULL)
2545 #ifndef BFD_ASSEMBLER
2546 abs_section_sym = &abs_symbol;
2547 #else
2548 abs_section_sym = section_symbol (absolute_section);
2549 #endif
2552 /* If the linker is doing the relaxing, we must not do any fixups.
2554 Well, strictly speaking that's not true -- we could do any that
2555 are PC-relative and don't cross regions that could change size.
2556 And for the i960 we might be able to turn callx/callj into bal
2557 anyways in cases where we know the maximum displacement. */
2558 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2560 for (; fixP; fixP = fixP->fx_next)
2561 if (!fixP->fx_done)
2563 if (fixP->fx_addsy == NULL)
2565 /* There was no symbol required by this relocation.
2566 However, BFD doesn't really handle relocations
2567 without symbols well. So fake up a local symbol in
2568 the absolute section. */
2569 fixP->fx_addsy = abs_section_sym;
2571 symbol_mark_used_in_reloc (fixP->fx_addsy);
2572 if (fixP->fx_subsy != NULL)
2573 symbol_mark_used_in_reloc (fixP->fx_subsy);
2574 seg_reloc_count++;
2576 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2577 return seg_reloc_count;
2580 for (; fixP; fixP = fixP->fx_next)
2582 #ifdef DEBUG5
2583 fprintf (stderr, "\nprocessing fixup:\n");
2584 print_fixup (fixP);
2585 #endif
2587 fragP = fixP->fx_frag;
2588 know (fragP);
2589 #ifdef TC_VALIDATE_FIX
2590 TC_VALIDATE_FIX (fixP, this_segment, skip);
2591 #endif
2592 add_number = fixP->fx_offset;
2594 if (fixP->fx_addsy != NULL
2595 && symbol_mri_common_p (fixP->fx_addsy))
2597 add_number += S_GET_VALUE (fixP->fx_addsy);
2598 fixP->fx_offset = add_number;
2599 fixP->fx_addsy
2600 = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2603 if (fixP->fx_addsy != NULL)
2604 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2606 if (fixP->fx_subsy != NULL)
2608 segT sub_symbol_segment;
2609 resolve_symbol_value (fixP->fx_subsy);
2610 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2611 if (fixP->fx_addsy != NULL
2612 && sub_symbol_segment == add_symbol_segment
2613 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2615 add_number += S_GET_VALUE (fixP->fx_addsy);
2616 add_number -= S_GET_VALUE (fixP->fx_subsy);
2617 fixP->fx_offset = add_number;
2618 fixP->fx_addsy = NULL;
2619 fixP->fx_subsy = NULL;
2620 #ifdef TC_M68K
2621 /* See the comment below about 68k weirdness. */
2622 fixP->fx_pcrel = 0;
2623 #endif
2625 else if (sub_symbol_segment == absolute_section
2626 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2628 add_number -= S_GET_VALUE (fixP->fx_subsy);
2629 fixP->fx_offset = add_number;
2630 fixP->fx_subsy = NULL;
2632 else if (sub_symbol_segment == this_segment
2633 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2635 add_number -= S_GET_VALUE (fixP->fx_subsy);
2636 fixP->fx_offset = (add_number + fixP->fx_dot_value
2637 + fixP->fx_frag->fr_address);
2639 /* Make it pc-relative. If the back-end code has not
2640 selected a pc-relative reloc, cancel the adjustment
2641 we do later on all pc-relative relocs. */
2642 if (0
2643 #ifdef TC_M68K
2644 /* Do this for m68k even if it's already described
2645 as pc-relative. On the m68k, an operand of
2646 "pc@(foo-.-2)" should address "foo" in a
2647 pc-relative mode. */
2648 || 1
2649 #endif
2650 || !fixP->fx_pcrel)
2651 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2652 fixP->fx_subsy = NULL;
2653 fixP->fx_pcrel = 1;
2655 else if (!TC_VALIDATE_FIX_SUB (fixP))
2657 as_bad_where (fixP->fx_file, fixP->fx_line,
2658 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2659 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2660 segment_name (add_symbol_segment),
2661 S_GET_NAME (fixP->fx_subsy),
2662 segment_name (sub_symbol_segment));
2666 if (fixP->fx_addsy)
2668 if (add_symbol_segment == this_segment
2669 && !TC_FORCE_RELOCATION_LOCAL (fixP))
2671 /* This fixup was made when the symbol's segment was
2672 SEG_UNKNOWN, but it is now in the local segment.
2673 So we know how to do the address without relocation. */
2674 add_number += S_GET_VALUE (fixP->fx_addsy);
2675 fixP->fx_offset = add_number;
2676 if (fixP->fx_pcrel)
2677 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2678 fixP->fx_addsy = NULL;
2679 fixP->fx_pcrel = 0;
2681 else if (add_symbol_segment == absolute_section
2682 && !TC_FORCE_RELOCATION_ABS (fixP))
2684 add_number += S_GET_VALUE (fixP->fx_addsy);
2685 fixP->fx_offset = add_number;
2686 fixP->fx_addsy = NULL;
2688 else if (add_symbol_segment != undefined_section
2689 #ifdef BFD_ASSEMBLER
2690 && ! bfd_is_com_section (add_symbol_segment)
2691 #endif
2692 && MD_APPLY_SYM_VALUE (fixP))
2693 add_number += S_GET_VALUE (fixP->fx_addsy);
2696 if (fixP->fx_pcrel)
2698 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2699 if (!fixP->fx_done && fixP->fx_addsy == NULL)
2701 /* There was no symbol required by this relocation.
2702 However, BFD doesn't really handle relocations
2703 without symbols well. So fake up a local symbol in
2704 the absolute section. */
2705 fixP->fx_addsy = abs_section_sym;
2709 if (!fixP->fx_done)
2710 md_apply_fix3 (fixP, &add_number, this_segment);
2712 if (!fixP->fx_done)
2714 ++seg_reloc_count;
2715 if (fixP->fx_addsy == NULL)
2716 fixP->fx_addsy = abs_section_sym;
2717 symbol_mark_used_in_reloc (fixP->fx_addsy);
2718 if (fixP->fx_subsy != NULL)
2719 symbol_mark_used_in_reloc (fixP->fx_subsy);
2722 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2724 if (fixP->fx_size < sizeof (valueT))
2726 valueT mask;
2728 mask = 0;
2729 mask--; /* Set all bits to one. */
2730 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2731 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2733 char buf[50], buf2[50];
2734 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2735 if (add_number > 1000)
2736 sprint_value (buf2, add_number);
2737 else
2738 sprintf (buf2, "%ld", (long) add_number);
2739 as_bad_where (fixP->fx_file, fixP->fx_line,
2740 _("value of %s too large for field of %d bytes at %s"),
2741 buf2, fixP->fx_size, buf);
2742 } /* Generic error checking. */
2744 #ifdef WARN_SIGNED_OVERFLOW_WORD
2745 /* Warn if a .word value is too large when treated as a signed
2746 number. We already know it is not too negative. This is to
2747 catch over-large switches generated by gcc on the 68k. */
2748 if (!flag_signed_overflow_ok
2749 && fixP->fx_size == 2
2750 && add_number > 0x7fff)
2751 as_bad_where (fixP->fx_file, fixP->fx_line,
2752 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2753 (long) add_number,
2754 (long) (fragP->fr_address + fixP->fx_where));
2755 #endif
2756 } /* Not a bit fix. */
2758 #ifdef TC_VALIDATE_FIX
2759 skip: ATTRIBUTE_UNUSED_LABEL
2761 #endif
2762 #ifdef DEBUG5
2763 fprintf (stderr, "result:\n");
2764 print_fixup (fixP);
2765 #endif
2766 } /* For each fixS in this segment. */
2768 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2769 return seg_reloc_count;
2772 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2774 void
2775 number_to_chars_bigendian (char *buf, valueT val, int n)
2777 if (n <= 0)
2778 abort ();
2779 while (n--)
2781 buf[n] = val & 0xff;
2782 val >>= 8;
2786 void
2787 number_to_chars_littleendian (char *buf, valueT val, int n)
2789 if (n <= 0)
2790 abort ();
2791 while (n--)
2793 *buf++ = val & 0xff;
2794 val >>= 8;
2798 void
2799 write_print_statistics (FILE *file)
2801 fprintf (file, "fixups: %d\n", n_fixups);
2804 /* For debugging. */
2805 extern int indent_level;
2807 void
2808 print_fixup (fixS *fixp)
2810 indent_level = 1;
2811 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2812 if (fixp->fx_pcrel)
2813 fprintf (stderr, " pcrel");
2814 if (fixp->fx_pcrel_adjust)
2815 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2816 if (fixp->fx_im_disp)
2818 #ifdef TC_NS32K
2819 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2820 #else
2821 fprintf (stderr, " im_disp");
2822 #endif
2824 if (fixp->fx_tcbit)
2825 fprintf (stderr, " tcbit");
2826 if (fixp->fx_done)
2827 fprintf (stderr, " done");
2828 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2829 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2830 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2831 #ifdef BFD_ASSEMBLER
2832 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2833 fixp->fx_r_type);
2834 #else
2835 #ifdef NEED_FX_R_TYPE
2836 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2837 #endif
2838 #endif
2839 if (fixp->fx_addsy)
2841 fprintf (stderr, "\n +<");
2842 print_symbol_value_1 (stderr, fixp->fx_addsy);
2843 fprintf (stderr, ">");
2845 if (fixp->fx_subsy)
2847 fprintf (stderr, "\n -<");
2848 print_symbol_value_1 (stderr, fixp->fx_subsy);
2849 fprintf (stderr, ">");
2851 fprintf (stderr, "\n");
2852 #ifdef TC_FIX_DATA_PRINT
2853 TC_FIX_DATA_PRINT (stderr, fixp);
2854 #endif