| 1 | /* BFD back-end for Intel 386 COFF files. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 3 | 2000, 2001, 2002, 2003, 2004, 2007 |
| 4 | Free Software Foundation, Inc. |
| 5 | Written by Cygnus Support. |
| 6 | |
| 7 | This file is part of BFD, the Binary File Descriptor library. |
| 8 | |
| 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 |
| 11 | the Free Software Foundation; either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 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. |
| 18 | |
| 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 |
| 21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 22 | MA 02110-1301, USA. */ |
| 23 | |
| 24 | #include "sysdep.h" |
| 25 | #include "bfd.h" |
| 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 | |
| 40 | #include "libcoff.h" |
| 41 | |
| 42 | static bfd_reloc_status_type coff_i386_reloc |
| 43 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); |
| 44 | static reloc_howto_type *coff_i386_rtype_to_howto |
| 45 | PARAMS ((bfd *, asection *, struct internal_reloc *, |
| 46 | struct coff_link_hash_entry *, struct internal_syment *, |
| 47 | bfd_vma *)); |
| 48 | static reloc_howto_type *coff_i386_reloc_type_lookup |
| 49 | PARAMS ((bfd *, bfd_reloc_code_real_type)); |
| 50 | |
| 51 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2) |
| 52 | /* The page size is a guess based on ELF. */ |
| 53 | |
| 54 | #define COFF_PAGE_SIZE 0x1000 |
| 55 | |
| 56 | /* For some reason when using i386 COFF the value stored in the .text |
| 57 | section for a reference to a common symbol is the value itself plus |
| 58 | any desired offset. Ian Taylor, Cygnus Support. */ |
| 59 | |
| 60 | /* If we are producing relocatable output, we need to do some |
| 61 | adjustments to the object file that are not done by the |
| 62 | bfd_perform_relocation function. This function is called by every |
| 63 | reloc type to make any required adjustments. */ |
| 64 | |
| 65 | static bfd_reloc_status_type |
| 66 | coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd, |
| 67 | error_message) |
| 68 | bfd *abfd; |
| 69 | arelent *reloc_entry; |
| 70 | asymbol *symbol; |
| 71 | PTR data; |
| 72 | asection *input_section ATTRIBUTE_UNUSED; |
| 73 | bfd *output_bfd; |
| 74 | char **error_message ATTRIBUTE_UNUSED; |
| 75 | { |
| 76 | symvalue diff; |
| 77 | |
| 78 | #ifndef COFF_WITH_PE |
| 79 | if (output_bfd == (bfd *) NULL) |
| 80 | return bfd_reloc_continue; |
| 81 | #endif |
| 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 |
| 107 | relocatable output. This seems to be always wrong for 386 |
| 108 | COFF, so we handle the addend here instead. */ |
| 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. */ |
| 121 | if (howto->pc_relative && howto->pcrel_offset) |
| 122 | diff = -(1 << howto->size); |
| 123 | else if (symbol->flags & BSF_WEAK) |
| 124 | diff = reloc_entry->addend - symbol->value; |
| 125 | else |
| 126 | diff = -reloc_entry->addend; |
| 127 | } |
| 128 | else |
| 129 | #endif |
| 130 | diff = reloc_entry->addend; |
| 131 | } |
| 132 | |
| 133 | #ifdef COFF_WITH_PE |
| 134 | /* FIXME: How should this case be handled? */ |
| 135 | if (reloc_entry->howto->type == R_IMAGEBASE |
| 136 | && output_bfd != NULL |
| 137 | && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour) |
| 138 | diff -= pe_data (output_bfd)->pe_opthdr.ImageBase; |
| 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); |
| 163 | bfd_put_16 (abfd, (bfd_vma) x, addr); |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | case 2: |
| 168 | { |
| 169 | long x = bfd_get_32 (abfd, addr); |
| 170 | DOIT (x); |
| 171 | bfd_put_32 (abfd, (bfd_vma) x, addr); |
| 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 |
| 185 | /* Return TRUE if this relocation should appear in the output .reloc |
| 186 | section. */ |
| 187 | |
| 188 | static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *)); |
| 189 | |
| 190 | static bfd_boolean in_reloc_p (abfd, howto) |
| 191 | bfd * abfd ATTRIBUTE_UNUSED; |
| 192 | reloc_howto_type *howto; |
| 193 | { |
| 194 | return ! howto->pc_relative && howto->type != R_IMAGEBASE; |
| 195 | } |
| 196 | #endif /* COFF_WITH_PE */ |
| 197 | |
| 198 | #ifndef PCRELOFFSET |
| 199 | #define PCRELOFFSET FALSE |
| 200 | #endif |
| 201 | |
| 202 | static reloc_howto_type howto_table[] = |
| 203 | { |
| 204 | EMPTY_HOWTO (0), |
| 205 | EMPTY_HOWTO (1), |
| 206 | EMPTY_HOWTO (2), |
| 207 | EMPTY_HOWTO (3), |
| 208 | EMPTY_HOWTO (4), |
| 209 | EMPTY_HOWTO (5), |
| 210 | HOWTO (R_DIR32, /* type */ |
| 211 | 0, /* rightshift */ |
| 212 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 213 | 32, /* bitsize */ |
| 214 | FALSE, /* pc_relative */ |
| 215 | 0, /* bitpos */ |
| 216 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 217 | coff_i386_reloc, /* special_function */ |
| 218 | "dir32", /* name */ |
| 219 | TRUE, /* partial_inplace */ |
| 220 | 0xffffffff, /* src_mask */ |
| 221 | 0xffffffff, /* dst_mask */ |
| 222 | TRUE), /* pcrel_offset */ |
| 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 */ |
| 228 | FALSE, /* pc_relative */ |
| 229 | 0, /* bitpos */ |
| 230 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 231 | coff_i386_reloc, /* special_function */ |
| 232 | "rva32", /* name */ |
| 233 | TRUE, /* partial_inplace */ |
| 234 | 0xffffffff, /* src_mask */ |
| 235 | 0xffffffff, /* dst_mask */ |
| 236 | FALSE), /* pcrel_offset */ |
| 237 | EMPTY_HOWTO (010), |
| 238 | EMPTY_HOWTO (011), |
| 239 | EMPTY_HOWTO (012), |
| 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 |
| 256 | EMPTY_HOWTO (013), |
| 257 | #endif |
| 258 | EMPTY_HOWTO (014), |
| 259 | EMPTY_HOWTO (015), |
| 260 | EMPTY_HOWTO (016), |
| 261 | /* Byte relocation (017). */ |
| 262 | HOWTO (R_RELBYTE, /* type */ |
| 263 | 0, /* rightshift */ |
| 264 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 265 | 8, /* bitsize */ |
| 266 | FALSE, /* pc_relative */ |
| 267 | 0, /* bitpos */ |
| 268 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 269 | coff_i386_reloc, /* special_function */ |
| 270 | "8", /* name */ |
| 271 | TRUE, /* partial_inplace */ |
| 272 | 0x000000ff, /* src_mask */ |
| 273 | 0x000000ff, /* dst_mask */ |
| 274 | PCRELOFFSET), /* pcrel_offset */ |
| 275 | /* 16-bit word relocation (020). */ |
| 276 | HOWTO (R_RELWORD, /* type */ |
| 277 | 0, /* rightshift */ |
| 278 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 279 | 16, /* bitsize */ |
| 280 | FALSE, /* pc_relative */ |
| 281 | 0, /* bitpos */ |
| 282 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 283 | coff_i386_reloc, /* special_function */ |
| 284 | "16", /* name */ |
| 285 | TRUE, /* partial_inplace */ |
| 286 | 0x0000ffff, /* src_mask */ |
| 287 | 0x0000ffff, /* dst_mask */ |
| 288 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 294 | FALSE, /* pc_relative */ |
| 295 | 0, /* bitpos */ |
| 296 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 297 | coff_i386_reloc, /* special_function */ |
| 298 | "32", /* name */ |
| 299 | TRUE, /* partial_inplace */ |
| 300 | 0xffffffff, /* src_mask */ |
| 301 | 0xffffffff, /* dst_mask */ |
| 302 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 308 | TRUE, /* pc_relative */ |
| 309 | 0, /* bitpos */ |
| 310 | complain_overflow_signed, /* complain_on_overflow */ |
| 311 | coff_i386_reloc, /* special_function */ |
| 312 | "DISP8", /* name */ |
| 313 | TRUE, /* partial_inplace */ |
| 314 | 0x000000ff, /* src_mask */ |
| 315 | 0x000000ff, /* dst_mask */ |
| 316 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 322 | TRUE, /* pc_relative */ |
| 323 | 0, /* bitpos */ |
| 324 | complain_overflow_signed, /* complain_on_overflow */ |
| 325 | coff_i386_reloc, /* special_function */ |
| 326 | "DISP16", /* name */ |
| 327 | TRUE, /* partial_inplace */ |
| 328 | 0x0000ffff, /* src_mask */ |
| 329 | 0x0000ffff, /* dst_mask */ |
| 330 | PCRELOFFSET), /* pcrel_offset */ |
| 331 | /* 32-bit longword PC relative relocation (024). */ |
| 332 | HOWTO (R_PCRLONG, /* type */ |
| 333 | 0, /* rightshift */ |
| 334 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 335 | 32, /* bitsize */ |
| 336 | TRUE, /* pc_relative */ |
| 337 | 0, /* bitpos */ |
| 338 | complain_overflow_signed, /* complain_on_overflow */ |
| 339 | coff_i386_reloc, /* special_function */ |
| 340 | "DISP32", /* name */ |
| 341 | TRUE, /* partial_inplace */ |
| 342 | 0xffffffff, /* src_mask */ |
| 343 | 0xffffffff, /* dst_mask */ |
| 344 | PCRELOFFSET) /* pcrel_offset */ |
| 345 | }; |
| 346 | |
| 347 | /* Turn a howto into a reloc nunmber */ |
| 348 | |
| 349 | #define SELECT_RELOC(x,howto) { x.r_type = howto->type; } |
| 350 | #define BADMAG(x) I386BADMAG(x) |
| 351 | #define I386 1 /* Customize coffcode.h */ |
| 352 | |
| 353 | #define RTYPE2HOWTO(cache_ptr, dst) \ |
| 354 | ((cache_ptr)->howto = \ |
| 355 | ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \ |
| 356 | ? howto_table + (dst)->r_type \ |
| 357 | : NULL)) |
| 358 | |
| 359 | /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared |
| 360 | library. On some other COFF targets STYP_BSS is normally |
| 361 | STYP_NOLOAD. */ |
| 362 | #define BSS_NOLOAD_IS_SHARED_LIBRARY |
| 363 | |
| 364 | /* Compute the addend of a reloc. If the reloc is to a common symbol, |
| 365 | the object file contains the value of the common symbol. By the |
| 366 | time this is called, the linker may be using a different symbol |
| 367 | from a different object file with a different value. Therefore, we |
| 368 | hack wildly to locate the original symbol from this file so that we |
| 369 | can make the correct adjustment. This macro sets coffsym to the |
| 370 | symbol from the original file, and uses it to set the addend value |
| 371 | correctly. If this is not a common symbol, the usual addend |
| 372 | calculation is done, except that an additional tweak is needed for |
| 373 | PC relative relocs. |
| 374 | FIXME: This macro refers to symbols and asect; these are from the |
| 375 | calling function, not the macro arguments. */ |
| 376 | |
| 377 | #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \ |
| 378 | { \ |
| 379 | coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \ |
| 380 | if (ptr && bfd_asymbol_bfd (ptr) != abfd) \ |
| 381 | coffsym = (obj_symbols (abfd) \ |
| 382 | + (cache_ptr->sym_ptr_ptr - symbols)); \ |
| 383 | else if (ptr) \ |
| 384 | coffsym = coff_symbol_from (abfd, ptr); \ |
| 385 | if (coffsym != (coff_symbol_type *) NULL \ |
| 386 | && coffsym->native->u.syment.n_scnum == 0) \ |
| 387 | cache_ptr->addend = - coffsym->native->u.syment.n_value; \ |
| 388 | else if (ptr && bfd_asymbol_bfd (ptr) == abfd \ |
| 389 | && ptr->section != (asection *) NULL) \ |
| 390 | cache_ptr->addend = - (ptr->section->vma + ptr->value); \ |
| 391 | else \ |
| 392 | cache_ptr->addend = 0; \ |
| 393 | if (ptr && howto_table[reloc.r_type].pc_relative) \ |
| 394 | cache_ptr->addend += asect->vma; \ |
| 395 | } |
| 396 | |
| 397 | /* We use the special COFF backend linker. For normal i386 COFF, we |
| 398 | can use the generic relocate_section routine. For PE, we need our |
| 399 | own routine. */ |
| 400 | |
| 401 | #ifndef COFF_WITH_PE |
| 402 | |
| 403 | #define coff_relocate_section _bfd_coff_generic_relocate_section |
| 404 | |
| 405 | #else /* COFF_WITH_PE */ |
| 406 | |
| 407 | /* The PE relocate section routine. The only difference between this |
| 408 | and the regular routine is that we don't want to do anything for a |
| 409 | relocatable link. */ |
| 410 | |
| 411 | static bfd_boolean coff_pe_i386_relocate_section |
| 412 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
| 413 | struct internal_reloc *, struct internal_syment *, asection **)); |
| 414 | |
| 415 | static bfd_boolean |
| 416 | coff_pe_i386_relocate_section (output_bfd, info, input_bfd, |
| 417 | input_section, contents, relocs, syms, |
| 418 | sections) |
| 419 | 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; |
| 427 | { |
| 428 | if (info->relocatable) |
| 429 | return TRUE; |
| 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 * |
| 443 | coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp) |
| 444 | bfd *abfd ATTRIBUTE_UNUSED; |
| 445 | asection *sec; |
| 446 | struct internal_reloc *rel; |
| 447 | struct coff_link_hash_entry *h; |
| 448 | struct internal_syment *sym; |
| 449 | bfd_vma *addendp; |
| 450 | { |
| 451 | reloc_howto_type *howto; |
| 452 | |
| 453 | if (rel->r_type >= sizeof (howto_table) / sizeof (howto_table[0])) |
| 454 | { |
| 455 | bfd_set_error (bfd_error_bad_value); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | howto = howto_table + rel->r_type; |
| 460 | |
| 461 | #ifdef COFF_WITH_PE |
| 462 | /* Cancel out code in _bfd_coff_generic_relocate_section. */ |
| 463 | *addendp = 0; |
| 464 | #endif |
| 465 | |
| 466 | if (howto->pc_relative) |
| 467 | *addendp += sec->vma; |
| 468 | |
| 469 | if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0) |
| 470 | { |
| 471 | /* This is a common symbol. The section contents include the |
| 472 | size (sym->n_value) as an addend. The relocate_section |
| 473 | function will be adding in the final value of the symbol. We |
| 474 | need to subtract out the current size in order to get the |
| 475 | correct result. */ |
| 476 | |
| 477 | BFD_ASSERT (h != NULL); |
| 478 | |
| 479 | #ifndef COFF_WITH_PE |
| 480 | /* I think we *do* want to bypass this. If we don't, I have |
| 481 | seen some data parameters get the wrong relocation address. |
| 482 | If I link two versions with and without this section bypassed |
| 483 | and then do a binary comparison, the addresses which are |
| 484 | different can be looked up in the map. The case in which |
| 485 | this section has been bypassed has addresses which correspond |
| 486 | to values I can find in the map. */ |
| 487 | *addendp -= sym->n_value; |
| 488 | #endif |
| 489 | } |
| 490 | |
| 491 | #ifndef COFF_WITH_PE |
| 492 | /* If the output symbol is common (in which case this must be a |
| 493 | relocatable link), we need to add in the final size of the |
| 494 | common symbol. */ |
| 495 | if (h != NULL && h->root.type == bfd_link_hash_common) |
| 496 | *addendp += h->root.u.c.size; |
| 497 | #endif |
| 498 | |
| 499 | #ifdef COFF_WITH_PE |
| 500 | if (howto->pc_relative) |
| 501 | { |
| 502 | *addendp -= 4; |
| 503 | |
| 504 | /* If the symbol is defined, then the generic code is going to |
| 505 | add back the symbol value in order to cancel out an |
| 506 | adjustment it made to the addend. However, we set the addend |
| 507 | to 0 at the start of this function. We need to adjust here, |
| 508 | to avoid the adjustment the generic code will make. FIXME: |
| 509 | This is getting a bit hackish. */ |
| 510 | if (sym != NULL && sym->n_scnum != 0) |
| 511 | *addendp -= sym->n_value; |
| 512 | } |
| 513 | |
| 514 | if (rel->r_type == R_IMAGEBASE |
| 515 | && (bfd_get_flavour(sec->output_section->owner) |
| 516 | == bfd_target_coff_flavour)) |
| 517 | { |
| 518 | *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase; |
| 519 | } |
| 520 | |
| 521 | BFD_ASSERT (sym != NULL); |
| 522 | if (rel->r_type == R_SECREL32 && sym != NULL) |
| 523 | { |
| 524 | bfd_vma osect_vma; |
| 525 | |
| 526 | if (h && (h->type == bfd_link_hash_defined |
| 527 | || h->type == bfd_link_hash_defweak)) |
| 528 | osect_vma = h->root.u.def.section->output_section->vma; |
| 529 | else |
| 530 | { |
| 531 | asection *sec; |
| 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 | |
| 537 | for (sec = abfd->sections, i = 1; i < sym->n_scnum; i++) |
| 538 | sec = sec->next; |
| 539 | |
| 540 | osect_vma = sec->output_section->vma; |
| 541 | } |
| 542 | |
| 543 | *addendp -= osect_vma; |
| 544 | } |
| 545 | #endif |
| 546 | |
| 547 | return howto; |
| 548 | } |
| 549 | |
| 550 | #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup |
| 551 | #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup |
| 552 | |
| 553 | static reloc_howto_type * |
| 554 | coff_i386_reloc_type_lookup (abfd, code) |
| 555 | bfd *abfd ATTRIBUTE_UNUSED; |
| 556 | bfd_reloc_code_real_type code; |
| 557 | { |
| 558 | switch (code) |
| 559 | { |
| 560 | case BFD_RELOC_RVA: |
| 561 | return howto_table + R_IMAGEBASE; |
| 562 | case BFD_RELOC_32: |
| 563 | return howto_table + R_DIR32; |
| 564 | case BFD_RELOC_32_PCREL: |
| 565 | return howto_table + R_PCRLONG; |
| 566 | case BFD_RELOC_16: |
| 567 | return howto_table + R_RELWORD; |
| 568 | case BFD_RELOC_16_PCREL: |
| 569 | return howto_table + R_PCRWORD; |
| 570 | case BFD_RELOC_8: |
| 571 | return howto_table + R_RELBYTE; |
| 572 | case BFD_RELOC_8_PCREL: |
| 573 | return howto_table + R_PCRBYTE; |
| 574 | #ifdef COFF_WITH_PE |
| 575 | case BFD_RELOC_32_SECREL: |
| 576 | return howto_table + R_SECREL32; |
| 577 | #endif |
| 578 | default: |
| 579 | BFD_FAIL (); |
| 580 | return 0; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static reloc_howto_type * |
| 585 | coff_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 586 | const char *r_name) |
| 587 | { |
| 588 | unsigned int i; |
| 589 | |
| 590 | for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++) |
| 591 | if (howto_table[i].name != NULL |
| 592 | && strcasecmp (howto_table[i].name, r_name) == 0) |
| 593 | return &howto_table[i]; |
| 594 | |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | #define coff_rtype_to_howto coff_i386_rtype_to_howto |
| 599 | |
| 600 | #ifdef TARGET_UNDERSCORE |
| 601 | |
| 602 | /* If i386 gcc uses underscores for symbol names, then it does not use |
| 603 | a leading dot for local labels, so if TARGET_UNDERSCORE is defined |
| 604 | we treat all symbols starting with L as local. */ |
| 605 | |
| 606 | static bfd_boolean coff_i386_is_local_label_name |
| 607 | PARAMS ((bfd *, const char *)); |
| 608 | |
| 609 | static bfd_boolean |
| 610 | coff_i386_is_local_label_name (abfd, name) |
| 611 | bfd *abfd; |
| 612 | const char *name; |
| 613 | { |
| 614 | if (name[0] == 'L') |
| 615 | return TRUE; |
| 616 | |
| 617 | return _bfd_coff_is_local_label_name (abfd, name); |
| 618 | } |
| 619 | |
| 620 | #define coff_bfd_is_local_label_name coff_i386_is_local_label_name |
| 621 | |
| 622 | #endif /* TARGET_UNDERSCORE */ |
| 623 | |
| 624 | #include "coffcode.h" |
| 625 | |
| 626 | const bfd_target |
| 627 | #ifdef TARGET_SYM |
| 628 | TARGET_SYM = |
| 629 | #else |
| 630 | i386coff_vec = |
| 631 | #endif |
| 632 | { |
| 633 | #ifdef TARGET_NAME |
| 634 | TARGET_NAME, |
| 635 | #else |
| 636 | "coff-i386", /* name */ |
| 637 | #endif |
| 638 | bfd_target_coff_flavour, |
| 639 | BFD_ENDIAN_LITTLE, /* data byte order is little */ |
| 640 | BFD_ENDIAN_LITTLE, /* header byte order is little */ |
| 641 | |
| 642 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 643 | HAS_LINENO | HAS_DEBUG | |
| 644 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
| 645 | |
| 646 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */ |
| 647 | #ifdef COFF_WITH_PE |
| 648 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY |
| 649 | #endif |
| 650 | | SEC_CODE | SEC_DATA), |
| 651 | |
| 652 | #ifdef TARGET_UNDERSCORE |
| 653 | TARGET_UNDERSCORE, /* leading underscore */ |
| 654 | #else |
| 655 | 0, /* leading underscore */ |
| 656 | #endif |
| 657 | '/', /* ar_pad_char */ |
| 658 | 15, /* ar_max_namelen */ |
| 659 | |
| 660 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 661 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 662 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ |
| 663 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 664 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 665 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ |
| 666 | |
| 667 | /* Note that we allow an object file to be treated as a core file as well. */ |
| 668 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ |
| 669 | bfd_generic_archive_p, coff_object_p}, |
| 670 | {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */ |
| 671 | bfd_false}, |
| 672 | {bfd_false, coff_write_object_contents, /* bfd_write_contents */ |
| 673 | _bfd_write_archive_contents, bfd_false}, |
| 674 | |
| 675 | BFD_JUMP_TABLE_GENERIC (coff), |
| 676 | BFD_JUMP_TABLE_COPY (coff), |
| 677 | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
| 678 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), |
| 679 | BFD_JUMP_TABLE_SYMBOLS (coff), |
| 680 | BFD_JUMP_TABLE_RELOCS (coff), |
| 681 | BFD_JUMP_TABLE_WRITE (coff), |
| 682 | BFD_JUMP_TABLE_LINK (coff), |
| 683 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 684 | |
| 685 | NULL, |
| 686 | |
| 687 | COFF_SWAP_TABLE |
| 688 | }; |