don't sanitize out v9 stuff any more
[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. */
1045 if (flagseen['Z'])
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)
6efd877d
KR
1131 if (flagseen['R'])
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
1cf7548e
KR
1488 /* If *_frob_file changes the symbol value at this point, it is
1489 responsible for moving the changed value into symp->bsym->value
1490 as well. Hopefully all symbol value changing can be done in
1491 *_frob_symbol. */
c79e67a3
KR
1492#ifdef tc_frob_file
1493 tc_frob_file ();
1494#endif
f2f7d044
ILT
1495#ifdef obj_frob_file
1496 obj_frob_file ();
1497#endif
1498
43ca9aa6 1499 /* Now that all the sizes are known, and contents correct, we can
1cf7548e
KR
1500 start writing to the file. */
1501 set_symtab ();
1502
8d6c34a1
KR
1503 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1504
43ca9aa6 1505 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
43ca9aa6
KR
1506#endif /* BFD_ASSEMBLER */
1507}
d5364748 1508#endif /* ! BFD */
fecd2382
RP
1509
1510/*
1511 * relax_segment()
1512 *
1513 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1514 * values.
1515 *
1516 * Relax the frags.
1517 *
1518 * After this, all frags in this segment have addresses that are correct
1519 * within the segment. Since segments live in different file addresses,
1520 * these frag addresses may not be the same as final object-file addresses.
1521 */
45432836 1522
a58374d7
ILT
1523#ifndef md_relax_frag
1524
d5364748 1525/* Subroutines of relax_segment. */
43ca9aa6
KR
1526static int
1527is_dnrange (f1, f2)
1528 struct frag *f1;
1529 struct frag *f2;
1530{
1531 for (; f1; f1 = f1->fr_next)
1532 if (f1->fr_next == f2)
1533 return 1;
1534 return 0;
1535}
1536
a58374d7
ILT
1537#endif /* ! defined (md_relax_frag) */
1538
43ca9aa6 1539/* Relax_align. Advance location counter to next address that has 'alignment'
d5364748 1540 lowest order bits all 0s, return size of adjustment made. */
43ca9aa6
KR
1541static relax_addressT
1542relax_align (address, alignment)
1543 register relax_addressT address; /* Address now. */
d5364748 1544 register int alignment; /* Alignment (binary). */
43ca9aa6
KR
1545{
1546 relax_addressT mask;
1547 relax_addressT new_address;
1548
1549 mask = ~((~0) << alignment);
1550 new_address = (address + mask) & (~mask);
1551 if (linkrelax)
1552 /* We must provide lots of padding, so the linker can discard it
1553 when needed. The linker will not add extra space, ever. */
1554 new_address += (1 << alignment);
1555 return (new_address - address);
1556}
45432836 1557
6efd877d
KR
1558void
1559relax_segment (segment_frag_root, segment)
1560 struct frag *segment_frag_root;
43ca9aa6 1561 segT segment;
fecd2382 1562{
6efd877d
KR
1563 register struct frag *fragP;
1564 register relax_addressT address;
43ca9aa6 1565#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
6efd877d 1566 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
45432836 1567#endif
6efd877d
KR
1568 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1569 subseg_change (segment, 0);
1570
7f2cb270
KR
1571 /* For each frag in segment: count and store (a 1st guess of)
1572 fr_address. */
6efd877d
KR
1573 address = 0;
1574 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1575 {
1576 fragP->fr_address = address;
1577 address += fragP->fr_fix;
1578
1579 switch (fragP->fr_type)
1580 {
1581 case rs_fill:
1582 address += fragP->fr_offset * fragP->fr_var;
1583 break;
1584
1585 case rs_align:
98c6bbbe
KR
1586 {
1587 int offset = relax_align (address, (int) fragP->fr_offset);
1588 if (offset % fragP->fr_var != 0)
1589 {
262b22cd
ILT
1590 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1591 offset, (long) fragP->fr_var);
98c6bbbe
KR
1592 offset -= (offset % fragP->fr_var);
1593 }
1594 address += offset;
1595 }
6efd877d
KR
1596 break;
1597
1598 case rs_org:
7f2cb270 1599 /* Assume .org is nugatory. It will grow with 1st relax. */
6efd877d
KR
1600 break;
1601
1602 case rs_machine_dependent:
1603 address += md_estimate_size_before_relax (fragP, segment);
1604 break;
1605
fecd2382 1606#ifndef WORKING_DOT_WORD
6efd877d
KR
1607 /* Broken words don't concern us yet */
1608 case rs_broken_word:
1609 break;
fecd2382 1610#endif
6efd877d
KR
1611
1612 default:
1613 BAD_CASE (fragP->fr_type);
1614 break;
1615 } /* switch(fr_type) */
1616 } /* for each frag in the segment */
1617
7f2cb270 1618 /* Do relax(). */
6efd877d 1619 {
7f2cb270 1620 long stretch; /* May be any size, 0 or negative. */
6efd877d
KR
1621 /* Cumulative number of addresses we have */
1622 /* relaxed this pass. */
1623 /* We may have relaxed more than one address. */
7f2cb270
KR
1624 long stretched; /* Have we stretched on this pass? */
1625 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1626 grew, and another shrank. If a branch instruction doesn't fit anymore,
1627 we could be scrod. */
6efd877d
KR
1628
1629 do
1630 {
1631 stretch = stretched = 0;
1632 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1633 {
7f2cb270
KR
1634 long growth = 0;
1635 unsigned long was_address;
1636 long offset;
1637 symbolS *symbolP;
1638 long target;
1639 long after;
6efd877d
KR
1640
1641 was_address = fragP->fr_address;
1642 address = fragP->fr_address += stretch;
1643 symbolP = fragP->fr_symbol;
1644 offset = fragP->fr_offset;
6efd877d
KR
1645
1646 switch (fragP->fr_type)
1647 {
1648 case rs_fill: /* .fill never relaxes. */
1649 growth = 0;
1650 break;
1651
fecd2382 1652#ifndef WORKING_DOT_WORD
6efd877d 1653 /* JF: This is RMS's idea. I do *NOT* want to be blamed
7f2cb270
KR
1654 for it I do not want to write it. I do not want to have
1655 anything to do with it. This is not the proper way to
1656 implement this misfeature. */
6efd877d
KR
1657 case rs_broken_word:
1658 {
1659 struct broken_word *lie;
1660 struct broken_word *untruth;
6efd877d
KR
1661
1662 /* Yes this is ugly (storing the broken_word pointer
7f2cb270
KR
1663 in the symbol slot). Still, this whole chunk of
1664 code is ugly, and I don't feel like doing anything
1665 about it. Think of it as stubbornness in action. */
6efd877d
KR
1666 growth = 0;
1667 for (lie = (struct broken_word *) (fragP->fr_symbol);
1668 lie && lie->dispfrag == fragP;
1669 lie = lie->next_broken_word)
1670 {
1671
1672 if (lie->added)
1673 continue;
1674
7f2cb270
KR
1675 offset = (lie->add->sy_frag->fr_address
1676 + S_GET_VALUE (lie->add)
1677 + lie->addnum
1678 - (lie->sub->sy_frag->fr_address
1679 + S_GET_VALUE (lie->sub)));
6efd877d
KR
1680 if (offset <= -32768 || offset >= 32767)
1681 {
1682 if (flagseen['K'])
d5364748
KR
1683 {
1684 char buf[50];
80aab579 1685 sprint_value (buf, (addressT) lie->addnum);
d5364748
KR
1686 as_warn (".word %s-%s+%s didn't fit",
1687 S_GET_NAME (lie->add),
1688 S_GET_NAME (lie->sub),
1689 buf);
1690 }
6efd877d
KR
1691 lie->added = 1;
1692 if (fragP->fr_subtype == 0)
1693 {
1694 fragP->fr_subtype++;
1695 growth += md_short_jump_size;
1696 }
7f2cb270
KR
1697 for (untruth = lie->next_broken_word;
1698 untruth && untruth->dispfrag == lie->dispfrag;
1699 untruth = untruth->next_broken_word)
6efd877d
KR
1700 if ((untruth->add->sy_frag == lie->add->sy_frag)
1701 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1702 {
1703 untruth->added = 2;
1704 untruth->use_jump = lie;
1705 }
1706 growth += md_long_jump_size;
1707 }
1708 }
1709
1710 break;
1711 } /* case rs_broken_word */
fecd2382 1712#endif
6efd877d 1713 case rs_align:
43ca9aa6
KR
1714 growth = (relax_align ((relax_addressT) (address
1715 + fragP->fr_fix),
d5364748 1716 (int) offset)
43ca9aa6
KR
1717 - relax_align ((relax_addressT) (was_address
1718 + fragP->fr_fix),
d5364748 1719 (int) offset));
6efd877d
KR
1720 break;
1721
1722 case rs_org:
1723 target = offset;
1724
1725 if (symbolP)
1726 {
43ca9aa6
KR
1727#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1728 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1729 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1730 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1731 || S_GET_SEGMENT (symbolP) == SEG_BSS);
6efd877d 1732 know (symbolP->sy_frag);
43ca9aa6
KR
1733 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1734 || (symbolP->sy_frag == &zero_address_frag));
45432836 1735#endif
6efd877d
KR
1736 target += S_GET_VALUE (symbolP)
1737 + symbolP->sy_frag->fr_address;
1738 } /* if we have a symbol */
1739
1740 know (fragP->fr_next);
1741 after = fragP->fr_next->fr_address;
1742 growth = ((target - after) > 0) ? (target - after) : 0;
43ca9aa6
KR
1743 /* Growth may be negative, but variable part of frag
1744 cannot have fewer than 0 chars. That is, we can't
1745 .org backwards. */
6efd877d
KR
1746
1747 growth -= stretch; /* This is an absolute growth factor */
1748 break;
1749
1750 case rs_machine_dependent:
a58374d7
ILT
1751#ifdef md_relax_frag
1752 growth = md_relax_frag (fragP, stretch);
1753#else
1754 /* The default way to relax a frag is to look through
1755 md_relax_table. */
6efd877d 1756 {
7f2cb270
KR
1757 const relax_typeS *this_type;
1758 const relax_typeS *start_type;
1759 relax_substateT next_state;
1760 relax_substateT this_state;
80aab579 1761 long aim;
6efd877d 1762
7f2cb270
KR
1763 this_state = fragP->fr_subtype;
1764 start_type = this_type = md_relax_table + this_state;
6efd877d
KR
1765 target = offset;
1766
1767 if (symbolP)
1768 {
80aab579 1769#ifndef DIFF_EXPR_OK
43ca9aa6
KR
1770#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1771 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1772 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1773 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1774 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
45432836 1775#endif
6efd877d 1776 know (symbolP->sy_frag);
80aab579 1777#endif
43ca9aa6
KR
1778 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1779 || symbolP->sy_frag == &zero_address_frag);
6efd877d
KR
1780 target +=
1781 S_GET_VALUE (symbolP)
1782 + symbolP->sy_frag->fr_address;
1783
1784 /* If frag has yet to be reached on this pass,
7f2cb270
KR
1785 assume it will move by STRETCH just as we did.
1786 If this is not so, it will be because some frag
d5364748
KR
1787 between grows, and that will force another pass.
1788
1789 Beware zero-length frags.
1790
1791 There should be a faster way to do this. */
6efd877d
KR
1792
1793 if (symbolP->sy_frag->fr_address >= was_address
1794 && is_dnrange (fragP, symbolP->sy_frag))
1795 {
1796 target += stretch;
7f2cb270 1797 }
d5364748 1798 }
6efd877d
KR
1799
1800 aim = target - address - fragP->fr_fix;
1801 /* The displacement is affected by the instruction size
7f2cb270
KR
1802 for the 32k architecture. I think we ought to be able
1803 to add fragP->fr_pcrel_adjust in all cases (it should be
1804 zero if not used), but just in case it breaks something
1805 else we'll put this inside #ifdef NS32K ... #endif */
d5364748
KR
1806#ifndef TC_NS32K
1807 if (fragP->fr_pcrel_adjust)
1808 abort ();
1809#endif
6efd877d 1810 aim += fragP->fr_pcrel_adjust;
6efd877d
KR
1811
1812 if (aim < 0)
1813 {
1814 /* Look backwards. */
1815 for (next_state = this_type->rlx_more; next_state;)
7f2cb270
KR
1816 if (aim >= this_type->rlx_backward)
1817 next_state = 0;
1818 else
1819 {
1820 /* Grow to next state. */
1821 this_state = next_state;
1822 this_type = md_relax_table + this_state;
1823 next_state = this_type->rlx_more;
1824 }
6efd877d
KR
1825 }
1826 else
1827 {
a39116f1 1828#ifdef M68K_AIM_KLUDGE
6efd877d 1829 M68K_AIM_KLUDGE (aim, this_state, this_type);
fecd2382 1830#endif
6efd877d
KR
1831 /* Look forwards. */
1832 for (next_state = this_type->rlx_more; next_state;)
7f2cb270
KR
1833 if (aim <= this_type->rlx_forward)
1834 next_state = 0;
1835 else
1836 {
1837 /* Grow to next state. */
1838 this_state = next_state;
1839 this_type = md_relax_table + this_state;
1840 next_state = this_type->rlx_more;
1841 }
6efd877d
KR
1842 }
1843
7f2cb270
KR
1844 growth = this_type->rlx_length - start_type->rlx_length;
1845 if (growth != 0)
6efd877d 1846 fragP->fr_subtype = this_state;
7f2cb270 1847 }
a58374d7 1848#endif
7f2cb270 1849 break;
6efd877d
KR
1850
1851 default:
1852 BAD_CASE (fragP->fr_type);
1853 break;
1854 }
1855 if (growth)
1856 {
1857 stretch += growth;
1858 stretched++;
1859 }
1860 } /* For each frag in the segment. */
1861 }
1862 while (stretched); /* Until nothing further to relax. */
1863 } /* do_relax */
1864
1865 /*
7f2cb270
KR
1866 * We now have valid fr_address'es for each frag.
1867 */
6efd877d
KR
1868
1869 /*
7f2cb270
KR
1870 * All fr_address's are correct, relative to their own segment.
1871 * We have made all the fixS we will ever make.
1872 */
6efd877d 1873} /* relax_segment() */
fecd2382 1874
80aab579
ILT
1875#if defined (BFD_ASSEMBLER) || !defined (BFD)
1876
fecd2382 1877/* fixup_segment()
6efd877d 1878
fecd2382
RP
1879 Go through all the fixS's in a segment and see which ones can be
1880 handled now. (These consist of fixS where we have since discovered
1881 the value of a symbol, or the address of the frag involved.)
1882 For each one, call md_apply_fix to put the fix into the frag data.
6efd877d 1883
fecd2382
RP
1884 Result is a count of how many relocation structs will be needed to
1885 handle the remaining fixS's that we couldn't completely handle here.
1886 These will be output later by emit_relocations(). */
1887
09952cd9 1888static long
6efd877d 1889fixup_segment (fixP, this_segment_type)
09952cd9 1890 register fixS *fixP;
6efd877d 1891 segT this_segment_type; /* N_TYPE bits for segment. */
fecd2382 1892{
f5c32424
KR
1893 long seg_reloc_count = 0;
1894 symbolS *add_symbolP;
1895 symbolS *sub_symbolP;
d5364748 1896 valueT add_number;
f5c32424
KR
1897 int size;
1898 char *place;
1899 long where;
1900 char pcrel;
1901 fragS *fragP;
1902 segT add_symbol_segment = absolute_section;
1903
1904 /* If the linker is doing the relaxing, we must not do any fixups.
1905
1906 Well, strictly speaking that's not true -- we could do any that are
1907 PC-relative and don't cross regions that could change size. And for the
1908 i960 (the only machine for which we've got a relaxing linker right now),
1909 we might be able to turn callx/callj into bal anyways in cases where we
1910 know the maximum displacement. */
6efd877d 1911 if (linkrelax)
f5c32424
KR
1912 {
1913 for (; fixP; fixP = fixP->fx_next)
1914 seg_reloc_count++;
1915 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1916 return seg_reloc_count;
1917 }
6efd877d 1918
f5c32424
KR
1919 for (; fixP; fixP = fixP->fx_next)
1920 {
1921 fragP = fixP->fx_frag;
1922 know (fragP);
1923 where = fixP->fx_where;
1924 place = fragP->fr_literal + where;
1925 size = fixP->fx_size;
1926 add_symbolP = fixP->fx_addsy;
1927#ifdef TC_VALIDATE_FIX
1928 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
fecd2382 1929#endif
f5c32424
KR
1930 sub_symbolP = fixP->fx_subsy;
1931 add_number = fixP->fx_offset;
1932 pcrel = fixP->fx_pcrel;
1933
1934 if (add_symbolP)
1935 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
1936
1937 if (sub_symbolP)
1938 {
1939 if (!add_symbolP)
1940 {
1941 /* Its just -sym */
1942 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
6efd877d 1943 add_number -= S_GET_VALUE (sub_symbolP);
f5c32424
KR
1944 else if (pcrel
1945 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
1946 {
1947 /* Should try converting to a constant. */
1948 goto bad_sub_reloc;
1949 }
1950 else
1951 bad_sub_reloc:
741f4d66
KR
1952 as_bad_where (fixP->fx_file, fixP->fx_line,
1953 "Negative of non-absolute symbol %s",
1954 S_GET_NAME (sub_symbolP));
f5c32424
KR
1955 }
1956 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1957 && (SEG_NORMAL (add_symbol_segment)
1958 || (add_symbol_segment == absolute_section)))
1959 {
1960 /* Difference of 2 symbols from same segment.
1961 Can't make difference of 2 undefineds: 'value' means
1962 something different for N_UNDF. */
fecd2382 1963#ifdef TC_I960
f5c32424
KR
1964 /* Makes no sense to use the difference of 2 arbitrary symbols
1965 as the target of a call instruction. */
1966 if (fixP->fx_tcbit)
741f4d66
KR
1967 as_bad_where (fixP->fx_file, fixP->fx_line,
1968 "callj to difference of 2 symbols");
6efd877d 1969#endif /* TC_I960 */
f5c32424
KR
1970 add_number += S_GET_VALUE (add_symbolP) -
1971 S_GET_VALUE (sub_symbolP);
6efd877d 1972
f5c32424 1973 add_symbolP = NULL;
335d35c8 1974
f5c32424
KR
1975 /* Let the target machine make the final determination
1976 as to whether or not a relocation will be needed to
1977 handle this fixup. */
1978 if (!TC_FORCE_RELOCATION (fixP))
98c6bbbe
KR
1979 {
1980 fixP->fx_addsy = NULL;
1981 }
f5c32424
KR
1982 }
1983 else
1984 {
1985 /* Different segments in subtraction. */
1986 know (!(S_IS_EXTERNAL (sub_symbolP)
1987 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
1988
1989 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
1990 add_number -= S_GET_VALUE (sub_symbolP);
6efd877d 1991
80aab579 1992#ifdef DIFF_EXPR_OK
f5c32424
KR
1993 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
1994#if 0 /* Do this even if it's already described as pc-relative. For example,
1995 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
1996 pc-relative mode. */
1997 && pcrel
0f894895 1998#endif
f5c32424
KR
1999 )
2000 {
2001 /* Make it pc-relative. */
2002 add_number += (md_pcrel_from (fixP)
2003 - S_GET_VALUE (sub_symbolP));
2004 pcrel = 1;
2005 fixP->fx_pcrel = 1;
2006 sub_symbolP = 0;
2007 fixP->fx_subsy = 0;
2008 }
98c6bbbe
KR
2009#endif
2010#ifdef BFD_ASSEMBLER
2011 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2012 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2013 {
2014 /* Leave it alone. */
2015 }
80aab579 2016#endif
f5c32424
KR
2017 else
2018 {
2019 char buf[50];
2020 sprint_value (buf, fragP->fr_address + where);
741f4d66
KR
2021 as_bad_where (fixP->fx_file, fixP->fx_line,
2022 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2023 segment_name (S_GET_SEGMENT (sub_symbolP)),
2024 S_GET_NAME (sub_symbolP), buf);
f5c32424
KR
2025 }
2026 }
2027 }
6efd877d 2028
f5c32424
KR
2029 if (add_symbolP)
2030 {
2031 if (add_symbol_segment == this_segment_type && pcrel)
2032 {
2033 /*
2034 * This fixup was made when the symbol's segment was
2035 * SEG_UNKNOWN, but it is now in the local segment.
2036 * So we know how to do the address without relocation.
2037 */
fecd2382 2038#ifdef TC_I960
f5c32424
KR
2039 /* reloc_callj() may replace a 'call' with a 'calls' or a
2040 'bal', in which cases it modifies *fixP as appropriate.
2041 In the case of a 'calls', no further work is required,
2042 and *fixP has been set up to make the rest of the code
2043 below a no-op. */
2044 reloc_callj (fixP);
fecd2382 2045#endif /* TC_I960 */
6efd877d 2046
f5c32424
KR
2047 add_number += S_GET_VALUE (add_symbolP);
2048 add_number -= md_pcrel_from (fixP);
2049 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2050
2051 /* Let the target machine make the final determination
2052 as to whether or not a relocation will be needed to
2053 handle this fixup. */
2054 if (!TC_FORCE_RELOCATION (fixP))
98c6bbbe
KR
2055 {
2056 fixP->fx_pcrel = 0;
2057 fixP->fx_addsy = NULL;
2058 }
f5c32424
KR
2059 }
2060 else
2061 {
2062 if (add_symbol_segment == absolute_section)
2063 {
fecd2382 2064#ifdef TC_I960
f5c32424
KR
2065 /* See comment about reloc_callj() above. */
2066 reloc_callj (fixP);
fecd2382 2067#endif /* TC_I960 */
f5c32424
KR
2068 add_number += S_GET_VALUE (add_symbolP);
2069
2070 /* Let the target machine make the final determination
2071 as to whether or not a relocation will be needed to
2072 handle this fixup. */
2073 if (!TC_FORCE_RELOCATION (fixP))
e67a0640
KR
2074 {
2075 fixP->fx_addsy = NULL;
2076 add_symbolP = NULL;
2077 }
f5c32424
KR
2078 }
2079 else if (add_symbol_segment == undefined_section
43ca9aa6 2080#ifdef BFD_ASSEMBLER
f5c32424 2081 || bfd_is_com_section (add_symbol_segment)
43ca9aa6 2082#endif
f5c32424
KR
2083 )
2084 {
fecd2382 2085#ifdef TC_I960
f5c32424
KR
2086 if ((int) fixP->fx_bit_fixP == 13)
2087 {
2088 /* This is a COBR instruction. They have only a
2089 * 13-bit displacement and are only to be used
2090 * for local branches: flag as error, don't generate
2091 * relocation.
2092 */
741f4d66
KR
2093 as_bad_where (fixP->fx_file, fixP->fx_line,
2094 "can't use COBR format with external label");
98c6bbbe
KR
2095 fixP->fx_addsy = NULL;
2096 fixP->fx_done = 1;
f5c32424
KR
2097 continue;
2098 } /* COBR */
fecd2382 2099#endif /* TC_I960 */
f5c32424 2100
fecd2382 2101#ifdef OBJ_COFF
c593cf41 2102#ifdef TE_I386AIX
f5c32424
KR
2103 if (S_IS_COMMON (add_symbolP))
2104 add_number += S_GET_VALUE (add_symbolP);
c593cf41 2105#endif /* TE_I386AIX */
fecd2382 2106#endif /* OBJ_COFF */
f5c32424
KR
2107 ++seg_reloc_count;
2108 }
2109 else
2110 {
2111 seg_reloc_count++;
2112 add_number += S_GET_VALUE (add_symbolP);
2113 }
2114 }
2115 }
6efd877d 2116
f5c32424
KR
2117 if (pcrel)
2118 {
2119 add_number -= md_pcrel_from (fixP);
2120 if (add_symbolP == 0)
2121 {
1cf7548e 2122#ifndef BFD_ASSEMBLER
f5c32424 2123 fixP->fx_addsy = &abs_symbol;
1cf7548e
KR
2124#else
2125 fixP->fx_addsy = section_symbol (absolute_section);
2126#endif
2127 fixP->fx_addsy->sy_used_in_reloc = 1;
f5c32424 2128 ++seg_reloc_count;
98c6bbbe
KR
2129 }
2130 }
6efd877d 2131
f5c32424
KR
2132 if (!fixP->fx_bit_fixP && size > 0)
2133 {
2134 valueT mask = 0;
df44a852 2135 if (size < sizeof (mask))
f5c32424 2136 {
df44a852
KR
2137 /* set all bits to one */
2138 mask--;
2139 /* Technically, combining these produces an undefined result
2140 if size is sizeof (valueT), though I think these two
2141 half-way operations should both be defined. And the
2142 compiler should be able to combine them if it's valid on
2143 the host architecture. */
2144 mask <<= size * 4;
2145 mask <<= size * 4;
2146 if ((add_number & mask) != 0
2147 && (add_number & mask) != mask)
2148 {
2149 char buf[50], buf2[50];
2150 sprint_value (buf, fragP->fr_address + where);
2151 if (add_number > 1000)
2152 sprint_value (buf2, add_number);
2153 else
2154 sprintf (buf2, "%ld", (long) add_number);
2155 as_bad_where (fixP->fx_file, fixP->fx_line,
2156 "Value of %s too large for field of %d bytes at %s",
2157 buf2, size, buf);
2158 } /* generic error checking */
2159 }
f5c32424
KR
2160#ifdef WARN_SIGNED_OVERFLOW_WORD
2161 /* Warn if a .word value is too large when treated as a signed
2162 number. We already know it is not too negative. This is to
2163 catch over-large switches generated by gcc on the 68k. */
2164 if (!flagseen['J']
2165 && size == 2
2166 && add_number > 0x7fff)
2167 as_bad_where (fixP->fx_file, fixP->fx_line,
2168 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2169 (long) add_number,
2170 (unsigned long) (fragP->fr_address + where));
6efd877d 2171#endif
f5c32424 2172 } /* not a bit fix */
6efd877d 2173
98c6bbbe
KR
2174 if (!fixP->fx_done)
2175 {
43ca9aa6 2176#ifdef BFD_ASSEMBLER
98c6bbbe 2177 md_apply_fix (fixP, &add_number);
43ca9aa6 2178#else
98c6bbbe
KR
2179 md_apply_fix (fixP, add_number);
2180#endif
2181
2182#ifndef TC_HANDLES_FX_DONE
2183 /* If the tc-* files haven't been converted, assume it's handling
2184 it the old way, where a null fx_addsy means that the fix has
2185 been applied completely, and no further work is needed. */
2186 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2187 fixP->fx_done = 1;
43ca9aa6 2188#endif
98c6bbbe
KR
2189 }
2190#ifdef TC_VALIDATE_FIX
f5c32424 2191 skip:
98c6bbbe 2192#endif
f5c32424
KR
2193 ;
2194 } /* For each fixS in this segment. */
6efd877d 2195
f5c32424
KR
2196 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2197 return seg_reloc_count;
2198}
6efd877d 2199
f5c32424 2200#endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
6efd877d 2201
f5c32424
KR
2202void
2203number_to_chars_bigendian (buf, val, n)
2204 char *buf;
2205 valueT val;
2206 int n;
2207{
2208 if (n > sizeof (val)|| n <= 0)
2209 abort ();
2210 while (n--)
2211 {
2212 buf[n] = val & 0xff;
2213 val >>= 8;
2214 }
d5364748 2215}
fecd2382 2216
f5c32424
KR
2217void
2218number_to_chars_littleendian (buf, val, n)
2219 char *buf;
2220 valueT val;
2221 int n;
2222{
2223 if (n > sizeof (val) || n <= 0)
2224 abort ();
2225 while (n--)
2226 {
2227 *buf++ = val & 0xff;
2228 val >>= 8;
2229 }
2230}
80aab579 2231
fecd2382 2232/* end of write.c */
This page took 0.249782 seconds and 4 git commands to generate.