From 3ee0f48429e6571a87320f2f2e56a48e6717cff1 Mon Sep 17 00:00:00 2001 From: Pieter Agten Date: Wed, 4 Dec 2019 14:20:09 +0100 Subject: [PATCH 1/3] Create a separate entry point for the ELF file, instead of using the SGX entry point --- .../spec/x86_64_fortanix_unknown_sgx.rs | 3 ++- src/libstd/sys/sgx/abi/entry.S | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs index dbcd77bc753e8..3d590a5af0623 100644 --- a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs +++ b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs @@ -7,7 +7,7 @@ pub fn target() -> Result { "--as-needed", "--eh-frame-hdr", "-z" , "noexecstack", - "-e","sgx_entry", + "-e","elf_entry", "-Bstatic", "--gc-sections", "-z","text", @@ -29,6 +29,7 @@ pub fn target() -> Result { ]; const EXPORT_SYMBOLS: &[&str] = &[ + "elf_entry", "sgx_entry", "HEAP_BASE", "HEAP_SIZE", diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index cd26c7ca200b0..a11eb18b0f77e 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -104,6 +104,26 @@ IMAGE_BASE: and %gs:tcsls_flags,%\reg .endm +/* We place the ELF entry point in a separate section so it can be removed by + elf2sgxs */ +.section .text_no_sgx, "ax" +.Lelf_entry_error_msg: + .ascii "Error: This file is an SGX enclave which cannot be executed as a standard Linux binary.\nSee the installation guide at https://edp.fortanix.com/docs/installation/guide/ on how to use 'cargo run' or follow the steps at https://edp.fortanix.com/docs/tasks/deployment/ for manual deployment.\n" +.global elf_entry +.type elf_entry,function +elf_entry: +/* print error message */ + movq $1, %rax /* write() syscall */ + movq $2, %rdi /* write to stderr */ + lea .Lelf_entry_error_msg(%rip), %rsi + movq $288, %rdx /* num chars to write */ + syscall + + movq $60, %rax /* exit() syscall */ + movq $0, %rdi /* error code */ + syscall +/* end elf_entry */ + .text .global sgx_entry .type sgx_entry,function From 6354d48dc5839878742e7fbf21a120c5c51a946f Mon Sep 17 00:00:00 2001 From: Pieter Agten Date: Thu, 5 Dec 2019 12:24:38 +0100 Subject: [PATCH 2/3] Processed review comments --- .../spec/x86_64_fortanix_unknown_sgx.rs | 1 - src/libstd/sys/sgx/abi/entry.S | 24 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs index 3d590a5af0623..2c9ba9f2ca971 100644 --- a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs +++ b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs @@ -29,7 +29,6 @@ pub fn target() -> Result { ]; const EXPORT_SYMBOLS: &[&str] = &[ - "elf_entry", "sgx_entry", "HEAP_BASE", "HEAP_SIZE", diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index a11eb18b0f77e..ca2848c61d934 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -109,19 +109,29 @@ IMAGE_BASE: .section .text_no_sgx, "ax" .Lelf_entry_error_msg: .ascii "Error: This file is an SGX enclave which cannot be executed as a standard Linux binary.\nSee the installation guide at https://edp.fortanix.com/docs/installation/guide/ on how to use 'cargo run' or follow the steps at https://edp.fortanix.com/docs/tasks/deployment/ for manual deployment.\n" +.Lelf_entry_error_msg_end: + .global elf_entry .type elf_entry,function elf_entry: /* print error message */ - movq $1, %rax /* write() syscall */ - movq $2, %rdi /* write to stderr */ - lea .Lelf_entry_error_msg(%rip), %rsi - movq $288, %rdx /* num chars to write */ + movq $1,%rax /* write() syscall */ + movq $2,%rdi /* write to stderr (fd 2) */ + lea .Lelf_entry_error_msg(%rip),%rsi + movq $.Lelf_entry_error_msg_end-.Lelf_entry_error_msg,%rdx +.Lelf_entry_call: syscall - - movq $60, %rax /* exit() syscall */ - movq $0, %rdi /* error code */ + test %rax,%rax + jle .Lelf_exit /* exit on error */ + add %rax,%rsi + sub %rax,%rdx /* all chars written? */ + jnz .Lelf_entry_call + +.Lelf_exit: + movq $60,%rax /* exit() syscall */ + movq $1,%rdi /* exit code 1 */ syscall + ud2 /* should not be reached */ /* end elf_entry */ .text From f02ffb8b4ca760117875f3b5326e9cff6598dde3 Mon Sep 17 00:00:00 2001 From: Pieter Agten Date: Fri, 6 Dec 2019 10:54:53 +0100 Subject: [PATCH 3/3] Rewrite %rax register before syscall because it is overwritten by the syscall itself --- src/libstd/sys/sgx/abi/entry.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index ca2848c61d934..a3e059e813173 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -115,11 +115,11 @@ IMAGE_BASE: .type elf_entry,function elf_entry: /* print error message */ - movq $1,%rax /* write() syscall */ - movq $2,%rdi /* write to stderr (fd 2) */ + movq $2,%rdi /* write to stderr (fd 2) */ lea .Lelf_entry_error_msg(%rip),%rsi movq $.Lelf_entry_error_msg_end-.Lelf_entry_error_msg,%rdx .Lelf_entry_call: + movq $1,%rax /* write() syscall */ syscall test %rax,%rax jle .Lelf_exit /* exit on error */