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