aboutsummaryrefslogtreecommitdiff
path: root/bfd
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2021-05-26 12:13:13 -0700
committerH.J. Lu <hjl.tools@gmail.com>2021-05-26 12:13:24 -0700
commit50c95a739c91ae70cf8481936611aa1f5397a384 (patch)
treef539432154713f55c321e9dbd16251eb4f51eec7 /bfd
parent11bb5c41eb98d8e7d4d75dfcf620f6f627523e77 (diff)
downloadfsf-binutils-gdb-50c95a739c91ae70cf8481936611aa1f5397a384.zip
fsf-binutils-gdb-50c95a739c91ae70cf8481936611aa1f5397a384.tar.gz
fsf-binutils-gdb-50c95a739c91ae70cf8481936611aa1f5397a384.tar.bz2
x86: Propery check PC16 reloc overflow in 16-bit mode instructions
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 caused linker failure when building 16-bit program in a 32-bit ELF container. Update GNU_PROPERTY_X86_FEATURE_2_USED with #define GNU_PROPERTY_X86_FEATURE_2_CODE16 (1U << 12) to indicate that 16-bit mode instructions are used in the input object: https://groups.google.com/g/x86-64-abi/c/UvvXWeHIGMA to indicate that 16-bit mode instructions are used in the object to allow linker to properly perform relocation overflow check for 16-bit PC-relative relocations in 16-bit mode instructions. 1. Update x86 assembler to always generate the GNU property note with GNU_PROPERTY_X86_FEATURE_2_CODE16 for .code16 in ELF object. 2. Update i386 and x86-64 linkers to use 16-bit PC16 relocations if input object is marked with GNU_PROPERTY_X86_FEATURE_2_CODE16. bfd/ PR ld/27905 * elf32-i386.c: Include "libiberty.h". (elf_howto_table): Add 16-bit R_386_PC16 entry. (elf_i386_rtype_to_howto): Add a BFD argument. Use 16-bit R_386_PC16 if input has 16-bit mode instructions. (elf_i386_info_to_howto_rel): Update elf_i386_rtype_to_howto call. (elf_i386_tls_transition): Likewise. (elf_i386_relocate_section): Likewise. * elf64-x86-64.c (x86_64_elf_howto_table): Add 16-bit R_X86_64_PC16 entry. (elf_x86_64_rtype_to_howto): Use 16-bit R_X86_64_PC16 if input has 16-bit mode instructions. * elfxx-x86.c (_bfd_x86_elf_parse_gnu_properties): Set elf_x86_has_code16 if relocatable input is marked with GNU_PROPERTY_X86_FEATURE_2_CODE16. * elfxx-x86.h (elf_x86_obj_tdata): Add has_code16. (elf_x86_has_code16): New. binutils/ PR ld/27905 * readelf.c (decode_x86_feature_2): Support GNU_PROPERTY_X86_FEATURE_2_CODE16. gas/ PR ld/27905 * config/tc-i386.c (set_code_flag): Update x86_feature_2_used with GNU_PROPERTY_X86_FEATURE_2_CODE16 for .code16 in ELF object. (set_16bit_gcc_code_flag): Likewise. (x86_cleanup): Always generate the GNU property note if x86_feature_2_used isn't 0. * testsuite/gas/i386/code16-2.d: New file. * testsuite/gas/i386/code16-2.s: Likewise. * testsuite/gas/i386/x86-64-code16-2.d: Likewise. * testsuite/gas/i386/i386.exp: Run code16-2 and x86-64-code16-2. include/ PR ld/27905 * elf/common.h (GNU_PROPERTY_X86_FEATURE_2_CODE16): New. ld/ PR ld/27905 * testsuite/ld-i386/code16.d: New file. * testsuite/ld-i386/code16.t: Likewise. * testsuite/ld-x86-64/code16.d: Likewise. * testsuite/ld-x86-64/code16.t: Likewise. * testsuite/ld-i386/i386.exp: Run code16. * testsuite/ld-x86-64/x86-64.exp: Likewise.
Diffstat (limited to 'bfd')
-rw-r--r--bfd/ChangeLog21
-rw-r--r--bfd/elf32-i386.c23
-rw-r--r--bfd/elf64-x86-64.c12
-rw-r--r--bfd/elfxx-x86.c9
-rw-r--r--bfd/elfxx-x86.h6
5 files changed, 64 insertions, 7 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 608ea54..82005a4 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,24 @@
+2021-05-26 H.J. Lu <hongjiu.lu@intel.com>
+
+ PR ld/27905
+ * elf32-i386.c: Include "libiberty.h".
+ (elf_howto_table): Add 16-bit R_386_PC16 entry.
+ (elf_i386_rtype_to_howto): Add a BFD argument. Use 16-bit
+ R_386_PC16 if input has 16-bit mode instructions.
+ (elf_i386_info_to_howto_rel): Update elf_i386_rtype_to_howto
+ call.
+ (elf_i386_tls_transition): Likewise.
+ (elf_i386_relocate_section): Likewise.
+ * elf64-x86-64.c (x86_64_elf_howto_table): Add 16-bit
+ R_X86_64_PC16 entry.
+ (elf_x86_64_rtype_to_howto): Use 16-bit R_X86_64_PC16 if input
+ has 16-bit mode instructions.
+ * elfxx-x86.c (_bfd_x86_elf_parse_gnu_properties): Set
+ elf_x86_has_code16 if relocatable input is marked with
+ GNU_PROPERTY_X86_FEATURE_2_CODE16.
+ * elfxx-x86.h (elf_x86_obj_tdata): Add has_code16.
+ (elf_x86_has_code16): New.
+
2021-05-26 Nelson Chu <nelson.chu@sifive.com>
* elfxx-riscv.h (check_unknown_prefixed_ext): New bool.
diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 4451f9b..c68741a 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -22,6 +22,7 @@
#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
@@ -175,10 +176,14 @@ 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)
@@ -369,7 +374,7 @@ elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
}
static reloc_howto_type *
-elf_i386_rtype_to_howto (unsigned r_type)
+elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
{
unsigned int indx;
@@ -384,6 +389,11 @@ elf_i386_rtype_to_howto (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];
}
@@ -394,7 +404,8 @@ 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 (r_type)) == NULL)
+ if ((cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type))
+ == NULL)
{
/* xgettext:c-format */
_bfd_error_handler (_("%pB: unsupported relocation type %#x"),
@@ -1142,8 +1153,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 (from_type);
- to = elf_i386_rtype_to_howto (to_type);
+ from = elf_i386_rtype_to_howto (abfd, from_type);
+ to = elf_i386_rtype_to_howto (abfd, to_type);
if (h)
name = h->root.root.string;
@@ -2074,7 +2085,7 @@ elf_i386_relocate_section (bfd *output_bfd,
continue;
}
- howto = elf_i386_rtype_to_howto (r_type);
+ howto = elf_i386_rtype_to_howto (input_bfd, r_type);
if (howto == NULL)
return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index d0c994e..d420561 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -187,6 +187,10 @@ 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,
@@ -270,6 +274,14 @@ 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)
{
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index 62d516a..29dc7f0 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -2353,6 +2353,7 @@ _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
@@ -2361,7 +2362,13 @@ _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
return property_corrupt;
}
prop = _bfd_elf_get_property (abfd, type, datasz);
- prop->u.number |= bfd_h_get_32 (abfd, ptr);
+ 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->pr_kind = property_number;
return property_number;
}
diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h
index db11327..e834430 100644
--- a/bfd/elfxx-x86.h
+++ b/bfd/elfxx-x86.h
@@ -549,6 +549,9 @@ 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
@@ -584,6 +587,9 @@ 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)