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