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