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