Import gdb-7.0
[dragonfly.git] / contrib / gdb-6.2.1 / bfd / coff-i386.c
blob65700b6038b201384be878eb30cf14459e095ba6
1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
27 #include "coff/i386.h"
29 #include "coff/internal.h"
31 #ifdef COFF_WITH_PE
32 #include "coff/pe.h"
33 #endif
35 #ifdef COFF_GO32_EXE
36 #include "coff/go32exe.h"
37 #endif
39 #include "libcoff.h"
41 static bfd_reloc_status_type coff_i386_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static reloc_howto_type *coff_i386_rtype_to_howto
44 PARAMS ((bfd *, asection *, struct internal_reloc *,
45 struct coff_link_hash_entry *, struct internal_syment *,
46 bfd_vma *));
47 static reloc_howto_type *coff_i386_reloc_type_lookup
48 PARAMS ((bfd *, bfd_reloc_code_real_type));
50 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
51 /* The page size is a guess based on ELF. */
53 #define COFF_PAGE_SIZE 0x1000
55 /* For some reason when using i386 COFF the value stored in the .text
56 section for a reference to a common symbol is the value itself plus
57 any desired offset. Ian Taylor, Cygnus Support. */
59 /* If we are producing relocatable output, we need to do some
60 adjustments to the object file that are not done by the
61 bfd_perform_relocation function. This function is called by every
62 reloc type to make any required adjustments. */
64 static bfd_reloc_status_type
65 coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
66 error_message)
67 bfd *abfd;
68 arelent *reloc_entry;
69 asymbol *symbol;
70 PTR data;
71 asection *input_section ATTRIBUTE_UNUSED;
72 bfd *output_bfd;
73 char **error_message ATTRIBUTE_UNUSED;
75 symvalue diff;
77 #ifndef COFF_WITH_PE
78 if (output_bfd == (bfd *) NULL)
79 return bfd_reloc_continue;
80 #endif
82 if (bfd_is_com_section (symbol->section))
84 #ifndef COFF_WITH_PE
85 /* We are relocating a common symbol. The current value in the
86 object file is ORIG + OFFSET, where ORIG is the value of the
87 common symbol as seen by the object file when it was compiled
88 (this may be zero if the symbol was undefined) and OFFSET is
89 the offset into the common symbol (normally zero, but may be
90 non-zero when referring to a field in a common structure).
91 ORIG is the negative of reloc_entry->addend, which is set by
92 the CALC_ADDEND macro below. We want to replace the value in
93 the object file with NEW + OFFSET, where NEW is the value of
94 the common symbol which we are going to put in the final
95 object file. NEW is symbol->value. */
96 diff = symbol->value + reloc_entry->addend;
97 #else
98 /* In PE mode, we do not offset the common symbol. */
99 diff = reloc_entry->addend;
100 #endif
102 else
104 /* For some reason bfd_perform_relocation always effectively
105 ignores the addend for a COFF target when producing
106 relocatable output. This seems to be always wrong for 386
107 COFF, so we handle the addend here instead. */
108 #ifdef COFF_WITH_PE
109 if (output_bfd == (bfd *) NULL)
111 reloc_howto_type *howto = reloc_entry->howto;
113 /* Although PC relative relocations are very similar between
114 PE and non-PE formats, but they are off by 1 << howto->size
115 bytes. For the external relocation, PE is very different
116 from others. See md_apply_fix3 () in gas/config/tc-i386.c.
117 When we link PE and non-PE object files together to
118 generate a non-PE executable, we have to compensate it
119 here. */
120 if (howto->pc_relative && howto->pcrel_offset)
121 diff = -(1 << howto->size);
122 else
123 diff = -reloc_entry->addend;
125 else
126 #endif
127 diff = reloc_entry->addend;
130 #ifdef COFF_WITH_PE
131 /* FIXME: How should this case be handled? */
132 if (reloc_entry->howto->type == R_IMAGEBASE
133 && output_bfd != NULL
134 && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour)
135 diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
136 #endif
138 #define DOIT(x) \
139 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
141 if (diff != 0)
143 reloc_howto_type *howto = reloc_entry->howto;
144 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
146 switch (howto->size)
148 case 0:
150 char x = bfd_get_8 (abfd, addr);
151 DOIT (x);
152 bfd_put_8 (abfd, x, addr);
154 break;
156 case 1:
158 short x = bfd_get_16 (abfd, addr);
159 DOIT (x);
160 bfd_put_16 (abfd, (bfd_vma) x, addr);
162 break;
164 case 2:
166 long x = bfd_get_32 (abfd, addr);
167 DOIT (x);
168 bfd_put_32 (abfd, (bfd_vma) x, addr);
170 break;
172 default:
173 abort ();
177 /* Now let bfd_perform_relocation finish everything up. */
178 return bfd_reloc_continue;
181 #ifdef COFF_WITH_PE
182 /* Return TRUE if this relocation should appear in the output .reloc
183 section. */
185 static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
187 static bfd_boolean in_reloc_p (abfd, howto)
188 bfd * abfd ATTRIBUTE_UNUSED;
189 reloc_howto_type *howto;
191 return ! howto->pc_relative && howto->type != R_IMAGEBASE;
193 #endif /* COFF_WITH_PE */
195 #ifndef PCRELOFFSET
196 #define PCRELOFFSET FALSE
197 #endif
199 static reloc_howto_type howto_table[] =
201 EMPTY_HOWTO (0),
202 EMPTY_HOWTO (1),
203 EMPTY_HOWTO (2),
204 EMPTY_HOWTO (3),
205 EMPTY_HOWTO (4),
206 EMPTY_HOWTO (5),
207 HOWTO (R_DIR32, /* type */
208 0, /* rightshift */
209 2, /* size (0 = byte, 1 = short, 2 = long) */
210 32, /* bitsize */
211 FALSE, /* pc_relative */
212 0, /* bitpos */
213 complain_overflow_bitfield, /* complain_on_overflow */
214 coff_i386_reloc, /* special_function */
215 "dir32", /* name */
216 TRUE, /* partial_inplace */
217 0xffffffff, /* src_mask */
218 0xffffffff, /* dst_mask */
219 TRUE), /* pcrel_offset */
220 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
221 HOWTO (R_IMAGEBASE, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 FALSE, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_bitfield, /* complain_on_overflow */
228 coff_i386_reloc, /* special_function */
229 "rva32", /* name */
230 TRUE, /* partial_inplace */
231 0xffffffff, /* src_mask */
232 0xffffffff, /* dst_mask */
233 FALSE), /* pcrel_offset */
234 EMPTY_HOWTO (010),
235 EMPTY_HOWTO (011),
236 EMPTY_HOWTO (012),
237 #ifdef COFF_WITH_PE
238 /* 32-bit longword section relative relocation (013). */
239 HOWTO (R_SECREL32, /* type */
240 0, /* rightshift */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
242 32, /* bitsize */
243 FALSE, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 coff_i386_reloc, /* special_function */
247 "secrel32", /* name */
248 TRUE, /* partial_inplace */
249 0xffffffff, /* src_mask */
250 0xffffffff, /* dst_mask */
251 TRUE), /* pcrel_offset */
252 #else
253 EMPTY_HOWTO (013),
254 #endif
255 EMPTY_HOWTO (014),
256 EMPTY_HOWTO (015),
257 EMPTY_HOWTO (016),
258 /* Byte relocation (017). */
259 HOWTO (R_RELBYTE, /* type */
260 0, /* rightshift */
261 0, /* size (0 = byte, 1 = short, 2 = long) */
262 8, /* bitsize */
263 FALSE, /* pc_relative */
264 0, /* bitpos */
265 complain_overflow_bitfield, /* complain_on_overflow */
266 coff_i386_reloc, /* special_function */
267 "8", /* name */
268 TRUE, /* partial_inplace */
269 0x000000ff, /* src_mask */
270 0x000000ff, /* dst_mask */
271 PCRELOFFSET), /* pcrel_offset */
272 /* 16-bit word relocation (020). */
273 HOWTO (R_RELWORD, /* type */
274 0, /* rightshift */
275 1, /* size (0 = byte, 1 = short, 2 = long) */
276 16, /* bitsize */
277 FALSE, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_bitfield, /* complain_on_overflow */
280 coff_i386_reloc, /* special_function */
281 "16", /* name */
282 TRUE, /* partial_inplace */
283 0x0000ffff, /* src_mask */
284 0x0000ffff, /* dst_mask */
285 PCRELOFFSET), /* pcrel_offset */
286 /* 32-bit longword relocation (021). */
287 HOWTO (R_RELLONG, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 32, /* bitsize */
291 FALSE, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 coff_i386_reloc, /* special_function */
295 "32", /* name */
296 TRUE, /* partial_inplace */
297 0xffffffff, /* src_mask */
298 0xffffffff, /* dst_mask */
299 PCRELOFFSET), /* pcrel_offset */
300 /* Byte PC relative relocation (022). */
301 HOWTO (R_PCRBYTE, /* type */
302 0, /* rightshift */
303 0, /* size (0 = byte, 1 = short, 2 = long) */
304 8, /* bitsize */
305 TRUE, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_signed, /* complain_on_overflow */
308 coff_i386_reloc, /* special_function */
309 "DISP8", /* name */
310 TRUE, /* partial_inplace */
311 0x000000ff, /* src_mask */
312 0x000000ff, /* dst_mask */
313 PCRELOFFSET), /* pcrel_offset */
314 /* 16-bit word PC relative relocation (023). */
315 HOWTO (R_PCRWORD, /* type */
316 0, /* rightshift */
317 1, /* size (0 = byte, 1 = short, 2 = long) */
318 16, /* bitsize */
319 TRUE, /* pc_relative */
320 0, /* bitpos */
321 complain_overflow_signed, /* complain_on_overflow */
322 coff_i386_reloc, /* special_function */
323 "DISP16", /* name */
324 TRUE, /* partial_inplace */
325 0x0000ffff, /* src_mask */
326 0x0000ffff, /* dst_mask */
327 PCRELOFFSET), /* pcrel_offset */
328 /* 32-bit longword PC relative relocation (024). */
329 HOWTO (R_PCRLONG, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 32, /* bitsize */
333 TRUE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_signed, /* complain_on_overflow */
336 coff_i386_reloc, /* special_function */
337 "DISP32", /* name */
338 TRUE, /* partial_inplace */
339 0xffffffff, /* src_mask */
340 0xffffffff, /* dst_mask */
341 PCRELOFFSET) /* pcrel_offset */
344 /* Turn a howto into a reloc nunmber */
346 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
347 #define BADMAG(x) I386BADMAG(x)
348 #define I386 1 /* Customize coffcode.h */
350 #define RTYPE2HOWTO(cache_ptr, dst) \
351 ((cache_ptr)->howto = \
352 ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0]) \
353 ? howto_table + (dst)->r_type \
354 : NULL))
356 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
357 library. On some other COFF targets STYP_BSS is normally
358 STYP_NOLOAD. */
359 #define BSS_NOLOAD_IS_SHARED_LIBRARY
361 /* Compute the addend of a reloc. If the reloc is to a common symbol,
362 the object file contains the value of the common symbol. By the
363 time this is called, the linker may be using a different symbol
364 from a different object file with a different value. Therefore, we
365 hack wildly to locate the original symbol from this file so that we
366 can make the correct adjustment. This macro sets coffsym to the
367 symbol from the original file, and uses it to set the addend value
368 correctly. If this is not a common symbol, the usual addend
369 calculation is done, except that an additional tweak is needed for
370 PC relative relocs.
371 FIXME: This macro refers to symbols and asect; these are from the
372 calling function, not the macro arguments. */
374 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
376 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
377 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
378 coffsym = (obj_symbols (abfd) \
379 + (cache_ptr->sym_ptr_ptr - symbols)); \
380 else if (ptr) \
381 coffsym = coff_symbol_from (abfd, ptr); \
382 if (coffsym != (coff_symbol_type *) NULL \
383 && coffsym->native->u.syment.n_scnum == 0) \
384 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
385 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
386 && ptr->section != (asection *) NULL) \
387 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
388 else \
389 cache_ptr->addend = 0; \
390 if (ptr && howto_table[reloc.r_type].pc_relative) \
391 cache_ptr->addend += asect->vma; \
394 /* We use the special COFF backend linker. For normal i386 COFF, we
395 can use the generic relocate_section routine. For PE, we need our
396 own routine. */
398 #ifndef COFF_WITH_PE
400 #define coff_relocate_section _bfd_coff_generic_relocate_section
402 #else /* COFF_WITH_PE */
404 /* The PE relocate section routine. The only difference between this
405 and the regular routine is that we don't want to do anything for a
406 relocatable link. */
408 static bfd_boolean coff_pe_i386_relocate_section
409 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
410 struct internal_reloc *, struct internal_syment *, asection **));
412 static bfd_boolean
413 coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
414 input_section, contents, relocs, syms,
415 sections)
416 bfd *output_bfd;
417 struct bfd_link_info *info;
418 bfd *input_bfd;
419 asection *input_section;
420 bfd_byte *contents;
421 struct internal_reloc *relocs;
422 struct internal_syment *syms;
423 asection **sections;
425 if (info->relocatable)
426 return TRUE;
428 return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
429 input_section, contents,
430 relocs, syms, sections);
433 #define coff_relocate_section coff_pe_i386_relocate_section
435 #endif /* COFF_WITH_PE */
437 /* Convert an rtype to howto for the COFF backend linker. */
439 static reloc_howto_type *
440 coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
441 bfd *abfd ATTRIBUTE_UNUSED;
442 asection *sec;
443 struct internal_reloc *rel;
444 struct coff_link_hash_entry *h;
445 struct internal_syment *sym;
446 bfd_vma *addendp;
448 reloc_howto_type *howto;
450 if (rel->r_type > sizeof (howto_table) / sizeof (howto_table[0]))
452 bfd_set_error (bfd_error_bad_value);
453 return NULL;
456 howto = howto_table + rel->r_type;
458 #ifdef COFF_WITH_PE
459 /* Cancel out code in _bfd_coff_generic_relocate_section. */
460 *addendp = 0;
461 #endif
463 if (howto->pc_relative)
464 *addendp += sec->vma;
466 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
468 /* This is a common symbol. The section contents include the
469 size (sym->n_value) as an addend. The relocate_section
470 function will be adding in the final value of the symbol. We
471 need to subtract out the current size in order to get the
472 correct result. */
474 BFD_ASSERT (h != NULL);
476 #ifndef COFF_WITH_PE
477 /* I think we *do* want to bypass this. If we don't, I have
478 seen some data parameters get the wrong relocation address.
479 If I link two versions with and without this section bypassed
480 and then do a binary comparison, the addresses which are
481 different can be looked up in the map. The case in which
482 this section has been bypassed has addresses which correspond
483 to values I can find in the map. */
484 *addendp -= sym->n_value;
485 #endif
488 #ifndef COFF_WITH_PE
489 /* If the output symbol is common (in which case this must be a
490 relocatable link), we need to add in the final size of the
491 common symbol. */
492 if (h != NULL && h->root.type == bfd_link_hash_common)
493 *addendp += h->root.u.c.size;
494 #endif
496 #ifdef COFF_WITH_PE
497 if (howto->pc_relative)
499 *addendp -= 4;
501 /* If the symbol is defined, then the generic code is going to
502 add back the symbol value in order to cancel out an
503 adjustment it made to the addend. However, we set the addend
504 to 0 at the start of this function. We need to adjust here,
505 to avoid the adjustment the generic code will make. FIXME:
506 This is getting a bit hackish. */
507 if (sym != NULL && sym->n_scnum != 0)
508 *addendp -= sym->n_value;
511 if (rel->r_type == R_IMAGEBASE
512 && (bfd_get_flavour(sec->output_section->owner)
513 == bfd_target_coff_flavour))
515 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
518 if (rel->r_type == R_SECREL32)
520 bfd_vma osect_vma;
522 if (h && (h->type == bfd_link_hash_defined
523 || h->type == bfd_link_hash_defweak))
524 osect_vma = h->root.u.def.section->output_section->vma;
525 else
527 asection *sec;
528 int i;
530 /* Sigh, the only way to get the section to offset against
531 is to find it the hard way. */
533 for (sec = abfd->sections, i = 1; i < sym->n_scnum; i++)
534 sec = sec->next;
536 osect_vma = sec->output_section->vma;
539 *addendp -= osect_vma;
541 #endif
543 return howto;
546 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
548 static reloc_howto_type *
549 coff_i386_reloc_type_lookup (abfd, code)
550 bfd *abfd ATTRIBUTE_UNUSED;
551 bfd_reloc_code_real_type code;
553 switch (code)
555 case BFD_RELOC_RVA:
556 return howto_table + R_IMAGEBASE;
557 case BFD_RELOC_32:
558 return howto_table + R_DIR32;
559 case BFD_RELOC_32_PCREL:
560 return howto_table + R_PCRLONG;
561 case BFD_RELOC_16:
562 return howto_table + R_RELWORD;
563 case BFD_RELOC_16_PCREL:
564 return howto_table + R_PCRWORD;
565 case BFD_RELOC_8:
566 return howto_table + R_RELBYTE;
567 case BFD_RELOC_8_PCREL:
568 return howto_table + R_PCRBYTE;
569 #ifdef COFF_WITH_PE
570 case BFD_RELOC_32_SECREL:
571 return howto_table + R_SECREL32;
572 #endif
573 default:
574 BFD_FAIL ();
575 return 0;
579 #define coff_rtype_to_howto coff_i386_rtype_to_howto
581 #ifdef TARGET_UNDERSCORE
583 /* If i386 gcc uses underscores for symbol names, then it does not use
584 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
585 we treat all symbols starting with L as local. */
587 static bfd_boolean coff_i386_is_local_label_name
588 PARAMS ((bfd *, const char *));
590 static bfd_boolean
591 coff_i386_is_local_label_name (abfd, name)
592 bfd *abfd;
593 const char *name;
595 if (name[0] == 'L')
596 return TRUE;
598 return _bfd_coff_is_local_label_name (abfd, name);
601 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
603 #endif /* TARGET_UNDERSCORE */
605 #include "coffcode.h"
607 const bfd_target
608 #ifdef TARGET_SYM
609 TARGET_SYM =
610 #else
611 i386coff_vec =
612 #endif
614 #ifdef TARGET_NAME
615 TARGET_NAME,
616 #else
617 "coff-i386", /* name */
618 #endif
619 bfd_target_coff_flavour,
620 BFD_ENDIAN_LITTLE, /* data byte order is little */
621 BFD_ENDIAN_LITTLE, /* header byte order is little */
623 (HAS_RELOC | EXEC_P | /* object flags */
624 HAS_LINENO | HAS_DEBUG |
625 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
627 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
628 #ifdef COFF_WITH_PE
629 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY
630 #endif
631 | SEC_CODE | SEC_DATA),
633 #ifdef TARGET_UNDERSCORE
634 TARGET_UNDERSCORE, /* leading underscore */
635 #else
636 0, /* leading underscore */
637 #endif
638 '/', /* ar_pad_char */
639 15, /* ar_max_namelen */
641 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
642 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
643 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
644 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
645 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
646 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
648 /* Note that we allow an object file to be treated as a core file as well. */
649 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
650 bfd_generic_archive_p, coff_object_p},
651 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
652 bfd_false},
653 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
654 _bfd_write_archive_contents, bfd_false},
656 BFD_JUMP_TABLE_GENERIC (coff),
657 BFD_JUMP_TABLE_COPY (coff),
658 BFD_JUMP_TABLE_CORE (_bfd_nocore),
659 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
660 BFD_JUMP_TABLE_SYMBOLS (coff),
661 BFD_JUMP_TABLE_RELOCS (coff),
662 BFD_JUMP_TABLE_WRITE (coff),
663 BFD_JUMP_TABLE_LINK (coff),
664 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
666 NULL,
668 COFF_SWAP_TABLE