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