* targets.c (bfd_target_list): Don't return the default target twice.
[binutils.git] / gas / write.c
blobb4cc7e090b05a43dadde81548461cfbcc9ae1da1
1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002
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 (S_FORCE_RELOC ((FIX)->fx_addsy))
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) \
59 (S_FORCE_RELOC ((FIX)->fx_subsy))
60 #endif
62 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
63 #ifdef DIFF_EXPR_OK
64 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) \
65 (S_FORCE_RELOC ((FIX)->fx_subsy))
66 #else
67 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1
68 #endif
69 #endif
71 #ifndef TC_VALIDATE_FIX_SUB
72 #ifdef UNDEFINED_DIFFERENCE_OK
73 /* The PA needs this for PIC code generation. */
74 #define TC_VALIDATE_FIX_SUB(FIX) 1
75 #else
76 #ifdef BFD_ASSEMBLER
77 #define TC_VALIDATE_FIX_SUB(FIX) \
78 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
79 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
80 #else
81 #define TC_VALIDATE_FIX_SUB(FIX) 0
82 #endif
83 #endif
84 #endif
86 #ifndef TC_LINKRELAX_FIXUP
87 #define TC_LINKRELAX_FIXUP(SEG) 1
88 #endif
90 #ifndef MD_APPLY_SYM_VALUE
91 #define MD_APPLY_SYM_VALUE(FIX) 1
92 #endif
94 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
95 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
96 #endif
98 #ifndef MD_PCREL_FROM_SECTION
99 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
100 #endif
102 #ifndef WORKING_DOT_WORD
103 extern const int md_short_jump_size;
104 extern const int md_long_jump_size;
105 #endif
107 /* Used to control final evaluation of expressions. */
108 int finalize_syms = 0;
110 int symbol_table_frozen;
112 symbolS *abs_section_sym;
114 /* Remember the value of dot when parsing expressions. */
115 addressT dot_value;
117 void print_fixup PARAMS ((fixS *));
119 #ifdef BFD_ASSEMBLER
120 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
122 /* We generally attach relocs to frag chains. However, after we have
123 chained these all together into a segment, any relocs we add after
124 that must be attached to a segment. This will include relocs added
125 in md_estimate_size_for_relax, for example. */
126 static int frags_chained = 0;
127 #endif
129 #ifndef BFD_ASSEMBLER
131 #ifndef MANY_SEGMENTS
132 struct frag *text_frag_root;
133 struct frag *data_frag_root;
134 struct frag *bss_frag_root;
136 struct frag *text_last_frag; /* Last frag in segment. */
137 struct frag *data_last_frag; /* Last frag in segment. */
138 static struct frag *bss_last_frag; /* Last frag in segment. */
139 #endif
141 #ifndef BFD
142 static object_headers headers;
143 #endif
145 long string_byte_count;
146 char *next_object_file_charP; /* Tracks object file bytes. */
148 #ifndef OBJ_VMS
149 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
150 #endif
152 #endif /* BFD_ASSEMBLER */
154 static int n_fixups;
156 #ifdef BFD_ASSEMBLER
157 #define RELOC_ENUM enum bfd_reloc_code_real
158 #else
159 #define RELOC_ENUM int
160 #endif
162 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
163 symbolS *add, symbolS *sub,
164 offsetT offset, int pcrel,
165 RELOC_ENUM r_type));
166 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
167 static long fixup_segment PARAMS ((fixS *, segT));
168 #endif
169 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
170 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
171 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
172 #endif
173 #ifdef BFD_ASSEMBLER
174 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
175 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
176 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
177 static void fix_segment PARAMS ((bfd *, asection *, PTR));
178 static void write_relocs PARAMS ((bfd *, asection *, PTR));
179 static void write_contents PARAMS ((bfd *, asection *, PTR));
180 static void set_symtab PARAMS ((void));
181 #endif
182 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
183 static void merge_data_into_text PARAMS ((void));
184 #endif
185 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
186 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
187 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
188 static void relax_and_size_all_segments PARAMS ((void));
189 #endif
191 /* Create a fixS in obstack 'notes'. */
193 static fixS *
194 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
195 r_type)
196 fragS *frag; /* Which frag? */
197 int where; /* Where in that frag? */
198 int size; /* 1, 2, or 4 usually. */
199 symbolS *add_symbol; /* X_add_symbol. */
200 symbolS *sub_symbol; /* X_op_symbol. */
201 offsetT offset; /* X_add_number. */
202 int pcrel; /* TRUE if PC-relative relocation. */
203 RELOC_ENUM r_type ATTRIBUTE_UNUSED; /* Relocation type. */
205 fixS *fixP;
207 n_fixups++;
209 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
211 fixP->fx_frag = frag;
212 fixP->fx_where = where;
213 fixP->fx_size = size;
214 /* We've made fx_size a narrow field; check that it's wide enough. */
215 if (fixP->fx_size != size)
217 as_bad (_("field fx_size too small to hold %d"), size);
218 abort ();
220 fixP->fx_addsy = add_symbol;
221 fixP->fx_subsy = sub_symbol;
222 fixP->fx_offset = offset;
223 fixP->fx_dot_value = dot_value;
224 fixP->fx_pcrel = pcrel;
225 fixP->fx_plt = 0;
226 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
227 fixP->fx_r_type = r_type;
228 #endif
229 fixP->fx_im_disp = 0;
230 fixP->fx_pcrel_adjust = 0;
231 fixP->fx_bit_fixP = 0;
232 fixP->fx_addnumber = 0;
233 fixP->fx_tcbit = 0;
234 fixP->fx_done = 0;
235 fixP->fx_no_overflow = 0;
236 fixP->fx_signed = 0;
238 #ifdef USING_CGEN
239 fixP->fx_cgen.insn = NULL;
240 fixP->fx_cgen.opinfo = 0;
241 #endif
243 #ifdef TC_FIX_TYPE
244 TC_INIT_FIX_DATA (fixP);
245 #endif
247 as_where (&fixP->fx_file, &fixP->fx_line);
249 /* Usually, we want relocs sorted numerically, but while
250 comparing to older versions of gas that have relocs
251 reverse sorted, it is convenient to have this compile
252 time option. xoxorich. */
255 #ifdef BFD_ASSEMBLER
256 fixS **seg_fix_rootP = (frags_chained
257 ? &seg_info (now_seg)->fix_root
258 : &frchain_now->fix_root);
259 fixS **seg_fix_tailP = (frags_chained
260 ? &seg_info (now_seg)->fix_tail
261 : &frchain_now->fix_tail);
262 #endif
264 #ifdef REVERSE_SORT_RELOCS
266 fixP->fx_next = *seg_fix_rootP;
267 *seg_fix_rootP = fixP;
269 #else /* REVERSE_SORT_RELOCS */
271 fixP->fx_next = NULL;
273 if (*seg_fix_tailP)
274 (*seg_fix_tailP)->fx_next = fixP;
275 else
276 *seg_fix_rootP = fixP;
277 *seg_fix_tailP = fixP;
279 #endif /* REVERSE_SORT_RELOCS */
282 return fixP;
285 /* Create a fixup relative to a symbol (plus a constant). */
287 fixS *
288 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
289 fragS *frag; /* Which frag? */
290 int where; /* Where in that frag? */
291 int size; /* 1, 2, or 4 usually. */
292 symbolS *add_symbol; /* X_add_symbol. */
293 offsetT offset; /* X_add_number. */
294 int pcrel; /* TRUE if PC-relative relocation. */
295 RELOC_ENUM r_type; /* Relocation type. */
297 return fix_new_internal (frag, where, size, add_symbol,
298 (symbolS *) NULL, offset, pcrel, r_type);
301 /* Create a fixup for an expression. Currently we only support fixups
302 for difference expressions. That is itself more than most object
303 file formats support anyhow. */
305 fixS *
306 fix_new_exp (frag, where, size, exp, pcrel, r_type)
307 fragS *frag; /* Which frag? */
308 int where; /* Where in that frag? */
309 int size; /* 1, 2, or 4 usually. */
310 expressionS *exp; /* Expression. */
311 int pcrel; /* TRUE if PC-relative relocation. */
312 RELOC_ENUM r_type; /* Relocation type. */
314 symbolS *add = NULL;
315 symbolS *sub = NULL;
316 offsetT off = 0;
318 switch (exp->X_op)
320 case O_absent:
321 break;
323 case O_register:
324 as_bad (_("register value used as expression"));
325 break;
327 case O_add:
328 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
329 the difference expression cannot immediately be reduced. */
331 symbolS *stmp = make_expr_symbol (exp);
333 exp->X_op = O_symbol;
334 exp->X_op_symbol = 0;
335 exp->X_add_symbol = stmp;
336 exp->X_add_number = 0;
338 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
341 case O_symbol_rva:
342 add = exp->X_add_symbol;
343 off = exp->X_add_number;
345 #if defined(BFD_ASSEMBLER)
346 r_type = BFD_RELOC_RVA;
347 #else
348 #if defined(TC_RVA_RELOC)
349 r_type = TC_RVA_RELOC;
350 #else
351 as_fatal (_("rva not supported"));
352 #endif
353 #endif
354 break;
356 case O_uminus:
357 sub = exp->X_add_symbol;
358 off = exp->X_add_number;
359 break;
361 case O_subtract:
362 sub = exp->X_op_symbol;
363 /* Fall through. */
364 case O_symbol:
365 add = exp->X_add_symbol;
366 /* Fall through. */
367 case O_constant:
368 off = exp->X_add_number;
369 break;
371 default:
372 add = make_expr_symbol (exp);
373 break;
376 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
379 /* Append a string onto another string, bumping the pointer along. */
380 void
381 append (charPP, fromP, length)
382 char **charPP;
383 char *fromP;
384 unsigned long length;
386 /* Don't trust memcpy() of 0 chars. */
387 if (length == 0)
388 return;
390 memcpy (*charPP, fromP, length);
391 *charPP += length;
394 #ifndef BFD_ASSEMBLER
395 int section_alignment[SEG_MAXIMUM_ORDINAL];
396 #endif
398 /* This routine records the largest alignment seen for each segment.
399 If the beginning of the segment is aligned on the worst-case
400 boundary, all of the other alignments within it will work. At
401 least one object format really uses this info. */
403 void
404 record_alignment (seg, align)
405 /* Segment to which alignment pertains. */
406 segT seg;
407 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
408 boundary, etc.) */
409 int align;
411 if (seg == absolute_section)
412 return;
413 #ifdef BFD_ASSEMBLER
414 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
415 bfd_set_section_alignment (stdoutput, seg, align);
416 #else
417 if (align > section_alignment[(int) seg])
418 section_alignment[(int) seg] = align;
419 #endif
423 get_recorded_alignment (seg)
424 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 (abfd, sec, countparg)
441 bfd *abfd ATTRIBUTE_UNUSED;
442 asection *sec;
443 PTR countparg;
445 int *countp = (int *) countparg;
447 sec->index = *countp;
448 ++*countp;
451 #endif /* defined (BFD_ASSEMBLER) */
453 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
455 static fragS *
456 chain_frchains_together_1 (section, frchp)
457 segT section;
458 struct frchain *frchp;
460 fragS dummy, *prev_frag = &dummy;
461 #ifdef BFD_ASSEMBLER
462 fixS fix_dummy, *prev_fix = &fix_dummy;
463 #endif
465 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
467 prev_frag->fr_next = frchp->frch_root;
468 prev_frag = frchp->frch_last;
469 assert (prev_frag->fr_type != 0);
470 #ifdef BFD_ASSEMBLER
471 if (frchp->fix_root != (fixS *) NULL)
473 if (seg_info (section)->fix_root == (fixS *) NULL)
474 seg_info (section)->fix_root = frchp->fix_root;
475 prev_fix->fx_next = frchp->fix_root;
476 seg_info (section)->fix_tail = frchp->fix_tail;
477 prev_fix = frchp->fix_tail;
479 #endif
481 assert (prev_frag->fr_type != 0);
482 prev_frag->fr_next = 0;
483 return prev_frag;
486 #endif
488 #ifdef BFD_ASSEMBLER
490 static void
491 chain_frchains_together (abfd, section, xxx)
492 bfd *abfd ATTRIBUTE_UNUSED;
493 segT section;
494 PTR xxx ATTRIBUTE_UNUSED;
496 segment_info_type *info;
498 /* BFD may have introduced its own sections without using
499 subseg_new, so it is possible that seg_info is NULL. */
500 info = seg_info (section);
501 if (info != (segment_info_type *) NULL)
502 info->frchainP->frch_last
503 = chain_frchains_together_1 (section, info->frchainP);
505 /* Now that we've chained the frags together, we must add new fixups
506 to the segment, not to the frag chain. */
507 frags_chained = 1;
510 #endif
512 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
514 static void
515 remove_subsegs (head, seg, root, last)
516 frchainS *head;
517 int seg;
518 fragS **root;
519 fragS **last;
521 *root = head->frch_root;
522 *last = chain_frchains_together_1 (seg, head);
525 #endif /* BFD */
527 #if defined (BFD_ASSEMBLER) || !defined (BFD)
529 #ifdef BFD_ASSEMBLER
530 static void
531 cvt_frag_to_fill (sec, fragP)
532 segT sec ATTRIBUTE_UNUSED;
533 fragS *fragP;
534 #else
535 static void
536 cvt_frag_to_fill (headersP, sec, fragP)
537 object_headers *headersP;
538 segT sec;
539 fragS *fragP;
540 #endif
542 switch (fragP->fr_type)
544 case rs_align:
545 case rs_align_code:
546 case rs_align_test:
547 case rs_org:
548 case rs_space:
549 #ifdef HANDLE_ALIGN
550 HANDLE_ALIGN (fragP);
551 #endif
552 know (fragP->fr_next != NULL);
553 fragP->fr_offset = (fragP->fr_next->fr_address
554 - fragP->fr_address
555 - fragP->fr_fix) / fragP->fr_var;
556 if (fragP->fr_offset < 0)
558 as_bad_where (fragP->fr_file, fragP->fr_line,
559 _("attempt to .org/.space backwards? (%ld)"),
560 (long) fragP->fr_offset);
561 fragP->fr_offset = 0;
563 fragP->fr_type = rs_fill;
564 break;
566 case rs_fill:
567 break;
569 case rs_leb128:
571 valueT value = S_GET_VALUE (fragP->fr_symbol);
572 int size;
574 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
575 fragP->fr_subtype);
577 fragP->fr_fix += size;
578 fragP->fr_type = rs_fill;
579 fragP->fr_var = 0;
580 fragP->fr_offset = 0;
581 fragP->fr_symbol = NULL;
583 break;
585 case rs_cfa:
586 eh_frame_convert_frag (fragP);
587 break;
589 case rs_dwarf2dbg:
590 dwarf2dbg_convert_frag (fragP);
591 break;
593 case rs_machine_dependent:
594 #ifdef BFD_ASSEMBLER
595 md_convert_frag (stdoutput, sec, fragP);
596 #else
597 md_convert_frag (headersP, sec, fragP);
598 #endif
600 assert (fragP->fr_next == NULL
601 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
602 == fragP->fr_fix));
604 /* After md_convert_frag, we make the frag into a ".space 0".
605 md_convert_frag() should set up any fixSs and constants
606 required. */
607 frag_wane (fragP);
608 break;
610 #ifndef WORKING_DOT_WORD
611 case rs_broken_word:
613 struct broken_word *lie;
615 if (fragP->fr_subtype)
617 fragP->fr_fix += md_short_jump_size;
618 for (lie = (struct broken_word *) (fragP->fr_symbol);
619 lie && lie->dispfrag == fragP;
620 lie = lie->next_broken_word)
621 if (lie->added == 1)
622 fragP->fr_fix += md_long_jump_size;
624 frag_wane (fragP);
626 break;
627 #endif
629 default:
630 BAD_CASE (fragP->fr_type);
631 break;
635 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
637 #ifdef BFD_ASSEMBLER
638 static void relax_seg PARAMS ((bfd *, asection *, PTR));
639 static void
640 relax_seg (abfd, sec, xxx)
641 bfd *abfd ATTRIBUTE_UNUSED;
642 asection *sec;
643 PTR xxx;
645 segment_info_type *seginfo = seg_info (sec);
647 if (seginfo && seginfo->frchainP
648 && relax_segment (seginfo->frchainP->frch_root, sec))
650 int *result = (int *) xxx;
651 *result = 1;
655 static void size_seg PARAMS ((bfd *, asection *, PTR));
656 static void
657 size_seg (abfd, sec, xxx)
658 bfd *abfd;
659 asection *sec;
660 PTR xxx ATTRIBUTE_UNUSED;
662 flagword flags;
663 fragS *fragp;
664 segment_info_type *seginfo;
665 int x;
666 valueT size, newsize;
668 subseg_change (sec, 0);
670 seginfo = seg_info (sec);
671 if (seginfo && seginfo->frchainP)
673 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
674 cvt_frag_to_fill (sec, fragp);
675 for (fragp = seginfo->frchainP->frch_root;
676 fragp->fr_next;
677 fragp = fragp->fr_next)
678 /* Walk to last elt. */
680 size = fragp->fr_address + fragp->fr_fix;
682 else
683 size = 0;
685 flags = bfd_get_section_flags (abfd, sec);
687 if (size > 0 && ! seginfo->bss)
688 flags |= SEC_HAS_CONTENTS;
690 /* @@ This is just an approximation. */
691 if (seginfo && seginfo->fix_root)
692 flags |= SEC_RELOC;
693 else
694 flags &= ~SEC_RELOC;
695 x = bfd_set_section_flags (abfd, sec, flags);
696 assert (x == true);
698 newsize = md_section_align (sec, size);
699 x = bfd_set_section_size (abfd, sec, newsize);
700 assert (x == true);
702 /* If the size had to be rounded up, add some padding in the last
703 non-empty frag. */
704 assert (newsize >= size);
705 if (size != newsize)
707 fragS *last = seginfo->frchainP->frch_last;
708 fragp = seginfo->frchainP->frch_root;
709 while (fragp->fr_next != last)
710 fragp = fragp->fr_next;
711 last->fr_address = size;
712 if ((newsize - size) % fragp->fr_var == 0)
713 fragp->fr_offset += (newsize - size) / fragp->fr_var;
714 else
715 /* If we hit this abort, it's likely due to subsegs_finish not
716 providing sufficient alignment on the last frag, and the
717 machine dependent code using alignment frags with fr_var
718 greater than 1. */
719 abort ();
722 #ifdef tc_frob_section
723 tc_frob_section (sec);
724 #endif
725 #ifdef obj_frob_section
726 obj_frob_section (sec);
727 #endif
730 #ifdef DEBUG2
731 static void
732 dump_section_relocs (abfd, sec, stream_)
733 bfd *abfd ATTRIBUTE_UNUSED;
734 asection *sec;
735 char *stream_;
737 FILE *stream = (FILE *) stream_;
738 segment_info_type *seginfo = seg_info (sec);
739 fixS *fixp = seginfo->fix_root;
741 if (!fixp)
742 return;
744 fprintf (stream, "sec %s relocs:\n", sec->name);
745 while (fixp)
747 symbolS *s = fixp->fx_addsy;
749 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
750 (int) fixp->fx_r_type);
751 if (s == NULL)
752 fprintf (stream, "no sym\n");
753 else
755 print_symbol_value_1 (stream, s);
756 fprintf (stream, "\n");
758 fixp = fixp->fx_next;
761 #else
762 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
763 #endif
765 #ifndef EMIT_SECTION_SYMBOLS
766 #define EMIT_SECTION_SYMBOLS 1
767 #endif
769 /* This pass over fixups decides whether symbols can be replaced with
770 section symbols. */
772 static void
773 adjust_reloc_syms (abfd, sec, xxx)
774 bfd *abfd ATTRIBUTE_UNUSED;
775 asection *sec;
776 PTR xxx ATTRIBUTE_UNUSED;
778 segment_info_type *seginfo = seg_info (sec);
779 fixS *fixp;
781 if (seginfo == NULL)
782 return;
784 dump_section_relocs (abfd, sec, stderr);
786 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
787 if (fixp->fx_done)
788 /* Ignore it. */
790 else if (fixp->fx_addsy)
792 symbolS *sym;
793 asection *symsec;
795 #ifdef DEBUG5
796 fprintf (stderr, "\n\nadjusting fixup:\n");
797 print_fixup (fixp);
798 #endif
800 sym = fixp->fx_addsy;
802 /* All symbols should have already been resolved at this
803 point. It is possible to see unresolved expression
804 symbols, though, since they are not in the regular symbol
805 table. */
806 resolve_symbol_value (sym);
808 if (fixp->fx_subsy != NULL)
809 resolve_symbol_value (fixp->fx_subsy);
811 /* If this symbol is equated to an undefined symbol, convert
812 the fixup to being against that symbol. */
813 if (symbol_equated_reloc_p (sym))
815 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
816 sym = symbol_get_value_expression (sym)->X_add_symbol;
817 fixp->fx_addsy = sym;
820 if (symbol_mri_common_p (sym))
822 /* These symbols are handled specially in fixup_segment. */
823 continue;
826 /* If the symbol is undefined, common, weak, or global (ELF
827 shared libs), we can't replace it with the section symbol. */
828 if (S_FORCE_RELOC (fixp->fx_addsy))
829 continue;
831 /* Is there some other (target cpu dependent) reason we can't adjust
832 this one? (E.g. relocations involving function addresses on
833 the PA. */
834 #ifdef tc_fix_adjustable
835 if (! tc_fix_adjustable (fixp))
836 continue;
837 #endif
839 /* Since we're reducing to section symbols, don't attempt to reduce
840 anything that's already using one. */
841 if (symbol_section_p (sym))
842 continue;
844 symsec = S_GET_SEGMENT (sym);
845 if (symsec == NULL)
846 abort ();
848 if (bfd_is_abs_section (symsec))
850 /* The fixup_segment routine normally will not use this
851 symbol in a relocation. */
852 continue;
855 /* Don't try to reduce relocs which refer to non-local symbols
856 in .linkonce sections. It can lead to confusion when a
857 debugging section refers to a .linkonce section. I hope
858 this will always be correct. */
859 if (symsec != sec && ! S_IS_LOCAL (sym))
861 if ((symsec->flags & SEC_LINK_ONCE) != 0
862 || (IS_ELF
863 /* The GNU toolchain uses an extension for ELF: a
864 section beginning with the magic string
865 .gnu.linkonce is a linkonce section. */
866 && strncmp (segment_name (symsec), ".gnu.linkonce",
867 sizeof ".gnu.linkonce" - 1) == 0))
868 continue;
871 /* Never adjust a reloc against local symbol in a merge section
872 with non-zero addend. */
873 if ((symsec->flags & SEC_MERGE) != 0 && fixp->fx_offset != 0)
874 continue;
876 /* Never adjust a reloc against TLS local symbol. */
877 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
878 continue;
880 /* We refetch the segment when calling section_symbol, rather
881 than using symsec, because S_GET_VALUE may wind up changing
882 the section when it calls resolve_symbol_value. */
883 fixp->fx_offset += S_GET_VALUE (sym);
884 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
885 #ifdef DEBUG5
886 fprintf (stderr, "\nadjusted fixup:\n");
887 print_fixup (fixp);
888 #endif
891 dump_section_relocs (abfd, sec, stderr);
894 static void
895 fix_segment (abfd, sec, xxx)
896 bfd *abfd ATTRIBUTE_UNUSED;
897 asection *sec;
898 PTR xxx ATTRIBUTE_UNUSED;
900 segment_info_type *seginfo = seg_info (sec);
902 fixup_segment (seginfo->fix_root, sec);
905 static void
906 write_relocs (abfd, sec, xxx)
907 bfd *abfd;
908 asection *sec;
909 PTR xxx ATTRIBUTE_UNUSED;
911 segment_info_type *seginfo = seg_info (sec);
912 unsigned int i;
913 unsigned int n;
914 arelent **relocs;
915 fixS *fixp;
916 char *err;
918 /* If seginfo is NULL, we did not create this section; don't do
919 anything with it. */
920 if (seginfo == NULL)
921 return;
923 n = 0;
924 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
925 n++;
927 #ifndef RELOC_EXPANSION_POSSIBLE
928 /* Set up reloc information as well. */
929 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
930 memset ((char *) relocs, 0, n * sizeof (arelent *));
932 i = 0;
933 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
935 arelent *reloc;
936 bfd_reloc_status_type s;
937 symbolS *sym;
939 if (fixp->fx_done)
941 n--;
942 continue;
945 /* If this is an undefined symbol which was equated to another
946 symbol, then generate the reloc against the latter symbol
947 rather than the former. */
948 sym = fixp->fx_addsy;
949 while (symbol_equated_reloc_p (sym))
951 symbolS *n;
953 /* We must avoid looping, as that can occur with a badly
954 written program. */
955 n = symbol_get_value_expression (sym)->X_add_symbol;
956 if (n == sym)
957 break;
958 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
959 sym = n;
961 fixp->fx_addsy = sym;
963 reloc = tc_gen_reloc (sec, fixp);
964 if (!reloc)
966 n--;
967 continue;
970 #if 0
971 /* This test is triggered inappropriately for the SH. */
972 if (fixp->fx_where + fixp->fx_size
973 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
974 abort ();
975 #endif
977 s = bfd_install_relocation (stdoutput, reloc,
978 fixp->fx_frag->fr_literal,
979 fixp->fx_frag->fr_address,
980 sec, &err);
981 switch (s)
983 case bfd_reloc_ok:
984 break;
985 case bfd_reloc_overflow:
986 as_bad_where (fixp->fx_file, fixp->fx_line,
987 _("relocation overflow"));
988 break;
989 case bfd_reloc_outofrange:
990 as_bad_where (fixp->fx_file, fixp->fx_line,
991 _("relocation out of range"));
992 break;
993 default:
994 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
995 fixp->fx_file, fixp->fx_line, s);
997 relocs[i++] = reloc;
999 #else
1000 n = n * MAX_RELOC_EXPANSION;
1001 /* Set up reloc information as well. */
1002 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1004 i = 0;
1005 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1007 arelent **reloc;
1008 char *data;
1009 bfd_reloc_status_type s;
1010 symbolS *sym;
1011 int j;
1013 if (fixp->fx_done)
1015 n--;
1016 continue;
1019 /* If this is an undefined symbol which was equated to another
1020 symbol, then generate the reloc against the latter symbol
1021 rather than the former. */
1022 sym = fixp->fx_addsy;
1023 while (symbol_equated_reloc_p (sym))
1025 symbolS *n;
1027 /* We must avoid looping, as that can occur with a badly
1028 written program. */
1029 n = symbol_get_value_expression (sym)->X_add_symbol;
1030 if (n == sym)
1031 break;
1032 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1033 sym = n;
1035 fixp->fx_addsy = sym;
1037 reloc = tc_gen_reloc (sec, fixp);
1039 for (j = 0; reloc[j]; j++)
1041 relocs[i++] = reloc[j];
1042 assert (i <= n);
1044 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1045 if (fixp->fx_where + fixp->fx_size
1046 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1047 as_bad_where (fixp->fx_file, fixp->fx_line,
1048 _("internal error: fixup not contained within frag"));
1049 for (j = 0; reloc[j]; j++)
1051 s = bfd_install_relocation (stdoutput, reloc[j],
1052 fixp->fx_frag->fr_literal,
1053 fixp->fx_frag->fr_address,
1054 sec, &err);
1055 switch (s)
1057 case bfd_reloc_ok:
1058 break;
1059 case bfd_reloc_overflow:
1060 as_bad_where (fixp->fx_file, fixp->fx_line,
1061 _("relocation overflow"));
1062 break;
1063 case bfd_reloc_outofrange:
1064 as_bad_where (fixp->fx_file, fixp->fx_line,
1065 _("relocation out of range"));
1066 break;
1067 default:
1068 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1069 fixp->fx_file, fixp->fx_line, s);
1073 n = i;
1074 #endif
1076 #ifdef DEBUG4
1078 int i, j, nsyms;
1079 asymbol **sympp;
1080 sympp = bfd_get_outsymbols (stdoutput);
1081 nsyms = bfd_get_symcount (stdoutput);
1082 for (i = 0; i < n; i++)
1083 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1085 for (j = 0; j < nsyms; j++)
1086 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1087 break;
1088 if (j == nsyms)
1089 abort ();
1092 #endif
1094 if (n)
1095 bfd_set_reloc (stdoutput, sec, relocs, n);
1096 else
1097 bfd_set_section_flags (abfd, sec,
1098 (bfd_get_section_flags (abfd, sec)
1099 & (flagword) ~SEC_RELOC));
1101 #ifdef SET_SECTION_RELOCS
1102 SET_SECTION_RELOCS (sec, relocs, n);
1103 #endif
1105 #ifdef DEBUG3
1107 int i;
1108 arelent *r;
1109 asymbol *s;
1110 fprintf (stderr, "relocs for sec %s\n", sec->name);
1111 for (i = 0; i < n; i++)
1113 r = relocs[i];
1114 s = *r->sym_ptr_ptr;
1115 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1116 i, r, r->address, s->name, r->addend);
1119 #endif
1122 static void
1123 write_contents (abfd, sec, xxx)
1124 bfd *abfd ATTRIBUTE_UNUSED;
1125 asection *sec;
1126 PTR xxx ATTRIBUTE_UNUSED;
1128 segment_info_type *seginfo = seg_info (sec);
1129 unsigned long offset = 0;
1130 fragS *f;
1132 /* Write out the frags. */
1133 if (seginfo == NULL
1134 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1135 return;
1137 for (f = seginfo->frchainP->frch_root;
1139 f = f->fr_next)
1141 int x;
1142 unsigned long fill_size;
1143 char *fill_literal;
1144 long count;
1146 assert (f->fr_type == rs_fill);
1147 if (f->fr_fix)
1149 x = bfd_set_section_contents (stdoutput, sec,
1150 f->fr_literal, (file_ptr) offset,
1151 (bfd_size_type) f->fr_fix);
1152 if (x == false)
1154 bfd_perror (stdoutput->filename);
1155 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1156 exit (EXIT_FAILURE);
1158 offset += f->fr_fix;
1160 fill_literal = f->fr_literal + f->fr_fix;
1161 fill_size = f->fr_var;
1162 count = f->fr_offset;
1163 assert (count >= 0);
1164 if (fill_size && count)
1166 char buf[256];
1167 if (fill_size > sizeof (buf))
1169 /* Do it the old way. Can this ever happen? */
1170 while (count--)
1172 x = bfd_set_section_contents (stdoutput, sec,
1173 fill_literal,
1174 (file_ptr) offset,
1175 (bfd_size_type) fill_size);
1176 if (x == false)
1178 bfd_perror (stdoutput->filename);
1179 as_perror (_("FATAL: Can't write %s"),
1180 stdoutput->filename);
1181 exit (EXIT_FAILURE);
1183 offset += fill_size;
1186 else
1188 /* Build a buffer full of fill objects and output it as
1189 often as necessary. This saves on the overhead of
1190 potentially lots of bfd_set_section_contents calls. */
1191 int n_per_buf, i;
1192 if (fill_size == 1)
1194 n_per_buf = sizeof (buf);
1195 memset (buf, *fill_literal, n_per_buf);
1197 else
1199 char *bufp;
1200 n_per_buf = sizeof (buf) / fill_size;
1201 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1202 memcpy (bufp, fill_literal, fill_size);
1204 for (; count > 0; count -= n_per_buf)
1206 n_per_buf = n_per_buf > count ? count : n_per_buf;
1207 x = bfd_set_section_contents
1208 (stdoutput, sec, buf, (file_ptr) offset,
1209 (bfd_size_type) n_per_buf * fill_size);
1210 if (x != true)
1211 as_fatal (_("cannot write to output file"));
1212 offset += n_per_buf * fill_size;
1218 #endif
1220 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1221 static void
1222 merge_data_into_text ()
1224 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1225 seg_info (text_section)->frchainP->frch_last->fr_next =
1226 seg_info (data_section)->frchainP->frch_root;
1227 seg_info (text_section)->frchainP->frch_last =
1228 seg_info (data_section)->frchainP->frch_last;
1229 seg_info (data_section)->frchainP = 0;
1230 #else
1231 fixS *tmp;
1233 text_last_frag->fr_next = data_frag_root;
1234 text_last_frag = data_last_frag;
1235 data_last_frag = NULL;
1236 data_frag_root = NULL;
1237 if (text_fix_root)
1239 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1240 tmp->fx_next = data_fix_root;
1241 text_fix_tail = data_fix_tail;
1243 else
1244 text_fix_root = data_fix_root;
1245 data_fix_root = NULL;
1246 #endif
1248 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1250 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1251 static void
1252 relax_and_size_all_segments ()
1254 fragS *fragP;
1256 relax_segment (text_frag_root, SEG_TEXT);
1257 relax_segment (data_frag_root, SEG_DATA);
1258 relax_segment (bss_frag_root, SEG_BSS);
1260 /* Now the addresses of frags are correct within the segment. */
1261 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1262 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1263 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1265 /* Join the 2 segments into 1 huge segment.
1266 To do this, re-compute every rn_address in the SEG_DATA frags.
1267 Then join the data frags after the text frags.
1269 Determine a_data [length of data segment]. */
1270 if (data_frag_root)
1272 register relax_addressT slide;
1274 know ((text_last_frag->fr_type == rs_fill)
1275 && (text_last_frag->fr_offset == 0));
1277 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1278 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1279 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1280 #ifdef OBJ_BOUT
1281 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1282 /* For b.out: If the data section has a strict alignment
1283 requirement, its load address in the .o file will be
1284 rounded up from the size of the text section. These
1285 two values are *not* the same! Similarly for the bss
1286 section.... */
1287 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1288 #endif
1290 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1291 fragP->fr_address += slide;
1293 know (text_last_frag != 0);
1294 text_last_frag->fr_next = data_frag_root;
1296 else
1298 H_SET_DATA_SIZE (&headers, 0);
1301 #ifdef OBJ_BOUT
1302 /* See above comments on b.out data section address. */
1304 long bss_vma;
1305 if (data_last_frag == 0)
1306 bss_vma = H_GET_TEXT_SIZE (&headers);
1307 else
1308 bss_vma = data_last_frag->fr_address;
1309 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1310 bss_address_frag.fr_address = bss_vma;
1312 #else /* ! OBJ_BOUT */
1313 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1314 H_GET_DATA_SIZE (&headers));
1316 #endif /* ! OBJ_BOUT */
1318 /* Slide all the frags. */
1319 if (bss_frag_root)
1321 relax_addressT slide = bss_address_frag.fr_address;
1323 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1324 fragP->fr_address += slide;
1327 if (bss_last_frag)
1328 H_SET_BSS_SIZE (&headers,
1329 bss_last_frag->fr_address - bss_frag_root->fr_address);
1330 else
1331 H_SET_BSS_SIZE (&headers, 0);
1333 #endif /* ! BFD_ASSEMBLER && ! BFD */
1335 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1337 #ifdef BFD_ASSEMBLER
1338 static void
1339 set_symtab ()
1341 int nsyms;
1342 asymbol **asympp;
1343 symbolS *symp;
1344 boolean result;
1345 extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
1347 /* Count symbols. We can't rely on a count made by the loop in
1348 write_object_file, because *_frob_file may add a new symbol or
1349 two. */
1350 nsyms = 0;
1351 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1352 nsyms++;
1354 if (nsyms)
1356 int i;
1357 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1359 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1360 symp = symbol_rootP;
1361 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1363 asympp[i] = symbol_get_bfdsym (symp);
1364 symbol_mark_written (symp);
1367 else
1368 asympp = 0;
1369 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1370 assert (result == true);
1371 symbol_table_frozen = 1;
1373 #endif
1375 /* Finish the subsegments. After every sub-segment, we fake an
1376 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1377 ".fill 0" because that is the kind of frag that requires least
1378 thought. ".align" frags like to have a following frag since that
1379 makes calculating their intended length trivial. */
1381 #ifndef SUB_SEGMENT_ALIGN
1382 #ifdef HANDLE_ALIGN
1383 /* The last subsegment gets an aligment corresponding to the alignment
1384 of the section. This allows proper nop-filling at the end of
1385 code-bearing sections. */
1386 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1387 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1388 ? get_recorded_alignment (SEG) : 0)
1389 #else
1390 #ifdef BFD_ASSEMBLER
1391 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1392 #else
1393 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1394 #endif
1395 #endif
1396 #endif
1398 void
1399 subsegs_finish ()
1401 struct frchain *frchainP;
1403 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1405 int alignment = 0;
1407 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1409 /* This now gets called even if we had errors. In that case,
1410 any alignment is meaningless, and, moreover, will look weird
1411 if we are generating a listing. */
1412 if (!had_errors ())
1413 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1415 if (subseg_text_p (now_seg))
1416 frag_align_code (alignment, 0);
1417 else
1418 frag_align (alignment, 0, 0);
1420 /* frag_align will have left a new frag.
1421 Use this last frag for an empty ".fill".
1423 For this segment ...
1424 Create a last frag. Do not leave a "being filled in frag". */
1425 frag_wane (frag_now);
1426 frag_now->fr_fix = 0;
1427 know (frag_now->fr_next == NULL);
1431 /* Write the object file. */
1433 void
1434 write_object_file ()
1436 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1437 fragS *fragP; /* Track along all frags. */
1438 #endif
1440 /* Do we really want to write it? */
1442 int n_warns, n_errs;
1443 n_warns = had_warnings ();
1444 n_errs = had_errors ();
1445 /* The -Z flag indicates that an object file should be generated,
1446 regardless of warnings and errors. */
1447 if (flag_always_generate_output)
1449 if (n_warns || n_errs)
1450 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1451 n_errs, n_errs == 1 ? "" : "s",
1452 n_warns, n_warns == 1 ? "" : "s");
1454 else
1456 if (n_errs)
1457 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1458 n_errs, n_errs == 1 ? "" : "s",
1459 n_warns, n_warns == 1 ? "" : "s");
1463 #ifdef OBJ_VMS
1464 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1465 a routine to check for the definition of the procedure "_main",
1466 and if so -- fix it up so that it can be program entry point. */
1467 vms_check_for_main ();
1468 #endif /* OBJ_VMS */
1470 /* From now on, we don't care about sub-segments. Build one frag chain
1471 for each segment. Linked thru fr_next. */
1473 #ifdef BFD_ASSEMBLER
1474 /* Remove the sections created by gas for its own purposes. */
1476 asection **seclist;
1477 int i;
1479 seclist = &stdoutput->sections;
1480 while (*seclist)
1482 if (*seclist == reg_section || *seclist == expr_section)
1484 bfd_section_list_remove (stdoutput, seclist);
1485 stdoutput->section_count--;
1487 else
1488 seclist = &(*seclist)->next;
1490 i = 0;
1491 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1494 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1495 #else
1496 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1497 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1498 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1499 #endif
1501 /* We have two segments. If user gave -R flag, then we must put the
1502 data frags into the text segment. Do this before relaxing so
1503 we know to take advantage of -R and make shorter addresses. */
1504 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1505 if (flag_readonly_data_in_text)
1507 merge_data_into_text ();
1509 #endif
1511 #ifdef BFD_ASSEMBLER
1512 while (1)
1514 int changed;
1516 #ifndef WORKING_DOT_WORD
1517 /* We need to reset the markers in the broken word list and
1518 associated frags between calls to relax_segment (via
1519 relax_seg). Since the broken word list is global, we do it
1520 once per round, rather than locally in relax_segment for each
1521 segment. */
1522 struct broken_word *brokp;
1524 for (brokp = broken_words;
1525 brokp != (struct broken_word *) NULL;
1526 brokp = brokp->next_broken_word)
1528 brokp->added = 0;
1530 if (brokp->dispfrag != (fragS *) NULL
1531 && brokp->dispfrag->fr_type == rs_broken_word)
1532 brokp->dispfrag->fr_subtype = 0;
1534 #endif
1536 changed = 0;
1537 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1538 if (!changed)
1539 break;
1542 /* Note - Most ports will use the default value of
1543 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1544 local symbols to be resolved, removing their frag information.
1545 Some ports however, will not have finished relaxing all of
1546 their frags and will still need the local symbol frag
1547 information. These ports can set
1548 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1549 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1551 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1552 #else
1553 relax_and_size_all_segments ();
1554 #endif /* BFD_ASSEMBLER */
1556 /* Relaxation has completed. Freeze all syms. */
1557 finalize_syms = 1;
1559 #ifndef BFD_ASSEMBLER
1560 /* Crawl the symbol chain.
1562 For each symbol whose value depends on a frag, take the address of
1563 that frag and subsume it into the value of the symbol.
1564 After this, there is just one way to lookup a symbol value.
1565 Values are left in their final state for object file emission.
1566 We adjust the values of 'L' local symbols, even if we do
1567 not intend to emit them to the object file, because their values
1568 are needed for fix-ups.
1570 Unless we saw a -L flag, remove all symbols that begin with 'L'
1571 from the symbol chain. (They are still pointed to by the fixes.)
1573 Count the remaining symbols.
1574 Assign a symbol number to each symbol.
1575 Count the number of string-table chars we will emit.
1576 Put this info into the headers as appropriate. */
1577 know (zero_address_frag.fr_address == 0);
1578 string_byte_count = sizeof (string_byte_count);
1580 obj_crawl_symbol_chain (&headers);
1582 if (string_byte_count == sizeof (string_byte_count))
1583 string_byte_count = 0;
1585 H_SET_STRING_SIZE (&headers, string_byte_count);
1587 /* Addresses of frags now reflect addresses we use in the object file.
1588 Symbol values are correct.
1589 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1590 Also converting any machine-dependent frags using md_convert_frag(); */
1591 subseg_change (SEG_TEXT, 0);
1593 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1595 /* At this point we have linked all the frags into a single
1596 chain. However, cvt_frag_to_fill may call md_convert_frag
1597 which may call fix_new. We need to ensure that fix_new adds
1598 the fixup to the right section. */
1599 if (fragP == data_frag_root)
1600 subseg_change (SEG_DATA, 0);
1602 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1604 /* Some assert macros don't work with # directives mixed in. */
1605 #ifndef NDEBUG
1606 if (!(fragP->fr_next == NULL
1607 #ifdef OBJ_BOUT
1608 || fragP->fr_next == data_frag_root
1609 #endif
1610 || ((fragP->fr_next->fr_address - fragP->fr_address)
1611 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1612 abort ();
1613 #endif
1615 #endif /* ! BFD_ASSEMBLER */
1617 #ifndef WORKING_DOT_WORD
1619 struct broken_word *lie;
1620 struct broken_word **prevP;
1622 prevP = &broken_words;
1623 for (lie = broken_words; lie; lie = lie->next_broken_word)
1624 if (!lie->added)
1626 expressionS exp;
1628 subseg_change (lie->seg, lie->subseg);
1629 exp.X_op = O_subtract;
1630 exp.X_add_symbol = lie->add;
1631 exp.X_op_symbol = lie->sub;
1632 exp.X_add_number = lie->addnum;
1633 #ifdef BFD_ASSEMBLER
1634 #ifdef TC_CONS_FIX_NEW
1635 TC_CONS_FIX_NEW (lie->frag,
1636 lie->word_goes_here - lie->frag->fr_literal,
1637 2, &exp);
1638 #else
1639 fix_new_exp (lie->frag,
1640 lie->word_goes_here - lie->frag->fr_literal,
1641 2, &exp, 0, BFD_RELOC_16);
1642 #endif
1643 #else
1644 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1645 fix_new_exp (lie->frag,
1646 lie->word_goes_here - lie->frag->fr_literal,
1647 2, &exp, 0, NO_RELOC);
1648 #else
1649 #ifdef TC_NS32K
1650 fix_new_ns32k_exp (lie->frag,
1651 lie->word_goes_here - lie->frag->fr_literal,
1652 2, &exp, 0, 0, 2, 0, 0);
1653 #else
1654 fix_new_exp (lie->frag,
1655 lie->word_goes_here - lie->frag->fr_literal,
1656 2, &exp, 0, 0);
1657 #endif /* TC_NS32K */
1658 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1659 #endif /* BFD_ASSEMBLER */
1660 *prevP = lie->next_broken_word;
1662 else
1663 prevP = &(lie->next_broken_word);
1665 for (lie = broken_words; lie;)
1667 struct broken_word *untruth;
1668 char *table_ptr;
1669 addressT table_addr;
1670 addressT from_addr, to_addr;
1671 int n, m;
1673 subseg_change (lie->seg, lie->subseg);
1674 fragP = lie->dispfrag;
1676 /* Find out how many broken_words go here. */
1677 n = 0;
1678 for (untruth = lie;
1679 untruth && untruth->dispfrag == fragP;
1680 untruth = untruth->next_broken_word)
1681 if (untruth->added == 1)
1682 n++;
1684 table_ptr = lie->dispfrag->fr_opcode;
1685 table_addr = (lie->dispfrag->fr_address
1686 + (table_ptr - lie->dispfrag->fr_literal));
1687 /* Create the jump around the long jumps. This is a short
1688 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1689 from_addr = table_addr;
1690 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1691 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1692 lie->add);
1693 table_ptr += md_short_jump_size;
1694 table_addr += md_short_jump_size;
1696 for (m = 0;
1697 lie && lie->dispfrag == fragP;
1698 m++, lie = lie->next_broken_word)
1700 if (lie->added == 2)
1701 continue;
1702 /* Patch the jump table. */
1703 /* This is the offset from ??? to table_ptr+0. */
1704 to_addr = table_addr - S_GET_VALUE (lie->sub);
1705 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1706 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1707 #endif
1708 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1709 for (untruth = lie->next_broken_word;
1710 untruth && untruth->dispfrag == fragP;
1711 untruth = untruth->next_broken_word)
1713 if (untruth->use_jump == lie)
1714 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1717 /* Install the long jump. */
1718 /* This is a long jump from table_ptr+0 to the final target. */
1719 from_addr = table_addr;
1720 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1721 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1722 lie->add);
1723 table_ptr += md_long_jump_size;
1724 table_addr += md_long_jump_size;
1728 #endif /* not WORKING_DOT_WORD */
1730 #ifndef BFD_ASSEMBLER
1731 #ifndef OBJ_VMS
1732 { /* not vms */
1733 char *the_object_file;
1734 long object_file_size;
1735 /* Scan every FixS performing fixups. We had to wait until now to
1736 do this because md_convert_frag() may have made some fixSs. */
1737 int trsize, drsize;
1739 subseg_change (SEG_TEXT, 0);
1740 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1741 subseg_change (SEG_DATA, 0);
1742 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1743 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1745 /* FIXME: Move this stuff into the pre-write-hook. */
1746 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1747 H_SET_ENTRY_POINT (&headers, 0);
1749 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1751 object_file_size = H_GET_FILE_SIZE (&headers);
1752 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1754 output_file_create (out_file_name);
1756 obj_header_append (&next_object_file_charP, &headers);
1758 know ((next_object_file_charP - the_object_file)
1759 == H_GET_HEADER_SIZE (&headers));
1761 /* Emit code. */
1762 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1764 register long count;
1765 register char *fill_literal;
1766 register long fill_size;
1768 PROGRESS (1);
1769 know (fragP->fr_type == rs_fill);
1770 append (&next_object_file_charP, fragP->fr_literal,
1771 (unsigned long) fragP->fr_fix);
1772 fill_literal = fragP->fr_literal + fragP->fr_fix;
1773 fill_size = fragP->fr_var;
1774 know (fragP->fr_offset >= 0);
1776 for (count = fragP->fr_offset; count; count--)
1777 append (&next_object_file_charP, fill_literal,
1778 (unsigned long) fill_size);
1781 know ((next_object_file_charP - the_object_file)
1782 == (H_GET_HEADER_SIZE (&headers)
1783 + H_GET_TEXT_SIZE (&headers)
1784 + H_GET_DATA_SIZE (&headers)));
1786 /* Emit relocations. */
1787 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1788 (relax_addressT) 0);
1789 know ((next_object_file_charP - the_object_file)
1790 == (H_GET_HEADER_SIZE (&headers)
1791 + H_GET_TEXT_SIZE (&headers)
1792 + H_GET_DATA_SIZE (&headers)
1793 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1794 #ifdef TC_I960
1795 /* Make addresses in data relocation directives relative to beginning of
1796 first data fragment, not end of last text fragment: alignment of the
1797 start of the data segment may place a gap between the segments. */
1798 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1799 data0_frchainP->frch_root->fr_address);
1800 #else /* TC_I960 */
1801 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1802 text_last_frag->fr_address);
1803 #endif /* TC_I960 */
1805 know ((next_object_file_charP - the_object_file)
1806 == (H_GET_HEADER_SIZE (&headers)
1807 + H_GET_TEXT_SIZE (&headers)
1808 + H_GET_DATA_SIZE (&headers)
1809 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1810 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1812 /* Emit line number entries. */
1813 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1814 know ((next_object_file_charP - the_object_file)
1815 == (H_GET_HEADER_SIZE (&headers)
1816 + H_GET_TEXT_SIZE (&headers)
1817 + H_GET_DATA_SIZE (&headers)
1818 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1819 + H_GET_DATA_RELOCATION_SIZE (&headers)
1820 + H_GET_LINENO_SIZE (&headers)));
1822 /* Emit symbols. */
1823 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1824 know ((next_object_file_charP - the_object_file)
1825 == (H_GET_HEADER_SIZE (&headers)
1826 + H_GET_TEXT_SIZE (&headers)
1827 + H_GET_DATA_SIZE (&headers)
1828 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1829 + H_GET_DATA_RELOCATION_SIZE (&headers)
1830 + H_GET_LINENO_SIZE (&headers)
1831 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1833 /* Emit strings. */
1834 if (string_byte_count > 0)
1835 obj_emit_strings (&next_object_file_charP);
1837 #ifdef BFD_HEADERS
1838 bfd_seek (stdoutput, (file_ptr) 0, 0);
1839 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1840 #else
1842 /* Write the data to the file. */
1843 output_file_append (the_object_file, object_file_size, out_file_name);
1844 free (the_object_file);
1845 #endif
1847 #else /* OBJ_VMS */
1848 /* Now do the VMS-dependent part of writing the object file. */
1849 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1850 H_GET_DATA_SIZE (&headers),
1851 H_GET_BSS_SIZE (&headers),
1852 text_frag_root, data_frag_root);
1853 #endif /* OBJ_VMS */
1854 #else /* BFD_ASSEMBLER */
1856 /* Resolve symbol values. This needs to be done before processing
1857 the relocations. */
1858 if (symbol_rootP)
1860 symbolS *symp;
1862 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1863 resolve_symbol_value (symp);
1865 resolve_local_symbol_values ();
1867 PROGRESS (1);
1869 #ifdef tc_frob_file_before_adjust
1870 tc_frob_file_before_adjust ();
1871 #endif
1872 #ifdef obj_frob_file_before_adjust
1873 obj_frob_file_before_adjust ();
1874 #endif
1876 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1878 #ifdef tc_frob_file_before_fix
1879 tc_frob_file_before_fix ();
1880 #endif
1881 #ifdef obj_frob_file_before_fix
1882 obj_frob_file_before_fix ();
1883 #endif
1885 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1887 /* Set up symbol table, and write it out. */
1888 if (symbol_rootP)
1890 symbolS *symp;
1892 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1894 int punt = 0;
1895 const char *name;
1897 if (symbol_mri_common_p (symp))
1899 if (S_IS_EXTERNAL (symp))
1900 as_bad (_("%s: global symbols not supported in common sections"),
1901 S_GET_NAME (symp));
1902 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1903 continue;
1906 name = S_GET_NAME (symp);
1907 if (name)
1909 const char *name2 =
1910 decode_local_label_name ((char *) S_GET_NAME (symp));
1911 /* They only differ if `name' is a fb or dollar local
1912 label name. */
1913 if (name2 != name && ! S_IS_DEFINED (symp))
1914 as_bad (_("local label `%s' is not defined"), name2);
1917 /* Do it again, because adjust_reloc_syms might introduce
1918 more symbols. They'll probably only be section symbols,
1919 but they'll still need to have the values computed. */
1920 resolve_symbol_value (symp);
1922 /* Skip symbols which were equated to undefined or common
1923 symbols. */
1924 if (symbol_equated_reloc_p (symp))
1926 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1927 continue;
1930 /* So far, common symbols have been treated like undefined symbols.
1931 Put them in the common section now. */
1932 if (S_IS_DEFINED (symp) == 0
1933 && S_GET_VALUE (symp) != 0)
1934 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1935 #if 0
1936 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1937 S_GET_NAME (symp), symp,
1938 S_GET_VALUE (symp),
1939 symbol_get_bfdsym (symp)->flags,
1940 segment_name (S_GET_SEGMENT (symp)));
1941 #endif
1943 #ifdef obj_frob_symbol
1944 obj_frob_symbol (symp, punt);
1945 #endif
1946 #ifdef tc_frob_symbol
1947 if (! punt || symbol_used_in_reloc_p (symp))
1948 tc_frob_symbol (symp, punt);
1949 #endif
1951 /* If we don't want to keep this symbol, splice it out of
1952 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1953 want section symbols. Otherwise, we skip local symbols
1954 and symbols that the frob_symbol macros told us to punt,
1955 but we keep such symbols if they are used in relocs. */
1956 if (symp == abs_section_sym
1957 || (! EMIT_SECTION_SYMBOLS
1958 && symbol_section_p (symp))
1959 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1960 opposites. Sometimes the former checks flags and the
1961 latter examines the name... */
1962 || (!S_IS_EXTERN (symp)
1963 && (punt || S_IS_LOCAL (symp))
1964 && ! symbol_used_in_reloc_p (symp)))
1966 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1968 /* After symbol_remove, symbol_next(symp) still returns
1969 the one that came after it in the chain. So we don't
1970 need to do any extra cleanup work here. */
1971 continue;
1974 /* Make sure we really got a value for the symbol. */
1975 if (! symbol_resolved_p (symp))
1977 as_bad (_("can't resolve value for symbol `%s'"),
1978 S_GET_NAME (symp));
1979 symbol_mark_resolved (symp);
1982 /* Set the value into the BFD symbol. Up til now the value
1983 has only been kept in the gas symbolS struct. */
1984 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1988 PROGRESS (1);
1990 /* Now do any format-specific adjustments to the symbol table, such
1991 as adding file symbols. */
1992 #ifdef tc_adjust_symtab
1993 tc_adjust_symtab ();
1994 #endif
1995 #ifdef obj_adjust_symtab
1996 obj_adjust_symtab ();
1997 #endif
1999 /* Now that all the sizes are known, and contents correct, we can
2000 start writing to the file. */
2001 set_symtab ();
2003 /* If *_frob_file changes the symbol value at this point, it is
2004 responsible for moving the changed value into symp->bsym->value
2005 as well. Hopefully all symbol value changing can be done in
2006 *_frob_symbol. */
2007 #ifdef tc_frob_file
2008 tc_frob_file ();
2009 #endif
2010 #ifdef obj_frob_file
2011 obj_frob_file ();
2012 #endif
2014 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2016 #ifdef tc_frob_file_after_relocs
2017 tc_frob_file_after_relocs ();
2018 #endif
2019 #ifdef obj_frob_file_after_relocs
2020 obj_frob_file_after_relocs ();
2021 #endif
2023 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2024 #endif /* BFD_ASSEMBLER */
2026 #endif /* ! BFD */
2028 #ifdef TC_GENERIC_RELAX_TABLE
2030 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2032 long
2033 relax_frag (segment, fragP, stretch)
2034 segT segment;
2035 fragS *fragP;
2036 long stretch;
2038 const relax_typeS *this_type;
2039 const relax_typeS *start_type;
2040 relax_substateT next_state;
2041 relax_substateT this_state;
2042 long growth;
2043 offsetT aim;
2044 addressT target;
2045 addressT address;
2046 symbolS *symbolP;
2047 const relax_typeS *table;
2049 target = fragP->fr_offset;
2050 address = fragP->fr_address;
2051 table = TC_GENERIC_RELAX_TABLE;
2052 this_state = fragP->fr_subtype;
2053 start_type = this_type = table + this_state;
2054 symbolP = fragP->fr_symbol;
2056 if (symbolP)
2058 fragS *sym_frag;
2060 sym_frag = symbol_get_frag (symbolP);
2062 #ifndef DIFF_EXPR_OK
2063 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2064 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2065 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2066 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2067 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2068 #endif
2069 know (sym_frag != NULL);
2070 #endif
2071 know (S_GET_SEGMENT (symbolP) != absolute_section
2072 || sym_frag == &zero_address_frag);
2073 target += S_GET_VALUE (symbolP);
2075 /* If frag has yet to be reached on this pass,
2076 assume it will move by STRETCH just as we did.
2077 If this is not so, it will be because some frag
2078 between grows, and that will force another pass. */
2080 if (stretch != 0
2081 && sym_frag->relax_marker != fragP->relax_marker
2082 && S_GET_SEGMENT (symbolP) == segment)
2084 target += stretch;
2088 aim = target - address - fragP->fr_fix;
2089 #ifdef TC_PCREL_ADJUST
2090 /* Currently only the ns32k family needs this. */
2091 aim += TC_PCREL_ADJUST (fragP);
2092 /* #else */
2093 /* This machine doesn't want to use pcrel_adjust.
2094 In that case, pcrel_adjust should be zero. */
2095 #if 0
2096 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2097 #endif
2098 #endif
2099 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2100 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2101 #endif
2103 if (aim < 0)
2105 /* Look backwards. */
2106 for (next_state = this_type->rlx_more; next_state;)
2107 if (aim >= this_type->rlx_backward)
2108 next_state = 0;
2109 else
2111 /* Grow to next state. */
2112 this_state = next_state;
2113 this_type = table + this_state;
2114 next_state = this_type->rlx_more;
2117 else
2119 /* Look forwards. */
2120 for (next_state = this_type->rlx_more; next_state;)
2121 if (aim <= this_type->rlx_forward)
2122 next_state = 0;
2123 else
2125 /* Grow to next state. */
2126 this_state = next_state;
2127 this_type = table + this_state;
2128 next_state = this_type->rlx_more;
2132 growth = this_type->rlx_length - start_type->rlx_length;
2133 if (growth != 0)
2134 fragP->fr_subtype = this_state;
2135 return growth;
2138 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2140 /* Relax_align. Advance location counter to next address that has 'alignment'
2141 lowest order bits all 0s, return size of adjustment made. */
2142 static relax_addressT
2143 relax_align (address, alignment)
2144 register relax_addressT address; /* Address now. */
2145 register int alignment; /* Alignment (binary). */
2147 relax_addressT mask;
2148 relax_addressT new_address;
2150 mask = ~((~0) << alignment);
2151 new_address = (address + mask) & (~mask);
2152 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2153 if (linkrelax)
2154 /* We must provide lots of padding, so the linker can discard it
2155 when needed. The linker will not add extra space, ever. */
2156 new_address += (1 << alignment);
2157 #endif
2158 return (new_address - address);
2161 /* Now we have a segment, not a crowd of sub-segments, we can make
2162 fr_address values.
2164 Relax the frags.
2166 After this, all frags in this segment have addresses that are correct
2167 within the segment. Since segments live in different file addresses,
2168 these frag addresses may not be the same as final object-file
2169 addresses. */
2172 relax_segment (segment_frag_root, segment)
2173 struct frag *segment_frag_root;
2174 segT segment;
2176 register struct frag *fragP;
2177 register relax_addressT address;
2178 int ret;
2180 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2181 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2182 #endif
2183 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2184 subseg_change (segment, 0);
2186 /* For each frag in segment: count and store (a 1st guess of)
2187 fr_address. */
2188 address = 0;
2189 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2191 fragP->relax_marker = 0;
2192 fragP->fr_address = address;
2193 address += fragP->fr_fix;
2195 switch (fragP->fr_type)
2197 case rs_fill:
2198 address += fragP->fr_offset * fragP->fr_var;
2199 break;
2201 case rs_align:
2202 case rs_align_code:
2203 case rs_align_test:
2205 addressT offset = relax_align (address, (int) fragP->fr_offset);
2207 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2208 offset = 0;
2210 if (offset % fragP->fr_var != 0)
2212 as_bad_where (fragP->fr_file, fragP->fr_line,
2213 _("alignment padding (%lu bytes) not a multiple of %ld"),
2214 (unsigned long) offset, (long) fragP->fr_var);
2215 offset -= (offset % fragP->fr_var);
2218 address += offset;
2220 break;
2222 case rs_org:
2223 case rs_space:
2224 /* Assume .org is nugatory. It will grow with 1st relax. */
2225 break;
2227 case rs_machine_dependent:
2228 /* If fr_symbol is an expression, this call to
2229 resolve_symbol_value sets up the correct segment, which will
2230 likely be needed in md_estimate_size_before_relax. */
2231 if (fragP->fr_symbol)
2232 resolve_symbol_value (fragP->fr_symbol);
2234 address += md_estimate_size_before_relax (fragP, segment);
2235 break;
2237 #ifndef WORKING_DOT_WORD
2238 /* Broken words don't concern us yet. */
2239 case rs_broken_word:
2240 break;
2241 #endif
2243 case rs_leb128:
2244 /* Initial guess is always 1; doing otherwise can result in
2245 stable solutions that are larger than the minimum. */
2246 address += fragP->fr_offset = 1;
2247 break;
2249 case rs_cfa:
2250 address += eh_frame_estimate_size_before_relax (fragP);
2251 break;
2253 case rs_dwarf2dbg:
2254 address += dwarf2dbg_estimate_size_before_relax (fragP);
2255 break;
2257 default:
2258 BAD_CASE (fragP->fr_type);
2259 break;
2263 /* Do relax(). */
2265 long stretch; /* May be any size, 0 or negative. */
2266 /* Cumulative number of addresses we have relaxed this pass.
2267 We may have relaxed more than one address. */
2268 int stretched; /* Have we stretched on this pass? */
2269 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2270 grew, and another shrank. If a branch instruction doesn't fit anymore,
2271 we could be scrod. */
2275 stretch = 0;
2276 stretched = 0;
2278 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2280 long growth = 0;
2281 addressT was_address;
2282 offsetT offset;
2283 symbolS *symbolP;
2285 fragP->relax_marker ^= 1;
2286 was_address = fragP->fr_address;
2287 address = fragP->fr_address += stretch;
2288 symbolP = fragP->fr_symbol;
2289 offset = fragP->fr_offset;
2291 switch (fragP->fr_type)
2293 case rs_fill: /* .fill never relaxes. */
2294 growth = 0;
2295 break;
2297 #ifndef WORKING_DOT_WORD
2298 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2299 for it I do not want to write it. I do not want to have
2300 anything to do with it. This is not the proper way to
2301 implement this misfeature. */
2302 case rs_broken_word:
2304 struct broken_word *lie;
2305 struct broken_word *untruth;
2307 /* Yes this is ugly (storing the broken_word pointer
2308 in the symbol slot). Still, this whole chunk of
2309 code is ugly, and I don't feel like doing anything
2310 about it. Think of it as stubbornness in action. */
2311 growth = 0;
2312 for (lie = (struct broken_word *) (fragP->fr_symbol);
2313 lie && lie->dispfrag == fragP;
2314 lie = lie->next_broken_word)
2317 if (lie->added)
2318 continue;
2320 offset = (S_GET_VALUE (lie->add)
2321 + lie->addnum
2322 - S_GET_VALUE (lie->sub));
2323 if (offset <= -32768 || offset >= 32767)
2325 if (flag_warn_displacement)
2327 char buf[50];
2328 sprint_value (buf, (addressT) lie->addnum);
2329 as_warn_where (fragP->fr_file, fragP->fr_line,
2330 _(".word %s-%s+%s didn't fit"),
2331 S_GET_NAME (lie->add),
2332 S_GET_NAME (lie->sub),
2333 buf);
2335 lie->added = 1;
2336 if (fragP->fr_subtype == 0)
2338 fragP->fr_subtype++;
2339 growth += md_short_jump_size;
2341 for (untruth = lie->next_broken_word;
2342 untruth && untruth->dispfrag == lie->dispfrag;
2343 untruth = untruth->next_broken_word)
2344 if ((symbol_get_frag (untruth->add)
2345 == symbol_get_frag (lie->add))
2346 && (S_GET_VALUE (untruth->add)
2347 == S_GET_VALUE (lie->add)))
2349 untruth->added = 2;
2350 untruth->use_jump = lie;
2352 growth += md_long_jump_size;
2356 break;
2357 } /* case rs_broken_word */
2358 #endif
2359 case rs_align:
2360 case rs_align_code:
2361 case rs_align_test:
2363 addressT oldoff, newoff;
2365 oldoff = relax_align (was_address + fragP->fr_fix,
2366 (int) offset);
2367 newoff = relax_align (address + fragP->fr_fix,
2368 (int) offset);
2370 if (fragP->fr_subtype != 0)
2372 if (oldoff > fragP->fr_subtype)
2373 oldoff = 0;
2374 if (newoff > fragP->fr_subtype)
2375 newoff = 0;
2378 growth = newoff - oldoff;
2380 break;
2382 case rs_org:
2384 addressT target = offset;
2385 addressT after;
2387 if (symbolP)
2389 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2390 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2391 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2392 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2393 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2394 know (symbolP->sy_frag);
2395 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2396 || (symbolP->sy_frag == &zero_address_frag));
2397 #endif
2398 /* Convert from an actual address to an octet offset
2399 into the section. Here it is assumed that the
2400 section's VMA is zero, and can omit subtracting it
2401 from the symbol's value to get the address offset. */
2402 know (S_GET_SECTION (symbolP)->vma == 0);
2403 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2406 know (fragP->fr_next);
2407 after = fragP->fr_next->fr_address;
2408 growth = target - after;
2409 if (growth < 0)
2411 /* Growth may be negative, but variable part of frag
2412 cannot have fewer than 0 chars. That is, we can't
2413 .org backwards. */
2414 as_bad_where (fragP->fr_file, fragP->fr_line,
2415 _("attempt to .org backwards"));
2417 /* We've issued an error message. Change the
2418 frag to avoid cascading errors. */
2419 fragP->fr_type = rs_align;
2420 fragP->fr_subtype = 0;
2421 fragP->fr_offset = 0;
2422 fragP->fr_fix = after - address;
2423 growth = stretch;
2426 /* This is an absolute growth factor */
2427 growth -= stretch;
2428 break;
2431 case rs_space:
2432 growth = 0;
2433 if (symbolP)
2435 offsetT amount;
2437 amount = S_GET_VALUE (symbolP);
2438 if (S_GET_SEGMENT (symbolP) != absolute_section
2439 || S_IS_COMMON (symbolP)
2440 || ! S_IS_DEFINED (symbolP))
2442 as_bad_where (fragP->fr_file, fragP->fr_line,
2443 _(".space specifies non-absolute value"));
2444 /* Prevent repeat of this error message. */
2445 fragP->fr_symbol = 0;
2447 else if (amount < 0)
2449 as_warn_where (fragP->fr_file, fragP->fr_line,
2450 _(".space or .fill with negative value, ignored"));
2451 fragP->fr_symbol = 0;
2453 else
2454 growth = (was_address + fragP->fr_fix + amount
2455 - fragP->fr_next->fr_address);
2457 break;
2459 case rs_machine_dependent:
2460 #ifdef md_relax_frag
2461 growth = md_relax_frag (segment, fragP, stretch);
2462 #else
2463 #ifdef TC_GENERIC_RELAX_TABLE
2464 /* The default way to relax a frag is to look through
2465 TC_GENERIC_RELAX_TABLE. */
2466 growth = relax_frag (segment, fragP, stretch);
2467 #endif /* TC_GENERIC_RELAX_TABLE */
2468 #endif
2469 break;
2471 case rs_leb128:
2473 valueT value;
2474 int size;
2476 value = resolve_symbol_value (fragP->fr_symbol);
2477 size = sizeof_leb128 (value, fragP->fr_subtype);
2478 growth = size - fragP->fr_offset;
2479 fragP->fr_offset = size;
2481 break;
2483 case rs_cfa:
2484 growth = eh_frame_relax_frag (fragP);
2485 break;
2487 case rs_dwarf2dbg:
2488 growth = dwarf2dbg_relax_frag (fragP);
2489 break;
2491 default:
2492 BAD_CASE (fragP->fr_type);
2493 break;
2495 if (growth)
2497 stretch += growth;
2498 stretched = 1;
2500 } /* For each frag in the segment. */
2502 while (stretched); /* Until nothing further to relax. */
2503 } /* do_relax */
2505 ret = 0;
2506 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2507 if (fragP->last_fr_address != fragP->fr_address)
2509 fragP->last_fr_address = fragP->fr_address;
2510 ret = 1;
2512 return ret;
2515 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2517 /* fixup_segment()
2519 Go through all the fixS's in a segment and see which ones can be
2520 handled now. (These consist of fixS where we have since discovered
2521 the value of a symbol, or the address of the frag involved.)
2522 For each one, call md_apply_fix3 to put the fix into the frag data.
2524 Result is a count of how many relocation structs will be needed to
2525 handle the remaining fixS's that we couldn't completely handle here.
2526 These will be output later by emit_relocations(). */
2528 static long
2529 fixup_segment (fixP, this_segment)
2530 fixS *fixP;
2531 segT this_segment;
2533 long seg_reloc_count = 0;
2534 valueT add_number;
2535 fragS *fragP;
2536 segT add_symbol_segment = absolute_section;
2538 if (fixP != NULL && abs_section_sym == NULL)
2540 #ifndef BFD_ASSEMBLER
2541 abs_section_sym = &abs_symbol;
2542 #else
2543 abs_section_sym = section_symbol (absolute_section);
2544 #endif
2547 /* If the linker is doing the relaxing, we must not do any fixups.
2549 Well, strictly speaking that's not true -- we could do any that
2550 are PC-relative and don't cross regions that could change size.
2551 And for the i960 we might be able to turn callx/callj into bal
2552 anyways in cases where we know the maximum displacement. */
2553 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2555 for (; fixP; fixP = fixP->fx_next)
2556 if (!fixP->fx_done)
2558 if (fixP->fx_addsy == NULL)
2560 /* There was no symbol required by this relocation.
2561 However, BFD doesn't really handle relocations
2562 without symbols well. So fake up a local symbol in
2563 the absolute section. */
2564 fixP->fx_addsy = abs_section_sym;
2566 symbol_mark_used_in_reloc (fixP->fx_addsy);
2567 if (fixP->fx_subsy != NULL)
2568 symbol_mark_used_in_reloc (fixP->fx_subsy);
2569 seg_reloc_count++;
2571 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2572 return seg_reloc_count;
2575 for (; fixP; fixP = fixP->fx_next)
2577 #ifdef DEBUG5
2578 fprintf (stderr, "\nprocessing fixup:\n");
2579 print_fixup (fixP);
2580 #endif
2582 fragP = fixP->fx_frag;
2583 know (fragP);
2584 #ifdef TC_VALIDATE_FIX
2585 TC_VALIDATE_FIX (fixP, this_segment, skip);
2586 #endif
2587 add_number = fixP->fx_offset;
2589 if (fixP->fx_addsy != NULL
2590 && symbol_mri_common_p (fixP->fx_addsy))
2592 know (fixP->fx_addsy->sy_value.X_op == O_symbol);
2593 add_number += S_GET_VALUE (fixP->fx_addsy);
2594 fixP->fx_offset = add_number;
2595 fixP->fx_addsy
2596 = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2599 if (fixP->fx_addsy != NULL)
2600 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2602 if (fixP->fx_subsy != NULL)
2604 segT sub_symbol_segment;
2605 resolve_symbol_value (fixP->fx_subsy);
2606 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2607 if (fixP->fx_addsy != NULL
2608 && sub_symbol_segment == add_symbol_segment
2609 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2611 add_number += S_GET_VALUE (fixP->fx_addsy);
2612 add_number -= S_GET_VALUE (fixP->fx_subsy);
2613 fixP->fx_offset = add_number;
2614 /* If the back-end code has selected a pc-relative
2615 reloc, adjust the value to be pc-relative. */
2616 if (1
2617 #ifdef TC_M68K
2618 /* See the comment below about 68k weirdness. */
2619 && 0
2620 #endif
2621 && fixP->fx_pcrel)
2622 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2623 fixP->fx_addsy = NULL;
2624 fixP->fx_subsy = NULL;
2625 fixP->fx_pcrel = 0;
2627 else if (sub_symbol_segment == absolute_section
2628 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2630 add_number -= S_GET_VALUE (fixP->fx_subsy);
2631 fixP->fx_offset = add_number;
2632 fixP->fx_subsy = NULL;
2634 else if (sub_symbol_segment == this_segment
2635 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2637 add_number -= S_GET_VALUE (fixP->fx_subsy);
2638 fixP->fx_offset = (add_number + fixP->fx_dot_value
2639 + fixP->fx_frag->fr_address);
2641 /* Make it pc-relative. If the back-end code has not
2642 selected a pc-relative reloc, cancel the adjustment
2643 we do later on all pc-relative relocs. */
2644 if (0
2645 #ifdef TC_M68K
2646 /* Do this for m68k even if it's already described
2647 as pc-relative. On the m68k, an operand of
2648 "pc@(foo-.-2)" should address "foo" in a
2649 pc-relative mode. */
2650 || 1
2651 #endif
2652 || !fixP->fx_pcrel)
2653 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2654 fixP->fx_subsy = NULL;
2655 fixP->fx_pcrel = 1;
2657 else if (!TC_VALIDATE_FIX_SUB (fixP))
2659 as_bad_where (fixP->fx_file, fixP->fx_line,
2660 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2661 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2662 segment_name (add_symbol_segment),
2663 S_GET_NAME (fixP->fx_subsy),
2664 segment_name (sub_symbol_segment));
2668 if (fixP->fx_addsy)
2670 if (add_symbol_segment == this_segment
2671 && !TC_FORCE_RELOCATION_LOCAL (fixP))
2673 /* This fixup was made when the symbol's segment was
2674 SEG_UNKNOWN, but it is now in the local segment.
2675 So we know how to do the address without relocation. */
2676 add_number += S_GET_VALUE (fixP->fx_addsy);
2677 fixP->fx_offset = add_number;
2678 if (fixP->fx_pcrel)
2679 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2680 fixP->fx_addsy = NULL;
2681 fixP->fx_pcrel = 0;
2683 else if (add_symbol_segment == absolute_section
2684 && !TC_FORCE_RELOCATION_ABS (fixP))
2686 add_number += S_GET_VALUE (fixP->fx_addsy);
2687 fixP->fx_offset = add_number;
2688 fixP->fx_addsy = NULL;
2690 else if (add_symbol_segment != undefined_section
2691 #ifdef BFD_ASSEMBLER
2692 && ! bfd_is_com_section (add_symbol_segment)
2693 #endif
2694 && MD_APPLY_SYM_VALUE (fixP))
2695 add_number += S_GET_VALUE (fixP->fx_addsy);
2698 if (fixP->fx_pcrel)
2700 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2701 if (!fixP->fx_done && fixP->fx_addsy == NULL)
2703 /* There was no symbol required by this relocation.
2704 However, BFD doesn't really handle relocations
2705 without symbols well. So fake up a local symbol in
2706 the absolute section. */
2707 fixP->fx_addsy = abs_section_sym;
2711 if (!fixP->fx_done)
2712 md_apply_fix3 (fixP, &add_number, this_segment);
2714 if (!fixP->fx_done)
2716 ++seg_reloc_count;
2717 if (fixP->fx_addsy == NULL)
2718 fixP->fx_addsy = abs_section_sym;
2719 symbol_mark_used_in_reloc (fixP->fx_addsy);
2720 if (fixP->fx_subsy != NULL)
2721 symbol_mark_used_in_reloc (fixP->fx_subsy);
2724 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2726 if (fixP->fx_size < sizeof (valueT))
2728 valueT mask;
2730 mask = 0;
2731 mask--; /* Set all bits to one. */
2732 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2733 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2735 char buf[50], buf2[50];
2736 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2737 if (add_number > 1000)
2738 sprint_value (buf2, add_number);
2739 else
2740 sprintf (buf2, "%ld", (long) add_number);
2741 as_bad_where (fixP->fx_file, fixP->fx_line,
2742 _("value of %s too large for field of %d bytes at %s"),
2743 buf2, fixP->fx_size, buf);
2744 } /* Generic error checking. */
2746 #ifdef WARN_SIGNED_OVERFLOW_WORD
2747 /* Warn if a .word value is too large when treated as a signed
2748 number. We already know it is not too negative. This is to
2749 catch over-large switches generated by gcc on the 68k. */
2750 if (!flag_signed_overflow_ok
2751 && fixP->fx_size == 2
2752 && add_number > 0x7fff)
2753 as_bad_where (fixP->fx_file, fixP->fx_line,
2754 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2755 (long) add_number,
2756 (long) (fragP->fr_address + fixP->fx_where));
2757 #endif
2758 } /* Not a bit fix. */
2760 #ifdef TC_VALIDATE_FIX
2761 skip: ATTRIBUTE_UNUSED_LABEL
2763 #endif
2764 #ifdef DEBUG5
2765 fprintf (stderr, "result:\n");
2766 print_fixup (fixP);
2767 #endif
2768 } /* For each fixS in this segment. */
2770 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2771 return seg_reloc_count;
2774 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2776 void
2777 number_to_chars_bigendian (buf, val, n)
2778 char *buf;
2779 valueT val;
2780 int n;
2782 if (n <= 0)
2783 abort ();
2784 while (n--)
2786 buf[n] = val & 0xff;
2787 val >>= 8;
2791 void
2792 number_to_chars_littleendian (buf, val, n)
2793 char *buf;
2794 valueT val;
2795 int n;
2797 if (n <= 0)
2798 abort ();
2799 while (n--)
2801 *buf++ = val & 0xff;
2802 val >>= 8;
2806 void
2807 write_print_statistics (file)
2808 FILE *file;
2810 fprintf (file, "fixups: %d\n", n_fixups);
2813 /* For debugging. */
2814 extern int indent_level;
2816 void
2817 print_fixup (fixp)
2818 fixS *fixp;
2820 indent_level = 1;
2821 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2822 if (fixp->fx_pcrel)
2823 fprintf (stderr, " pcrel");
2824 if (fixp->fx_pcrel_adjust)
2825 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2826 if (fixp->fx_im_disp)
2828 #ifdef TC_NS32K
2829 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2830 #else
2831 fprintf (stderr, " im_disp");
2832 #endif
2834 if (fixp->fx_tcbit)
2835 fprintf (stderr, " tcbit");
2836 if (fixp->fx_done)
2837 fprintf (stderr, " done");
2838 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2839 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2840 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2841 #ifdef BFD_ASSEMBLER
2842 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2843 fixp->fx_r_type);
2844 #else
2845 #ifdef NEED_FX_R_TYPE
2846 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2847 #endif
2848 #endif
2849 if (fixp->fx_addsy)
2851 fprintf (stderr, "\n +<");
2852 print_symbol_value_1 (stderr, fixp->fx_addsy);
2853 fprintf (stderr, ">");
2855 if (fixp->fx_subsy)
2857 fprintf (stderr, "\n -<");
2858 print_symbol_value_1 (stderr, fixp->fx_subsy);
2859 fprintf (stderr, ">");
2861 fprintf (stderr, "\n");
2862 #ifdef TC_FIX_DATA_PRINT
2863 TC_FIX_DATA_PRINT (stderr, fixp);
2864 #endif