diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -451,6 +451,9 @@ // _TLS_MODULE_BASE_ on targets that support TLSDESC. static Defined *tlsModuleBase; + + // __build_id symbol. + static Defined *buildId; }; // A buffer class that is large enough to hold any Symbol-derived diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -52,6 +52,7 @@ Defined *ElfSym::relaIpltEnd; Defined *ElfSym::riscvGlobalPointer; Defined *ElfSym::tlsModuleBase; +Defined *ElfSym::buildId; static uint64_t getSymVA(const Symbol &sym, int64_t &addend) { switch (sym.kind()) { diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -287,6 +287,9 @@ // different in different DSOs, so we chose the start address of the DSO. addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN); + if (config->buildId != BuildIdKind::None) + ElfSym::buildId = addOptionalRegular("__build_id", nullptr, 0, STV_HIDDEN); + // If linker script do layout we do not need to create any standard symbols. if (script->hasSectionsCommand) return; @@ -1122,6 +1125,10 @@ } } } + + // __build_id points to the build ID section. + if (ElfSym::buildId) + ElfSym::buildId->section = mainPart->buildId; } // We want to find how similar two ranks are. diff --git a/lld/test/ELF/build-id.s b/lld/test/ELF/build-id.s --- a/lld/test/ELF/build-id.s +++ b/lld/test/ELF/build-id.s @@ -5,6 +5,9 @@ # RUN: ld.lld --build-id %t -o %t2 # RUN: llvm-readobj -S %t2 | FileCheck -check-prefix=ALIGN %s +# RUN: ld.lld --build-id %t -o %t2 +# RUN: llvm-objdump -t -h %t2 | FileCheck --check-prefix=SYMBOL %s + # RUN: ld.lld --build-id %t -o %t2 -threads # RUN: llvm-objdump -s %t2 | FileCheck --check-prefix=DEFAULT %s # RUN: ld.lld --build-id=fast %t -o %t2 -threads @@ -50,6 +53,8 @@ .section .note.test, "a", @note .quad 42 +.global __build_id + # ALIGN: Name: .note.gnu.build-id # ALIGN-NEXT: Type: SHT_NOTE # ALIGN-NEXT: Flags [ @@ -62,18 +67,24 @@ # ALIGN-NEXT: Info: # ALIGN-NEXT: AddressAlignment: 4 +# SYMBOL: Sections: +# SYMBOL: Idx Name Size VMA Type +# SYMBOL: 2 .note.gnu.build-id 00000018 [[ADDR:[0-9a-f]+]] +# SYMBOL: SYMBOL TABLE: +# SYMBOL: [[ADDR]] l .note.gnu.build-id 0000000000000000 .hidden __build_id + # DEFAULT: Contents of section .note.test: # DEFAULT: Contents of section .note.gnu.build-id: # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU. -# DEFAULT-NEXT: 7e8ddeff 3ed41fa3 +# DEFAULT-NEXT: 8c3e113c 431ad49e # MD5: Contents of section .note.gnu.build-id: # MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU. -# MD5-NEXT: 7b00fd9e 054ceb4b 06f64d0e 482cb476 +# MD5-NEXT: d4eb8a29 e12cc877 2bd79cd9 f237b3cb # SHA1: Contents of section .note.gnu.build-id: # SHA1-NEXT: 04000000 14000000 03000000 474e5500 ............GNU. -# SHA1-NEXT: 221a99da dd1d2bf3 05e48a91 dde8a0cb +# SHA1-NEXT: 2f6c2fa7 55e6345f db61c4cd 5d554ba2 # UUID: Contents of section .note.gnu.build-id: # UUID-NEXT: 04000000 10000000 03000000 474e5500 ............GNU.