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