Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* write.c - emit .o file |
f7e42eb4 | 2 | Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
20ee54e8 | 3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
6 | This file is part of GAS, the GNU Assembler. | |
7 | ||
8 | GAS is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GAS is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
20 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
21 | 02110-1301, USA. */ | |
252b5132 | 22 | |
efaf0ba4 | 23 | /* This thing should be set up to do byteordering correctly. But... */ |
252b5132 RH |
24 | |
25 | #include "as.h" | |
26 | #include "subsegs.h" | |
27 | #include "obstack.h" | |
28 | #include "output-file.h" | |
220e750f | 29 | #include "dwarf2dbg.h" |
e7ff5c73 | 30 | #include "libbfd.h" |
252b5132 | 31 | |
252b5132 | 32 | #ifndef TC_ADJUST_RELOC_COUNT |
df44284e | 33 | #define TC_ADJUST_RELOC_COUNT(FIX, COUNT) |
252b5132 RH |
34 | #endif |
35 | ||
36 | #ifndef TC_FORCE_RELOCATION | |
a161fe53 | 37 | #define TC_FORCE_RELOCATION(FIX) \ |
ae6063d4 | 38 | (generic_force_reloc (FIX)) |
252b5132 RH |
39 | #endif |
40 | ||
a161fe53 AM |
41 | #ifndef TC_FORCE_RELOCATION_ABS |
42 | #define TC_FORCE_RELOCATION_ABS(FIX) \ | |
43 | (TC_FORCE_RELOCATION (FIX)) | |
44 | #endif | |
45 | ||
46 | #ifndef TC_FORCE_RELOCATION_LOCAL | |
47 | #define TC_FORCE_RELOCATION_LOCAL(FIX) \ | |
48 | (!(FIX)->fx_pcrel \ | |
a161fe53 AM |
49 | || TC_FORCE_RELOCATION (FIX)) |
50 | #endif | |
51 | ||
52 | #ifndef TC_FORCE_RELOCATION_SUB_SAME | |
53 | #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \ | |
426318c5 | 54 | (! SEG_NORMAL (SEG)) |
a161fe53 AM |
55 | #endif |
56 | ||
57 | #ifndef TC_FORCE_RELOCATION_SUB_ABS | |
4f3cafa2 | 58 | #define TC_FORCE_RELOCATION_SUB_ABS(FIX) 0 |
a161fe53 AM |
59 | #endif |
60 | ||
61 | #ifndef TC_FORCE_RELOCATION_SUB_LOCAL | |
62 | #ifdef DIFF_EXPR_OK | |
4f3cafa2 | 63 | #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 0 |
a161fe53 | 64 | #else |
4f3cafa2 | 65 | #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1 |
a161fe53 AM |
66 | #endif |
67 | #endif | |
68 | ||
69 | #ifndef TC_VALIDATE_FIX_SUB | |
70 | #ifdef UNDEFINED_DIFFERENCE_OK | |
71 | /* The PA needs this for PIC code generation. */ | |
72 | #define TC_VALIDATE_FIX_SUB(FIX) 1 | |
73 | #else | |
a161fe53 AM |
74 | #define TC_VALIDATE_FIX_SUB(FIX) \ |
75 | ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \ | |
76 | || (FIX)->fx_r_type == BFD_RELOC_GPREL16) | |
a161fe53 | 77 | #endif |
252b5132 RH |
78 | #endif |
79 | ||
58a77e41 EC |
80 | #ifndef TC_LINKRELAX_FIXUP |
81 | #define TC_LINKRELAX_FIXUP(SEG) 1 | |
82 | #endif | |
83 | ||
a161fe53 AM |
84 | #ifndef MD_APPLY_SYM_VALUE |
85 | #define MD_APPLY_SYM_VALUE(FIX) 1 | |
8f36cd18 | 86 | #endif |
ef99799a | 87 | |
87548816 NC |
88 | #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG |
89 | #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1 | |
90 | #endif | |
91 | ||
252b5132 | 92 | #ifndef MD_PCREL_FROM_SECTION |
df44284e | 93 | #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX) |
252b5132 RH |
94 | #endif |
95 | ||
c9cd7160 L |
96 | #ifndef TC_FAKE_LABEL |
97 | #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0) | |
98 | #endif | |
99 | ||
6386f3a7 AM |
100 | /* Used to control final evaluation of expressions. */ |
101 | int finalize_syms = 0; | |
e46d99eb | 102 | |
252b5132 | 103 | int symbol_table_frozen; |
a161fe53 AM |
104 | |
105 | symbolS *abs_section_sym; | |
106 | ||
26346241 AM |
107 | /* Remember the value of dot when parsing expressions. */ |
108 | addressT dot_value; | |
109 | ||
d7342424 | 110 | void print_fixup (fixS *); |
252b5132 | 111 | |
252b5132 RH |
112 | /* We generally attach relocs to frag chains. However, after we have |
113 | chained these all together into a segment, any relocs we add after | |
114 | that must be attached to a segment. This will include relocs added | |
115 | in md_estimate_size_for_relax, for example. */ | |
116 | static int frags_chained = 0; | |
252b5132 RH |
117 | |
118 | static int n_fixups; | |
119 | ||
df44284e | 120 | #define RELOC_ENUM enum bfd_reloc_code_real |
df44284e | 121 | |
efaf0ba4 NC |
122 | /* Create a fixS in obstack 'notes'. */ |
123 | ||
252b5132 | 124 | static fixS * |
d7342424 KH |
125 | fix_new_internal (fragS *frag, /* Which frag? */ |
126 | int where, /* Where in that frag? */ | |
127 | int size, /* 1, 2, or 4 usually. */ | |
128 | symbolS *add_symbol, /* X_add_symbol. */ | |
129 | symbolS *sub_symbol, /* X_op_symbol. */ | |
130 | offsetT offset, /* X_add_number. */ | |
131 | int pcrel, /* TRUE if PC-relative relocation. */ | |
132 | RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */) | |
252b5132 RH |
133 | { |
134 | fixS *fixP; | |
135 | ||
136 | n_fixups++; | |
137 | ||
e7ff5c73 | 138 | fixP = obstack_alloc (¬es, sizeof (fixS)); |
252b5132 RH |
139 | |
140 | fixP->fx_frag = frag; | |
141 | fixP->fx_where = where; | |
142 | fixP->fx_size = size; | |
143 | /* We've made fx_size a narrow field; check that it's wide enough. */ | |
144 | if (fixP->fx_size != size) | |
145 | { | |
146 | as_bad (_("field fx_size too small to hold %d"), size); | |
147 | abort (); | |
148 | } | |
149 | fixP->fx_addsy = add_symbol; | |
150 | fixP->fx_subsy = sub_symbol; | |
151 | fixP->fx_offset = offset; | |
26346241 | 152 | fixP->fx_dot_value = dot_value; |
252b5132 | 153 | fixP->fx_pcrel = pcrel; |
252b5132 | 154 | fixP->fx_r_type = r_type; |
252b5132 RH |
155 | fixP->fx_im_disp = 0; |
156 | fixP->fx_pcrel_adjust = 0; | |
157 | fixP->fx_bit_fixP = 0; | |
158 | fixP->fx_addnumber = 0; | |
159 | fixP->fx_tcbit = 0; | |
20ee54e8 | 160 | fixP->fx_tcbit2 = 0; |
252b5132 RH |
161 | fixP->fx_done = 0; |
162 | fixP->fx_no_overflow = 0; | |
163 | fixP->fx_signed = 0; | |
164 | ||
165 | #ifdef USING_CGEN | |
166 | fixP->fx_cgen.insn = NULL; | |
167 | fixP->fx_cgen.opinfo = 0; | |
168 | #endif | |
169 | ||
170 | #ifdef TC_FIX_TYPE | |
efaf0ba4 | 171 | TC_INIT_FIX_DATA (fixP); |
252b5132 RH |
172 | #endif |
173 | ||
174 | as_where (&fixP->fx_file, &fixP->fx_line); | |
175 | ||
176 | /* Usually, we want relocs sorted numerically, but while | |
177 | comparing to older versions of gas that have relocs | |
178 | reverse sorted, it is convenient to have this compile | |
efaf0ba4 | 179 | time option. xoxorich. */ |
252b5132 RH |
180 | { |
181 | ||
252b5132 RH |
182 | fixS **seg_fix_rootP = (frags_chained |
183 | ? &seg_info (now_seg)->fix_root | |
184 | : &frchain_now->fix_root); | |
185 | fixS **seg_fix_tailP = (frags_chained | |
186 | ? &seg_info (now_seg)->fix_tail | |
187 | : &frchain_now->fix_tail); | |
252b5132 RH |
188 | |
189 | #ifdef REVERSE_SORT_RELOCS | |
190 | ||
191 | fixP->fx_next = *seg_fix_rootP; | |
192 | *seg_fix_rootP = fixP; | |
193 | ||
efaf0ba4 | 194 | #else /* REVERSE_SORT_RELOCS */ |
252b5132 RH |
195 | |
196 | fixP->fx_next = NULL; | |
197 | ||
198 | if (*seg_fix_tailP) | |
199 | (*seg_fix_tailP)->fx_next = fixP; | |
200 | else | |
201 | *seg_fix_rootP = fixP; | |
202 | *seg_fix_tailP = fixP; | |
203 | ||
efaf0ba4 | 204 | #endif /* REVERSE_SORT_RELOCS */ |
252b5132 RH |
205 | } |
206 | ||
207 | return fixP; | |
208 | } | |
209 | ||
210 | /* Create a fixup relative to a symbol (plus a constant). */ | |
211 | ||
212 | fixS * | |
d7342424 KH |
213 | fix_new (fragS *frag, /* Which frag? */ |
214 | int where, /* Where in that frag? */ | |
215 | int size, /* 1, 2, or 4 usually. */ | |
216 | symbolS *add_symbol, /* X_add_symbol. */ | |
217 | offsetT offset, /* X_add_number. */ | |
218 | int pcrel, /* TRUE if PC-relative relocation. */ | |
219 | RELOC_ENUM r_type /* Relocation type. */) | |
252b5132 RH |
220 | { |
221 | return fix_new_internal (frag, where, size, add_symbol, | |
222 | (symbolS *) NULL, offset, pcrel, r_type); | |
223 | } | |
224 | ||
225 | /* Create a fixup for an expression. Currently we only support fixups | |
226 | for difference expressions. That is itself more than most object | |
227 | file formats support anyhow. */ | |
228 | ||
229 | fixS * | |
d7342424 KH |
230 | fix_new_exp (fragS *frag, /* Which frag? */ |
231 | int where, /* Where in that frag? */ | |
232 | int size, /* 1, 2, or 4 usually. */ | |
233 | expressionS *exp, /* Expression. */ | |
234 | int pcrel, /* TRUE if PC-relative relocation. */ | |
235 | RELOC_ENUM r_type /* Relocation type. */) | |
252b5132 RH |
236 | { |
237 | symbolS *add = NULL; | |
238 | symbolS *sub = NULL; | |
239 | offsetT off = 0; | |
240 | ||
241 | switch (exp->X_op) | |
242 | { | |
243 | case O_absent: | |
244 | break; | |
245 | ||
37006e43 NC |
246 | case O_register: |
247 | as_bad (_("register value used as expression")); | |
248 | break; | |
249 | ||
252b5132 RH |
250 | case O_add: |
251 | /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if | |
252 | the difference expression cannot immediately be reduced. */ | |
253 | { | |
254 | symbolS *stmp = make_expr_symbol (exp); | |
58a77e41 | 255 | |
252b5132 RH |
256 | exp->X_op = O_symbol; |
257 | exp->X_op_symbol = 0; | |
258 | exp->X_add_symbol = stmp; | |
259 | exp->X_add_number = 0; | |
58a77e41 | 260 | |
252b5132 RH |
261 | return fix_new_exp (frag, where, size, exp, pcrel, r_type); |
262 | } | |
263 | ||
264 | case O_symbol_rva: | |
265 | add = exp->X_add_symbol; | |
266 | off = exp->X_add_number; | |
252b5132 | 267 | r_type = BFD_RELOC_RVA; |
252b5132 RH |
268 | break; |
269 | ||
270 | case O_uminus: | |
271 | sub = exp->X_add_symbol; | |
272 | off = exp->X_add_number; | |
273 | break; | |
274 | ||
275 | case O_subtract: | |
276 | sub = exp->X_op_symbol; | |
277 | /* Fall through. */ | |
278 | case O_symbol: | |
279 | add = exp->X_add_symbol; | |
efaf0ba4 | 280 | /* Fall through. */ |
252b5132 RH |
281 | case O_constant: |
282 | off = exp->X_add_number; | |
283 | break; | |
284 | ||
285 | default: | |
286 | add = make_expr_symbol (exp); | |
287 | break; | |
288 | } | |
289 | ||
efaf0ba4 | 290 | return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type); |
252b5132 RH |
291 | } |
292 | ||
ae6063d4 AM |
293 | /* Generic function to determine whether a fixup requires a relocation. */ |
294 | int | |
d7342424 | 295 | generic_force_reloc (fixS *fix) |
ae6063d4 | 296 | { |
ae6063d4 AM |
297 | if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT |
298 | || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
299 | return 1; | |
7be1c489 | 300 | |
61e192bf NC |
301 | if (fix->fx_addsy == NULL) |
302 | return 0; | |
303 | ||
ae6063d4 AM |
304 | return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL); |
305 | } | |
306 | ||
252b5132 RH |
307 | /* Append a string onto another string, bumping the pointer along. */ |
308 | void | |
d7342424 | 309 | append (char **charPP, char *fromP, unsigned long length) |
252b5132 | 310 | { |
efaf0ba4 | 311 | /* Don't trust memcpy() of 0 chars. */ |
252b5132 RH |
312 | if (length == 0) |
313 | return; | |
314 | ||
315 | memcpy (*charPP, fromP, length); | |
316 | *charPP += length; | |
317 | } | |
318 | ||
efaf0ba4 NC |
319 | /* This routine records the largest alignment seen for each segment. |
320 | If the beginning of the segment is aligned on the worst-case | |
321 | boundary, all of the other alignments within it will work. At | |
322 | least one object format really uses this info. */ | |
323 | ||
252b5132 | 324 | void |
d7342424 KH |
325 | record_alignment (/* Segment to which alignment pertains. */ |
326 | segT seg, | |
327 | /* Alignment, as a power of 2 (e.g., 1 => 2-byte | |
328 | boundary, 2 => 4-byte boundary, etc.) */ | |
329 | int align) | |
252b5132 RH |
330 | { |
331 | if (seg == absolute_section) | |
332 | return; | |
7be1c489 | 333 | |
252b5132 RH |
334 | if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg)) |
335 | bfd_set_section_alignment (stdoutput, seg, align); | |
252b5132 RH |
336 | } |
337 | ||
0a9ef439 | 338 | int |
d7342424 | 339 | get_recorded_alignment (segT seg) |
0a9ef439 RH |
340 | { |
341 | if (seg == absolute_section) | |
342 | return 0; | |
7be1c489 | 343 | |
0a9ef439 | 344 | return bfd_get_section_alignment (stdoutput, seg); |
0a9ef439 RH |
345 | } |
346 | ||
252b5132 RH |
347 | /* Reset the section indices after removing the gas created sections. */ |
348 | ||
349 | static void | |
e7ff5c73 | 350 | renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg) |
252b5132 RH |
351 | { |
352 | int *countp = (int *) countparg; | |
353 | ||
354 | sec->index = *countp; | |
355 | ++*countp; | |
356 | } | |
357 | ||
252b5132 | 358 | static fragS * |
d7342424 | 359 | chain_frchains_together_1 (segT section, struct frchain *frchp) |
252b5132 RH |
360 | { |
361 | fragS dummy, *prev_frag = &dummy; | |
252b5132 | 362 | fixS fix_dummy, *prev_fix = &fix_dummy; |
252b5132 | 363 | |
c9049d30 | 364 | for (; frchp; frchp = frchp->frch_next) |
252b5132 RH |
365 | { |
366 | prev_frag->fr_next = frchp->frch_root; | |
367 | prev_frag = frchp->frch_last; | |
368 | assert (prev_frag->fr_type != 0); | |
252b5132 RH |
369 | if (frchp->fix_root != (fixS *) NULL) |
370 | { | |
371 | if (seg_info (section)->fix_root == (fixS *) NULL) | |
372 | seg_info (section)->fix_root = frchp->fix_root; | |
373 | prev_fix->fx_next = frchp->fix_root; | |
374 | seg_info (section)->fix_tail = frchp->fix_tail; | |
375 | prev_fix = frchp->fix_tail; | |
376 | } | |
252b5132 RH |
377 | } |
378 | assert (prev_frag->fr_type != 0); | |
033cd5fd | 379 | assert (prev_frag != &dummy); |
252b5132 RH |
380 | prev_frag->fr_next = 0; |
381 | return prev_frag; | |
382 | } | |
383 | ||
252b5132 | 384 | static void |
d7342424 KH |
385 | chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED, |
386 | segT section, | |
e7ff5c73 | 387 | void *xxx ATTRIBUTE_UNUSED) |
252b5132 RH |
388 | { |
389 | segment_info_type *info; | |
390 | ||
391 | /* BFD may have introduced its own sections without using | |
392 | subseg_new, so it is possible that seg_info is NULL. */ | |
393 | info = seg_info (section); | |
394 | if (info != (segment_info_type *) NULL) | |
efaf0ba4 NC |
395 | info->frchainP->frch_last |
396 | = chain_frchains_together_1 (section, info->frchainP); | |
252b5132 RH |
397 | |
398 | /* Now that we've chained the frags together, we must add new fixups | |
399 | to the segment, not to the frag chain. */ | |
400 | frags_chained = 1; | |
401 | } | |
402 | ||
252b5132 | 403 | static void |
d7342424 | 404 | cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP) |
252b5132 RH |
405 | { |
406 | switch (fragP->fr_type) | |
407 | { | |
408 | case rs_align: | |
409 | case rs_align_code: | |
0a9ef439 | 410 | case rs_align_test: |
252b5132 RH |
411 | case rs_org: |
412 | case rs_space: | |
413 | #ifdef HANDLE_ALIGN | |
414 | HANDLE_ALIGN (fragP); | |
415 | #endif | |
416 | know (fragP->fr_next != NULL); | |
417 | fragP->fr_offset = (fragP->fr_next->fr_address | |
418 | - fragP->fr_address | |
419 | - fragP->fr_fix) / fragP->fr_var; | |
420 | if (fragP->fr_offset < 0) | |
421 | { | |
14ad458a ILT |
422 | as_bad_where (fragP->fr_file, fragP->fr_line, |
423 | _("attempt to .org/.space backwards? (%ld)"), | |
424 | (long) fragP->fr_offset); | |
3f3cdb03 | 425 | fragP->fr_offset = 0; |
252b5132 RH |
426 | } |
427 | fragP->fr_type = rs_fill; | |
428 | break; | |
429 | ||
430 | case rs_fill: | |
431 | break; | |
432 | ||
433 | case rs_leb128: | |
434 | { | |
435 | valueT value = S_GET_VALUE (fragP->fr_symbol); | |
436 | int size; | |
437 | ||
438 | size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value, | |
439 | fragP->fr_subtype); | |
440 | ||
441 | fragP->fr_fix += size; | |
442 | fragP->fr_type = rs_fill; | |
443 | fragP->fr_var = 0; | |
444 | fragP->fr_offset = 0; | |
445 | fragP->fr_symbol = NULL; | |
446 | } | |
447 | break; | |
448 | ||
449 | case rs_cfa: | |
450 | eh_frame_convert_frag (fragP); | |
451 | break; | |
452 | ||
220e750f RH |
453 | case rs_dwarf2dbg: |
454 | dwarf2dbg_convert_frag (fragP); | |
455 | break; | |
456 | ||
252b5132 | 457 | case rs_machine_dependent: |
252b5132 | 458 | md_convert_frag (stdoutput, sec, fragP); |
252b5132 RH |
459 | |
460 | assert (fragP->fr_next == NULL | |
461 | || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address) | |
462 | == fragP->fr_fix)); | |
463 | ||
efaf0ba4 NC |
464 | /* After md_convert_frag, we make the frag into a ".space 0". |
465 | md_convert_frag() should set up any fixSs and constants | |
466 | required. */ | |
252b5132 RH |
467 | frag_wane (fragP); |
468 | break; | |
469 | ||
470 | #ifndef WORKING_DOT_WORD | |
471 | case rs_broken_word: | |
472 | { | |
473 | struct broken_word *lie; | |
474 | ||
475 | if (fragP->fr_subtype) | |
476 | { | |
477 | fragP->fr_fix += md_short_jump_size; | |
478 | for (lie = (struct broken_word *) (fragP->fr_symbol); | |
479 | lie && lie->dispfrag == fragP; | |
480 | lie = lie->next_broken_word) | |
481 | if (lie->added == 1) | |
482 | fragP->fr_fix += md_long_jump_size; | |
483 | } | |
484 | frag_wane (fragP); | |
485 | } | |
486 | break; | |
487 | #endif | |
488 | ||
489 | default: | |
490 | BAD_CASE (fragP->fr_type); | |
491 | break; | |
492 | } | |
09b935ac AM |
493 | #ifdef md_frag_check |
494 | md_frag_check (fragP); | |
495 | #endif | |
252b5132 RH |
496 | } |
497 | ||
32638454 AM |
498 | struct relax_seg_info |
499 | { | |
500 | int pass; | |
501 | int changed; | |
502 | }; | |
d7342424 | 503 | |
252b5132 | 504 | static void |
32638454 | 505 | relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx) |
e46d99eb AM |
506 | { |
507 | segment_info_type *seginfo = seg_info (sec); | |
32638454 | 508 | struct relax_seg_info *info = (struct relax_seg_info *) xxx; |
e46d99eb AM |
509 | |
510 | if (seginfo && seginfo->frchainP | |
32638454 AM |
511 | && relax_segment (seginfo->frchainP->frch_root, sec, info->pass)) |
512 | info->changed = 1; | |
e46d99eb AM |
513 | } |
514 | ||
e46d99eb | 515 | static void |
e7ff5c73 | 516 | size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED) |
252b5132 RH |
517 | { |
518 | flagword flags; | |
519 | fragS *fragp; | |
520 | segment_info_type *seginfo; | |
521 | int x; | |
522 | valueT size, newsize; | |
523 | ||
524 | subseg_change (sec, 0); | |
525 | ||
252b5132 RH |
526 | seginfo = seg_info (sec); |
527 | if (seginfo && seginfo->frchainP) | |
528 | { | |
252b5132 RH |
529 | for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next) |
530 | cvt_frag_to_fill (sec, fragp); | |
531 | for (fragp = seginfo->frchainP->frch_root; | |
532 | fragp->fr_next; | |
533 | fragp = fragp->fr_next) | |
efaf0ba4 NC |
534 | /* Walk to last elt. */ |
535 | ; | |
252b5132 RH |
536 | size = fragp->fr_address + fragp->fr_fix; |
537 | } | |
538 | else | |
539 | size = 0; | |
540 | ||
e46d99eb AM |
541 | flags = bfd_get_section_flags (abfd, sec); |
542 | ||
252b5132 RH |
543 | if (size > 0 && ! seginfo->bss) |
544 | flags |= SEC_HAS_CONTENTS; | |
545 | ||
546 | /* @@ This is just an approximation. */ | |
547 | if (seginfo && seginfo->fix_root) | |
548 | flags |= SEC_RELOC; | |
549 | else | |
550 | flags &= ~SEC_RELOC; | |
551 | x = bfd_set_section_flags (abfd, sec, flags); | |
b34976b6 | 552 | assert (x); |
252b5132 RH |
553 | |
554 | newsize = md_section_align (sec, size); | |
555 | x = bfd_set_section_size (abfd, sec, newsize); | |
b34976b6 | 556 | assert (x); |
252b5132 RH |
557 | |
558 | /* If the size had to be rounded up, add some padding in the last | |
559 | non-empty frag. */ | |
560 | assert (newsize >= size); | |
561 | if (size != newsize) | |
562 | { | |
563 | fragS *last = seginfo->frchainP->frch_last; | |
564 | fragp = seginfo->frchainP->frch_root; | |
565 | while (fragp->fr_next != last) | |
566 | fragp = fragp->fr_next; | |
567 | last->fr_address = size; | |
18e1d487 AM |
568 | if ((newsize - size) % fragp->fr_var == 0) |
569 | fragp->fr_offset += (newsize - size) / fragp->fr_var; | |
570 | else | |
571 | /* If we hit this abort, it's likely due to subsegs_finish not | |
572 | providing sufficient alignment on the last frag, and the | |
573 | machine dependent code using alignment frags with fr_var | |
574 | greater than 1. */ | |
575 | abort (); | |
252b5132 RH |
576 | } |
577 | ||
578 | #ifdef tc_frob_section | |
579 | tc_frob_section (sec); | |
580 | #endif | |
581 | #ifdef obj_frob_section | |
582 | obj_frob_section (sec); | |
583 | #endif | |
584 | } | |
585 | ||
586 | #ifdef DEBUG2 | |
587 | static void | |
988392e2 | 588 | dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream) |
252b5132 | 589 | { |
252b5132 RH |
590 | segment_info_type *seginfo = seg_info (sec); |
591 | fixS *fixp = seginfo->fix_root; | |
592 | ||
593 | if (!fixp) | |
594 | return; | |
595 | ||
596 | fprintf (stream, "sec %s relocs:\n", sec->name); | |
597 | while (fixp) | |
598 | { | |
599 | symbolS *s = fixp->fx_addsy; | |
e723ef7c ILT |
600 | |
601 | fprintf (stream, " %08lx: type %d ", (unsigned long) fixp, | |
602 | (int) fixp->fx_r_type); | |
603 | if (s == NULL) | |
604 | fprintf (stream, "no sym\n"); | |
605 | else | |
252b5132 | 606 | { |
e723ef7c ILT |
607 | print_symbol_value_1 (stream, s); |
608 | fprintf (stream, "\n"); | |
252b5132 | 609 | } |
252b5132 RH |
610 | fixp = fixp->fx_next; |
611 | } | |
612 | } | |
613 | #else | |
614 | #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0) | |
615 | #endif | |
616 | ||
617 | #ifndef EMIT_SECTION_SYMBOLS | |
618 | #define EMIT_SECTION_SYMBOLS 1 | |
619 | #endif | |
620 | ||
df44284e AM |
621 | /* This pass over fixups decides whether symbols can be replaced with |
622 | section symbols. */ | |
623 | ||
252b5132 | 624 | static void |
d7342424 KH |
625 | adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED, |
626 | asection *sec, | |
e7ff5c73 | 627 | void *xxx ATTRIBUTE_UNUSED) |
252b5132 RH |
628 | { |
629 | segment_info_type *seginfo = seg_info (sec); | |
630 | fixS *fixp; | |
631 | ||
632 | if (seginfo == NULL) | |
633 | return; | |
634 | ||
635 | dump_section_relocs (abfd, sec, stderr); | |
636 | ||
637 | for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) | |
638 | if (fixp->fx_done) | |
efaf0ba4 NC |
639 | /* Ignore it. */ |
640 | ; | |
252b5132 RH |
641 | else if (fixp->fx_addsy) |
642 | { | |
643 | symbolS *sym; | |
644 | asection *symsec; | |
645 | ||
646 | #ifdef DEBUG5 | |
647 | fprintf (stderr, "\n\nadjusting fixup:\n"); | |
648 | print_fixup (fixp); | |
649 | #endif | |
650 | ||
651 | sym = fixp->fx_addsy; | |
652 | ||
653 | /* All symbols should have already been resolved at this | |
654 | point. It is possible to see unresolved expression | |
655 | symbols, though, since they are not in the regular symbol | |
656 | table. */ | |
df44284e | 657 | resolve_symbol_value (sym); |
efaf0ba4 | 658 | |
49309057 | 659 | if (fixp->fx_subsy != NULL) |
6386f3a7 | 660 | resolve_symbol_value (fixp->fx_subsy); |
252b5132 | 661 | |
60938e80 L |
662 | /* If this symbol is equated to an undefined or common symbol, |
663 | convert the fixup to being against that symbol. */ | |
06e77878 AO |
664 | if (symbol_equated_reloc_p (sym) |
665 | || S_IS_WEAKREFR (sym)) | |
252b5132 | 666 | { |
49309057 | 667 | fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number; |
50afb01e | 668 | sym = symbol_get_value_expression (sym)->X_add_symbol; |
252b5132 RH |
669 | fixp->fx_addsy = sym; |
670 | } | |
671 | ||
df44284e | 672 | if (symbol_mri_common_p (sym)) |
252b5132 RH |
673 | { |
674 | /* These symbols are handled specially in fixup_segment. */ | |
df44284e | 675 | continue; |
252b5132 RH |
676 | } |
677 | ||
a161fe53 AM |
678 | /* If the symbol is undefined, common, weak, or global (ELF |
679 | shared libs), we can't replace it with the section symbol. */ | |
ae6063d4 | 680 | if (S_FORCE_RELOC (fixp->fx_addsy, 1)) |
a161fe53 AM |
681 | continue; |
682 | ||
683 | /* Is there some other (target cpu dependent) reason we can't adjust | |
684 | this one? (E.g. relocations involving function addresses on | |
685 | the PA. */ | |
686 | #ifdef tc_fix_adjustable | |
687 | if (! tc_fix_adjustable (fixp)) | |
688 | continue; | |
689 | #endif | |
690 | ||
691 | /* Since we're reducing to section symbols, don't attempt to reduce | |
692 | anything that's already using one. */ | |
693 | if (symbol_section_p (sym)) | |
694 | continue; | |
695 | ||
252b5132 | 696 | symsec = S_GET_SEGMENT (sym); |
252b5132 RH |
697 | if (symsec == NULL) |
698 | abort (); | |
efaf0ba4 | 699 | |
252b5132 RH |
700 | if (bfd_is_abs_section (symsec)) |
701 | { | |
a161fe53 AM |
702 | /* The fixup_segment routine normally will not use this |
703 | symbol in a relocation. */ | |
df44284e | 704 | continue; |
252b5132 RH |
705 | } |
706 | ||
7565ed77 ILT |
707 | /* Don't try to reduce relocs which refer to non-local symbols |
708 | in .linkonce sections. It can lead to confusion when a | |
709 | debugging section refers to a .linkonce section. I hope | |
710 | this will always be correct. */ | |
711 | if (symsec != sec && ! S_IS_LOCAL (sym)) | |
252b5132 | 712 | { |
a161fe53 AM |
713 | if ((symsec->flags & SEC_LINK_ONCE) != 0 |
714 | || (IS_ELF | |
715 | /* The GNU toolchain uses an extension for ELF: a | |
716 | section beginning with the magic string | |
717 | .gnu.linkonce is a linkonce section. */ | |
718 | && strncmp (segment_name (symsec), ".gnu.linkonce", | |
719 | sizeof ".gnu.linkonce" - 1) == 0)) | |
720 | continue; | |
252b5132 | 721 | } |
f7460f5f JJ |
722 | |
723 | /* Never adjust a reloc against local symbol in a merge section | |
724 | with non-zero addend. */ | |
5045ec7a AM |
725 | if ((symsec->flags & SEC_MERGE) != 0 |
726 | && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL)) | |
a161fe53 | 727 | continue; |
13ae64f3 JJ |
728 | |
729 | /* Never adjust a reloc against TLS local symbol. */ | |
df44284e | 730 | if ((symsec->flags & SEC_THREAD_LOCAL) != 0) |
a161fe53 | 731 | continue; |
252b5132 | 732 | |
a161fe53 | 733 | /* We refetch the segment when calling section_symbol, rather |
252b5132 | 734 | than using symsec, because S_GET_VALUE may wind up changing |
efaf0ba4 | 735 | the section when it calls resolve_symbol_value. */ |
252b5132 RH |
736 | fixp->fx_offset += S_GET_VALUE (sym); |
737 | fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym)); | |
e723ef7c ILT |
738 | #ifdef DEBUG5 |
739 | fprintf (stderr, "\nadjusted fixup:\n"); | |
740 | print_fixup (fixp); | |
741 | #endif | |
252b5132 | 742 | } |
252b5132 RH |
743 | |
744 | dump_section_relocs (abfd, sec, stderr); | |
745 | } | |
746 | ||
e7ff5c73 AM |
747 | /* fixup_segment() |
748 | ||
749 | Go through all the fixS's in a segment and see which ones can be | |
750 | handled now. (These consist of fixS where we have since discovered | |
751 | the value of a symbol, or the address of the frag involved.) | |
752 | For each one, call md_apply_fix to put the fix into the frag data. | |
753 | ||
754 | Result is a count of how many relocation structs will be needed to | |
755 | handle the remaining fixS's that we couldn't completely handle here. | |
756 | These will be output later by emit_relocations(). */ | |
757 | ||
758 | static long | |
759 | fixup_segment (fixS *fixP, segT this_segment) | |
760 | { | |
761 | long seg_reloc_count = 0; | |
762 | valueT add_number; | |
763 | fragS *fragP; | |
764 | segT add_symbol_segment = absolute_section; | |
765 | ||
766 | if (fixP != NULL && abs_section_sym == NULL) | |
767 | abs_section_sym = section_symbol (absolute_section); | |
768 | ||
769 | /* If the linker is doing the relaxing, we must not do any fixups. | |
770 | ||
771 | Well, strictly speaking that's not true -- we could do any that | |
772 | are PC-relative and don't cross regions that could change size. | |
773 | And for the i960 we might be able to turn callx/callj into bal | |
774 | anyways in cases where we know the maximum displacement. */ | |
775 | if (linkrelax && TC_LINKRELAX_FIXUP (this_segment)) | |
776 | { | |
777 | for (; fixP; fixP = fixP->fx_next) | |
778 | if (!fixP->fx_done) | |
779 | { | |
780 | if (fixP->fx_addsy == NULL) | |
781 | { | |
782 | /* There was no symbol required by this relocation. | |
783 | However, BFD doesn't really handle relocations | |
784 | without symbols well. So fake up a local symbol in | |
785 | the absolute section. */ | |
786 | fixP->fx_addsy = abs_section_sym; | |
787 | } | |
788 | symbol_mark_used_in_reloc (fixP->fx_addsy); | |
789 | if (fixP->fx_subsy != NULL) | |
790 | symbol_mark_used_in_reloc (fixP->fx_subsy); | |
791 | seg_reloc_count++; | |
792 | } | |
793 | TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count); | |
794 | return seg_reloc_count; | |
795 | } | |
796 | ||
797 | for (; fixP; fixP = fixP->fx_next) | |
798 | { | |
799 | #ifdef DEBUG5 | |
800 | fprintf (stderr, "\nprocessing fixup:\n"); | |
801 | print_fixup (fixP); | |
802 | #endif | |
803 | ||
804 | fragP = fixP->fx_frag; | |
805 | know (fragP); | |
806 | #ifdef TC_VALIDATE_FIX | |
807 | TC_VALIDATE_FIX (fixP, this_segment, skip); | |
808 | #endif | |
809 | add_number = fixP->fx_offset; | |
810 | ||
811 | if (fixP->fx_addsy != NULL | |
812 | && symbol_mri_common_p (fixP->fx_addsy)) | |
813 | { | |
814 | add_number += S_GET_VALUE (fixP->fx_addsy); | |
815 | fixP->fx_offset = add_number; | |
816 | fixP->fx_addsy | |
817 | = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol; | |
818 | } | |
819 | ||
820 | if (fixP->fx_addsy != NULL) | |
821 | add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy); | |
822 | ||
823 | if (fixP->fx_subsy != NULL) | |
824 | { | |
825 | segT sub_symbol_segment; | |
826 | resolve_symbol_value (fixP->fx_subsy); | |
827 | sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy); | |
828 | if (fixP->fx_addsy != NULL | |
829 | && sub_symbol_segment == add_symbol_segment | |
830 | && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment)) | |
831 | { | |
832 | add_number += S_GET_VALUE (fixP->fx_addsy); | |
833 | add_number -= S_GET_VALUE (fixP->fx_subsy); | |
834 | fixP->fx_offset = add_number; | |
835 | fixP->fx_addsy = NULL; | |
836 | fixP->fx_subsy = NULL; | |
837 | #ifdef TC_M68K | |
838 | /* See the comment below about 68k weirdness. */ | |
839 | fixP->fx_pcrel = 0; | |
840 | #endif | |
841 | } | |
842 | else if (sub_symbol_segment == absolute_section | |
843 | && !TC_FORCE_RELOCATION_SUB_ABS (fixP)) | |
844 | { | |
845 | add_number -= S_GET_VALUE (fixP->fx_subsy); | |
846 | fixP->fx_offset = add_number; | |
847 | fixP->fx_subsy = NULL; | |
848 | } | |
849 | else if (sub_symbol_segment == this_segment | |
850 | && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP)) | |
851 | { | |
852 | add_number -= S_GET_VALUE (fixP->fx_subsy); | |
853 | fixP->fx_offset = (add_number + fixP->fx_dot_value | |
854 | + fixP->fx_frag->fr_address); | |
855 | ||
856 | /* Make it pc-relative. If the back-end code has not | |
857 | selected a pc-relative reloc, cancel the adjustment | |
858 | we do later on all pc-relative relocs. */ | |
859 | if (0 | |
860 | #ifdef TC_M68K | |
861 | /* Do this for m68k even if it's already described | |
862 | as pc-relative. On the m68k, an operand of | |
863 | "pc@(foo-.-2)" should address "foo" in a | |
864 | pc-relative mode. */ | |
865 | || 1 | |
866 | #endif | |
867 | || !fixP->fx_pcrel) | |
868 | add_number += MD_PCREL_FROM_SECTION (fixP, this_segment); | |
869 | fixP->fx_subsy = NULL; | |
870 | fixP->fx_pcrel = 1; | |
871 | } | |
872 | else if (!TC_VALIDATE_FIX_SUB (fixP)) | |
873 | { | |
874 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
875 | _("can't resolve `%s' {%s section} - `%s' {%s section}"), | |
876 | fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0", | |
877 | segment_name (add_symbol_segment), | |
878 | S_GET_NAME (fixP->fx_subsy), | |
879 | segment_name (sub_symbol_segment)); | |
880 | } | |
881 | } | |
882 | ||
883 | if (fixP->fx_addsy) | |
884 | { | |
885 | if (add_symbol_segment == this_segment | |
886 | && !TC_FORCE_RELOCATION_LOCAL (fixP)) | |
887 | { | |
888 | /* This fixup was made when the symbol's segment was | |
889 | SEG_UNKNOWN, but it is now in the local segment. | |
890 | So we know how to do the address without relocation. */ | |
891 | add_number += S_GET_VALUE (fixP->fx_addsy); | |
892 | fixP->fx_offset = add_number; | |
893 | if (fixP->fx_pcrel) | |
894 | add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment); | |
895 | fixP->fx_addsy = NULL; | |
896 | fixP->fx_pcrel = 0; | |
897 | } | |
898 | else if (add_symbol_segment == absolute_section | |
899 | && !TC_FORCE_RELOCATION_ABS (fixP)) | |
900 | { | |
901 | add_number += S_GET_VALUE (fixP->fx_addsy); | |
902 | fixP->fx_offset = add_number; | |
903 | fixP->fx_addsy = NULL; | |
904 | } | |
905 | else if (add_symbol_segment != undefined_section | |
906 | && ! bfd_is_com_section (add_symbol_segment) | |
907 | && MD_APPLY_SYM_VALUE (fixP)) | |
908 | add_number += S_GET_VALUE (fixP->fx_addsy); | |
909 | } | |
910 | ||
911 | if (fixP->fx_pcrel) | |
912 | { | |
913 | add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment); | |
914 | if (!fixP->fx_done && fixP->fx_addsy == NULL) | |
915 | { | |
916 | /* There was no symbol required by this relocation. | |
917 | However, BFD doesn't really handle relocations | |
918 | without symbols well. So fake up a local symbol in | |
919 | the absolute section. */ | |
920 | fixP->fx_addsy = abs_section_sym; | |
921 | } | |
922 | } | |
923 | ||
924 | if (!fixP->fx_done) | |
925 | md_apply_fix (fixP, &add_number, this_segment); | |
926 | ||
927 | if (!fixP->fx_done) | |
928 | { | |
929 | ++seg_reloc_count; | |
930 | if (fixP->fx_addsy == NULL) | |
931 | fixP->fx_addsy = abs_section_sym; | |
932 | symbol_mark_used_in_reloc (fixP->fx_addsy); | |
933 | if (fixP->fx_subsy != NULL) | |
934 | symbol_mark_used_in_reloc (fixP->fx_subsy); | |
935 | } | |
936 | ||
937 | if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0) | |
938 | { | |
939 | if (fixP->fx_size < sizeof (valueT)) | |
940 | { | |
941 | valueT mask; | |
942 | ||
943 | mask = 0; | |
944 | mask--; /* Set all bits to one. */ | |
945 | mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0); | |
946 | if ((add_number & mask) != 0 && (add_number & mask) != mask) | |
947 | { | |
948 | char buf[50], buf2[50]; | |
949 | sprint_value (buf, fragP->fr_address + fixP->fx_where); | |
950 | if (add_number > 1000) | |
951 | sprint_value (buf2, add_number); | |
952 | else | |
953 | sprintf (buf2, "%ld", (long) add_number); | |
954 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
955 | _("value of %s too large for field of %d bytes at %s"), | |
956 | buf2, fixP->fx_size, buf); | |
957 | } /* Generic error checking. */ | |
958 | } | |
959 | #ifdef WARN_SIGNED_OVERFLOW_WORD | |
960 | /* Warn if a .word value is too large when treated as a signed | |
961 | number. We already know it is not too negative. This is to | |
962 | catch over-large switches generated by gcc on the 68k. */ | |
963 | if (!flag_signed_overflow_ok | |
964 | && fixP->fx_size == 2 | |
965 | && add_number > 0x7fff) | |
966 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
967 | _("signed .word overflow; switch may be too large; %ld at 0x%lx"), | |
968 | (long) add_number, | |
969 | (long) (fragP->fr_address + fixP->fx_where)); | |
970 | #endif | |
971 | } /* Not a bit fix. */ | |
972 | ||
973 | #ifdef TC_VALIDATE_FIX | |
974 | skip: ATTRIBUTE_UNUSED_LABEL | |
975 | ; | |
976 | #endif | |
977 | #ifdef DEBUG5 | |
978 | fprintf (stderr, "result:\n"); | |
979 | print_fixup (fixP); | |
980 | #endif | |
981 | } /* For each fixS in this segment. */ | |
982 | ||
983 | TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count); | |
984 | return seg_reloc_count; | |
985 | } | |
986 | ||
a161fe53 | 987 | static void |
d7342424 KH |
988 | fix_segment (bfd *abfd ATTRIBUTE_UNUSED, |
989 | asection *sec, | |
e7ff5c73 | 990 | void *xxx ATTRIBUTE_UNUSED) |
a161fe53 AM |
991 | { |
992 | segment_info_type *seginfo = seg_info (sec); | |
993 | ||
994 | fixup_segment (seginfo->fix_root, sec); | |
995 | } | |
996 | ||
252b5132 | 997 | static void |
e7ff5c73 | 998 | write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED) |
252b5132 RH |
999 | { |
1000 | segment_info_type *seginfo = seg_info (sec); | |
927781e2 | 1001 | unsigned int i; |
252b5132 RH |
1002 | unsigned int n; |
1003 | arelent **relocs; | |
1004 | fixS *fixp; | |
1005 | char *err; | |
1006 | ||
1007 | /* If seginfo is NULL, we did not create this section; don't do | |
1008 | anything with it. */ | |
1009 | if (seginfo == NULL) | |
1010 | return; | |
1011 | ||
252b5132 RH |
1012 | n = 0; |
1013 | for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) | |
1014 | n++; | |
1015 | ||
1016 | #ifndef RELOC_EXPANSION_POSSIBLE | |
1017 | /* Set up reloc information as well. */ | |
e7ff5c73 | 1018 | relocs = xcalloc (n, sizeof (arelent *)); |
252b5132 RH |
1019 | |
1020 | i = 0; | |
1021 | for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next) | |
1022 | { | |
1023 | arelent *reloc; | |
1024 | bfd_reloc_status_type s; | |
1025 | symbolS *sym; | |
1026 | ||
1027 | if (fixp->fx_done) | |
1028 | { | |
1029 | n--; | |
1030 | continue; | |
1031 | } | |
1032 | ||
1033 | /* If this is an undefined symbol which was equated to another | |
e0890092 | 1034 | symbol, then generate the reloc against the latter symbol |
252b5132 RH |
1035 | rather than the former. */ |
1036 | sym = fixp->fx_addsy; | |
e0890092 | 1037 | while (symbol_equated_reloc_p (sym)) |
252b5132 RH |
1038 | { |
1039 | symbolS *n; | |
1040 | ||
1041 | /* We must avoid looping, as that can occur with a badly | |
1042 | written program. */ | |
49309057 | 1043 | n = symbol_get_value_expression (sym)->X_add_symbol; |
252b5132 RH |
1044 | if (n == sym) |
1045 | break; | |
49309057 | 1046 | fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number; |
252b5132 RH |
1047 | sym = n; |
1048 | } | |
1049 | fixp->fx_addsy = sym; | |
1050 | ||
1051 | reloc = tc_gen_reloc (sec, fixp); | |
1052 | if (!reloc) | |
1053 | { | |
1054 | n--; | |
1055 | continue; | |
1056 | } | |
1057 | ||
59c871b4 BE |
1058 | /* |
1059 | This test is triggered inappropriately for the SH: | |
1060 | if (fixp->fx_where + fixp->fx_size | |
1061 | > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset) | |
1062 | abort (); | |
1063 | */ | |
252b5132 RH |
1064 | |
1065 | s = bfd_install_relocation (stdoutput, reloc, | |
1066 | fixp->fx_frag->fr_literal, | |
1067 | fixp->fx_frag->fr_address, | |
1068 | sec, &err); | |
1069 | switch (s) | |
1070 | { | |
1071 | case bfd_reloc_ok: | |
1072 | break; | |
1073 | case bfd_reloc_overflow: | |
df44284e AM |
1074 | as_bad_where (fixp->fx_file, fixp->fx_line, |
1075 | _("relocation overflow")); | |
252b5132 RH |
1076 | break; |
1077 | case bfd_reloc_outofrange: | |
df44284e AM |
1078 | as_bad_where (fixp->fx_file, fixp->fx_line, |
1079 | _("relocation out of range")); | |
252b5132 RH |
1080 | break; |
1081 | default: | |
1082 | as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"), | |
1083 | fixp->fx_file, fixp->fx_line, s); | |
1084 | } | |
1085 | relocs[i++] = reloc; | |
1086 | } | |
1087 | #else | |
1088 | n = n * MAX_RELOC_EXPANSION; | |
1089 | /* Set up reloc information as well. */ | |
e7ff5c73 | 1090 | relocs = xcalloc (n, sizeof (arelent *)); |
252b5132 RH |
1091 | |
1092 | i = 0; | |
1093 | for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next) | |
1094 | { | |
1095 | arelent **reloc; | |
252b5132 RH |
1096 | bfd_reloc_status_type s; |
1097 | symbolS *sym; | |
1098 | int j; | |
1099 | ||
1100 | if (fixp->fx_done) | |
1101 | { | |
1102 | n--; | |
1103 | continue; | |
1104 | } | |
1105 | ||
1106 | /* If this is an undefined symbol which was equated to another | |
44852b19 | 1107 | symbol, then generate the reloc against the latter symbol |
252b5132 RH |
1108 | rather than the former. */ |
1109 | sym = fixp->fx_addsy; | |
e0890092 | 1110 | while (symbol_equated_reloc_p (sym)) |
df44284e AM |
1111 | { |
1112 | symbolS *n; | |
1113 | ||
1114 | /* We must avoid looping, as that can occur with a badly | |
1115 | written program. */ | |
1116 | n = symbol_get_value_expression (sym)->X_add_symbol; | |
1117 | if (n == sym) | |
1118 | break; | |
1119 | fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number; | |
1120 | sym = n; | |
1121 | } | |
252b5132 RH |
1122 | fixp->fx_addsy = sym; |
1123 | ||
1124 | reloc = tc_gen_reloc (sec, fixp); | |
1125 | ||
1126 | for (j = 0; reloc[j]; j++) | |
1127 | { | |
efaf0ba4 NC |
1128 | relocs[i++] = reloc[j]; |
1129 | assert (i <= n); | |
252b5132 | 1130 | } |
252b5132 RH |
1131 | if (fixp->fx_where + fixp->fx_size |
1132 | > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset) | |
1133 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
1134 | _("internal error: fixup not contained within frag")); | |
1135 | for (j = 0; reloc[j]; j++) | |
efaf0ba4 | 1136 | { |
252b5132 RH |
1137 | s = bfd_install_relocation (stdoutput, reloc[j], |
1138 | fixp->fx_frag->fr_literal, | |
1139 | fixp->fx_frag->fr_address, | |
1140 | sec, &err); | |
efaf0ba4 | 1141 | switch (s) |
252b5132 RH |
1142 | { |
1143 | case bfd_reloc_ok: | |
1144 | break; | |
1145 | case bfd_reloc_overflow: | |
1146 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
1147 | _("relocation overflow")); | |
1148 | break; | |
df44284e AM |
1149 | case bfd_reloc_outofrange: |
1150 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
1151 | _("relocation out of range")); | |
1152 | break; | |
252b5132 | 1153 | default: |
df44284e AM |
1154 | as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"), |
1155 | fixp->fx_file, fixp->fx_line, s); | |
252b5132 | 1156 | } |
efaf0ba4 | 1157 | } |
252b5132 RH |
1158 | } |
1159 | n = i; | |
1160 | #endif | |
1161 | ||
1162 | #ifdef DEBUG4 | |
1163 | { | |
988392e2 | 1164 | unsigned int i, j, nsyms; |
252b5132 RH |
1165 | asymbol **sympp; |
1166 | sympp = bfd_get_outsymbols (stdoutput); | |
1167 | nsyms = bfd_get_symcount (stdoutput); | |
1168 | for (i = 0; i < n; i++) | |
1169 | if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0) | |
1170 | { | |
1171 | for (j = 0; j < nsyms; j++) | |
1172 | if (sympp[j] == *relocs[i]->sym_ptr_ptr) | |
1173 | break; | |
1174 | if (j == nsyms) | |
1175 | abort (); | |
1176 | } | |
1177 | } | |
1178 | #endif | |
1179 | ||
1180 | if (n) | |
1181 | bfd_set_reloc (stdoutput, sec, relocs, n); | |
1182 | else | |
1183 | bfd_set_section_flags (abfd, sec, | |
1184 | (bfd_get_section_flags (abfd, sec) | |
1185 | & (flagword) ~SEC_RELOC)); | |
1186 | ||
945a1a6b ILT |
1187 | #ifdef SET_SECTION_RELOCS |
1188 | SET_SECTION_RELOCS (sec, relocs, n); | |
1189 | #endif | |
1190 | ||
252b5132 RH |
1191 | #ifdef DEBUG3 |
1192 | { | |
988392e2 | 1193 | unsigned int i; |
252b5132 RH |
1194 | arelent *r; |
1195 | asymbol *s; | |
1196 | fprintf (stderr, "relocs for sec %s\n", sec->name); | |
1197 | for (i = 0; i < n; i++) | |
1198 | { | |
1199 | r = relocs[i]; | |
1200 | s = *r->sym_ptr_ptr; | |
988392e2 CG |
1201 | fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n", |
1202 | i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend); | |
252b5132 RH |
1203 | } |
1204 | } | |
1205 | #endif | |
1206 | } | |
1207 | ||
1208 | static void | |
d7342424 KH |
1209 | write_contents (bfd *abfd ATTRIBUTE_UNUSED, |
1210 | asection *sec, | |
e7ff5c73 | 1211 | void *xxx ATTRIBUTE_UNUSED) |
252b5132 RH |
1212 | { |
1213 | segment_info_type *seginfo = seg_info (sec); | |
685736be | 1214 | addressT offset = 0; |
252b5132 RH |
1215 | fragS *f; |
1216 | ||
1217 | /* Write out the frags. */ | |
1218 | if (seginfo == NULL | |
efaf0ba4 | 1219 | || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)) |
252b5132 RH |
1220 | return; |
1221 | ||
1222 | for (f = seginfo->frchainP->frch_root; | |
1223 | f; | |
1224 | f = f->fr_next) | |
1225 | { | |
1226 | int x; | |
685736be | 1227 | addressT fill_size; |
252b5132 | 1228 | char *fill_literal; |
685736be | 1229 | offsetT count; |
252b5132 RH |
1230 | |
1231 | assert (f->fr_type == rs_fill); | |
1232 | if (f->fr_fix) | |
1233 | { | |
1234 | x = bfd_set_section_contents (stdoutput, sec, | |
1235 | f->fr_literal, (file_ptr) offset, | |
1236 | (bfd_size_type) f->fr_fix); | |
b34976b6 | 1237 | if (!x) |
885afe7b AM |
1238 | as_fatal (_("can't write %s: %s"), stdoutput->filename, |
1239 | bfd_errmsg (bfd_get_error ())); | |
252b5132 RH |
1240 | offset += f->fr_fix; |
1241 | } | |
1242 | fill_literal = f->fr_literal + f->fr_fix; | |
1243 | fill_size = f->fr_var; | |
1244 | count = f->fr_offset; | |
1245 | assert (count >= 0); | |
1246 | if (fill_size && count) | |
1247 | { | |
1248 | char buf[256]; | |
efaf0ba4 | 1249 | if (fill_size > sizeof (buf)) |
252b5132 | 1250 | { |
efaf0ba4 | 1251 | /* Do it the old way. Can this ever happen? */ |
252b5132 RH |
1252 | while (count--) |
1253 | { | |
1254 | x = bfd_set_section_contents (stdoutput, sec, | |
1255 | fill_literal, | |
1256 | (file_ptr) offset, | |
1257 | (bfd_size_type) fill_size); | |
b34976b6 | 1258 | if (!x) |
885afe7b AM |
1259 | as_fatal (_("can't write %s: %s"), stdoutput->filename, |
1260 | bfd_errmsg (bfd_get_error ())); | |
252b5132 RH |
1261 | offset += fill_size; |
1262 | } | |
1263 | } | |
1264 | else | |
1265 | { | |
1266 | /* Build a buffer full of fill objects and output it as | |
1267 | often as necessary. This saves on the overhead of | |
1268 | potentially lots of bfd_set_section_contents calls. */ | |
1269 | int n_per_buf, i; | |
1270 | if (fill_size == 1) | |
1271 | { | |
1272 | n_per_buf = sizeof (buf); | |
1273 | memset (buf, *fill_literal, n_per_buf); | |
1274 | } | |
1275 | else | |
1276 | { | |
1277 | char *bufp; | |
efaf0ba4 | 1278 | n_per_buf = sizeof (buf) / fill_size; |
252b5132 | 1279 | for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size) |
efaf0ba4 | 1280 | memcpy (bufp, fill_literal, fill_size); |
252b5132 RH |
1281 | } |
1282 | for (; count > 0; count -= n_per_buf) | |
1283 | { | |
1284 | n_per_buf = n_per_buf > count ? count : n_per_buf; | |
efaf0ba4 NC |
1285 | x = bfd_set_section_contents |
1286 | (stdoutput, sec, buf, (file_ptr) offset, | |
1287 | (bfd_size_type) n_per_buf * fill_size); | |
b34976b6 | 1288 | if (!x) |
0e389e77 | 1289 | as_fatal (_("cannot write to output file")); |
252b5132 RH |
1290 | offset += n_per_buf * fill_size; |
1291 | } | |
1292 | } | |
1293 | } | |
1294 | } | |
1295 | } | |
252b5132 | 1296 | |
252b5132 | 1297 | static void |
d7342424 | 1298 | merge_data_into_text (void) |
252b5132 | 1299 | { |
252b5132 RH |
1300 | seg_info (text_section)->frchainP->frch_last->fr_next = |
1301 | seg_info (data_section)->frchainP->frch_root; | |
1302 | seg_info (text_section)->frchainP->frch_last = | |
1303 | seg_info (data_section)->frchainP->frch_last; | |
1304 | seg_info (data_section)->frchainP = 0; | |
252b5132 | 1305 | } |
252b5132 | 1306 | |
252b5132 | 1307 | static void |
d7342424 | 1308 | set_symtab (void) |
252b5132 RH |
1309 | { |
1310 | int nsyms; | |
1311 | asymbol **asympp; | |
1312 | symbolS *symp; | |
b34976b6 | 1313 | bfd_boolean result; |
252b5132 RH |
1314 | |
1315 | /* Count symbols. We can't rely on a count made by the loop in | |
1316 | write_object_file, because *_frob_file may add a new symbol or | |
1317 | two. */ | |
1318 | nsyms = 0; | |
1319 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) | |
1320 | nsyms++; | |
1321 | ||
1322 | if (nsyms) | |
1323 | { | |
1324 | int i; | |
0e1a166b | 1325 | bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *); |
252b5132 | 1326 | |
e7ff5c73 | 1327 | asympp = bfd_alloc (stdoutput, amt); |
252b5132 RH |
1328 | symp = symbol_rootP; |
1329 | for (i = 0; i < nsyms; i++, symp = symbol_next (symp)) | |
1330 | { | |
49309057 ILT |
1331 | asympp[i] = symbol_get_bfdsym (symp); |
1332 | symbol_mark_written (symp); | |
252b5132 RH |
1333 | } |
1334 | } | |
1335 | else | |
1336 | asympp = 0; | |
1337 | result = bfd_set_symtab (stdoutput, asympp, nsyms); | |
b34976b6 | 1338 | assert (result); |
252b5132 RH |
1339 | symbol_table_frozen = 1; |
1340 | } | |
252b5132 RH |
1341 | |
1342 | /* Finish the subsegments. After every sub-segment, we fake an | |
1343 | ".align ...". This conforms to BSD4.2 brane-damage. We then fake | |
1344 | ".fill 0" because that is the kind of frag that requires least | |
1345 | thought. ".align" frags like to have a following frag since that | |
1346 | makes calculating their intended length trivial. */ | |
1347 | ||
1348 | #ifndef SUB_SEGMENT_ALIGN | |
18e1d487 | 1349 | #ifdef HANDLE_ALIGN |
436d9e46 | 1350 | /* The last subsegment gets an alignment corresponding to the alignment |
18e1d487 AM |
1351 | of the section. This allows proper nop-filling at the end of |
1352 | code-bearing sections. */ | |
1353 | #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \ | |
c9049d30 | 1354 | (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0) |
18e1d487 | 1355 | #else |
18e1d487 | 1356 | #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0 |
252b5132 RH |
1357 | #endif |
1358 | #endif | |
1359 | ||
1360 | void | |
d7342424 | 1361 | subsegs_finish (void) |
252b5132 RH |
1362 | { |
1363 | struct frchain *frchainP; | |
c9049d30 | 1364 | asection *s; |
252b5132 | 1365 | |
c9049d30 | 1366 | for (s = stdoutput->sections; s; s = s->next) |
252b5132 | 1367 | { |
c9049d30 AM |
1368 | segment_info_type *seginfo = seg_info (s); |
1369 | if (!seginfo) | |
1370 | continue; | |
0a9ef439 | 1371 | |
c9049d30 AM |
1372 | for (frchainP = seginfo->frchainP; |
1373 | frchainP != NULL; | |
1374 | frchainP = frchainP->frch_next) | |
381a1ab3 | 1375 | { |
c9049d30 AM |
1376 | int alignment = 0; |
1377 | ||
1378 | subseg_set (s, frchainP->frch_subseg); | |
381a1ab3 | 1379 | |
c9049d30 AM |
1380 | /* This now gets called even if we had errors. In that case, |
1381 | any alignment is meaningless, and, moreover, will look weird | |
1382 | if we are generating a listing. */ | |
1383 | if (!had_errors ()) | |
1384 | { | |
1385 | alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP); | |
1386 | if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE) | |
1387 | && now_seg->entsize) | |
381a1ab3 | 1388 | { |
c9049d30 AM |
1389 | unsigned int entsize = now_seg->entsize; |
1390 | int entalign = 0; | |
1391 | ||
1392 | while ((entsize & 1) == 0) | |
1393 | { | |
1394 | ++entalign; | |
1395 | entsize >>= 1; | |
1396 | } | |
1397 | if (entalign > alignment) | |
1398 | alignment = entalign; | |
381a1ab3 | 1399 | } |
381a1ab3 | 1400 | } |
0a9ef439 | 1401 | |
c9049d30 AM |
1402 | if (subseg_text_p (now_seg)) |
1403 | frag_align_code (alignment, 0); | |
1404 | else | |
1405 | frag_align (alignment, 0, 0); | |
252b5132 | 1406 | |
c9049d30 AM |
1407 | /* frag_align will have left a new frag. |
1408 | Use this last frag for an empty ".fill". | |
252b5132 | 1409 | |
c9049d30 AM |
1410 | For this segment ... |
1411 | Create a last frag. Do not leave a "being filled in frag". */ | |
1412 | frag_wane (frag_now); | |
1413 | frag_now->fr_fix = 0; | |
1414 | know (frag_now->fr_next == NULL); | |
1415 | } | |
252b5132 RH |
1416 | } |
1417 | } | |
1418 | ||
1419 | /* Write the object file. */ | |
1420 | ||
1421 | void | |
d7342424 | 1422 | write_object_file (void) |
252b5132 | 1423 | { |
32638454 | 1424 | struct relax_seg_info rsi; |
7be1c489 | 1425 | #ifndef WORKING_DOT_WORD |
efaf0ba4 | 1426 | fragS *fragP; /* Track along all frags. */ |
252b5132 RH |
1427 | #endif |
1428 | ||
1429 | /* Do we really want to write it? */ | |
1430 | { | |
1431 | int n_warns, n_errs; | |
1432 | n_warns = had_warnings (); | |
1433 | n_errs = had_errors (); | |
1434 | /* The -Z flag indicates that an object file should be generated, | |
1435 | regardless of warnings and errors. */ | |
1436 | if (flag_always_generate_output) | |
1437 | { | |
1438 | if (n_warns || n_errs) | |
0e389e77 | 1439 | as_warn (_("%d error%s, %d warning%s, generating bad object file"), |
252b5132 RH |
1440 | n_errs, n_errs == 1 ? "" : "s", |
1441 | n_warns, n_warns == 1 ? "" : "s"); | |
1442 | } | |
1443 | else | |
1444 | { | |
1445 | if (n_errs) | |
0e389e77 | 1446 | as_fatal (_("%d error%s, %d warning%s, no object file generated"), |
252b5132 RH |
1447 | n_errs, n_errs == 1 ? "" : "s", |
1448 | n_warns, n_warns == 1 ? "" : "s"); | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | #ifdef OBJ_VMS | |
1453 | /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call | |
1454 | a routine to check for the definition of the procedure "_main", | |
efaf0ba4 | 1455 | and if so -- fix it up so that it can be program entry point. */ |
252b5132 | 1456 | vms_check_for_main (); |
efaf0ba4 | 1457 | #endif /* OBJ_VMS */ |
252b5132 RH |
1458 | |
1459 | /* From now on, we don't care about sub-segments. Build one frag chain | |
1460 | for each segment. Linked thru fr_next. */ | |
1461 | ||
252b5132 RH |
1462 | /* Remove the sections created by gas for its own purposes. */ |
1463 | { | |
252b5132 RH |
1464 | int i; |
1465 | ||
5daa8fe7 L |
1466 | bfd_section_list_remove (stdoutput, reg_section); |
1467 | bfd_section_list_remove (stdoutput, expr_section); | |
1468 | stdoutput->section_count -= 2; | |
252b5132 RH |
1469 | i = 0; |
1470 | bfd_map_over_sections (stdoutput, renumber_sections, &i); | |
1471 | } | |
1472 | ||
1473 | bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0); | |
252b5132 RH |
1474 | |
1475 | /* We have two segments. If user gave -R flag, then we must put the | |
1476 | data frags into the text segment. Do this before relaxing so | |
1477 | we know to take advantage of -R and make shorter addresses. */ | |
252b5132 RH |
1478 | if (flag_readonly_data_in_text) |
1479 | { | |
1480 | merge_data_into_text (); | |
1481 | } | |
252b5132 | 1482 | |
32638454 | 1483 | rsi.pass = 0; |
e46d99eb AM |
1484 | while (1) |
1485 | { | |
aacb5251 HPN |
1486 | #ifndef WORKING_DOT_WORD |
1487 | /* We need to reset the markers in the broken word list and | |
1488 | associated frags between calls to relax_segment (via | |
1489 | relax_seg). Since the broken word list is global, we do it | |
1490 | once per round, rather than locally in relax_segment for each | |
1491 | segment. */ | |
1492 | struct broken_word *brokp; | |
1493 | ||
1494 | for (brokp = broken_words; | |
1495 | brokp != (struct broken_word *) NULL; | |
1496 | brokp = brokp->next_broken_word) | |
1497 | { | |
1498 | brokp->added = 0; | |
1499 | ||
1500 | if (brokp->dispfrag != (fragS *) NULL | |
1501 | && brokp->dispfrag->fr_type == rs_broken_word) | |
1502 | brokp->dispfrag->fr_subtype = 0; | |
1503 | } | |
1504 | #endif | |
1505 | ||
32638454 AM |
1506 | rsi.changed = 0; |
1507 | bfd_map_over_sections (stdoutput, relax_seg, &rsi); | |
1508 | rsi.pass++; | |
1509 | if (!rsi.changed) | |
e46d99eb AM |
1510 | break; |
1511 | } | |
e027f3e8 | 1512 | |
87548816 NC |
1513 | /* Note - Most ports will use the default value of |
1514 | TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force | |
1515 | local symbols to be resolved, removing their frag information. | |
1516 | Some ports however, will not have finished relaxing all of | |
1517 | their frags and will still need the local symbol frag | |
1518 | information. These ports can set | |
1519 | TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */ | |
1520 | finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG; | |
1521 | ||
e46d99eb | 1522 | bfd_map_over_sections (stdoutput, size_seg, (char *) 0); |
e46d99eb | 1523 | |
38b87a1b NC |
1524 | /* Relaxation has completed. Freeze all syms. */ |
1525 | finalize_syms = 1; | |
1526 | ||
e0001a05 NC |
1527 | #ifdef md_post_relax_hook |
1528 | md_post_relax_hook; | |
1529 | #endif | |
1530 | ||
252b5132 RH |
1531 | #ifndef WORKING_DOT_WORD |
1532 | { | |
1533 | struct broken_word *lie; | |
1534 | struct broken_word **prevP; | |
1535 | ||
1536 | prevP = &broken_words; | |
1537 | for (lie = broken_words; lie; lie = lie->next_broken_word) | |
1538 | if (!lie->added) | |
1539 | { | |
1540 | expressionS exp; | |
1541 | ||
1542 | subseg_change (lie->seg, lie->subseg); | |
1543 | exp.X_op = O_subtract; | |
1544 | exp.X_add_symbol = lie->add; | |
1545 | exp.X_op_symbol = lie->sub; | |
1546 | exp.X_add_number = lie->addnum; | |
252b5132 RH |
1547 | #ifdef TC_CONS_FIX_NEW |
1548 | TC_CONS_FIX_NEW (lie->frag, | |
efaf0ba4 NC |
1549 | lie->word_goes_here - lie->frag->fr_literal, |
1550 | 2, &exp); | |
252b5132 RH |
1551 | #else |
1552 | fix_new_exp (lie->frag, | |
1553 | lie->word_goes_here - lie->frag->fr_literal, | |
1554 | 2, &exp, 0, BFD_RELOC_16); | |
1555 | #endif | |
252b5132 RH |
1556 | *prevP = lie->next_broken_word; |
1557 | } | |
1558 | else | |
1559 | prevP = &(lie->next_broken_word); | |
1560 | ||
1561 | for (lie = broken_words; lie;) | |
1562 | { | |
1563 | struct broken_word *untruth; | |
1564 | char *table_ptr; | |
1565 | addressT table_addr; | |
1566 | addressT from_addr, to_addr; | |
1567 | int n, m; | |
1568 | ||
1569 | subseg_change (lie->seg, lie->subseg); | |
1570 | fragP = lie->dispfrag; | |
1571 | ||
1572 | /* Find out how many broken_words go here. */ | |
1573 | n = 0; | |
efaf0ba4 NC |
1574 | for (untruth = lie; |
1575 | untruth && untruth->dispfrag == fragP; | |
1576 | untruth = untruth->next_broken_word) | |
252b5132 RH |
1577 | if (untruth->added == 1) |
1578 | n++; | |
1579 | ||
1580 | table_ptr = lie->dispfrag->fr_opcode; | |
efaf0ba4 NC |
1581 | table_addr = (lie->dispfrag->fr_address |
1582 | + (table_ptr - lie->dispfrag->fr_literal)); | |
252b5132 RH |
1583 | /* Create the jump around the long jumps. This is a short |
1584 | jump from table_ptr+0 to table_ptr+n*long_jump_size. */ | |
1585 | from_addr = table_addr; | |
1586 | to_addr = table_addr + md_short_jump_size + n * md_long_jump_size; | |
efaf0ba4 NC |
1587 | md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, |
1588 | lie->add); | |
252b5132 RH |
1589 | table_ptr += md_short_jump_size; |
1590 | table_addr += md_short_jump_size; | |
1591 | ||
efaf0ba4 NC |
1592 | for (m = 0; |
1593 | lie && lie->dispfrag == fragP; | |
1594 | m++, lie = lie->next_broken_word) | |
252b5132 RH |
1595 | { |
1596 | if (lie->added == 2) | |
1597 | continue; | |
efaf0ba4 NC |
1598 | /* Patch the jump table. */ |
1599 | /* This is the offset from ??? to table_ptr+0. */ | |
252b5132 | 1600 | to_addr = table_addr - S_GET_VALUE (lie->sub); |
753f6b12 HPN |
1601 | #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD |
1602 | TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie); | |
252b5132 RH |
1603 | #endif |
1604 | md_number_to_chars (lie->word_goes_here, to_addr, 2); | |
efaf0ba4 NC |
1605 | for (untruth = lie->next_broken_word; |
1606 | untruth && untruth->dispfrag == fragP; | |
1607 | untruth = untruth->next_broken_word) | |
252b5132 RH |
1608 | { |
1609 | if (untruth->use_jump == lie) | |
1610 | md_number_to_chars (untruth->word_goes_here, to_addr, 2); | |
1611 | } | |
1612 | ||
efaf0ba4 NC |
1613 | /* Install the long jump. */ |
1614 | /* This is a long jump from table_ptr+0 to the final target. */ | |
252b5132 RH |
1615 | from_addr = table_addr; |
1616 | to_addr = S_GET_VALUE (lie->add) + lie->addnum; | |
efaf0ba4 NC |
1617 | md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, |
1618 | lie->add); | |
252b5132 RH |
1619 | table_ptr += md_long_jump_size; |
1620 | table_addr += md_long_jump_size; | |
1621 | } | |
1622 | } | |
1623 | } | |
efaf0ba4 | 1624 | #endif /* not WORKING_DOT_WORD */ |
252b5132 | 1625 | |
252b5132 RH |
1626 | /* Resolve symbol values. This needs to be done before processing |
1627 | the relocations. */ | |
1628 | if (symbol_rootP) | |
1629 | { | |
1630 | symbolS *symp; | |
1631 | ||
1632 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) | |
6386f3a7 | 1633 | resolve_symbol_value (symp); |
252b5132 | 1634 | } |
49309057 | 1635 | resolve_local_symbol_values (); |
252b5132 RH |
1636 | |
1637 | PROGRESS (1); | |
1638 | ||
1639 | #ifdef tc_frob_file_before_adjust | |
1640 | tc_frob_file_before_adjust (); | |
1641 | #endif | |
1642 | #ifdef obj_frob_file_before_adjust | |
1643 | obj_frob_file_before_adjust (); | |
1644 | #endif | |
1645 | ||
efaf0ba4 | 1646 | bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0); |
252b5132 | 1647 | |
a161fe53 AM |
1648 | #ifdef tc_frob_file_before_fix |
1649 | tc_frob_file_before_fix (); | |
1650 | #endif | |
1651 | #ifdef obj_frob_file_before_fix | |
1652 | obj_frob_file_before_fix (); | |
1653 | #endif | |
1654 | ||
1655 | bfd_map_over_sections (stdoutput, fix_segment, (char *) 0); | |
1656 | ||
252b5132 RH |
1657 | /* Set up symbol table, and write it out. */ |
1658 | if (symbol_rootP) | |
1659 | { | |
1660 | symbolS *symp; | |
aaac53f5 | 1661 | bfd_boolean skip_next_symbol = FALSE; |
252b5132 RH |
1662 | |
1663 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) | |
1664 | { | |
1665 | int punt = 0; | |
1666 | const char *name; | |
1667 | ||
aaac53f5 HPN |
1668 | if (skip_next_symbol) |
1669 | { | |
1670 | /* Don't do anything besides moving the value of the | |
1671 | symbol from the GAS value-field to the BFD value-field. */ | |
1672 | symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp); | |
1673 | skip_next_symbol = FALSE; | |
1674 | continue; | |
1675 | } | |
1676 | ||
49309057 | 1677 | if (symbol_mri_common_p (symp)) |
252b5132 RH |
1678 | { |
1679 | if (S_IS_EXTERNAL (symp)) | |
1680 | as_bad (_("%s: global symbols not supported in common sections"), | |
1681 | S_GET_NAME (symp)); | |
1682 | symbol_remove (symp, &symbol_rootP, &symbol_lastP); | |
1683 | continue; | |
1684 | } | |
1685 | ||
1686 | name = S_GET_NAME (symp); | |
1687 | if (name) | |
1688 | { | |
efaf0ba4 NC |
1689 | const char *name2 = |
1690 | decode_local_label_name ((char *) S_GET_NAME (symp)); | |
252b5132 RH |
1691 | /* They only differ if `name' is a fb or dollar local |
1692 | label name. */ | |
1693 | if (name2 != name && ! S_IS_DEFINED (symp)) | |
0e389e77 | 1694 | as_bad (_("local label `%s' is not defined"), name2); |
252b5132 RH |
1695 | } |
1696 | ||
1697 | /* Do it again, because adjust_reloc_syms might introduce | |
1698 | more symbols. They'll probably only be section symbols, | |
1699 | but they'll still need to have the values computed. */ | |
6386f3a7 | 1700 | resolve_symbol_value (symp); |
252b5132 RH |
1701 | |
1702 | /* Skip symbols which were equated to undefined or common | |
1703 | symbols. */ | |
06e77878 AO |
1704 | if (symbol_equated_reloc_p (symp) |
1705 | || S_IS_WEAKREFR (symp)) | |
252b5132 | 1706 | { |
60938e80 L |
1707 | const char *name = S_GET_NAME (symp); |
1708 | if (S_IS_COMMON (symp) | |
c9cd7160 | 1709 | && !TC_FAKE_LABEL (name) |
06e77878 | 1710 | && !S_IS_WEAKREFR (symp) |
60938e80 L |
1711 | && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp))) |
1712 | { | |
1713 | expressionS *e = symbol_get_value_expression (symp); | |
1714 | as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"), | |
1715 | name, S_GET_NAME (e->X_add_symbol)); | |
1716 | } | |
252b5132 RH |
1717 | symbol_remove (symp, &symbol_rootP, &symbol_lastP); |
1718 | continue; | |
1719 | } | |
1720 | ||
252b5132 RH |
1721 | #ifdef obj_frob_symbol |
1722 | obj_frob_symbol (symp, punt); | |
1723 | #endif | |
1724 | #ifdef tc_frob_symbol | |
49309057 | 1725 | if (! punt || symbol_used_in_reloc_p (symp)) |
252b5132 RH |
1726 | tc_frob_symbol (symp, punt); |
1727 | #endif | |
1728 | ||
1729 | /* If we don't want to keep this symbol, splice it out of | |
1730 | the chain now. If EMIT_SECTION_SYMBOLS is 0, we never | |
1731 | want section symbols. Otherwise, we skip local symbols | |
1732 | and symbols that the frob_symbol macros told us to punt, | |
1733 | but we keep such symbols if they are used in relocs. */ | |
a161fe53 AM |
1734 | if (symp == abs_section_sym |
1735 | || (! EMIT_SECTION_SYMBOLS | |
1736 | && symbol_section_p (symp)) | |
e97b3f28 | 1737 | /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always |
252b5132 RH |
1738 | opposites. Sometimes the former checks flags and the |
1739 | latter examines the name... */ | |
e97b3f28 | 1740 | || (!S_IS_EXTERNAL (symp) |
461b725f AO |
1741 | && (punt || S_IS_LOCAL (symp) || |
1742 | (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp))) | |
49309057 | 1743 | && ! symbol_used_in_reloc_p (symp))) |
252b5132 RH |
1744 | { |
1745 | symbol_remove (symp, &symbol_rootP, &symbol_lastP); | |
efaf0ba4 | 1746 | |
252b5132 RH |
1747 | /* After symbol_remove, symbol_next(symp) still returns |
1748 | the one that came after it in the chain. So we don't | |
1749 | need to do any extra cleanup work here. */ | |
252b5132 RH |
1750 | continue; |
1751 | } | |
1752 | ||
1753 | /* Make sure we really got a value for the symbol. */ | |
49309057 | 1754 | if (! symbol_resolved_p (symp)) |
252b5132 | 1755 | { |
0e389e77 | 1756 | as_bad (_("can't resolve value for symbol `%s'"), |
252b5132 | 1757 | S_GET_NAME (symp)); |
49309057 | 1758 | symbol_mark_resolved (symp); |
252b5132 RH |
1759 | } |
1760 | ||
1761 | /* Set the value into the BFD symbol. Up til now the value | |
1762 | has only been kept in the gas symbolS struct. */ | |
49309057 | 1763 | symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp); |
aaac53f5 HPN |
1764 | |
1765 | /* A warning construct is a warning symbol followed by the | |
1766 | symbol warned about. Don't let anything object-format or | |
1767 | target-specific muck with it; it's ready for output. */ | |
1768 | if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) | |
1769 | skip_next_symbol = TRUE; | |
252b5132 RH |
1770 | } |
1771 | } | |
1772 | ||
1773 | PROGRESS (1); | |
1774 | ||
1775 | /* Now do any format-specific adjustments to the symbol table, such | |
1776 | as adding file symbols. */ | |
1777 | #ifdef tc_adjust_symtab | |
1778 | tc_adjust_symtab (); | |
1779 | #endif | |
1780 | #ifdef obj_adjust_symtab | |
1781 | obj_adjust_symtab (); | |
1782 | #endif | |
1783 | ||
1784 | /* Now that all the sizes are known, and contents correct, we can | |
1785 | start writing to the file. */ | |
1786 | set_symtab (); | |
1787 | ||
1788 | /* If *_frob_file changes the symbol value at this point, it is | |
1789 | responsible for moving the changed value into symp->bsym->value | |
1790 | as well. Hopefully all symbol value changing can be done in | |
1791 | *_frob_symbol. */ | |
1792 | #ifdef tc_frob_file | |
1793 | tc_frob_file (); | |
1794 | #endif | |
1795 | #ifdef obj_frob_file | |
1796 | obj_frob_file (); | |
1797 | #endif | |
1798 | ||
1799 | bfd_map_over_sections (stdoutput, write_relocs, (char *) 0); | |
1800 | ||
1801 | #ifdef tc_frob_file_after_relocs | |
1802 | tc_frob_file_after_relocs (); | |
1803 | #endif | |
1804 | #ifdef obj_frob_file_after_relocs | |
1805 | obj_frob_file_after_relocs (); | |
1806 | #endif | |
1807 | ||
1808 | bfd_map_over_sections (stdoutput, write_contents, (char *) 0); | |
252b5132 | 1809 | } |
252b5132 RH |
1810 | |
1811 | #ifdef TC_GENERIC_RELAX_TABLE | |
252b5132 RH |
1812 | /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */ |
1813 | ||
1814 | long | |
d7342424 | 1815 | relax_frag (segT segment, fragS *fragP, long stretch) |
252b5132 RH |
1816 | { |
1817 | const relax_typeS *this_type; | |
1818 | const relax_typeS *start_type; | |
1819 | relax_substateT next_state; | |
1820 | relax_substateT this_state; | |
685736be | 1821 | offsetT growth; |
38686296 AM |
1822 | offsetT aim; |
1823 | addressT target; | |
1824 | addressT address; | |
1825 | symbolS *symbolP; | |
1826 | const relax_typeS *table; | |
1827 | ||
1828 | target = fragP->fr_offset; | |
1829 | address = fragP->fr_address; | |
1830 | table = TC_GENERIC_RELAX_TABLE; | |
252b5132 RH |
1831 | this_state = fragP->fr_subtype; |
1832 | start_type = this_type = table + this_state; | |
38686296 | 1833 | symbolP = fragP->fr_symbol; |
252b5132 RH |
1834 | |
1835 | if (symbolP) | |
1836 | { | |
c842b53a ILT |
1837 | fragS *sym_frag; |
1838 | ||
1839 | sym_frag = symbol_get_frag (symbolP); | |
1840 | ||
252b5132 | 1841 | #ifndef DIFF_EXPR_OK |
c842b53a | 1842 | know (sym_frag != NULL); |
252b5132 | 1843 | #endif |
a161fe53 | 1844 | know (S_GET_SEGMENT (symbolP) != absolute_section |
c842b53a | 1845 | || sym_frag == &zero_address_frag); |
ac62c346 | 1846 | target += S_GET_VALUE (symbolP); |
252b5132 RH |
1847 | |
1848 | /* If frag has yet to be reached on this pass, | |
1849 | assume it will move by STRETCH just as we did. | |
1850 | If this is not so, it will be because some frag | |
38686296 | 1851 | between grows, and that will force another pass. */ |
252b5132 | 1852 | |
c842b53a | 1853 | if (stretch != 0 |
38686296 AM |
1854 | && sym_frag->relax_marker != fragP->relax_marker |
1855 | && S_GET_SEGMENT (symbolP) == segment) | |
252b5132 RH |
1856 | { |
1857 | target += stretch; | |
1858 | } | |
1859 | } | |
1860 | ||
1861 | aim = target - address - fragP->fr_fix; | |
1862 | #ifdef TC_PCREL_ADJUST | |
efaf0ba4 NC |
1863 | /* Currently only the ns32k family needs this. */ |
1864 | aim += TC_PCREL_ADJUST (fragP); | |
efaf0ba4 | 1865 | #endif |
59c871b4 BE |
1866 | |
1867 | #ifdef md_prepare_relax_scan | |
1868 | /* Formerly called M68K_AIM_KLUDGE. */ | |
252b5132 RH |
1869 | md_prepare_relax_scan (fragP, address, aim, this_state, this_type); |
1870 | #endif | |
1871 | ||
1872 | if (aim < 0) | |
1873 | { | |
efaf0ba4 | 1874 | /* Look backwards. */ |
252b5132 RH |
1875 | for (next_state = this_type->rlx_more; next_state;) |
1876 | if (aim >= this_type->rlx_backward) | |
1877 | next_state = 0; | |
1878 | else | |
1879 | { | |
efaf0ba4 | 1880 | /* Grow to next state. */ |
252b5132 RH |
1881 | this_state = next_state; |
1882 | this_type = table + this_state; | |
1883 | next_state = this_type->rlx_more; | |
1884 | } | |
1885 | } | |
1886 | else | |
1887 | { | |
efaf0ba4 | 1888 | /* Look forwards. */ |
252b5132 RH |
1889 | for (next_state = this_type->rlx_more; next_state;) |
1890 | if (aim <= this_type->rlx_forward) | |
1891 | next_state = 0; | |
1892 | else | |
1893 | { | |
efaf0ba4 | 1894 | /* Grow to next state. */ |
252b5132 RH |
1895 | this_state = next_state; |
1896 | this_type = table + this_state; | |
1897 | next_state = this_type->rlx_more; | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | growth = this_type->rlx_length - start_type->rlx_length; | |
1902 | if (growth != 0) | |
1903 | fragP->fr_subtype = this_state; | |
1904 | return growth; | |
1905 | } | |
1906 | ||
efaf0ba4 | 1907 | #endif /* defined (TC_GENERIC_RELAX_TABLE) */ |
252b5132 RH |
1908 | |
1909 | /* Relax_align. Advance location counter to next address that has 'alignment' | |
1910 | lowest order bits all 0s, return size of adjustment made. */ | |
1911 | static relax_addressT | |
d7342424 KH |
1912 | relax_align (register relax_addressT address, /* Address now. */ |
1913 | register int alignment /* Alignment (binary). */) | |
252b5132 RH |
1914 | { |
1915 | relax_addressT mask; | |
1916 | relax_addressT new_address; | |
1917 | ||
1918 | mask = ~((~0) << alignment); | |
1919 | new_address = (address + mask) & (~mask); | |
1920 | #ifdef LINKER_RELAXING_SHRINKS_ONLY | |
1921 | if (linkrelax) | |
1922 | /* We must provide lots of padding, so the linker can discard it | |
1923 | when needed. The linker will not add extra space, ever. */ | |
1924 | new_address += (1 << alignment); | |
1925 | #endif | |
1926 | return (new_address - address); | |
1927 | } | |
1928 | ||
efaf0ba4 NC |
1929 | /* Now we have a segment, not a crowd of sub-segments, we can make |
1930 | fr_address values. | |
58a77e41 | 1931 | |
efaf0ba4 | 1932 | Relax the frags. |
58a77e41 | 1933 | |
efaf0ba4 NC |
1934 | After this, all frags in this segment have addresses that are correct |
1935 | within the segment. Since segments live in different file addresses, | |
1936 | these frag addresses may not be the same as final object-file | |
1937 | addresses. */ | |
1938 | ||
e46d99eb | 1939 | int |
32638454 | 1940 | relax_segment (struct frag *segment_frag_root, segT segment, int pass) |
252b5132 | 1941 | { |
4111faa5 NC |
1942 | unsigned long frag_count; |
1943 | struct frag *fragP; | |
1944 | relax_addressT address; | |
e46d99eb AM |
1945 | int ret; |
1946 | ||
efaf0ba4 | 1947 | /* In case md_estimate_size_before_relax() wants to make fixSs. */ |
252b5132 RH |
1948 | subseg_change (segment, 0); |
1949 | ||
1950 | /* For each frag in segment: count and store (a 1st guess of) | |
1951 | fr_address. */ | |
1952 | address = 0; | |
4111faa5 NC |
1953 | for (frag_count = 0, fragP = segment_frag_root; |
1954 | fragP; | |
1955 | fragP = fragP->fr_next, frag_count ++) | |
252b5132 | 1956 | { |
38686296 | 1957 | fragP->relax_marker = 0; |
252b5132 RH |
1958 | fragP->fr_address = address; |
1959 | address += fragP->fr_fix; | |
1960 | ||
1961 | switch (fragP->fr_type) | |
1962 | { | |
1963 | case rs_fill: | |
1964 | address += fragP->fr_offset * fragP->fr_var; | |
1965 | break; | |
1966 | ||
1967 | case rs_align: | |
1968 | case rs_align_code: | |
0a9ef439 | 1969 | case rs_align_test: |
252b5132 RH |
1970 | { |
1971 | addressT offset = relax_align (address, (int) fragP->fr_offset); | |
1972 | ||
1973 | if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype) | |
1974 | offset = 0; | |
1975 | ||
1976 | if (offset % fragP->fr_var != 0) | |
1977 | { | |
54d3cad9 AM |
1978 | as_bad_where (fragP->fr_file, fragP->fr_line, |
1979 | _("alignment padding (%lu bytes) not a multiple of %ld"), | |
1980 | (unsigned long) offset, (long) fragP->fr_var); | |
252b5132 RH |
1981 | offset -= (offset % fragP->fr_var); |
1982 | } | |
1983 | ||
1984 | address += offset; | |
1985 | } | |
1986 | break; | |
1987 | ||
1988 | case rs_org: | |
1989 | case rs_space: | |
1990 | /* Assume .org is nugatory. It will grow with 1st relax. */ | |
1991 | break; | |
1992 | ||
1993 | case rs_machine_dependent: | |
e0890092 AM |
1994 | /* If fr_symbol is an expression, this call to |
1995 | resolve_symbol_value sets up the correct segment, which will | |
1996 | likely be needed in md_estimate_size_before_relax. */ | |
1997 | if (fragP->fr_symbol) | |
1998 | resolve_symbol_value (fragP->fr_symbol); | |
1999 | ||
252b5132 RH |
2000 | address += md_estimate_size_before_relax (fragP, segment); |
2001 | break; | |
2002 | ||
2003 | #ifndef WORKING_DOT_WORD | |
efaf0ba4 | 2004 | /* Broken words don't concern us yet. */ |
252b5132 RH |
2005 | case rs_broken_word: |
2006 | break; | |
2007 | #endif | |
2008 | ||
2009 | case rs_leb128: | |
efaf0ba4 | 2010 | /* Initial guess is always 1; doing otherwise can result in |
252b5132 RH |
2011 | stable solutions that are larger than the minimum. */ |
2012 | address += fragP->fr_offset = 1; | |
2013 | break; | |
2014 | ||
2015 | case rs_cfa: | |
2016 | address += eh_frame_estimate_size_before_relax (fragP); | |
2017 | break; | |
2018 | ||
220e750f RH |
2019 | case rs_dwarf2dbg: |
2020 | address += dwarf2dbg_estimate_size_before_relax (fragP); | |
2021 | break; | |
2022 | ||
252b5132 RH |
2023 | default: |
2024 | BAD_CASE (fragP->fr_type); | |
2025 | break; | |
efaf0ba4 NC |
2026 | } |
2027 | } | |
252b5132 RH |
2028 | |
2029 | /* Do relax(). */ | |
2030 | { | |
4111faa5 | 2031 | unsigned long max_iterations; |
685736be | 2032 | offsetT stretch; /* May be any size, 0 or negative. */ |
efaf0ba4 NC |
2033 | /* Cumulative number of addresses we have relaxed this pass. |
2034 | We may have relaxed more than one address. */ | |
e46d99eb | 2035 | int stretched; /* Have we stretched on this pass? */ |
252b5132 RH |
2036 | /* This is 'cuz stretch may be zero, when, in fact some piece of code |
2037 | grew, and another shrank. If a branch instruction doesn't fit anymore, | |
2038 | we could be scrod. */ | |
2039 | ||
4111faa5 NC |
2040 | /* We want to prevent going into an infinite loop where one frag grows |
2041 | depending upon the location of a symbol which is in turn moved by | |
2042 | the growing frag. eg: | |
2043 | ||
2044 | foo = . | |
2045 | .org foo+16 | |
2046 | foo = . | |
2047 | ||
2048 | So we dictate that this algorithm can be at most O2. */ | |
2049 | max_iterations = frag_count * frag_count; | |
2050 | /* Check for overflow. */ | |
2051 | if (max_iterations < frag_count) | |
2052 | max_iterations = frag_count; | |
2053 | ||
32638454 | 2054 | ret = 0; |
252b5132 RH |
2055 | do |
2056 | { | |
e46d99eb AM |
2057 | stretch = 0; |
2058 | stretched = 0; | |
58a77e41 | 2059 | |
252b5132 RH |
2060 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) |
2061 | { | |
685736be | 2062 | offsetT growth = 0; |
252b5132 RH |
2063 | addressT was_address; |
2064 | offsetT offset; | |
2065 | symbolS *symbolP; | |
2066 | ||
38686296 | 2067 | fragP->relax_marker ^= 1; |
252b5132 RH |
2068 | was_address = fragP->fr_address; |
2069 | address = fragP->fr_address += stretch; | |
2070 | symbolP = fragP->fr_symbol; | |
2071 | offset = fragP->fr_offset; | |
2072 | ||
2073 | switch (fragP->fr_type) | |
2074 | { | |
efaf0ba4 | 2075 | case rs_fill: /* .fill never relaxes. */ |
252b5132 RH |
2076 | growth = 0; |
2077 | break; | |
2078 | ||
2079 | #ifndef WORKING_DOT_WORD | |
2080 | /* JF: This is RMS's idea. I do *NOT* want to be blamed | |
2081 | for it I do not want to write it. I do not want to have | |
2082 | anything to do with it. This is not the proper way to | |
2083 | implement this misfeature. */ | |
2084 | case rs_broken_word: | |
2085 | { | |
2086 | struct broken_word *lie; | |
2087 | struct broken_word *untruth; | |
2088 | ||
2089 | /* Yes this is ugly (storing the broken_word pointer | |
2090 | in the symbol slot). Still, this whole chunk of | |
2091 | code is ugly, and I don't feel like doing anything | |
2092 | about it. Think of it as stubbornness in action. */ | |
2093 | growth = 0; | |
2094 | for (lie = (struct broken_word *) (fragP->fr_symbol); | |
2095 | lie && lie->dispfrag == fragP; | |
2096 | lie = lie->next_broken_word) | |
2097 | { | |
2098 | ||
2099 | if (lie->added) | |
2100 | continue; | |
2101 | ||
ac62c346 | 2102 | offset = (S_GET_VALUE (lie->add) |
252b5132 | 2103 | + lie->addnum |
ac62c346 | 2104 | - S_GET_VALUE (lie->sub)); |
252b5132 RH |
2105 | if (offset <= -32768 || offset >= 32767) |
2106 | { | |
2107 | if (flag_warn_displacement) | |
2108 | { | |
2109 | char buf[50]; | |
2110 | sprint_value (buf, (addressT) lie->addnum); | |
54d3cad9 AM |
2111 | as_warn_where (fragP->fr_file, fragP->fr_line, |
2112 | _(".word %s-%s+%s didn't fit"), | |
2113 | S_GET_NAME (lie->add), | |
2114 | S_GET_NAME (lie->sub), | |
2115 | buf); | |
252b5132 RH |
2116 | } |
2117 | lie->added = 1; | |
2118 | if (fragP->fr_subtype == 0) | |
2119 | { | |
2120 | fragP->fr_subtype++; | |
2121 | growth += md_short_jump_size; | |
2122 | } | |
2123 | for (untruth = lie->next_broken_word; | |
2124 | untruth && untruth->dispfrag == lie->dispfrag; | |
2125 | untruth = untruth->next_broken_word) | |
49309057 ILT |
2126 | if ((symbol_get_frag (untruth->add) |
2127 | == symbol_get_frag (lie->add)) | |
2128 | && (S_GET_VALUE (untruth->add) | |
2129 | == S_GET_VALUE (lie->add))) | |
252b5132 RH |
2130 | { |
2131 | untruth->added = 2; | |
2132 | untruth->use_jump = lie; | |
2133 | } | |
2134 | growth += md_long_jump_size; | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | break; | |
efaf0ba4 | 2139 | } /* case rs_broken_word */ |
252b5132 RH |
2140 | #endif |
2141 | case rs_align: | |
2142 | case rs_align_code: | |
0a9ef439 | 2143 | case rs_align_test: |
252b5132 RH |
2144 | { |
2145 | addressT oldoff, newoff; | |
2146 | ||
2147 | oldoff = relax_align (was_address + fragP->fr_fix, | |
2148 | (int) offset); | |
2149 | newoff = relax_align (address + fragP->fr_fix, | |
2150 | (int) offset); | |
2151 | ||
2152 | if (fragP->fr_subtype != 0) | |
2153 | { | |
2154 | if (oldoff > fragP->fr_subtype) | |
2155 | oldoff = 0; | |
2156 | if (newoff > fragP->fr_subtype) | |
2157 | newoff = 0; | |
2158 | } | |
2159 | ||
2160 | growth = newoff - oldoff; | |
2161 | } | |
2162 | break; | |
2163 | ||
2164 | case rs_org: | |
2165 | { | |
e46d99eb AM |
2166 | addressT target = offset; |
2167 | addressT after; | |
252b5132 RH |
2168 | |
2169 | if (symbolP) | |
2170 | { | |
6e917903 TW |
2171 | /* Convert from an actual address to an octet offset |
2172 | into the section. Here it is assumed that the | |
2173 | section's VMA is zero, and can omit subtracting it | |
2174 | from the symbol's value to get the address offset. */ | |
c9dea48b | 2175 | know (S_GET_SEGMENT (symbolP)->vma == 0); |
6e917903 | 2176 | target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE; |
ac62c346 | 2177 | } |
252b5132 RH |
2178 | |
2179 | know (fragP->fr_next); | |
2180 | after = fragP->fr_next->fr_address; | |
2181 | growth = target - after; | |
2182 | if (growth < 0) | |
2183 | { | |
32638454 AM |
2184 | growth = 0; |
2185 | ||
2186 | /* Don't error on first few frag relax passes. | |
2187 | The symbol might be an expression involving | |
2188 | symbol values from other sections. If those | |
2189 | sections have not yet been processed their | |
2190 | frags will all have zero addresses, so we | |
2191 | will calculate incorrect values for them. The | |
2192 | number of passes we allow before giving an | |
2193 | error is somewhat arbitrary. It should be at | |
2194 | least one, with larger values requiring | |
2195 | increasingly contrived dependencies between | |
2196 | frags to trigger a false error. */ | |
2197 | if (pass < 2) | |
2198 | { | |
2199 | /* Force another pass. */ | |
2200 | ret = 1; | |
2201 | break; | |
2202 | } | |
2203 | ||
252b5132 RH |
2204 | /* Growth may be negative, but variable part of frag |
2205 | cannot have fewer than 0 chars. That is, we can't | |
efaf0ba4 | 2206 | .org backwards. */ |
14ad458a | 2207 | as_bad_where (fragP->fr_file, fragP->fr_line, |
685736be | 2208 | _("attempt to move .org backwards")); |
14ad458a ILT |
2209 | |
2210 | /* We've issued an error message. Change the | |
2211 | frag to avoid cascading errors. */ | |
2212 | fragP->fr_type = rs_align; | |
2213 | fragP->fr_subtype = 0; | |
2214 | fragP->fr_offset = 0; | |
e750405d | 2215 | fragP->fr_fix = after - was_address; |
32638454 | 2216 | break; |
252b5132 RH |
2217 | } |
2218 | ||
efaf0ba4 NC |
2219 | /* This is an absolute growth factor */ |
2220 | growth -= stretch; | |
252b5132 RH |
2221 | break; |
2222 | } | |
2223 | ||
2224 | case rs_space: | |
54d3cad9 | 2225 | growth = 0; |
252b5132 RH |
2226 | if (symbolP) |
2227 | { | |
766c03c9 AM |
2228 | offsetT amount; |
2229 | ||
2230 | amount = S_GET_VALUE (symbolP); | |
9e40345d | 2231 | if (S_GET_SEGMENT (symbolP) != absolute_section |
252b5132 RH |
2232 | || S_IS_COMMON (symbolP) |
2233 | || ! S_IS_DEFINED (symbolP)) | |
252b5132 | 2234 | { |
54d3cad9 AM |
2235 | as_bad_where (fragP->fr_file, fragP->fr_line, |
2236 | _(".space specifies non-absolute value")); | |
2237 | /* Prevent repeat of this error message. */ | |
2238 | fragP->fr_symbol = 0; | |
2239 | } | |
2240 | else if (amount < 0) | |
2241 | { | |
32638454 AM |
2242 | /* Don't error on first few frag relax passes. |
2243 | See rs_org comment for a longer explanation. */ | |
2244 | if (pass < 2) | |
2245 | { | |
2246 | ret = 1; | |
2247 | break; | |
2248 | } | |
2249 | ||
54d3cad9 AM |
2250 | as_warn_where (fragP->fr_file, fragP->fr_line, |
2251 | _(".space or .fill with negative value, ignored")); | |
766c03c9 | 2252 | fragP->fr_symbol = 0; |
252b5132 | 2253 | } |
54d3cad9 | 2254 | else |
050be34e | 2255 | growth = (was_address + fragP->fr_fix + amount |
54d3cad9 | 2256 | - fragP->fr_next->fr_address); |
252b5132 | 2257 | } |
252b5132 RH |
2258 | break; |
2259 | ||
2260 | case rs_machine_dependent: | |
2261 | #ifdef md_relax_frag | |
c842b53a | 2262 | growth = md_relax_frag (segment, fragP, stretch); |
252b5132 RH |
2263 | #else |
2264 | #ifdef TC_GENERIC_RELAX_TABLE | |
2265 | /* The default way to relax a frag is to look through | |
2266 | TC_GENERIC_RELAX_TABLE. */ | |
c842b53a | 2267 | growth = relax_frag (segment, fragP, stretch); |
efaf0ba4 | 2268 | #endif /* TC_GENERIC_RELAX_TABLE */ |
252b5132 RH |
2269 | #endif |
2270 | break; | |
2271 | ||
2272 | case rs_leb128: | |
2273 | { | |
2274 | valueT value; | |
685736be | 2275 | offsetT size; |
252b5132 | 2276 | |
6386f3a7 | 2277 | value = resolve_symbol_value (fragP->fr_symbol); |
252b5132 RH |
2278 | size = sizeof_leb128 (value, fragP->fr_subtype); |
2279 | growth = size - fragP->fr_offset; | |
2280 | fragP->fr_offset = size; | |
2281 | } | |
2282 | break; | |
2283 | ||
2284 | case rs_cfa: | |
2285 | growth = eh_frame_relax_frag (fragP); | |
2286 | break; | |
2287 | ||
220e750f RH |
2288 | case rs_dwarf2dbg: |
2289 | growth = dwarf2dbg_relax_frag (fragP); | |
2290 | break; | |
2291 | ||
252b5132 RH |
2292 | default: |
2293 | BAD_CASE (fragP->fr_type); | |
2294 | break; | |
2295 | } | |
2296 | if (growth) | |
2297 | { | |
2298 | stretch += growth; | |
e46d99eb | 2299 | stretched = 1; |
252b5132 | 2300 | } |
4111faa5 | 2301 | } |
252b5132 | 2302 | } |
4111faa5 NC |
2303 | /* Until nothing further to relax. */ |
2304 | while (stretched && -- max_iterations); | |
2305 | ||
2306 | if (stretched) | |
2307 | as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"), | |
2308 | segment_name (segment)); | |
2309 | } | |
252b5132 | 2310 | |
e46d99eb AM |
2311 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) |
2312 | if (fragP->last_fr_address != fragP->fr_address) | |
2313 | { | |
2314 | fragP->last_fr_address = fragP->fr_address; | |
2315 | ret = 1; | |
2316 | } | |
2317 | return ret; | |
efaf0ba4 | 2318 | } |
252b5132 | 2319 | |
252b5132 | 2320 | void |
d7342424 | 2321 | number_to_chars_bigendian (char *buf, valueT val, int n) |
252b5132 | 2322 | { |
937149dd | 2323 | if (n <= 0) |
252b5132 RH |
2324 | abort (); |
2325 | while (n--) | |
2326 | { | |
2327 | buf[n] = val & 0xff; | |
2328 | val >>= 8; | |
2329 | } | |
2330 | } | |
2331 | ||
2332 | void | |
d7342424 | 2333 | number_to_chars_littleendian (char *buf, valueT val, int n) |
252b5132 | 2334 | { |
937149dd | 2335 | if (n <= 0) |
252b5132 RH |
2336 | abort (); |
2337 | while (n--) | |
2338 | { | |
2339 | *buf++ = val & 0xff; | |
2340 | val >>= 8; | |
2341 | } | |
2342 | } | |
2343 | ||
2344 | void | |
d7342424 | 2345 | write_print_statistics (FILE *file) |
252b5132 | 2346 | { |
6d4d30bb | 2347 | fprintf (file, "fixups: %d\n", n_fixups); |
252b5132 RH |
2348 | } |
2349 | ||
efaf0ba4 | 2350 | /* For debugging. */ |
252b5132 RH |
2351 | extern int indent_level; |
2352 | ||
2353 | void | |
d7342424 | 2354 | print_fixup (fixS *fixp) |
252b5132 RH |
2355 | { |
2356 | indent_level = 1; | |
2357 | fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line); | |
2358 | if (fixp->fx_pcrel) | |
2359 | fprintf (stderr, " pcrel"); | |
2360 | if (fixp->fx_pcrel_adjust) | |
2361 | fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust); | |
2362 | if (fixp->fx_im_disp) | |
2363 | { | |
2364 | #ifdef TC_NS32K | |
2365 | fprintf (stderr, " im_disp=%d", fixp->fx_im_disp); | |
2366 | #else | |
2367 | fprintf (stderr, " im_disp"); | |
2368 | #endif | |
2369 | } | |
2370 | if (fixp->fx_tcbit) | |
2371 | fprintf (stderr, " tcbit"); | |
2372 | if (fixp->fx_done) | |
2373 | fprintf (stderr, " done"); | |
2374 | fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx", | |
2375 | fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where, | |
2376 | (long) fixp->fx_offset, (long) fixp->fx_addnumber); | |
252b5132 RH |
2377 | fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type), |
2378 | fixp->fx_r_type); | |
252b5132 RH |
2379 | if (fixp->fx_addsy) |
2380 | { | |
2381 | fprintf (stderr, "\n +<"); | |
2382 | print_symbol_value_1 (stderr, fixp->fx_addsy); | |
2383 | fprintf (stderr, ">"); | |
2384 | } | |
2385 | if (fixp->fx_subsy) | |
2386 | { | |
2387 | fprintf (stderr, "\n -<"); | |
2388 | print_symbol_value_1 (stderr, fixp->fx_subsy); | |
2389 | fprintf (stderr, ">"); | |
2390 | } | |
2391 | fprintf (stderr, "\n"); | |
2392 | #ifdef TC_FIX_DATA_PRINT | |
2393 | TC_FIX_DATA_PRINT (stderr, fixp); | |
2394 | #endif | |
2395 | } |