Skip to content

Commit

Permalink
x64: Fix ELF initial stack state
Browse files Browse the repository at this point in the history
Fix ELF initial stack state as per Figure 3.9 ("Initial Process Stack")
in the x86-64 ABI specification. This is needed for statically linked
executables.

Signed-off-by: Pekka Enberg <[email protected]>
  • Loading branch information
Pekka Enberg committed Nov 18, 2014
1 parent 463ca65 commit cdc758b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions arch/aarch64/arch-elf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ enum {
/* for pltgot relocation */
#define ARCH_JUMP_SLOT R_AARCH64_JUMP_SLOT

inline void elf_entry_point(void* ep)
{
// Not implemented:
assert(0);
}

#endif /* ARCH_ELF_HH */
12 changes: 12 additions & 0 deletions arch/x64/arch-elf.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ enum {
/* for pltgot relocation */
#define ARCH_JUMP_SLOT R_X86_64_JUMP_SLOT

inline void elf_entry_point(void* ep)
{
asm volatile (
"pushq $0\n\t" // Zero
"pushq $0\n\t" // Environment pointers
"pushq $0\n\t" // Zero
"pushq $0\n\t" // Argument count
"jmpq *%0\n\t"
:
: "r"(ep));
}

#endif /* ARCH_ELF_HH */
4 changes: 2 additions & 2 deletions core/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ application::application(const std::string& command, const std::vector<std::stri

_main = _lib->lookup<int (int, char**)>("main");
if (!_main) {
_entry_point = reinterpret_cast<void(*)()>(_lib->entry_point());
_entry_point = _lib->entry_point();
}
if (!_entry_point && !_main) {
throw launch_error("Failed looking up main");
Expand Down Expand Up @@ -202,7 +202,7 @@ void application::main()
// may be called twice, TLS may be overriden and the program may not
// received correct arguments, environment variables and auxiliary
// vector.
_entry_point();
elf_entry_point(_entry_point);
}

// _entry_point() doesn't return
Expand Down
2 changes: 1 addition & 1 deletion include/osv/app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private:
mutex _termination_mutex;
std::shared_ptr<elf::object> _lib;
main_func_t* _main;
void (*_entry_point)();
void* _entry_point;
static app_registry apps;

// Must be destroyed before _lib
Expand Down

0 comments on commit cdc758b

Please sign in to comment.