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