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