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