[lldb] [llvm] [LLDB] Warn about truncated DWARF section names on Windows (PR #145175)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 23 08:47:37 PDT 2025
https://github.com/Nerixyz updated https://github.com/llvm/llvm-project/pull/145175
>From 72f30f6d2a1dfb5523bafd4a535b078b3de5cfc6 Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Sat, 21 Jun 2025 17:36:58 +0200
Subject: [PATCH] [LLDB] Warn about truncated DWARF section names on Windows
---
.../ObjectFile/PECOFF/ObjectFilePECOFF.cpp | 16 +++++++++
.../Shell/ObjectFile/PECOFF/lit.local.cfg | 2 +-
.../Shell/ObjectFile/PECOFF/truncated-dwarf.c | 7 ++++
lldb/test/Shell/helper/build.py | 33 ++++++++++++++++---
llvm/docs/ReleaseNotes.md | 2 ++
5 files changed, 54 insertions(+), 6 deletions(-)
create mode 100644 lldb/test/Shell/ObjectFile/PECOFF/truncated-dwarf.c
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
index 4984445dcbab9..e4e59a8c7bdd5 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -1036,12 +1036,18 @@ void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
m_sections_up->AddSection(header_sp);
unified_section_list.AddSection(header_sp);
+ std::vector<llvm::StringRef> truncated_dwarf_sections;
const uint32_t nsects = m_sect_headers.size();
for (uint32_t idx = 0; idx < nsects; ++idx) {
llvm::StringRef sect_name = GetSectionName(m_sect_headers[idx]);
ConstString const_sect_name(sect_name);
SectionType section_type = GetSectionType(sect_name, m_sect_headers[idx]);
+ // Detect unknown sections matching ".debug_*"
+ if (section_type == eSectionTypeOther && sect_name.size() == 8 &&
+ sect_name.starts_with(".debug_"))
+ truncated_dwarf_sections.emplace_back(sect_name);
+
SectionSP section_sp(new Section(
module_sp, // Module to which this section belongs
this, // Object file to which this section belongs
@@ -1071,6 +1077,16 @@ void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
m_sections_up->AddSection(section_sp);
unified_section_list.AddSection(section_sp);
}
+
+ if (!truncated_dwarf_sections.empty())
+ module_sp->ReportWarning(
+ "contains {} DWARF sections with truncated names ({}).\nWindows "
+ "executable (PECOFF) images produced by the default link.exe don't "
+ "include the required section names. A third party linker like "
+ "lld-link is required (compile with -fuse-ld=lld-link when using "
+ "Clang).",
+ truncated_dwarf_sections.size(),
+ llvm::join(truncated_dwarf_sections, ", "));
}
}
diff --git a/lldb/test/Shell/ObjectFile/PECOFF/lit.local.cfg b/lldb/test/Shell/ObjectFile/PECOFF/lit.local.cfg
index 9ef350be1dee2..1ae00d07fc3e6 100644
--- a/lldb/test/Shell/ObjectFile/PECOFF/lit.local.cfg
+++ b/lldb/test/Shell/ObjectFile/PECOFF/lit.local.cfg
@@ -1 +1 @@
-config.suffixes = ['.yaml', '.test']
+config.suffixes = ['.yaml', '.test', '.c']
diff --git a/lldb/test/Shell/ObjectFile/PECOFF/truncated-dwarf.c b/lldb/test/Shell/ObjectFile/PECOFF/truncated-dwarf.c
new file mode 100644
index 0000000000000..43dc252739ebc
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/PECOFF/truncated-dwarf.c
@@ -0,0 +1,7 @@
+// REQUIRES: target-windows
+// RUN: %build --compiler=clang-cl --force-dwarf-symbols --force-ms-link -o %t.exe -- %s
+// RUN: %lldb -f %t.exe 2>&1 | FileCheck %s
+
+int main(void) {}
+
+// CHECK: warning: {{.*}} contains 4 DWARF sections with truncated names (.debug_{{[a-z]}}, .debug_{{[a-z]}}, .debug_{{[a-z]}}, .debug_{{[a-z]}})
diff --git a/lldb/test/Shell/helper/build.py b/lldb/test/Shell/helper/build.py
index caaa14f90af1c..c73aa8a11b396 100755
--- a/lldb/test/Shell/helper/build.py
+++ b/lldb/test/Shell/helper/build.py
@@ -173,6 +173,22 @@
help="Specify the C/C++ standard.",
)
+parser.add_argument(
+ "--force-dwarf-symbols",
+ dest="force_dwarf_symbols",
+ action="store_true",
+ default=False,
+ help="When compiling with clang-cl on Windows, use DWARF instead of CodeView",
+)
+
+parser.add_argument(
+ "--force-ms-link",
+ dest="force_ms_link",
+ action="store_true",
+ default=False,
+ help="When compiling with clang-cl on Windows, always use link.exe",
+)
+
args = parser.parse_args(args=sys.argv[1:])
@@ -379,15 +395,20 @@ def __init__(self, toolchain_type, args):
)
if self.mode == "link" or self.mode == "compile-and-link":
- self.linker = (
- self._find_linker("link")
- if toolchain_type == "msvc"
- else self._find_linker("lld-link", args.tools_dir)
- )
+ if toolchain_type == "msvc" or args.force_ms_link:
+ search_paths = []
+ if toolchain_type != "msvc":
+ search_paths.append(
+ os.path.dirname(find_executable("cl", args.tools_dir))
+ )
+ self.linker = self._find_linker("link", search_paths)
+ else:
+ self.linker = self._find_linker("lld-link", args.tools_dir)
if not self.linker:
raise ValueError("Unable to find an appropriate linker.")
self.compile_env, self.link_env = self._get_visual_studio_environment()
+ self.force_dwarf_symbols = args.force_dwarf_symbols
def _find_linker(self, name, search_paths=[]):
compiler_dir = os.path.dirname(self.compiler)
@@ -678,6 +699,8 @@ def _get_compilation_command(self, source, obj):
args.append("/GR-")
args.append("/Z7")
if self.toolchain_type == "clang-cl":
+ if self.force_dwarf_symbols:
+ args.append("-gdwarf")
args.append("-Xclang")
args.append("-fkeep-static-consts")
args.append("-fms-compatibility-version=19")
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 0395f43c61953..1ee7ccde985f5 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -299,6 +299,8 @@ Changes to LLDB
stop reason = SIGSEGV: sent by tkill system call (sender pid=649752, uid=2667987)
```
* ELF Cores can now have their siginfo structures inspected using `thread siginfo`.
+* LLDB now detects when the names of known DWARF sections in a PECOFF file have
+ been truncated by link.exe on Windows, and advises the user how to avoid this.
### Changes to lldb-dap
More information about the llvm-commits
mailing list