07b9dd7391e58c4b20e0c1491fde88dbe975f79c
[deliverable/binutils-gdb.git] / gas / write.c
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.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
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.
17
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. */
22
23 /* This thing should be set up to do byteordering correctly. But... */
24
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
32 #ifndef TC_ADJUST_RELOC_COUNT
33 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
34 #endif
35
36 #ifndef TC_FORCE_RELOCATION
37 #define TC_FORCE_RELOCATION(FIX) \
38 (generic_force_reloc (FIX))
39 #endif
40
41 #ifndef TC_FORCE_RELOCATION_ABS
42 #define TC_FORCE_RELOCATION_ABS(FIX) \
43 (TC_FORCE_RELOCATION (FIX))
44 #endif
45
46 #ifndef TC_FORCE_RELOCATION_LOCAL
47 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
48 (!(FIX)->fx_pcrel \
49 || TC_FORCE_RELOCATION (FIX))
50 #endif
51
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
54 (! SEG_NORMAL (SEG))
55 #endif
56
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX) 0
59 #endif
60
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
68
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
79
80 #ifndef TC_LINKRELAX_FIXUP
81 #define TC_LINKRELAX_FIXUP(SEG) 1
82 #endif
83
84 #ifndef MD_APPLY_SYM_VALUE
85 #define MD_APPLY_SYM_VALUE(FIX) 1
86 #endif
87
88 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
89 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
90 #endif
91
92 #ifndef MD_PCREL_FROM_SECTION
93 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
94 #endif
95
96 #ifndef TC_FAKE_LABEL
97 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
98 #endif
99
100 /* Used to control final evaluation of expressions. */
101 int finalize_syms = 0;
102
103 int symbol_table_frozen;
104
105 symbolS *abs_section_sym;
106
107 /* Remember the value of dot when parsing expressions. */
108 addressT dot_value;
109
110 void print_fixup (fixS *);
111
112 /* We generally attach relocs to frag chains. However, after we have
113 chained these all together into a segment, any relocs we add after
114 that must be attached to a segment. This will include relocs added
115 in md_estimate_size_for_relax, for example. */
116 static int frags_chained = 0;
117
118 static int n_fixups;
119
120 #define RELOC_ENUM enum bfd_reloc_code_real
121
122 /* Create a fixS in obstack 'notes'. */
123
124 static fixS *
125 fix_new_internal (fragS *frag, /* Which frag? */
126 int where, /* Where in that frag? */
127 int size, /* 1, 2, or 4 usually. */
128 symbolS *add_symbol, /* X_add_symbol. */
129 symbolS *sub_symbol, /* X_op_symbol. */
130 offsetT offset, /* X_add_number. */
131 int pcrel, /* TRUE if PC-relative relocation. */
132 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
133 {
134 fixS *fixP;
135
136 n_fixups++;
137
138 fixP = obstack_alloc (&notes, sizeof (fixS));
139
140 fixP->fx_frag = frag;
141 fixP->fx_where = where;
142 fixP->fx_size = size;
143 /* We've made fx_size a narrow field; check that it's wide enough. */
144 if (fixP->fx_size != size)
145 {
146 as_bad (_("field fx_size too small to hold %d"), size);
147 abort ();
148 }
149 fixP->fx_addsy = add_symbol;
150 fixP->fx_subsy = sub_symbol;
151 fixP->fx_offset = offset;
152 fixP->fx_dot_value = dot_value;
153 fixP->fx_pcrel = pcrel;
154 fixP->fx_r_type = r_type;
155 fixP->fx_im_disp = 0;
156 fixP->fx_pcrel_adjust = 0;
157 fixP->fx_bit_fixP = 0;
158 fixP->fx_addnumber = 0;
159 fixP->fx_tcbit = 0;
160 fixP->fx_tcbit2 = 0;
161 fixP->fx_done = 0;
162 fixP->fx_no_overflow = 0;
163 fixP->fx_signed = 0;
164
165 #ifdef USING_CGEN
166 fixP->fx_cgen.insn = NULL;
167 fixP->fx_cgen.opinfo = 0;
168 #endif
169
170 #ifdef TC_FIX_TYPE
171 TC_INIT_FIX_DATA (fixP);
172 #endif
173
174 as_where (&fixP->fx_file, &fixP->fx_line);
175
176 /* Usually, we want relocs sorted numerically, but while
177 comparing to older versions of gas that have relocs
178 reverse sorted, it is convenient to have this compile
179 time option. xoxorich. */
180 {
181
182 fixS **seg_fix_rootP = (frags_chained
183 ? &seg_info (now_seg)->fix_root
184 : &frchain_now->fix_root);
185 fixS **seg_fix_tailP = (frags_chained
186 ? &seg_info (now_seg)->fix_tail
187 : &frchain_now->fix_tail);
188
189 #ifdef REVERSE_SORT_RELOCS
190
191 fixP->fx_next = *seg_fix_rootP;
192 *seg_fix_rootP = fixP;
193
194 #else /* REVERSE_SORT_RELOCS */
195
196 fixP->fx_next = NULL;
197
198 if (*seg_fix_tailP)
199 (*seg_fix_tailP)->fx_next = fixP;
200 else
201 *seg_fix_rootP = fixP;
202 *seg_fix_tailP = fixP;
203
204 #endif /* REVERSE_SORT_RELOCS */
205 }
206
207 return fixP;
208 }
209
210 /* Create a fixup relative to a symbol (plus a constant). */
211
212 fixS *
213 fix_new (fragS *frag, /* Which frag? */
214 int where, /* Where in that frag? */
215 int size, /* 1, 2, or 4 usually. */
216 symbolS *add_symbol, /* X_add_symbol. */
217 offsetT offset, /* X_add_number. */
218 int pcrel, /* TRUE if PC-relative relocation. */
219 RELOC_ENUM r_type /* Relocation type. */)
220 {
221 return fix_new_internal (frag, where, size, add_symbol,
222 (symbolS *) NULL, offset, pcrel, r_type);
223 }
224
225 /* Create a fixup for an expression. Currently we only support fixups
226 for difference expressions. That is itself more than most object
227 file formats support anyhow. */
228
229 fixS *
230 fix_new_exp (fragS *frag, /* Which frag? */
231 int where, /* Where in that frag? */
232 int size, /* 1, 2, or 4 usually. */
233 expressionS *exp, /* Expression. */
234 int pcrel, /* TRUE if PC-relative relocation. */
235 RELOC_ENUM r_type /* Relocation type. */)
236 {
237 symbolS *add = NULL;
238 symbolS *sub = NULL;
239 offsetT off = 0;
240
241 switch (exp->X_op)
242 {
243 case O_absent:
244 break;
245
246 case O_register:
247 as_bad (_("register value used as expression"));
248 break;
249
250 case O_add:
251 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
252 the difference expression cannot immediately be reduced. */
253 {
254 symbolS *stmp = make_expr_symbol (exp);
255
256 exp->X_op = O_symbol;
257 exp->X_op_symbol = 0;
258 exp->X_add_symbol = stmp;
259 exp->X_add_number = 0;
260
261 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
262 }
263
264 case O_symbol_rva:
265 add = exp->X_add_symbol;
266 off = exp->X_add_number;
267 r_type = BFD_RELOC_RVA;
268 break;
269
270 case O_uminus:
271 sub = exp->X_add_symbol;
272 off = exp->X_add_number;
273 break;
274
275 case O_subtract:
276 sub = exp->X_op_symbol;
277 /* Fall through. */
278 case O_symbol:
279 add = exp->X_add_symbol;
280 /* Fall through. */
281 case O_constant:
282 off = exp->X_add_number;
283 break;
284
285 default:
286 add = make_expr_symbol (exp);
287 break;
288 }
289
290 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
291 }
292
293 /* Generic function to determine whether a fixup requires a relocation. */
294 int
295 generic_force_reloc (fixS *fix)
296 {
297 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
298 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
299 return 1;
300
301 if (fix->fx_addsy == NULL)
302 return 0;
303
304 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
305 }
306
307 /* Append a string onto another string, bumping the pointer along. */
308 void
309 append (char **charPP, char *fromP, unsigned long length)
310 {
311 /* Don't trust memcpy() of 0 chars. */
312 if (length == 0)
313 return;
314
315 memcpy (*charPP, fromP, length);
316 *charPP += length;
317 }
318
319 /* This routine records the largest alignment seen for each segment.
320 If the beginning of the segment is aligned on the worst-case
321 boundary, all of the other alignments within it will work. At
322 least one object format really uses this info. */
323
324 void
325 record_alignment (/* Segment to which alignment pertains. */
326 segT seg,
327 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
328 boundary, 2 => 4-byte boundary, etc.) */
329 int align)
330 {
331 if (seg == absolute_section)
332 return;
333
334 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
335 bfd_set_section_alignment (stdoutput, seg, align);
336 }
337
338 int
339 get_recorded_alignment (segT seg)
340 {
341 if (seg == absolute_section)
342 return 0;
343
344 return bfd_get_section_alignment (stdoutput, seg);
345 }
346
347 /* Reset the section indices after removing the gas created sections. */
348
349 static void
350 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
351 {
352 int *countp = (int *) countparg;
353
354 sec->index = *countp;
355 ++*countp;
356 }
357
358 static fragS *
359 chain_frchains_together_1 (segT section, struct frchain *frchp)
360 {
361 fragS dummy, *prev_frag = &dummy;
362 fixS fix_dummy, *prev_fix = &fix_dummy;
363
364 for (; frchp; frchp = frchp->frch_next)
365 {
366 prev_frag->fr_next = frchp->frch_root;
367 prev_frag = frchp->frch_last;
368 assert (prev_frag->fr_type != 0);
369 if (frchp->fix_root != (fixS *) NULL)
370 {
371 if (seg_info (section)->fix_root == (fixS *) NULL)
372 seg_info (section)->fix_root = frchp->fix_root;
373 prev_fix->fx_next = frchp->fix_root;
374 seg_info (section)->fix_tail = frchp->fix_tail;
375 prev_fix = frchp->fix_tail;
376 }
377 }
378 assert (prev_frag->fr_type != 0);
379 assert (prev_frag != &dummy);
380 prev_frag->fr_next = 0;
381 return prev_frag;
382 }
383
384 static void
385 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
386 segT section,
387 void *xxx ATTRIBUTE_UNUSED)
388 {
389 segment_info_type *info;
390
391 /* BFD may have introduced its own sections without using
392 subseg_new, so it is possible that seg_info is NULL. */
393 info = seg_info (section);
394 if (info != (segment_info_type *) NULL)
395 info->frchainP->frch_last
396 = chain_frchains_together_1 (section, info->frchainP);
397
398 /* Now that we've chained the frags together, we must add new fixups
399 to the segment, not to the frag chain. */
400 frags_chained = 1;
401 }
402
403 static void
404 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
405 {
406 switch (fragP->fr_type)
407 {
408 case rs_align:
409 case rs_align_code:
410 case rs_align_test:
411 case rs_org:
412 case rs_space:
413 #ifdef HANDLE_ALIGN
414 HANDLE_ALIGN (fragP);
415 #endif
416 know (fragP->fr_next != NULL);
417 fragP->fr_offset = (fragP->fr_next->fr_address
418 - fragP->fr_address
419 - fragP->fr_fix) / fragP->fr_var;
420 if (fragP->fr_offset < 0)
421 {
422 as_bad_where (fragP->fr_file, fragP->fr_line,
423 _("attempt to .org/.space backwards? (%ld)"),
424 (long) fragP->fr_offset);
425 fragP->fr_offset = 0;
426 }
427 fragP->fr_type = rs_fill;
428 break;
429
430 case rs_fill:
431 break;
432
433 case rs_leb128:
434 {
435 valueT value = S_GET_VALUE (fragP->fr_symbol);
436 int size;
437
438 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
439 fragP->fr_subtype);
440
441 fragP->fr_fix += size;
442 fragP->fr_type = rs_fill;
443 fragP->fr_var = 0;
444 fragP->fr_offset = 0;
445 fragP->fr_symbol = NULL;
446 }
447 break;
448
449 case rs_cfa:
450 eh_frame_convert_frag (fragP);
451 break;
452
453 case rs_dwarf2dbg:
454 dwarf2dbg_convert_frag (fragP);
455 break;
456
457 case rs_machine_dependent:
458 md_convert_frag (stdoutput, sec, fragP);
459
460 assert (fragP->fr_next == NULL
461 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
462 == fragP->fr_fix));
463
464 /* After md_convert_frag, we make the frag into a ".space 0".
465 md_convert_frag() should set up any fixSs and constants
466 required. */
467 frag_wane (fragP);
468 break;
469
470 #ifndef WORKING_DOT_WORD
471 case rs_broken_word:
472 {
473 struct broken_word *lie;
474
475 if (fragP->fr_subtype)
476 {
477 fragP->fr_fix += md_short_jump_size;
478 for (lie = (struct broken_word *) (fragP->fr_symbol);
479 lie && lie->dispfrag == fragP;
480 lie = lie->next_broken_word)
481 if (lie->added == 1)
482 fragP->fr_fix += md_long_jump_size;
483 }
484 frag_wane (fragP);
485 }
486 break;
487 #endif
488
489 default:
490 BAD_CASE (fragP->fr_type);
491 break;
492 }
493 #ifdef md_frag_check
494 md_frag_check (fragP);
495 #endif
496 }
497
498 struct relax_seg_info
499 {
500 int pass;
501 int changed;
502 };
503
504 static void
505 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
506 {
507 segment_info_type *seginfo = seg_info (sec);
508 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
509
510 if (seginfo && seginfo->frchainP
511 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
512 info->changed = 1;
513 }
514
515 static void
516 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
517 {
518 flagword flags;
519 fragS *fragp;
520 segment_info_type *seginfo;
521 int x;
522 valueT size, newsize;
523
524 subseg_change (sec, 0);
525
526 seginfo = seg_info (sec);
527 if (seginfo && seginfo->frchainP)
528 {
529 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
530 cvt_frag_to_fill (sec, fragp);
531 for (fragp = seginfo->frchainP->frch_root;
532 fragp->fr_next;
533 fragp = fragp->fr_next)
534 /* Walk to last elt. */
535 ;
536 size = fragp->fr_address + fragp->fr_fix;
537 }
538 else
539 size = 0;
540
541 flags = bfd_get_section_flags (abfd, sec);
542
543 if (size > 0 && ! seginfo->bss)
544 flags |= SEC_HAS_CONTENTS;
545
546 /* @@ This is just an approximation. */
547 if (seginfo && seginfo->fix_root)
548 flags |= SEC_RELOC;
549 else
550 flags &= ~SEC_RELOC;
551 x = bfd_set_section_flags (abfd, sec, flags);
552 assert (x);
553
554 newsize = md_section_align (sec, size);
555 x = bfd_set_section_size (abfd, sec, newsize);
556 assert (x);
557
558 /* If the size had to be rounded up, add some padding in the last
559 non-empty frag. */
560 assert (newsize >= size);
561 if (size != newsize)
562 {
563 fragS *last = seginfo->frchainP->frch_last;
564 fragp = seginfo->frchainP->frch_root;
565 while (fragp->fr_next != last)
566 fragp = fragp->fr_next;
567 last->fr_address = size;
568 if ((newsize - size) % fragp->fr_var == 0)
569 fragp->fr_offset += (newsize - size) / fragp->fr_var;
570 else
571 /* If we hit this abort, it's likely due to subsegs_finish not
572 providing sufficient alignment on the last frag, and the
573 machine dependent code using alignment frags with fr_var
574 greater than 1. */
575 abort ();
576 }
577
578 #ifdef tc_frob_section
579 tc_frob_section (sec);
580 #endif
581 #ifdef obj_frob_section
582 obj_frob_section (sec);
583 #endif
584 }
585
586 #ifdef DEBUG2
587 static void
588 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
589 {
590 segment_info_type *seginfo = seg_info (sec);
591 fixS *fixp = seginfo->fix_root;
592
593 if (!fixp)
594 return;
595
596 fprintf (stream, "sec %s relocs:\n", sec->name);
597 while (fixp)
598 {
599 symbolS *s = fixp->fx_addsy;
600
601 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
602 (int) fixp->fx_r_type);
603 if (s == NULL)
604 fprintf (stream, "no sym\n");
605 else
606 {
607 print_symbol_value_1 (stream, s);
608 fprintf (stream, "\n");
609 }
610 fixp = fixp->fx_next;
611 }
612 }
613 #else
614 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
615 #endif
616
617 #ifndef EMIT_SECTION_SYMBOLS
618 #define EMIT_SECTION_SYMBOLS 1
619 #endif
620
621 /* This pass over fixups decides whether symbols can be replaced with
622 section symbols. */
623
624 static void
625 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
626 asection *sec,
627 void *xxx ATTRIBUTE_UNUSED)
628 {
629 segment_info_type *seginfo = seg_info (sec);
630 fixS *fixp;
631
632 if (seginfo == NULL)
633 return;
634
635 dump_section_relocs (abfd, sec, stderr);
636
637 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
638 if (fixp->fx_done)
639 /* Ignore it. */
640 ;
641 else if (fixp->fx_addsy)
642 {
643 symbolS *sym;
644 asection *symsec;
645
646 #ifdef DEBUG5
647 fprintf (stderr, "\n\nadjusting fixup:\n");
648 print_fixup (fixp);
649 #endif
650
651 sym = fixp->fx_addsy;
652
653 /* All symbols should have already been resolved at this
654 point. It is possible to see unresolved expression
655 symbols, though, since they are not in the regular symbol
656 table. */
657 resolve_symbol_value (sym);
658
659 if (fixp->fx_subsy != NULL)
660 resolve_symbol_value (fixp->fx_subsy);
661
662 /* If this symbol is equated to an undefined or common symbol,
663 convert the fixup to being against that symbol. */
664 while (symbol_equated_reloc_p (sym)
665 || S_IS_WEAKREFR (sym))
666 {
667 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
668 if (sym == newsym)
669 break;
670 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
671 fixp->fx_addsy = newsym;
672 sym = newsym;
673 }
674
675 if (symbol_mri_common_p (sym))
676 {
677 fixp->fx_offset += S_GET_VALUE (sym);
678 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
679 continue;
680 }
681
682 /* If the symbol is undefined, common, weak, or global (ELF
683 shared libs), we can't replace it with the section symbol. */
684 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
685 continue;
686
687 /* Is there some other (target cpu dependent) reason we can't adjust
688 this one? (E.g. relocations involving function addresses on
689 the PA. */
690 #ifdef tc_fix_adjustable
691 if (! tc_fix_adjustable (fixp))
692 continue;
693 #endif
694
695 /* Since we're reducing to section symbols, don't attempt to reduce
696 anything that's already using one. */
697 if (symbol_section_p (sym))
698 continue;
699
700 symsec = S_GET_SEGMENT (sym);
701 if (symsec == NULL)
702 abort ();
703
704 if (bfd_is_abs_section (symsec))
705 {
706 /* The fixup_segment routine normally will not use this
707 symbol in a relocation. */
708 continue;
709 }
710
711 /* Don't try to reduce relocs which refer to non-local symbols
712 in .linkonce sections. It can lead to confusion when a
713 debugging section refers to a .linkonce section. I hope
714 this will always be correct. */
715 if (symsec != sec && ! S_IS_LOCAL (sym))
716 {
717 if ((symsec->flags & SEC_LINK_ONCE) != 0
718 || (IS_ELF
719 /* The GNU toolchain uses an extension for ELF: a
720 section beginning with the magic string
721 .gnu.linkonce is a linkonce section. */
722 && strncmp (segment_name (symsec), ".gnu.linkonce",
723 sizeof ".gnu.linkonce" - 1) == 0))
724 continue;
725 }
726
727 /* Never adjust a reloc against local symbol in a merge section
728 with non-zero addend. */
729 if ((symsec->flags & SEC_MERGE) != 0
730 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
731 continue;
732
733 /* Never adjust a reloc against TLS local symbol. */
734 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
735 continue;
736
737 /* We refetch the segment when calling section_symbol, rather
738 than using symsec, because S_GET_VALUE may wind up changing
739 the section when it calls resolve_symbol_value. */
740 fixp->fx_offset += S_GET_VALUE (sym);
741 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
742 #ifdef DEBUG5
743 fprintf (stderr, "\nadjusted fixup:\n");
744 print_fixup (fixp);
745 #endif
746 }
747
748 dump_section_relocs (abfd, sec, stderr);
749 }
750
751 /* fixup_segment()
752
753 Go through all the fixS's in a segment and see which ones can be
754 handled now. (These consist of fixS where we have since discovered
755 the value of a symbol, or the address of the frag involved.)
756 For each one, call md_apply_fix to put the fix into the frag data.
757
758 Result is a count of how many relocation structs will be needed to
759 handle the remaining fixS's that we couldn't completely handle here.
760 These will be output later by emit_relocations(). */
761
762 static long
763 fixup_segment (fixS *fixP, segT this_segment)
764 {
765 long seg_reloc_count = 0;
766 valueT add_number;
767 fragS *fragP;
768 segT add_symbol_segment = absolute_section;
769
770 if (fixP != NULL && abs_section_sym == NULL)
771 abs_section_sym = section_symbol (absolute_section);
772
773 /* If the linker is doing the relaxing, we must not do any fixups.
774
775 Well, strictly speaking that's not true -- we could do any that
776 are PC-relative and don't cross regions that could change size.
777 And for the i960 we might be able to turn callx/callj into bal
778 anyways in cases where we know the maximum displacement. */
779 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
780 {
781 for (; fixP; fixP = fixP->fx_next)
782 if (!fixP->fx_done)
783 {
784 if (fixP->fx_addsy == NULL)
785 {
786 /* There was no symbol required by this relocation.
787 However, BFD doesn't really handle relocations
788 without symbols well. So fake up a local symbol in
789 the absolute section. */
790 fixP->fx_addsy = abs_section_sym;
791 }
792 symbol_mark_used_in_reloc (fixP->fx_addsy);
793 if (fixP->fx_subsy != NULL)
794 symbol_mark_used_in_reloc (fixP->fx_subsy);
795 seg_reloc_count++;
796 }
797 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
798 return seg_reloc_count;
799 }
800
801 for (; fixP; fixP = fixP->fx_next)
802 {
803 #ifdef DEBUG5
804 fprintf (stderr, "\nprocessing fixup:\n");
805 print_fixup (fixP);
806 #endif
807
808 fragP = fixP->fx_frag;
809 know (fragP);
810 #ifdef TC_VALIDATE_FIX
811 TC_VALIDATE_FIX (fixP, this_segment, skip);
812 #endif
813 add_number = fixP->fx_offset;
814
815 if (fixP->fx_addsy != NULL)
816 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
817
818 if (fixP->fx_subsy != NULL)
819 {
820 segT sub_symbol_segment;
821 resolve_symbol_value (fixP->fx_subsy);
822 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
823 if (fixP->fx_addsy != NULL
824 && sub_symbol_segment == add_symbol_segment
825 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
826 {
827 add_number += S_GET_VALUE (fixP->fx_addsy);
828 add_number -= S_GET_VALUE (fixP->fx_subsy);
829 fixP->fx_offset = add_number;
830 fixP->fx_addsy = NULL;
831 fixP->fx_subsy = NULL;
832 #ifdef TC_M68K
833 /* See the comment below about 68k weirdness. */
834 fixP->fx_pcrel = 0;
835 #endif
836 }
837 else if (sub_symbol_segment == absolute_section
838 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
839 {
840 add_number -= S_GET_VALUE (fixP->fx_subsy);
841 fixP->fx_offset = add_number;
842 fixP->fx_subsy = NULL;
843 }
844 else if (sub_symbol_segment == this_segment
845 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
846 {
847 add_number -= S_GET_VALUE (fixP->fx_subsy);
848 fixP->fx_offset = (add_number + fixP->fx_dot_value
849 + fixP->fx_frag->fr_address);
850
851 /* Make it pc-relative. If the back-end code has not
852 selected a pc-relative reloc, cancel the adjustment
853 we do later on all pc-relative relocs. */
854 if (0
855 #ifdef TC_M68K
856 /* Do this for m68k even if it's already described
857 as pc-relative. On the m68k, an operand of
858 "pc@(foo-.-2)" should address "foo" in a
859 pc-relative mode. */
860 || 1
861 #endif
862 || !fixP->fx_pcrel)
863 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
864 fixP->fx_subsy = NULL;
865 fixP->fx_pcrel = 1;
866 }
867 else if (!TC_VALIDATE_FIX_SUB (fixP))
868 {
869 as_bad_where (fixP->fx_file, fixP->fx_line,
870 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
871 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
872 segment_name (add_symbol_segment),
873 S_GET_NAME (fixP->fx_subsy),
874 segment_name (sub_symbol_segment));
875 }
876 }
877
878 if (fixP->fx_addsy)
879 {
880 if (add_symbol_segment == this_segment
881 && !TC_FORCE_RELOCATION_LOCAL (fixP))
882 {
883 /* This fixup was made when the symbol's segment was
884 SEG_UNKNOWN, but it is now in the local segment.
885 So we know how to do the address without relocation. */
886 add_number += S_GET_VALUE (fixP->fx_addsy);
887 fixP->fx_offset = add_number;
888 if (fixP->fx_pcrel)
889 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
890 fixP->fx_addsy = NULL;
891 fixP->fx_pcrel = 0;
892 }
893 else if (add_symbol_segment == absolute_section
894 && !TC_FORCE_RELOCATION_ABS (fixP))
895 {
896 add_number += S_GET_VALUE (fixP->fx_addsy);
897 fixP->fx_offset = add_number;
898 fixP->fx_addsy = NULL;
899 }
900 else if (add_symbol_segment != undefined_section
901 && ! bfd_is_com_section (add_symbol_segment)
902 && MD_APPLY_SYM_VALUE (fixP))
903 add_number += S_GET_VALUE (fixP->fx_addsy);
904 }
905
906 if (fixP->fx_pcrel)
907 {
908 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
909 if (!fixP->fx_done && fixP->fx_addsy == NULL)
910 {
911 /* There was no symbol required by this relocation.
912 However, BFD doesn't really handle relocations
913 without symbols well. So fake up a local symbol in
914 the absolute section. */
915 fixP->fx_addsy = abs_section_sym;
916 }
917 }
918
919 if (!fixP->fx_done)
920 md_apply_fix (fixP, &add_number, this_segment);
921
922 if (!fixP->fx_done)
923 {
924 ++seg_reloc_count;
925 if (fixP->fx_addsy == NULL)
926 fixP->fx_addsy = abs_section_sym;
927 symbol_mark_used_in_reloc (fixP->fx_addsy);
928 if (fixP->fx_subsy != NULL)
929 symbol_mark_used_in_reloc (fixP->fx_subsy);
930 }
931
932 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
933 {
934 if (fixP->fx_size < sizeof (valueT))
935 {
936 valueT mask;
937
938 mask = 0;
939 mask--; /* Set all bits to one. */
940 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
941 if ((add_number & mask) != 0 && (add_number & mask) != mask)
942 {
943 char buf[50], buf2[50];
944 sprint_value (buf, fragP->fr_address + fixP->fx_where);
945 if (add_number > 1000)
946 sprint_value (buf2, add_number);
947 else
948 sprintf (buf2, "%ld", (long) add_number);
949 as_bad_where (fixP->fx_file, fixP->fx_line,
950 _("value of %s too large for field of %d bytes at %s"),
951 buf2, fixP->fx_size, buf);
952 } /* Generic error checking. */
953 }
954 #ifdef WARN_SIGNED_OVERFLOW_WORD
955 /* Warn if a .word value is too large when treated as a signed
956 number. We already know it is not too negative. This is to
957 catch over-large switches generated by gcc on the 68k. */
958 if (!flag_signed_overflow_ok
959 && fixP->fx_size == 2
960 && add_number > 0x7fff)
961 as_bad_where (fixP->fx_file, fixP->fx_line,
962 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
963 (long) add_number,
964 (long) (fragP->fr_address + fixP->fx_where));
965 #endif
966 } /* Not a bit fix. */
967
968 #ifdef TC_VALIDATE_FIX
969 skip: ATTRIBUTE_UNUSED_LABEL
970 ;
971 #endif
972 #ifdef DEBUG5
973 fprintf (stderr, "result:\n");
974 print_fixup (fixP);
975 #endif
976 } /* For each fixS in this segment. */
977
978 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
979 return seg_reloc_count;
980 }
981
982 static void
983 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
984 asection *sec,
985 void *xxx ATTRIBUTE_UNUSED)
986 {
987 segment_info_type *seginfo = seg_info (sec);
988
989 fixup_segment (seginfo->fix_root, sec);
990 }
991
992 static void
993 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
994 {
995 segment_info_type *seginfo = seg_info (sec);
996 unsigned int i;
997 unsigned int n;
998 arelent **relocs;
999 fixS *fixp;
1000 char *err;
1001
1002 /* If seginfo is NULL, we did not create this section; don't do
1003 anything with it. */
1004 if (seginfo == NULL)
1005 return;
1006
1007 n = 0;
1008 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1009 n++;
1010
1011 #ifndef RELOC_EXPANSION_POSSIBLE
1012 /* Set up reloc information as well. */
1013 relocs = xcalloc (n, sizeof (arelent *));
1014
1015 i = 0;
1016 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1017 {
1018 arelent *reloc;
1019 bfd_reloc_status_type s;
1020 symbolS *sym;
1021
1022 if (fixp->fx_done)
1023 {
1024 n--;
1025 continue;
1026 }
1027
1028 reloc = tc_gen_reloc (sec, fixp);
1029 if (!reloc)
1030 {
1031 n--;
1032 continue;
1033 }
1034
1035 /*
1036 This test is triggered inappropriately for the SH:
1037 if (fixp->fx_where + fixp->fx_size
1038 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1039 abort ();
1040 */
1041
1042 s = bfd_install_relocation (stdoutput, reloc,
1043 fixp->fx_frag->fr_literal,
1044 fixp->fx_frag->fr_address,
1045 sec, &err);
1046 switch (s)
1047 {
1048 case bfd_reloc_ok:
1049 break;
1050 case bfd_reloc_overflow:
1051 as_bad_where (fixp->fx_file, fixp->fx_line,
1052 _("relocation overflow"));
1053 break;
1054 case bfd_reloc_outofrange:
1055 as_bad_where (fixp->fx_file, fixp->fx_line,
1056 _("relocation out of range"));
1057 break;
1058 default:
1059 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1060 fixp->fx_file, fixp->fx_line, s);
1061 }
1062 relocs[i++] = reloc;
1063 }
1064 #else
1065 n = n * MAX_RELOC_EXPANSION;
1066 /* Set up reloc information as well. */
1067 relocs = xcalloc (n, sizeof (arelent *));
1068
1069 i = 0;
1070 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1071 {
1072 arelent **reloc;
1073 bfd_reloc_status_type s;
1074 symbolS *sym;
1075 int j;
1076
1077 if (fixp->fx_done)
1078 {
1079 n--;
1080 continue;
1081 }
1082
1083 reloc = tc_gen_reloc (sec, fixp);
1084
1085 for (j = 0; reloc[j]; j++)
1086 {
1087 relocs[i++] = reloc[j];
1088 assert (i <= n);
1089 }
1090 if (fixp->fx_where + fixp->fx_size
1091 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1092 as_bad_where (fixp->fx_file, fixp->fx_line,
1093 _("internal error: fixup not contained within frag"));
1094 for (j = 0; reloc[j]; j++)
1095 {
1096 s = bfd_install_relocation (stdoutput, reloc[j],
1097 fixp->fx_frag->fr_literal,
1098 fixp->fx_frag->fr_address,
1099 sec, &err);
1100 switch (s)
1101 {
1102 case bfd_reloc_ok:
1103 break;
1104 case bfd_reloc_overflow:
1105 as_bad_where (fixp->fx_file, fixp->fx_line,
1106 _("relocation overflow"));
1107 break;
1108 case bfd_reloc_outofrange:
1109 as_bad_where (fixp->fx_file, fixp->fx_line,
1110 _("relocation out of range"));
1111 break;
1112 default:
1113 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1114 fixp->fx_file, fixp->fx_line, s);
1115 }
1116 }
1117 }
1118 n = i;
1119 #endif
1120
1121 #ifdef DEBUG4
1122 {
1123 unsigned int i, j, nsyms;
1124 asymbol **sympp;
1125 sympp = bfd_get_outsymbols (stdoutput);
1126 nsyms = bfd_get_symcount (stdoutput);
1127 for (i = 0; i < n; i++)
1128 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1129 {
1130 for (j = 0; j < nsyms; j++)
1131 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1132 break;
1133 if (j == nsyms)
1134 abort ();
1135 }
1136 }
1137 #endif
1138
1139 if (n)
1140 bfd_set_reloc (stdoutput, sec, relocs, n);
1141 else
1142 bfd_set_section_flags (abfd, sec,
1143 (bfd_get_section_flags (abfd, sec)
1144 & (flagword) ~SEC_RELOC));
1145
1146 #ifdef SET_SECTION_RELOCS
1147 SET_SECTION_RELOCS (sec, relocs, n);
1148 #endif
1149
1150 #ifdef DEBUG3
1151 {
1152 unsigned int i;
1153 arelent *r;
1154 asymbol *s;
1155 fprintf (stderr, "relocs for sec %s\n", sec->name);
1156 for (i = 0; i < n; i++)
1157 {
1158 r = relocs[i];
1159 s = *r->sym_ptr_ptr;
1160 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1161 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1162 }
1163 }
1164 #endif
1165 }
1166
1167 static void
1168 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1169 asection *sec,
1170 void *xxx ATTRIBUTE_UNUSED)
1171 {
1172 segment_info_type *seginfo = seg_info (sec);
1173 addressT offset = 0;
1174 fragS *f;
1175
1176 /* Write out the frags. */
1177 if (seginfo == NULL
1178 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1179 return;
1180
1181 for (f = seginfo->frchainP->frch_root;
1182 f;
1183 f = f->fr_next)
1184 {
1185 int x;
1186 addressT fill_size;
1187 char *fill_literal;
1188 offsetT count;
1189
1190 assert (f->fr_type == rs_fill);
1191 if (f->fr_fix)
1192 {
1193 x = bfd_set_section_contents (stdoutput, sec,
1194 f->fr_literal, (file_ptr) offset,
1195 (bfd_size_type) f->fr_fix);
1196 if (!x)
1197 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1198 bfd_errmsg (bfd_get_error ()));
1199 offset += f->fr_fix;
1200 }
1201 fill_literal = f->fr_literal + f->fr_fix;
1202 fill_size = f->fr_var;
1203 count = f->fr_offset;
1204 assert (count >= 0);
1205 if (fill_size && count)
1206 {
1207 char buf[256];
1208 if (fill_size > sizeof (buf))
1209 {
1210 /* Do it the old way. Can this ever happen? */
1211 while (count--)
1212 {
1213 x = bfd_set_section_contents (stdoutput, sec,
1214 fill_literal,
1215 (file_ptr) offset,
1216 (bfd_size_type) fill_size);
1217 if (!x)
1218 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1219 bfd_errmsg (bfd_get_error ()));
1220 offset += fill_size;
1221 }
1222 }
1223 else
1224 {
1225 /* Build a buffer full of fill objects and output it as
1226 often as necessary. This saves on the overhead of
1227 potentially lots of bfd_set_section_contents calls. */
1228 int n_per_buf, i;
1229 if (fill_size == 1)
1230 {
1231 n_per_buf = sizeof (buf);
1232 memset (buf, *fill_literal, n_per_buf);
1233 }
1234 else
1235 {
1236 char *bufp;
1237 n_per_buf = sizeof (buf) / fill_size;
1238 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1239 memcpy (bufp, fill_literal, fill_size);
1240 }
1241 for (; count > 0; count -= n_per_buf)
1242 {
1243 n_per_buf = n_per_buf > count ? count : n_per_buf;
1244 x = bfd_set_section_contents
1245 (stdoutput, sec, buf, (file_ptr) offset,
1246 (bfd_size_type) n_per_buf * fill_size);
1247 if (!x)
1248 as_fatal (_("cannot write to output file"));
1249 offset += n_per_buf * fill_size;
1250 }
1251 }
1252 }
1253 }
1254 }
1255
1256 static void
1257 merge_data_into_text (void)
1258 {
1259 seg_info (text_section)->frchainP->frch_last->fr_next =
1260 seg_info (data_section)->frchainP->frch_root;
1261 seg_info (text_section)->frchainP->frch_last =
1262 seg_info (data_section)->frchainP->frch_last;
1263 seg_info (data_section)->frchainP = 0;
1264 }
1265
1266 static void
1267 set_symtab (void)
1268 {
1269 int nsyms;
1270 asymbol **asympp;
1271 symbolS *symp;
1272 bfd_boolean result;
1273
1274 /* Count symbols. We can't rely on a count made by the loop in
1275 write_object_file, because *_frob_file may add a new symbol or
1276 two. */
1277 nsyms = 0;
1278 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1279 nsyms++;
1280
1281 if (nsyms)
1282 {
1283 int i;
1284 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1285
1286 asympp = bfd_alloc (stdoutput, amt);
1287 symp = symbol_rootP;
1288 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1289 {
1290 asympp[i] = symbol_get_bfdsym (symp);
1291 symbol_mark_written (symp);
1292 }
1293 }
1294 else
1295 asympp = 0;
1296 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1297 assert (result);
1298 symbol_table_frozen = 1;
1299 }
1300
1301 /* Finish the subsegments. After every sub-segment, we fake an
1302 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1303 ".fill 0" because that is the kind of frag that requires least
1304 thought. ".align" frags like to have a following frag since that
1305 makes calculating their intended length trivial. */
1306
1307 #ifndef SUB_SEGMENT_ALIGN
1308 #ifdef HANDLE_ALIGN
1309 /* The last subsegment gets an alignment corresponding to the alignment
1310 of the section. This allows proper nop-filling at the end of
1311 code-bearing sections. */
1312 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1313 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1314 #else
1315 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1316 #endif
1317 #endif
1318
1319 void
1320 subsegs_finish (void)
1321 {
1322 struct frchain *frchainP;
1323 asection *s;
1324
1325 for (s = stdoutput->sections; s; s = s->next)
1326 {
1327 segment_info_type *seginfo = seg_info (s);
1328 if (!seginfo)
1329 continue;
1330
1331 for (frchainP = seginfo->frchainP;
1332 frchainP != NULL;
1333 frchainP = frchainP->frch_next)
1334 {
1335 int alignment = 0;
1336
1337 subseg_set (s, frchainP->frch_subseg);
1338
1339 /* This now gets called even if we had errors. In that case,
1340 any alignment is meaningless, and, moreover, will look weird
1341 if we are generating a listing. */
1342 if (!had_errors ())
1343 {
1344 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1345 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1346 && now_seg->entsize)
1347 {
1348 unsigned int entsize = now_seg->entsize;
1349 int entalign = 0;
1350
1351 while ((entsize & 1) == 0)
1352 {
1353 ++entalign;
1354 entsize >>= 1;
1355 }
1356 if (entalign > alignment)
1357 alignment = entalign;
1358 }
1359 }
1360
1361 if (subseg_text_p (now_seg))
1362 frag_align_code (alignment, 0);
1363 else
1364 frag_align (alignment, 0, 0);
1365
1366 /* frag_align will have left a new frag.
1367 Use this last frag for an empty ".fill".
1368
1369 For this segment ...
1370 Create a last frag. Do not leave a "being filled in frag". */
1371 frag_wane (frag_now);
1372 frag_now->fr_fix = 0;
1373 know (frag_now->fr_next == NULL);
1374 }
1375 }
1376 }
1377
1378 /* Write the object file. */
1379
1380 void
1381 write_object_file (void)
1382 {
1383 struct relax_seg_info rsi;
1384 #ifndef WORKING_DOT_WORD
1385 fragS *fragP; /* Track along all frags. */
1386 #endif
1387
1388 /* Do we really want to write it? */
1389 {
1390 int n_warns, n_errs;
1391 n_warns = had_warnings ();
1392 n_errs = had_errors ();
1393 /* The -Z flag indicates that an object file should be generated,
1394 regardless of warnings and errors. */
1395 if (flag_always_generate_output)
1396 {
1397 if (n_warns || n_errs)
1398 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1399 n_errs, n_errs == 1 ? "" : "s",
1400 n_warns, n_warns == 1 ? "" : "s");
1401 }
1402 else
1403 {
1404 if (n_errs)
1405 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1406 n_errs, n_errs == 1 ? "" : "s",
1407 n_warns, n_warns == 1 ? "" : "s");
1408 }
1409 }
1410
1411 #ifdef OBJ_VMS
1412 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1413 a routine to check for the definition of the procedure "_main",
1414 and if so -- fix it up so that it can be program entry point. */
1415 vms_check_for_main ();
1416 #endif /* OBJ_VMS */
1417
1418 /* From now on, we don't care about sub-segments. Build one frag chain
1419 for each segment. Linked thru fr_next. */
1420
1421 /* Remove the sections created by gas for its own purposes. */
1422 {
1423 int i;
1424
1425 bfd_section_list_remove (stdoutput, reg_section);
1426 bfd_section_list_remove (stdoutput, expr_section);
1427 stdoutput->section_count -= 2;
1428 i = 0;
1429 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1430 }
1431
1432 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1433
1434 /* We have two segments. If user gave -R flag, then we must put the
1435 data frags into the text segment. Do this before relaxing so
1436 we know to take advantage of -R and make shorter addresses. */
1437 if (flag_readonly_data_in_text)
1438 {
1439 merge_data_into_text ();
1440 }
1441
1442 rsi.pass = 0;
1443 while (1)
1444 {
1445 #ifndef WORKING_DOT_WORD
1446 /* We need to reset the markers in the broken word list and
1447 associated frags between calls to relax_segment (via
1448 relax_seg). Since the broken word list is global, we do it
1449 once per round, rather than locally in relax_segment for each
1450 segment. */
1451 struct broken_word *brokp;
1452
1453 for (brokp = broken_words;
1454 brokp != (struct broken_word *) NULL;
1455 brokp = brokp->next_broken_word)
1456 {
1457 brokp->added = 0;
1458
1459 if (brokp->dispfrag != (fragS *) NULL
1460 && brokp->dispfrag->fr_type == rs_broken_word)
1461 brokp->dispfrag->fr_subtype = 0;
1462 }
1463 #endif
1464
1465 rsi.changed = 0;
1466 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1467 rsi.pass++;
1468 if (!rsi.changed)
1469 break;
1470 }
1471
1472 /* Note - Most ports will use the default value of
1473 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1474 local symbols to be resolved, removing their frag information.
1475 Some ports however, will not have finished relaxing all of
1476 their frags and will still need the local symbol frag
1477 information. These ports can set
1478 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1479 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1480
1481 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1482
1483 /* Relaxation has completed. Freeze all syms. */
1484 finalize_syms = 1;
1485
1486 #ifdef md_post_relax_hook
1487 md_post_relax_hook;
1488 #endif
1489
1490 #ifndef WORKING_DOT_WORD
1491 {
1492 struct broken_word *lie;
1493 struct broken_word **prevP;
1494
1495 prevP = &broken_words;
1496 for (lie = broken_words; lie; lie = lie->next_broken_word)
1497 if (!lie->added)
1498 {
1499 expressionS exp;
1500
1501 subseg_change (lie->seg, lie->subseg);
1502 exp.X_op = O_subtract;
1503 exp.X_add_symbol = lie->add;
1504 exp.X_op_symbol = lie->sub;
1505 exp.X_add_number = lie->addnum;
1506 #ifdef TC_CONS_FIX_NEW
1507 TC_CONS_FIX_NEW (lie->frag,
1508 lie->word_goes_here - lie->frag->fr_literal,
1509 2, &exp);
1510 #else
1511 fix_new_exp (lie->frag,
1512 lie->word_goes_here - lie->frag->fr_literal,
1513 2, &exp, 0, BFD_RELOC_16);
1514 #endif
1515 *prevP = lie->next_broken_word;
1516 }
1517 else
1518 prevP = &(lie->next_broken_word);
1519
1520 for (lie = broken_words; lie;)
1521 {
1522 struct broken_word *untruth;
1523 char *table_ptr;
1524 addressT table_addr;
1525 addressT from_addr, to_addr;
1526 int n, m;
1527
1528 subseg_change (lie->seg, lie->subseg);
1529 fragP = lie->dispfrag;
1530
1531 /* Find out how many broken_words go here. */
1532 n = 0;
1533 for (untruth = lie;
1534 untruth && untruth->dispfrag == fragP;
1535 untruth = untruth->next_broken_word)
1536 if (untruth->added == 1)
1537 n++;
1538
1539 table_ptr = lie->dispfrag->fr_opcode;
1540 table_addr = (lie->dispfrag->fr_address
1541 + (table_ptr - lie->dispfrag->fr_literal));
1542 /* Create the jump around the long jumps. This is a short
1543 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1544 from_addr = table_addr;
1545 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1546 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1547 lie->add);
1548 table_ptr += md_short_jump_size;
1549 table_addr += md_short_jump_size;
1550
1551 for (m = 0;
1552 lie && lie->dispfrag == fragP;
1553 m++, lie = lie->next_broken_word)
1554 {
1555 if (lie->added == 2)
1556 continue;
1557 /* Patch the jump table. */
1558 /* This is the offset from ??? to table_ptr+0. */
1559 to_addr = table_addr - S_GET_VALUE (lie->sub);
1560 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1561 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1562 #endif
1563 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1564 for (untruth = lie->next_broken_word;
1565 untruth && untruth->dispfrag == fragP;
1566 untruth = untruth->next_broken_word)
1567 {
1568 if (untruth->use_jump == lie)
1569 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1570 }
1571
1572 /* Install the long jump. */
1573 /* This is a long jump from table_ptr+0 to the final target. */
1574 from_addr = table_addr;
1575 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1576 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1577 lie->add);
1578 table_ptr += md_long_jump_size;
1579 table_addr += md_long_jump_size;
1580 }
1581 }
1582 }
1583 #endif /* not WORKING_DOT_WORD */
1584
1585 /* Resolve symbol values. This needs to be done before processing
1586 the relocations. */
1587 if (symbol_rootP)
1588 {
1589 symbolS *symp;
1590
1591 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1592 resolve_symbol_value (symp);
1593 }
1594 resolve_local_symbol_values ();
1595
1596 PROGRESS (1);
1597
1598 #ifdef tc_frob_file_before_adjust
1599 tc_frob_file_before_adjust ();
1600 #endif
1601 #ifdef obj_frob_file_before_adjust
1602 obj_frob_file_before_adjust ();
1603 #endif
1604
1605 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1606
1607 #ifdef tc_frob_file_before_fix
1608 tc_frob_file_before_fix ();
1609 #endif
1610 #ifdef obj_frob_file_before_fix
1611 obj_frob_file_before_fix ();
1612 #endif
1613
1614 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1615
1616 /* Set up symbol table, and write it out. */
1617 if (symbol_rootP)
1618 {
1619 symbolS *symp;
1620 bfd_boolean skip_next_symbol = FALSE;
1621
1622 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1623 {
1624 int punt = 0;
1625 const char *name;
1626
1627 if (skip_next_symbol)
1628 {
1629 /* Don't do anything besides moving the value of the
1630 symbol from the GAS value-field to the BFD value-field. */
1631 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1632 skip_next_symbol = FALSE;
1633 continue;
1634 }
1635
1636 if (symbol_mri_common_p (symp))
1637 {
1638 if (S_IS_EXTERNAL (symp))
1639 as_bad (_("%s: global symbols not supported in common sections"),
1640 S_GET_NAME (symp));
1641 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1642 continue;
1643 }
1644
1645 name = S_GET_NAME (symp);
1646 if (name)
1647 {
1648 const char *name2 =
1649 decode_local_label_name ((char *) S_GET_NAME (symp));
1650 /* They only differ if `name' is a fb or dollar local
1651 label name. */
1652 if (name2 != name && ! S_IS_DEFINED (symp))
1653 as_bad (_("local label `%s' is not defined"), name2);
1654 }
1655
1656 /* Do it again, because adjust_reloc_syms might introduce
1657 more symbols. They'll probably only be section symbols,
1658 but they'll still need to have the values computed. */
1659 resolve_symbol_value (symp);
1660
1661 /* Skip symbols which were equated to undefined or common
1662 symbols. */
1663 if (symbol_equated_reloc_p (symp)
1664 || S_IS_WEAKREFR (symp))
1665 {
1666 const char *name = S_GET_NAME (symp);
1667 if (S_IS_COMMON (symp)
1668 && !TC_FAKE_LABEL (name)
1669 && !S_IS_WEAKREFR (symp)
1670 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1671 {
1672 expressionS *e = symbol_get_value_expression (symp);
1673 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1674 name, S_GET_NAME (e->X_add_symbol));
1675 }
1676 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1677 continue;
1678 }
1679
1680 #ifdef obj_frob_symbol
1681 obj_frob_symbol (symp, punt);
1682 #endif
1683 #ifdef tc_frob_symbol
1684 if (! punt || symbol_used_in_reloc_p (symp))
1685 tc_frob_symbol (symp, punt);
1686 #endif
1687
1688 /* If we don't want to keep this symbol, splice it out of
1689 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1690 want section symbols. Otherwise, we skip local symbols
1691 and symbols that the frob_symbol macros told us to punt,
1692 but we keep such symbols if they are used in relocs. */
1693 if (symp == abs_section_sym
1694 || (! EMIT_SECTION_SYMBOLS
1695 && symbol_section_p (symp))
1696 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1697 opposites. Sometimes the former checks flags and the
1698 latter examines the name... */
1699 || (!S_IS_EXTERNAL (symp)
1700 && (punt || S_IS_LOCAL (symp) ||
1701 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1702 && ! symbol_used_in_reloc_p (symp)))
1703 {
1704 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1705
1706 /* After symbol_remove, symbol_next(symp) still returns
1707 the one that came after it in the chain. So we don't
1708 need to do any extra cleanup work here. */
1709 continue;
1710 }
1711
1712 /* Make sure we really got a value for the symbol. */
1713 if (! symbol_resolved_p (symp))
1714 {
1715 as_bad (_("can't resolve value for symbol `%s'"),
1716 S_GET_NAME (symp));
1717 symbol_mark_resolved (symp);
1718 }
1719
1720 /* Set the value into the BFD symbol. Up til now the value
1721 has only been kept in the gas symbolS struct. */
1722 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1723
1724 /* A warning construct is a warning symbol followed by the
1725 symbol warned about. Don't let anything object-format or
1726 target-specific muck with it; it's ready for output. */
1727 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1728 skip_next_symbol = TRUE;
1729 }
1730 }
1731
1732 PROGRESS (1);
1733
1734 /* Now do any format-specific adjustments to the symbol table, such
1735 as adding file symbols. */
1736 #ifdef tc_adjust_symtab
1737 tc_adjust_symtab ();
1738 #endif
1739 #ifdef obj_adjust_symtab
1740 obj_adjust_symtab ();
1741 #endif
1742
1743 /* Now that all the sizes are known, and contents correct, we can
1744 start writing to the file. */
1745 set_symtab ();
1746
1747 /* If *_frob_file changes the symbol value at this point, it is
1748 responsible for moving the changed value into symp->bsym->value
1749 as well. Hopefully all symbol value changing can be done in
1750 *_frob_symbol. */
1751 #ifdef tc_frob_file
1752 tc_frob_file ();
1753 #endif
1754 #ifdef obj_frob_file
1755 obj_frob_file ();
1756 #endif
1757
1758 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1759
1760 #ifdef tc_frob_file_after_relocs
1761 tc_frob_file_after_relocs ();
1762 #endif
1763 #ifdef obj_frob_file_after_relocs
1764 obj_frob_file_after_relocs ();
1765 #endif
1766
1767 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1768 }
1769
1770 #ifdef TC_GENERIC_RELAX_TABLE
1771 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1772
1773 long
1774 relax_frag (segT segment, fragS *fragP, long stretch)
1775 {
1776 const relax_typeS *this_type;
1777 const relax_typeS *start_type;
1778 relax_substateT next_state;
1779 relax_substateT this_state;
1780 offsetT growth;
1781 offsetT aim;
1782 addressT target;
1783 addressT address;
1784 symbolS *symbolP;
1785 const relax_typeS *table;
1786
1787 target = fragP->fr_offset;
1788 address = fragP->fr_address;
1789 table = TC_GENERIC_RELAX_TABLE;
1790 this_state = fragP->fr_subtype;
1791 start_type = this_type = table + this_state;
1792 symbolP = fragP->fr_symbol;
1793
1794 if (symbolP)
1795 {
1796 fragS *sym_frag;
1797
1798 sym_frag = symbol_get_frag (symbolP);
1799
1800 #ifndef DIFF_EXPR_OK
1801 know (sym_frag != NULL);
1802 #endif
1803 know (S_GET_SEGMENT (symbolP) != absolute_section
1804 || sym_frag == &zero_address_frag);
1805 target += S_GET_VALUE (symbolP);
1806
1807 /* If frag has yet to be reached on this pass,
1808 assume it will move by STRETCH just as we did.
1809 If this is not so, it will be because some frag
1810 between grows, and that will force another pass. */
1811
1812 if (stretch != 0
1813 && sym_frag->relax_marker != fragP->relax_marker
1814 && S_GET_SEGMENT (symbolP) == segment)
1815 {
1816 target += stretch;
1817 }
1818 }
1819
1820 aim = target - address - fragP->fr_fix;
1821 #ifdef TC_PCREL_ADJUST
1822 /* Currently only the ns32k family needs this. */
1823 aim += TC_PCREL_ADJUST (fragP);
1824 #endif
1825
1826 #ifdef md_prepare_relax_scan
1827 /* Formerly called M68K_AIM_KLUDGE. */
1828 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1829 #endif
1830
1831 if (aim < 0)
1832 {
1833 /* Look backwards. */
1834 for (next_state = this_type->rlx_more; next_state;)
1835 if (aim >= this_type->rlx_backward)
1836 next_state = 0;
1837 else
1838 {
1839 /* Grow to next state. */
1840 this_state = next_state;
1841 this_type = table + this_state;
1842 next_state = this_type->rlx_more;
1843 }
1844 }
1845 else
1846 {
1847 /* Look forwards. */
1848 for (next_state = this_type->rlx_more; next_state;)
1849 if (aim <= this_type->rlx_forward)
1850 next_state = 0;
1851 else
1852 {
1853 /* Grow to next state. */
1854 this_state = next_state;
1855 this_type = table + this_state;
1856 next_state = this_type->rlx_more;
1857 }
1858 }
1859
1860 growth = this_type->rlx_length - start_type->rlx_length;
1861 if (growth != 0)
1862 fragP->fr_subtype = this_state;
1863 return growth;
1864 }
1865
1866 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1867
1868 /* Relax_align. Advance location counter to next address that has 'alignment'
1869 lowest order bits all 0s, return size of adjustment made. */
1870 static relax_addressT
1871 relax_align (register relax_addressT address, /* Address now. */
1872 register int alignment /* Alignment (binary). */)
1873 {
1874 relax_addressT mask;
1875 relax_addressT new_address;
1876
1877 mask = ~((~0) << alignment);
1878 new_address = (address + mask) & (~mask);
1879 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1880 if (linkrelax)
1881 /* We must provide lots of padding, so the linker can discard it
1882 when needed. The linker will not add extra space, ever. */
1883 new_address += (1 << alignment);
1884 #endif
1885 return (new_address - address);
1886 }
1887
1888 /* Now we have a segment, not a crowd of sub-segments, we can make
1889 fr_address values.
1890
1891 Relax the frags.
1892
1893 After this, all frags in this segment have addresses that are correct
1894 within the segment. Since segments live in different file addresses,
1895 these frag addresses may not be the same as final object-file
1896 addresses. */
1897
1898 int
1899 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
1900 {
1901 unsigned long frag_count;
1902 struct frag *fragP;
1903 relax_addressT address;
1904 int ret;
1905
1906 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1907 subseg_change (segment, 0);
1908
1909 /* For each frag in segment: count and store (a 1st guess of)
1910 fr_address. */
1911 address = 0;
1912 for (frag_count = 0, fragP = segment_frag_root;
1913 fragP;
1914 fragP = fragP->fr_next, frag_count ++)
1915 {
1916 fragP->relax_marker = 0;
1917 fragP->fr_address = address;
1918 address += fragP->fr_fix;
1919
1920 switch (fragP->fr_type)
1921 {
1922 case rs_fill:
1923 address += fragP->fr_offset * fragP->fr_var;
1924 break;
1925
1926 case rs_align:
1927 case rs_align_code:
1928 case rs_align_test:
1929 {
1930 addressT offset = relax_align (address, (int) fragP->fr_offset);
1931
1932 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
1933 offset = 0;
1934
1935 if (offset % fragP->fr_var != 0)
1936 {
1937 as_bad_where (fragP->fr_file, fragP->fr_line,
1938 _("alignment padding (%lu bytes) not a multiple of %ld"),
1939 (unsigned long) offset, (long) fragP->fr_var);
1940 offset -= (offset % fragP->fr_var);
1941 }
1942
1943 address += offset;
1944 }
1945 break;
1946
1947 case rs_org:
1948 case rs_space:
1949 /* Assume .org is nugatory. It will grow with 1st relax. */
1950 break;
1951
1952 case rs_machine_dependent:
1953 /* If fr_symbol is an expression, this call to
1954 resolve_symbol_value sets up the correct segment, which will
1955 likely be needed in md_estimate_size_before_relax. */
1956 if (fragP->fr_symbol)
1957 resolve_symbol_value (fragP->fr_symbol);
1958
1959 address += md_estimate_size_before_relax (fragP, segment);
1960 break;
1961
1962 #ifndef WORKING_DOT_WORD
1963 /* Broken words don't concern us yet. */
1964 case rs_broken_word:
1965 break;
1966 #endif
1967
1968 case rs_leb128:
1969 /* Initial guess is always 1; doing otherwise can result in
1970 stable solutions that are larger than the minimum. */
1971 address += fragP->fr_offset = 1;
1972 break;
1973
1974 case rs_cfa:
1975 address += eh_frame_estimate_size_before_relax (fragP);
1976 break;
1977
1978 case rs_dwarf2dbg:
1979 address += dwarf2dbg_estimate_size_before_relax (fragP);
1980 break;
1981
1982 default:
1983 BAD_CASE (fragP->fr_type);
1984 break;
1985 }
1986 }
1987
1988 /* Do relax(). */
1989 {
1990 unsigned long max_iterations;
1991 offsetT stretch; /* May be any size, 0 or negative. */
1992 /* Cumulative number of addresses we have relaxed this pass.
1993 We may have relaxed more than one address. */
1994 int stretched; /* Have we stretched on this pass? */
1995 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1996 grew, and another shrank. If a branch instruction doesn't fit anymore,
1997 we could be scrod. */
1998
1999 /* We want to prevent going into an infinite loop where one frag grows
2000 depending upon the location of a symbol which is in turn moved by
2001 the growing frag. eg:
2002
2003 foo = .
2004 .org foo+16
2005 foo = .
2006
2007 So we dictate that this algorithm can be at most O2. */
2008 max_iterations = frag_count * frag_count;
2009 /* Check for overflow. */
2010 if (max_iterations < frag_count)
2011 max_iterations = frag_count;
2012
2013 ret = 0;
2014 do
2015 {
2016 stretch = 0;
2017 stretched = 0;
2018
2019 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2020 {
2021 offsetT growth = 0;
2022 addressT was_address;
2023 offsetT offset;
2024 symbolS *symbolP;
2025
2026 fragP->relax_marker ^= 1;
2027 was_address = fragP->fr_address;
2028 address = fragP->fr_address += stretch;
2029 symbolP = fragP->fr_symbol;
2030 offset = fragP->fr_offset;
2031
2032 switch (fragP->fr_type)
2033 {
2034 case rs_fill: /* .fill never relaxes. */
2035 growth = 0;
2036 break;
2037
2038 #ifndef WORKING_DOT_WORD
2039 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2040 for it I do not want to write it. I do not want to have
2041 anything to do with it. This is not the proper way to
2042 implement this misfeature. */
2043 case rs_broken_word:
2044 {
2045 struct broken_word *lie;
2046 struct broken_word *untruth;
2047
2048 /* Yes this is ugly (storing the broken_word pointer
2049 in the symbol slot). Still, this whole chunk of
2050 code is ugly, and I don't feel like doing anything
2051 about it. Think of it as stubbornness in action. */
2052 growth = 0;
2053 for (lie = (struct broken_word *) (fragP->fr_symbol);
2054 lie && lie->dispfrag == fragP;
2055 lie = lie->next_broken_word)
2056 {
2057
2058 if (lie->added)
2059 continue;
2060
2061 offset = (S_GET_VALUE (lie->add)
2062 + lie->addnum
2063 - S_GET_VALUE (lie->sub));
2064 if (offset <= -32768 || offset >= 32767)
2065 {
2066 if (flag_warn_displacement)
2067 {
2068 char buf[50];
2069 sprint_value (buf, (addressT) lie->addnum);
2070 as_warn_where (fragP->fr_file, fragP->fr_line,
2071 _(".word %s-%s+%s didn't fit"),
2072 S_GET_NAME (lie->add),
2073 S_GET_NAME (lie->sub),
2074 buf);
2075 }
2076 lie->added = 1;
2077 if (fragP->fr_subtype == 0)
2078 {
2079 fragP->fr_subtype++;
2080 growth += md_short_jump_size;
2081 }
2082 for (untruth = lie->next_broken_word;
2083 untruth && untruth->dispfrag == lie->dispfrag;
2084 untruth = untruth->next_broken_word)
2085 if ((symbol_get_frag (untruth->add)
2086 == symbol_get_frag (lie->add))
2087 && (S_GET_VALUE (untruth->add)
2088 == S_GET_VALUE (lie->add)))
2089 {
2090 untruth->added = 2;
2091 untruth->use_jump = lie;
2092 }
2093 growth += md_long_jump_size;
2094 }
2095 }
2096
2097 break;
2098 } /* case rs_broken_word */
2099 #endif
2100 case rs_align:
2101 case rs_align_code:
2102 case rs_align_test:
2103 {
2104 addressT oldoff, newoff;
2105
2106 oldoff = relax_align (was_address + fragP->fr_fix,
2107 (int) offset);
2108 newoff = relax_align (address + fragP->fr_fix,
2109 (int) offset);
2110
2111 if (fragP->fr_subtype != 0)
2112 {
2113 if (oldoff > fragP->fr_subtype)
2114 oldoff = 0;
2115 if (newoff > fragP->fr_subtype)
2116 newoff = 0;
2117 }
2118
2119 growth = newoff - oldoff;
2120 }
2121 break;
2122
2123 case rs_org:
2124 {
2125 addressT target = offset;
2126 addressT after;
2127
2128 if (symbolP)
2129 {
2130 /* Convert from an actual address to an octet offset
2131 into the section. Here it is assumed that the
2132 section's VMA is zero, and can omit subtracting it
2133 from the symbol's value to get the address offset. */
2134 know (S_GET_SEGMENT (symbolP)->vma == 0);
2135 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2136 }
2137
2138 know (fragP->fr_next);
2139 after = fragP->fr_next->fr_address;
2140 growth = target - after;
2141 if (growth < 0)
2142 {
2143 growth = 0;
2144
2145 /* Don't error on first few frag relax passes.
2146 The symbol might be an expression involving
2147 symbol values from other sections. If those
2148 sections have not yet been processed their
2149 frags will all have zero addresses, so we
2150 will calculate incorrect values for them. The
2151 number of passes we allow before giving an
2152 error is somewhat arbitrary. It should be at
2153 least one, with larger values requiring
2154 increasingly contrived dependencies between
2155 frags to trigger a false error. */
2156 if (pass < 2)
2157 {
2158 /* Force another pass. */
2159 ret = 1;
2160 break;
2161 }
2162
2163 /* Growth may be negative, but variable part of frag
2164 cannot have fewer than 0 chars. That is, we can't
2165 .org backwards. */
2166 as_bad_where (fragP->fr_file, fragP->fr_line,
2167 _("attempt to move .org backwards"));
2168
2169 /* We've issued an error message. Change the
2170 frag to avoid cascading errors. */
2171 fragP->fr_type = rs_align;
2172 fragP->fr_subtype = 0;
2173 fragP->fr_offset = 0;
2174 fragP->fr_fix = after - was_address;
2175 break;
2176 }
2177
2178 /* This is an absolute growth factor */
2179 growth -= stretch;
2180 break;
2181 }
2182
2183 case rs_space:
2184 growth = 0;
2185 if (symbolP)
2186 {
2187 offsetT amount;
2188
2189 amount = S_GET_VALUE (symbolP);
2190 if (S_GET_SEGMENT (symbolP) != absolute_section
2191 || S_IS_COMMON (symbolP)
2192 || ! S_IS_DEFINED (symbolP))
2193 {
2194 as_bad_where (fragP->fr_file, fragP->fr_line,
2195 _(".space specifies non-absolute value"));
2196 /* Prevent repeat of this error message. */
2197 fragP->fr_symbol = 0;
2198 }
2199 else if (amount < 0)
2200 {
2201 /* Don't error on first few frag relax passes.
2202 See rs_org comment for a longer explanation. */
2203 if (pass < 2)
2204 {
2205 ret = 1;
2206 break;
2207 }
2208
2209 as_warn_where (fragP->fr_file, fragP->fr_line,
2210 _(".space or .fill with negative value, ignored"));
2211 fragP->fr_symbol = 0;
2212 }
2213 else
2214 growth = (was_address + fragP->fr_fix + amount
2215 - fragP->fr_next->fr_address);
2216 }
2217 break;
2218
2219 case rs_machine_dependent:
2220 #ifdef md_relax_frag
2221 growth = md_relax_frag (segment, fragP, stretch);
2222 #else
2223 #ifdef TC_GENERIC_RELAX_TABLE
2224 /* The default way to relax a frag is to look through
2225 TC_GENERIC_RELAX_TABLE. */
2226 growth = relax_frag (segment, fragP, stretch);
2227 #endif /* TC_GENERIC_RELAX_TABLE */
2228 #endif
2229 break;
2230
2231 case rs_leb128:
2232 {
2233 valueT value;
2234 offsetT size;
2235
2236 value = resolve_symbol_value (fragP->fr_symbol);
2237 size = sizeof_leb128 (value, fragP->fr_subtype);
2238 growth = size - fragP->fr_offset;
2239 fragP->fr_offset = size;
2240 }
2241 break;
2242
2243 case rs_cfa:
2244 growth = eh_frame_relax_frag (fragP);
2245 break;
2246
2247 case rs_dwarf2dbg:
2248 growth = dwarf2dbg_relax_frag (fragP);
2249 break;
2250
2251 default:
2252 BAD_CASE (fragP->fr_type);
2253 break;
2254 }
2255 if (growth)
2256 {
2257 stretch += growth;
2258 stretched = 1;
2259 }
2260 }
2261 }
2262 /* Until nothing further to relax. */
2263 while (stretched && -- max_iterations);
2264
2265 if (stretched)
2266 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2267 segment_name (segment));
2268 }
2269
2270 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2271 if (fragP->last_fr_address != fragP->fr_address)
2272 {
2273 fragP->last_fr_address = fragP->fr_address;
2274 ret = 1;
2275 }
2276 return ret;
2277 }
2278
2279 void
2280 number_to_chars_bigendian (char *buf, valueT val, int n)
2281 {
2282 if (n <= 0)
2283 abort ();
2284 while (n--)
2285 {
2286 buf[n] = val & 0xff;
2287 val >>= 8;
2288 }
2289 }
2290
2291 void
2292 number_to_chars_littleendian (char *buf, valueT val, int n)
2293 {
2294 if (n <= 0)
2295 abort ();
2296 while (n--)
2297 {
2298 *buf++ = val & 0xff;
2299 val >>= 8;
2300 }
2301 }
2302
2303 void
2304 write_print_statistics (FILE *file)
2305 {
2306 fprintf (file, "fixups: %d\n", n_fixups);
2307 }
2308
2309 /* For debugging. */
2310 extern int indent_level;
2311
2312 void
2313 print_fixup (fixS *fixp)
2314 {
2315 indent_level = 1;
2316 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2317 if (fixp->fx_pcrel)
2318 fprintf (stderr, " pcrel");
2319 if (fixp->fx_pcrel_adjust)
2320 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2321 if (fixp->fx_im_disp)
2322 {
2323 #ifdef TC_NS32K
2324 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2325 #else
2326 fprintf (stderr, " im_disp");
2327 #endif
2328 }
2329 if (fixp->fx_tcbit)
2330 fprintf (stderr, " tcbit");
2331 if (fixp->fx_done)
2332 fprintf (stderr, " done");
2333 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2334 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2335 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2336 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2337 fixp->fx_r_type);
2338 if (fixp->fx_addsy)
2339 {
2340 fprintf (stderr, "\n +<");
2341 print_symbol_value_1 (stderr, fixp->fx_addsy);
2342 fprintf (stderr, ">");
2343 }
2344 if (fixp->fx_subsy)
2345 {
2346 fprintf (stderr, "\n -<");
2347 print_symbol_value_1 (stderr, fixp->fx_subsy);
2348 fprintf (stderr, ">");
2349 }
2350 fprintf (stderr, "\n");
2351 #ifdef TC_FIX_DATA_PRINT
2352 TC_FIX_DATA_PRINT (stderr, fixp);
2353 #endif
2354 }
This page took 0.085103 seconds and 3 git commands to generate.