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