Skip to content

[LLD][COFF] add __buildid symbol. #74652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,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: