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