Skip to content

Commit 47b4bbf

Browse files
authored
[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.
1 parent 64addd6 commit 47b4bbf

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

lld/COFF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
23822382
ctx.symtab.addAbsolute(mangle("__CTOR_LIST__"), 0);
23832383
ctx.symtab.addAbsolute(mangle("__DTOR_LIST__"), 0);
23842384
}
2385+
if (config->debug || config->buildIDHash != BuildIDHash::None)
2386+
if (ctx.symtab.findUnderscore("__buildid"))
2387+
ctx.symtab.addUndefined(mangle("__buildid"));
23852388

23862389
// This code may add new undefined symbols to the link, which may enqueue more
23872390
// symbol resolution tasks, so we need to continue executing tasks until we

lld/COFF/Symbols.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,21 @@ class DefinedAbsolute : public Defined {
269269
// __safe_se_handler_table.
270270
class DefinedSynthetic : public Defined {
271271
public:
272-
explicit DefinedSynthetic(StringRef name, Chunk *c)
273-
: Defined(DefinedSyntheticKind, name), c(c) {}
272+
explicit DefinedSynthetic(StringRef name, Chunk *c, uint32_t offset = 0)
273+
: Defined(DefinedSyntheticKind, name), c(c), offset(offset) {}
274274

275275
static bool classof(const Symbol *s) {
276276
return s->kind() == DefinedSyntheticKind;
277277
}
278278

279279
// A null chunk indicates that this is __ImageBase. Otherwise, this is some
280280
// other synthesized chunk, like SEHTableChunk.
281-
uint32_t getRVA() { return c ? c->getRVA() : 0; }
281+
uint32_t getRVA() { return c ? c->getRVA() + offset : 0; }
282282
Chunk *getChunk() { return c; }
283283

284284
private:
285285
Chunk *c;
286+
uint32_t offset;
286287
};
287288

288289
// This class represents a symbol defined in an archive file. It is

lld/COFF/Writer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,9 @@ void Writer::createMiscChunks() {
11211121
// if we're ultimately not going to write CodeView data to the PDB.
11221122
buildId = make<CVDebugRecordChunk>(ctx);
11231123
debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
1124+
if (Symbol *buildidSym = ctx.symtab.findUnderscore("__buildid"))
1125+
replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
1126+
buildId, 4);
11241127
}
11251128

11261129
if (config->cetCompat) {

lld/docs/windows_support.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,14 @@ Using Ninja
9595
1. Check out LLVM and LLD from the LLVM SVN repository (or Git mirror),
9696
#. run ``cmake -G ninja <llvm-source-dir>`` from VS command prompt,
9797
#. run ``ninja lld``
98+
99+
Extensions
100+
==========
101+
102+
LLD flags
103+
---------
104+
105+
* ``/build-id``: Always generate GUID hash. When PDB is generated, LLD uses PDB
106+
content hash for GUID. Otherwise, LLD uses output binary content hash for GUID.
107+
LLD also provides ``__buildid`` symbol pointing to the 16 bytes GUID hash if
108+
there is a reference to it.

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
3+
# RUN: lld-link -debug:symtab -entry:main %t.obj -build-id -Brepro -out:%t.exe
4+
# RUN: llvm-objdump -s -t %t.exe | FileCheck %s
5+
6+
# Check __buildid points to 0x14000203c which is after the signature RSDS.
7+
8+
# CHECK: SYMBOL TABLE:
9+
# CHECK-NEXT: 0x0000003c __buildid
10+
# CHECK: Contents of section .rdata:
11+
# CHECK-NEXT: 140002000
12+
# CHECK-NEXT: 140002010
13+
# CHECK-NEXT: 140002020
14+
# CHECK-NEXT: 140002030 {{.*}} {{.*}} 52534453 {{.*}}
15+
# CHECK-NEXT: 140002040
16+
17+
.globl main
18+
main:
19+
nop
20+
21+
.section .bss,"bw",discard,__buildid
22+
.global __buildid
23+
__buildid:

0 commit comments

Comments
 (0)