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