bc76962c600efc442d7d180e360320f69808685d
[deliverable/binutils-gdb.git] / gas / write.c
1 /* write.c - emit .o file
2 Copyright (C) 1986-2015 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 /* This thing should be set up to do byteordering correctly. But... */
22
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "output-file.h"
27 #include "dwarf2dbg.h"
28 #include "libbfd.h"
29 #include "compress-debug.h"
30
31 #ifndef TC_FORCE_RELOCATION
32 #define TC_FORCE_RELOCATION(FIX) \
33 (generic_force_reloc (FIX))
34 #endif
35
36 #ifndef TC_FORCE_RELOCATION_ABS
37 #define TC_FORCE_RELOCATION_ABS(FIX) \
38 (TC_FORCE_RELOCATION (FIX))
39 #endif
40
41 #ifndef TC_FORCE_RELOCATION_LOCAL
42 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
43 (!(FIX)->fx_pcrel \
44 || TC_FORCE_RELOCATION (FIX))
45 #endif
46
47 #ifndef TC_FORCE_RELOCATION_SUB_SAME
48 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
49 (! SEG_NORMAL (SEG))
50 #endif
51
52 #ifndef md_register_arithmetic
53 # define md_register_arithmetic 1
54 #endif
55
56 #ifndef TC_FORCE_RELOCATION_SUB_ABS
57 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
58 (!md_register_arithmetic && (SEG) == reg_section)
59 #endif
60
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
62 #ifdef DIFF_EXPR_OK
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
64 (!md_register_arithmetic && (SEG) == reg_section)
65 #else
66 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
67 #endif
68 #endif
69
70 #ifndef TC_VALIDATE_FIX_SUB
71 #ifdef UNDEFINED_DIFFERENCE_OK
72 /* The PA needs this for PIC code generation. */
73 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
74 (md_register_arithmetic || (SEG) != reg_section)
75 #else
76 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
77 ((md_register_arithmetic || (SEG) != reg_section) \
78 && ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
79 || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
80 #endif
81 #endif
82
83 #ifndef TC_LINKRELAX_FIXUP
84 #define TC_LINKRELAX_FIXUP(SEG) 1
85 #endif
86
87 #ifndef MD_APPLY_SYM_VALUE
88 #define MD_APPLY_SYM_VALUE(FIX) 1
89 #endif
90
91 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
92 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
93 #endif
94
95 #ifndef MD_PCREL_FROM_SECTION
96 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
97 #endif
98
99 #ifndef TC_FAKE_LABEL
100 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
101 #endif
102
103 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
104 fixups that far past the end of a frag. Having such fixups
105 is of course most most likely a bug in setting fx_size correctly.
106 A negative value disables the fixup check entirely, which is
107 appropriate for something like the Renesas / SuperH SH_COUNT
108 reloc. */
109 #ifndef TC_FX_SIZE_SLACK
110 #define TC_FX_SIZE_SLACK(FIX) 0
111 #endif
112
113 /* Used to control final evaluation of expressions. */
114 int finalize_syms = 0;
115
116 int symbol_table_frozen;
117
118 symbolS *abs_section_sym;
119
120 /* Remember the value of dot when parsing expressions. */
121 addressT dot_value;
122
123 /* The frag that dot_value is based from. */
124 fragS *dot_frag;
125
126 /* Relocs generated by ".reloc" pseudo. */
127 struct reloc_list* reloc_list;
128
129 void print_fixup (fixS *);
130
131 /* We generally attach relocs to frag chains. However, after we have
132 chained these all together into a segment, any relocs we add after
133 that must be attached to a segment. This will include relocs added
134 in md_estimate_size_for_relax, for example. */
135 static int frags_chained = 0;
136
137 static int n_fixups;
138
139 #define RELOC_ENUM enum bfd_reloc_code_real
140
141 /* Create a fixS in obstack 'notes'. */
142
143 static fixS *
144 fix_new_internal (fragS *frag, /* Which frag? */
145 int where, /* Where in that frag? */
146 int size, /* 1, 2, or 4 usually. */
147 symbolS *add_symbol, /* X_add_symbol. */
148 symbolS *sub_symbol, /* X_op_symbol. */
149 offsetT offset, /* X_add_number. */
150 int pcrel, /* TRUE if PC-relative relocation. */
151 RELOC_ENUM r_type /* Relocation type. */,
152 int at_beginning) /* Add to the start of the list? */
153 {
154 fixS *fixP;
155
156 n_fixups++;
157
158 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
159
160 fixP->fx_frag = frag;
161 fixP->fx_where = where;
162 fixP->fx_size = size;
163 /* We've made fx_size a narrow field; check that it's wide enough. */
164 if (fixP->fx_size != size)
165 {
166 as_bad (_("field fx_size too small to hold %d"), size);
167 abort ();
168 }
169 fixP->fx_addsy = add_symbol;
170 fixP->fx_subsy = sub_symbol;
171 fixP->fx_offset = offset;
172 fixP->fx_dot_value = dot_value;
173 fixP->fx_dot_frag = dot_frag;
174 fixP->fx_pcrel = pcrel;
175 fixP->fx_r_type = r_type;
176 fixP->fx_im_disp = 0;
177 fixP->fx_pcrel_adjust = 0;
178 fixP->fx_bit_fixP = 0;
179 fixP->fx_addnumber = 0;
180 fixP->fx_tcbit = 0;
181 fixP->fx_tcbit2 = 0;
182 fixP->fx_done = 0;
183 fixP->fx_no_overflow = 0;
184 fixP->fx_signed = 0;
185
186 #ifdef USING_CGEN
187 fixP->fx_cgen.insn = NULL;
188 fixP->fx_cgen.opinfo = 0;
189 #endif
190
191 #ifdef TC_FIX_TYPE
192 TC_INIT_FIX_DATA (fixP);
193 #endif
194
195 as_where (&fixP->fx_file, &fixP->fx_line);
196
197 {
198
199 fixS **seg_fix_rootP = (frags_chained
200 ? &seg_info (now_seg)->fix_root
201 : &frchain_now->fix_root);
202 fixS **seg_fix_tailP = (frags_chained
203 ? &seg_info (now_seg)->fix_tail
204 : &frchain_now->fix_tail);
205
206 if (at_beginning)
207 {
208 fixP->fx_next = *seg_fix_rootP;
209 *seg_fix_rootP = fixP;
210 if (fixP->fx_next == NULL)
211 *seg_fix_tailP = fixP;
212 }
213 else
214 {
215 fixP->fx_next = NULL;
216 if (*seg_fix_tailP)
217 (*seg_fix_tailP)->fx_next = fixP;
218 else
219 *seg_fix_rootP = fixP;
220 *seg_fix_tailP = fixP;
221 }
222 }
223
224 return fixP;
225 }
226
227 /* Create a fixup relative to a symbol (plus a constant). */
228
229 fixS *
230 fix_new (fragS *frag, /* Which frag? */
231 int where, /* Where in that frag? */
232 int size, /* 1, 2, or 4 usually. */
233 symbolS *add_symbol, /* X_add_symbol. */
234 offsetT offset, /* X_add_number. */
235 int pcrel, /* TRUE if PC-relative relocation. */
236 RELOC_ENUM r_type /* Relocation type. */)
237 {
238 return fix_new_internal (frag, where, size, add_symbol,
239 (symbolS *) NULL, offset, pcrel, r_type, FALSE);
240 }
241
242 /* Create a fixup for an expression. Currently we only support fixups
243 for difference expressions. That is itself more than most object
244 file formats support anyhow. */
245
246 fixS *
247 fix_new_exp (fragS *frag, /* Which frag? */
248 int where, /* Where in that frag? */
249 int size, /* 1, 2, or 4 usually. */
250 expressionS *exp, /* Expression. */
251 int pcrel, /* TRUE if PC-relative relocation. */
252 RELOC_ENUM r_type /* Relocation type. */)
253 {
254 symbolS *add = NULL;
255 symbolS *sub = NULL;
256 offsetT off = 0;
257
258 switch (exp->X_op)
259 {
260 case O_absent:
261 break;
262
263 case O_register:
264 as_bad (_("register value used as expression"));
265 break;
266
267 case O_add:
268 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
269 the difference expression cannot immediately be reduced. */
270 {
271 symbolS *stmp = make_expr_symbol (exp);
272
273 exp->X_op = O_symbol;
274 exp->X_op_symbol = 0;
275 exp->X_add_symbol = stmp;
276 exp->X_add_number = 0;
277
278 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
279 }
280
281 case O_symbol_rva:
282 add = exp->X_add_symbol;
283 off = exp->X_add_number;
284 r_type = BFD_RELOC_RVA;
285 break;
286
287 case O_uminus:
288 sub = exp->X_add_symbol;
289 off = exp->X_add_number;
290 break;
291
292 case O_subtract:
293 sub = exp->X_op_symbol;
294 /* Fall through. */
295 case O_symbol:
296 add = exp->X_add_symbol;
297 /* Fall through. */
298 case O_constant:
299 off = exp->X_add_number;
300 break;
301
302 default:
303 add = make_expr_symbol (exp);
304 break;
305 }
306
307 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
308 r_type, FALSE);
309 }
310
311 /* Create a fixup at the beginning of FRAG. The arguments are the same
312 as for fix_new, except that WHERE is implicitly 0. */
313
314 fixS *
315 fix_at_start (fragS *frag, int size, symbolS *add_symbol,
316 offsetT offset, int pcrel, RELOC_ENUM r_type)
317 {
318 return fix_new_internal (frag, 0, size, add_symbol,
319 (symbolS *) NULL, offset, pcrel, r_type, TRUE);
320 }
321
322 /* Generic function to determine whether a fixup requires a relocation. */
323 int
324 generic_force_reloc (fixS *fix)
325 {
326 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
327 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
328 return 1;
329
330 if (fix->fx_addsy == NULL)
331 return 0;
332
333 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
334 }
335
336 /* Append a string onto another string, bumping the pointer along. */
337 void
338 append (char **charPP, char *fromP, unsigned long length)
339 {
340 /* Don't trust memcpy() of 0 chars. */
341 if (length == 0)
342 return;
343
344 memcpy (*charPP, fromP, length);
345 *charPP += length;
346 }
347
348 /* This routine records the largest alignment seen for each segment.
349 If the beginning of the segment is aligned on the worst-case
350 boundary, all of the other alignments within it will work. At
351 least one object format really uses this info. */
352
353 void
354 record_alignment (/* Segment to which alignment pertains. */
355 segT seg,
356 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
357 boundary, 2 => 4-byte boundary, etc.) */
358 int align)
359 {
360 if (seg == absolute_section)
361 return;
362
363 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
364 bfd_set_section_alignment (stdoutput, seg, align);
365 }
366
367 int
368 get_recorded_alignment (segT seg)
369 {
370 if (seg == absolute_section)
371 return 0;
372
373 return bfd_get_section_alignment (stdoutput, seg);
374 }
375
376 /* Reset the section indices after removing the gas created sections. */
377
378 static void
379 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
380 {
381 int *countp = (int *) countparg;
382
383 sec->index = *countp;
384 ++*countp;
385 }
386
387 static fragS *
388 chain_frchains_together_1 (segT section, struct frchain *frchp)
389 {
390 fragS dummy, *prev_frag = &dummy;
391 fixS fix_dummy, *prev_fix = &fix_dummy;
392
393 for (; frchp; frchp = frchp->frch_next)
394 {
395 prev_frag->fr_next = frchp->frch_root;
396 prev_frag = frchp->frch_last;
397 gas_assert (prev_frag->fr_type != 0);
398 if (frchp->fix_root != (fixS *) NULL)
399 {
400 if (seg_info (section)->fix_root == (fixS *) NULL)
401 seg_info (section)->fix_root = frchp->fix_root;
402 prev_fix->fx_next = frchp->fix_root;
403 seg_info (section)->fix_tail = frchp->fix_tail;
404 prev_fix = frchp->fix_tail;
405 }
406 }
407 gas_assert (prev_frag != &dummy
408 && prev_frag->fr_type != 0);
409 prev_frag->fr_next = 0;
410 return prev_frag;
411 }
412
413 static void
414 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
415 segT section,
416 void *xxx ATTRIBUTE_UNUSED)
417 {
418 segment_info_type *info;
419
420 /* BFD may have introduced its own sections without using
421 subseg_new, so it is possible that seg_info is NULL. */
422 info = seg_info (section);
423 if (info != (segment_info_type *) NULL)
424 info->frchainP->frch_last
425 = chain_frchains_together_1 (section, info->frchainP);
426
427 /* Now that we've chained the frags together, we must add new fixups
428 to the segment, not to the frag chain. */
429 frags_chained = 1;
430 }
431
432 static void
433 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
434 {
435 switch (fragP->fr_type)
436 {
437 case rs_align:
438 case rs_align_code:
439 case rs_align_test:
440 case rs_org:
441 case rs_space:
442 #ifdef HANDLE_ALIGN
443 HANDLE_ALIGN (fragP);
444 #endif
445 know (fragP->fr_next != NULL);
446 fragP->fr_offset = (fragP->fr_next->fr_address
447 - fragP->fr_address
448 - fragP->fr_fix) / fragP->fr_var;
449 if (fragP->fr_offset < 0)
450 {
451 as_bad_where (fragP->fr_file, fragP->fr_line,
452 _("attempt to .org/.space backwards? (%ld)"),
453 (long) fragP->fr_offset);
454 fragP->fr_offset = 0;
455 }
456 fragP->fr_type = rs_fill;
457 break;
458
459 case rs_fill:
460 break;
461
462 case rs_leb128:
463 {
464 valueT value = S_GET_VALUE (fragP->fr_symbol);
465 int size;
466
467 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
468 fragP->fr_subtype);
469
470 fragP->fr_fix += size;
471 fragP->fr_type = rs_fill;
472 fragP->fr_var = 0;
473 fragP->fr_offset = 0;
474 fragP->fr_symbol = NULL;
475 }
476 break;
477
478 case rs_cfa:
479 eh_frame_convert_frag (fragP);
480 break;
481
482 case rs_dwarf2dbg:
483 dwarf2dbg_convert_frag (fragP);
484 break;
485
486 case rs_machine_dependent:
487 md_convert_frag (stdoutput, sec, fragP);
488
489 gas_assert (fragP->fr_next == NULL
490 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
491 == fragP->fr_fix));
492
493 /* After md_convert_frag, we make the frag into a ".space 0".
494 md_convert_frag() should set up any fixSs and constants
495 required. */
496 frag_wane (fragP);
497 break;
498
499 #ifndef WORKING_DOT_WORD
500 case rs_broken_word:
501 {
502 struct broken_word *lie;
503
504 if (fragP->fr_subtype)
505 {
506 fragP->fr_fix += md_short_jump_size;
507 for (lie = (struct broken_word *) (fragP->fr_symbol);
508 lie && lie->dispfrag == fragP;
509 lie = lie->next_broken_word)
510 if (lie->added == 1)
511 fragP->fr_fix += md_long_jump_size;
512 }
513 frag_wane (fragP);
514 }
515 break;
516 #endif
517
518 default:
519 BAD_CASE (fragP->fr_type);
520 break;
521 }
522 #ifdef md_frag_check
523 md_frag_check (fragP);
524 #endif
525 }
526
527 struct relax_seg_info
528 {
529 int pass;
530 int changed;
531 };
532
533 static void
534 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
535 {
536 segment_info_type *seginfo = seg_info (sec);
537 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
538
539 if (seginfo && seginfo->frchainP
540 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
541 info->changed = 1;
542 }
543
544 static void
545 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
546 {
547 flagword flags;
548 fragS *fragp;
549 segment_info_type *seginfo;
550 int x;
551 valueT size, newsize;
552
553 subseg_change (sec, 0);
554
555 seginfo = seg_info (sec);
556 if (seginfo && seginfo->frchainP)
557 {
558 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
559 cvt_frag_to_fill (sec, fragp);
560 for (fragp = seginfo->frchainP->frch_root;
561 fragp->fr_next;
562 fragp = fragp->fr_next)
563 /* Walk to last elt. */
564 ;
565 size = fragp->fr_address + fragp->fr_fix;
566 }
567 else
568 size = 0;
569
570 flags = bfd_get_section_flags (abfd, sec);
571 if (size == 0 && bfd_get_section_size (sec) != 0 &&
572 (flags & SEC_HAS_CONTENTS) != 0)
573 return;
574
575 if (size > 0 && ! seginfo->bss)
576 flags |= SEC_HAS_CONTENTS;
577
578 flags &= ~SEC_RELOC;
579 x = bfd_set_section_flags (abfd, sec, flags);
580 gas_assert (x);
581
582 newsize = md_section_align (sec, size);
583 x = bfd_set_section_size (abfd, sec, newsize);
584 gas_assert (x);
585
586 /* If the size had to be rounded up, add some padding in the last
587 non-empty frag. */
588 gas_assert (newsize >= size);
589 if (size != newsize)
590 {
591 fragS *last = seginfo->frchainP->frch_last;
592 fragp = seginfo->frchainP->frch_root;
593 while (fragp->fr_next != last)
594 fragp = fragp->fr_next;
595 last->fr_address = size;
596 if ((newsize - size) % fragp->fr_var == 0)
597 fragp->fr_offset += (newsize - size) / fragp->fr_var;
598 else
599 /* If we hit this abort, it's likely due to subsegs_finish not
600 providing sufficient alignment on the last frag, and the
601 machine dependent code using alignment frags with fr_var
602 greater than 1. */
603 abort ();
604 }
605
606 #ifdef tc_frob_section
607 tc_frob_section (sec);
608 #endif
609 #ifdef obj_frob_section
610 obj_frob_section (sec);
611 #endif
612 }
613
614 #ifdef DEBUG2
615 static void
616 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
617 {
618 segment_info_type *seginfo = seg_info (sec);
619 fixS *fixp = seginfo->fix_root;
620
621 if (!fixp)
622 return;
623
624 fprintf (stream, "sec %s relocs:\n", sec->name);
625 while (fixp)
626 {
627 symbolS *s = fixp->fx_addsy;
628
629 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
630 (int) fixp->fx_r_type);
631 if (s == NULL)
632 fprintf (stream, "no sym\n");
633 else
634 {
635 print_symbol_value_1 (stream, s);
636 fprintf (stream, "\n");
637 }
638 fixp = fixp->fx_next;
639 }
640 }
641 #else
642 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
643 #endif
644
645 #ifndef EMIT_SECTION_SYMBOLS
646 #define EMIT_SECTION_SYMBOLS 1
647 #endif
648
649 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
650 and check for validity. Convert RELOC_LIST from using U.A fields
651 to U.B fields. */
652 static void
653 resolve_reloc_expr_symbols (void)
654 {
655 bfd_vma addr_mask = 1;
656 struct reloc_list *r;
657
658 /* Avoid a shift by the width of type. */
659 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
660 addr_mask <<= 1;
661 addr_mask -= 1;
662
663 for (r = reloc_list; r; r = r->next)
664 {
665 reloc_howto_type *howto = r->u.a.howto;
666 expressionS *symval;
667 symbolS *sym;
668 bfd_vma offset, addend;
669 asection *sec;
670
671 resolve_symbol_value (r->u.a.offset_sym);
672 symval = symbol_get_value_expression (r->u.a.offset_sym);
673
674 offset = 0;
675 sym = NULL;
676 if (symval->X_op == O_constant)
677 sym = r->u.a.offset_sym;
678 else if (symval->X_op == O_symbol)
679 {
680 sym = symval->X_add_symbol;
681 offset = symval->X_add_number;
682 symval = symbol_get_value_expression (symval->X_add_symbol);
683 }
684 if (sym == NULL
685 || symval->X_op != O_constant
686 || (sec = S_GET_SEGMENT (sym)) == NULL
687 || !SEG_NORMAL (sec))
688 {
689 as_bad_where (r->file, r->line, _("invalid offset expression"));
690 sec = NULL;
691 }
692 else
693 offset += S_GET_VALUE (sym);
694
695 sym = NULL;
696 addend = r->u.a.addend;
697 if (r->u.a.sym != NULL)
698 {
699 resolve_symbol_value (r->u.a.sym);
700 symval = symbol_get_value_expression (r->u.a.sym);
701 if (symval->X_op == O_constant)
702 sym = r->u.a.sym;
703 else if (symval->X_op == O_symbol)
704 {
705 sym = symval->X_add_symbol;
706 addend += symval->X_add_number;
707 symval = symbol_get_value_expression (symval->X_add_symbol);
708 }
709 if (symval->X_op != O_constant)
710 {
711 as_bad_where (r->file, r->line, _("invalid reloc expression"));
712 sec = NULL;
713 }
714 else if (sym != NULL)
715 {
716 /* Convert relocs against local symbols to refer to the
717 corresponding section symbol plus offset instead. Keep
718 PC-relative relocs of the REL variety intact though to
719 prevent the offset from overflowing the relocated field,
720 unless it has enough bits to cover the whole address
721 space. */
722 if (S_IS_LOCAL (sym) && !symbol_section_p (sym)
723 && (sec->use_rela_p
724 || (howto->partial_inplace
725 && (!howto->pc_relative
726 || howto->src_mask == addr_mask))))
727 {
728 asection *symsec = S_GET_SEGMENT (sym);
729 if (!(((symsec->flags & SEC_MERGE) != 0
730 && addend != 0)
731 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
732 {
733 addend += S_GET_VALUE (sym);
734 sym = section_symbol (symsec);
735 }
736 }
737 symbol_mark_used_in_reloc (sym);
738 }
739 }
740 if (sym == NULL)
741 {
742 if (abs_section_sym == NULL)
743 abs_section_sym = section_symbol (absolute_section);
744 sym = abs_section_sym;
745 }
746
747 r->u.b.sec = sec;
748 r->u.b.s = symbol_get_bfdsym (sym);
749 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
750 r->u.b.r.address = offset;
751 r->u.b.r.addend = addend;
752 r->u.b.r.howto = howto;
753 }
754 }
755
756 /* This pass over fixups decides whether symbols can be replaced with
757 section symbols. */
758
759 static void
760 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
761 asection *sec,
762 void *xxx ATTRIBUTE_UNUSED)
763 {
764 segment_info_type *seginfo = seg_info (sec);
765 fixS *fixp;
766
767 if (seginfo == NULL)
768 return;
769
770 dump_section_relocs (abfd, sec, stderr);
771
772 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
773 if (fixp->fx_done)
774 /* Ignore it. */
775 ;
776 else if (fixp->fx_addsy)
777 {
778 symbolS *sym;
779 asection *symsec;
780
781 #ifdef DEBUG5
782 fprintf (stderr, "\n\nadjusting fixup:\n");
783 print_fixup (fixp);
784 #endif
785
786 sym = fixp->fx_addsy;
787
788 /* All symbols should have already been resolved at this
789 point. It is possible to see unresolved expression
790 symbols, though, since they are not in the regular symbol
791 table. */
792 resolve_symbol_value (sym);
793
794 if (fixp->fx_subsy != NULL)
795 resolve_symbol_value (fixp->fx_subsy);
796
797 /* If this symbol is equated to an undefined or common symbol,
798 convert the fixup to being against that symbol. */
799 while (symbol_equated_reloc_p (sym)
800 || S_IS_WEAKREFR (sym))
801 {
802 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
803 if (sym == newsym)
804 break;
805 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
806 fixp->fx_addsy = newsym;
807 sym = newsym;
808 }
809
810 if (symbol_mri_common_p (sym))
811 {
812 fixp->fx_offset += S_GET_VALUE (sym);
813 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
814 continue;
815 }
816
817 /* If the symbol is undefined, common, weak, or global (ELF
818 shared libs), we can't replace it with the section symbol. */
819 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
820 continue;
821
822 /* Is there some other (target cpu dependent) reason we can't adjust
823 this one? (E.g. relocations involving function addresses on
824 the PA. */
825 #ifdef tc_fix_adjustable
826 if (! tc_fix_adjustable (fixp))
827 continue;
828 #endif
829
830 /* Since we're reducing to section symbols, don't attempt to reduce
831 anything that's already using one. */
832 if (symbol_section_p (sym))
833 continue;
834
835 symsec = S_GET_SEGMENT (sym);
836 if (symsec == NULL)
837 abort ();
838
839 if (bfd_is_abs_section (symsec)
840 || symsec == reg_section)
841 {
842 /* The fixup_segment routine normally will not use this
843 symbol in a relocation. */
844 continue;
845 }
846
847 /* Don't try to reduce relocs which refer to non-local symbols
848 in .linkonce sections. It can lead to confusion when a
849 debugging section refers to a .linkonce section. I hope
850 this will always be correct. */
851 if (symsec != sec && ! S_IS_LOCAL (sym))
852 {
853 if ((symsec->flags & SEC_LINK_ONCE) != 0
854 || (IS_ELF
855 /* The GNU toolchain uses an extension for ELF: a
856 section beginning with the magic string
857 .gnu.linkonce is a linkonce section. */
858 && strncmp (segment_name (symsec), ".gnu.linkonce",
859 sizeof ".gnu.linkonce" - 1) == 0))
860 continue;
861 }
862
863 /* Never adjust a reloc against local symbol in a merge section
864 with non-zero addend. */
865 if ((symsec->flags & SEC_MERGE) != 0
866 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
867 continue;
868
869 /* Never adjust a reloc against TLS local symbol. */
870 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
871 continue;
872
873 /* We refetch the segment when calling section_symbol, rather
874 than using symsec, because S_GET_VALUE may wind up changing
875 the section when it calls resolve_symbol_value. */
876 fixp->fx_offset += S_GET_VALUE (sym);
877 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
878 #ifdef DEBUG5
879 fprintf (stderr, "\nadjusted fixup:\n");
880 print_fixup (fixp);
881 #endif
882 }
883
884 dump_section_relocs (abfd, sec, stderr);
885 }
886
887 /* fixup_segment()
888
889 Go through all the fixS's in a segment and see which ones can be
890 handled now. (These consist of fixS where we have since discovered
891 the value of a symbol, or the address of the frag involved.)
892 For each one, call md_apply_fix to put the fix into the frag data.
893 Ones that we couldn't completely handle here will be output later
894 by emit_relocations. */
895
896 static void
897 fixup_segment (fixS *fixP, segT this_segment)
898 {
899 valueT add_number;
900 fragS *fragP;
901 segT add_symbol_segment = absolute_section;
902
903 if (fixP != NULL && abs_section_sym == NULL)
904 abs_section_sym = section_symbol (absolute_section);
905
906 /* If the linker is doing the relaxing, we must not do any fixups.
907
908 Well, strictly speaking that's not true -- we could do any that
909 are PC-relative and don't cross regions that could change size.
910 And for the i960 we might be able to turn callx/callj into bal
911 anyways in cases where we know the maximum displacement. */
912 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
913 {
914 for (; fixP; fixP = fixP->fx_next)
915 if (!fixP->fx_done)
916 {
917 if (fixP->fx_addsy == NULL)
918 {
919 /* There was no symbol required by this relocation.
920 However, BFD doesn't really handle relocations
921 without symbols well. So fake up a local symbol in
922 the absolute section. */
923 fixP->fx_addsy = abs_section_sym;
924 }
925 symbol_mark_used_in_reloc (fixP->fx_addsy);
926 if (fixP->fx_subsy != NULL)
927 symbol_mark_used_in_reloc (fixP->fx_subsy);
928 }
929 return;
930 }
931
932 for (; fixP; fixP = fixP->fx_next)
933 {
934 #ifdef DEBUG5
935 fprintf (stderr, "\nprocessing fixup:\n");
936 print_fixup (fixP);
937 #endif
938
939 fragP = fixP->fx_frag;
940 know (fragP);
941 #ifdef TC_VALIDATE_FIX
942 TC_VALIDATE_FIX (fixP, this_segment, skip);
943 #endif
944 add_number = fixP->fx_offset;
945
946 if (fixP->fx_addsy != NULL)
947 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
948
949 if (fixP->fx_subsy != NULL)
950 {
951 segT sub_symbol_segment;
952 resolve_symbol_value (fixP->fx_subsy);
953 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
954 if (fixP->fx_addsy != NULL
955 && sub_symbol_segment == add_symbol_segment
956 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
957 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
958 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
959 {
960 add_number += S_GET_VALUE (fixP->fx_addsy);
961 add_number -= S_GET_VALUE (fixP->fx_subsy);
962 fixP->fx_offset = add_number;
963 fixP->fx_addsy = NULL;
964 fixP->fx_subsy = NULL;
965 #ifdef TC_M68K
966 /* See the comment below about 68k weirdness. */
967 fixP->fx_pcrel = 0;
968 #endif
969 }
970 else if (sub_symbol_segment == absolute_section
971 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
972 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
973 {
974 add_number -= S_GET_VALUE (fixP->fx_subsy);
975 fixP->fx_offset = add_number;
976 fixP->fx_subsy = NULL;
977 }
978 else if (sub_symbol_segment == this_segment
979 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
980 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
981 {
982 add_number -= S_GET_VALUE (fixP->fx_subsy);
983 fixP->fx_offset = (add_number + fixP->fx_dot_value
984 + fixP->fx_dot_frag->fr_address);
985
986 /* Make it pc-relative. If the back-end code has not
987 selected a pc-relative reloc, cancel the adjustment
988 we do later on all pc-relative relocs. */
989 if (0
990 #ifdef TC_M68K
991 /* Do this for m68k even if it's already described
992 as pc-relative. On the m68k, an operand of
993 "pc@(foo-.-2)" should address "foo" in a
994 pc-relative mode. */
995 || 1
996 #endif
997 || !fixP->fx_pcrel)
998 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
999 fixP->fx_subsy = NULL;
1000 fixP->fx_pcrel = 1;
1001 }
1002 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1003 {
1004 if (!md_register_arithmetic
1005 && (add_symbol_segment == reg_section
1006 || sub_symbol_segment == reg_section))
1007 as_bad_where (fixP->fx_file, fixP->fx_line,
1008 _("register value used as expression"));
1009 else
1010 as_bad_where (fixP->fx_file, fixP->fx_line,
1011 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1012 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
1013 segment_name (add_symbol_segment),
1014 S_GET_NAME (fixP->fx_subsy),
1015 segment_name (sub_symbol_segment));
1016 }
1017 else if (sub_symbol_segment != undefined_section
1018 && ! bfd_is_com_section (sub_symbol_segment)
1019 && MD_APPLY_SYM_VALUE (fixP))
1020 add_number -= S_GET_VALUE (fixP->fx_subsy);
1021 }
1022
1023 if (fixP->fx_addsy)
1024 {
1025 if (add_symbol_segment == this_segment
1026 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1027 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1028 {
1029 /* This fixup was made when the symbol's segment was
1030 SEG_UNKNOWN, but it is now in the local segment.
1031 So we know how to do the address without relocation. */
1032 add_number += S_GET_VALUE (fixP->fx_addsy);
1033 fixP->fx_offset = add_number;
1034 if (fixP->fx_pcrel)
1035 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1036 fixP->fx_addsy = NULL;
1037 fixP->fx_pcrel = 0;
1038 }
1039 else if (add_symbol_segment == absolute_section
1040 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1041 && !TC_FORCE_RELOCATION_ABS (fixP))
1042 {
1043 add_number += S_GET_VALUE (fixP->fx_addsy);
1044 fixP->fx_offset = add_number;
1045 fixP->fx_addsy = NULL;
1046 }
1047 else if (add_symbol_segment != undefined_section
1048 && ! bfd_is_com_section (add_symbol_segment)
1049 && MD_APPLY_SYM_VALUE (fixP))
1050 add_number += S_GET_VALUE (fixP->fx_addsy);
1051 }
1052
1053 if (fixP->fx_pcrel)
1054 {
1055 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1056 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1057 {
1058 /* There was no symbol required by this relocation.
1059 However, BFD doesn't really handle relocations
1060 without symbols well. So fake up a local symbol in
1061 the absolute section. */
1062 fixP->fx_addsy = abs_section_sym;
1063 }
1064 }
1065
1066 if (!fixP->fx_done)
1067 md_apply_fix (fixP, &add_number, this_segment);
1068
1069 if (!fixP->fx_done)
1070 {
1071 if (fixP->fx_addsy == NULL)
1072 fixP->fx_addsy = abs_section_sym;
1073 symbol_mark_used_in_reloc (fixP->fx_addsy);
1074 if (fixP->fx_subsy != NULL)
1075 symbol_mark_used_in_reloc (fixP->fx_subsy);
1076 }
1077
1078 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1079 {
1080 if (fixP->fx_size < sizeof (valueT))
1081 {
1082 valueT mask;
1083
1084 mask = 0;
1085 mask--; /* Set all bits to one. */
1086 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1087 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1088 {
1089 char buf[50], buf2[50];
1090 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1091 if (add_number > 1000)
1092 sprint_value (buf2, add_number);
1093 else
1094 sprintf (buf2, "%ld", (long) add_number);
1095 as_bad_where (fixP->fx_file, fixP->fx_line,
1096 _("value of %s too large for field of %d bytes at %s"),
1097 buf2, fixP->fx_size, buf);
1098 } /* Generic error checking. */
1099 }
1100 #ifdef WARN_SIGNED_OVERFLOW_WORD
1101 /* Warn if a .word value is too large when treated as a signed
1102 number. We already know it is not too negative. This is to
1103 catch over-large switches generated by gcc on the 68k. */
1104 if (!flag_signed_overflow_ok
1105 && fixP->fx_size == 2
1106 && add_number > 0x7fff)
1107 as_bad_where (fixP->fx_file, fixP->fx_line,
1108 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1109 (long) add_number,
1110 (long) (fragP->fr_address + fixP->fx_where));
1111 #endif
1112 } /* Not a bit fix. */
1113
1114 #ifdef TC_VALIDATE_FIX
1115 skip: ATTRIBUTE_UNUSED_LABEL
1116 ;
1117 #endif
1118 #ifdef DEBUG5
1119 fprintf (stderr, "result:\n");
1120 print_fixup (fixP);
1121 #endif
1122 } /* For each fixS in this segment. */
1123 }
1124
1125 static void
1126 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1127 asection *sec,
1128 void *xxx ATTRIBUTE_UNUSED)
1129 {
1130 segment_info_type *seginfo = seg_info (sec);
1131
1132 fixup_segment (seginfo->fix_root, sec);
1133 }
1134
1135 static void
1136 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1137 char *file, unsigned int line)
1138 {
1139 char *err;
1140 bfd_reloc_status_type s;
1141 asymbol *sym;
1142
1143 if (reloc->sym_ptr_ptr != NULL
1144 && (sym = *reloc->sym_ptr_ptr) != NULL
1145 && (sym->flags & BSF_KEEP) == 0
1146 && ((sym->flags & BSF_SECTION_SYM) == 0
1147 || (EMIT_SECTION_SYMBOLS
1148 && !bfd_is_abs_section (sym->section))))
1149 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1150
1151 s = bfd_install_relocation (stdoutput, reloc,
1152 fragp->fr_literal, fragp->fr_address,
1153 sec, &err);
1154 switch (s)
1155 {
1156 case bfd_reloc_ok:
1157 break;
1158 case bfd_reloc_overflow:
1159 as_bad_where (file, line, _("relocation overflow"));
1160 break;
1161 case bfd_reloc_outofrange:
1162 as_bad_where (file, line, _("relocation out of range"));
1163 break;
1164 default:
1165 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1166 file, line, s);
1167 }
1168 }
1169
1170 static fragS *
1171 get_frag_for_reloc (fragS *last_frag,
1172 const segment_info_type *seginfo,
1173 const struct reloc_list *r)
1174 {
1175 fragS *f;
1176
1177 for (f = last_frag; f != NULL; f = f->fr_next)
1178 if (f->fr_address <= r->u.b.r.address
1179 && r->u.b.r.address < f->fr_address + f->fr_fix)
1180 return f;
1181
1182 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1183 if (f->fr_address <= r->u.b.r.address
1184 && r->u.b.r.address < f->fr_address + f->fr_fix)
1185 return f;
1186
1187 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1188 if (f->fr_address <= r->u.b.r.address
1189 && r->u.b.r.address <= f->fr_address + f->fr_fix)
1190 return f;
1191
1192 as_bad_where (r->file, r->line,
1193 _("reloc not within (fixed part of) section"));
1194 return NULL;
1195 }
1196
1197 static void
1198 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1199 {
1200 segment_info_type *seginfo = seg_info (sec);
1201 unsigned int n;
1202 struct reloc_list *my_reloc_list, **rp, *r;
1203 arelent **relocs;
1204 fixS *fixp;
1205 fragS *last_frag;
1206
1207 /* If seginfo is NULL, we did not create this section; don't do
1208 anything with it. */
1209 if (seginfo == NULL)
1210 return;
1211
1212 n = 0;
1213 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1214 if (!fixp->fx_done)
1215 n++;
1216
1217 #ifdef RELOC_EXPANSION_POSSIBLE
1218 n *= MAX_RELOC_EXPANSION;
1219 #endif
1220
1221 /* Extract relocs for this section from reloc_list. */
1222 rp = &reloc_list;
1223 my_reloc_list = NULL;
1224 while ((r = *rp) != NULL)
1225 {
1226 if (r->u.b.sec == sec)
1227 {
1228 *rp = r->next;
1229 r->next = my_reloc_list;
1230 my_reloc_list = r;
1231 n++;
1232 }
1233 else
1234 rp = &r->next;
1235 }
1236
1237 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
1238
1239 n = 0;
1240 r = my_reloc_list;
1241 last_frag = NULL;
1242 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1243 {
1244 int fx_size, slack;
1245 offsetT loc;
1246 arelent **reloc;
1247 #ifndef RELOC_EXPANSION_POSSIBLE
1248 arelent *rel;
1249
1250 reloc = &rel;
1251 #endif
1252
1253 if (fixp->fx_done)
1254 continue;
1255
1256 fx_size = fixp->fx_size;
1257 slack = TC_FX_SIZE_SLACK (fixp);
1258 if (slack > 0)
1259 fx_size = fx_size > slack ? fx_size - slack : 0;
1260 loc = fixp->fx_where + fx_size;
1261 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1262 as_bad_where (fixp->fx_file, fixp->fx_line,
1263 _("internal error: fixup not contained within frag"));
1264
1265 #ifndef RELOC_EXPANSION_POSSIBLE
1266 *reloc = tc_gen_reloc (sec, fixp);
1267 #else
1268 reloc = tc_gen_reloc (sec, fixp);
1269 #endif
1270
1271 while (*reloc)
1272 {
1273 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1274 {
1275 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1276 if (f != NULL)
1277 {
1278 last_frag = f;
1279 relocs[n++] = &r->u.b.r;
1280 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1281 }
1282 r = r->next;
1283 }
1284 relocs[n++] = *reloc;
1285 install_reloc (sec, *reloc, fixp->fx_frag,
1286 fixp->fx_file, fixp->fx_line);
1287 #ifndef RELOC_EXPANSION_POSSIBLE
1288 break;
1289 #else
1290 reloc++;
1291 #endif
1292 }
1293 }
1294
1295 while (r != NULL)
1296 {
1297 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1298 if (f != NULL)
1299 {
1300 last_frag = f;
1301 relocs[n++] = &r->u.b.r;
1302 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1303 }
1304 r = r->next;
1305 }
1306
1307 #ifdef DEBUG4
1308 {
1309 unsigned int k, j, nsyms;
1310 asymbol **sympp;
1311 sympp = bfd_get_outsymbols (stdoutput);
1312 nsyms = bfd_get_symcount (stdoutput);
1313 for (k = 0; k < n; k++)
1314 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1315 {
1316 for (j = 0; j < nsyms; j++)
1317 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1318 break;
1319 if (j == nsyms)
1320 abort ();
1321 }
1322 }
1323 #endif
1324
1325 if (n)
1326 {
1327 flagword flags = bfd_get_section_flags (abfd, sec);
1328 flags |= SEC_RELOC;
1329 bfd_set_section_flags (abfd, sec, flags);
1330 bfd_set_reloc (stdoutput, sec, relocs, n);
1331 }
1332
1333 #ifdef SET_SECTION_RELOCS
1334 SET_SECTION_RELOCS (sec, relocs, n);
1335 #endif
1336
1337 #ifdef DEBUG3
1338 {
1339 unsigned int k;
1340
1341 fprintf (stderr, "relocs for sec %s\n", sec->name);
1342 for (k = 0; k < n; k++)
1343 {
1344 arelent *rel = relocs[k];
1345 asymbol *s = *rel->sym_ptr_ptr;
1346 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1347 k, rel, (unsigned long)rel->address, s->name,
1348 (unsigned long)rel->addend);
1349 }
1350 }
1351 #endif
1352 }
1353
1354 static int
1355 compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
1356 fragS **last_newf, struct obstack *ob)
1357 {
1358 int out_size;
1359 int total_out_size = 0;
1360 fragS *f = *last_newf;
1361 char *next_out;
1362 int avail_out;
1363
1364 /* Call the compression routine repeatedly until it has finished
1365 processing the frag. */
1366 while (in_size > 0)
1367 {
1368 /* Reserve all the space available in the current chunk.
1369 If none is available, start a new frag. */
1370 avail_out = obstack_room (ob);
1371 if (avail_out <= 0)
1372 {
1373 obstack_finish (ob);
1374 f = frag_alloc (ob);
1375 f->fr_type = rs_fill;
1376 (*last_newf)->fr_next = f;
1377 *last_newf = f;
1378 avail_out = obstack_room (ob);
1379 }
1380 if (avail_out <= 0)
1381 as_fatal (_("can't extend frag"));
1382 next_out = obstack_next_free (ob);
1383 obstack_blank_fast (ob, avail_out);
1384 out_size = compress_data (strm, &contents, &in_size,
1385 &next_out, &avail_out);
1386 if (out_size < 0)
1387 return -1;
1388
1389 f->fr_fix += out_size;
1390 total_out_size += out_size;
1391
1392 /* Return unused space. */
1393 if (avail_out > 0)
1394 obstack_blank_fast (ob, -avail_out);
1395 }
1396
1397 return total_out_size;
1398 }
1399
1400 static void
1401 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1402 {
1403 segment_info_type *seginfo = seg_info (sec);
1404 fragS *f;
1405 fragS *first_newf;
1406 fragS *last_newf;
1407 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1408 bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
1409 bfd_size_type compressed_size;
1410 const char *section_name;
1411 char *compressed_name;
1412 char *header;
1413 struct z_stream_s *strm;
1414 int x;
1415 flagword flags = bfd_get_section_flags (abfd, sec);
1416 unsigned int header_size, compression_header_size;
1417 /* Maximimum compression header is 24 bytes. */
1418 bfd_byte compression_header[24];
1419
1420 if (seginfo == NULL
1421 || sec->size < 32
1422 || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
1423 return;
1424
1425 section_name = bfd_get_section_name (stdoutput, sec);
1426 if (strncmp (section_name, ".debug_", 7) != 0)
1427 return;
1428
1429 strm = compress_init ();
1430 if (strm == NULL)
1431 return;
1432
1433 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
1434 stdoutput->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI;
1435 else
1436 stdoutput->flags |= BFD_COMPRESS;
1437 compression_header_size
1438 = bfd_get_compression_header_size (stdoutput, NULL);
1439
1440 /* Create a new frag to contain the "ZLIB" header. */
1441 header_size = 12 + compression_header_size;
1442 first_newf = frag_alloc (ob);
1443 if (obstack_room (ob) < header_size)
1444 first_newf = frag_alloc (ob);
1445 if (obstack_room (ob) < header_size)
1446 as_fatal (_("can't extend frag %u chars"), header_size);
1447 last_newf = first_newf;
1448 obstack_blank_fast (ob, header_size);
1449 last_newf->fr_type = rs_fill;
1450 last_newf->fr_fix = header_size;
1451 header = last_newf->fr_literal;
1452 compressed_size = header_size;
1453
1454 /* Stream the frags through the compression engine, adding new frags
1455 as necessary to accomodate the compressed output. */
1456 for (f = seginfo->frchainP->frch_root;
1457 f;
1458 f = f->fr_next)
1459 {
1460 offsetT fill_size;
1461 char *fill_literal;
1462 offsetT count;
1463 int out_size;
1464
1465 gas_assert (f->fr_type == rs_fill);
1466 if (f->fr_fix)
1467 {
1468 out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
1469 &last_newf, ob);
1470 if (out_size < 0)
1471 return;
1472 compressed_size += out_size;
1473 }
1474 fill_literal = f->fr_literal + f->fr_fix;
1475 fill_size = f->fr_var;
1476 count = f->fr_offset;
1477 gas_assert (count >= 0);
1478 if (fill_size && count)
1479 {
1480 while (count--)
1481 {
1482 out_size = compress_frag (strm, fill_literal, (int) fill_size,
1483 &last_newf, ob);
1484 if (out_size < 0)
1485 return;
1486 compressed_size += out_size;
1487 }
1488 }
1489 }
1490
1491 /* Flush the compression state. */
1492 for (;;)
1493 {
1494 int avail_out;
1495 char *next_out;
1496 int out_size;
1497
1498 /* Reserve all the space available in the current chunk.
1499 If none is available, start a new frag. */
1500 avail_out = obstack_room (ob);
1501 if (avail_out <= 0)
1502 {
1503 fragS *newf;
1504
1505 obstack_finish (ob);
1506 newf = frag_alloc (ob);
1507 newf->fr_type = rs_fill;
1508 last_newf->fr_next = newf;
1509 last_newf = newf;
1510 avail_out = obstack_room (ob);
1511 }
1512 if (avail_out <= 0)
1513 as_fatal (_("can't extend frag"));
1514 next_out = obstack_next_free (ob);
1515 obstack_blank_fast (ob, avail_out);
1516 x = compress_finish (strm, &next_out, &avail_out, &out_size);
1517 if (x < 0)
1518 return;
1519
1520 last_newf->fr_fix += out_size;
1521 compressed_size += out_size;
1522
1523 /* Return unused space. */
1524 if (avail_out > 0)
1525 obstack_blank_fast (ob, -avail_out);
1526
1527 if (x == 0)
1528 break;
1529 }
1530
1531 /* PR binutils/18087: If compression didn't make the section smaller,
1532 just keep it uncompressed. */
1533 if (compressed_size >= uncompressed_size)
1534 return;
1535
1536 if (compression_header_size)
1537 memcpy (header, compression_header, compression_header_size);
1538 memcpy (header + compression_header_size, "ZLIB", 4);
1539 bfd_putb64 (uncompressed_size, header + compression_header_size + 4);
1540
1541 /* Replace the uncompressed frag list with the compressed frag list. */
1542 seginfo->frchainP->frch_root = first_newf;
1543 seginfo->frchainP->frch_last = last_newf;
1544
1545 /* Update the section size and its name. */
1546 bfd_update_compression_header (abfd, (bfd_byte *) header, sec);
1547 x = bfd_set_section_size (abfd, sec, compressed_size);
1548 gas_assert (x);
1549 if (!compression_header_size)
1550 {
1551 compressed_name = (char *) xmalloc (strlen (section_name) + 2);
1552 compressed_name[0] = '.';
1553 compressed_name[1] = 'z';
1554 strcpy (compressed_name + 2, section_name + 1);
1555 bfd_section_name (stdoutput, sec) = compressed_name;
1556 }
1557 }
1558
1559 static void
1560 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1561 asection *sec,
1562 void *xxx ATTRIBUTE_UNUSED)
1563 {
1564 segment_info_type *seginfo = seg_info (sec);
1565 addressT offset = 0;
1566 fragS *f;
1567
1568 /* Write out the frags. */
1569 if (seginfo == NULL
1570 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1571 return;
1572
1573 for (f = seginfo->frchainP->frch_root;
1574 f;
1575 f = f->fr_next)
1576 {
1577 int x;
1578 addressT fill_size;
1579 char *fill_literal;
1580 offsetT count;
1581
1582 gas_assert (f->fr_type == rs_fill);
1583 if (f->fr_fix)
1584 {
1585 x = bfd_set_section_contents (stdoutput, sec,
1586 f->fr_literal, (file_ptr) offset,
1587 (bfd_size_type) f->fr_fix);
1588 if (!x)
1589 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1590 bfd_errmsg (bfd_get_error ()));
1591 offset += f->fr_fix;
1592 }
1593 fill_literal = f->fr_literal + f->fr_fix;
1594 fill_size = f->fr_var;
1595 count = f->fr_offset;
1596 gas_assert (count >= 0);
1597 if (fill_size && count)
1598 {
1599 char buf[256];
1600 if (fill_size > sizeof (buf))
1601 {
1602 /* Do it the old way. Can this ever happen? */
1603 while (count--)
1604 {
1605 x = bfd_set_section_contents (stdoutput, sec,
1606 fill_literal,
1607 (file_ptr) offset,
1608 (bfd_size_type) fill_size);
1609 if (!x)
1610 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1611 bfd_errmsg (bfd_get_error ()));
1612 offset += fill_size;
1613 }
1614 }
1615 else
1616 {
1617 /* Build a buffer full of fill objects and output it as
1618 often as necessary. This saves on the overhead of
1619 potentially lots of bfd_set_section_contents calls. */
1620 int n_per_buf, i;
1621 if (fill_size == 1)
1622 {
1623 n_per_buf = sizeof (buf);
1624 memset (buf, *fill_literal, n_per_buf);
1625 }
1626 else
1627 {
1628 char *bufp;
1629 n_per_buf = sizeof (buf) / fill_size;
1630 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1631 memcpy (bufp, fill_literal, fill_size);
1632 }
1633 for (; count > 0; count -= n_per_buf)
1634 {
1635 n_per_buf = n_per_buf > count ? count : n_per_buf;
1636 x = bfd_set_section_contents
1637 (stdoutput, sec, buf, (file_ptr) offset,
1638 (bfd_size_type) n_per_buf * fill_size);
1639 if (!x)
1640 as_fatal (_("cannot write to output file '%s': %s"),
1641 stdoutput->filename,
1642 bfd_errmsg (bfd_get_error ()));
1643 offset += n_per_buf * fill_size;
1644 }
1645 }
1646 }
1647 }
1648 }
1649
1650 static void
1651 merge_data_into_text (void)
1652 {
1653 seg_info (text_section)->frchainP->frch_last->fr_next =
1654 seg_info (data_section)->frchainP->frch_root;
1655 seg_info (text_section)->frchainP->frch_last =
1656 seg_info (data_section)->frchainP->frch_last;
1657 seg_info (data_section)->frchainP = 0;
1658 }
1659
1660 static void
1661 set_symtab (void)
1662 {
1663 int nsyms;
1664 asymbol **asympp;
1665 symbolS *symp;
1666 bfd_boolean result;
1667
1668 /* Count symbols. We can't rely on a count made by the loop in
1669 write_object_file, because *_frob_file may add a new symbol or
1670 two. */
1671 nsyms = 0;
1672 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1673 nsyms++;
1674
1675 if (nsyms)
1676 {
1677 int i;
1678 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1679
1680 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1681 symp = symbol_rootP;
1682 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1683 {
1684 asympp[i] = symbol_get_bfdsym (symp);
1685 if (asympp[i]->flags != BSF_SECTION_SYM
1686 || !(bfd_is_const_section (asympp[i]->section)
1687 && asympp[i]->section->symbol == asympp[i]))
1688 asympp[i]->flags |= BSF_KEEP;
1689 symbol_mark_written (symp);
1690 }
1691 }
1692 else
1693 asympp = 0;
1694 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1695 gas_assert (result);
1696 symbol_table_frozen = 1;
1697 }
1698
1699 /* Finish the subsegments. After every sub-segment, we fake an
1700 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1701 ".fill 0" because that is the kind of frag that requires least
1702 thought. ".align" frags like to have a following frag since that
1703 makes calculating their intended length trivial. */
1704
1705 #ifndef SUB_SEGMENT_ALIGN
1706 #ifdef HANDLE_ALIGN
1707 /* The last subsegment gets an alignment corresponding to the alignment
1708 of the section. This allows proper nop-filling at the end of
1709 code-bearing sections. */
1710 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1711 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1712 ? get_recorded_alignment (SEG) \
1713 : 0)
1714 #else
1715 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1716 #endif
1717 #endif
1718
1719 static void
1720 subsegs_finish_section (asection *s)
1721 {
1722 struct frchain *frchainP;
1723 segment_info_type *seginfo = seg_info (s);
1724 if (!seginfo)
1725 return;
1726
1727 for (frchainP = seginfo->frchainP;
1728 frchainP != NULL;
1729 frchainP = frchainP->frch_next)
1730 {
1731 int alignment = 0;
1732
1733 subseg_set (s, frchainP->frch_subseg);
1734
1735 /* This now gets called even if we had errors. In that case,
1736 any alignment is meaningless, and, moreover, will look weird
1737 if we are generating a listing. */
1738 if (!had_errors ())
1739 {
1740 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1741 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1742 && now_seg->entsize)
1743 {
1744 unsigned int entsize = now_seg->entsize;
1745 int entalign = 0;
1746
1747 while ((entsize & 1) == 0)
1748 {
1749 ++entalign;
1750 entsize >>= 1;
1751 }
1752
1753 if (entalign > alignment)
1754 alignment = entalign;
1755 }
1756 }
1757
1758 if (subseg_text_p (now_seg))
1759 frag_align_code (alignment, 0);
1760 else
1761 frag_align (alignment, 0, 0);
1762
1763 /* frag_align will have left a new frag.
1764 Use this last frag for an empty ".fill".
1765
1766 For this segment ...
1767 Create a last frag. Do not leave a "being filled in frag". */
1768 frag_wane (frag_now);
1769 frag_now->fr_fix = 0;
1770 know (frag_now->fr_next == NULL);
1771 }
1772 }
1773
1774 static void
1775 subsegs_finish (void)
1776 {
1777 asection *s;
1778
1779 for (s = stdoutput->sections; s; s = s->next)
1780 subsegs_finish_section (s);
1781 }
1782
1783 #ifdef OBJ_ELF
1784 static void
1785 create_obj_attrs_section (void)
1786 {
1787 segT s;
1788 char *p;
1789 offsetT size;
1790 const char *name;
1791
1792 size = bfd_elf_obj_attr_size (stdoutput);
1793 if (size)
1794 {
1795 name = get_elf_backend_data (stdoutput)->obj_attrs_section;
1796 if (!name)
1797 name = ".gnu.attributes";
1798 s = subseg_new (name, 0);
1799 elf_section_type (s)
1800 = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
1801 bfd_set_section_flags (stdoutput, s, SEC_READONLY | SEC_DATA);
1802 frag_now_fix ();
1803 p = frag_more (size);
1804 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
1805
1806 subsegs_finish_section (s);
1807 relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
1808 size_seg (stdoutput, s, NULL);
1809 }
1810 }
1811 #endif
1812
1813 /* Write the object file. */
1814
1815 void
1816 write_object_file (void)
1817 {
1818 struct relax_seg_info rsi;
1819 #ifndef WORKING_DOT_WORD
1820 fragS *fragP; /* Track along all frags. */
1821 #endif
1822
1823 subsegs_finish ();
1824
1825 #ifdef md_pre_output_hook
1826 md_pre_output_hook;
1827 #endif
1828
1829 #ifdef md_pre_relax_hook
1830 md_pre_relax_hook;
1831 #endif
1832
1833 /* From now on, we don't care about sub-segments. Build one frag chain
1834 for each segment. Linked thru fr_next. */
1835
1836 /* Remove the sections created by gas for its own purposes. */
1837 {
1838 int i;
1839
1840 bfd_section_list_remove (stdoutput, reg_section);
1841 bfd_section_list_remove (stdoutput, expr_section);
1842 stdoutput->section_count -= 2;
1843 i = 0;
1844 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1845 }
1846
1847 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1848
1849 /* We have two segments. If user gave -R flag, then we must put the
1850 data frags into the text segment. Do this before relaxing so
1851 we know to take advantage of -R and make shorter addresses. */
1852 if (flag_readonly_data_in_text)
1853 {
1854 merge_data_into_text ();
1855 }
1856
1857 rsi.pass = 0;
1858 while (1)
1859 {
1860 #ifndef WORKING_DOT_WORD
1861 /* We need to reset the markers in the broken word list and
1862 associated frags between calls to relax_segment (via
1863 relax_seg). Since the broken word list is global, we do it
1864 once per round, rather than locally in relax_segment for each
1865 segment. */
1866 struct broken_word *brokp;
1867
1868 for (brokp = broken_words;
1869 brokp != (struct broken_word *) NULL;
1870 brokp = brokp->next_broken_word)
1871 {
1872 brokp->added = 0;
1873
1874 if (brokp->dispfrag != (fragS *) NULL
1875 && brokp->dispfrag->fr_type == rs_broken_word)
1876 brokp->dispfrag->fr_subtype = 0;
1877 }
1878 #endif
1879
1880 rsi.changed = 0;
1881 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1882 rsi.pass++;
1883 if (!rsi.changed)
1884 break;
1885 }
1886
1887 /* Note - Most ports will use the default value of
1888 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1889 local symbols to be resolved, removing their frag information.
1890 Some ports however, will not have finished relaxing all of
1891 their frags and will still need the local symbol frag
1892 information. These ports can set
1893 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1894 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1895
1896 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1897
1898 /* Relaxation has completed. Freeze all syms. */
1899 finalize_syms = 1;
1900
1901 #ifdef md_post_relax_hook
1902 md_post_relax_hook;
1903 #endif
1904
1905 #ifdef OBJ_ELF
1906 if (IS_ELF)
1907 create_obj_attrs_section ();
1908 #endif
1909
1910 #ifndef WORKING_DOT_WORD
1911 {
1912 struct broken_word *lie;
1913 struct broken_word **prevP;
1914
1915 prevP = &broken_words;
1916 for (lie = broken_words; lie; lie = lie->next_broken_word)
1917 if (!lie->added)
1918 {
1919 expressionS exp;
1920
1921 subseg_change (lie->seg, lie->subseg);
1922 exp.X_op = O_subtract;
1923 exp.X_add_symbol = lie->add;
1924 exp.X_op_symbol = lie->sub;
1925 exp.X_add_number = lie->addnum;
1926 #ifdef TC_CONS_FIX_NEW
1927 TC_CONS_FIX_NEW (lie->frag,
1928 lie->word_goes_here - lie->frag->fr_literal,
1929 2, &exp, TC_PARSE_CONS_RETURN_NONE);
1930 #else
1931 fix_new_exp (lie->frag,
1932 lie->word_goes_here - lie->frag->fr_literal,
1933 2, &exp, 0, BFD_RELOC_16);
1934 #endif
1935 *prevP = lie->next_broken_word;
1936 }
1937 else
1938 prevP = &(lie->next_broken_word);
1939
1940 for (lie = broken_words; lie;)
1941 {
1942 struct broken_word *untruth;
1943 char *table_ptr;
1944 addressT table_addr;
1945 addressT from_addr, to_addr;
1946 int n, m;
1947
1948 subseg_change (lie->seg, lie->subseg);
1949 fragP = lie->dispfrag;
1950
1951 /* Find out how many broken_words go here. */
1952 n = 0;
1953 for (untruth = lie;
1954 untruth && untruth->dispfrag == fragP;
1955 untruth = untruth->next_broken_word)
1956 if (untruth->added == 1)
1957 n++;
1958
1959 table_ptr = lie->dispfrag->fr_opcode;
1960 table_addr = (lie->dispfrag->fr_address
1961 + (table_ptr - lie->dispfrag->fr_literal));
1962 /* Create the jump around the long jumps. This is a short
1963 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1964 from_addr = table_addr;
1965 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1966 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1967 lie->add);
1968 table_ptr += md_short_jump_size;
1969 table_addr += md_short_jump_size;
1970
1971 for (m = 0;
1972 lie && lie->dispfrag == fragP;
1973 m++, lie = lie->next_broken_word)
1974 {
1975 if (lie->added == 2)
1976 continue;
1977 /* Patch the jump table. */
1978 for (untruth = (struct broken_word *) (fragP->fr_symbol);
1979 untruth && untruth->dispfrag == fragP;
1980 untruth = untruth->next_broken_word)
1981 {
1982 if (untruth->use_jump == lie)
1983 {
1984 /* This is the offset from ??? to table_ptr+0.
1985 The target is the same for all users of this
1986 md_long_jump, but the "sub" bases (and hence the
1987 offsets) may be different. */
1988 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
1989 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1990 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
1991 #endif
1992 md_number_to_chars (untruth->word_goes_here, to_word, 2);
1993 }
1994 }
1995
1996 /* Install the long jump. */
1997 /* This is a long jump from table_ptr+0 to the final target. */
1998 from_addr = table_addr;
1999 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
2000 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2001 lie->add);
2002 table_ptr += md_long_jump_size;
2003 table_addr += md_long_jump_size;
2004 }
2005 }
2006 }
2007 #endif /* not WORKING_DOT_WORD */
2008
2009 /* Resolve symbol values. This needs to be done before processing
2010 the relocations. */
2011 if (symbol_rootP)
2012 {
2013 symbolS *symp;
2014
2015 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2016 resolve_symbol_value (symp);
2017 }
2018 resolve_local_symbol_values ();
2019 resolve_reloc_expr_symbols ();
2020
2021 PROGRESS (1);
2022
2023 #ifdef tc_frob_file_before_adjust
2024 tc_frob_file_before_adjust ();
2025 #endif
2026 #ifdef obj_frob_file_before_adjust
2027 obj_frob_file_before_adjust ();
2028 #endif
2029
2030 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
2031
2032 #ifdef tc_frob_file_before_fix
2033 tc_frob_file_before_fix ();
2034 #endif
2035 #ifdef obj_frob_file_before_fix
2036 obj_frob_file_before_fix ();
2037 #endif
2038
2039 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2040
2041 /* Set up symbol table, and write it out. */
2042 if (symbol_rootP)
2043 {
2044 symbolS *symp;
2045 bfd_boolean skip_next_symbol = FALSE;
2046
2047 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2048 {
2049 int punt = 0;
2050 const char *name;
2051
2052 if (skip_next_symbol)
2053 {
2054 /* Don't do anything besides moving the value of the
2055 symbol from the GAS value-field to the BFD value-field. */
2056 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2057 skip_next_symbol = FALSE;
2058 continue;
2059 }
2060
2061 if (symbol_mri_common_p (symp))
2062 {
2063 if (S_IS_EXTERNAL (symp))
2064 as_bad (_("%s: global symbols not supported in common sections"),
2065 S_GET_NAME (symp));
2066 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2067 continue;
2068 }
2069
2070 name = S_GET_NAME (symp);
2071 if (name)
2072 {
2073 const char *name2 =
2074 decode_local_label_name ((char *) S_GET_NAME (symp));
2075 /* They only differ if `name' is a fb or dollar local
2076 label name. */
2077 if (name2 != name && ! S_IS_DEFINED (symp))
2078 as_bad (_("local label `%s' is not defined"), name2);
2079 }
2080
2081 /* Do it again, because adjust_reloc_syms might introduce
2082 more symbols. They'll probably only be section symbols,
2083 but they'll still need to have the values computed. */
2084 resolve_symbol_value (symp);
2085
2086 /* Skip symbols which were equated to undefined or common
2087 symbols. */
2088 if (symbol_equated_reloc_p (symp)
2089 || S_IS_WEAKREFR (symp))
2090 {
2091 const char *sname = S_GET_NAME (symp);
2092
2093 if (S_IS_COMMON (symp)
2094 && !TC_FAKE_LABEL (sname)
2095 && !S_IS_WEAKREFR (symp)
2096 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
2097 {
2098 expressionS *e = symbol_get_value_expression (symp);
2099
2100 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
2101 sname, S_GET_NAME (e->X_add_symbol));
2102 }
2103 if (S_GET_SEGMENT (symp) == reg_section)
2104 {
2105 /* Report error only if we know the symbol name. */
2106 if (S_GET_NAME (symp) != reg_section->name)
2107 as_bad (_("can't make global register symbol `%s'"),
2108 sname);
2109 }
2110 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2111 continue;
2112 }
2113
2114 #ifdef obj_frob_symbol
2115 obj_frob_symbol (symp, punt);
2116 #endif
2117 #ifdef tc_frob_symbol
2118 if (! punt || symbol_used_in_reloc_p (symp))
2119 tc_frob_symbol (symp, punt);
2120 #endif
2121
2122 /* If we don't want to keep this symbol, splice it out of
2123 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2124 want section symbols. Otherwise, we skip local symbols
2125 and symbols that the frob_symbol macros told us to punt,
2126 but we keep such symbols if they are used in relocs. */
2127 if (symp == abs_section_sym
2128 || (! EMIT_SECTION_SYMBOLS
2129 && symbol_section_p (symp))
2130 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2131 opposites. Sometimes the former checks flags and the
2132 latter examines the name... */
2133 || (!S_IS_EXTERNAL (symp)
2134 && (punt || S_IS_LOCAL (symp) ||
2135 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2136 && ! symbol_used_in_reloc_p (symp)))
2137 {
2138 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2139
2140 /* After symbol_remove, symbol_next(symp) still returns
2141 the one that came after it in the chain. So we don't
2142 need to do any extra cleanup work here. */
2143 continue;
2144 }
2145
2146 /* Make sure we really got a value for the symbol. */
2147 if (! symbol_resolved_p (symp))
2148 {
2149 as_bad (_("can't resolve value for symbol `%s'"),
2150 S_GET_NAME (symp));
2151 symbol_mark_resolved (symp);
2152 }
2153
2154 /* Set the value into the BFD symbol. Up til now the value
2155 has only been kept in the gas symbolS struct. */
2156 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2157
2158 /* A warning construct is a warning symbol followed by the
2159 symbol warned about. Don't let anything object-format or
2160 target-specific muck with it; it's ready for output. */
2161 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2162 skip_next_symbol = TRUE;
2163 }
2164 }
2165
2166 PROGRESS (1);
2167
2168 /* Now do any format-specific adjustments to the symbol table, such
2169 as adding file symbols. */
2170 #ifdef tc_adjust_symtab
2171 tc_adjust_symtab ();
2172 #endif
2173 #ifdef obj_adjust_symtab
2174 obj_adjust_symtab ();
2175 #endif
2176
2177 /* Stop if there is an error. */
2178 if (had_errors ())
2179 return;
2180
2181 /* Now that all the sizes are known, and contents correct, we can
2182 start writing to the file. */
2183 set_symtab ();
2184
2185 /* If *_frob_file changes the symbol value at this point, it is
2186 responsible for moving the changed value into symp->bsym->value
2187 as well. Hopefully all symbol value changing can be done in
2188 *_frob_symbol. */
2189 #ifdef tc_frob_file
2190 tc_frob_file ();
2191 #endif
2192 #ifdef obj_frob_file
2193 obj_frob_file ();
2194 #endif
2195 #ifdef obj_coff_generate_pdata
2196 obj_coff_generate_pdata ();
2197 #endif
2198 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2199
2200 #ifdef tc_frob_file_after_relocs
2201 tc_frob_file_after_relocs ();
2202 #endif
2203 #ifdef obj_frob_file_after_relocs
2204 obj_frob_file_after_relocs ();
2205 #endif
2206
2207 /* Once all relocations have been written, we can compress the
2208 contents of the debug sections. This needs to be done before
2209 we start writing any sections, because it will affect the file
2210 layout, which is fixed once we start writing contents. */
2211 if (flag_compress_debug)
2212 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2213
2214 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2215 }
2216
2217 #ifdef TC_GENERIC_RELAX_TABLE
2218 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2219
2220 long
2221 relax_frag (segT segment, fragS *fragP, long stretch)
2222 {
2223 const relax_typeS *this_type;
2224 const relax_typeS *start_type;
2225 relax_substateT next_state;
2226 relax_substateT this_state;
2227 offsetT growth;
2228 offsetT aim;
2229 addressT target;
2230 addressT address;
2231 symbolS *symbolP;
2232 const relax_typeS *table;
2233
2234 target = fragP->fr_offset;
2235 address = fragP->fr_address;
2236 table = TC_GENERIC_RELAX_TABLE;
2237 this_state = fragP->fr_subtype;
2238 start_type = this_type = table + this_state;
2239 symbolP = fragP->fr_symbol;
2240
2241 if (symbolP)
2242 {
2243 fragS *sym_frag;
2244
2245 sym_frag = symbol_get_frag (symbolP);
2246
2247 #ifndef DIFF_EXPR_OK
2248 know (sym_frag != NULL);
2249 #endif
2250 know (S_GET_SEGMENT (symbolP) != absolute_section
2251 || sym_frag == &zero_address_frag);
2252 target += S_GET_VALUE (symbolP);
2253
2254 /* If SYM_FRAG has yet to be reached on this pass, assume it
2255 will move by STRETCH just as we did, unless there is an
2256 alignment frag between here and SYM_FRAG. An alignment may
2257 well absorb any STRETCH, and we don't want to choose a larger
2258 branch insn by overestimating the needed reach of this
2259 branch. It isn't critical to calculate TARGET exactly; We
2260 know we'll be doing another pass if STRETCH is non-zero. */
2261
2262 if (stretch != 0
2263 && sym_frag->relax_marker != fragP->relax_marker
2264 && S_GET_SEGMENT (symbolP) == segment)
2265 {
2266 if (stretch < 0
2267 || sym_frag->region == fragP->region)
2268 target += stretch;
2269 /* If we get here we know we have a forward branch. This
2270 relax pass may have stretched previous instructions so
2271 far that omitting STRETCH would make the branch
2272 negative. Don't allow this in case the negative reach is
2273 large enough to require a larger branch instruction. */
2274 else if (target < address)
2275 target = fragP->fr_next->fr_address + stretch;
2276 }
2277 }
2278
2279 aim = target - address - fragP->fr_fix;
2280 #ifdef TC_PCREL_ADJUST
2281 /* Currently only the ns32k family needs this. */
2282 aim += TC_PCREL_ADJUST (fragP);
2283 #endif
2284
2285 #ifdef md_prepare_relax_scan
2286 /* Formerly called M68K_AIM_KLUDGE. */
2287 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2288 #endif
2289
2290 if (aim < 0)
2291 {
2292 /* Look backwards. */
2293 for (next_state = this_type->rlx_more; next_state;)
2294 if (aim >= this_type->rlx_backward)
2295 next_state = 0;
2296 else
2297 {
2298 /* Grow to next state. */
2299 this_state = next_state;
2300 this_type = table + this_state;
2301 next_state = this_type->rlx_more;
2302 }
2303 }
2304 else
2305 {
2306 /* Look forwards. */
2307 for (next_state = this_type->rlx_more; next_state;)
2308 if (aim <= this_type->rlx_forward)
2309 next_state = 0;
2310 else
2311 {
2312 /* Grow to next state. */
2313 this_state = next_state;
2314 this_type = table + this_state;
2315 next_state = this_type->rlx_more;
2316 }
2317 }
2318
2319 growth = this_type->rlx_length - start_type->rlx_length;
2320 if (growth != 0)
2321 fragP->fr_subtype = this_state;
2322 return growth;
2323 }
2324
2325 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2326
2327 /* Relax_align. Advance location counter to next address that has 'alignment'
2328 lowest order bits all 0s, return size of adjustment made. */
2329 static relax_addressT
2330 relax_align (relax_addressT address, /* Address now. */
2331 int alignment /* Alignment (binary). */)
2332 {
2333 relax_addressT mask;
2334 relax_addressT new_address;
2335
2336 mask = ~((relax_addressT) ~0 << alignment);
2337 new_address = (address + mask) & (~mask);
2338 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2339 if (linkrelax)
2340 /* We must provide lots of padding, so the linker can discard it
2341 when needed. The linker will not add extra space, ever. */
2342 new_address += (1 << alignment);
2343 #endif
2344 return (new_address - address);
2345 }
2346
2347 /* Now we have a segment, not a crowd of sub-segments, we can make
2348 fr_address values.
2349
2350 Relax the frags.
2351
2352 After this, all frags in this segment have addresses that are correct
2353 within the segment. Since segments live in different file addresses,
2354 these frag addresses may not be the same as final object-file
2355 addresses. */
2356
2357 int
2358 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2359 {
2360 unsigned long frag_count;
2361 struct frag *fragP;
2362 relax_addressT address;
2363 int region;
2364 int ret;
2365
2366 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2367 subseg_change (segment, 0);
2368
2369 /* For each frag in segment: count and store (a 1st guess of)
2370 fr_address. */
2371 address = 0;
2372 region = 0;
2373 for (frag_count = 0, fragP = segment_frag_root;
2374 fragP;
2375 fragP = fragP->fr_next, frag_count ++)
2376 {
2377 fragP->region = region;
2378 fragP->relax_marker = 0;
2379 fragP->fr_address = address;
2380 address += fragP->fr_fix;
2381
2382 switch (fragP->fr_type)
2383 {
2384 case rs_fill:
2385 address += fragP->fr_offset * fragP->fr_var;
2386 break;
2387
2388 case rs_align:
2389 case rs_align_code:
2390 case rs_align_test:
2391 {
2392 addressT offset = relax_align (address, (int) fragP->fr_offset);
2393
2394 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2395 offset = 0;
2396
2397 if (offset % fragP->fr_var != 0)
2398 {
2399 as_bad_where (fragP->fr_file, fragP->fr_line,
2400 _("alignment padding (%lu bytes) not a multiple of %ld"),
2401 (unsigned long) offset, (long) fragP->fr_var);
2402 offset -= (offset % fragP->fr_var);
2403 }
2404
2405 address += offset;
2406 region += 1;
2407 }
2408 break;
2409
2410 case rs_org:
2411 /* Assume .org is nugatory. It will grow with 1st relax. */
2412 region += 1;
2413 break;
2414
2415 case rs_space:
2416 break;
2417
2418 case rs_machine_dependent:
2419 /* If fr_symbol is an expression, this call to
2420 resolve_symbol_value sets up the correct segment, which will
2421 likely be needed in md_estimate_size_before_relax. */
2422 if (fragP->fr_symbol)
2423 resolve_symbol_value (fragP->fr_symbol);
2424
2425 address += md_estimate_size_before_relax (fragP, segment);
2426 break;
2427
2428 #ifndef WORKING_DOT_WORD
2429 /* Broken words don't concern us yet. */
2430 case rs_broken_word:
2431 break;
2432 #endif
2433
2434 case rs_leb128:
2435 /* Initial guess is always 1; doing otherwise can result in
2436 stable solutions that are larger than the minimum. */
2437 address += fragP->fr_offset = 1;
2438 break;
2439
2440 case rs_cfa:
2441 address += eh_frame_estimate_size_before_relax (fragP);
2442 break;
2443
2444 case rs_dwarf2dbg:
2445 address += dwarf2dbg_estimate_size_before_relax (fragP);
2446 break;
2447
2448 default:
2449 BAD_CASE (fragP->fr_type);
2450 break;
2451 }
2452 }
2453
2454 /* Do relax(). */
2455 {
2456 unsigned long max_iterations;
2457
2458 /* Cumulative address adjustment. */
2459 offsetT stretch;
2460
2461 /* Have we made any adjustment this pass? We can't just test
2462 stretch because one piece of code may have grown and another
2463 shrank. */
2464 int stretched;
2465
2466 /* Most horrible, but gcc may give us some exception data that
2467 is impossible to assemble, of the form
2468
2469 .align 4
2470 .byte 0, 0
2471 .uleb128 end - start
2472 start:
2473 .space 128*128 - 1
2474 .align 4
2475 end:
2476
2477 If the leb128 is two bytes in size, then end-start is 128*128,
2478 which requires a three byte leb128. If the leb128 is three
2479 bytes in size, then end-start is 128*128-1, which requires a
2480 two byte leb128. We work around this dilemma by inserting
2481 an extra 4 bytes of alignment just after the .align. This
2482 works because the data after the align is accessed relative to
2483 the end label.
2484
2485 This counter is used in a tiny state machine to detect
2486 whether a leb128 followed by an align is impossible to
2487 relax. */
2488 int rs_leb128_fudge = 0;
2489
2490 /* We want to prevent going into an infinite loop where one frag grows
2491 depending upon the location of a symbol which is in turn moved by
2492 the growing frag. eg:
2493
2494 foo = .
2495 .org foo+16
2496 foo = .
2497
2498 So we dictate that this algorithm can be at most O2. */
2499 max_iterations = frag_count * frag_count;
2500 /* Check for overflow. */
2501 if (max_iterations < frag_count)
2502 max_iterations = frag_count;
2503
2504 ret = 0;
2505 do
2506 {
2507 stretch = 0;
2508 stretched = 0;
2509
2510 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2511 {
2512 offsetT growth = 0;
2513 addressT was_address;
2514 offsetT offset;
2515 symbolS *symbolP;
2516
2517 fragP->relax_marker ^= 1;
2518 was_address = fragP->fr_address;
2519 address = fragP->fr_address += stretch;
2520 symbolP = fragP->fr_symbol;
2521 offset = fragP->fr_offset;
2522
2523 switch (fragP->fr_type)
2524 {
2525 case rs_fill: /* .fill never relaxes. */
2526 growth = 0;
2527 break;
2528
2529 #ifndef WORKING_DOT_WORD
2530 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2531 for it I do not want to write it. I do not want to have
2532 anything to do with it. This is not the proper way to
2533 implement this misfeature. */
2534 case rs_broken_word:
2535 {
2536 struct broken_word *lie;
2537 struct broken_word *untruth;
2538
2539 /* Yes this is ugly (storing the broken_word pointer
2540 in the symbol slot). Still, this whole chunk of
2541 code is ugly, and I don't feel like doing anything
2542 about it. Think of it as stubbornness in action. */
2543 growth = 0;
2544 for (lie = (struct broken_word *) (fragP->fr_symbol);
2545 lie && lie->dispfrag == fragP;
2546 lie = lie->next_broken_word)
2547 {
2548
2549 if (lie->added)
2550 continue;
2551
2552 offset = (S_GET_VALUE (lie->add)
2553 + lie->addnum
2554 - S_GET_VALUE (lie->sub));
2555 if (offset <= -32768 || offset >= 32767)
2556 {
2557 if (flag_warn_displacement)
2558 {
2559 char buf[50];
2560 sprint_value (buf, (addressT) lie->addnum);
2561 as_warn_where (fragP->fr_file, fragP->fr_line,
2562 _(".word %s-%s+%s didn't fit"),
2563 S_GET_NAME (lie->add),
2564 S_GET_NAME (lie->sub),
2565 buf);
2566 }
2567 if (fragP->fr_subtype == 0)
2568 {
2569 fragP->fr_subtype++;
2570 growth += md_short_jump_size;
2571 }
2572
2573 /* Redirect *all* words of this table with the same
2574 target, lest we have to handle the case where the
2575 same target but with a offset that fits on this
2576 round overflows at the next relaxation round. */
2577 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2578 untruth && untruth->dispfrag == lie->dispfrag;
2579 untruth = untruth->next_broken_word)
2580 if ((symbol_get_frag (untruth->add)
2581 == symbol_get_frag (lie->add))
2582 && (S_GET_VALUE (untruth->add)
2583 == S_GET_VALUE (lie->add)))
2584 {
2585 untruth->added = 2;
2586 untruth->use_jump = lie;
2587 }
2588
2589 lie->added = 1;
2590 growth += md_long_jump_size;
2591 }
2592 }
2593
2594 break;
2595 } /* case rs_broken_word */
2596 #endif
2597 case rs_align:
2598 case rs_align_code:
2599 case rs_align_test:
2600 {
2601 addressT oldoff, newoff;
2602
2603 oldoff = relax_align (was_address + fragP->fr_fix,
2604 (int) offset);
2605 newoff = relax_align (address + fragP->fr_fix,
2606 (int) offset);
2607
2608 if (fragP->fr_subtype != 0)
2609 {
2610 if (oldoff > fragP->fr_subtype)
2611 oldoff = 0;
2612 if (newoff > fragP->fr_subtype)
2613 newoff = 0;
2614 }
2615
2616 growth = newoff - oldoff;
2617
2618 /* If this align happens to follow a leb128 and
2619 we have determined that the leb128 is bouncing
2620 in size, then break the cycle by inserting an
2621 extra alignment. */
2622 if (growth < 0
2623 && (rs_leb128_fudge & 16) != 0
2624 && (rs_leb128_fudge & 15) >= 2)
2625 {
2626 segment_info_type *seginfo = seg_info (segment);
2627 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2628 struct frag *newf;
2629
2630 newf = frag_alloc (ob);
2631 obstack_blank_fast (ob, fragP->fr_var);
2632 obstack_finish (ob);
2633 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2634 memcpy (newf->fr_literal,
2635 fragP->fr_literal + fragP->fr_fix,
2636 fragP->fr_var);
2637 newf->fr_type = rs_fill;
2638 newf->fr_address = address + fragP->fr_fix + newoff;
2639 newf->fr_fix = 0;
2640 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2641 / fragP->fr_var);
2642 if (newf->fr_offset * newf->fr_var
2643 != (offsetT) 1 << fragP->fr_offset)
2644 {
2645 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2646 newf->fr_var = 1;
2647 }
2648 /* Include size of new frag in GROWTH. */
2649 growth += newf->fr_offset * newf->fr_var;
2650 /* Adjust the new frag address for the amount
2651 we'll add when we process the new frag. */
2652 newf->fr_address -= stretch + growth;
2653 newf->relax_marker ^= 1;
2654 fragP->fr_next = newf;
2655 #ifdef DEBUG
2656 as_warn (_("padding added"));
2657 #endif
2658 }
2659 }
2660 break;
2661
2662 case rs_org:
2663 {
2664 addressT target = offset;
2665 addressT after;
2666
2667 if (symbolP)
2668 {
2669 /* Convert from an actual address to an octet offset
2670 into the section. Here it is assumed that the
2671 section's VMA is zero, and can omit subtracting it
2672 from the symbol's value to get the address offset. */
2673 know (S_GET_SEGMENT (symbolP)->vma == 0);
2674 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2675 }
2676
2677 know (fragP->fr_next);
2678 after = fragP->fr_next->fr_address + stretch;
2679 growth = target - after;
2680 if (growth < 0)
2681 {
2682 growth = 0;
2683
2684 /* Don't error on first few frag relax passes.
2685 The symbol might be an expression involving
2686 symbol values from other sections. If those
2687 sections have not yet been processed their
2688 frags will all have zero addresses, so we
2689 will calculate incorrect values for them. The
2690 number of passes we allow before giving an
2691 error is somewhat arbitrary. It should be at
2692 least one, with larger values requiring
2693 increasingly contrived dependencies between
2694 frags to trigger a false error. */
2695 if (pass < 2)
2696 {
2697 /* Force another pass. */
2698 ret = 1;
2699 break;
2700 }
2701
2702 /* Growth may be negative, but variable part of frag
2703 cannot have fewer than 0 chars. That is, we can't
2704 .org backwards. */
2705 as_bad_where (fragP->fr_file, fragP->fr_line,
2706 _("attempt to move .org backwards"));
2707
2708 /* We've issued an error message. Change the
2709 frag to avoid cascading errors. */
2710 fragP->fr_type = rs_align;
2711 fragP->fr_subtype = 0;
2712 fragP->fr_offset = 0;
2713 fragP->fr_fix = after - address;
2714 }
2715 }
2716 break;
2717
2718 case rs_space:
2719 growth = 0;
2720 if (symbolP)
2721 {
2722 offsetT amount;
2723
2724 amount = S_GET_VALUE (symbolP);
2725 if (S_GET_SEGMENT (symbolP) != absolute_section
2726 || S_IS_COMMON (symbolP)
2727 || ! S_IS_DEFINED (symbolP))
2728 {
2729 as_bad_where (fragP->fr_file, fragP->fr_line,
2730 _(".space specifies non-absolute value"));
2731 /* Prevent repeat of this error message. */
2732 fragP->fr_symbol = 0;
2733 }
2734 else if (amount < 0)
2735 {
2736 /* Don't error on first few frag relax passes.
2737 See rs_org comment for a longer explanation. */
2738 if (pass < 2)
2739 {
2740 ret = 1;
2741 break;
2742 }
2743
2744 as_warn_where (fragP->fr_file, fragP->fr_line,
2745 _(".space or .fill with negative value, ignored"));
2746 fragP->fr_symbol = 0;
2747 }
2748 else
2749 growth = (was_address + fragP->fr_fix + amount
2750 - fragP->fr_next->fr_address);
2751 }
2752 break;
2753
2754 case rs_machine_dependent:
2755 #ifdef md_relax_frag
2756 growth = md_relax_frag (segment, fragP, stretch);
2757 #else
2758 #ifdef TC_GENERIC_RELAX_TABLE
2759 /* The default way to relax a frag is to look through
2760 TC_GENERIC_RELAX_TABLE. */
2761 growth = relax_frag (segment, fragP, stretch);
2762 #endif /* TC_GENERIC_RELAX_TABLE */
2763 #endif
2764 break;
2765
2766 case rs_leb128:
2767 {
2768 valueT value;
2769 offsetT size;
2770
2771 value = resolve_symbol_value (fragP->fr_symbol);
2772 size = sizeof_leb128 (value, fragP->fr_subtype);
2773 growth = size - fragP->fr_offset;
2774 fragP->fr_offset = size;
2775 }
2776 break;
2777
2778 case rs_cfa:
2779 growth = eh_frame_relax_frag (fragP);
2780 break;
2781
2782 case rs_dwarf2dbg:
2783 growth = dwarf2dbg_relax_frag (fragP);
2784 break;
2785
2786 default:
2787 BAD_CASE (fragP->fr_type);
2788 break;
2789 }
2790 if (growth)
2791 {
2792 stretch += growth;
2793 stretched = 1;
2794 if (fragP->fr_type == rs_leb128)
2795 rs_leb128_fudge += 16;
2796 else if (fragP->fr_type == rs_align
2797 && (rs_leb128_fudge & 16) != 0
2798 && stretch == 0)
2799 rs_leb128_fudge += 16;
2800 else
2801 rs_leb128_fudge = 0;
2802 }
2803 }
2804
2805 if (stretch == 0
2806 && (rs_leb128_fudge & 16) == 0
2807 && (rs_leb128_fudge & -16) != 0)
2808 rs_leb128_fudge += 1;
2809 else
2810 rs_leb128_fudge = 0;
2811 }
2812 /* Until nothing further to relax. */
2813 while (stretched && -- max_iterations);
2814
2815 if (stretched)
2816 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2817 segment_name (segment));
2818 }
2819
2820 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2821 if (fragP->last_fr_address != fragP->fr_address)
2822 {
2823 fragP->last_fr_address = fragP->fr_address;
2824 ret = 1;
2825 }
2826 return ret;
2827 }
2828
2829 void
2830 number_to_chars_bigendian (char *buf, valueT val, int n)
2831 {
2832 if (n <= 0)
2833 abort ();
2834 while (n--)
2835 {
2836 buf[n] = val & 0xff;
2837 val >>= 8;
2838 }
2839 }
2840
2841 void
2842 number_to_chars_littleendian (char *buf, valueT val, int n)
2843 {
2844 if (n <= 0)
2845 abort ();
2846 while (n--)
2847 {
2848 *buf++ = val & 0xff;
2849 val >>= 8;
2850 }
2851 }
2852
2853 void
2854 write_print_statistics (FILE *file)
2855 {
2856 fprintf (file, "fixups: %d\n", n_fixups);
2857 }
2858
2859 /* For debugging. */
2860 extern int indent_level;
2861
2862 void
2863 print_fixup (fixS *fixp)
2864 {
2865 indent_level = 1;
2866 fprintf (stderr, "fix ");
2867 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2868 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
2869 if (fixp->fx_pcrel)
2870 fprintf (stderr, " pcrel");
2871 if (fixp->fx_pcrel_adjust)
2872 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2873 if (fixp->fx_im_disp)
2874 {
2875 #ifdef TC_NS32K
2876 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2877 #else
2878 fprintf (stderr, " im_disp");
2879 #endif
2880 }
2881 if (fixp->fx_tcbit)
2882 fprintf (stderr, " tcbit");
2883 if (fixp->fx_done)
2884 fprintf (stderr, " done");
2885 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2886 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2887 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2888 (long) fixp->fx_where,
2889 (unsigned long) fixp->fx_offset,
2890 (unsigned long) fixp->fx_addnumber);
2891 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2892 fixp->fx_r_type);
2893 if (fixp->fx_addsy)
2894 {
2895 fprintf (stderr, "\n +<");
2896 print_symbol_value_1 (stderr, fixp->fx_addsy);
2897 fprintf (stderr, ">");
2898 }
2899 if (fixp->fx_subsy)
2900 {
2901 fprintf (stderr, "\n -<");
2902 print_symbol_value_1 (stderr, fixp->fx_subsy);
2903 fprintf (stderr, ">");
2904 }
2905 fprintf (stderr, "\n");
2906 #ifdef TC_FIX_DATA_PRINT
2907 TC_FIX_DATA_PRINT (stderr, fixp);
2908 #endif
2909 }
This page took 0.099692 seconds and 4 git commands to generate.