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