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