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