Commit | Line | Data |
---|---|---|
fecd2382 | 1 | /* write.c - emit .o file |
98c6bbbe | 2 | Copyright (C) 1986, 87, 90, 91, 92, 93, 1994 Free Software Foundation, Inc. |
6efd877d | 3 | |
a39116f1 | 4 | This file is part of GAS, the GNU Assembler. |
6efd877d | 5 | |
a39116f1 RP |
6 | GAS is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
6efd877d | 10 | |
a39116f1 RP |
11 | GAS is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
6efd877d | 15 | |
a39116f1 RP |
16 | You should have received a copy of the GNU General Public License |
17 | along with GAS; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
fecd2382 | 19 | |
c593cf41 | 20 | /* This thing should be set up to do byteordering correctly. But... */ |
fecd2382 RP |
21 | |
22 | #include "as.h" | |
fecd2382 RP |
23 | #include "subsegs.h" |
24 | #include "obstack.h" | |
25 | #include "output-file.h" | |
26 | ||
d9d6f094 KR |
27 | /* This looks like a good idea. Let's try turning it on always, for now. */ |
28 | #undef BFD_FAST_SECTION_FILL | |
29 | #define BFD_FAST_SECTION_FILL | |
30 | ||
43ca9aa6 KR |
31 | /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop |
32 | instruction so that the disassembler does not choke on it. */ | |
6d5460ab RP |
33 | #ifndef NOP_OPCODE |
34 | #define NOP_OPCODE 0x00 | |
35 | #endif | |
36 | ||
f5c32424 KR |
37 | #ifndef TC_ADJUST_RELOC_COUNT |
38 | #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT) | |
39 | #endif | |
40 | ||
335d35c8 JL |
41 | #ifndef TC_FORCE_RELOCATION |
42 | #define TC_FORCE_RELOCATION(FIXP) 0 | |
43 | #endif | |
44 | ||
43ca9aa6 KR |
45 | #ifndef WORKING_DOT_WORD |
46 | extern CONST int md_short_jump_size; | |
47 | extern CONST int md_long_jump_size; | |
48 | #endif | |
49 | ||
1cf7548e KR |
50 | int symbol_table_frozen; |
51 | ||
1b96bdce ILT |
52 | #ifdef BFD_ASSEMBLER |
53 | /* We generally attach relocs to frag chains. However, after we have | |
54 | chained these all together into a segment, any relocs we add after | |
55 | that must be attached to a segment. This will include relocs added | |
56 | in md_estimate_size_for_relax, for example. */ | |
57 | static int frags_chained = 0; | |
58 | #endif | |
59 | ||
43ca9aa6 KR |
60 | #ifndef BFD_ASSEMBLER |
61 | ||
45432836 | 62 | #ifndef MANY_SEGMENTS |
09952cd9 KR |
63 | struct frag *text_frag_root; |
64 | struct frag *data_frag_root; | |
65 | struct frag *bss_frag_root; | |
fecd2382 | 66 | |
09952cd9 KR |
67 | struct frag *text_last_frag; /* Last frag in segment. */ |
68 | struct frag *data_last_frag; /* Last frag in segment. */ | |
65bfcf2e | 69 | static struct frag *bss_last_frag; /* Last frag in segment. */ |
45432836 | 70 | #endif |
fecd2382 | 71 | |
1cf7548e | 72 | #ifndef BFD |
fecd2382 | 73 | static object_headers headers; |
fecd2382 | 74 | static char *the_object_file; |
80aab579 ILT |
75 | #endif |
76 | ||
77 | long string_byte_count; | |
fecd2382 RP |
78 | char *next_object_file_charP; /* Tracks object file bytes. */ |
79 | ||
3eb802b5 | 80 | #ifndef OBJ_VMS |
fecd2382 | 81 | int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE; |
3eb802b5 | 82 | #endif |
fecd2382 | 83 | |
43ca9aa6 KR |
84 | #endif /* BFD_ASSEMBLER */ |
85 | ||
7a0405b9 | 86 | #ifdef BFD_ASSEMBLER |
b23f6743 | 87 | static fixS *fix_new_internal PARAMS ((fragS *, int where, int size, |
5ac34ac3 ILT |
88 | symbolS *add, symbolS *sub, |
89 | offsetT offset, int pcrel, | |
7a0405b9 | 90 | bfd_reloc_code_real_type r_type)); |
5ac34ac3 | 91 | #else |
b23f6743 | 92 | static fixS *fix_new_internal PARAMS ((fragS *, int where, int size, |
7a0405b9 ILT |
93 | symbolS *add, symbolS *sub, |
94 | offsetT offset, int pcrel, | |
95 | int r_type)); | |
5ac34ac3 | 96 | #endif |
80aab579 | 97 | #if defined (BFD_ASSEMBLER) || !defined (BFD) |
6efd877d | 98 | static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type)); |
80aab579 | 99 | #endif |
d5364748 | 100 | static relax_addressT relax_align PARAMS ((relax_addressT addr, int align)); |
fecd2382 RP |
101 | |
102 | /* | |
103 | * fix_new() | |
104 | * | |
105 | * Create a fixS in obstack 'notes'. | |
106 | */ | |
5ac34ac3 ILT |
107 | static fixS * |
108 | fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel, | |
109 | r_type) | |
6efd877d KR |
110 | fragS *frag; /* Which frag? */ |
111 | int where; /* Where in that frag? */ | |
b23f6743 | 112 | int size; /* 1, 2, or 4 usually. */ |
6efd877d | 113 | symbolS *add_symbol; /* X_add_symbol. */ |
5ac34ac3 | 114 | symbolS *sub_symbol; /* X_op_symbol. */ |
d5364748 | 115 | offsetT offset; /* X_add_number. */ |
6efd877d | 116 | int pcrel; /* TRUE if PC-relative relocation. */ |
43ca9aa6 KR |
117 | #ifdef BFD_ASSEMBLER |
118 | bfd_reloc_code_real_type r_type; /* Relocation type */ | |
119 | #else | |
6efd877d | 120 | int r_type; /* Relocation type */ |
43ca9aa6 | 121 | #endif |
fecd2382 | 122 | { |
6efd877d KR |
123 | fixS *fixP; |
124 | ||
125 | fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS)); | |
126 | ||
127 | fixP->fx_frag = frag; | |
128 | fixP->fx_where = where; | |
129 | fixP->fx_size = size; | |
130 | fixP->fx_addsy = add_symbol; | |
131 | fixP->fx_subsy = sub_symbol; | |
132 | fixP->fx_offset = offset; | |
133 | fixP->fx_pcrel = pcrel; | |
43ca9aa6 | 134 | #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER) |
6efd877d | 135 | fixP->fx_r_type = r_type; |
c593cf41 | 136 | #endif |
6efd877d KR |
137 | fixP->fx_im_disp = 0; |
138 | fixP->fx_pcrel_adjust = 0; | |
6efd877d | 139 | fixP->fx_bit_fixP = 0; |
43ca9aa6 | 140 | fixP->fx_addnumber = 0; |
20b39b6f | 141 | fixP->tc_fix_data = NULL; |
a58374d7 | 142 | fixP->fx_tcbit = 0; |
98c6bbbe | 143 | fixP->fx_done = 0; |
43ca9aa6 KR |
144 | |
145 | #ifdef TC_something | |
146 | fixP->fx_bsr = 0; | |
147 | #endif | |
43ca9aa6 | 148 | |
84fa9814 KR |
149 | as_where (&fixP->fx_file, &fixP->fx_line); |
150 | ||
43ca9aa6 KR |
151 | /* Usually, we want relocs sorted numerically, but while |
152 | comparing to older versions of gas that have relocs | |
153 | reverse sorted, it is convenient to have this compile | |
154 | time option. xoxorich. */ | |
155 | ||
156 | { | |
6efd877d | 157 | |
43ca9aa6 | 158 | #ifdef BFD_ASSEMBLER |
1b96bdce ILT |
159 | fixS **seg_fix_rootP = (frags_chained |
160 | ? &seg_info (now_seg)->fix_root | |
161 | : &frchain_now->fix_root); | |
162 | fixS **seg_fix_tailP = (frags_chained | |
163 | ? &seg_info (now_seg)->fix_tail | |
164 | : &frchain_now->fix_tail); | |
43ca9aa6 | 165 | #endif |
09952cd9 | 166 | |
f6e504fe | 167 | #ifdef REVERSE_SORT_RELOCS |
6efd877d | 168 | |
43ca9aa6 KR |
169 | fixP->fx_next = *seg_fix_rootP; |
170 | *seg_fix_rootP = fixP; | |
6efd877d | 171 | |
f6e504fe | 172 | #else /* REVERSE_SORT_RELOCS */ |
6efd877d | 173 | |
43ca9aa6 | 174 | fixP->fx_next = NULL; |
6efd877d | 175 | |
43ca9aa6 KR |
176 | if (*seg_fix_tailP) |
177 | (*seg_fix_tailP)->fx_next = fixP; | |
178 | else | |
179 | *seg_fix_rootP = fixP; | |
180 | *seg_fix_tailP = fixP; | |
6efd877d | 181 | |
f6e504fe | 182 | #endif /* REVERSE_SORT_RELOCS */ |
6efd877d | 183 | |
43ca9aa6 KR |
184 | } |
185 | ||
186 | return fixP; | |
187 | } | |
188 | ||
5ac34ac3 ILT |
189 | /* Create a fixup relative to a symbol (plus a constant). */ |
190 | ||
191 | fixS * | |
192 | fix_new (frag, where, size, add_symbol, offset, pcrel, r_type) | |
193 | fragS *frag; /* Which frag? */ | |
194 | int where; /* Where in that frag? */ | |
195 | short int size; /* 1, 2, or 4 usually. */ | |
196 | symbolS *add_symbol; /* X_add_symbol. */ | |
197 | offsetT offset; /* X_add_number. */ | |
198 | int pcrel; /* TRUE if PC-relative relocation. */ | |
199 | #ifdef BFD_ASSEMBLER | |
200 | bfd_reloc_code_real_type r_type; /* Relocation type */ | |
201 | #else | |
202 | int r_type; /* Relocation type */ | |
203 | #endif | |
204 | { | |
205 | return fix_new_internal (frag, where, size, add_symbol, | |
206 | (symbolS *) NULL, offset, pcrel, r_type); | |
207 | } | |
208 | ||
209 | /* Create a fixup for an expression. Currently we only support fixups | |
210 | for difference expressions. That is itself more than most object | |
211 | file formats support anyhow. */ | |
212 | ||
213 | fixS * | |
214 | fix_new_exp (frag, where, size, exp, pcrel, r_type) | |
215 | fragS *frag; /* Which frag? */ | |
216 | int where; /* Where in that frag? */ | |
217 | short int size; /* 1, 2, or 4 usually. */ | |
218 | expressionS *exp; /* Expression. */ | |
219 | int pcrel; /* TRUE if PC-relative relocation. */ | |
220 | #ifdef BFD_ASSEMBLER | |
221 | bfd_reloc_code_real_type r_type; /* Relocation type */ | |
222 | #else | |
223 | int r_type; /* Relocation type */ | |
224 | #endif | |
225 | { | |
226 | symbolS *add = NULL; | |
227 | symbolS *sub = NULL; | |
228 | offsetT off = 0; | |
229 | ||
230 | switch (exp->X_op) | |
231 | { | |
232 | case O_absent: | |
233 | break; | |
234 | ||
b23f6743 KR |
235 | case O_uminus: |
236 | sub = exp->X_add_symbol; | |
237 | off = exp->X_add_number; | |
238 | break; | |
239 | ||
5ac34ac3 ILT |
240 | case O_subtract: |
241 | sub = exp->X_op_symbol; | |
242 | /* Fall through. */ | |
243 | case O_symbol: | |
244 | add = exp->X_add_symbol; | |
245 | /* Fall through. */ | |
246 | case O_constant: | |
247 | off = exp->X_add_number; | |
248 | break; | |
249 | ||
250 | default: | |
251 | as_bad ("expression too complex for fixup"); | |
252 | } | |
253 | ||
254 | return fix_new_internal (frag, where, size, add, sub, off, | |
255 | pcrel, r_type); | |
256 | } | |
257 | ||
43ca9aa6 KR |
258 | /* Append a string onto another string, bumping the pointer along. */ |
259 | void | |
260 | append (charPP, fromP, length) | |
261 | char **charPP; | |
262 | char *fromP; | |
263 | unsigned long length; | |
264 | { | |
265 | /* Don't trust memcpy() of 0 chars. */ | |
266 | if (length == 0) | |
267 | return; | |
268 | ||
80aab579 | 269 | memcpy (*charPP, fromP, length); |
43ca9aa6 KR |
270 | *charPP += length; |
271 | } | |
272 | ||
273 | #ifndef BFD_ASSEMBLER | |
274 | int section_alignment[SEG_MAXIMUM_ORDINAL]; | |
275 | #endif | |
276 | ||
277 | /* | |
278 | * This routine records the largest alignment seen for each segment. | |
279 | * If the beginning of the segment is aligned on the worst-case | |
280 | * boundary, all of the other alignments within it will work. At | |
281 | * least one object format really uses this info. | |
282 | */ | |
283 | void | |
284 | record_alignment (seg, align) | |
285 | /* Segment to which alignment pertains */ | |
286 | segT seg; | |
287 | /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte | |
288 | boundary, etc.) */ | |
289 | int align; | |
290 | { | |
291 | #ifdef BFD_ASSEMBLER | |
292 | if (align > bfd_get_section_alignment (stdoutput, seg)) | |
293 | bfd_set_section_alignment (stdoutput, seg, align); | |
294 | #else | |
295 | if (align > section_alignment[(int) seg]) | |
296 | section_alignment[(int) seg] = align; | |
297 | #endif | |
298 | } | |
299 | ||
300 | #if defined (BFD_ASSEMBLER) || ! defined (BFD) | |
301 | ||
302 | static fragS * | |
303 | chain_frchains_together_1 (section, frchp) | |
304 | segT section; | |
305 | struct frchain *frchp; | |
306 | { | |
307 | fragS dummy, *prev_frag = &dummy; | |
262b22cd ILT |
308 | fixS fix_dummy, *prev_fix = &fix_dummy; |
309 | ||
43ca9aa6 KR |
310 | for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next) |
311 | { | |
312 | prev_frag->fr_next = frchp->frch_root; | |
313 | prev_frag = frchp->frch_last; | |
3a0e38ee | 314 | #ifdef BFD_ASSEMBLER |
262b22cd ILT |
315 | if (frchp->fix_root != (fixS *) NULL) |
316 | { | |
317 | if (seg_info (section)->fix_root == (fixS *) NULL) | |
318 | seg_info (section)->fix_root = frchp->fix_root; | |
319 | prev_fix->fx_next = frchp->fix_root; | |
1b96bdce | 320 | seg_info (section)->fix_tail = frchp->fix_tail; |
262b22cd ILT |
321 | prev_fix = frchp->fix_tail; |
322 | } | |
3a0e38ee | 323 | #endif |
43ca9aa6 KR |
324 | } |
325 | prev_frag->fr_next = 0; | |
326 | return prev_frag; | |
327 | } | |
328 | ||
329 | #endif | |
330 | ||
331 | #ifdef BFD_ASSEMBLER | |
332 | ||
333 | static void | |
334 | chain_frchains_together (abfd, section, xxx) | |
335 | bfd *abfd; /* unused */ | |
336 | segT section; | |
a58374d7 | 337 | PTR xxx; /* unused */ |
43ca9aa6 | 338 | { |
f2f7d044 ILT |
339 | segment_info_type *info; |
340 | ||
341 | /* BFD may have introduced its own sections without using | |
342 | subseg_new, so it is possible that seg_info is NULL. */ | |
343 | info = seg_info (section); | |
344 | if (info != (segment_info_type *) NULL) | |
0f894895 JL |
345 | info->frchainP->frch_last |
346 | = chain_frchains_together_1 (section, info->frchainP); | |
1b96bdce ILT |
347 | |
348 | /* Now that we've chained the frags together, we must add new fixups | |
349 | to the segment, not to the frag chain. */ | |
350 | frags_chained = 1; | |
43ca9aa6 KR |
351 | } |
352 | ||
353 | #endif | |
542e1629 | 354 | |
d5364748 | 355 | #if !defined (BFD) && !defined (BFD_ASSEMBLER) |
65bfcf2e | 356 | |
6efd877d KR |
357 | void |
358 | remove_subsegs (head, seg, root, last) | |
359 | frchainS *head; | |
360 | int seg; | |
361 | fragS **root; | |
362 | fragS **last; | |
65bfcf2e | 363 | { |
65bfcf2e | 364 | *root = head->frch_root; |
43ca9aa6 KR |
365 | *last = chain_frchains_together_1 (seg, head); |
366 | } | |
367 | ||
368 | #endif /* BFD */ | |
369 | ||
80aab579 ILT |
370 | #if defined (BFD_ASSEMBLER) || !defined (BFD) |
371 | ||
43ca9aa6 | 372 | #ifdef BFD_ASSEMBLER |
58d4951d ILT |
373 | static void |
374 | cvt_frag_to_fill (sec, fragP) | |
375 | segT sec; | |
43ca9aa6 | 376 | fragS *fragP; |
43ca9aa6 | 377 | #else |
58d4951d ILT |
378 | static void |
379 | cvt_frag_to_fill (headers, fragP) | |
380 | object_headers *headers; | |
381 | fragS *fragP; | |
43ca9aa6 | 382 | #endif |
58d4951d | 383 | { |
43ca9aa6 | 384 | switch (fragP->fr_type) |
6efd877d | 385 | { |
43ca9aa6 KR |
386 | case rs_align: |
387 | case rs_org: | |
388 | #ifdef HANDLE_ALIGN | |
389 | HANDLE_ALIGN (fragP); | |
390 | #endif | |
391 | fragP->fr_type = rs_fill; | |
43ca9aa6 | 392 | know (fragP->fr_next != NULL); |
43ca9aa6 KR |
393 | fragP->fr_offset = (fragP->fr_next->fr_address |
394 | - fragP->fr_address | |
98c6bbbe | 395 | - fragP->fr_fix) / fragP->fr_var; |
43ca9aa6 | 396 | break; |
65bfcf2e | 397 | |
43ca9aa6 KR |
398 | case rs_fill: |
399 | break; | |
400 | ||
401 | case rs_machine_dependent: | |
402 | #ifdef BFD_ASSEMBLER | |
403 | md_convert_frag (stdoutput, sec, fragP); | |
404 | #else | |
405 | md_convert_frag (headers, fragP); | |
406 | #endif | |
407 | ||
d5364748 | 408 | assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix)); |
43ca9aa6 KR |
409 | |
410 | /* | |
411 | * After md_convert_frag, we make the frag into a ".space 0". | |
412 | * Md_convert_frag() should set up any fixSs and constants | |
413 | * required. | |
414 | */ | |
415 | frag_wane (fragP); | |
416 | break; | |
417 | ||
418 | #ifndef WORKING_DOT_WORD | |
419 | case rs_broken_word: | |
420 | { | |
421 | struct broken_word *lie; | |
422 | ||
423 | if (fragP->fr_subtype) | |
424 | { | |
425 | fragP->fr_fix += md_short_jump_size; | |
426 | for (lie = (struct broken_word *) (fragP->fr_symbol); | |
427 | lie && lie->dispfrag == fragP; | |
428 | lie = lie->next_broken_word) | |
429 | if (lie->added == 1) | |
430 | fragP->fr_fix += md_long_jump_size; | |
431 | } | |
432 | frag_wane (fragP); | |
433 | } | |
434 | break; | |
435 | #endif | |
436 | ||
437 | default: | |
438 | BAD_CASE (fragP->fr_type); | |
439 | break; | |
6efd877d | 440 | } |
65bfcf2e | 441 | } |
6efd877d | 442 | |
80aab579 ILT |
443 | #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */ |
444 | ||
43ca9aa6 KR |
445 | #ifdef BFD_ASSEMBLER |
446 | static void | |
447 | relax_and_size_seg (abfd, sec, xxx) | |
448 | bfd *abfd; | |
449 | asection *sec; | |
a58374d7 | 450 | PTR xxx; |
43ca9aa6 KR |
451 | { |
452 | flagword flags; | |
13e9182d KR |
453 | fragS *fragp; |
454 | segment_info_type *seginfo; | |
455 | int x; | |
456 | valueT size, newsize; | |
43ca9aa6 KR |
457 | |
458 | flags = bfd_get_section_flags (abfd, sec); | |
459 | ||
d9d6f094 | 460 | seginfo = seg_info (sec); |
13e9182d | 461 | if (seginfo && seginfo->frchainP) |
43ca9aa6 | 462 | { |
13e9182d KR |
463 | relax_segment (seginfo->frchainP->frch_root, sec); |
464 | for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next) | |
465 | cvt_frag_to_fill (sec, fragp); | |
466 | for (fragp = seginfo->frchainP->frch_root; | |
467 | fragp->fr_next; | |
468 | fragp = fragp->fr_next) | |
469 | /* walk to last elt */; | |
470 | size = fragp->fr_address + fragp->fr_fix; | |
471 | } | |
472 | else | |
473 | size = 0; | |
a58374d7 ILT |
474 | |
475 | if (size > 0 && ! seginfo->bss) | |
476 | flags |= SEC_HAS_CONTENTS; | |
477 | ||
478 | /* @@ This is just an approximation. */ | |
5f6efd5a | 479 | if (seginfo && seginfo->fix_root) |
a58374d7 ILT |
480 | flags |= SEC_RELOC; |
481 | else | |
482 | flags &= ~SEC_RELOC; | |
483 | x = bfd_set_section_flags (abfd, sec, flags); | |
484 | assert (x == true); | |
485 | ||
20b39b6f JL |
486 | newsize = md_section_align (sec, size); |
487 | x = bfd_set_section_size (abfd, sec, newsize); | |
13e9182d KR |
488 | assert (x == true); |
489 | ||
490 | /* If the size had to be rounded up, add some padding in the last | |
491 | non-empty frag. */ | |
13e9182d KR |
492 | assert (newsize >= size); |
493 | if (size != newsize) | |
494 | { | |
495 | fragS *last = seginfo->frchainP->frch_last; | |
496 | fragp = seginfo->frchainP->frch_root; | |
497 | while (fragp->fr_next != last) | |
498 | fragp = fragp->fr_next; | |
499 | last->fr_address = size; | |
500 | fragp->fr_offset += newsize - size; | |
501 | } | |
502 | ||
43ca9aa6 KR |
503 | #ifdef tc_frob_section |
504 | tc_frob_section (sec); | |
505 | #endif | |
506 | #ifdef obj_frob_section | |
507 | obj_frob_section (sec); | |
508 | #endif | |
509 | } | |
510 | ||
d5364748 KR |
511 | #ifdef DEBUG2 |
512 | static void | |
513 | dump_section_relocs (abfd, sec, stream_) | |
514 | bfd *abfd; | |
515 | asection *sec; | |
516 | char *stream_; | |
517 | { | |
518 | FILE *stream = (FILE *) stream_; | |
519 | segment_info_type *seginfo = seg_info (sec); | |
520 | fixS *fixp = seginfo->fix_root; | |
521 | ||
522 | if (!fixp) | |
523 | return; | |
524 | ||
525 | fprintf (stream, "sec %s relocs:\n", sec->name); | |
526 | while (fixp) | |
527 | { | |
528 | symbolS *s = fixp->fx_addsy; | |
529 | if (s) | |
1cf7548e KR |
530 | { |
531 | fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s), | |
532 | s->bsym->section->name); | |
533 | if (s->bsym->flags & BSF_SECTION_SYM) | |
534 | { | |
535 | fprintf (stream, " section sym"); | |
536 | if (S_GET_VALUE (s)) | |
537 | fprintf (stream, "+%x", S_GET_VALUE (s)); | |
538 | } | |
539 | else | |
540 | fprintf (stream, "+%x", S_GET_VALUE (s)); | |
541 | fprintf (stream, ")+%x\n", fixp->fx_offset); | |
542 | } | |
d5364748 KR |
543 | else |
544 | fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type); | |
545 | fixp = fixp->fx_next; | |
546 | } | |
547 | } | |
548 | #else | |
549 | #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM) | |
550 | #endif | |
551 | ||
98c6bbbe KR |
552 | #ifndef EMIT_SECTION_SYMBOLS |
553 | #define EMIT_SECTION_SYMBOLS 1 | |
554 | #endif | |
555 | ||
d5364748 KR |
556 | static void |
557 | adjust_reloc_syms (abfd, sec, xxx) | |
558 | bfd *abfd; | |
559 | asection *sec; | |
a58374d7 | 560 | PTR xxx; |
d5364748 KR |
561 | { |
562 | segment_info_type *seginfo = seg_info (sec); | |
563 | fixS *fixp; | |
564 | ||
565 | if (seginfo == NULL) | |
566 | return; | |
567 | ||
568 | dump_section_relocs (abfd, sec, stderr); | |
569 | ||
570 | for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) | |
98c6bbbe KR |
571 | if (fixp->fx_done) |
572 | /* ignore it */; | |
573 | else if (fixp->fx_addsy) | |
d5364748 KR |
574 | { |
575 | symbolS *sym = fixp->fx_addsy; | |
576 | asection *symsec = sym->bsym->section; | |
d5364748 | 577 | |
a58374d7 ILT |
578 | /* If it's one of these sections, assume the symbol is |
579 | definitely going to be output. The code in | |
580 | md_estimate_size_before_relax in tc-mips.c uses this test | |
581 | as well, so if you change this code you should look at that | |
582 | code. */ | |
d5364748 KR |
583 | if (symsec == &bfd_und_section |
584 | || symsec == &bfd_abs_section | |
585 | || bfd_is_com_section (symsec)) | |
80aab579 ILT |
586 | { |
587 | fixp->fx_addsy->sy_used_in_reloc = 1; | |
588 | continue; | |
589 | } | |
d5364748 KR |
590 | |
591 | /* Since we're reducing to section symbols, don't attempt to reduce | |
592 | anything that's already using one. */ | |
98c6bbbe | 593 | if (sym->bsym->flags & BSF_SECTION_SYM) |
80aab579 ILT |
594 | { |
595 | fixp->fx_addsy->sy_used_in_reloc = 1; | |
596 | continue; | |
597 | } | |
d5364748 KR |
598 | |
599 | /* Is there some other reason we can't adjust this one? (E.g., | |
600 | call/bal links in i960-bout symbols.) */ | |
601 | #ifdef obj_fix_adjustable | |
602 | if (! obj_fix_adjustable (fixp)) | |
80aab579 ILT |
603 | { |
604 | fixp->fx_addsy->sy_used_in_reloc = 1; | |
605 | continue; | |
606 | } | |
d5364748 | 607 | #endif |
efa0c22e KR |
608 | |
609 | /* Is there some other (target cpu dependent) reason we can't adjust | |
610 | this one? (E.g. relocations involving function addresses on | |
611 | the PA. */ | |
612 | #ifdef tc_fix_adjustable | |
613 | if (! tc_fix_adjustable (fixp)) | |
20b39b6f JL |
614 | { |
615 | fixp->fx_addsy->sy_used_in_reloc = 1; | |
616 | continue; | |
617 | } | |
efa0c22e KR |
618 | #endif |
619 | ||
d5364748 KR |
620 | /* If the section symbol isn't going to be output, the relocs |
621 | at least should still work. If not, figure out what to do | |
622 | when we run into that case. */ | |
623 | fixp->fx_offset += S_GET_VALUE (sym); | |
624 | if (sym->sy_frag) | |
625 | fixp->fx_offset += sym->sy_frag->fr_address; | |
6868afe6 | 626 | fixp->fx_addsy = section_symbol (symsec); |
80aab579 | 627 | fixp->fx_addsy->sy_used_in_reloc = 1; |
d5364748 | 628 | } |
1cf7548e | 629 | #if 1/*def RELOC_REQUIRES_SYMBOL*/ |
e67a0640 KR |
630 | else |
631 | { | |
632 | /* There was no symbol required by this relocation. However, | |
633 | BFD doesn't really handle relocations without symbols well. | |
634 | (At least, the COFF support doesn't.) So for now we fake up | |
635 | a local symbol in the absolute section. */ | |
e8501a72 | 636 | |
1cf7548e KR |
637 | fixp->fx_addsy = section_symbol (absolute_section); |
638 | fixp->fx_addsy->sy_used_in_reloc = 1; | |
e67a0640 KR |
639 | } |
640 | #endif | |
d5364748 KR |
641 | |
642 | dump_section_relocs (abfd, sec, stderr); | |
643 | } | |
644 | ||
43ca9aa6 | 645 | static void |
8d6c34a1 | 646 | write_relocs (abfd, sec, xxx) |
43ca9aa6 KR |
647 | bfd *abfd; |
648 | asection *sec; | |
a58374d7 | 649 | PTR xxx; |
43ca9aa6 KR |
650 | { |
651 | segment_info_type *seginfo = seg_info (sec); | |
80aab579 ILT |
652 | int i; |
653 | unsigned int n; | |
43ca9aa6 KR |
654 | arelent **relocs; |
655 | fixS *fixp; | |
98c6bbbe | 656 | char *err; |
43ca9aa6 | 657 | |
d5364748 KR |
658 | /* If seginfo is NULL, we did not create this section; don't do |
659 | anything with it. */ | |
660 | if (seginfo == NULL) | |
43ca9aa6 KR |
661 | return; |
662 | ||
663 | fixup_segment (seginfo->fix_root, sec); | |
664 | ||
3d3c5039 | 665 | n = 0; |
d5364748 KR |
666 | for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) |
667 | n++; | |
43ca9aa6 | 668 | |
d5364748 | 669 | #ifndef RELOC_EXPANSION_POSSIBLE |
43ca9aa6 | 670 | /* Set up reloc information as well. */ |
43ca9aa6 KR |
671 | relocs = (arelent **) bfd_alloc_by_size_t (stdoutput, |
672 | n * sizeof (arelent *)); | |
8d6c34a1 | 673 | memset ((char*)relocs, 0, n * sizeof (arelent*)); |
43ca9aa6 | 674 | |
3d3c5039 ILT |
675 | i = 0; |
676 | for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next) | |
677 | { | |
678 | arelent *reloc; | |
3d3c5039 ILT |
679 | char *data; |
680 | bfd_reloc_status_type s; | |
681 | ||
98c6bbbe | 682 | if (fixp->fx_done) |
3d3c5039 | 683 | { |
3d3c5039 ILT |
684 | n--; |
685 | continue; | |
686 | } | |
687 | reloc = tc_gen_reloc (sec, fixp); | |
688 | if (!reloc) | |
689 | { | |
690 | n--; | |
691 | continue; | |
692 | } | |
693 | data = fixp->fx_frag->fr_literal + fixp->fx_where; | |
c43d56f7 | 694 | if (fixp->fx_where + fixp->fx_size |
3d3c5039 ILT |
695 | > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset) |
696 | abort (); | |
335d35c8 | 697 | |
84fa9814 KR |
698 | if (reloc->howto->partial_inplace == false |
699 | && reloc->howto->pcrel_offset == true | |
700 | && reloc->howto->pc_relative == true) | |
701 | { | |
702 | /* bfd_perform_relocation screws this up */ | |
703 | reloc->addend += reloc->address; | |
704 | } | |
335d35c8 | 705 | /* Pass bogus address so that when bfd_perform_relocation adds |
f5c32424 KR |
706 | `reloc->address' back in, it'll come up with `data', which is where |
707 | we want it to operate. We can't just do it by fudging reloc->address, | |
708 | since that might be used in the calculations(?). */ | |
3d3c5039 | 709 | s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address, |
98c6bbbe | 710 | sec, stdoutput, &err); |
3d3c5039 ILT |
711 | switch (s) |
712 | { | |
713 | case bfd_reloc_ok: | |
714 | break; | |
df44a852 KR |
715 | case bfd_reloc_overflow: |
716 | as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow"); | |
717 | break; | |
3d3c5039 ILT |
718 | default: |
719 | as_fatal ("bad return from bfd_perform_relocation"); | |
720 | } | |
721 | relocs[i++] = reloc; | |
722 | } | |
d5364748 KR |
723 | #else |
724 | n = n * MAX_RELOC_EXPANSION; | |
725 | /* Set up reloc information as well. */ | |
726 | relocs = (arelent **) bfd_alloc_by_size_t (stdoutput, | |
727 | n * sizeof (arelent *)); | |
728 | ||
729 | i = 0; | |
730 | for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next) | |
731 | { | |
732 | arelent **reloc; | |
d5364748 KR |
733 | char *data; |
734 | bfd_reloc_status_type s; | |
735 | int j; | |
736 | ||
98c6bbbe | 737 | if (fixp->fx_done) |
d5364748 | 738 | { |
d5364748 KR |
739 | n--; |
740 | continue; | |
741 | } | |
742 | reloc = tc_gen_reloc (sec, fixp); | |
743 | ||
744 | for (j = 0; reloc[j]; j++) | |
745 | { | |
746 | relocs[i++] = reloc[j]; | |
747 | assert(i <= n); | |
748 | } | |
749 | data = fixp->fx_frag->fr_literal + fixp->fx_where; | |
c43d56f7 | 750 | if (fixp->fx_where + fixp->fx_size |
d5364748 KR |
751 | > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset) |
752 | abort (); | |
753 | for (j = 0; reloc[j]; j++) | |
754 | { | |
98c6bbbe | 755 | s = bfd_perform_relocation (stdoutput, reloc[j], |
a58374d7 | 756 | data - reloc[0]->address, |
98c6bbbe | 757 | sec, stdoutput, &err); |
d5364748 KR |
758 | switch (s) |
759 | { | |
98c6bbbe KR |
760 | case bfd_reloc_ok: |
761 | break; | |
762 | default: | |
763 | as_fatal ("bad return from bfd_perform_relocation"); | |
d5364748 KR |
764 | } |
765 | } | |
766 | } | |
767 | n = i; | |
768 | #endif | |
769 | ||
1cf7548e KR |
770 | #ifdef DEBUG4 |
771 | { | |
772 | int i, j, nsyms; | |
773 | asymbol **sympp; | |
774 | sympp = bfd_get_outsymbols (stdoutput); | |
775 | nsyms = bfd_get_symcount (stdoutput); | |
776 | for (i = 0; i < n; i++) | |
777 | if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0) | |
778 | { | |
779 | for (j = 0; j < nsyms; j++) | |
780 | if (sympp[j] == *relocs[i]->sym_ptr_ptr) | |
781 | break; | |
782 | if (j == nsyms) | |
783 | abort (); | |
784 | } | |
785 | } | |
786 | #endif | |
787 | ||
3d3c5039 ILT |
788 | if (n) |
789 | bfd_set_reloc (stdoutput, sec, relocs, n); | |
d5364748 KR |
790 | else |
791 | bfd_set_section_flags (abfd, sec, | |
80aab579 ILT |
792 | (bfd_get_section_flags (abfd, sec) |
793 | & (flagword) ~SEC_RELOC)); | |
98c6bbbe KR |
794 | |
795 | #ifdef DEBUG3 | |
d5364748 KR |
796 | { |
797 | int i; | |
798 | arelent *r; | |
799 | asymbol *s; | |
800 | fprintf (stderr, "relocs for sec %s\n", sec->name); | |
801 | for (i = 0; i < n; i++) | |
802 | { | |
803 | r = relocs[i]; | |
804 | s = *r->sym_ptr_ptr; | |
805 | fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n", | |
806 | i, r, r->address, s->name, r->addend); | |
807 | } | |
808 | } | |
809 | #endif | |
8d6c34a1 | 810 | } |
d5364748 | 811 | |
8d6c34a1 KR |
812 | static void |
813 | write_contents (abfd, sec, xxx) | |
814 | bfd *abfd; | |
815 | asection *sec; | |
a58374d7 | 816 | PTR xxx; |
8d6c34a1 KR |
817 | { |
818 | segment_info_type *seginfo = seg_info (sec); | |
819 | unsigned long offset = 0; | |
80aab579 | 820 | fragS *f; |
3d3c5039 ILT |
821 | |
822 | /* Write out the frags. */ | |
f37449aa ILT |
823 | if (seginfo == NULL |
824 | || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)) | |
d5364748 KR |
825 | return; |
826 | ||
80aab579 ILT |
827 | for (f = seginfo->frchainP->frch_root; |
828 | f; | |
829 | f = f->fr_next) | |
43ca9aa6 KR |
830 | { |
831 | int x; | |
832 | unsigned long fill_size; | |
833 | char *fill_literal; | |
834 | long count; | |
835 | ||
80aab579 ILT |
836 | assert (f->fr_type == rs_fill); |
837 | if (f->fr_fix) | |
43ca9aa6 KR |
838 | { |
839 | x = bfd_set_section_contents (stdoutput, sec, | |
80aab579 ILT |
840 | f->fr_literal, (file_ptr) offset, |
841 | (bfd_size_type) f->fr_fix); | |
1cf7548e KR |
842 | if (x == false) |
843 | { | |
844 | bfd_perror (stdoutput->filename); | |
845 | as_perror ("FATAL: Can't write %s", stdoutput->filename); | |
846 | exit (42); | |
847 | } | |
80aab579 | 848 | offset += f->fr_fix; |
43ca9aa6 | 849 | } |
80aab579 ILT |
850 | fill_literal = f->fr_literal + f->fr_fix; |
851 | fill_size = f->fr_var; | |
852 | count = f->fr_offset; | |
43ca9aa6 KR |
853 | assert (count >= 0); |
854 | if (fill_size && count) | |
d9d6f094 KR |
855 | #ifdef BFD_FAST_SECTION_FILL |
856 | { | |
857 | char buf[256]; | |
858 | if (fill_size > sizeof(buf)) { | |
859 | /* Do it the old way. Can this ever happen? */ | |
43ca9aa6 KR |
860 | while (count--) |
861 | { | |
862 | x = bfd_set_section_contents (stdoutput, sec, | |
80aab579 | 863 | fill_literal, (file_ptr) offset, |
d5364748 | 864 | (bfd_size_type) fill_size); |
1cf7548e KR |
865 | if (x == false) |
866 | { | |
867 | bfd_perror (stdoutput->filename); | |
868 | as_perror ("FATAL: Can't write %s", stdoutput->filename); | |
869 | exit (42); | |
870 | } | |
43ca9aa6 KR |
871 | offset += fill_size; |
872 | } | |
d9d6f094 KR |
873 | } |
874 | else { | |
875 | /* Build a buffer full of fill objects and output it as | |
876 | * often as necessary. This saves on the overhead of potentially | |
877 | * lots of bfd_set_section_contents calls. | |
878 | */ | |
879 | int n_per_buf, bytes, i; | |
880 | if (fill_size == 1) | |
881 | { | |
882 | n_per_buf = sizeof (buf); | |
883 | memset (buf, *fill_literal, n_per_buf); | |
884 | } | |
885 | else | |
886 | { | |
887 | char *bufp; | |
888 | n_per_buf = sizeof(buf)/fill_size; | |
889 | for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size) | |
890 | memcpy(bufp, fill_literal, fill_size); | |
891 | } | |
892 | for (; count > 0; count -= n_per_buf) | |
893 | { | |
894 | n_per_buf = n_per_buf > count ? count : n_per_buf; | |
895 | x = bfd_set_section_contents (stdoutput, sec, | |
896 | buf, (file_ptr) offset, | |
897 | (bfd_size_type) n_per_buf * fill_size); | |
898 | assert (x == true); | |
899 | offset += n_per_buf * fill_size; | |
900 | } | |
901 | } | |
902 | } | |
903 | #else | |
904 | while (count--) | |
905 | { | |
906 | x = bfd_set_section_contents (stdoutput, sec, | |
907 | fill_literal, (file_ptr) offset, | |
908 | (bfd_size_type) fill_size); | |
909 | assert (x == true); | |
910 | offset += fill_size; | |
911 | } | |
912 | #endif | |
43ca9aa6 | 913 | } |
43ca9aa6 KR |
914 | } |
915 | #endif | |
916 | ||
80aab579 | 917 | #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT)) |
b23f6743 KR |
918 | static void |
919 | merge_data_into_text () | |
920 | { | |
4064305e | 921 | #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS) |
b23f6743 KR |
922 | seg_info (text_section)->frchainP->frch_last->fr_next = |
923 | seg_info (data_section)->frchainP->frch_root; | |
924 | seg_info (text_section)->frchainP->frch_last = | |
925 | seg_info (data_section)->frchainP->frch_last; | |
926 | seg_info (data_section)->frchainP = 0; | |
927 | #else | |
928 | fixS *tmp; | |
929 | ||
930 | text_last_frag->fr_next = data_frag_root; | |
931 | text_last_frag = data_last_frag; | |
932 | data_last_frag = NULL; | |
933 | data_frag_root = NULL; | |
934 | if (text_fix_root) | |
935 | { | |
936 | for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);; | |
937 | tmp->fx_next = data_fix_root; | |
938 | text_fix_tail = data_fix_tail; | |
939 | } | |
940 | else | |
941 | text_fix_root = data_fix_root; | |
942 | data_fix_root = NULL; | |
943 | #endif | |
944 | } | |
80aab579 | 945 | #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */ |
b23f6743 KR |
946 | |
947 | #if !defined (BFD_ASSEMBLER) && !defined (BFD) | |
948 | static void | |
949 | relax_and_size_all_segments () | |
950 | { | |
13e9182d KR |
951 | fragS *fragP; |
952 | ||
b23f6743 KR |
953 | relax_segment (text_frag_root, SEG_TEXT); |
954 | relax_segment (data_frag_root, SEG_DATA); | |
955 | relax_segment (bss_frag_root, SEG_BSS); | |
956 | /* | |
957 | * Now the addresses of frags are correct within the segment. | |
958 | */ | |
959 | ||
960 | know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0); | |
961 | H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address); | |
962 | text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers); | |
963 | ||
964 | /* | |
965 | * Join the 2 segments into 1 huge segment. | |
966 | * To do this, re-compute every rn_address in the SEG_DATA frags. | |
967 | * Then join the data frags after the text frags. | |
968 | * | |
969 | * Determine a_data [length of data segment]. | |
970 | */ | |
971 | if (data_frag_root) | |
972 | { | |
973 | register relax_addressT slide; | |
974 | ||
975 | know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0)); | |
976 | ||
977 | H_SET_DATA_SIZE (&headers, data_last_frag->fr_address); | |
978 | data_last_frag->fr_address = H_GET_DATA_SIZE (&headers); | |
979 | slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */ | |
980 | #ifdef OBJ_BOUT | |
981 | #define RoundUp(N,S) (((N)+(S)-1)&-(S)) | |
982 | /* For b.out: If the data section has a strict alignment | |
983 | requirement, its load address in the .o file will be | |
984 | rounded up from the size of the text section. These | |
985 | two values are *not* the same! Similarly for the bss | |
986 | section.... */ | |
987 | slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]); | |
988 | #endif | |
989 | ||
990 | for (fragP = data_frag_root; fragP; fragP = fragP->fr_next) | |
991 | { | |
992 | fragP->fr_address += slide; | |
993 | } /* for each data frag */ | |
994 | ||
995 | know (text_last_frag != 0); | |
996 | text_last_frag->fr_next = data_frag_root; | |
997 | } | |
998 | else | |
999 | { | |
1000 | H_SET_DATA_SIZE (&headers, 0); | |
1001 | } | |
1002 | ||
1003 | #ifdef OBJ_BOUT | |
1004 | /* See above comments on b.out data section address. */ | |
1005 | { | |
1006 | long bss_vma; | |
1007 | if (data_last_frag == 0) | |
1008 | bss_vma = H_GET_TEXT_SIZE (&headers); | |
1009 | else | |
1010 | bss_vma = data_last_frag->fr_address; | |
1011 | bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]); | |
1012 | bss_address_frag.fr_address = bss_vma; | |
1013 | } | |
1014 | #else /* ! OBJ_BOUT */ | |
1015 | bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) + | |
1016 | H_GET_DATA_SIZE (&headers)); | |
1017 | ||
efa0c22e | 1018 | #endif /* ! OBJ_BOUT */ |
b23f6743 KR |
1019 | |
1020 | /* Slide all the frags */ | |
1021 | if (bss_frag_root) | |
1022 | { | |
1023 | relax_addressT slide = bss_address_frag.fr_address; | |
1024 | ||
1025 | for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next) | |
1026 | { | |
1027 | fragP->fr_address += slide; | |
1028 | } /* for each bss frag */ | |
1029 | } | |
1030 | ||
b23f6743 KR |
1031 | if (bss_last_frag) |
1032 | H_SET_BSS_SIZE (&headers, | |
1033 | bss_last_frag->fr_address - bss_frag_root->fr_address); | |
1034 | else | |
1035 | H_SET_BSS_SIZE (&headers, 0); | |
1036 | } | |
1037 | #endif /* ! BFD_ASSEMBLER && ! BFD */ | |
1038 | ||
d5364748 KR |
1039 | #if defined (BFD_ASSEMBLER) || !defined (BFD) |
1040 | ||
1b96bdce | 1041 | #ifdef BFD_ASSEMBLER |
1cf7548e KR |
1042 | static void |
1043 | set_symtab () | |
1044 | { | |
1045 | int nsyms; | |
1046 | asymbol **asympp; | |
1047 | symbolS *symp; | |
1048 | boolean result; | |
1049 | extern PTR bfd_alloc PARAMS ((bfd *, size_t)); | |
1050 | ||
1051 | /* Count symbols. We can't rely on a count made by the loop in | |
1052 | write_object_file, because *_frob_file may add a new symbol or | |
1053 | two. */ | |
1054 | nsyms = 0; | |
1055 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) | |
1056 | nsyms++; | |
1057 | ||
1058 | if (nsyms) | |
1059 | { | |
1060 | int i; | |
1061 | ||
1062 | asympp = (asymbol **) bfd_alloc (stdoutput, | |
1063 | nsyms * sizeof (asymbol *)); | |
1064 | symp = symbol_rootP; | |
1065 | for (i = 0; i < nsyms; i++, symp = symbol_next (symp)) | |
1066 | { | |
1067 | asympp[i] = symp->bsym; | |
1068 | symp->written = 1; | |
1069 | } | |
1070 | } | |
1071 | else | |
1072 | asympp = 0; | |
1073 | result = bfd_set_symtab (stdoutput, asympp, nsyms); | |
1074 | assert (result == true); | |
1075 | symbol_table_frozen = 1; | |
1076 | } | |
1b96bdce | 1077 | #endif |
1cf7548e | 1078 | |
6efd877d KR |
1079 | void |
1080 | write_object_file () | |
45432836 | 1081 | { |
741f4d66 | 1082 | struct frchain *frchainP; /* Track along all frchains. */ |
58d4951d | 1083 | #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD) |
741f4d66 | 1084 | fragS *fragP; /* Track along all frags. */ |
58d4951d | 1085 | #endif |
6efd877d | 1086 | |
43ca9aa6 KR |
1087 | /* Do we really want to write it? */ |
1088 | { | |
1089 | int n_warns, n_errs; | |
1090 | n_warns = had_warnings (); | |
1091 | n_errs = had_errors (); | |
1092 | /* The -Z flag indicates that an object file should be generated, | |
1093 | regardless of warnings and errors. */ | |
def66e24 | 1094 | if (flag_always_generate_output) |
43ca9aa6 KR |
1095 | { |
1096 | if (n_warns || n_errs) | |
1097 | as_warn ("%d error%s, %d warning%s, generating bad object file.\n", | |
1098 | n_errs, n_errs == 1 ? "" : "s", | |
1099 | n_warns, n_warns == 1 ? "" : "s"); | |
1100 | } | |
1101 | else | |
1102 | { | |
1103 | if (n_errs) | |
1104 | as_fatal ("%d error%s, %d warning%s, no object file generated.\n", | |
1105 | n_errs, n_errs == 1 ? "" : "s", | |
1106 | n_warns, n_warns == 1 ? "" : "s"); | |
1107 | } | |
1108 | } | |
1109 | ||
3eb802b5 | 1110 | #ifdef OBJ_VMS |
1cf7548e KR |
1111 | /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call |
1112 | a routine to check for the definition of the procedure "_main", | |
1113 | and if so -- fix it up so that it can be program entry point. */ | |
6efd877d | 1114 | VMS_Check_For_Main (); |
fecd2382 | 1115 | #endif /* VMS */ |
43ca9aa6 KR |
1116 | |
1117 | /* After every sub-segment, we fake an ".align ...". This conforms to | |
1118 | BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of | |
1119 | frag that requires least thought. ".align" frags like to have a | |
1120 | following frag since that makes calculating their intended length | |
1121 | trivial. | |
1122 | ||
1123 | @@ Is this really necessary?? */ | |
3eb802b5 | 1124 | #ifndef SUB_SEGMENT_ALIGN |
43ca9aa6 | 1125 | #ifdef BFD_ASSEMBLER |
f2f7d044 | 1126 | #define SUB_SEGMENT_ALIGN(SEG) (0) |
43ca9aa6 | 1127 | #else |
f2f7d044 | 1128 | #define SUB_SEGMENT_ALIGN(SEG) (2) |
43ca9aa6 | 1129 | #endif |
3eb802b5 | 1130 | #endif |
6efd877d KR |
1131 | for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next) |
1132 | { | |
43ca9aa6 | 1133 | subseg_set (frchainP->frch_seg, frchainP->frch_subseg); |
f2f7d044 | 1134 | frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE); |
43ca9aa6 KR |
1135 | /* frag_align will have left a new frag. |
1136 | Use this last frag for an empty ".fill". | |
1137 | ||
1138 | For this segment ... | |
1139 | Create a last frag. Do not leave a "being filled in frag". */ | |
6efd877d KR |
1140 | frag_wane (frag_now); |
1141 | frag_now->fr_fix = 0; | |
1142 | know (frag_now->fr_next == NULL); | |
43ca9aa6 | 1143 | } |
6efd877d | 1144 | |
43ca9aa6 KR |
1145 | /* From now on, we don't care about sub-segments. Build one frag chain |
1146 | for each segment. Linked thru fr_next. */ | |
65bfcf2e | 1147 | |
43ca9aa6 KR |
1148 | #ifdef BFD_ASSEMBLER |
1149 | /* Remove the sections created by gas for its own purposes. */ | |
1150 | { | |
1151 | asection **seclist, *sec; | |
1152 | seclist = &stdoutput->sections; | |
1153 | while (seclist && *seclist) | |
1154 | { | |
1155 | sec = *seclist; | |
5ac34ac3 | 1156 | while (sec == reg_section || sec == expr_section) |
43ca9aa6 KR |
1157 | { |
1158 | sec = sec->next; | |
1159 | *seclist = sec; | |
1160 | stdoutput->section_count--; | |
1161 | if (!sec) | |
1162 | break; | |
1163 | } | |
1164 | if (*seclist) | |
1165 | seclist = &(*seclist)->next; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0); | |
1170 | #else | |
6efd877d KR |
1171 | remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag); |
1172 | remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag); | |
1173 | remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag); | |
43ca9aa6 | 1174 | #endif |
6efd877d | 1175 | |
43ca9aa6 KR |
1176 | /* We have two segments. If user gave -R flag, then we must put the |
1177 | data frags into the text segment. Do this before relaxing so | |
1178 | we know to take advantage of -R and make shorter addresses. */ | |
1179 | #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER) | |
def66e24 | 1180 | if (flag_readonly_data_in_text) |
6efd877d | 1181 | { |
b23f6743 | 1182 | merge_data_into_text (); |
6efd877d KR |
1183 | } |
1184 | #endif | |
43ca9aa6 KR |
1185 | |
1186 | #ifdef BFD_ASSEMBLER | |
1187 | bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0); | |
1188 | #else | |
b23f6743 | 1189 | relax_and_size_all_segments (); |
43ca9aa6 | 1190 | #endif /* BFD_ASSEMBLER */ |
65bfcf2e | 1191 | |
43ca9aa6 | 1192 | #ifndef BFD_ASSEMBLER |
6efd877d | 1193 | /* |
7f2cb270 KR |
1194 | * |
1195 | * Crawl the symbol chain. | |
1196 | * | |
1197 | * For each symbol whose value depends on a frag, take the address of | |
1198 | * that frag and subsume it into the value of the symbol. | |
1199 | * After this, there is just one way to lookup a symbol value. | |
1200 | * Values are left in their final state for object file emission. | |
1201 | * We adjust the values of 'L' local symbols, even if we do | |
1202 | * not intend to emit them to the object file, because their values | |
1203 | * are needed for fix-ups. | |
1204 | * | |
1205 | * Unless we saw a -L flag, remove all symbols that begin with 'L' | |
1206 | * from the symbol chain. (They are still pointed to by the fixes.) | |
1207 | * | |
1208 | * Count the remaining symbols. | |
1209 | * Assign a symbol number to each symbol. | |
1210 | * Count the number of string-table chars we will emit. | |
1211 | * Put this info into the headers as appropriate. | |
1212 | * | |
1213 | */ | |
6efd877d KR |
1214 | know (zero_address_frag.fr_address == 0); |
1215 | string_byte_count = sizeof (string_byte_count); | |
1216 | ||
1217 | obj_crawl_symbol_chain (&headers); | |
1218 | ||
1219 | if (string_byte_count == sizeof (string_byte_count)) | |
43ca9aa6 | 1220 | string_byte_count = 0; |
6efd877d KR |
1221 | |
1222 | H_SET_STRING_SIZE (&headers, string_byte_count); | |
1223 | ||
1224 | /* | |
7f2cb270 KR |
1225 | * Addresses of frags now reflect addresses we use in the object file. |
1226 | * Symbol values are correct. | |
1227 | * Scan the frags, converting any ".org"s and ".align"s to ".fill"s. | |
1228 | * Also converting any machine-dependent frags using md_convert_frag(); | |
1229 | */ | |
6efd877d KR |
1230 | subseg_change (SEG_TEXT, 0); |
1231 | ||
1232 | for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) | |
1233 | { | |
43ca9aa6 | 1234 | cvt_frag_to_fill (&headers, fragP); |
6efd877d | 1235 | |
43ca9aa6 KR |
1236 | /* Some assert macros don't work with # directives mixed in. */ |
1237 | #ifndef NDEBUG | |
1238 | if (!(fragP->fr_next == NULL | |
ebfb4167 | 1239 | #ifdef OBJ_BOUT |
43ca9aa6 | 1240 | || fragP->fr_next == data_frag_root |
ebfb4167 | 1241 | #endif |
6efd877d | 1242 | || ((fragP->fr_next->fr_address - fragP->fr_address) |
43ca9aa6 KR |
1243 | == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var)))) |
1244 | abort (); | |
1245 | #endif | |
1246 | } | |
1247 | #endif /* ! BFD_ASSEMBLER */ | |
6efd877d | 1248 | |
fecd2382 | 1249 | #ifndef WORKING_DOT_WORD |
6efd877d KR |
1250 | { |
1251 | struct broken_word *lie; | |
1252 | struct broken_word **prevP; | |
1253 | ||
1254 | prevP = &broken_words; | |
1255 | for (lie = broken_words; lie; lie = lie->next_broken_word) | |
1256 | if (!lie->added) | |
a39116f1 | 1257 | { |
5ac34ac3 ILT |
1258 | expressionS exp; |
1259 | ||
1260 | exp.X_op = O_subtract; | |
1261 | exp.X_add_symbol = lie->add; | |
1262 | exp.X_op_symbol = lie->sub; | |
1263 | exp.X_add_number = lie->addnum; | |
43ca9aa6 | 1264 | #ifdef BFD_ASSEMBLER |
d9d6f094 KR |
1265 | #ifdef TC_CONS_FIX_NEW |
1266 | TC_CONS_FIX_NEW (lie->frag, | |
1267 | lie->word_goes_here - lie->frag->fr_literal, | |
1268 | 2, &exp); | |
1269 | #else | |
5ac34ac3 ILT |
1270 | fix_new_exp (lie->frag, |
1271 | lie->word_goes_here - lie->frag->fr_literal, | |
1272 | 2, &exp, 0, BFD_RELOC_NONE); | |
d9d6f094 | 1273 | #endif |
43ca9aa6 KR |
1274 | #else |
1275 | #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE) | |
5ac34ac3 ILT |
1276 | fix_new_exp (lie->frag, |
1277 | lie->word_goes_here - lie->frag->fr_literal, | |
1278 | 2, &exp, 0, NO_RELOC); | |
43ca9aa6 | 1279 | #else |
fecd2382 | 1280 | #ifdef TC_NS32K |
5ac34ac3 ILT |
1281 | fix_new_ns32k_exp (lie->frag, |
1282 | lie->word_goes_here - lie->frag->fr_literal, | |
1283 | 2, &exp, 0, 0, 2, 0, 0); | |
343fb08d | 1284 | #else |
5ac34ac3 ILT |
1285 | fix_new_exp (lie->frag, |
1286 | lie->word_goes_here - lie->frag->fr_literal, | |
1287 | 2, &exp, 0, 0); | |
fecd2382 | 1288 | #endif /* TC_NS32K */ |
43ca9aa6 KR |
1289 | #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */ |
1290 | #endif /* BFD_ASSEMBLER */ | |
6efd877d | 1291 | *prevP = lie->next_broken_word; |
a39116f1 | 1292 | } |
6efd877d KR |
1293 | else |
1294 | prevP = &(lie->next_broken_word); | |
1295 | ||
1296 | for (lie = broken_words; lie;) | |
1297 | { | |
1298 | struct broken_word *untruth; | |
1299 | char *table_ptr; | |
d5364748 KR |
1300 | addressT table_addr; |
1301 | addressT from_addr, to_addr; | |
6efd877d KR |
1302 | int n, m; |
1303 | ||
6efd877d KR |
1304 | fragP = lie->dispfrag; |
1305 | ||
43ca9aa6 | 1306 | /* Find out how many broken_words go here. */ |
6efd877d KR |
1307 | n = 0; |
1308 | for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word) | |
1309 | if (untruth->added == 1) | |
1310 | n++; | |
1311 | ||
1312 | table_ptr = lie->dispfrag->fr_opcode; | |
1313 | table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal); | |
43ca9aa6 KR |
1314 | /* Create the jump around the long jumps. This is a short |
1315 | jump from table_ptr+0 to table_ptr+n*long_jump_size. */ | |
6efd877d KR |
1316 | from_addr = table_addr; |
1317 | to_addr = table_addr + md_short_jump_size + n * md_long_jump_size; | |
1318 | md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add); | |
1319 | table_ptr += md_short_jump_size; | |
1320 | table_addr += md_short_jump_size; | |
1321 | ||
1322 | for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word) | |
1323 | { | |
1324 | if (lie->added == 2) | |
1325 | continue; | |
1326 | /* Patch the jump table */ | |
1327 | /* This is the offset from ??? to table_ptr+0 */ | |
d5364748 | 1328 | to_addr = table_addr - S_GET_VALUE (lie->sub); |
d9d6f094 KR |
1329 | #ifdef BFD_ASSEMBLER |
1330 | to_addr -= lie->sub->sy_frag->fr_address; | |
1331 | #endif | |
6efd877d KR |
1332 | md_number_to_chars (lie->word_goes_here, to_addr, 2); |
1333 | for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word) | |
1334 | { | |
1335 | if (untruth->use_jump == lie) | |
1336 | md_number_to_chars (untruth->word_goes_here, to_addr, 2); | |
1337 | } | |
1338 | ||
1339 | /* Install the long jump */ | |
1340 | /* this is a long jump from table_ptr+0 to the final target */ | |
1341 | from_addr = table_addr; | |
1342 | to_addr = S_GET_VALUE (lie->add) + lie->addnum; | |
d9d6f094 KR |
1343 | #ifdef BFD_ASSEMBLER |
1344 | to_addr += lie->add->sy_frag->fr_address; | |
1345 | #endif | |
6efd877d KR |
1346 | md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add); |
1347 | table_ptr += md_long_jump_size; | |
1348 | table_addr += md_long_jump_size; | |
1349 | } | |
1350 | } | |
1351 | } | |
fecd2382 | 1352 | #endif /* not WORKING_DOT_WORD */ |
6efd877d | 1353 | |
43ca9aa6 | 1354 | #ifndef BFD_ASSEMBLER |
3eb802b5 | 1355 | #ifndef OBJ_VMS |
6efd877d | 1356 | { /* not vms */ |
1cf7548e | 1357 | long object_file_size; |
6efd877d | 1358 | /* |
3eb802b5 ILT |
1359 | * Scan every FixS performing fixups. We had to wait until now to do |
1360 | * this because md_convert_frag() may have made some fixSs. | |
1361 | */ | |
6efd877d KR |
1362 | int trsize, drsize; |
1363 | ||
1364 | subseg_change (SEG_TEXT, 0); | |
d5364748 | 1365 | trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT); |
6efd877d | 1366 | subseg_change (SEG_DATA, 0); |
d5364748 | 1367 | drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA); |
6efd877d KR |
1368 | H_SET_RELOCATION_SIZE (&headers, trsize, drsize); |
1369 | ||
1370 | /* FIXME move this stuff into the pre-write-hook */ | |
1371 | H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file); | |
1372 | H_SET_ENTRY_POINT (&headers, 0); | |
1373 | ||
1374 | obj_pre_write_hook (&headers); /* extra coff stuff */ | |
6efd877d KR |
1375 | |
1376 | object_file_size = H_GET_FILE_SIZE (&headers); | |
1377 | next_object_file_charP = the_object_file = xmalloc (object_file_size); | |
1378 | ||
1379 | output_file_create (out_file_name); | |
1380 | ||
1381 | obj_header_append (&next_object_file_charP, &headers); | |
1382 | ||
1383 | know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers)); | |
1384 | ||
1385 | /* | |
43ca9aa6 KR |
1386 | * Emit code. |
1387 | */ | |
6efd877d KR |
1388 | for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) |
1389 | { | |
1390 | register long count; | |
1391 | register char *fill_literal; | |
1392 | register long fill_size; | |
1393 | ||
1394 | know (fragP->fr_type == rs_fill); | |
1395 | append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix); | |
1396 | fill_literal = fragP->fr_literal + fragP->fr_fix; | |
1397 | fill_size = fragP->fr_var; | |
1398 | know (fragP->fr_offset >= 0); | |
1399 | ||
1400 | for (count = fragP->fr_offset; count; count--) | |
1401 | { | |
1402 | append (&next_object_file_charP, fill_literal, (unsigned long) fill_size); | |
1403 | } /* for each */ | |
1404 | ||
1405 | } /* for each code frag. */ | |
1406 | ||
1407 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers))); | |
1408 | ||
1409 | /* | |
43ca9aa6 KR |
1410 | * Emit relocations. |
1411 | */ | |
6efd877d KR |
1412 | obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0); |
1413 | 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 | 1414 | #ifdef TC_I960 |
6efd877d | 1415 | /* Make addresses in data relocation directives relative to beginning of |
43ca9aa6 KR |
1416 | * first data fragment, not end of last text fragment: alignment of the |
1417 | * start of the data segment may place a gap between the segments. | |
1418 | */ | |
6efd877d | 1419 | obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address); |
fecd2382 | 1420 | #else /* TC_I960 */ |
6efd877d | 1421 | obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address); |
fecd2382 | 1422 | #endif /* TC_I960 */ |
6efd877d KR |
1423 | |
1424 | 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))); | |
1425 | ||
1426 | /* | |
43ca9aa6 KR |
1427 | * Emit line number entries. |
1428 | */ | |
6efd877d KR |
1429 | OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file); |
1430 | 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))); | |
1431 | ||
1432 | /* | |
3eb802b5 ILT |
1433 | * Emit symbols. |
1434 | */ | |
6efd877d KR |
1435 | obj_emit_symbols (&next_object_file_charP, symbol_rootP); |
1436 | 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))); | |
1437 | ||
1438 | /* | |
3eb802b5 ILT |
1439 | * Emit strings. |
1440 | */ | |
6efd877d KR |
1441 | |
1442 | if (string_byte_count > 0) | |
1443 | { | |
1444 | obj_emit_strings (&next_object_file_charP); | |
1445 | } /* only if we have a string table */ | |
1446 | ||
45432836 | 1447 | #ifdef BFD_HEADERS |
6efd877d KR |
1448 | bfd_seek (stdoutput, 0, 0); |
1449 | bfd_write (the_object_file, 1, object_file_size, stdoutput); | |
45432836 | 1450 | #else |
6efd877d KR |
1451 | |
1452 | /* Write the data to the file */ | |
1453 | output_file_append (the_object_file, object_file_size, out_file_name); | |
45432836 | 1454 | #endif |
6efd877d KR |
1455 | } /* non vms output */ |
1456 | #else /* VMS */ | |
1457 | /* | |
3eb802b5 ILT |
1458 | * Now do the VMS-dependent part of writing the object file |
1459 | */ | |
85825401 ILT |
1460 | VMS_write_object_file (H_GET_TEXT_SIZE (&headers), |
1461 | H_GET_DATA_SIZE (&headers), | |
1462 | H_GET_BSS_SIZE (&headers), | |
3eb802b5 | 1463 | text_frag_root, data_frag_root); |
6efd877d | 1464 | #endif /* VMS */ |
43ca9aa6 | 1465 | #else /* BFD_ASSEMBLER */ |
6efd877d | 1466 | |
d5364748 KR |
1467 | bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0); |
1468 | ||
43ca9aa6 KR |
1469 | /* Set up symbol table, and write it out. */ |
1470 | if (symbol_rootP) | |
1471 | { | |
43ca9aa6 KR |
1472 | symbolS *symp; |
1473 | ||
1474 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) | |
1475 | { | |
262b22cd ILT |
1476 | int punt = 0; |
1477 | ||
5868b1fe ILT |
1478 | if (! symp->sy_resolved) |
1479 | { | |
5ac34ac3 | 1480 | if (symp->sy_value.X_op == O_constant) |
5868b1fe ILT |
1481 | { |
1482 | /* This is the normal case; skip the call. */ | |
1483 | S_SET_VALUE (symp, | |
1484 | (S_GET_VALUE (symp) | |
1485 | + symp->sy_frag->fr_address)); | |
1486 | symp->sy_resolved = 1; | |
1487 | } | |
1488 | else | |
1489 | resolve_symbol_value (symp); | |
1490 | } | |
1491 | ||
43ca9aa6 KR |
1492 | /* So far, common symbols have been treated like undefined symbols. |
1493 | Put them in the common section now. */ | |
1494 | if (S_IS_DEFINED (symp) == 0 | |
1495 | && S_GET_VALUE (symp) != 0) | |
d9d6f094 | 1496 | S_SET_SEGMENT (symp, bfd_com_section_ptr); |
43ca9aa6 | 1497 | #if 0 |
5868b1fe | 1498 | printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n", |
43ca9aa6 KR |
1499 | S_GET_NAME (symp), symp, |
1500 | S_GET_VALUE (symp), | |
d5364748 | 1501 | symp->bsym->flags, |
43ca9aa6 KR |
1502 | segment_name (symp->bsym->section)); |
1503 | #endif | |
335d35c8 | 1504 | |
80aab579 | 1505 | #ifdef obj_frob_symbol |
262b22cd | 1506 | obj_frob_symbol (symp, punt); |
43ca9aa6 KR |
1507 | #endif |
1508 | #ifdef tc_frob_symbol | |
262b22cd | 1509 | if (! punt || symp->sy_used_in_reloc) |
335d35c8 | 1510 | tc_frob_symbol (symp, punt); |
43ca9aa6 | 1511 | #endif |
80aab579 | 1512 | |
262b22cd ILT |
1513 | /* If we don't want to keep this symbol, splice it out of |
1514 | the chain now. If EMIT_SECTION_SYMBOLS is 0, we never | |
1515 | want section symbols. Otherwise, we skip local symbols | |
1516 | and symbols that the frob_symbol macros told us to punt, | |
1517 | but we keep such symbols if they are used in relocs. */ | |
1518 | if ((! EMIT_SECTION_SYMBOLS | |
1519 | && (symp->bsym->flags & BSF_SECTION_SYM) != 0) | |
1cf7548e KR |
1520 | /* Note that S_IS_EXTERN and S_IS_LOCAL are not always |
1521 | opposites. Sometimes the former checks flags and the | |
1522 | latter examines the name... */ | |
1523 | || (!S_IS_EXTERN (symp) | |
1524 | && (S_IS_LOCAL (symp) || punt) | |
262b22cd | 1525 | && ! symp->sy_used_in_reloc)) |
43ca9aa6 | 1526 | { |
1cf7548e KR |
1527 | symbol_remove (symp, &symbol_rootP, &symbol_lastP); |
1528 | /* After symbol_remove, symbol_next(symp) still returns | |
1529 | the one that came after it in the chain. So we don't | |
1530 | need to do any extra cleanup work here. */ | |
262b22cd | 1531 | |
262b22cd | 1532 | continue; |
43ca9aa6 | 1533 | } |
85051959 | 1534 | |
80aab579 ILT |
1535 | /* Make sure we really got a value for the symbol. */ |
1536 | if (! symp->sy_resolved) | |
1537 | { | |
1538 | as_bad ("can't resolve value for symbol \"%s\"", | |
1539 | S_GET_NAME (symp)); | |
1540 | symp->sy_resolved = 1; | |
1541 | } | |
1542 | ||
85051959 ILT |
1543 | /* Set the value into the BFD symbol. Up til now the value |
1544 | has only been kept in the gas symbolS struct. */ | |
1545 | symp->bsym->value = S_GET_VALUE (symp); | |
43ca9aa6 KR |
1546 | } |
1547 | } | |
1548 | ||
def66e24 DM |
1549 | /* Now do any format-specific adjustments to the symbol table, such |
1550 | as adding file symbols. */ | |
1551 | #ifdef obj_adjust_symtab | |
1552 | obj_adjust_symtab (); | |
1553 | #endif | |
1554 | ||
1555 | /* Now that all the sizes are known, and contents correct, we can | |
1556 | start writing to the file. */ | |
1557 | set_symtab (); | |
1558 | ||
1cf7548e KR |
1559 | /* If *_frob_file changes the symbol value at this point, it is |
1560 | responsible for moving the changed value into symp->bsym->value | |
1561 | as well. Hopefully all symbol value changing can be done in | |
1562 | *_frob_symbol. */ | |
c79e67a3 KR |
1563 | #ifdef tc_frob_file |
1564 | tc_frob_file (); | |
1565 | #endif | |
f2f7d044 ILT |
1566 | #ifdef obj_frob_file |
1567 | obj_frob_file (); | |
1568 | #endif | |
1569 | ||
8d6c34a1 KR |
1570 | bfd_map_over_sections (stdoutput, write_relocs, (char *) 0); |
1571 | ||
43ca9aa6 | 1572 | bfd_map_over_sections (stdoutput, write_contents, (char *) 0); |
43ca9aa6 KR |
1573 | #endif /* BFD_ASSEMBLER */ |
1574 | } | |
d5364748 | 1575 | #endif /* ! BFD */ |
fecd2382 RP |
1576 | |
1577 | /* | |
1578 | * relax_segment() | |
1579 | * | |
1580 | * Now we have a segment, not a crowd of sub-segments, we can make fr_address | |
1581 | * values. | |
1582 | * | |
1583 | * Relax the frags. | |
1584 | * | |
1585 | * After this, all frags in this segment have addresses that are correct | |
1586 | * within the segment. Since segments live in different file addresses, | |
1587 | * these frag addresses may not be the same as final object-file addresses. | |
1588 | */ | |
45432836 | 1589 | |
a58374d7 ILT |
1590 | #ifndef md_relax_frag |
1591 | ||
d5364748 | 1592 | /* Subroutines of relax_segment. */ |
43ca9aa6 KR |
1593 | static int |
1594 | is_dnrange (f1, f2) | |
1595 | struct frag *f1; | |
1596 | struct frag *f2; | |
1597 | { | |
1598 | for (; f1; f1 = f1->fr_next) | |
1599 | if (f1->fr_next == f2) | |
1600 | return 1; | |
1601 | return 0; | |
1602 | } | |
1603 | ||
a58374d7 ILT |
1604 | #endif /* ! defined (md_relax_frag) */ |
1605 | ||
43ca9aa6 | 1606 | /* Relax_align. Advance location counter to next address that has 'alignment' |
d5364748 | 1607 | lowest order bits all 0s, return size of adjustment made. */ |
43ca9aa6 KR |
1608 | static relax_addressT |
1609 | relax_align (address, alignment) | |
1610 | register relax_addressT address; /* Address now. */ | |
d5364748 | 1611 | register int alignment; /* Alignment (binary). */ |
43ca9aa6 KR |
1612 | { |
1613 | relax_addressT mask; | |
1614 | relax_addressT new_address; | |
1615 | ||
1616 | mask = ~((~0) << alignment); | |
1617 | new_address = (address + mask) & (~mask); | |
1618 | if (linkrelax) | |
1619 | /* We must provide lots of padding, so the linker can discard it | |
1620 | when needed. The linker will not add extra space, ever. */ | |
1621 | new_address += (1 << alignment); | |
1622 | return (new_address - address); | |
1623 | } | |
45432836 | 1624 | |
6efd877d KR |
1625 | void |
1626 | relax_segment (segment_frag_root, segment) | |
1627 | struct frag *segment_frag_root; | |
43ca9aa6 | 1628 | segT segment; |
fecd2382 | 1629 | { |
6efd877d KR |
1630 | register struct frag *fragP; |
1631 | register relax_addressT address; | |
43ca9aa6 | 1632 | #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER) |
6efd877d | 1633 | know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS); |
45432836 | 1634 | #endif |
6efd877d KR |
1635 | /* In case md_estimate_size_before_relax() wants to make fixSs. */ |
1636 | subseg_change (segment, 0); | |
1637 | ||
7f2cb270 KR |
1638 | /* For each frag in segment: count and store (a 1st guess of) |
1639 | fr_address. */ | |
6efd877d KR |
1640 | address = 0; |
1641 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) | |
1642 | { | |
1643 | fragP->fr_address = address; | |
1644 | address += fragP->fr_fix; | |
1645 | ||
1646 | switch (fragP->fr_type) | |
1647 | { | |
1648 | case rs_fill: | |
1649 | address += fragP->fr_offset * fragP->fr_var; | |
1650 | break; | |
1651 | ||
1652 | case rs_align: | |
98c6bbbe KR |
1653 | { |
1654 | int offset = relax_align (address, (int) fragP->fr_offset); | |
1655 | if (offset % fragP->fr_var != 0) | |
1656 | { | |
262b22cd ILT |
1657 | as_bad ("alignment padding (%d bytes) not a multiple of %ld", |
1658 | offset, (long) fragP->fr_var); | |
98c6bbbe KR |
1659 | offset -= (offset % fragP->fr_var); |
1660 | } | |
1661 | address += offset; | |
1662 | } | |
6efd877d KR |
1663 | break; |
1664 | ||
1665 | case rs_org: | |
7f2cb270 | 1666 | /* Assume .org is nugatory. It will grow with 1st relax. */ |
6efd877d KR |
1667 | break; |
1668 | ||
1669 | case rs_machine_dependent: | |
1670 | address += md_estimate_size_before_relax (fragP, segment); | |
1671 | break; | |
1672 | ||
fecd2382 | 1673 | #ifndef WORKING_DOT_WORD |
6efd877d KR |
1674 | /* Broken words don't concern us yet */ |
1675 | case rs_broken_word: | |
1676 | break; | |
fecd2382 | 1677 | #endif |
6efd877d KR |
1678 | |
1679 | default: | |
1680 | BAD_CASE (fragP->fr_type); | |
1681 | break; | |
1682 | } /* switch(fr_type) */ | |
1683 | } /* for each frag in the segment */ | |
1684 | ||
7f2cb270 | 1685 | /* Do relax(). */ |
6efd877d | 1686 | { |
7f2cb270 | 1687 | long stretch; /* May be any size, 0 or negative. */ |
6efd877d KR |
1688 | /* Cumulative number of addresses we have */ |
1689 | /* relaxed this pass. */ | |
1690 | /* We may have relaxed more than one address. */ | |
7f2cb270 KR |
1691 | long stretched; /* Have we stretched on this pass? */ |
1692 | /* This is 'cuz stretch may be zero, when, in fact some piece of code | |
1693 | grew, and another shrank. If a branch instruction doesn't fit anymore, | |
1694 | we could be scrod. */ | |
6efd877d KR |
1695 | |
1696 | do | |
1697 | { | |
1698 | stretch = stretched = 0; | |
1699 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) | |
1700 | { | |
7f2cb270 KR |
1701 | long growth = 0; |
1702 | unsigned long was_address; | |
1703 | long offset; | |
1704 | symbolS *symbolP; | |
1705 | long target; | |
1706 | long after; | |
6efd877d KR |
1707 | |
1708 | was_address = fragP->fr_address; | |
1709 | address = fragP->fr_address += stretch; | |
1710 | symbolP = fragP->fr_symbol; | |
1711 | offset = fragP->fr_offset; | |
6efd877d KR |
1712 | |
1713 | switch (fragP->fr_type) | |
1714 | { | |
1715 | case rs_fill: /* .fill never relaxes. */ | |
1716 | growth = 0; | |
1717 | break; | |
1718 | ||
fecd2382 | 1719 | #ifndef WORKING_DOT_WORD |
6efd877d | 1720 | /* JF: This is RMS's idea. I do *NOT* want to be blamed |
7f2cb270 KR |
1721 | for it I do not want to write it. I do not want to have |
1722 | anything to do with it. This is not the proper way to | |
1723 | implement this misfeature. */ | |
6efd877d KR |
1724 | case rs_broken_word: |
1725 | { | |
1726 | struct broken_word *lie; | |
1727 | struct broken_word *untruth; | |
6efd877d KR |
1728 | |
1729 | /* Yes this is ugly (storing the broken_word pointer | |
7f2cb270 KR |
1730 | in the symbol slot). Still, this whole chunk of |
1731 | code is ugly, and I don't feel like doing anything | |
1732 | about it. Think of it as stubbornness in action. */ | |
6efd877d KR |
1733 | growth = 0; |
1734 | for (lie = (struct broken_word *) (fragP->fr_symbol); | |
1735 | lie && lie->dispfrag == fragP; | |
1736 | lie = lie->next_broken_word) | |
1737 | { | |
1738 | ||
1739 | if (lie->added) | |
1740 | continue; | |
1741 | ||
7f2cb270 KR |
1742 | offset = (lie->add->sy_frag->fr_address |
1743 | + S_GET_VALUE (lie->add) | |
1744 | + lie->addnum | |
1745 | - (lie->sub->sy_frag->fr_address | |
1746 | + S_GET_VALUE (lie->sub))); | |
6efd877d KR |
1747 | if (offset <= -32768 || offset >= 32767) |
1748 | { | |
def66e24 | 1749 | if (flag_warn_displacement) |
d5364748 KR |
1750 | { |
1751 | char buf[50]; | |
80aab579 | 1752 | sprint_value (buf, (addressT) lie->addnum); |
d5364748 KR |
1753 | as_warn (".word %s-%s+%s didn't fit", |
1754 | S_GET_NAME (lie->add), | |
1755 | S_GET_NAME (lie->sub), | |
1756 | buf); | |
1757 | } | |
6efd877d KR |
1758 | lie->added = 1; |
1759 | if (fragP->fr_subtype == 0) | |
1760 | { | |
1761 | fragP->fr_subtype++; | |
1762 | growth += md_short_jump_size; | |
1763 | } | |
7f2cb270 KR |
1764 | for (untruth = lie->next_broken_word; |
1765 | untruth && untruth->dispfrag == lie->dispfrag; | |
1766 | untruth = untruth->next_broken_word) | |
6efd877d KR |
1767 | if ((untruth->add->sy_frag == lie->add->sy_frag) |
1768 | && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add)) | |
1769 | { | |
1770 | untruth->added = 2; | |
1771 | untruth->use_jump = lie; | |
1772 | } | |
1773 | growth += md_long_jump_size; | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | break; | |
1778 | } /* case rs_broken_word */ | |
fecd2382 | 1779 | #endif |
6efd877d | 1780 | case rs_align: |
43ca9aa6 KR |
1781 | growth = (relax_align ((relax_addressT) (address |
1782 | + fragP->fr_fix), | |
d5364748 | 1783 | (int) offset) |
43ca9aa6 KR |
1784 | - relax_align ((relax_addressT) (was_address |
1785 | + fragP->fr_fix), | |
d5364748 | 1786 | (int) offset)); |
6efd877d KR |
1787 | break; |
1788 | ||
1789 | case rs_org: | |
1790 | target = offset; | |
1791 | ||
1792 | if (symbolP) | |
1793 | { | |
43ca9aa6 KR |
1794 | #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER) |
1795 | know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) | |
1796 | || (S_GET_SEGMENT (symbolP) == SEG_DATA) | |
1797 | || (S_GET_SEGMENT (symbolP) == SEG_TEXT) | |
1798 | || S_GET_SEGMENT (symbolP) == SEG_BSS); | |
6efd877d | 1799 | know (symbolP->sy_frag); |
43ca9aa6 KR |
1800 | know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) |
1801 | || (symbolP->sy_frag == &zero_address_frag)); | |
45432836 | 1802 | #endif |
6efd877d KR |
1803 | target += S_GET_VALUE (symbolP) |
1804 | + symbolP->sy_frag->fr_address; | |
1805 | } /* if we have a symbol */ | |
1806 | ||
1807 | know (fragP->fr_next); | |
1808 | after = fragP->fr_next->fr_address; | |
1809 | growth = ((target - after) > 0) ? (target - after) : 0; | |
43ca9aa6 KR |
1810 | /* Growth may be negative, but variable part of frag |
1811 | cannot have fewer than 0 chars. That is, we can't | |
1812 | .org backwards. */ | |
6efd877d KR |
1813 | |
1814 | growth -= stretch; /* This is an absolute growth factor */ | |
1815 | break; | |
1816 | ||
1817 | case rs_machine_dependent: | |
a58374d7 ILT |
1818 | #ifdef md_relax_frag |
1819 | growth = md_relax_frag (fragP, stretch); | |
1820 | #else | |
1821 | /* The default way to relax a frag is to look through | |
1822 | md_relax_table. */ | |
6efd877d | 1823 | { |
7f2cb270 KR |
1824 | const relax_typeS *this_type; |
1825 | const relax_typeS *start_type; | |
1826 | relax_substateT next_state; | |
1827 | relax_substateT this_state; | |
80aab579 | 1828 | long aim; |
6efd877d | 1829 | |
7f2cb270 KR |
1830 | this_state = fragP->fr_subtype; |
1831 | start_type = this_type = md_relax_table + this_state; | |
6efd877d KR |
1832 | target = offset; |
1833 | ||
1834 | if (symbolP) | |
1835 | { | |
80aab579 | 1836 | #ifndef DIFF_EXPR_OK |
43ca9aa6 KR |
1837 | #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER) |
1838 | know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) | |
1839 | || (S_GET_SEGMENT (symbolP) == SEG_DATA) | |
1840 | || (S_GET_SEGMENT (symbolP) == SEG_BSS) | |
1841 | || (S_GET_SEGMENT (symbolP) == SEG_TEXT)); | |
45432836 | 1842 | #endif |
6efd877d | 1843 | know (symbolP->sy_frag); |
80aab579 | 1844 | #endif |
43ca9aa6 KR |
1845 | know (!(S_GET_SEGMENT (symbolP) == absolute_section) |
1846 | || symbolP->sy_frag == &zero_address_frag); | |
6efd877d KR |
1847 | target += |
1848 | S_GET_VALUE (symbolP) | |
1849 | + symbolP->sy_frag->fr_address; | |
1850 | ||
1851 | /* If frag has yet to be reached on this pass, | |
7f2cb270 KR |
1852 | assume it will move by STRETCH just as we did. |
1853 | If this is not so, it will be because some frag | |
d5364748 KR |
1854 | between grows, and that will force another pass. |
1855 | ||
1856 | Beware zero-length frags. | |
1857 | ||
1858 | There should be a faster way to do this. */ | |
6efd877d KR |
1859 | |
1860 | if (symbolP->sy_frag->fr_address >= was_address | |
1861 | && is_dnrange (fragP, symbolP->sy_frag)) | |
1862 | { | |
1863 | target += stretch; | |
7f2cb270 | 1864 | } |
d5364748 | 1865 | } |
6efd877d KR |
1866 | |
1867 | aim = target - address - fragP->fr_fix; | |
d9d6f094 KR |
1868 | #ifdef TC_PCREL_ADJUST |
1869 | /* Currently only the ns32k family needs this */ | |
1870 | aim += TC_PCREL_ADJUST(fragP); | |
1871 | #else | |
1872 | /* This machine doesn't want to use pcrel_adjust. | |
1873 | In that case, pcrel_adjust should be zero. */ | |
1874 | assert (fragP->fr_pcrel_adjust == 0); | |
d5364748 | 1875 | #endif |
6efd877d KR |
1876 | |
1877 | if (aim < 0) | |
1878 | { | |
1879 | /* Look backwards. */ | |
1880 | for (next_state = this_type->rlx_more; next_state;) | |
7f2cb270 KR |
1881 | if (aim >= this_type->rlx_backward) |
1882 | next_state = 0; | |
1883 | else | |
1884 | { | |
1885 | /* Grow to next state. */ | |
1886 | this_state = next_state; | |
1887 | this_type = md_relax_table + this_state; | |
1888 | next_state = this_type->rlx_more; | |
1889 | } | |
6efd877d KR |
1890 | } |
1891 | else | |
1892 | { | |
a39116f1 | 1893 | #ifdef M68K_AIM_KLUDGE |
6efd877d | 1894 | M68K_AIM_KLUDGE (aim, this_state, this_type); |
fecd2382 | 1895 | #endif |
6efd877d KR |
1896 | /* Look forwards. */ |
1897 | for (next_state = this_type->rlx_more; next_state;) | |
7f2cb270 KR |
1898 | if (aim <= this_type->rlx_forward) |
1899 | next_state = 0; | |
1900 | else | |
1901 | { | |
1902 | /* Grow to next state. */ | |
1903 | this_state = next_state; | |
1904 | this_type = md_relax_table + this_state; | |
1905 | next_state = this_type->rlx_more; | |
1906 | } | |
6efd877d KR |
1907 | } |
1908 | ||
7f2cb270 KR |
1909 | growth = this_type->rlx_length - start_type->rlx_length; |
1910 | if (growth != 0) | |
6efd877d | 1911 | fragP->fr_subtype = this_state; |
7f2cb270 | 1912 | } |
a58374d7 | 1913 | #endif |
7f2cb270 | 1914 | break; |
6efd877d KR |
1915 | |
1916 | default: | |
1917 | BAD_CASE (fragP->fr_type); | |
1918 | break; | |
1919 | } | |
1920 | if (growth) | |
1921 | { | |
1922 | stretch += growth; | |
1923 | stretched++; | |
1924 | } | |
1925 | } /* For each frag in the segment. */ | |
1926 | } | |
1927 | while (stretched); /* Until nothing further to relax. */ | |
1928 | } /* do_relax */ | |
1929 | ||
1930 | /* | |
7f2cb270 KR |
1931 | * We now have valid fr_address'es for each frag. |
1932 | */ | |
6efd877d KR |
1933 | |
1934 | /* | |
7f2cb270 KR |
1935 | * All fr_address's are correct, relative to their own segment. |
1936 | * We have made all the fixS we will ever make. | |
1937 | */ | |
6efd877d | 1938 | } /* relax_segment() */ |
fecd2382 | 1939 | |
80aab579 ILT |
1940 | #if defined (BFD_ASSEMBLER) || !defined (BFD) |
1941 | ||
d9d6f094 KR |
1942 | #ifndef TC_RELOC_RTSYM_LOC_FIXUP |
1943 | #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1) | |
1944 | #endif | |
1945 | ||
fecd2382 | 1946 | /* fixup_segment() |
6efd877d | 1947 | |
fecd2382 RP |
1948 | Go through all the fixS's in a segment and see which ones can be |
1949 | handled now. (These consist of fixS where we have since discovered | |
1950 | the value of a symbol, or the address of the frag involved.) | |
1951 | For each one, call md_apply_fix to put the fix into the frag data. | |
6efd877d | 1952 | |
fecd2382 RP |
1953 | Result is a count of how many relocation structs will be needed to |
1954 | handle the remaining fixS's that we couldn't completely handle here. | |
1955 | These will be output later by emit_relocations(). */ | |
1956 | ||
09952cd9 | 1957 | static long |
6efd877d | 1958 | fixup_segment (fixP, this_segment_type) |
09952cd9 | 1959 | register fixS *fixP; |
6efd877d | 1960 | segT this_segment_type; /* N_TYPE bits for segment. */ |
fecd2382 | 1961 | { |
f5c32424 KR |
1962 | long seg_reloc_count = 0; |
1963 | symbolS *add_symbolP; | |
1964 | symbolS *sub_symbolP; | |
d5364748 | 1965 | valueT add_number; |
f5c32424 KR |
1966 | int size; |
1967 | char *place; | |
1968 | long where; | |
1969 | char pcrel; | |
1970 | fragS *fragP; | |
1971 | segT add_symbol_segment = absolute_section; | |
1972 | ||
1973 | /* If the linker is doing the relaxing, we must not do any fixups. | |
1974 | ||
1975 | Well, strictly speaking that's not true -- we could do any that are | |
1976 | PC-relative and don't cross regions that could change size. And for the | |
1977 | i960 (the only machine for which we've got a relaxing linker right now), | |
1978 | we might be able to turn callx/callj into bal anyways in cases where we | |
1979 | know the maximum displacement. */ | |
6efd877d | 1980 | if (linkrelax) |
f5c32424 KR |
1981 | { |
1982 | for (; fixP; fixP = fixP->fx_next) | |
1983 | seg_reloc_count++; | |
1984 | TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count); | |
1985 | return seg_reloc_count; | |
1986 | } | |
6efd877d | 1987 | |
f5c32424 KR |
1988 | for (; fixP; fixP = fixP->fx_next) |
1989 | { | |
1990 | fragP = fixP->fx_frag; | |
1991 | know (fragP); | |
1992 | where = fixP->fx_where; | |
1993 | place = fragP->fr_literal + where; | |
1994 | size = fixP->fx_size; | |
1995 | add_symbolP = fixP->fx_addsy; | |
1996 | #ifdef TC_VALIDATE_FIX | |
1997 | TC_VALIDATE_FIX (fixP, this_segment_type, skip); | |
fecd2382 | 1998 | #endif |
f5c32424 KR |
1999 | sub_symbolP = fixP->fx_subsy; |
2000 | add_number = fixP->fx_offset; | |
2001 | pcrel = fixP->fx_pcrel; | |
2002 | ||
2003 | if (add_symbolP) | |
2004 | add_symbol_segment = S_GET_SEGMENT (add_symbolP); | |
2005 | ||
2006 | if (sub_symbolP) | |
2007 | { | |
2008 | if (!add_symbolP) | |
2009 | { | |
2010 | /* Its just -sym */ | |
2011 | if (S_GET_SEGMENT (sub_symbolP) == absolute_section) | |
6efd877d | 2012 | add_number -= S_GET_VALUE (sub_symbolP); |
f5c32424 KR |
2013 | else if (pcrel |
2014 | && S_GET_SEGMENT (sub_symbolP) == this_segment_type) | |
2015 | { | |
2016 | /* Should try converting to a constant. */ | |
2017 | goto bad_sub_reloc; | |
2018 | } | |
2019 | else | |
2020 | bad_sub_reloc: | |
741f4d66 KR |
2021 | as_bad_where (fixP->fx_file, fixP->fx_line, |
2022 | "Negative of non-absolute symbol %s", | |
2023 | S_GET_NAME (sub_symbolP)); | |
f5c32424 KR |
2024 | } |
2025 | else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment) | |
2026 | && (SEG_NORMAL (add_symbol_segment) | |
2027 | || (add_symbol_segment == absolute_section))) | |
2028 | { | |
2029 | /* Difference of 2 symbols from same segment. | |
2030 | Can't make difference of 2 undefineds: 'value' means | |
2031 | something different for N_UNDF. */ | |
fecd2382 | 2032 | #ifdef TC_I960 |
f5c32424 KR |
2033 | /* Makes no sense to use the difference of 2 arbitrary symbols |
2034 | as the target of a call instruction. */ | |
2035 | if (fixP->fx_tcbit) | |
741f4d66 KR |
2036 | as_bad_where (fixP->fx_file, fixP->fx_line, |
2037 | "callj to difference of 2 symbols"); | |
6efd877d | 2038 | #endif /* TC_I960 */ |
f5c32424 KR |
2039 | add_number += S_GET_VALUE (add_symbolP) - |
2040 | S_GET_VALUE (sub_symbolP); | |
6efd877d | 2041 | |
f5c32424 | 2042 | add_symbolP = NULL; |
335d35c8 | 2043 | |
f5c32424 KR |
2044 | /* Let the target machine make the final determination |
2045 | as to whether or not a relocation will be needed to | |
2046 | handle this fixup. */ | |
2047 | if (!TC_FORCE_RELOCATION (fixP)) | |
98c6bbbe KR |
2048 | { |
2049 | fixP->fx_addsy = NULL; | |
2050 | } | |
f5c32424 KR |
2051 | } |
2052 | else | |
2053 | { | |
2054 | /* Different segments in subtraction. */ | |
2055 | know (!(S_IS_EXTERNAL (sub_symbolP) | |
2056 | && (S_GET_SEGMENT (sub_symbolP) == absolute_section))); | |
2057 | ||
2058 | if ((S_GET_SEGMENT (sub_symbolP) == absolute_section)) | |
2059 | add_number -= S_GET_VALUE (sub_symbolP); | |
6efd877d | 2060 | |
80aab579 | 2061 | #ifdef DIFF_EXPR_OK |
f5c32424 KR |
2062 | else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type |
2063 | #if 0 /* Do this even if it's already described as pc-relative. For example, | |
2064 | on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a | |
2065 | pc-relative mode. */ | |
2066 | && pcrel | |
0f894895 | 2067 | #endif |
f5c32424 KR |
2068 | ) |
2069 | { | |
2070 | /* Make it pc-relative. */ | |
2071 | add_number += (md_pcrel_from (fixP) | |
2072 | - S_GET_VALUE (sub_symbolP)); | |
2073 | pcrel = 1; | |
2074 | fixP->fx_pcrel = 1; | |
2075 | sub_symbolP = 0; | |
2076 | fixP->fx_subsy = 0; | |
2077 | } | |
98c6bbbe KR |
2078 | #endif |
2079 | #ifdef BFD_ASSEMBLER | |
2080 | else if (fixP->fx_r_type == BFD_RELOC_GPREL32 | |
2081 | || fixP->fx_r_type == BFD_RELOC_GPREL16) | |
2082 | { | |
2083 | /* Leave it alone. */ | |
2084 | } | |
80aab579 | 2085 | #endif |
f5c32424 KR |
2086 | else |
2087 | { | |
2088 | char buf[50]; | |
2089 | sprint_value (buf, fragP->fr_address + where); | |
741f4d66 KR |
2090 | as_bad_where (fixP->fx_file, fixP->fx_line, |
2091 | "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.", | |
2092 | segment_name (S_GET_SEGMENT (sub_symbolP)), | |
2093 | S_GET_NAME (sub_symbolP), buf); | |
f5c32424 KR |
2094 | } |
2095 | } | |
2096 | } | |
6efd877d | 2097 | |
f5c32424 KR |
2098 | if (add_symbolP) |
2099 | { | |
d9d6f094 KR |
2100 | if (add_symbol_segment == this_segment_type && pcrel |
2101 | && TC_RELOC_RTSYM_LOC_FIXUP (fixP->fx_r_type)) | |
f5c32424 KR |
2102 | { |
2103 | /* | |
2104 | * This fixup was made when the symbol's segment was | |
2105 | * SEG_UNKNOWN, but it is now in the local segment. | |
2106 | * So we know how to do the address without relocation. | |
2107 | */ | |
fecd2382 | 2108 | #ifdef TC_I960 |
f5c32424 KR |
2109 | /* reloc_callj() may replace a 'call' with a 'calls' or a |
2110 | 'bal', in which cases it modifies *fixP as appropriate. | |
2111 | In the case of a 'calls', no further work is required, | |
2112 | and *fixP has been set up to make the rest of the code | |
2113 | below a no-op. */ | |
2114 | reloc_callj (fixP); | |
fecd2382 | 2115 | #endif /* TC_I960 */ |
6efd877d | 2116 | |
f5c32424 KR |
2117 | add_number += S_GET_VALUE (add_symbolP); |
2118 | add_number -= md_pcrel_from (fixP); | |
2119 | pcrel = 0; /* Lie. Don't want further pcrel processing. */ | |
2120 | ||
2121 | /* Let the target machine make the final determination | |
2122 | as to whether or not a relocation will be needed to | |
2123 | handle this fixup. */ | |
2124 | if (!TC_FORCE_RELOCATION (fixP)) | |
98c6bbbe KR |
2125 | { |
2126 | fixP->fx_pcrel = 0; | |
2127 | fixP->fx_addsy = NULL; | |
2128 | } | |
f5c32424 KR |
2129 | } |
2130 | else | |
2131 | { | |
2132 | if (add_symbol_segment == absolute_section) | |
2133 | { | |
fecd2382 | 2134 | #ifdef TC_I960 |
f5c32424 KR |
2135 | /* See comment about reloc_callj() above. */ |
2136 | reloc_callj (fixP); | |
fecd2382 | 2137 | #endif /* TC_I960 */ |
f5c32424 KR |
2138 | add_number += S_GET_VALUE (add_symbolP); |
2139 | ||
2140 | /* Let the target machine make the final determination | |
2141 | as to whether or not a relocation will be needed to | |
2142 | handle this fixup. */ | |
2143 | if (!TC_FORCE_RELOCATION (fixP)) | |
e67a0640 KR |
2144 | { |
2145 | fixP->fx_addsy = NULL; | |
2146 | add_symbolP = NULL; | |
2147 | } | |
f5c32424 KR |
2148 | } |
2149 | else if (add_symbol_segment == undefined_section | |
43ca9aa6 | 2150 | #ifdef BFD_ASSEMBLER |
f5c32424 | 2151 | || bfd_is_com_section (add_symbol_segment) |
43ca9aa6 | 2152 | #endif |
f5c32424 KR |
2153 | ) |
2154 | { | |
fecd2382 | 2155 | #ifdef TC_I960 |
f5c32424 KR |
2156 | if ((int) fixP->fx_bit_fixP == 13) |
2157 | { | |
2158 | /* This is a COBR instruction. They have only a | |
2159 | * 13-bit displacement and are only to be used | |
2160 | * for local branches: flag as error, don't generate | |
2161 | * relocation. | |
2162 | */ | |
741f4d66 KR |
2163 | as_bad_where (fixP->fx_file, fixP->fx_line, |
2164 | "can't use COBR format with external label"); | |
98c6bbbe KR |
2165 | fixP->fx_addsy = NULL; |
2166 | fixP->fx_done = 1; | |
f5c32424 KR |
2167 | continue; |
2168 | } /* COBR */ | |
fecd2382 | 2169 | #endif /* TC_I960 */ |
f5c32424 | 2170 | |
fecd2382 | 2171 | #ifdef OBJ_COFF |
c593cf41 | 2172 | #ifdef TE_I386AIX |
f5c32424 KR |
2173 | if (S_IS_COMMON (add_symbolP)) |
2174 | add_number += S_GET_VALUE (add_symbolP); | |
c593cf41 | 2175 | #endif /* TE_I386AIX */ |
fecd2382 | 2176 | #endif /* OBJ_COFF */ |
f5c32424 KR |
2177 | ++seg_reloc_count; |
2178 | } | |
2179 | else | |
2180 | { | |
2181 | seg_reloc_count++; | |
2182 | add_number += S_GET_VALUE (add_symbolP); | |
2183 | } | |
2184 | } | |
2185 | } | |
6efd877d | 2186 | |
f5c32424 KR |
2187 | if (pcrel) |
2188 | { | |
2189 | add_number -= md_pcrel_from (fixP); | |
2190 | if (add_symbolP == 0) | |
2191 | { | |
1cf7548e | 2192 | #ifndef BFD_ASSEMBLER |
f5c32424 | 2193 | fixP->fx_addsy = &abs_symbol; |
1cf7548e KR |
2194 | #else |
2195 | fixP->fx_addsy = section_symbol (absolute_section); | |
2196 | #endif | |
2197 | fixP->fx_addsy->sy_used_in_reloc = 1; | |
f5c32424 | 2198 | ++seg_reloc_count; |
98c6bbbe KR |
2199 | } |
2200 | } | |
6efd877d | 2201 | |
f5c32424 KR |
2202 | if (!fixP->fx_bit_fixP && size > 0) |
2203 | { | |
2204 | valueT mask = 0; | |
df44a852 | 2205 | if (size < sizeof (mask)) |
f5c32424 | 2206 | { |
df44a852 KR |
2207 | /* set all bits to one */ |
2208 | mask--; | |
2209 | /* Technically, combining these produces an undefined result | |
2210 | if size is sizeof (valueT), though I think these two | |
2211 | half-way operations should both be defined. And the | |
2212 | compiler should be able to combine them if it's valid on | |
2213 | the host architecture. */ | |
2214 | mask <<= size * 4; | |
2215 | mask <<= size * 4; | |
2216 | if ((add_number & mask) != 0 | |
2217 | && (add_number & mask) != mask) | |
2218 | { | |
2219 | char buf[50], buf2[50]; | |
2220 | sprint_value (buf, fragP->fr_address + where); | |
2221 | if (add_number > 1000) | |
2222 | sprint_value (buf2, add_number); | |
2223 | else | |
2224 | sprintf (buf2, "%ld", (long) add_number); | |
2225 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
2226 | "Value of %s too large for field of %d bytes at %s", | |
2227 | buf2, size, buf); | |
2228 | } /* generic error checking */ | |
2229 | } | |
f5c32424 KR |
2230 | #ifdef WARN_SIGNED_OVERFLOW_WORD |
2231 | /* Warn if a .word value is too large when treated as a signed | |
2232 | number. We already know it is not too negative. This is to | |
2233 | catch over-large switches generated by gcc on the 68k. */ | |
def66e24 | 2234 | if (!flag_signed_overflow_ok |
f5c32424 KR |
2235 | && size == 2 |
2236 | && add_number > 0x7fff) | |
2237 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
2238 | "Signed .word overflow; switch may be too large; %ld at 0x%lx", | |
2239 | (long) add_number, | |
2240 | (unsigned long) (fragP->fr_address + where)); | |
6efd877d | 2241 | #endif |
f5c32424 | 2242 | } /* not a bit fix */ |
6efd877d | 2243 | |
98c6bbbe KR |
2244 | if (!fixP->fx_done) |
2245 | { | |
43ca9aa6 | 2246 | #ifdef BFD_ASSEMBLER |
98c6bbbe | 2247 | md_apply_fix (fixP, &add_number); |
43ca9aa6 | 2248 | #else |
98c6bbbe KR |
2249 | md_apply_fix (fixP, add_number); |
2250 | #endif | |
2251 | ||
2252 | #ifndef TC_HANDLES_FX_DONE | |
2253 | /* If the tc-* files haven't been converted, assume it's handling | |
2254 | it the old way, where a null fx_addsy means that the fix has | |
2255 | been applied completely, and no further work is needed. */ | |
2256 | if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0) | |
2257 | fixP->fx_done = 1; | |
43ca9aa6 | 2258 | #endif |
98c6bbbe KR |
2259 | } |
2260 | #ifdef TC_VALIDATE_FIX | |
f5c32424 | 2261 | skip: |
98c6bbbe | 2262 | #endif |
f5c32424 KR |
2263 | ; |
2264 | } /* For each fixS in this segment. */ | |
6efd877d | 2265 | |
f5c32424 KR |
2266 | TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count); |
2267 | return seg_reloc_count; | |
2268 | } | |
6efd877d | 2269 | |
f5c32424 | 2270 | #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */ |
6efd877d | 2271 | |
f5c32424 KR |
2272 | void |
2273 | number_to_chars_bigendian (buf, val, n) | |
2274 | char *buf; | |
2275 | valueT val; | |
2276 | int n; | |
2277 | { | |
2278 | if (n > sizeof (val)|| n <= 0) | |
2279 | abort (); | |
2280 | while (n--) | |
2281 | { | |
2282 | buf[n] = val & 0xff; | |
2283 | val >>= 8; | |
2284 | } | |
d5364748 | 2285 | } |
fecd2382 | 2286 | |
f5c32424 KR |
2287 | void |
2288 | number_to_chars_littleendian (buf, val, n) | |
2289 | char *buf; | |
2290 | valueT val; | |
2291 | int n; | |
2292 | { | |
2293 | if (n > sizeof (val) || n <= 0) | |
2294 | abort (); | |
2295 | while (n--) | |
2296 | { | |
2297 | *buf++ = val & 0xff; | |
2298 | val >>= 8; | |
2299 | } | |
2300 | } | |
80aab579 | 2301 | |
fecd2382 | 2302 | /* end of write.c */ |