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