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