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