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