]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/coff-i386.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / coff-i386.c
index c6e085f9b63e710af157177dda2180656d17c246..24a05d3aa65b5a79eb38f0cea24908ca6234a267 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for Intel 386 COFF files.
-   Copyright (C) 1990-2021 Free Software Foundation, Inc.
+   Copyright (C) 1990-2023 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -109,14 +109,14 @@ coff_i386_reloc (bfd *abfd,
          reloc_howto_type *howto = reloc_entry->howto;
 
          /* Although PC relative relocations are very similar between
-            PE and non-PE formats, but they are off by 1 << howto->size
+            PE and non-PE formats, but they are off by howto->size
             bytes. For the external relocation, PE is very different
             from others. See md_apply_fix3 () in gas/config/tc-i386.c.
             When we link PE and non-PE object files together to
             generate a non-PE executable, we have to compensate it
             here.  */
          if (howto->pc_relative && howto->pcrel_offset)
-           diff = -(1 << howto->size);
+           diff = -bfd_get_reloc_size (howto);
          else if (symbol->flags & BSF_WEAK)
            diff = reloc_entry->addend - symbol->value;
          else
@@ -148,9 +148,9 @@ coff_i386_reloc (bfd *abfd,
       if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
        return bfd_reloc_outofrange;
 
-      switch (howto->size)
+      switch (bfd_get_reloc_size (howto))
        {
-       case 0:
+       case 1:
          {
            char x = bfd_get_8 (abfd, addr);
            DOIT (x);
@@ -158,7 +158,7 @@ coff_i386_reloc (bfd *abfd,
          }
          break;
 
-       case 1:
+       case 2:
          {
            short x = bfd_get_16 (abfd, addr);
            DOIT (x);
@@ -166,7 +166,7 @@ coff_i386_reloc (bfd *abfd,
          }
          break;
 
-       case 2:
+       case 4:
          {
            long x = bfd_get_32 (abfd, addr);
            DOIT (x);
@@ -190,8 +190,10 @@ coff_i386_reloc (bfd *abfd,
 static bool
 in_reloc_p (bfd *abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto)
 {
-  return ! howto->pc_relative && howto->type != R_IMAGEBASE
-        && howto->type != R_SECREL32;
+  return ! howto->pc_relative
+    && howto->type != R_IMAGEBASE
+    && howto->type != R_SECREL32
+    && howto->type != R_SECTION;
 }
 #endif /* COFF_WITH_PE */
 
@@ -209,7 +211,7 @@ static reloc_howto_type howto_table[] =
   EMPTY_HOWTO (5),
   HOWTO (R_DIR32,              /* type */
         0,                     /* rightshift */
-        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        4,                     /* size */
         32,                    /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -223,7 +225,7 @@ static reloc_howto_type howto_table[] =
   /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
   HOWTO (R_IMAGEBASE,          /* type */
         0,                     /* rightshift */
-        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        4,                     /* size */
         32,                    /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -236,12 +238,25 @@ static reloc_howto_type howto_table[] =
         false),                /* pcrel_offset */
   EMPTY_HOWTO (010),
   EMPTY_HOWTO (011),
-  EMPTY_HOWTO (012),
 #ifdef COFF_WITH_PE
+  /* 16-bit word section relocation (012).  */
+  HOWTO (R_SECTION,            /* type */
+        0,                     /* rightshift */
+        2,                     /* size */
+        16,                    /* bitsize */
+        false,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_bitfield, /* complain_on_overflow */
+        coff_i386_reloc,       /* special_function */
+        "secidx",              /* name */
+        true,                  /* partial_inplace */
+        0xffffffff,            /* src_mask */
+        0xffffffff,            /* dst_mask */
+        true),                 /* pcrel_offset */
   /* 32-bit longword section relative relocation (013).  */
   HOWTO (R_SECREL32,           /* type */
         0,                     /* rightshift */
-        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        4,                     /* size */
         32,                    /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -253,6 +268,7 @@ static reloc_howto_type howto_table[] =
         0xffffffff,            /* dst_mask */
         true),                 /* pcrel_offset */
 #else
+  EMPTY_HOWTO (012),
   EMPTY_HOWTO (013),
 #endif
   EMPTY_HOWTO (014),
@@ -261,7 +277,7 @@ static reloc_howto_type howto_table[] =
   /* Byte relocation (017).  */
   HOWTO (R_RELBYTE,            /* type */
         0,                     /* rightshift */
-        0,                     /* size (0 = byte, 1 = short, 2 = long) */
+        1,                     /* size */
         8,                     /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -275,7 +291,7 @@ static reloc_howto_type howto_table[] =
   /* 16-bit word relocation (020).  */
   HOWTO (R_RELWORD,            /* type */
         0,                     /* rightshift */
-        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        2,                     /* size */
         16,                    /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -289,7 +305,7 @@ static reloc_howto_type howto_table[] =
   /* 32-bit longword relocation (021). */
   HOWTO (R_RELLONG,            /* type */
         0,                     /* rightshift */
-        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        4,                     /* size */
         32,                    /* bitsize */
         false,                 /* pc_relative */
         0,                     /* bitpos */
@@ -303,7 +319,7 @@ static reloc_howto_type howto_table[] =
   /* Byte PC relative relocation (022).         */
   HOWTO (R_PCRBYTE,            /* type */
         0,                     /* rightshift */
-        0,                     /* size (0 = byte, 1 = short, 2 = long) */
+        1,                     /* size */
         8,                     /* bitsize */
         true,                  /* pc_relative */
         0,                     /* bitpos */
@@ -317,7 +333,7 @@ static reloc_howto_type howto_table[] =
   /* 16-bit word PC relative relocation (023). */
   HOWTO (R_PCRWORD,            /* type */
         0,                     /* rightshift */
-        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        2,                     /* size */
         16,                    /* bitsize */
         true,                  /* pc_relative */
         0,                     /* bitpos */
@@ -331,7 +347,7 @@ static reloc_howto_type howto_table[] =
   /* 32-bit longword PC relative relocation (024).  */
   HOWTO (R_PCRLONG,            /* type */
         0,                     /* rightshift */
-        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        4,                     /* size */
         32,                    /* bitsize */
         true,                  /* pc_relative */
         0,                     /* bitpos */
@@ -407,9 +423,9 @@ static reloc_howto_type howto_table[] =
 
 #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
-   relocatable link.  */
+/* The PE relocate section routine.  We handle secidx relocations here,
+   as well as making sure that we don't do anything for a relocatable
+   link.  */
 
 static bool
 coff_pe_i386_relocate_section (bfd *output_bfd,
@@ -421,9 +437,78 @@ coff_pe_i386_relocate_section (bfd *output_bfd,
                               struct internal_syment *syms,
                               asection **sections)
 {
+  struct internal_reloc *rel;
+  struct internal_reloc *relend;
+
   if (bfd_link_relocatable (info))
     return true;
 
+  rel = relocs;
+  relend = rel + input_section->reloc_count;
+
+  for (; rel < relend; rel++)
+    {
+      long symndx;
+      struct coff_link_hash_entry *h;
+      asection *sec, *s;
+      uint16_t idx = 0, i = 1;
+
+      if (rel->r_type != R_SECTION)
+       continue;
+
+      /* Make sure that _bfd_coff_generic_relocate_section won't parse
+         this reloc after us.  */
+      rel->r_type = 0;
+
+      symndx = rel->r_symndx;
+
+      if (symndx < 0
+         || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
+       continue;
+
+      h = obj_coff_sym_hashes (input_bfd)[symndx];
+
+      if (h == NULL)
+       sec = sections[symndx];
+      else
+       {
+         if (h->root.type == bfd_link_hash_defined
+             || h->root.type == bfd_link_hash_defweak)
+           {
+             /* Defined weak symbols are a GNU extension.  */
+             sec = h->root.u.def.section;
+           }
+         else
+           {
+             sec = NULL;
+           }
+       }
+
+      if (!sec)
+       continue;
+
+      if (bfd_is_abs_section (sec))
+       continue;
+
+      if (discarded_section (sec))
+       continue;
+
+      s = output_bfd->sections;
+      while (s)
+       {
+         if (s == sec->output_section)
+           {
+             idx = i;
+             break;
+           }
+
+         i++;
+         s = s->next;
+       }
+
+      bfd_putl16 (idx, contents + rel->r_vaddr - input_section->vma);
+    }
+
   return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
                                             input_section, contents,
                                             relocs, syms, sections);
@@ -573,6 +658,8 @@ coff_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 #ifdef COFF_WITH_PE
     case BFD_RELOC_32_SECREL:
       return howto_table + R_SECREL32;
+    case BFD_RELOC_16_SECIDX:
+      return howto_table + R_SECTION;
 #endif
     default:
       BFD_FAIL ();