Files removed as of the 1999-08-23 snapshot.
[deliverable/binutils-gdb.git] / bfd / coff-i386.c
index 79b9f94f9afc0b93cbdb20a9d82dfc6f0da6b9a6..e686b7b6cc5b359ceed443c9294328e5e6cbc732 100644 (file)
@@ -1,5 +1,6 @@
 /* BFD back-end for Intel 386 COFF files.
-   Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
+   Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
+   Free Software Foundation, Inc.
    Written by Cygnus Support.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -41,8 +42,10 @@ static bfd_reloc_status_type coff_i386_reloc
 static reloc_howto_type *coff_i386_rtype_to_howto
   PARAMS ((bfd *, asection *, struct internal_reloc *,
           struct coff_link_hash_entry *, struct internal_syment *,
-
           bfd_vma *));
+static reloc_howto_type *coff_i386_reloc_type_lookup
+  PARAMS ((bfd *, bfd_reloc_code_real_type));
+static const bfd_target *i3coff_object_p PARAMS ((bfd *));
 
 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
 /* The page size is a guess based on ELF.  */
@@ -65,9 +68,9 @@ coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
      arelent *reloc_entry;
      asymbol *symbol;
      PTR data;
-     asection *input_section;
+     asection *input_section ATTRIBUTE_UNUSED;
      bfd *output_bfd;
