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