file as.info-5 was initially added on branch binutils-2_10-branch.
[binutils.git] / bfd / coff-i386.c
blob84d4d145c7b6f269acb708e40a8d95f1448b8678
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. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
26 #include "coff/i386.h"
28 #include "coff/internal.h"
30 #ifdef COFF_WITH_PE
31 #include "coff/pe.h"
32 #endif
34 #ifdef COFF_GO32_EXE
35 #include "coff/go32exe.h"
36 #endif
38 #include "libcoff.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 *,
45 bfd_vma *));
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,
65 error_message)
66 bfd *abfd;
67 arelent *reloc_entry;
68 asymbol *symbol;
69 PTR data;
70 asection *input_section ATTRIBUTE_UNUSED;
71 bfd *output_bfd;
72 char **error_message ATTRIBUTE_UNUSED;
74 symvalue diff;
76 if (output_bfd == (bfd *) NULL)
77 return bfd_reloc_continue;
79 if (bfd_is_com_section (symbol->section))
81 #ifndef COFF_WITH_PE
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;
94 #else
95 /* In PE mode, we do not offset the common symbol. */
96 diff = reloc_entry->addend;
97 #endif
99 else
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;
108 #ifdef COFF_WITH_PE
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;
112 #endif
114 #define DOIT(x) \
115 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
117 if (diff != 0)
119 reloc_howto_type *howto = reloc_entry->howto;
120 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
122 switch (howto->size)
124 case 0:
126 char x = bfd_get_8 (abfd, addr);
127 DOIT (x);
128 bfd_put_8 (abfd, x, addr);
130 break;
132 case 1:
134 short x = bfd_get_16 (abfd, addr);
135 DOIT (x);
136 bfd_put_16 (abfd, x, addr);
138 break;
140 case 2:
142 long x = bfd_get_32 (abfd, addr);
143 DOIT (x);
144 bfd_put_32 (abfd, x, addr);
146 break;
148 default:
149 abort ();
153 /* Now let bfd_perform_relocation finish everything up. */
154 return bfd_reloc_continue;
157 #ifdef COFF_WITH_PE
159 /* Return true if this relocation should appear in the output .reloc
160 section. */
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 */
173 #ifndef PCRELOFFSET
174 #define PCRELOFFSET false
175 #endif
177 static reloc_howto_type howto_table[] =
179 EMPTY_HOWTO (0),
180 EMPTY_HOWTO (1),
181 EMPTY_HOWTO (2),
182 EMPTY_HOWTO (3),
183 EMPTY_HOWTO (4),
184 EMPTY_HOWTO (5),
185 HOWTO (R_DIR32, /* type */
186 0, /* rightshift */
187 2, /* size (0 = byte, 1 = short, 2 = long) */
188 32, /* bitsize */
189 false, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 coff_i386_reloc, /* special_function */
193 "dir32", /* name */
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 */
200 0, /* rightshift */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
202 32, /* bitsize */
203 false, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_bitfield, /* complain_on_overflow */
206 coff_i386_reloc, /* special_function */
207 "rva32", /* name */
208 true, /* partial_inplace */
209 0xffffffff, /* src_mask */
210 0xffffffff, /* dst_mask */
211 false), /* pcrel_offset */
212 EMPTY_HOWTO (010),
213 EMPTY_HOWTO (011),
214 EMPTY_HOWTO (012),
215 EMPTY_HOWTO (013),
216 EMPTY_HOWTO (014),
217 EMPTY_HOWTO (015),
218 EMPTY_HOWTO (016),
219 /* Byte relocation (017). */
220 HOWTO (R_RELBYTE, /* type */
221 0, /* rightshift */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
223 8, /* bitsize */
224 false, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_bitfield, /* complain_on_overflow */
227 coff_i386_reloc, /* special_function */
228 "8", /* name */
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 */
235 0, /* rightshift */
236 1, /* size (0 = byte, 1 = short, 2 = long) */
237 16, /* bitsize */
238 false, /* pc_relative */
239 0, /* bitpos */
240 complain_overflow_bitfield, /* complain_on_overflow */
241 coff_i386_reloc, /* special_function */
242 "16", /* name */
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 */
249 0, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 32, /* bitsize */
252 false, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_bitfield, /* complain_on_overflow */
255 coff_i386_reloc, /* special_function */
256 "32", /* name */
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 */
263 0, /* rightshift */
264 0, /* size (0 = byte, 1 = short, 2 = long) */
265 8, /* bitsize */
266 true, /* pc_relative */
267 0, /* bitpos */
268 complain_overflow_signed, /* complain_on_overflow */
269 coff_i386_reloc, /* special_function */
270 "DISP8", /* name */
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 */
277 0, /* rightshift */
278 1, /* size (0 = byte, 1 = short, 2 = long) */
279 16, /* bitsize */
280 true, /* pc_relative */
281 0, /* bitpos */
282 complain_overflow_signed, /* complain_on_overflow */
283 coff_i386_reloc, /* special_function */
284 "DISP16", /* name */
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 */
291 0, /* rightshift */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
293 32, /* bitsize */
294 true, /* pc_relative */
295 0, /* bitpos */
296 complain_overflow_signed, /* complain_on_overflow */
297 coff_i386_reloc, /* special_function */
298 "DISP32", /* name */
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 \
315 : NULL))
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
319 STYP_NOLOAD. */
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
331 PC relative relocs.
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)); \
341 else if (ptr) \
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); \
349 else \
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
357 own routine. */
359 #ifndef COFF_WITH_PE
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 **));
373 static boolean
374 coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
375 input_section, contents, relocs, syms,
376 sections)
377 bfd *output_bfd;
378 struct bfd_link_info *info;
379 bfd *input_bfd;
380 asection *input_section;
381 bfd_byte *contents;
382 struct internal_reloc *relocs;
383 struct internal_syment *syms;
384 asection **sections;
386 if (info->relocateable)
387 return true;
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;
403 asection *sec;
404 struct internal_reloc *rel;
405 struct coff_link_hash_entry *h;
406 struct internal_syment *sym;
407 bfd_vma *addendp;
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);
414 return NULL;
417 howto = howto_table + rel->r_type;
419 #ifdef COFF_WITH_PE
420 /* Cancel out code in _bfd_coff_generic_relocate_section. */
421 *addendp = 0;
422 #endif
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
433 correct result. */
435 BFD_ASSERT (h != NULL);
437 #ifndef COFF_WITH_PE
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;
446 #endif
449 #ifndef COFF_WITH_PE
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
452 common symbol. */
453 if (h != NULL && h->root.type == bfd_link_hash_common)
454 *addendp += h->root.u.c.size;
455 #endif
457 #ifdef COFF_WITH_PE
458 if (howto->pc_relative)
460 *addendp -= 4;
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;
476 #endif
478 return howto;
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;
488 switch (code)
490 case BFD_RELOC_RVA:
491 return howto_table + R_IMAGEBASE;
492 case BFD_RELOC_32:
493 return howto_table + R_DIR32;
494 case BFD_RELOC_32_PCREL:
495 return howto_table + R_PCRLONG;
496 case BFD_RELOC_16:
497 return howto_table + R_RELWORD;
498 case BFD_RELOC_16_PCREL:
499 return howto_table + R_PCRWORD;
500 case BFD_RELOC_8:
501 return howto_table + R_RELBYTE;
502 case BFD_RELOC_8_PCREL:
503 return howto_table + R_PCRBYTE;
504 default:
505 BFD_FAIL ();
506 return 0;
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 *));
520 static boolean
521 coff_i386_is_local_label_name (abfd, name)
522 bfd *abfd;
523 const char *name;
525 if (name[0] == 'L')
526 return true;
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"
537 const bfd_target
538 #ifdef TARGET_SYM
539 TARGET_SYM =
540 #else
541 i386coff_vec =
542 #endif
544 #ifdef TARGET_NAME
545 TARGET_NAME,
546 #else
547 "coff-i386", /* name */
548 #endif
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 */
558 #ifdef COFF_WITH_PE
559 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES
560 #endif
561 | SEC_CODE | SEC_DATA),
563 #ifdef TARGET_UNDERSCORE
564 TARGET_UNDERSCORE, /* leading underscore */
565 #else
566 0, /* leading underscore */
567 #endif
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 */
582 bfd_false},
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),
596 NULL,
598 COFF_SWAP_TABLE