aboutsummaryrefslogtreecommitdiff
path: root/gas
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 /gas
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 'gas')
-rw-r--r--gas/ChangeLog14
-rw-r--r--gas/config/tc-i386.c55
-rw-r--r--gas/testsuite/gas/i386/code16-2.d8
-rw-r--r--gas/testsuite/gas/i386/code16-2.s10
-rw-r--r--gas/testsuite/gas/i386/i386.exp2
-rw-r--r--gas/testsuite/gas/i386/x86-64-code16-2.d9
6 files changed, 80 insertions, 18 deletions
diff --git a/gas/ChangeLog b/gas/ChangeLog
index b550ba1..31f9e36 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,17 @@
+2021-05-26 H.J. Lu <hongjiu.lu@intel.com>
+
+ 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.
+
2021-05-26 Sebastien Villemot <sebastien@debian.org>
PR gas/27906
diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index d344198..c17f4da 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -2695,6 +2695,10 @@ 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
@@ -2706,6 +2710,10 @@ 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
@@ -9032,7 +9040,7 @@ x86_cleanup (void)
unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
unsigned int padding;
- if (!IS_ELF || !x86_used_note)
+ if (!IS_ELF || (!x86_used_note && !x86_feature_2_used))
return;
x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
@@ -9072,15 +9080,23 @@ x86_cleanup (void)
bfd_set_section_alignment (sec, alignment);
elf_section_type (sec) = SHT_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;
+ 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;
+ }
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)
@@ -9102,20 +9118,23 @@ x86_cleanup (void)
/* Write n_name. */
memcpy (p + 4 * 3, "GNU", 4);
- /* Write 4-byte type. */
- md_number_to_chars (p + 4 * 4,
- (valueT) GNU_PROPERTY_X86_ISA_1_USED, 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 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,
diff --git a/gas/testsuite/gas/i386/code16-2.d b/gas/testsuite/gas/i386/code16-2.d
new file mode 100644
index 0000000..37b66c8
--- /dev/null
+++ b/gas/testsuite/gas/i386/code16-2.d
@@ -0,0 +1,8 @@
+#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
diff --git a/gas/testsuite/gas/i386/code16-2.s b/gas/testsuite/gas/i386/code16-2.s
new file mode 100644
index 0000000..66e5d55
--- /dev/null
+++ b/gas/testsuite/gas/i386/code16-2.s
@@ -0,0 +1,10 @@
+ .code16gcc
+ .text
+ .section .text.default_process_op.isra.0,"ax",@progbits
+ .type default_process_op.isra.0, @function
+default_process_op.isra.0:
+ ret
+ .section .text.mpt_scsi_process_op,"ax",@progbits
+ .type mpt_scsi_process_op, @function
+mpt_scsi_process_op:
+ jmp default_process_op.isra.0
diff --git a/gas/testsuite/gas/i386/i386.exp b/gas/testsuite/gas/i386/i386.exp
index 39010bd..a459c6f 100644
--- a/gas/testsuite/gas/i386/i386.exp
+++ b/gas/testsuite/gas/i386/i386.exp
@@ -734,6 +734,7 @@ if {[is_elf_format] || [istarget "*-*-vxworks*"]} then {
run_dump_test "property-ldmxcsr"
run_dump_test "property-vldmxcsr"
run_dump_test "property-vzeroall"
+ run_dump_test "code16-2"
if {![istarget "*-*-dragonfly*"]
&& ![istarget "*-*-gnu*"]
@@ -1298,6 +1299,7 @@ if [gas_64_check] then {
run_dump_test "x86-64-property-8"
run_dump_test "x86-64-property-9"
run_dump_test "x86-64-property-14"
+ run_dump_test "x86-64-code16-2"
if {[istarget "*-*-linux*"]} then {
run_dump_test "x86-64-align-branch-3"
diff --git a/gas/testsuite/gas/i386/x86-64-code16-2.d b/gas/testsuite/gas/i386/x86-64-code16-2.d
new file mode 100644
index 0000000..dbabd67
--- /dev/null
+++ b/gas/testsuite/gas/i386/x86-64-code16-2.d
@@ -0,0 +1,9 @@
+#source: code16-2.s
+#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