Skip to content

Commit 23981c4

Browse files
[libc] Refactor internal auxv usage to reduce getauxval dependencies (#162489)
1 parent 419f5f1 commit 23981c4

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

libc/src/__support/OSUtil/linux/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ add_object_library(
7070
libc.src.__support.CPP.string_view
7171
libc.src.__support.threads.callonce
7272
libc.src.__support.threads.linux.futex_word_type
73+
libc.src.__support.OSUtil.linux.auxv
7374
libc.hdr.types.struct_timeval
7475
libc.hdr.types.struct_timespec
7576
libc.hdr.types.clockid_t
7677
libc.hdr.types.time_t
7778
libc.hdr.link_macros
78-
libc.src.errno.errno
79-
libc.src.sys.auxv.getauxval
8079
)

libc/src/__support/OSUtil/linux/vdso.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
#include "src/__support/CPP/array.h"
1212
#include "src/__support/CPP/optional.h"
1313
#include "src/__support/CPP/string_view.h"
14-
#include "src/__support/libc_errno.h"
14+
#include "src/__support/OSUtil/linux/auxv.h"
1515
#include "src/__support/threads/callonce.h"
1616
#include "src/__support/threads/linux/futex_word.h"
17-
#include "src/sys/auxv/getauxval.h"
1817
#include <linux/auxvec.h>
1918

2019
// TODO: This is a temporary workaround to avoid including elf.h
@@ -189,17 +188,13 @@ void Symbol::initialize_vdso_global_cache() {
189188
for (auto &i : global_cache)
190189
i = nullptr;
191190

192-
// get the address of the VDSO, protect errno since getauxval may change
193-
// it
194-
int errno_backup = libc_errno;
195-
uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
191+
cpp::optional<unsigned long> auxv_res = auxv::get(AT_SYSINFO_EHDR);
192+
uintptr_t vdso_ehdr_addr = auxv_res ? static_cast<uintptr_t>(*auxv_res) : 0;
196193
// Get the memory address of the vDSO ELF header.
197194
auto vdso_ehdr = reinterpret_cast<ElfW(Ehdr) *>(vdso_ehdr_addr);
198195
// leave the table unpopulated if we don't have vDSO
199-
if (vdso_ehdr == nullptr) {
200-
libc_errno = errno_backup;
196+
if (vdso_ehdr == nullptr)
201197
return;
202-
}
203198

204199
// locate the section header inside the elf using the section header
205200
// offset

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ add_entrypoint_object(
563563
DEPENDS
564564
libc.include.unistd
565565
libc.include.sys_auxv
566-
libc.src.errno.errno
567-
libc.src.sys.auxv.getauxval
566+
libc.src.__support.libc_errno
567+
libc.src.__support.OSUtil.linux.auxv
568568
)
569569

570570
add_entrypoint_object(

libc/src/unistd/linux/sysconf.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
#include "src/__support/common.h"
1212

1313
#include "hdr/unistd_macros.h"
14+
#include "src/__support/OSUtil/linux/auxv.h"
1415
#include "src/__support/libc_errno.h"
1516
#include "src/__support/macros/config.h"
16-
#include "src/sys/auxv/getauxval.h"
17-
#include <sys/auxv.h>
1817

1918
namespace LIBC_NAMESPACE_DECL {
2019

2120
LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
2221
long ret = 0;
23-
if (name == _SC_PAGESIZE)
24-
return static_cast<long>(getauxval(AT_PAGESZ));
22+
if (name == _SC_PAGESIZE) {
23+
cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
24+
if (page_size)
25+
return static_cast<long>(*page_size);
26+
ret = -1;
27+
}
2528

2629
// TODO: Complete the rest of the sysconf options.
2730
if (ret < 0) {

0 commit comments

Comments
 (0)