xref: /openbmc/linux/tools/perf/util/symbol-minimal.c (revision e553d2a5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "dso.h"
3 #include "symbol.h"
4 #include "symsrc.h"
5 #include "util.h"
6 
7 #include <errno.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <byteswap.h>
14 #include <sys/stat.h>
15 #include <linux/zalloc.h>
16 
17 static bool check_need_swap(int file_endian)
18 {
19 	const int data = 1;
20 	u8 *check = (u8 *)&data;
21 	int host_endian;
22 
23 	if (check[0] == 1)
24 		host_endian = ELFDATA2LSB;
25 	else
26 		host_endian = ELFDATA2MSB;
27 
28 	return host_endian != file_endian;
29 }
30 
31 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
32 
33 #define NT_GNU_BUILD_ID	3
34 
35 static int read_build_id(void *note_data, size_t note_len, void *bf,
36 			 size_t size, bool need_swap)
37 {
38 	struct {
39 		u32 n_namesz;
40 		u32 n_descsz;
41 		u32 n_type;
42 	} *nhdr;
43 	void *ptr;
44 
45 	ptr = note_data;
46 	while (ptr < (note_data + note_len)) {
47 		const char *name;
48 		size_t namesz, descsz;
49 
50 		nhdr = ptr;
51 		if (need_swap) {
52 			nhdr->n_namesz = bswap_32(nhdr->n_namesz);
53 			nhdr->n_descsz = bswap_32(nhdr->n_descsz);
54 			nhdr->n_type = bswap_32(nhdr->n_type);
55 		}
56 
57 		namesz = NOTE_ALIGN(nhdr->n_namesz);
58 		descsz = NOTE_ALIGN(nhdr->n_descsz);
59 
60 		ptr += sizeof(*nhdr);
61 		name = ptr;
62 		ptr += namesz;
63 		if (nhdr->n_type == NT_GNU_BUILD_ID &&
64 		    nhdr->n_namesz == sizeof("GNU")) {
65 			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
66 				size_t sz = min(size, descsz);
67 				memcpy(bf, ptr, sz);
68 				memset(bf + sz, 0, size - sz);
69 				return 0;
70 			}
71 		}
72 		ptr += descsz;
73 	}
74 
75 	return -1;
76 }
77 
78 int filename__read_debuglink(const char *filename __maybe_unused,
79 			     char *debuglink __maybe_unused,
80 			     size_t size __maybe_unused)
81 {
82 	return -1;
83 }
84 
85 /*
86  * Just try PT_NOTE header otherwise fails
87  */
88 int filename__read_build_id(const char *filename, void *bf, size_t size)
89 {
90 	FILE *fp;
91 	int ret = -1;
92 	bool need_swap = false;
93 	u8 e_ident[EI_NIDENT];
94 	size_t buf_size;
95 	void *buf;
96 	int i;
97 
98 	fp = fopen(filename, "r");
99 	if (fp == NULL)
100 		return -1;
101 
102 	if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
103 		goto out;
104 
105 	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
106 	    e_ident[EI_VERSION] != EV_CURRENT)
107 		goto out;
108 
109 	need_swap = check_need_swap(e_ident[EI_DATA]);
110 
111 	/* for simplicity */
112 	fseek(fp, 0, SEEK_SET);
113 
114 	if (e_ident[EI_CLASS] == ELFCLASS32) {
115 		Elf32_Ehdr ehdr;
116 		Elf32_Phdr *phdr;
117 
118 		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
119 			goto out;
120 
121 		if (need_swap) {
122 			ehdr.e_phoff = bswap_32(ehdr.e_phoff);
123 			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
124 			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
125 		}
126 
127 		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
128 		buf = malloc(buf_size);
129 		if (buf == NULL)
130 			goto out;
131 
132 		fseek(fp, ehdr.e_phoff, SEEK_SET);
133 		if (fread(buf, buf_size, 1, fp) != 1)
134 			goto out_free;
135 
136 		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
137 			void *tmp;
138 			long offset;
139 
140 			if (need_swap) {
141 				phdr->p_type = bswap_32(phdr->p_type);
142 				phdr->p_offset = bswap_32(phdr->p_offset);
143 				phdr->p_filesz = bswap_32(phdr->p_filesz);
144 			}
145 
146 			if (phdr->p_type != PT_NOTE)
147 				continue;
148 
149 			buf_size = phdr->p_filesz;
150 			offset = phdr->p_offset;
151 			tmp = realloc(buf, buf_size);
152 			if (tmp == NULL)
153 				goto out_free;
154 
155 			buf = tmp;
156 			fseek(fp, offset, SEEK_SET);
157 			if (fread(buf, buf_size, 1, fp) != 1)
158 				goto out_free;
159 
160 			ret = read_build_id(buf, buf_size, bf, size, need_swap);
161 			if (ret == 0)
162 				ret = size;
163 			break;
164 		}
165 	} else {
166 		Elf64_Ehdr ehdr;
167 		Elf64_Phdr *phdr;
168 
169 		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
170 			goto out;
171 
172 		if (need_swap) {
173 			ehdr.e_phoff = bswap_64(ehdr.e_phoff);
174 			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
175 			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
176 		}
177 
178 		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
179 		buf = malloc(buf_size);
180 		if (buf == NULL)
181 			goto out;
182 
183 		fseek(fp, ehdr.e_phoff, SEEK_SET);
184 		if (fread(buf, buf_size, 1, fp) != 1)
185 			goto out_free;
186 
187 		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
188 			void *tmp;
189 			long offset;
190 
191 			if (need_swap) {
192 				phdr->p_type = bswap_32(phdr->p_type);
193 				phdr->p_offset = bswap_64(phdr->p_offset);
194 				phdr->p_filesz = bswap_64(phdr->p_filesz);
195 			}
196 
197 			if (phdr->p_type != PT_NOTE)
198 				continue;
199 
200 			buf_size = phdr->p_filesz;
201 			offset = phdr->p_offset;
202 			tmp = realloc(buf, buf_size);
203 			if (tmp == NULL)
204 				goto out_free;
205 
206 			buf = tmp;
207 			fseek(fp, offset, SEEK_SET);
208 			if (fread(buf, buf_size, 1, fp) != 1)
209 				goto out_free;
210 
211 			ret = read_build_id(buf, buf_size, bf, size, need_swap);
212 			if (ret == 0)
213 				ret = size;
214 			break;
215 		}
216 	}
217 out_free:
218 	free(buf);
219 out:
220 	fclose(fp);
221 	return ret;
222 }
223 
224 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
225 {
226 	int fd;
227 	int ret = -1;
228 	struct stat stbuf;
229 	size_t buf_size;
230 	void *buf;
231 
232 	fd = open(filename, O_RDONLY);
233 	if (fd < 0)
234 		return -1;
235 
236 	if (fstat(fd, &stbuf) < 0)
237 		goto out;
238 
239 	buf_size = stbuf.st_size;
240 	buf = malloc(buf_size);
241 	if (buf == NULL)
242 		goto out;
243 
244 	if (read(fd, buf, buf_size) != (ssize_t) buf_size)
245 		goto out_free;
246 
247 	ret = read_build_id(buf, buf_size, build_id, size, false);
248 out_free:
249 	free(buf);
250 out:
251 	close(fd);
252 	return ret;
253 }
254 
255 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
256 	         enum dso_binary_type type)
257 {
258 	int fd = open(name, O_RDONLY);
259 	if (fd < 0)
260 		goto out_errno;
261 
262 	ss->name = strdup(name);
263 	if (!ss->name)
264 		goto out_close;
265 
266 	ss->fd = fd;
267 	ss->type = type;
268 
269 	return 0;
270 out_close:
271 	close(fd);
272 out_errno:
273 	dso->load_errno = errno;
274 	return -1;
275 }
276 
277 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
278 {
279 	/* Assume all sym sources could be a runtime image. */
280 	return true;
281 }
282 
283 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
284 {
285 	return false;
286 }
287 
288 void symsrc__destroy(struct symsrc *ss)
289 {
290 	zfree(&ss->name);
291 	close(ss->fd);
292 }
293 
294 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
295 				struct symsrc *ss __maybe_unused)
296 {
297 	return 0;
298 }
299 
300 static int fd__is_64_bit(int fd)
301 {
302 	u8 e_ident[EI_NIDENT];
303 
304 	if (lseek(fd, 0, SEEK_SET))
305 		return -1;
306 
307 	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
308 		return -1;
309 
310 	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
311 	    e_ident[EI_VERSION] != EV_CURRENT)
312 		return -1;
313 
314 	return e_ident[EI_CLASS] == ELFCLASS64;
315 }
316 
317 enum dso_type dso__type_fd(int fd)
318 {
319 	Elf64_Ehdr ehdr;
320 	int ret;
321 
322 	ret = fd__is_64_bit(fd);
323 	if (ret < 0)
324 		return DSO__TYPE_UNKNOWN;
325 
326 	if (ret)
327 		return DSO__TYPE_64BIT;
328 
329 	if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
330 		return DSO__TYPE_UNKNOWN;
331 
332 	if (ehdr.e_machine == EM_X86_64)
333 		return DSO__TYPE_X32BIT;
334 
335 	return DSO__TYPE_32BIT;
336 }
337 
338 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
339 		  struct symsrc *ss,
340 		  struct symsrc *runtime_ss __maybe_unused,
341 		  int kmodule __maybe_unused)
342 {
343 	unsigned char build_id[BUILD_ID_SIZE];
344 	int ret;
345 
346 	ret = fd__is_64_bit(ss->fd);
347 	if (ret >= 0)
348 		dso->is_64_bit = ret;
349 
350 	if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
351 		dso__set_build_id(dso, build_id);
352 	}
353 	return 0;
354 }
355 
356 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
357 		    mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
358 		    bool *is_64_bit __maybe_unused)
359 {
360 	return -1;
361 }
362 
363 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
364 {
365 	return -1;
366 }
367 
368 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
369 {
370 }
371 
372 int kcore_copy(const char *from_dir __maybe_unused,
373 	       const char *to_dir __maybe_unused)
374 {
375 	return -1;
376 }
377 
378 void symbol__elf_init(void)
379 {
380 }
381 
382 char *dso__demangle_sym(struct dso *dso __maybe_unused,
383 			int kmodule __maybe_unused,
384 			const char *elf_name __maybe_unused)
385 {
386 	return NULL;
387 }
388