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