Initial revision
[deliverable/binutils-gdb.git] / gas / write.c
CommitLineData
fecd2382
RP
1/* write.c - emit .o file
2 Copyright (C) 1986, 1987, 1990, 1991 Free Software Foundation, Inc.
3
4This file is part of GAS, the GNU Assembler.
5
6GAS is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GAS is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GAS; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/* static const char rcsid[] = "$Id$"; */
21
22/*
23
24 This thing should be set up to do byteordering correctly. But...
25
26 In order to cross-assemble the target machine must have an a.out header
27 similar to the one in a.out.h on THIS machine. Byteorder doesn't matter,
28 we take special care of it, but the numbers must be the same SIZE (# of
29 bytes) and in the same PLACE. If this is not true, you will have some
30 trouble.
31 */
32
33#include "as.h"
34
35#include "subsegs.h"
36#include "obstack.h"
37#include "output-file.h"
38
39/* Hook for machine dependent relocation information output routine.
40 If not defined, the variable is allocated in BSS (Fortran common model).
41 If some other module defines it, we will see their value. */
42
43void (*md_emit_relocations)();
44
45/*
46 * In: length of relocation (or of address) in chars: 1, 2 or 4.
47 * Out: GNU LD relocation length code: 0, 1, or 2.
48 */
49
50unsigned char
51nbytes_r_length [] = {
52 42, 0, 1, 42, 2
53 };
54
55
56static struct frag *text_frag_root;
57static struct frag *data_frag_root;
58
59static struct frag *text_last_frag; /* Last frag in segment. */
60static struct frag *data_last_frag; /* Last frag in segment. */
61
62static object_headers headers;
63
64long string_byte_count;
65
66static char *the_object_file;
67
68char *next_object_file_charP; /* Tracks object file bytes. */
69
70int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
71
72/* static long length; JF unused */ /* String length, including trailing '\0'. */
73
74
75#ifdef __STDC__
76
77static int is_dnrange(struct frag *f1, struct frag *f2);
78static long fixup_segment(fixS *fixP, segT this_segment_type);
79static relax_addressT relax_align(relax_addressT address, long alignment);
80static void relax_segment(struct frag *segment_frag_root, segT segment_type);
81
82#else
83
84static int is_dnrange();
85static long fixup_segment();
86static relax_addressT relax_align();
87static void relax_segment();
88
89#endif /* __STDC__ */
90
91/*
92 * fix_new()
93 *
94 * Create a fixS in obstack 'notes'.
95 */
96fixS *fix_new(frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
97fragS *frag; /* Which frag? */
98int where; /* Where in that frag? */
99short int size; /* 1, 2 or 4 usually. */
100symbolS *add_symbol; /* X_add_symbol. */
101symbolS *sub_symbol; /* X_subtract_symbol. */
102long offset; /* X_add_number. */
103int pcrel; /* TRUE if PC-relative relocation. */
104enum reloc_type r_type; /* Relocation type */
105{
106 register fixS * fixP;
107
108 fixP = (fixS *)obstack_alloc(&notes,sizeof(fixS));
109
110 fixP->fx_frag = frag;
111 fixP->fx_where = where;
112 fixP->fx_size = size;
113 fixP->fx_addsy = add_symbol;
114 fixP->fx_subsy = sub_symbol;
115 fixP->fx_offset = offset;
116 fixP->fx_pcrel = pcrel;
117 fixP->fx_r_type = r_type;
118 fixP->fx_next = NULL;
119
120 /* JF these 'cuz of the NS32K stuff */
121 fixP->fx_im_disp = 0;
122 fixP->fx_pcrel_adjust = 0;
123 fixP->fx_bsr = 0;
124 fixP->fx_bit_fixP = 0;
125
126 if (*seg_fix_tailP)
127 (*seg_fix_tailP)->fx_next = fixP;
128 else
129 *seg_fix_rootP = fixP;
130 *seg_fix_tailP = fixP;
131 fixP->fx_callj = 0;
132 return fixP;
133}
134\f
135void write_object_file() {
136 register struct frchain * frchainP; /* Track along all frchains. */
137 register fragS * fragP; /* Track along all frags. */
138 register struct frchain * next_frchainP;
139 register fragS * * prev_fragPP;
140/* register char * name; */
141/* symbolS *symbolP; */
142/* register symbolS ** symbolPP; */
143 /* register fixS * fixP; JF unused */
144 unsigned int data_siz;
145
146#ifdef DONTDEF
147 void gdb_emit();
148 void gdb_end();
149#endif
150 long object_file_size;
151
152#ifdef VMS
153 /*
154 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
155 * call a routine to check for the definition of the procedure
156 * "_main", and if so -- fix it up so that it can be program
157 * entry point.
158 */
159 VMS_Check_For_Main();
160#endif /* VMS */
161 /*
162 * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
163 * brane-damage. We then fake ".fill 0" because that is the kind of frag
164 * that requires least thought. ".align" frags like to have a following
165 * frag since that makes calculating their intended length trivial.
166 */
167#define SUB_SEGMENT_ALIGN (2)
168 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next) {
169#ifdef VMS
170 /*
171 * Under VAX/VMS, the linker (and PSECT specifications)
172 * take care of correctly aligning the segments.
173 * Doing the alignment here (on initialized data) can
174 * mess up the calculation of global data PSECT sizes.
175 */
176#undef SUB_SEGMENT_ALIGN
177#define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
178#endif /* VMS */
179 subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
180 frag_align (SUB_SEGMENT_ALIGN, 0);
181 /* frag_align will have left a new frag. */
182 /* Use this last frag for an empty ".fill". */
183 /*
184 * For this segment ...
185 * Create a last frag. Do not leave a "being filled in frag".
186 */
187 frag_wane (frag_now);
188 frag_now->fr_fix = 0;
189 know( frag_now->fr_next == NULL );
190 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
191 /* Above shows we haven't left a half-completed object on obstack. */
192 } /* walk the frag chain */
193\f
194 /*
195 * From now on, we don't care about sub-segments.
196 * Build one frag chain for each segment. Linked thru fr_next.
197 * We know that there is at least 1 text frchain & at least 1 data frchain.
198 */
199 prev_fragPP = &text_frag_root;
200 for (frchainP = frchain_root; frchainP; frchainP = next_frchainP) {
201 know( frchainP->frch_root );
202 * prev_fragPP = frchainP->frch_root;
203 prev_fragPP = & frchainP->frch_last->fr_next;
204
205 if (((next_frchainP = frchainP->frch_next) == NULL)
206 || next_frchainP == data0_frchainP) {
207 prev_fragPP = & data_frag_root;
208 if (next_frchainP) {
209 text_last_frag = frchainP->frch_last;
210 } else {
211 data_last_frag = frchainP->frch_last;
212 }
213 }
214 } /* walk the frag chain */
215
216 /*
217 * We have two segments. If user gave -R flag, then we must put the
218 * data frags into the text segment. Do this before relaxing so
219 * we know to take advantage of -R and make shorter addresses.
220 */
221 if (flagseen[ 'R' ]) {
222 fixS *tmp;
223
224 text_last_frag->fr_next = data_frag_root;
225 text_last_frag = data_last_frag;
226 data_last_frag = NULL;
227 data_frag_root = NULL;
228 if (text_fix_root) {
229 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next) ;;
230 tmp->fx_next=data_fix_root;
231 } else
232 text_fix_root=data_fix_root;
233 data_fix_root=NULL;
234 }
235
236 relax_segment(text_frag_root, SEG_TEXT);
237 relax_segment(data_frag_root, SEG_DATA);
238 /*
239 * Now the addresses of frags are correct within the segment.
240 */
241
242 know(text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
243 H_SET_TEXT_SIZE(&headers,text_last_frag->fr_address);
244 text_last_frag->fr_address=H_GET_TEXT_SIZE(&headers);
245
246 /*
247 * Join the 2 segments into 1 huge segment.
248 * To do this, re-compute every rn_address in the SEG_DATA frags.
249 * Then join the data frags after the text frags.
250 *
251 * Determine a_data [length of data segment].
252 */
253 if (data_frag_root) {
254 register relax_addressT slide;
255
256 know( text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0 );
257 H_SET_DATA_SIZE(&headers, data_last_frag->fr_address);
258 data_last_frag->fr_address = H_GET_DATA_SIZE(&headers);
259 slide = H_GET_TEXT_SIZE(&headers); /* & in file of the data segment. */
260
261 for (fragP = data_frag_root;
262 fragP;
263 fragP = fragP->fr_next)
264 {
265 fragP->fr_address += slide;
266 }
267 know( text_last_frag );
268 text_last_frag->fr_next = data_frag_root;
269 } else {
270 H_SET_DATA_SIZE(&headers,0);
271 data_siz = 0;
272 }
273
274 bss_address_frag.fr_address = H_GET_TEXT_SIZE(&headers) +
275 H_GET_DATA_SIZE(&headers);
276
277 H_SET_BSS_SIZE(&headers,local_bss_counter);
278
279 /*
280 *
281 * Crawl the symbol chain.
282 *
283 * For each symbol whose value depends on a frag, take the address of
284 * that frag and subsume it into the value of the symbol.
285 * After this, there is just one way to lookup a symbol value.
286 * Values are left in their final state for object file emission.
287 * We adjust the values of 'L' local symbols, even if we do
288 * not intend to emit them to the object file, because their values
289 * are needed for fix-ups.
290 *
291 * Unless we saw a -L flag, remove all symbols that begin with 'L'
292 * from the symbol chain. (They are still pointed to by the fixes.)
293 *
294 * Count the remaining symbols.
295 * Assign a symbol number to each symbol.
296 * Count the number of string-table chars we will emit.
297 * Put this info into the headers as appropriate.
298 *
299 */
300 know(zero_address_frag.fr_address == 0);
301 string_byte_count = sizeof(string_byte_count);
302
303 obj_crawl_symbol_chain(&headers);
304
305 if (string_byte_count == sizeof(string_byte_count)) {
306 string_byte_count = 0;
307 } /* if no strings, then no count. */
308
309 H_SET_STRING_SIZE(&headers, string_byte_count);
310
311 /*
312 * Addresses of frags now reflect addresses we use in the object file.
313 * Symbol values are correct.
314 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
315 * Also converting any machine-dependent frags using md_convert_frag();
316 */
317 subseg_change(SEG_TEXT, 0);
318
319 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) {
320 switch (fragP->fr_type) {
321 case rs_align:
322 case rs_org:
323 fragP->fr_type = rs_fill;
324 know( fragP->fr_var == 1 );
325 know( fragP->fr_next );
326 fragP->fr_offset
327 = fragP->fr_next->fr_address
328 - fragP->fr_address
329 - fragP->fr_fix;
330 break;
331
332 case rs_fill:
333 break;
334
335 case rs_machine_dependent:
336 md_convert_frag (fragP);
337 /*
338 * After md_convert_frag, we make the frag into a ".space 0".
339 * Md_convert_frag() should set up any fixSs and constants
340 * required.
341 */
342 frag_wane (fragP);
343 break;
344
345#ifndef WORKING_DOT_WORD
346 case rs_broken_word: {
347 struct broken_word *lie;
348 extern md_short_jump_size;
349 extern md_long_jump_size;
350
351 if (fragP->fr_subtype) {
352 fragP->fr_fix+=md_short_jump_size;
353 for (lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
354 if (lie->added==1)
355 fragP->fr_fix+=md_long_jump_size;
356 }
357 frag_wane(fragP);
358 }
359 break;
360#endif
361
362 default:
363 BAD_CASE( fragP->fr_type );
364 break;
365 } /* switch (fr_type) */
366 } /* for each frag. */
367
368#ifndef WORKING_DOT_WORD
369 {
370 struct broken_word *lie;
371 struct broken_word **prevP;
372
373 prevP= &broken_words;
374 for (lie=broken_words; lie; lie=lie->next_broken_word)
375 if (!lie->added) {
376#ifdef TC_NS32K
377 fix_new_ns32k(lie->frag,
378 lie->word_goes_here - lie->frag->fr_literal,
379 2,
380 lie->add,
381 lie->sub,
382 lie->addnum,
383 0, 0, 2, 0, 0);
384#else /* TC_NS32K */
385 fix_new( lie->frag, lie->word_goes_here - lie->frag->fr_literal,
386 2, lie->add,
387 lie->sub, lie->addnum,
388 0, NO_RELOC);
389#endif /* TC_NS32K */
390 /* md_number_to_chars(lie->word_goes_here,
391 S_GET_VALUE(lie->add)
392 + lie->addnum
393 - S_GET_VALUE(lie->sub),
394 2); */
395 *prevP=lie->next_broken_word;
396 } else
397 prevP= &(lie->next_broken_word);
398
399 for (lie=broken_words;lie;) {
400 struct broken_word *untruth;
401 char *table_ptr;
402 long table_addr;
403 long from_addr,
404 to_addr;
405 int n,
406 m;
407
408 extern md_short_jump_size;
409 extern md_long_jump_size;
410
411 fragP=lie->dispfrag;
412
413 /* Find out how many broken_words go here */
414 n=0;
415 for (untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
416 if (untruth->added==1)
417 n++;
418
419 table_ptr=lie->dispfrag->fr_opcode;
420 table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
421 /* Create the jump around the long jumps */
422 /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
423 from_addr=table_addr;
424 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
425 md_create_short_jump(table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
426 table_ptr+=md_short_jump_size;
427 table_addr+=md_short_jump_size;
428
429 for (m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
430 if (lie->added==2)
431 continue;
432 /* Patch the jump table */
433 /* This is the offset from ??? to table_ptr+0 */
434 to_addr = table_addr
435 - S_GET_VALUE(lie->sub);
436 md_number_to_chars(lie->word_goes_here,to_addr,2);
437 for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
438 if (untruth->use_jump==lie)
439 md_number_to_chars(untruth->word_goes_here,to_addr,2);
440 }
441
442 /* Install the long jump */
443 /* this is a long jump from table_ptr+0 to the final target */
444 from_addr=table_addr;
445 to_addr=S_GET_VALUE(lie->add) + lie->addnum;
446 md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
447 table_ptr+=md_long_jump_size;
448 table_addr+=md_long_jump_size;
449 }
450 }
451 }
452#endif /* not WORKING_DOT_WORD */
453
454#ifndef VMS
455 { /* not vms */
456 /*
457 * Scan every FixS performing fixups. We had to wait until now to do
458 * this because md_convert_frag() may have made some fixSs.
459 */
460 H_SET_RELOCATION_SIZE(&headers,
461 md_reloc_size * fixup_segment(text_fix_root, SEG_TEXT),
462 md_reloc_size * fixup_segment(data_fix_root, SEG_DATA));
463
464 /* FIXME move this stuff into the pre-write-hook */
465 H_SET_MAGIC_NUMBER(&headers, magic_number_for_object_file);
466 H_SET_ENTRY_POINT(&headers,0);
467
468#ifdef EXEC_MACHINE_TYPE
469 H_SET_MACHINE_TYPE(&headers,EXEC_MACHINE_TYPE);
470#endif
471#ifdef EXEC_VERSION
472 H_SET_VERSION(&headers,EXEC_VERSION);
473#endif
474
475 obj_pre_write_hook(&headers); /* extra coff stuff */
476
477 if ((had_warnings() && flagseen['Z'])
478 || had_errors() > 0) {
479 if (flagseen['Z']) {
480 as_warn("%d error%s, %d warning%s, generating bad object file.\n",
481 had_errors(), had_errors() == 1 ? "" : "s",
482 had_warnings(), had_warnings() == 1 ? "" : "s");
483 } else {
484 as_fatal("%d error%s, %d warning%s, no object file generated.\n",
485 had_errors(), had_errors() == 1 ? "" : "s",
486 had_warnings(), had_warnings() == 1 ? "" : "s");
487 } /* on want output */
488 } /* on error condition */
489
490 object_file_size = H_GET_FILE_SIZE(&headers);
491 next_object_file_charP = the_object_file = xmalloc(object_file_size);
492
493 output_file_create(out_file_name);
494
495 obj_header_append(&next_object_file_charP, &headers);
496
497 /*
498 * Emit code.
499 */
500 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) {
501 register long count;
502 register char * fill_literal;
503 register long fill_size;
504
505 know( fragP->fr_type == rs_fill );
506 append (& next_object_file_charP, fragP->fr_literal, (unsigned long)fragP->fr_fix);
507 fill_literal= fragP->fr_literal + fragP->fr_fix;
508 fill_size = fragP->fr_var;
509 know( fragP->fr_offset >= 0 );
510 for (count = fragP->fr_offset; count; count --)
511 append (& next_object_file_charP, fill_literal, (unsigned long)fill_size);
512 } /* for each code frag. */
513
514 /*
515 * Emit relocations.
516 */
517 obj_emit_relocations(&next_object_file_charP, text_fix_root, (relax_addressT)0);
518
519#ifdef TC_I960
520 /* Make addresses in data relocation directives relative to beginning of
521 * first data fragment, not end of last text fragment: alignment of the
522 * start of the data segment may place a gap between the segments.
523 */
524 obj_emit_relocations(&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
525#else /* TC_I960 */
526 obj_emit_relocations(&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
527#endif /* TC_I960 */
528
529 /*
530 * Emit line number entries.
531 */
532 OBJ_EMIT_LINENO(&next_object_file_charP, lineno_rootP, the_object_file);
533
534 /*
535 * Emit symbols.
536 */
537 obj_emit_symbols(&next_object_file_charP, symbol_rootP);
538
539 /*
540 * Emit strings.
541 */
542
543 if (string_byte_count > 0) {
544 obj_emit_strings(&next_object_file_charP);
545 } /* only if we have a string table */
546
547 know(next_object_file_charP == the_object_file + object_file_size);
548 /* Write the data to the file */
549 output_file_append(the_object_file,object_file_size,out_file_name);
550
551#ifdef DONTDEF
552 if (flagseen['G']) /* GDB symbol file to be appended? */
553 {
554 gdb_emit (out_file_name);
555 gdb_end ();
556 }
557#endif /* DONTDEF */
558
559 output_file_close(out_file_name);
560 } /* non vms output */
561#else /* VMS */
562 /*
563 * Now do the VMS-dependent part of writing the object file
564 */
565 VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
566#endif /* VMS */
567} /* write_object_file() */
568
569/*
570 * relax_segment()
571 *
572 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
573 * values.
574 *
575 * Relax the frags.
576 *
577 * After this, all frags in this segment have addresses that are correct
578 * within the segment. Since segments live in different file addresses,
579 * these frag addresses may not be the same as final object-file addresses.
580 */
581#ifndef VMS
582static
583#endif /* not VMS */
584void relax_segment(segment_frag_root, segment_type)
585 struct frag * segment_frag_root;
586 segT segment_type; /* SEG_DATA or SEG_TEXT */
587{
588 register struct frag * fragP;
589 register relax_addressT address;
590 /* register relax_addressT old_address; JF unused */
591 /* register relax_addressT new_address; JF unused */
592
593 know( segment_type == SEG_DATA || segment_type == SEG_TEXT );
594
595 /* In case md_estimate_size_before_relax() wants to make fixSs. */
596 subseg_change(segment_type, 0);
597
598 /*
599 * For each frag in segment: count and store (a 1st guess of) fr_address.
600 */
601 address = 0;
602 for ( fragP = segment_frag_root; fragP; fragP = fragP->fr_next )
603 {
604 fragP->fr_address = address;
605 address += fragP->fr_fix;
606 switch (fragP->fr_type)
607 {
608 case rs_fill:
609 address += fragP->fr_offset * fragP->fr_var;
610 break;
611
612 case rs_align:
613 address += relax_align(address, fragP->fr_offset);
614 break;
615
616 case rs_org:
617 /*
618 * Assume .org is nugatory. It will grow with 1st relax.
619 */
620 break;
621
622 case rs_machine_dependent:
623 address += md_estimate_size_before_relax(fragP, segment_type);
624 break;
625
626#ifndef WORKING_DOT_WORD
627 /* Broken words don't concern us yet */
628 case rs_broken_word:
629 break;
630#endif
631
632 default:
633 BAD_CASE( fragP->fr_type );
634 break;
635 } /* switch(fr_type) */
636 } /* for each frag in the segment */
637\f
638 /*
639 * Do relax().
640 */
641 {
642 register long stretch; /* May be any size, 0 or negative. */
643 /* Cumulative number of addresses we have */
644 /* relaxed this pass. */
645 /* We may have relaxed more than one address. */
646 register long stretched; /* Have we stretched on this pass? */
647 /* This is 'cuz stretch may be zero, when,
648 in fact some piece of code grew, and
649 another shrank. If a branch instruction
650 doesn't fit anymore, we could be scrod */
651
652 do
653 {
654 stretch = stretched = 0;
655 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
656 {
657 register long growth = 0;
658 register unsigned long was_address;
659 /* register long var; */
660 register long offset;
661 register symbolS *symbolP;
662 register long target;
663 register long after;
664 register long aim;
665
666 was_address = fragP->fr_address;
667 address = fragP->fr_address += stretch;
668 symbolP = fragP->fr_symbol;
669 offset = fragP->fr_offset;
670 /* var = fragP->fr_var; */
671 switch (fragP->fr_type)
672 {
673 case rs_fill: /* .fill never relaxes. */
674 growth = 0;
675 break;
676
677#ifndef WORKING_DOT_WORD
678 /* JF: This is RMS's idea. I do *NOT* want to be blamed
679 for it I do not want to write it. I do not want to have
680 anything to do with it. This is not the proper way to
681 implement this misfeature. */
682 case rs_broken_word:
683 {
684 struct broken_word *lie;
685 struct broken_word *untruth;
686 extern int md_short_jump_size;
687 extern int md_long_jump_size;
688
689 /* Yes this is ugly (storing the broken_word pointer
690 in the symbol slot). Still, this whole chunk of
691 code is ugly, and I don't feel like doing anything
692 about it. Think of it as stubbornness in action */
693 growth=0;
694 for (lie=(struct broken_word *)(fragP->fr_symbol);
695 lie && lie->dispfrag==fragP;
696 lie=lie->next_broken_word) {
697
698 if (lie->added)
699 continue;
700 offset= lie->add->sy_frag->fr_address+ S_GET_VALUE(lie->add) + lie->addnum -
701 (lie->sub->sy_frag->fr_address+ S_GET_VALUE(lie->sub));
702 if (offset<=-32768 || offset>=32767) {
703 if (flagseen['k'])
704 as_warn(".word %s-%s+%ld didn't fit",
705 S_GET_NAME(lie->add),
706 S_GET_NAME(lie->sub),
707 lie->addnum);
708 lie->added=1;
709 if (fragP->fr_subtype==0) {
710 fragP->fr_subtype++;
711 growth+=md_short_jump_size;
712 }
713 for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
714 if ((untruth->add->sy_frag == lie->add->sy_frag)
715 && S_GET_VALUE(untruth->add) == S_GET_VALUE(lie->add)) {
716 untruth->added=2;
717 untruth->use_jump=lie;
718 }
719 growth+=md_long_jump_size;
720 }
721 }
722 }
723 break;
724#endif
725 case rs_align:
726 growth = relax_align ((relax_addressT)(address + fragP->fr_fix), offset)
727 - relax_align ((relax_addressT)(was_address + fragP->fr_fix), offset);
728 break;
729
730 case rs_org:
731 target = offset;
732 if (symbolP)
733 {
734 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) ||
735 (S_GET_SEGMENT(symbolP) == SEG_DATA) ||
736 (S_GET_SEGMENT(symbolP) == SEG_TEXT));
737 know(symbolP->sy_frag);
738 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) ||
739 symbolP->sy_frag==&zero_address_frag );
740 target +=
741 S_GET_VALUE(symbolP)
742 + symbolP->sy_frag->fr_address;
743 }
744 know( fragP->fr_next );
745 after = fragP->fr_next->fr_address;
746 growth = ((target - after ) > 0) ? (target - after) : 0;
747 /* Growth may be -ve, but variable part */
748 /* of frag cannot have < 0 chars. */
749 /* That is, we can't .org backwards. */
750
751 growth -= stretch; /* This is an absolute growth factor */
752 break;
753
754 case rs_machine_dependent:
755 {
756 register const relax_typeS * this_type;
757 register const relax_typeS * start_type;
758 register relax_substateT next_state;
759 register relax_substateT this_state;
760
761 start_type = this_type
762 = md_relax_table + (this_state = fragP->fr_subtype);
763 target = offset;
764 if (symbolP)
765 {
766 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) ||
767 (S_GET_SEGMENT(symbolP) == SEG_DATA) ||
768 (S_GET_SEGMENT(symbolP) == SEG_TEXT));
769 know(symbolP->sy_frag);
770 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) ||
771 symbolP->sy_frag==&zero_address_frag );
772 target +=
773 S_GET_VALUE(symbolP)
774 + symbolP->sy_frag->fr_address;
775
776 /* If frag has yet to be reached on this pass,
777 assume it will move by STRETCH just as we did.
778 If this is not so, it will be because some frag
779 between grows, and that will force another pass. */
780
781 /* JF was just address */
782 /* JF also added is_dnrange hack */
783 /* There's gotta be a better/faster/etc way
784 to do this. . . */
785 /* gnu@cygnus.com: I changed this from > to >=
786 because I ran into a zero-length frag (fr_fix=0)
787 which was created when the obstack needed a new
788 chunk JUST AFTER the opcode of a branch. Since
789 fr_fix is zero, fr_address of this frag is the same
790 as fr_address of the next frag. This
791 zero-length frag was variable and jumped to .+2
792 (in the next frag), but since the > comparison
793 below failed (the two were =, not >), "stretch"
794 was not added to the target. Stretch was 178, so
795 the offset appeared to be .-176 instead, which did
796 not fit into a byte branch, so the assembler
797 relaxed the branch to a word. This didn't compare
798 with what happened when the same source file was
799 assembled on other machines, which is how I found it.
800 You might want to think about what other places have
801 trouble with zero length frags... */
802
803 if (symbolP->sy_frag->fr_address >= was_address && is_dnrange(fragP,symbolP->sy_frag))
804 target += stretch;
805
806 }
807 aim = target - address - fragP->fr_fix;
808 /* The displacement is affected by the instruction size
809 * for the 32k architecture. I think we ought to be able
810 * to add fragP->fr_pcrel_adjust in all cases (it should be
811 * zero if not used), but just in case it breaks something
812 * else we'll put this inside #ifdef NS32K ... #endif
813 */
814#ifdef TC_NS32K
815 aim += fragP->fr_pcrel_adjust;
816#endif /* TC_NS32K */
817
818 if (aim < 0)
819 {
820 /* Look backwards. */
821 for (next_state = this_type->rlx_more; next_state; )
822 {
823 if (aim >= this_type->rlx_backward)
824 next_state = 0;
825 else
826 { /* Grow to next state. */
827 this_type = md_relax_table + (this_state = next_state);
828 next_state = this_type->rlx_more;
829 }
830 }
831 }
832 else
833 {
834#ifdef DONTDEF
835/* JF these next few lines of code are for the mc68020 which can't handle short
836 offsets of zero in branch instructions. What a kludge! */
837 if (aim==0 && this_state==(1<<2+0)) { /* FOO hard encoded from m.c */
838 aim=this_type->rlx_forward+1; /* Force relaxation into word mode */
839 }
840#endif
841/* JF end of 68020 code */
842 /* Look forwards. */
843 for (next_state = this_type->rlx_more; next_state; )
844 {
845 if (aim <= this_type->rlx_forward)
846 next_state = 0;
847 else
848 { /* Grow to next state. */
849 this_type = md_relax_table + (this_state = next_state);
850 next_state = this_type->rlx_more;
851 }
852 }
853 }
854 if ((growth = this_type->rlx_length - start_type->rlx_length) != 0)
855 fragP->fr_subtype = this_state;
856 }
857 break;
858
859 default:
860 BAD_CASE( fragP->fr_type );
861 break;
862 }
863 if (growth) {
864 stretch += growth;
865 stretched++;
866 }
867 } /* For each frag in the segment. */
868 } while (stretched); /* Until nothing further to relax. */
869 }
870
871 /*
872 * We now have valid fr_address'es for each frag.
873 */
874
875 /*
876 * All fr_address's are correct, relative to their own segment.
877 * We have made all the fixS we will ever make.
878 */
879} /* relax_segment() */
880\f
881/*
882 * Relax_align. Advance location counter to next address that has 'alignment'
883 * lowest order bits all 0s.
884 */
885
886 /* How many addresses does the .align take? */
887static relax_addressT relax_align(address, alignment)
888register relax_addressT address; /* Address now. */
889register long alignment; /* Alignment (binary). */
890{
891 relax_addressT mask;
892 relax_addressT new_address;
893
894 mask = ~ ( (~0) << alignment );
895 new_address = (address + mask) & (~ mask);
896 return (new_address - address);
897} /* relax_align() */
898\f
899/* fixup_segment()
900
901 Go through all the fixS's in a segment and see which ones can be
902 handled now. (These consist of fixS where we have since discovered
903 the value of a symbol, or the address of the frag involved.)
904 For each one, call md_apply_fix to put the fix into the frag data.
905
906 Result is a count of how many relocation structs will be needed to
907 handle the remaining fixS's that we couldn't completely handle here.
908 These will be output later by emit_relocations(). */
909
910static long fixup_segment(fixP, this_segment_type)
911register fixS * fixP;
912segT this_segment_type; /* N_TYPE bits for segment. */
913{
914 register long seg_reloc_count;
915 register symbolS *add_symbolP;
916 register symbolS *sub_symbolP;
917 register long add_number;
918 register int size;
919 register char *place;
920 register long where;
921 register char pcrel;
922 register fragS *fragP;
923 register segT add_symbol_segment = SEG_ABSOLUTE;
924 fixS *topP = fixP;
925
926
927 seg_reloc_count = 0;
928
929 for ( ; fixP; fixP = fixP->fx_next) {
930 fragP = fixP->fx_frag;
931 know(fragP);
932 where = fixP->fx_where;
933 place = fragP->fr_literal + where;
934 size = fixP->fx_size;
935 add_symbolP = fixP->fx_addsy;
936#ifdef TC_I960
937 if (fixP->fx_callj && TC_S_IS_CALLNAME(add_symbolP)) {
938 /* Relocation should be done via the
939 associated 'bal' entry point
940 symbol. */
941
942 if (!TC_S_IS_BALNAME(tc_get_bal_of_call(add_symbolP))) {
943 as_bad("No 'bal' entry point for leafproc %s",
944 S_GET_NAME(add_symbolP));
945 continue;
946 }
947 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call(add_symbolP);
948 } /* callj relocation */
949#endif
950 sub_symbolP = fixP->fx_subsy;
951 add_number = fixP->fx_offset;
952 pcrel = fixP->fx_pcrel;
953
954 if (add_symbolP) {
955 add_symbol_segment = S_GET_SEGMENT(add_symbolP);
956 } /* if there is an addend */
957
958 if (sub_symbolP) {
959 if (!add_symbolP) {
960 /* Its just -sym */
961 if (S_GET_SEGMENT(sub_symbolP) != SEG_ABSOLUTE) {
962 as_bad("Negative of non-absolute symbol %s", S_GET_NAME(sub_symbolP));
963 } /* not absolute */
964
965 add_number -= S_GET_VALUE(sub_symbolP);
966
967 /* if sub_symbol is in the same segment that add_symbol
968 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
969 } else if ((S_GET_SEGMENT(sub_symbolP) == add_symbol_segment)
970 && ((add_symbol_segment == SEG_DATA)
971 || (add_symbol_segment == SEG_TEXT)
972 || (add_symbol_segment == SEG_BSS)
973 || (add_symbol_segment == SEG_ABSOLUTE))) {
974 /* Difference of 2 symbols from same segment. */
975 /* Can't make difference of 2 undefineds: 'value' means */
976 /* something different for N_UNDF. */
977#ifdef TC_I960
978 /* Makes no sense to use the difference of 2 arbitrary symbols
979 * as the target of a call instruction.
980 */
981 if (fixP->fx_callj) {
982 as_bad("callj to difference of 2 symbols");
983 }
984#endif /* TC_I960 */
985 add_number += S_GET_VALUE(add_symbolP) -
986 S_GET_VALUE(sub_symbolP);
987
988 add_symbolP = NULL;
989 fixP->fx_addsy = NULL;
990 } else {
991 /* Different segments in subtraction. */
992 know(!(S_IS_EXTERNAL(sub_symbolP) && (S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)));
993
994 if ((S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)) {
995 add_number -= S_GET_VALUE(sub_symbolP);
996 } else {
997 as_bad("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
998 segment_name(S_GET_SEGMENT(sub_symbolP)),
999 S_GET_NAME(sub_symbolP), fragP->fr_address + where);
1000 } /* if absolute */
1001 }
1002 } /* if sub_symbolP */
1003
1004 if (add_symbolP) {
1005 if (add_symbol_segment == this_segment_type && pcrel) {
1006 /*
1007 * This fixup was made when the symbol's segment was
1008 * SEG_UNKNOWN, but it is now in the local segment.
1009 * So we know how to do the address without relocation.
1010 */
1011#ifdef TC_I960
1012 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1013 * in which cases it modifies *fixP as appropriate. In the case
1014 * of a 'calls', no further work is required, and *fixP has been
1015 * set up to make the rest of the code below a no-op.
1016 */
1017 reloc_callj(fixP);
1018#endif /* TC_I960 */
1019
1020 add_number += S_GET_VALUE(add_symbolP);
1021 add_number -= md_pcrel_from (fixP);
1022 pcrel = 0; /* Lie. Don't want further pcrel processing. */
1023 fixP->fx_addsy = NULL; /* No relocations please. */
1024 } else {
1025 switch (add_symbol_segment) {
1026 case SEG_ABSOLUTE:
1027#ifdef TC_I960
1028 reloc_callj(fixP); /* See comment about reloc_callj() above*/
1029#endif /* TC_I960 */
1030 add_number += S_GET_VALUE(add_symbolP);
1031 fixP->fx_addsy = NULL;
1032 add_symbolP = NULL;
1033 break;
1034
1035 case SEG_BSS:
1036 case SEG_DATA:
1037 case SEG_TEXT:
1038 seg_reloc_count ++;
1039 add_number += S_GET_VALUE(add_symbolP);
1040 break;
1041
1042 case SEG_UNKNOWN:
1043#ifdef TC_I960
1044 if ((int)fixP->fx_bit_fixP == 13) {
1045 /* This is a COBR instruction. They have only a
1046 * 13-bit displacement and are only to be used
1047 * for local branches: flag as error, don't generate
1048 * relocation.
1049 */
1050 as_bad("can't use COBR format with external label");
1051 fixP->fx_addsy = NULL; /* No relocations please. */
1052 continue;
1053 } /* COBR */
1054#endif /* TC_I960 */
1055 /* FIXME-SOON: I think this is trash, but I'm not sure. xoxorich. */
1056#ifdef comment
1057#ifdef OBJ_COFF
1058 if (S_IS_COMMON(add_symbolP))
1059 add_number += S_GET_VALUE(add_symbolP);
1060#endif /* OBJ_COFF */
1061#endif /* comment */
1062
1063 ++seg_reloc_count;
1064 break;
1065
1066 default:
1067 BAD_CASE(add_symbol_segment);
1068 break;
1069 } /* switch on symbol seg */
1070 } /* if not in local seg */
1071 } /* if there was a + symbol */
1072
1073 if (pcrel) {
1074 add_number -= md_pcrel_from(fixP);
1075 if (add_symbolP == 0) {
1076 fixP->fx_addsy = & abs_symbol;
1077 ++seg_reloc_count;
1078 } /* if there's an add_symbol */
1079 } /* if pcrel */
1080
1081 if (!fixP->fx_bit_fixP) {
1082 if ((size==1 &&
1083 (add_number& ~0xFF) && (add_number&~0xFF!=(-1&~0xFF))) ||
1084 (size==2 &&
1085 (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF)))) {
1086 as_bad("Value of %d too large for field of %d bytes at 0x%x",
1087 add_number, size, fragP->fr_address + where);
1088 } /* generic error checking */
1089 } /* not a bit fix */
1090
1091 md_apply_fix(fixP, add_number);
1092 } /* For each fixS in this segment. */
1093
1094#ifdef OBJ_COFF
1095#ifdef TC_I960
1096 /* two relocs per callj under coff. */
1097 for (fixP = topP; fixP; fixP = fixP->fx_next) {
1098 if (fixP->fx_callj && fixP->fx_addsy != 0) {
1099 ++seg_reloc_count;
1100 } /* if callj and not already fixed. */
1101 } /* for each fix */
1102#endif /* TC_I960 */
1103#endif /* OBJ_COFF */
1104 return(seg_reloc_count);
1105} /* fixup_segment() */
1106
1107
1108static int is_dnrange(f1,f2)
1109struct frag *f1;
1110struct frag *f2;
1111{
1112 while (f1) {
1113 if (f1->fr_next==f2)
1114 return 1;
1115 f1=f1->fr_next;
1116 }
1117 return 0;
1118} /* is_dnrange() */
1119
1120/* Append a string onto another string, bumping the pointer along. */
1121void
1122append (charPP, fromP, length)
1123char **charPP;
1124char *fromP;
1125unsigned long length;
1126{
1127 if (length) { /* Don't trust bcopy() of 0 chars. */
1128 bcopy(fromP, *charPP, (int) length);
1129 *charPP += length;
1130 }
1131}
1132
1133int section_alignment[SEG_MAXIMUM_ORDINAL];
1134
1135/*
1136 * This routine records the largest alignment seen for each segment.
1137 * If the beginning of the segment is aligned on the worst-case
1138 * boundary, all of the other alignments within it will work. At
1139 * least one object format really uses this info.
1140 */
1141void record_alignment(seg, align)
1142segT seg; /* Segment to which alignment pertains */
1143int align; /* Alignment, as a power of 2
1144 * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1145 */
1146{
1147
1148 if ( align > section_alignment[(int) seg] ){
1149 section_alignment[(int) seg] = align;
1150 } /* if highest yet */
1151
1152 return;
1153} /* record_alignment() */
1154
1155/*
1156 * Local Variables:
1157 * comment-column: 0
1158 * fill-column: 131
1159 * End:
1160 */
1161
1162/* end of write.c */
This page took 0.063268 seconds and 4 git commands to generate.