[PATCH 3/7] buildid: Add API to parse build ID out of buffer

From: Stephen Boyd
Date: Mon Mar 01 2021 - 18:05:11 EST


Add an API that can parse the build ID in hex string form out of a
buffer to support printing a kernel module's build ID.

Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Jessica Yu <jeyu@xxxxxxxxxx>
Cc: Evan Green <evgreen@xxxxxxxxxxxx>
Cc: Hsin-Yi Wang <hsinyi@xxxxxxxxxxxx>
Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
---
include/linux/buildid.h | 1 +
lib/buildid.c | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+)

diff --git a/include/linux/buildid.h b/include/linux/buildid.h
index dd134a96a87c..f5a5b920d18c 100644
--- a/include/linux/buildid.h
+++ b/include/linux/buildid.h
@@ -10,6 +10,7 @@
int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
__u32 *size);

+int build_id_parse_buf(const void *buf, char *build_id, u32 buf_size);
const char *vmlinux_build_id(void);

#endif
diff --git a/lib/buildid.c b/lib/buildid.c
index 57daf928b133..4e1d7c51dc5f 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -165,6 +165,26 @@ static void build_id2hex(char *dst, const unsigned char *src, __u32 size)
dst[2 * size] = '\0';
}

+/**
+ * build_id_parse_buf - Get build ID in hex string format from elf section note
+ * @buf: Elf note section(s) to parse
+ * @build_id: Build ID in hex string format parsed from @buf
+ * @buf_size: Size of @buf in bytes
+ *
+ * Return: 0 on success, -EINVAL otherwise
+ */
+int build_id_parse_buf(const void *buf, char *build_id, u32 buf_size)
+{
+ unsigned char build_id_buf[BUILD_ID_SIZE_MAX];
+ __u32 size;
+ int ret;
+
+ ret = parse_build_id_buf(build_id_buf, &size, buf, buf_size);
+ if (!ret)
+ build_id2hex(build_id, build_id_buf, size);
+
+ return ret;
+}
/**
* vmlinux_build_id - Get the running kernel's build-id in hex string format
*
--
https://chromeos.dev