[libc-commits] [libc] [libc] Add barebones dl_iterate_phdr implementation (PR #194196)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Sun Apr 26 01:18:08 PDT 2026


================
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===/
+
+#include "hdr/types/size_t.h"
+#include "src/link/dl_iterate_phdr.h"
+#include "test/UnitTest/Test.h"
+
+int SaveReturn1(struct dl_phdr_info *info, [[maybe_unused]] size_t info_size,
+                void *arg) {
+  *static_cast<void **>(arg) = info;
+  return 1;
+}
+
+TEST(LlvmLibcLinkDlIteratePhdrTest, OnlyExecutable) {
+  struct dl_phdr_info executable_info;
+  EXPECT_EQ(LIBC_NAMESPACE::dl_iterate_phdr(SaveReturn1, &executable_info), 1);
+  int program_header_count = executable_info.dlpi_phnum;
----------------
kaladron wrote:

In the best case, I think this is a stack use after return.  SaveReturn1 is taking the address of the stack-local variable in dl_iterate_phdr, so this address is no longer valid by the time it gets here.  The static_cast in SaveReturn1 needs to be a copy rather than a pointer.

In the worst case, I think the test function just put a pointer address into the first 8 bytes of the struct.

(Sorry, tracing this by hand, so I mighit have it confused!)

https://github.com/llvm/llvm-project/pull/194196


More information about the libc-commits mailing list