]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
x86: Restore PC16 relocation overflow check
authorH.J. Lu <hjl.tools@gmail.com>
Fri, 28 May 2021 16:34:10 +0000 (09:34 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 28 May 2021 16:34:28 +0000 (09:34 -0700)
The x86-64 psABI has

---
A program or object file using R_X86_64_8, R_X86_64_16, R_X86_64_PC16
or R_X86_64_PC8 relocations is not conformant to this ABI, these
relocations are only added for documentation purposes.
---

Since x86 PC16 relocations have been used for 16-bit programs in an ELF32
or ELF64 container, PC16 relocation should wrap-around in 16-bit address
space.  Revert

commit a7664973b24a242cd9ea17deb5eaf503065fc0bd
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Apr 26 10:41:35 2021 +0200

    x86: correct overflow checking for 16-bit PC-relative relocs

and xfail the related tests.  Also revert

commit 50c95a739c91ae70cf8481936611aa1f5397a384
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed May 26 12:13:13 2021 -0700

    x86: Propery check PC16 reloc overflow in 16-bit mode instructions

while keeping PR ld/27905 tests for PC16 relocation in 16-bit programs.

bfd/

PR ld/27905
* elf32-i386.c: Don't include "libiberty.h".
(elf_howto_table): Revert commits a7664973b24 and 50c95a739c9.
(elf_i386_rtype_to_howto): Revert commit 50c95a739c9.
(elf_i386_info_to_howto_rel): Likewise.
(elf_i386_tls_transition): Likewise.
(elf_i386_relocate_section): Likewise.
* elf64-x86-64.c (x86_64_elf_howto_table): Revert commits
a7664973b24 and 50c95a739c9.
(elf_x86_64_rtype_to_howto): Revert commit 50c95a739c9.
* elfxx-x86.c (_bfd_x86_elf_parse_gnu_properties): Likewise.
* elfxx-x86.h (elf_x86_obj_tdata): Likewise.
(elf_x86_has_code16): Likewise.

binutils/

PR ld/27905
* readelf.c (decode_x86_feature_2): Revert commit 50c95a739c9.

gas/

PR ld/27905
* config/tc-i386.c (set_code_flag): Revert commit 50c95a739c9.
(set_16bit_gcc_code_flag): Likewise.
(x86_cleanup): Likewise.
* testsuite/gas/i386/code16-2.d: Updated.
* testsuite/gas/i386/x86-64-code16-2.d: Likewise.

include/

PR ld/27905
* elf/common.h (GNU_PROPERTY_X86_FEATURE_2_CODE16): Removed.

ld/

PR ld/27905
* testsuite/ld-i386/pcrel16-2.d: xfail.
* testsuite/ld-x86-64/pcrel16-2.d: Likewise.

16 files changed:
bfd/ChangeLog
bfd/elf32-i386.c
bfd/elf64-x86-64.c
bfd/elfxx-x86.c
bfd/elfxx-x86.h
binutils/ChangeLog
binutils/readelf.c
gas/ChangeLog
gas/config/tc-i386.c
gas/testsuite/gas/i386/code16-2.d
gas/testsuite/gas/i386/x86-64-code16-2.d
include/ChangeLog
include/elf/common.h
ld/ChangeLog
ld/testsuite/ld-i386/pcrel16-2.d
ld/testsuite/ld-x86-64/pcrel16-2.d

index 82005a414218cf733f6713f8b9277c3912d20bb4..6242f3c76c7e0d72eb9f8846ae3fcf28299a2ede 100644 (file)
@@ -1,3 +1,19 @@
+2021-05-28  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27905
+       * elf32-i386.c: Don't include "libiberty.h".
+       (elf_howto_table): Revert commits a7664973b24 and 50c95a739c9.
+       (elf_i386_rtype_to_howto): Revert commit 50c95a739c9.
+       (elf_i386_info_to_howto_rel): Likewise.
+       (elf_i386_tls_transition): Likewise.
+       (elf_i386_relocate_section): Likewise.
+       * elf64-x86-64.c (x86_64_elf_howto_table): Revert commits
+       a7664973b24 and 50c95a739c9.
+       (elf_x86_64_rtype_to_howto): Revert commit 50c95a739c9.
+       * elfxx-x86.c (_bfd_x86_elf_parse_gnu_properties): Likewise.
+       * elfxx-x86.h (elf_x86_obj_tdata): Likewise.
+       (elf_x86_has_code16): Likewise.
+
 2021-05-26  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/27905
index c68741af02cb6b087cf5ffc962da20b3fbdc3aea..cf7cd076b1728fc8458e88ad16b23a11b7b19270 100644 (file)
@@ -22,7 +22,6 @@
 #include "elf-vxworks.h"
 #include "dwarf2.h"
 #include "opcode/i386.h"
-#include "libiberty.h"
 
 /* 386 uses REL relocations instead of RELA.  */
 #define USE_REL        1
@@ -94,7 +93,7 @@ static reloc_howto_type elf_howto_table[]=
   HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_386_16",
        true, 0xffff, 0xffff, false),
