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