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