Skip to content

Commit

Permalink
Support -z x86-64-v[234]
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jul 21, 2024
1 parent b919548 commit 5606087
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,12 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.z_rewrite_endbr = true;
} else if (read_z_flag("rodynamic")) {
ctx.arg.z_rodynamic = true;
} else if (read_z_flag("x86-64-v2")) {
ctx.arg.z_x86_64_isa_level |= GNU_PROPERTY_X86_ISA_1_V2;
} else if (read_z_flag("x86-64-v3")) {
ctx.arg.z_x86_64_isa_level |= GNU_PROPERTY_X86_ISA_1_V3;
} else if (read_z_flag("x86-64-v4")) {
ctx.arg.z_x86_64_isa_level |= GNU_PROPERTY_X86_ISA_1_V4;
} else if (read_flag("nmagic")) {
ctx.arg.nmagic = true;
} else if (read_flag("no-nmagic")) {
Expand Down
6 changes: 6 additions & 0 deletions elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ enum : u32 {
GNU_PROPERTY_X86_FEATURE_1_IBT = 1,
GNU_PROPERTY_X86_FEATURE_1_SHSTK = 2,
GNU_PROPERTY_X86_FEATURE_1_AND = 0xc0000002,

GNU_PROPERTY_X86_ISA_1_NEEDED = 0xc0008002,
GNU_PROPERTY_X86_ISA_1_BASELINE = 1,
GNU_PROPERTY_X86_ISA_1_V2 = 2,
GNU_PROPERTY_X86_ISA_1_V3 = 4,
GNU_PROPERTY_X86_ISA_1_V4 = 8,
};

enum : u32 {
Expand Down
1 change: 1 addition & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,7 @@ struct Context {
std::vector<std::string_view> exclude_libs;
std::vector<std::string_view> filter;
std::vector<std::string_view> trace_symbol;
u32 z_x86_64_isa_level = 0;
u64 image_base = 0x200000;
u64 shuffle_sections_seed = 0;
} arg;
Expand Down
2 changes: 2 additions & 0 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,8 @@ void NotePropertySection<E>::update_shdr(Context<E> &ctx) {
if (ctx.arg.z_shstk)
properties[GNU_PROPERTY_X86_FEATURE_1_AND] |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;

properties[GNU_PROPERTY_X86_ISA_1_NEEDED] |= ctx.arg.z_x86_64_isa_level;

std::erase_if(properties, [](std::pair<u32, u32> kv) {
return kv.second == 0;
});
Expand Down
15 changes: 15 additions & 0 deletions test/elf/x86_64_isa-level.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xc -
int main() {}
EOF

$CC -B. -o $t/exe2 $t/a.o -Wl,-z,x86-64-v2
readelf -n $t/exe2 | grep -q 'x86 ISA needed: .*x86-64-v2'

$CC -B. -o $t/exe3 $t/a.o -Wl,-z,x86-64-v3
readelf -n $t/exe3 | grep -q 'x86 ISA needed: .*x86-64-v3'

$CC -B. -o $t/exe4 $t/a.o -Wl,-z,x86-64-v4
readelf -n $t/exe4 | grep -q 'x86 ISA needed: .*x86-64-v4'

0 comments on commit 5606087

Please sign in to comment.