Index: lld/ELF/SyntheticSections.cpp =================================================================== --- lld/ELF/SyntheticSections.cpp +++ lld/ELF/SyntheticSections.cpp @@ -368,7 +368,7 @@ }); break; case BuildIdKind::Uuid: - if (auto EC = getRandomBytes(HashBuf, HashSize)) + if (auto EC = getUuidV4(HashBuf)) error("entropy source failure: " + EC.message()); break; case BuildIdKind::Hexstring: Index: llvm/include/llvm/Support/RandomNumberGenerator.h =================================================================== --- llvm/include/llvm/Support/RandomNumberGenerator.h +++ llvm/include/llvm/Support/RandomNumberGenerator.h @@ -65,6 +65,9 @@ // Get random vector of specified size std::error_code getRandomBytes(void *Buffer, size_t Size); + +// Get UUID v4. +std::error_code getUuidV4(void *Buffer); } #endif Index: llvm/lib/Support/RandomNumberGenerator.cpp =================================================================== --- llvm/lib/Support/RandomNumberGenerator.cpp +++ llvm/lib/Support/RandomNumberGenerator.cpp @@ -89,3 +89,15 @@ return std::error_code(errno, std::system_category()); #endif } + +// UUID v4 is a 122-bit random number in the form of +// RRRRRRRR-RRRR-4RRR-xRRR-RRRRRRRRRRRR, where R is a random byte and +// x's most siginificant two bits are 0b10. +std::error_code llvm::getUuidV4(void *Buffer) { + if (auto EC = getRandomBytes(Buffer, 16)) + return EC; + char *Buf = (char *)Buffer; + Buf[7] = 0x40 | (Buf[7] & 0xf); + Buf[9] = 0x80 | (Buf[9] & 0x3f); + return std::error_code(); +}