[llvm] [PDB] Add public symbol lookup by address (PR #157361)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 7 13:02:17 PDT 2025
https://github.com/Nerixyz created https://github.com/llvm/llvm-project/pull/157361
This adds a method on the `PublicsStream` to look up symbols using their address (segment + offset).
It's largely a reimplementation of [`NearestSym`](https://github.com/microsoft/microsoft-pdb/blob/805655a28bd8198004be2ac27e6e0290121a5e89/PDB/dbi/gsi.cpp#L1492-L1581) from the reference. However, we don't return the nearest symbol, but the exact symbol.
Still, in case of ICF, we return the symbol that's first in the address map. Users can then use the returned offset to read the next records to check if multiple symbols overlap, if desired.
>From #149701.
>From d76099b00cde91af9e69f2308e45f031f67e1194 Mon Sep 17 00:00:00 2001
From: Nerixyz <nerixdev at outlook.de>
Date: Sun, 7 Sep 2025 20:30:27 +0200
Subject: [PATCH] [PDB] Add public symbol lookup by address
---
.../llvm/DebugInfo/PDB/Native/PublicsStream.h | 18 ++++
.../DebugInfo/PDB/Native/PublicsStream.cpp | 91 ++++++++++++++++++
llvm/unittests/DebugInfo/PDB/CMakeLists.txt | 1 +
.../DebugInfo/PDB/Inputs/PublicSymbols.cpp | 46 +++++++++
.../DebugInfo/PDB/Inputs/PublicSymbols.pdb | Bin 0 -> 53248 bytes
.../DebugInfo/PDB/PublicsStreamTest.cpp | 61 ++++++++++++
6 files changed, 217 insertions(+)
create mode 100644 llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.cpp
create mode 100644 llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.pdb
create mode 100644 llvm/unittests/DebugInfo/PDB/PublicsStreamTest.cpp
diff --git a/llvm/include/llvm/DebugInfo/PDB/Native/PublicsStream.h b/llvm/include/llvm/DebugInfo/PDB/Native/PublicsStream.h
index 2cb4bee8ca5df..c5fdad057e867 100644
--- a/llvm/include/llvm/DebugInfo/PDB/Native/PublicsStream.h
+++ b/llvm/include/llvm/DebugInfo/PDB/Native/PublicsStream.h
@@ -18,9 +18,13 @@ namespace llvm {
namespace msf {
class MappedBlockStream;
}
+namespace codeview {
+class PublicSym32;
+}
namespace pdb {
struct PublicsStreamHeader;
struct SectionOffset;
+class SymbolStream;
class PublicsStream {
public:
@@ -42,6 +46,20 @@ class PublicsStream {
return SectionOffsets;
}
+ /// Find a public symbol by a segment and offset.
+ ///
+ /// In case there is more than one symbol (for example due to ICF), the first
+ /// one is returned.
+ ///
+ /// \return If a symbol was found, the symbol at the provided address is
+ /// returned as well as the index of this symbol in the address map. If
+ /// the binary was linked with ICF, there might be more symbols with the
+ /// same address after the returned one. If no symbol is found,
+ /// `std::nullopt` is returned.
+ LLVM_ABI std::optional<std::pair<codeview::PublicSym32, size_t>>
+ findByAddress(const SymbolStream &Symbols, uint16_t Segment,
+ uint32_t Offset) const;
+
private:
std::unique_ptr<msf::MappedBlockStream> Stream;
GSIHashTable PublicsTable;
diff --git a/llvm/lib/DebugInfo/PDB/Native/PublicsStream.cpp b/llvm/lib/DebugInfo/PDB/Native/PublicsStream.cpp
index c350e0e0b3e19..984e6e70adba2 100644
--- a/llvm/lib/DebugInfo/PDB/Native/PublicsStream.cpp
+++ b/llvm/lib/DebugInfo/PDB/Native/PublicsStream.cpp
@@ -22,9 +22,12 @@
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
+#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
+#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/DebugInfo/PDB/Native/RawError.h"
#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
+#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/Error.h"
#include <cstdint>
@@ -96,3 +99,91 @@ Error PublicsStream::reload() {
"Corrupted publics stream.");
return Error::success();
}
+
+static uint32_t compareSegmentOffset(uint16_t LhsSegment, uint32_t LhsOffset,
+ uint16_t RhsSegment, uint32_t RhsOffset) {
+ if (LhsSegment == RhsSegment)
+ return LhsOffset - RhsOffset;
+ return LhsSegment - RhsSegment;
+}
+
+static uint32_t compareSegmentOffset(uint16_t LhsSegment, uint32_t LhsOffst,
+ const codeview::PublicSym32 &Rhs) {
+ return compareSegmentOffset(LhsSegment, LhsOffst, Rhs.Segment, Rhs.Offset);
+}
+
+// This is a reimplementation of NearestSym:
+// https://github.com/microsoft/microsoft-pdb/blob/805655a28bd8198004be2ac27e6e0290121a5e89/PDB/dbi/gsi.cpp#L1492-L1581
+std::optional<std::pair<codeview::PublicSym32, size_t>>
+PublicsStream::findByAddress(const SymbolStream &Symbols, uint16_t Segment,
+ uint32_t Offset) const {
+ // The address map is sorted by address, so we do binary search.
+ // Each element is an offset into the symbols for a public symbol.
+ auto Lo = AddressMap.begin();
+ auto Hi = AddressMap.end();
+ Hi -= 1;
+
+ while (Lo < Hi) {
+ auto Cur = Lo + ((Hi - Lo + 1) / 2);
+ auto Sym = Symbols.readRecord(Cur->value());
+ if (Sym.kind() != codeview::S_PUB32)
+ return std::nullopt; // this is most likely corrupted debug info
+
+ auto Psym =
+ codeview::SymbolDeserializer::deserializeAs<codeview::PublicSym32>(Sym);
+ if (!Psym) {
+ consumeError(Psym.takeError());
+ return std::nullopt;
+ }
+
+ uint32_t Cmp = compareSegmentOffset(Segment, Offset, *Psym);
+ if (Cmp < 0) {
+ Cur -= 1;
+ Hi = Cur;
+ } else if (Cmp == 0)
+ Lo = Hi = Cur;
+ else
+ Lo = Cur;
+ }
+
+ auto Sym = Symbols.readRecord(Lo->value());
+ if (Sym.kind() != codeview::S_PUB32)
+ return std::nullopt; // this is most likely corrupted debug info
+
+ auto MaybePsym =
+ codeview::SymbolDeserializer::deserializeAs<codeview::PublicSym32>(Sym);
+ if (!MaybePsym) {
+ consumeError(MaybePsym.takeError());
+ return std::nullopt;
+ }
+ codeview::PublicSym32 Psym = std::move(*MaybePsym);
+
+ uint32_t Cmp = compareSegmentOffset(Segment, Offset, Psym);
+ if (Cmp != 0)
+ return std::nullopt;
+
+ // We found a symbol. Due to ICF, multiple symbols can have the same
+ // address, so return the first one
+ while (Lo != AddressMap.begin()) {
+ --Lo;
+ Sym = Symbols.readRecord(Lo->value());
+ if (Sym.kind() != codeview::S_PUB32)
+ return std::nullopt;
+ MaybePsym =
+ codeview::SymbolDeserializer::deserializeAs<codeview::PublicSym32>(Sym);
+ if (!MaybePsym) {
+ consumeError(MaybePsym.takeError());
+ return std::nullopt;
+ }
+
+ if (MaybePsym->Segment != Segment || MaybePsym->Offset != Offset) {
+ ++Lo;
+ break;
+ }
+
+ Psym = std::move(*MaybePsym);
+ }
+
+ std::ptrdiff_t IterOffset = Lo - AddressMap.begin();
+ return std::pair{Psym, static_cast<size_t>(IterOffset)};
+}
diff --git a/llvm/unittests/DebugInfo/PDB/CMakeLists.txt b/llvm/unittests/DebugInfo/PDB/CMakeLists.txt
index ba2a732848f4d..b1b9d2d98c944 100644
--- a/llvm/unittests/DebugInfo/PDB/CMakeLists.txt
+++ b/llvm/unittests/DebugInfo/PDB/CMakeLists.txt
@@ -11,6 +11,7 @@ add_llvm_unittest_with_input_files(DebugInfoPDBTests
StringTableBuilderTest.cpp
PDBApiTest.cpp
PDBVariantTest.cpp
+ PublicsStreamTest.cpp
)
target_link_libraries(DebugInfoPDBTests PRIVATE LLVMTestingSupport)
diff --git a/llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.cpp b/llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.cpp
new file mode 100644
index 0000000000000..0aeab04543caf
--- /dev/null
+++ b/llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.cpp
@@ -0,0 +1,46 @@
+// clang-format off
+
+// Compile with
+// cl /Z7 /GR- /GS- PublicSymbols.cpp -c /Gy
+// link .\PublicSymbols.obj /DEBUG /NODEFAULTLIB /out:PublicSymbols.exe /ENTRY:main /OPT:ICF
+// llvm-pdbutil pdb2yaml --publics-stream PublicSymbols.pdb > PublicSymbols.yaml
+// llvm-pdbutil yaml2pdb PublicSymbols.yaml
+//
+// rm PublicSymbols.exe && rm PublicSymbols.obj && rm PublicSymbols.yaml
+
+int foobar(int i){ return i + 1; }
+// these should be merged with ICF
+int dup1(int i){ return i + 2; }
+int dup2(int i){ return i + 2; }
+int dup3(int i){ return i + 2; }
+
+class AClass {
+public:
+ void AMethod(int, char*) {}
+ static bool Something(char c) {
+ return c == ' ';
+ }
+};
+
+struct Base {
+ virtual ~Base() = default;
+};
+struct Derived : public Base {};
+struct Derived2 : public Base {};
+struct Derived3 : public Derived2, public Derived {};
+
+int AGlobal;
+
+void operator delete(void *,unsigned __int64) {}
+
+int main() {
+ foobar(1);
+ dup1(1);
+ dup2(1);
+ dup3(1);
+ AClass a;
+ a.AMethod(1, nullptr);
+ AClass::Something(' ');
+ Derived3 d3;
+ return AGlobal;
+}
diff --git a/llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.pdb b/llvm/unittests/DebugInfo/PDB/Inputs/PublicSymbols.pdb
new file mode 100644
index 0000000000000000000000000000000000000000..ffa3275d58d7b77cb6aad0b89ffb0dceeb1ad39e
GIT binary patch
literal 53248
zcmeI*PiP!v9LDk4G-}g75~WC*gN!1gR+?P{O#(t at la1M8A}MKUDnhcF#4K)hC7Y at T
z4>@@7(1HgK3LfIkgMx>8_n;SvptpL9;8AG>3m$6q`OUnuvzg6e2(%{g`@*yD?!3Qw
z_npsf4zrs~Cd;p_)atdxRe!W^^wcSTa{2{-;B+>7{77$M+EeTHybg88l?i*Sb|~t&
zMF0T=5I_I{1Q0*~0R#|00D*lX at HY(s1Q0*~0R#|0009ILKmY**4!nT3dEgI`14IA;
z1Q0*~0R#|0009ILK;VBbaBQFao&w86009ILKmY**5I_I{1Q0*~f!zz(2Ozy&eZSib
zo%V<RGz1Vp009ILKmY**5I_I{1Q0lI0`Kc$e($z9s`#^kKQ+Fl&jyxNo6-HNx^I2S
z^+TP}<L|Z6wm<FaK6Q^TyZ&U{yZW+ft^StlhurvAZeAO+eCy`>%k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b@|GmJ6r*n;8+uukvyTD!FM7jFL+3I4=>#LTQ7wWnQo&HgvVtd&i3|Kp&bcOdk
z?>^VlA%Fk^2q1s}0tg_000IagfWV#+uv2$ymlV4WVBPiVyc0Ut_jRs6q1fO2A64S(
p1K57{F at T-!+s6R$>A&vM5I_I{1Q0*~0R#|0009ILKwysv`~zW}Izs>e
literal 0
HcmV?d00001
diff --git a/llvm/unittests/DebugInfo/PDB/PublicsStreamTest.cpp b/llvm/unittests/DebugInfo/PDB/PublicsStreamTest.cpp
new file mode 100644
index 0000000000000..708b42a3c51b1
--- /dev/null
+++ b/llvm/unittests/DebugInfo/PDB/PublicsStreamTest.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
+#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
+#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
+#include "llvm/Support/BinaryByteStream.h"
+#include "llvm/Support/MemoryBuffer.h"
+
+#include "llvm/Testing/Support/SupportHelpers.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::pdb;
+
+extern const char *TestMainArgv0;
+
+static std::string getExePath() {
+ SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
+ llvm::sys::path::append(InputsDir, "PublicSymbols.pdb");
+ return std::string(InputsDir);
+}
+
+TEST(PublicsStreamTest, FindByAddress) {
+ std::string ExePath = getExePath();
+ auto Buffer = MemoryBuffer::getFile(ExePath, /*IsText=*/false,
+ /*RequiresNullTerminator=*/false);
+ ASSERT_TRUE(bool(Buffer));
+ auto Stream = std::make_unique<MemoryBufferByteStream>(
+ std::move(*Buffer), llvm::endianness::little);
+
+ BumpPtrAllocator Alloc;
+ PDBFile File(ExePath, std::move(Stream), Alloc);
+ ASSERT_FALSE(bool(File.parseFileHeaders()));
+ ASSERT_FALSE(bool(File.parseStreamData()));
+
+ auto Publics = File.getPDBPublicsStream();
+ ASSERT_TRUE(bool(Publics));
+ auto Symbols = File.getPDBSymbolStream();
+ ASSERT_TRUE(bool(Symbols));
+
+ auto VTableDerived = Publics->findByAddress(*Symbols, 2, 8);
+ ASSERT_TRUE(VTableDerived.has_value());
+ // both derived and derived2 have their vftables there - but derived2 is first (due to ICF)
+ ASSERT_EQ(VTableDerived->first.Name, "??_7Derived2@@6B@");
+ ASSERT_EQ(VTableDerived->second, 26);
+
+ ASSERT_FALSE(Publics->findByAddress(*Symbols, 2, 7).has_value());
+ ASSERT_FALSE(Publics->findByAddress(*Symbols, 2, 9).has_value());
+
+ auto GlobalSym = Publics->findByAddress(*Symbols, 3, 0);
+ ASSERT_TRUE(GlobalSym.has_value());
+ ASSERT_EQ(GlobalSym->first.Name, "?AGlobal@@3HA");
+ ASSERT_EQ(GlobalSym->second, 30);
+}
More information about the llvm-commits
mailing list