* binary.c (binary_set_section_contents): Don't return early for a
[deliverable/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
179ce6e1
ILT
33#ifdef COFF_GO32_EXE
34#include "coff/go32exe.h"
35#endif
36
517496c5
SC
37#include "libcoff.h"
38
82b1edf7
KR
39static bfd_reloc_status_type coff_i386_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
89665c85 41static reloc_howto_type *coff_i386_rtype_to_howto
82b1edf7
KR
42 PARAMS ((bfd *, asection *, struct internal_reloc *,
43 struct coff_link_hash_entry *, struct internal_syment *,
89665c85 44
82b1edf7
KR
45 bfd_vma *));
46
47#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
82b1edf7 48/* The page size is a guess based on ELF. */
a5655244 49
82b1edf7 50#define COFF_PAGE_SIZE 0x1000
48ee0757
SS
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
61static bfd_reloc_status_type
6812b607
ILT
62coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
63 error_message)
48ee0757
SS
64 bfd *abfd;
65 arelent *reloc_entry;
66 asymbol *symbol;
67 PTR data;
68 asection *input_section;
69 bfd *output_bfd;
6812b607 70 char **error_message;
48ee0757
SS
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
a5655244 101#ifdef COFF_WITH_PE
3ce6d941
ILT
102 /* FIXME: How should this case be handled? */
103 if (reloc_entry->howto->type == R_IMAGEBASE && diff != 0)
104 abort ();
a5655244 105#endif
48ee0757 106
a5655244
ILT
107#define DOIT(x) \
108 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
48ee0757 109
a5655244
ILT
110 if (diff != 0)
111 {
112 reloc_howto_type *howto = reloc_entry->howto;
113 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
48ee0757 114
a5655244 115 switch (howto->size)
48ee0757 116 {
a5655244
ILT
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 ();
48ee0757 143 }
a5655244 144 }
48ee0757
SS
145
146 /* Now let bfd_perform_relocation finish everything up. */
147 return bfd_reloc_continue;
148}
517496c5 149
3ce6d941
ILT
150#ifdef COFF_WITH_PE
151/* Return true if this relocation should
152 appear in the output .reloc section. */
89665c85 153
3ce6d941
ILT
154static 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
89665c85
SC
161
162#ifndef PCRELOFFSET
163#define PCRELOFFSET false
164#endif
165
517496c5
SC
166static reloc_howto_type howto_table[] =
167{
48ee0757
SS
168 {0},
169 {1},
170 {2},
171 {3},
172 {4},
173 {5},
174 HOWTO (R_DIR32, /* type */
89665c85
SC
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}, */
a5655244 188 HOWTO (R_IMAGEBASE, /* type */
48ee0757
SS
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 */
a5655244 196 "rva32", /* name */
48ee0757
SS
197 true, /* partial_inplace */
198 0xffffffff, /* src_mask */
199 0xffffffff, /* dst_mask */
200 false), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 220 PCRELOFFSET), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 233 PCRELOFFSET), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 246 PCRELOFFSET), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 259 PCRELOFFSET), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 272 PCRELOFFSET), /* pcrel_offset */
48ee0757
SS
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 */
89665c85 285 PCRELOFFSET) /* pcrel_offset */
517496c5
SC
286};
287
517496c5
SC
288/* Turn a howto into a reloc nunmber */
289
6812b607 290#define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
517496c5 291#define BADMAG(x) I386BADMAG(x)
3b4f1a5d
SC
292#define I386 1 /* Customize coffcode.h */
293
c9301d7b 294#define RTYPE2HOWTO(cache_ptr, dst) \
82b1edf7 295 (cache_ptr)->howto = howto_table + (dst)->r_type;
3b4f1a5d 296
48ee0757
SS
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
517496c5 301
48ee0757
SS
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
82b1edf7
KR
335/* We use the special COFF backend linker. */
336#define coff_relocate_section _bfd_coff_generic_relocate_section
337
89665c85 338static reloc_howto_type *
82b1edf7
KR
339coff_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{
89665c85
SC
347
348 reloc_howto_type *howto;
82b1edf7
KR
349
350 howto = howto_table + rel->r_type;
351
89665c85
SC
352#ifdef COFF_WITH_PE
353 *addendp = 0;
354#endif
355
82b1edf7
KR
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. */
89665c85 366
82b1edf7 367 BFD_ASSERT (h != NULL);
89665c85
SC
368
369
370#ifndef COFF_WITH_PE
a5655244
ILT
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 */
82b1edf7 377 *addendp -= sym->n_value;
89665c85 378#endif
82b1edf7
KR
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. */
89665c85 384 if (h != NULL && h->root.type == bfd_link_hash_common)
82b1edf7
KR
385 *addendp += h->root.u.c.size;
386
89665c85
SC
387
388#ifdef COFF_WITH_PE
389 if (howto->pc_relative)
a5655244
ILT
390 *addendp -= 4;
391
392 if (rel->r_type == R_IMAGEBASE)
393 {
394 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
395 }
89665c85
SC
396#endif
397
82b1edf7
KR
398 return howto;
399}
400
3ce6d941
ILT
401
402#define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
403
404
405static reloc_howto_type *
406coff_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
40496096 424#define coff_rtype_to_howto coff_i386_rtype_to_howto
3ce6d941 425
40496096 426#ifdef TARGET_UNDERSCORE
3ce6d941 427
40496096
ILT
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
432static boolean coff_i386_is_local_label_name PARAMS ((bfd *, const char *));
433
434static boolean
435coff_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 */
82b1edf7 448
48ee0757 449#include "coffcode.h"
517496c5 450
82b1edf7 451static const bfd_target *
3ce6d941
ILT
452i3coff_object_p (abfd)
453 bfd *abfd;
cbd8493e 454{
3ce6d941
ILT
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);
cbd8493e 512}
517496c5 513
82b1edf7 514const bfd_target
48ee0757
SS
515#ifdef TARGET_SYM
516 TARGET_SYM =
517#else
518 i386coff_vec =
519#endif
517496c5 520{
48ee0757
SS
521#ifdef TARGET_NAME
522 TARGET_NAME,
523#else
3b4f1a5d 524 "coff-i386", /* name */
48ee0757 525#endif
6a469027 526 bfd_target_coff_flavour,
3ce6d941
ILT
527 BFD_ENDIAN_LITTLE, /* data byte order is little */
528 BFD_ENDIAN_LITTLE, /* header byte order is little */
517496c5
SC
529
530 (HAS_RELOC | EXEC_P | /* object flags */
531 HAS_LINENO | HAS_DEBUG |
82b1edf7 532 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
517496c5 533
3ce6d941 534#ifndef COFF_WITH_PE
40496096
ILT
535 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
536 | SEC_CODE | SEC_DATA),
3ce6d941
ILT
537#else
538 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
40496096 539 | SEC_CODE | SEC_DATA
3ce6d941
ILT
540 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
541#endif
542
82b1edf7
KR
543#ifdef TARGET_UNDERSCORE
544 TARGET_UNDERSCORE, /* leading underscore */
545#else
294eaca4 546 0, /* leading underscore */
82b1edf7 547#endif
517496c5
SC
548 '/', /* ar_pad_char */
549 15, /* ar_max_namelen */
550
48ee0757
SS
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 */
517496c5 557
6a469027
JG
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},
517496c5 565
6812b607
ILT
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),
82b1edf7 574 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
6812b607 575
48ee0757
SS
576 COFF_SWAP_TABLE,
577};
This page took 0.252276 seconds and 4 git commands to generate.