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