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