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