Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for Intel 386 COFF files. |
82704155 | 2 | Copyright (C) 1990-2019 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Cygnus Support. |
4 | ||
977cdf5a | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
977cdf5a NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
977cdf5a | 10 | (at your option) any later version. |
252b5132 | 11 | |
977cdf5a NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
977cdf5a NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 RH |
24 | #include "libbfd.h" |
25 | ||
26 | #include "coff/i386.h" | |
27 | ||
28 | #include "coff/internal.h" | |
29 | ||
30 | #ifdef COFF_WITH_PE | |
31 | #include "coff/pe.h" | |
32 | #endif | |
33 | ||
34 | #ifdef COFF_GO32_EXE | |
35 | #include "coff/go32exe.h" | |
36 | #endif | |
37 | ||
2b5c217d NC |
38 | #ifndef bfd_pe_print_pdata |
39 | #define bfd_pe_print_pdata NULL | |
40 | #endif | |
41 | ||
252b5132 RH |
42 | #include "libcoff.h" |
43 | ||
bb294208 AM |
44 | /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */ |
45 | #define OCTETS_PER_BYTE(ABFD, SEC) 1 | |
46 | ||
252b5132 | 47 | static reloc_howto_type *coff_i386_rtype_to_howto |
e05da72d NC |
48 | (bfd *, asection *, struct internal_reloc *, |
49 | struct coff_link_hash_entry *, struct internal_syment *, | |
50 | bfd_vma *); | |
00692651 | 51 | static reloc_howto_type *coff_i386_reloc_type_lookup |
e05da72d | 52 | (bfd *, bfd_reloc_code_real_type); |
252b5132 RH |
53 | |
54 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2) | |
55 | /* The page size is a guess based on ELF. */ | |
56 | ||
57 | #define COFF_PAGE_SIZE 0x1000 | |
58 | ||
59 | /* For some reason when using i386 COFF the value stored in the .text | |
60 | section for a reference to a common symbol is the value itself plus | |
61 | any desired offset. Ian Taylor, Cygnus Support. */ | |
62 | ||
1049f94e | 63 | /* If we are producing relocatable output, we need to do some |
252b5132 RH |
64 | adjustments to the object file that are not done by the |
65 | bfd_perform_relocation function. This function is called by every | |
66 | reloc type to make any required adjustments. */ | |
67 | ||
68 | static bfd_reloc_status_type | |
2c3fc389 NC |
69 | coff_i386_reloc (bfd *abfd, |
70 | arelent *reloc_entry, | |
71 | asymbol *symbol, | |
72 | void * data, | |
bb294208 | 73 | asection *input_section, |
2c3fc389 NC |
74 | bfd *output_bfd, |
75 | char **error_message ATTRIBUTE_UNUSED) | |
252b5132 RH |
76 | { |
77 | symvalue diff; | |
78 | ||
689be717 | 79 | #ifndef COFF_WITH_PE |
252b5132 RH |
80 | if (output_bfd == (bfd *) NULL) |
81 | return bfd_reloc_continue; | |
689be717 | 82 | #endif |
252b5132 RH |
83 | |
84 | if (bfd_is_com_section (symbol->section)) | |
85 | { | |
86 | #ifndef COFF_WITH_PE | |
87 | /* We are relocating a common symbol. The current value in the | |
88 | object file is ORIG + OFFSET, where ORIG is the value of the | |
89 | common symbol as seen by the object file when it was compiled | |
90 | (this may be zero if the symbol was undefined) and OFFSET is | |
91 | the offset into the common symbol (normally zero, but may be | |
92 | non-zero when referring to a field in a common structure). | |
93 | ORIG is the negative of reloc_entry->addend, which is set by | |
94 | the CALC_ADDEND macro below. We want to replace the value in | |
95 | the object file with NEW + OFFSET, where NEW is the value of | |
96 | the common symbol which we are going to put in the final | |
97 | object file. NEW is symbol->value. */ | |
98 | diff = symbol->value + reloc_entry->addend; | |
99 | #else | |
100 | /* In PE mode, we do not offset the common symbol. */ | |
101 | diff = reloc_entry->addend; | |
102 | #endif | |
103 | } | |
104 | else | |
105 | { | |
106 | /* For some reason bfd_perform_relocation always effectively | |
107 | ignores the addend for a COFF target when producing | |
1049f94e | 108 | relocatable output. This seems to be always wrong for 386 |
252b5132 | 109 | COFF, so we handle the addend here instead. */ |
689be717 L |
110 | #ifdef COFF_WITH_PE |
111 | if (output_bfd == (bfd *) NULL) | |
112 | { | |
113 | reloc_howto_type *howto = reloc_entry->howto; | |
114 | ||
115 | /* Although PC relative relocations are very similar between | |
116 | PE and non-PE formats, but they are off by 1 << howto->size | |
117 | bytes. For the external relocation, PE is very different | |
118 | from others. See md_apply_fix3 () in gas/config/tc-i386.c. | |
119 | When we link PE and non-PE object files together to | |
120 | generate a non-PE executable, we have to compensate it | |
121 | here. */ | |
82e51918 | 122 | if (howto->pc_relative && howto->pcrel_offset) |
689be717 | 123 | diff = -(1 << howto->size); |
977cdf5a NC |
124 | else if (symbol->flags & BSF_WEAK) |
125 | diff = reloc_entry->addend - symbol->value; | |
689be717 L |
126 | else |
127 | diff = -reloc_entry->addend; | |
128 | } | |
129 | else | |
130 | #endif | |
131 | diff = reloc_entry->addend; | |
252b5132 RH |
132 | } |
133 | ||
134 | #ifdef COFF_WITH_PE | |
135 | /* FIXME: How should this case be handled? */ | |
8e191bd3 ILT |
136 | if (reloc_entry->howto->type == R_IMAGEBASE |
137 | && output_bfd != NULL | |
138 | && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour) | |
00692651 | 139 | diff -= pe_data (output_bfd)->pe_opthdr.ImageBase; |
252b5132 RH |
140 | #endif |
141 | ||
142 | #define DOIT(x) \ | |
143 | x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask)) | |
144 | ||
ec70be9f NC |
145 | if (diff != 0) |
146 | { | |
147 | reloc_howto_type *howto = reloc_entry->howto; | |
bb294208 AM |
148 | bfd_size_type octets = (reloc_entry->address |
149 | * OCTETS_PER_BYTE (abfd, input_section)); | |
150 | unsigned char *addr = (unsigned char *) data + octets; | |
ec70be9f | 151 | |
bb294208 | 152 | if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets)) |
b23dc97f NC |
153 | return bfd_reloc_outofrange; |
154 | ||
ec70be9f NC |
155 | switch (howto->size) |
156 | { | |
157 | case 0: | |
158 | { | |
159 | char x = bfd_get_8 (abfd, addr); | |
160 | DOIT (x); | |
161 | bfd_put_8 (abfd, x, addr); | |
162 | } | |
163 | break; | |
252b5132 | 164 | |
ec70be9f | 165 | case 1: |
252b5132 | 166 | { |
ec70be9f NC |
167 | short x = bfd_get_16 (abfd, addr); |
168 | DOIT (x); | |
169 | bfd_put_16 (abfd, (bfd_vma) x, addr); | |
252b5132 | 170 | } |
ec70be9f NC |
171 | break; |
172 | ||
173 | case 2: | |
174 | { | |
175 | long x = bfd_get_32 (abfd, addr); | |
176 | DOIT (x); | |
177 | bfd_put_32 (abfd, (bfd_vma) x, addr); | |
178 | } | |
179 | break; | |
180 | ||
181 | default: | |
182 | abort (); | |
183 | } | |
184 | } | |
252b5132 RH |
185 | |
186 | /* Now let bfd_perform_relocation finish everything up. */ | |
187 | return bfd_reloc_continue; | |
188 | } | |
189 | ||
190 | #ifdef COFF_WITH_PE | |
b34976b6 | 191 | /* Return TRUE if this relocation should appear in the output .reloc |
6fb72d08 ILT |
192 | section. */ |
193 | ||
2c3fc389 NC |
194 | static bfd_boolean in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, |
195 | reloc_howto_type *howto) | |
252b5132 | 196 | { |
b706dc83 KT |
197 | return ! howto->pc_relative && howto->type != R_IMAGEBASE |
198 | && howto->type != R_SECREL32; | |
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 | ||
36e9d67b NC |
351 | #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0])) |
352 | ||
252b5132 RH |
353 | /* Turn a howto into a reloc nunmber */ |
354 | ||
355 | #define SELECT_RELOC(x,howto) { x.r_type = howto->type; } | |
356 | #define BADMAG(x) I386BADMAG(x) | |
357 | #define I386 1 /* Customize coffcode.h */ | |
358 | ||
36e9d67b NC |
359 | #define RTYPE2HOWTO(cache_ptr, dst) \ |
360 | ((cache_ptr)->howto = \ | |
361 | ((dst)->r_type < NUM_HOWTOS \ | |
362 | ? howto_table + (dst)->r_type \ | |
00692651 | 363 | : NULL)) |
252b5132 RH |
364 | |
365 | /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared | |
366 | library. On some other COFF targets STYP_BSS is normally | |
367 | STYP_NOLOAD. */ | |
368 | #define BSS_NOLOAD_IS_SHARED_LIBRARY | |
369 | ||
370 | /* Compute the addend of a reloc. If the reloc is to a common symbol, | |
371 | the object file contains the value of the common symbol. By the | |
372 | time this is called, the linker may be using a different symbol | |
373 | from a different object file with a different value. Therefore, we | |
374 | hack wildly to locate the original symbol from this file so that we | |
375 | can make the correct adjustment. This macro sets coffsym to the | |
376 | symbol from the original file, and uses it to set the addend value | |
377 | correctly. If this is not a common symbol, the usual addend | |
378 | calculation is done, except that an additional tweak is needed for | |
379 | PC relative relocs. | |
380 | FIXME: This macro refers to symbols and asect; these are from the | |
381 | calling function, not the macro arguments. */ | |
382 | ||
383 | #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \ | |
384 | { \ | |
385 | coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \ | |
386 | if (ptr && bfd_asymbol_bfd (ptr) != abfd) \ | |
387 | coffsym = (obj_symbols (abfd) \ | |
07d6d2b8 | 388 | + (cache_ptr->sym_ptr_ptr - symbols)); \ |
252b5132 | 389 | else if (ptr) \ |
f4943d82 | 390 | coffsym = coff_symbol_from (ptr); \ |
252b5132 RH |
391 | if (coffsym != (coff_symbol_type *) NULL \ |
392 | && coffsym->native->u.syment.n_scnum == 0) \ | |
393 | cache_ptr->addend = - coffsym->native->u.syment.n_value; \ | |
394 | else if (ptr && bfd_asymbol_bfd (ptr) == abfd \ | |
395 | && ptr->section != (asection *) NULL) \ | |
396 | cache_ptr->addend = - (ptr->section->vma + ptr->value); \ | |
397 | else \ | |
398 | cache_ptr->addend = 0; \ | |
36e9d67b NC |
399 | if (ptr && reloc.r_type < NUM_HOWTOS \ |
400 | && howto_table[reloc.r_type].pc_relative) \ | |
252b5132 RH |
401 | cache_ptr->addend += asect->vma; \ |
402 | } | |
403 | ||
404 | /* We use the special COFF backend linker. For normal i386 COFF, we | |
405 | can use the generic relocate_section routine. For PE, we need our | |
406 | own routine. */ | |
407 | ||
408 | #ifndef COFF_WITH_PE | |
409 | ||
410 | #define coff_relocate_section _bfd_coff_generic_relocate_section | |
411 | ||
412 | #else /* COFF_WITH_PE */ | |
413 | ||
414 | /* The PE relocate section routine. The only difference between this | |
415 | and the regular routine is that we don't want to do anything for a | |
1049f94e | 416 | relocatable link. */ |
252b5132 | 417 | |
b34976b6 | 418 | static bfd_boolean |
2c3fc389 NC |
419 | coff_pe_i386_relocate_section (bfd *output_bfd, |
420 | struct bfd_link_info *info, | |
421 | bfd *input_bfd, | |
422 | asection *input_section, | |
423 | bfd_byte *contents, | |
424 | struct internal_reloc *relocs, | |
425 | struct internal_syment *syms, | |
426 | asection **sections) | |
252b5132 | 427 | { |
0e1862bb | 428 | if (bfd_link_relocatable (info)) |
b34976b6 | 429 | return TRUE; |
252b5132 RH |
430 | |
431 | return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd, | |
432 | input_section, contents, | |
433 | relocs, syms, sections); | |
434 | } | |
435 | ||
436 | #define coff_relocate_section coff_pe_i386_relocate_section | |
437 | ||
438 | #endif /* COFF_WITH_PE */ | |
439 | ||
440 | /* Convert an rtype to howto for the COFF backend linker. */ | |
441 | ||
442 | static reloc_howto_type * | |
2c3fc389 NC |
443 | coff_i386_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
444 | asection *sec, | |
445 | struct internal_reloc *rel, | |
446 | struct coff_link_hash_entry *h, | |
447 | struct internal_syment *sym, | |
448 | bfd_vma *addendp) | |
252b5132 | 449 | { |
252b5132 RH |
450 | reloc_howto_type *howto; |
451 | ||
36e9d67b | 452 | if (rel->r_type >= NUM_HOWTOS) |
00692651 ILT |
453 | { |
454 | bfd_set_error (bfd_error_bad_value); | |
455 | return NULL; | |
456 | } | |
457 | ||
252b5132 RH |
458 | howto = howto_table + rel->r_type; |
459 | ||
460 | #ifdef COFF_WITH_PE | |
00692651 | 461 | /* Cancel out code in _bfd_coff_generic_relocate_section. */ |
252b5132 RH |
462 | *addendp = 0; |
463 | #endif | |
464 | ||
465 | if (howto->pc_relative) | |
466 | *addendp += sec->vma; | |
467 | ||
468 | if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0) | |
469 | { | |
470 | /* This is a common symbol. The section contents include the | |
471 | size (sym->n_value) as an addend. The relocate_section | |
472 | function will be adding in the final value of the symbol. We | |
473 | need to subtract out the current size in order to get the | |
474 | correct result. */ | |
b7be1db6 | 475 | |
252b5132 RH |
476 | BFD_ASSERT (h != NULL); |
477 | ||
478 | #ifndef COFF_WITH_PE | |
479 | /* I think we *do* want to bypass this. If we don't, I have | |
480 | seen some data parameters get the wrong relocation address. | |
481 | If I link two versions with and without this section bypassed | |
482 | and then do a binary comparison, the addresses which are | |
483 | different can be looked up in the map. The case in which | |
484 | this section has been bypassed has addresses which correspond | |
485 | to values I can find in the map. */ | |
486 | *addendp -= sym->n_value; | |
487 | #endif | |
488 | } | |
489 | ||
490 | #ifndef COFF_WITH_PE | |
491 | /* If the output symbol is common (in which case this must be a | |
1049f94e | 492 | relocatable link), we need to add in the final size of the |
252b5132 | 493 | common symbol. */ |
b7be1db6 | 494 | if (h != NULL && h->root.type == bfd_link_hash_common) |
252b5132 RH |
495 | *addendp += h->root.u.c.size; |
496 | #endif | |
497 | ||
498 | #ifdef COFF_WITH_PE | |
499 | if (howto->pc_relative) | |
500 | { | |
501 | *addendp -= 4; | |
502 | ||
503 | /* If the symbol is defined, then the generic code is going to | |
07d6d2b8 AM |
504 | add back the symbol value in order to cancel out an |
505 | adjustment it made to the addend. However, we set the addend | |
506 | to 0 at the start of this function. We need to adjust here, | |
507 | to avoid the adjustment the generic code will make. FIXME: | |
508 | This is getting a bit hackish. */ | |
252b5132 RH |
509 | if (sym != NULL && sym->n_scnum != 0) |
510 | *addendp -= sym->n_value; | |
511 | } | |
512 | ||
8e191bd3 ILT |
513 | if (rel->r_type == R_IMAGEBASE |
514 | && (bfd_get_flavour(sec->output_section->owner) | |
515 | == bfd_target_coff_flavour)) | |
252b5132 RH |
516 | { |
517 | *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase; | |
518 | } | |
6482c264 | 519 | |
a93d5cb1 NC |
520 | /* PR 17099 - Absolute R_PCRLONG relocations do not need a symbol. */ |
521 | if (rel->r_type == R_PCRLONG && sym == NULL) | |
522 | *addendp -= rel->r_vaddr; | |
523 | else | |
524 | BFD_ASSERT (sym != NULL); | |
525 | ||
507f5898 | 526 | if (rel->r_type == R_SECREL32 && sym != NULL) |
6482c264 NC |
527 | { |
528 | bfd_vma osect_vma; | |
529 | ||
8a5dcf53 AM |
530 | if (h && (h->root.type == bfd_link_hash_defined |
531 | || h->root.type == bfd_link_hash_defweak)) | |
6482c264 NC |
532 | osect_vma = h->root.u.def.section->output_section->vma; |
533 | else | |
534 | { | |
91d6fa6a | 535 | asection *s; |
6482c264 NC |
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 | ||
91d6fa6a NC |
541 | for (s = abfd->sections, i = 1; i < sym->n_scnum; i++) |
542 | s = s->next; | |
6482c264 | 543 | |
91d6fa6a | 544 | osect_vma = s->output_section->vma; |
6482c264 NC |
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 | 557 | static reloc_howto_type * |
2c3fc389 NC |
558 | coff_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
559 | bfd_reloc_code_real_type code) | |
252b5132 RH |
560 | { |
561 | switch (code) | |
562 | { | |
563 | case BFD_RELOC_RVA: | |
b7be1db6 | 564 | return howto_table + R_IMAGEBASE; |
252b5132 RH |
565 | case BFD_RELOC_32: |
566 | return howto_table + R_DIR32; | |
567 | case BFD_RELOC_32_PCREL: | |
568 | return howto_table + R_PCRLONG; | |
b7be1db6 AM |
569 | case BFD_RELOC_16: |
570 | return howto_table + R_RELWORD; | |
571 | case BFD_RELOC_16_PCREL: | |
572 | return howto_table + R_PCRWORD; | |
573 | case BFD_RELOC_8: | |
574 | return howto_table + R_RELBYTE; | |
575 | case BFD_RELOC_8_PCREL: | |
576 | return howto_table + R_PCRBYTE; | |
6482c264 NC |
577 | #ifdef COFF_WITH_PE |
578 | case BFD_RELOC_32_SECREL: | |
579 | return howto_table + R_SECREL32; | |
580 | #endif | |
252b5132 RH |
581 | default: |
582 | BFD_FAIL (); | |
583 | return 0; | |
584 | } | |
585 | } | |
586 | ||
157090f7 AM |
587 | static reloc_howto_type * |
588 | coff_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
589 | const char *r_name) | |
590 | { | |
591 | unsigned int i; | |
592 | ||
36e9d67b | 593 | for (i = 0; i < NUM_HOWTOS; i++) |
157090f7 AM |
594 | if (howto_table[i].name != NULL |
595 | && strcasecmp (howto_table[i].name, r_name) == 0) | |
596 | return &howto_table[i]; | |
597 | ||
598 | return NULL; | |
599 | } | |
600 | ||
252b5132 RH |
601 | #define coff_rtype_to_howto coff_i386_rtype_to_howto |
602 | ||
603 | #ifdef TARGET_UNDERSCORE | |
604 | ||
605 | /* If i386 gcc uses underscores for symbol names, then it does not use | |
606 | a leading dot for local labels, so if TARGET_UNDERSCORE is defined | |
607 | we treat all symbols starting with L as local. */ | |
608 | ||
b34976b6 | 609 | static bfd_boolean |
2c3fc389 | 610 | coff_i386_is_local_label_name (bfd *abfd, const char *name) |
252b5132 RH |
611 | { |
612 | if (name[0] == 'L') | |
b34976b6 | 613 | return TRUE; |
252b5132 RH |
614 | |
615 | return _bfd_coff_is_local_label_name (abfd, name); | |
616 | } | |
617 | ||
618 | #define coff_bfd_is_local_label_name coff_i386_is_local_label_name | |
619 | ||
620 | #endif /* TARGET_UNDERSCORE */ | |
621 | ||
622 | #include "coffcode.h" | |
623 | ||
252b5132 RH |
624 | const bfd_target |
625 | #ifdef TARGET_SYM | |
626 | TARGET_SYM = | |
627 | #else | |
6d00b590 | 628 | i386_coff_vec = |
252b5132 RH |
629 | #endif |
630 | { | |
631 | #ifdef TARGET_NAME | |
632 | TARGET_NAME, | |
633 | #else | |
634 | "coff-i386", /* name */ | |
635 | #endif | |
636 | bfd_target_coff_flavour, | |
637 | BFD_ENDIAN_LITTLE, /* data byte order is little */ | |
638 | BFD_ENDIAN_LITTLE, /* header byte order is little */ | |
639 | ||
640 | (HAS_RELOC | EXEC_P | /* object flags */ | |
641 | HAS_LINENO | HAS_DEBUG | | |
a29a8af8 | 642 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ), |
252b5132 | 643 | |
252b5132 | 644 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */ |
6fb72d08 | 645 | #ifdef COFF_WITH_PE |
a29a8af8 | 646 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING |
252b5132 | 647 | #endif |
a29a8af8 | 648 | | SEC_CODE | SEC_DATA | SEC_EXCLUDE ), |
252b5132 RH |
649 | |
650 | #ifdef TARGET_UNDERSCORE | |
651 | TARGET_UNDERSCORE, /* leading underscore */ | |
652 | #else | |
653 | 0, /* leading underscore */ | |
654 | #endif | |
655 | '/', /* ar_pad_char */ | |
656 | 15, /* ar_max_namelen */ | |
0aabe54e | 657 | 0, /* match priority. */ |
252b5132 RH |
658 | |
659 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
660 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
661 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
662 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
663 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
664 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
665 | ||
5fcfd273 | 666 | /* Note that we allow an object file to be treated as a core file as well. */ |
4b24dd1a | 667 | |
156f6ad8 | 668 | #ifdef COFF_CHECK_FORMAT |
d00dd7dc AM |
669 | { /* bfd_check_format */ |
670 | _bfd_dummy_target, | |
671 | COFF_CHECK_FORMAT, | |
672 | bfd_generic_archive_p, | |
673 | COFF_CHECK_FORMAT | |
674 | }, | |
156f6ad8 | 675 | #else |
d00dd7dc AM |
676 | { |
677 | _bfd_dummy_target, | |
678 | coff_object_p, | |
679 | bfd_generic_archive_p, | |
680 | coff_object_p | |
681 | }, | |
156f6ad8 | 682 | #endif |
d00dd7dc AM |
683 | { /* bfd_set_format */ |
684 | _bfd_bool_bfd_false_error, | |
685 | coff_mkobject, | |
686 | _bfd_generic_mkarchive, | |
687 | _bfd_bool_bfd_false_error | |
688 | }, | |
689 | { /* bfd_write_contents */ | |
690 | _bfd_bool_bfd_false_error, | |
691 | coff_write_object_contents, | |
692 | _bfd_write_archive_contents, | |
693 | _bfd_bool_bfd_false_error | |
694 | }, | |
695 | ||
696 | BFD_JUMP_TABLE_GENERIC (coff), | |
697 | BFD_JUMP_TABLE_COPY (coff), | |
698 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
699 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), | |
700 | BFD_JUMP_TABLE_SYMBOLS (coff), | |
701 | BFD_JUMP_TABLE_RELOCS (coff), | |
702 | BFD_JUMP_TABLE_WRITE (coff), | |
703 | BFD_JUMP_TABLE_LINK (coff), | |
704 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
252b5132 | 705 | |
c3c89269 | 706 | NULL, |
b7be1db6 | 707 | |
c3c89269 | 708 | COFF_SWAP_TABLE |
252b5132 | 709 | }; |