[lld] [LLD][COFF] add __build_guid symbol. (PR #74652)
Zequan Wu via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 14 09:54:45 PST 2023
https://github.com/ZequanWu updated https://github.com/llvm/llvm-project/pull/74652
>From b1e78a419a1caa377a4afe99c4ae7ed7dc330c85 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 6 Dec 2023 15:40:59 -0500
Subject: [PATCH 1/7] [LLD][COFF] add __lld_buildid symbol.
---
lld/COFF/Driver.cpp | 6 ++++-
lld/COFF/Writer.cpp | 2 ++
lld/test/COFF/build-id-sym.s | 22 +++++++++++++++++
lld/test/COFF/pdb-comdat.test | 10 ++++----
lld/test/COFF/pdb-global-constants.test | 6 ++---
lld/test/COFF/pdb-global-gc.yaml | 2 +-
lld/test/COFF/pdb-globals.test | 24 +++++++++----------
lld/test/COFF/pdb-import-gc.yaml | 2 +-
lld/test/COFF/pdb-local-constants.test | 2 +-
lld/test/COFF/pdb-publics-import.test | 8 ++++---
lld/test/COFF/pdb-safeseh.yaml | 2 +-
lld/test/COFF/pdb-secrel-absolute.yaml | 2 +-
lld/test/COFF/pdb-symbol-types.yaml | 8 +++----
.../pdb-type-server-guid-collision-valid.test | 2 +-
lld/test/COFF/pdb-type-server-simple.test | 8 +++----
lld/test/COFF/pdb.test | 17 ++++++++-----
lld/test/COFF/precomp-link.test | 2 +-
lld/test/COFF/precomp-summary-fail.test | 2 +-
18 files changed, 81 insertions(+), 46 deletions(-)
create mode 100644 lld/test/COFF/build-id-sym.s
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 99c1a60735adc5..4542a129ae764d 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-
+#include "/tmp/debug.h"
#include "Driver.h"
#include "COFFLinkerContext.h"
#include "Config.h"
@@ -2382,6 +2382,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
ctx.symtab.addAbsolute(mangle("__DTOR_LIST__"), 0);
}
+ if (config->debug || config->buildIDHash != BuildIDHash::None) {
+ ctx.symtab.addAbsolute(mangle("__lld_buildid"), 0);
+ }
+
// 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
// converge.
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 7b1ff8071e2e3d..ec252058607b4a 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1121,6 +1121,8 @@ 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);
+ Symbol *buildidSym = ctx.symtab.findUnderscore("__lld_buildid");
+ replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(), buildId);
}
if (config->cetCompat) {
diff --git a/lld/test/COFF/build-id-sym.s b/lld/test/COFF/build-id-sym.s
new file mode 100644
index 00000000000000..9e151e807d2d0f
--- /dev/null
+++ b/lld/test/COFF/build-id-sym.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
+# RUN: lld-link -entry:main %t.obj -build-id -Brepro -out:%t.exe
+# RUN: llvm-objdump -s %t.exe | FileCheck %s
+
+# Check __lld_buildid points to 0x140002038 which is the start of build id.
+
+# CHECK: Contents of section .rdata:
+# CHECK-NEXT: 140002000 00000000 8064d9b6 00000000 02000000 .....d..........
+# CHECK-NEXT: 140002010 19000000 38200000 38060000 00000000 ....8 ..8.......
+# CHECK-NEXT: 140002020 8064d9b6 00000000 10000000 00000000 .d..............
+# CHECK-NEXT: 140002030 00000000 00000000 52534453 8064d9b6 ........RSDS.d..
+
+# CHECK: Contents of section .data:
+# CHECK-NEXT: 140003000 38200040 01000000
+
+.globl main
+main:
+ nop
+
+.data
+ .quad __lld_buildid
diff --git a/lld/test/COFF/pdb-comdat.test b/lld/test/COFF/pdb-comdat.test
index 86c07f4af2f5c7..83bd8e63a8abce 100644
--- a/lld/test/COFF/pdb-comdat.test
+++ b/lld/test/COFF/pdb-comdat.test
@@ -41,15 +41,15 @@ CHECK-LABEL: Mod 0002 | `* Linker *`:
CHECK-LABEL: Global Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 84 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 112 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 120
-CHECK-NEXT: 148 | S_PROCREF [size = 20] `bar`
+CHECK-NEXT: 176 | S_PROCREF [size = 20] `bar`
CHECK-NEXT: module = 2, sum name = 0, offset = 120
-CHECK-NEXT: 128 | S_PROCREF [size = 20] `foo`
+CHECK-NEXT: 156 | S_PROCREF [size = 20] `foo`
CHECK-NEXT: module = 1, sum name = 0, offset = 208
-CHECK-NEXT: 104 | S_GDATA32 [size = 24] `global`
+CHECK-NEXT: 132 | S_GDATA32 [size = 24] `global`
CHECK-NEXT: type = 0x0074 (int), addr = 0003:0000
-CHECK-NEXT: 168 | S_GDATA32 [size = 24] `global`
+CHECK-NEXT: 196 | S_GDATA32 [size = 24] `global`
CHECK-NEXT: type = 0x0074 (int), addr = 0003:0000
CHECK: Symbols
diff --git a/lld/test/COFF/pdb-global-constants.test b/lld/test/COFF/pdb-global-constants.test
index b646fc9c2aa3ef..935bf08600a63f 100644
--- a/lld/test/COFF/pdb-global-constants.test
+++ b/lld/test/COFF/pdb-global-constants.test
@@ -20,9 +20,9 @@
# int foobar() { return Foo + Bar; }
CHECK: Global Symbols
-CHECK: 88 | S_CONSTANT [size = 16] `Bar`
+CHECK: 116 | S_CONSTANT [size = 16] `Bar`
CHECK-NEXT: type = 0x1002 (const int), value = 42
-CHECK-NEXT: 72 | S_CONSTANT [size = 16] `Foo`
+CHECK-NEXT: 100 | S_CONSTANT [size = 16] `Foo`
CHECK-NEXT: type = 0x1002 (const int), value = 41
-CHECK-NEXT: 128 | S_CONSTANT [size = 16] `Foo`
+CHECK-NEXT: 156 | S_CONSTANT [size = 16] `Foo`
CHECK-NEXT: type = 0x1002 (const int), value = 42
diff --git a/lld/test/COFF/pdb-global-gc.yaml b/lld/test/COFF/pdb-global-gc.yaml
index 5351ef545df87c..55e885555ace38 100644
--- a/lld/test/COFF/pdb-global-gc.yaml
+++ b/lld/test/COFF/pdb-global-gc.yaml
@@ -16,7 +16,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 20 | S_GDATA32 [size = 28] `__wc_mb_cur`
+# CHECK-NEXT: 48 | S_GDATA32 [size = 28] `__wc_mb_cur`
# CHECK-NEXT: type = 0x0070 (char), addr = 0000:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-globals.test b/lld/test/COFF/pdb-globals.test
index a8fa2f615a735d..e7d567cde6f1ab 100644
--- a/lld/test/COFF/pdb-globals.test
+++ b/lld/test/COFF/pdb-globals.test
@@ -20,29 +20,29 @@ RUN: llvm-pdbutil dump -symbols -globals %t.pdb | FileCheck %s
CHECK-LABEL: Global Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 444 | S_UDT [size = 20] `HelloPoint`
+CHECK-NEXT: 476 | S_UDT [size = 20] `HelloPoint`
CHECK-NEXT: original type = 0x1007
-CHECK-NEXT: 240 | S_LPROCREF [size = 24] `LocalFunc`
+CHECK-NEXT: 272 | S_LPROCREF [size = 24] `LocalFunc`
CHECK-NEXT: module = 1, sum name = 0, offset = 424
-CHECK-NEXT: 192 | S_PROCREF [size = 28] `GlobalFunc`
+CHECK-NEXT: 224 | S_PROCREF [size = 28] `GlobalFunc`
CHECK-NEXT: module = 1, sum name = 0, offset = 136
-CHECK-NEXT: 220 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 252 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 224
-CHECK-NEXT: 340 | S_CONSTANT [size = 24] `ConstexprVar`
+CHECK-NEXT: 372 | S_CONSTANT [size = 24] `ConstexprVar`
CHECK-NEXT: type = 0x100B (const int), value = 18
-CHECK-NEXT: 264 | S_GDATA32 [size = 28] `__purecall`
+CHECK-NEXT: 296 | S_GDATA32 [size = 28] `__purecall`
CHECK-NEXT: type = 0x0403 (void*), addr = 0003:0004
-CHECK-NEXT: 292 | S_GDATA32 [size = 24] `GlobalVar`
+CHECK-NEXT: 324 | S_GDATA32 [size = 24] `GlobalVar`
CHECK-NEXT: type = 0x100C (const int*), addr = 0003:0000
-CHECK-NEXT: 316 | S_GTHREAD32 [size = 24] `GlobalTLS`
+CHECK-NEXT: 348 | S_GTHREAD32 [size = 24] `GlobalTLS`
CHECK-NEXT: type = 0x0074 (int), addr = 0004:0000
-CHECK-NEXT: 392 | S_LTHREAD32 [size = 24] `StaticTLS`
+CHECK-NEXT: 424 | S_LTHREAD32 [size = 24] `StaticTLS`
CHECK-NEXT: type = 0x0074 (int), addr = 0004:0004
-CHECK-NEXT: 416 | S_UDT [size = 28] `HelloPointTypedef`
+CHECK-NEXT: 448 | S_UDT [size = 28] `HelloPointTypedef`
CHECK-NEXT: original type = 0x1007
-CHECK-NEXT: 364 | S_LDATA32 [size = 28] `ConstantVar`
+CHECK-NEXT: 396 | S_LDATA32 [size = 28] `ConstantVar`
CHECK-NEXT: type = 0x100B (const int), addr = 0002:0000
-CHECK-NEXT: 464 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
+CHECK-NEXT: 496 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
CHECK-NEXT: module = 1, sum name = 0, offset = 572
CHECK-LABEL: Symbols
diff --git a/lld/test/COFF/pdb-import-gc.yaml b/lld/test/COFF/pdb-import-gc.yaml
index 8f50a111411cc9..5b0f6074fe0148 100644
--- a/lld/test/COFF/pdb-import-gc.yaml
+++ b/lld/test/COFF/pdb-import-gc.yaml
@@ -14,7 +14,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 20 | S_GDATA32 [size = 32] `__imp___wc_mb_cur`
+# CHECK-NEXT: 48 | S_GDATA32 [size = 32] `__imp___wc_mb_cur`
# CHECK-NEXT: type = 0x0070 (char), addr = 0000:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-local-constants.test b/lld/test/COFF/pdb-local-constants.test
index 3a9538252ed802..8a4baad2938386 100644
--- a/lld/test/COFF/pdb-local-constants.test
+++ b/lld/test/COFF/pdb-local-constants.test
@@ -13,7 +13,7 @@
#
CHECK: Global Symbols
-CHECK: 40 | S_CONSTANT [size = 20] `g_const`
+CHECK: 68 | S_CONSTANT [size = 20] `g_const`
CHECK-NEXT: type = 0x1002 (const int), value = 321
CHECK: Symbols
diff --git a/lld/test/COFF/pdb-publics-import.test b/lld/test/COFF/pdb-publics-import.test
index 83f087b662186c..08b8b66730889a 100644
--- a/lld/test/COFF/pdb-publics-import.test
+++ b/lld/test/COFF/pdb-publics-import.test
@@ -54,11 +54,13 @@ CHECK-NEXT: pdb file ni: 1 `{{.*}}pdb-publics-import.test.tmp2.pdb`, src file ni
CHECK: Public Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 112 | S_PUB32 [size = 20] `main`
+CHECK-NEXT: 64 | S_PUB32 [size = 28] `__lld_buildid`
+CHECK-NEXT: flags = none, addr = 0002:0028
+CHECK-NEXT: 140 | S_PUB32 [size = 20] `main`
CHECK-NEXT: flags = function, addr = 0001:0000
-CHECK-NEXT: 64 | S_PUB32 [size = 24] `exportfn1`
+CHECK-NEXT: 92 | S_PUB32 [size = 24] `exportfn1`
CHECK-NEXT: flags = function, addr = 0001:0016
-CHECK-NEXT: 88 | S_PUB32 [size = 24] `exportfn2`
+CHECK-NEXT: 116 | S_PUB32 [size = 24] `exportfn2`
CHECK-NEXT: flags = function, addr = 0001:0032
CHECK-NEXT: 32 | S_PUB32 [size = 32] `__imp_exportfn2`
CHECK-NEXT: flags = none, addr = 0002:0136
diff --git a/lld/test/COFF/pdb-safeseh.yaml b/lld/test/COFF/pdb-safeseh.yaml
index cc7ddb19a49c64..6914c081cff9bf 100644
--- a/lld/test/COFF/pdb-safeseh.yaml
+++ b/lld/test/COFF/pdb-safeseh.yaml
@@ -10,7 +10,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 20 | S_GDATA32 [size = 40] `___safe_se_handler_table`
+# CHECK-NEXT: 52 | S_GDATA32 [size = 40] `___safe_se_handler_table`
# CHECK-NEXT: type = 0x0022 (unsigned long), addr = 0003:0000
--- !COFF
diff --git a/lld/test/COFF/pdb-secrel-absolute.yaml b/lld/test/COFF/pdb-secrel-absolute.yaml
index 330106b3bbed0b..a8f83ec0c11bde 100644
--- a/lld/test/COFF/pdb-secrel-absolute.yaml
+++ b/lld/test/COFF/pdb-secrel-absolute.yaml
@@ -10,7 +10,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 20 | S_GDATA32 [size = 36] `__guard_fids_table`
+# CHECK-NEXT: 48 | S_GDATA32 [size = 36] `__guard_fids_table`
# CHECK-NEXT: type = 0x0022 (unsigned long), addr = 0003:0000
--- !COFF
diff --git a/lld/test/COFF/pdb-symbol-types.yaml b/lld/test/COFF/pdb-symbol-types.yaml
index d8741afd8cded4..9e046b2da6119b 100644
--- a/lld/test/COFF/pdb-symbol-types.yaml
+++ b/lld/test/COFF/pdb-symbol-types.yaml
@@ -16,13 +16,13 @@
# CHECK-LABEL: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 48 | S_PROCREF [size = 20] `main`
+# CHECK-NEXT: 76 | S_PROCREF [size = 20] `main`
# CHECK-NEXT: module = 1, sum name = 0, offset = 116
-# CHECK-NEXT: 96 | S_UDT [size = 16] `UDT_Foo`
+# CHECK-NEXT: 124 | S_UDT [size = 16] `UDT_Foo`
# CHECK-NEXT: original type = 0x1004
-# CHECK-NEXT: 112 | S_UDT [size = 12] `Foo`
+# CHECK-NEXT: 140 | S_UDT [size = 12] `Foo`
# CHECK-NEXT: original type = 0x1004
-# CHECK-NEXT: 68 | S_GDATA32 [size = 28] `global_foo`
+# CHECK-NEXT: 96 | S_GDATA32 [size = 28] `global_foo`
# CHECK-NEXT: type = 0x1004 (Foo), addr = 0003:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-type-server-guid-collision-valid.test b/lld/test/COFF/pdb-type-server-guid-collision-valid.test
index a3819eb2788601..23cfa0e0588bb3 100644
--- a/lld/test/COFF/pdb-type-server-guid-collision-valid.test
+++ b/lld/test/COFF/pdb-type-server-guid-collision-valid.test
@@ -13,5 +13,5 @@ RUN: llvm-pdbutil dump -globals collision.pdb | FileCheck %s -check-prefix DUMP
DUMP-LABEL: Global Symbols
DUMP: ============================================================
-DUMP: 100 | S_GDATA32 [size = 24] `bar_gv`
+DUMP: 128 | S_GDATA32 [size = 24] `bar_gv`
DUMP-NEXT: type = 0x104E (Bar), addr = 0002:0004
diff --git a/lld/test/COFF/pdb-type-server-simple.test b/lld/test/COFF/pdb-type-server-simple.test
index e9757d187e2f10..e437eb27f26e8b 100644
--- a/lld/test/COFF/pdb-type-server-simple.test
+++ b/lld/test/COFF/pdb-type-server-simple.test
@@ -66,11 +66,11 @@ CHECK: {{.*}}: `b.c`
CHECK-LABEL: Global Symbols
CHECK: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 36 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 64 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 104
-CHECK-NEXT: 68 | S_PROCREF [size = 16] `g`
+CHECK-NEXT: 96 | S_PROCREF [size = 16] `g`
CHECK-NEXT: module = 2, sum name = 0, offset = 104
-CHECK-NEXT: 56 | S_UDT [size = 12] `Foo`
+CHECK-NEXT: 84 | S_UDT [size = 12] `Foo`
CHECK-NEXT: original type = 0x1006
CHECK-LABEL: Symbols
@@ -112,7 +112,7 @@ SUMMARY-NEXT: 16 Merged IPI records
SUMMARY-NEXT: 3 Output PDB strings
SUMMARY-NEXT: 4 Global symbol records
SUMMARY-NEXT: 14 Module symbol records
-SUMMARY-NEXT: 2 Public symbol records
+SUMMARY-NEXT: 3 Public symbol records
SUMMARY: Top 10 types responsible for the most TPI input:
SUMMARY-NEXT: index total bytes count size
diff --git a/lld/test/COFF/pdb.test b/lld/test/COFF/pdb.test
index 5492fececccd55..6db5e79f95d10e 100644
--- a/lld/test/COFF/pdb.test
+++ b/lld/test/COFF/pdb.test
@@ -184,23 +184,28 @@ RAW-NEXT: 0x1006: ` -I"C:\Program Files (x86)\Windows Kits\8.1\includ
RAW: Public Symbols
RAW-NEXT: ============================================================
RAW-NEXT: Publics Header
-RAW-NEXT: sym hash = 556, thunk table addr = 0000:0000
+RAW-NEXT: sym hash = 568, thunk table addr = 0000:0000
RAW-NEXT: GSI Header
-RAW-NEXT: sig = 0xFFFFFFFF, hdr = 0xF12F091A, hr size = 16, num buckets = 524
+RAW-NEXT: sig = 0xFFFFFFFF, hdr = 0xF12F091A, hr size = 24, num buckets = 528
RAW-NEXT: Records
-RAW-NEXT: 20 | S_PUB32 [size = 20] `main`
+RAW-NEXT: 0 | S_PUB32 [size = 28] `__lld_buildid`
+RAW-NEXT: flags = none, addr = 0002:0028
+RAW-NEXT: 48 | S_PUB32 [size = 20] `main`
RAW-NEXT: flags = function, addr = 0001:0000
-RAW-NEXT: 0 | S_PUB32 [size = 20] `foo`
+RAW-NEXT: 28 | S_PUB32 [size = 20] `foo`
RAW-NEXT: flags = function, addr = 0001:0016
RAW-NOT: S_PUB32
RAW-NEXT: Hash Entries
-RAW-NEXT: off = 21, refcnt = 1
RAW-NEXT: off = 1, refcnt = 1
+RAW-NEXT: off = 49, refcnt = 1
+RAW-NEXT: off = 29, refcnt = 1
RAW-NEXT: Hash Buckets
RAW-NEXT: 0x00000000
RAW-NEXT: 0x0000000c
+RAW-NEXT: 0x00000018
RAW-NEXT: Address Map
-RAW-NEXT: off = 20
+RAW-NEXT: off = 48
+RAW-NEXT: off = 28
RAW-NEXT: off = 0
RAW: Section Headers
RAW-NEXT: ============================================================
diff --git a/lld/test/COFF/precomp-link.test b/lld/test/COFF/precomp-link.test
index 53d981c4e62280..5985f16d12ccb9 100644
--- a/lld/test/COFF/precomp-link.test
+++ b/lld/test/COFF/precomp-link.test
@@ -68,7 +68,7 @@ SUMMARY-NEXT: 170 Merged IPI records
SUMMARY-NEXT: 5 Output PDB strings
SUMMARY-NEXT: 167 Global symbol records
SUMMARY-NEXT: 20 Module symbol records
-SUMMARY-NEXT: 3 Public symbol records
+SUMMARY-NEXT: 4 Public symbol records
// precomp.h
#pragma once
diff --git a/lld/test/COFF/precomp-summary-fail.test b/lld/test/COFF/precomp-summary-fail.test
index 5ebba9a1d3c747..dfafae8973ec88 100644
--- a/lld/test/COFF/precomp-summary-fail.test
+++ b/lld/test/COFF/precomp-summary-fail.test
@@ -21,4 +21,4 @@ SUMMARY-NEXT: 2 Merged IPI records
SUMMARY-NEXT: 1 Output PDB strings
SUMMARY-NEXT: 0 Global symbol records
SUMMARY-NEXT: 4 Module symbol records
-SUMMARY-NEXT: 0 Public symbol records
+SUMMARY-NEXT: 1 Public symbol records
>From f9f62f9d47300bd7b051b77c8d3b7eb5b3cf88c3 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 6 Dec 2023 15:46:12 -0500
Subject: [PATCH 2/7] fixup! [LLD][COFF] add __lld_buildid symbol.
---
lld/COFF/Driver.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 4542a129ae764d..b76ba3ca5bc247 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-#include "/tmp/debug.h"
+
#include "Driver.h"
#include "COFFLinkerContext.h"
#include "Config.h"
>From e4da2bf4159277646f29335b5ccffc48d7ab8572 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Tue, 12 Dec 2023 14:43:46 -0500
Subject: [PATCH 3/7] Add __lld_buildid symbol only when referenced.
---
lld/COFF/Driver.cpp | 7 +++---
lld/COFF/Writer.cpp | 5 ++--
lld/test/COFF/build-id-sym.s | 21 ++++++++--------
lld/test/COFF/pdb-comdat.test | 10 ++++----
lld/test/COFF/pdb-global-constants.test | 6 ++---
lld/test/COFF/pdb-global-gc.yaml | 2 +-
lld/test/COFF/pdb-globals.test | 24 +++++++++----------
lld/test/COFF/pdb-import-gc.yaml | 2 +-
lld/test/COFF/pdb-local-constants.test | 2 +-
lld/test/COFF/pdb-publics-import.test | 8 +++----
lld/test/COFF/pdb-safeseh.yaml | 2 +-
lld/test/COFF/pdb-secrel-absolute.yaml | 2 +-
lld/test/COFF/pdb-symbol-types.yaml | 8 +++----
.../pdb-type-server-guid-collision-valid.test | 2 +-
lld/test/COFF/pdb-type-server-simple.test | 8 +++----
lld/test/COFF/pdb.test | 17 +++++--------
lld/test/COFF/precomp-link.test | 2 +-
lld/test/COFF/precomp-summary-fail.test | 2 +-
18 files changed, 62 insertions(+), 68 deletions(-)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index b76ba3ca5bc247..5ca2235ebca5a5 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2381,10 +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) {
- ctx.symtab.addAbsolute(mangle("__lld_buildid"), 0);
- }
+ if (config->debug || config->buildIDHash != BuildIDHash::None)
+ if (ctx.symtab.findUnderscore("__lld_buildid"))
+ ctx.symtab.addUndefined(mangle("__lld_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/Writer.cpp b/lld/COFF/Writer.cpp
index ec252058607b4a..4bd1f0b24ba27c 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1121,8 +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);
- Symbol *buildidSym = ctx.symtab.findUnderscore("__lld_buildid");
- replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(), buildId);
+ if (Symbol *buildidSym = ctx.symtab.findUnderscore("__lld_buildid"))
+ replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
+ buildId);
}
if (config->cetCompat) {
diff --git a/lld/test/COFF/build-id-sym.s b/lld/test/COFF/build-id-sym.s
index 9e151e807d2d0f..61efbe97adfd8b 100644
--- a/lld/test/COFF/build-id-sym.s
+++ b/lld/test/COFF/build-id-sym.s
@@ -1,22 +1,23 @@
# REQUIRES: x86
# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
-# RUN: lld-link -entry:main %t.obj -build-id -Brepro -out:%t.exe
-# RUN: llvm-objdump -s %t.exe | FileCheck %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 __lld_buildid points to 0x140002038 which is the start of build id.
+# CHECK: SYMBOL TABLE:
+# CHECK-NEXT: 0x00000038 __lld_buildid
# CHECK: Contents of section .rdata:
-# CHECK-NEXT: 140002000 00000000 8064d9b6 00000000 02000000 .....d..........
+# CHECK-NEXT: 140002000 00000000 21b9bff1 00000000 02000000 ....!...........
# CHECK-NEXT: 140002010 19000000 38200000 38060000 00000000 ....8 ..8.......
-# CHECK-NEXT: 140002020 8064d9b6 00000000 10000000 00000000 .d..............
-# CHECK-NEXT: 140002030 00000000 00000000 52534453 8064d9b6 ........RSDS.d..
-
-# CHECK: Contents of section .data:
-# CHECK-NEXT: 140003000 38200040 01000000
+# CHECK-NEXT: 140002020 21b9bff1 00000000 10000000 00000000 !...............
+# CHECK-NEXT: 140002030 00000000 00000000 52534453 21b9bff1 ........RSDS!...
+# CHECK-NEXT: 140002040 0450a611 4c4c4420 5044422e 01000000 .P..LLD PDB.....
.globl main
main:
nop
-.data
- .quad __lld_buildid
+.section .bss,"bw",discard,__lld_buildid
+.global __lld_buildid
+__lld_buildid:
diff --git a/lld/test/COFF/pdb-comdat.test b/lld/test/COFF/pdb-comdat.test
index 83bd8e63a8abce..86c07f4af2f5c7 100644
--- a/lld/test/COFF/pdb-comdat.test
+++ b/lld/test/COFF/pdb-comdat.test
@@ -41,15 +41,15 @@ CHECK-LABEL: Mod 0002 | `* Linker *`:
CHECK-LABEL: Global Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 112 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 84 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 120
-CHECK-NEXT: 176 | S_PROCREF [size = 20] `bar`
+CHECK-NEXT: 148 | S_PROCREF [size = 20] `bar`
CHECK-NEXT: module = 2, sum name = 0, offset = 120
-CHECK-NEXT: 156 | S_PROCREF [size = 20] `foo`
+CHECK-NEXT: 128 | S_PROCREF [size = 20] `foo`
CHECK-NEXT: module = 1, sum name = 0, offset = 208
-CHECK-NEXT: 132 | S_GDATA32 [size = 24] `global`
+CHECK-NEXT: 104 | S_GDATA32 [size = 24] `global`
CHECK-NEXT: type = 0x0074 (int), addr = 0003:0000
-CHECK-NEXT: 196 | S_GDATA32 [size = 24] `global`
+CHECK-NEXT: 168 | S_GDATA32 [size = 24] `global`
CHECK-NEXT: type = 0x0074 (int), addr = 0003:0000
CHECK: Symbols
diff --git a/lld/test/COFF/pdb-global-constants.test b/lld/test/COFF/pdb-global-constants.test
index 935bf08600a63f..b646fc9c2aa3ef 100644
--- a/lld/test/COFF/pdb-global-constants.test
+++ b/lld/test/COFF/pdb-global-constants.test
@@ -20,9 +20,9 @@
# int foobar() { return Foo + Bar; }
CHECK: Global Symbols
-CHECK: 116 | S_CONSTANT [size = 16] `Bar`
+CHECK: 88 | S_CONSTANT [size = 16] `Bar`
CHECK-NEXT: type = 0x1002 (const int), value = 42
-CHECK-NEXT: 100 | S_CONSTANT [size = 16] `Foo`
+CHECK-NEXT: 72 | S_CONSTANT [size = 16] `Foo`
CHECK-NEXT: type = 0x1002 (const int), value = 41
-CHECK-NEXT: 156 | S_CONSTANT [size = 16] `Foo`
+CHECK-NEXT: 128 | S_CONSTANT [size = 16] `Foo`
CHECK-NEXT: type = 0x1002 (const int), value = 42
diff --git a/lld/test/COFF/pdb-global-gc.yaml b/lld/test/COFF/pdb-global-gc.yaml
index 55e885555ace38..5351ef545df87c 100644
--- a/lld/test/COFF/pdb-global-gc.yaml
+++ b/lld/test/COFF/pdb-global-gc.yaml
@@ -16,7 +16,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 48 | S_GDATA32 [size = 28] `__wc_mb_cur`
+# CHECK-NEXT: 20 | S_GDATA32 [size = 28] `__wc_mb_cur`
# CHECK-NEXT: type = 0x0070 (char), addr = 0000:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-globals.test b/lld/test/COFF/pdb-globals.test
index e7d567cde6f1ab..a8fa2f615a735d 100644
--- a/lld/test/COFF/pdb-globals.test
+++ b/lld/test/COFF/pdb-globals.test
@@ -20,29 +20,29 @@ RUN: llvm-pdbutil dump -symbols -globals %t.pdb | FileCheck %s
CHECK-LABEL: Global Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 476 | S_UDT [size = 20] `HelloPoint`
+CHECK-NEXT: 444 | S_UDT [size = 20] `HelloPoint`
CHECK-NEXT: original type = 0x1007
-CHECK-NEXT: 272 | S_LPROCREF [size = 24] `LocalFunc`
+CHECK-NEXT: 240 | S_LPROCREF [size = 24] `LocalFunc`
CHECK-NEXT: module = 1, sum name = 0, offset = 424
-CHECK-NEXT: 224 | S_PROCREF [size = 28] `GlobalFunc`
+CHECK-NEXT: 192 | S_PROCREF [size = 28] `GlobalFunc`
CHECK-NEXT: module = 1, sum name = 0, offset = 136
-CHECK-NEXT: 252 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 220 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 224
-CHECK-NEXT: 372 | S_CONSTANT [size = 24] `ConstexprVar`
+CHECK-NEXT: 340 | S_CONSTANT [size = 24] `ConstexprVar`
CHECK-NEXT: type = 0x100B (const int), value = 18
-CHECK-NEXT: 296 | S_GDATA32 [size = 28] `__purecall`
+CHECK-NEXT: 264 | S_GDATA32 [size = 28] `__purecall`
CHECK-NEXT: type = 0x0403 (void*), addr = 0003:0004
-CHECK-NEXT: 324 | S_GDATA32 [size = 24] `GlobalVar`
+CHECK-NEXT: 292 | S_GDATA32 [size = 24] `GlobalVar`
CHECK-NEXT: type = 0x100C (const int*), addr = 0003:0000
-CHECK-NEXT: 348 | S_GTHREAD32 [size = 24] `GlobalTLS`
+CHECK-NEXT: 316 | S_GTHREAD32 [size = 24] `GlobalTLS`
CHECK-NEXT: type = 0x0074 (int), addr = 0004:0000
-CHECK-NEXT: 424 | S_LTHREAD32 [size = 24] `StaticTLS`
+CHECK-NEXT: 392 | S_LTHREAD32 [size = 24] `StaticTLS`
CHECK-NEXT: type = 0x0074 (int), addr = 0004:0004
-CHECK-NEXT: 448 | S_UDT [size = 28] `HelloPointTypedef`
+CHECK-NEXT: 416 | S_UDT [size = 28] `HelloPointTypedef`
CHECK-NEXT: original type = 0x1007
-CHECK-NEXT: 396 | S_LDATA32 [size = 28] `ConstantVar`
+CHECK-NEXT: 364 | S_LDATA32 [size = 28] `ConstantVar`
CHECK-NEXT: type = 0x100B (const int), addr = 0002:0000
-CHECK-NEXT: 496 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
+CHECK-NEXT: 464 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
CHECK-NEXT: module = 1, sum name = 0, offset = 572
CHECK-LABEL: Symbols
diff --git a/lld/test/COFF/pdb-import-gc.yaml b/lld/test/COFF/pdb-import-gc.yaml
index 5b0f6074fe0148..8f50a111411cc9 100644
--- a/lld/test/COFF/pdb-import-gc.yaml
+++ b/lld/test/COFF/pdb-import-gc.yaml
@@ -14,7 +14,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 48 | S_GDATA32 [size = 32] `__imp___wc_mb_cur`
+# CHECK-NEXT: 20 | S_GDATA32 [size = 32] `__imp___wc_mb_cur`
# CHECK-NEXT: type = 0x0070 (char), addr = 0000:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-local-constants.test b/lld/test/COFF/pdb-local-constants.test
index 8a4baad2938386..3a9538252ed802 100644
--- a/lld/test/COFF/pdb-local-constants.test
+++ b/lld/test/COFF/pdb-local-constants.test
@@ -13,7 +13,7 @@
#
CHECK: Global Symbols
-CHECK: 68 | S_CONSTANT [size = 20] `g_const`
+CHECK: 40 | S_CONSTANT [size = 20] `g_const`
CHECK-NEXT: type = 0x1002 (const int), value = 321
CHECK: Symbols
diff --git a/lld/test/COFF/pdb-publics-import.test b/lld/test/COFF/pdb-publics-import.test
index 08b8b66730889a..83f087b662186c 100644
--- a/lld/test/COFF/pdb-publics-import.test
+++ b/lld/test/COFF/pdb-publics-import.test
@@ -54,13 +54,11 @@ CHECK-NEXT: pdb file ni: 1 `{{.*}}pdb-publics-import.test.tmp2.pdb`, src file ni
CHECK: Public Symbols
CHECK-NEXT: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 64 | S_PUB32 [size = 28] `__lld_buildid`
-CHECK-NEXT: flags = none, addr = 0002:0028
-CHECK-NEXT: 140 | S_PUB32 [size = 20] `main`
+CHECK-NEXT: 112 | S_PUB32 [size = 20] `main`
CHECK-NEXT: flags = function, addr = 0001:0000
-CHECK-NEXT: 92 | S_PUB32 [size = 24] `exportfn1`
+CHECK-NEXT: 64 | S_PUB32 [size = 24] `exportfn1`
CHECK-NEXT: flags = function, addr = 0001:0016
-CHECK-NEXT: 116 | S_PUB32 [size = 24] `exportfn2`
+CHECK-NEXT: 88 | S_PUB32 [size = 24] `exportfn2`
CHECK-NEXT: flags = function, addr = 0001:0032
CHECK-NEXT: 32 | S_PUB32 [size = 32] `__imp_exportfn2`
CHECK-NEXT: flags = none, addr = 0002:0136
diff --git a/lld/test/COFF/pdb-safeseh.yaml b/lld/test/COFF/pdb-safeseh.yaml
index 6914c081cff9bf..cc7ddb19a49c64 100644
--- a/lld/test/COFF/pdb-safeseh.yaml
+++ b/lld/test/COFF/pdb-safeseh.yaml
@@ -10,7 +10,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 52 | S_GDATA32 [size = 40] `___safe_se_handler_table`
+# CHECK-NEXT: 20 | S_GDATA32 [size = 40] `___safe_se_handler_table`
# CHECK-NEXT: type = 0x0022 (unsigned long), addr = 0003:0000
--- !COFF
diff --git a/lld/test/COFF/pdb-secrel-absolute.yaml b/lld/test/COFF/pdb-secrel-absolute.yaml
index a8f83ec0c11bde..330106b3bbed0b 100644
--- a/lld/test/COFF/pdb-secrel-absolute.yaml
+++ b/lld/test/COFF/pdb-secrel-absolute.yaml
@@ -10,7 +10,7 @@
# CHECK: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 48 | S_GDATA32 [size = 36] `__guard_fids_table`
+# CHECK-NEXT: 20 | S_GDATA32 [size = 36] `__guard_fids_table`
# CHECK-NEXT: type = 0x0022 (unsigned long), addr = 0003:0000
--- !COFF
diff --git a/lld/test/COFF/pdb-symbol-types.yaml b/lld/test/COFF/pdb-symbol-types.yaml
index 9e046b2da6119b..d8741afd8cded4 100644
--- a/lld/test/COFF/pdb-symbol-types.yaml
+++ b/lld/test/COFF/pdb-symbol-types.yaml
@@ -16,13 +16,13 @@
# CHECK-LABEL: Global Symbols
# CHECK-NEXT: ============================================================
# CHECK-NEXT: Records
-# CHECK-NEXT: 76 | S_PROCREF [size = 20] `main`
+# CHECK-NEXT: 48 | S_PROCREF [size = 20] `main`
# CHECK-NEXT: module = 1, sum name = 0, offset = 116
-# CHECK-NEXT: 124 | S_UDT [size = 16] `UDT_Foo`
+# CHECK-NEXT: 96 | S_UDT [size = 16] `UDT_Foo`
# CHECK-NEXT: original type = 0x1004
-# CHECK-NEXT: 140 | S_UDT [size = 12] `Foo`
+# CHECK-NEXT: 112 | S_UDT [size = 12] `Foo`
# CHECK-NEXT: original type = 0x1004
-# CHECK-NEXT: 96 | S_GDATA32 [size = 28] `global_foo`
+# CHECK-NEXT: 68 | S_GDATA32 [size = 28] `global_foo`
# CHECK-NEXT: type = 0x1004 (Foo), addr = 0003:0000
# CHECK: Symbols
diff --git a/lld/test/COFF/pdb-type-server-guid-collision-valid.test b/lld/test/COFF/pdb-type-server-guid-collision-valid.test
index 23cfa0e0588bb3..a3819eb2788601 100644
--- a/lld/test/COFF/pdb-type-server-guid-collision-valid.test
+++ b/lld/test/COFF/pdb-type-server-guid-collision-valid.test
@@ -13,5 +13,5 @@ RUN: llvm-pdbutil dump -globals collision.pdb | FileCheck %s -check-prefix DUMP
DUMP-LABEL: Global Symbols
DUMP: ============================================================
-DUMP: 128 | S_GDATA32 [size = 24] `bar_gv`
+DUMP: 100 | S_GDATA32 [size = 24] `bar_gv`
DUMP-NEXT: type = 0x104E (Bar), addr = 0002:0004
diff --git a/lld/test/COFF/pdb-type-server-simple.test b/lld/test/COFF/pdb-type-server-simple.test
index e437eb27f26e8b..e9757d187e2f10 100644
--- a/lld/test/COFF/pdb-type-server-simple.test
+++ b/lld/test/COFF/pdb-type-server-simple.test
@@ -66,11 +66,11 @@ CHECK: {{.*}}: `b.c`
CHECK-LABEL: Global Symbols
CHECK: ============================================================
CHECK-NEXT: Records
-CHECK-NEXT: 64 | S_PROCREF [size = 20] `main`
+CHECK-NEXT: 36 | S_PROCREF [size = 20] `main`
CHECK-NEXT: module = 1, sum name = 0, offset = 104
-CHECK-NEXT: 96 | S_PROCREF [size = 16] `g`
+CHECK-NEXT: 68 | S_PROCREF [size = 16] `g`
CHECK-NEXT: module = 2, sum name = 0, offset = 104
-CHECK-NEXT: 84 | S_UDT [size = 12] `Foo`
+CHECK-NEXT: 56 | S_UDT [size = 12] `Foo`
CHECK-NEXT: original type = 0x1006
CHECK-LABEL: Symbols
@@ -112,7 +112,7 @@ SUMMARY-NEXT: 16 Merged IPI records
SUMMARY-NEXT: 3 Output PDB strings
SUMMARY-NEXT: 4 Global symbol records
SUMMARY-NEXT: 14 Module symbol records
-SUMMARY-NEXT: 3 Public symbol records
+SUMMARY-NEXT: 2 Public symbol records
SUMMARY: Top 10 types responsible for the most TPI input:
SUMMARY-NEXT: index total bytes count size
diff --git a/lld/test/COFF/pdb.test b/lld/test/COFF/pdb.test
index 6db5e79f95d10e..5492fececccd55 100644
--- a/lld/test/COFF/pdb.test
+++ b/lld/test/COFF/pdb.test
@@ -184,28 +184,23 @@ RAW-NEXT: 0x1006: ` -I"C:\Program Files (x86)\Windows Kits\8.1\includ
RAW: Public Symbols
RAW-NEXT: ============================================================
RAW-NEXT: Publics Header
-RAW-NEXT: sym hash = 568, thunk table addr = 0000:0000
+RAW-NEXT: sym hash = 556, thunk table addr = 0000:0000
RAW-NEXT: GSI Header
-RAW-NEXT: sig = 0xFFFFFFFF, hdr = 0xF12F091A, hr size = 24, num buckets = 528
+RAW-NEXT: sig = 0xFFFFFFFF, hdr = 0xF12F091A, hr size = 16, num buckets = 524
RAW-NEXT: Records
-RAW-NEXT: 0 | S_PUB32 [size = 28] `__lld_buildid`
-RAW-NEXT: flags = none, addr = 0002:0028
-RAW-NEXT: 48 | S_PUB32 [size = 20] `main`
+RAW-NEXT: 20 | S_PUB32 [size = 20] `main`
RAW-NEXT: flags = function, addr = 0001:0000
-RAW-NEXT: 28 | S_PUB32 [size = 20] `foo`
+RAW-NEXT: 0 | S_PUB32 [size = 20] `foo`
RAW-NEXT: flags = function, addr = 0001:0016
RAW-NOT: S_PUB32
RAW-NEXT: Hash Entries
+RAW-NEXT: off = 21, refcnt = 1
RAW-NEXT: off = 1, refcnt = 1
-RAW-NEXT: off = 49, refcnt = 1
-RAW-NEXT: off = 29, refcnt = 1
RAW-NEXT: Hash Buckets
RAW-NEXT: 0x00000000
RAW-NEXT: 0x0000000c
-RAW-NEXT: 0x00000018
RAW-NEXT: Address Map
-RAW-NEXT: off = 48
-RAW-NEXT: off = 28
+RAW-NEXT: off = 20
RAW-NEXT: off = 0
RAW: Section Headers
RAW-NEXT: ============================================================
diff --git a/lld/test/COFF/precomp-link.test b/lld/test/COFF/precomp-link.test
index 5985f16d12ccb9..53d981c4e62280 100644
--- a/lld/test/COFF/precomp-link.test
+++ b/lld/test/COFF/precomp-link.test
@@ -68,7 +68,7 @@ SUMMARY-NEXT: 170 Merged IPI records
SUMMARY-NEXT: 5 Output PDB strings
SUMMARY-NEXT: 167 Global symbol records
SUMMARY-NEXT: 20 Module symbol records
-SUMMARY-NEXT: 4 Public symbol records
+SUMMARY-NEXT: 3 Public symbol records
// precomp.h
#pragma once
diff --git a/lld/test/COFF/precomp-summary-fail.test b/lld/test/COFF/precomp-summary-fail.test
index dfafae8973ec88..5ebba9a1d3c747 100644
--- a/lld/test/COFF/precomp-summary-fail.test
+++ b/lld/test/COFF/precomp-summary-fail.test
@@ -21,4 +21,4 @@ SUMMARY-NEXT: 2 Merged IPI records
SUMMARY-NEXT: 1 Output PDB strings
SUMMARY-NEXT: 0 Global symbol records
SUMMARY-NEXT: 4 Module symbol records
-SUMMARY-NEXT: 1 Public symbol records
+SUMMARY-NEXT: 0 Public symbol records
>From 719a05d793c05f2bffdae1585e6885a22555c527 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Tue, 12 Dec 2023 16:53:42 -0500
Subject: [PATCH 4/7] Rename symbol to __build_guid
---
lld/COFF/Driver.cpp | 4 ++--
lld/COFF/Writer.cpp | 2 +-
lld/test/COFF/build-id-sym.s | 18 +++++++++---------
3 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 5ca2235ebca5a5..d0cbd77428862c 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2382,8 +2382,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
ctx.symtab.addAbsolute(mangle("__DTOR_LIST__"), 0);
}
if (config->debug || config->buildIDHash != BuildIDHash::None)
- if (ctx.symtab.findUnderscore("__lld_buildid"))
- ctx.symtab.addUndefined(mangle("__lld_buildid"));
+ if (ctx.symtab.findUnderscore("__build_guid"))
+ ctx.symtab.addUndefined(mangle("__build_guid"));
// 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/Writer.cpp b/lld/COFF/Writer.cpp
index 4bd1f0b24ba27c..0751a7d1faa14c 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1121,7 +1121,7 @@ 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("__lld_buildid"))
+ if (Symbol *buildidSym = ctx.symtab.findUnderscore("__build_guid"))
replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
buildId);
}
diff --git a/lld/test/COFF/build-id-sym.s b/lld/test/COFF/build-id-sym.s
index 61efbe97adfd8b..22bd84e087f1ef 100644
--- a/lld/test/COFF/build-id-sym.s
+++ b/lld/test/COFF/build-id-sym.s
@@ -3,21 +3,21 @@
# 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 __lld_buildid points to 0x140002038 which is the start of build id.
+# Check __build_guid points to 0x140002038 which is the start of build id.
# CHECK: SYMBOL TABLE:
-# CHECK-NEXT: 0x00000038 __lld_buildid
+# CHECK-NEXT: 0x00000038 __build_guid
# CHECK: Contents of section .rdata:
-# CHECK-NEXT: 140002000 00000000 21b9bff1 00000000 02000000 ....!...........
+# CHECK-NEXT: 140002000 00000000 dc1b27c9 00000000 02000000 ......'.........
# CHECK-NEXT: 140002010 19000000 38200000 38060000 00000000 ....8 ..8.......
-# CHECK-NEXT: 140002020 21b9bff1 00000000 10000000 00000000 !...............
-# CHECK-NEXT: 140002030 00000000 00000000 52534453 21b9bff1 ........RSDS!...
-# CHECK-NEXT: 140002040 0450a611 4c4c4420 5044422e 01000000 .P..LLD PDB.....
+# CHECK-NEXT: 140002020 dc1b27c9 00000000 10000000 00000000 ..'.............
+# CHECK-NEXT: 140002030 00000000 00000000 52534453 dc1b27c9 ........RSDS..'.
+# CHECK-NEXT: 140002040 eef6f7b6 4c4c4420 5044422e 01000000 ....LLD PDB.....
.globl main
main:
nop
-.section .bss,"bw",discard,__lld_buildid
-.global __lld_buildid
-__lld_buildid:
+.section .bss,"bw",discard,__build_guid
+.global __build_guid
+__build_guid:
>From a542f44d6233b5e5df57365c3c1d62e23b1b75d6 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 13 Dec 2023 11:49:01 -0500
Subject: [PATCH 5/7] Add an offset in DefinedSynthetic to allow symbols point
to chunk's RVA + offset address.
---
lld/COFF/Symbols.h | 7 ++++---
lld/COFF/Writer.cpp | 2 +-
lld/test/COFF/build-id-sym.s | 12 ++++++------
3 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index 750269fd0bbba7..6e7f21f0f17e31 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 0751a7d1faa14c..dda7f3229b6ef9 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1123,7 +1123,7 @@ void Writer::createMiscChunks() {
debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
if (Symbol *buildidSym = ctx.symtab.findUnderscore("__build_guid"))
replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
- buildId);
+ buildId, 4);
}
if (config->cetCompat) {
diff --git a/lld/test/COFF/build-id-sym.s b/lld/test/COFF/build-id-sym.s
index 22bd84e087f1ef..e85572eff48f76 100644
--- a/lld/test/COFF/build-id-sym.s
+++ b/lld/test/COFF/build-id-sym.s
@@ -3,16 +3,16 @@
# 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 __build_guid points to 0x140002038 which is the start of build id.
+# Check __build_guid points to 0x14000203c which is the start of build id.
# CHECK: SYMBOL TABLE:
-# CHECK-NEXT: 0x00000038 __build_guid
+# CHECK-NEXT: 0x0000003c __build_guid
# CHECK: Contents of section .rdata:
-# CHECK-NEXT: 140002000 00000000 dc1b27c9 00000000 02000000 ......'.........
+# CHECK-NEXT: 140002000 00000000 1c8e2e86 00000000 02000000 ................
# CHECK-NEXT: 140002010 19000000 38200000 38060000 00000000 ....8 ..8.......
-# CHECK-NEXT: 140002020 dc1b27c9 00000000 10000000 00000000 ..'.............
-# CHECK-NEXT: 140002030 00000000 00000000 52534453 dc1b27c9 ........RSDS..'.
-# CHECK-NEXT: 140002040 eef6f7b6 4c4c4420 5044422e 01000000 ....LLD PDB.....
+# CHECK-NEXT: 140002020 1c8e2e86 00000000 10000000 00000000 ................
+# CHECK-NEXT: 140002030 00000000 00000000 52534453 1c8e2e86 ........RSDS....
+# CHECK-NEXT: 140002040 2635cc06 4c4c4420 5044422e 01000000 &5..LLD PDB.....
.globl main
main:
>From 0a80f1fc5f22527d9e7a93be449203e0d5f465f6 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 13 Dec 2023 11:58:00 -0500
Subject: [PATCH 6/7] fixup! Add an offset in DefinedSynthetic to allow symbols
point to chunk's RVA + offset address.
Format
---
lld/COFF/Symbols.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index 6e7f21f0f17e31..ca69fb2d052706 100644
--- a/lld/COFF/Symbols.h
+++ b/lld/COFF/Symbols.h
@@ -278,7 +278,7 @@ 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() + offset: 0; }
+ uint32_t getRVA() { return c ? c->getRVA() + offset : 0; }
Chunk *getChunk() { return c; }
private:
>From e0378417c0a9bce64db89fd410252146fea1b851 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Thu, 14 Dec 2023 12:54:31 -0500
Subject: [PATCH 7/7] add docs for __build_guid symbol
---
lld/docs/windows_support.rst | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/lld/docs/windows_support.rst b/lld/docs/windows_support.rst
index 620040ee8196a6..e13034401aa0f3 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 ``__build_guid`` symbol pointing to the 16 bytes GUID hash if
+ there is a reference to it.
More information about the llvm-commits
mailing list