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