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