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