[lld] Support finding pdb files from outputpath (PR #94153)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 3 21:15:29 PDT 2024


https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/94153

>From 753d0551c8a41e63530963f40f918533060753b0 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Tue, 4 Jun 2024 12:14:33 +0800
Subject: [PATCH] Support finding pdb files from outputpath

---
 lld/COFF/InputFiles.cpp                       |  38 ++++++++++++------
 .../COFF/Inputs/pdb-from-outputpath-a.obj     | Bin 0 -> 1467 bytes
 .../COFF/Inputs/pdb-from-outputpath-a.pdb     | Bin 0 -> 61440 bytes
 .../COFF/Inputs/pdb-from-outputpath-b.obj     | Bin 0 -> 1100 bytes
 .../COFF/Inputs/pdb-from-outputpath-b.pdb     | Bin 0 -> 77824 bytes
 lld/test/COFF/pdb-from-outputpath.test        |  37 +++++++++++++++++
 6 files changed, 63 insertions(+), 12 deletions(-)
 create mode 100644 lld/test/COFF/Inputs/pdb-from-outputpath-a.obj
 create mode 100644 lld/test/COFF/Inputs/pdb-from-outputpath-a.pdb
 create mode 100644 lld/test/COFF/Inputs/pdb-from-outputpath-b.obj
 create mode 100644 lld/test/COFF/Inputs/pdb-from-outputpath-b.pdb
 create mode 100644 lld/test/COFF/pdb-from-outputpath.test

diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index 037fae45242c6..01e47dc45fe9a 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -735,8 +735,8 @@ void ObjFile::initializeFlags() {
             (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
       }
       if (sym->kind() == SymbolKind::S_OBJNAME) {
-        auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
-            sym.get()));
+        auto objName =
+            cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(sym.get()));
         if (objName.Signature)
           pchSignature = objName.Signature;
       }
@@ -818,11 +818,8 @@ void ObjFile::initializeDependencies() {
   debugTypesObj = makeTpiSource(ctx, this);
 }
 
-// Make a PDB path assuming the PDB is in the same folder as the OBJ
-static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
-  StringRef localPath =
-      !file->parentName.empty() ? file->parentName : file->getName();
-  SmallString<128> path = sys::path::parent_path(localPath);
+static std::string appendPdbPath(StringRef filePath, StringRef tSPath) {
+  SmallString<128> path = sys::path::parent_path(filePath);
 
   // Currently, type server PDBs are only created by MSVC cl, which only runs
   // on Windows, so we can assume type server paths are Windows style.
@@ -831,6 +828,20 @@ static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
   return std::string(path);
 }
 
+// Make a PDB path assuming the PDB is in the same folder as the OBJ
+static std::string getPdbBaseNameFromObj(ObjFile *file, StringRef tSPath) {
+  StringRef localPath =
+      !file->parentName.empty() ? file->parentName : file->getName();
+
+  return appendPdbPath(localPath, tSPath);
+}
+
+// Make a PDB path assuming the PDB is in the same folder as the OutputFilePath
+static std::string getPdbBaseNameFromOutput(StringRef outputFilePath,
+                                            StringRef tSPath) {
+  return appendPdbPath(outputFilePath, tSPath);
+}
+
 // The casing of the PDB path stamped in the OBJ can differ from the actual path
 // on disk. With this, we ensure to always use lowercase as a key for the
 // pdbInputFileInstances map, at least on Windows.
@@ -843,15 +854,18 @@ static std::string normalizePdbPath(StringRef path) {
 }
 
 // If existing, return the actual PDB path on disk.
