1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/buildid.h>
5 #include <linux/pagemap.h>
9 * Parse build id from the note segment. This logic can be shared between
10 * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
13 static inline int parse_build_id(void *page_addr,
14 unsigned char *build_id,
19 Elf32_Word note_offs = 0, new_offs;
21 /* check for overflow */
22 if (note_start < page_addr || note_start + note_size < note_start)
25 /* only supports note that fits in the first page */
26 if (note_start + note_size > page_addr + PAGE_SIZE)
29 while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
30 Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
32 if (nhdr->n_type == BUILD_ID &&
33 nhdr->n_namesz == sizeof("GNU") &&
35 nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
37 note_start + note_offs +
38 ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
40 memset(build_id + nhdr->n_descsz, 0,
41 BUILD_ID_SIZE_MAX - nhdr->n_descsz);
43 *size = nhdr->n_descsz;
46 new_offs = note_offs + sizeof(Elf32_Nhdr) +
47 ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
48 if (new_offs <= note_offs) /* overflow */
55 /* Parse build ID from 32-bit ELF */
56 static int get_build_id_32(void *page_addr, unsigned char *build_id,
59 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
63 /* only supports phdr that fits in one page */
65 (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
68 phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
70 for (i = 0; i < ehdr->e_phnum; ++i) {
71 if (phdr[i].p_type == PT_NOTE &&
72 !parse_build_id(page_addr, build_id, size,
73 page_addr + phdr[i].p_offset,
80 /* Parse build ID from 64-bit ELF */
81 static int get_build_id_64(void *page_addr, unsigned char *build_id,
84 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
88 /* only supports phdr that fits in one page */
90 (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
93 phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
95 for (i = 0; i < ehdr->e_phnum; ++i) {
96 if (phdr[i].p_type == PT_NOTE &&
97 !parse_build_id(page_addr, build_id, size,
98 page_addr + phdr[i].p_offset,
106 * Parse build ID of ELF file mapped to vma
108 * @build_id: buffer to store build id, at least BUILD_ID_SIZE long
109 * @size: returns actual build id size in case of success
111 * Returns 0 on success, otherwise error (< 0).
113 int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
121 /* only works for page backed storage */
125 page = find_get_page(vma->vm_file->f_mapping, 0);
127 return -EFAULT; /* page not mapped */
130 page_addr = kmap_atomic(page);
131 ehdr = (Elf32_Ehdr *)page_addr;
133 /* compare magic x7f "ELF" */
134 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
137 /* only support executable file and shared object file */
138 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
141 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
142 ret = get_build_id_32(page_addr, build_id, size);
143 else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
144 ret = get_build_id_64(page_addr, build_id, size);
146 kunmap_atomic(page_addr);