2013-11-13 07:46:55 +08:00
|
|
|
# Expect script for ELF MPX tests.
|
2015-01-01 22:15:26 +08:00
|
|
|
# Copyright (C) 2013-2015 Free Software Foundation, Inc.
|
2013-11-13 07:46:55 +08:00
|
|
|
#
|
|
|
|
# This file is part of the GNU Binutils.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
# The following tests require running the executable generated by ld,
|
|
|
|
# or enough of a build environment to create a fully linked executable.
|
|
|
|
# This is not commonly available when testing a cross-built linker.
|
|
|
|
if ![isnative] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Only on Linux for now.
|
|
|
|
if ![istarget "x86_64-*-linux*"] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check to see if the C compiler works
|
|
|
|
if { [which $CC] == 0 } {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set build_tests {
|
|
|
|
{"Build libmpx1a.a"
|
|
|
|
"" "-Wa,-madd-bnd-prefix -fPIC"
|
|
|
|
{mpx1a.c} {{readelf {-r --wide} mpx1a.rd}} "libmpx1a.a"}
|
|
|
|
{"Build libmpx1b.a"
|
|
|
|
"" ""
|
|
|
|
{mpx1b.c} {} "libmpx1b.a"}
|
|
|
|
{"Build libmpx1c.a"
|
|
|
|
"" "-Wa,-madd-bnd-prefix"
|
|
|
|
{mpx1c.c} {{readelf {-r --wide} mpx1c.rd}} "libmpx1c.a"}
|
2013-11-21 04:35:10 +08:00
|
|
|
{"Build libmpx2a.a"
|
|
|
|
"" "-Wa,-madd-bnd-prefix -fPIE"
|
|
|
|
{mpx2a.c} {{readelf {-r --wide} mpx2a.rd}} "libmpx2a.a"}
|
|
|
|
{"Build libmpx2b.a"
|
|
|
|
"" "-fPIE"
|
|
|
|
{mpx2b.c} {} "libmpx2b.a"}
|
|
|
|
{"Build libmpx2c.a"
|
|
|
|
"" "-Wa,-madd-bnd-prefix -fPIE"
|
|
|
|
{mpx2c.c} {{readelf {-r --wide} mpx2c.rd}} "libmpx2c.a"}
|
2013-11-13 07:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
run_cc_link_tests $build_tests
|
|
|
|
|
|
|
|
set run_tests {
|
|
|
|
{"Run mpx1"
|
|
|
|
"tmpdir/mpx1a.o tmpdir/mpx1b.o tmpdir/mpx1c.o" ""
|
|
|
|
{dummy.s} "mpx1" "mpx1.out"}
|
2013-11-21 04:35:10 +08:00
|
|
|
{"Run mpx1 with -static"
|
|
|
|
"tmpdir/mpx1a.o tmpdir/mpx1b.o tmpdir/mpx1c.o -static" ""
|
|
|
|
{dummy.s} "mpx1static" "mpx1.out"}
|
|
|
|
{"Run mpx2"
|
|
|
|
"tmpdir/mpx2a.o tmpdir/mpx2b.o tmpdir/mpx2c.o -pie" ""
|
|
|
|
{dummy.s} "mpx2" "mpx2.out"}
|
|
|
|
{"Run mpx2 with -static"
|
|
|
|
"tmpdir/mpx2a.o tmpdir/mpx2b.o tmpdir/mpx2c.o -static" ""
|
|
|
|
{dummy.s} "mpx2static" "mpx2.out"}
|
2013-11-13 07:46:55 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 15:52:36 +08:00
|
|
|
run_ld_link_tests {
|
|
|
|
{"Build libcall.so"
|
2014-11-18 21:55:32 +08:00
|
|
|
"-m elf_x86_64 -shared -z bndplt" "" "--64"
|
2014-11-18 15:52:36 +08:00
|
|
|
{mpx3b.s} {} "libcall.so"}
|
|
|
|
{"Build mpx3"
|
2014-11-18 21:55:32 +08:00
|
|
|
"-m elf_x86_64 -z bndplt tmpdir/libcall.so" "" "--64"
|
2014-11-18 15:52:36 +08:00
|
|
|
{mpx3a.s} {{objdump -dw mpx3.dd}} "mpx3"}
|
|
|
|
{"Build libcall1.so"
|
2014-11-18 21:55:32 +08:00
|
|
|
"-m elf_x86_64 -shared -z bndplt" "" "--64"
|
2014-11-18 15:52:36 +08:00
|
|
|
{mpx4b.s} {} "libcall1.so"}
|
|
|
|
{"Build mpx4"
|
2014-11-18 21:55:32 +08:00
|
|
|
"-m elf_x86_64 -z bndplt tmpdir/libcall1.so" "" "--64"
|
2014-11-18 15:52:36 +08:00
|
|
|
{mpx4a.s} {{objdump -dw mpx4.dd}} "mpx4"}
|
|
|
|
}
|
|
|
|
|
2013-11-13 07:46:55 +08:00
|
|
|
run_ld_link_exec_tests [] $run_tests
|
2013-11-21 20:53:05 +08:00
|
|
|
|
|
|
|
run_dump_test "bnd-branch-1"
|
Create the second PLT for BND relocations
Intel MPX introduces 4 bound registers, which will be used for parameter
passing in x86-64. Bound registers are cleared by branch instructions.
Branch instructions with BND prefix will keep bound register contents.
This leads to 2 requirements to 64-bit MPX run-time:
1. Dynamic linker (ld.so) should save and restore bound registers during
symbol lookup.
2. Change the current 16-byte PLT0:
ff 35 08 00 00 00 pushq GOT+8(%rip)
ff 25 00 10 00 jmpq *GOT+16(%rip)
0f 1f 40 00 nopl 0x0(%rax)
and 16-byte PLT1:
ff 25 00 00 00 00 jmpq *name@GOTPCREL(%rip)
68 00 00 00 00 pushq $index
e9 00 00 00 00 jmpq PLT0
which clear bound registers, to preserve bound registers.
We use 2 new relocations:
to mark branch instructions with BND prefix.
When linker sees any R_X86_64_PC32_BND or R_X86_64_PLT32_BND relocations,
it switches to a different PLT0:
ff 35 08 00 00 00 pushq GOT+8(%rip)
f2 ff 25 00 10 00 bnd jmpq *GOT+16(%rip)
0f 1f 00 nopl (%rax)
to preserve bound registers for symbol lookup and it also creates an
external PLT section, .pl.bnd. Linker will create a BND PLT1 entry
in .plt:
68 00 00 00 00 pushq $index
f2 e9 00 00 00 00 bnd jmpq PLT0
0f 1f 44 00 00 nopl 0(%rax,%rax,1)
and a 8-byte BND PLT entry in .plt.bnd:
f2 ff 25 00 00 00 00 bnd jmpq *name@GOTPCREL(%rip)
90 nop
Otherwise, linker will create a legacy PLT1 entry in .plt:
68 00 00 00 00 pushq $index
e9 00 00 00 00 jmpq PLT0
66 0f 1f 44 00 00 nopw 0(%rax,%rax,1)
and a 8-byte legacy PLT in .plt.bnd:
ff 25 00 00 00 00 jmpq *name@GOTPCREL(%rip)
66 90 xchg %ax,%ax
The initial value of the GOT entry for "name" will be set to the the
"pushq" instruction in the corresponding entry in .plt. Linker will
resolve reference of symbol "name" to the entry in the second PLT,
.plt.bnd.
Prelink stores the offset of pushq of PLT1 (plt_base + 0x10) in GOT[1]
and GOT[1] is stored in GOT[3]. We can undo prelink in GOT by computing
the corresponding the pushq offset with
GOT[1] + (GOT offset - &GOT[3]) * 2
Since for each entry in .plt except for PLT0 we create a 8-byte entry in
.plt.bnd, there is extra 8-byte per PLT symbol.
We also investigated the 16-byte entry for .plt.bnd. We compared the
8-byte entry vs the the 16-byte entry for .plt.bnd on Sandy Bridge.
There are no performance differences in SPEC CPU 2000/2006 as well as
micro benchmarks.
Pros:
No change to undo prelink in dynamic linker.
Only 8-byte memory overhead for each PLT symbol.
Cons:
Extra .plt.bnd section is needed.
Extra 8 byte for legacy branches to PLT.
GDB is unware of the new layout of .plt and .plt.bnd.
bfd/
* elf64-x86-64.c (elf_x86_64_bnd_plt0_entry): New.
(elf_x86_64_legacy_plt_entry): Likewise.
(elf_x86_64_bnd_plt_entry): Likewise.
(elf_x86_64_legacy_plt2_entry): Likewise.
(elf_x86_64_bnd_plt2_entry): Likewise.
(elf_x86_64_bnd_arch_bed): Likewise.
(elf_x86_64_link_hash_entry): Add has_bnd_reloc and plt_bnd.
(elf_x86_64_link_hash_table): Add plt_bnd.
(elf_x86_64_link_hash_newfunc): Initialize has_bnd_reloc and
plt_bnd.
(elf_x86_64_copy_indirect_symbol): Also copy has_bnd_reloc.
(elf_x86_64_check_relocs): Create the second PLT for Intel MPX
in 64-bit mode.
(elf_x86_64_allocate_dynrelocs): Handle the second PLT for IFUNC
symbols. Resolve call to the second PLT if it is created.
(elf_x86_64_size_dynamic_sections): Keep the second PLT section.
(elf_x86_64_relocate_section): Resolve PLT references to the
second PLT if it is created.
(elf_x86_64_finish_dynamic_symbol): Use BND PLT0 and fill the
second PLT entry for BND relocation.
(elf_x86_64_finish_dynamic_sections): Use MPX backend data if
the second PLT is created.
(elf_x86_64_get_synthetic_symtab): New.
(bfd_elf64_get_synthetic_symtab): Likewise. Undefine for NaCl.
ld/
* emulparams/elf_x86_64.sh (TINY_READONLY_SECTION): New.
ld/testsuite/
* ld-x86-64/mpx.exp: Run bnd-ifunc-1 and bnd-plt-1.
* ld-x86-64/bnd-ifunc-1.d: New file.
* ld-x86-64/bnd-ifunc-1.s: Likewise.
* ld-x86-64/bnd-plt-1.d: Likewise.
2013-11-21 01:01:04 +08:00
|
|
|
run_dump_test "bnd-ifunc-1"
|
2014-07-16 23:16:24 +08:00
|
|
|
run_dump_test "bnd-ifunc-2"
|
Create the second PLT for BND relocations
Intel MPX introduces 4 bound registers, which will be used for parameter
passing in x86-64. Bound registers are cleared by branch instructions.
Branch instructions with BND prefix will keep bound register contents.
This leads to 2 requirements to 64-bit MPX run-time:
1. Dynamic linker (ld.so) should save and restore bound registers during
symbol lookup.
2. Change the current 16-byte PLT0:
ff 35 08 00 00 00 pushq GOT+8(%rip)
ff 25 00 10 00 jmpq *GOT+16(%rip)
0f 1f 40 00 nopl 0x0(%rax)
and 16-byte PLT1:
ff 25 00 00 00 00 jmpq *name@GOTPCREL(%rip)
68 00 00 00 00 pushq $index
e9 00 00 00 00 jmpq PLT0
which clear bound registers, to preserve bound registers.
We use 2 new relocations:
to mark branch instructions with BND prefix.
When linker sees any R_X86_64_PC32_BND or R_X86_64_PLT32_BND relocations,
it switches to a different PLT0:
ff 35 08 00 00 00 pushq GOT+8(%rip)
f2 ff 25 00 10 00 bnd jmpq *GOT+16(%rip)
0f 1f 00 nopl (%rax)
to preserve bound registers for symbol lookup and it also creates an
external PLT section, .pl.bnd. Linker will create a BND PLT1 entry
in .plt:
68 00 00 00 00 pushq $index
f2 e9 00 00 00 00 bnd jmpq PLT0
0f 1f 44 00 00 nopl 0(%rax,%rax,1)
and a 8-byte BND PLT entry in .plt.bnd:
f2 ff 25 00 00 00 00 bnd jmpq *name@GOTPCREL(%rip)
90 nop
Otherwise, linker will create a legacy PLT1 entry in .plt:
68 00 00 00 00 pushq $index
e9 00 00 00 00 jmpq PLT0
66 0f 1f 44 00 00 nopw 0(%rax,%rax,1)
and a 8-byte legacy PLT in .plt.bnd:
ff 25 00 00 00 00 jmpq *name@GOTPCREL(%rip)
66 90 xchg %ax,%ax
The initial value of the GOT entry for "name" will be set to the the
"pushq" instruction in the corresponding entry in .plt. Linker will
resolve reference of symbol "name" to the entry in the second PLT,
.plt.bnd.
Prelink stores the offset of pushq of PLT1 (plt_base + 0x10) in GOT[1]
and GOT[1] is stored in GOT[3]. We can undo prelink in GOT by computing
the corresponding the pushq offset with
GOT[1] + (GOT offset - &GOT[3]) * 2
Since for each entry in .plt except for PLT0 we create a 8-byte entry in
.plt.bnd, there is extra 8-byte per PLT symbol.
We also investigated the 16-byte entry for .plt.bnd. We compared the
8-byte entry vs the the 16-byte entry for .plt.bnd on Sandy Bridge.
There are no performance differences in SPEC CPU 2000/2006 as well as
micro benchmarks.
Pros:
No change to undo prelink in dynamic linker.
Only 8-byte memory overhead for each PLT symbol.
Cons:
Extra .plt.bnd section is needed.
Extra 8 byte for legacy branches to PLT.
GDB is unware of the new layout of .plt and .plt.bnd.
bfd/
* elf64-x86-64.c (elf_x86_64_bnd_plt0_entry): New.
(elf_x86_64_legacy_plt_entry): Likewise.
(elf_x86_64_bnd_plt_entry): Likewise.
(elf_x86_64_legacy_plt2_entry): Likewise.
(elf_x86_64_bnd_plt2_entry): Likewise.
(elf_x86_64_bnd_arch_bed): Likewise.
(elf_x86_64_link_hash_entry): Add has_bnd_reloc and plt_bnd.
(elf_x86_64_link_hash_table): Add plt_bnd.
(elf_x86_64_link_hash_newfunc): Initialize has_bnd_reloc and
plt_bnd.
(elf_x86_64_copy_indirect_symbol): Also copy has_bnd_reloc.
(elf_x86_64_check_relocs): Create the second PLT for Intel MPX
in 64-bit mode.
(elf_x86_64_allocate_dynrelocs): Handle the second PLT for IFUNC
symbols. Resolve call to the second PLT if it is created.
(elf_x86_64_size_dynamic_sections): Keep the second PLT section.
(elf_x86_64_relocate_section): Resolve PLT references to the
second PLT if it is created.
(elf_x86_64_finish_dynamic_symbol): Use BND PLT0 and fill the
second PLT entry for BND relocation.
(elf_x86_64_finish_dynamic_sections): Use MPX backend data if
the second PLT is created.
(elf_x86_64_get_synthetic_symtab): New.
(bfd_elf64_get_synthetic_symtab): Likewise. Undefine for NaCl.
ld/
* emulparams/elf_x86_64.sh (TINY_READONLY_SECTION): New.
ld/testsuite/
* ld-x86-64/mpx.exp: Run bnd-ifunc-1 and bnd-plt-1.
* ld-x86-64/bnd-ifunc-1.d: New file.
* ld-x86-64/bnd-ifunc-1.s: Likewise.
* ld-x86-64/bnd-plt-1.d: Likewise.
2013-11-21 01:01:04 +08:00
|
|
|
run_dump_test "bnd-plt-1"
|