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