source: trunk/src/binutils/gas/write.c@ 86

Last change on this file since 86 was 86, checked in by bird, 22 years ago

Applied the original 2.11.2 diff.

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