2020-10-10 18:17:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-10 18:17:49 +03:00
|
|
|
*/
|
|
|
|
|
2023-09-08 20:30:35 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
|
|
|
#include <Kernel/API/POSIX/sys/stat.h>
|
2020-10-10 18:17:49 +03:00
|
|
|
#include <LibELF/AuxiliaryVector.h>
|
2021-01-02 02:54:43 +03:00
|
|
|
#include <LibELF/DynamicLinker.h>
|
2021-07-26 16:03:29 +03:00
|
|
|
#include <LibELF/Relocation.h>
|
2023-09-08 20:30:35 +03:00
|
|
|
#include <fcntl.h>
|
2023-01-03 17:30:44 +03:00
|
|
|
#include <sys/internals.h>
|
2023-09-08 20:30:35 +03:00
|
|
|
#include <syscall.h>
|
2023-01-03 17:30:44 +03:00
|
|
|
#include <unistd.h>
|
2020-10-10 18:17:49 +03:00
|
|
|
|
|
|
|
char* __static_environ[] = { nullptr }; // We don't get the environment without some libc workarounds..
|
|
|
|
|
|
|
|
static void init_libc()
|
|
|
|
{
|
|
|
|
environ = __static_environ;
|
|
|
|
__environ_is_malloced = false;
|
|
|
|
__stdio_is_initialized = false;
|
2021-01-02 02:54:43 +03:00
|
|
|
// Initialise the copy of libc included statically in Loader.so,
|
|
|
|
// initialisation of the dynamic libc.so is done by the DynamicLinker
|
|
|
|
__libc_init();
|
2020-10-10 18:17:49 +03:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:39:36 +03:00
|
|
|
static void perform_self_relocations(auxv_t* auxvp)
|
2020-10-10 18:17:49 +03:00
|
|
|
{
|
|
|
|
// We need to relocate ourselves.
|
|
|
|
// (these relocations seem to be generated because of our vtables)
|
|
|
|
|
2020-10-17 14:39:36 +03:00
|
|
|
FlatPtr base_address = 0;
|
|
|
|
bool found_base_address = false;
|
|
|
|
for (; auxvp->a_type != AT_NULL; ++auxvp) {
|
2020-12-25 16:48:30 +03:00
|
|
|
if (auxvp->a_type == ELF::AuxiliaryValue::BaseAddress) {
|
2020-10-17 14:39:36 +03:00
|
|
|
base_address = auxvp->a_un.a_val;
|
|
|
|
found_base_address = true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(found_base_address);
|
2021-07-26 16:03:29 +03:00
|
|
|
if (!ELF::perform_relative_relocations(base_address))
|
2021-07-06 11:17:11 +03:00
|
|
|
exit(1);
|
2020-10-10 18:17:49 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 00:48:42 +03:00
|
|
|
static void display_help()
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
char const message[] =
|
2020-12-31 00:48:42 +03:00
|
|
|
R"(You have invoked `Loader.so'. This is the helper program for programs that
|
|
|
|
use shared libraries. Special directives embedded in executables tell the
|
|
|
|
kernel to load this program.
|
|
|
|
|
|
|
|
This helper program loads the shared libraries needed by the program,
|
|
|
|
prepares the program to run, and runs it. You do not need to invoke
|
2023-09-08 20:30:35 +03:00
|
|
|
this helper program directly. If you still want to run a program with the loader,
|
|
|
|
run: /usr/lib/Loader.so [ELF_BINARY])";
|
2021-01-02 02:54:43 +03:00
|
|
|
fprintf(stderr, "%s", message);
|
2020-12-31 00:48:42 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:54:43 +03:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
// The compiler expects a previous declaration
|
2021-09-05 17:08:13 +03:00
|
|
|
void _start(int, char**, char**) __attribute__((used));
|
|
|
|
void _entry(int, char**, char**) __attribute__((used));
|
2021-01-02 02:54:43 +03:00
|
|
|
|
2021-07-11 16:57:56 +03:00
|
|
|
NAKED void _start(int, char**, char**)
|
|
|
|
{
|
2023-04-14 17:44:17 +03:00
|
|
|
#if ARCH(AARCH64)
|
2023-04-22 18:30:09 +03:00
|
|
|
// Make sure backtrace computation stops here by setting FP and LR to 0.
|
|
|
|
// FIXME: The kernel should ensure that registers are zeroed on program start
|
2022-10-12 23:18:24 +03:00
|
|
|
asm(
|
2023-04-22 18:30:09 +03:00
|
|
|
"mov x29, 0\n"
|
|
|
|
"mov x30, 0\n"
|
2022-10-12 23:18:24 +03:00
|
|
|
"bl _entry\n");
|
2023-09-18 13:56:39 +03:00
|
|
|
#elif ARCH(RISCV64)
|
|
|
|
asm(
|
|
|
|
"li fp, 0\n"
|
|
|
|
"li ra, 0\n"
|
|
|
|
"tail _entry@plt\n");
|
|
|
|
#elif ARCH(X86_64)
|
2021-07-11 16:57:56 +03:00
|
|
|
asm(
|
|
|
|
"push $0\n"
|
|
|
|
"jmp _entry@plt\n");
|
2023-09-18 13:56:39 +03:00
|
|
|
#else
|
|
|
|
# error "Unknown architecture"
|
2022-10-12 23:18:24 +03:00
|
|
|
#endif
|
2021-07-11 16:57:56 +03:00
|
|
|
}
|
2021-07-09 01:58:43 +03:00
|
|
|
|
2023-09-08 20:30:35 +03:00
|
|
|
static ErrorOr<int> open_executable(char const* path)
|
|
|
|
{
|
|
|
|
int rc = open(path, O_RDONLY | O_EXEC);
|
|
|
|
if (rc < 0)
|
|
|
|
return Error::from_errno(errno);
|
|
|
|
int checked_fd = rc;
|
|
|
|
ArmedScopeGuard close_on_failure([checked_fd] {
|
|
|
|
close(checked_fd);
|
|
|
|
});
|
|
|
|
|
|
|
|
struct stat executable_stat = {};
|
|
|
|
rc = fstat(checked_fd, &executable_stat);
|
|
|
|
if (rc < 0)
|
|
|
|
return Error::from_errno(errno);
|
|
|
|
if (!S_ISREG(executable_stat.st_mode)) {
|
|
|
|
if (S_ISDIR(executable_stat.st_mode))
|
|
|
|
return Error::from_errno(EISDIR);
|
|
|
|
return Error::from_errno(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
close_on_failure.disarm();
|
|
|
|
return checked_fd;
|
|
|
|
}
|
|
|
|
|
2021-07-09 01:58:43 +03:00
|
|
|
void _entry(int argc, char** argv, char** envp)
|
2020-10-10 18:17:49 +03:00
|
|
|
{
|
2021-01-02 02:54:43 +03:00
|
|
|
char** env;
|
|
|
|
for (env = envp; *env; ++env) {
|
|
|
|
}
|
|
|
|
|
|
|
|
auxv_t* auxvp = (auxv_t*)++env;
|
|
|
|
perform_self_relocations(auxvp);
|
|
|
|
init_libc();
|
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
int main_program_fd = -1;
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString main_program_path;
|
2021-01-09 10:51:44 +03:00
|
|
|
bool is_secure = false;
|
2020-10-10 18:17:49 +03:00
|
|
|
for (; auxvp->a_type != AT_NULL; ++auxvp) {
|
2020-12-25 16:48:30 +03:00
|
|
|
if (auxvp->a_type == ELF::AuxiliaryValue::ExecFileDescriptor) {
|
2020-10-10 18:17:49 +03:00
|
|
|
main_program_fd = auxvp->a_un.a_val;
|
|
|
|
}
|
2020-12-25 16:48:30 +03:00
|
|
|
if (auxvp->a_type == ELF::AuxiliaryValue::ExecFilename) {
|
2022-10-28 17:30:27 +03:00
|
|
|
main_program_path = (char const*)auxvp->a_un.a_ptr;
|
2020-11-11 22:13:55 +03:00
|
|
|
}
|
2021-01-09 10:51:44 +03:00
|
|
|
if (auxvp->a_type == ELF::AuxiliaryValue::Secure) {
|
|
|
|
is_secure = auxvp->a_un.a_val == 1;
|
|
|
|
}
|
2020-10-10 18:17:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 13:17:20 +03:00
|
|
|
if (main_program_fd == -1) {
|
2020-12-31 00:48:42 +03:00
|
|
|
// We've been invoked directly as an executable rather than as the
|
2023-09-08 20:30:35 +03:00
|
|
|
// ELF interpreter for some other binary. The second argv string should
|
|
|
|
// be the path to the ELF executable, and if we don't have enough strings in argv
|
|
|
|
// (argc < 2), just fail with message to stderr about this.
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
display_help();
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
auto error_or_fd = open_executable(argv[1]);
|
|
|
|
if (error_or_fd.is_error()) {
|
|
|
|
warnln("Loader.so: Loading {} failed: {}", argv[1], strerror(error_or_fd.error().code()));
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
main_program_fd = error_or_fd.release_value();
|
|
|
|
main_program_path = argv[1];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
2020-12-31 00:48:42 +03:00
|
|
|
}
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(main_program_fd >= 0);
|
2022-10-28 17:30:27 +03:00
|
|
|
VERIFY(!main_program_path.is_empty());
|
2021-01-03 00:31:01 +03:00
|
|
|
|
2022-10-28 17:30:27 +03:00
|
|
|
ELF::DynamicLinker::linker_main(move(main_program_path), main_program_fd, is_secure, argc, argv, envp);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-10 18:17:49 +03:00
|
|
|
}
|
|
|
|
}
|