-     char **error_message;
+     char **error_message ATTRIBUTE_UNUSED;
 {
   symvalue diff;
 
@@ -76,6 +79,7 @@ coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
 
   if (bfd_is_com_section (symbol->section))
     {
+#ifndef COFF_WITH_PE
       /* We are relocating a common symbol.  The current value in the
         object file is ORIG + OFFSET, where ORIG is the value of the
         common symbol as seen by the object file when it was compiled
@@ -88,6 +92,10 @@ coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
         the common symbol which we are going to put in the final
         object file.  NEW is symbol->value.  */
       diff = symbol->value + reloc_entry->addend;
+#else
+      /* In PE mode, we do not offset the common symbol.  */
+      diff = reloc_entry->addend;
+#endif
     }
   else
     {
@@ -100,8 +108,8 @@ coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
 
 #ifdef COFF_WITH_PE
   /* FIXME: How should this case be handled?  */
-  if (reloc_entry->howto->type == R_IMAGEBASE && diff != 0)
-    abort ();
+  if (reloc_entry->howto->type == R_IMAGEBASE)
+    diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
 #endif
 
 #define DOIT(x) \
@@ -152,7 +160,7 @@ coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
    appear in the output .reloc section. */
 
 static boolean in_reloc_p(abfd, howto)
-     bfd * abfd;
+     bfd * abfd ATTRIBUTE_UNUSED;
      reloc_howto_type *howto;
 {
   return ! howto->pc_relative && howto->type != R_IMAGEBASE;
@@ -165,12 +173,12 @@ static boolean in_reloc_p(abfd, howto)
 
 static reloc_howto_type howto_table[] = 
 {
-  {0},
-  {1},
-  {2},
-  {3},
-  {4},
-  {5},
+  EMPTY_HOWTO (0),
+  EMPTY_HOWTO (1),
+  EMPTY_HOWTO (2),
+  EMPTY_HOWTO (3),
+  EMPTY_HOWTO (4),
+  EMPTY_HOWTO (5),
   HOWTO (R_DIR32,               /* type */                                 
         0,                     /* rightshift */                           
         2,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -184,7 +192,7 @@ static reloc_howto_type howto_table[] =
         0xffffffff,            /* src_mask */                             
         0xffffffff,            /* dst_mask */                             
         true),                /* pcrel_offset */
-  /* {7}, */
+  /* PE IMAGE_REL_I386_DIR32NB relocation (7).  */
   HOWTO (R_IMAGEBASE,            /* type */                                 
         0,                     /* rightshift */                           
         2,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -198,14 +206,15 @@ static reloc_howto_type howto_table[] =
         0xffffffff,            /* src_mask */                             
         0xffffffff,            /* dst_mask */                             
         false),                /* pcrel_offset */
-  {010},
-  {011},
-  {012},
-  {013},
-  {014},
-  {015},
-  {016},
-  HOWTO (R_RELBYTE,            /* type */                                 
+  EMPTY_HOWTO (010),
+  EMPTY_HOWTO (011),
+  EMPTY_HOWTO (012),
+  EMPTY_HOWTO (013),
+  EMPTY_HOWTO (014),
+  EMPTY_HOWTO (015),
+  EMPTY_HOWTO (016),
+  /* Byte relocation (017).  */
+  HOWTO (R_RELBYTE,            /* type */
         0,                     /* rightshift */                           
         0,                     /* size (0 = byte, 1 = short, 2 = long) */ 
         8,                     /* bitsize */                   
@@ -218,6 +227,7 @@ static reloc_howto_type howto_table[] =
         0x000000ff,            /* src_mask */                             
         0x000000ff,            /* dst_mask */                             
         PCRELOFFSET),          /* pcrel_offset */
+  /* 16-bit word relocation (020).  */
   HOWTO (R_RELWORD,            /* type */                                 
         0,                     /* rightshift */                           
         1,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -231,6 +241,7 @@ static reloc_howto_type howto_table[] =
         0x0000ffff,            /* src_mask */                             
         0x0000ffff,            /* dst_mask */                             
         PCRELOFFSET),          /* pcrel_offset */
+  /* 32-bit longword relocation (021).  */
   HOWTO (R_RELLONG,            /* type */                                 
         0,                     /* rightshift */                           
         2,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -244,6 +255,7 @@ static reloc_howto_type howto_table[] =
         0xffffffff,            /* src_mask */                             
         0xffffffff,            /* dst_mask */                             
         PCRELOFFSET),          /* pcrel_offset */
+  /* Byte PC relative relocation (022).  */
   HOWTO (R_PCRBYTE,            /* type */                                 
         0,                     /* rightshift */                           
         0,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -257,6 +269,7 @@ static reloc_howto_type howto_table[] =
         0x000000ff,            /* src_mask */                             
         0x000000ff,            /* dst_mask */                             
         PCRELOFFSET),          /* pcrel_offset */
+  /* 16-bit word PC relative relocation (023).  */
   HOWTO (R_PCRWORD,            /* type */                                 
         0,                     /* rightshift */                           
         1,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -270,6 +283,7 @@ static reloc_howto_type howto_table[] =
         0x0000ffff,            /* src_mask */                             
         0x0000ffff,            /* dst_mask */                             
         PCRELOFFSET),          /* pcrel_offset */
+  /* 32-bit longword PC relative relocation (024).  */
   HOWTO (R_PCRLONG,            /* type */                                 
         0,                     /* rightshift */                           
         2,                     /* size (0 = byte, 1 = short, 2 = long) */ 
@@ -291,8 +305,11 @@ static reloc_howto_type howto_table[] =
 #define BADMAG(x) I386BADMAG(x)
 #define I386 1                 /* Customize coffcode.h */
 
-#define RTYPE2HOWTO(cache_ptr, dst) \
-           (cache_ptr)->howto = howto_table + (dst)->r_type;
+#define RTYPE2HOWTO(cache_ptr, dst)                                    \
+  ((cache_ptr)->howto =                                                        \
+   ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0])     \
+    ? howto_table + (dst)->r_type                                      \
+    : NULL))
 
 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
    library.  On some other COFF targets STYP_BSS is normally
@@ -332,24 +349,72 @@ static reloc_howto_type howto_table[] =
       cache_ptr->addend += asect->vma;                         \
   }
 
-/* We use the special COFF backend linker.  */
+/* We use the special COFF backend linker.  For normal i386 COFF, we
+   can use the generic relocate_section routine.  For PE, we need our
+   own routine.  */
+
+#ifndef COFF_WITH_PE
+
 #define coff_relocate_section _bfd_coff_generic_relocate_section
 
+#else /* COFF_WITH_PE */
+
+/* The PE relocate section routine.  The only difference between this
+   and the regular routine is that we don't want to do anything for a
+   relocateable link.  */
+
+static boolean coff_pe_i386_relocate_section
+  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
+          struct internal_reloc *, struct internal_syment *, asection **));
+
+static boolean
+coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
+                              input_section, contents, relocs, syms,
+                              sections)
+     bfd *output_bfd;
+     struct bfd_link_info *info;
+     bfd *input_bfd;
+     asection *input_section;
+     bfd_byte *contents;
+     struct internal_reloc *relocs;
+     struct internal_syment *syms;
+     asection **sections;
+{
+  if (info->relocateable)
+    return true;
+
+  return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
+                                            input_section, contents,
+                                            relocs, syms, sections);
+}
+
+#define coff_relocate_section coff_pe_i386_relocate_section
+
+#endif /* COFF_WITH_PE */
+
+/* Convert an rtype to howto for the COFF backend linker.  */
+
 static reloc_howto_type *
 coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
