[libc-commits] [libc] linux: add support to parse PT_GNU_PROPERTY (PR #174772)
via libc-commits
libc-commits at lists.llvm.org
Thu Jan 8 02:52:58 PST 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- libc/startup/linux/gnu_property_section.cpp libc/startup/linux/gnu_property_section.h libc/startup/linux/do_start.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/startup/linux/gnu_property_section.cpp b/libc/startup/linux/gnu_property_section.cpp
index ab2418a49..c905706f6 100644
--- a/libc/startup/linux/gnu_property_section.cpp
+++ b/libc/startup/linux/gnu_property_section.cpp
@@ -7,10 +7,10 @@
//===----------------------------------------------------------------------===//
#include "startup/linux/gnu_property_section.h"
-#include "src/__support/macros/config.h"
+#include "include/llvm-libc-macros/link-macros.h"
#include "src/__support/CPP/string_view.h"
+#include "src/__support/macros/config.h"
#include "src/string/memory_utils/utils.h"
-#include "include/llvm-libc-macros/link-macros.h"
#include <linux/elf.h>
@@ -27,85 +27,90 @@
namespace LIBC_NAMESPACE_DECL {
- bool GnuPropertySection::Parse(const ElfW(Phdr)* gnu_property_phdr, const ElfW(Addr) base) {
- if (!gnu_property_phdr) {
- return false;
- }
+bool GnuPropertySection::Parse(const ElfW(Phdr) * gnu_property_phdr,
+ const ElfW(Addr) base) {
+ if (!gnu_property_phdr) {
+ return false;
+ }
- const auto note_nhdr_size = gnu_property_phdr->p_memsz;
- // Sanity check we are using the correct phdr and the memory size is large
- // enough to fit the program property note.
- if (gnu_property_phdr->p_type != PT_GNU_PROPERTY ||
- note_nhdr_size < sizeof(ElfW(ProgramPropertyNote))) {
- return false;
- }
+ const auto note_nhdr_size = gnu_property_phdr->p_memsz;
+ // Sanity check we are using the correct phdr and the memory size is large
+ // enough to fit the program property note.
+ if (gnu_property_phdr->p_type != PT_GNU_PROPERTY ||
+ note_nhdr_size < sizeof(ElfW(ProgramPropertyNote))) {
+ return false;
+ }
- const ElfW(ProgramPropertyNote) *note_nhdr =
- reinterpret_cast<ElfW(ProgramPropertyNote)*>(base + gnu_property_phdr->p_vaddr);
- if (!note_nhdr) {
- return false;
- }
+ const ElfW(ProgramPropertyNote) *note_nhdr =
+ reinterpret_cast<ElfW(ProgramPropertyNote) *>(base +
+ gnu_property_phdr->p_vaddr);
+ if (!note_nhdr) {
+ return false;
+ }
- const ElfW(Word) nhdr_desc_size = note_nhdr->nhdr.n_descsz;
+ const ElfW(Word) nhdr_desc_size = note_nhdr->nhdr.n_descsz;
- // sizeof(*note_nhdr) does not include the size of n_desc,
- // since it is not known at compile time.
- // The size of it combined with n_descsz cannot exceed the total size of the
- // program property note.
- if ((sizeof(*note_nhdr) + nhdr_desc_size) > note_nhdr_size) {
- return false;
+ // sizeof(*note_nhdr) does not include the size of n_desc,
+ // since it is not known at compile time.
+ // The size of it combined with n_descsz cannot exceed the total size of the
+ // program property note.
+ if ((sizeof(*note_nhdr) + nhdr_desc_size) > note_nhdr_size) {
+ return false;
+ }
+
+ if (note_nhdr->nhdr.n_namesz != 4 ||
+ note_nhdr->nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
+ cpp::string_view(reinterpret_cast<const char *>(note_nhdr->n_name), 4) !=
+ cpp::string_view("GNU", 4)) {
+ return false;
+ }
+
+ // program property note is valid, we can parse the program property array.
+ ElfW(Word) offset = 0;
+ while (offset < nhdr_desc_size) {
+ if ((nhdr_desc_size - offset) < sizeof(ElfW(ProgramProperty))) {
+ // We can no longer even fit the statically known size of
+ // ProgramProperty. Doing the cast and reading pr_type/pr_datasz is no
+ // longer in bounds.
+ break;
}
+ const ElfW(ProgramProperty) *property =
+ reinterpret_cast<const ElfW(ProgramProperty) *>(
+ ¬e_nhdr->n_desc[offset]);
- if (note_nhdr->nhdr.n_namesz != 4 ||
- note_nhdr->nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
- cpp::string_view(reinterpret_cast<const char *>(note_nhdr->n_name), 4) !=
- cpp::string_view("GNU", 4)) {
+ // Sanity check that property is correctly aligned.
+ if (distance_to_align_up<sizeof(ElfW(Word))>(property) > 0) {
return false;
}
- // program property note is valid, we can parse the program property array.
- ElfW(Word) offset = 0;
- while (offset < nhdr_desc_size) {
- if ((nhdr_desc_size - offset) < sizeof(ElfW(ProgramProperty))) {
- // We can no longer even fit the statically known size of
- // ProgramProperty. Doing the cast and reading pr_type/pr_datasz is no
- // longer in bounds.
- break;
- }
- const ElfW(ProgramProperty) *property =
- reinterpret_cast<const ElfW(ProgramProperty)*>(¬e_nhdr->n_desc[offset]);
-
- // Sanity check that property is correctly aligned.
- if (distance_to_align_up<sizeof(ElfW(Word))>(property) > 0) {
- return false;
- }
-
- const ElfW(Xword) property_size = sizeof(*property) + property->pr_datasz;
- // Also check that pr_data does not reach out of bounds.
- if ((offset + property_size) > nhdr_desc_size) {
- return false;
- }
+ const ElfW(Xword) property_size = sizeof(*property) + property->pr_datasz;
+ // Also check that pr_data does not reach out of bounds.
+ if ((offset + property_size) > nhdr_desc_size) {
+ return false;
+ }
- switch (property->pr_type) {
+ switch (property->pr_type) {
#ifdef LIBC_TARGET_ARCH_IS_X86_64
- case GNU_PROPERTY_X86_FEATURE_1_AND: {
- // PR_DATASZ should always be 4 bytes, for both 32bit and 64bit.
- if (property->pr_datasz != 4) {
- return false;
- }
- const uint32_t feature_bitmap = *reinterpret_cast<const uint32_t*>(&property->pr_data[0]);
- features_.shstk_supported = (feature_bitmap & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
- break;
+ case GNU_PROPERTY_X86_FEATURE_1_AND: {
+ // PR_DATASZ should always be 4 bytes, for both 32bit and 64bit.
+ if (property->pr_datasz != 4) {
+ return false;
}
+ const uint32_t feature_bitmap =
+ *reinterpret_cast<const uint32_t *>(&property->pr_data[0]);
+ features_.shstk_supported =
+ (feature_bitmap & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
+ break;
+ }
#endif
- default:
- break;
- }
-
- offset += property_size;
+ default:
+ break;
}
- return true;
+ offset += property_size;
}
+ return true;
+}
+
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/startup/linux/gnu_property_section.h b/libc/startup/linux/gnu_property_section.h
index 483000838..0c2cf70ad 100644
--- a/libc/startup/linux/gnu_property_section.h
+++ b/libc/startup/linux/gnu_property_section.h
@@ -8,12 +8,12 @@
#ifndef LLVM_LIBC_STARTUP_LINUX_GNU_PROPERTY_SECTION_H
#define LLVM_LIBC_STARTUP_LINUX_GNU_PROPERTY_SECTION_H
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/attributes.h"
#include "include/llvm-libc-macros/link-macros.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
-#include <stddef.h>
#include <linux/elf.h>
+#include <stddef.h>
namespace LIBC_NAMESPACE_DECL {
@@ -78,7 +78,7 @@ private:
public:
LIBC_INLINE GnuPropertySection() = default;
- bool Parse(const ElfW(Phdr)* gnu_property_phdr, const ElfW(Addr) base);
+ bool Parse(const ElfW(Phdr) * gnu_property_phdr, const ElfW(Addr) base);
#ifdef LIBC_TARGET_ARCH_IS_X86_64
LIBC_INLINE bool IsSHSTKSupported() const {
``````````
</details>
https://github.com/llvm/llvm-project/pull/174772
More information about the libc-commits
mailing list