* bfd/archures.c (bfd_mach_mcf5200, bfd_mach_mcf5206e,
[binutils.git] / gas / write.c
blob5c336d8bb97b59b44bc474bf984d71d55430b161
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 #define TC_VALIDATE_FIX_SUB(FIX) \
75 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
76 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
77 #endif
78 #endif
80 #ifndef TC_LINKRELAX_FIXUP
81 #define TC_LINKRELAX_FIXUP(SEG) 1
82 #endif
84 #ifndef MD_APPLY_SYM_VALUE
85 #define MD_APPLY_SYM_VALUE(FIX) 1
86 #endif
88 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
89 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
90 #endif
92 #ifndef MD_PCREL_FROM_SECTION
93 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
94 #endif
96 #ifndef TC_FAKE_LABEL
97 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
98 #endif
100 /* Used to control final evaluation of expressions. */
101 int finalize_syms = 0;
103 int symbol_table_frozen;
105 symbolS *abs_section_sym;
107 /* Remember the value of dot when parsing expressions. */
108 addressT dot_value;
110 void print_fixup (fixS *);
112 static void renumber_sections (bfd *, asection *, PTR);
114 /* We generally attach relocs to frag chains. However, after we have
115 chained these all together into a segment, any relocs we add after
116 that must be attached to a segment. This will include relocs added
117 in md_estimate_size_for_relax, for example. */
118 static int frags_chained = 0;
120 static int n_fixups;
122 #define RELOC_ENUM enum bfd_reloc_code_real
124 static fixS *fix_new_internal (fragS *, int where, int size,
125 symbolS *add, symbolS *sub,
126 offsetT offset, int pcrel,
127 RELOC_ENUM r_type);
128 static long fixup_segment (fixS *, segT);
129 static relax_addressT relax_align (relax_addressT addr, int align);
130 static fragS *chain_frchains_together_1 (segT, struct frchain *);
131 static void chain_frchains_together (bfd *, segT, PTR);
132 static void cvt_frag_to_fill (segT, fragS *);
133 static void adjust_reloc_syms (bfd *, asection *, PTR);
134 static void fix_segment (bfd *, asection *, PTR);
135 static void write_relocs (bfd *, asection *, PTR);
136 static void write_contents (bfd *, asection *, PTR);
137 static void set_symtab (void);
138 static void merge_data_into_text (void);
140 /* Create a fixS in obstack 'notes'. */
142 static fixS *
143 fix_new_internal (fragS *frag, /* Which frag? */
144 int where, /* Where in that frag? */
145 int size, /* 1, 2, or 4 usually. */
146 symbolS *add_symbol, /* X_add_symbol. */
147 symbolS *sub_symbol, /* X_op_symbol. */
148 offsetT offset, /* X_add_number. */
149 int pcrel, /* TRUE if PC-relative relocation. */
150 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
152 fixS *fixP;
154 n_fixups++;
156 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
158 fixP->fx_frag = frag;
159 fixP->fx_where = where;
160 fixP->fx_size = size;
161 /* We've made fx_size a narrow field; check that it's wide enough. */
162 if (fixP->fx_size != size)
164 as_bad (_("field fx_size too small to hold %d"), size);
165 abort ();
167 fixP->fx_addsy = add_symbol;
168 fixP->fx_subsy = sub_symbol;
169 fixP->fx_offset = offset;
170 fixP->fx_dot_value = dot_value;
171 fixP->fx_pcrel = pcrel;
172 fixP->fx_plt = 0;
173 fixP->fx_r_type = r_type;
174 fixP->fx_im_disp = 0;
175 fixP->fx_pcrel_adjust = 0;
176 fixP->fx_bit_fixP = 0;
177 fixP->fx_addnumber = 0;
178 fixP->fx_tcbit = 0;
179 fixP->fx_done = 0;
180 fixP->fx_no_overflow = 0;
181 fixP->fx_signed = 0;
183 #ifdef USING_CGEN
184 fixP->fx_cgen.insn = NULL;
185 fixP->fx_cgen.opinfo = 0;
186 #endif
188 #ifdef TC_FIX_TYPE
189 TC_INIT_FIX_DATA (fixP);
190 #endif
192 as_where (&fixP->fx_file, &fixP->fx_line);
194 /* Usually, we want relocs sorted numerically, but while
195 comparing to older versions of gas that have relocs
196 reverse sorted, it is convenient to have this compile
197 time option. xoxorich. */
200 fixS **seg_fix_rootP = (frags_chained
201 ? &seg_info (now_seg)->fix_root
202 : &frchain_now->fix_root);
203 fixS **seg_fix_tailP = (frags_chained
204 ? &seg_info (now_seg)->fix_tail
205 : &frchain_now->fix_tail);
207 #ifdef REVERSE_SORT_RELOCS
209 fixP->fx_next = *seg_fix_rootP;
210 *seg_fix_rootP = fixP;
212 #else /* REVERSE_SORT_RELOCS */
214 fixP->fx_next = NULL;
216 if (*seg_fix_tailP)
217 (*seg_fix_tailP)->fx_next = fixP;
218 else
219 *seg_fix_rootP = fixP;
220 *seg_fix_tailP = fixP;
222 #endif /* REVERSE_SORT_RELOCS */
225 return fixP;
228 /* Create a fixup relative to a symbol (plus a constant). */
230 fixS *
231 fix_new (fragS *frag, /* Which frag? */
232 int where, /* Where in that frag? */
233 int size, /* 1, 2, or 4 usually. */
234 symbolS *add_symbol, /* X_add_symbol. */
235 offsetT offset, /* X_add_number. */
236 int pcrel, /* TRUE if PC-relative relocation. */
237 RELOC_ENUM r_type /* Relocation type. */)
239 return fix_new_internal (frag, where, size, add_symbol,
240 (symbolS *) NULL, offset, pcrel, r_type);
243 /* Create a fixup for an expression. Currently we only support fixups
244 for difference expressions. That is itself more than most object
245 file formats support anyhow. */
247 fixS *
248 fix_new_exp (fragS *frag, /* Which frag? */
249 int where, /* Where in that frag? */
250 int size, /* 1, 2, or 4 usually. */
251 expressionS *exp, /* Expression. */
252 int pcrel, /* TRUE if PC-relative relocation. */
253 RELOC_ENUM r_type /* Relocation type. */)
255 symbolS *add = NULL;
256 symbolS *sub = NULL;
257 offsetT off = 0;
259 switch (exp->X_op)
261 case O_absent:
262 break;
264 case O_register:
265 as_bad (_("register value used as expression"));
266 break;
268 case O_add:
269 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
270 the difference expression cannot immediately be reduced. */
272 symbolS *stmp = make_expr_symbol (exp);
274 exp->X_op = O_symbol;
275 exp->X_op_symbol = 0;
276 exp->X_add_symbol = stmp;
277 exp->X_add_number = 0;
279 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
282 case O_symbol_rva:
283 add = exp->X_add_symbol;
284 off = exp->X_add_number;
285 r_type = BFD_RELOC_RVA;
286 break;
288 case O_uminus:
289 sub = exp->X_add_symbol;
290 off = exp->X_add_number;
291 break;
293 case O_subtract:
294 sub = exp->X_op_symbol;
295 /* Fall through. */
296 case O_symbol:
297 add = exp->X_add_symbol;
298 /* Fall through. */
299 case O_constant:
300 off = exp->X_add_number;
301 break;
303 default:
304 add = make_expr_symbol (exp);
305 break;
308 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
311 /* Generic function to determine whether a fixup requires a relocation. */
313 generic_force_reloc (fixS *fix)
315 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
316 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
317 return 1;
319 if (fix->fx_addsy == NULL)
320 return 0;
322 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
325 /* Append a string onto another string, bumping the pointer along. */
326 void
327 append (char **charPP, char *fromP, unsigned long length)
329 /* Don't trust memcpy() of 0 chars. */
330 if (length == 0)
331 return;
333 memcpy (*charPP, fromP, length);
334 *charPP += length;
337 /* This routine records the largest alignment seen for each segment.
338 If the beginning of the segment is aligned on the worst-case
339 boundary, all of the other alignments within it will work. At
340 least one object format really uses this info. */
342 void
343 record_alignment (/* Segment to which alignment pertains. */
344 segT seg,
345 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
346 boundary, 2 => 4-byte boundary, etc.) */
347 int align)
349 if (seg == absolute_section)
350 return;
352 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
353 bfd_set_section_alignment (stdoutput, seg, align);
357 get_recorded_alignment (segT seg)
359 if (seg == absolute_section)
360 return 0;
362 return bfd_get_section_alignment (stdoutput, seg);
365 /* Reset the section indices after removing the gas created sections. */
367 static void
368 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR countparg)
370 int *countp = (int *) countparg;
372 sec->index = *countp;
373 ++*countp;
376 static fragS *
377 chain_frchains_together_1 (segT section, struct frchain *frchp)
379 fragS dummy, *prev_frag = &dummy;
380 fixS fix_dummy, *prev_fix = &fix_dummy;
382 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
384 prev_frag->fr_next = frchp->frch_root;
385 prev_frag = frchp->frch_last;
386 assert (prev_frag->fr_type != 0);
387 if (frchp->fix_root != (fixS *) NULL)
389 if (seg_info (section)->fix_root == (fixS *) NULL)
390 seg_info (section)->fix_root = frchp->fix_root;
391 prev_fix->fx_next = frchp->fix_root;
392 seg_info (section)->fix_tail = frchp->fix_tail;
393 prev_fix = frchp->fix_tail;
396 assert (prev_frag->fr_type != 0);
397 prev_frag->fr_next = 0;
398 return prev_frag;
401 static void
402 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
403 segT section,
404 PTR xxx ATTRIBUTE_UNUSED)
406 segment_info_type *info;
408 /* BFD may have introduced its own sections without using
409 subseg_new, so it is possible that seg_info is NULL. */
410 info = seg_info (section);
411 if (info != (segment_info_type *) NULL)
412 info->frchainP->frch_last
413 = chain_frchains_together_1 (section, info->frchainP);
415 /* Now that we've chained the frags together, we must add new fixups
416 to the segment, not to the frag chain. */
417 frags_chained = 1;
420 static void
421 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
423 switch (fragP->fr_type)
425 case rs_align:
426 case rs_align_code:
427 case rs_align_test:
428 case rs_org:
429 case rs_space:
430 #ifdef HANDLE_ALIGN
431 HANDLE_ALIGN (fragP);
432 #endif
433 know (fragP->fr_next != NULL);
434 fragP->fr_offset = (fragP->fr_next->fr_address
435 - fragP->fr_address
436 - fragP->fr_fix) / fragP->fr_var;
437 if (fragP->fr_offset < 0)
439 as_bad_where (fragP->fr_file, fragP->fr_line,
440 _("attempt to .org/.space backwards? (%ld)"),
441 (long) fragP->fr_offset);
442 fragP->fr_offset = 0;
444 fragP->fr_type = rs_fill;
445 break;
447 case rs_fill:
448 break;
450 case rs_leb128:
452 valueT value = S_GET_VALUE (fragP->fr_symbol);
453 int size;
455 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
456 fragP->fr_subtype);
458 fragP->fr_fix += size;
459 fragP->fr_type = rs_fill;
460 fragP->fr_var = 0;
461 fragP->fr_offset = 0;
462 fragP->fr_symbol = NULL;
464 break;
466 case rs_cfa:
467 eh_frame_convert_frag (fragP);
468 break;
470 case rs_dwarf2dbg:
471 dwarf2dbg_convert_frag (fragP);
472 break;
474 case rs_machine_dependent:
475 md_convert_frag (stdoutput, sec, fragP);
477 assert (fragP->fr_next == NULL
478 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
479 == fragP->fr_fix));
481 /* After md_convert_frag, we make the frag into a ".space 0".
482 md_convert_frag() should set up any fixSs and constants
483 required. */
484 frag_wane (fragP);
485 break;
487 #ifndef WORKING_DOT_WORD
488 case rs_broken_word:
490 struct broken_word *lie;
492 if (fragP->fr_subtype)
494 fragP->fr_fix += md_short_jump_size;
495 for (lie = (struct broken_word *) (fragP->fr_symbol);
496 lie && lie->dispfrag == fragP;
497 lie = lie->next_broken_word)
498 if (lie->added == 1)
499 fragP->fr_fix += md_long_jump_size;
501 frag_wane (fragP);
503 break;
504 #endif
506 default:
507 BAD_CASE (fragP->fr_type);
508 break;
510 #ifdef md_frag_check
511 md_frag_check (fragP);
512 #endif
515 static void relax_seg (bfd *, asection *, PTR);
517 static void
518 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR xxx)
520 segment_info_type *seginfo = seg_info (sec);
522 if (seginfo && seginfo->frchainP
523 && relax_segment (seginfo->frchainP->frch_root, sec))
525 int *result = (int *) xxx;
526 *result = 1;
530 static void size_seg (bfd *, asection *, PTR);
532 static void
533 size_seg (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
535 flagword flags;
536 fragS *fragp;
537 segment_info_type *seginfo;
538 int x;
539 valueT size, newsize;
541 subseg_change (sec, 0);
543 seginfo = seg_info (sec);
544 if (seginfo && seginfo->frchainP)
546 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
547 cvt_frag_to_fill (sec, fragp);
548 for (fragp = seginfo->frchainP->frch_root;
549 fragp->fr_next;
550 fragp = fragp->fr_next)
551 /* Walk to last elt. */
553 size = fragp->fr_address + fragp->fr_fix;
555 else
556 size = 0;
558 flags = bfd_get_section_flags (abfd, sec);
560 if (size > 0 && ! seginfo->bss)
561 flags |= SEC_HAS_CONTENTS;
563 /* @@ This is just an approximation. */
564 if (seginfo && seginfo->fix_root)
565 flags |= SEC_RELOC;
566 else
567 flags &= ~SEC_RELOC;
568 x = bfd_set_section_flags (abfd, sec, flags);
569 assert (x);
571 newsize = md_section_align (sec, size);
572 x = bfd_set_section_size (abfd, sec, newsize);
573 assert (x);
575 /* If the size had to be rounded up, add some padding in the last
576 non-empty frag. */
577 assert (newsize >= size);
578 if (size != newsize)
580 fragS *last = seginfo->frchainP->frch_last;
581 fragp = seginfo->frchainP->frch_root;
582 while (fragp->fr_next != last)
583 fragp = fragp->fr_next;
584 last->fr_address = size;
585 if ((newsize - size) % fragp->fr_var == 0)
586 fragp->fr_offset += (newsize - size) / fragp->fr_var;
587 else
588 /* If we hit this abort, it's likely due to subsegs_finish not
589 providing sufficient alignment on the last frag, and the
590 machine dependent code using alignment frags with fr_var
591 greater than 1. */
592 abort ();
595 #ifdef tc_frob_section
596 tc_frob_section (sec);
597 #endif
598 #ifdef obj_frob_section
599 obj_frob_section (sec);
600 #endif
603 #ifdef DEBUG2
604 static void
605 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
607 segment_info_type *seginfo = seg_info (sec);
608 fixS *fixp = seginfo->fix_root;
610 if (!fixp)
611 return;
613 fprintf (stream, "sec %s relocs:\n", sec->name);
614 while (fixp)
616 symbolS *s = fixp->fx_addsy;
618 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
619 (int) fixp->fx_r_type);
620 if (s == NULL)
621 fprintf (stream, "no sym\n");
622 else
624 print_symbol_value_1 (stream, s);
625 fprintf (stream, "\n");
627 fixp = fixp->fx_next;
630 #else
631 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
632 #endif
634 #ifndef EMIT_SECTION_SYMBOLS
635 #define EMIT_SECTION_SYMBOLS 1
636 #endif
638 /* This pass over fixups decides whether symbols can be replaced with
639 section symbols. */
641 static void
642 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
643 asection *sec,
644 PTR xxx ATTRIBUTE_UNUSED)
646 segment_info_type *seginfo = seg_info (sec);
647 fixS *fixp;
649 if (seginfo == NULL)
650 return;
652 dump_section_relocs (abfd, sec, stderr);
654 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
655 if (fixp->fx_done)
656 /* Ignore it. */
658 else if (fixp->fx_addsy)
660 symbolS *sym;
661 asection *symsec;
663 #ifdef DEBUG5
664 fprintf (stderr, "\n\nadjusting fixup:\n");
665 print_fixup (fixp);
666 #endif
668 sym = fixp->fx_addsy;
670 /* All symbols should have already been resolved at this
671 point. It is possible to see unresolved expression
672 symbols, though, since they are not in the regular symbol
673 table. */
674 resolve_symbol_value (sym);
676 if (fixp->fx_subsy != NULL)
677 resolve_symbol_value (fixp->fx_subsy);
679 /* If this symbol is equated to an undefined or common symbol,
680 convert the fixup to being against that symbol. */
681 if (symbol_equated_reloc_p (sym)
682 || S_IS_WEAKREFR (sym))
684 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
685 sym = symbol_get_value_expression (sym)->X_add_symbol;
686 fixp->fx_addsy = sym;
689 if (symbol_mri_common_p (sym))
691 /* These symbols are handled specially in fixup_segment. */
692 continue;
695 /* If the symbol is undefined, common, weak, or global (ELF
696 shared libs), we can't replace it with the section symbol. */
697 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
698 continue;
700 /* Is there some other (target cpu dependent) reason we can't adjust
701 this one? (E.g. relocations involving function addresses on
702 the PA. */
703 #ifdef tc_fix_adjustable
704 if (! tc_fix_adjustable (fixp))
705 continue;
706 #endif
708 /* Since we're reducing to section symbols, don't attempt to reduce
709 anything that's already using one. */
710 if (symbol_section_p (sym))
711 continue;
713 symsec = S_GET_SEGMENT (sym);
714 if (symsec == NULL)
715 abort ();
717 if (bfd_is_abs_section (symsec))
719 /* The fixup_segment routine normally will not use this
720 symbol in a relocation. */
721 continue;
724 /* Don't try to reduce relocs which refer to non-local symbols
725 in .linkonce sections. It can lead to confusion when a
726 debugging section refers to a .linkonce section. I hope
727 this will always be correct. */
728 if (symsec != sec && ! S_IS_LOCAL (sym))
730 if ((symsec->flags & SEC_LINK_ONCE) != 0
731 || (IS_ELF
732 /* The GNU toolchain uses an extension for ELF: a
733 section beginning with the magic string
734 .gnu.linkonce is a linkonce section. */
735 && strncmp (segment_name (symsec), ".gnu.linkonce",
736 sizeof ".gnu.linkonce" - 1) == 0))
737 continue;
740 /* Never adjust a reloc against local symbol in a merge section
741 with non-zero addend. */
742 if ((symsec->flags & SEC_MERGE) != 0
743 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
744 continue;
746 /* Never adjust a reloc against TLS local symbol. */
747 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
748 continue;
750 /* We refetch the segment when calling section_symbol, rather
751 than using symsec, because S_GET_VALUE may wind up changing
752 the section when it calls resolve_symbol_value. */
753 fixp->fx_offset += S_GET_VALUE (sym);
754 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
755 #ifdef DEBUG5
756 fprintf (stderr, "\nadjusted fixup:\n");
757 print_fixup (fixp);
758 #endif
761 dump_section_relocs (abfd, sec, stderr);
764 static void
765 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
766 asection *sec,
767 PTR xxx ATTRIBUTE_UNUSED)
769 segment_info_type *seginfo = seg_info (sec);
771 fixup_segment (seginfo->fix_root, sec);
774 static void
775 write_relocs (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
777 segment_info_type *seginfo = seg_info (sec);
778 unsigned int i;
779 unsigned int n;
780 arelent **relocs;
781 fixS *fixp;
782 char *err;
784 /* If seginfo is NULL, we did not create this section; don't do
785 anything with it. */
786 if (seginfo == NULL)
787 return;
789 n = 0;
790 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
791 n++;
793 #ifndef RELOC_EXPANSION_POSSIBLE
794 /* Set up reloc information as well. */
795 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
797 i = 0;
798 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
800 arelent *reloc;
801 bfd_reloc_status_type s;
802 symbolS *sym;
804 if (fixp->fx_done)
806 n--;
807 continue;
810 /* If this is an undefined symbol which was equated to another
811 symbol, then generate the reloc against the latter symbol
812 rather than the former. */
813 sym = fixp->fx_addsy;
814 while (symbol_equated_reloc_p (sym))
816 symbolS *n;
818 /* We must avoid looping, as that can occur with a badly
819 written program. */
820 n = symbol_get_value_expression (sym)->X_add_symbol;
821 if (n == sym)
822 break;
823 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
824 sym = n;
826 fixp->fx_addsy = sym;
828 reloc = tc_gen_reloc (sec, fixp);
829 if (!reloc)
831 n--;
832 continue;
836 This test is triggered inappropriately for the SH:
837 if (fixp->fx_where + fixp->fx_size
838 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
839 abort ();
842 s = bfd_install_relocation (stdoutput, reloc,
843 fixp->fx_frag->fr_literal,
844 fixp->fx_frag->fr_address,
845 sec, &err);
846 switch (s)
848 case bfd_reloc_ok:
849 break;
850 case bfd_reloc_overflow:
851 as_bad_where (fixp->fx_file, fixp->fx_line,
852 _("relocation overflow"));
853 break;
854 case bfd_reloc_outofrange:
855 as_bad_where (fixp->fx_file, fixp->fx_line,
856 _("relocation out of range"));
857 break;
858 default:
859 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
860 fixp->fx_file, fixp->fx_line, s);
862 relocs[i++] = reloc;
864 #else
865 n = n * MAX_RELOC_EXPANSION;
866 /* Set up reloc information as well. */
867 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
869 i = 0;
870 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
872 arelent **reloc;
873 bfd_reloc_status_type s;
874 symbolS *sym;
875 int j;
877 if (fixp->fx_done)
879 n--;
880 continue;
883 /* If this is an undefined symbol which was equated to another
884 symbol, then generate the reloc against the latter symbol
885 rather than the former. */
886 sym = fixp->fx_addsy;
887 while (symbol_equated_reloc_p (sym))
889 symbolS *n;
891 /* We must avoid looping, as that can occur with a badly
892 written program. */
893 n = symbol_get_value_expression (sym)->X_add_symbol;
894 if (n == sym)
895 break;
896 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
897 sym = n;
899 fixp->fx_addsy = sym;
901 reloc = tc_gen_reloc (sec, fixp);
903 for (j = 0; reloc[j]; j++)
905 relocs[i++] = reloc[j];
906 assert (i <= n);
908 if (fixp->fx_where + fixp->fx_size
909 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
910 as_bad_where (fixp->fx_file, fixp->fx_line,
911 _("internal error: fixup not contained within frag"));
912 for (j = 0; reloc[j]; j++)
914 s = bfd_install_relocation (stdoutput, reloc[j],
915 fixp->fx_frag->fr_literal,
916 fixp->fx_frag->fr_address,
917 sec, &err);
918 switch (s)
920 case bfd_reloc_ok:
921 break;
922 case bfd_reloc_overflow:
923 as_bad_where (fixp->fx_file, fixp->fx_line,
924 _("relocation overflow"));
925 break;
926 case bfd_reloc_outofrange:
927 as_bad_where (fixp->fx_file, fixp->fx_line,
928 _("relocation out of range"));
929 break;
930 default:
931 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
932 fixp->fx_file, fixp->fx_line, s);
936 n = i;
937 #endif
939 #ifdef DEBUG4
941 unsigned int i, j, nsyms;
942 asymbol **sympp;
943 sympp = bfd_get_outsymbols (stdoutput);
944 nsyms = bfd_get_symcount (stdoutput);
945 for (i = 0; i < n; i++)
946 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
948 for (j = 0; j < nsyms; j++)
949 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
950 break;
951 if (j == nsyms)
952 abort ();
955 #endif
957 if (n)
958 bfd_set_reloc (stdoutput, sec, relocs, n);
959 else
960 bfd_set_section_flags (abfd, sec,
961 (bfd_get_section_flags (abfd, sec)
962 & (flagword) ~SEC_RELOC));
964 #ifdef SET_SECTION_RELOCS
965 SET_SECTION_RELOCS (sec, relocs, n);
966 #endif
968 #ifdef DEBUG3
970 unsigned int i;
971 arelent *r;
972 asymbol *s;
973 fprintf (stderr, "relocs for sec %s\n", sec->name);
974 for (i = 0; i < n; i++)
976 r = relocs[i];
977 s = *r->sym_ptr_ptr;
978 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
979 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
982 #endif
985 static void
986 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
987 asection *sec,
988 PTR xxx ATTRIBUTE_UNUSED)
990 segment_info_type *seginfo = seg_info (sec);
991 addressT offset = 0;
992 fragS *f;
994 /* Write out the frags. */
995 if (seginfo == NULL
996 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
997 return;
999 for (f = seginfo->frchainP->frch_root;
1001 f = f->fr_next)
1003 int x;
1004 addressT fill_size;
1005 char *fill_literal;
1006 offsetT count;
1008 assert (f->fr_type == rs_fill);
1009 if (f->fr_fix)
1011 x = bfd_set_section_contents (stdoutput, sec,
1012 f->fr_literal, (file_ptr) offset,
1013 (bfd_size_type) f->fr_fix);
1014 if (!x)
1016 bfd_perror (stdoutput->filename);
1017 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1018 exit (EXIT_FAILURE);
1020 offset += f->fr_fix;
1022 fill_literal = f->fr_literal + f->fr_fix;
1023 fill_size = f->fr_var;
1024 count = f->fr_offset;
1025 assert (count >= 0);
1026 if (fill_size && count)
1028 char buf[256];
1029 if (fill_size > sizeof (buf))
1031 /* Do it the old way. Can this ever happen? */
1032 while (count--)
1034 x = bfd_set_section_contents (stdoutput, sec,
1035 fill_literal,
1036 (file_ptr) offset,
1037 (bfd_size_type) fill_size);
1038 if (!x)
1040 bfd_perror (stdoutput->filename);
1041 as_perror (_("FATAL: Can't write %s"),
1042 stdoutput->filename);
1043 exit (EXIT_FAILURE);
1045 offset += fill_size;
1048 else
1050 /* Build a buffer full of fill objects and output it as
1051 often as necessary. This saves on the overhead of
1052 potentially lots of bfd_set_section_contents calls. */
1053 int n_per_buf, i;
1054 if (fill_size == 1)
1056 n_per_buf = sizeof (buf);
1057 memset (buf, *fill_literal, n_per_buf);
1059 else
1061 char *bufp;
1062 n_per_buf = sizeof (buf) / fill_size;
1063 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1064 memcpy (bufp, fill_literal, fill_size);
1066 for (; count > 0; count -= n_per_buf)
1068 n_per_buf = n_per_buf > count ? count : n_per_buf;
1069 x = bfd_set_section_contents
1070 (stdoutput, sec, buf, (file_ptr) offset,
1071 (bfd_size_type) n_per_buf * fill_size);
1072 if (!x)
1073 as_fatal (_("cannot write to output file"));
1074 offset += n_per_buf * fill_size;
1081 static void
1082 merge_data_into_text (void)
1084 seg_info (text_section)->frchainP->frch_last->fr_next =
1085 seg_info (data_section)->frchainP->frch_root;
1086 seg_info (text_section)->frchainP->frch_last =
1087 seg_info (data_section)->frchainP->frch_last;
1088 seg_info (data_section)->frchainP = 0;
1091 static void
1092 set_symtab (void)
1094 int nsyms;
1095 asymbol **asympp;
1096 symbolS *symp;
1097 bfd_boolean result;
1098 extern PTR bfd_alloc (bfd *, bfd_size_type);
1100 /* Count symbols. We can't rely on a count made by the loop in
1101 write_object_file, because *_frob_file may add a new symbol or
1102 two. */
1103 nsyms = 0;
1104 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1105 nsyms++;
1107 if (nsyms)
1109 int i;
1110 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1112 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1113 symp = symbol_rootP;
1114 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1116 asympp[i] = symbol_get_bfdsym (symp);
1117 symbol_mark_written (symp);
1120 else
1121 asympp = 0;
1122 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1123 assert (result);
1124 symbol_table_frozen = 1;
1127 /* Finish the subsegments. After every sub-segment, we fake an
1128 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1129 ".fill 0" because that is the kind of frag that requires least
1130 thought. ".align" frags like to have a following frag since that
1131 makes calculating their intended length trivial. */
1133 #ifndef SUB_SEGMENT_ALIGN
1134 #ifdef HANDLE_ALIGN
1135 /* The last subsegment gets an alignment corresponding to the alignment
1136 of the section. This allows proper nop-filling at the end of
1137 code-bearing sections. */
1138 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1139 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1140 ? get_recorded_alignment (SEG) : 0)
1141 #else
1142 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1143 #endif
1144 #endif
1146 void
1147 subsegs_finish (void)
1149 struct frchain *frchainP;
1151 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1153 int alignment = 0;
1155 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1157 /* This now gets called even if we had errors. In that case,
1158 any alignment is meaningless, and, moreover, will look weird
1159 if we are generating a listing. */
1160 if (!had_errors ())
1162 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1163 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1164 && now_seg->entsize)
1166 unsigned int entsize = now_seg->entsize;
1167 int entalign = 0;
1169 while ((entsize & 1) == 0)
1171 ++entalign;
1172 entsize >>= 1;
1174 if (entalign > alignment)
1175 alignment = entalign;
1179 if (subseg_text_p (now_seg))
1180 frag_align_code (alignment, 0);
1181 else
1182 frag_align (alignment, 0, 0);
1184 /* frag_align will have left a new frag.
1185 Use this last frag for an empty ".fill".
1187 For this segment ...
1188 Create a last frag. Do not leave a "being filled in frag". */
1189 frag_wane (frag_now);
1190 frag_now->fr_fix = 0;
1191 know (frag_now->fr_next == NULL);
1195 /* Write the object file. */
1197 void
1198 write_object_file (void)
1200 #ifndef WORKING_DOT_WORD
1201 fragS *fragP; /* Track along all frags. */
1202 #endif
1204 /* Do we really want to write it? */
1206 int n_warns, n_errs;
1207 n_warns = had_warnings ();
1208 n_errs = had_errors ();
1209 /* The -Z flag indicates that an object file should be generated,
1210 regardless of warnings and errors. */
1211 if (flag_always_generate_output)
1213 if (n_warns || n_errs)
1214 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1215 n_errs, n_errs == 1 ? "" : "s",
1216 n_warns, n_warns == 1 ? "" : "s");
1218 else
1220 if (n_errs)
1221 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1222 n_errs, n_errs == 1 ? "" : "s",
1223 n_warns, n_warns == 1 ? "" : "s");
1227 #ifdef OBJ_VMS
1228 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1229 a routine to check for the definition of the procedure "_main",
1230 and if so -- fix it up so that it can be program entry point. */
1231 vms_check_for_main ();
1232 #endif /* OBJ_VMS */
1234 /* From now on, we don't care about sub-segments. Build one frag chain
1235 for each segment. Linked thru fr_next. */
1237 /* Remove the sections created by gas for its own purposes. */
1239 int i;
1241 bfd_section_list_remove (stdoutput, reg_section);
1242 bfd_section_list_remove (stdoutput, expr_section);
1243 stdoutput->section_count -= 2;
1244 i = 0;
1245 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1248 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1250 /* We have two segments. If user gave -R flag, then we must put the
1251 data frags into the text segment. Do this before relaxing so
1252 we know to take advantage of -R and make shorter addresses. */
1253 if (flag_readonly_data_in_text)
1255 merge_data_into_text ();
1258 while (1)
1260 int changed;
1262 #ifndef WORKING_DOT_WORD
1263 /* We need to reset the markers in the broken word list and
1264 associated frags between calls to relax_segment (via
1265 relax_seg). Since the broken word list is global, we do it
1266 once per round, rather than locally in relax_segment for each
1267 segment. */
1268 struct broken_word *brokp;
1270 for (brokp = broken_words;
1271 brokp != (struct broken_word *) NULL;
1272 brokp = brokp->next_broken_word)
1274 brokp->added = 0;
1276 if (brokp->dispfrag != (fragS *) NULL
1277 && brokp->dispfrag->fr_type == rs_broken_word)
1278 brokp->dispfrag->fr_subtype = 0;
1280 #endif
1282 changed = 0;
1283 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1284 if (!changed)
1285 break;
1288 /* Note - Most ports will use the default value of
1289 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1290 local symbols to be resolved, removing their frag information.
1291 Some ports however, will not have finished relaxing all of
1292 their frags and will still need the local symbol frag
1293 information. These ports can set
1294 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1295 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1297 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1299 /* Relaxation has completed. Freeze all syms. */
1300 finalize_syms = 1;
1302 #ifdef md_post_relax_hook
1303 md_post_relax_hook;
1304 #endif
1306 #ifndef WORKING_DOT_WORD
1308 struct broken_word *lie;
1309 struct broken_word **prevP;
1311 prevP = &broken_words;
1312 for (lie = broken_words; lie; lie = lie->next_broken_word)
1313 if (!lie->added)
1315 expressionS exp;
1317 subseg_change (lie->seg, lie->subseg);
1318 exp.X_op = O_subtract;
1319 exp.X_add_symbol = lie->add;
1320 exp.X_op_symbol = lie->sub;
1321 exp.X_add_number = lie->addnum;
1322 #ifdef TC_CONS_FIX_NEW
1323 TC_CONS_FIX_NEW (lie->frag,
1324 lie->word_goes_here - lie->frag->fr_literal,
1325 2, &exp);
1326 #else
1327 fix_new_exp (lie->frag,
1328 lie->word_goes_here - lie->frag->fr_literal,
1329 2, &exp, 0, BFD_RELOC_16);
1330 #endif
1331 *prevP = lie->next_broken_word;
1333 else
1334 prevP = &(lie->next_broken_word);
1336 for (lie = broken_words; lie;)
1338 struct broken_word *untruth;
1339 char *table_ptr;
1340 addressT table_addr;
1341 addressT from_addr, to_addr;
1342 int n, m;
1344 subseg_change (lie->seg, lie->subseg);
1345 fragP = lie->dispfrag;
1347 /* Find out how many broken_words go here. */
1348 n = 0;
1349 for (untruth = lie;
1350 untruth && untruth->dispfrag == fragP;
1351 untruth = untruth->next_broken_word)
1352 if (untruth->added == 1)
1353 n++;
1355 table_ptr = lie->dispfrag->fr_opcode;
1356 table_addr = (lie->dispfrag->fr_address
1357 + (table_ptr - lie->dispfrag->fr_literal));
1358 /* Create the jump around the long jumps. This is a short
1359 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1360 from_addr = table_addr;
1361 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1362 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1363 lie->add);
1364 table_ptr += md_short_jump_size;
1365 table_addr += md_short_jump_size;
1367 for (m = 0;
1368 lie && lie->dispfrag == fragP;
1369 m++, lie = lie->next_broken_word)
1371 if (lie->added == 2)
1372 continue;
1373 /* Patch the jump table. */
1374 /* This is the offset from ??? to table_ptr+0. */
1375 to_addr = table_addr - S_GET_VALUE (lie->sub);
1376 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1377 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1378 #endif
1379 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1380 for (untruth = lie->next_broken_word;
1381 untruth && untruth->dispfrag == fragP;
1382 untruth = untruth->next_broken_word)
1384 if (untruth->use_jump == lie)
1385 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1388 /* Install the long jump. */
1389 /* This is a long jump from table_ptr+0 to the final target. */
1390 from_addr = table_addr;
1391 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1392 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1393 lie->add);
1394 table_ptr += md_long_jump_size;
1395 table_addr += md_long_jump_size;
1399 #endif /* not WORKING_DOT_WORD */
1401 /* Resolve symbol values. This needs to be done before processing
1402 the relocations. */
1403 if (symbol_rootP)
1405 symbolS *symp;
1407 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1408 resolve_symbol_value (symp);
1410 resolve_local_symbol_values ();
1412 PROGRESS (1);
1414 #ifdef tc_frob_file_before_adjust
1415 tc_frob_file_before_adjust ();
1416 #endif
1417 #ifdef obj_frob_file_before_adjust
1418 obj_frob_file_before_adjust ();
1419 #endif
1421 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1423 #ifdef tc_frob_file_before_fix
1424 tc_frob_file_before_fix ();
1425 #endif
1426 #ifdef obj_frob_file_before_fix
1427 obj_frob_file_before_fix ();
1428 #endif
1430 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1432 /* Set up symbol table, and write it out. */
1433 if (symbol_rootP)
1435 symbolS *symp;
1436 bfd_boolean skip_next_symbol = FALSE;
1438 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1440 int punt = 0;
1441 const char *name;
1443 if (skip_next_symbol)
1445 /* Don't do anything besides moving the value of the
1446 symbol from the GAS value-field to the BFD value-field. */
1447 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1448 skip_next_symbol = FALSE;
1449 continue;
1452 if (symbol_mri_common_p (symp))
1454 if (S_IS_EXTERNAL (symp))
1455 as_bad (_("%s: global symbols not supported in common sections"),
1456 S_GET_NAME (symp));
1457 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1458 continue;
1461 name = S_GET_NAME (symp);
1462 if (name)
1464 const char *name2 =
1465 decode_local_label_name ((char *) S_GET_NAME (symp));
1466 /* They only differ if `name' is a fb or dollar local
1467 label name. */
1468 if (name2 != name && ! S_IS_DEFINED (symp))
1469 as_bad (_("local label `%s' is not defined"), name2);
1472 /* Do it again, because adjust_reloc_syms might introduce
1473 more symbols. They'll probably only be section symbols,
1474 but they'll still need to have the values computed. */
1475 resolve_symbol_value (symp);
1477 /* Skip symbols which were equated to undefined or common
1478 symbols. */
1479 if (symbol_equated_reloc_p (symp)
1480 || S_IS_WEAKREFR (symp))
1482 const char *name = S_GET_NAME (symp);
1483 if (S_IS_COMMON (symp)
1484 && !TC_FAKE_LABEL (name)
1485 && !S_IS_WEAKREFR (symp)
1486 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1488 expressionS *e = symbol_get_value_expression (symp);
1489 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1490 name, S_GET_NAME (e->X_add_symbol));
1492 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1493 continue;
1496 #ifdef obj_frob_symbol
1497 obj_frob_symbol (symp, punt);
1498 #endif
1499 #ifdef tc_frob_symbol
1500 if (! punt || symbol_used_in_reloc_p (symp))
1501 tc_frob_symbol (symp, punt);
1502 #endif
1504 /* If we don't want to keep this symbol, splice it out of
1505 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1506 want section symbols. Otherwise, we skip local symbols
1507 and symbols that the frob_symbol macros told us to punt,
1508 but we keep such symbols if they are used in relocs. */
1509 if (symp == abs_section_sym
1510 || (! EMIT_SECTION_SYMBOLS
1511 && symbol_section_p (symp))
1512 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1513 opposites. Sometimes the former checks flags and the
1514 latter examines the name... */
1515 || (!S_IS_EXTERNAL (symp)
1516 && (punt || S_IS_LOCAL (symp) ||
1517 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1518 && ! symbol_used_in_reloc_p (symp)))
1520 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1522 /* After symbol_remove, symbol_next(symp) still returns
1523 the one that came after it in the chain. So we don't
1524 need to do any extra cleanup work here. */
1525 continue;
1528 /* Make sure we really got a value for the symbol. */
1529 if (! symbol_resolved_p (symp))
1531 as_bad (_("can't resolve value for symbol `%s'"),
1532 S_GET_NAME (symp));
1533 symbol_mark_resolved (symp);
1536 /* Set the value into the BFD symbol. Up til now the value
1537 has only been kept in the gas symbolS struct. */
1538 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1540 /* A warning construct is a warning symbol followed by the
1541 symbol warned about. Don't let anything object-format or
1542 target-specific muck with it; it's ready for output. */
1543 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1544 skip_next_symbol = TRUE;
1548 PROGRESS (1);
1550 /* Now do any format-specific adjustments to the symbol table, such
1551 as adding file symbols. */
1552 #ifdef tc_adjust_symtab
1553 tc_adjust_symtab ();
1554 #endif
1555 #ifdef obj_adjust_symtab
1556 obj_adjust_symtab ();
1557 #endif
1559 /* Now that all the sizes are known, and contents correct, we can
1560 start writing to the file. */
1561 set_symtab ();
1563 /* If *_frob_file changes the symbol value at this point, it is
1564 responsible for moving the changed value into symp->bsym->value
1565 as well. Hopefully all symbol value changing can be done in
1566 *_frob_symbol. */
1567 #ifdef tc_frob_file
1568 tc_frob_file ();
1569 #endif
1570 #ifdef obj_frob_file
1571 obj_frob_file ();
1572 #endif
1574 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1576 #ifdef tc_frob_file_after_relocs
1577 tc_frob_file_after_relocs ();
1578 #endif
1579 #ifdef obj_frob_file_after_relocs
1580 obj_frob_file_after_relocs ();
1581 #endif
1583 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1586 #ifdef TC_GENERIC_RELAX_TABLE
1587 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1589 long
1590 relax_frag (segT segment, fragS *fragP, long stretch)
1592 const relax_typeS *this_type;
1593 const relax_typeS *start_type;
1594 relax_substateT next_state;
1595 relax_substateT this_state;
1596 offsetT growth;
1597 offsetT aim;
1598 addressT target;
1599 addressT address;
1600 symbolS *symbolP;
1601 const relax_typeS *table;
1603 target = fragP->fr_offset;
1604 address = fragP->fr_address;
1605 table = TC_GENERIC_RELAX_TABLE;
1606 this_state = fragP->fr_subtype;
1607 start_type = this_type = table + this_state;
1608 symbolP = fragP->fr_symbol;
1610 if (symbolP)
1612 fragS *sym_frag;
1614 sym_frag = symbol_get_frag (symbolP);
1616 #ifndef DIFF_EXPR_OK
1617 know (sym_frag != NULL);
1618 #endif
1619 know (S_GET_SEGMENT (symbolP) != absolute_section
1620 || sym_frag == &zero_address_frag);
1621 target += S_GET_VALUE (symbolP);
1623 /* If frag has yet to be reached on this pass,
1624 assume it will move by STRETCH just as we did.
1625 If this is not so, it will be because some frag
1626 between grows, and that will force another pass. */
1628 if (stretch != 0
1629 && sym_frag->relax_marker != fragP->relax_marker
1630 && S_GET_SEGMENT (symbolP) == segment)
1632 target += stretch;
1636 aim = target - address - fragP->fr_fix;
1637 #ifdef TC_PCREL_ADJUST
1638 /* Currently only the ns32k family needs this. */
1639 aim += TC_PCREL_ADJUST (fragP);
1640 #endif
1642 #ifdef md_prepare_relax_scan
1643 /* Formerly called M68K_AIM_KLUDGE. */
1644 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1645 #endif
1647 if (aim < 0)
1649 /* Look backwards. */
1650 for (next_state = this_type->rlx_more; next_state;)
1651 if (aim >= this_type->rlx_backward)
1652 next_state = 0;
1653 else
1655 /* Grow to next state. */
1656 this_state = next_state;
1657 this_type = table + this_state;
1658 next_state = this_type->rlx_more;
1661 else
1663 /* Look forwards. */
1664 for (next_state = this_type->rlx_more; next_state;)
1665 if (aim <= this_type->rlx_forward)
1666 next_state = 0;
1667 else
1669 /* Grow to next state. */
1670 this_state = next_state;
1671 this_type = table + this_state;
1672 next_state = this_type->rlx_more;
1676 growth = this_type->rlx_length - start_type->rlx_length;
1677 if (growth != 0)
1678 fragP->fr_subtype = this_state;
1679 return growth;
1682 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1684 /* Relax_align. Advance location counter to next address that has 'alignment'
1685 lowest order bits all 0s, return size of adjustment made. */
1686 static relax_addressT
1687 relax_align (register relax_addressT address, /* Address now. */
1688 register int alignment /* Alignment (binary). */)
1690 relax_addressT mask;
1691 relax_addressT new_address;
1693 mask = ~((~0) << alignment);
1694 new_address = (address + mask) & (~mask);
1695 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1696 if (linkrelax)
1697 /* We must provide lots of padding, so the linker can discard it
1698 when needed. The linker will not add extra space, ever. */
1699 new_address += (1 << alignment);
1700 #endif
1701 return (new_address - address);
1704 /* Now we have a segment, not a crowd of sub-segments, we can make
1705 fr_address values.
1707 Relax the frags.
1709 After this, all frags in this segment have addresses that are correct
1710 within the segment. Since segments live in different file addresses,
1711 these frag addresses may not be the same as final object-file
1712 addresses. */
1715 relax_segment (struct frag *segment_frag_root, segT segment)
1717 unsigned long frag_count;
1718 struct frag *fragP;
1719 relax_addressT address;
1720 int ret;
1722 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1723 subseg_change (segment, 0);
1725 /* For each frag in segment: count and store (a 1st guess of)
1726 fr_address. */
1727 address = 0;
1728 for (frag_count = 0, fragP = segment_frag_root;
1729 fragP;
1730 fragP = fragP->fr_next, frag_count ++)
1732 fragP->relax_marker = 0;
1733 fragP->fr_address = address;
1734 address += fragP->fr_fix;
1736 switch (fragP->fr_type)
1738 case rs_fill:
1739 address += fragP->fr_offset * fragP->fr_var;
1740 break;
1742 case rs_align:
1743 case rs_align_code:
1744 case rs_align_test:
1746 addressT offset = relax_align (address, (int) fragP->fr_offset);
1748 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
1749 offset = 0;
1751 if (offset % fragP->fr_var != 0)
1753 as_bad_where (fragP->fr_file, fragP->fr_line,
1754 _("alignment padding (%lu bytes) not a multiple of %ld"),
1755 (unsigned long) offset, (long) fragP->fr_var);
1756 offset -= (offset % fragP->fr_var);
1759 address += offset;
1761 break;
1763 case rs_org:
1764 case rs_space:
1765 /* Assume .org is nugatory. It will grow with 1st relax. */
1766 break;
1768 case rs_machine_dependent:
1769 /* If fr_symbol is an expression, this call to
1770 resolve_symbol_value sets up the correct segment, which will
1771 likely be needed in md_estimate_size_before_relax. */
1772 if (fragP->fr_symbol)
1773 resolve_symbol_value (fragP->fr_symbol);
1775 address += md_estimate_size_before_relax (fragP, segment);
1776 break;
1778 #ifndef WORKING_DOT_WORD
1779 /* Broken words don't concern us yet. */
1780 case rs_broken_word:
1781 break;
1782 #endif
1784 case rs_leb128:
1785 /* Initial guess is always 1; doing otherwise can result in
1786 stable solutions that are larger than the minimum. */
1787 address += fragP->fr_offset = 1;
1788 break;
1790 case rs_cfa:
1791 address += eh_frame_estimate_size_before_relax (fragP);
1792 break;
1794 case rs_dwarf2dbg:
1795 address += dwarf2dbg_estimate_size_before_relax (fragP);
1796 break;
1798 default:
1799 BAD_CASE (fragP->fr_type);
1800 break;
1804 /* Do relax(). */
1806 unsigned long max_iterations;
1807 offsetT stretch; /* May be any size, 0 or negative. */
1808 /* Cumulative number of addresses we have relaxed this pass.
1809 We may have relaxed more than one address. */
1810 int stretched; /* Have we stretched on this pass? */
1811 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1812 grew, and another shrank. If a branch instruction doesn't fit anymore,
1813 we could be scrod. */
1815 /* We want to prevent going into an infinite loop where one frag grows
1816 depending upon the location of a symbol which is in turn moved by
1817 the growing frag. eg:
1819 foo = .
1820 .org foo+16
1821 foo = .
1823 So we dictate that this algorithm can be at most O2. */
1824 max_iterations = frag_count * frag_count;
1825 /* Check for overflow. */
1826 if (max_iterations < frag_count)
1827 max_iterations = frag_count;
1831 stretch = 0;
1832 stretched = 0;
1834 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1836 offsetT growth = 0;
1837 addressT was_address;
1838 offsetT offset;
1839 symbolS *symbolP;
1841 fragP->relax_marker ^= 1;
1842 was_address = fragP->fr_address;
1843 address = fragP->fr_address += stretch;
1844 symbolP = fragP->fr_symbol;
1845 offset = fragP->fr_offset;
1847 switch (fragP->fr_type)
1849 case rs_fill: /* .fill never relaxes. */
1850 growth = 0;
1851 break;
1853 #ifndef WORKING_DOT_WORD
1854 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1855 for it I do not want to write it. I do not want to have
1856 anything to do with it. This is not the proper way to
1857 implement this misfeature. */
1858 case rs_broken_word:
1860 struct broken_word *lie;
1861 struct broken_word *untruth;
1863 /* Yes this is ugly (storing the broken_word pointer
1864 in the symbol slot). Still, this whole chunk of
1865 code is ugly, and I don't feel like doing anything
1866 about it. Think of it as stubbornness in action. */
1867 growth = 0;
1868 for (lie = (struct broken_word *) (fragP->fr_symbol);
1869 lie && lie->dispfrag == fragP;
1870 lie = lie->next_broken_word)
1873 if (lie->added)
1874 continue;
1876 offset = (S_GET_VALUE (lie->add)
1877 + lie->addnum
1878 - S_GET_VALUE (lie->sub));
1879 if (offset <= -32768 || offset >= 32767)
1881 if (flag_warn_displacement)
1883 char buf[50];
1884 sprint_value (buf, (addressT) lie->addnum);
1885 as_warn_where (fragP->fr_file, fragP->fr_line,
1886 _(".word %s-%s+%s didn't fit"),
1887 S_GET_NAME (lie->add),
1888 S_GET_NAME (lie->sub),
1889 buf);
1891 lie->added = 1;
1892 if (fragP->fr_subtype == 0)
1894 fragP->fr_subtype++;
1895 growth += md_short_jump_size;
1897 for (untruth = lie->next_broken_word;
1898 untruth && untruth->dispfrag == lie->dispfrag;
1899 untruth = untruth->next_broken_word)
1900 if ((symbol_get_frag (untruth->add)
1901 == symbol_get_frag (lie->add))
1902 && (S_GET_VALUE (untruth->add)
1903 == S_GET_VALUE (lie->add)))
1905 untruth->added = 2;
1906 untruth->use_jump = lie;
1908 growth += md_long_jump_size;
1912 break;
1913 } /* case rs_broken_word */
1914 #endif
1915 case rs_align:
1916 case rs_align_code:
1917 case rs_align_test:
1919 addressT oldoff, newoff;
1921 oldoff = relax_align (was_address + fragP->fr_fix,
1922 (int) offset);
1923 newoff = relax_align (address + fragP->fr_fix,
1924 (int) offset);
1926 if (fragP->fr_subtype != 0)
1928 if (oldoff > fragP->fr_subtype)
1929 oldoff = 0;
1930 if (newoff > fragP->fr_subtype)
1931 newoff = 0;
1934 growth = newoff - oldoff;
1936 break;
1938 case rs_org:
1940 addressT target = offset;
1941 addressT after;
1943 if (symbolP)
1945 /* Convert from an actual address to an octet offset
1946 into the section. Here it is assumed that the
1947 section's VMA is zero, and can omit subtracting it
1948 from the symbol's value to get the address offset. */
1949 know (S_GET_SEGMENT (symbolP)->vma == 0);
1950 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
1953 know (fragP->fr_next);
1954 after = fragP->fr_next->fr_address;
1955 growth = target - after;
1956 if (growth < 0)
1958 /* Growth may be negative, but variable part of frag
1959 cannot have fewer than 0 chars. That is, we can't
1960 .org backwards. */
1961 as_bad_where (fragP->fr_file, fragP->fr_line,
1962 _("attempt to move .org backwards"));
1964 /* We've issued an error message. Change the
1965 frag to avoid cascading errors. */
1966 fragP->fr_type = rs_align;
1967 fragP->fr_subtype = 0;
1968 fragP->fr_offset = 0;
1969 fragP->fr_fix = after - was_address;
1970 growth = stretch;
1973 /* This is an absolute growth factor */
1974 growth -= stretch;
1975 break;
1978 case rs_space:
1979 growth = 0;
1980 if (symbolP)
1982 offsetT amount;
1984 amount = S_GET_VALUE (symbolP);
1985 if (S_GET_SEGMENT (symbolP) != absolute_section
1986 || S_IS_COMMON (symbolP)
1987 || ! S_IS_DEFINED (symbolP))
1989 as_bad_where (fragP->fr_file, fragP->fr_line,
1990 _(".space specifies non-absolute value"));
1991 /* Prevent repeat of this error message. */
1992 fragP->fr_symbol = 0;
1994 else if (amount < 0)
1996 as_warn_where (fragP->fr_file, fragP->fr_line,
1997 _(".space or .fill with negative value, ignored"));
1998 fragP->fr_symbol = 0;
2000 else
2001 growth = (was_address + fragP->fr_fix + amount
2002 - fragP->fr_next->fr_address);
2004 break;
2006 case rs_machine_dependent:
2007 #ifdef md_relax_frag
2008 growth = md_relax_frag (segment, fragP, stretch);
2009 #else
2010 #ifdef TC_GENERIC_RELAX_TABLE
2011 /* The default way to relax a frag is to look through
2012 TC_GENERIC_RELAX_TABLE. */
2013 growth = relax_frag (segment, fragP, stretch);
2014 #endif /* TC_GENERIC_RELAX_TABLE */
2015 #endif
2016 break;
2018 case rs_leb128:
2020 valueT value;
2021 offsetT size;
2023 value = resolve_symbol_value (fragP->fr_symbol);
2024 size = sizeof_leb128 (value, fragP->fr_subtype);
2025 growth = size - fragP->fr_offset;
2026 fragP->fr_offset = size;
2028 break;
2030 case rs_cfa:
2031 growth = eh_frame_relax_frag (fragP);
2032 break;
2034 case rs_dwarf2dbg:
2035 growth = dwarf2dbg_relax_frag (fragP);
2036 break;
2038 default:
2039 BAD_CASE (fragP->fr_type);
2040 break;
2042 if (growth)
2044 stretch += growth;
2045 stretched = 1;
2049 /* Until nothing further to relax. */
2050 while (stretched && -- max_iterations);
2052 if (stretched)
2053 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2054 segment_name (segment));
2057 ret = 0;
2058 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2059 if (fragP->last_fr_address != fragP->fr_address)
2061 fragP->last_fr_address = fragP->fr_address;
2062 ret = 1;
2064 return ret;
2067 /* fixup_segment()
2069 Go through all the fixS's in a segment and see which ones can be
2070 handled now. (These consist of fixS where we have since discovered
2071 the value of a symbol, or the address of the frag involved.)
2072 For each one, call md_apply_fix to put the fix into the frag data.
2074 Result is a count of how many relocation structs will be needed to
2075 handle the remaining fixS's that we couldn't completely handle here.
2076 These will be output later by emit_relocations(). */
2078 static long
2079 fixup_segment (fixS *fixP, segT this_segment)
2081 long seg_reloc_count = 0;
2082 valueT add_number;
2083 fragS *fragP;
2084 segT add_symbol_segment = absolute_section;
2086 if (fixP != NULL && abs_section_sym == NULL)
2087 abs_section_sym = section_symbol (absolute_section);
2089 /* If the linker is doing the relaxing, we must not do any fixups.
2091 Well, strictly speaking that's not true -- we could do any that
2092 are PC-relative and don't cross regions that could change size.
2093 And for the i960 we might be able to turn callx/callj into bal
2094 anyways in cases where we know the maximum displacement. */
2095 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2097 for (; fixP; fixP = fixP->fx_next)
2098 if (!fixP->fx_done)
2100 if (fixP->fx_addsy == NULL)
2102 /* There was no symbol required by this relocation.
2103 However, BFD doesn't really handle relocations
2104 without symbols well. So fake up a local symbol in
2105 the absolute section. */
2106 fixP->fx_addsy = abs_section_sym;
2108 symbol_mark_used_in_reloc (fixP->fx_addsy);
2109 if (fixP->fx_subsy != NULL)
2110 symbol_mark_used_in_reloc (fixP->fx_subsy);
2111 seg_reloc_count++;
2113 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2114 return seg_reloc_count;
2117 for (; fixP; fixP = fixP->fx_next)
2119 #ifdef DEBUG5
2120 fprintf (stderr, "\nprocessing fixup:\n");
2121 print_fixup (fixP);
2122 #endif
2124 fragP = fixP->fx_frag;
2125 know (fragP);
2126 #ifdef TC_VALIDATE_FIX
2127 TC_VALIDATE_FIX (fixP, this_segment, skip);
2128 #endif
2129 add_number = fixP->fx_offset;
2131 if (fixP->fx_addsy != NULL
2132 && symbol_mri_common_p (fixP->fx_addsy))
2134 add_number += S_GET_VALUE (fixP->fx_addsy);
2135 fixP->fx_offset = add_number;
2136 fixP->fx_addsy
2137 = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2140 if (fixP->fx_addsy != NULL)
2141 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2143 if (fixP->fx_subsy != NULL)
2145 segT sub_symbol_segment;
2146 resolve_symbol_value (fixP->fx_subsy);
2147 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2148 if (fixP->fx_addsy != NULL
2149 && sub_symbol_segment == add_symbol_segment
2150 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2152 add_number += S_GET_VALUE (fixP->fx_addsy);
2153 add_number -= S_GET_VALUE (fixP->fx_subsy);
2154 fixP->fx_offset = add_number;
2155 fixP->fx_addsy = NULL;
2156 fixP->fx_subsy = NULL;
2157 #ifdef TC_M68K
2158 /* See the comment below about 68k weirdness. */
2159 fixP->fx_pcrel = 0;
2160 #endif
2162 else if (sub_symbol_segment == absolute_section
2163 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2165 add_number -= S_GET_VALUE (fixP->fx_subsy);
2166 fixP->fx_offset = add_number;
2167 fixP->fx_subsy = NULL;
2169 else if (sub_symbol_segment == this_segment
2170 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2172 add_number -= S_GET_VALUE (fixP->fx_subsy);
2173 fixP->fx_offset = (add_number + fixP->fx_dot_value
2174 + fixP->fx_frag->fr_address);
2176 /* Make it pc-relative. If the back-end code has not
2177 selected a pc-relative reloc, cancel the adjustment
2178 we do later on all pc-relative relocs. */
2179 if (0
2180 #ifdef TC_M68K
2181 /* Do this for m68k even if it's already described
2182 as pc-relative. On the m68k, an operand of
2183 "pc@(foo-.-2)" should address "foo" in a
2184 pc-relative mode. */
2185 || 1
2186 #endif
2187 || !fixP->fx_pcrel)
2188 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2189 fixP->fx_subsy = NULL;
2190 fixP->fx_pcrel = 1;
2192 else if (!TC_VALIDATE_FIX_SUB (fixP))
2194 as_bad_where (fixP->fx_file, fixP->fx_line,
2195 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2196 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2197 segment_name (add_symbol_segment),
2198 S_GET_NAME (fixP->fx_subsy),
2199 segment_name (sub_symbol_segment));
2203 if (fixP->fx_addsy)
2205 if (add_symbol_segment == this_segment
2206 && !TC_FORCE_RELOCATION_LOCAL (fixP))
2208 /* This fixup was made when the symbol's segment was
2209 SEG_UNKNOWN, but it is now in the local segment.
2210 So we know how to do the address without relocation. */
2211 add_number += S_GET_VALUE (fixP->fx_addsy);
2212 fixP->fx_offset = add_number;
2213 if (fixP->fx_pcrel)
2214 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2215 fixP->fx_addsy = NULL;
2216 fixP->fx_pcrel = 0;
2218 else if (add_symbol_segment == absolute_section
2219 && !TC_FORCE_RELOCATION_ABS (fixP))
2221 add_number += S_GET_VALUE (fixP->fx_addsy);
2222 fixP->fx_offset = add_number;
2223 fixP->fx_addsy = NULL;
2225 else if (add_symbol_segment != undefined_section
2226 && ! bfd_is_com_section (add_symbol_segment)
2227 && MD_APPLY_SYM_VALUE (fixP))
2228 add_number += S_GET_VALUE (fixP->fx_addsy);
2231 if (fixP->fx_pcrel)
2233 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2234 if (!fixP->fx_done && fixP->fx_addsy == NULL)
2236 /* There was no symbol required by this relocation.
2237 However, BFD doesn't really handle relocations
2238 without symbols well. So fake up a local symbol in
2239 the absolute section. */
2240 fixP->fx_addsy = abs_section_sym;
2244 if (!fixP->fx_done)
2245 md_apply_fix (fixP, &add_number, this_segment);
2247 if (!fixP->fx_done)
2249 ++seg_reloc_count;
2250 if (fixP->fx_addsy == NULL)
2251 fixP->fx_addsy = abs_section_sym;
2252 symbol_mark_used_in_reloc (fixP->fx_addsy);
2253 if (fixP->fx_subsy != NULL)
2254 symbol_mark_used_in_reloc (fixP->fx_subsy);
2257 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2259 if (fixP->fx_size < sizeof (valueT))
2261 valueT mask;
2263 mask = 0;
2264 mask--; /* Set all bits to one. */
2265 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2266 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2268 char buf[50], buf2[50];
2269 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2270 if (add_number > 1000)
2271 sprint_value (buf2, add_number);
2272 else
2273 sprintf (buf2, "%ld", (long) add_number);
2274 as_bad_where (fixP->fx_file, fixP->fx_line,
2275 _("value of %s too large for field of %d bytes at %s"),
2276 buf2, fixP->fx_size, buf);
2277 } /* Generic error checking. */
2279 #ifdef WARN_SIGNED_OVERFLOW_WORD
2280 /* Warn if a .word value is too large when treated as a signed
2281 number. We already know it is not too negative. This is to
2282 catch over-large switches generated by gcc on the 68k. */
2283 if (!flag_signed_overflow_ok
2284 && fixP->fx_size == 2
2285 && add_number > 0x7fff)
2286 as_bad_where (fixP->fx_file, fixP->fx_line,
2287 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2288 (long) add_number,
2289 (long) (fragP->fr_address + fixP->fx_where));
2290 #endif
2291 } /* Not a bit fix. */
2293 #ifdef TC_VALIDATE_FIX
2294 skip: ATTRIBUTE_UNUSED_LABEL
2296 #endif
2297 #ifdef DEBUG5
2298 fprintf (stderr, "result:\n");
2299 print_fixup (fixP);
2300 #endif
2301 } /* For each fixS in this segment. */
2303 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2304 return seg_reloc_count;
2307 void
2308 number_to_chars_bigendian (char *buf, valueT val, int n)
2310 if (n <= 0)
2311 abort ();
2312 while (n--)
2314 buf[n] = val & 0xff;
2315 val >>= 8;
2319 void
2320 number_to_chars_littleendian (char *buf, valueT val, int n)
2322 if (n <= 0)
2323 abort ();
2324 while (n--)
2326 *buf++ = val & 0xff;
2327 val >>= 8;
2331 void
2332 write_print_statistics (FILE *file)
2334 fprintf (file, "fixups: %d\n", n_fixups);
2337 /* For debugging. */
2338 extern int indent_level;
2340 void
2341 print_fixup (fixS *fixp)
2343 indent_level = 1;
2344 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2345 if (fixp->fx_pcrel)
2346 fprintf (stderr, " pcrel");
2347 if (fixp->fx_pcrel_adjust)
2348 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2349 if (fixp->fx_im_disp)
2351 #ifdef TC_NS32K
2352 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2353 #else
2354 fprintf (stderr, " im_disp");
2355 #endif
2357 if (fixp->fx_tcbit)
2358 fprintf (stderr, " tcbit");
2359 if (fixp->fx_done)
2360 fprintf (stderr, " done");
2361 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2362 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2363 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2364 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2365 fixp->fx_r_type);
2366 if (fixp->fx_addsy)
2368 fprintf (stderr, "\n +<");
2369 print_symbol_value_1 (stderr, fixp->fx_addsy);
2370 fprintf (stderr, ">");
2372 if (fixp->fx_subsy)
2374 fprintf (stderr, "\n -<");
2375 print_symbol_value_1 (stderr, fixp->fx_subsy);
2376 fprintf (stderr, ">");
2378 fprintf (stderr, "\n");
2379 #ifdef TC_FIX_DATA_PRINT
2380 TC_FIX_DATA_PRINT (stderr, fixp);
2381 #endif