Index: lld/ELF/Symbols.h =================================================================== --- lld/ELF/Symbols.h +++ lld/ELF/Symbols.h @@ -456,6 +456,10 @@ // _TLS_MODULE_BASE_ on targets that support TLSDESC. static Defined *tlsModuleBase; + + // __build_id_start and __build_id_end symbols. + static Defined *buildIdStart; + static Defined *buildIdEnd; }; // A buffer class that is large enough to hold any Symbol-derived Index: lld/ELF/Symbols.cpp =================================================================== --- lld/ELF/Symbols.cpp +++ lld/ELF/Symbols.cpp @@ -65,6 +65,8 @@ Defined *ElfSym::riscvGlobalPointer; Defined *ElfSym::tlsModuleBase; DenseMap elf::backwardReferences; +Defined *ElfSym::buildIdStart; +Defined *ElfSym::buildIdEnd; static uint64_t getSymVA(const Symbol &sym, int64_t &addend) { switch (sym.kind()) { Index: lld/ELF/SyntheticSections.h =================================================================== --- lld/ELF/SyntheticSections.h +++ lld/ELF/SyntheticSections.h @@ -160,10 +160,10 @@ // .note.gnu.build-id section. class BuildIdSection : public SyntheticSection { +public: // First 16 bytes are a header. static const unsigned headerSize = 16; -public: const size_t hashSize; BuildIdSection(); void writeTo(uint8_t *buf) override; Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -310,6 +310,11 @@ // 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::buildIdStart = addOptionalRegular("__build_id_start", nullptr, 0, STV_HIDDEN); + ElfSym::buildIdEnd = addOptionalRegular("__build_id_end", nullptr, 0, STV_HIDDEN); + } + // If linker script do layout we do not need to create any standard symbols. if (script->hasSectionsCommand) return; @@ -1193,6 +1198,14 @@ } } } + + // __build_id_start and __build_id_end points to the build ID. + if (config->buildId != BuildIdKind::None) { + ElfSym::buildIdStart->section = mainPart->buildId; + ElfSym::buildIdStart->value = BuildIdSection::headerSize; + ElfSym::buildIdEnd->section = mainPart->buildId; + ElfSym::buildIdEnd->value = mainPart->buildId->getSize(); + } } // We want to find how similar two ranks are. Index: lld/test/ELF/build-id.s =================================================================== --- lld/test/ELF/build-id.s +++ lld/test/ELF/build-id.s @@ -4,8 +4,9 @@ # RUN: ld.lld --build-id %t -o %t2 # RUN: llvm-readobj -S %t2 | FileCheck -check-prefix=ALIGN %s +# RUN: llvm-readelf -S -s %t2 | FileCheck --check-prefix=SYMBOL %s -# RUN: ld.lld --build-id %t -o %t2 +# 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 # RUN: llvm-objdump -s %t2 | FileCheck --check-prefix=DEFAULT %s @@ -50,6 +51,9 @@ .section .note.test, "a", @note .quad 42 +.global __build_id_start +.global __build_id_end + # ALIGN: Name: .note.gnu.build-id # ALIGN-NEXT: Type: SHT_NOTE # ALIGN-NEXT: Flags [ @@ -62,18 +66,22 @@ # ALIGN-NEXT: Info: # ALIGN-NEXT: AddressAlignment: 4 +# SYMBOL: .note.gnu.build-id NOTE [[#%x,ADDR:]] +# SYMBOL: [[#ADDR+24]] 0 NOTYPE LOCAL HIDDEN 2 __build_id_end +# SYMBOL: [[#ADDR+16]] 0 NOTYPE LOCAL HIDDEN 2 __build_id_start + # 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.