Skip to content

Commit

Permalink
[LLD][COFF] add __buildid symbol. (#74652)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ZequanWu committed Dec 14, 2023
1 parent 64addd6 commit 47b4bbf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lld/COFF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,21 @@ 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;
}

// 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
Expand Down
3 changes: 3 additions & 0 deletions lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions lld/docs/windows_support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
23 changes: 23 additions & 0 deletions lld/test/COFF/build-id-sym.s
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit 47b4bbf

Please sign in to comment.