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