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