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