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