NaCl changes
[nacl-binutils.git] / gas / write.c
blob8607e742f0252e72f647db4e718a2d1296d3bba0
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
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 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"
32 #ifndef TC_ADJUST_RELOC_COUNT
33 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
34 #endif
36 #ifndef TC_FORCE_RELOCATION
37 #define TC_FORCE_RELOCATION(FIX) \
38 (generic_force_reloc (FIX))
39 #endif
41 #ifndef TC_FORCE_RELOCATION_ABS
42 #define TC_FORCE_RELOCATION_ABS(FIX) \
43 (TC_FORCE_RELOCATION (FIX))
44 #endif
46 #ifndef TC_FORCE_RELOCATION_LOCAL
47 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
48 (!(FIX)->fx_pcrel \
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 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
101 fixups that far past the end of a frag. Having such fixups
102 is of course most most likely a bug in setting fx_size correctly.
103 A negative value disables the fixup check entirely, which is
104 appropriate for something like the Renesas / SuperH SH_COUNT
105 reloc. */
106 #ifndef TC_FX_SIZE_SLACK
107 #define TC_FX_SIZE_SLACK(FIX) 0
108 #endif
110 /* Used to control final evaluation of expressions. */
111 int finalize_syms = 0;
113 int symbol_table_frozen;
115 symbolS *abs_section_sym;
117 /* Remember the value of dot when parsing expressions. */
118 addressT dot_value;
120 /* Relocs generated by ".reloc" pseudo. */
121 struct reloc_list* reloc_list;
123 void print_fixup (fixS *);
125 /* We generally attach relocs to frag chains. However, after we have
126 chained these all together into a segment, any relocs we add after
127 that must be attached to a segment. This will include relocs added
128 in md_estimate_size_for_relax, for example. */
129 static int frags_chained = 0;
131 static int n_fixups;
133 #define RELOC_ENUM enum bfd_reloc_code_real
135 /* Create a fixS in obstack 'notes'. */
137 static fixS *
138 fix_new_internal (fragS *frag, /* Which frag? */
139 int where, /* Where in that frag? */
140 int size, /* 1, 2, or 4 usually. */
141 symbolS *add_symbol, /* X_add_symbol. */
142 symbolS *sub_symbol, /* X_op_symbol. */
143 offsetT offset, /* X_add_number. */
144 int pcrel, /* TRUE if PC-relative relocation. */
145 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
147 fixS *fixP;
149 n_fixups++;
151 fixP = obstack_alloc (&notes, sizeof (fixS));
153 frag->nacl_fixup = fixP;
155 fixP->fx_frag = frag;
156 fixP->fx_where = where;
157 fixP->fx_size = size;
158 /* We've made fx_size a narrow field; check that it's wide enough. */
159 if (fixP->fx_size != size)
161 as_bad (_("field fx_size too small to hold %d"), size);
162 abort ();
164 fixP->fx_addsy = add_symbol;
165 fixP->fx_subsy = sub_symbol;
166 fixP->fx_offset = offset;
167 fixP->fx_dot_value = dot_value;
168 fixP->fx_pcrel = pcrel;
169 fixP->fx_r_type = r_type;
170 fixP->fx_im_disp = 0;
171 fixP->fx_pcrel_adjust = 0;
172 fixP->fx_bit_fixP = 0;
173 fixP->fx_addnumber = 0;
174 fixP->fx_tcbit = 0;
175 fixP->fx_tcbit2 = 0;
176 fixP->fx_done = 0;
177 fixP->fx_no_overflow = 0;
178 fixP->fx_signed = 0;
180 #ifdef USING_CGEN
181 fixP->fx_cgen.insn = NULL;
182 fixP->fx_cgen.opinfo = 0;
183 #endif
185 #ifdef TC_FIX_TYPE
186 TC_INIT_FIX_DATA (fixP);
187 #endif
189 as_where (&fixP->fx_file, &fixP->fx_line);
191 /* Usually, we want relocs sorted numerically, but while
192 comparing to older versions of gas that have relocs
193 reverse sorted, it is convenient to have this compile
194 time option. xoxorich. */
197 fixS **seg_fix_rootP = (frags_chained
198 ? &seg_info (now_seg)->fix_root
199 : &frchain_now->fix_root);
200 fixS **seg_fix_tailP = (frags_chained
201 ? &seg_info (now_seg)->fix_tail
202 : &frchain_now->fix_tail);
204 #ifdef REVERSE_SORT_RELOCS
206 fixP->fx_next = *seg_fix_rootP;
207 *seg_fix_rootP = fixP;
209 #else /* REVERSE_SORT_RELOCS */
211 fixP->fx_next = NULL;
213 if (*seg_fix_tailP)
214 (*seg_fix_tailP)->fx_next = fixP;
215 else
216 *seg_fix_rootP = fixP;
217 *seg_fix_tailP = fixP;
219 #endif /* REVERSE_SORT_RELOCS */
222 return fixP;
225 /* Create a fixup relative to a symbol (plus a constant). */
227 fixS *
228 fix_new (fragS *frag, /* Which frag? */
229 int where, /* Where in that frag? */
230 int size, /* 1, 2, or 4 usually. */
231 symbolS *add_symbol, /* X_add_symbol. */
232 offsetT offset, /* X_add_number. */
233 int pcrel, /* TRUE if PC-relative relocation. */
234 RELOC_ENUM r_type /* Relocation type. */)
236 return fix_new_internal (frag, where, size, add_symbol,
237 (symbolS *) NULL, offset, pcrel, r_type);
240 /* Create a fixup for an expression. Currently we only support fixups
241 for difference expressions. That is itself more than most object
242 file formats support anyhow. */
244 fixS *
245 fix_new_exp (fragS *frag, /* Which frag? */
246 int where, /* Where in that frag? */
247 int size, /* 1, 2, or 4 usually. */
248 expressionS *exp, /* Expression. */
249 int pcrel, /* TRUE if PC-relative relocation. */
250 RELOC_ENUM r_type /* Relocation type. */)
252 symbolS *add = NULL;
253 symbolS *sub = NULL;
254 offsetT off = 0;
256 switch (exp->X_op)
258 case O_absent:
259 break;
261 case O_register:
262 as_bad (_("register value used as expression"));
263 break;
265 case O_add:
266 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
267 the difference expression cannot immediately be reduced. */
269 symbolS *stmp = make_expr_symbol (exp);
271 exp->X_op = O_symbol;
272 exp->X_op_symbol = 0;
273 exp->X_add_symbol = stmp;
274 exp->X_add_number = 0;
276 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
279 case O_symbol_rva:
280 add = exp->X_add_symbol;
281 off = exp->X_add_number;
282 r_type = BFD_RELOC_RVA;
283 break;
285 case O_uminus:
286 sub = exp->X_add_symbol;
287 off = exp->X_add_number;
288 break;
290 case O_subtract:
291 sub = exp->X_op_symbol;
292 /* Fall through. */
293 case O_symbol:
294 add = exp->X_add_symbol;
295 /* Fall through. */
296 case O_constant:
297 off = exp->X_add_number;
298 break;
300 default:
301 add = make_expr_symbol (exp);
302 break;
305 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
308 /* Generic function to determine whether a fixup requires a relocation. */
310 generic_force_reloc (fixS *fix)
312 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
313 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
314 return 1;
316 if (fix->fx_addsy == NULL)
317 return 0;
319 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
322 /* Append a string onto another string, bumping the pointer along. */
323 void
324 append (char **charPP, char *fromP, unsigned long length)
326 /* Don't trust memcpy() of 0 chars. */
327 if (length == 0)
328 return;
330 memcpy (*charPP, fromP, length);
331 *charPP += length;
334 /* This routine records the largest alignment seen for each segment.
335 If the beginning of the segment is aligned on the worst-case
336 boundary, all of the other alignments within it will work. At
337 least one object format really uses this info. */
339 void
340 record_alignment (/* Segment to which alignment pertains. */
341 segT seg,
342 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
343 boundary, 2 => 4-byte boundary, etc.) */
344 int align)
346 if (seg == absolute_section)
347 return;
349 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
350 bfd_set_section_alignment (stdoutput, seg, align);
354 get_recorded_alignment (segT seg)
356 if (seg == absolute_section)
357 return 0;
359 return bfd_get_section_alignment (stdoutput, seg);
362 /* Reset the section indices after removing the gas created sections. */
364 static void
365 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
367 int *countp = (int *) countparg;
369 sec->index = *countp;
370 ++*countp;
373 static fragS *
374 chain_frchains_together_1 (segT section, struct frchain *frchp)
376 fragS dummy, *prev_frag = &dummy;
377 fixS fix_dummy, *prev_fix = &fix_dummy;
379 for (; frchp; frchp = frchp->frch_next)
381 prev_frag->fr_next = frchp->frch_root;
382 prev_frag = frchp->frch_last;
383 assert (prev_frag->fr_type != 0);
384 if (frchp->fix_root != (fixS *) NULL)
386 if (seg_info (section)->fix_root == (fixS *) NULL)
387 seg_info (section)->fix_root = frchp->fix_root;
388 prev_fix->fx_next = frchp->fix_root;
389 seg_info (section)->fix_tail = frchp->fix_tail;
390 prev_fix = frchp->fix_tail;
393 assert (prev_frag->fr_type != 0);
394 assert (prev_frag != &dummy);
395 prev_frag->fr_next = 0;
396 return prev_frag;
399 static void
400 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
401 segT section,
402 void *xxx ATTRIBUTE_UNUSED)
404 segment_info_type *info;
406 /* BFD may have introduced its own sections without using
407 subseg_new, so it is possible that seg_info is NULL. */
408 info = seg_info (section);
409 if (info != (segment_info_type *) NULL)
410 info->frchainP->frch_last
411 = chain_frchains_together_1 (section, info->frchainP);
413 /* Now that we've chained the frags together, we must add new fixups
414 to the segment, not to the frag chain. */
415 frags_chained = 1;
418 static void
419 move_call_insn_to_end(fragS *fragP, fragS *next ATTRIBUTE_UNUSED)
421 if (fragP->fr_offset != 0) {
422 // fragP->fr_fix is the start of the fixup code (i.e. nops).
423 int i;
424 unsigned char *tmp = alloca(fragP->fr_fix);
425 memcpy(tmp, fragP->fr_literal, fragP->fr_fix);
426 for (i = 0; i < fragP->fr_var; i++) {
427 fragP->fr_literal[i] = fragP->fr_literal[fragP->fr_fix+i];
429 for (i = 0; i< fragP->fr_fix; i++) {
430 fragP->fr_literal[fragP->fr_var+i] = tmp[i];
432 // TODO: this code should be obsolete. Remove it.
433 // If it was a direct call, there's a fixup for the target address.
434 // This needs to corrected to point to the new location of the
435 // constant after we moved the nops.
436 // If there is no fixup, but this is a call, then it is an indirect
437 // call, and we need to put in the fixups for the sandbox code.
438 if (fragP->nacl_fixup) {
439 fragP->nacl_fixup->fx_where += fragP->fr_var;
441 else if (getenv("NACL_CONTROL_ENFORCE_RANGE")) {
442 symbolS* and_mask = symbol_find_or_make("__nacl_and_mask");
443 symbolS* exec_start = symbol_find_or_make("__executable_start");
444 fix_new (fragP, 2+fragP->fr_var, 4, and_mask, 0, 0, BFD_RELOC_32);
445 fix_new (fragP, 8+fragP->fr_var, 4, exec_start, 0, 0, BFD_RELOC_32);
450 static void
451 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
453 switch (fragP->fr_type)
455 case rs_align:
456 case rs_align_code:
457 case rs_align_test:
458 case rs_org:
459 case rs_space:
460 #ifdef HANDLE_ALIGN
461 HANDLE_ALIGN (fragP);
462 #endif
463 know (fragP->fr_next != NULL);
464 fragP->fr_offset = (fragP->fr_next->fr_address
465 - fragP->fr_address
466 - fragP->fr_fix) / fragP->fr_var;
467 if (fragP->fr_offset < 0)
469 as_bad_where (fragP->fr_file, fragP->fr_line,
470 _("attempt to .org/.space backwards? (%ld)"),
471 (long) fragP->fr_offset);
472 fragP->fr_offset = 0;
475 if (fragP->is_call && (nacl_alignment > 0)) {
476 move_call_insn_to_end(fragP, NULL);
478 fragP->fr_type = rs_fill;
479 break;
481 case rs_fill:
482 break;
484 case rs_leb128:
486 valueT value = S_GET_VALUE (fragP->fr_symbol);
487 int size;
489 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
490 fragP->fr_subtype);
492 fragP->fr_fix += size;
493 fragP->fr_type = rs_fill;
494 fragP->fr_var = 0;
495 fragP->fr_offset = 0;
496 fragP->fr_symbol = NULL;
498 break;
500 case rs_cfa:
501 eh_frame_convert_frag (fragP);
502 break;
504 case rs_dwarf2dbg:
505 dwarf2dbg_convert_frag (fragP);
506 break;
508 case rs_machine_dependent:
509 md_convert_frag (stdoutput, sec, fragP);
511 assert (fragP->fr_next == NULL
512 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
513 == fragP->fr_fix));
515 /* After md_convert_frag, we make the frag into a ".space 0".
516 md_convert_frag() should set up any fixSs and constants
517 required. */
518 frag_wane (fragP);
519 break;
521 #ifndef WORKING_DOT_WORD
522 case rs_broken_word:
524 struct broken_word *lie;
526 if (fragP->fr_subtype)
528 fragP->fr_fix += md_short_jump_size;
529 for (lie = (struct broken_word *) (fragP->fr_symbol);
530 lie && lie->dispfrag == fragP;
531 lie = lie->next_broken_word)
532 if (lie->added == 1)
533 fragP->fr_fix += md_long_jump_size;
535 frag_wane (fragP);
537 break;
538 #endif
540 default:
541 BAD_CASE (fragP->fr_type);
542 break;
544 #ifdef md_frag_check
545 md_frag_check (fragP);
546 #endif
549 struct relax_seg_info
551 int pass;
552 int changed;
555 static void
556 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
558 segment_info_type *seginfo = seg_info (sec);
559 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
561 if (seginfo && seginfo->frchainP
562 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
563 info->changed = 1;
566 static void
567 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
569 flagword flags;
570 fragS *fragp;
571 segment_info_type *seginfo;
572 int x;
573 valueT size, newsize;
575 subseg_change (sec, 0);
577 seginfo = seg_info (sec);
578 if (seginfo && seginfo->frchainP)
580 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
581 cvt_frag_to_fill (sec, fragp);
582 for (fragp = seginfo->frchainP->frch_root;
583 fragp->fr_next;
584 fragp = fragp->fr_next)
585 /* Walk to last elt. */
587 size = fragp->fr_address + fragp->fr_fix;
589 else
590 size = 0;
592 flags = bfd_get_section_flags (abfd, sec);
594 if (size > 0 && ! seginfo->bss)
595 flags |= SEC_HAS_CONTENTS;
597 flags &= ~SEC_RELOC;
598 x = bfd_set_section_flags (abfd, sec, flags);
599 assert (x);
601 newsize = md_section_align (sec, size);
602 x = bfd_set_section_size (abfd, sec, newsize);
603 assert (x);
605 /* If the size had to be rounded up, add some padding in the last
606 non-empty frag. */
607 assert (newsize >= size);
608 if (size != newsize)
610 fragS *last = seginfo->frchainP->frch_last;
611 fragp = seginfo->frchainP->frch_root;
612 while (fragp->fr_next != last)
613 fragp = fragp->fr_next;
614 last->fr_address = size;
615 if ((newsize - size) % fragp->fr_var == 0)
616 fragp->fr_offset += (newsize - size) / fragp->fr_var;
617 else
618 /* If we hit this abort, it's likely due to subsegs_finish not
619 providing sufficient alignment on the last frag, and the
620 machine dependent code using alignment frags with fr_var
621 greater than 1. */
622 abort ();
625 #ifdef tc_frob_section
626 tc_frob_section (sec);
627 #endif
628 #ifdef obj_frob_section
629 obj_frob_section (sec);
630 #endif
633 #ifdef DEBUG2
634 static void
635 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
637 segment_info_type *seginfo = seg_info (sec);
638 fixS *fixp = seginfo->fix_root;
640 if (!fixp)
641 return;
643 fprintf (stream, "sec %s relocs:\n", sec->name);
644 while (fixp)
646 symbolS *s = fixp->fx_addsy;
648 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
649 (int) fixp->fx_r_type);
650 if (s == NULL)
651 fprintf (stream, "no sym\n");
652 else
654 print_symbol_value_1 (stream, s);
655 fprintf (stream, "\n");
657 fixp = fixp->fx_next;
660 #else
661 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
662 #endif
664 #ifndef EMIT_SECTION_SYMBOLS
665 #define EMIT_SECTION_SYMBOLS 1
666 #endif
668 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
669 and check for validity. Convert RELOC_LIST from using U.A fields
670 to U.B fields. */
671 static void
672 resolve_reloc_expr_symbols (void)
674 struct reloc_list *r;
676 for (r = reloc_list; r; r = r->next)
678 expressionS *symval;
679 symbolS *sym;
680 bfd_vma offset, addend;
681 asection *sec;
682 reloc_howto_type *howto;
684 resolve_symbol_value (r->u.a.offset_sym);
685 symval = symbol_get_value_expression (r->u.a.offset_sym);
687 offset = 0;
688 sym = NULL;
689 if (symval->X_op == O_constant)
690 sym = r->u.a.offset_sym;
691 else if (symval->X_op == O_symbol)
693 sym = symval->X_add_symbol;
694 offset = symval->X_add_number;
695 symval = symbol_get_value_expression (symval->X_add_symbol);
697 if (sym == NULL
698 || symval->X_op != O_constant
699 || (sec = S_GET_SEGMENT (sym)) == NULL
700 || !SEG_NORMAL (sec))
702 as_bad_where (r->file, r->line, _("invalid offset expression"));
703 sec = NULL;
705 else
706 offset += S_GET_VALUE (sym);
708 sym = NULL;
709 addend = r->u.a.addend;
710 if (r->u.a.sym != NULL)
712 resolve_symbol_value (r->u.a.sym);
713 symval = symbol_get_value_expression (r->u.a.sym);
714 if (symval->X_op == O_constant)
715 sym = r->u.a.sym;
716 else if (symval->X_op == O_symbol)
718 sym = symval->X_add_symbol;
719 addend += symval->X_add_number;
720 symval = symbol_get_value_expression (symval->X_add_symbol);
722 if (symval->X_op != O_constant)
724 as_bad_where (r->file, r->line, _("invalid reloc expression"));
725 sec = NULL;
727 else if (sym != NULL)
728 symbol_mark_used_in_reloc (sym);
730 if (sym == NULL)
732 if (abs_section_sym == NULL)
733 abs_section_sym = section_symbol (absolute_section);
734 sym = abs_section_sym;
737 howto = r->u.a.howto;
739 r->u.b.sec = sec;
740 r->u.b.s = symbol_get_bfdsym (sym);
741 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
742 r->u.b.r.address = offset;
743 r->u.b.r.addend = addend;
744 r->u.b.r.howto = howto;
748 /* This pass over fixups decides whether symbols can be replaced with
749 section symbols. */
751 static void
752 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
753 asection *sec,
754 void *xxx ATTRIBUTE_UNUSED)
756 segment_info_type *seginfo = seg_info (sec);
757 fixS *fixp;
759 if (seginfo == NULL)
760 return;
762 dump_section_relocs (abfd, sec, stderr);
764 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
765 if (fixp->fx_done)
766 /* Ignore it. */
768 else if (fixp->fx_addsy)
770 symbolS *sym;
771 asection *symsec;
773 #ifdef DEBUG5
774 fprintf (stderr, "\n\nadjusting fixup:\n");
775 print_fixup (fixp);
776 #endif
778 sym = fixp->fx_addsy;
780 /* All symbols should have already been resolved at this
781 point. It is possible to see unresolved expression
782 symbols, though, since they are not in the regular symbol
783 table. */
784 resolve_symbol_value (sym);
786 if (fixp->fx_subsy != NULL)
787 resolve_symbol_value (fixp->fx_subsy);
789 /* If this symbol is equated to an undefined or common symbol,
790 convert the fixup to being against that symbol. */
791 while (symbol_equated_reloc_p (sym)
792 || S_IS_WEAKREFR (sym))
794 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
795 if (sym == newsym)
796 break;
797 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
798 fixp->fx_addsy = newsym;
799 sym = newsym;
802 if (symbol_mri_common_p (sym))
804 fixp->fx_offset += S_GET_VALUE (sym);
805 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
806 continue;
809 /* If the symbol is undefined, common, weak, or global (ELF
810 shared libs), we can't replace it with the section symbol. */
811 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
812 continue;
814 /* Is there some other (target cpu dependent) reason we can't adjust
815 this one? (E.g. relocations involving function addresses on
816 the PA. */
817 #ifdef tc_fix_adjustable
818 if (! tc_fix_adjustable (fixp))
819 continue;
820 #endif
822 /* Since we're reducing to section symbols, don't attempt to reduce
823 anything that's already using one. */
824 if (symbol_section_p (sym))
825 continue;
827 symsec = S_GET_SEGMENT (sym);
828 if (symsec == NULL)
829 abort ();
831 if (bfd_is_abs_section (symsec))
833 /* The fixup_segment routine normally will not use this
834 symbol in a relocation. */
835 continue;
838 /* Don't try to reduce relocs which refer to non-local symbols
839 in .linkonce sections. It can lead to confusion when a
840 debugging section refers to a .linkonce section. I hope
841 this will always be correct. */
842 if (symsec != sec && ! S_IS_LOCAL (sym))
844 if ((symsec->flags & SEC_LINK_ONCE) != 0
845 || (IS_ELF
846 /* The GNU toolchain uses an extension for ELF: a
847 section beginning with the magic string
848 .gnu.linkonce is a linkonce section. */
849 && strncmp (segment_name (symsec), ".gnu.linkonce",
850 sizeof ".gnu.linkonce" - 1) == 0))
851 continue;
854 /* Never adjust a reloc against local symbol in a merge section
855 with non-zero addend. */
856 if ((symsec->flags & SEC_MERGE) != 0
857 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
858 continue;
860 /* Never adjust a reloc against TLS local symbol. */
861 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
862 continue;
864 /* We refetch the segment when calling section_symbol, rather
865 than using symsec, because S_GET_VALUE may wind up changing
866 the section when it calls resolve_symbol_value. */
867 fixp->fx_offset += S_GET_VALUE (sym);
868 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
869 #ifdef DEBUG5
870 fprintf (stderr, "\nadjusted fixup:\n");
871 print_fixup (fixp);
872 #endif
875 dump_section_relocs (abfd, sec, stderr);
878 /* fixup_segment()
880 Go through all the fixS's in a segment and see which ones can be
881 handled now. (These consist of fixS where we have since discovered
882 the value of a symbol, or the address of the frag involved.)
883 For each one, call md_apply_fix to put the fix into the frag data.
885 Result is a count of how many relocation structs will be needed to
886 handle the remaining fixS's that we couldn't completely handle here.
887 These will be output later by emit_relocations(). */
889 static long
890 fixup_segment (fixS *fixP, segT this_segment)
892 long seg_reloc_count = 0;
893 valueT add_number;
894 fragS *fragP;
895 segT add_symbol_segment = absolute_section;
897 if (fixP != NULL && abs_section_sym == NULL)
898 abs_section_sym = section_symbol (absolute_section);
900 /* If the linker is doing the relaxing, we must not do any fixups.
902 Well, strictly speaking that's not true -- we could do any that
903 are PC-relative and don't cross regions that could change size.
904 And for the i960 we might be able to turn callx/callj into bal
905 anyways in cases where we know the maximum displacement. */
906 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
908 for (; fixP; fixP = fixP->fx_next)
909 if (!fixP->fx_done)
911 if (fixP->fx_addsy == NULL)
913 /* There was no symbol required by this relocation.
914 However, BFD doesn't really handle relocations
915 without symbols well. So fake up a local symbol in
916 the absolute section. */
917 fixP->fx_addsy = abs_section_sym;
919 symbol_mark_used_in_reloc (fixP->fx_addsy);
920 if (fixP->fx_subsy != NULL)
921 symbol_mark_used_in_reloc (fixP->fx_subsy);
922 seg_reloc_count++;
924 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
925 return seg_reloc_count;
928 for (; fixP; fixP = fixP->fx_next)
930 #ifdef DEBUG5
931 fprintf (stderr, "\nprocessing fixup:\n");
932 print_fixup (fixP);
933 #endif
935 fragP = fixP->fx_frag;
936 know (fragP);
937 #ifdef TC_VALIDATE_FIX
938 TC_VALIDATE_FIX (fixP, this_segment, skip);
939 #endif
940 add_number = fixP->fx_offset;
942 if (fixP->fx_addsy != NULL)
943 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
945 if (fixP->fx_subsy != NULL)
947 segT sub_symbol_segment;
948 resolve_symbol_value (fixP->fx_subsy);
949 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
950 if (fixP->fx_addsy != NULL
951 && sub_symbol_segment == add_symbol_segment
952 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
954 add_number += S_GET_VALUE (fixP->fx_addsy);
955 add_number -= S_GET_VALUE (fixP->fx_subsy);
956 fixP->fx_offset = add_number;
957 fixP->fx_addsy = NULL;
958 fixP->fx_subsy = NULL;
959 #ifdef TC_M68K
960 /* See the comment below about 68k weirdness. */
961 fixP->fx_pcrel = 0;
962 #endif
964 else if (sub_symbol_segment == absolute_section
965 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
967 add_number -= S_GET_VALUE (fixP->fx_subsy);
968 fixP->fx_offset = add_number;
969 fixP->fx_subsy = NULL;
971 else if (sub_symbol_segment == this_segment
972 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
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))
996 as_bad_where (fixP->fx_file, fixP->fx_line,
997 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
998 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
999 segment_name (add_symbol_segment),
1000 S_GET_NAME (fixP->fx_subsy),
1001 segment_name (sub_symbol_segment));
1005 if (fixP->fx_addsy)
1007 if (add_symbol_segment == this_segment
1008 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1010 /* This fixup was made when the symbol's segment was
1011 SEG_UNKNOWN, but it is now in the local segment.
1012 So we know how to do the address without relocation. */
1013 add_number += S_GET_VALUE (fixP->fx_addsy);
1014 fixP->fx_offset = add_number;
1015 if (fixP->fx_pcrel)
1016 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1017 fixP->fx_addsy = NULL;
1018 fixP->fx_pcrel = 0;
1020 else if (add_symbol_segment == absolute_section
1021 && !TC_FORCE_RELOCATION_ABS (fixP))
1023 add_number += S_GET_VALUE (fixP->fx_addsy);
1024 fixP->fx_offset = add_number;
1025 fixP->fx_addsy = NULL;
1027 else if (add_symbol_segment != undefined_section
1028 && ! bfd_is_com_section (add_symbol_segment)
1029 && MD_APPLY_SYM_VALUE (fixP))
1030 add_number += S_GET_VALUE (fixP->fx_addsy);
1033 if (fixP->fx_pcrel)
1035 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1036 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1038 /* There was no symbol required by this relocation.
1039 However, BFD doesn't really handle relocations
1040 without symbols well. So fake up a local symbol in
1041 the absolute section. */
1042 fixP->fx_addsy = abs_section_sym;
1046 if (!fixP->fx_done)
1047 md_apply_fix (fixP, &add_number, this_segment);
1049 if (!fixP->fx_done)
1051 ++seg_reloc_count;
1052 if (fixP->fx_addsy == NULL)
1053 fixP->fx_addsy = abs_section_sym;
1054 symbol_mark_used_in_reloc (fixP->fx_addsy);
1055 if (fixP->fx_subsy != NULL)
1056 symbol_mark_used_in_reloc (fixP->fx_subsy);
1059 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1061 if (fixP->fx_size < sizeof (valueT))
1063 valueT mask;
1065 mask = 0;
1066 mask--; /* Set all bits to one. */
1067 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1068 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1070 char buf[50], buf2[50];
1071 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1072 if (add_number > 1000)
1073 sprint_value (buf2, add_number);
1074 else
1075 sprintf (buf2, "%ld", (long) add_number);
1076 as_bad_where (fixP->fx_file, fixP->fx_line,
1077 _("value of %s too large for field of %d bytes at %s"),
1078 buf2, fixP->fx_size, buf);
1079 } /* Generic error checking. */
1081 #ifdef WARN_SIGNED_OVERFLOW_WORD
1082 /* Warn if a .word value is too large when treated as a signed
1083 number. We already know it is not too negative. This is to
1084 catch over-large switches generated by gcc on the 68k. */
1085 if (!flag_signed_overflow_ok
1086 && fixP->fx_size == 2
1087 && add_number > 0x7fff)
1088 as_bad_where (fixP->fx_file, fixP->fx_line,
1089 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1090 (long) add_number,
1091 (long) (fragP->fr_address + fixP->fx_where));
1092 #endif
1093 } /* Not a bit fix. */
1095 #ifdef TC_VALIDATE_FIX
1096 skip: ATTRIBUTE_UNUSED_LABEL
1098 #endif
1099 #ifdef DEBUG5
1100 fprintf (stderr, "result:\n");
1101 print_fixup (fixP);
1102 #endif
1103 } /* For each fixS in this segment. */
1105 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1106 return seg_reloc_count;
1109 static void
1110 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1111 asection *sec,
1112 void *xxx ATTRIBUTE_UNUSED)
1114 segment_info_type *seginfo = seg_info (sec);
1116 fixup_segment (seginfo->fix_root, sec);
1119 static void
1120 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1121 char *file, unsigned int line)
1123 char *err;
1124 bfd_reloc_status_type s;
1126 s = bfd_install_relocation (stdoutput, reloc,
1127 fragp->fr_literal, fragp->fr_address,
1128 sec, &err);
1129 switch (s)
1131 case bfd_reloc_ok:
1132 break;
1133 case bfd_reloc_overflow:
1134 as_bad_where (file, line, _("relocation overflow"));
1135 break;
1136 case bfd_reloc_outofrange:
1137 as_bad_where (file, line, _("relocation out of range"));
1138 break;
1139 default:
1140 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1141 file, line, s);
1145 static void
1146 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1148 segment_info_type *seginfo = seg_info (sec);
1149 unsigned int i;
1150 unsigned int n;
1151 struct reloc_list *my_reloc_list, **rp, *r;
1152 arelent **relocs;
1153 fixS *fixp;
1155 /* If seginfo is NULL, we did not create this section; don't do
1156 anything with it. */
1157 if (seginfo == NULL)
1158 return;
1160 n = 0;
1161 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1162 if (!fixp->fx_done)
1163 n++;
1165 #ifdef RELOC_EXPANSION_POSSIBLE
1166 n *= MAX_RELOC_EXPANSION;
1167 #endif
1169 /* Extract relocs for this section from reloc_list. */
1170 rp = &reloc_list;
1171 my_reloc_list = NULL;
1172 while ((r = *rp) != NULL)
1174 if (r->u.b.sec == sec)
1176 *rp = r->next;
1177 r->next = my_reloc_list;
1178 my_reloc_list = r;
1179 n++;
1181 else
1182 rp = &r->next;
1185 relocs = xcalloc (n, sizeof (arelent *));
1187 i = 0;
1188 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1190 int j;
1191 int fx_size, slack;
1192 /* NativeClient change here to handle moving calls. */
1193 int limitsize;
1194 offsetT loc;
1196 if (fixp->fx_done)
1197 continue;
1199 fx_size = fixp->fx_size;
1200 slack = TC_FX_SIZE_SLACK (fixp);
1201 if (slack > 0)
1202 fx_size = fx_size > slack ? fx_size - slack : 0;
1203 loc = fixp->fx_where + fx_size;
1204 limitsize = (fixp->fx_frag->is_call ?
1205 (fixp->fx_frag->fr_fix + fixp->fx_frag->fr_var) :
1206 fixp->fx_frag->fr_fix);
1207 if (slack >= 0 && loc > limitsize)
1208 as_bad_where (fixp->fx_file, fixp->fx_line,
1209 _("internal error: fixup not contained within frag"));
1211 #ifndef RELOC_EXPANSION_POSSIBLE
1213 arelent *reloc = tc_gen_reloc (sec, fixp);
1215 if (!reloc)
1216 continue;
1217 relocs[i++] = reloc;
1218 j = 1;
1220 #else
1222 arelent **reloc = tc_gen_reloc (sec, fixp);
1224 for (j = 0; reloc[j]; j++)
1225 relocs[i++] = reloc[j];
1227 #endif
1229 for ( ; j != 0; --j)
1230 install_reloc (sec, relocs[i - j], fixp->fx_frag,
1231 fixp->fx_file, fixp->fx_line);
1233 n = i;
1235 #ifdef DEBUG4
1237 unsigned int i, j, nsyms;
1238 asymbol **sympp;
1239 sympp = bfd_get_outsymbols (stdoutput);
1240 nsyms = bfd_get_symcount (stdoutput);
1241 for (i = 0; i < n; i++)
1242 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1244 for (j = 0; j < nsyms; j++)
1245 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1246 break;
1247 if (j == nsyms)
1248 abort ();
1251 #endif
1253 for (r = my_reloc_list; r != NULL; r = r->next)
1255 fragS *f;
1256 for (f = seginfo->frchainP->frch_root; f; f = f->fr_next)
1257 if (f->fr_address <= r->u.b.r.address
1258 && r->u.b.r.address < f->fr_address + f->fr_fix)
1259 break;
1260 if (f == NULL)
1261 as_bad_where (r->file, r->line,
1262 _("reloc not within (fixed part of) section"));
1263 else
1265 relocs[n++] = &r->u.b.r;
1266 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1270 if (n)
1272 flagword flags = bfd_get_section_flags (abfd, sec);
1273 flags |= SEC_RELOC;
1274 bfd_set_section_flags (abfd, sec, flags);
1275 bfd_set_reloc (stdoutput, sec, relocs, n);
1278 #ifdef SET_SECTION_RELOCS
1279 SET_SECTION_RELOCS (sec, relocs, n);
1280 #endif
1282 #ifdef DEBUG3
1284 unsigned int i;
1285 arelent *r;
1286 asymbol *s;
1287 fprintf (stderr, "relocs for sec %s\n", sec->name);
1288 for (i = 0; i < n; i++)
1290 r = relocs[i];
1291 s = *r->sym_ptr_ptr;
1292 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1293 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1296 #endif
1299 static void
1300 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1301 asection *sec,
1302 void *xxx ATTRIBUTE_UNUSED)
1304 segment_info_type *seginfo = seg_info (sec);
1305 addressT offset = 0;
1306 fragS *f;
1308 /* Write out the frags. */
1309 if (seginfo == NULL
1310 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1311 return;
1313 for (f = seginfo->frchainP->frch_root;
1315 f = f->fr_next)
1317 int x;
1318 addressT fill_size;
1319 char *fill_literal;
1320 offsetT count;
1322 assert (f->fr_type == rs_fill);
1323 if (f->fr_fix)
1325 x = bfd_set_section_contents (stdoutput, sec,
1326 f->fr_literal, (file_ptr) offset,
1327 (bfd_size_type) f->fr_fix);
1328 if (!x)
1329 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1330 bfd_errmsg (bfd_get_error ()));
1331 offset += f->fr_fix;
1333 fill_literal = f->fr_literal + f->fr_fix;
1334 fill_size = f->fr_var;
1335 count = f->fr_offset;
1336 assert (count >= 0);
1337 if (fill_size && count)
1339 char buf[256];
1340 if (fill_size > sizeof (buf))
1342 /* Do it the old way. Can this ever happen? */
1343 while (count--)
1345 x = bfd_set_section_contents (stdoutput, sec,
1346 fill_literal,
1347 (file_ptr) offset,
1348 (bfd_size_type) fill_size);
1349 if (!x)
1350 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1351 bfd_errmsg (bfd_get_error ()));
1352 offset += fill_size;
1355 else
1357 /* Build a buffer full of fill objects and output it as
1358 often as necessary. This saves on the overhead of
1359 potentially lots of bfd_set_section_contents calls. */
1360 int n_per_buf, i;
1361 if (fill_size == 1)
1363 n_per_buf = sizeof (buf);
1364 memset (buf, *fill_literal, n_per_buf);
1366 else
1368 char *bufp;
1369 n_per_buf = sizeof (buf) / fill_size;
1370 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1371 memcpy (bufp, fill_literal, fill_size);
1373 for (; count > 0; count -= n_per_buf)
1375 n_per_buf = n_per_buf > count ? count : n_per_buf;
1376 x = bfd_set_section_contents
1377 (stdoutput, sec, buf, (file_ptr) offset,
1378 (bfd_size_type) n_per_buf * fill_size);
1379 if (!x)
1380 as_fatal (_("cannot write to output file"));
1381 offset += n_per_buf * fill_size;
1388 static void
1389 merge_data_into_text (void)
1391 seg_info (text_section)->frchainP->frch_last->fr_next =
1392 seg_info (data_section)->frchainP->frch_root;
1393 seg_info (text_section)->frchainP->frch_last =
1394 seg_info (data_section)->frchainP->frch_last;
1395 seg_info (data_section)->frchainP = 0;
1398 static void
1399 set_symtab (void)
1401 int nsyms;
1402 asymbol **asympp;
1403 symbolS *symp;
1404 bfd_boolean result;
1406 /* Count symbols. We can't rely on a count made by the loop in
1407 write_object_file, because *_frob_file may add a new symbol or
1408 two. */
1409 nsyms = 0;
1410 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1411 nsyms++;
1413 if (nsyms)
1415 int i;
1416 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1418 asympp = bfd_alloc (stdoutput, amt);
1419 symp = symbol_rootP;
1420 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1422 asympp[i] = symbol_get_bfdsym (symp);
1423 symbol_mark_written (symp);
1426 else
1427 asympp = 0;
1428 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1429 assert (result);
1430 symbol_table_frozen = 1;
1433 /* Finish the subsegments. After every sub-segment, we fake an
1434 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1435 ".fill 0" because that is the kind of frag that requires least
1436 thought. ".align" frags like to have a following frag since that
1437 makes calculating their intended length trivial. */
1439 #ifndef SUB_SEGMENT_ALIGN
1440 #ifdef HANDLE_ALIGN
1441 /* The last subsegment gets an alignment corresponding to the alignment
1442 of the section. This allows proper nop-filling at the end of
1443 code-bearing sections. */
1444 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1445 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1446 #else
1447 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1448 #endif
1449 #endif
1451 void
1452 subsegs_finish (void)
1454 struct frchain *frchainP;
1455 asection *s;
1457 for (s = stdoutput->sections; s; s = s->next)
1459 segment_info_type *seginfo = seg_info (s);
1460 if (!seginfo)
1461 continue;
1463 for (frchainP = seginfo->frchainP;
1464 frchainP != NULL;
1465 frchainP = frchainP->frch_next)
1467 int alignment = 0;
1469 subseg_set (s, frchainP->frch_subseg);
1471 /* This now gets called even if we had errors. In that case,
1472 any alignment is meaningless, and, moreover, will look weird
1473 if we are generating a listing. */
1474 if (!had_errors ())
1476 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1477 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1478 && now_seg->entsize)
1480 unsigned int entsize = now_seg->entsize;
1481 int entalign = 0;
1483 while ((entsize & 1) == 0)
1485 ++entalign;
1486 entsize >>= 1;
1488 if (entalign > alignment)
1489 alignment = entalign;
1493 if (subseg_text_p (now_seg))
1494 frag_align_code (alignment, 0);
1495 else
1496 frag_align (alignment, 0, 0);
1498 /* frag_align will have left a new frag.
1499 Use this last frag for an empty ".fill".
1501 For this segment ...
1502 Create a last frag. Do not leave a "being filled in frag". */
1503 frag_wane (frag_now);
1504 frag_now->fr_fix = 0;
1505 know (frag_now->fr_next == NULL);
1510 /* Write the object file. */
1512 void
1513 write_object_file (void)
1515 struct relax_seg_info rsi;
1516 #ifndef WORKING_DOT_WORD
1517 fragS *fragP; /* Track along all frags. */
1518 #endif
1520 /* Do we really want to write it? */
1522 int n_warns, n_errs;
1523 n_warns = had_warnings ();
1524 n_errs = had_errors ();
1525 /* The -Z flag indicates that an object file should be generated,
1526 regardless of warnings and errors. */
1527 if (flag_always_generate_output)
1529 if (n_warns || n_errs)
1530 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1531 n_errs, n_errs == 1 ? "" : "s",
1532 n_warns, n_warns == 1 ? "" : "s");
1534 else
1536 if (n_errs)
1537 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1538 n_errs, n_errs == 1 ? "" : "s",
1539 n_warns, n_warns == 1 ? "" : "s");
1543 #ifdef OBJ_VMS
1544 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1545 a routine to check for the definition of the procedure "_main",
1546 and if so -- fix it up so that it can be program entry point. */
1547 vms_check_for_main ();
1548 #endif /* OBJ_VMS */
1550 /* From now on, we don't care about sub-segments. Build one frag chain
1551 for each segment. Linked thru fr_next. */
1553 /* Remove the sections created by gas for its own purposes. */
1555 int i;
1557 bfd_section_list_remove (stdoutput, reg_section);
1558 bfd_section_list_remove (stdoutput, expr_section);
1559 stdoutput->section_count -= 2;
1560 i = 0;
1561 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1564 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1566 /* We have two segments. If user gave -R flag, then we must put the
1567 data frags into the text segment. Do this before relaxing so
1568 we know to take advantage of -R and make shorter addresses. */
1569 if (flag_readonly_data_in_text)
1571 merge_data_into_text ();
1574 rsi.pass = 0;
1575 while (1)
1577 #ifndef WORKING_DOT_WORD
1578 /* We need to reset the markers in the broken word list and
1579 associated frags between calls to relax_segment (via
1580 relax_seg). Since the broken word list is global, we do it
1581 once per round, rather than locally in relax_segment for each
1582 segment. */
1583 struct broken_word *brokp;
1585 for (brokp = broken_words;
1586 brokp != (struct broken_word *) NULL;
1587 brokp = brokp->next_broken_word)
1589 brokp->added = 0;
1591 if (brokp->dispfrag != (fragS *) NULL
1592 && brokp->dispfrag->fr_type == rs_broken_word)
1593 brokp->dispfrag->fr_subtype = 0;
1595 #endif
1597 rsi.changed = 0;
1598 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1599 rsi.pass++;
1600 if (!rsi.changed)
1601 break;
1604 /* Note - Most ports will use the default value of
1605 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1606 local symbols to be resolved, removing their frag information.
1607 Some ports however, will not have finished relaxing all of
1608 their frags and will still need the local symbol frag
1609 information. These ports can set
1610 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1611 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1613 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1615 /* Relaxation has completed. Freeze all syms. */
1616 finalize_syms = 1;
1618 #ifdef md_post_relax_hook
1619 md_post_relax_hook;
1620 #endif
1622 #ifndef WORKING_DOT_WORD
1624 struct broken_word *lie;
1625 struct broken_word **prevP;
1627 prevP = &broken_words;
1628 for (lie = broken_words; lie; lie = lie->next_broken_word)
1629 if (!lie->added)
1631 expressionS exp;
1633 subseg_change (lie->seg, lie->subseg);
1634 exp.X_op = O_subtract;
1635 exp.X_add_symbol = lie->add;
1636 exp.X_op_symbol = lie->sub;
1637 exp.X_add_number = lie->addnum;
1638 #ifdef TC_CONS_FIX_NEW
1639 TC_CONS_FIX_NEW (lie->frag,
1640 lie->word_goes_here - lie->frag->fr_literal,
1641 2, &exp);
1642 #else
1643 fix_new_exp (lie->frag,
1644 lie->word_goes_here - lie->frag->fr_literal,
1645 2, &exp, 0, BFD_RELOC_16);
1646 #endif
1647 *prevP = lie->next_broken_word;
1649 else
1650 prevP = &(lie->next_broken_word);
1652 for (lie = broken_words; lie;)
1654 struct broken_word *untruth;
1655 char *table_ptr;
1656 addressT table_addr;
1657 addressT from_addr, to_addr;
1658 int n, m;
1660 subseg_change (lie->seg, lie->subseg);
1661 fragP = lie->dispfrag;
1663 /* Find out how many broken_words go here. */
1664 n = 0;
1665 for (untruth = lie;
1666 untruth && untruth->dispfrag == fragP;
1667 untruth = untruth->next_broken_word)
1668 if (untruth->added == 1)
1669 n++;
1671 table_ptr = lie->dispfrag->fr_opcode;
1672 table_addr = (lie->dispfrag->fr_address
1673 + (table_ptr - lie->dispfrag->fr_literal));
1674 /* Create the jump around the long jumps. This is a short
1675 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1676 from_addr = table_addr;
1677 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1678 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1679 lie->add);
1680 table_ptr += md_short_jump_size;
1681 table_addr += md_short_jump_size;
1683 for (m = 0;
1684 lie && lie->dispfrag == fragP;
1685 m++, lie = lie->next_broken_word)
1687 if (lie->added == 2)
1688 continue;
1689 /* Patch the jump table. */
1690 /* This is the offset from ??? to table_ptr+0. */
1691 to_addr = table_addr - S_GET_VALUE (lie->sub);
1692 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1693 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1694 #endif
1695 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1696 for (untruth = lie->next_broken_word;
1697 untruth && untruth->dispfrag == fragP;
1698 untruth = untruth->next_broken_word)
1700 if (untruth->use_jump == lie)
1701 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1704 /* Install the long jump. */
1705 /* This is a long jump from table_ptr+0 to the final target. */
1706 from_addr = table_addr;
1707 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1708 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1709 lie->add);
1710 table_ptr += md_long_jump_size;
1711 table_addr += md_long_jump_size;
1715 #endif /* not WORKING_DOT_WORD */
1717 /* Resolve symbol values. This needs to be done before processing
1718 the relocations. */
1719 if (symbol_rootP)
1721 symbolS *symp;
1723 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1724 resolve_symbol_value (symp);
1726 resolve_local_symbol_values ();
1727 resolve_reloc_expr_symbols ();
1729 PROGRESS (1);
1731 #ifdef tc_frob_file_before_adjust
1732 tc_frob_file_before_adjust ();
1733 #endif
1734 #ifdef obj_frob_file_before_adjust
1735 obj_frob_file_before_adjust ();
1736 #endif
1738 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1740 #ifdef tc_frob_file_before_fix
1741 tc_frob_file_before_fix ();
1742 #endif
1743 #ifdef obj_frob_file_before_fix
1744 obj_frob_file_before_fix ();
1745 #endif
1747 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1749 /* Set up symbol table, and write it out. */
1750 if (symbol_rootP)
1752 symbolS *symp;
1753 bfd_boolean skip_next_symbol = FALSE;
1755 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1757 int punt = 0;
1758 const char *name;
1760 if (skip_next_symbol)
1762 /* Don't do anything besides moving the value of the
1763 symbol from the GAS value-field to the BFD value-field. */
1764 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1765 skip_next_symbol = FALSE;
1766 continue;
1769 if (symbol_mri_common_p (symp))
1771 if (S_IS_EXTERNAL (symp))
1772 as_bad (_("%s: global symbols not supported in common sections"),
1773 S_GET_NAME (symp));
1774 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1775 continue;
1778 name = S_GET_NAME (symp);
1779 if (name)
1781 const char *name2 =
1782 decode_local_label_name ((char *) S_GET_NAME (symp));
1783 /* They only differ if `name' is a fb or dollar local
1784 label name. */
1785 if (name2 != name && ! S_IS_DEFINED (symp))
1786 as_bad (_("local label `%s' is not defined"), name2);
1789 /* Do it again, because adjust_reloc_syms might introduce
1790 more symbols. They'll probably only be section symbols,
1791 but they'll still need to have the values computed. */
1792 resolve_symbol_value (symp);
1794 /* Skip symbols which were equated to undefined or common
1795 symbols. */
1796 if (symbol_equated_reloc_p (symp)
1797 || S_IS_WEAKREFR (symp))
1799 const char *name = S_GET_NAME (symp);
1800 if (S_IS_COMMON (symp)
1801 && !TC_FAKE_LABEL (name)
1802 && !S_IS_WEAKREFR (symp)
1803 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1805 expressionS *e = symbol_get_value_expression (symp);
1806 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1807 name, S_GET_NAME (e->X_add_symbol));
1809 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1810 continue;
1813 #ifdef obj_frob_symbol
1814 obj_frob_symbol (symp, punt);
1815 #endif
1816 #ifdef tc_frob_symbol
1817 if (! punt || symbol_used_in_reloc_p (symp))
1818 tc_frob_symbol (symp, punt);
1819 #endif
1821 /* If we don't want to keep this symbol, splice it out of
1822 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1823 want section symbols. Otherwise, we skip local symbols
1824 and symbols that the frob_symbol macros told us to punt,
1825 but we keep such symbols if they are used in relocs. */
1826 if (symp == abs_section_sym
1827 || (! EMIT_SECTION_SYMBOLS
1828 && symbol_section_p (symp))
1829 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1830 opposites. Sometimes the former checks flags and the
1831 latter examines the name... */
1832 || (!S_IS_EXTERNAL (symp)
1833 && (punt || S_IS_LOCAL (symp) ||
1834 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1835 && ! symbol_used_in_reloc_p (symp)))
1837 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1839 /* After symbol_remove, symbol_next(symp) still returns
1840 the one that came after it in the chain. So we don't
1841 need to do any extra cleanup work here. */
1842 continue;
1845 /* Make sure we really got a value for the symbol. */
1846 if (! symbol_resolved_p (symp))
1848 as_bad (_("can't resolve value for symbol `%s'"),
1849 S_GET_NAME (symp));
1850 symbol_mark_resolved (symp);
1853 /* Set the value into the BFD symbol. Up til now the value
1854 has only been kept in the gas symbolS struct. */
1855 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1857 /* A warning construct is a warning symbol followed by the
1858 symbol warned about. Don't let anything object-format or
1859 target-specific muck with it; it's ready for output. */
1860 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1861 skip_next_symbol = TRUE;
1865 PROGRESS (1);
1867 /* Now do any format-specific adjustments to the symbol table, such
1868 as adding file symbols. */
1869 #ifdef tc_adjust_symtab
1870 tc_adjust_symtab ();
1871 #endif
1872 #ifdef obj_adjust_symtab
1873 obj_adjust_symtab ();
1874 #endif
1876 /* Now that all the sizes are known, and contents correct, we can
1877 start writing to the file. */
1878 set_symtab ();
1880 /* If *_frob_file changes the symbol value at this point, it is
1881 responsible for moving the changed value into symp->bsym->value
1882 as well. Hopefully all symbol value changing can be done in
1883 *_frob_symbol. */
1884 #ifdef tc_frob_file
1885 tc_frob_file ();
1886 #endif
1887 #ifdef obj_frob_file
1888 obj_frob_file ();
1889 #endif
1891 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1893 #ifdef tc_frob_file_after_relocs
1894 tc_frob_file_after_relocs ();
1895 #endif
1896 #ifdef obj_frob_file_after_relocs
1897 obj_frob_file_after_relocs ();
1898 #endif
1900 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1903 #ifdef TC_GENERIC_RELAX_TABLE
1904 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1906 long
1907 relax_frag (segT segment, fragS *fragP, long stretch)
1909 const relax_typeS *this_type;
1910 const relax_typeS *start_type;
1911 relax_substateT next_state;
1912 relax_substateT this_state;
1913 offsetT growth;
1914 offsetT aim;
1915 addressT target;
1916 addressT address;
1917 symbolS *symbolP;
1918 const relax_typeS *table;
1920 target = fragP->fr_offset;
1921 address = fragP->fr_address;
1922 table = TC_GENERIC_RELAX_TABLE;
1923 this_state = fragP->fr_subtype;
1924 start_type = this_type = table + this_state;
1925 symbolP = fragP->fr_symbol;
1927 if (symbolP)
1929 fragS *sym_frag;
1931 sym_frag = symbol_get_frag (symbolP);
1933 #ifndef DIFF_EXPR_OK
1934 know (sym_frag != NULL);
1935 #endif
1936 know (S_GET_SEGMENT (symbolP) != absolute_section
1937 || sym_frag == &zero_address_frag);
1938 target += S_GET_VALUE (symbolP);
1940 /* If frag has yet to be reached on this pass,
1941 assume it will move by STRETCH just as we did.
1942 If this is not so, it will be because some frag
1943 between grows, and that will force another pass. */
1945 if (stretch != 0
1946 && sym_frag->relax_marker != fragP->relax_marker
1947 && S_GET_SEGMENT (symbolP) == segment)
1949 target += stretch;
1953 aim = target - address - fragP->fr_fix;
1954 #ifdef TC_PCREL_ADJUST
1955 /* Currently only the ns32k family needs this. */
1956 aim += TC_PCREL_ADJUST (fragP);
1957 #endif
1959 #ifdef md_prepare_relax_scan
1960 /* Formerly called M68K_AIM_KLUDGE. */
1961 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1962 #endif
1964 if (aim < 0)
1966 /* Look backwards. */
1967 for (next_state = this_type->rlx_more; next_state;)
1968 if (aim >= this_type->rlx_backward)
1969 next_state = 0;
1970 else
1972 /* Grow to next state. */
1973 this_state = next_state;
1974 this_type = table + this_state;
1975 next_state = this_type->rlx_more;
1978 else
1980 /* Look forwards. */
1981 for (next_state = this_type->rlx_more; next_state;)
1982 if (aim <= this_type->rlx_forward)
1983 next_state = 0;
1984 else
1986 /* Grow to next state. */
1987 this_state = next_state;
1988 this_type = table + this_state;
1989 next_state = this_type->rlx_more;
1993 growth = this_type->rlx_length - start_type->rlx_length;
1994 if (growth != 0)
1995 fragP->fr_subtype = this_state;
1996 return growth;
1999 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2001 /* Relax_align. Advance location counter to next address that has 'alignment'
2002 lowest order bits all 0s, return size of adjustment made. */
2003 static relax_addressT
2004 relax_align (register relax_addressT address, /* Address now. */
2005 register int alignment /* Alignment (binary). */)
2007 relax_addressT mask;
2008 relax_addressT new_address;
2010 mask = ~((~0) << alignment);
2011 new_address = (address + mask) & (~mask);
2012 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2013 if (linkrelax)
2014 /* We must provide lots of padding, so the linker can discard it
2015 when needed. The linker will not add extra space, ever. */
2016 new_address += (1 << alignment);
2017 #endif
2018 return (new_address - address);
2021 /* Now we have a segment, not a crowd of sub-segments, we can make
2022 fr_address values.
2024 Relax the frags.
2026 After this, all frags in this segment have addresses that are correct
2027 within the segment. Since segments live in different file addresses,
2028 these frag addresses may not be the same as final object-file
2029 addresses. */
2032 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2034 unsigned long frag_count;
2035 struct frag *fragP;
2036 relax_addressT address;
2037 int ret;
2039 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2040 subseg_change (segment, 0);
2042 /* For each frag in segment: count and store (a 1st guess of)
2043 fr_address. */
2044 address = 0;
2045 for (frag_count = 0, fragP = segment_frag_root;
2046 fragP;
2047 fragP = fragP->fr_next, frag_count ++)
2049 fragP->relax_marker = 0;
2050 fragP->fr_address = address;
2051 address += fragP->fr_fix;
2053 switch (fragP->fr_type)
2055 case rs_fill:
2056 address += fragP->fr_offset * fragP->fr_var;
2057 break;
2059 case rs_align:
2060 case rs_align_code:
2061 case rs_align_test:
2063 addressT offset = relax_align (address, (int) fragP->fr_offset);
2065 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2066 offset = 0;
2068 if (offset % fragP->fr_var != 0)
2070 as_bad_where (fragP->fr_file, fragP->fr_line,
2071 _("alignment padding (%lu bytes) not a multiple of %ld"),
2072 (unsigned long) offset, (long) fragP->fr_var);
2073 offset -= (offset % fragP->fr_var);
2076 address += offset;
2078 break;
2080 case rs_org:
2081 case rs_space:
2082 /* Assume .org is nugatory. It will grow with 1st relax. */
2083 break;
2085 case rs_machine_dependent:
2086 /* If fr_symbol is an expression, this call to
2087 resolve_symbol_value sets up the correct segment, which will
2088 likely be needed in md_estimate_size_before_relax. */
2089 if (fragP->fr_symbol)
2090 resolve_symbol_value (fragP->fr_symbol);
2092 address += md_estimate_size_before_relax (fragP, segment);
2093 break;
2095 #ifndef WORKING_DOT_WORD
2096 /* Broken words don't concern us yet. */
2097 case rs_broken_word:
2098 break;
2099 #endif
2101 case rs_leb128:
2102 /* Initial guess is always 1; doing otherwise can result in
2103 stable solutions that are larger than the minimum. */
2104 address += fragP->fr_offset = 1;
2105 break;
2107 case rs_cfa:
2108 address += eh_frame_estimate_size_before_relax (fragP);
2109 break;
2111 case rs_dwarf2dbg:
2112 address += dwarf2dbg_estimate_size_before_relax (fragP);
2113 break;
2115 default:
2116 BAD_CASE (fragP->fr_type);
2117 break;
2121 /* Do relax(). */
2123 unsigned long max_iterations;
2125 /* Cumulative address adjustment. */
2126 offsetT stretch;
2128 /* Have we made any adjustment this pass? We can't just test
2129 stretch because one piece of code may have grown and another
2130 shrank. */
2131 int stretched;
2133 /* Most horrible, but gcc may give us some exception data that
2134 is impossible to assemble, of the form
2136 .align 4
2137 .byte 0, 0
2138 .uleb128 end - start
2139 start:
2140 .space 128*128 - 1
2141 .align 4
2142 end:
2144 If the leb128 is two bytes in size, then end-start is 128*128,
2145 which requires a three byte leb128. If the leb128 is three
2146 bytes in size, then end-start is 128*128-1, which requires a
2147 two byte leb128. We work around this dilemma by inserting
2148 an extra 4 bytes of alignment just after the .align. This
2149 works because the data after the align is accessed relative to
2150 the end label.
2152 This counter is used in a tiny state machine to detect
2153 whether a leb128 followed by an align is impossible to
2154 relax. */
2155 int rs_leb128_fudge = 0;
2157 /* We want to prevent going into an infinite loop where one frag grows
2158 depending upon the location of a symbol which is in turn moved by
2159 the growing frag. eg:
2161 foo = .
2162 .org foo+16
2163 foo = .
2165 So we dictate that this algorithm can be at most O2. */
2166 max_iterations = frag_count * frag_count;
2167 /* Check for overflow. */
2168 if (max_iterations < frag_count)
2169 max_iterations = frag_count;
2171 ret = 0;
2174 stretch = 0;
2175 stretched = 0;
2177 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2179 offsetT growth = 0;
2180 addressT was_address;
2181 offsetT offset;
2182 symbolS *symbolP;
2184 fragP->relax_marker ^= 1;
2185 was_address = fragP->fr_address;
2186 address = fragP->fr_address += stretch;
2187 symbolP = fragP->fr_symbol;
2188 offset = fragP->fr_offset;
2190 switch (fragP->fr_type)
2192 case rs_fill: /* .fill never relaxes. */
2193 growth = 0;
2194 break;
2196 #ifndef WORKING_DOT_WORD
2197 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2198 for it I do not want to write it. I do not want to have
2199 anything to do with it. This is not the proper way to
2200 implement this misfeature. */
2201 case rs_broken_word:
2203 struct broken_word *lie;
2204 struct broken_word *untruth;
2206 /* Yes this is ugly (storing the broken_word pointer
2207 in the symbol slot). Still, this whole chunk of
2208 code is ugly, and I don't feel like doing anything
2209 about it. Think of it as stubbornness in action. */
2210 growth = 0;
2211 for (lie = (struct broken_word *) (fragP->fr_symbol);
2212 lie && lie->dispfrag == fragP;
2213 lie = lie->next_broken_word)
2216 if (lie->added)
2217 continue;
2219 offset = (S_GET_VALUE (lie->add)
2220 + lie->addnum
2221 - S_GET_VALUE (lie->sub));
2222 if (offset <= -32768 || offset >= 32767)
2224 if (flag_warn_displacement)
2226 char buf[50];
2227 sprint_value (buf, (addressT) lie->addnum);
2228 as_warn_where (fragP->fr_file, fragP->fr_line,
2229 _(".word %s-%s+%s didn't fit"),
2230 S_GET_NAME (lie->add),
2231 S_GET_NAME (lie->sub),
2232 buf);
2234 lie->added = 1;
2235 if (fragP->fr_subtype == 0)
2237 fragP->fr_subtype++;
2238 growth += md_short_jump_size;
2240 for (untruth = lie->next_broken_word;
2241 untruth && untruth->dispfrag == lie->dispfrag;
2242 untruth = untruth->next_broken_word)
2243 if ((symbol_get_frag (untruth->add)
2244 == symbol_get_frag (lie->add))
2245 && (S_GET_VALUE (untruth->add)
2246 == S_GET_VALUE (lie->add)))
2248 untruth->added = 2;
2249 untruth->use_jump = lie;
2251 growth += md_long_jump_size;
2255 break;
2256 } /* case rs_broken_word */
2257 #endif
2258 case rs_align:
2259 case rs_align_code:
2260 case rs_align_test:
2262 addressT oldoff, newoff;
2264 oldoff = relax_align (was_address + fragP->fr_fix,
2265 (int) offset);
2266 newoff = relax_align (address + fragP->fr_fix,
2267 (int) offset);
2269 if (fragP->fr_subtype != 0)
2271 if (oldoff > fragP->fr_subtype)
2272 oldoff = 0;
2273 if (newoff > fragP->fr_subtype)
2274 newoff = 0;
2277 growth = newoff - oldoff;
2279 /* If this align happens to follow a leb128 and
2280 we have determined that the leb128 is bouncing
2281 in size, then break the cycle by inserting an
2282 extra alignment. */
2283 if (growth < 0
2284 && (rs_leb128_fudge & 16) != 0
2285 && (rs_leb128_fudge & 15) >= 2)
2287 segment_info_type *seginfo = seg_info (segment);
2288 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2289 struct frag *newf;
2291 newf = frag_alloc (ob);
2292 obstack_blank_fast (ob, fragP->fr_var);
2293 obstack_finish (ob);
2294 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2295 memcpy (newf->fr_literal,
2296 fragP->fr_literal + fragP->fr_fix,
2297 fragP->fr_var);
2298 newf->fr_type = rs_fill;
2299 newf->fr_fix = 0;
2300 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2301 / fragP->fr_var);
2302 if (newf->fr_offset * newf->fr_var
2303 != (offsetT) 1 << fragP->fr_offset)
2305 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2306 newf->fr_var = 1;
2308 /* Include growth of new frag, because rs_fill
2309 frags don't normally grow. */
2310 growth += newf->fr_offset * newf->fr_var;
2311 /* The new frag address is newoff. Adjust this
2312 for the amount we'll add when we process the
2313 new frag. */
2314 newf->fr_address = newoff - stretch - growth;
2315 newf->relax_marker ^= 1;
2316 fragP->fr_next = newf;
2317 #ifdef DEBUG
2318 as_warn (_("padding added"));
2319 #endif
2322 break;
2324 case rs_org:
2326 addressT target = offset;
2327 addressT after;
2329 if (symbolP)
2331 /* Convert from an actual address to an octet offset
2332 into the section. Here it is assumed that the
2333 section's VMA is zero, and can omit subtracting it
2334 from the symbol's value to get the address offset. */
2335 know (S_GET_SEGMENT (symbolP)->vma == 0);
2336 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2339 know (fragP->fr_next);
2340 after = fragP->fr_next->fr_address;
2341 growth = target - after;
2342 if (growth < 0)
2344 growth = 0;
2346 /* Don't error on first few frag relax passes.
2347 The symbol might be an expression involving
2348 symbol values from other sections. If those
2349 sections have not yet been processed their
2350 frags will all have zero addresses, so we
2351 will calculate incorrect values for them. The
2352 number of passes we allow before giving an
2353 error is somewhat arbitrary. It should be at
2354 least one, with larger values requiring
2355 increasingly contrived dependencies between
2356 frags to trigger a false error. */
2357 if (pass < 2)
2359 /* Force another pass. */
2360 ret = 1;
2361 break;
2364 /* Growth may be negative, but variable part of frag
2365 cannot have fewer than 0 chars. That is, we can't
2366 .org backwards. */
2367 as_bad_where (fragP->fr_file, fragP->fr_line,
2368 _("attempt to move .org backwards"));
2370 /* We've issued an error message. Change the
2371 frag to avoid cascading errors. */
2372 fragP->fr_type = rs_align;
2373 fragP->fr_subtype = 0;
2374 fragP->fr_offset = 0;
2375 fragP->fr_fix = after - was_address;
2376 break;
2379 /* This is an absolute growth factor */
2380 growth -= stretch;
2381 break;
2384 case rs_space:
2385 growth = 0;
2386 if (symbolP)
2388 offsetT amount;
2390 amount = S_GET_VALUE (symbolP);
2391 if (S_GET_SEGMENT (symbolP) != absolute_section
2392 || S_IS_COMMON (symbolP)
2393 || ! S_IS_DEFINED (symbolP))
2395 as_bad_where (fragP->fr_file, fragP->fr_line,
2396 _(".space specifies non-absolute value"));
2397 /* Prevent repeat of this error message. */
2398 fragP->fr_symbol = 0;
2400 else if (amount < 0)
2402 /* Don't error on first few frag relax passes.
2403 See rs_org comment for a longer explanation. */
2404 if (pass < 2)
2406 ret = 1;
2407 break;
2410 as_warn_where (fragP->fr_file, fragP->fr_line,
2411 _(".space or .fill with negative value, ignored"));
2412 fragP->fr_symbol = 0;
2414 else
2415 growth = (was_address + fragP->fr_fix + amount
2416 - fragP->fr_next->fr_address);
2418 break;
2420 case rs_machine_dependent:
2421 #ifdef md_relax_frag
2422 growth = md_relax_frag (segment, fragP, stretch);
2423 #else
2424 #ifdef TC_GENERIC_RELAX_TABLE
2425 /* The default way to relax a frag is to look through
2426 TC_GENERIC_RELAX_TABLE. */
2427 growth = relax_frag (segment, fragP, stretch);
2428 #endif /* TC_GENERIC_RELAX_TABLE */
2429 #endif
2430 break;
2432 case rs_leb128:
2434 valueT value;
2435 offsetT size;
2437 value = resolve_symbol_value (fragP->fr_symbol);
2438 size = sizeof_leb128 (value, fragP->fr_subtype);
2439 growth = size - fragP->fr_offset;
2440 fragP->fr_offset = size;
2442 break;
2444 case rs_cfa:
2445 growth = eh_frame_relax_frag (fragP);
2446 break;
2448 case rs_dwarf2dbg:
2449 growth = dwarf2dbg_relax_frag (fragP);
2450 break;
2452 default:
2453 BAD_CASE (fragP->fr_type);
2454 break;
2456 if (growth)
2458 stretch += growth;
2459 stretched = 1;
2460 if (fragP->fr_type == rs_leb128)
2461 rs_leb128_fudge += 16;
2462 else if (fragP->fr_type == rs_align
2463 && (rs_leb128_fudge & 16) != 0
2464 && stretch == 0)
2465 rs_leb128_fudge += 16;
2466 else
2467 rs_leb128_fudge = 0;
2471 if (stretch == 0
2472 && (rs_leb128_fudge & 16) == 0
2473 && (rs_leb128_fudge & -16) != 0)
2474 rs_leb128_fudge += 1;
2475 else
2476 rs_leb128_fudge = 0;
2478 /* Until nothing further to relax. */
2479 while (stretched && -- max_iterations);
2481 if (stretched)
2482 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2483 segment_name (segment));
2486 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2487 if (fragP->last_fr_address != fragP->fr_address)
2489 fragP->last_fr_address = fragP->fr_address;
2490 ret = 1;
2492 return ret;
2495 void
2496 number_to_chars_bigendian (char *buf, valueT val, int n)
2498 if (n <= 0)
2499 abort ();
2500 while (n--)
2502 buf[n] = val & 0xff;
2503 val >>= 8;
2507 void
2508 number_to_chars_littleendian (char *buf, valueT val, int n)
2510 if (n <= 0)
2511 abort ();
2512 while (n--)
2514 *buf++ = val & 0xff;
2515 val >>= 8;
2519 void
2520 write_print_statistics (FILE *file)
2522 fprintf (file, "fixups: %d\n", n_fixups);
2525 /* For debugging. */
2526 extern int indent_level;
2528 void
2529 print_fixup (fixS *fixp)
2531 indent_level = 1;
2532 fprintf (stderr, "fix ");
2533 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2534 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
2535 if (fixp->fx_pcrel)
2536 fprintf (stderr, " pcrel");
2537 if (fixp->fx_pcrel_adjust)
2538 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2539 if (fixp->fx_im_disp)
2541 #ifdef TC_NS32K
2542 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2543 #else
2544 fprintf (stderr, " im_disp");
2545 #endif
2547 if (fixp->fx_tcbit)
2548 fprintf (stderr, " tcbit");
2549 if (fixp->fx_done)
2550 fprintf (stderr, " done");
2551 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2552 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2553 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2554 (long) fixp->fx_where,
2555 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2556 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2557 fixp->fx_r_type);
2558 if (fixp->fx_addsy)
2560 fprintf (stderr, "\n +<");
2561 print_symbol_value_1 (stderr, fixp->fx_addsy);
2562 fprintf (stderr, ">");
2564 if (fixp->fx_subsy)
2566 fprintf (stderr, "\n -<");
2567 print_symbol_value_1 (stderr, fixp->fx_subsy);
2568 fprintf (stderr, ">");
2570 fprintf (stderr, "\n");
2571 #ifdef TC_FIX_DATA_PRINT
2572 TC_FIX_DATA_PRINT (stderr, fixp);
2573 #endif