-static std::optional<std::string> findPdbPath(StringRef pdbPath,
-                                              ObjFile *dependentFile) {
+static std::optional<std::string>
+findPdbPath(StringRef pdbPath, ObjFile *dependentFile, StringRef outputFile) {
   // Ensure the file exists before anything else. In some cases, if the path
   // points to a removable device, Driver::enqueuePath() would fail with an
   // error (EAGAIN, "resource unavailable try again") which we want to skip
   // silently.
   if (llvm::sys::fs::exists(pdbPath))
     return normalizePdbPath(pdbPath);
-  std::string ret = getPdbBaseName(dependentFile, pdbPath);
+  std::string ret = getPdbBaseNameFromObj(dependentFile, pdbPath);
+  if (llvm::sys::fs::exists(ret))
+    return normalizePdbPath(ret);
+  ret = getPdbBaseNameFromOutput(outputFile, pdbPath);
   if (llvm::sys::fs::exists(ret))
     return normalizePdbPath(ret);
   return std::nullopt;
@@ -865,7 +879,7 @@ PDBInputFile::~PDBInputFile() = default;
 PDBInputFile *PDBInputFile::findFromRecordPath(const COFFLinkerContext &ctx,
                                                StringRef path,
                                                ObjFile *fromFile) {
-  auto p = findPdbPath(path.str(), fromFile);
+  auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
   if (!p)
     return nullptr;
   auto it = ctx.pdbInputFileInstances.find(*p);
@@ -931,7 +945,7 @@ std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
 }
 
 void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
-  auto p = findPdbPath(path.str(), fromFile);
+  auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
   if (!p)
     return;
   auto it = ctx.pdbInputFileInstances.emplace(*p, nullptr);
diff --git a/lld/test/COFF/Inputs/pdb-from-outputpath-a.obj b/lld/test/COFF/Inputs/pdb-from-outputpath-a.obj
new file mode 100644
index 0000000000000000000000000000000000000000..e89a18971a659ccece124a0d8cd5ffb598f86edd
GIT binary patch
literal 1467
zcmbVMT}TvB6h5=Fsc4$BvJqx6+C(NgDn^N9th(xkt84z$L#w#EGpn2K&ayL_^h1Io
zJrq<AJ at gO+WkC`K_Fx1(^!r#32KCsJU<8&>L}usA&o)-{&|&VJ at 1FbJbI;s!=19|-
za9Qn-x|{%8!4OeO$q{W#Hm6XDpq#O0rq0d>QIf->gF?5pGK-w!I5Jtof-b=W`Hd#8
z$KnyFG&z<{=V|h|Cd3n#Z99TV<Xtzp6ao*O1LC+8(n1ysAPeE0Km1jB#N<ZaX5`Hw
z?~%zR<pJbHhKAMs`_c|;5xx7(A7ta%JYVT;ZfWTC_xKMt)|KF^slBIUqcPz3b~Lm%
zcbD+=`71b1<ERIQLj_HB!CqBPsliAj67<UIu%;w~nyhNo!B8+Mg)5>dC0?P7YDs)T
z+E7JERKh2q4i0iK0PqS5pcD$GAg$YDk(8n;QH?L{+RF!$S}Yzr6-x~AO-ei&8<A5+
z4E=a^8MNv#P#h$5y%Zy*-I0$kL*k)W0*b(uPuK7lY2+XeIF84q4BQo9qTql4dj5nK
z`yTR7LT8lg2mo(&g623TC>@(n)A<>w>8{9 at 4Otv{Zc!9i#d#?XJ at qW-`X&@SOR%dC
zI{S}3zc$~OELyvJrQYYgy;!_9`_=>S8M!=;e_pg0(epxOlL3A%7tfkQOlRl7yos{m
zYuffbZKK#b!0m20lQ;8lu)OVN*TVFSqje|6_aC{%jKhj=@<Z(lufUg~58p+<z%L;H
z-{jNUCoTjzIGn&Z8E}Sa!AP8e0CEwqxNaf(DEuC@?aGY0_Y^)ZWR|DfJ__>H-UiP-
z>HU{2OVzh(7W$4Z-gzlsI3m6|bl<&g_j0Ps^o at T+1S?03Nd;3*W__JJd7^Si6RWB&
zG0cQ%8B<2YxJ2m$!(#6lbQvr|U1Ouz`-KCSScW<V#r{j|y at I^640Vf*BHH={?PeJ&
zH9B?b#|_os4jRgQ^H`#KU7K?!kVWu!)L1m~lL7Y5f~AR`={`xS&z(PDAbMLd>m0E6
z87ySnMWsa1pRIUZjLH=2 at 6$P8?>d>tU>W<*V{F-hN*GO?j3p#Nk6+MRnt^`;W~uH_

literal 0
HcmV?d00001

diff --git a/lld/test/COFF/Inputs/pdb-from-outputpath-a.pdb b/lld/test/COFF/Inputs/pdb-from-outputpath-a.pdb
new file mode 100644
index 0000000000000000000000000000000000000000..c6a67e080a9c7bb0080856ba4e6d1e58e011150f
GIT binary patch
literal 61440
zcmeI*&ub%P7{KvolKxD8SPEO*MRX`4%5G+pv~-Jz+mafU8c~z2RYxJ4rrp6b6EYLF
zf~X*R@*;TFi!93?Ja|x6JoMnnzrn+vya+DsUS;d^Ce1d~RJV17rhZ at gWM-20ednFe
zbOP_pWZtY-toebz8rtde>9c3;Y%XJ8a1x1;*q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ohhb3?OdYixg0+)mU0tg_000IagfB*srAb<b at -77HkbJChM&DTD at dG6PB
zo$b>n{hQwew4VR}xXzmx&^V`I63_o{I`E1B0tg_000IagfB*srAb>zO2z;0k8F at tH
z^c$9Z|3OqXFGl3XorwIFk4iFQ$&cHXe5FtP|9Dd*`>Q2Ct%-d17WV{n!@i}B2q1s}
z0tg_000IagfB*srv;^9(|BveR|Ega2 at 85U*-`pQy!d(BK(=h3Y`xxts@&8ss`{<((
zb{?1SD1-4GFw7z2|FsPq$@qVa|JVF#;}rn}5I_I{1Q0*~ffFlW`t-KOqQ)%^ldkJI
zfYUlP_xjIim}dm)TQaCIq+!MeFy&$zBm at vZ009ILKmY**5I_I{1Q0m>0>j6 at iL?*_
r1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**{z2eRV}bfP

literal 0
HcmV?d00001

diff --git a/lld/test/COFF/Inputs/pdb-from-outputpath-b.obj b/lld/test/COFF/Inputs/pdb-from-outputpath-b.obj
new file mode 100644
index 0000000000000000000000000000000000000000..b13dc12b6972d646b8c99f58d272c76d9518ff8c
GIT binary patch
literal 1100
zcmbVLO=uHQ5T5O}8WSs7V-SLeB?j7Jv#B6GM8*8KO45i;L^Mddn at t+qbhp`U5fzCL
z{Glj03I!1_9xEsa_8`)OhhC}|K@>cA@!%mEsCcO}yV)l3;K5<%y?I~0%=>m`Cx5#S
zUTK#LNfuxqm?q>avZOD`ts?|5_+%Zd-R=Y-FN=#s-=qcA82|_RN{@;3;uZpDM9}4{
zy01KM@%k}XZ*eS)hqF_yd>IR)VDm4Ykx!bL)kcq5G#JFlhx)tB4#4w#AQn%Av&l^I
zbR_7-C7Q~3yTNoaHW5z6C%rt$ZJ=LAKM4#+>7v10R+B4QPLiZtOx9*~Ma}85rXS6T
zIW;f(3l*j8R~B^@pJ{!@FA9oy1%hybgDF%GE>!2L!Oos4Nfkv?3OYY{Zir8-dZ}Ex
zR+=yJQKhVw=H!a0;S%0G0Aq#*(m_UL>qFm*-s8l at v?3gW{q3U#n|p-Au5z3WPYiO>
zj0!!F=HLecyh$-u%1wsdg9Ao{cK}qW9(11_Y#fUT5{d#%JX6Vu%d_t at Hv>cJ_o{X)
zv34<a<VO7M+QQ4 at 8BbW;I`iQ3>M+1h;-UYun1W=~o`mj#B<H}po?*NP-2pFJk=?l4
z#D48y<M)z2+xT+KXIqAKYT^XA-5*xId_DhYIDY4&cCh*6!aEZCAI-!TL*wx7f_uR8
z+T)5n{O#PjzwoWU>3{q*yLtC{y|FraY6DXn7Lu at sLMiX6^)lPr04#(G^0Y3DjMN#X
z+Cr9;IiZ}#-vCUcbJ|EH%h0 at aTj!+FK(GwW8og`VI;V`Xvy9c$F}?Q2<Q3a=RKR14
bJ7<pau}r3eV}?S<ywL~9GSvQVqyPQ^q%+2B