-  HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_signed,
+  HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_386_PC16",
        true, 0xffff, 0xffff, true),
   HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
@@ -176,14 +175,10 @@ static reloc_howto_type elf_howto_table[]=
         false,                 /* partial_inplace */
         0,                     /* src_mask */
         0,                     /* dst_mask */
-        false),                /* pcrel_offset */
+        false)                 /* pcrel_offset */
 
 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
 
-/* Use complain_overflow_bitfield on R_386_PC16 for code16.  */
-  HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
-       bfd_elf_generic_reloc, "R_386_PC16",
-       true, 0xffff, 0xffff, true)
 };
 
 #define X86_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
@@ -374,7 +369,7 @@ elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 }
 
 static reloc_howto_type *
-elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
+elf_i386_rtype_to_howto (unsigned r_type)
 {
   unsigned int indx;
 
@@ -389,11 +384,6 @@ elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
   /* PR 17512: file: 0f67f69d.  */
   if (elf_howto_table [indx].type != r_type)
     return NULL;
-
-  /* Use complain_overflow_bitfield on R_386_PC16 for code16.  */
-  if (r_type == (unsigned int) R_386_PC16 && elf_x86_has_code16 (abfd))
-    indx = ARRAY_SIZE (elf_howto_table) - 1;
-
   return &elf_howto_table[indx];
 }
 
