]> 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, 1997 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24
25 #include "coff/i386.h"
26
27 #include "coff/internal.h"
28
29 #ifdef COFF_WITH_PE
30 #include "coff/pe.h"
31 #endif
32
33 #ifdef COFF_GO32_EXE
34 #include "coff/go32exe.h"
35 #endif
36
37 #include "libcoff.h"
38
39 static bfd_reloc_status_type coff_i386_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static reloc_howto_type *coff_i386_rtype_to_howto
42 PARAMS ((bfd *, asection *, struct internal_reloc *,
43 struct coff_link_hash_entry *, struct internal_syment *,
44
45 bfd_vma *));
46
47 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
48 /* The page size is a guess based on ELF. */
49
50 #define COFF_PAGE_SIZE 0x1000
51
52 /* For some reason when using i386 COFF the value stored in the .text
53 section for a reference to a common symbol is the value itself plus
54 any desired offset. Ian Taylor, Cygnus Support. */
55
56 /* If we are producing relocateable output, we need to do some
57 adjustments to the object file that are not done by the
58 bfd_perform_relocation function. This function is called by every
59 reloc type to make any required adjustments. */
60
61 static bfd_reloc_status_type
62 coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
63 error_message)
64 bfd *abfd;
65 arelent *reloc_entry;
66 asymbol *symbol;
67 PTR data;
68 asection *input_section;
69 bfd *output_bfd;
70 char **error_message;
71 {
72 symvalue diff;
73
74 if (output_bfd == (bfd *) NULL)
75 return bfd_reloc_continue;
76
77 if (bfd_is_com_section (symbol->section))
78 {
79 /* We are relocating a common symbol. The current value in the
80 object file is ORIG + OFFSET, where ORIG is the value of the
81 common symbol as seen by the object file when it was compiled
82 (this may be zero if the symbol was undefined) and OFFSET is
83 the offset into the common symbol (normally zero, but may be
84 non-zero when referring to a field in a common structure).
85 ORIG is the negative of reloc_entry->addend, which is set by
86 the CALC_ADDEND macro below. We want to replace the value in
87 the object file with NEW + OFFSET, where NEW is the value of
88 the common symbol which we are going to put in the final
89 object file. NEW is symbol->value. */
90 diff = symbol->value + reloc_entry->addend;
91 }
92 else
93 {
94 /* For some reason bfd_perform_relocation always effectively
95 ignores the addend for a COFF target when producing
96 relocateable output. This seems to be always wrong for 386
97 COFF, so we handle the addend here instead. */
98 diff = reloc_entry->addend;
99 }
100
101 #ifdef COFF_WITH_PE
102 /* FIXME: How should this case be handled? */
103 if (reloc_entry->howto->type == R_IMAGEBASE && diff != 0)
104 abort ();
105 #endif
106
107 #define DOIT(x) \
108 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
109
110 if (diff != 0)
111 {
112 reloc_howto_type *howto = reloc_entry->howto;
113 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
114
115 switch (howto->size)
116 {
117 case 0:
118 {
119 char x = bfd_get_8 (abfd, addr);
120 DOIT (x);
121 bfd_put_8 (abfd, x, addr);
122 }
123 break;
124
125 case 1:
126 {
127 short x = bfd_get_16 (abfd, addr);
128 DOIT (x);
129 bfd_put_16 (abfd, x, addr);
130 }
131 break;
132
133 case 2:
134 {
135 long x = bfd_get_32 (abfd, addr);
136 DOIT (x);
137 bfd_put_32 (abfd, x, addr);
138 }
139 break;
140
141 default:
142 abort ();
143 }
144 }
145
146 /* Now let bfd_perform_relocation finish everything up. */
147 return bfd_reloc_continue;
148 }
149
150 #ifdef COFF_WITH_PE
151 /* Return true if this relocation should
152 appear in the output .reloc section. */
153
154 static boolean in_reloc_p(abfd, howto)
155 bfd * abfd;
156 reloc_howto_type *howto;
157 {
158 return ! howto->pc_relative && howto->type != R_IMAGEBASE;
159 }
160 #endif
161
162 #ifndef PCRELOFFSET
163 #define PCRELOFFSET false
164 #endif
165
166 static reloc_howto_type howto_table[] =
167 {
168 {0},
169 {1},
170 {2},
171 {3},
172 {4},
173 {5},
174 HOWTO (R_DIR32, /* type */
175 0, /* rightshift */
176 2, /* size (0 = byte, 1 = short, 2 = long) */
177 32, /* bitsize */
178 false, /* pc_relative */
179 0, /* bitpos */
180 complain_overflow_bitfield, /* complain_on_overflow */
181 coff_i386_reloc, /* special_function */
182 "dir32", /* name */
183 true, /* partial_inplace */
184 0xffffffff, /* src_mask */
185 0xffffffff, /* dst_mask */
186 true), /* pcrel_offset */
187 /* {7}, */
188 HOWTO (R_IMAGEBASE, /* type */
189 0, /* rightshift */
190 2, /* size (0 = byte, 1 = short, 2 = long) */
191 32, /* bitsize */
192 false, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_bitfield, /* complain_on_overflow */
195 coff_i386_reloc, /* special_function */
196 "rva32", /* name */
197 true, /* partial_inplace */
198 0xffffffff, /* src_mask */
199 0xffffffff, /* dst_mask */
200 false), /* pcrel_offset */
201 {010},
202 {011},
203 {012},
204 {013},
205 {014},
206 {015},
207 {016},
208 HOWTO (R_RELBYTE, /* type */
209 0, /* rightshift */
210 0, /* size (0 = byte, 1 = short, 2 = long) */
211 8, /* bitsize */
212 false, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_bitfield, /* complain_on_overflow */
215 coff_i386_reloc, /* special_function */
216 "8", /* name */
217 true, /* partial_inplace */
218 0x000000ff, /* src_mask */
219 0x000000ff, /* dst_mask */
220 PCRELOFFSET), /* pcrel_offset */
221 HOWTO (R_RELWORD, /* type */
222 0, /* rightshift */
223 1, /* size (0 = byte, 1 = short, 2 = long) */
224 16, /* bitsize */
225 false, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_bitfield, /* complain_on_overflow */
228 coff_i386_reloc, /* special_function */
229 "16", /* name */
230 true, /* partial_inplace */
231 0x0000ffff, /* src_mask */
232 0x0000ffff, /* dst_mask */
233 PCRELOFFSET), /* pcrel_offset */
234 HOWTO (R_RELLONG, /* type */
235 0, /* rightshift */
236 2, /* size (0 = byte, 1 = short, 2 = long) */
237 32, /* bitsize */
238 false, /* pc_relative */
239 0, /* bitpos */
240 complain_overflow_bitfield, /* complain_on_overflow */
241 coff_i386_reloc, /* special_function */
242 "32", /* name */
243 true, /* partial_inplace */
244 0xffffffff, /* src_mask */
245 0xffffffff, /* dst_mask */
246 PCRELOFFSET), /* pcrel_offset */
247 HOWTO (R_PCRBYTE, /* type */
248 0, /* rightshift */
249 0, /* size (0 = byte, 1 = short, 2 = long) */
250 8, /* bitsize */
251 true, /* pc_relative */
252 0, /* bitpos */
253 complain_overflow_signed, /* complain_on_overflow */
254 coff_i386_reloc, /* special_function */
255 "DISP8", /* name */
256 true, /* partial_inplace */
257 0x000000ff, /* src_mask */
258 0x000000ff, /* dst_mask */
259 PCRELOFFSET), /* pcrel_offset */
260 HOWTO (R_PCRWORD, /* type */
261 0, /* rightshift */
262 1, /* size (0 = byte, 1 = short, 2 = long) */
263 16, /* bitsize */
264 true, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_signed, /* complain_on_overflow */
267 coff_i386_reloc, /* special_function */
268 "DISP16", /* name */
269 true, /* partial_inplace */
270 0x0000ffff, /* src_mask */
271 0x0000ffff, /* dst_mask */
272 PCRELOFFSET), /* pcrel_offset */
273 HOWTO (R_PCRLONG, /* type */
274 0, /* rightshift */
275 2, /* size (0 = byte, 1 = short, 2 = long) */
276 32, /* bitsize */
277 true, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_signed, /* complain_on_overflow */
280 coff_i386_reloc, /* special_function */
281 "DISP32", /* name */
282 true, /* partial_inplace */
283 0xffffffff, /* src_mask */
284 0xffffffff, /* dst_mask */
285 PCRELOFFSET) /* pcrel_offset */
286 };
287
288 /* Turn a howto into a reloc nunmber */
289
290 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
291 #define BADMAG(x) I386BADMAG(x)
292 #define I386 1 /* Customize coffcode.h */
293
294 #define RTYPE2HOWTO(cache_ptr, dst) \
295 (cache_ptr)->howto = howto_table + (dst)->r_type;
296
297 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
298 library. On some other COFF targets STYP_BSS is normally
299 STYP_NOLOAD. */
300 #define BSS_NOLOAD_IS_SHARED_LIBRARY
301
302 /* Compute the addend of a reloc. If the reloc is to a common symbol,
303 the object file contains the value of the common symbol. By the
304 time this is called, the linker may be using a different symbol
305 from a different object file with a different value. Therefore, we
306 hack wildly to locate the original symbol from this file so that we
307 can make the correct adjustment. This macro sets coffsym to the
308 symbol from the original file, and uses it to set the addend value
309 correctly. If this is not a common symbol, the usual addend
310 calculation is done, except that an additional tweak is needed for
311 PC relative relocs.
312 FIXME: This macro refers to symbols and asect; these are from the
313 calling function, not the macro arguments. */
314
315 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
316 { \
317 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
318 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
319 coffsym = (obj_symbols (abfd) \
320 + (cache_ptr->sym_ptr_ptr - symbols)); \
321 else if (ptr) \
322 coffsym = coff_symbol_from (abfd, ptr); \
323 if (coffsym != (coff_symbol_type *) NULL \
324 && coffsym->native->u.syment.n_scnum == 0) \
325 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
326 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
327 && ptr->section != (asection *) NULL) \
328 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
329 else \
330 cache_ptr->addend = 0; \
331 if (ptr && howto_table[reloc.r_type].pc_relative) \
332 cache_ptr->addend += asect->vma; \
333 }
334
335 /* We use the special COFF backend linker. */
336 #define coff_relocate_section _bfd_coff_generic_relocate_section
337
338 static reloc_howto_type *
339 coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
340 bfd *abfd;
341 asection *sec;
342 struct internal_reloc *rel;
343 struct coff_link_hash_entry *h;
344 struct internal_syment *sym;
345 bfd_vma *addendp;
346 {
347
348 reloc_howto_type *howto;
349
350 howto = howto_table + rel->r_type;
351
352 #ifdef COFF_WITH_PE
353 *addendp = 0;
354 #endif
355
356 if (howto->pc_relative)
357 *addendp += sec->vma;
358
359 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
360 {
361 /* This is a common symbol. The section contents include the
362 size (sym->n_value) as an addend. The relocate_section
363 function will be adding in the final value of the symbol. We
364 need to subtract out the current size in order to get the
365 correct result. */
366
367 BFD_ASSERT (h != NULL);
368
369
370 #ifndef COFF_WITH_PE
371 /* I think we *do* want to bypass this. If we don't, I have seen some data
372 parameters get the wrong relcation address. If I link two versions
373 with and without this section bypassed and then do a binary comparison,
374 the addresses which are different can be looked up in the map. The
375 case in which this section has been bypassed has addresses which correspond
376 to values I can find in the map */
377 *addendp -= sym->n_value;
378 #endif
379 }
380
381 /* If the output symbol is common (in which case this must be a
382 relocateable link), we need to add in the final size of the
383 common symbol. */
384 if (h != NULL && h->root.type == bfd_link_hash_common)
385 *addendp += h->root.u.c.size;
386
387
388 #ifdef COFF_WITH_PE
389 if (howto->pc_relative)
390 *addendp -= 4;
391
392 if (rel->r_type == R_IMAGEBASE)
393 {
394 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
395 }
396 #endif
397
398 return howto;
399 }
400
401
402 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
403
404
405 static reloc_howto_type *
406 coff_i386_reloc_type_lookup (abfd, code)
407 bfd *abfd;
408 bfd_reloc_code_real_type code;
409 {
410 switch (code)
411 {
412 case BFD_RELOC_RVA:
413 return howto_table +R_IMAGEBASE;
414 case BFD_RELOC_32:
415 return howto_table + R_DIR32;
416 case BFD_RELOC_32_PCREL:
417 return howto_table + R_PCRLONG;
418 default:
419 BFD_FAIL ();
420 return 0;
421 }
422 }
423
424 #define coff_rtype_to_howto coff_i386_rtype_to_howto
425
426 #ifdef TARGET_UNDERSCORE
427
428 /* If i386 gcc uses underscores for symbol names, then it does not use
429 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
430 we treat all symbols starting with L as local. */
431
432 static boolean coff_i386_is_local_label_name PARAMS ((bfd *, const char *));
433
434 static boolean
435 coff_i386_is_local_label_name (abfd, name)
436 bfd *abfd;
437 const char *name;
438 {
439 if (name[0] == 'L')
440 return true;
441
442 return _bfd_coff_is_local_label_name (abfd, name);
443 }
444
445 #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
446
447 #endif /* TARGET_UNDERSCORE */
448
449 #include "coffcode.h"
450
451 static const bfd_target *
452 i3coff_object_p (abfd)
453 bfd *abfd;
454 {
455 #ifdef COFF_IMAGE_WITH_PE
456 /* We need to hack badly to handle a PE image correctly. In PE
457 images created by the GNU linker, the offset to the COFF header
458 is always the size. However, this is not the case in images
459 generated by other PE linkers. The PE format stores a four byte
460 offset to the PE signature just before the COFF header at
461 location 0x3c of the file. We pick up that offset, verify that
462 the PE signature is there, and then set ourselves up to read in
463 the COFF header. */
464 {
465 bfd_byte ext_offset[4];
466 file_ptr offset;
467 bfd_byte ext_signature[4];
468 unsigned long signature;
469
470 if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0
471 || bfd_read (ext_offset, 1, 4, abfd) != 4)
472 {
473 if (bfd_get_error () != bfd_error_system_call)
474 bfd_set_error (bfd_error_wrong_format);
475 return NULL;
476 }
477 offset = bfd_h_get_32 (abfd, ext_offset);
478 if (bfd_seek (abfd, offset, SEEK_SET) != 0
479 || bfd_read (ext_signature, 1, 4, abfd) != 4)
480 {
481 if (bfd_get_error () != bfd_error_system_call)
482 bfd_set_error (bfd_error_wrong_format);
483 return NULL;
484 }
485 signature = bfd_h_get_32 (abfd, ext_signature);
486
487 if (signature != 0x4550)
488 {
489 bfd_set_error (bfd_error_wrong_format);
490 return NULL;
491 }
492
493 /* Here is the hack. coff_object_p wants to read filhsz bytes to
494 pick up the COFF header. We adjust so that that will work. 20
495 is the size of the i386 COFF filehdr. */
496
497 if (bfd_seek (abfd,
498 (bfd_tell (abfd)
499 - bfd_coff_filhsz (abfd)
500 + 20),
501 SEEK_SET)
502 != 0)
503 {
504 if (bfd_get_error () != bfd_error_system_call)
505 bfd_set_error (bfd_error_wrong_format);
506 return NULL;
507 }
508 }
509 #endif
510
511 return coff_object_p (abfd);
512 }
513
514 const bfd_target
515 #ifdef TARGET_SYM
516 TARGET_SYM =
517 #else
518 i386coff_vec =
519 #endif
520 {
521 #ifdef TARGET_NAME
522 TARGET_NAME,
523 #else
524 "coff-i386", /* name */
525 #endif
526 bfd_target_coff_flavour,
527 BFD_ENDIAN_LITTLE, /* data byte order is little */
528 BFD_ENDIAN_LITTLE, /* header byte order is little */
529
530 (HAS_RELOC | EXEC_P | /* object flags */
531 HAS_LINENO | HAS_DEBUG |
532 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
533
534 #ifndef COFF_WITH_PE
535 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
536 | SEC_CODE | SEC_DATA),
537 #else
538 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
539 | SEC_CODE | SEC_DATA
540 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
541 #endif
542
543 #ifdef TARGET_UNDERSCORE
544 TARGET_UNDERSCORE, /* leading underscore */
545 #else
546 0, /* leading underscore */
547 #endif
548 '/', /* ar_pad_char */
549 15, /* ar_max_namelen */
550
551 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
552 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
553 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
554 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
555 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
556 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
557
558 /* Note that we allow an object file to be treated as a core file as well. */
559 {_bfd_dummy_target, i3coff_object_p, /* bfd_check_format */
560 bfd_generic_archive_p, i3coff_object_p},
561 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
562 bfd_false},
563 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
564 _bfd_write_archive_contents, bfd_false},
565
566 BFD_JUMP_TABLE_GENERIC (coff),
567 BFD_JUMP_TABLE_COPY (coff),
568 BFD_JUMP_TABLE_CORE (_bfd_nocore),
569 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
570 BFD_JUMP_TABLE_SYMBOLS (coff),
571 BFD_JUMP_TABLE_RELOCS (coff),
572 BFD_JUMP_TABLE_WRITE (coff),
573 BFD_JUMP_TABLE_LINK (coff),
574 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
575
576 COFF_SWAP_TABLE,
577 };