1 /* BFD back-end for Intel 386 COFF files.
2 Copyright (C) 1990-2019 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 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
45 #define OCTETS_PER_BYTE(ABFD, SEC) 1
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
*,
51 static reloc_howto_type
*coff_i386_reloc_type_lookup
52 (bfd
*, bfd_reloc_code_real_type
);
54 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
55 /* The page size is a guess based on ELF. */
57 #define COFF_PAGE_SIZE 0x1000
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. */
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. */
68 static bfd_reloc_status_type
69 coff_i386_reloc (bfd
*abfd
,
73 asection
*input_section
,
75 char **error_message ATTRIBUTE_UNUSED
)
80 if (output_bfd
== (bfd
*) NULL
)
81 return bfd_reloc_continue
;
84 if (bfd_is_com_section (symbol
->section
))
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
;
100 /* In PE mode, we do not offset the common symbol. */
101 diff
= reloc_entry
->addend
;
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. */
111 if (output_bfd
== (bfd
*) NULL
)
113 reloc_howto_type
*howto
= reloc_entry
->howto
;
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
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
;
127 diff
= -reloc_entry
->addend
;
131 diff
= reloc_entry
->addend
;
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
;
143 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
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
;
152 if (!bfd_reloc_offset_in_range (howto
, abfd
, input_section
, octets
))
153 return bfd_reloc_outofrange
;
159 char x
= bfd_get_8 (abfd
, addr
);
161 bfd_put_8 (abfd
, x
, addr
);
167 short x
= bfd_get_16 (abfd
, addr
);
169 bfd_put_16 (abfd
, (bfd_vma
) x
, addr
);
175 long x
= bfd_get_32 (abfd
, addr
);
177 bfd_put_32 (abfd
, (bfd_vma
) x
, addr
);
186 /* Now let bfd_perform_relocation finish everything up. */
187 return bfd_reloc_continue
;
191 /* Return TRUE if this relocation should appear in the output .reloc
194 static bfd_boolean
in_reloc_p (bfd
* abfd ATTRIBUTE_UNUSED
,
195 reloc_howto_type
*howto
)
197 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
198 && howto
->type
!= R_SECREL32
;
200 #endif /* COFF_WITH_PE */
203 #define PCRELOFFSET FALSE
206 static reloc_howto_type howto_table
[] =
214 HOWTO (R_DIR32
, /* type */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
218 FALSE
, /* pc_relative */
220 complain_overflow_bitfield
, /* complain_on_overflow */
221 coff_i386_reloc
, /* special_function */
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 */
230 2, /* size (0 = byte, 1 = short, 2 = long) */
232 FALSE
, /* pc_relative */
234 complain_overflow_bitfield
, /* complain_on_overflow */
235 coff_i386_reloc
, /* special_function */
237 TRUE
, /* partial_inplace */
238 0xffffffff, /* src_mask */
239 0xffffffff, /* dst_mask */
240 FALSE
), /* pcrel_offset */
245 /* 32-bit longword section relative relocation (013). */
246 HOWTO (R_SECREL32
, /* type */
248 2, /* size (0 = byte, 1 = short, 2 = long) */
250 FALSE
, /* pc_relative */
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 */
265 /* Byte relocation (017). */
266 HOWTO (R_RELBYTE
, /* type */
268 0, /* size (0 = byte, 1 = short, 2 = long) */
270 FALSE
, /* pc_relative */
272 complain_overflow_bitfield
, /* complain_on_overflow */
273 coff_i386_reloc
, /* special_function */
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 */
282 1, /* size (0 = byte, 1 = short, 2 = long) */
284 FALSE
, /* pc_relative */
286 complain_overflow_bitfield
, /* complain_on_overflow */
287 coff_i386_reloc
, /* special_function */
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 */
296 2, /* size (0 = byte, 1 = short, 2 = long) */
298 FALSE
, /* pc_relative */
300 complain_overflow_bitfield
, /* complain_on_overflow */
301 coff_i386_reloc
, /* special_function */
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 */
310 0, /* size (0 = byte, 1 = short, 2 = long) */
312 TRUE
, /* pc_relative */
314 complain_overflow_signed
, /* complain_on_overflow */
315 coff_i386_reloc
, /* special_function */
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 */
324 1, /* size (0 = byte, 1 = short, 2 = long) */
326 TRUE
, /* pc_relative */
328 complain_overflow_signed
, /* complain_on_overflow */
329 coff_i386_reloc
, /* special_function */
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 */
338 2, /* size (0 = byte, 1 = short, 2 = long) */
340 TRUE
, /* pc_relative */
342 complain_overflow_signed
, /* complain_on_overflow */
343 coff_i386_reloc
, /* special_function */
345 TRUE
, /* partial_inplace */
346 0xffffffff, /* src_mask */
347 0xffffffff, /* dst_mask */
348 PCRELOFFSET
) /* pcrel_offset */
351 #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
353 /* Turn a howto into a reloc nunmber */
355 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
356 #define BADMAG(x) I386BADMAG(x)
357 #define I386 1 /* Customize coffcode.h */
359 #define RTYPE2HOWTO(cache_ptr, dst) \
360 ((cache_ptr)->howto = \
361 ((dst)->r_type < NUM_HOWTOS \
362 ? howto_table + (dst)->r_type \
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
368 #define BSS_NOLOAD_IS_SHARED_LIBRARY
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
380 FIXME: This macro refers to symbols and asect; these are from the
381 calling function, not the macro arguments. */
383 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
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)); \
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); \
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; \
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
410 #define coff_relocate_section _bfd_coff_generic_relocate_section
412 #else /* COFF_WITH_PE */
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
419 coff_pe_i386_relocate_section (bfd
*output_bfd
,
420 struct bfd_link_info
*info
,
422 asection
*input_section
,
424 struct internal_reloc
*relocs
,
425 struct internal_syment
*syms
,
428 if (bfd_link_relocatable (info
))
431 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
432 input_section
, contents
,
433 relocs
, syms
, sections
);
436 #define coff_relocate_section coff_pe_i386_relocate_section
438 #endif /* COFF_WITH_PE */
440 /* Convert an rtype to howto for the COFF backend linker. */
442 static reloc_howto_type
*
443 coff_i386_rtype_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
445 struct internal_reloc
*rel
,
446 struct coff_link_hash_entry
*h
,
447 struct internal_syment
*sym
,
450 reloc_howto_type
*howto
;
452 if (rel
->r_type
>= NUM_HOWTOS
)
454 bfd_set_error (bfd_error_bad_value
);
458 howto
= howto_table
+ rel
->r_type
;
461 /* Cancel out code in _bfd_coff_generic_relocate_section. */
465 if (howto
->pc_relative
)
466 *addendp
+= sec
->vma
;
468 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
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
476 BFD_ASSERT (h
!= NULL
);
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
;
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
494 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
495 *addendp
+= h
->root
.u
.c
.size
;
499 if (howto
->pc_relative
)
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
;
513 if (rel
->r_type
== R_IMAGEBASE
514 && (bfd_get_flavour(sec
->output_section
->owner
)
515 == bfd_target_coff_flavour
))
517 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
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
;
524 BFD_ASSERT (sym
!= NULL
);
526 if (rel
->r_type
== R_SECREL32
&& sym
!= NULL
)
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
;
538 /* Sigh, the only way to get the section to offset against
539 is to find it the hard way. */
541 for (s
= abfd
->sections
, i
= 1; i
< sym
->n_scnum
; i
++)
544 osect_vma
= s
->output_section
->vma
;
547 *addendp
-= osect_vma
;
554 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
555 #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup
557 static reloc_howto_type
*
558 coff_i386_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
559 bfd_reloc_code_real_type code
)
564 return howto_table
+ R_IMAGEBASE
;
566 return howto_table
+ R_DIR32
;
567 case BFD_RELOC_32_PCREL
:
568 return howto_table
+ R_PCRLONG
;
570 return howto_table
+ R_RELWORD
;
571 case BFD_RELOC_16_PCREL
:
572 return howto_table
+ R_PCRWORD
;
574 return howto_table
+ R_RELBYTE
;
575 case BFD_RELOC_8_PCREL
:
576 return howto_table
+ R_PCRBYTE
;
578 case BFD_RELOC_32_SECREL
:
579 return howto_table
+ R_SECREL32
;
587 static reloc_howto_type
*
588 coff_i386_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
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
];
601 #define coff_rtype_to_howto coff_i386_rtype_to_howto
603 #ifdef TARGET_UNDERSCORE
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. */
610 coff_i386_is_local_label_name (bfd
*abfd
, const char *name
)
615 return _bfd_coff_is_local_label_name (abfd
, name
);
618 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
620 #endif /* TARGET_UNDERSCORE */
622 #include "coffcode.h"
634 "coff-i386", /* name */
636 bfd_target_coff_flavour
,
637 BFD_ENDIAN_LITTLE
, /* data byte order is little */
638 BFD_ENDIAN_LITTLE
, /* header byte order is little */
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
),
644 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
646 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_READONLY
| SEC_DEBUGGING
648 | SEC_CODE
| SEC_DATA
| SEC_EXCLUDE
),
650 #ifdef TARGET_UNDERSCORE
651 TARGET_UNDERSCORE
, /* leading underscore */
653 0, /* leading underscore */
655 '/', /* ar_pad_char */
656 15, /* ar_max_namelen */
657 0, /* match priority. */
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 */
666 /* Note that we allow an object file to be treated as a core file as well. */
668 #ifdef COFF_CHECK_FORMAT
669 { /* bfd_check_format */
672 bfd_generic_archive_p
,
679 bfd_generic_archive_p
,
683 { /* bfd_set_format */
684 _bfd_bool_bfd_false_error
,
686 _bfd_generic_mkarchive
,
687 _bfd_bool_bfd_false_error
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
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
),