1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "coff/i386.h"
28 #include "coff/internal.h"
35 #include "coff/go32exe.h"
40 static bfd_reloc_status_type coff_i386_reloc
41 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
42 static reloc_howto_type
*coff_i386_rtype_to_howto
43 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
44 struct coff_link_hash_entry
*, struct internal_syment
*,
46 static reloc_howto_type
*coff_i386_reloc_type_lookup
47 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
48 static const bfd_target
*i3coff_object_p
PARAMS ((bfd
*));
50 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
51 /* The page size is a guess based on ELF. */
53 #define COFF_PAGE_SIZE 0x1000
55 /* For some reason when using i386 COFF the value stored in the .text
56 section for a reference to a common symbol is the value itself plus
57 any desired offset. Ian Taylor, Cygnus Support. */
59 /* If we are producing relocateable output, we need to do some
60 adjustments to the object file that are not done by the
61 bfd_perform_relocation function. This function is called by every
62 reloc type to make any required adjustments. */
64 static bfd_reloc_status_type
65 coff_i386_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
71 asection
*input_section ATTRIBUTE_UNUSED
;
73 char **error_message ATTRIBUTE_UNUSED
;
77 if (output_bfd
== (bfd
*) NULL
)
78 return bfd_reloc_continue
;
80 if (bfd_is_com_section (symbol
->section
))
83 /* We are relocating a common symbol. The current value in the
84 object file is ORIG + OFFSET, where ORIG is the value of the
85 common symbol as seen by the object file when it was compiled
86 (this may be zero if the symbol was undefined) and OFFSET is
87 the offset into the common symbol (normally zero, but may be
88 non-zero when referring to a field in a common structure).
89 ORIG is the negative of reloc_entry->addend, which is set by
90 the CALC_ADDEND macro below. We want to replace the value in
91 the object file with NEW + OFFSET, where NEW is the value of
92 the common symbol which we are going to put in the final
93 object file. NEW is symbol->value. */
94 diff
= symbol
->value
+ reloc_entry
->addend
;
96 /* In PE mode, we do not offset the common symbol. */
97 diff
= reloc_entry
->addend
;
102 /* For some reason bfd_perform_relocation always effectively
103 ignores the addend for a COFF target when producing
104 relocateable output. This seems to be always wrong for 386
105 COFF, so we handle the addend here instead. */
106 diff
= reloc_entry
->addend
;
110 /* FIXME: How should this case be handled? */
111 if (reloc_entry
->howto
->type
== R_IMAGEBASE
)
112 diff
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
116 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
120 reloc_howto_type
*howto
= reloc_entry
->howto
;
121 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
127 char x
= bfd_get_8 (abfd
, addr
);
129 bfd_put_8 (abfd
, x
, addr
);
135 short x
= bfd_get_16 (abfd
, addr
);
137 bfd_put_16 (abfd
, x
, addr
);
143 long x
= bfd_get_32 (abfd
, addr
);
145 bfd_put_32 (abfd
, x
, addr
);
154 /* Now let bfd_perform_relocation finish everything up. */
155 return bfd_reloc_continue
;
159 /* Return true if this relocation should
160 appear in the output .reloc section. */
162 static boolean
in_reloc_p(abfd
, howto
)
163 bfd
* abfd ATTRIBUTE_UNUSED
;
164 reloc_howto_type
*howto
;
166 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
171 #define PCRELOFFSET false
174 static reloc_howto_type howto_table
[] =
182 HOWTO (R_DIR32
, /* type */
184 2, /* size (0 = byte, 1 = short, 2 = long) */
186 false, /* pc_relative */
188 complain_overflow_bitfield
, /* complain_on_overflow */
189 coff_i386_reloc
, /* special_function */
191 true, /* partial_inplace */
192 0xffffffff, /* src_mask */
193 0xffffffff, /* dst_mask */
194 true), /* pcrel_offset */
195 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
196 HOWTO (R_IMAGEBASE
, /* type */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
200 false, /* pc_relative */
202 complain_overflow_bitfield
, /* complain_on_overflow */
203 coff_i386_reloc
, /* special_function */
205 true, /* partial_inplace */
206 0xffffffff, /* src_mask */
207 0xffffffff, /* dst_mask */
208 false), /* pcrel_offset */
216 /* Byte relocation (017). */
217 HOWTO (R_RELBYTE
, /* type */
219 0, /* size (0 = byte, 1 = short, 2 = long) */
221 false, /* pc_relative */
223 complain_overflow_bitfield
, /* complain_on_overflow */
224 coff_i386_reloc
, /* special_function */
226 true, /* partial_inplace */
227 0x000000ff, /* src_mask */
228 0x000000ff, /* dst_mask */
229 PCRELOFFSET
), /* pcrel_offset */
230 /* 16-bit word relocation (020). */
231 HOWTO (R_RELWORD
, /* type */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
235 false, /* pc_relative */
237 complain_overflow_bitfield
, /* complain_on_overflow */
238 coff_i386_reloc
, /* special_function */
240 true, /* partial_inplace */
241 0x0000ffff, /* src_mask */
242 0x0000ffff, /* dst_mask */
243 PCRELOFFSET
), /* pcrel_offset */
244 /* 32-bit longword relocation (021). */
245 HOWTO (R_RELLONG
, /* type */
247 2, /* size (0 = byte, 1 = short, 2 = long) */
249 false, /* pc_relative */
251 complain_overflow_bitfield
, /* complain_on_overflow */
252 coff_i386_reloc
, /* special_function */
254 true, /* partial_inplace */
255 0xffffffff, /* src_mask */
256 0xffffffff, /* dst_mask */
257 PCRELOFFSET
), /* pcrel_offset */
258 /* Byte PC relative relocation (022). */
259 HOWTO (R_PCRBYTE
, /* type */
261 0, /* size (0 = byte, 1 = short, 2 = long) */
263 true, /* pc_relative */
265 complain_overflow_signed
, /* complain_on_overflow */
266 coff_i386_reloc
, /* special_function */
268 true, /* partial_inplace */
269 0x000000ff, /* src_mask */
270 0x000000ff, /* dst_mask */
271 PCRELOFFSET
), /* pcrel_offset */
272 /* 16-bit word PC relative relocation (023). */
273 HOWTO (R_PCRWORD
, /* type */
275 1, /* size (0 = byte, 1 = short, 2 = long) */
277 true, /* pc_relative */
279 complain_overflow_signed
, /* complain_on_overflow */
280 coff_i386_reloc
, /* special_function */
282 true, /* partial_inplace */
283 0x0000ffff, /* src_mask */
284 0x0000ffff, /* dst_mask */
285 PCRELOFFSET
), /* pcrel_offset */
286 /* 32-bit longword PC relative relocation (024). */
287 HOWTO (R_PCRLONG
, /* type */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
291 true, /* pc_relative */
293 complain_overflow_signed
, /* complain_on_overflow */
294 coff_i386_reloc
, /* special_function */
296 true, /* partial_inplace */
297 0xffffffff, /* src_mask */
298 0xffffffff, /* dst_mask */
299 PCRELOFFSET
) /* pcrel_offset */
302 /* Turn a howto into a reloc nunmber */
304 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
305 #define BADMAG(x) I386BADMAG(x)
306 #define I386 1 /* Customize coffcode.h */
308 #define RTYPE2HOWTO(cache_ptr, dst) \
309 ((cache_ptr)->howto = \
310 ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
311 ? howto_table + (dst)->r_type \
314 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
315 library. On some other COFF targets STYP_BSS is normally
317 #define BSS_NOLOAD_IS_SHARED_LIBRARY
319 /* Compute the addend of a reloc. If the reloc is to a common symbol,
320 the object file contains the value of the common symbol. By the
321 time this is called, the linker may be using a different symbol
322 from a different object file with a different value. Therefore, we
323 hack wildly to locate the original symbol from this file so that we
324 can make the correct adjustment. This macro sets coffsym to the
325 symbol from the original file, and uses it to set the addend value
326 correctly. If this is not a common symbol, the usual addend
327 calculation is done, except that an additional tweak is needed for
329 FIXME: This macro refers to symbols and asect; these are from the
330 calling function, not the macro arguments. */
332 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
334 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
335 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
336 coffsym = (obj_symbols (abfd) \
337 + (cache_ptr->sym_ptr_ptr - symbols)); \
339 coffsym = coff_symbol_from (abfd, ptr); \
340 if (coffsym != (coff_symbol_type *) NULL \
341 && coffsym->native->u.syment.n_scnum == 0) \
342 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
343 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
344 && ptr->section != (asection *) NULL) \
345 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
347 cache_ptr->addend = 0; \
348 if (ptr && howto_table[reloc.r_type].pc_relative) \
349 cache_ptr->addend += asect->vma; \
352 /* We use the special COFF backend linker. For normal i386 COFF, we
353 can use the generic relocate_section routine. For PE, we need our
358 #define coff_relocate_section _bfd_coff_generic_relocate_section
360 #else /* COFF_WITH_PE */
362 /* The PE relocate section routine. The only difference between this
363 and the regular routine is that we don't want to do anything for a
364 relocateable link. */
366 static boolean coff_pe_i386_relocate_section
367 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
368 struct internal_reloc
*, struct internal_syment
*, asection
**));
371 coff_pe_i386_relocate_section (output_bfd
, info
, input_bfd
,
372 input_section
, contents
, relocs
, syms
,
375 struct bfd_link_info
*info
;
377 asection
*input_section
;
379 struct internal_reloc
*relocs
;
380 struct internal_syment
*syms
;
383 if (info
->relocateable
)
386 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
387 input_section
, contents
,
388 relocs
, syms
, sections
);
391 #define coff_relocate_section coff_pe_i386_relocate_section
393 #endif /* COFF_WITH_PE */
395 /* Convert an rtype to howto for the COFF backend linker. */
397 static reloc_howto_type
*
398 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
399 bfd
*abfd ATTRIBUTE_UNUSED
;
401 struct internal_reloc
*rel
;
402 struct coff_link_hash_entry
*h
;
403 struct internal_syment
*sym
;
406 reloc_howto_type
*howto
;
408 if (rel
->r_type
> sizeof (howto_table
) / sizeof (howto_table
[0]))
410 bfd_set_error (bfd_error_bad_value
);
414 howto
= howto_table
+ rel
->r_type
;
417 /* Cancel out code in _bfd_coff_generic_relocate_section. */
421 if (howto
->pc_relative
)
422 *addendp
+= sec
->vma
;
424 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
426 /* This is a common symbol. The section contents include the
427 size (sym->n_value) as an addend. The relocate_section
428 function will be adding in the final value of the symbol. We
429 need to subtract out the current size in order to get the
432 BFD_ASSERT (h
!= NULL
);
435 /* I think we *do* want to bypass this. If we don't, I have
436 seen some data parameters get the wrong relocation address.
437 If I link two versions with and without this section bypassed
438 and then do a binary comparison, the addresses which are
439 different can be looked up in the map. The case in which
440 this section has been bypassed has addresses which correspond
441 to values I can find in the map. */
442 *addendp
-= sym
->n_value
;
447 /* If the output symbol is common (in which case this must be a
448 relocateable link), we need to add in the final size of the
450 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
451 *addendp
+= h
->root
.u
.c
.size
;
455 if (howto
->pc_relative
)
459 /* If the symbol is defined, then the generic code is going to
460 add back the symbol value in order to cancel out an
461 adjustment it made to the addend. However, we set the addend
462 to 0 at the start of this function. We need to adjust here,
463 to avoid the adjustment the generic code will make. FIXME:
464 This is getting a bit hackish. */
465 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
466 *addendp
-= sym
->n_value
;
469 if (rel
->r_type
== R_IMAGEBASE
)
471 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
478 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
480 static reloc_howto_type
*
481 coff_i386_reloc_type_lookup (abfd
, code
)
482 bfd
*abfd ATTRIBUTE_UNUSED
;
483 bfd_reloc_code_real_type code
;
488 return howto_table
+R_IMAGEBASE
;
490 return howto_table
+ R_DIR32
;
491 case BFD_RELOC_32_PCREL
:
492 return howto_table
+ R_PCRLONG
;
499 #define coff_rtype_to_howto coff_i386_rtype_to_howto
501 #ifdef TARGET_UNDERSCORE
503 /* If i386 gcc uses underscores for symbol names, then it does not use
504 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
505 we treat all symbols starting with L as local. */
507 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
510 coff_i386_is_local_label_name (abfd
, name
)
517 return _bfd_coff_is_local_label_name (abfd
, name
);
520 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
522 #endif /* TARGET_UNDERSCORE */
524 #include "coffcode.h"
526 static const bfd_target
*
527 i3coff_object_p (abfd
)
530 #ifdef COFF_IMAGE_WITH_PE
531 /* We need to hack badly to handle a PE image correctly. In PE
532 images created by the GNU linker, the offset to the COFF header
533 is always the size. However, this is not the case in images
534 generated by other PE linkers. The PE format stores a four byte
535 offset to the PE signature just before the COFF header at
536 location 0x3c of the file. We pick up that offset, verify that
537 the PE signature is there, and then set ourselves up to read in
540 bfd_byte ext_offset
[4];
542 bfd_byte ext_signature
[4];
543 unsigned long signature
;
545 if (bfd_seek (abfd
, 0x3c, SEEK_SET
) != 0
546 || bfd_read (ext_offset
, 1, 4, abfd
) != 4)
548 if (bfd_get_error () != bfd_error_system_call
)
549 bfd_set_error (bfd_error_wrong_format
);
552 offset
= bfd_h_get_32 (abfd
, ext_offset
);
553 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
554 || bfd_read (ext_signature
, 1, 4, abfd
) != 4)
556 if (bfd_get_error () != bfd_error_system_call
)
557 bfd_set_error (bfd_error_wrong_format
);
560 signature
= bfd_h_get_32 (abfd
, ext_signature
);
562 if (signature
!= 0x4550)
564 bfd_set_error (bfd_error_wrong_format
);
568 /* Here is the hack. coff_object_p wants to read filhsz bytes to
569 pick up the COFF header. We adjust so that that will work. 20
570 is the size of the i386 COFF filehdr. */
574 - bfd_coff_filhsz (abfd
)
579 if (bfd_get_error () != bfd_error_system_call
)
580 bfd_set_error (bfd_error_wrong_format
);
586 return coff_object_p (abfd
);
599 "coff-i386", /* name */
601 bfd_target_coff_flavour
,
602 BFD_ENDIAN_LITTLE
, /* data byte order is little */
603 BFD_ENDIAN_LITTLE
, /* header byte order is little */
605 (HAS_RELOC
| EXEC_P
| /* object flags */
606 HAS_LINENO
| HAS_DEBUG
|
607 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
610 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
611 | SEC_CODE
| SEC_DATA
),
613 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
614 | SEC_CODE
| SEC_DATA
615 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
618 #ifdef TARGET_UNDERSCORE
619 TARGET_UNDERSCORE
, /* leading underscore */
621 0, /* leading underscore */
623 '/', /* ar_pad_char */
624 15, /* ar_max_namelen */
626 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
627 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
628 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
629 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
630 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
631 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
633 /* Note that we allow an object file to be treated as a core file as well. */
634 {_bfd_dummy_target
, i3coff_object_p
, /* bfd_check_format */
635 bfd_generic_archive_p
, i3coff_object_p
},
636 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
638 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
639 _bfd_write_archive_contents
, bfd_false
},
641 BFD_JUMP_TABLE_GENERIC (coff
),
642 BFD_JUMP_TABLE_COPY (coff
),
643 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
644 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
645 BFD_JUMP_TABLE_SYMBOLS (coff
),
646 BFD_JUMP_TABLE_RELOCS (coff
),
647 BFD_JUMP_TABLE_WRITE (coff
),
648 BFD_JUMP_TABLE_LINK (coff
),
649 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),