Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for Intel 386 COFF files. |
7898deda | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
2b5c217d | 3 | 2000, 2001, 2002, 2003, 2004, 2007, 2008 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | Written by Cygnus Support. | |
6 | ||
977cdf5a | 7 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 8 | |
977cdf5a NC |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 11 | the Free Software Foundation; either version 3 of the License, or |
977cdf5a | 12 | (at your option) any later version. |
252b5132 | 13 | |
977cdf5a NC |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
252b5132 | 18 | |
977cdf5a NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
22 | MA 02110-1301, USA. */ | |
252b5132 | 23 | |
252b5132 | 24 | #include "sysdep.h" |
3db64b00 | 25 | #include "bfd.h" |
252b5132 RH |
26 | #include "libbfd.h" |
27 | ||
28 | #include "coff/i386.h" | |
29 | ||
30 | #include "coff/internal.h" | |
31 | ||
32 | #ifdef COFF_WITH_PE | |
33 | #include "coff/pe.h" | |
34 | #endif | |
35 | ||
36 | #ifdef COFF_GO32_EXE | |
37 | #include "coff/go32exe.h" | |
38 | #endif | |
39 | ||
2b5c217d NC |
40 | #ifndef bfd_pe_print_pdata |
41 | #define bfd_pe_print_pdata NULL | |
42 | #endif | |
43 | ||
252b5132 RH |
44 | #include "libcoff.h" |
45 | ||
b7be1db6 | 46 | static bfd_reloc_status_type coff_i386_reloc |
252b5132 RH |
47 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
48 | static reloc_howto_type *coff_i386_rtype_to_howto | |
49 | PARAMS ((bfd *, asection *, struct internal_reloc *, | |
50 | struct coff_link_hash_entry *, struct internal_syment *, | |
252b5132 | 51 | bfd_vma *)); |
00692651 ILT |
52 | static reloc_howto_type *coff_i386_reloc_type_lookup |
53 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
252b5132 RH |
54 | |
55 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2) | |
56 | /* The page size is a guess based on ELF. */ | |
57 | ||
58 | #define COFF_PAGE_SIZE 0x1000 | |
59 | ||
60 | /* For some reason when using i386 COFF the value stored in the .text | |
61 | section for a reference to a common symbol is the value itself plus | |
62 | any desired offset. Ian Taylor, Cygnus Support. */ | |
63 | ||
1049f94e | 64 | /* If we are producing relocatable output, we need to do some |
252b5132 RH |
65 | adjustments to the object file that are not done by the |
66 | bfd_perform_relocation function. This function is called by every | |
67 | reloc type to make any required adjustments. */ | |
68 | ||
69 | static bfd_reloc_status_type | |
70 | coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd, | |
71 | error_message) | |
72 | bfd *abfd; | |
73 | arelent *reloc_entry; | |
74 | asymbol *symbol; | |
75 | PTR data; | |
5f771d47 | 76 | asection *input_section ATTRIBUTE_UNUSED; |
252b5132 | 77 | bfd *output_bfd; |
5f771d47 | 78 | char **error_message ATTRIBUTE_UNUSED; |
252b5132 RH |
79 | { |
80 | symvalue diff; | |
81 | ||
689be717 | 82 | #ifndef COFF_WITH_PE |
252b5132 RH |
83 | if (output_bfd == (bfd *) NULL) |
84 | return bfd_reloc_continue; | |
689be717 | 85 | #endif |
252b5132 RH |
86 | |
87 | if (bfd_is_com_section (symbol->section)) | |
88 | { | |
89 | #ifndef COFF_WITH_PE | |
90 | /* We are relocating a common symbol. The current value in the | |
91 | object file is ORIG + OFFSET, where ORIG is the value of the | |
92 | common symbol as seen by the object file when it was compiled | |
93 | (this may be zero if the symbol was undefined) and OFFSET is | |
94 | the offset into the common symbol (normally zero, but may be | |
95 | non-zero when referring to a field in a common structure). | |
96 | ORIG is the negative of reloc_entry->addend, which is set by | |
97 | the CALC_ADDEND macro below. We want to replace the value in | |
98 | the object file with NEW + OFFSET, where NEW is the value of | |
99 | the common symbol which we are going to put in the final | |
100 | object file. NEW is symbol->value. */ | |
101 | diff = symbol->value + reloc_entry->addend; | |
102 | #else | |
103 | /* In PE mode, we do not offset the common symbol. */ | |
104 | diff = reloc_entry->addend; | |
105 | #endif | |
106 | } | |
107 | else | |
108 | { | |
109 | /* For some reason bfd_perform_relocation always effectively | |
110 | ignores the addend for a COFF target when producing | |
1049f94e | 111 | relocatable output. This seems to be always wrong for 386 |
252b5132 | 112 | COFF, so we handle the addend here instead. */ |
689be717 L |
113 | #ifdef COFF_WITH_PE |
114 | if (output_bfd == (bfd *) NULL) | |
115 | { | |
116 | reloc_howto_type *howto = reloc_entry->howto; | |
117 | ||
118 | /* Although PC relative relocations are very similar between | |
119 | PE and non-PE formats, but they are off by 1 << howto->size | |
120 | bytes. For the external relocation, PE is very different | |
121 | from others. See md_apply_fix3 () in gas/config/tc-i386.c. | |
122 | When we link PE and non-PE object files together to | |
123 | generate a non-PE executable, we have to compensate it | |
124 | here. */ | |
82e51918 | 125 | if (howto->pc_relative && howto->pcrel_offset) |
689be717 | 126 | diff = -(1 << howto->size); |
977cdf5a NC |
127 | else if (symbol->flags & BSF_WEAK) |
128 | diff = reloc_entry->addend - symbol->value; | |
689be717 L |
129 | else |
130 | diff = -reloc_entry->addend; | |
131 | } | |
132 | else | |
133 | #endif | |
134 | diff = reloc_entry->addend; | |
252b5132 RH |
135 | } |
136 | ||
137 | #ifdef COFF_WITH_PE | |
138 | /* FIXME: How should this case be handled? */ | |
8e191bd3 ILT |
139 | if (reloc_entry->howto->type == R_IMAGEBASE |
140 | && output_bfd != NULL | |
141 | && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour) | |
00692651 | 142 | diff -= pe_data (output_bfd)->pe_opthdr.ImageBase; |
252b5132 RH |
143 | #endif |
144 | ||
145 | #define DOIT(x) \ | |
146 | x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask)) | |
147 | ||
148 | if (diff != 0) | |
149 | { | |
150 | reloc_howto_type *howto = reloc_entry->howto; | |
151 | unsigned char *addr = (unsigned char *) data + reloc_entry->address; | |
152 | ||
153 | switch (howto->size) | |
154 | { | |
155 | case 0: | |
156 | { | |
157 | char x = bfd_get_8 (abfd, addr); | |
158 | DOIT (x); | |
159 | bfd_put_8 (abfd, x, addr); | |
160 | } | |
161 | break; | |
162 | ||
163 | case 1: | |
164 | { | |
165 | short x = bfd_get_16 (abfd, addr); | |
166 | DOIT (x); | |
dc810e39 | 167 | bfd_put_16 (abfd, (bfd_vma) x, addr); |
252b5132 RH |
168 | } |
169 | break; | |
170 | ||
171 | case 2: | |
172 | { | |
173 | long x = bfd_get_32 (abfd, addr); | |
174 | DOIT (x); | |
dc810e39 | 175 | bfd_put_32 (abfd, (bfd_vma) x, addr); |
252b5132 RH |
176 | } |
177 | break; | |
178 | ||
179 | default: | |
180 | abort (); | |
181 | } | |
182 | } | |
183 | ||
184 | /* Now let bfd_perform_relocation finish everything up. */ | |
185 | return bfd_reloc_continue; | |
186 | } | |
187 | ||
188 | #ifdef COFF_WITH_PE | |
b34976b6 | 189 | /* Return TRUE if this relocation should appear in the output .reloc |
6fb72d08 ILT |
190 | section. */ |
191 | ||
b34976b6 | 192 | static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *)); |
6fb72d08 | 193 | |
b34976b6 | 194 | static bfd_boolean in_reloc_p (abfd, howto) |
60bcf0fa | 195 | bfd * abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
196 | reloc_howto_type *howto; |
197 | { | |
198 | return ! howto->pc_relative && howto->type != R_IMAGEBASE; | |
b7be1db6 | 199 | } |
6fb72d08 | 200 | #endif /* COFF_WITH_PE */ |
252b5132 RH |
201 | |
202 | #ifndef PCRELOFFSET | |
b34976b6 | 203 | #define PCRELOFFSET FALSE |
252b5132 RH |
204 | #endif |
205 | ||
b7be1db6 | 206 | static reloc_howto_type howto_table[] = |
252b5132 | 207 | { |
5f771d47 ILT |
208 | EMPTY_HOWTO (0), |
209 | EMPTY_HOWTO (1), | |
210 | EMPTY_HOWTO (2), | |
211 | EMPTY_HOWTO (3), | |
212 | EMPTY_HOWTO (4), | |
213 | EMPTY_HOWTO (5), | |
b7be1db6 AM |
214 | HOWTO (R_DIR32, /* type */ |
215 | 0, /* rightshift */ | |
216 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
217 | 32, /* bitsize */ | |
b34976b6 | 218 | FALSE, /* pc_relative */ |
b7be1db6 | 219 | 0, /* bitpos */ |
252b5132 | 220 | complain_overflow_bitfield, /* complain_on_overflow */ |
b7be1db6 AM |
221 | coff_i386_reloc, /* special_function */ |
222 | "dir32", /* name */ | |
b34976b6 | 223 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
224 | 0xffffffff, /* src_mask */ |
225 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 226 | TRUE), /* pcrel_offset */ |
b7be1db6 AM |
227 | /* PE IMAGE_REL_I386_DIR32NB relocation (7). */ |
228 | HOWTO (R_IMAGEBASE, /* type */ | |
229 | 0, /* rightshift */ | |
230 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
231 | 32, /* bitsize */ | |
b34976b6 | 232 | FALSE, /* pc_relative */ |
b7be1db6 | 233 | 0, /* bitpos */ |
252b5132 | 234 | complain_overflow_bitfield, /* complain_on_overflow */ |
b7be1db6 AM |
235 | coff_i386_reloc, /* special_function */ |
236 | "rva32", /* name */ | |
b34976b6 | 237 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
238 | 0xffffffff, /* src_mask */ |
239 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 240 | FALSE), /* pcrel_offset */ |
5f771d47 ILT |
241 | EMPTY_HOWTO (010), |
242 | EMPTY_HOWTO (011), | |
243 | EMPTY_HOWTO (012), | |
6482c264 NC |
244 | #ifdef COFF_WITH_PE |
245 | /* 32-bit longword section relative relocation (013). */ | |
246 | HOWTO (R_SECREL32, /* type */ | |
247 | 0, /* rightshift */ | |
248 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
249 | 32, /* bitsize */ | |
250 | FALSE, /* pc_relative */ | |
251 | 0, /* bitpos */ | |
252 | complain_overflow_bitfield, /* complain_on_overflow */ | |
253 | coff_i386_reloc, /* special_function */ | |
254 | "secrel32", /* name */ | |
255 | TRUE, /* partial_inplace */ | |
256 | 0xffffffff, /* src_mask */ | |
257 | 0xffffffff, /* dst_mask */ | |
258 | TRUE), /* pcrel_offset */ | |
259 | #else | |
5f771d47 | 260 | EMPTY_HOWTO (013), |
6482c264 | 261 | #endif |
5f771d47 ILT |
262 | EMPTY_HOWTO (014), |
263 | EMPTY_HOWTO (015), | |
264 | EMPTY_HOWTO (016), | |
00692651 ILT |
265 | /* Byte relocation (017). */ |
266 | HOWTO (R_RELBYTE, /* type */ | |
b7be1db6 AM |
267 | 0, /* rightshift */ |
268 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
269 | 8, /* bitsize */ | |
b34976b6 | 270 | FALSE, /* pc_relative */ |
b7be1db6 | 271 | 0, /* bitpos */ |
252b5132 | 272 | complain_overflow_bitfield, /* complain_on_overflow */ |
b7be1db6 AM |
273 | coff_i386_reloc, /* special_function */ |
274 | "8", /* name */ | |
b34976b6 | 275 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
276 | 0x000000ff, /* src_mask */ |
277 | 0x000000ff, /* dst_mask */ | |
252b5132 | 278 | PCRELOFFSET), /* pcrel_offset */ |
00692651 | 279 | /* 16-bit word relocation (020). */ |
b7be1db6 AM |
280 | HOWTO (R_RELWORD, /* type */ |
281 | 0, /* rightshift */ | |
282 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
283 | 16, /* bitsize */ | |
b34976b6 | 284 | FALSE, /* pc_relative */ |
b7be1db6 | 285 | 0, /* bitpos */ |
252b5132 | 286 | complain_overflow_bitfield, /* complain_on_overflow */ |
b7be1db6 AM |
287 | coff_i386_reloc, /* special_function */ |
288 | "16", /* name */ | |
b34976b6 | 289 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
290 | 0x0000ffff, /* src_mask */ |
291 | 0x0000ffff, /* dst_mask */ | |
252b5132 | 292 | PCRELOFFSET), /* pcrel_offset */ |
b7be1db6 AM |
293 | /* 32-bit longword relocation (021). */ |
294 | HOWTO (R_RELLONG, /* type */ | |
295 | 0, /* rightshift */ | |
296 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
297 | 32, /* bitsize */ | |
b34976b6 | 298 | FALSE, /* pc_relative */ |
b7be1db6 | 299 | 0, /* bitpos */ |
252b5132 | 300 | complain_overflow_bitfield, /* complain_on_overflow */ |
b7be1db6 AM |
301 | coff_i386_reloc, /* special_function */ |
302 | "32", /* name */ | |
b34976b6 | 303 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
304 | 0xffffffff, /* src_mask */ |
305 | 0xffffffff, /* dst_mask */ | |
252b5132 | 306 | PCRELOFFSET), /* pcrel_offset */ |
b7be1db6 AM |
307 | /* Byte PC relative relocation (022). */ |
308 | HOWTO (R_PCRBYTE, /* type */ | |
309 | 0, /* rightshift */ | |
310 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
311 | 8, /* bitsize */ | |
b34976b6 | 312 | TRUE, /* pc_relative */ |
b7be1db6 | 313 | 0, /* bitpos */ |
252b5132 | 314 | complain_overflow_signed, /* complain_on_overflow */ |
b7be1db6 AM |
315 | coff_i386_reloc, /* special_function */ |
316 | "DISP8", /* name */ | |
b34976b6 | 317 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
318 | 0x000000ff, /* src_mask */ |
319 | 0x000000ff, /* dst_mask */ | |
252b5132 | 320 | PCRELOFFSET), /* pcrel_offset */ |
b7be1db6 AM |
321 | /* 16-bit word PC relative relocation (023). */ |
322 | HOWTO (R_PCRWORD, /* type */ | |
323 | 0, /* rightshift */ | |
324 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
325 | 16, /* bitsize */ | |
b34976b6 | 326 | TRUE, /* pc_relative */ |
b7be1db6 | 327 | 0, /* bitpos */ |
252b5132 | 328 | complain_overflow_signed, /* complain_on_overflow */ |
b7be1db6 AM |
329 | coff_i386_reloc, /* special_function */ |
330 | "DISP16", /* name */ | |
b34976b6 | 331 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
332 | 0x0000ffff, /* src_mask */ |
333 | 0x0000ffff, /* dst_mask */ | |
252b5132 | 334 | PCRELOFFSET), /* pcrel_offset */ |
00692651 | 335 | /* 32-bit longword PC relative relocation (024). */ |
b7be1db6 AM |
336 | HOWTO (R_PCRLONG, /* type */ |
337 | 0, /* rightshift */ | |
338 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
339 | 32, /* bitsize */ | |
b34976b6 | 340 | TRUE, /* pc_relative */ |
b7be1db6 | 341 | 0, /* bitpos */ |
252b5132 | 342 | complain_overflow_signed, /* complain_on_overflow */ |
b7be1db6 AM |
343 | coff_i386_reloc, /* special_function */ |
344 | "DISP32", /* name */ | |
b34976b6 | 345 | TRUE, /* partial_inplace */ |
b7be1db6 AM |
346 | 0xffffffff, /* src_mask */ |
347 | 0xffffffff, /* dst_mask */ | |
252b5132 RH |
348 | PCRELOFFSET) /* pcrel_offset */ |
349 | }; | |
350 | ||
351 | /* Turn a howto into a reloc nunmber */ | |
352 | ||
353 | #define SELECT_RELOC(x,howto) { x.r_type = howto->type; } | |
354 | #define BADMAG(x) I386BADMAG(x) | |
355 | #define I386 1 /* Customize coffcode.h */ | |
356 | ||
00692651 ILT |
357 | #define RTYPE2HOWTO(cache_ptr, dst) \ |
358 | ((cache_ptr)->howto = \ | |
359 | ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \ | |
360 | ? howto_table + (dst)->r_type \ | |
361 | : NULL)) | |
252b5132 RH |
362 | |
363 | /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared | |
364 | library. On some other COFF targets STYP_BSS is normally | |
365 | STYP_NOLOAD. */ | |
366 | #define BSS_NOLOAD_IS_SHARED_LIBRARY | |
367 | ||
368 | /* Compute the addend of a reloc. If the reloc is to a common symbol, | |
369 | the object file contains the value of the common symbol. By the | |
370 | time this is called, the linker may be using a different symbol | |
371 | from a different object file with a different value. Therefore, we | |
372 | hack wildly to locate the original symbol from this file so that we | |
373 | can make the correct adjustment. This macro sets coffsym to the | |
374 | symbol from the original file, and uses it to set the addend value | |
375 | correctly. If this is not a common symbol, the usual addend | |
376 | calculation is done, except that an additional tweak is needed for | |
377 | PC relative relocs. | |
378 | FIXME: This macro refers to symbols and asect; these are from the | |
379 | calling function, not the macro arguments. */ | |
380 | ||
381 | #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \ | |
382 | { \ | |
383 | coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \ | |
384 | if (ptr && bfd_asymbol_bfd (ptr) != abfd) \ | |
385 | coffsym = (obj_symbols (abfd) \ | |
386 | + (cache_ptr->sym_ptr_ptr - symbols)); \ | |
387 | else if (ptr) \ | |
388 | coffsym = coff_symbol_from (abfd, ptr); \ | |
389 | if (coffsym != (coff_symbol_type *) NULL \ | |
390 | && coffsym->native->u.syment.n_scnum == 0) \ | |
391 | cache_ptr->addend = - coffsym->native->u.syment.n_value; \ | |
392 | else if (ptr && bfd_asymbol_bfd (ptr) == abfd \ | |
393 | && ptr->section != (asection *) NULL) \ | |
394 | cache_ptr->addend = - (ptr->section->vma + ptr->value); \ | |
395 | else \ | |
396 | cache_ptr->addend = 0; \ | |
397 | if (ptr && howto_table[reloc.r_type].pc_relative) \ | |
398 | cache_ptr->addend += asect->vma; \ | |
399 | } | |
400 | ||
401 | /* We use the special COFF backend linker. For normal i386 COFF, we | |
402 | can use the generic relocate_section routine. For PE, we need our | |
403 | own routine. */ | |
404 | ||
405 | #ifndef COFF_WITH_PE | |
406 | ||
407 | #define coff_relocate_section _bfd_coff_generic_relocate_section | |
408 | ||
409 | #else /* COFF_WITH_PE */ | |
410 | ||
411 | /* The PE relocate section routine. The only difference between this | |
412 | and the regular routine is that we don't want to do anything for a | |
1049f94e | 413 | relocatable link. */ |
252b5132 | 414 | |
b34976b6 | 415 | static bfd_boolean coff_pe_i386_relocate_section |
252b5132 RH |
416 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
417 | struct internal_reloc *, struct internal_syment *, asection **)); | |
418 | ||
b34976b6 | 419 | static bfd_boolean |
252b5132 RH |
420 | coff_pe_i386_relocate_section (output_bfd, info, input_bfd, |
421 | input_section, contents, relocs, syms, | |
422 | sections) | |
423 | bfd *output_bfd; | |
424 | struct bfd_link_info *info; | |
425 | bfd *input_bfd; | |
426 | asection *input_section; | |
427 | bfd_byte *contents; | |
428 | struct internal_reloc *relocs; | |
429 | struct internal_syment *syms; | |
430 | asection **sections; | |
431 | { | |
1049f94e | 432 | if (info->relocatable) |
b34976b6 | 433 | return TRUE; |
252b5132 RH |
434 | |
435 | return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd, | |
436 | input_section, contents, | |
437 | relocs, syms, sections); | |
438 | } | |
439 | ||
440 | #define coff_relocate_section coff_pe_i386_relocate_section | |
441 | ||
442 | #endif /* COFF_WITH_PE */ | |
443 | ||
444 | /* Convert an rtype to howto for the COFF backend linker. */ | |
445 | ||
446 | static reloc_howto_type * | |
447 | coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp) | |
5f771d47 | 448 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
449 | asection *sec; |
450 | struct internal_reloc *rel; | |
451 | struct coff_link_hash_entry *h; | |
452 | struct internal_syment *sym; | |
453 | bfd_vma *addendp; | |
454 | { | |
252b5132 RH |
455 | reloc_howto_type *howto; |
456 | ||
50a39e97 | 457 | if (rel->r_type >= sizeof (howto_table) / sizeof (howto_table[0])) |
00692651 ILT |
458 | { |
459 | bfd_set_error (bfd_error_bad_value); | |
460 | return NULL; | |
461 | } | |
462 | ||
252b5132 RH |
463 | howto = howto_table + rel->r_type; |
464 | ||
465 | #ifdef COFF_WITH_PE | |
00692651 | 466 | /* Cancel out code in _bfd_coff_generic_relocate_section. */ |
252b5132 RH |
467 | *addendp = 0; |
468 | #endif | |
469 | ||
470 | if (howto->pc_relative) | |
471 | *addendp += sec->vma; | |
472 | ||
473 | if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0) | |
474 | { | |
475 | /* This is a common symbol. The section contents include the | |
476 | size (sym->n_value) as an addend. The relocate_section | |
477 | function will be adding in the final value of the symbol. We | |
478 | need to subtract out the current size in order to get the | |
479 | correct result. */ | |
b7be1db6 | 480 | |
252b5132 RH |
481 | BFD_ASSERT (h != NULL); |
482 | ||
483 | #ifndef COFF_WITH_PE | |
484 | /* I think we *do* want to bypass this. If we don't, I have | |
485 | seen some data parameters get the wrong relocation address. | |
486 | If I link two versions with and without this section bypassed | |
487 | and then do a binary comparison, the addresses which are | |
488 | different can be looked up in the map. The case in which | |
489 | this section has been bypassed has addresses which correspond | |
490 | to values I can find in the map. */ | |
491 | *addendp -= sym->n_value; | |
492 | #endif | |
493 | } | |
494 | ||
495 | #ifndef COFF_WITH_PE | |
496 | /* If the output symbol is common (in which case this must be a | |
1049f94e | 497 | relocatable link), we need to add in the final size of the |
252b5132 | 498 | common symbol. */ |
b7be1db6 | 499 | if (h != NULL && h->root.type == bfd_link_hash_common) |
252b5132 RH |
500 | *addendp += h->root.u.c.size; |
501 | #endif | |
502 | ||
503 | #ifdef COFF_WITH_PE | |
504 | if (howto->pc_relative) | |
505 | { | |
506 | *addendp -= 4; | |
507 | ||
508 | /* If the symbol is defined, then the generic code is going to | |
509 | add back the symbol value in order to cancel out an | |
510 | adjustment it made to the addend. However, we set the addend | |
511 | to 0 at the start of this function. We need to adjust here, | |
512 | to avoid the adjustment the generic code will make. FIXME: | |
513 | This is getting a bit hackish. */ | |
514 | if (sym != NULL && sym->n_scnum != 0) | |
515 | *addendp -= sym->n_value; | |
516 | } | |
517 | ||
8e191bd3 ILT |
518 | if (rel->r_type == R_IMAGEBASE |
519 | && (bfd_get_flavour(sec->output_section->owner) | |
520 | == bfd_target_coff_flavour)) | |
252b5132 RH |
521 | { |
522 | *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase; | |
523 | } | |
6482c264 | 524 | |
507f5898 MS |
525 | BFD_ASSERT (sym != NULL); |
526 | if (rel->r_type == R_SECREL32 && sym != NULL) | |
6482c264 NC |
527 | { |
528 | bfd_vma osect_vma; | |
529 | ||
530 | if (h && (h->type == bfd_link_hash_defined | |
531 | || h->type == bfd_link_hash_defweak)) | |
532 | osect_vma = h->root.u.def.section->output_section->vma; | |
533 | else | |
534 | { | |
535 | asection *sec; | |
536 | int i; | |
537 | ||
538 | /* Sigh, the only way to get the section to offset against | |
539 | is to find it the hard way. */ | |
540 | ||
541 | for (sec = abfd->sections, i = 1; i < sym->n_scnum; i++) | |
542 | sec = sec->next; | |
543 | ||
544 | osect_vma = sec->output_section->vma; | |
545 | } | |
546 | ||
547 | *addendp -= osect_vma; | |
548 | } | |
252b5132 RH |
549 | #endif |
550 | ||
551 | return howto; | |
552 | } | |
553 | ||
252b5132 | 554 | #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup |
157090f7 | 555 | #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup |
252b5132 | 556 | |
252b5132 RH |
557 | static reloc_howto_type * |
558 | coff_i386_reloc_type_lookup (abfd, code) | |
5f771d47 | 559 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
560 | bfd_reloc_code_real_type code; |
561 | { | |
562 | switch (code) | |
563 | { | |
564 | case BFD_RELOC_RVA: | |
b7be1db6 | 565 | return howto_table + R_IMAGEBASE; |
252b5132 RH |
566 | case BFD_RELOC_32: |
567 | return howto_table + R_DIR32; | |
568 | case BFD_RELOC_32_PCREL: | |
569 | return howto_table + R_PCRLONG; | |
b7be1db6 AM |
570 | case BFD_RELOC_16: |
571 | return howto_table + R_RELWORD; | |
572 | case BFD_RELOC_16_PCREL: | |
573 | return howto_table + R_PCRWORD; | |
574 | case BFD_RELOC_8: | |
575 | return howto_table + R_RELBYTE; | |
576 | case BFD_RELOC_8_PCREL: | |
577 | return howto_table + R_PCRBYTE; | |
6482c264 NC |
578 | #ifdef COFF_WITH_PE |
579 | case BFD_RELOC_32_SECREL: | |
580 | return howto_table + R_SECREL32; | |
581 | #endif | |
252b5132 RH |
582 | default: |
583 | BFD_FAIL (); | |
584 | return 0; | |
585 | } | |
586 | } | |
587 | ||
157090f7 AM |
588 | static reloc_howto_type * |
589 | coff_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
590 | const char *r_name) | |
591 | { | |
592 | unsigned int i; | |
593 | ||
594 | for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++) | |
595 | if (howto_table[i].name != NULL | |
596 | && strcasecmp (howto_table[i].name, r_name) == 0) | |
597 | return &howto_table[i]; | |
598 | ||
599 | return NULL; | |
600 | } | |
601 | ||
252b5132 RH |
602 | #define coff_rtype_to_howto coff_i386_rtype_to_howto |
603 | ||
604 | #ifdef TARGET_UNDERSCORE | |
605 | ||
606 | /* If i386 gcc uses underscores for symbol names, then it does not use | |
607 | a leading dot for local labels, so if TARGET_UNDERSCORE is defined | |
608 | we treat all symbols starting with L as local. */ | |
609 | ||
b34976b6 AM |
610 | static bfd_boolean coff_i386_is_local_label_name |
611 | PARAMS ((bfd *, const char *)); | |
252b5132 | 612 | |
b34976b6 | 613 | static bfd_boolean |
252b5132 RH |
614 | coff_i386_is_local_label_name (abfd, name) |
615 | bfd *abfd; | |
616 | const char *name; | |
617 | { | |
618 | if (name[0] == 'L') | |
b34976b6 | 619 | return TRUE; |
252b5132 RH |
620 | |
621 | return _bfd_coff_is_local_label_name (abfd, name); | |
622 | } | |
623 | ||
624 | #define coff_bfd_is_local_label_name coff_i386_is_local_label_name | |
625 | ||
626 | #endif /* TARGET_UNDERSCORE */ | |
627 | ||
628 | #include "coffcode.h" | |
629 | ||
252b5132 RH |
630 | const bfd_target |
631 | #ifdef TARGET_SYM | |
632 | TARGET_SYM = | |
633 | #else | |
634 | i386coff_vec = | |
635 | #endif | |
636 | { | |
637 | #ifdef TARGET_NAME | |
638 | TARGET_NAME, | |
639 | #else | |
640 | "coff-i386", /* name */ | |
641 | #endif | |
642 | bfd_target_coff_flavour, | |
643 | BFD_ENDIAN_LITTLE, /* data byte order is little */ | |
644 | BFD_ENDIAN_LITTLE, /* header byte order is little */ | |
645 | ||
646 | (HAS_RELOC | EXEC_P | /* object flags */ | |
647 | HAS_LINENO | HAS_DEBUG | | |
648 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | |
649 | ||
252b5132 | 650 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */ |
6fb72d08 | 651 | #ifdef COFF_WITH_PE |
baf80c32 | 652 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY |
252b5132 | 653 | #endif |
6fb72d08 | 654 | | SEC_CODE | SEC_DATA), |
252b5132 RH |
655 | |
656 | #ifdef TARGET_UNDERSCORE | |
657 | TARGET_UNDERSCORE, /* leading underscore */ | |
658 | #else | |
659 | 0, /* leading underscore */ | |
660 | #endif | |
661 | '/', /* ar_pad_char */ | |
662 | 15, /* ar_max_namelen */ | |
663 | ||
664 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
665 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
666 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
667 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
668 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
669 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
670 | ||
5fcfd273 | 671 | /* Note that we allow an object file to be treated as a core file as well. */ |
cb665cd3 NC |
672 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ |
673 | bfd_generic_archive_p, coff_object_p}, | |
252b5132 RH |
674 | {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */ |
675 | bfd_false}, | |
676 | {bfd_false, coff_write_object_contents, /* bfd_write_contents */ | |
677 | _bfd_write_archive_contents, bfd_false}, | |
678 | ||
679 | BFD_JUMP_TABLE_GENERIC (coff), | |
680 | BFD_JUMP_TABLE_COPY (coff), | |
681 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
682 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), | |
683 | BFD_JUMP_TABLE_SYMBOLS (coff), | |
684 | BFD_JUMP_TABLE_RELOCS (coff), | |
685 | BFD_JUMP_TABLE_WRITE (coff), | |
686 | BFD_JUMP_TABLE_LINK (coff), | |
687 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
688 | ||
c3c89269 | 689 | NULL, |
b7be1db6 | 690 | |
c3c89269 | 691 | COFF_SWAP_TABLE |
252b5132 | 692 | }; |