Checkpoint for ECOFF support. Can assemble hello, world.
[deliverable/binutils-gdb.git] / gas / write.c
CommitLineData
fecd2382 1/* write.c - emit .o file
43ca9aa6 2 Copyright (C) 1986, 1987, 1990, 1991, 1992, 1993 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
43ca9aa6
KR
33#ifndef WORKING_DOT_WORD
34extern CONST int md_short_jump_size;
35extern CONST int md_long_jump_size;
36#endif
37
38#ifndef BFD_ASSEMBLER
39
45432836 40#ifndef MANY_SEGMENTS
09952cd9
KR
41struct frag *text_frag_root;
42struct frag *data_frag_root;
43struct frag *bss_frag_root;
fecd2382 44
09952cd9
KR
45struct frag *text_last_frag; /* Last frag in segment. */
46struct frag *data_last_frag; /* Last frag in segment. */
65bfcf2e 47static struct frag *bss_last_frag; /* Last frag in segment. */
45432836 48#endif
fecd2382
RP
49
50static object_headers headers;
51
52long string_byte_count;
53
54static char *the_object_file;
55
56char *next_object_file_charP; /* Tracks object file bytes. */
57
3eb802b5 58#ifndef OBJ_VMS
fecd2382 59int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
3eb802b5 60#endif
fecd2382 61
43ca9aa6
KR
62#endif /* BFD_ASSEMBLER */
63
6efd877d 64static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
09952cd9 65static relax_addressT relax_align PARAMS ((relax_addressT addr, long align));
6efd877d 66void relax_segment PARAMS ((struct frag * seg_frag_root, segT seg_type));
fecd2382
RP
67
68/*
69 * fix_new()
70 *
71 * Create a fixS in obstack 'notes'.
72 */
6efd877d
KR
73fixS *
74fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
75 fragS *frag; /* Which frag? */
76 int where; /* Where in that frag? */
77 short int size; /* 1, 2, or 4 usually. */
78 symbolS *add_symbol; /* X_add_symbol. */
79 symbolS *sub_symbol; /* X_subtract_symbol. */
80 long offset; /* X_add_number. */
81 int pcrel; /* TRUE if PC-relative relocation. */
43ca9aa6
KR
82#ifdef BFD_ASSEMBLER
83 bfd_reloc_code_real_type r_type; /* Relocation type */
84#else
6efd877d 85 int r_type; /* Relocation type */
43ca9aa6 86#endif
fecd2382 87{
6efd877d
KR
88 fixS *fixP;
89
90 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
91
92 fixP->fx_frag = frag;
93 fixP->fx_where = where;
94 fixP->fx_size = size;
95 fixP->fx_addsy = add_symbol;
96 fixP->fx_subsy = sub_symbol;
97 fixP->fx_offset = offset;
98 fixP->fx_pcrel = pcrel;
43ca9aa6 99#if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
6efd877d 100 fixP->fx_r_type = r_type;
c593cf41 101#endif
6efd877d
KR
102 fixP->fx_im_disp = 0;
103 fixP->fx_pcrel_adjust = 0;
6efd877d 104 fixP->fx_bit_fixP = 0;
43ca9aa6
KR
105 fixP->fx_addnumber = 0;
106
107#ifdef TC_something
108 fixP->fx_bsr = 0;
109#endif
110#ifdef TC_I960
111 fixP->fx_callj = 0;
112#endif
113
114 /* Usually, we want relocs sorted numerically, but while
115 comparing to older versions of gas that have relocs
116 reverse sorted, it is convenient to have this compile
117 time option. xoxorich. */
118
119 {
6efd877d 120
43ca9aa6
KR
121#ifdef BFD_ASSEMBLER
122 fixS **seg_fix_rootP = & (seg_info (now_seg)->fix_root);
123 fixS **seg_fix_tailP = & (seg_info (now_seg)->fix_tail);
124#endif
09952cd9 125
f6e504fe 126#ifdef REVERSE_SORT_RELOCS
6efd877d 127
43ca9aa6
KR
128 fixP->fx_next = *seg_fix_rootP;
129 *seg_fix_rootP = fixP;
6efd877d 130
f6e504fe 131#else /* REVERSE_SORT_RELOCS */
6efd877d 132
43ca9aa6 133 fixP->fx_next = NULL;
6efd877d 134
43ca9aa6
KR
135 if (*seg_fix_tailP)
136 (*seg_fix_tailP)->fx_next = fixP;
137 else
138 *seg_fix_rootP = fixP;
139 *seg_fix_tailP = fixP;
6efd877d 140
f6e504fe 141#endif /* REVERSE_SORT_RELOCS */
6efd877d 142
43ca9aa6
KR
143 }
144
145 return fixP;
146}
147
148/* Append a string onto another string, bumping the pointer along. */
149void
150append (charPP, fromP, length)
151 char **charPP;
152 char *fromP;
153 unsigned long length;
154{
155 /* Don't trust memcpy() of 0 chars. */
156 if (length == 0)
157 return;
158
159 memcpy (*charPP, fromP, (int) length);
160 *charPP += length;
161}
162
163#ifndef BFD_ASSEMBLER
164int section_alignment[SEG_MAXIMUM_ORDINAL];
165#endif
166
167/*
168 * This routine records the largest alignment seen for each segment.
169 * If the beginning of the segment is aligned on the worst-case
170 * boundary, all of the other alignments within it will work. At
171 * least one object format really uses this info.
172 */
173void
174record_alignment (seg, align)
175 /* Segment to which alignment pertains */
176 segT seg;
177 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
178 boundary, etc.) */
179 int align;
180{
181#ifdef BFD_ASSEMBLER
182 if (align > bfd_get_section_alignment (stdoutput, seg))
183 bfd_set_section_alignment (stdoutput, seg, align);
184#else
185 if (align > section_alignment[(int) seg])
186 section_alignment[(int) seg] = align;
187#endif
188}
189
190#if defined (BFD_ASSEMBLER) || ! defined (BFD)
191
192static fragS *
193chain_frchains_together_1 (section, frchp)
194 segT section;
195 struct frchain *frchp;
196{
197 fragS dummy, *prev_frag = &dummy;
198 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
199 {
200 prev_frag->fr_next = frchp->frch_root;
201 prev_frag = frchp->frch_last;
202 }
203 prev_frag->fr_next = 0;
204 return prev_frag;
205}
206
207#endif
208
209#ifdef BFD_ASSEMBLER
210
211static void
212chain_frchains_together (abfd, section, xxx)
213 bfd *abfd; /* unused */
214 segT section;
215 char *xxx; /* unused */
216{
217 chain_frchains_together_1 (section, seg_info (section)->frchainP);
218}
219
220#endif
542e1629 221
45432836 222#ifndef BFD
65bfcf2e 223
6efd877d
KR
224void
225remove_subsegs (head, seg, root, last)
226 frchainS *head;
227 int seg;
228 fragS **root;
229 fragS **last;
65bfcf2e 230{
65bfcf2e 231 *root = head->frch_root;
43ca9aa6
KR
232 *last = chain_frchains_together_1 (seg, head);
233}
234
235#endif /* BFD */
236
237#ifndef BFD
238
239static void
240cvt_frag_to_fill (x, fragP)
241#ifdef BFD_ASSEMBLER
242 segT x;
243#else
244 object_headers *x;
245#endif
246 fragS *fragP;
247{
248#ifdef BFD_ASSEMBLER
249 segT sec = x;
250#else
251 object_headers *headers = x;
252#endif
253
254 switch (fragP->fr_type)
6efd877d 255 {
43ca9aa6
KR
256 case rs_align:
257 case rs_org:
258#ifdef HANDLE_ALIGN
259 HANDLE_ALIGN (fragP);
260#endif
261 fragP->fr_type = rs_fill;
262 know (fragP->fr_var == 1);
263 know (fragP->fr_next != NULL);
264
265 fragP->fr_offset = (fragP->fr_next->fr_address
266 - fragP->fr_address
267 - fragP->fr_fix);
268 break;
65bfcf2e 269
43ca9aa6
KR
270 case rs_fill:
271 break;
272
273 case rs_machine_dependent:
274#ifdef BFD_ASSEMBLER
275 md_convert_frag (stdoutput, sec, fragP);
276#else
277 md_convert_frag (headers, fragP);
278#endif
279
280 assert (fragP->fr_next == NULL
281 || (fragP->fr_next->fr_address - fragP->fr_address
282 == fragP->fr_fix));
283
284 /*
285 * After md_convert_frag, we make the frag into a ".space 0".
286 * Md_convert_frag() should set up any fixSs and constants
287 * required.
288 */
289 frag_wane (fragP);
290 break;
291
292#ifndef WORKING_DOT_WORD
293 case rs_broken_word:
294 {
295 struct broken_word *lie;
296
297 if (fragP->fr_subtype)
298 {
299 fragP->fr_fix += md_short_jump_size;
300 for (lie = (struct broken_word *) (fragP->fr_symbol);
301 lie && lie->dispfrag == fragP;
302 lie = lie->next_broken_word)
303 if (lie->added == 1)
304 fragP->fr_fix += md_long_jump_size;
305 }
306 frag_wane (fragP);
307 }
308 break;
309#endif
310
311 default:
312 BAD_CASE (fragP->fr_type);
313 break;
6efd877d 314 }
65bfcf2e 315}
6efd877d 316
43ca9aa6
KR
317#ifdef BFD_ASSEMBLER
318static void
319relax_and_size_seg (abfd, sec, xxx)
320 bfd *abfd;
321 asection *sec;
322 char *xxx;
323{
324 flagword flags;
325
326 flags = bfd_get_section_flags (abfd, sec);
327
328 if (flags & SEC_ALLOC)
329 {
330 fragS *fragp;
331 segment_info_type *seginfo;
332 int x;
333 unsigned long size, newsize;
334
335 seginfo = (segment_info_type *) bfd_get_section_userdata (abfd, sec);
336 relax_segment (seginfo->frchainP->frch_root, sec);
337 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
338 cvt_frag_to_fill (sec, fragp);
339 for (fragp = seginfo->frchainP->frch_root;
340 fragp->fr_next;
341 fragp = fragp->fr_next)
342 /* walk to last elt */;
343 size = fragp->fr_address;
344 if (size > 0)
345 {
346 flags |= SEC_HAS_CONTENTS;
347 /* @@ This is just an approximation. */
348 if (seginfo->fix_root)
349 flags |= SEC_RELOC;
350 x = bfd_set_section_flags (abfd, sec, flags);
351 assert (x == true);
352 }
353 size = md_section_align (sec, size);
354 x = bfd_set_section_size (abfd, sec, size);
355 assert (x == true);
356
357 /* If the size had to be rounded up, add some padding in the last
358 non-empty frag. */
359 newsize = bfd_get_section_size_before_reloc (sec);
360 assert (newsize >= size);
361 if (size != newsize)
362 {
363 fragS *last = seginfo->frchainP->frch_last;
364 fragp = seginfo->frchainP->frch_root;
365 while (fragp->fr_next != last)
366 fragp = fragp->fr_next;
367 last->fr_address = size;
368 fragp->fr_offset += newsize - size;
369 }
370 }
371#ifdef tc_frob_section
372 tc_frob_section (sec);
373#endif
374#ifdef obj_frob_section
375 obj_frob_section (sec);
376#endif
377}
378
379static void
380write_contents (abfd, sec, xxx)
381 bfd *abfd;
382 asection *sec;
383 char *xxx;
384{
385 segment_info_type *seginfo = seg_info (sec);
386 unsigned long offset = 0;
387 fragS *frags;
388 int i, n;
389 arelent **relocs;
390 fixS *fixp;
391
392 if (! (bfd_get_section_flags (abfd, sec) & SEC_LOAD))
393 return;
394
395 fixup_segment (seginfo->fix_root, sec);
396
397 for (i = 0, fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
398 if (fixp->fx_addsy)
399 {
400 symbolS *sym = fixp->fx_addsy;
401 asection *sec = sym->bsym->section;
402 if (sec == &bfd_und_section
403 || sec == &bfd_abs_section
404 || sec == &bfd_com_section)
405 continue;
406 if (sym->bsym == sec->symbol)
407 continue;
408 /* If the section symbol isn't going to be output, the relocs
409 at least should still work. If not, figure out what to do
410 when we run into that case. */
411 fixp->fx_offset += S_GET_VALUE (sym);
412 fixp->fx_addsy = symbol_find (sec->name);
413 if (!fixp->fx_addsy)
414 {
415 fixp->fx_addsy = symbol_make (sec->name);
416 fixp->fx_addsy->bsym = sec->symbol;
417 }
418 }
419
420 /* Force calculations (size, vma) to get done. */
421 bfd_set_section_contents (stdoutput, sec, "", 0, 0);
422
423 /* Set up reloc information as well. */
424 n = 0;
425 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
426 n++;
427 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
428 n * sizeof (arelent *));
429
430 for (frags = seginfo->frchainP->frch_root, fixp = seginfo->fix_root, i = 0;
431 frags;
432 frags = frags->fr_next)
433 {
434 int x;
435 unsigned long fill_size;
436 char *fill_literal;
437 long count;
438
439 assert (frags->fr_type == rs_fill);
440 while (fixp
441 && fixp->fx_frag == frags)
442 {
443 arelent *reloc;
444 extern arelent *tc_gen_reloc ();
445 char *data;
446 static bfd_reloc_status_type s;
447
448 if (fixp->fx_addsy == 0)
449 {
450 /* @@ Need some other flag to indicate which have already
451 been performed... */
452 n--;
453 goto next;
454 }
455 reloc = tc_gen_reloc (sec, fixp);
456 if (!reloc)
457 {
458 n--;
459 goto next;
460 }
461 data = frags->fr_literal + fixp->fx_where;
462 if (fixp->fx_where + 4 > frags->fr_fix + frags->fr_offset)
463 abort ();
464 s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address,
465 sec, stdoutput);
466 switch (s)
467 {
468 case bfd_reloc_ok:
469 break;
470 default:
471 printf ("bad s value\n");
472 abort ();
473 }
474 relocs[i++] = reloc;
475 next:
476 fixp = fixp->fx_next;
477 }
478 if (frags->fr_fix)
479 {
480 x = bfd_set_section_contents (stdoutput, sec,
481 frags->fr_literal, offset,
482 frags->fr_fix);
483 assert (x == true);
484 offset += frags->fr_fix;
485 }
486 fill_literal = frags->fr_literal + frags->fr_fix;
487 fill_size = frags->fr_var;
488 count = frags->fr_offset;
489 assert (count >= 0);
490 if (fill_size && count)
491 while (count--)
492 {
493 x = bfd_set_section_contents (stdoutput, sec,
494 fill_literal, offset, fill_size);
495 assert (x == true);
496 offset += fill_size;
497 }
498 }
499 /* Did we miss any relocs? */
500 if (fixp != 0)
501 abort ();
502
503 if (n)
504 bfd_set_reloc (stdoutput, sec, relocs, n);
505}
506#endif
507
6efd877d
KR
508void
509write_object_file ()
45432836 510{
6efd877d
KR
511 register struct frchain *frchainP; /* Track along all frchains. */
512 register fragS *fragP; /* Track along all frags. */
513 register struct frchain *next_frchainP;
514 register fragS **prev_fragPP;
6efd877d
KR
515
516 long object_file_size;
517
43ca9aa6
KR
518 /* Do we really want to write it? */
519 {
520 int n_warns, n_errs;
521 n_warns = had_warnings ();
522 n_errs = had_errors ();
523 /* The -Z flag indicates that an object file should be generated,
524 regardless of warnings and errors. */
525 if (flagseen['Z'])
526 {
527 if (n_warns || n_errs)
528 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
529 n_errs, n_errs == 1 ? "" : "s",
530 n_warns, n_warns == 1 ? "" : "s");
531 }
532 else
533 {
534 if (n_errs)
535 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
536 n_errs, n_errs == 1 ? "" : "s",
537 n_warns, n_warns == 1 ? "" : "s");
538 }
539 }
540
3eb802b5 541#ifdef OBJ_VMS
6efd877d 542 /*
3eb802b5
ILT
543 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
544 * call a routine to check for the definition of the procedure
545 * "_main", and if so -- fix it up so that it can be program
546 * entry point.
547 */
6efd877d 548 VMS_Check_For_Main ();
fecd2382 549#endif /* VMS */
43ca9aa6
KR
550
551 /* After every sub-segment, we fake an ".align ...". This conforms to
552 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
553 frag that requires least thought. ".align" frags like to have a
554 following frag since that makes calculating their intended length
555 trivial.
556
557 @@ Is this really necessary?? */
3eb802b5 558#ifndef SUB_SEGMENT_ALIGN
43ca9aa6
KR
559#ifdef BFD_ASSEMBLER
560#define SUB_SEGMENT_ALIGN (0)
561#else
fecd2382 562#define SUB_SEGMENT_ALIGN (2)
43ca9aa6 563#endif
3eb802b5 564#endif
6efd877d
KR
565 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
566 {
43ca9aa6
KR
567#ifdef BFD_ASSEMBLER
568 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
569#else
6efd877d 570 subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
43ca9aa6 571#endif
6efd877d 572 frag_align (SUB_SEGMENT_ALIGN, NOP_OPCODE);
43ca9aa6
KR
573 /* frag_align will have left a new frag.
574 Use this last frag for an empty ".fill".
575
576 For this segment ...
577 Create a last frag. Do not leave a "being filled in frag". */
6efd877d
KR
578 frag_wane (frag_now);
579 frag_now->fr_fix = 0;
580 know (frag_now->fr_next == NULL);
581 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
582 /* Above shows we haven't left a half-completed object on obstack. */
43ca9aa6 583 }
6efd877d 584
43ca9aa6
KR
585 /* From now on, we don't care about sub-segments. Build one frag chain
586 for each segment. Linked thru fr_next. */
65bfcf2e 587
43ca9aa6
KR
588#ifdef BFD_ASSEMBLER
589 /* Remove the sections created by gas for its own purposes. */
590 {
591 asection **seclist, *sec;
592 seclist = &stdoutput->sections;
593 while (seclist && *seclist)
594 {
595 sec = *seclist;
596 while (sec == big_section
597 || sec == reg_section
598 || sec == pass1_section
599 || sec == diff_section
600 || sec == absent_section)
601 {
602 sec = sec->next;
603 *seclist = sec;
604 stdoutput->section_count--;
605 if (!sec)
606 break;
607 }
608 if (*seclist)
609 seclist = &(*seclist)->next;
610 }
611 }
612
613 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
614#else
6efd877d
KR
615 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
616 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
617 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
43ca9aa6 618#endif
6efd877d 619
43ca9aa6
KR
620 /* We have two segments. If user gave -R flag, then we must put the
621 data frags into the text segment. Do this before relaxing so
622 we know to take advantage of -R and make shorter addresses. */
623#if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
6efd877d
KR
624 if (flagseen['R'])
625 {
43ca9aa6
KR
626#ifdef BFD_ASSEMBLER
627 seg_info (text_section)->frchainP->frch_last->fr_next =
628 seg_info (data_section)->frchainP->frch_root;
629 seg_info (text_section)->frchainP->frch_last =
630 seg_info (data_section)->frchainP->frch_last;
631 seg_info (data_section)->frchainP = 0;
632#else
6efd877d
KR
633 fixS *tmp;
634
635 text_last_frag->fr_next = data_frag_root;
636 text_last_frag = data_last_frag;
637 data_last_frag = NULL;
638 data_frag_root = NULL;
639 if (text_fix_root)
640 {
641 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
642 tmp->fx_next = data_fix_root;
643 text_fix_tail = data_fix_tail;
a39116f1 644 }
6efd877d
KR
645 else
646 text_fix_root = data_fix_root;
647 data_fix_root = NULL;
43ca9aa6 648#endif
6efd877d
KR
649 }
650#endif
43ca9aa6
KR
651
652#ifdef BFD_ASSEMBLER
653 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
654#else
6efd877d
KR
655 relax_segment (text_frag_root, SEG_TEXT);
656 relax_segment (data_frag_root, SEG_DATA);
657 relax_segment (bss_frag_root, SEG_BSS);
658 /*
7f2cb270
KR
659 * Now the addresses of frags are correct within the segment.
660 */
6efd877d
KR
661
662 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
663 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
664 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
665
666 /*
7f2cb270
KR
667 * Join the 2 segments into 1 huge segment.
668 * To do this, re-compute every rn_address in the SEG_DATA frags.
669 * Then join the data frags after the text frags.
670 *
671 * Determine a_data [length of data segment].
672 */
6efd877d
KR
673 if (data_frag_root)
674 {
675 register relax_addressT slide;
676
677 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
678
679 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
680 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
681 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
ebfb4167
MT
682#ifdef OBJ_BOUT
683#define RoundUp(N,S) (((N)+(S)-1)&-(S))
6efd877d 684 /* For b.out: If the data section has a strict alignment
7f2cb270
KR
685 requirement, its load address in the .o file will be
686 rounded up from the size of the text section. These
687 two values are *not* the same! Similarly for the bss
688 section.... */
6efd877d 689 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
ebfb4167
MT
690#endif
691
6efd877d
KR
692 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
693 {
694 fragP->fr_address += slide;
695 } /* for each data frag */
696
697 know (text_last_frag != 0);
698 text_last_frag->fr_next = data_frag_root;
699 }
700 else
701 {
702 H_SET_DATA_SIZE (&headers, 0);
6efd877d 703 }
ebfb4167
MT
704
705#ifdef OBJ_BOUT
6efd877d
KR
706 /* See above comments on b.out data section address. */
707 {
708 long bss_vma;
709 if (data_last_frag == 0)
710 bss_vma = H_GET_TEXT_SIZE (&headers);
711 else
712 bss_vma = data_last_frag->fr_address;
713 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
714 bss_address_frag.fr_address = bss_vma;
715 }
43ca9aa6 716#else /* ! OBJ_BOUT */
6efd877d
KR
717 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
718 H_GET_DATA_SIZE (&headers));
719
65bfcf2e 720
6efd877d
KR
721 /* Slide all the frags */
722 if (bss_frag_root)
723 {
85825401 724 relax_addressT slide = bss_address_frag.fr_address;
09952cd9 725
6efd877d 726 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
65bfcf2e 727 {
6efd877d
KR
728 fragP->fr_address += slide;
729 } /* for each bss frag */
730 }
65bfcf2e 731
43ca9aa6 732#endif /* ! OBJ_BOUT */
85825401 733
6efd877d 734 if (bss_last_frag)
85825401
ILT
735 H_SET_BSS_SIZE (&headers,
736 bss_last_frag->fr_address - bss_frag_root->fr_address);
737 else
738 H_SET_BSS_SIZE (&headers, 0);
43ca9aa6 739#endif /* BFD_ASSEMBLER */
65bfcf2e 740
43ca9aa6 741#ifndef BFD_ASSEMBLER
6efd877d 742 /*
7f2cb270
KR
743 *
744 * Crawl the symbol chain.
745 *
746 * For each symbol whose value depends on a frag, take the address of
747 * that frag and subsume it into the value of the symbol.
748 * After this, there is just one way to lookup a symbol value.
749 * Values are left in their final state for object file emission.
750 * We adjust the values of 'L' local symbols, even if we do
751 * not intend to emit them to the object file, because their values
752 * are needed for fix-ups.
753 *
754 * Unless we saw a -L flag, remove all symbols that begin with 'L'
755 * from the symbol chain. (They are still pointed to by the fixes.)
756 *
757 * Count the remaining symbols.
758 * Assign a symbol number to each symbol.
759 * Count the number of string-table chars we will emit.
760 * Put this info into the headers as appropriate.
761 *
762 */
6efd877d
KR
763 know (zero_address_frag.fr_address == 0);
764 string_byte_count = sizeof (string_byte_count);
765
766 obj_crawl_symbol_chain (&headers);
767
768 if (string_byte_count == sizeof (string_byte_count))
43ca9aa6 769 string_byte_count = 0;
6efd877d
KR
770
771 H_SET_STRING_SIZE (&headers, string_byte_count);
772
773 /*
7f2cb270
KR
774 * Addresses of frags now reflect addresses we use in the object file.
775 * Symbol values are correct.
776 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
777 * Also converting any machine-dependent frags using md_convert_frag();
778 */
6efd877d
KR
779 subseg_change (SEG_TEXT, 0);
780
781 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
782 {
43ca9aa6 783 cvt_frag_to_fill (&headers, fragP);
6efd877d 784
43ca9aa6
KR
785 /* Some assert macros don't work with # directives mixed in. */
786#ifndef NDEBUG
787 if (!(fragP->fr_next == NULL
ebfb4167 788#ifdef OBJ_BOUT
43ca9aa6 789 || fragP->fr_next == data_frag_root
ebfb4167 790#endif
6efd877d 791 || ((fragP->fr_next->fr_address - fragP->fr_address)
43ca9aa6
KR
792 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
793 abort ();
794#endif
795 }
796#endif /* ! BFD_ASSEMBLER */
6efd877d 797
fecd2382 798#ifndef WORKING_DOT_WORD
6efd877d
KR
799 {
800 struct broken_word *lie;
801 struct broken_word **prevP;
802
803 prevP = &broken_words;
804 for (lie = broken_words; lie; lie = lie->next_broken_word)
805 if (!lie->added)
a39116f1 806 {
43ca9aa6
KR
807#ifdef BFD_ASSEMBLER
808 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
809 2, lie->add, lie->sub, lie->addnum, 0, BFD_RELOC_NONE);
810#else
811#if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
812 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
813 2, lie->add,
814 lie->sub, lie->addnum,
815 0, NO_RELOC);
816#else
fecd2382 817#ifdef TC_NS32K
6efd877d
KR
818 fix_new_ns32k (lie->frag,
819 lie->word_goes_here - lie->frag->fr_literal,
820 2,
821 lie->add,
822 lie->sub,
823 lie->addnum,
824 0, 0, 2, 0, 0);
343fb08d 825#else
6efd877d
KR
826 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
827 2, lie->add,
828 lie->sub, lie->addnum,
829 0, 0);
fecd2382 830#endif /* TC_NS32K */
43ca9aa6
KR
831#endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
832#endif /* BFD_ASSEMBLER */
6efd877d 833 *prevP = lie->next_broken_word;
a39116f1 834 }
6efd877d
KR
835 else
836 prevP = &(lie->next_broken_word);
837
838 for (lie = broken_words; lie;)
839 {
840 struct broken_word *untruth;
841 char *table_ptr;
842 long table_addr;
843 long from_addr, to_addr;
844 int n, m;
845
6efd877d
KR
846 fragP = lie->dispfrag;
847
43ca9aa6 848 /* Find out how many broken_words go here. */
6efd877d
KR
849 n = 0;
850 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
851 if (untruth->added == 1)
852 n++;
853
854 table_ptr = lie->dispfrag->fr_opcode;
855 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
43ca9aa6
KR
856 /* Create the jump around the long jumps. This is a short
857 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
6efd877d
KR
858 from_addr = table_addr;
859 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
860 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
861 table_ptr += md_short_jump_size;
862 table_addr += md_short_jump_size;
863
864 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
865 {
866 if (lie->added == 2)
867 continue;
868 /* Patch the jump table */
869 /* This is the offset from ??? to table_ptr+0 */
870 to_addr = table_addr
871 - S_GET_VALUE (lie->sub);
872 md_number_to_chars (lie->word_goes_here, to_addr, 2);
873 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
874 {
875 if (untruth->use_jump == lie)
876 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
877 }
878
879 /* Install the long jump */
880 /* this is a long jump from table_ptr+0 to the final target */
881 from_addr = table_addr;
882 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
883 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
884 table_ptr += md_long_jump_size;
885 table_addr += md_long_jump_size;
886 }
887 }
888 }
fecd2382 889#endif /* not WORKING_DOT_WORD */
6efd877d 890
43ca9aa6 891#ifndef BFD_ASSEMBLER
3eb802b5 892#ifndef OBJ_VMS
6efd877d
KR
893 { /* not vms */
894 /*
3eb802b5
ILT
895 * Scan every FixS performing fixups. We had to wait until now to do
896 * this because md_convert_frag() may have made some fixSs.
897 */
6efd877d
KR
898 int trsize, drsize;
899
900 subseg_change (SEG_TEXT, 0);
901 trsize = md_reloc_size * fixup_segment (text_fix_root,
902 SEG_TEXT);
903 subseg_change (SEG_DATA, 0);
904 drsize = md_reloc_size * fixup_segment (data_fix_root,
905 SEG_DATA);
906 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
907
908 /* FIXME move this stuff into the pre-write-hook */
909 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
910 H_SET_ENTRY_POINT (&headers, 0);
911
912 obj_pre_write_hook (&headers); /* extra coff stuff */
6efd877d
KR
913
914 object_file_size = H_GET_FILE_SIZE (&headers);
915 next_object_file_charP = the_object_file = xmalloc (object_file_size);
916
917 output_file_create (out_file_name);
918
919 obj_header_append (&next_object_file_charP, &headers);
920
921 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
922
923 /*
43ca9aa6
KR
924 * Emit code.
925 */
6efd877d
KR
926 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
927 {
928 register long count;
929 register char *fill_literal;
930 register long fill_size;
931
932 know (fragP->fr_type == rs_fill);
933 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
934 fill_literal = fragP->fr_literal + fragP->fr_fix;
935 fill_size = fragP->fr_var;
936 know (fragP->fr_offset >= 0);
937
938 for (count = fragP->fr_offset; count; count--)
939 {
940 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
941 } /* for each */
942
943 } /* for each code frag. */
944
945 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
946
947 /*
43ca9aa6
KR
948 * Emit relocations.
949 */
6efd877d
KR
950 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
951 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 952#ifdef TC_I960
6efd877d 953 /* Make addresses in data relocation directives relative to beginning of
43ca9aa6
KR
954 * first data fragment, not end of last text fragment: alignment of the
955 * start of the data segment may place a gap between the segments.
956 */
6efd877d 957 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
fecd2382 958#else /* TC_I960 */
6efd877d 959 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
fecd2382 960#endif /* TC_I960 */
6efd877d
KR
961
962 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)));
963
964 /*
43ca9aa6
KR
965 * Emit line number entries.
966 */
6efd877d
KR
967 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
968 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)));
969
970 /*
3eb802b5
ILT
971 * Emit symbols.
972 */
6efd877d
KR
973 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
974 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)));
975
976 /*
3eb802b5
ILT
977 * Emit strings.
978 */
6efd877d
KR
979
980 if (string_byte_count > 0)
981 {
982 obj_emit_strings (&next_object_file_charP);
983 } /* only if we have a string table */
984
45432836 985#ifdef BFD_HEADERS
6efd877d
KR
986 bfd_seek (stdoutput, 0, 0);
987 bfd_write (the_object_file, 1, object_file_size, stdoutput);
45432836 988#else
6efd877d
KR
989
990 /* Write the data to the file */
991 output_file_append (the_object_file, object_file_size, out_file_name);
45432836 992#endif
6efd877d
KR
993
994 output_file_close (out_file_name);
995 } /* non vms output */
996#else /* VMS */
997 /*
3eb802b5
ILT
998 * Now do the VMS-dependent part of writing the object file
999 */
85825401
ILT
1000 VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
1001 H_GET_DATA_SIZE (&headers),
1002 H_GET_BSS_SIZE (&headers),
3eb802b5 1003 text_frag_root, data_frag_root);
6efd877d 1004#endif /* VMS */
43ca9aa6
KR
1005#else /* BFD_ASSEMBLER */
1006#ifdef obj_frob_file
1007 obj_frob_file ();
1008#endif
6efd877d 1009
43ca9aa6
KR
1010 /* Set up symbol table, and write it out. */
1011 if (symbol_rootP)
1012 {
1013 int i = 0, n;
1014 symbolS *symp;
1015
1016 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1017 {
1018 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
1019 /* So far, common symbols have been treated like undefined symbols.
1020 Put them in the common section now. */
1021 if (S_IS_DEFINED (symp) == 0
1022 && S_GET_VALUE (symp) != 0)
1023 S_SET_SEGMENT (symp, &bfd_com_section);
1024#if 0
1025 printf ("symbol `%s'\n\t@%x: value=%d type=%d forward=%x seg=%s\n",
1026 S_GET_NAME (symp), symp,
1027 S_GET_VALUE (symp),
1028 S_GET_DATA_TYPE (symp),
1029 symp->sy_forward,
1030 segment_name (symp->bsym->section));
1031#endif
1032 {
1033 int punt = 0;
1034#ifdef obj_frob_symbol
1035 obj_frob_symbol (symp, punt);
1036 if (punt)
1037 goto punt_it;
1038#endif
1039#ifdef tc_frob_symbol
1040 tc_frob_symbol (symp, punt);
1041 if (punt)
1042 goto punt_it;
1043#endif
1044 }
1045 /* If we don't want to keep this symbol, splice it out of the
1046 chain now. */
1047 if (S_IS_LOCAL (symp))
1048 {
1049 symbolS *prev, *next;
1050 punt_it:
1051 prev = symbol_previous (symp);
1052 next = symbol_next (symp);
1053#ifdef DEBUG
1054 /* debugging: verify consistency */
1055 {
1056 symbolS *p = symp, *n = symp;
1057 while (symbol_previous (p))
1058 p = symbol_previous (p);
1059 while (symbol_next (n))
1060 n = symbol_next (n);
1061 verify_symbol_chain (p, n);
1062 }
1063#endif
1064 if (prev)
1065 {
1066 symbol_next (prev) = next;
1067 symp = prev;
1068 }
1069 else
1070 abort ();
1071 if (next)
1072 symbol_previous (next) = prev;
1073 else
1074 symbol_lastP = prev;
1075#ifdef DEBUG
1076 /* debugging: verify consistency */
1077 {
1078 symbolS *p = symp, *n = symp;
1079 while (symbol_previous (p))
1080 p = symbol_previous (p);
1081 while (symbol_next (n))
1082 n = symbol_next (n);
1083 verify_symbol_chain (p, n);
1084 }
45432836 1085#endif
43ca9aa6
KR
1086 continue;
1087 }
1088 i++;
1089 }
1090 n = i;
1091 if (n)
1092 {
1093 asymbol **asympp;
1094 boolean result;
1095
1096 asympp = (asymbol **) bfd_alloc (stdoutput, n * sizeof (asymbol *));
1097 symp = symbol_rootP;
1098 for (i = 0; i < n; i++, symp = symbol_next (symp))
1099 {
1100 asympp[i] = symp->bsym;
1101 symp->written = 1;
1102 }
1103 result = bfd_set_symtab (stdoutput, asympp, n);
1104 assert (result == true);
1105 }
1106 }
1107
1108 /* Now that all the sizes are known, and contents correct, we can
1109 start writing the file. */
1110 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1111
1112 output_file_close (out_file_name);
1113#endif /* BFD_ASSEMBLER */
1114}
1115#endif /* BFD */
fecd2382
RP
1116
1117/*
1118 * relax_segment()
1119 *
1120 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1121 * values.
1122 *
1123 * Relax the frags.
1124 *
1125 * After this, all frags in this segment have addresses that are correct
1126 * within the segment. Since segments live in different file addresses,
1127 * these frag addresses may not be the same as final object-file addresses.
1128 */
45432836
JG
1129
1130
43ca9aa6
KR
1131static int
1132is_dnrange (f1, f2)
1133 struct frag *f1;
1134 struct frag *f2;
1135{
1136 for (; f1; f1 = f1->fr_next)
1137 if (f1->fr_next == f2)
1138 return 1;
1139 return 0;
1140}
1141
1142/* Relax_align. Advance location counter to next address that has 'alignment'
1143 lowest order bits all 0s. */
1144
1145/* How many addresses does the .align take? */
1146static relax_addressT
1147relax_align (address, alignment)
1148 register relax_addressT address; /* Address now. */
1149 register long alignment; /* Alignment (binary). */
1150{
1151 relax_addressT mask;
1152 relax_addressT new_address;
1153
1154 mask = ~((~0) << alignment);
1155 new_address = (address + mask) & (~mask);
1156 if (linkrelax)
1157 /* We must provide lots of padding, so the linker can discard it
1158 when needed. The linker will not add extra space, ever. */
1159 new_address += (1 << alignment);
1160 return (new_address - address);
1161}
45432836 1162
6efd877d
KR
1163void
1164relax_segment (segment_frag_root, segment)
1165 struct frag *segment_frag_root;
43ca9aa6 1166 segT segment;
fecd2382 1167{
6efd877d
KR
1168 register struct frag *fragP;
1169 register relax_addressT address;
43ca9aa6 1170#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
6efd877d 1171 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
45432836 1172#endif
6efd877d
KR
1173 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1174 subseg_change (segment, 0);
1175
7f2cb270
KR
1176 /* For each frag in segment: count and store (a 1st guess of)
1177 fr_address. */
6efd877d
KR
1178 address = 0;
1179 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1180 {
1181 fragP->fr_address = address;
1182 address += fragP->fr_fix;
1183
1184 switch (fragP->fr_type)
1185 {
1186 case rs_fill:
1187 address += fragP->fr_offset * fragP->fr_var;
1188 break;
1189
1190 case rs_align:
1191 address += relax_align (address, fragP->fr_offset);
1192 break;
1193
1194 case rs_org:
7f2cb270 1195 /* Assume .org is nugatory. It will grow with 1st relax. */
6efd877d
KR
1196 break;
1197
1198 case rs_machine_dependent:
1199 address += md_estimate_size_before_relax (fragP, segment);
1200 break;
1201
fecd2382 1202#ifndef WORKING_DOT_WORD
6efd877d
KR
1203 /* Broken words don't concern us yet */
1204 case rs_broken_word:
1205 break;
fecd2382 1206#endif
6efd877d
KR
1207
1208 default:
1209 BAD_CASE (fragP->fr_type);
1210 break;
1211 } /* switch(fr_type) */
1212 } /* for each frag in the segment */
1213
7f2cb270 1214 /* Do relax(). */
6efd877d 1215 {
7f2cb270 1216 long stretch; /* May be any size, 0 or negative. */
6efd877d
KR
1217 /* Cumulative number of addresses we have */
1218 /* relaxed this pass. */
1219 /* We may have relaxed more than one address. */
7f2cb270
KR
1220 long stretched; /* Have we stretched on this pass? */
1221 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1222 grew, and another shrank. If a branch instruction doesn't fit anymore,
1223 we could be scrod. */
6efd877d
KR
1224
1225 do
1226 {
1227 stretch = stretched = 0;
1228 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1229 {
7f2cb270
KR
1230 long growth = 0;
1231 unsigned long was_address;
1232 long offset;
1233 symbolS *symbolP;
1234 long target;
1235 long after;
1236 long aim;
6efd877d
KR
1237
1238 was_address = fragP->fr_address;
1239 address = fragP->fr_address += stretch;
1240 symbolP = fragP->fr_symbol;
1241 offset = fragP->fr_offset;
6efd877d
KR
1242
1243 switch (fragP->fr_type)
1244 {
1245 case rs_fill: /* .fill never relaxes. */
1246 growth = 0;
1247 break;
1248
fecd2382 1249#ifndef WORKING_DOT_WORD
6efd877d 1250 /* JF: This is RMS's idea. I do *NOT* want to be blamed
7f2cb270
KR
1251 for it I do not want to write it. I do not want to have
1252 anything to do with it. This is not the proper way to
1253 implement this misfeature. */
6efd877d
KR
1254 case rs_broken_word:
1255 {
1256 struct broken_word *lie;
1257 struct broken_word *untruth;
6efd877d
KR
1258
1259 /* Yes this is ugly (storing the broken_word pointer
7f2cb270
KR
1260 in the symbol slot). Still, this whole chunk of
1261 code is ugly, and I don't feel like doing anything
1262 about it. Think of it as stubbornness in action. */
6efd877d
KR
1263 growth = 0;
1264 for (lie = (struct broken_word *) (fragP->fr_symbol);
1265 lie && lie->dispfrag == fragP;
1266 lie = lie->next_broken_word)
1267 {
1268
1269 if (lie->added)
1270 continue;
1271
7f2cb270
KR
1272 offset = (lie->add->sy_frag->fr_address
1273 + S_GET_VALUE (lie->add)
1274 + lie->addnum
1275 - (lie->sub->sy_frag->fr_address
1276 + S_GET_VALUE (lie->sub)));
6efd877d
KR
1277 if (offset <= -32768 || offset >= 32767)
1278 {
1279 if (flagseen['K'])
1280 as_warn (".word %s-%s+%ld didn't fit",
1281 S_GET_NAME (lie->add),
1282 S_GET_NAME (lie->sub),
1283 lie->addnum);
1284 lie->added = 1;
1285 if (fragP->fr_subtype == 0)
1286 {
1287 fragP->fr_subtype++;
1288 growth += md_short_jump_size;
1289 }
7f2cb270
KR
1290 for (untruth = lie->next_broken_word;
1291 untruth && untruth->dispfrag == lie->dispfrag;
1292 untruth = untruth->next_broken_word)
6efd877d
KR
1293 if ((untruth->add->sy_frag == lie->add->sy_frag)
1294 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1295 {
1296 untruth->added = 2;
1297 untruth->use_jump = lie;
1298 }
1299 growth += md_long_jump_size;
1300 }
1301 }
1302
1303 break;
1304 } /* case rs_broken_word */
fecd2382 1305#endif
6efd877d 1306 case rs_align:
43ca9aa6
KR
1307 growth = (relax_align ((relax_addressT) (address
1308 + fragP->fr_fix),
1309 offset)
1310 - relax_align ((relax_addressT) (was_address
1311 + fragP->fr_fix),
1312 offset));
6efd877d
KR
1313 break;
1314
1315 case rs_org:
1316 target = offset;
1317
1318 if (symbolP)
1319 {
43ca9aa6
KR
1320#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1321 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1322 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1323 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1324 || S_GET_SEGMENT (symbolP) == SEG_BSS);
6efd877d 1325 know (symbolP->sy_frag);
43ca9aa6
KR
1326 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1327 || (symbolP->sy_frag == &zero_address_frag));
45432836 1328#endif
6efd877d
KR
1329 target += S_GET_VALUE (symbolP)
1330 + symbolP->sy_frag->fr_address;
1331 } /* if we have a symbol */
1332
1333 know (fragP->fr_next);
1334 after = fragP->fr_next->fr_address;
1335 growth = ((target - after) > 0) ? (target - after) : 0;
43ca9aa6
KR
1336 /* Growth may be negative, but variable part of frag
1337 cannot have fewer than 0 chars. That is, we can't
1338 .org backwards. */
6efd877d
KR
1339
1340 growth -= stretch; /* This is an absolute growth factor */
1341 break;
1342
1343 case rs_machine_dependent:
1344 {
7f2cb270
KR
1345 const relax_typeS *this_type;
1346 const relax_typeS *start_type;
1347 relax_substateT next_state;
1348 relax_substateT this_state;
6efd877d 1349
7f2cb270
KR
1350 this_state = fragP->fr_subtype;
1351 start_type = this_type = md_relax_table + this_state;
6efd877d
KR
1352 target = offset;
1353
1354 if (symbolP)
1355 {
43ca9aa6
KR
1356#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1357 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1358 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1359 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1360 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
45432836 1361#endif
6efd877d 1362 know (symbolP->sy_frag);
43ca9aa6
KR
1363 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1364 || symbolP->sy_frag == &zero_address_frag);
6efd877d
KR
1365 target +=
1366 S_GET_VALUE (symbolP)
1367 + symbolP->sy_frag->fr_address;
1368
1369 /* If frag has yet to be reached on this pass,
7f2cb270
KR
1370 assume it will move by STRETCH just as we did.
1371 If this is not so, it will be because some frag
1372 between grows, and that will force another pass. */
6efd877d
KR
1373
1374 /* JF was just address */
1375 /* JF also added is_dnrange hack */
1376 /* There's gotta be a better/faster/etc way
7f2cb270 1377 to do this. . . */
6efd877d 1378 /* gnu@cygnus.com: I changed this from > to >=
7f2cb270
KR
1379 because I ran into a zero-length frag (fr_fix=0)
1380 which was created when the obstack needed a new
1381 chunk JUST AFTER the opcode of a branch. Since
1382 fr_fix is zero, fr_address of this frag is the same
1383 as fr_address of the next frag. This
1384 zero-length frag was variable and jumped to .+2
1385 (in the next frag), but since the > comparison
1386 below failed (the two were =, not >), "stretch"
1387 was not added to the target. Stretch was 178, so
1388 the offset appeared to be .-176 instead, which did
1389 not fit into a byte branch, so the assembler
1390 relaxed the branch to a word. This didn't compare
1391 with what happened when the same source file was
1392 assembled on other machines, which is how I found it.
1393 You might want to think about what other places have
1394 trouble with zero length frags... */
6efd877d
KR
1395
1396 if (symbolP->sy_frag->fr_address >= was_address
1397 && is_dnrange (fragP, symbolP->sy_frag))
1398 {
1399 target += stretch;
7f2cb270 1400 }
6efd877d
KR
1401 } /* if there's a symbol attached */
1402
1403 aim = target - address - fragP->fr_fix;
1404 /* The displacement is affected by the instruction size
7f2cb270
KR
1405 for the 32k architecture. I think we ought to be able
1406 to add fragP->fr_pcrel_adjust in all cases (it should be
1407 zero if not used), but just in case it breaks something
1408 else we'll put this inside #ifdef NS32K ... #endif */
fecd2382 1409#ifdef TC_NS32K
6efd877d 1410 aim += fragP->fr_pcrel_adjust;
fecd2382 1411#endif /* TC_NS32K */
6efd877d
KR
1412
1413 if (aim < 0)
1414 {
1415 /* Look backwards. */
1416 for (next_state = this_type->rlx_more; next_state;)
7f2cb270
KR
1417 if (aim >= this_type->rlx_backward)
1418 next_state = 0;
1419 else
1420 {
1421 /* Grow to next state. */
1422 this_state = next_state;
1423 this_type = md_relax_table + this_state;
1424 next_state = this_type->rlx_more;
1425 }
6efd877d
KR
1426 }
1427 else
1428 {
a39116f1 1429#ifdef M68K_AIM_KLUDGE
6efd877d 1430 M68K_AIM_KLUDGE (aim, this_state, this_type);
fecd2382 1431#endif
6efd877d
KR
1432 /* Look forwards. */
1433 for (next_state = this_type->rlx_more; next_state;)
7f2cb270
KR
1434 if (aim <= this_type->rlx_forward)
1435 next_state = 0;
1436 else
1437 {
1438 /* Grow to next state. */
1439 this_state = next_state;
1440 this_type = md_relax_table + this_state;
1441 next_state = this_type->rlx_more;
1442 }
6efd877d
KR
1443 }
1444
7f2cb270
KR
1445 growth = this_type->rlx_length - start_type->rlx_length;
1446 if (growth != 0)
6efd877d 1447 fragP->fr_subtype = this_state;
7f2cb270
KR
1448 }
1449 break;
6efd877d
KR
1450
1451 default:
1452 BAD_CASE (fragP->fr_type);
1453 break;
1454 }
1455 if (growth)
1456 {
1457 stretch += growth;
1458 stretched++;
1459 }
1460 } /* For each frag in the segment. */
1461 }
1462 while (stretched); /* Until nothing further to relax. */
1463 } /* do_relax */
1464
1465 /*
7f2cb270
KR
1466 * We now have valid fr_address'es for each frag.
1467 */
6efd877d
KR
1468
1469 /*
7f2cb270
KR
1470 * All fr_address's are correct, relative to their own segment.
1471 * We have made all the fixS we will ever make.
1472 */
6efd877d 1473} /* relax_segment() */
fecd2382 1474
fecd2382 1475/* fixup_segment()
6efd877d 1476
fecd2382
RP
1477 Go through all the fixS's in a segment and see which ones can be
1478 handled now. (These consist of fixS where we have since discovered
1479 the value of a symbol, or the address of the frag involved.)
1480 For each one, call md_apply_fix to put the fix into the frag data.
6efd877d 1481
fecd2382
RP
1482 Result is a count of how many relocation structs will be needed to
1483 handle the remaining fixS's that we couldn't completely handle here.
1484 These will be output later by emit_relocations(). */
1485
09952cd9 1486static long
6efd877d 1487fixup_segment (fixP, this_segment_type)
09952cd9 1488 register fixS *fixP;
6efd877d 1489 segT this_segment_type; /* N_TYPE bits for segment. */
fecd2382 1490{
6efd877d
KR
1491 register long seg_reloc_count;
1492 register symbolS *add_symbolP;
1493 register symbolS *sub_symbolP;
43ca9aa6 1494 long add_number;
6efd877d
KR
1495 register int size;
1496 register char *place;
1497 register long where;
1498 register char pcrel;
1499 register fragS *fragP;
43ca9aa6 1500 register segT add_symbol_segment = absolute_section;
6efd877d 1501
6efd877d 1502 seg_reloc_count = 0;
6efd877d
KR
1503 /* If the linker is doing the relaxing, we must not do any fixups */
1504 if (linkrelax)
43ca9aa6
KR
1505 for (; fixP; fixP = fixP->fx_next)
1506 seg_reloc_count++;
6efd877d 1507 else
6efd877d
KR
1508 for (; fixP; fixP = fixP->fx_next)
1509 {
1510 fragP = fixP->fx_frag;
1511 know (fragP);
1512 where = fixP->fx_where;
1513 place = fragP->fr_literal + where;
1514 size = fixP->fx_size;
1515 add_symbolP = fixP->fx_addsy;
fecd2382 1516#ifdef TC_I960
6efd877d
KR
1517 if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP))
1518 {
43ca9aa6
KR
1519 /* Relocation should be done via the associated 'bal'
1520 entry point symbol. */
6efd877d
KR
1521
1522 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
1523 {
1524 as_bad ("No 'bal' entry point for leafproc %s",
1525 S_GET_NAME (add_symbolP));
1526 continue;
1527 }
1528 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
43ca9aa6 1529 }
fecd2382 1530#endif
6efd877d
KR
1531 sub_symbolP = fixP->fx_subsy;
1532 add_number = fixP->fx_offset;
1533 pcrel = fixP->fx_pcrel;
1534
1535 if (add_symbolP)
43ca9aa6 1536 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
6efd877d
KR
1537
1538 if (sub_symbolP)
1539 {
1540 if (!add_symbolP)
1541 {
1542 /* Its just -sym */
43ca9aa6
KR
1543 if (S_GET_SEGMENT (sub_symbolP) != absolute_section)
1544 as_bad ("Negative of non-absolute symbol %s",
1545 S_GET_NAME (sub_symbolP));
6efd877d
KR
1546
1547 add_number -= S_GET_VALUE (sub_symbolP);
6efd877d
KR
1548 }
1549 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1550 && (SEG_NORMAL (add_symbol_segment)
43ca9aa6 1551 || (add_symbol_segment == absolute_section)))
6efd877d 1552 {
43ca9aa6
KR
1553 /* Difference of 2 symbols from same segment.
1554 Can't make difference of 2 undefineds: 'value' means
1555 something different for N_UNDF. */
fecd2382 1556#ifdef TC_I960
6efd877d 1557 /* Makes no sense to use the difference of 2 arbitrary symbols
43ca9aa6 1558 as the target of a call instruction. */
6efd877d
KR
1559 if (fixP->fx_callj)
1560 {
1561 as_bad ("callj to difference of 2 symbols");
1562 }
1563#endif /* TC_I960 */
1564 add_number += S_GET_VALUE (add_symbolP) -
1565 S_GET_VALUE (sub_symbolP);
1566
1567 add_symbolP = NULL;
1568 fixP->fx_addsy = NULL;
1569 }
1570 else
1571 {
1572 /* Different segments in subtraction. */
43ca9aa6
KR
1573 know (!(S_IS_EXTERNAL (sub_symbolP)
1574 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
6efd877d 1575
43ca9aa6 1576 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
6efd877d
KR
1577 {
1578 add_number -= S_GET_VALUE (sub_symbolP);
1579 }
1580 else
1581 {
1582 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1583 segment_name (S_GET_SEGMENT (sub_symbolP)),
1584 S_GET_NAME (sub_symbolP), fragP->fr_address + where);
1585 } /* if absolute */
1586 }
1587 } /* if sub_symbolP */
1588
1589 if (add_symbolP)
1590 {
1591 if (add_symbol_segment == this_segment_type && pcrel)
1592 {
1593 /*
43ca9aa6
KR
1594 * This fixup was made when the symbol's segment was
1595 * SEG_UNKNOWN, but it is now in the local segment.
1596 * So we know how to do the address without relocation.
1597 */
fecd2382 1598#ifdef TC_I960
43ca9aa6
KR
1599 /* reloc_callj() may replace a 'call' with a 'calls' or a
1600 'bal', in which cases it modifies *fixP as appropriate.
1601 In the case of a 'calls', no further work is required,
1602 and *fixP has been set up to make the rest of the code
1603 below a no-op. */
6efd877d 1604 reloc_callj (fixP);
fecd2382 1605#endif /* TC_I960 */
6efd877d
KR
1606
1607 add_number += S_GET_VALUE (add_symbolP);
1608 add_number -= md_pcrel_from (fixP);
1609 pcrel = 0; /* Lie. Don't want further pcrel processing. */
1610 fixP->fx_addsy = NULL; /* No relocations please. */
1611 }
1612 else
1613 {
43ca9aa6 1614 if (add_symbol_segment == absolute_section)
6efd877d 1615 {
fecd2382 1616#ifdef TC_I960
43ca9aa6
KR
1617 /* See comment about reloc_callj() above. */
1618 reloc_callj (fixP);
fecd2382 1619#endif /* TC_I960 */
6efd877d
KR
1620 add_number += S_GET_VALUE (add_symbolP);
1621 fixP->fx_addsy = NULL;
1622 add_symbolP = NULL;
43ca9aa6
KR
1623 }
1624 else if (add_symbol_segment == undefined_section
1625#ifdef BFD_ASSEMBLER
1626 || add_symbol_segment == &bfd_com_section
1627#endif
1628 )
1629 {
fecd2382 1630#ifdef TC_I960
6efd877d
KR
1631 if ((int) fixP->fx_bit_fixP == 13)
1632 {
1633 /* This is a COBR instruction. They have only a
43ca9aa6
KR
1634 * 13-bit displacement and are only to be used
1635 * for local branches: flag as error, don't generate
1636 * relocation.
1637 */
6efd877d
KR
1638 as_bad ("can't use COBR format with external label");
1639 fixP->fx_addsy = NULL; /* No relocations please. */
1640 continue;
1641 } /* COBR */
fecd2382 1642#endif /* TC_I960 */
6d5460ab 1643
fecd2382 1644#ifdef OBJ_COFF
c593cf41 1645#ifdef TE_I386AIX
6efd877d
KR
1646 if (S_IS_COMMON (add_symbolP))
1647 add_number += S_GET_VALUE (add_symbolP);
c593cf41 1648#endif /* TE_I386AIX */
fecd2382 1649#endif /* OBJ_COFF */
6efd877d 1650 ++seg_reloc_count;
43ca9aa6
KR
1651 }
1652 else
1653 {
1654 seg_reloc_count++;
1655 add_number += S_GET_VALUE (add_symbolP);
1656 }
6efd877d
KR
1657 } /* if not in local seg */
1658 } /* if there was a + symbol */
1659
1660 if (pcrel)
1661 {
1662 add_number -= md_pcrel_from (fixP);
1663 if (add_symbolP == 0)
1664 {
1665 fixP->fx_addsy = &abs_symbol;
1666 ++seg_reloc_count;
1667 } /* if there's an add_symbol */
1668 } /* if pcrel */
1669
1670 if (!fixP->fx_bit_fixP)
1671 {
1672 if ((size == 1 &&
43ca9aa6
KR
1673 (add_number & ~0xFF)
1674 && ((add_number & ~0xFF) != (-1 & ~0xFF)))
1675 || (size == 2
1676 && (add_number & ~0xFFFF)
1677 && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
6efd877d
KR
1678 {
1679 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
1680 add_number, size, fragP->fr_address + where);
1681 } /* generic error checking */
1682#ifdef WARN_SIGNED_OVERFLOW_WORD
43ca9aa6
KR
1683 /* Warn if a .word value is too large when treated as a signed
1684 number. We already know it is not too negative. This is to
1685 catch over-large switches generated by gcc on the 68k. */
6efd877d
KR
1686 if (!flagseen['J']
1687 && size == 2
1688 && add_number > 0x7fff)
1689 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
1690 add_number, fragP->fr_address + where);
1691#endif
1692 } /* not a bit fix */
1693
43ca9aa6
KR
1694#ifdef BFD_ASSEMBLER
1695 md_apply_fix (fixP, &add_number);
1696#else
6efd877d 1697 md_apply_fix (fixP, add_number);
43ca9aa6 1698#endif
6efd877d
KR
1699 } /* For each fixS in this segment. */
1700
fecd2382
RP
1701#ifdef OBJ_COFF
1702#ifdef TC_I960
6efd877d
KR
1703 {
1704 fixS *topP = fixP;
1705
1706 /* two relocs per callj under coff. */
1707 for (fixP = topP; fixP; fixP = fixP->fx_next)
1708 {
1709 if (fixP->fx_callj && fixP->fx_addsy != 0)
1710 {
1711 ++seg_reloc_count;
1712 } /* if callj and not already fixed. */
1713 } /* for each fix */
1714 }
fecd2382 1715#endif /* TC_I960 */
6efd877d 1716
fecd2382 1717#endif /* OBJ_COFF */
6efd877d
KR
1718 return (seg_reloc_count);
1719} /* fixup_segment() */
fecd2382 1720
fecd2382 1721/* end of write.c */
This page took 0.134969 seconds and 4 git commands to generate.