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