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