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