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