1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/i386.h"
27 #include "coff/internal.h"
34 #include "coff/go32exe.h"
39 static bfd_reloc_status_type coff_i386_reloc
40 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
41 static reloc_howto_type
*coff_i386_rtype_to_howto
42 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
43 struct coff_link_hash_entry
*, struct internal_syment
*,
47 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
48 /* The page size is a guess based on ELF. */
50 #define COFF_PAGE_SIZE 0x1000
52 /* For some reason when using i386 COFF the value stored in the .text
53 section for a reference to a common symbol is the value itself plus
54 any desired offset. Ian Taylor, Cygnus Support. */
56 /* If we are producing relocateable output, we need to do some
57 adjustments to the object file that are not done by the
58 bfd_perform_relocation function. This function is called by every
59 reloc type to make any required adjustments. */
61 static bfd_reloc_status_type
62 coff_i386_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
68 asection
*input_section
;
74 if (output_bfd
== (bfd
*) NULL
)
75 return bfd_reloc_continue
;
77 if (bfd_is_com_section (symbol
->section
))
79 /* We are relocating a common symbol. The current value in the
80 object file is ORIG + OFFSET, where ORIG is the value of the
81 common symbol as seen by the object file when it was compiled
82 (this may be zero if the symbol was undefined) and OFFSET is
83 the offset into the common symbol (normally zero, but may be
84 non-zero when referring to a field in a common structure).
85 ORIG is the negative of reloc_entry->addend, which is set by
86 the CALC_ADDEND macro below. We want to replace the value in
87 the object file with NEW + OFFSET, where NEW is the value of
88 the common symbol which we are going to put in the final
89 object file. NEW is symbol->value. */
90 diff
= symbol
->value
+ reloc_entry
->addend
;
94 /* For some reason bfd_perform_relocation always effectively
95 ignores the addend for a COFF target when producing
96 relocateable output. This seems to be always wrong for 386
97 COFF, so we handle the addend here instead. */
98 diff
= reloc_entry
->addend
;
102 /* FIXME: How should this case be handled? */
103 if (reloc_entry
->howto
->type
== R_IMAGEBASE
&& diff
!= 0)
108 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
112 reloc_howto_type
*howto
= reloc_entry
->howto
;
113 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
119 char x
= bfd_get_8 (abfd
, addr
);
121 bfd_put_8 (abfd
, x
, addr
);
127 short x
= bfd_get_16 (abfd
, addr
);
129 bfd_put_16 (abfd
, x
, addr
);
135 long x
= bfd_get_32 (abfd
, addr
);
137 bfd_put_32 (abfd
, x
, addr
);
146 /* Now let bfd_perform_relocation finish everything up. */
147 return bfd_reloc_continue
;
151 /* Return true if this relocation should
152 appear in the output .reloc section. */
154 static boolean
in_reloc_p(abfd
, howto
)
156 reloc_howto_type
*howto
;
158 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
163 #define PCRELOFFSET false
166 static reloc_howto_type howto_table
[] =
174 HOWTO (R_DIR32
, /* type */
176 2, /* size (0 = byte, 1 = short, 2 = long) */
178 false, /* pc_relative */
180 complain_overflow_bitfield
, /* complain_on_overflow */
181 coff_i386_reloc
, /* special_function */
183 true, /* partial_inplace */
184 0xffffffff, /* src_mask */
185 0xffffffff, /* dst_mask */
186 true), /* pcrel_offset */
188 HOWTO (R_IMAGEBASE
, /* type */
190 2, /* size (0 = byte, 1 = short, 2 = long) */
192 false, /* pc_relative */
194 complain_overflow_bitfield
, /* complain_on_overflow */
195 coff_i386_reloc
, /* special_function */
197 true, /* partial_inplace */
198 0xffffffff, /* src_mask */
199 0xffffffff, /* dst_mask */
200 false), /* pcrel_offset */
208 HOWTO (R_RELBYTE
, /* type */
210 0, /* size (0 = byte, 1 = short, 2 = long) */
212 false, /* pc_relative */
214 complain_overflow_bitfield
, /* complain_on_overflow */
215 coff_i386_reloc
, /* special_function */
217 true, /* partial_inplace */
218 0x000000ff, /* src_mask */
219 0x000000ff, /* dst_mask */
220 PCRELOFFSET
), /* pcrel_offset */
221 HOWTO (R_RELWORD
, /* type */
223 1, /* size (0 = byte, 1 = short, 2 = long) */
225 false, /* pc_relative */
227 complain_overflow_bitfield
, /* complain_on_overflow */
228 coff_i386_reloc
, /* special_function */
230 true, /* partial_inplace */
231 0x0000ffff, /* src_mask */
232 0x0000ffff, /* dst_mask */
233 PCRELOFFSET
), /* pcrel_offset */
234 HOWTO (R_RELLONG
, /* type */
236 2, /* size (0 = byte, 1 = short, 2 = long) */
238 false, /* pc_relative */
240 complain_overflow_bitfield
, /* complain_on_overflow */
241 coff_i386_reloc
, /* special_function */
243 true, /* partial_inplace */
244 0xffffffff, /* src_mask */
245 0xffffffff, /* dst_mask */
246 PCRELOFFSET
), /* pcrel_offset */
247 HOWTO (R_PCRBYTE
, /* type */
249 0, /* size (0 = byte, 1 = short, 2 = long) */
251 true, /* pc_relative */
253 complain_overflow_signed
, /* complain_on_overflow */
254 coff_i386_reloc
, /* special_function */
256 true, /* partial_inplace */
257 0x000000ff, /* src_mask */
258 0x000000ff, /* dst_mask */
259 PCRELOFFSET
), /* pcrel_offset */
260 HOWTO (R_PCRWORD
, /* type */
262 1, /* size (0 = byte, 1 = short, 2 = long) */
264 true, /* pc_relative */
266 complain_overflow_signed
, /* complain_on_overflow */
267 coff_i386_reloc
, /* special_function */
269 true, /* partial_inplace */
270 0x0000ffff, /* src_mask */
271 0x0000ffff, /* dst_mask */
272 PCRELOFFSET
), /* pcrel_offset */
273 HOWTO (R_PCRLONG
, /* type */
275 2, /* 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 0xffffffff, /* src_mask */
284 0xffffffff, /* dst_mask */
285 PCRELOFFSET
) /* pcrel_offset */
288 /* Turn a howto into a reloc nunmber */
290 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
291 #define BADMAG(x) I386BADMAG(x)
292 #define I386 1 /* Customize coffcode.h */
294 #define RTYPE2HOWTO(cache_ptr, dst) \
295 (cache_ptr)->howto = howto_table + (dst)->r_type;
297 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
298 library. On some other COFF targets STYP_BSS is normally
300 #define BSS_NOLOAD_IS_SHARED_LIBRARY
302 /* Compute the addend of a reloc. If the reloc is to a common symbol,
303 the object file contains the value of the common symbol. By the
304 time this is called, the linker may be using a different symbol
305 from a different object file with a different value. Therefore, we
306 hack wildly to locate the original symbol from this file so that we
307 can make the correct adjustment. This macro sets coffsym to the
308 symbol from the original file, and uses it to set the addend value
309 correctly. If this is not a common symbol, the usual addend
310 calculation is done, except that an additional tweak is needed for
312 FIXME: This macro refers to symbols and asect; these are from the
313 calling function, not the macro arguments. */
315 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
317 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
318 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
319 coffsym = (obj_symbols (abfd) \
320 + (cache_ptr->sym_ptr_ptr - symbols)); \
322 coffsym = coff_symbol_from (abfd, ptr); \
323 if (coffsym != (coff_symbol_type *) NULL \
324 && coffsym->native->u.syment.n_scnum == 0) \
325 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
326 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
327 && ptr->section != (asection *) NULL) \
328 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
330 cache_ptr->addend = 0; \
331 if (ptr && howto_table[reloc.r_type].pc_relative) \
332 cache_ptr->addend += asect->vma; \
335 /* We use the special COFF backend linker. */
336 #define coff_relocate_section _bfd_coff_generic_relocate_section
338 static reloc_howto_type
*
339 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
342 struct internal_reloc
*rel
;
343 struct coff_link_hash_entry
*h
;
344 struct internal_syment
*sym
;
348 reloc_howto_type
*howto
;
350 howto
= howto_table
+ rel
->r_type
;
356 if (howto
->pc_relative
)
357 *addendp
+= sec
->vma
;
359 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
361 /* This is a common symbol. The section contents include the
362 size (sym->n_value) as an addend. The relocate_section
363 function will be adding in the final value of the symbol. We
364 need to subtract out the current size in order to get the
367 BFD_ASSERT (h
!= NULL
);
371 /* I think we *do* want to bypass this. If we don't, I have seen some data
372 parameters get the wrong relcation address. If I link two versions
373 with and without this section bypassed and then do a binary comparison,
374 the addresses which are different can be looked up in the map. The
375 case in which this section has been bypassed has addresses which correspond
376 to values I can find in the map */
377 *addendp
-= sym
->n_value
;
381 /* If the output symbol is common (in which case this must be a
382 relocateable link), we need to add in the final size of the
384 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
385 *addendp
+= h
->root
.u
.c
.size
;
389 if (howto
->pc_relative
)
392 if (rel
->r_type
== R_IMAGEBASE
)
394 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
402 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
405 static reloc_howto_type
*
406 coff_i386_reloc_type_lookup (abfd
, code
)
408 bfd_reloc_code_real_type code
;
413 return howto_table
+R_IMAGEBASE
;
415 return howto_table
+ R_DIR32
;
416 case BFD_RELOC_32_PCREL
:
417 return howto_table
+ R_PCRLONG
;
424 #define coff_rtype_to_howto coff_i386_rtype_to_howto
426 #ifdef TARGET_UNDERSCORE
428 /* If i386 gcc uses underscores for symbol names, then it does not use
429 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
430 we treat all symbols starting with L as local. */
432 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
435 coff_i386_is_local_label_name (abfd
, name
)
442 return _bfd_coff_is_local_label_name (abfd
, name
);
445 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
447 #endif /* TARGET_UNDERSCORE */
449 #include "coffcode.h"
451 static const bfd_target
*
452 i3coff_object_p (abfd
)
455 #ifdef COFF_IMAGE_WITH_PE
456 /* We need to hack badly to handle a PE image correctly. In PE
457 images created by the GNU linker, the offset to the COFF header
458 is always the size. However, this is not the case in images
459 generated by other PE linkers. The PE format stores a four byte
460 offset to the PE signature just before the COFF header at
461 location 0x3c of the file. We pick up that offset, verify that
462 the PE signature is there, and then set ourselves up to read in
465 bfd_byte ext_offset
[4];
467 bfd_byte ext_signature
[4];
468 unsigned long signature
;
470 if (bfd_seek (abfd
, 0x3c, SEEK_SET
) != 0
471 || bfd_read (ext_offset
, 1, 4, abfd
) != 4)
473 if (bfd_get_error () != bfd_error_system_call
)
474 bfd_set_error (bfd_error_wrong_format
);
477 offset
= bfd_h_get_32 (abfd
, ext_offset
);
478 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
479 || bfd_read (ext_signature
, 1, 4, abfd
) != 4)
481 if (bfd_get_error () != bfd_error_system_call
)
482 bfd_set_error (bfd_error_wrong_format
);
485 signature
= bfd_h_get_32 (abfd
, ext_signature
);
487 if (signature
!= 0x4550)
489 bfd_set_error (bfd_error_wrong_format
);
493 /* Here is the hack. coff_object_p wants to read filhsz bytes to
494 pick up the COFF header. We adjust so that that will work. 20
495 is the size of the i386 COFF filehdr. */
499 - bfd_coff_filhsz (abfd
)
504 if (bfd_get_error () != bfd_error_system_call
)
505 bfd_set_error (bfd_error_wrong_format
);
511 return coff_object_p (abfd
);
524 "coff-i386", /* name */
526 bfd_target_coff_flavour
,
527 BFD_ENDIAN_LITTLE
, /* data byte order is little */
528 BFD_ENDIAN_LITTLE
, /* header byte order is little */
530 (HAS_RELOC
| EXEC_P
| /* object flags */
531 HAS_LINENO
| HAS_DEBUG
|
532 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
535 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
536 | SEC_CODE
| SEC_DATA
),
538 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
539 | SEC_CODE
| SEC_DATA
540 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
543 #ifdef TARGET_UNDERSCORE
544 TARGET_UNDERSCORE
, /* leading underscore */
546 0, /* leading underscore */
548 '/', /* ar_pad_char */
549 15, /* ar_max_namelen */
551 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
552 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
553 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
554 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
555 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
556 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
558 /* Note that we allow an object file to be treated as a core file as well. */
559 {_bfd_dummy_target
, i3coff_object_p
, /* bfd_check_format */
560 bfd_generic_archive_p
, i3coff_object_p
},
561 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
563 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
564 _bfd_write_archive_contents
, bfd_false
},
566 BFD_JUMP_TABLE_GENERIC (coff
),
567 BFD_JUMP_TABLE_COPY (coff
),
568 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
569 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
570 BFD_JUMP_TABLE_SYMBOLS (coff
),
571 BFD_JUMP_TABLE_RELOCS (coff
),
572 BFD_JUMP_TABLE_WRITE (coff
),
573 BFD_JUMP_TABLE_LINK (coff
),
574 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),