]> Git Repo - linux.git/blame - lib/buildid.c
drm/v3d: Use v3d_perfmon_find()
[linux.git] / lib / buildid.c
CommitLineData
bd7525da
JO
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/buildid.h>
83cc6fa0 4#include <linux/cache.h>
bd7525da 5#include <linux/elf.h>
7eaf3cf3 6#include <linux/kernel.h>
bd7525da
JO
7#include <linux/pagemap.h>
8
9#define BUILD_ID 3
7eaf3cf3 10
bd7525da
JO
11/*
12 * Parse build id from the note segment. This logic can be shared between
13 * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are
14 * identical.
15 */
7eaf3cf3
SB
16static int parse_build_id_buf(unsigned char *build_id,
17 __u32 *size,
18 const void *note_start,
19 Elf32_Word note_size)
bd7525da
JO
20{
21 Elf32_Word note_offs = 0, new_offs;
22
bd7525da
JO
23 while (note_offs + sizeof(Elf32_Nhdr) < note_size) {
24 Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs);
25
26 if (nhdr->n_type == BUILD_ID &&
27 nhdr->n_namesz == sizeof("GNU") &&
a010d79b 28 !strcmp((char *)(nhdr + 1), "GNU") &&
bd7525da
JO
29 nhdr->n_descsz > 0 &&
30 nhdr->n_descsz <= BUILD_ID_SIZE_MAX) {
31 memcpy(build_id,
32 note_start + note_offs +
33 ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr),
34 nhdr->n_descsz);
35 memset(build_id + nhdr->n_descsz, 0,
36 BUILD_ID_SIZE_MAX - nhdr->n_descsz);
921f88fc
JO
37 if (size)
38 *size = nhdr->n_descsz;
bd7525da
JO
39 return 0;
40 }
41 new_offs = note_offs + sizeof(Elf32_Nhdr) +
42 ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4);
43 if (new_offs <= note_offs) /* overflow */
44 break;
45 note_offs = new_offs;
46 }
7eaf3cf3 47
bd7525da
JO
48 return -EINVAL;
49}
50
60eec326 51static inline int parse_build_id(const void *page_addr,
7eaf3cf3
SB
52 unsigned char *build_id,
53 __u32 *size,
60eec326 54 const void *note_start,
7eaf3cf3
SB
55 Elf32_Word note_size)
56{
57 /* check for overflow */
58 if (note_start < page_addr || note_start + note_size < note_start)
59 return -EINVAL;
60
61 /* only supports note that fits in the first page */
62 if (note_start + note_size > page_addr + PAGE_SIZE)
63 return -EINVAL;
64
65 return parse_build_id_buf(build_id, size, note_start, note_size);
66}
67
bd7525da 68/* Parse build ID from 32-bit ELF */
60eec326 69static int get_build_id_32(const void *page_addr, unsigned char *build_id,
921f88fc 70 __u32 *size)
bd7525da
JO
71{
72 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
73 Elf32_Phdr *phdr;
74 int i;
75
961a2851
AD
76 /*
77 * FIXME
78 * Neither ELF spec nor ELF loader require that program headers
79 * start immediately after ELF header.
80 */
81 if (ehdr->e_phoff != sizeof(Elf32_Ehdr))
82 return -EINVAL;
bd7525da
JO
83 /* only supports phdr that fits in one page */
84 if (ehdr->e_phnum >
85 (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
86 return -EINVAL;
87
88 phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
89
90 for (i = 0; i < ehdr->e_phnum; ++i) {
91 if (phdr[i].p_type == PT_NOTE &&
921f88fc 92 !parse_build_id(page_addr, build_id, size,
bd7525da
JO
93 page_addr + phdr[i].p_offset,
94 phdr[i].p_filesz))
95 return 0;
96 }
97 return -EINVAL;
98}
99
100/* Parse build ID from 64-bit ELF */
60eec326 101static int get_build_id_64(const void *page_addr, unsigned char *build_id,
921f88fc 102 __u32 *size)
bd7525da
JO
103{
104 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
105 Elf64_Phdr *phdr;
106 int i;
107
961a2851
AD
108 /*
109 * FIXME
110 * Neither ELF spec nor ELF loader require that program headers
111 * start immediately after ELF header.
112 */
113 if (ehdr->e_phoff != sizeof(Elf64_Ehdr))
114 return -EINVAL;
bd7525da
JO
115 /* only supports phdr that fits in one page */
116 if (ehdr->e_phnum >
117 (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
118 return -EINVAL;
119
120 phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
121
122 for (i = 0; i < ehdr->e_phnum; ++i) {
123 if (phdr[i].p_type == PT_NOTE &&
921f88fc 124 !parse_build_id(page_addr, build_id, size,
bd7525da
JO
125 page_addr + phdr[i].p_offset,
126 phdr[i].p_filesz))
127 return 0;
128 }
129 return -EINVAL;
130}
131
921f88fc
JO
132/*
133 * Parse build ID of ELF file mapped to vma
134 * @vma: vma object
135 * @build_id: buffer to store build id, at least BUILD_ID_SIZE long
136 * @size: returns actual build id size in case of success
137 *
3f14d029 138 * Return: 0 on success, -EINVAL otherwise
921f88fc
JO
139 */
140int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id,
141 __u32 *size)
bd7525da
JO
142{
143 Elf32_Ehdr *ehdr;
144 struct page *page;
145 void *page_addr;
146 int ret;
147
148 /* only works for page backed storage */
149 if (!vma->vm_file)
150 return -EINVAL;
151
152 page = find_get_page(vma->vm_file->f_mapping, 0);
153 if (!page)
154 return -EFAULT; /* page not mapped */
155
156 ret = -EINVAL;
c44f063e 157 page_addr = kmap_local_page(page);
bd7525da
JO
158 ehdr = (Elf32_Ehdr *)page_addr;
159
160 /* compare magic x7f "ELF" */
161 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
162 goto out;
163
164 /* only support executable file and shared object file */
165 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
166 goto out;
167
168 if (ehdr->e_ident[EI_CLASS] == ELFCLASS32)
921f88fc 169 ret = get_build_id_32(page_addr, build_id, size);
bd7525da 170 else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
921f88fc 171 ret = get_build_id_64(page_addr, build_id, size);
bd7525da 172out:
c44f063e 173 kunmap_local(page_addr);
bd7525da
JO
174 put_page(page);
175 return ret;
176}
7eaf3cf3
SB
177
178/**
179 * build_id_parse_buf - Get build ID from a buffer
70e79866 180 * @buf: ELF note section(s) to parse
7eaf3cf3
SB
181 * @buf_size: Size of @buf in bytes
182 * @build_id: Build ID parsed from @buf, at least BUILD_ID_SIZE_MAX long
183 *
184 * Return: 0 on success, -EINVAL otherwise
185 */
186int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
187{
188 return parse_build_id_buf(build_id, NULL, buf, buf_size);
189}
83cc6fa0 190
443cbaf9 191#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_VMCORE_INFO)
83cc6fa0
SB
192unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init;
193
194/**
195 * init_vmlinux_build_id - Compute and stash the running kernel's build ID
196 */
197void __init init_vmlinux_build_id(void)
198{
377d9095
AB
199 extern const void __start_notes;
200 extern const void __stop_notes;
83cc6fa0
SB
201 unsigned int size = &__stop_notes - &__start_notes;
202
203 build_id_parse_buf(&__start_notes, vmlinux_build_id, size);
204}
22f4e66d 205#endif
This page took 0.212875 seconds and 4 git commands to generate.