Skip to content

Commit

Permalink
Auto merge of #67084 - Pagten:feature/print-msg-from-elf-entrypoint, …
Browse files Browse the repository at this point in the history
…r=Amanieu

SGX: Change ELF entrypoint

This fixes [rust-sgx issue #148](fortanix/rust-sgx#148).

A new entry point is created for the ELF file generated by `rustc`, separate from the enclave entry point. When the ELF file is executed as a Linux binary, the error message below is written to stderr.

> Error: This file is an SGX enclave which cannot be executed as a standard Linux binary.
> See 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.

When the ELF file is converted to an SGXS using `elf2sgxs`, the old entry point is still set as the enclave entry point. In a future pull request in the rust-sgx repository, `elf2sgxs` will be modified to remove the code in the ELF entry point, since this code is not needed in the enclave.
  • Loading branch information
bors committed Dec 14, 2019
2 parents 8843b28 + f02ffb8 commit 12307b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs
Expand Up @@ -7,7 +7,7 @@ pub fn target() -> Result<Target, String> {
"--as-needed",
"--eh-frame-hdr",
"-z" , "noexecstack",
"-e","sgx_entry",
"-e","elf_entry",
"-Bstatic",
"--gc-sections",
"-z","text",
Expand Down
30 changes: 30 additions & 0 deletions src/libstd/sys/sgx/abi/entry.S
Expand Up @@ -104,6 +104,36 @@ 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"
.Lelf_entry_error_msg_end:

.global elf_entry
.type elf_entry,function
elf_entry:
/* print error message */
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 */
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
.global sgx_entry
.type sgx_entry,function
Expand Down

0 comments on commit 12307b3

Please sign in to comment.