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