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