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