Merge tag 'powerpc-5.14-4' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-2.6-microblaze.git] / lib / buildid.c
index 6156997..dfc6262 100644 (file)
@@ -1,36 +1,31 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/buildid.h>
+#include <linux/cache.h>
 #include <linux/elf.h>
+#include <linux/kernel.h>
 #include <linux/pagemap.h>
 
 #define BUILD_ID 3
+
 /*
  * Parse build id from the note segment. This logic can be shared between
  * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
  * identical.
  */
-static inline int parse_build_id(void *page_addr,
-                                unsigned char *build_id,
-                                __u32 *size,
-                                void *note_start,
-                                Elf32_Word note_size)
+static int parse_build_id_buf(unsigned char *build_id,
+                             __u32 *size,
+                             const void *note_start,
+                             Elf32_Word note_size)
 {
        Elf32_Word note_offs = 0, new_offs;
 
-       /* check for overflow */
-       if (note_start < page_addr || note_start + note_size < note_start)
-               return -EINVAL;
-
-       /* only supports note that fits in the first page */
-       if (note_start + note_size > page_addr + PAGE_SIZE)
-               return -EINVAL;
-
        while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
                Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
 
                if (nhdr->n_type == BUILD_ID &&
                    nhdr->n_namesz == sizeof("GNU") &&
+                   !strcmp((char *)(nhdr + 1), "GNU") &&
                    nhdr->n_descsz > 0 &&
                    nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
                        memcpy(build_id,
@@ -49,11 +44,29 @@ static inline int parse_build_id(void *page_addr,
                        break;
                note_offs = new_offs;
        }
+
        return -EINVAL;
 }
 
+static inline int parse_build_id(const void *page_addr,
+                                unsigned char *build_id,
+                                __u32 *size,
+                                const void *note_start,
+                                Elf32_Word note_size)
+{
+       /* check for overflow */
+       if (note_start < page_addr || note_start + note_size < note_start)
+               return -EINVAL;
+
+       /* only supports note that fits in the first page */
+       if (note_start + note_size > page_addr + PAGE_SIZE)
+               return -EINVAL;
+
+       return parse_build_id_buf(build_id, size, note_start, note_size);
+}
+
 /* Parse build ID from 32-bit ELF */
-static int get_build_id_32(void *page_addr, unsigned char *build_id,
+static int get_build_id_32(const void *page_addr, unsigned char *build_id,
                           __u32 *size)
 {
        Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
@@ -78,7 +91,7 @@ static int get_build_id_32(void *page_addr, unsigned char *build_id,
 }
 
 /* Parse build ID from 64-bit ELF */
-static int get_build_id_64(void *page_addr, unsigned char *build_id,
+static int get_build_id_64(const void *page_addr, unsigned char *build_id,
                           __u32 *size)
 {
        Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
@@ -108,7 +121,7 @@ static int get_build_id_64(void *page_addr, unsigned char *build_id,
  * @build_id: buffer to store build id, at least BUILD_ID_SIZE long
  * @size:     returns actual build id size in case of success
  *
- * Returns 0 on success, otherwise error (< 0).
+ * Return: 0 on success, -EINVAL otherwise
  */
 int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
                   __u32 *size)
@@ -147,3 +160,32 @@ out:
        put_page(page);
        return ret;
 }
+
+/**
+ * build_id_parse_buf - Get build ID from a buffer
+ * @buf:      Elf note section(s) to parse
+ * @buf_size: Size of @buf in bytes
+ * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long
+ *
+ * Return: 0 on success, -EINVAL otherwise
+ */
+int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
+{
+       return parse_build_id_buf(build_id, NULL, buf, buf_size);
+}
+
+#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_CRASH_CORE)
+unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
+
+/**
+ * init_vmlinux_build_id - Compute and stash the running kernel's build ID
+ */
+void __init init_vmlinux_build_id(void)
+{
+       extern const void __start_notes __weak;
+       extern const void __stop_notes __weak;
+       unsigned int size = &__stop_notes - &__start_notes;
+
+       build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
+}
+#endif