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