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