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