[llvm] [llvm][ELF]Add Shdr check for getBuildID(#126418) (PR #126537)

Ruoyu Qiu via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 11 00:32:23 PST 2025


https://github.com/cabbaken updated https://github.com/llvm/llvm-project/pull/126537

>From a19e1a05cde04298aee6732811353d1814d21eb8 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Mon, 10 Feb 2025 15:56:31 +0000
Subject: [PATCH 1/2] [llvm-objdump][ELF]Add Shdr buildID check(#126418)

Add Section Header check for getBuildID, fix crash
with invalid Program Header.

Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
 llvm/lib/Object/BuildID.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/llvm/lib/Object/BuildID.cpp b/llvm/lib/Object/BuildID.cpp
index 89d6bc3ab550db8..d44605712982725 100644
--- a/llvm/lib/Object/BuildID.cpp
+++ b/llvm/lib/Object/BuildID.cpp
@@ -24,6 +24,20 @@ using namespace llvm::object;
 namespace {
 
 template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
+  auto Sections = cantFail(Obj.sections());
+  if (!Sections.empty()) {
+    for (const auto &S : Sections) {
+      if (S.sh_type != ELF::SHT_NOTE)
+        continue;
+      Error Err = Error::success();
+      for (auto N : Obj.notes(S, Err))
+        if (N.getType() == ELF::NT_GNU_BUILD_ID &&
+            N.getName() == ELF::ELF_NOTE_GNU)
+          return N.getDesc(S.sh_addralign);
+      consumeError(std::move(Err));
+    }
+  }
+
   auto PhdrsOrErr = Obj.program_headers();
   if (!PhdrsOrErr) {
     consumeError(PhdrsOrErr.takeError());

>From 169f9db3abaf765d58a4f7fd3e8cbc44469fafbe Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Tue, 11 Feb 2025 08:29:19 +0000
Subject: [PATCH 2/2] [LLVM][ELF]Add Align check for getDesc()

Add Aligh check to avoid crashing if Aligh is 0.

Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
 llvm/include/llvm/Object/ELFTypes.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 87e4dbe44809107..84410099003169c 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -660,7 +660,7 @@ class Elf_Note_Impl {
 
   /// Get the note's descriptor.
   ArrayRef<uint8_t> getDesc(size_t Align) const {
-    if (!Nhdr.n_descsz)
+    if (!Nhdr.n_descsz || !Align)
       return ArrayRef<uint8_t>();
     return ArrayRef<uint8_t>(
         reinterpret_cast<const uint8_t *>(&Nhdr) +



More information about the llvm-commits mailing list