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