Skip to content

[libc][NFC] unify startup library's code style with the rest #74041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libc/config/linux/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ struct Args {
// Data structure which captures properties of a linux application.
struct AppProperties {
// Page size used for the application.
uintptr_t pageSize;
uintptr_t page_size;

Args *args;

// The properties of an application's TLS image.
TLSImage tls;

// Environment data.
EnvironType *envPtr;
EnvironType *env_ptr;
};

extern AppProperties app;
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/getenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(char *, getenv, (const char *name)) {
char **env_ptr = reinterpret_cast<char **>(LIBC_NAMESPACE::app.envPtr);
char **env_ptr = reinterpret_cast<char **>(LIBC_NAMESPACE::app.env_ptr);

if (name == nullptr || env_ptr == nullptr)
return nullptr;
Expand Down
18 changes: 9 additions & 9 deletions libc/startup/linux/aarch64/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
// We cannot check the return value with MAP_FAILED as that is the return
// of the mmap function and not the mmap syscall.
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.pageSize)
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, 1);
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
Expand Down Expand Up @@ -144,7 +144,7 @@ __attribute__((noinline)) static void do_start() {
// value. We step over it (the "+ 1" below) to get to the env values.
uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
uint64_t *env_end_marker = env_ptr;
app.envPtr = env_ptr;
app.env_ptr = env_ptr;
while (*env_end_marker)
++env_end_marker;

Expand All @@ -153,28 +153,28 @@ __attribute__((noinline)) static void do_start() {

// After the env array, is the aux-vector. The end of the aux-vector is
// denoted by an AT_NULL entry.
Elf64_Phdr *programHdrTable = nullptr;
uintptr_t programHdrCount;
Elf64_Phdr *program_hdr_table = nullptr;
uintptr_t program_hdr_count;
for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
aux_entry->type != AT_NULL; ++aux_entry) {
switch (aux_entry->type) {
case AT_PHDR:
programHdrTable = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
program_hdr_table = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
break;
case AT_PHNUM:
programHdrCount = aux_entry->value;
program_hdr_count = aux_entry->value;
break;
case AT_PAGESZ:
app.pageSize = aux_entry->value;
app.page_size = aux_entry->value;
break;
default:
break; // TODO: Read other useful entries from the aux vector.
}
}

app.tls.size = 0;
for (uintptr_t i = 0; i < programHdrCount; ++i) {
Elf64_Phdr *phdr = programHdrTable + i;
for (uintptr_t i = 0; i < program_hdr_count; ++i) {
Elf64_Phdr *phdr = program_hdr_table + i;
if (phdr->p_type != PT_TLS)
continue;
// TODO: p_vaddr value has to be adjusted for static-pie executables.
Expand Down
18 changes: 9 additions & 9 deletions libc/startup/linux/riscv/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void init_tls(TLSDescriptor &tls_descriptor) {
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
// We cannot check the return value with MAP_FAILED as that is the return
// of the mmap function and not the mmap syscall.
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.pageSize)
if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, 1);
uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
Expand Down Expand Up @@ -147,7 +147,7 @@ __attribute__((noinline)) static void do_start() {
// value. We step over it (the "+ 1" below) to get to the env values.
LIBC_NAMESPACE::ArgVEntryType *env_ptr = app.args->argv + app.args->argc + 1;
LIBC_NAMESPACE::ArgVEntryType *env_end_marker = env_ptr;
app.envPtr = env_ptr;
app.env_ptr = env_ptr;
while (*env_end_marker)
++env_end_marker;

Expand All @@ -156,28 +156,28 @@ __attribute__((noinline)) static void do_start() {

// After the env array, is the aux-vector. The end of the aux-vector is
// denoted by an AT_NULL entry.
PgrHdrTableType *programHdrTable = nullptr;
uintptr_t programHdrCount;
PgrHdrTableType *program_hdr_table = nullptr;
uintptr_t program_hdr_count;
for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
aux_entry->type != AT_NULL; ++aux_entry) {
switch (aux_entry->type) {
case AT_PHDR:
programHdrTable = reinterpret_cast<PgrHdrTableType *>(aux_entry->value);
program_hdr_table = reinterpret_cast<PgrHdrTableType *>(aux_entry->value);
break;
case AT_PHNUM:
programHdrCount = aux_entry->value;
program_hdr_count = aux_entry->value;
break;
case AT_PAGESZ:
app.pageSize = aux_entry->value;
app.page_size = aux_entry->value;
break;
default:
break; // TODO: Read other useful entries from the aux vector.
}
}

app.tls.size = 0;
for (uintptr_t i = 0; i < programHdrCount; ++i) {
PgrHdrTableType *phdr = programHdrTable + i;
for (uintptr_t i = 0; i < program_hdr_count; ++i) {
PgrHdrTableType *phdr = program_hdr_table + i;
if (phdr->p_type != PT_TLS)
continue;
// TODO: p_vaddr value has to be adjusted for static-pie executables.
Expand Down
53 changes: 27 additions & 26 deletions libc/startup/linux/x86_64/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ extern "C" void __stack_chk_fail() {
namespace LIBC_NAMESPACE {

#ifdef SYS_mmap2
static constexpr long mmapSyscallNumber = SYS_mmap2;
static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;
#elif SYS_mmap
static constexpr long mmapSyscallNumber = SYS_mmap;
static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
#else
#error "mmap and mmap2 syscalls not available."
#endif
Expand All @@ -54,49 +54,50 @@ void init_tls(TLSDescriptor &tls_descriptor) {
}

// We will assume the alignment is always a power of two.
uintptr_t tlsSize = app.tls.size & -app.tls.align;
if (tlsSize != app.tls.size)
tlsSize += app.tls.align;
uintptr_t tls_size = app.tls.size & -app.tls.align;
if (tls_size != app.tls.size)
tls_size += app.tls.align;

// Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
// address of the TLS block. So, we add more size to accomodate this address
// entry.
// We also need to include space for the stack canary. The canary is at
// offset 0x28 (40) and is of size uintptr_t.
uintptr_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t) + 40;
uintptr_t tls_size_with_addr = tls_size + sizeof(uintptr_t) + 40;

// We cannot call the mmap function here as the functions set errno on
// failure. Since errno is implemented via a thread local variable, we cannot
// use errno before TLS is setup.
long mmapRetVal = LIBC_NAMESPACE::syscall_impl<long>(
mmapSyscallNumber, nullptr, tlsSizeWithAddr, PROT_READ | PROT_WRITE,
long mmap_retval = LIBC_NAMESPACE::syscall_impl<long>(
MMAP_SYSCALL_NUMBER, nullptr, tls_size_with_addr, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
// We cannot check the return value with MAP_FAILED as that is the return
// of the mmap function and not the mmap syscall.
if (mmapRetVal < 0 && static_cast<uintptr_t>(mmapRetVal) > -app.pageSize)
if (mmap_retval < 0 && static_cast<uintptr_t>(mmap_retval) > -app.page_size)
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, 1);
uintptr_t *tlsAddr = reinterpret_cast<uintptr_t *>(mmapRetVal);
uintptr_t *tls_addr = reinterpret_cast<uintptr_t *>(mmap_retval);

// x86_64 TLS faces down from the thread pointer with the first entry
// pointing to the address of the first real TLS byte.
uintptr_t endPtr = reinterpret_cast<uintptr_t>(tlsAddr) + tlsSize;
*reinterpret_cast<uintptr_t *>(endPtr) = endPtr;
uintptr_t end_ptr = reinterpret_cast<uintptr_t>(tls_addr) + tls_size;
*reinterpret_cast<uintptr_t *>(end_ptr) = end_ptr;

LIBC_NAMESPACE::inline_memcpy(reinterpret_cast<char *>(tlsAddr),
LIBC_NAMESPACE::inline_memcpy(reinterpret_cast<char *>(tls_addr),
reinterpret_cast<const char *>(app.tls.address),
app.tls.init_size);
uintptr_t *stackGuardAddr = reinterpret_cast<uintptr_t *>(endPtr + 40);
uintptr_t *stack_guard_addr = reinterpret_cast<uintptr_t *>(end_ptr + 40);
// Setting the stack guard to a random value.
// We cannot call the get_random function here as the function sets errno on
// failure. Since errno is implemented via a thread local variable, we cannot
// use errno before TLS is setup.
ssize_t stackGuardRetVal = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_getrandom, reinterpret_cast<long>(stackGuardAddr), sizeof(uint64_t),
ssize_t stack_guard_retval = LIBC_NAMESPACE::syscall_impl<ssize_t>(
SYS_getrandom, reinterpret_cast<long>(stack_guard_addr), sizeof(uint64_t),
0);
if (stackGuardRetVal < 0)
if (stack_guard_retval < 0)
LIBC_NAMESPACE::syscall_impl(SYS_exit, 1);

tls_descriptor = {tlsSizeWithAddr, uintptr_t(tlsAddr), endPtr};
tls_descriptor = {tls_size_with_addr, reinterpret_cast<uintptr_t>(tls_addr),
end_ptr};
return;
}

Expand Down Expand Up @@ -181,7 +182,7 @@ extern "C" void _start() {
// value. We step over it (the "+ 1" below) to get to the env values.
uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
uint64_t *env_end_marker = env_ptr;
app.envPtr = env_ptr;
app.env_ptr = env_ptr;
while (*env_end_marker)
++env_end_marker;

Expand All @@ -190,28 +191,28 @@ extern "C" void _start() {

// After the env array, is the aux-vector. The end of the aux-vector is
// denoted by an AT_NULL entry.
Elf64_Phdr *programHdrTable = nullptr;
uintptr_t programHdrCount;
Elf64_Phdr *program_hdr_table = nullptr;
uintptr_t program_hdr_count = 0;
for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
aux_entry->type != AT_NULL; ++aux_entry) {
switch (aux_entry->type) {
case AT_PHDR:
programHdrTable = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
program_hdr_table = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
break;
case AT_PHNUM:
programHdrCount = aux_entry->value;
program_hdr_count = aux_entry->value;
break;
case AT_PAGESZ:
app.pageSize = aux_entry->value;
app.page_size = aux_entry->value;
break;
default:
break; // TODO: Read other useful entries from the aux vector.
}
}

app.tls.size = 0;
for (uintptr_t i = 0; i < programHdrCount; ++i) {
Elf64_Phdr *phdr = programHdrTable + i;
for (uintptr_t i = 0; i < program_hdr_count; ++i) {
Elf64_Phdr *phdr = program_hdr_table + i;
if (phdr->p_type != PT_TLS)
continue;
// TODO: p_vaddr value has to be adjusted for static-pie executables.
Expand Down