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