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