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
;
77 if (output_bfd
== (bfd
*) NULL
)
78 return bfd_reloc_continue
;
81 if (bfd_is_com_section (symbol
->section
))
84 /* We are relocating a common symbol. The current value in the
85 object file is ORIG + OFFSET, where ORIG is the value of the
86 common symbol as seen by the object file when it was compiled
87 (this may be zero if the symbol was undefined) and OFFSET is
88 the offset into the common symbol (normally zero, but may be
89 non-zero when referring to a field in a common structure).
90 ORIG is the negative of reloc_entry->addend, which is set by
91 the CALC_ADDEND macro below. We want to replace the value in
92 the object file with NEW + OFFSET, where NEW is the value of
93 the common symbol which we are going to put in the final
94 object file. NEW is symbol->value. */
95 diff
= symbol
->value
+ reloc_entry
->addend
;
97 /* In PE mode, we do not offset the common symbol. */
98 diff
= reloc_entry
->addend
;
103 /* For some reason bfd_perform_relocation always effectively
104 ignores the addend for a COFF target when producing
105 relocateable output. This seems to be always wrong for 386
106 COFF, so we handle the addend here instead. */
108 if (output_bfd
== (bfd
*) NULL
)
110 reloc_howto_type
*howto
= reloc_entry
->howto
;
112 /* Although PC relative relocations are very similar between
113 PE and non-PE formats, but they are off by 1 << howto->size
114 bytes. For the external relocation, PE is very different
115 from others. See md_apply_fix3 () in gas/config/tc-i386.c.
116 When we link PE and non-PE object files together to
117 generate a non-PE executable, we have to compensate it
119 if (howto
->pc_relative
== true && howto
->pcrel_offset
== true)
120 diff
= -(1 << howto
->size
);
122 diff
= -reloc_entry
->addend
;
126 diff
= reloc_entry
->addend
;
130 /* FIXME: How should this case be handled? */
131 if (reloc_entry
->howto
->type
== R_IMAGEBASE
)
132 diff
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
136 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
140 reloc_howto_type
*howto
= reloc_entry
->howto
;
141 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
147 char x
= bfd_get_8 (abfd
, addr
);
149 bfd_put_8 (abfd
, x
, addr
);
155 short x
= bfd_get_16 (abfd
, addr
);
157 bfd_put_16 (abfd
, x
, addr
);
163 long x
= bfd_get_32 (abfd
, addr
);
165 bfd_put_32 (abfd
, x
, addr
);
174 /* Now let bfd_perform_relocation finish everything up. */
175 return bfd_reloc_continue
;
180 /* Return true if this relocation should appear in the output .reloc
183 static boolean in_reloc_p
PARAMS ((bfd
*, reloc_howto_type
*));
185 static boolean
in_reloc_p (abfd
, howto
)
186 bfd
*abfd ATTRIBUTE_UNUSED
;
187 reloc_howto_type
*howto
;
189 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
192 #endif /* COFF_WITH_PE */
195 #define PCRELOFFSET false
198 static reloc_howto_type howto_table
[] =
206 HOWTO (R_DIR32
, /* type */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
210 false, /* pc_relative */
212 complain_overflow_bitfield
, /* complain_on_overflow */
213 coff_i386_reloc
, /* special_function */
215 true, /* partial_inplace */
216 0xffffffff, /* src_mask */
217 0xffffffff, /* dst_mask */
218 true), /* pcrel_offset */
219 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
220 HOWTO (R_IMAGEBASE
, /* type */
222 2, /* 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 0xffffffff, /* src_mask */
231 0xffffffff, /* dst_mask */
232 false), /* pcrel_offset */
240 /* Byte relocation (017). */
241 HOWTO (R_RELBYTE
, /* type */
243 0, /* size (0 = byte, 1 = short, 2 = long) */
245 false, /* pc_relative */
247 complain_overflow_bitfield
, /* complain_on_overflow */
248 coff_i386_reloc
, /* special_function */
250 true, /* partial_inplace */
251 0x000000ff, /* src_mask */
252 0x000000ff, /* dst_mask */
253 PCRELOFFSET
), /* pcrel_offset */
254 /* 16-bit word relocation (020). */
255 HOWTO (R_RELWORD
, /* type */
257 1, /* size (0 = byte, 1 = short, 2 = long) */
259 false, /* pc_relative */
261 complain_overflow_bitfield
, /* complain_on_overflow */
262 coff_i386_reloc
, /* special_function */
264 true, /* partial_inplace */
265 0x0000ffff, /* src_mask */
266 0x0000ffff, /* dst_mask */
267 PCRELOFFSET
), /* pcrel_offset */
268 /* 32-bit longword relocation (021). */
269 HOWTO (R_RELLONG
, /* type */
271 2, /* size (0 = byte, 1 = short, 2 = long) */
273 false, /* pc_relative */
275 complain_overflow_bitfield
, /* 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 */
282 /* Byte PC relative relocation (022). */
283 HOWTO (R_PCRBYTE
, /* type */
285 0, /* size (0 = byte, 1 = short, 2 = long) */
287 true, /* pc_relative */
289 complain_overflow_signed
, /* complain_on_overflow */
290 coff_i386_reloc
, /* special_function */
292 true, /* partial_inplace */
293 0x000000ff, /* src_mask */
294 0x000000ff, /* dst_mask */
295 PCRELOFFSET
), /* pcrel_offset */
296 /* 16-bit word PC relative relocation (023). */
297 HOWTO (R_PCRWORD
, /* type */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
301 true, /* pc_relative */
303 complain_overflow_signed
, /* complain_on_overflow */
304 coff_i386_reloc
, /* special_function */
306 true, /* partial_inplace */
307 0x0000ffff, /* src_mask */
308 0x0000ffff, /* dst_mask */
309 PCRELOFFSET
), /* pcrel_offset */
310 /* 32-bit longword PC relative relocation (024). */
311 HOWTO (R_PCRLONG
, /* type */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
315 true, /* pc_relative */
317 complain_overflow_signed
, /* complain_on_overflow */
318 coff_i386_reloc
, /* special_function */
320 true, /* partial_inplace */
321 0xffffffff, /* src_mask */
322 0xffffffff, /* dst_mask */
323 PCRELOFFSET
) /* pcrel_offset */
326 /* Turn a howto into a reloc nunmber */
328 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
329 #define BADMAG(x) I386BADMAG(x)
330 #define I386 1 /* Customize coffcode.h */
332 #define RTYPE2HOWTO(cache_ptr, dst) \
333 ((cache_ptr)->howto = \
334 ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
335 ? howto_table + (dst)->r_type \
338 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
339 library. On some other COFF targets STYP_BSS is normally
341 #define BSS_NOLOAD_IS_SHARED_LIBRARY
343 /* Compute the addend of a reloc. If the reloc is to a common symbol,
344 the object file contains the value of the common symbol. By the
345 time this is called, the linker may be using a different symbol
346 from a different object file with a different value. Therefore, we
347 hack wildly to locate the original symbol from this file so that we
348 can make the correct adjustment. This macro sets coffsym to the
349 symbol from the original file, and uses it to set the addend value
350 correctly. If this is not a common symbol, the usual addend
351 calculation is done, except that an additional tweak is needed for
353 FIXME: This macro refers to symbols and asect; these are from the
354 calling function, not the macro arguments. */
356 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
358 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
359 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
360 coffsym = (obj_symbols (abfd) \
361 + (cache_ptr->sym_ptr_ptr - symbols)); \
363 coffsym = coff_symbol_from (abfd, ptr); \
364 if (coffsym != (coff_symbol_type *) NULL \
365 && coffsym->native->u.syment.n_scnum == 0) \
366 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
367 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
368 && ptr->section != (asection *) NULL) \
369 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
371 cache_ptr->addend = 0; \
372 if (ptr && howto_table[reloc.r_type].pc_relative) \
373 cache_ptr->addend += asect->vma; \
376 /* We use the special COFF backend linker. For normal i386 COFF, we
377 can use the generic relocate_section routine. For PE, we need our
382 #define coff_relocate_section _bfd_coff_generic_relocate_section
384 #else /* COFF_WITH_PE */
386 /* The PE relocate section routine. The only difference between this
387 and the regular routine is that we don't want to do anything for a
388 relocateable link. */
390 static boolean coff_pe_i386_relocate_section
391 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
392 struct internal_reloc
*, struct internal_syment
*, asection
**));
395 coff_pe_i386_relocate_section (output_bfd
, info
, input_bfd
,
396 input_section
, contents
, relocs
, syms
,
399 struct bfd_link_info
*info
;
401 asection
*input_section
;
403 struct internal_reloc
*relocs
;
404 struct internal_syment
*syms
;
407 if (info
->relocateable
)
410 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
411 input_section
, contents
,
412 relocs
, syms
, sections
);
415 #define coff_relocate_section coff_pe_i386_relocate_section
417 #endif /* COFF_WITH_PE */
419 /* Convert an rtype to howto for the COFF backend linker. */
421 static reloc_howto_type
*
422 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
423 bfd
*abfd ATTRIBUTE_UNUSED
;
425 struct internal_reloc
*rel
;
426 struct coff_link_hash_entry
*h
;
427 struct internal_syment
*sym
;
430 reloc_howto_type
*howto
;
432 if (rel
->r_type
> sizeof (howto_table
) / sizeof (howto_table
[0]))
434 bfd_set_error (bfd_error_bad_value
);
438 howto
= howto_table
+ rel
->r_type
;
441 /* Cancel out code in _bfd_coff_generic_relocate_section. */
445 if (howto
->pc_relative
)
446 *addendp
+= sec
->vma
;
448 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
450 /* This is a common symbol. The section contents include the
451 size (sym->n_value) as an addend. The relocate_section
452 function will be adding in the final value of the symbol. We
453 need to subtract out the current size in order to get the
456 BFD_ASSERT (h
!= NULL
);
459 /* I think we *do* want to bypass this. If we don't, I have
460 seen some data parameters get the wrong relocation address.
461 If I link two versions with and without this section bypassed
462 and then do a binary comparison, the addresses which are
463 different can be looked up in the map. The case in which
464 this section has been bypassed has addresses which correspond
465 to values I can find in the map. */
466 *addendp
-= sym
->n_value
;
471 /* If the output symbol is common (in which case this must be a
472 relocateable link), we need to add in the final size of the
474 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
475 *addendp
+= h
->root
.u
.c
.size
;
479 if (howto
->pc_relative
)
483 /* If the symbol is defined, then the generic code is going to
484 add back the symbol value in order to cancel out an
485 adjustment it made to the addend. However, we set the addend
486 to 0 at the start of this function. We need to adjust here,
487 to avoid the adjustment the generic code will make. FIXME:
488 This is getting a bit hackish. */
489 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
490 *addendp
-= sym
->n_value
;
493 if (rel
->r_type
== R_IMAGEBASE
)
495 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
502 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
504 static reloc_howto_type
*
505 coff_i386_reloc_type_lookup (abfd
, code
)
506 bfd
*abfd ATTRIBUTE_UNUSED
;
507 bfd_reloc_code_real_type code
;
512 return howto_table
+ R_IMAGEBASE
;
514 return howto_table
+ R_DIR32
;
515 case BFD_RELOC_32_PCREL
:
516 return howto_table
+ R_PCRLONG
;
518 return howto_table
+ R_RELWORD
;
519 case BFD_RELOC_16_PCREL
:
520 return howto_table
+ R_PCRWORD
;
522 return howto_table
+ R_RELBYTE
;
523 case BFD_RELOC_8_PCREL
:
524 return howto_table
+ R_PCRBYTE
;
531 #define coff_rtype_to_howto coff_i386_rtype_to_howto
533 #ifdef TARGET_UNDERSCORE
535 /* If i386 gcc uses underscores for symbol names, then it does not use
536 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
537 we treat all symbols starting with L as local. */
539 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
542 coff_i386_is_local_label_name (abfd
, name
)
549 return _bfd_coff_is_local_label_name (abfd
, name
);
552 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
554 #endif /* TARGET_UNDERSCORE */
556 #include "coffcode.h"
568 "coff-i386", /* name */
570 bfd_target_coff_flavour
,
571 BFD_ENDIAN_LITTLE
, /* data byte order is little */
572 BFD_ENDIAN_LITTLE
, /* header byte order is little */
574 (HAS_RELOC
| EXEC_P
| /* object flags */
575 HAS_LINENO
| HAS_DEBUG
|
576 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
578 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
580 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
582 | SEC_CODE
| SEC_DATA
),
584 #ifdef TARGET_UNDERSCORE
585 TARGET_UNDERSCORE
, /* leading underscore */
587 0, /* leading underscore */
589 '/', /* ar_pad_char */
590 15, /* ar_max_namelen */
592 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
593 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
594 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
595 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
596 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
597 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
599 /* Note that we allow an object file to be treated as a core file as well. */
600 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
601 bfd_generic_archive_p
, coff_object_p
},
602 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
604 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
605 _bfd_write_archive_contents
, bfd_false
},
607 BFD_JUMP_TABLE_GENERIC (coff
),
608 BFD_JUMP_TABLE_COPY (coff
),
609 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
610 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
611 BFD_JUMP_TABLE_SYMBOLS (coff
),
612 BFD_JUMP_TABLE_RELOCS (coff
),
613 BFD_JUMP_TABLE_WRITE (coff
),
614 BFD_JUMP_TABLE_LINK (coff
),
615 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),