1 /* BFD back-end for Intel 386 COFF files.
2 Copyright (C) 1990-2017 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
26 #include "coff/i386.h"
28 #include "coff/internal.h"
35 #include "coff/go32exe.h"
38 #ifndef bfd_pe_print_pdata
39 #define bfd_pe_print_pdata NULL
44 static reloc_howto_type
*coff_i386_rtype_to_howto
45 (bfd
*, asection
*, struct internal_reloc
*,
46 struct coff_link_hash_entry
*, struct internal_syment
*,
48 static reloc_howto_type
*coff_i386_reloc_type_lookup
49 (bfd
*, bfd_reloc_code_real_type
);
51 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
52 /* The page size is a guess based on ELF. */
54 #define COFF_PAGE_SIZE 0x1000
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. */
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. */
65 static bfd_reloc_status_type
66 coff_i386_reloc (bfd
*abfd
,
70 asection
*input_section ATTRIBUTE_UNUSED
,
72 char **error_message ATTRIBUTE_UNUSED
)
77 if (output_bfd
== (bfd
*) NULL
)
78 return bfd_reloc_continue
;
81 if (bfd_is_com_section (symbol
->section
))
84 /* We are relocating a common symbol. The current value in the
85 object file is ORIG + OFFSET, where ORIG is the value of the
86 common symbol as seen by the object file when it was compiled
87 (this may be zero if the symbol was undefined) and OFFSET is
88 the offset into the common symbol (normally zero, but may be
89 non-zero when referring to a field in a common structure).
90 ORIG is the negative of reloc_entry->addend, which is set by
91 the CALC_ADDEND macro below. We want to replace the value in
92 the object file with NEW + OFFSET, where NEW is the value of
93 the common symbol which we are going to put in the final
94 object file. NEW is symbol->value. */
95 diff
= symbol
->value
+ reloc_entry
->addend
;
97 /* In PE mode, we do not offset the common symbol. */
98 diff
= reloc_entry
->addend
;
103 /* For some reason bfd_perform_relocation always effectively
104 ignores the addend for a COFF target when producing
105 relocatable output. This seems to be always wrong for 386
106 COFF, so we handle the addend here instead. */
108 if (output_bfd
== (bfd
*) NULL
)
110 reloc_howto_type
*howto
= reloc_entry
->howto
;
112 /* Although PC relative relocations are very similar between
113 PE and non-PE formats, but they are off by 1 << howto->size
114 bytes. For the external relocation, PE is very different
115 from others. See md_apply_fix3 () in gas/config/tc-i386.c.
116 When we link PE and non-PE object files together to
117 generate a non-PE executable, we have to compensate it
119 if (howto
->pc_relative
&& howto
->pcrel_offset
)
120 diff
= -(1 << howto
->size
);
121 else if (symbol
->flags
& BSF_WEAK
)
122 diff
= reloc_entry
->addend
- symbol
->value
;
124 diff
= -reloc_entry
->addend
;
128 diff
= reloc_entry
->addend
;
132 /* FIXME: How should this case be handled? */
133 if (reloc_entry
->howto
->type
== R_IMAGEBASE
134 && output_bfd
!= NULL
135 && bfd_get_flavour(output_bfd
) == bfd_target_coff_flavour
)
136 diff
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
140 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
144 reloc_howto_type
*howto
= reloc_entry
->howto
;
145 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
151 char x
= bfd_get_8 (abfd
, addr
);
153 bfd_put_8 (abfd
, x
, addr
);
159 short x
= bfd_get_16 (abfd
, addr
);
161 bfd_put_16 (abfd
, (bfd_vma
) x
, addr
);
167 long x
= bfd_get_32 (abfd
, addr
);
169 bfd_put_32 (abfd
, (bfd_vma
) x
, addr
);
178 /* Now let bfd_perform_relocation finish everything up. */
179 return bfd_reloc_continue
;
183 /* Return TRUE if this relocation should appear in the output .reloc
186 static bfd_boolean
in_reloc_p (bfd
* abfd ATTRIBUTE_UNUSED
,
187 reloc_howto_type
*howto
)
189 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
190 && howto
->type
!= R_SECREL32
;
192 #endif /* COFF_WITH_PE */
195 #define PCRELOFFSET FALSE
198 static reloc_howto_type howto_table
[] =
206 HOWTO (R_DIR32
, /* type */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
210 FALSE
, /* pc_relative */
212 complain_overflow_bitfield
, /* complain_on_overflow */
213 coff_i386_reloc
, /* special_function */
215 TRUE
, /* partial_inplace */
216 0xffffffff, /* src_mask */
217 0xffffffff, /* dst_mask */
218 TRUE
), /* pcrel_offset */
219 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
220 HOWTO (R_IMAGEBASE
, /* type */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
224 FALSE
, /* pc_relative */
226 complain_overflow_bitfield
, /* complain_on_overflow */
227 coff_i386_reloc
, /* special_function */
229 TRUE
, /* partial_inplace */
230 0xffffffff, /* src_mask */
231 0xffffffff, /* dst_mask */
232 FALSE
), /* pcrel_offset */
237 /* 32-bit longword section relative relocation (013). */
238 HOWTO (R_SECREL32
, /* type */
240 2, /* size (0 = byte, 1 = short, 2 = long) */
242 FALSE
, /* pc_relative */
244 complain_overflow_bitfield
, /* complain_on_overflow */
245 coff_i386_reloc
, /* special_function */
246 "secrel32", /* name */
247 TRUE
, /* partial_inplace */
248 0xffffffff, /* src_mask */
249 0xffffffff, /* dst_mask */
250 TRUE
), /* pcrel_offset */
257 /* Byte relocation (017). */
258 HOWTO (R_RELBYTE
, /* type */
260 0, /* size (0 = byte, 1 = short, 2 = long) */
262 FALSE
, /* pc_relative */
264 complain_overflow_bitfield
, /* complain_on_overflow */
265 coff_i386_reloc
, /* special_function */
267 TRUE
, /* partial_inplace */
268 0x000000ff, /* src_mask */
269 0x000000ff, /* dst_mask */
270 PCRELOFFSET
), /* pcrel_offset */
271 /* 16-bit word relocation (020). */
272 HOWTO (R_RELWORD
, /* type */
274 1, /* size (0 = byte, 1 = short, 2 = long) */
276 FALSE
, /* pc_relative */
278 complain_overflow_bitfield
, /* complain_on_overflow */
279 coff_i386_reloc
, /* special_function */
281 TRUE
, /* partial_inplace */
282 0x0000ffff, /* src_mask */
283 0x0000ffff, /* dst_mask */
284 PCRELOFFSET
), /* pcrel_offset */
285 /* 32-bit longword relocation (021). */
286 HOWTO (R_RELLONG
, /* type */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
290 FALSE
, /* pc_relative */
292 complain_overflow_bitfield
, /* complain_on_overflow */
293 coff_i386_reloc
, /* special_function */
295 TRUE
, /* partial_inplace */
296 0xffffffff, /* src_mask */
297 0xffffffff, /* dst_mask */
298 PCRELOFFSET
), /* pcrel_offset */
299 /* Byte PC relative relocation (022). */
300 HOWTO (R_PCRBYTE
, /* type */
302 0, /* size (0 = byte, 1 = short, 2 = long) */
304 TRUE
, /* pc_relative */
306 complain_overflow_signed
, /* complain_on_overflow */
307 coff_i386_reloc
, /* special_function */
309 TRUE
, /* partial_inplace */
310 0x000000ff, /* src_mask */
311 0x000000ff, /* dst_mask */
312 PCRELOFFSET
), /* pcrel_offset */
313 /* 16-bit word PC relative relocation (023). */
314 HOWTO (R_PCRWORD
, /* type */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
318 TRUE
, /* pc_relative */
320 complain_overflow_signed
, /* complain_on_overflow */
321 coff_i386_reloc
, /* special_function */
323 TRUE
, /* partial_inplace */
324 0x0000ffff, /* src_mask */
325 0x0000ffff, /* dst_mask */
326 PCRELOFFSET
), /* pcrel_offset */
327 /* 32-bit longword PC relative relocation (024). */
328 HOWTO (R_PCRLONG
, /* type */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
332 TRUE
, /* pc_relative */
334 complain_overflow_signed
, /* complain_on_overflow */
335 coff_i386_reloc
, /* special_function */
337 TRUE
, /* partial_inplace */
338 0xffffffff, /* src_mask */
339 0xffffffff, /* dst_mask */
340 PCRELOFFSET
) /* pcrel_offset */
343 #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
345 /* Turn a howto into a reloc nunmber */
347 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
348 #define BADMAG(x) I386BADMAG(x)
349 #define I386 1 /* Customize coffcode.h */
351 #define RTYPE2HOWTO(cache_ptr, dst) \
352 ((cache_ptr)->howto = \
353 ((dst)->r_type < NUM_HOWTOS \
354 ? howto_table + (dst)->r_type \
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
360 #define BSS_NOLOAD_IS_SHARED_LIBRARY
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
372 FIXME: This macro refers to symbols and asect; these are from the
373 calling function, not the macro arguments. */
375 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
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)); \
382 coffsym = coff_symbol_from (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); \
390 cache_ptr->addend = 0; \
391 if (ptr && reloc.r_type < NUM_HOWTOS \
392 && howto_table[reloc.r_type].pc_relative) \
393 cache_ptr->addend += asect->vma; \
396 /* We use the special COFF backend linker. For normal i386 COFF, we
397 can use the generic relocate_section routine. For PE, we need our
402 #define coff_relocate_section _bfd_coff_generic_relocate_section
404 #else /* COFF_WITH_PE */
406 /* The PE relocate section routine. The only difference between this
407 and the regular routine is that we don't want to do anything for a
411 coff_pe_i386_relocate_section (bfd
*output_bfd
,
412 struct bfd_link_info
*info
,
414 asection
*input_section
,
416 struct internal_reloc
*relocs
,
417 struct internal_syment
*syms
,
420 if (bfd_link_relocatable (info
))
423 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
424 input_section
, contents
,
425 relocs
, syms
, sections
);
428 #define coff_relocate_section coff_pe_i386_relocate_section
430 #endif /* COFF_WITH_PE */
432 /* Convert an rtype to howto for the COFF backend linker. */
434 static reloc_howto_type
*
435 coff_i386_rtype_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
437 struct internal_reloc
*rel
,
438 struct coff_link_hash_entry
*h
,
439 struct internal_syment
*sym
,
442 reloc_howto_type
*howto
;
444 if (rel
->r_type
>= NUM_HOWTOS
)
446 bfd_set_error (bfd_error_bad_value
);
450 howto
= howto_table
+ rel
->r_type
;
453 /* Cancel out code in _bfd_coff_generic_relocate_section. */
457 if (howto
->pc_relative
)
458 *addendp
+= sec
->vma
;
460 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
462 /* This is a common symbol. The section contents include the
463 size (sym->n_value) as an addend. The relocate_section
464 function will be adding in the final value of the symbol. We
465 need to subtract out the current size in order to get the
468 BFD_ASSERT (h
!= NULL
);
471 /* I think we *do* want to bypass this. If we don't, I have
472 seen some data parameters get the wrong relocation address.
473 If I link two versions with and without this section bypassed
474 and then do a binary comparison, the addresses which are
475 different can be looked up in the map. The case in which
476 this section has been bypassed has addresses which correspond
477 to values I can find in the map. */
478 *addendp
-= sym
->n_value
;
483 /* If the output symbol is common (in which case this must be a
484 relocatable link), we need to add in the final size of the
486 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
487 *addendp
+= h
->root
.u
.c
.size
;
491 if (howto
->pc_relative
)
495 /* If the symbol is defined, then the generic code is going to
496 add back the symbol value in order to cancel out an
497 adjustment it made to the addend. However, we set the addend
498 to 0 at the start of this function. We need to adjust here,
499 to avoid the adjustment the generic code will make. FIXME:
500 This is getting a bit hackish. */
501 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
502 *addendp
-= sym
->n_value
;
505 if (rel
->r_type
== R_IMAGEBASE
506 && (bfd_get_flavour(sec
->output_section
->owner
)
507 == bfd_target_coff_flavour
))
509 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
512 /* PR 17099 - Absolute R_PCRLONG relocations do not need a symbol. */
513 if (rel
->r_type
== R_PCRLONG
&& sym
== NULL
)
514 *addendp
-= rel
->r_vaddr
;
516 BFD_ASSERT (sym
!= NULL
);
518 if (rel
->r_type
== R_SECREL32
&& sym
!= NULL
)
522 if (h
&& (h
->root
.type
== bfd_link_hash_defined
523 || h
->root
.type
== bfd_link_hash_defweak
))
524 osect_vma
= h
->root
.u
.def
.section
->output_section
->vma
;
530 /* Sigh, the only way to get the section to offset against
531 is to find it the hard way. */
533 for (s
= abfd
->sections
, i
= 1; i
< sym
->n_scnum
; i
++)
536 osect_vma
= s
->output_section
->vma
;
539 *addendp
-= osect_vma
;
546 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
547 #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup
549 static reloc_howto_type
*
550 coff_i386_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
551 bfd_reloc_code_real_type code
)
556 return howto_table
+ R_IMAGEBASE
;
558 return howto_table
+ R_DIR32
;
559 case BFD_RELOC_32_PCREL
:
560 return howto_table
+ R_PCRLONG
;
562 return howto_table
+ R_RELWORD
;
563 case BFD_RELOC_16_PCREL
:
564 return howto_table
+ R_PCRWORD
;
566 return howto_table
+ R_RELBYTE
;
567 case BFD_RELOC_8_PCREL
:
568 return howto_table
+ R_PCRBYTE
;
570 case BFD_RELOC_32_SECREL
:
571 return howto_table
+ R_SECREL32
;
579 static reloc_howto_type
*
580 coff_i386_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
585 for (i
= 0; i
< NUM_HOWTOS
; i
++)
586 if (howto_table
[i
].name
!= NULL
587 && strcasecmp (howto_table
[i
].name
, r_name
) == 0)
588 return &howto_table
[i
];
593 #define coff_rtype_to_howto coff_i386_rtype_to_howto
595 #ifdef TARGET_UNDERSCORE
597 /* If i386 gcc uses underscores for symbol names, then it does not use
598 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
599 we treat all symbols starting with L as local. */
602 coff_i386_is_local_label_name (bfd
*abfd
, const char *name
)
607 return _bfd_coff_is_local_label_name (abfd
, name
);
610 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
612 #endif /* TARGET_UNDERSCORE */
614 #include "coffcode.h"
626 "coff-i386", /* name */
628 bfd_target_coff_flavour
,
629 BFD_ENDIAN_LITTLE
, /* data byte order is little */
630 BFD_ENDIAN_LITTLE
, /* header byte order is little */
632 (HAS_RELOC
| EXEC_P
| /* object flags */
633 HAS_LINENO
| HAS_DEBUG
|
634 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
| BFD_COMPRESS
| BFD_DECOMPRESS
),
636 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
638 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_READONLY
| SEC_DEBUGGING
640 | SEC_CODE
| SEC_DATA
| SEC_EXCLUDE
),
642 #ifdef TARGET_UNDERSCORE
643 TARGET_UNDERSCORE
, /* leading underscore */
645 0, /* leading underscore */
647 '/', /* ar_pad_char */
648 15, /* ar_max_namelen */
649 0, /* match priority. */
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
, /* data */
654 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
655 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
656 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
658 /* Note that we allow an object file to be treated as a core file as well. */
659 /* bfd_check_format */
660 #ifdef COFF_CHECK_FORMAT
661 {_bfd_dummy_target
, COFF_CHECK_FORMAT
,
662 bfd_generic_archive_p
, COFF_CHECK_FORMAT
},
664 {_bfd_dummy_target
, coff_object_p
, bfd_generic_archive_p
, coff_object_p
},
666 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
668 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
669 _bfd_write_archive_contents
, bfd_false
},
671 BFD_JUMP_TABLE_GENERIC (coff
),
672 BFD_JUMP_TABLE_COPY (coff
),
673 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
674 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
675 BFD_JUMP_TABLE_SYMBOLS (coff
),
676 BFD_JUMP_TABLE_RELOCS (coff
),
677 BFD_JUMP_TABLE_WRITE (coff
),
678 BFD_JUMP_TABLE_LINK (coff
),
679 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),