* coff-i386.c (i3coff_object_p): If COFF_IMAGE_WITH_PE, hack to
[external/binutils.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 #include "libcoff.h"
34
35 static bfd_reloc_status_type coff_i386_reloc 
36   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
37 static reloc_howto_type *coff_i386_rtype_to_howto
38   PARAMS ((bfd *, asection *, struct internal_reloc *,
39            struct coff_link_hash_entry *, struct internal_syment *,
40
41            bfd_vma *));
42
43 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
44 /* The page size is a guess based on ELF.  */
45
46 #define COFF_PAGE_SIZE 0x1000
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
57 static bfd_reloc_status_type
58 coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
59                  error_message)
60      bfd *abfd;
61      arelent *reloc_entry;
62      asymbol *symbol;
63      PTR data;
64      asection *input_section;
65      bfd *output_bfd;
66      char **error_message;
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
97 #ifdef COFF_WITH_PE
98   /* FIXME: How should this case be handled?  */
99   if (reloc_entry->howto->type == R_IMAGEBASE && diff != 0)
100     abort ();
101 #endif
102
103 #define DOIT(x) \
104   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
105
106     if (diff != 0)
107       {
108         reloc_howto_type *howto = reloc_entry->howto;
109         unsigned char *addr = (unsigned char *) data + reloc_entry->address;
110
111         switch (howto->size)
112           {
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 ();
139           }
140       }
141
142   /* Now let bfd_perform_relocation finish everything up.  */
143   return bfd_reloc_continue;
144 }
145
146 #ifdef COFF_WITH_PE
147 /* Return true if this relocation should
148    appear in the output .reloc section. */
149
150 static 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
157
158 #ifndef PCRELOFFSET
159 #define PCRELOFFSET false
160 #endif
161
162 static reloc_howto_type howto_table[] = 
163 {
164   {0},
165   {1},
166   {2},
167   {3},
168   {4},
169   {5},
170   HOWTO (R_DIR32,               /* type */                                 
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}, */
184   HOWTO (R_IMAGEBASE,            /* type */                                 
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 */                     
192          "rva32",                  /* name */                                 
193          true,                  /* partial_inplace */                      
194          0xffffffff,            /* src_mask */                             
195          0xffffffff,            /* dst_mask */                             
196          false),                /* pcrel_offset */
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 */                             
216          PCRELOFFSET),          /* pcrel_offset */
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 */                             
229          PCRELOFFSET),          /* pcrel_offset */
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 */                             
242          PCRELOFFSET),          /* pcrel_offset */
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 */                             
255          PCRELOFFSET),          /* pcrel_offset */
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 */                             
268          PCRELOFFSET),          /* pcrel_offset */
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 */                             
281          PCRELOFFSET)           /* pcrel_offset */
282 };
283
284 /* Turn a howto into a reloc  nunmber */
285
286 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
287 #define BADMAG(x) I386BADMAG(x)
288 #define I386 1                  /* Customize coffcode.h */
289
290 #define RTYPE2HOWTO(cache_ptr, dst) \
291             (cache_ptr)->howto = howto_table + (dst)->r_type;
292
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
297
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
331 /* We use the special COFF backend linker.  */
332 #define coff_relocate_section _bfd_coff_generic_relocate_section
333
334 static reloc_howto_type *
335 coff_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 {
343
344   reloc_howto_type *howto;
345
346   howto = howto_table + rel->r_type;
347
348 #ifdef COFF_WITH_PE
349   *addendp = 0;
350 #endif
351
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.  */
362  
363       BFD_ASSERT (h != NULL);
364
365
366 #ifndef COFF_WITH_PE
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 */
373       *addendp -= sym->n_value;
374 #endif
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.  */
380   if (h != NULL && h->root.type == bfd_link_hash_common) 
381     *addendp += h->root.u.c.size;
382
383
384 #ifdef COFF_WITH_PE
385   if (howto->pc_relative)
386     *addendp -= 4;
387
388   if (rel->r_type == R_IMAGEBASE)
389     {
390       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
391     }
392 #endif
393
394   return howto;
395 }
396
397
398 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
399
400
401 static reloc_howto_type *
402 coff_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
422 #define coff_rtype_to_howto coff_i386_rtype_to_howto
423
424 #include "coffcode.h"
425
426 static const bfd_target *
427 i3coff_object_p (abfd)
428      bfd *abfd;
429 {
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);
487 }
488
489 const bfd_target
490 #ifdef TARGET_SYM
491   TARGET_SYM =
492 #else
493   i386coff_vec =
494 #endif
495 {
496 #ifdef TARGET_NAME
497   TARGET_NAME,
498 #else
499   "coff-i386",                  /* name */
500 #endif
501   bfd_target_coff_flavour,
502   BFD_ENDIAN_LITTLE,            /* data byte order is little */
503   BFD_ENDIAN_LITTLE,            /* header byte order is little */
504
505   (HAS_RELOC | EXEC_P |         /* object flags */
506    HAS_LINENO | HAS_DEBUG |
507    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
508
509 #ifndef COFF_WITH_PE
510   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
511 #else
512   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
513    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
514 #endif
515
516 #ifdef TARGET_UNDERSCORE
517   TARGET_UNDERSCORE,            /* leading underscore */
518 #else
519   0,                            /* leading underscore */
520 #endif
521   '/',                          /* ar_pad_char */
522   15,                           /* ar_max_namelen */
523
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 */
530
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},
538
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),
547      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
548
549   COFF_SWAP_TABLE,
550 };