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