1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 07:24:58 +00:00

UserspaceEmulator: Put the executable name in argv[0] :^)

The emulated program can now find its own name in argv[0]. Very cool!
This commit is contained in:
Andreas Kling 2020-07-12 20:24:10 +02:00
parent ddf7b817df
commit 079021a607
5 changed files with 23 additions and 6 deletions

View file

@ -26,6 +26,7 @@
#include "Emulator.h"
#include "SoftCPU.h"
#include <AK/LexicalPath.h>
#include <AK/LogStream.h>
#include <Kernel/API/Syscall.h>
#include <stdio.h>
@ -92,9 +93,10 @@ private:
u8* m_data { nullptr };
};
Emulator::Emulator(NonnullRefPtr<ELF::Loader> elf)
Emulator::Emulator(const String& executable_path, NonnullRefPtr<ELF::Loader> elf)
: m_elf(move(elf))
, m_cpu(*this)
, m_executable_path(executable_path)
{
setup_stack();
}
@ -105,16 +107,19 @@ void Emulator::setup_stack()
m_mmu.add_region(move(stack_region));
m_cpu.set_esp(stack_location + stack_size);
m_cpu.push_string(LexicalPath(m_executable_path).basename());
u32 argv0 = m_cpu.esp();
m_cpu.push32(0); // char** envp = { nullptr }
u32 envp = m_cpu.esp();
m_cpu.push32(0); // char** argv = { nullptr }
m_cpu.push32(0); // char** argv = { argv0, nullptr }
m_cpu.push32(argv0);
u32 argv = m_cpu.esp();
m_cpu.push32(0); // (alignment)
m_cpu.push32(0); // (alignment)
u32 argc = 0;
u32 argc = 1;
m_cpu.push32(envp);
m_cpu.push32(argv);
m_cpu.push32(argc);