Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf genelf: Remove libcrypto dependency and use built-in sha1()

genelf is the only file in perf that depends on libcrypto (or openssl)
which only calculates a Build ID (SHA1, MD5, or URANDOM). SHA1 was
expected to be the default option, but MD5 was used by default due to
previous issues when linking against Java. This commit switches genelf
to use the in-house sha1(), and also removes MD5 and URANDOM options
since we have a reliable SHA1 implementation to rely on. It passes the
tools/perf/tests/shell/test_java_symbol.sh test.

Signed-off-by: Yuzhuo Jing <yuzhuo@google.com>
Co-developed-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250625202311.23244-4-ebiggers@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Yuzhuo Jing and committed by
Namhyung Kim
e3f612c1 43830468

+3 -82
+3 -82
tools/perf/util/genelf.c
··· 12 12 #include <libelf.h> 13 13 #include <string.h> 14 14 #include <stdlib.h> 15 - #include <unistd.h> 16 15 #include <inttypes.h> 17 - #include <fcntl.h> 18 16 #include <err.h> 19 17 #ifdef HAVE_LIBDW_SUPPORT 20 18 #include <dwarf.h> 21 19 #endif 22 20 23 21 #include "genelf.h" 22 + #include "sha1.h" 24 23 #include "../util/jitdump.h" 25 24 #include <linux/compiler.h> 26 25 27 26 #ifndef NT_GNU_BUILD_ID 28 27 #define NT_GNU_BUILD_ID 3 29 28 #endif 30 - 31 - #define BUILD_ID_URANDOM /* different uuid for each run */ 32 - 33 - #ifdef HAVE_LIBCRYPTO_SUPPORT 34 - 35 - #define BUILD_ID_MD5 36 - #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */ 37 - #undef BUILD_ID_URANDOM /* different uuid for each run */ 38 - 39 - #ifdef BUILD_ID_SHA 40 - #include <openssl/sha.h> 41 - #endif 42 - 43 - #ifdef BUILD_ID_MD5 44 - #include <openssl/evp.h> 45 - #include <openssl/md5.h> 46 - #endif 47 - #endif 48 - 49 29 50 30 typedef struct { 51 31 unsigned int namesz; /* Size of entry's owner string */ ··· 51 71 static struct buildid_note { 52 72 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */ 53 73 char name[4]; /* GNU\0 */ 54 - char build_id[20]; 74 + u8 build_id[SHA1_DIGEST_SIZE]; 55 75 } bnote; 56 76 57 77 static Elf_Sym symtab[]={ ··· 71 91 .st_size = 0, /* for now */ 72 92 } 73 93 }; 74 - 75 - #ifdef BUILD_ID_URANDOM 76 - static void 77 - gen_build_id(struct buildid_note *note, 78 - unsigned long load_addr __maybe_unused, 79 - const void *code __maybe_unused, 80 - size_t csize __maybe_unused) 81 - { 82 - int fd; 83 - size_t sz = sizeof(note->build_id); 84 - ssize_t sret; 85 - 86 - fd = open("/dev/urandom", O_RDONLY); 87 - if (fd == -1) 88 - err(1, "cannot access /dev/urandom for buildid"); 89 - 90 - sret = read(fd, note->build_id, sz); 91 - 92 - close(fd); 93 - 94 - if (sret != (ssize_t)sz) 95 - memset(note->build_id, 0, sz); 96 - } 97 - #endif 98 - 99 - #ifdef BUILD_ID_SHA 100 - static void 101 - gen_build_id(struct buildid_note *note, 102 - unsigned long load_addr __maybe_unused, 103 - const void *code, 104 - size_t csize) 105 - { 106 - if (sizeof(note->build_id) < SHA_DIGEST_LENGTH) 107 - errx(1, "build_id too small for SHA1"); 108 - 109 - SHA1(code, csize, (unsigned char *)note->build_id); 110 - } 111 - #endif 112 - 113 - #ifdef BUILD_ID_MD5 114 - static void 115 - gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize) 116 - { 117 - EVP_MD_CTX *mdctx; 118 - 119 - if (sizeof(note->build_id) < 16) 120 - errx(1, "build_id too small for MD5"); 121 - 122 - mdctx = EVP_MD_CTX_new(); 123 - if (!mdctx) 124 - errx(2, "failed to create EVP_MD_CTX"); 125 - 126 - EVP_DigestInit_ex(mdctx, EVP_md5(), NULL); 127 - EVP_DigestUpdate(mdctx, &load_addr, sizeof(load_addr)); 128 - EVP_DigestUpdate(mdctx, code, csize); 129 - EVP_DigestFinal_ex(mdctx, (unsigned char *)note->build_id, NULL); 130 - EVP_MD_CTX_free(mdctx); 131 - } 132 - #endif 133 94 134 95 static int 135 96 jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size, ··· 394 473 /* 395 474 * build-id generation 396 475 */ 397 - gen_build_id(&bnote, load_addr, code, csize); 476 + sha1(code, csize, bnote.build_id); 398 477 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */ 399 478 bnote.desc.descsz = sizeof(bnote.build_id); 400 479 bnote.desc.type = NT_GNU_BUILD_ID;