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