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