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
;
179 /* Return true if this relocation should appear in the output .reloc
182 static boolean in_reloc_p
PARAMS ((bfd
*, reloc_howto_type
*));
184 static boolean
in_reloc_p (abfd
, howto
)
185 bfd
* abfd ATTRIBUTE_UNUSED
;
186 reloc_howto_type
*howto
;
188 return ! howto
->pc_relative
&& howto
->type
!= R_IMAGEBASE
;
190 #endif /* COFF_WITH_PE */
193 #define PCRELOFFSET false
196 static reloc_howto_type howto_table
[] =
204 HOWTO (R_DIR32
, /* type */
206 2, /* 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 0xffffffff, /* src_mask */
215 0xffffffff, /* dst_mask */
216 true), /* pcrel_offset */
217 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
218 HOWTO (R_IMAGEBASE
, /* type */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
222 false, /* pc_relative */
224 complain_overflow_bitfield
, /* complain_on_overflow */
225 coff_i386_reloc
, /* special_function */
227 true, /* partial_inplace */
228 0xffffffff, /* src_mask */
229 0xffffffff, /* dst_mask */
230 false), /* pcrel_offset */
238 /* Byte relocation (017). */
239 HOWTO (R_RELBYTE
, /* type */
241 0, /* size (0 = byte, 1 = short, 2 = long) */
243 false, /* pc_relative */
245 complain_overflow_bitfield
, /* complain_on_overflow */
246 coff_i386_reloc
, /* special_function */
248 true, /* partial_inplace */
249 0x000000ff, /* src_mask */
250 0x000000ff, /* dst_mask */
251 PCRELOFFSET
), /* pcrel_offset */
252 /* 16-bit word relocation (020). */
253 HOWTO (R_RELWORD
, /* type */
255 1, /* size (0 = byte, 1 = short, 2 = long) */
257 false, /* pc_relative */
259 complain_overflow_bitfield
, /* complain_on_overflow */
260 coff_i386_reloc
, /* special_function */
262 true, /* partial_inplace */
263 0x0000ffff, /* src_mask */
264 0x0000ffff, /* dst_mask */
265 PCRELOFFSET
), /* pcrel_offset */
266 /* 32-bit longword relocation (021). */
267 HOWTO (R_RELLONG
, /* type */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
271 false, /* pc_relative */
273 complain_overflow_bitfield
, /* complain_on_overflow */
274 coff_i386_reloc
, /* special_function */
276 true, /* partial_inplace */
277 0xffffffff, /* src_mask */
278 0xffffffff, /* dst_mask */
279 PCRELOFFSET
), /* pcrel_offset */
280 /* Byte PC relative relocation (022). */
281 HOWTO (R_PCRBYTE
, /* type */
283 0, /* size (0 = byte, 1 = short, 2 = long) */
285 true, /* pc_relative */
287 complain_overflow_signed
, /* complain_on_overflow */
288 coff_i386_reloc
, /* special_function */
290 true, /* partial_inplace */
291 0x000000ff, /* src_mask */
292 0x000000ff, /* dst_mask */
293 PCRELOFFSET
), /* pcrel_offset */
294 /* 16-bit word PC relative relocation (023). */
295 HOWTO (R_PCRWORD
, /* type */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
299 true, /* pc_relative */
301 complain_overflow_signed
, /* complain_on_overflow */
302 coff_i386_reloc
, /* special_function */
304 true, /* partial_inplace */
305 0x0000ffff, /* src_mask */
306 0x0000ffff, /* dst_mask */
307 PCRELOFFSET
), /* pcrel_offset */
308 /* 32-bit longword PC relative relocation (024). */
309 HOWTO (R_PCRLONG
, /* type */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
313 true, /* pc_relative */
315 complain_overflow_signed
, /* complain_on_overflow */
316 coff_i386_reloc
, /* special_function */
318 true, /* partial_inplace */
319 0xffffffff, /* src_mask */
320 0xffffffff, /* dst_mask */
321 PCRELOFFSET
) /* pcrel_offset */
324 /* Turn a howto into a reloc nunmber */
326 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
327 #define BADMAG(x) I386BADMAG(x)
328 #define I386 1 /* Customize coffcode.h */
330 #define RTYPE2HOWTO(cache_ptr, dst) \
331 ((cache_ptr)->howto = \
332 ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
333 ? howto_table + (dst)->r_type \
336 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
337 library. On some other COFF targets STYP_BSS is normally
339 #define BSS_NOLOAD_IS_SHARED_LIBRARY
341 /* Compute the addend of a reloc. If the reloc is to a common symbol,
342 the object file contains the value of the common symbol. By the
343 time this is called, the linker may be using a different symbol
344 from a different object file with a different value. Therefore, we
345 hack wildly to locate the original symbol from this file so that we
346 can make the correct adjustment. This macro sets coffsym to the
347 symbol from the original file, and uses it to set the addend value
348 correctly. If this is not a common symbol, the usual addend
349 calculation is done, except that an additional tweak is needed for
351 FIXME: This macro refers to symbols and asect; these are from the
352 calling function, not the macro arguments. */
354 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
356 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
357 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
358 coffsym = (obj_symbols (abfd) \
359 + (cache_ptr->sym_ptr_ptr - symbols)); \
361 coffsym = coff_symbol_from (abfd, ptr); \
362 if (coffsym != (coff_symbol_type *) NULL \
363 && coffsym->native->u.syment.n_scnum == 0) \
364 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
365 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
366 && ptr->section != (asection *) NULL) \
367 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
369 cache_ptr->addend = 0; \
370 if (ptr && howto_table[reloc.r_type].pc_relative) \
371 cache_ptr->addend += asect->vma; \
374 /* We use the special COFF backend linker. For normal i386 COFF, we
375 can use the generic relocate_section routine. For PE, we need our
380 #define coff_relocate_section _bfd_coff_generic_relocate_section
382 #else /* COFF_WITH_PE */
384 /* The PE relocate section routine. The only difference between this
385 and the regular routine is that we don't want to do anything for a
386 relocateable link. */
388 static boolean coff_pe_i386_relocate_section
389 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
390 struct internal_reloc
*, struct internal_syment
*, asection
**));
393 coff_pe_i386_relocate_section (output_bfd
, info
, input_bfd
,
394 input_section
, contents
, relocs
, syms
,
397 struct bfd_link_info
*info
;
399 asection
*input_section
;
401 struct internal_reloc
*relocs
;
402 struct internal_syment
*syms
;
405 if (info
->relocateable
)
408 return _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
409 input_section
, contents
,
410 relocs
, syms
, sections
);
413 #define coff_relocate_section coff_pe_i386_relocate_section
415 #endif /* COFF_WITH_PE */
417 /* Convert an rtype to howto for the COFF backend linker. */
419 static reloc_howto_type
*
420 coff_i386_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
421 bfd
*abfd ATTRIBUTE_UNUSED
;
423 struct internal_reloc
*rel
;
424 struct coff_link_hash_entry
*h
;
425 struct internal_syment
*sym
;
428 reloc_howto_type
*howto
;
430 if (rel
->r_type
> sizeof (howto_table
) / sizeof (howto_table
[0]))
432 bfd_set_error (bfd_error_bad_value
);
436 howto
= howto_table
+ rel
->r_type
;
439 /* Cancel out code in _bfd_coff_generic_relocate_section. */
443 if (howto
->pc_relative
)
444 *addendp
+= sec
->vma
;
446 if (sym
!= NULL
&& sym
->n_scnum
== 0 && sym
->n_value
!= 0)
448 /* This is a common symbol. The section contents include the
449 size (sym->n_value) as an addend. The relocate_section
450 function will be adding in the final value of the symbol. We
451 need to subtract out the current size in order to get the
454 BFD_ASSERT (h
!= NULL
);
457 /* I think we *do* want to bypass this. If we don't, I have
458 seen some data parameters get the wrong relocation address.
459 If I link two versions with and without this section bypassed
460 and then do a binary comparison, the addresses which are
461 different can be looked up in the map. The case in which
462 this section has been bypassed has addresses which correspond
463 to values I can find in the map. */
464 *addendp
-= sym
->n_value
;
469 /* If the output symbol is common (in which case this must be a
470 relocateable link), we need to add in the final size of the
472 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_common
)
473 *addendp
+= h
->root
.u
.c
.size
;
477 if (howto
->pc_relative
)
481 /* If the symbol is defined, then the generic code is going to
482 add back the symbol value in order to cancel out an
483 adjustment it made to the addend. However, we set the addend
484 to 0 at the start of this function. We need to adjust here,
485 to avoid the adjustment the generic code will make. FIXME:
486 This is getting a bit hackish. */
487 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
488 *addendp
-= sym
->n_value
;
491 if (rel
->r_type
== R_IMAGEBASE
)
493 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
500 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
502 static reloc_howto_type
*
503 coff_i386_reloc_type_lookup (abfd
, code
)
504 bfd
*abfd ATTRIBUTE_UNUSED
;
505 bfd_reloc_code_real_type code
;
510 return howto_table
+ R_IMAGEBASE
;
512 return howto_table
+ R_DIR32
;
513 case BFD_RELOC_32_PCREL
:
514 return howto_table
+ R_PCRLONG
;
516 return howto_table
+ R_RELWORD
;
517 case BFD_RELOC_16_PCREL
:
518 return howto_table
+ R_PCRWORD
;
520 return howto_table
+ R_RELBYTE
;
521 case BFD_RELOC_8_PCREL
:
522 return howto_table
+ R_PCRBYTE
;
529 #define coff_rtype_to_howto coff_i386_rtype_to_howto
531 #ifdef TARGET_UNDERSCORE
533 /* If i386 gcc uses underscores for symbol names, then it does not use
534 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
535 we treat all symbols starting with L as local. */
537 static boolean coff_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
540 coff_i386_is_local_label_name (abfd
, name
)
547 return _bfd_coff_is_local_label_name (abfd
, name
);
550 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
552 #endif /* TARGET_UNDERSCORE */
554 #include "coffcode.h"
566 "coff-i386", /* name */
568 bfd_target_coff_flavour
,
569 BFD_ENDIAN_LITTLE
, /* data byte order is little */
570 BFD_ENDIAN_LITTLE
, /* header byte order is little */
572 (HAS_RELOC
| EXEC_P
| /* object flags */
573 HAS_LINENO
| HAS_DEBUG
|
574 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
576 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
578 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
580 | SEC_CODE
| SEC_DATA
),
582 #ifdef TARGET_UNDERSCORE
583 TARGET_UNDERSCORE
, /* leading underscore */
585 0, /* leading underscore */
587 '/', /* ar_pad_char */
588 15, /* ar_max_namelen */
590 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
591 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
592 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
593 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
594 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
595 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
597 /* Note that we allow an object file to be treated as a core file as well. */
598 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
599 bfd_generic_archive_p
, coff_object_p
},
600 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
602 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
603 _bfd_write_archive_contents
, bfd_false
},
605 BFD_JUMP_TABLE_GENERIC (coff
),
606 BFD_JUMP_TABLE_COPY (coff
),
607 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
608 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
609 BFD_JUMP_TABLE_SYMBOLS (coff
),
610 BFD_JUMP_TABLE_RELOCS (coff
),
611 BFD_JUMP_TABLE_WRITE (coff
),
612 BFD_JUMP_TABLE_LINK (coff
),
613 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),