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