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