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