| 1 | /* BFD back-end for Intel 386 COFF files. |
| 2 | Copyright (C) 1990-2020 Free Software Foundation, Inc. |
| 3 | Written by Cygnus Support. |
| 4 | |
| 5 | This file is part of BFD, the Binary File Descriptor library. |
| 6 | |
| 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 |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 20 | MA 02110-1301, USA. */ |
| 21 | |
| 22 | #include "sysdep.h" |
| 23 | #include "bfd.h" |
| 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 | |
| 38 | #ifndef bfd_pe_print_pdata |
| 39 | #define bfd_pe_print_pdata NULL |
| 40 | #endif |
| 41 | |
| 42 | #include "libcoff.h" |
| 43 | |
| 44 | /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */ |
| 45 | #define OCTETS_PER_BYTE(ABFD, SEC) 1 |
| 46 | |
| 47 | static reloc_howto_type *coff_i386_rtype_to_howto |
| 48 | (bfd *, asection *, struct internal_reloc *, |
| 49 | struct coff_link_hash_entry *, struct internal_syment *, |
| 50 | bfd_vma *); |
| 51 | static reloc_howto_type *coff_i386_reloc_type_lookup |
| 52 | (bfd *, bfd_reloc_code_real_type); |
| 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 | |
| 63 | /* If we are producing relocatable output, we need to do some |
| 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 |
| 69 | coff_i386_reloc (bfd *abfd, |
| 70 | arelent *reloc_entry, |
| 71 | asymbol *symbol, |
| 72 | void * data, |
| 73 | asection *input_section, |
| 74 | bfd *output_bfd, |
| 75 | char **error_message ATTRIBUTE_UNUSED) |
| 76 | { |
| 77 | symvalue diff; |
| 78 | |
| 79 | #ifndef COFF_WITH_PE |
| 80 | if (output_bfd == (bfd *) NULL) |
| 81 | return bfd_reloc_continue; |
| 82 | #endif |
| 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 |
| 108 | relocatable output. This seems to be always wrong for 386 |
| 109 | COFF, so we handle the addend here instead. */ |
| 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. */ |
| 122 | if (howto->pc_relative && howto->pcrel_offset) |
| 123 | diff = -(1 << howto->size); |
| 124 | else if (symbol->flags & BSF_WEAK) |
| 125 | diff = reloc_entry->addend - symbol->value; |
| 126 | else |
| 127 | diff = -reloc_entry->addend; |
| 128 | } |
| 129 | else |
| 130 | #endif |
| 131 | diff = reloc_entry->addend; |
| 132 | } |
| 133 | |
| 134 | #ifdef COFF_WITH_PE |
| 135 | /* FIXME: How should this case be handled? */ |
| 136 | if (reloc_entry->howto->type == R_IMAGEBASE |
| 137 | && output_bfd != NULL |
| 138 | && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour) |
| 139 | diff -= pe_data (output_bfd)->pe_opthdr.ImageBase; |
| 140 | #endif |
| 141 | |
| 142 | #define DOIT(x) \ |
| 143 | x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask)) |
| 144 | |
| 145 | if (diff != 0) |
| 146 | { |
| 147 | reloc_howto_type *howto = reloc_entry->howto; |
| 148 | bfd_size_type octets = (reloc_entry->address |
| 149 | * OCTETS_PER_BYTE (abfd, input_section)); |
| 150 | unsigned char *addr = (unsigned char *) data + octets; |
| 151 | |
| 152 | if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets)) |
| 153 | return bfd_reloc_outofrange; |
| 154 | |
| 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; |
| 164 | |
| 165 | case 1: |
| 166 | { |
| 167 | short x = bfd_get_16 (abfd, addr); |
| 168 | DOIT (x); |
| 169 | bfd_put_16 (abfd, (bfd_vma) x, addr); |
| 170 | } |
| 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 | } |
| 185 | |
| 186 | /* Now let bfd_perform_relocation finish everything up. */ |
| 187 | return bfd_reloc_continue; |
| 188 | } |
| 189 | |
| 190 | #ifdef COFF_WITH_PE |
| 191 | /* Return TRUE if this relocation should appear in the output .reloc |
| 192 | section. */ |
| 193 | |
| 194 | static bfd_boolean in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, |
| 195 | reloc_howto_type *howto) |
| 196 | { |
| 197 | return ! howto->pc_relative && howto->type != R_IMAGEBASE |
| 198 | && howto->type != R_SECREL32; |
| 199 | } |
| 200 | #endif /* COFF_WITH_PE */ |
| 201 | |
| 202 | #ifndef PCRELOFFSET |
| 203 | #define PCRELOFFSET FALSE |
| 204 | #endif |
| 205 | |
| 206 | static reloc_howto_type howto_table[] = |
| 207 | { |
| 208 | EMPTY_HOWTO (0), |
| 209 | EMPTY_HOWTO (1), |
| 210 | EMPTY_HOWTO (2), |
| 211 | EMPTY_HOWTO (3), |
| 212 | EMPTY_HOWTO (4), |
| 213 | EMPTY_HOWTO (5), |
| 214 | HOWTO (R_DIR32, /* type */ |
| 215 | 0, /* rightshift */ |
| 216 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 217 | 32, /* bitsize */ |
| 218 | FALSE, /* pc_relative */ |
| 219 | 0, /* bitpos */ |
| 220 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 221 | coff_i386_reloc, /* special_function */ |
| 222 | "dir32", /* name */ |
| 223 | TRUE, /* partial_inplace */ |
| 224 | 0xffffffff, /* src_mask */ |
| 225 | 0xffffffff, /* dst_mask */ |
| 226 | TRUE), /* pcrel_offset */ |
| 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 */ |
| 232 | FALSE, /* pc_relative */ |
| 233 | 0, /* bitpos */ |
| 234 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 235 | coff_i386_reloc, /* special_function */ |
| 236 | "rva32", /* name */ |
| 237 | TRUE, /* partial_inplace */ |
| 238 | 0xffffffff, /* src_mask */ |
| 239 | 0xffffffff, /* dst_mask */ |
| 240 | FALSE), /* pcrel_offset */ |
| 241 | EMPTY_HOWTO (010), |
| 242 | EMPTY_HOWTO (011), |
| 243 | EMPTY_HOWTO (012), |
| 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 |
| 260 | EMPTY_HOWTO (013), |
| 261 | #endif |
| 262 | EMPTY_HOWTO (014), |
| 263 | EMPTY_HOWTO (015), |
| 264 | EMPTY_HOWTO (016), |
| 265 | /* Byte relocation (017). */ |
| 266 | HOWTO (R_RELBYTE, /* type */ |
| 267 | 0, /* rightshift */ |
| 268 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 269 | 8, /* bitsize */ |
| 270 | FALSE, /* pc_relative */ |
| 271 | 0, /* bitpos */ |
| 272 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 273 | coff_i386_reloc, /* special_function */ |
| 274 | "8", /* name */ |
| 275 | TRUE, /* partial_inplace */ |
| 276 | 0x000000ff, /* src_mask */ |
| 277 | 0x000000ff, /* dst_mask */ |
| 278 | PCRELOFFSET), /* pcrel_offset */ |
| 279 | /* 16-bit word relocation (020). */ |
| 280 | HOWTO (R_RELWORD, /* type */ |
| 281 | 0, /* rightshift */ |
| 282 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 283 | 16, /* bitsize */ |
| 284 | FALSE, /* pc_relative */ |
| 285 | 0, /* bitpos */ |
| 286 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 287 | coff_i386_reloc, /* special_function */ |
| 288 | "16", /* name */ |
| 289 | TRUE, /* partial_inplace */ |
| 290 | 0x0000ffff, /* src_mask */ |
| 291 | 0x0000ffff, /* dst_mask */ |
| 292 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 298 | FALSE, /* pc_relative */ |
| 299 | 0, /* bitpos */ |
| 300 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 301 | coff_i386_reloc, /* special_function */ |
| 302 | "32", /* name */ |
| 303 | TRUE, /* partial_inplace */ |
| 304 | 0xffffffff, /* src_mask */ |
| 305 | 0xffffffff, /* dst_mask */ |
| 306 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 312 | TRUE, /* pc_relative */ |
| 313 | 0, /* bitpos */ |
| 314 | complain_overflow_signed, /* complain_on_overflow */ |
| 315 | coff_i386_reloc, /* special_function */ |
| 316 | "DISP8", /* name */ |
| 317 | TRUE, /* partial_inplace */ |
| 318 | 0x000000ff, /* src_mask */ |
| 319 | 0x000000ff, /* dst_mask */ |
| 320 | PCRELOFFSET), /* pcrel_offset */ |
| 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 */ |
| 326 | TRUE, /* pc_relative */ |
| 327 | 0, /* bitpos */ |
| 328 | complain_overflow_signed, /* complain_on_overflow */ |
| 329 | coff_i386_reloc, /* special_function */ |
| 330 | "DISP16", /* name */ |
| 331 | TRUE, /* partial_inplace */ |
| 332 | 0x0000ffff, /* src_mask */ |
| 333 | 0x0000ffff, /* dst_mask */ |
| 334 | PCRELOFFSET), /* pcrel_offset */ |
| 335 | /* 32-bit longword PC relative relocation (024). */ |
| 336 | HOWTO (R_PCRLONG, /* type */ |
| 337 | 0, /* rightshift */ |
| 338 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 339 | 32, /* bitsize */ |
| 340 | TRUE, /* pc_relative */ |
| 341 | 0, /* bitpos */ |
| 342 | complain_overflow_signed, /* complain_on_overflow */ |
| 343 | coff_i386_reloc, /* special_function */ |
| 344 | "DISP32", /* name */ |
| 345 | TRUE, /* partial_inplace */ |
| 346 | 0xffffffff, /* src_mask */ |
| 347 | 0xffffffff, /* dst_mask */ |
| 348 | PCRELOFFSET) /* pcrel_offset */ |
| 349 | }; |
| 350 | |
| 351 | #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0])) |
| 352 | |
| 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 | |
| 359 | #define RTYPE2HOWTO(cache_ptr, dst) \ |
| 360 | ((cache_ptr)->howto = \ |
| 361 | ((dst)->r_type < NUM_HOWTOS \ |
| 362 | ? howto_table + (dst)->r_type \ |
| 363 | : NULL)) |
| 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) \ |
| 388 | + (cache_ptr->sym_ptr_ptr - symbols)); \ |
| 389 | else if (ptr) \ |
| 390 | coffsym = coff_symbol_from (ptr); \ |
| 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; \ |
| 399 | if (ptr && reloc.r_type < NUM_HOWTOS \ |
| 400 | && howto_table[reloc.r_type].pc_relative) \ |
| 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 |
| 416 | relocatable link. */ |
| 417 | |
| 418 | static bfd_boolean |
| 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) |
| 427 | { |
| 428 | if (bfd_link_relocatable (info)) |
| 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 (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) |
| 449 | { |
| 450 | reloc_howto_type *howto; |
| 451 | |
| 452 | if (rel->r_type >= NUM_HOWTOS) |
| 453 | { |
| 454 | bfd_set_error (bfd_error_bad_value); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | howto = howto_table + rel->r_type; |
| 459 | |
| 460 | #ifdef COFF_WITH_PE |
| 461 | /* Cancel out code in _bfd_coff_generic_relocate_section. */ |
| 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. */ |
| 475 | |
| 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 |
| 492 | relocatable link), we need to add in the final size of the |
| 493 | common symbol. */ |
| 494 | if (h != NULL && h->root.type == bfd_link_hash_common) |
| 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 |
| 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. */ |
| 509 | if (sym != NULL && sym->n_scnum != 0) |
| 510 | *addendp -= sym->n_value; |
| 511 | } |
| 512 | |
| 513 | if (rel->r_type == R_IMAGEBASE |
| 514 | && (bfd_get_flavour(sec->output_section->owner) |
| 515 | == bfd_target_coff_flavour)) |
| 516 | { |
| 517 | *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase; |
| 518 | } |
| 519 | |
| 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 | |
| 526 | if (rel->r_type == R_SECREL32 && sym != NULL) |
| 527 | { |
| 528 | bfd_vma osect_vma; |
| 529 | |
| 530 | if (h && (h->root.type == bfd_link_hash_defined |
| 531 | || h->root.type == bfd_link_hash_defweak)) |
| 532 | osect_vma = h->root.u.def.section->output_section->vma; |
| 533 | else |
| 534 | { |
| 535 | asection *s; |
| 536 | int i; |
| 537 | |
| 538 | /* Sigh, the only way to get the section to offset against |
| 539 | is to find it the hard way. */ |
| 540 | |
| 541 | for (s = abfd->sections, i = 1; i < sym->n_scnum; i++) |
| 542 | s = s->next; |
| 543 | |
| 544 | osect_vma = s->output_section->vma; |
| 545 | } |
| 546 | |
| 547 | *addendp -= osect_vma; |
| 548 | } |
| 549 | #endif |
| 550 | |
| 551 | return howto; |
| 552 | } |
| 553 | |
| 554 | #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup |
| 555 | #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup |
| 556 | |
| 557 | static reloc_howto_type * |
| 558 | coff_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 559 | bfd_reloc_code_real_type code) |
| 560 | { |
| 561 | switch (code) |
| 562 | { |
| 563 | case BFD_RELOC_RVA: |
| 564 | return howto_table + R_IMAGEBASE; |
| 565 | case BFD_RELOC_32: |
| 566 | return howto_table + R_DIR32; |
| 567 | case BFD_RELOC_32_PCREL: |
| 568 | return howto_table + R_PCRLONG; |
| 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; |
| 577 | #ifdef COFF_WITH_PE |
| 578 | case BFD_RELOC_32_SECREL: |
| 579 | return howto_table + R_SECREL32; |
| 580 | #endif |
| 581 | default: |
| 582 | BFD_FAIL (); |
| 583 | return 0; |
| 584 | } |
| 585 | } |
| 586 | |
| 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 | |
| 593 | for (i = 0; i < NUM_HOWTOS; i++) |
| 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 | |
| 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 | |
| 609 | static bfd_boolean |
| 610 | coff_i386_is_local_label_name (bfd *abfd, const char *name) |
| 611 | { |
| 612 | if (name[0] == 'L') |
| 613 | return TRUE; |
| 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 | |
| 624 | const bfd_target |
| 625 | #ifdef TARGET_SYM |
| 626 | TARGET_SYM = |
| 627 | #else |
| 628 | i386_coff_vec = |
| 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 | |
| 642 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ), |
| 643 | |
| 644 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */ |
| 645 | #ifdef COFF_WITH_PE |
| 646 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING |
| 647 | #endif |
| 648 | | SEC_CODE | SEC_DATA | SEC_EXCLUDE ), |
| 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 */ |
| 657 | 0, /* match priority. */ |
| 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 | |
| 666 | /* Note that we allow an object file to be treated as a core file as well. */ |
| 667 | |
| 668 | #ifdef COFF_CHECK_FORMAT |
| 669 | { /* bfd_check_format */ |
| 670 | _bfd_dummy_target, |
| 671 | COFF_CHECK_FORMAT, |
| 672 | bfd_generic_archive_p, |
| 673 | COFF_CHECK_FORMAT |
| 674 | }, |
| 675 | #else |
| 676 | { |
| 677 | _bfd_dummy_target, |
| 678 | coff_object_p, |
| 679 | bfd_generic_archive_p, |
| 680 | coff_object_p |
| 681 | }, |
| 682 | #endif |
| 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), |
| 705 | |
| 706 | NULL, |
| 707 | |
| 708 | COFF_SWAP_TABLE |
| 709 | }; |