@@ -404,8 +394,7 @@ elf_i386_info_to_howto_rel (bfd *abfd,
 {
   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
 
-  if ((cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type))
-      == NULL)
+  if ((cache_ptr->howto = elf_i386_rtype_to_howto (r_type)) == NULL)
     {
       /* xgettext:c-format */
       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
@@ -1153,8 +1142,8 @@ elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
       reloc_howto_type *from, *to;
       const char *name;
 
-      from = elf_i386_rtype_to_howto (abfd, from_type);
-      to = elf_i386_rtype_to_howto (abfd, to_type);
+      from = elf_i386_rtype_to_howto (from_type);
+      to = elf_i386_rtype_to_howto (to_type);
 
       if (h)
        name = h->root.root.string;
@@ -2085,7 +2074,7 @@ elf_i386_relocate_section (bfd *output_bfd,
          continue;
        }
 
-      howto = elf_i386_rtype_to_howto (input_bfd, r_type);
+      howto = elf_i386_rtype_to_howto (r_type);
       if (howto == NULL)
        return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
 
index d420561c156a33250c717f136c52678054ada2fb..98fb88113c0d257056c08caa7b4e3797815bb1d8 100644 (file)
@@ -82,7 +82,7 @@ static reloc_howto_type x86_64_elf_howto_table[] =
        false),
   HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
-  HOWTO(R_X86_64_PC16, 0, 1, 16, true, 0, complain_overflow_signed,
+  HOWTO(R_X86_64_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
   HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
@@ -187,10 +187,6 @@ static reloc_howto_type x86_64_elf_howto_table[] =
         _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
         false),
 
-/* Use complain_overflow_bitfield on R_X86_64_PC16 for code16.  */
-  HOWTO(R_X86_64_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
-       bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
-
 /* Use complain_overflow_bitfield on R_X86_64_32 for x32.  */
   HOWTO(R_X86_64_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
        bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
@@ -274,14 +270,6 @@ elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
       else
        i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
     }
-  else if (r_type == (unsigned int) R_X86_64_PC16)
-    {
-      /* Use complain_overflow_bitfield on R_X86_64_PC16 for code16.  */
-      if (elf_x86_has_code16 (abfd))
-       i = ARRAY_SIZE (x86_64_elf_howto_table) - 2;
-      else
-       i = r_type;
-    }
   else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
           || r_type >= (unsigned int) R_X86_64_max)
     {
index 29dc7f04b4d8a6128420567411c0c6bb4dcd0ad1..62d516aab8d3bba2aec7f436c99705ea6d4ea740 100644 (file)
@@ -2353,7 +2353,6 @@ _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
       || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
          && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
     {
-      unsigned int number;
       if (datasz != 4)
        {
          _bfd_error_handler
@@ -2362,13 +2361,7 @@ _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
          return property_corrupt;
        }
       prop = _bfd_elf_get_property (abfd, type, datasz);
-      number = bfd_h_get_32 (abfd, ptr);
-      if ((abfd->flags
-          & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
-         && type == GNU_PROPERTY_X86_FEATURE_2_USED
-         && (number & GNU_PROPERTY_X86_FEATURE_2_CODE16) != 0)
-       elf_x86_has_code16 (abfd) = 1;
-      prop->u.number |= number;
+      prop->u.number |= bfd_h_get_32 (abfd, ptr);
       prop->pr_kind = property_number;
       return property_number;
     }
index e834430549223663606c342fbd01e499fa5962a6..db11327e96f403bed1d4f582117fc04493c9735d 100644 (file)
@@ -549,9 +549,6 @@ struct elf_x86_obj_tdata
 
   /* GOTPLT entries for TLS descriptors.  */
   bfd_vma *local_tlsdesc_gotent;
-
-  /* Set if the objec file has 16-bit code.  */
-  unsigned int has_code16 : 1;
 };
 
 enum elf_x86_plt_type
@@ -587,9 +584,6 @@ struct elf_x86_plt
 #define elf_x86_local_tlsdesc_gotent(abfd) \
   (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
 
-#define elf_x86_has_code16(abfd) \
-  (elf_x86_tdata (abfd)->has_code16)
-
 #define elf_x86_compute_jump_table_size(htab) \
   ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
 
index 7e69f9d90f52c7619188ae6cf2ba20f7146ad1b6..384c12eca17ce60c49de65ac8ec33fe1102ea47e 100644 (file)
@@ -1,3 +1,8 @@
+2021-05-28  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27905
+       * readelf.c (decode_x86_feature_2): Revert commit 50c95a739c9.
+
 2021-05-27  Alan Modra  <amodra@gmail.com>
 
        * readelf (usage): Order -w letters to match --debug-dump= and
index a6ed24c03bd0f03b2eefc156136706e19b7a721b..d773b9a49314abbccaee07b071ada2bbba6003b1 100644 (file)
@@ -19139,9 +19139,6 @@ decode_x86_feature_2 (unsigned int bitmask)
        case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
          printf ("XSAVEC");
          break;
-       case GNU_PROPERTY_X86_FEATURE_2_CODE16:
-         printf ("CODE16");
-         break;
        default:
          printf (_("<unknown: %x>"), bit);
          break;
index 3e2120dcb93c55e9889e6280c230513c142f34ad..49d2ceb058b5a3baf7d347abcda8b7d85df01109 100644 (file)
@@ -1,3 +1,12 @@
+2021-05-28  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27905
+       * config/tc-i386.c (set_code_flag): Revert commit 50c95a739c9.
+       (set_16bit_gcc_code_flag): Likewise.
+       (x86_cleanup): Likewise.
+       * testsuite/gas/i386/code16-2.d: Updated.
+       * testsuite/gas/i386/x86-64-code16-2.d: Likewise.
+
 2021-05-27  Peter Bergner  <bergner@linux.ibm.com>
 
        * testsuite/gas/ppc/vsx.d <xxmr, xxlnot>: Add tests.
index c17f4da16fe32c1d53b7334110822bb6a1a614e5..d3441988e3475e7690658b36e9bbdb01cea0abcf 100644 (file)
@@ -2695,10 +2695,6 @@ static void
 set_code_flag (int value)
 {
   update_code_flag (value, 0);
-#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
-  if (value == CODE_16BIT)
-    x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_CODE16;
-#endif
 }
 
 static void
@@ -2710,10 +2706,6 @@ set_16bit_gcc_code_flag (int new_code_flag)
   cpu_arch_flags.bitfield.cpu64 = 0;
   cpu_arch_flags.bitfield.cpuno64 = 1;
   stackop_size = LONG_MNEM_SUFFIX;
-#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
-  if (new_code_flag == CODE_16BIT)
-    x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_CODE16;
-#endif
 }
 
 static void
@@ -9040,7 +9032,7 @@ x86_cleanup (void)
   unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
   unsigned int padding;
 
-  if (!IS_ELF || (!x86_used_note && !x86_feature_2_used))
+  if (!IS_ELF || !x86_used_note)
     return;
 
   x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
@@ -9080,23 +9072,15 @@ x86_cleanup (void)
   bfd_set_section_alignment (sec, alignment);
   elf_section_type (sec) = SHT_NOTE;
 
-  if (x86_used_note)
-    {
-      /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
-        + 4-byte data  */
-      isa_1_descsz_raw = 4 + 4 + 4;
-      /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
-      isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
-    }
-  else
-    {
-      isa_1_descsz_raw = 0;
-      isa_1_descsz = 0;
-    }
+  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
+                                 + 4-byte data  */
+  isa_1_descsz_raw = 4 + 4 + 4;
+  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
+  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
 
   feature_2_descsz_raw = isa_1_descsz;
   /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
-     + 4-byte data  */
+                                     + 4-byte data  */
   feature_2_descsz_raw += 4 + 4 + 4;
   /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
   feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
@@ -9118,23 +9102,20 @@ x86_cleanup (void)
   /* Write n_name.  */
   memcpy (p + 4 * 3, "GNU", 4);
 
-  if (isa_1_descsz != 0)
-    {
-      /* Write 4-byte type.  */
-      md_number_to_chars (p + 4 * 4,
-                         (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
+  /* Write 4-byte type.  */
+  md_number_to_chars (p + 4 * 4,
+                     (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
 
-      /* Write 4-byte data size.  */
-      md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
+  /* Write 4-byte data size.  */
+  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
 
-      /* Write 4-byte data.  */
-      md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
+  /* Write 4-byte data.  */
+  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
 
-      /* Zero out paddings.  */
-      padding = isa_1_descsz - isa_1_descsz_raw;
-      if (padding)
-       memset (p + 4 * 7, 0, padding);
-    }
+  /* Zero out paddings.  */
+  padding = isa_1_descsz - isa_1_descsz_raw;
+  if (padding)
+    memset (p + 4 * 7, 0, padding);
 
   /* Write 4-byte type.  */
   md_number_to_chars (p + isa_1_descsz + 4 * 4,
index 37b66c85f4e379ed36abbfe15e56983f2f22c47f..f18c8cd62da1aac91ea3b8fd70a5a72e5cc73cd9 100644 (file)
@@ -1,8 +1,3 @@
 #name: i386 code16 2
 #as: -mx86-used-note=no --generate-missing-build-notes=no
 #readelf: -n
-
-Displaying notes found in: .note.gnu.property
-[      ]+Owner[        ]+Data size[    ]+Description
-  GNU                  0x[0-9a-f]+     NT_GNU_PROPERTY_TYPE_0
-      Properties: x86 feature used: x86, CODE16
index dbabd67e8880b6e112bc470e9e2d105a5230813d..5052353c00d88e69787fe98e7a2f262f7de7b54d 100644 (file)
@@ -2,8 +2,3 @@
 #name: x86-64 code16 2
 #as: -mx86-used-note=no --generate-missing-build-notes=no
 #readelf: -n
-
-Displaying notes found in: .note.gnu.property
-[      ]+Owner[        ]+Data size[    ]+Description
-  GNU                  0x[0-9a-f]+     NT_GNU_PROPERTY_TYPE_0
-      Properties: x86 feature used: x86, CODE16
index 355c391fe466bff6f6862f7a66288e36cced7622..19dc8e3db6f12821d3fce306b262b6ccec8b053c 100644 (file)
@@ -1,3 +1,8 @@
+2021-05-28  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27905
+       * elf/common.h (GNU_PROPERTY_X86_FEATURE_2_CODE16): Removed.
+
 2021-05-26  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/27905
index 564ab711a204816fce74dffd41d2297569424c54..24d0a09b7c8f74558aaabbce295e19792f264b6d 100644 (file)
 #define GNU_PROPERTY_X86_FEATURE_2_XSAVEC      (1U << 9)
 #define GNU_PROPERTY_X86_FEATURE_2_TMM         (1U << 10)
 #define GNU_PROPERTY_X86_FEATURE_2_MASK                (1U << 11)
-#define GNU_PROPERTY_X86_FEATURE_2_CODE16      (1U << 12)
 
 #define GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED \
   (GNU_PROPERTY_X86_UINT32_OR_LO + 0)
index 8d25dd87a1b334bd95aadaca13846260c922bf42..263dee0305994f029d47129fc12fe60b800d6078 100644 (file)
@@ -1,3 +1,9 @@
+2021-05-28  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/27905
+       * testsuite/ld-i386/pcrel16-2.d: xfail.
+       * testsuite/ld-x86-64/pcrel16-2.d: Likewise.
+
 2021-05-26  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/27905
index c1c340fc75cd5549c961ac2da822428a90b93594..de9c779e81025ee06bf38d33f8877c859847e8f0 100644 (file)
@@ -3,3 +3,4 @@
 #ld: -melf_i386
 #error: .*relocation truncated to fit: R_386_PC16 .*t16.*
 #error: .*relocation truncated to fit: R_386_PC16 .*_start.*
+#xfail: *-*-*
index 5346a5b619dd7d73bcbb4b797589ecaf245c6d69..991dcd09646b90ec02936ed91fb3d3df9d01c237 100644 (file)
@@ -3,3 +3,4 @@
 #ld:
 #error: .*relocation truncated to fit: R_X86_64_PC16 .*t16.*
 #error: .*relocation truncated to fit: R_X86_64_PC16 .*_start.*
+#xfail: *-*-*