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