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