]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/coff-i386.c
This commit was generated by cvs2svn to track changes on a CVS vendor
[thirdparty/binutils-gdb.git] / bfd / coff-i386.c
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.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25
26 #include "coff/i386.h"
27
28 #include "coff/internal.h"
29
30 #ifdef COFF_WITH_PE
31 #include "coff/pe.h"
32 #endif
33
34 #ifdef COFF_GO32_EXE
35 #include "coff/go32exe.h"
36 #endif
37
38 #include "libcoff.h"
39
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));
48
49 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
50 /* The page size is a guess based on ELF. */
51
52 #define COFF_PAGE_SIZE 0x1000
53
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. */
57
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. */
62
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;
73 {
74 symvalue diff;
75
76 #ifndef COFF_WITH_PE
77 if (output_bfd == (bfd *) NULL)
78 return bfd_reloc_continue;
79 #endif
80
81 if (bfd_is_com_section (symbol->section))
82 {
83 #ifndef COFF_WITH_PE
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;
96 #else
97 /* In PE mode, we do not offset the common symbol. */
98 diff = reloc_entry->addend;
99 #endif
100 }
101 else
102 {
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. */
107 #ifdef COFF_WITH_PE
108 if (output_bfd == (bfd *) NULL)
109 {
110 reloc_howto_type *howto = reloc_entry->howto;
111
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
118 here. */
119 if (howto->pc_relative == true && howto->pcrel_offset == true)
120 diff = -(1 << howto->size);
121 else
122 diff = -reloc_entry->addend;
123 }
124 else
125 #endif
126 diff = reloc_entry->addend;
127 }
128
129 #ifdef COFF_WITH_PE
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;
133 #endif
134
135 #define DOIT(x) \
136 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
137
138 if (diff != 0)
139 {
140 reloc_howto_type *howto = reloc_entry->howto;
141 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
142
143 switch (howto->size)
144 {
145 case 0:
146 {
147 char x = bfd_get_8 (abfd, addr);
148 DOIT (x);
149 bfd_put_8 (abfd, x, addr);
150 }
151 break;
152
153 case 1:
154 {
155 short x = bfd_get_16 (abfd, addr);
156 DOIT (x);
157 bfd_put_16 (abfd, x, addr);
158 }
159 break;
160
161 case 2:
162 {
163 long x = bfd_get_32 (abfd, addr);
164 DOIT (x);
165 bfd_put_32 (abfd, x, addr);
166 }
167 break;
168
169 default:
170 abort ();
171 }
172 }
173
174 /* Now let bfd_perform_relocation finish everything up. */
175 return bfd_reloc_continue;
176 }
177
178 #ifdef COFF_WITH_PE
179 /* Return true if this relocation should appear in the output .reloc
180 section. */
181
182 static boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
183
184 static boolean in_reloc_p (abfd, howto)
185 bfd * abfd ATTRIBUTE_UNUSED;
186 reloc_howto_type *howto;
187 {
188 return ! howto->pc_relative && howto->type != R_IMAGEBASE;
189 }
190 #endif /* COFF_WITH_PE */
191
192 #ifndef PCRELOFFSET
193 #define PCRELOFFSET false
194 #endif
195
196 static reloc_howto_type howto_table[] =
197 {
198 EMPTY_HOWTO (0),
199 EMPTY_HOWTO (1),
200 EMPTY_HOWTO (2),
201 EMPTY_HOWTO (3),
202 EMPTY_HOWTO (4),
203 EMPTY_HOWTO (5),
204 HOWTO (R_DIR32, /* type */
205 0, /* rightshift */
206 2, /* size (0 = byte, 1 = short, 2 = long) */
207 32, /* bitsize */
208 false, /* pc_relative */
209 0, /* bitpos */
210 complain_overflow_bitfield, /* complain_on_overflow */
211 coff_i386_reloc, /* special_function */
212 "dir32", /* name */
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 */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 32, /* bitsize */
222 false, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_bitfield, /* complain_on_overflow */
225 coff_i386_reloc, /* special_function */
226 "rva32", /* name */
227 true, /* partial_inplace */
228 0xffffffff, /* src_mask */
229 0xffffffff, /* dst_mask */
230 false), /* pcrel_offset */
231 EMPTY_HOWTO (010),
232 EMPTY_HOWTO (011),
233 EMPTY_HOWTO (012),
234 EMPTY_HOWTO (013),
235 EMPTY_HOWTO (014),
236 EMPTY_HOWTO (015),
237 EMPTY_HOWTO (016),
238 /* Byte relocation (017). */
239 HOWTO (R_RELBYTE, /* type */
240 0, /* rightshift */
241 0, /* size (0 = byte, 1 = short, 2 = long) */
242 8, /* bitsize */
243 false, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 coff_i386_reloc, /* special_function */
247 "8", /* name */
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 */
254 0, /* rightshift */
255 1, /* size (0 = byte, 1 = short, 2 = long) */
256 16, /* bitsize */
257 false, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 coff_i386_reloc, /* special_function */
261 "16", /* name */
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 */
268 0, /* rightshift */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
270 32, /* bitsize */
271 false, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 coff_i386_reloc, /* special_function */
275 "32", /* name */
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 */
282 0, /* rightshift */
283 0, /* size (0 = byte, 1 = short, 2 = long) */
284 8, /* bitsize */
285 true, /* pc_relative */
286 0, /* bitpos */
287 complain_overflow_signed, /* complain_on_overflow */
288 coff_i386_reloc, /* special_function */
289 "DISP8", /* name */
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 */
296 0, /* rightshift */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
298 16, /* bitsize */
299 true, /* pc_relative */
300 0, /* bitpos */
301 complain_overflow_signed, /* complain_on_overflow */
302 coff_i386_reloc, /* special_function */
303 "DISP16", /* name */
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 */
310 0, /* rightshift */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
312 32, /* bitsize */
313 true, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_signed, /* complain_on_overflow */
316 coff_i386_reloc, /* special_function */
317 "DISP32", /* name */
318 true, /* partial_inplace */
319 0xffffffff, /* src_mask */
320 0xffffffff, /* dst_mask */
321 PCRELOFFSET) /* pcrel_offset */
322 };
323
324 /* Turn a howto into a reloc nunmber */
325
326 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
327 #define BADMAG(x) I386BADMAG(x)
328 #define I386 1 /* Customize coffcode.h */
329
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 \
334 : NULL))
335
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
338 STYP_NOLOAD. */
339 #define BSS_NOLOAD_IS_SHARED_LIBRARY
340
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
350 PC relative relocs.
351 FIXME: This macro refers to symbols and asect; these are from the
352 calling function, not the macro arguments. */
353
354 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
355 { \
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)); \
360 else if (ptr) \
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); \
368 else \
369 cache_ptr->addend = 0; \
370 if (ptr && howto_table[reloc.r_type].pc_relative) \
371 cache_ptr->addend += asect->vma; \
372 }
373
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
376 own routine. */
377
378 #ifndef COFF_WITH_PE
379
380 #define coff_relocate_section _bfd_coff_generic_relocate_section
381
382 #else /* COFF_WITH_PE */
383
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. */
387
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 **));
391
392 static boolean
393 coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
394 input_section, contents, relocs, syms,
395 sections)
396 bfd *output_bfd;
397 struct bfd_link_info *info;
398 bfd *input_bfd;
399 asection *input_section;
400 bfd_byte *contents;
401 struct internal_reloc *relocs;
402 struct internal_syment *syms;
403 asection **sections;
404 {
405 if (info->relocateable)
406 return true;
407
408 return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
409 input_section, contents,
410 relocs, syms, sections);
411 }
412
413 #define coff_relocate_section coff_pe_i386_relocate_section
414
415 #endif /* COFF_WITH_PE */
416
417 /* Convert an rtype to howto for the COFF backend linker. */
418
419 static reloc_howto_type *
420 coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
421 bfd *abfd ATTRIBUTE_UNUSED;
422 asection *sec;
423 struct internal_reloc *rel;
424 struct coff_link_hash_entry *h;
425 struct internal_syment *sym;
426 bfd_vma *addendp;
427 {
428 reloc_howto_type *howto;
429
430 if (rel->r_type > sizeof (howto_table) / sizeof (howto_table[0]))
431 {
432 bfd_set_error (bfd_error_bad_value);
433 return NULL;
434 }
435
436 howto = howto_table + rel->r_type;
437
438 #ifdef COFF_WITH_PE
439 /* Cancel out code in _bfd_coff_generic_relocate_section. */
440 *addendp = 0;
441 #endif
442
443 if (howto->pc_relative)
444 *addendp += sec->vma;
445
446 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
447 {
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
452 correct result. */
453
454 BFD_ASSERT (h != NULL);
455
456 #ifndef COFF_WITH_PE
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;
465 #endif
466 }
467
468 #ifndef COFF_WITH_PE
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
471 common symbol. */
472 if (h != NULL && h->root.type == bfd_link_hash_common)
473 *addendp += h->root.u.c.size;
474 #endif
475
476 #ifdef COFF_WITH_PE
477 if (howto->pc_relative)
478 {
479 *addendp -= 4;
480
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;
489 }
490
491 if (rel->r_type == R_IMAGEBASE)
492 {
493 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
494 }
495 #endif
496
497 return howto;
498 }
499
500 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
501
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;
506 {
507 switch (code)
508 {
509 case BFD_RELOC_RVA:
510 return howto_table + R_IMAGEBASE;
511 case BFD_RELOC_32:
512 return howto_table + R_DIR32;
513 case BFD_RELOC_32_PCREL:
514 return howto_table + R_PCRLONG;
515 case BFD_RELOC_16:
516 return howto_table + R_RELWORD;
517 case BFD_RELOC_16_PCREL:
518 return howto_table + R_PCRWORD;
519 case BFD_RELOC_8:
520 return howto_table + R_RELBYTE;
521 case BFD_RELOC_8_PCREL:
522 return howto_table + R_PCRBYTE;
523 default:
524 BFD_FAIL ();
525 return 0;
526 }
527 }
528
529 #define coff_rtype_to_howto coff_i386_rtype_to_howto
530
531 #ifdef TARGET_UNDERSCORE
532
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. */
536
537 static boolean coff_i386_is_local_label_name PARAMS ((bfd *, const char *));
538
539 static boolean
540 coff_i386_is_local_label_name (abfd, name)
541 bfd *abfd;
542 const char *name;
543 {
544 if (name[0] == 'L')
545 return true;
546
547 return _bfd_coff_is_local_label_name (abfd, name);
548 }
549
550 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
551
552 #endif /* TARGET_UNDERSCORE */
553
554 #include "coffcode.h"
555
556 const bfd_target
557 #ifdef TARGET_SYM
558 TARGET_SYM =
559 #else
560 i386coff_vec =
561 #endif
562 {
563 #ifdef TARGET_NAME
564 TARGET_NAME,
565 #else
566 "coff-i386", /* name */
567 #endif
568 bfd_target_coff_flavour,
569 BFD_ENDIAN_LITTLE, /* data byte order is little */
570 BFD_ENDIAN_LITTLE, /* header byte order is little */
571
572 (HAS_RELOC | EXEC_P | /* object flags */
573 HAS_LINENO | HAS_DEBUG |
574 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
575
576 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
577 #ifdef COFF_WITH_PE
578 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES
579 #endif
580 | SEC_CODE | SEC_DATA),
581
582 #ifdef TARGET_UNDERSCORE
583 TARGET_UNDERSCORE, /* leading underscore */
584 #else
585 0, /* leading underscore */
586 #endif
587 '/', /* ar_pad_char */
588 15, /* ar_max_namelen */
589
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 */
596
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 */
601 bfd_false},
602 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
603 _bfd_write_archive_contents, bfd_false},
604
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),
614
615 NULL,
616
617 COFF_SWAP_TABLE
618 };