[Lldb-commits] [lldb] [lldb] Don't crash on a .gnu_debuglink section without a file name (PR #228116)
Charles Zablit via lldb-commits
lldb-commits at lists.llvm.org
Fri Oct 2 08:48:11 PDT 2026
https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/228116
>From d3e7022c10faaf3ad4fb3e9b4a57baae3c92a760 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 1 Oct 2026 16:29:52 +0100
Subject: [PATCH 1/2] [lldb] Don't crash on a .gnu_debuglink section without a
file name
---
.../Plugins/ObjectFile/ELF/ObjectFileELF.cpp | 8 +-
.../ObjectFile/PECOFF/ObjectFilePECOFF.cpp | 5 +-
.../ELF/gnu-debuglink-without-file-name.yaml | 28 ++++++
.../gnu-debuglink-without-file-name.yaml | 89 +++++++++++++++++++
4 files changed, 126 insertions(+), 4 deletions(-)
create mode 100644 lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
create mode 100644 lldb/test/Shell/ObjectFile/PECOFF/gnu-debuglink-without-file-name.yaml
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index 5bb9f74b80e379..662f69c8e6df8c 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -1845,9 +1845,11 @@ size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
if (section_size && (data.SetData(object_data, sheader.sh_offset,
section_size) == section_size)) {
lldb::offset_t gnu_debuglink_offset = 0;
- gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
- gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
- data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
+ if (const char *file = data.GetCStr(&gnu_debuglink_offset)) {
+ gnu_debuglink_file = file;
+ gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
+ data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
+ }
}
}
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
index 985dd6b516bae9..3b90bcbc2bef6a 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -128,7 +128,10 @@ static bool GetDebugLinkContents(const llvm::object::COFFObjectFile &coff_obj,
content->data(), content->size(),
coff_obj.isLittleEndian() ? eByteOrderLittle : eByteOrderBig, 4);
lldb::offset_t gnu_debuglink_offset = 0;
- gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
+ const char *file = data.GetCStr(&gnu_debuglink_offset);
+ if (!file)
+ return false;
+ gnu_debuglink_file = file;
// Align to the next 4-byte offset
gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
diff --git a/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml b/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
new file mode 100644
index 00000000000000..c1610a8f2cf492
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
@@ -0,0 +1,28 @@
+# A .gnu_debuglink section whose file name is not NUL-terminated is not a debug
+# link.
+
+# RUN: yaml2obj %s -o %t
+# RUN: lldb-test object-file %t | FileCheck %s
+
+# CHECK: Plugin name: elf
+# CHECK: Name: .gnu_debuglink
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+ Entry: 0x00000000004003D0
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x00000000004003D0
+ AddressAlign: 0x0000000000000010
+ Content: DEADBEEFBAADF00D
+ - Name: .gnu_debuglink
+ Type: SHT_PROGBITS
+ AddressAlign: 0x0000000000000004
+ Content: '616263'
+...
diff --git a/lldb/test/Shell/ObjectFile/PECOFF/gnu-debuglink-without-file-name.yaml b/lldb/test/Shell/ObjectFile/PECOFF/gnu-debuglink-without-file-name.yaml
new file mode 100644
index 00000000000000..71de26c263e365
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/PECOFF/gnu-debuglink-without-file-name.yaml
@@ -0,0 +1,89 @@
+# A .gnu_debuglink section whose file name is empty or not NUL-terminated is
+# not a debug link. `llvm-objcopy --only-keep-debug` leaves an empty one when it
+# runs on a binary that already has a debug link.
+
+# RUN: yaml2obj --docnum=1 %s -o %t.empty
+# RUN: lldb-test object-file %t.empty | FileCheck %s
+# RUN: yaml2obj --docnum=2 %s -o %t.unterminated
+# RUN: lldb-test object-file %t.unterminated | FileCheck %s
+
+# CHECK: Plugin name: pe-coff
+# CHECK: Name: .gnu_debuglink
+
+--- !COFF
+OptionalHeader:
+ AddressOfEntryPoint: 5152
+ ImageBase: 5368709120
+ SectionAlignment: 4096
+ FileAlignment: 512
+ MajorOperatingSystemVersion: 6
+ MinorOperatingSystemVersion: 0
+ MajorImageVersion: 0
+ MinorImageVersion: 0
+ MajorSubsystemVersion: 6
+ MinorSubsystemVersion: 0
+ Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
+ DLLCharacteristics: [ IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE ]
+ SizeOfStackReserve: 1048576
+ SizeOfStackCommit: 4096
+ SizeOfHeapReserve: 1048576
+ SizeOfHeapCommit: 4096
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE ]
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 4096
+ VirtualSize: 64
+ SectionData: DEADBEEFBAADF00D
+ - Name: .data
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 8192
+ VirtualSize: 64
+ SectionData: DEADBEEFBAADF00D
+ - Name: .gnu_debuglink
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 16384
+ VirtualSize: 16
+ SectionData: ''
+symbols: []
+--- !COFF
+OptionalHeader:
+ AddressOfEntryPoint: 5152
+ ImageBase: 5368709120
+ SectionAlignment: 4096
+ FileAlignment: 512
+ MajorOperatingSystemVersion: 6
+ MinorOperatingSystemVersion: 0
+ MajorImageVersion: 0
+ MinorImageVersion: 0
+ MajorSubsystemVersion: 6
+ MinorSubsystemVersion: 0
+ Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
+ DLLCharacteristics: [ IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE ]
+ SizeOfStackReserve: 1048576
+ SizeOfStackCommit: 4096
+ SizeOfHeapReserve: 1048576
+ SizeOfHeapCommit: 4096
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE ]
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 4096
+ VirtualSize: 64
+ SectionData: DEADBEEFBAADF00D
+ - Name: .data
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 8192
+ VirtualSize: 64
+ SectionData: DEADBEEFBAADF00D
+ - Name: .gnu_debuglink
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+ VirtualAddress: 16384
+ VirtualSize: 16
+ SectionData: 616263
+symbols: []
+...
>From 1e38165401c9e11181e76f924d4d85a4169ac794 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 2 Oct 2026 17:47:49 +0200
Subject: [PATCH 2/2] fixup! [lldb] Don't crash on a .gnu_debuglink section
without a file name
---
.../ELF/gnu-debuglink-without-file-name.yaml | 28 ++++++++++++++++---
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml b/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
index c1610a8f2cf492..8671d459cb2bca 100644
--- a/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
+++ b/lldb/test/Shell/ObjectFile/ELF/gnu-debuglink-without-file-name.yaml
@@ -1,12 +1,32 @@
-# A .gnu_debuglink section whose file name is not NUL-terminated is not a debug
-# link.
+# A .gnu_debuglink section whose file name is empty or not NUL-terminated is
+# not a debug link.
-# RUN: yaml2obj %s -o %t
-# RUN: lldb-test object-file %t | FileCheck %s
+# RUN: yaml2obj --docnum=1 %s -o %t.empty
+# RUN: lldb-test object-file %t.empty | FileCheck %s
+# RUN: yaml2obj --docnum=2 %s -o %t.unterminated
+# RUN: lldb-test object-file %t.unterminated | FileCheck %s
# CHECK: Plugin name: elf
# CHECK: Name: .gnu_debuglink
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+ Entry: 0x00000000004003D0
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x00000000004003D0
+ AddressAlign: 0x0000000000000010
+ Content: DEADBEEFBAADF00D
+ - Name: .gnu_debuglink
+ Type: SHT_PROGBITS
+ AddressAlign: 0x0000000000000004
+ Content: ''
--- !ELF
FileHeader:
Class: ELFCLASS64
More information about the lldb-commits
mailing list