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