[lld] [lld][macho] Fix segfault while processing malformed object file. (PR #167025)

Prabhu Rajasekaran via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 08:28:27 PST 2025


https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/167025

>From e1ced6fbe18191dc2ac70ec1e9e3387a09140dac Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Fri, 7 Nov 2025 13:46:44 -0800
Subject: [PATCH 1/2] [lld][macho] Fix segfault while processing malformed
 object file.

---
 lld/MachO/InputFiles.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 20e4a1d755229..add59272d9f67 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -808,6 +808,11 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
       continue;
 
     if ((sym.n_type & N_TYPE) == N_SECT) {
+      if (sym.n_sect == 0) {
+        error("Section symbol " + StringRef(strtab + sym.n_strx) + " in " +
+              toString(this) + " has an invalid section index of 0");
+        llvm_unreachable("Section symbol without an associated section.");
+      }
       Subsections &subsections = sections[sym.n_sect - 1]->subsections;
       // parseSections() may have chosen not to parse this section.
       if (subsections.empty())

>From 5e2c87b42bc5b0d0eefb1ac5a1476ff80aef59e8 Mon Sep 17 00:00:00 2001
From: Prabhu Rajasekaran <prabhukrllvm at gmail.com>
Date: Mon, 10 Nov 2025 08:28:19 -0800
Subject: [PATCH 2/2] Emit fatal error instead of using error llvm_unreachable.

Co-authored-by: Ellis Hoag <ellis.sparky.hoag at gmail.com>
---
 lld/MachO/InputFiles.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index add59272d9f67..a3e5f72724009 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -809,9 +809,8 @@ void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
 
     if ((sym.n_type & N_TYPE) == N_SECT) {
       if (sym.n_sect == 0) {
-        error("Section symbol " + StringRef(strtab + sym.n_strx) + " in " +
+        fatal("Section symbol " + StringRef(strtab + sym.n_strx) + " in " +
               toString(this) + " has an invalid section index of 0");
-        llvm_unreachable("Section symbol without an associated section.");
       }
       Subsections &subsections = sections[sym.n_sect - 1]->subsections;
       // parseSections() may have chosen not to parse this section.



More information about the llvm-commits mailing list