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