literal 0
HcmV?d00001

diff --git a/lld/test/COFF/Inputs/pdb-from-outputpath-b.pdb b/lld/test/COFF/Inputs/pdb-from-outputpath-b.pdb
new file mode 100644
index 0000000000000000000000000000000000000000..18ea8cd7d7acb1b12b51d4b5531c09bb9249a107
GIT binary patch
literal 77824
zcmeI*OKcNI7{Kw*cAOV^m5NYMrLJ<It-u at Gi9*yvN#rCgN>hPs(x_TdIex&Zj at R<8
zMXGY((29G-4JoRqdg0JRPn1*DV{aTPB*Y=AxYJNhs3QHwhQxiO1qDKk{-b<5yY|dJ
zersFW?9A+(o2z=hR}8GwaO&`3Yc4%w9kt`}-tL1_Y0)$w64LBdjR_5tUTI`Z5kLR|
z1Q0*~0R#|0009ILK%hkgR{Q^0Cn10U0tg_000IagfB*srAb`NjEimxg%YA$t9Rdg-
zfB*srAb<b at 2q1s}0tl?HKfL?!u4&(IqA}TQxUjw+sV^8OGt-<8Npzpch45x+GdJUT
zVjr74rTN5!bW3=^^dIQa757PGZ|}Z{1oaBbV(RNQ!&+h^wf&=hXm5Srv`n9L4m9e9
zq^+sWMrCFYKmY**5I_I{1Q0*~0R#|0U`v6k--l)8%aBxl2uYWA_<zHVNaeT|=N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5!0A|A6-YoBa at I|4;k>7xcT#W)VOD
z0R#|0009ILcpd_m>_q6gf4XS;IN!XydS`y+$n?cq{{H({7H)LtYs2O2Qo*lx-8V%{
z7k-mJPSHm*w@;3kw at 3&efB*srAb<b at 2q1s}0tg_m8wJdmeiItA8rL*TI<I2 at 9?;y<
zFysBTYxHaMX!L2Arvhpb>D3 at 1fB*srAb<b at 2q1s}0tg_0!0s2&fw)NsAb<b at 2q1s}
k0tg_000IagfB*srAb<b at 2q1s}0tg_000Iag(BcAr0tZJ8qyPW_

literal 0
HcmV?d00001

diff --git a/lld/test/COFF/pdb-from-outputpath.test b/lld/test/COFF/pdb-from-outputpath.test
new file mode 100644
index 0000000000000..4c0dd7c7b6786
--- /dev/null
+++ b/lld/test/COFF/pdb-from-outputpath.test
@@ -0,0 +1,37 @@
+Replicate this scenario:
+
+$ rm -rf %t && mkdir -p %t/a && mkdir -p %t/a && cd %t
+
+$ cat a.c
+struct Foo { int x; };
+int g(struct Foo *p);
+int main() {
+  struct Foo f = {42};
+  return g(&f);
+}
+
+$ cat b.c
+struct Foo { int x; };
+int g(struct Foo *p) { return p->x; }
+
+$ cl -c a.c -Zi -Fda/pdb-from-outputpath-a.pdb -Foa/pdb-from-outputpath-a.obj
+
+$ cl -c b.c -Zi -Fdb/pdb-from-outputpath-b.pdb -Fob/pdb-from-outputpath-b.obj
+
+RUN: rm -rf %t && mkdir -p %t && mkdir -p %t/a && mkdir -p %t/b
+
+RUN: cp %p/Inputs/pdb-from-outputpath-a.obj %t/a/pdb-from-outputpath-a.obj
+RUN: mv %p/Inputs/pdb-from-outputpath-a.pdb %t/pdb-from-outputpath-a.pdb
+RUN: cp %p/Inputs/pdb-from-outputpath-b.obj %t/b/pdb-from-outputpath-b.obj
+RUN: mv %p/Inputs/pdb-from-outputpath-b.pdb %t/pdb-from-outputpath-b.pdb
+
+RUN: lld-link %t/a/pdb-from-outputpath-a.obj %t/b/pdb-from-outputpath-b.obj -debug -entry:main -nodefaultlib -out:%t/t.exe
+RUN: llvm-pdbutil dump -l %t/t.pdb | FileCheck %s
+
+CHECK:                            Lines
+CHECK: ============================================================
+CHECK-LABEL:   Mod 0000 | `{{.*}}pdb-from-outputpath-a.obj`:
+CHECK:       {{.*}} (SHA-256: 1112FA2413C56E03F4B5505E57C69AB45D701CFDAB923D4844A7D521FDACD940)
+CHECK-LABEL:   Mod 0001 | `{{.*}}pdb-from-outputpath-b.obj`:
+CHECK:       {{.*}} (SHA-256: D2547E2F2970ED80738146BB5A4D2B7C45C7BB71B73B541A4162F74A8EDDAF2C)
+CHECK-LABEL:   Mod 0002 | `* Linker *`:



More information about the llvm-commits mailing list