1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
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
));
49 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
50 /* The page size is a guess based on ELF. */
52 #define COFF_PAGE_SIZE 0x1000
54 /* For some reason when using i386 COFF the value stored in the .text
55 section for a reference to a common symbol is the value itself plus
56 any desired offset. Ian Taylor, Cygnus Support. */
58 /* If we are producing relocateable output, we need to do some
59 adjustments to the object file that are not done by the
60 bfd_perform_relocation function. This function is called by every
61 reloc type to make any required adjustments. */
63 static bfd_reloc_status_type
64 coff_i386_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
70 asection
*input_section ATTRIBUTE_UNUSED
;
72 char **error_message ATTRIBUTE_UNUSED
;
76 if (output_bfd
== (bfd
*) NULL
)
77 return bfd_reloc_continue
;
79 if (bfd_is_com_section (symbol
->section
))
82 /* We are relocating a common symbol. The current value in the
83 object file is ORIG + OFFSET, where ORIG is the value of the
84 common symbol as seen by the object file when it was compiled
85 (this may be zero if the symbol was undefined) and OFFSET is
86 the offset into the common symbol (normally zero, but may be
87 non-zero when referring to a field in a common structure).
88 ORIG is the negative of reloc_entry->addend, which is set by
89 the CALC_ADDEND macro below. We want to replace the value in
90 the object file with NEW + OFFSET, where NEW is the value of
91 the common symbol which we are going to put in the final
92 object file. NEW is symbol->value. */
93 diff
= symbol
->value
+ reloc_entry
->addend
;
95 /* In PE mode, we do not offset the common symbol. */
96 diff
= reloc_entry
->addend
;
101 /* For some reason bfd_perform_relocation always effectively
102 ignores the addend for a COFF target when producing
103 relocateable output. This seems to be always wrong for 386
104 COFF, so we handle the addend here instead. */
105 diff
= reloc_entry
->addend
;
109 /* FIXME: How should this case be handled? */
110 if (reloc_entry
->howto
->type
== R_IMAGEBASE
)
111 diff
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
115 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
119 reloc_howto_type
*howto
= reloc_entry
->howto
;
120 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
126 char x
= bfd_get_8 (abfd
, addr
);
128 bfd_put_8 (abfd
, x
, addr
);
134 short x
= bfd_get_16 (abfd
, addr
);
136 bfd_put_16 (abfd
, x
, addr
);
142 long x
= bfd_get_32 (abfd
, addr
);
144 bfd_put_32 (abfd
, x
, addr
);
153 /* Now let bfd_perform_relocation finish everything up. */
154 return bfd_reloc_continue
;
159 /* Return true if this relocation should appear in the output .reloc
162 static boolean in_reloc_p
PARAMS ((bfd
*, reloc_howto_type
*));
164 static boolean
in_reloc_p (abfd
, howto
)
165 bfd
*abfd ATTRIBUTE_UNUSED
;
166 reloc_howto_type
*howto
;
168 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
171 #endif /* COFF_WITH_PE */
174 #define PCRELOFFSET false
177 static reloc_howto_type howto_table
[] =
185 HOWTO (R_DIR32
, /* type */
187 2, /* size (0 = byte, 1 = short, 2 = long) */
189 false, /* pc_relative */
191 complain_overflow_bitfield
, /* complain_on_overflow */
192 coff_i386_reloc
, /* special_function */
194 true, /* partial_inplace */
195 0xffffffff, /* src_mask */
196 0xffffffff, /* dst_mask */
197 true), /* pcrel_offset */
198 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
199 HOWTO (R_IMAGEBASE
, /* type */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
203 false, /* pc_relative */
205 complain_overflow_bitfield
, /* complain_on_overflow */
206 coff_i386_reloc
, /* special_function */
208 true, /* partial_inplace */
209 0xffffffff, /* src_mask */
210 0xffffffff, /* dst_mask */
211 false), /* pcrel_offset */
219 /* Byte relocation (017). */
220 HOWTO (R_RELBYTE
, /* type */
222 0, /* 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 0x000000ff, /* src_mask */
231 0x000000ff, /* dst_mask */
232 PCRELOFFSET
), /* pcrel_offset */
233 /* 16-bit word relocation (020). */
234 HOWTO (R_RELWORD
, /* type */
236 1, /* 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 0x0000ffff, /* src_mask */
245 0x0000ffff, /* dst_mask */
246 PCRELOFFSET
), /* pcrel_offset */
247 /* 32-bit longword relocation (021). */
248 HOWTO (R_RELLONG
, /* type */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
252 false, /* pc_relative */
254 complain_overflow_bitfield
, /* complain_on_overflow */
255 coff_i386_reloc
, /* special_function */
257 true, /* partial_inplace */
258 0xffffffff, /* src_mask */
259 0xffffffff, /* dst_mask */
260 PCRELOFFSET
), /* pcrel_offset */
261 /* Byte PC relative relocation (022). */
262 HOWTO (R_PCRBYTE
, /* type */
264 0, /* size (0 = byte, 1 = short, 2 = long) */
266 true, /* pc_relative */
268 complain_overflow_signed
, /* complain_on_overflow */
269 coff_i386_reloc
, /* special_function */
271 true, /* partial_inplace */
272 0x000000ff, /* src_mask */
273 0x000000ff, /* dst_mask */
274 PCRELOFFSET
), /* pcrel_offset */
275 /* 16-bit word PC relative relocation (023). */
276 HOWTO (R_PCRWORD
, /* type */
278 1, /* size (0 = byte, 1 = short, 2 = long) */
280 true, /* pc_relative */
282 complain_overflow_signed
, /* complain_on_overflow */
283 coff_i386_reloc
, /* special_function */
285 true, /* partial_inplace */
286 0x0000ffff, /* src_mask */
287 0x0000ffff, /* dst_mask */
288 PCRELOFFSET
), /* pcrel_offset */
289 /* 32-bit longword PC relative relocation (024). */
290 HOWTO (R_PCRLONG
, /* type */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
294 true, /* pc_relative */
296 complain_overflow_signed
, /* complain_on_overflow */
297 coff_i386_reloc
, /* special_function */
299 true, /* partial_inplace */
300 0xffffffff, /* src_mask */
301 0xffffffff, /* dst_mask */
302 PCRELOFFSET
) /* pcrel_offset */
305 /* Turn a howto into a reloc nunmber */
307 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
308 #define BADMAG(x) I386BADMAG(x)
309 #define I386 1 /* Customize coffcode.h */
311 #define RTYPE2HOWTO(cache_ptr, dst) \
312 ((cache_ptr)->howto = \
313 ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
314 ? howto_table + (dst)->r_type \
317 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
318 library. On some other COFF targets STYP_BSS is normally
320 #define BSS_NOLOAD_IS_SHARED_LIBRARY
322 /* Compute the addend of a reloc. If the reloc is to a common symbol,
323 the object file contains the value of the common symbol. By the
324 time this is called, the linker may be using a different symbol
325 from a different object file with a different value. Therefore, we
326 hack wildly to locate the original symbol from this file so that we
327 can make the correct adjustment. This macro sets coffsym to the
328 symbol from the original file, and uses it to set the addend value
329 correctly. If this is not a common symbol, the usual addend
330 calculation is done, except that an additional tweak is needed for
332 FIXME: This macro refers to symbols and asect; these are from the
333 calling function, not the macro arguments. */
335 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
337 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
338 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
339 coffsym = (obj_symbols (abfd) \
340 + (cache_ptr->sym_ptr_ptr - symbols)); \
342 coffsym = coff_symbol_from (abfd, ptr); \
343 if (coffsym != (coff_symbol_type *) NULL \
344 && coffsym->native->u.syment.n_scnum == 0) \
345 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
346 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
347 && ptr->section != (asection *) NULL) \
348 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
350 cache_ptr->addend = 0; \
351 if (ptr && howto_table[reloc.r_type].pc_relative) \
352 cache_ptr->addend += asect->vma; \
355 /* We use the special COFF backend linker. For normal i386 COFF, we
356 can use the generic relocate_section routine. For PE, we need our
361 #define coff_relocate_section _bfd_coff_generic_relocate_section
363 #else /* COFF_WITH_PE */
365 /* The PE relocate section routine. The only difference between this
366 and the regular routine is that we don't want to do anything for a
367 relocateable link. */
369 static boolean coff_pe_i386_relocate_section
370 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
371 struct internal_reloc
*, struct internal_syment
*, asection
**));
374 coff_pe_i386_relocate_section (output_bfd
, info
, input_bfd
,
375 input_section
, contents
, relocs
, syms
,
378 struct bfd_link_info
*info
;
380 asection
*input_section
;
382 struct internal_reloc
*relocs
;
383 struct internal_syment
*syms
;
386 if (info
->relocateable
)
389 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
390 input_section
, contents
,
391 relocs
, syms
, sections
);
394 #define coff_relocate_section coff_pe_i386_relocate_section
396 #endif /* COFF_WITH_PE */
398 /* Convert an rtype to howto for the COFF backend linker. */
400 static reloc_howto_type
*
401 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
402 bfd
*abfd ATTRIBUTE_UNUSED
;
404 struct internal_reloc
*rel
;
405 struct coff_link_hash_entry
*h
;
406 struct internal_syment
*sym
;
409 reloc_howto_type
*howto
;
411 if (rel
->r_type
> sizeof (howto_table
) / sizeof (howto_table
[0]))
413 bfd_set_error (bfd_error_bad_value
);
417 howto
= howto_table
+ rel
->r_type
;
420 /* Cancel out code in _bfd_coff_generic_relocate_section. */
424 if (howto
->pc_relative
)
425 *addendp
+= sec
->vma
;
427 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
429 /* This is a common symbol. The section contents include the
430 size (sym->n_value) as an addend. The relocate_section
431 function will be adding in the final value of the symbol. We
432 need to subtract out the current size in order to get the
435 BFD_ASSERT (h
!= NULL
);
438 /* I think we *do* want to bypass this. If we don't, I have
439 seen some data parameters get the wrong relocation address.
440 If I link two versions with and without this section bypassed
441 and then do a binary comparison, the addresses which are
442 different can be looked up in the map. The case in which
443 this section has been bypassed has addresses which correspond
444 to values I can find in the map. */
445 *addendp
-= sym
->n_value
;
450 /* If the output symbol is common (in which case this must be a
451 relocateable link), we need to add in the final size of the
453 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
454 *addendp
+= h
->root
.u
.c
.size
;
458 if (howto
->pc_relative
)
462 /* If the symbol is defined, then the generic code is going to
463 add back the symbol value in order to cancel out an
464 adjustment it made to the addend. However, we set the addend
465 to 0 at the start of this function. We need to adjust here,
466 to avoid the adjustment the generic code will make. FIXME:
467 This is getting a bit hackish. */
468 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
469 *addendp
-= sym
->n_value
;
472 if (rel
->r_type
== R_IMAGEBASE
)
474 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
481 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
483 static reloc_howto_type
*
484 coff_i386_reloc_type_lookup (abfd
, code
)
485 bfd
*abfd ATTRIBUTE_UNUSED
;
486 bfd_reloc_code_real_type code
;
491 return howto_table
+ R_IMAGEBASE
;
493 return howto_table
+ R_DIR32
;
494 case BFD_RELOC_32_PCREL
:
495 return howto_table
+ R_PCRLONG
;
497 return howto_table
+ R_RELWORD
;
498 case BFD_RELOC_16_PCREL
:
499 return howto_table
+ R_PCRWORD
;
501 return howto_table
+ R_RELBYTE
;
502 case BFD_RELOC_8_PCREL
:
503 return howto_table
+ R_PCRBYTE
;
510 #define coff_rtype_to_howto coff_i386_rtype_to_howto
512 #ifdef TARGET_UNDERSCORE
514 /* If i386 gcc uses underscores for symbol names, then it does not use
515 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
516 we treat all symbols starting with L as local. */
518 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
521 coff_i386_is_local_label_name (abfd
, name
)
528 return _bfd_coff_is_local_label_name (abfd
, name
);
531 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
533 #endif /* TARGET_UNDERSCORE */
535 #include "coffcode.h"
547 "coff-i386", /* name */
549 bfd_target_coff_flavour
,
550 BFD_ENDIAN_LITTLE
, /* data byte order is little */
551 BFD_ENDIAN_LITTLE
, /* header byte order is little */
553 (HAS_RELOC
| EXEC_P
| /* object flags */
554 HAS_LINENO
| HAS_DEBUG
|
555 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
557 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
559 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
561 | SEC_CODE
| SEC_DATA
),
563 #ifdef TARGET_UNDERSCORE
564 TARGET_UNDERSCORE
, /* leading underscore */
566 0, /* leading underscore */
568 '/', /* ar_pad_char */
569 15, /* ar_max_namelen */
571 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
572 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
573 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
574 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
575 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
576 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
578 /* Note that we allow an object file to be treated as a core file as well. */
579 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
580 bfd_generic_archive_p
, coff_object_p
},
581 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
583 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
584 _bfd_write_archive_contents
, bfd_false
},
586 BFD_JUMP_TABLE_GENERIC (coff
),
587 BFD_JUMP_TABLE_COPY (coff
),
588 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
589 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
590 BFD_JUMP_TABLE_SYMBOLS (coff
),
591 BFD_JUMP_TABLE_RELOCS (coff
),
592 BFD_JUMP_TABLE_WRITE (coff
),
593 BFD_JUMP_TABLE_LINK (coff
),
594 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),