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