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"
35 static bfd_reloc_status_type coff_i386_reloc
36 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
37 static reloc_howto_type
*coff_i386_rtype_to_howto
38 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
39 struct coff_link_hash_entry
*, struct internal_syment
*,
43 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
44 /* The page size is a guess based on ELF. */
46 #define COFF_PAGE_SIZE 0x1000
48 /* For some reason when using i386 COFF the value stored in the .text
49 section for a reference to a common symbol is the value itself plus
50 any desired offset. Ian Taylor, Cygnus Support. */
52 /* If we are producing relocateable output, we need to do some
53 adjustments to the object file that are not done by the
54 bfd_perform_relocation function. This function is called by every
55 reloc type to make any required adjustments. */
57 static bfd_reloc_status_type
58 coff_i386_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
64 asection
*input_section
;
70 if (output_bfd
== (bfd
*) NULL
)
71 return bfd_reloc_continue
;
73 if (bfd_is_com_section (symbol
->section
))
75 /* We are relocating a common symbol. The current value in the
76 object file is ORIG + OFFSET, where ORIG is the value of the
77 common symbol as seen by the object file when it was compiled
78 (this may be zero if the symbol was undefined) and OFFSET is
79 the offset into the common symbol (normally zero, but may be
80 non-zero when referring to a field in a common structure).
81 ORIG is the negative of reloc_entry->addend, which is set by
82 the CALC_ADDEND macro below. We want to replace the value in
83 the object file with NEW + OFFSET, where NEW is the value of
84 the common symbol which we are going to put in the final
85 object file. NEW is symbol->value. */
86 diff
= symbol
->value
+ reloc_entry
->addend
;
90 /* For some reason bfd_perform_relocation always effectively
91 ignores the addend for a COFF target when producing
92 relocateable output. This seems to be always wrong for 386
93 COFF, so we handle the addend here instead. */
94 diff
= reloc_entry
->addend
;
98 /* FIXME: How should this case be handled? */
99 if (reloc_entry
->howto
->type
== R_IMAGEBASE
&& diff
!= 0)
104 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
108 reloc_howto_type
*howto
= reloc_entry
->howto
;
109 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
115 char x
= bfd_get_8 (abfd
, addr
);
117 bfd_put_8 (abfd
, x
, addr
);
123 short x
= bfd_get_16 (abfd
, addr
);
125 bfd_put_16 (abfd
, x
, addr
);
131 long x
= bfd_get_32 (abfd
, addr
);
133 bfd_put_32 (abfd
, x
, addr
);
142 /* Now let bfd_perform_relocation finish everything up. */
143 return bfd_reloc_continue
;
147 /* Return true if this relocation should
148 appear in the output .reloc section. */
150 static boolean
in_reloc_p(abfd
, howto
)
152 reloc_howto_type
*howto
;
154 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
159 #define PCRELOFFSET false
162 static reloc_howto_type howto_table
[] =
170 HOWTO (R_DIR32
, /* type */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
174 false, /* pc_relative */
176 complain_overflow_bitfield
, /* complain_on_overflow */
177 coff_i386_reloc
, /* special_function */
179 true, /* partial_inplace */
180 0xffffffff, /* src_mask */
181 0xffffffff, /* dst_mask */
182 true), /* pcrel_offset */
184 HOWTO (R_IMAGEBASE
, /* type */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
188 false, /* pc_relative */
190 complain_overflow_bitfield
, /* complain_on_overflow */
191 coff_i386_reloc
, /* special_function */
193 true, /* partial_inplace */
194 0xffffffff, /* src_mask */
195 0xffffffff, /* dst_mask */
196 false), /* pcrel_offset */
204 HOWTO (R_RELBYTE
, /* type */
206 0, /* size (0 = byte, 1 = short, 2 = long) */
208 false, /* pc_relative */
210 complain_overflow_bitfield
, /* complain_on_overflow */
211 coff_i386_reloc
, /* special_function */
213 true, /* partial_inplace */
214 0x000000ff, /* src_mask */
215 0x000000ff, /* dst_mask */
216 PCRELOFFSET
), /* pcrel_offset */
217 HOWTO (R_RELWORD
, /* type */
219 1, /* 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 0x0000ffff, /* src_mask */
228 0x0000ffff, /* dst_mask */
229 PCRELOFFSET
), /* pcrel_offset */
230 HOWTO (R_RELLONG
, /* type */
232 2, /* size (0 = byte, 1 = short, 2 = long) */
234 false, /* pc_relative */
236 complain_overflow_bitfield
, /* complain_on_overflow */
237 coff_i386_reloc
, /* special_function */
239 true, /* partial_inplace */
240 0xffffffff, /* src_mask */
241 0xffffffff, /* dst_mask */
242 PCRELOFFSET
), /* pcrel_offset */
243 HOWTO (R_PCRBYTE
, /* type */
245 0, /* size (0 = byte, 1 = short, 2 = long) */
247 true, /* pc_relative */
249 complain_overflow_signed
, /* complain_on_overflow */
250 coff_i386_reloc
, /* special_function */
252 true, /* partial_inplace */
253 0x000000ff, /* src_mask */
254 0x000000ff, /* dst_mask */
255 PCRELOFFSET
), /* pcrel_offset */
256 HOWTO (R_PCRWORD
, /* type */
258 1, /* size (0 = byte, 1 = short, 2 = long) */
260 true, /* pc_relative */
262 complain_overflow_signed
, /* complain_on_overflow */
263 coff_i386_reloc
, /* special_function */
265 true, /* partial_inplace */
266 0x0000ffff, /* src_mask */
267 0x0000ffff, /* dst_mask */
268 PCRELOFFSET
), /* pcrel_offset */
269 HOWTO (R_PCRLONG
, /* type */
271 2, /* size (0 = byte, 1 = short, 2 = long) */
273 true, /* pc_relative */
275 complain_overflow_signed
, /* complain_on_overflow */
276 coff_i386_reloc
, /* special_function */
278 true, /* partial_inplace */
279 0xffffffff, /* src_mask */
280 0xffffffff, /* dst_mask */
281 PCRELOFFSET
) /* pcrel_offset */
284 /* Turn a howto into a reloc nunmber */
286 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
287 #define BADMAG(x) I386BADMAG(x)
288 #define I386 1 /* Customize coffcode.h */
290 #define RTYPE2HOWTO(cache_ptr, dst) \
291 (cache_ptr)->howto = howto_table + (dst)->r_type;
293 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
294 library. On some other COFF targets STYP_BSS is normally
296 #define BSS_NOLOAD_IS_SHARED_LIBRARY
298 /* Compute the addend of a reloc. If the reloc is to a common symbol,
299 the object file contains the value of the common symbol. By the
300 time this is called, the linker may be using a different symbol
301 from a different object file with a different value. Therefore, we
302 hack wildly to locate the original symbol from this file so that we
303 can make the correct adjustment. This macro sets coffsym to the
304 symbol from the original file, and uses it to set the addend value
305 correctly. If this is not a common symbol, the usual addend
306 calculation is done, except that an additional tweak is needed for
308 FIXME: This macro refers to symbols and asect; these are from the
309 calling function, not the macro arguments. */
311 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
313 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
314 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
315 coffsym = (obj_symbols (abfd) \
316 + (cache_ptr->sym_ptr_ptr - symbols)); \
318 coffsym = coff_symbol_from (abfd, ptr); \
319 if (coffsym != (coff_symbol_type *) NULL \
320 && coffsym->native->u.syment.n_scnum == 0) \
321 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
322 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
323 && ptr->section != (asection *) NULL) \
324 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
326 cache_ptr->addend = 0; \
327 if (ptr && howto_table[reloc.r_type].pc_relative) \
328 cache_ptr->addend += asect->vma; \
331 /* We use the special COFF backend linker. */
332 #define coff_relocate_section _bfd_coff_generic_relocate_section
334 static reloc_howto_type
*
335 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
338 struct internal_reloc
*rel
;
339 struct coff_link_hash_entry
*h
;
340 struct internal_syment
*sym
;
344 reloc_howto_type
*howto
;
346 howto
= howto_table
+ rel
->r_type
;
352 if (howto
->pc_relative
)
353 *addendp
+= sec
->vma
;
355 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
357 /* This is a common symbol. The section contents include the
358 size (sym->n_value) as an addend. The relocate_section
359 function will be adding in the final value of the symbol. We
360 need to subtract out the current size in order to get the
363 BFD_ASSERT (h
!= NULL
);
367 /* I think we *do* want to bypass this. If we don't, I have seen some data
368 parameters get the wrong relcation address. If I link two versions
369 with and without this section bypassed and then do a binary comparison,
370 the addresses which are different can be looked up in the map. The
371 case in which this section has been bypassed has addresses which correspond
372 to values I can find in the map */
373 *addendp
-= sym
->n_value
;
377 /* If the output symbol is common (in which case this must be a
378 relocateable link), we need to add in the final size of the
380 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
381 *addendp
+= h
->root
.u
.c
.size
;
385 if (howto
->pc_relative
)
388 if (rel
->r_type
== R_IMAGEBASE
)
390 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
398 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
401 static reloc_howto_type
*
402 coff_i386_reloc_type_lookup (abfd
, code
)
404 bfd_reloc_code_real_type code
;
409 return howto_table
+R_IMAGEBASE
;
411 return howto_table
+ R_DIR32
;
412 case BFD_RELOC_32_PCREL
:
413 return howto_table
+ R_PCRLONG
;
420 #define coff_rtype_to_howto coff_i386_rtype_to_howto
422 #ifdef TARGET_UNDERSCORE
424 /* If i386 gcc uses underscores for symbol names, then it does not use
425 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
426 we treat all symbols starting with L as local. */
428 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
431 coff_i386_is_local_label_name (abfd
, name
)
438 return _bfd_coff_is_local_label_name (abfd
, name
);
441 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
443 #endif /* TARGET_UNDERSCORE */
445 #include "coffcode.h"
447 static const bfd_target
*
448 i3coff_object_p (abfd
)
451 #ifdef COFF_IMAGE_WITH_PE
452 /* We need to hack badly to handle a PE image correctly. In PE
453 images created by the GNU linker, the offset to the COFF header
454 is always the size. However, this is not the case in images
455 generated by other PE linkers. The PE format stores a four byte
456 offset to the PE signature just before the COFF header at
457 location 0x3c of the file. We pick up that offset, verify that
458 the PE signature is there, and then set ourselves up to read in
461 bfd_byte ext_offset
[4];
463 bfd_byte ext_signature
[4];
464 unsigned long signature
;
466 if (bfd_seek (abfd
, 0x3c, SEEK_SET
) != 0
467 || bfd_read (ext_offset
, 1, 4, abfd
) != 4)
469 if (bfd_get_error () != bfd_error_system_call
)
470 bfd_set_error (bfd_error_wrong_format
);
473 offset
= bfd_h_get_32 (abfd
, ext_offset
);
474 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
475 || bfd_read (ext_signature
, 1, 4, abfd
) != 4)
477 if (bfd_get_error () != bfd_error_system_call
)
478 bfd_set_error (bfd_error_wrong_format
);
481 signature
= bfd_h_get_32 (abfd
, ext_signature
);
483 if (signature
!= 0x4550)
485 bfd_set_error (bfd_error_wrong_format
);
489 /* Here is the hack. coff_object_p wants to read filhsz bytes to
490 pick up the COFF header. We adjust so that that will work. 20
491 is the size of the i386 COFF filehdr. */
495 - bfd_coff_filhsz (abfd
)
500 if (bfd_get_error () != bfd_error_system_call
)
501 bfd_set_error (bfd_error_wrong_format
);
507 return coff_object_p (abfd
);
520 "coff-i386", /* name */
522 bfd_target_coff_flavour
,
523 BFD_ENDIAN_LITTLE
, /* data byte order is little */
524 BFD_ENDIAN_LITTLE
, /* header byte order is little */
526 (HAS_RELOC
| EXEC_P
| /* object flags */
527 HAS_LINENO
| HAS_DEBUG
|
528 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
531 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
532 | SEC_CODE
| SEC_DATA
),
534 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
535 | SEC_CODE
| SEC_DATA
536 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
539 #ifdef TARGET_UNDERSCORE
540 TARGET_UNDERSCORE
, /* leading underscore */
542 0, /* leading underscore */
544 '/', /* ar_pad_char */
545 15, /* ar_max_namelen */
547 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
548 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
549 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
550 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
551 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
552 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
554 /* Note that we allow an object file to be treated as a core file as well. */
555 {_bfd_dummy_target
, i3coff_object_p
, /* bfd_check_format */
556 bfd_generic_archive_p
, i3coff_object_p
},
557 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
559 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
560 _bfd_write_archive_contents
, bfd_false
},
562 BFD_JUMP_TABLE_GENERIC (coff
),
563 BFD_JUMP_TABLE_COPY (coff
),
564 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
565 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
566 BFD_JUMP_TABLE_SYMBOLS (coff
),
567 BFD_JUMP_TABLE_RELOCS (coff
),
568 BFD_JUMP_TABLE_WRITE (coff
),
569 BFD_JUMP_TABLE_LINK (coff
),
570 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),