[lld] 47b4bbf - [LLD][COFF] add __buildid symbol. (#74652)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 14:43:14 PST 2023


Author: Zequan Wu
Date: 2023-12-14T17:43:10-05:00
New Revision: 47b4bbfe522cd7a9251b0a8813576ab6f0b59616

URL: https://github.com/llvm/llvm-project/commit/47b4bbfe522cd7a9251b0a8813576ab6f0b59616
DIFF: https://github.com/llvm/llvm-project/commit/47b4bbfe522cd7a9251b0a8813576ab6f0b59616.diff

LOG: [LLD][COFF] add __buildid symbol. (#74652)

After #71433, lld-link is able to always generate build id even when PDB
is not generated.

This adds the `__buildid` symbol to points to the start of 16 bytes guid
(which is after `RSDS`) and allows profile runtime to access it and dump
it to raw profile.

Added: 
    lld/test/COFF/build-id-sym.s

Modified: 
    lld/COFF/Driver.cpp
    lld/COFF/Symbols.h
    lld/COFF/Writer.cpp
    lld/docs/windows_support.rst

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index d4a2f5767a2e87..4e334e290145a3 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2382,6 +2382,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
     ctx.symtab.addAbsolute(mangle("__CTOR_LIST__"), 0);
     ctx.symtab.addAbsolute(mangle("__DTOR_LIST__"), 0);
   }
+  if (config->debug || config->buildIDHash != BuildIDHash::None)
+    if (ctx.symtab.findUnderscore("__buildid"))
+      ctx.symtab.addUndefined(mangle("__buildid"));
 
   // This code may add new undefined symbols to the link, which may enqueue more
   // symbol resolution tasks, so we need to continue executing tasks until we

diff  --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index 750269fd0bbba7..ca69fb2d052706 100644
--- a/lld/COFF/Symbols.h
+++ b/lld/COFF/Symbols.h
@@ -269,8 +269,8 @@ class DefinedAbsolute : public Defined {
 // __safe_se_handler_table.
 class DefinedSynthetic : public Defined {
 public:
-  explicit DefinedSynthetic(StringRef name, Chunk *c)
-      : Defined(DefinedSyntheticKind, name), c(c) {}
+  explicit DefinedSynthetic(StringRef name, Chunk *c, uint32_t offset = 0)
+      : Defined(DefinedSyntheticKind, name), c(c), offset(offset) {}
 
   static bool classof(const Symbol *s) {
     return s->kind() == DefinedSyntheticKind;
@@ -278,11 +278,12 @@ class DefinedSynthetic : public Defined {
 
   // A null chunk indicates that this is __ImageBase. Otherwise, this is some
   // other synthesized chunk, like SEHTableChunk.
-  uint32_t getRVA() { return c ? c->getRVA() : 0; }
+  uint32_t getRVA() { return c ? c->getRVA() + offset : 0; }
   Chunk *getChunk() { return c; }
 
 private:
   Chunk *c;
+  uint32_t offset;
 };
 
 // This class represents a symbol defined in an archive file. It is

diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 4eeb6ba804bdfe..45fcb51e19acd2 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1121,6 +1121,9 @@ void Writer::createMiscChunks() {
     // if we're ultimately not going to write CodeView data to the PDB.
     buildId = make<CVDebugRecordChunk>(ctx);
     debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
+    if (Symbol *buildidSym = ctx.symtab.findUnderscore("__buildid"))
+      replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
+                                      buildId, 4);
   }
 
   if (config->cetCompat) {

diff  --git a/lld/docs/windows_support.rst b/lld/docs/windows_support.rst
index 620040ee8196a6..e4640b4a5259ad 100644
--- a/lld/docs/windows_support.rst
+++ b/lld/docs/windows_support.rst
@@ -95,3 +95,14 @@ Using Ninja
 1. Check out LLVM and LLD from the LLVM SVN repository (or Git mirror),
 #. run ``cmake -G ninja <llvm-source-dir>`` from VS command prompt,
 #. run ``ninja lld``
+
+Extensions
+==========
+
+LLD flags
+---------
+
+* ``/build-id``: Always generate GUID hash. When PDB is generated, LLD uses PDB
+  content hash for GUID. Otherwise, LLD uses output binary content hash for GUID.
+  LLD also provides ``__buildid`` symbol pointing to the 16 bytes GUID hash if
+  there is a reference to it.

diff  --git a/lld/test/COFF/build-id-sym.s b/lld/test/COFF/build-id-sym.s
new file mode 100644
index 00000000000000..faed7bc40fd068
--- /dev/null
+++ b/lld/test/COFF/build-id-sym.s
@@ -0,0 +1,23 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
+# RUN: lld-link -debug:symtab -entry:main %t.obj -build-id -Brepro -out:%t.exe
+# RUN: llvm-objdump -s -t %t.exe | FileCheck %s
+
+# Check __buildid points to 0x14000203c which is after the signature RSDS.
+
+# CHECK:      SYMBOL TABLE:
+# CHECK-NEXT: 0x0000003c __buildid
+# CHECK:      Contents of section .rdata:
+# CHECK-NEXT:  140002000
+# CHECK-NEXT:  140002010
+# CHECK-NEXT:  140002020
+# CHECK-NEXT:  140002030 {{.*}} {{.*}} 52534453 {{.*}}
+# CHECK-NEXT:  140002040
+
+.globl main
+main:
+  nop
+
+.section .bss,"bw",discard,__buildid
+.global __buildid
+__buildid:


        


More information about the llvm-commits mailing list