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