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