2011-09-19 Tristan Gingold <gingold@adacore.com>
[binutils.git] / gas / write.c
blobcf59d7da883a0d7d716dd938e83dfd86bac56677
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, 2006, 2007, 2008, 2009,
4 2010, 2011 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 3, 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"
30 #include "libbfd.h"
31 #include "compress-debug.h"
33 #ifndef TC_ADJUST_RELOC_COUNT
34 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
35 #endif
37 #ifndef TC_FORCE_RELOCATION
38 #define TC_FORCE_RELOCATION(FIX) \
39 (generic_force_reloc (FIX))
40 #endif
42 #ifndef TC_FORCE_RELOCATION_ABS
43 #define TC_FORCE_RELOCATION_ABS(FIX) \
44 (TC_FORCE_RELOCATION (FIX))
45 #endif
47 #ifndef TC_FORCE_RELOCATION_LOCAL
48 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
49 (!(FIX)->fx_pcrel \
50 || TC_FORCE_RELOCATION (FIX))
51 #endif
53 #ifndef TC_FORCE_RELOCATION_SUB_SAME
54 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
55 (! SEG_NORMAL (SEG))
56 #endif
58 #ifndef md_register_arithmetic
59 # define md_register_arithmetic 1
60 #endif
62 #ifndef TC_FORCE_RELOCATION_SUB_ABS
63 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
64 (!md_register_arithmetic && (SEG) == reg_section)
65 #endif
67 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
68 #ifdef DIFF_EXPR_OK
69 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
70 (!md_register_arithmetic && (SEG) == reg_section)
71 #else
72 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
73 #endif
74 #endif
76 #ifndef TC_VALIDATE_FIX_SUB
77 #ifdef UNDEFINED_DIFFERENCE_OK
78 /* The PA needs this for PIC code generation. */
79 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
80 (md_register_arithmetic || (SEG) != reg_section)
81 #else
82 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
83 ((md_register_arithmetic || (SEG) != reg_section) \
84 && ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
85 || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
86 #endif
87 #endif
89 #ifndef TC_LINKRELAX_FIXUP
90 #define TC_LINKRELAX_FIXUP(SEG) 1
91 #endif
93 #ifndef MD_APPLY_SYM_VALUE
94 #define MD_APPLY_SYM_VALUE(FIX) 1
95 #endif
97 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
98 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
99 #endif
101 #ifndef MD_PCREL_FROM_SECTION
102 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
103 #endif
105 #ifndef TC_FAKE_LABEL
106 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
107 #endif
109 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
110 fixups that far past the end of a frag. Having such fixups
111 is of course most most likely a bug in setting fx_size correctly.
112 A negative value disables the fixup check entirely, which is
113 appropriate for something like the Renesas / SuperH SH_COUNT
114 reloc. */
115 #ifndef TC_FX_SIZE_SLACK
116 #define TC_FX_SIZE_SLACK(FIX) 0
117 #endif
119 /* Used to control final evaluation of expressions. */
120 int finalize_syms = 0;
122 int symbol_table_frozen;
124 symbolS *abs_section_sym;
126 /* Remember the value of dot when parsing expressions. */
127 addressT dot_value;
129 /* Relocs generated by ".reloc" pseudo. */
130 struct reloc_list* reloc_list;
132 void print_fixup (fixS *);
134 /* We generally attach relocs to frag chains. However, after we have
135 chained these all together into a segment, any relocs we add after
136 that must be attached to a segment. This will include relocs added
137 in md_estimate_size_for_relax, for example. */
138 static int frags_chained = 0;
140 static int n_fixups;
142 #define RELOC_ENUM enum bfd_reloc_code_real
144 /* Create a fixS in obstack 'notes'. */
146 static fixS *
147 fix_new_internal (fragS *frag, /* Which frag? */
148 int where, /* Where in that frag? */
149 int size, /* 1, 2, or 4 usually. */
150 symbolS *add_symbol, /* X_add_symbol. */
151 symbolS *sub_symbol, /* X_op_symbol. */
152 offsetT offset, /* X_add_number. */
153 int pcrel, /* TRUE if PC-relative relocation. */
154 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */,
155 int at_beginning) /* Add to the start of the list? */
157 fixS *fixP;
159 n_fixups++;
161 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
163 fixP->fx_frag = frag;
164 fixP->fx_where = where;
165 fixP->fx_size = size;
166 /* We've made fx_size a narrow field; check that it's wide enough. */
167 if (fixP->fx_size != size)
169 as_bad (_("field fx_size too small to hold %d"), size);
170 abort ();
172 fixP->fx_addsy = add_symbol;
173 fixP->fx_subsy = sub_symbol;
174 fixP->fx_offset = offset;
175 fixP->fx_dot_value = dot_value;
176 fixP->fx_pcrel = pcrel;
177 fixP->fx_r_type = r_type;
178 fixP->fx_im_disp = 0;
179 fixP->fx_pcrel_adjust = 0;
180 fixP->fx_bit_fixP = 0;
181 fixP->fx_addnumber = 0;
182 fixP->fx_tcbit = 0;
183 fixP->fx_tcbit2 = 0;
184 fixP->fx_done = 0;
185 fixP->fx_no_overflow = 0;
186 fixP->fx_signed = 0;
188 #ifdef USING_CGEN
189 fixP->fx_cgen.insn = NULL;
190 fixP->fx_cgen.opinfo = 0;
191 #endif
193 #ifdef TC_FIX_TYPE
194 TC_INIT_FIX_DATA (fixP);
195 #endif
197 as_where (&fixP->fx_file, &fixP->fx_line);
201 fixS **seg_fix_rootP = (frags_chained
202 ? &seg_info (now_seg)->fix_root
203 : &frchain_now->fix_root);
204 fixS **seg_fix_tailP = (frags_chained
205 ? &seg_info (now_seg)->fix_tail
206 : &frchain_now->fix_tail);
208 if (at_beginning)
210 fixP->fx_next = *seg_fix_rootP;
211 *seg_fix_rootP = fixP;
212 if (fixP->fx_next == NULL)
213 *seg_fix_tailP = fixP;
215 else
217 fixP->fx_next = NULL;
218 if (*seg_fix_tailP)
219 (*seg_fix_tailP)->fx_next = fixP;
220 else
221 *seg_fix_rootP = fixP;
222 *seg_fix_tailP = fixP;
226 return fixP;
229 /* Create a fixup relative to a symbol (plus a constant). */
231 fixS *
232 fix_new (fragS *frag, /* Which frag? */
233 int where, /* Where in that frag? */
234 int size, /* 1, 2, or 4 usually. */
235 symbolS *add_symbol, /* X_add_symbol. */
236 offsetT offset, /* X_add_number. */
237 int pcrel, /* TRUE if PC-relative relocation. */
238 RELOC_ENUM r_type /* Relocation type. */)
240 return fix_new_internal (frag, where, size, add_symbol,
241 (symbolS *) NULL, offset, pcrel, r_type, FALSE);
244 /* Create a fixup for an expression. Currently we only support fixups
245 for difference expressions. That is itself more than most object
246 file formats support anyhow. */
248 fixS *
249 fix_new_exp (fragS *frag, /* Which frag? */
250 int where, /* Where in that frag? */
251 int size, /* 1, 2, or 4 usually. */
252 expressionS *exp, /* Expression. */
253 int pcrel, /* TRUE if PC-relative relocation. */
254 RELOC_ENUM r_type /* Relocation type. */)
256 symbolS *add = NULL;
257 symbolS *sub = NULL;
258 offsetT off = 0;
260 switch (exp->X_op)
262 case O_absent:
263 break;
265 case O_register:
266 as_bad (_("register value used as expression"));
267 break;
269 case O_add:
270 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
271 the difference expression cannot immediately be reduced. */
273 symbolS *stmp = make_expr_symbol (exp);
275 exp->X_op = O_symbol;
276 exp->X_op_symbol = 0;
277 exp->X_add_symbol = stmp;
278 exp->X_add_number = 0;
280 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
283 case O_symbol_rva:
284 add = exp->X_add_symbol;
285 off = exp->X_add_number;
286 r_type = BFD_RELOC_RVA;
287 break;
289 case O_uminus:
290 sub = exp->X_add_symbol;
291 off = exp->X_add_number;
292 break;
294 case O_subtract:
295 sub = exp->X_op_symbol;
296 /* Fall through. */
297 case O_symbol:
298 add = exp->X_add_symbol;
299 /* Fall through. */
300 case O_constant:
301 off = exp->X_add_number;
302 break;
304 default:
305 add = make_expr_symbol (exp);
306 break;
309 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
310 r_type, FALSE);
313 /* Create a fixup at the beginning of FRAG. The arguments are the same
314 as for fix_new, except that WHERE is implicitly 0. */
316 fixS *
317 fix_at_start (fragS *frag, int size, symbolS *add_symbol,
318 offsetT offset, int pcrel, RELOC_ENUM r_type)
320 return fix_new_internal (frag, 0, size, add_symbol,
321 (symbolS *) NULL, offset, pcrel, r_type, TRUE);
324 /* Generic function to determine whether a fixup requires a relocation. */
326 generic_force_reloc (fixS *fix)
328 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
329 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
330 return 1;
332 if (fix->fx_addsy == NULL)
333 return 0;
335 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
338 /* Append a string onto another string, bumping the pointer along. */
339 void
340 append (char **charPP, char *fromP, unsigned long length)
342 /* Don't trust memcpy() of 0 chars. */
343 if (length == 0)
344 return;
346 memcpy (*charPP, fromP, length);
347 *charPP += length;
350 /* This routine records the largest alignment seen for each segment.
351 If the beginning of the segment is aligned on the worst-case
352 boundary, all of the other alignments within it will work. At
353 least one object format really uses this info. */
355 void
356 record_alignment (/* Segment to which alignment pertains. */
357 segT seg,
358 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
359 boundary, 2 => 4-byte boundary, etc.) */
360 int align)
362 if (seg == absolute_section)
363 return;
365 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
366 bfd_set_section_alignment (stdoutput, seg, align);
370 get_recorded_alignment (segT seg)
372 if (seg == absolute_section)
373 return 0;
375 return bfd_get_section_alignment (stdoutput, seg);
378 /* Reset the section indices after removing the gas created sections. */
380 static void
381 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
383 int *countp = (int *) countparg;
385 sec->index = *countp;
386 ++*countp;
389 static fragS *
390 chain_frchains_together_1 (segT section, struct frchain *frchp)
392 fragS dummy, *prev_frag = &dummy;
393 fixS fix_dummy, *prev_fix = &fix_dummy;
395 for (; frchp; frchp = frchp->frch_next)
397 prev_frag->fr_next = frchp->frch_root;
398 prev_frag = frchp->frch_last;
399 gas_assert (prev_frag->fr_type != 0);
400 if (frchp->fix_root != (fixS *) NULL)
402 if (seg_info (section)->fix_root == (fixS *) NULL)
403 seg_info (section)->fix_root = frchp->fix_root;
404 prev_fix->fx_next = frchp->fix_root;
405 seg_info (section)->fix_tail = frchp->fix_tail;
406 prev_fix = frchp->fix_tail;
409 gas_assert (prev_frag->fr_type != 0);
410 gas_assert (prev_frag != &dummy);
411 prev_frag->fr_next = 0;
412 return prev_frag;
415 static void
416 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
417 segT section,
418 void *xxx ATTRIBUTE_UNUSED)
420 segment_info_type *info;
422 /* BFD may have introduced its own sections without using
423 subseg_new, so it is possible that seg_info is NULL. */
424 info = seg_info (section);
425 if (info != (segment_info_type *) NULL)
426 info->frchainP->frch_last
427 = chain_frchains_together_1 (section, info->frchainP);
429 /* Now that we've chained the frags together, we must add new fixups
430 to the segment, not to the frag chain. */
431 frags_chained = 1;
434 static void
435 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
437 switch (fragP->fr_type)
439 case rs_align:
440 case rs_align_code:
441 case rs_align_test:
442 case rs_org:
443 case rs_space:
444 #ifdef HANDLE_ALIGN
445 HANDLE_ALIGN (fragP);
446 #endif
447 know (fragP->fr_next != NULL);
448 fragP->fr_offset = (fragP->fr_next->fr_address
449 - fragP->fr_address
450 - fragP->fr_fix) / fragP->fr_var;
451 if (fragP->fr_offset < 0)
453 as_bad_where (fragP->fr_file, fragP->fr_line,
454 _("attempt to .org/.space backwards? (%ld)"),
455 (long) fragP->fr_offset);
456 fragP->fr_offset = 0;
458 fragP->fr_type = rs_fill;
459 break;
461 case rs_fill:
462 break;
464 case rs_leb128:
466 valueT value = S_GET_VALUE (fragP->fr_symbol);
467 int size;
469 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
470 fragP->fr_subtype);
472 fragP->fr_fix += size;
473 fragP->fr_type = rs_fill;
474 fragP->fr_var = 0;
475 fragP->fr_offset = 0;
476 fragP->fr_symbol = NULL;
478 break;
480 case rs_cfa:
481 eh_frame_convert_frag (fragP);
482 break;
484 case rs_dwarf2dbg:
485 dwarf2dbg_convert_frag (fragP);
486 break;
488 case rs_machine_dependent:
489 md_convert_frag (stdoutput, sec, fragP);
491 gas_assert (fragP->fr_next == NULL
492 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
493 == fragP->fr_fix));
495 /* After md_convert_frag, we make the frag into a ".space 0".
496 md_convert_frag() should set up any fixSs and constants
497 required. */
498 frag_wane (fragP);
499 break;
501 #ifndef WORKING_DOT_WORD
502 case rs_broken_word:
504 struct broken_word *lie;
506 if (fragP->fr_subtype)
508 fragP->fr_fix += md_short_jump_size;
509 for (lie = (struct broken_word *) (fragP->fr_symbol);
510 lie && lie->dispfrag == fragP;
511 lie = lie->next_broken_word)
512 if (lie->added == 1)
513 fragP->fr_fix += md_long_jump_size;
515 frag_wane (fragP);
517 break;
518 #endif
520 default:
521 BAD_CASE (fragP->fr_type);
522 break;
524 #ifdef md_frag_check
525 md_frag_check (fragP);
526 #endif
529 struct relax_seg_info
531 int pass;
532 int changed;
535 static void
536 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
538 segment_info_type *seginfo = seg_info (sec);
539 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
541 if (seginfo && seginfo->frchainP
542 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
543 info->changed = 1;
546 static void
547 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
549 flagword flags;
550 fragS *fragp;
551 segment_info_type *seginfo;
552 int x;
553 valueT size, newsize;
555 subseg_change (sec, 0);
557 seginfo = seg_info (sec);
558 if (seginfo && seginfo->frchainP)
560 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
561 cvt_frag_to_fill (sec, fragp);
562 for (fragp = seginfo->frchainP->frch_root;
563 fragp->fr_next;
564 fragp = fragp->fr_next)
565 /* Walk to last elt. */
567 size = fragp->fr_address + fragp->fr_fix;
569 else
570 size = 0;
572 flags = bfd_get_section_flags (abfd, sec);
573 if (size == 0 && bfd_get_section_size (sec) != 0 &&
574 (flags & SEC_HAS_CONTENTS) != 0)
575 return;
577 if (size > 0 && ! seginfo->bss)
578 flags |= SEC_HAS_CONTENTS;
580 flags &= ~SEC_RELOC;
581 x = bfd_set_section_flags (abfd, sec, flags);
582 gas_assert (x);
584 newsize = md_section_align (sec, size);
585 x = bfd_set_section_size (abfd, sec, newsize);
586 gas_assert (x);
588 /* If the size had to be rounded up, add some padding in the last
589 non-empty frag. */
590 gas_assert (newsize >= size);
591 if (size != newsize)
593 fragS *last = seginfo->frchainP->frch_last;
594 fragp = seginfo->frchainP->frch_root;
595 while (fragp->fr_next != last)
596 fragp = fragp->fr_next;
597 last->fr_address = size;
598 if ((newsize - size) % fragp->fr_var == 0)
599 fragp->fr_offset += (newsize - size) / fragp->fr_var;
600 else
601 /* If we hit this abort, it's likely due to subsegs_finish not
602 providing sufficient alignment on the last frag, and the
603 machine dependent code using alignment frags with fr_var
604 greater than 1. */
605 abort ();
608 #ifdef tc_frob_section
609 tc_frob_section (sec);
610 #endif
611 #ifdef obj_frob_section
612 obj_frob_section (sec);
613 #endif
616 #ifdef DEBUG2
617 static void
618 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
620 segment_info_type *seginfo = seg_info (sec);
621 fixS *fixp = seginfo->fix_root;
623 if (!fixp)
624 return;
626 fprintf (stream, "sec %s relocs:\n", sec->name);
627 while (fixp)
629 symbolS *s = fixp->fx_addsy;
631 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
632 (int) fixp->fx_r_type);
633 if (s == NULL)
634 fprintf (stream, "no sym\n");
635 else
637 print_symbol_value_1 (stream, s);
638 fprintf (stream, "\n");
640 fixp = fixp->fx_next;
643 #else
644 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
645 #endif
647 #ifndef EMIT_SECTION_SYMBOLS
648 #define EMIT_SECTION_SYMBOLS 1
649 #endif
651 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
652 and check for validity. Convert RELOC_LIST from using U.A fields
653 to U.B fields. */
654 static void
655 resolve_reloc_expr_symbols (void)
657 struct reloc_list *r;
659 for (r = reloc_list; r; r = r->next)
661 expressionS *symval;
662 symbolS *sym;
663 bfd_vma offset, addend;
664 asection *sec;
665 reloc_howto_type *howto;
667 resolve_symbol_value (r->u.a.offset_sym);
668 symval = symbol_get_value_expression (r->u.a.offset_sym);
670 offset = 0;
671 sym = NULL;
672 if (symval->X_op == O_constant)
673 sym = r->u.a.offset_sym;
674 else if (symval->X_op == O_symbol)
676 sym = symval->X_add_symbol;
677 offset = symval->X_add_number;
678 symval = symbol_get_value_expression (symval->X_add_symbol);
680 if (sym == NULL
681 || symval->X_op != O_constant
682 || (sec = S_GET_SEGMENT (sym)) == NULL
683 || !SEG_NORMAL (sec))
685 as_bad_where (r->file, r->line, _("invalid offset expression"));
686 sec = NULL;
688 else
689 offset += S_GET_VALUE (sym);
691 sym = NULL;
692 addend = r->u.a.addend;
693 if (r->u.a.sym != NULL)
695 resolve_symbol_value (r->u.a.sym);
696 symval = symbol_get_value_expression (r->u.a.sym);
697 if (symval->X_op == O_constant)
698 sym = r->u.a.sym;
699 else if (symval->X_op == O_symbol)
701 sym = symval->X_add_symbol;
702 addend += symval->X_add_number;
703 symval = symbol_get_value_expression (symval->X_add_symbol);
705 if (symval->X_op != O_constant)
707 as_bad_where (r->file, r->line, _("invalid reloc expression"));
708 sec = NULL;
710 else if (sym != NULL)
712 if (S_IS_LOCAL (sym) && !symbol_section_p (sym))
714 asection *symsec = S_GET_SEGMENT (sym);
715 if (!(((symsec->flags & SEC_MERGE) != 0
716 && addend != 0)
717 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
719 addend += S_GET_VALUE (sym);
720 sym = section_symbol (symsec);
723 symbol_mark_used_in_reloc (sym);
726 if (sym == NULL)
728 if (abs_section_sym == NULL)
729 abs_section_sym = section_symbol (absolute_section);
730 sym = abs_section_sym;
733 howto = r->u.a.howto;
735 r->u.b.sec = sec;
736 r->u.b.s = symbol_get_bfdsym (sym);
737 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
738 r->u.b.r.address = offset;
739 r->u.b.r.addend = addend;
740 r->u.b.r.howto = howto;
744 /* This pass over fixups decides whether symbols can be replaced with
745 section symbols. */
747 static void
748 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
749 asection *sec,
750 void *xxx ATTRIBUTE_UNUSED)
752 segment_info_type *seginfo = seg_info (sec);
753 fixS *fixp;
755 if (seginfo == NULL)
756 return;
758 dump_section_relocs (abfd, sec, stderr);
760 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
761 if (fixp->fx_done)
762 /* Ignore it. */
764 else if (fixp->fx_addsy)
766 symbolS *sym;
767 asection *symsec;
769 #ifdef DEBUG5
770 fprintf (stderr, "\n\nadjusting fixup:\n");
771 print_fixup (fixp);
772 #endif
774 sym = fixp->fx_addsy;
776 /* All symbols should have already been resolved at this
777 point. It is possible to see unresolved expression
778 symbols, though, since they are not in the regular symbol
779 table. */
780 resolve_symbol_value (sym);
782 if (fixp->fx_subsy != NULL)
783 resolve_symbol_value (fixp->fx_subsy);
785 /* If this symbol is equated to an undefined or common symbol,
786 convert the fixup to being against that symbol. */
787 while (symbol_equated_reloc_p (sym)
788 || S_IS_WEAKREFR (sym))
790 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
791 if (sym == newsym)
792 break;
793 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
794 fixp->fx_addsy = newsym;
795 sym = newsym;
798 if (symbol_mri_common_p (sym))
800 fixp->fx_offset += S_GET_VALUE (sym);
801 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
802 continue;
805 /* If the symbol is undefined, common, weak, or global (ELF
806 shared libs), we can't replace it with the section symbol. */
807 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
808 continue;
810 /* Is there some other (target cpu dependent) reason we can't adjust
811 this one? (E.g. relocations involving function addresses on
812 the PA. */
813 #ifdef tc_fix_adjustable
814 if (! tc_fix_adjustable (fixp))
815 continue;
816 #endif
818 /* Since we're reducing to section symbols, don't attempt to reduce
819 anything that's already using one. */
820 if (symbol_section_p (sym))
821 continue;
823 symsec = S_GET_SEGMENT (sym);
824 if (symsec == NULL)
825 abort ();
827 if (bfd_is_abs_section (symsec))
829 /* The fixup_segment routine normally will not use this
830 symbol in a relocation. */
831 continue;
834 /* Don't try to reduce relocs which refer to non-local symbols
835 in .linkonce sections. It can lead to confusion when a
836 debugging section refers to a .linkonce section. I hope
837 this will always be correct. */
838 if (symsec != sec && ! S_IS_LOCAL (sym))
840 if ((symsec->flags & SEC_LINK_ONCE) != 0
841 || (IS_ELF
842 /* The GNU toolchain uses an extension for ELF: a
843 section beginning with the magic string
844 .gnu.linkonce is a linkonce section. */
845 && strncmp (segment_name (symsec), ".gnu.linkonce",
846 sizeof ".gnu.linkonce" - 1) == 0))
847 continue;
850 /* Never adjust a reloc against local symbol in a merge section
851 with non-zero addend. */
852 if ((symsec->flags & SEC_MERGE) != 0
853 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
854 continue;
856 /* Never adjust a reloc against TLS local symbol. */
857 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
858 continue;
860 /* We refetch the segment when calling section_symbol, rather
861 than using symsec, because S_GET_VALUE may wind up changing
862 the section when it calls resolve_symbol_value. */
863 fixp->fx_offset += S_GET_VALUE (sym);
864 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
865 #ifdef DEBUG5
866 fprintf (stderr, "\nadjusted fixup:\n");
867 print_fixup (fixp);
868 #endif
871 dump_section_relocs (abfd, sec, stderr);
874 /* fixup_segment()
876 Go through all the fixS's in a segment and see which ones can be
877 handled now. (These consist of fixS where we have since discovered
878 the value of a symbol, or the address of the frag involved.)
879 For each one, call md_apply_fix to put the fix into the frag data.
881 Result is a count of how many relocation structs will be needed to
882 handle the remaining fixS's that we couldn't completely handle here.
883 These will be output later by emit_relocations(). */
885 static long
886 fixup_segment (fixS *fixP, segT this_segment)
888 long seg_reloc_count = 0;
889 valueT add_number;
890 fragS *fragP;
891 segT add_symbol_segment = absolute_section;
893 if (fixP != NULL && abs_section_sym == NULL)
894 abs_section_sym = section_symbol (absolute_section);
896 /* If the linker is doing the relaxing, we must not do any fixups.
898 Well, strictly speaking that's not true -- we could do any that
899 are PC-relative and don't cross regions that could change size.
900 And for the i960 we might be able to turn callx/callj into bal
901 anyways in cases where we know the maximum displacement. */
902 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
904 for (; fixP; fixP = fixP->fx_next)
905 if (!fixP->fx_done)
907 if (fixP->fx_addsy == NULL)
909 /* There was no symbol required by this relocation.
910 However, BFD doesn't really handle relocations
911 without symbols well. So fake up a local symbol in
912 the absolute section. */
913 fixP->fx_addsy = abs_section_sym;
915 symbol_mark_used_in_reloc (fixP->fx_addsy);
916 if (fixP->fx_subsy != NULL)
917 symbol_mark_used_in_reloc (fixP->fx_subsy);
918 seg_reloc_count++;
920 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
921 return seg_reloc_count;
924 for (; fixP; fixP = fixP->fx_next)
926 #ifdef DEBUG5
927 fprintf (stderr, "\nprocessing fixup:\n");
928 print_fixup (fixP);
929 #endif
931 fragP = fixP->fx_frag;
932 know (fragP);
933 #ifdef TC_VALIDATE_FIX
934 TC_VALIDATE_FIX (fixP, this_segment, skip);
935 #endif
936 add_number = fixP->fx_offset;
938 if (fixP->fx_addsy != NULL)
939 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
941 if (fixP->fx_subsy != NULL)
943 segT sub_symbol_segment;
944 resolve_symbol_value (fixP->fx_subsy);
945 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
946 if (fixP->fx_addsy != NULL
947 && sub_symbol_segment == add_symbol_segment
948 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
949 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
950 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
952 add_number += S_GET_VALUE (fixP->fx_addsy);
953 add_number -= S_GET_VALUE (fixP->fx_subsy);
954 fixP->fx_offset = add_number;
955 fixP->fx_addsy = NULL;
956 fixP->fx_subsy = NULL;
957 #ifdef TC_M68K
958 /* See the comment below about 68k weirdness. */
959 fixP->fx_pcrel = 0;
960 #endif
962 else if (sub_symbol_segment == absolute_section
963 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
964 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
966 add_number -= S_GET_VALUE (fixP->fx_subsy);
967 fixP->fx_offset = add_number;
968 fixP->fx_subsy = NULL;
970 else if (sub_symbol_segment == this_segment
971 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
972 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
974 add_number -= S_GET_VALUE (fixP->fx_subsy);
975 fixP->fx_offset = (add_number + fixP->fx_dot_value
976 + fixP->fx_frag->fr_address);
978 /* Make it pc-relative. If the back-end code has not
979 selected a pc-relative reloc, cancel the adjustment
980 we do later on all pc-relative relocs. */
981 if (0
982 #ifdef TC_M68K
983 /* Do this for m68k even if it's already described
984 as pc-relative. On the m68k, an operand of
985 "pc@(foo-.-2)" should address "foo" in a
986 pc-relative mode. */
987 || 1
988 #endif
989 || !fixP->fx_pcrel)
990 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
991 fixP->fx_subsy = NULL;
992 fixP->fx_pcrel = 1;
994 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
996 if (!md_register_arithmetic
997 && (add_symbol_segment == reg_section
998 || sub_symbol_segment == reg_section))
999 as_bad_where (fixP->fx_file, fixP->fx_line,
1000 _("register value used as expression"));
1001 else
1002 as_bad_where (fixP->fx_file, fixP->fx_line,
1003 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1004 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
1005 segment_name (add_symbol_segment),
1006 S_GET_NAME (fixP->fx_subsy),
1007 segment_name (sub_symbol_segment));
1011 if (fixP->fx_addsy)
1013 if (add_symbol_segment == this_segment
1014 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1015 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1017 /* This fixup was made when the symbol's segment was
1018 SEG_UNKNOWN, but it is now in the local segment.
1019 So we know how to do the address without relocation. */
1020 add_number += S_GET_VALUE (fixP->fx_addsy);
1021 fixP->fx_offset = add_number;
1022 if (fixP->fx_pcrel)
1023 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1024 fixP->fx_addsy = NULL;
1025 fixP->fx_pcrel = 0;
1027 else if (add_symbol_segment == absolute_section
1028 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1029 && !TC_FORCE_RELOCATION_ABS (fixP))
1031 add_number += S_GET_VALUE (fixP->fx_addsy);
1032 fixP->fx_offset = add_number;
1033 fixP->fx_addsy = NULL;
1035 else if (add_symbol_segment != undefined_section
1036 && ! bfd_is_com_section (add_symbol_segment)
1037 && MD_APPLY_SYM_VALUE (fixP))
1038 add_number += S_GET_VALUE (fixP->fx_addsy);
1041 if (fixP->fx_pcrel)
1043 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1044 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1046 /* There was no symbol required by this relocation.
1047 However, BFD doesn't really handle relocations
1048 without symbols well. So fake up a local symbol in
1049 the absolute section. */
1050 fixP->fx_addsy = abs_section_sym;
1054 if (!fixP->fx_done)
1055 md_apply_fix (fixP, &add_number, this_segment);
1057 if (!fixP->fx_done)
1059 ++seg_reloc_count;
1060 if (fixP->fx_addsy == NULL)
1061 fixP->fx_addsy = abs_section_sym;
1062 symbol_mark_used_in_reloc (fixP->fx_addsy);
1063 if (fixP->fx_subsy != NULL)
1064 symbol_mark_used_in_reloc (fixP->fx_subsy);
1067 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1069 if (fixP->fx_size < sizeof (valueT))
1071 valueT mask;
1073 mask = 0;
1074 mask--; /* Set all bits to one. */
1075 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1076 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1078 char buf[50], buf2[50];
1079 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1080 if (add_number > 1000)
1081 sprint_value (buf2, add_number);
1082 else
1083 sprintf (buf2, "%ld", (long) add_number);
1084 as_bad_where (fixP->fx_file, fixP->fx_line,
1085 _("value of %s too large for field of %d bytes at %s"),
1086 buf2, fixP->fx_size, buf);
1087 } /* Generic error checking. */
1089 #ifdef WARN_SIGNED_OVERFLOW_WORD
1090 /* Warn if a .word value is too large when treated as a signed
1091 number. We already know it is not too negative. This is to
1092 catch over-large switches generated by gcc on the 68k. */
1093 if (!flag_signed_overflow_ok
1094 && fixP->fx_size == 2
1095 && add_number > 0x7fff)
1096 as_bad_where (fixP->fx_file, fixP->fx_line,
1097 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1098 (long) add_number,
1099 (long) (fragP->fr_address + fixP->fx_where));
1100 #endif
1101 } /* Not a bit fix. */
1103 #ifdef TC_VALIDATE_FIX
1104 skip: ATTRIBUTE_UNUSED_LABEL
1106 #endif
1107 #ifdef DEBUG5
1108 fprintf (stderr, "result:\n");
1109 print_fixup (fixP);
1110 #endif
1111 } /* For each fixS in this segment. */
1113 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1114 return seg_reloc_count;
1117 static void
1118 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1119 asection *sec,
1120 void *xxx ATTRIBUTE_UNUSED)
1122 segment_info_type *seginfo = seg_info (sec);
1124 fixup_segment (seginfo->fix_root, sec);
1127 static void
1128 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1129 char *file, unsigned int line)
1131 char *err;
1132 bfd_reloc_status_type s;
1133 asymbol *sym;
1135 if (reloc->sym_ptr_ptr != NULL
1136 && (sym = *reloc->sym_ptr_ptr) != NULL
1137 && (sym->flags & BSF_KEEP) == 0
1138 && ((sym->flags & BSF_SECTION_SYM) == 0
1139 || (EMIT_SECTION_SYMBOLS
1140 && !bfd_is_abs_section (sym->section))))
1141 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1143 s = bfd_install_relocation (stdoutput, reloc,
1144 fragp->fr_literal, fragp->fr_address,
1145 sec, &err);
1146 switch (s)
1148 case bfd_reloc_ok:
1149 break;
1150 case bfd_reloc_overflow:
1151 as_bad_where (file, line, _("relocation overflow"));
1152 break;
1153 case bfd_reloc_outofrange:
1154 as_bad_where (file, line, _("relocation out of range"));
1155 break;
1156 default:
1157 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1158 file, line, s);
1162 static fragS *
1163 get_frag_for_reloc (fragS *last_frag,
1164 const segment_info_type *seginfo,
1165 const struct reloc_list *r)
1167 fragS *f;
1169 for (f = last_frag; f != NULL; f = f->fr_next)
1170 if (f->fr_address <= r->u.b.r.address
1171 && r->u.b.r.address < f->fr_address + f->fr_fix)
1172 return f;
1174 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1175 if (f->fr_address <= r->u.b.r.address
1176 && r->u.b.r.address < f->fr_address + f->fr_fix)
1177 return f;
1179 as_bad_where (r->file, r->line,
1180 _("reloc not within (fixed part of) section"));
1181 return NULL;
1184 static void
1185 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1187 segment_info_type *seginfo = seg_info (sec);
1188 unsigned int n;
1189 struct reloc_list *my_reloc_list, **rp, *r;
1190 arelent **relocs;
1191 fixS *fixp;
1192 fragS *last_frag;
1194 /* If seginfo is NULL, we did not create this section; don't do
1195 anything with it. */
1196 if (seginfo == NULL)
1197 return;
1199 n = 0;
1200 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1201 if (!fixp->fx_done)
1202 n++;
1204 #ifdef RELOC_EXPANSION_POSSIBLE
1205 n *= MAX_RELOC_EXPANSION;
1206 #endif
1208 /* Extract relocs for this section from reloc_list. */
1209 rp = &reloc_list;
1210 my_reloc_list = NULL;
1211 while ((r = *rp) != NULL)
1213 if (r->u.b.sec == sec)
1215 *rp = r->next;
1216 r->next = my_reloc_list;
1217 my_reloc_list = r;
1218 n++;
1220 else
1221 rp = &r->next;
1224 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
1226 n = 0;
1227 r = my_reloc_list;
1228 last_frag = NULL;
1229 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1231 int fx_size, slack;
1232 offsetT loc;
1233 arelent **reloc;
1234 #ifndef RELOC_EXPANSION_POSSIBLE
1235 arelent *rel;
1237 reloc = &rel;
1238 #endif
1240 if (fixp->fx_done)
1241 continue;
1243 fx_size = fixp->fx_size;
1244 slack = TC_FX_SIZE_SLACK (fixp);
1245 if (slack > 0)
1246 fx_size = fx_size > slack ? fx_size - slack : 0;
1247 loc = fixp->fx_where + fx_size;
1248 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1249 as_bad_where (fixp->fx_file, fixp->fx_line,
1250 _("internal error: fixup not contained within frag"));
1252 #ifndef RELOC_EXPANSION_POSSIBLE
1253 *reloc = tc_gen_reloc (sec, fixp);
1254 #else
1255 reloc = tc_gen_reloc (sec, fixp);
1256 #endif
1258 while (*reloc)
1260 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1262 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1263 if (f != NULL)
1265 last_frag = f;
1266 relocs[n++] = &r->u.b.r;
1267 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1269 r = r->next;
1271 relocs[n++] = *reloc;
1272 install_reloc (sec, *reloc, fixp->fx_frag,
1273 fixp->fx_file, fixp->fx_line);
1274 #ifndef RELOC_EXPANSION_POSSIBLE
1275 break;
1276 #else
1277 reloc++;
1278 #endif
1282 while (r != NULL)
1284 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1285 if (f != NULL)
1287 last_frag = f;
1288 relocs[n++] = &r->u.b.r;
1289 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1291 r = r->next;
1294 #ifdef DEBUG4
1296 unsigned int k, j, nsyms;
1297 asymbol **sympp;
1298 sympp = bfd_get_outsymbols (stdoutput);
1299 nsyms = bfd_get_symcount (stdoutput);
1300 for (k = 0; k < n; k++)
1301 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1303 for (j = 0; j < nsyms; j++)
1304 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1305 break;
1306 if (j == nsyms)
1307 abort ();
1310 #endif
1312 if (n)
1314 flagword flags = bfd_get_section_flags (abfd, sec);
1315 flags |= SEC_RELOC;
1316 bfd_set_section_flags (abfd, sec, flags);
1317 bfd_set_reloc (stdoutput, sec, relocs, n);
1320 #ifdef SET_SECTION_RELOCS
1321 SET_SECTION_RELOCS (sec, relocs, n);
1322 #endif
1324 #ifdef DEBUG3
1326 unsigned int k;
1328 fprintf (stderr, "relocs for sec %s\n", sec->name);
1329 for (k = 0; k < n; k++)
1331 arelent *rel = relocs[k];
1332 asymbol *s = *rel->sym_ptr_ptr;
1333 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1334 k, rel, (unsigned long)rel->address, s->name,
1335 (unsigned long)rel->addend);
1338 #endif
1341 static int
1342 compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
1343 fragS **last_newf, struct obstack *ob)
1345 int out_size;
1346 int total_out_size = 0;
1347 fragS *f = *last_newf;
1348 char *next_out;
1349 int avail_out;
1351 /* Call the compression routine repeatedly until it has finished
1352 processing the frag. */
1353 while (in_size > 0)
1355 /* Reserve all the space available in the current chunk.
1356 If none is available, start a new frag. */
1357 avail_out = obstack_room (ob);
1358 if (avail_out <= 0)
1360 obstack_finish (ob);
1361 f = frag_alloc (ob);
1362 f->fr_type = rs_fill;
1363 (*last_newf)->fr_next = f;
1364 *last_newf = f;
1365 avail_out = obstack_room (ob);
1367 if (avail_out <= 0)
1368 as_fatal (_("can't extend frag"));
1369 next_out = obstack_next_free (ob);
1370 obstack_blank_fast (ob, avail_out);
1371 out_size = compress_data (strm, &contents, &in_size,
1372 &next_out, &avail_out);
1373 if (out_size < 0)
1374 return -1;
1376 f->fr_fix += out_size;
1377 total_out_size += out_size;
1379 /* Return unused space. */
1380 if (avail_out > 0)
1381 obstack_blank_fast (ob, -avail_out);
1384 return total_out_size;
1387 static void
1388 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1390 segment_info_type *seginfo = seg_info (sec);
1391 fragS *f;
1392 fragS *first_newf;
1393 fragS *last_newf;
1394 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1395 bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
1396 bfd_size_type compressed_size;
1397 const char *section_name;
1398 char *compressed_name;
1399 char *header;
1400 struct z_stream_s *strm;
1401 int x;
1402 flagword flags = bfd_get_section_flags (abfd, sec);
1404 if (seginfo == NULL
1405 || sec->size < 32
1406 || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
1407 return;
1409 section_name = bfd_get_section_name (stdoutput, sec);
1410 if (strncmp (section_name, ".debug_", 7) != 0)
1411 return;
1413 strm = compress_init ();
1414 if (strm == NULL)
1415 return;
1417 /* Create a new frag to contain the "ZLIB" header. */
1418 first_newf = frag_alloc (ob);
1419 if (obstack_room (ob) < 12)
1420 first_newf = frag_alloc (ob);
1421 if (obstack_room (ob) < 12)
1422 as_fatal (_("can't extend frag %u chars"), 12);
1423 last_newf = first_newf;
1424 obstack_blank_fast (ob, 12);
1425 last_newf->fr_type = rs_fill;
1426 last_newf->fr_fix = 12;
1427 header = last_newf->fr_literal;
1428 memcpy (header, "ZLIB", 4);
1429 header[11] = uncompressed_size; uncompressed_size >>= 8;
1430 header[10] = uncompressed_size; uncompressed_size >>= 8;
1431 header[9] = uncompressed_size; uncompressed_size >>= 8;
1432 header[8] = uncompressed_size; uncompressed_size >>= 8;
1433 header[7] = uncompressed_size; uncompressed_size >>= 8;
1434 header[6] = uncompressed_size; uncompressed_size >>= 8;
1435 header[5] = uncompressed_size; uncompressed_size >>= 8;
1436 header[4] = uncompressed_size;
1437 compressed_size = 12;
1439 /* Stream the frags through the compression engine, adding new frags
1440 as necessary to accomodate the compressed output. */
1441 for (f = seginfo->frchainP->frch_root;
1443 f = f->fr_next)
1445 offsetT fill_size;
1446 char *fill_literal;
1447 offsetT count;
1448 int out_size;
1450 gas_assert (f->fr_type == rs_fill);
1451 if (f->fr_fix)
1453 out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
1454 &last_newf, ob);
1455 if (out_size < 0)
1456 return;
1457 compressed_size += out_size;
1459 fill_literal = f->fr_literal + f->fr_fix;
1460 fill_size = f->fr_var;
1461 count = f->fr_offset;
1462 gas_assert (count >= 0);
1463 if (fill_size && count)
1465 while (count--)
1467 out_size = compress_frag (strm, fill_literal, (int) fill_size,
1468 &last_newf, ob);
1469 if (out_size < 0)
1470 return;
1471 compressed_size += out_size;
1476 /* Flush the compression state. */
1477 for (;;)
1479 int avail_out;
1480 char *next_out;
1481 int out_size;
1483 /* Reserve all the space available in the current chunk.
1484 If none is available, start a new frag. */
1485 avail_out = obstack_room (ob);
1486 if (avail_out <= 0)
1488 fragS *newf;
1490 obstack_finish (ob);
1491 newf = frag_alloc (ob);
1492 newf->fr_type = rs_fill;
1493 last_newf->fr_next = newf;
1494 last_newf = newf;
1495 avail_out = obstack_room (ob);
1497 if (avail_out <= 0)
1498 as_fatal (_("can't extend frag"));
1499 next_out = obstack_next_free (ob);
1500 obstack_blank_fast (ob, avail_out);
1501 x = compress_finish (strm, &next_out, &avail_out, &out_size);
1502 if (x < 0)
1503 return;
1505 last_newf->fr_fix += out_size;
1506 compressed_size += out_size;
1508 /* Return unused space. */
1509 if (avail_out > 0)
1510 obstack_blank_fast (ob, -avail_out);
1512 if (x == 0)
1513 break;
1516 /* Replace the uncompressed frag list with the compressed frag list. */
1517 seginfo->frchainP->frch_root = first_newf;
1518 seginfo->frchainP->frch_last = last_newf;
1520 /* Update the section size and its name. */
1521 x = bfd_set_section_size (abfd, sec, compressed_size);
1522 gas_assert (x);
1523 compressed_name = (char *) xmalloc (strlen (section_name) + 2);
1524 compressed_name[0] = '.';
1525 compressed_name[1] = 'z';
1526 strcpy (compressed_name + 2, section_name + 1);
1527 bfd_section_name (stdoutput, sec) = compressed_name;
1530 static void
1531 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1532 asection *sec,
1533 void *xxx ATTRIBUTE_UNUSED)
1535 segment_info_type *seginfo = seg_info (sec);
1536 addressT offset = 0;
1537 fragS *f;
1539 /* Write out the frags. */
1540 if (seginfo == NULL
1541 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1542 return;
1544 for (f = seginfo->frchainP->frch_root;
1546 f = f->fr_next)
1548 int x;
1549 addressT fill_size;
1550 char *fill_literal;
1551 offsetT count;
1553 gas_assert (f->fr_type == rs_fill);
1554 if (f->fr_fix)
1556 x = bfd_set_section_contents (stdoutput, sec,
1557 f->fr_literal, (file_ptr) offset,
1558 (bfd_size_type) f->fr_fix);
1559 if (!x)
1560 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1561 bfd_errmsg (bfd_get_error ()));
1562 offset += f->fr_fix;
1564 fill_literal = f->fr_literal + f->fr_fix;
1565 fill_size = f->fr_var;
1566 count = f->fr_offset;
1567 gas_assert (count >= 0);
1568 if (fill_size && count)
1570 char buf[256];
1571 if (fill_size > sizeof (buf))
1573 /* Do it the old way. Can this ever happen? */
1574 while (count--)
1576 x = bfd_set_section_contents (stdoutput, sec,
1577 fill_literal,
1578 (file_ptr) offset,
1579 (bfd_size_type) fill_size);
1580 if (!x)
1581 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1582 bfd_errmsg (bfd_get_error ()));
1583 offset += fill_size;
1586 else
1588 /* Build a buffer full of fill objects and output it as
1589 often as necessary. This saves on the overhead of
1590 potentially lots of bfd_set_section_contents calls. */
1591 int n_per_buf, i;
1592 if (fill_size == 1)
1594 n_per_buf = sizeof (buf);
1595 memset (buf, *fill_literal, n_per_buf);
1597 else
1599 char *bufp;
1600 n_per_buf = sizeof (buf) / fill_size;
1601 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1602 memcpy (bufp, fill_literal, fill_size);
1604 for (; count > 0; count -= n_per_buf)
1606 n_per_buf = n_per_buf > count ? count : n_per_buf;
1607 x = bfd_set_section_contents
1608 (stdoutput, sec, buf, (file_ptr) offset,
1609 (bfd_size_type) n_per_buf * fill_size);
1610 if (!x)
1611 as_fatal (_("cannot write to output file '%s': %s"),
1612 stdoutput->filename,
1613 bfd_errmsg (bfd_get_error ()));
1614 offset += n_per_buf * fill_size;
1621 static void
1622 merge_data_into_text (void)
1624 seg_info (text_section)->frchainP->frch_last->fr_next =
1625 seg_info (data_section)->frchainP->frch_root;
1626 seg_info (text_section)->frchainP->frch_last =
1627 seg_info (data_section)->frchainP->frch_last;
1628 seg_info (data_section)->frchainP = 0;
1631 static void
1632 set_symtab (void)
1634 int nsyms;
1635 asymbol **asympp;
1636 symbolS *symp;
1637 bfd_boolean result;
1639 /* Count symbols. We can't rely on a count made by the loop in
1640 write_object_file, because *_frob_file may add a new symbol or
1641 two. */
1642 nsyms = 0;
1643 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1644 nsyms++;
1646 if (nsyms)
1648 int i;
1649 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1651 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1652 symp = symbol_rootP;
1653 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1655 asympp[i] = symbol_get_bfdsym (symp);
1656 if (asympp[i]->flags != BSF_SECTION_SYM
1657 || !(bfd_is_const_section (asympp[i]->section)
1658 && asympp[i]->section->symbol == asympp[i]))
1659 asympp[i]->flags |= BSF_KEEP;
1660 symbol_mark_written (symp);
1663 else
1664 asympp = 0;
1665 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1666 gas_assert (result);
1667 symbol_table_frozen = 1;
1670 /* Finish the subsegments. After every sub-segment, we fake an
1671 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1672 ".fill 0" because that is the kind of frag that requires least
1673 thought. ".align" frags like to have a following frag since that
1674 makes calculating their intended length trivial. */
1676 #ifndef SUB_SEGMENT_ALIGN
1677 #ifdef HANDLE_ALIGN
1678 /* The last subsegment gets an alignment corresponding to the alignment
1679 of the section. This allows proper nop-filling at the end of
1680 code-bearing sections. */
1681 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1682 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1683 #else
1684 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1685 #endif
1686 #endif
1688 void
1689 subsegs_finish (void)
1691 struct frchain *frchainP;
1692 asection *s;
1694 for (s = stdoutput->sections; s; s = s->next)
1696 segment_info_type *seginfo = seg_info (s);
1697 if (!seginfo)
1698 continue;
1700 for (frchainP = seginfo->frchainP;
1701 frchainP != NULL;
1702 frchainP = frchainP->frch_next)
1704 int alignment = 0;
1706 subseg_set (s, frchainP->frch_subseg);
1708 /* This now gets called even if we had errors. In that case,
1709 any alignment is meaningless, and, moreover, will look weird
1710 if we are generating a listing. */
1711 if (!had_errors ())
1713 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1714 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1715 && now_seg->entsize)
1717 unsigned int entsize = now_seg->entsize;
1718 int entalign = 0;
1720 while ((entsize & 1) == 0)
1722 ++entalign;
1723 entsize >>= 1;
1725 if (entalign > alignment)
1726 alignment = entalign;
1730 if (subseg_text_p (now_seg))
1731 frag_align_code (alignment, 0);
1732 else
1733 frag_align (alignment, 0, 0);
1735 /* frag_align will have left a new frag.
1736 Use this last frag for an empty ".fill".
1738 For this segment ...
1739 Create a last frag. Do not leave a "being filled in frag". */
1740 frag_wane (frag_now);
1741 frag_now->fr_fix = 0;
1742 know (frag_now->fr_next == NULL);
1747 /* Write the object file. */
1749 void
1750 write_object_file (void)
1752 struct relax_seg_info rsi;
1753 #ifndef WORKING_DOT_WORD
1754 fragS *fragP; /* Track along all frags. */
1755 #endif
1757 /* Do we really want to write it? */
1759 int n_warns, n_errs;
1760 n_warns = had_warnings ();
1761 n_errs = had_errors ();
1762 /* The -Z flag indicates that an object file should be generated,
1763 regardless of warnings and errors. */
1764 if (flag_always_generate_output)
1766 if (n_warns || n_errs)
1767 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1768 n_errs, n_errs == 1 ? "" : "s",
1769 n_warns, n_warns == 1 ? "" : "s");
1771 else
1773 if (n_errs)
1774 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1775 n_errs, n_errs == 1 ? "" : "s",
1776 n_warns, n_warns == 1 ? "" : "s");
1780 /* From now on, we don't care about sub-segments. Build one frag chain
1781 for each segment. Linked thru fr_next. */
1783 /* Remove the sections created by gas for its own purposes. */
1785 int i;
1787 bfd_section_list_remove (stdoutput, reg_section);
1788 bfd_section_list_remove (stdoutput, expr_section);
1789 stdoutput->section_count -= 2;
1790 i = 0;
1791 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1794 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1796 /* We have two segments. If user gave -R flag, then we must put the
1797 data frags into the text segment. Do this before relaxing so
1798 we know to take advantage of -R and make shorter addresses. */
1799 if (flag_readonly_data_in_text)
1801 merge_data_into_text ();
1804 rsi.pass = 0;
1805 while (1)
1807 #ifndef WORKING_DOT_WORD
1808 /* We need to reset the markers in the broken word list and
1809 associated frags between calls to relax_segment (via
1810 relax_seg). Since the broken word list is global, we do it
1811 once per round, rather than locally in relax_segment for each
1812 segment. */
1813 struct broken_word *brokp;
1815 for (brokp = broken_words;
1816 brokp != (struct broken_word *) NULL;
1817 brokp = brokp->next_broken_word)
1819 brokp->added = 0;
1821 if (brokp->dispfrag != (fragS *) NULL
1822 && brokp->dispfrag->fr_type == rs_broken_word)
1823 brokp->dispfrag->fr_subtype = 0;
1825 #endif
1827 rsi.changed = 0;
1828 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1829 rsi.pass++;
1830 if (!rsi.changed)
1831 break;
1834 /* Note - Most ports will use the default value of
1835 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1836 local symbols to be resolved, removing their frag information.
1837 Some ports however, will not have finished relaxing all of
1838 their frags and will still need the local symbol frag
1839 information. These ports can set
1840 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1841 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1843 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1845 /* Relaxation has completed. Freeze all syms. */
1846 finalize_syms = 1;
1848 #ifdef md_post_relax_hook
1849 md_post_relax_hook;
1850 #endif
1852 #ifndef WORKING_DOT_WORD
1854 struct broken_word *lie;
1855 struct broken_word **prevP;
1857 prevP = &broken_words;
1858 for (lie = broken_words; lie; lie = lie->next_broken_word)
1859 if (!lie->added)
1861 expressionS exp;
1863 subseg_change (lie->seg, lie->subseg);
1864 exp.X_op = O_subtract;
1865 exp.X_add_symbol = lie->add;
1866 exp.X_op_symbol = lie->sub;
1867 exp.X_add_number = lie->addnum;
1868 #ifdef TC_CONS_FIX_NEW
1869 TC_CONS_FIX_NEW (lie->frag,
1870 lie->word_goes_here - lie->frag->fr_literal,
1871 2, &exp);
1872 #else
1873 fix_new_exp (lie->frag,
1874 lie->word_goes_here - lie->frag->fr_literal,
1875 2, &exp, 0, BFD_RELOC_16);
1876 #endif
1877 *prevP = lie->next_broken_word;
1879 else
1880 prevP = &(lie->next_broken_word);
1882 for (lie = broken_words; lie;)
1884 struct broken_word *untruth;
1885 char *table_ptr;
1886 addressT table_addr;
1887 addressT from_addr, to_addr;
1888 int n, m;
1890 subseg_change (lie->seg, lie->subseg);
1891 fragP = lie->dispfrag;
1893 /* Find out how many broken_words go here. */
1894 n = 0;
1895 for (untruth = lie;
1896 untruth && untruth->dispfrag == fragP;
1897 untruth = untruth->next_broken_word)
1898 if (untruth->added == 1)
1899 n++;
1901 table_ptr = lie->dispfrag->fr_opcode;
1902 table_addr = (lie->dispfrag->fr_address
1903 + (table_ptr - lie->dispfrag->fr_literal));
1904 /* Create the jump around the long jumps. This is a short
1905 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1906 from_addr = table_addr;
1907 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1908 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1909 lie->add);
1910 table_ptr += md_short_jump_size;
1911 table_addr += md_short_jump_size;
1913 for (m = 0;
1914 lie && lie->dispfrag == fragP;
1915 m++, lie = lie->next_broken_word)
1917 if (lie->added == 2)
1918 continue;
1919 /* Patch the jump table. */
1920 for (untruth = (struct broken_word *) (fragP->fr_symbol);
1921 untruth && untruth->dispfrag == fragP;
1922 untruth = untruth->next_broken_word)
1924 if (untruth->use_jump == lie)
1926 /* This is the offset from ??? to table_ptr+0.
1927 The target is the same for all users of this
1928 md_long_jump, but the "sub" bases (and hence the
1929 offsets) may be different. */
1930 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
1931 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1932 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
1933 #endif
1934 md_number_to_chars (untruth->word_goes_here, to_word, 2);
1938 /* Install the long jump. */
1939 /* This is a long jump from table_ptr+0 to the final target. */
1940 from_addr = table_addr;
1941 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1942 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1943 lie->add);
1944 table_ptr += md_long_jump_size;
1945 table_addr += md_long_jump_size;
1949 #endif /* not WORKING_DOT_WORD */
1951 /* Resolve symbol values. This needs to be done before processing
1952 the relocations. */
1953 if (symbol_rootP)
1955 symbolS *symp;
1957 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1958 resolve_symbol_value (symp);
1960 resolve_local_symbol_values ();
1961 resolve_reloc_expr_symbols ();
1963 PROGRESS (1);
1965 #ifdef tc_frob_file_before_adjust
1966 tc_frob_file_before_adjust ();
1967 #endif
1968 #ifdef obj_frob_file_before_adjust
1969 obj_frob_file_before_adjust ();
1970 #endif
1972 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1974 #ifdef tc_frob_file_before_fix
1975 tc_frob_file_before_fix ();
1976 #endif
1977 #ifdef obj_frob_file_before_fix
1978 obj_frob_file_before_fix ();
1979 #endif
1981 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1983 /* Set up symbol table, and write it out. */
1984 if (symbol_rootP)
1986 symbolS *symp;
1987 bfd_boolean skip_next_symbol = FALSE;
1989 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1991 int punt = 0;
1992 const char *name;
1994 if (skip_next_symbol)
1996 /* Don't do anything besides moving the value of the
1997 symbol from the GAS value-field to the BFD value-field. */
1998 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1999 skip_next_symbol = FALSE;
2000 continue;
2003 if (symbol_mri_common_p (symp))
2005 if (S_IS_EXTERNAL (symp))
2006 as_bad (_("%s: global symbols not supported in common sections"),
2007 S_GET_NAME (symp));
2008 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2009 continue;
2012 name = S_GET_NAME (symp);
2013 if (name)
2015 const char *name2 =
2016 decode_local_label_name ((char *) S_GET_NAME (symp));
2017 /* They only differ if `name' is a fb or dollar local
2018 label name. */
2019 if (name2 != name && ! S_IS_DEFINED (symp))
2020 as_bad (_("local label `%s' is not defined"), name2);
2023 /* Do it again, because adjust_reloc_syms might introduce
2024 more symbols. They'll probably only be section symbols,
2025 but they'll still need to have the values computed. */
2026 resolve_symbol_value (symp);
2028 /* Skip symbols which were equated to undefined or common
2029 symbols. */
2030 if (symbol_equated_reloc_p (symp)
2031 || S_IS_WEAKREFR (symp))
2033 const char *sname = S_GET_NAME (symp);
2035 if (S_IS_COMMON (symp)
2036 && !TC_FAKE_LABEL (sname)
2037 && !S_IS_WEAKREFR (symp)
2038 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
2040 expressionS *e = symbol_get_value_expression (symp);
2042 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
2043 sname, S_GET_NAME (e->X_add_symbol));
2045 if (S_GET_SEGMENT (symp) == reg_section)
2047 /* Report error only if we know the symbol name. */
2048 if (S_GET_NAME (symp) != reg_section->name)
2049 as_bad (_("can't make global register symbol `%s'"),
2050 sname);
2052 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2053 continue;
2056 #ifdef obj_frob_symbol
2057 obj_frob_symbol (symp, punt);
2058 #endif
2059 #ifdef tc_frob_symbol
2060 if (! punt || symbol_used_in_reloc_p (symp))
2061 tc_frob_symbol (symp, punt);
2062 #endif
2064 /* If we don't want to keep this symbol, splice it out of
2065 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2066 want section symbols. Otherwise, we skip local symbols
2067 and symbols that the frob_symbol macros told us to punt,
2068 but we keep such symbols if they are used in relocs. */
2069 if (symp == abs_section_sym
2070 || (! EMIT_SECTION_SYMBOLS
2071 && symbol_section_p (symp))
2072 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2073 opposites. Sometimes the former checks flags and the
2074 latter examines the name... */
2075 || (!S_IS_EXTERNAL (symp)
2076 && (punt || S_IS_LOCAL (symp) ||
2077 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2078 && ! symbol_used_in_reloc_p (symp)))
2080 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2082 /* After symbol_remove, symbol_next(symp) still returns
2083 the one that came after it in the chain. So we don't
2084 need to do any extra cleanup work here. */
2085 continue;
2088 /* Make sure we really got a value for the symbol. */
2089 if (! symbol_resolved_p (symp))
2091 as_bad (_("can't resolve value for symbol `%s'"),
2092 S_GET_NAME (symp));
2093 symbol_mark_resolved (symp);
2096 /* Set the value into the BFD symbol. Up til now the value
2097 has only been kept in the gas symbolS struct. */
2098 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2100 /* A warning construct is a warning symbol followed by the
2101 symbol warned about. Don't let anything object-format or
2102 target-specific muck with it; it's ready for output. */
2103 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2104 skip_next_symbol = TRUE;
2108 PROGRESS (1);
2110 /* Now do any format-specific adjustments to the symbol table, such
2111 as adding file symbols. */
2112 #ifdef tc_adjust_symtab
2113 tc_adjust_symtab ();
2114 #endif
2115 #ifdef obj_adjust_symtab
2116 obj_adjust_symtab ();
2117 #endif
2119 /* Stop if there is an error. */
2120 if (had_errors ())
2121 return;
2123 /* Now that all the sizes are known, and contents correct, we can
2124 start writing to the file. */
2125 set_symtab ();
2127 /* If *_frob_file changes the symbol value at this point, it is
2128 responsible for moving the changed value into symp->bsym->value
2129 as well. Hopefully all symbol value changing can be done in
2130 *_frob_symbol. */
2131 #ifdef tc_frob_file
2132 tc_frob_file ();
2133 #endif
2134 #ifdef obj_frob_file
2135 obj_frob_file ();
2136 #endif
2137 #ifdef obj_coff_generate_pdata
2138 obj_coff_generate_pdata ();
2139 #endif
2140 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2142 #ifdef tc_frob_file_after_relocs
2143 tc_frob_file_after_relocs ();
2144 #endif
2145 #ifdef obj_frob_file_after_relocs
2146 obj_frob_file_after_relocs ();
2147 #endif
2149 /* Once all relocations have been written, we can compress the
2150 contents of the debug sections. This needs to be done before
2151 we start writing any sections, because it will affect the file
2152 layout, which is fixed once we start writing contents. */
2153 if (flag_compress_debug)
2154 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2156 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2159 #ifdef TC_GENERIC_RELAX_TABLE
2160 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2162 long
2163 relax_frag (segT segment, fragS *fragP, long stretch)
2165 const relax_typeS *this_type;
2166 const relax_typeS *start_type;
2167 relax_substateT next_state;
2168 relax_substateT this_state;
2169 offsetT growth;
2170 offsetT aim;
2171 addressT target;
2172 addressT address;
2173 symbolS *symbolP;
2174 const relax_typeS *table;
2176 target = fragP->fr_offset;
2177 address = fragP->fr_address;
2178 table = TC_GENERIC_RELAX_TABLE;
2179 this_state = fragP->fr_subtype;
2180 start_type = this_type = table + this_state;
2181 symbolP = fragP->fr_symbol;
2183 if (symbolP)
2185 fragS *sym_frag;
2187 sym_frag = symbol_get_frag (symbolP);
2189 #ifndef DIFF_EXPR_OK
2190 know (sym_frag != NULL);
2191 #endif
2192 know (S_GET_SEGMENT (symbolP) != absolute_section
2193 || sym_frag == &zero_address_frag);
2194 target += S_GET_VALUE (symbolP);
2196 /* If SYM_FRAG has yet to be reached on this pass, assume it
2197 will move by STRETCH just as we did, unless there is an
2198 alignment frag between here and SYM_FRAG. An alignment may
2199 well absorb any STRETCH, and we don't want to choose a larger
2200 branch insn by overestimating the needed reach of this
2201 branch. It isn't critical to calculate TARGET exactly; We
2202 know we'll be doing another pass if STRETCH is non-zero. */
2204 if (stretch != 0
2205 && sym_frag->relax_marker != fragP->relax_marker
2206 && S_GET_SEGMENT (symbolP) == segment)
2208 if (stretch < 0
2209 || sym_frag->region == fragP->region)
2210 target += stretch;
2211 /* If we get here we know we have a forward branch. This
2212 relax pass may have stretched previous instructions so
2213 far that omitting STRETCH would make the branch
2214 negative. Don't allow this in case the negative reach is
2215 large enough to require a larger branch instruction. */
2216 else if (target < address)
2217 target = fragP->fr_next->fr_address + stretch;
2221 aim = target - address - fragP->fr_fix;
2222 #ifdef TC_PCREL_ADJUST
2223 /* Currently only the ns32k family needs this. */
2224 aim += TC_PCREL_ADJUST (fragP);
2225 #endif
2227 #ifdef md_prepare_relax_scan
2228 /* Formerly called M68K_AIM_KLUDGE. */
2229 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2230 #endif
2232 if (aim < 0)
2234 /* Look backwards. */
2235 for (next_state = this_type->rlx_more; next_state;)
2236 if (aim >= this_type->rlx_backward)
2237 next_state = 0;
2238 else
2240 /* Grow to next state. */
2241 this_state = next_state;
2242 this_type = table + this_state;
2243 next_state = this_type->rlx_more;
2246 else
2248 /* Look forwards. */
2249 for (next_state = this_type->rlx_more; next_state;)
2250 if (aim <= this_type->rlx_forward)
2251 next_state = 0;
2252 else
2254 /* Grow to next state. */
2255 this_state = next_state;
2256 this_type = table + this_state;
2257 next_state = this_type->rlx_more;
2261 growth = this_type->rlx_length - start_type->rlx_length;
2262 if (growth != 0)
2263 fragP->fr_subtype = this_state;
2264 return growth;
2267 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2269 /* Relax_align. Advance location counter to next address that has 'alignment'
2270 lowest order bits all 0s, return size of adjustment made. */
2271 static relax_addressT
2272 relax_align (register relax_addressT address, /* Address now. */
2273 register int alignment /* Alignment (binary). */)
2275 relax_addressT mask;
2276 relax_addressT new_address;
2278 mask = ~((~0) << alignment);
2279 new_address = (address + mask) & (~mask);
2280 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2281 if (linkrelax)
2282 /* We must provide lots of padding, so the linker can discard it
2283 when needed. The linker will not add extra space, ever. */
2284 new_address += (1 << alignment);
2285 #endif
2286 return (new_address - address);
2289 /* Now we have a segment, not a crowd of sub-segments, we can make
2290 fr_address values.
2292 Relax the frags.
2294 After this, all frags in this segment have addresses that are correct
2295 within the segment. Since segments live in different file addresses,
2296 these frag addresses may not be the same as final object-file
2297 addresses. */
2300 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2302 unsigned long frag_count;
2303 struct frag *fragP;
2304 relax_addressT address;
2305 int region;
2306 int ret;
2308 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2309 subseg_change (segment, 0);
2311 /* For each frag in segment: count and store (a 1st guess of)
2312 fr_address. */
2313 address = 0;
2314 region = 0;
2315 for (frag_count = 0, fragP = segment_frag_root;
2316 fragP;
2317 fragP = fragP->fr_next, frag_count ++)
2319 fragP->region = region;
2320 fragP->relax_marker = 0;
2321 fragP->fr_address = address;
2322 address += fragP->fr_fix;
2324 switch (fragP->fr_type)
2326 case rs_fill:
2327 address += fragP->fr_offset * fragP->fr_var;
2328 break;
2330 case rs_align:
2331 case rs_align_code:
2332 case rs_align_test:
2334 addressT offset = relax_align (address, (int) fragP->fr_offset);
2336 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2337 offset = 0;
2339 if (offset % fragP->fr_var != 0)
2341 as_bad_where (fragP->fr_file, fragP->fr_line,
2342 _("alignment padding (%lu bytes) not a multiple of %ld"),
2343 (unsigned long) offset, (long) fragP->fr_var);
2344 offset -= (offset % fragP->fr_var);
2347 address += offset;
2348 region += 1;
2350 break;
2352 case rs_org:
2353 /* Assume .org is nugatory. It will grow with 1st relax. */
2354 region += 1;
2355 break;
2357 case rs_space:
2358 break;
2360 case rs_machine_dependent:
2361 /* If fr_symbol is an expression, this call to
2362 resolve_symbol_value sets up the correct segment, which will
2363 likely be needed in md_estimate_size_before_relax. */
2364 if (fragP->fr_symbol)
2365 resolve_symbol_value (fragP->fr_symbol);
2367 address += md_estimate_size_before_relax (fragP, segment);
2368 break;
2370 #ifndef WORKING_DOT_WORD
2371 /* Broken words don't concern us yet. */
2372 case rs_broken_word:
2373 break;
2374 #endif
2376 case rs_leb128:
2377 /* Initial guess is always 1; doing otherwise can result in
2378 stable solutions that are larger than the minimum. */
2379 address += fragP->fr_offset = 1;
2380 break;
2382 case rs_cfa:
2383 address += eh_frame_estimate_size_before_relax (fragP);
2384 break;
2386 case rs_dwarf2dbg:
2387 address += dwarf2dbg_estimate_size_before_relax (fragP);
2388 break;
2390 default:
2391 BAD_CASE (fragP->fr_type);
2392 break;
2396 /* Do relax(). */
2398 unsigned long max_iterations;
2400 /* Cumulative address adjustment. */
2401 offsetT stretch;
2403 /* Have we made any adjustment this pass? We can't just test
2404 stretch because one piece of code may have grown and another
2405 shrank. */
2406 int stretched;
2408 /* Most horrible, but gcc may give us some exception data that
2409 is impossible to assemble, of the form
2411 .align 4
2412 .byte 0, 0
2413 .uleb128 end - start
2414 start:
2415 .space 128*128 - 1
2416 .align 4
2417 end:
2419 If the leb128 is two bytes in size, then end-start is 128*128,
2420 which requires a three byte leb128. If the leb128 is three
2421 bytes in size, then end-start is 128*128-1, which requires a
2422 two byte leb128. We work around this dilemma by inserting
2423 an extra 4 bytes of alignment just after the .align. This
2424 works because the data after the align is accessed relative to
2425 the end label.
2427 This counter is used in a tiny state machine to detect
2428 whether a leb128 followed by an align is impossible to
2429 relax. */
2430 int rs_leb128_fudge = 0;
2432 /* We want to prevent going into an infinite loop where one frag grows
2433 depending upon the location of a symbol which is in turn moved by
2434 the growing frag. eg:
2436 foo = .
2437 .org foo+16
2438 foo = .
2440 So we dictate that this algorithm can be at most O2. */
2441 max_iterations = frag_count * frag_count;
2442 /* Check for overflow. */
2443 if (max_iterations < frag_count)
2444 max_iterations = frag_count;
2446 ret = 0;
2449 stretch = 0;
2450 stretched = 0;
2452 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2454 offsetT growth = 0;
2455 addressT was_address;
2456 offsetT offset;
2457 symbolS *symbolP;
2459 fragP->relax_marker ^= 1;
2460 was_address = fragP->fr_address;
2461 address = fragP->fr_address += stretch;
2462 symbolP = fragP->fr_symbol;
2463 offset = fragP->fr_offset;
2465 switch (fragP->fr_type)
2467 case rs_fill: /* .fill never relaxes. */
2468 growth = 0;
2469 break;
2471 #ifndef WORKING_DOT_WORD
2472 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2473 for it I do not want to write it. I do not want to have
2474 anything to do with it. This is not the proper way to
2475 implement this misfeature. */
2476 case rs_broken_word:
2478 struct broken_word *lie;
2479 struct broken_word *untruth;
2481 /* Yes this is ugly (storing the broken_word pointer
2482 in the symbol slot). Still, this whole chunk of
2483 code is ugly, and I don't feel like doing anything
2484 about it. Think of it as stubbornness in action. */
2485 growth = 0;
2486 for (lie = (struct broken_word *) (fragP->fr_symbol);
2487 lie && lie->dispfrag == fragP;
2488 lie = lie->next_broken_word)
2491 if (lie->added)
2492 continue;
2494 offset = (S_GET_VALUE (lie->add)
2495 + lie->addnum
2496 - S_GET_VALUE (lie->sub));
2497 if (offset <= -32768 || offset >= 32767)
2499 if (flag_warn_displacement)
2501 char buf[50];
2502 sprint_value (buf, (addressT) lie->addnum);
2503 as_warn_where (fragP->fr_file, fragP->fr_line,
2504 _(".word %s-%s+%s didn't fit"),
2505 S_GET_NAME (lie->add),
2506 S_GET_NAME (lie->sub),
2507 buf);
2509 if (fragP->fr_subtype == 0)
2511 fragP->fr_subtype++;
2512 growth += md_short_jump_size;
2515 /* Redirect *all* words of this table with the same
2516 target, lest we have to handle the case where the
2517 same target but with a offset that fits on this
2518 round overflows at the next relaxation round. */
2519 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2520 untruth && untruth->dispfrag == lie->dispfrag;
2521 untruth = untruth->next_broken_word)
2522 if ((symbol_get_frag (untruth->add)
2523 == symbol_get_frag (lie->add))
2524 && (S_GET_VALUE (untruth->add)
2525 == S_GET_VALUE (lie->add)))
2527 untruth->added = 2;
2528 untruth->use_jump = lie;
2531 lie->added = 1;
2532 growth += md_long_jump_size;
2536 break;
2537 } /* case rs_broken_word */
2538 #endif
2539 case rs_align:
2540 case rs_align_code:
2541 case rs_align_test:
2543 addressT oldoff, newoff;
2545 oldoff = relax_align (was_address + fragP->fr_fix,
2546 (int) offset);
2547 newoff = relax_align (address + fragP->fr_fix,
2548 (int) offset);
2550 if (fragP->fr_subtype != 0)
2552 if (oldoff > fragP->fr_subtype)
2553 oldoff = 0;
2554 if (newoff > fragP->fr_subtype)
2555 newoff = 0;
2558 growth = newoff - oldoff;
2560 /* If this align happens to follow a leb128 and
2561 we have determined that the leb128 is bouncing
2562 in size, then break the cycle by inserting an
2563 extra alignment. */
2564 if (growth < 0
2565 && (rs_leb128_fudge & 16) != 0
2566 && (rs_leb128_fudge & 15) >= 2)
2568 segment_info_type *seginfo = seg_info (segment);
2569 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2570 struct frag *newf;
2572 newf = frag_alloc (ob);
2573 obstack_blank_fast (ob, fragP->fr_var);
2574 obstack_finish (ob);
2575 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2576 memcpy (newf->fr_literal,
2577 fragP->fr_literal + fragP->fr_fix,
2578 fragP->fr_var);
2579 newf->fr_type = rs_fill;
2580 newf->fr_address = address + fragP->fr_fix + newoff;
2581 newf->fr_fix = 0;
2582 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2583 / fragP->fr_var);
2584 if (newf->fr_offset * newf->fr_var
2585 != (offsetT) 1 << fragP->fr_offset)
2587 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2588 newf->fr_var = 1;
2590 /* Include size of new frag in GROWTH. */
2591 growth += newf->fr_offset * newf->fr_var;
2592 /* Adjust the new frag address for the amount
2593 we'll add when we process the new frag. */
2594 newf->fr_address -= stretch + growth;
2595 newf->relax_marker ^= 1;
2596 fragP->fr_next = newf;
2597 #ifdef DEBUG
2598 as_warn (_("padding added"));
2599 #endif
2602 break;
2604 case rs_org:
2606 addressT target = offset;
2607 addressT after;
2609 if (symbolP)
2611 /* Convert from an actual address to an octet offset
2612 into the section. Here it is assumed that the
2613 section's VMA is zero, and can omit subtracting it
2614 from the symbol's value to get the address offset. */
2615 know (S_GET_SEGMENT (symbolP)->vma == 0);
2616 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2619 know (fragP->fr_next);
2620 after = fragP->fr_next->fr_address + stretch;
2621 growth = target - after;
2622 if (growth < 0)
2624 growth = 0;
2626 /* Don't error on first few frag relax passes.
2627 The symbol might be an expression involving
2628 symbol values from other sections. If those
2629 sections have not yet been processed their
2630 frags will all have zero addresses, so we
2631 will calculate incorrect values for them. The
2632 number of passes we allow before giving an
2633 error is somewhat arbitrary. It should be at
2634 least one, with larger values requiring
2635 increasingly contrived dependencies between
2636 frags to trigger a false error. */
2637 if (pass < 2)
2639 /* Force another pass. */
2640 ret = 1;
2641 break;
2644 /* Growth may be negative, but variable part of frag
2645 cannot have fewer than 0 chars. That is, we can't
2646 .org backwards. */
2647 as_bad_where (fragP->fr_file, fragP->fr_line,
2648 _("attempt to move .org backwards"));
2650 /* We've issued an error message. Change the
2651 frag to avoid cascading errors. */
2652 fragP->fr_type = rs_align;
2653 fragP->fr_subtype = 0;
2654 fragP->fr_offset = 0;
2655 fragP->fr_fix = after - address;
2658 break;
2660 case rs_space:
2661 growth = 0;
2662 if (symbolP)
2664 offsetT amount;
2666 amount = S_GET_VALUE (symbolP);
2667 if (S_GET_SEGMENT (symbolP) != absolute_section
2668 || S_IS_COMMON (symbolP)
2669 || ! S_IS_DEFINED (symbolP))
2671 as_bad_where (fragP->fr_file, fragP->fr_line,
2672 _(".space specifies non-absolute value"));
2673 /* Prevent repeat of this error message. */
2674 fragP->fr_symbol = 0;
2676 else if (amount < 0)
2678 /* Don't error on first few frag relax passes.
2679 See rs_org comment for a longer explanation. */
2680 if (pass < 2)
2682 ret = 1;
2683 break;
2686 as_warn_where (fragP->fr_file, fragP->fr_line,
2687 _(".space or .fill with negative value, ignored"));
2688 fragP->fr_symbol = 0;
2690 else
2691 growth = (was_address + fragP->fr_fix + amount
2692 - fragP->fr_next->fr_address);
2694 break;
2696 case rs_machine_dependent:
2697 #ifdef md_relax_frag
2698 growth = md_relax_frag (segment, fragP, stretch);
2699 #else
2700 #ifdef TC_GENERIC_RELAX_TABLE
2701 /* The default way to relax a frag is to look through
2702 TC_GENERIC_RELAX_TABLE. */
2703 growth = relax_frag (segment, fragP, stretch);
2704 #endif /* TC_GENERIC_RELAX_TABLE */
2705 #endif
2706 break;
2708 case rs_leb128:
2710 valueT value;
2711 offsetT size;
2713 value = resolve_symbol_value (fragP->fr_symbol);
2714 size = sizeof_leb128 (value, fragP->fr_subtype);
2715 growth = size - fragP->fr_offset;
2716 fragP->fr_offset = size;
2718 break;
2720 case rs_cfa:
2721 growth = eh_frame_relax_frag (fragP);
2722 break;
2724 case rs_dwarf2dbg:
2725 growth = dwarf2dbg_relax_frag (fragP);
2726 break;
2728 default:
2729 BAD_CASE (fragP->fr_type);
2730 break;
2732 if (growth)
2734 stretch += growth;
2735 stretched = 1;
2736 if (fragP->fr_type == rs_leb128)
2737 rs_leb128_fudge += 16;
2738 else if (fragP->fr_type == rs_align
2739 && (rs_leb128_fudge & 16) != 0
2740 && stretch == 0)
2741 rs_leb128_fudge += 16;
2742 else
2743 rs_leb128_fudge = 0;
2747 if (stretch == 0
2748 && (rs_leb128_fudge & 16) == 0
2749 && (rs_leb128_fudge & -16) != 0)
2750 rs_leb128_fudge += 1;
2751 else
2752 rs_leb128_fudge = 0;
2754 /* Until nothing further to relax. */
2755 while (stretched && -- max_iterations);
2757 if (stretched)
2758 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2759 segment_name (segment));
2762 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2763 if (fragP->last_fr_address != fragP->fr_address)
2765 fragP->last_fr_address = fragP->fr_address;
2766 ret = 1;
2768 return ret;
2771 void
2772 number_to_chars_bigendian (char *buf, valueT val, int n)
2774 if (n <= 0)
2775 abort ();
2776 while (n--)
2778 buf[n] = val & 0xff;
2779 val >>= 8;
2783 void
2784 number_to_chars_littleendian (char *buf, valueT val, int n)
2786 if (n <= 0)
2787 abort ();
2788 while (n--)
2790 *buf++ = val & 0xff;
2791 val >>= 8;
2795 void
2796 write_print_statistics (FILE *file)
2798 fprintf (file, "fixups: %d\n", n_fixups);
2801 /* For debugging. */
2802 extern int indent_level;
2804 void
2805 print_fixup (fixS *fixp)
2807 indent_level = 1;
2808 fprintf (stderr, "fix ");
2809 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2810 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
2811 if (fixp->fx_pcrel)
2812 fprintf (stderr, " pcrel");
2813 if (fixp->fx_pcrel_adjust)
2814 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2815 if (fixp->fx_im_disp)
2817 #ifdef TC_NS32K
2818 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2819 #else
2820 fprintf (stderr, " im_disp");
2821 #endif
2823 if (fixp->fx_tcbit)
2824 fprintf (stderr, " tcbit");
2825 if (fixp->fx_done)
2826 fprintf (stderr, " done");
2827 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2828 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2829 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2830 (long) fixp->fx_where,
2831 (unsigned long) fixp->fx_offset,
2832 (unsigned long) fixp->fx_addnumber);
2833 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2834 fixp->fx_r_type);
2835 if (fixp->fx_addsy)
2837 fprintf (stderr, "\n +<");
2838 print_symbol_value_1 (stderr, fixp->fx_addsy);
2839 fprintf (stderr, ">");
2841 if (fixp->fx_subsy)
2843 fprintf (stderr, "\n -<");
2844 print_symbol_value_1 (stderr, fixp->fx_subsy);
2845 fprintf (stderr, ">");
2847 fprintf (stderr, "\n");
2848 #ifdef TC_FIX_DATA_PRINT
2849 TC_FIX_DATA_PRINT (stderr, fixp);
2850 #endif