Skip to content

[libc][startup]: create header for ElfW and use in startup #96510

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 2 commits into from
Jun 24, 2024
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
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ add_macro_header(
limits-macros.h
)

add_macro_header(
link_macros
HDR
link-macros.h
)

add_macro_header(
math_macros
HDR
Expand Down
13 changes: 13 additions & 0 deletions libc/include/llvm-libc-macros/link-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Definition of macros to for extra dynamic linker functionality ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifdef __LP64__
#define ElfW(type) Elf64_ ## type
#else
#define ElfW(type) Elf32_ ## type
#endif
1 change: 1 addition & 0 deletions libc/startup/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ add_object_library(
libc.config.linux.app_h
libc.include.sys_mman
libc.include.sys_syscall
libc.include.llvm-libc-macros.link_macros
libc.src.__support.threads.thread
libc.src.__support.OSUtil.osutil
libc.src.stdlib.exit
Expand Down
9 changes: 5 additions & 4 deletions libc/startup/linux/do_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "startup/linux/do_start.h"
#include "include/llvm-libc-macros/link-macros.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/threads/thread.h"
#include "src/stdlib/atexit.h"
Expand Down Expand Up @@ -79,13 +80,13 @@ static ThreadAttributes main_thread_attrib;

// After the env array, is the aux-vector. The end of the aux-vector is
// denoted by an AT_NULL entry.
Elf64_Phdr *program_hdr_table = nullptr;
ElfW(Phdr) *program_hdr_table = nullptr;
uintptr_t program_hdr_count = 0;
app.auxv_ptr = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
for (auto *aux_entry = app.auxv_ptr; aux_entry->id != AT_NULL; ++aux_entry) {
switch (aux_entry->id) {
case AT_PHDR:
program_hdr_table = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
program_hdr_table = reinterpret_cast<ElfW(Phdr) *>(aux_entry->value);
break;
case AT_PHNUM:
program_hdr_count = aux_entry->value;
Expand All @@ -100,10 +101,10 @@ static ThreadAttributes main_thread_attrib;

ptrdiff_t base = 0;
app.tls.size = 0;
Elf64_Phdr *tls_phdr = nullptr;
ElfW(Phdr) *tls_phdr = nullptr;

for (uintptr_t i = 0; i < program_hdr_count; ++i) {
Elf64_Phdr &phdr = program_hdr_table[i];
ElfW(Phdr) &phdr = program_hdr_table[i];
if (phdr.p_type == PT_PHDR)
base = reinterpret_cast<ptrdiff_t>(program_hdr_table) - phdr.p_vaddr;
if (phdr.p_type == PT_DYNAMIC && _DYNAMIC)
Expand Down
Loading