Skip to content

Conversation

@Prabhuk
Copy link
Contributor

@Prabhuk Prabhuk commented Nov 7, 2025

Ran into a use case where we had a MachO object file with a section symbol which did not have a section associated with it segfaults during linking. This patch aims to handle such cases gracefully and avoid the linker from crashing.

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-lld-macho

Author: Prabhu Rajasekaran (Prabhuk)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/167025.diff

1 Files Affected:

  • (modified) lld/MachO/InputFiles.cpp (+5)
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())

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-lld

Author: Prabhu Rajasekaran (Prabhuk)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/167025.diff

1 Files Affected:

  • (modified) lld/MachO/InputFiles.cpp (+5)
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())

@Prabhuk Prabhuk requested a review from mysterymath November 7, 2025 22:55
@Prabhuk
Copy link
Contributor Author

Prabhuk commented Nov 7, 2025

Trying to add a test by generating a yaml file from the malformed object file that led to the segfault.

Co-authored-by: Ellis Hoag <ellis.sparky.hoag@gmail.com>
Copy link
Contributor

@mysterymath mysterymath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Prabhuk Prabhuk requested a review from lhames November 11, 2025 21:10
@Prabhuk Prabhuk merged commit abb8c4b into llvm:main Nov 11, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants