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