-     bfd *abfd;
+     bfd *abfd ATTRIBUTE_UNUSED;
      asection *sec;
      struct internal_reloc *rel;
      struct coff_link_hash_entry *h;
      struct internal_syment *sym;
      bfd_vma *addendp;
 {
-
   reloc_howto_type *howto;
 
+  if (rel->r_type > sizeof (howto_table) / sizeof (howto_table[0]))
+    {
+      bfd_set_error (bfd_error_bad_value);
+      return NULL;
+    }
+
   howto = howto_table + rel->r_type;
 
 #ifdef COFF_WITH_PE
+  /* Cancel out code in _bfd_coff_generic_relocate_section.  */
   *addendp = 0;
 #endif
 
@@ -366,28 +431,40 @@ coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
  
       BFD_ASSERT (h != NULL);
 
-
 #ifndef COFF_WITH_PE
-      /* I think we *do* want to bypass this.  If we don't, I have seen some data
-        parameters get the wrong relcation address.  If I link two versions
-        with and without this section bypassed and then do a binary comparison,
-        the addresses which are different can be looked up in the map.  The 
-        case in which this section has been bypassed has addresses which correspond
-        to values I can find in the map */
+      /* I think we *do* want to bypass this.  If we don't, I have
+        seen some data parameters get the wrong relocation address.
+        If I link two versions with and without this section bypassed
+        and then do a binary comparison, the addresses which are
+        different can be looked up in the map.  The case in which
+        this section has been bypassed has addresses which correspond
+        to values I can find in the map.  */
       *addendp -= sym->n_value;
 #endif
     }
 
+#ifndef COFF_WITH_PE
   /* If the output symbol is common (in which case this must be a
      relocateable link), we need to add in the final size of the
      common symbol.  */
   if (h != NULL && h->root.type == bfd_link_hash_common) 
     *addendp += h->root.u.c.size;
-
+#endif
 
 #ifdef COFF_WITH_PE
   if (howto->pc_relative)
-    *addendp -= 4;
+    {
+      *addendp -= 4;
+
+      /* If the symbol is defined, then the generic code is going to
+         add back the symbol value in order to cancel out an
+         adjustment it made to the addend.  However, we set the addend
+         to 0 at the start of this function.  We need to adjust here,
+         to avoid the adjustment the generic code will make.  FIXME:
+         This is getting a bit hackish.  */
+      if (sym != NULL && sym->n_scnum != 0)
+       *addendp -= sym->n_value;
+    }
 
   if (rel->r_type == R_IMAGEBASE)
     {
@@ -398,13 +475,11 @@ coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
   return howto;
 }
 
-
 #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
 
-
 static reloc_howto_type *
 coff_i386_reloc_type_lookup (abfd, code)
-     bfd *abfd;
+     bfd *abfd ATTRIBUTE_UNUSED;
      bfd_reloc_code_real_type code;
 {
   switch (code)
@@ -573,5 +648,7 @@ const bfd_target
      BFD_JUMP_TABLE_LINK (coff),
      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
 
-  COFF_SWAP_TABLE,
+  NULL,
+  
+  COFF_SWAP_TABLE
 };
This page took 0.02703 seconds and 4 git commands to generate.