xref: /linux/tools/perf/util/dso.c (revision 1a5f9334a45a6b0c1cd7341cc72a3b87adad1d27)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <asm/bug.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #ifdef HAVE_LIBBPF_SUPPORT
15 #include <bpf/libbpf.h>
16 #include "bpf-event.h"
17 #include "bpf-utils.h"
18 #endif
19 #include "compress.h"
20 #include "env.h"
21 #include "namespaces.h"
22 #include "path.h"
23 #include "map.h"
24 #include "symbol.h"
25 #include "srcline.h"
26 #include "dso.h"
27 #include "dsos.h"
28 #include "machine.h"
29 #include "auxtrace.h"
30 #include "util.h" /* O_CLOEXEC for older systems */
31 #include "debug.h"
32 #include "string2.h"
33 #include "vdso.h"
34 #include "annotate-data.h"
35 #include "libdw.h"
36 
37 static const char * const debuglink_paths[] = {
38 	"%.0s%s",
39 	"%s/%s",
40 	"%s/.debug/%s",
41 	"/usr/lib/debug%s/%s"
42 };
43 
44 void dso__set_nsinfo(struct dso *dso, struct nsinfo *nsi)
45 {
46 	nsinfo__put(RC_CHK_ACCESS(dso)->nsinfo);
47 	RC_CHK_ACCESS(dso)->nsinfo = nsi;
48 }
49 
50 char dso__symtab_origin(const struct dso *dso)
51 {
52 	static const char origin[] = {
53 		[DSO_BINARY_TYPE__KALLSYMS]			= 'k',
54 		[DSO_BINARY_TYPE__VMLINUX]			= 'v',
55 		[DSO_BINARY_TYPE__JAVA_JIT]			= 'j',
56 		[DSO_BINARY_TYPE__DEBUGLINK]			= 'l',
57 		[DSO_BINARY_TYPE__BUILD_ID_CACHE]		= 'B',
58 		[DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO]	= 'D',
59 		[DSO_BINARY_TYPE__FEDORA_DEBUGINFO]		= 'f',
60 		[DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]		= 'u',
61 		[DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO]	= 'x',
62 		[DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]	= 'o',
63 		[DSO_BINARY_TYPE__BUILDID_DEBUGINFO]		= 'b',
64 		[DSO_BINARY_TYPE__SYSTEM_PATH_DSO]		= 'd',
65 		[DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]		= 'K',
66 		[DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP]	= 'm',
67 		[DSO_BINARY_TYPE__GUEST_KALLSYMS]		= 'g',
68 		[DSO_BINARY_TYPE__GUEST_KMODULE]		= 'G',
69 		[DSO_BINARY_TYPE__GUEST_KMODULE_COMP]		= 'M',
70 		[DSO_BINARY_TYPE__GUEST_VMLINUX]		= 'V',
71 		[DSO_BINARY_TYPE__GNU_DEBUGDATA]		= 'n',
72 	};
73 
74 	if (dso == NULL || dso__symtab_type(dso) == DSO_BINARY_TYPE__NOT_FOUND)
75 		return '!';
76 	return origin[dso__symtab_type(dso)];
77 }
78 
79 bool dso__is_object_file(const struct dso *dso)
80 {
81 	switch (dso__binary_type(dso)) {
82 	case DSO_BINARY_TYPE__KALLSYMS:
83 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
84 	case DSO_BINARY_TYPE__JAVA_JIT:
85 	case DSO_BINARY_TYPE__BPF_PROG_INFO:
86 	case DSO_BINARY_TYPE__BPF_IMAGE:
87 	case DSO_BINARY_TYPE__OOL:
88 		return false;
89 	case DSO_BINARY_TYPE__VMLINUX:
90 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
91 	case DSO_BINARY_TYPE__DEBUGLINK:
92 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
93 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
94 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
95 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
96 	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
97 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
98 	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
99 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
100 	case DSO_BINARY_TYPE__GUEST_KMODULE:
101 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
102 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
103 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
104 	case DSO_BINARY_TYPE__KCORE:
105 	case DSO_BINARY_TYPE__GUEST_KCORE:
106 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
107 	case DSO_BINARY_TYPE__NOT_FOUND:
108 	default:
109 		return true;
110 	}
111 }
112 
113 int dso__read_binary_type_filename(const struct dso *dso,
114 				   enum dso_binary_type type,
115 				   const char *root_dir, char *filename, size_t size)
116 {
117 	char build_id_hex[SBUILD_ID_SIZE];
118 	int ret = 0;
119 	size_t len;
120 
121 	switch (type) {
122 	case DSO_BINARY_TYPE__DEBUGLINK:
123 	{
124 		const char *last_slash;
125 		char dso_dir[PATH_MAX];
126 		char symfile[PATH_MAX];
127 		unsigned int i;
128 
129 		len = __symbol__join_symfs(filename, size, dso__long_name(dso));
130 		last_slash = filename + len;
131 		while (last_slash != filename && *last_slash != '/')
132 			last_slash--;
133 
134 		strncpy(dso_dir, filename, last_slash - filename);
135 		dso_dir[last_slash-filename] = '\0';
136 
137 		if (!is_regular_file(filename)) {
138 			ret = -1;
139 			break;
140 		}
141 
142 		ret = filename__read_debuglink(filename, symfile, PATH_MAX);
143 		if (ret)
144 			break;
145 
146 		/* Check predefined locations where debug file might reside */
147 		ret = -1;
148 		for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
149 			snprintf(filename, size,
150 					debuglink_paths[i], dso_dir, symfile);
151 			if (is_regular_file(filename)) {
152 				ret = 0;
153 				break;
154 			}
155 		}
156 
157 		break;
158 	}
159 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
160 		if (dso__build_id_filename(dso, filename, size, false) == NULL)
161 			ret = -1;
162 		break;
163 
164 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
165 		if (dso__build_id_filename(dso, filename, size, true) == NULL)
166 			ret = -1;
167 		break;
168 
169 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
170 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
171 		snprintf(filename + len, size - len, "%s.debug", dso__long_name(dso));
172 		break;
173 
174 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
175 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
176 		snprintf(filename + len, size - len, "%s", dso__long_name(dso));
177 		break;
178 
179 	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
180 		/*
181 		 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
182 		 * /usr/lib/debug/lib when it is expected to be in
183 		 * /usr/lib/debug/usr/lib
184 		 */
185 		if (strlen(dso__long_name(dso)) < 9 ||
186 		    strncmp(dso__long_name(dso), "/usr/lib/", 9)) {
187 			ret = -1;
188 			break;
189 		}
190 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
191 		snprintf(filename + len, size - len, "%s", dso__long_name(dso) + 4);
192 		break;
193 
194 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
195 	{
196 		const char *last_slash;
197 		size_t dir_size;
198 
199 		last_slash = dso__long_name(dso) + dso__long_name_len(dso);
200 		while (last_slash != dso__long_name(dso) && *last_slash != '/')
201 			last_slash--;
202 
203 		len = __symbol__join_symfs(filename, size, "");
204 		dir_size = last_slash - dso__long_name(dso) + 2;
205 		if (dir_size > (size - len)) {
206 			ret = -1;
207 			break;
208 		}
209 		len += scnprintf(filename + len, dir_size, "%s",  dso__long_name(dso));
210 		len += scnprintf(filename + len , size - len, ".debug%s",
211 								last_slash);
212 		break;
213 	}
214 
215 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
216 		if (!dso__has_build_id(dso)) {
217 			ret = -1;
218 			break;
219 		}
220 
221 		build_id__snprintf(dso__bid(dso), build_id_hex, sizeof(build_id_hex));
222 		len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
223 		snprintf(filename + len, size - len, "%.2s/%s.debug",
224 			 build_id_hex, build_id_hex + 2);
225 		break;
226 
227 	case DSO_BINARY_TYPE__VMLINUX:
228 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
229 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
230 	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
231 		__symbol__join_symfs(filename, size, dso__long_name(dso));
232 		break;
233 
234 	case DSO_BINARY_TYPE__GUEST_KMODULE:
235 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
236 		path__join3(filename, size, symbol_conf.symfs,
237 			    root_dir, dso__long_name(dso));
238 		break;
239 
240 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
241 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
242 		__symbol__join_symfs(filename, size, dso__long_name(dso));
243 		break;
244 
245 	case DSO_BINARY_TYPE__KCORE:
246 	case DSO_BINARY_TYPE__GUEST_KCORE:
247 		snprintf(filename, size, "%s", dso__long_name(dso));
248 		break;
249 
250 	default:
251 	case DSO_BINARY_TYPE__KALLSYMS:
252 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
253 	case DSO_BINARY_TYPE__JAVA_JIT:
254 	case DSO_BINARY_TYPE__BPF_PROG_INFO:
255 	case DSO_BINARY_TYPE__BPF_IMAGE:
256 	case DSO_BINARY_TYPE__OOL:
257 	case DSO_BINARY_TYPE__NOT_FOUND:
258 		ret = -1;
259 		break;
260 	}
261 
262 	return ret;
263 }
264 
265 enum {
266 	COMP_ID__NONE = 0,
267 };
268 
269 static const struct {
270 	const char *fmt;
271 	int (*decompress)(const char *input, int output);
272 	bool (*is_compressed)(const char *input);
273 } compressions[] = {
274 	[COMP_ID__NONE] = { .fmt = NULL, },
275 #ifdef HAVE_ZLIB_SUPPORT
276 	{ "gz", gzip_decompress_to_file, gzip_is_compressed },
277 #endif
278 #ifdef HAVE_LZMA_SUPPORT
279 	{ "xz", lzma_decompress_to_file, lzma_is_compressed },
280 #endif
281 	{ NULL, NULL, NULL },
282 };
283 
284 static int is_supported_compression(const char *ext)
285 {
286 	unsigned i;
287 
288 	for (i = 1; compressions[i].fmt; i++) {
289 		if (!strcmp(ext, compressions[i].fmt))
290 			return i;
291 	}
292 	return COMP_ID__NONE;
293 }
294 
295 bool is_kernel_module(const char *pathname, int cpumode)
296 {
297 	struct kmod_path m;
298 	int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
299 
300 	WARN_ONCE(mode != cpumode,
301 		  "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
302 		  cpumode);
303 
304 	switch (mode) {
305 	case PERF_RECORD_MISC_USER:
306 	case PERF_RECORD_MISC_HYPERVISOR:
307 	case PERF_RECORD_MISC_GUEST_USER:
308 		return false;
309 	/* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
310 	default:
311 		if (kmod_path__parse(&m, pathname)) {
312 			pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
313 					pathname);
314 			return true;
315 		}
316 	}
317 
318 	return m.kmod;
319 }
320 
321 bool dso__needs_decompress(struct dso *dso)
322 {
323 	return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
324 		dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
325 }
326 
327 int filename__decompress(const char *name, char *pathname,
328 			 size_t len, int comp, int *err)
329 {
330 	char tmpbuf[] = KMOD_DECOMP_NAME;
331 	int fd = -1;
332 
333 	/*
334 	 * We have proper compression id for DSO and yet the file
335 	 * behind the 'name' can still be plain uncompressed object.
336 	 *
337 	 * The reason is behind the logic we open the DSO object files,
338 	 * when we try all possible 'debug' objects until we find the
339 	 * data. So even if the DSO is represented by 'krava.xz' module,
340 	 * we can end up here opening ~/.debug/....23432432/debug' file
341 	 * which is not compressed.
342 	 *
343 	 * To keep this transparent, we detect this and return the file
344 	 * descriptor to the uncompressed file.
345 	 */
346 	if (!compressions[comp].is_compressed(name)) {
347 		fd = open(name, O_RDONLY | O_CLOEXEC);
348 		if (fd < 0)
349 			*err = errno;
350 		if (pathname && len > 0)
351 			pathname[0] = '\0';
352 		return fd;
353 	}
354 
355 	fd = mkostemp(tmpbuf, O_CLOEXEC);
356 	if (fd < 0) {
357 		*err = errno;
358 		return -1;
359 	}
360 
361 	if (compressions[comp].decompress(name, fd)) {
362 		*err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
363 		close(fd);
364 		fd = -1;
365 	}
366 
367 	if (!pathname || (fd < 0))
368 		unlink(tmpbuf);
369 
370 	if (pathname && (fd >= 0))
371 		strlcpy(pathname, tmpbuf, len);
372 
373 	return fd;
374 }
375 
376 static int decompress_kmodule(struct dso *dso, const char *name,
377 			      char *pathname, size_t len)
378 {
379 	if (!dso__needs_decompress(dso))
380 		return -1;
381 
382 	if (dso__comp(dso) == COMP_ID__NONE)
383 		return -1;
384 
385 	return filename__decompress(name, pathname, len, dso__comp(dso), dso__load_errno(dso));
386 }
387 
388 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
389 {
390 	return decompress_kmodule(dso, name, NULL, 0);
391 }
392 
393 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
394 				 char *pathname, size_t len)
395 {
396 	int fd = decompress_kmodule(dso, name, pathname, len);
397 
398 	close(fd);
399 	return fd >= 0 ? 0 : -1;
400 }
401 
402 /*
403  * Parses kernel module specified in @path and updates
404  * @m argument like:
405  *
406  *    @comp - true if @path contains supported compression suffix,
407  *            false otherwise
408  *    @kmod - true if @path contains '.ko' suffix in right position,
409  *            false otherwise
410  *    @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
411  *            of the kernel module without suffixes, otherwise strudup-ed
412  *            base name of @path
413  *    @ext  - if (@alloc_ext && @comp) is true, it contains strdup-ed string
414  *            the compression suffix
415  *
416  * Returns 0 if there's no strdup error, -ENOMEM otherwise.
417  */
418 int __kmod_path__parse(struct kmod_path *m, const char *path,
419 		       bool alloc_name)
420 {
421 	const char *name = strrchr(path, '/');
422 	const char *ext  = strrchr(path, '.');
423 	bool is_simple_name = false;
424 
425 	memset(m, 0x0, sizeof(*m));
426 	name = name ? name + 1 : path;
427 
428 	/*
429 	 * '.' is also a valid character for module name. For example:
430 	 * [aaa.bbb] is a valid module name. '[' should have higher
431 	 * priority than '.ko' suffix.
432 	 *
433 	 * The kernel names are from machine__mmap_name. Such
434 	 * name should belong to kernel itself, not kernel module.
435 	 */
436 	if (name[0] == '[') {
437 		is_simple_name = true;
438 		if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
439 		    (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
440 		    (strncmp(name, "[vdso]", 6) == 0) ||
441 		    (strncmp(name, "[vdso32]", 8) == 0) ||
442 		    (strncmp(name, "[vdsox32]", 9) == 0) ||
443 		    (strncmp(name, "[vsyscall]", 10) == 0)) {
444 			m->kmod = false;
445 
446 		} else
447 			m->kmod = true;
448 	}
449 
450 	/* No extension, just return name. */
451 	if ((ext == NULL) || is_simple_name) {
452 		if (alloc_name) {
453 			m->name = strdup(name);
454 			return m->name ? 0 : -ENOMEM;
455 		}
456 		return 0;
457 	}
458 
459 	m->comp = is_supported_compression(ext + 1);
460 	if (m->comp > COMP_ID__NONE)
461 		ext -= 3;
462 
463 	/* Check .ko extension only if there's enough name left. */
464 	if (ext > name)
465 		m->kmod = !strncmp(ext, ".ko", 3);
466 
467 	if (alloc_name) {
468 		if (m->kmod) {
469 			if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
470 				return -ENOMEM;
471 		} else {
472 			if (asprintf(&m->name, "%s", name) == -1)
473 				return -ENOMEM;
474 		}
475 
476 		strreplace(m->name, '-', '_');
477 	}
478 
479 	return 0;
480 }
481 
482 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
483 			  struct machine *machine)
484 {
485 	if (machine__is_host(machine))
486 		dso__set_symtab_type(dso, DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE);
487 	else
488 		dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KMODULE);
489 
490 	/* _KMODULE_COMP should be next to _KMODULE */
491 	if (m->kmod && m->comp) {
492 		dso__set_symtab_type(dso, dso__symtab_type(dso) + 1);
493 		dso__set_comp(dso, m->comp);
494 	}
495 
496 	dso__set_is_kmod(dso);
497 	dso__set_short_name(dso, strdup(m->name), true);
498 }
499 
500 /*
501  * Global list of open DSOs and the counter.
502  */
503 struct mutex _dso__data_open_lock;
504 static LIST_HEAD(dso__data_open);
505 static long dso__data_open_cnt GUARDED_BY(_dso__data_open_lock);
506 
507 static void dso__data_open_lock_init(void)
508 {
509 	mutex_init(&_dso__data_open_lock);
510 }
511 
512 static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_open_lock)
513 {
514 	static pthread_once_t data_open_lock_once = PTHREAD_ONCE_INIT;
515 
516 	pthread_once(&data_open_lock_once, dso__data_open_lock_init);
517 
518 	return &_dso__data_open_lock;
519 }
520 
521 static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
522 {
523 	list_add_tail(&dso__data(dso)->open_entry, &dso__data_open);
524 #ifdef REFCNT_CHECKING
525 	dso__data(dso)->dso = dso__get(dso);
526 #endif
527 	/* Assume the dso is part of dsos, hence the optional reference count above. */
528 	assert(dso__dsos(dso));
529 	dso__data_open_cnt++;
530 }
531 
532 static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
533 {
534 	list_del_init(&dso__data(dso)->open_entry);
535 #ifdef REFCNT_CHECKING
536 	mutex_unlock(dso__data_open_lock());
537 	dso__put(dso__data(dso)->dso);
538 	mutex_lock(dso__data_open_lock());
539 #endif
540 	WARN_ONCE(dso__data_open_cnt <= 0,
541 		  "DSO data fd counter out of bounds.");
542 	dso__data_open_cnt--;
543 }
544 
545 static void close_first_dso(void);
546 
547 static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
548 {
549 	do {
550 		int fd = open(name, O_RDONLY|O_CLOEXEC);
551 
552 		if (fd >= 0)
553 			return fd;
554 
555 		pr_debug("dso open failed: %m\n");
556 		if (!dso__data_open_cnt || errno != EMFILE)
557 			break;
558 
559 		close_first_dso();
560 	} while (1);
561 
562 	return -1;
563 }
564 
565 char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
566 {
567 	return filename_with_chroot(nsinfo__pid(dso__nsinfo_const(dso)), filename);
568 }
569 
570 static char *dso__get_filename(struct dso *dso, const char *root_dir,
571 			       bool *decomp)
572 {
573 	char *name = malloc(PATH_MAX);
574 
575 	*decomp = false;
576 
577 	if (name == NULL)
578 		return NULL;
579 
580 	if (dso__read_binary_type_filename(dso, dso__binary_type(dso),
581 					    root_dir, name, PATH_MAX))
582 		goto out;
583 
584 	if (!is_regular_file(name)) {
585 		char *new_name;
586 
587 		if (errno != ENOENT || dso__nsinfo(dso) == NULL)
588 			goto out;
589 
590 		new_name = dso__filename_with_chroot(dso, name);
591 		if (!new_name)
592 			goto out;
593 
594 		free(name);
595 		name = new_name;
596 	}
597 
598 	if (dso__needs_decompress(dso)) {
599 		char newpath[KMOD_DECOMP_LEN];
600 		size_t len = sizeof(newpath);
601 
602 		if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
603 			/*
604 			 * Use a standard errno value, not the negative custom
605 			 * DSO_LOAD_ERRNO stored in dso__load_errno(dso):
606 			 * __open_dso() computes fd = -errno, so a negative
607 			 * errno produces a positive fd that looks valid.
608 			 */
609 			errno = EIO;
610 			goto out;
611 		}
612 
613 		/* empty pathname means file wasn't actually compressed */
614 		if (newpath[0] != '\0') {
615 			char *tmp = strdup(newpath);
616 
617 			if (!tmp) {
618 				unlink(newpath);
619 				goto out;
620 			}
621 			free(name);
622 			name = tmp;
623 			*decomp = true;
624 		}
625 	}
626 	return name;
627 
628 out:
629 	free(name);
630 	return NULL;
631 }
632 
633 static int __open_dso(struct dso *dso, struct machine *machine)
634 	EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
635 {
636 	int fd = -EINVAL;
637 	char *name;
638 	bool decomp = false;
639 
640 	mutex_lock(dso__lock(dso));
641 
642 	name = dso__get_filename(dso, machine ? machine->root_dir : "", &decomp);
643 	if (name)
644 		fd = do_open(name);
645 	else
646 		fd = -errno;
647 
648 	if (decomp)
649 		unlink(name);
650 
651 	mutex_unlock(dso__lock(dso));
652 	free(name);
653 	return fd;
654 }
655 
656 static void check_data_close(void);
657 
658 /**
659  * dso_close - Open DSO data file
660  * @dso: dso object
661  *
662  * Open @dso's data file descriptor and updates
663  * list/count of open DSO objects.
664  */
665 static int open_dso(struct dso *dso, struct machine *machine)
666 	EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
667 {
668 	int fd;
669 	struct nscookie nsc;
670 
671 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
672 		mutex_lock(dso__lock(dso));
673 		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
674 		mutex_unlock(dso__lock(dso));
675 	}
676 	fd = __open_dso(dso, machine);
677 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE)
678 		nsinfo__mountns_exit(&nsc);
679 
680 	if (fd >= 0) {
681 		dso__list_add(dso);
682 		/*
683 		 * Check if we crossed the allowed number
684 		 * of opened DSOs and close one if needed.
685 		 */
686 		check_data_close();
687 	}
688 
689 	return fd;
690 }
691 
692 static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
693 {
694 	if (dso__data(dso)->fd >= 0) {
695 		close(dso__data(dso)->fd);
696 		dso__data(dso)->fd = -1;
697 		dso__data(dso)->file_size = 0;
698 		dso__list_del(dso);
699 	}
700 }
701 
702 /**
703  * dso_close - Close DSO data file
704  * @dso: dso object
705  *
706  * Close @dso's data file descriptor and updates
707  * list/count of open DSO objects.
708  */
709 static void close_dso(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
710 {
711 	close_data_fd(dso);
712 }
713 
714 static void close_first_dso(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
715 {
716 	struct dso_data *dso_data;
717 	struct dso *dso;
718 
719 	dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry);
720 #ifdef REFCNT_CHECKING
721 	dso = dso_data->dso;
722 #else
723 	dso = container_of(dso_data, struct dso, data);
724 #endif
725 	close_dso(dso);
726 }
727 
728 static rlim_t get_fd_limit(void)
729 {
730 	struct rlimit l;
731 	rlim_t limit = 0;
732 
733 	/* Allow half of the current open fd limit. */
734 	if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
735 		if (l.rlim_cur == RLIM_INFINITY)
736 			limit = l.rlim_cur;
737 		else
738 			limit = l.rlim_cur / 2;
739 	} else {
740 		pr_err("failed to get fd limit\n");
741 		limit = 1;
742 	}
743 
744 	return limit;
745 }
746 
747 static rlim_t fd_limit;
748 
749 /*
750  * Used only by tests/dso-data.c to reset the environment
751  * for tests. I dont expect we should change this during
752  * standard runtime.
753  */
754 void reset_fd_limit(void)
755 {
756 	fd_limit = 0;
757 }
758 
759 static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
760 {
761 	if (!fd_limit)
762 		fd_limit = get_fd_limit();
763 
764 	if (fd_limit == RLIM_INFINITY)
765 		return true;
766 
767 	return fd_limit > (rlim_t) dso__data_open_cnt;
768 }
769 
770 /*
771  * Check and close LRU dso if we crossed allowed limit
772  * for opened dso file descriptors. The limit is half
773  * of the RLIMIT_NOFILE files opened.
774 */
775 static void check_data_close(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
776 {
777 	bool cache_fd = may_cache_fd();
778 
779 	if (!cache_fd)
780 		close_first_dso();
781 }
782 
783 /**
784  * dso__data_close - Close DSO data file
785  * @dso: dso object
786  *
787  * External interface to close @dso's data file descriptor.
788  */
789 void dso__data_close(struct dso *dso)
790 {
791 	mutex_lock(dso__data_open_lock());
792 	close_dso(dso);
793 	mutex_unlock(dso__data_open_lock());
794 }
795 
796 static void try_to_open_dso(struct dso *dso, struct machine *machine)
797 	EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
798 {
799 	enum dso_binary_type binary_type_data[] = {
800 		DSO_BINARY_TYPE__BUILD_ID_CACHE,
801 		DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
802 		DSO_BINARY_TYPE__NOT_FOUND,
803 	};
804 	int i = 0;
805 	struct dso_data *dso_data = dso__data(dso);
806 
807 	if (dso_data->fd >= 0)
808 		return;
809 
810 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND) {
811 		dso_data->fd = open_dso(dso, machine);
812 		goto out;
813 	}
814 
815 	do {
816 		dso__set_binary_type(dso, binary_type_data[i++]);
817 
818 		dso_data->fd = open_dso(dso, machine);
819 		if (dso_data->fd >= 0)
820 			goto out;
821 
822 	} while (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND);
823 out:
824 	if (dso_data->fd >= 0)
825 		dso_data->status = DSO_DATA_STATUS_OK;
826 	else
827 		dso_data->status = DSO_DATA_STATUS_ERROR;
828 }
829 
830 /**
831  * dso__data_get_fd - Get dso's data file descriptor
832  * @dso: dso object
833  * @machine: machine object
834  *
835  * External interface to find dso's file, open it and
836  * returns file descriptor.  It should be paired with
837  * dso__data_put_fd() if it returns non-negative value.
838  */
839 bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd)
840 {
841 	*fd = -1;
842 	if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
843 		return false;
844 
845 	mutex_lock(dso__data_open_lock());
846 
847 	try_to_open_dso(dso, machine);
848 
849 	*fd = dso__data(dso)->fd;
850 	if (*fd >= 0)
851 		return true;
852 
853 	mutex_unlock(dso__data_open_lock());
854 	return false;
855 }
856 
857 void dso__data_put_fd(struct dso *dso __maybe_unused)
858 {
859 	mutex_unlock(dso__data_open_lock());
860 }
861 
862 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
863 {
864 	u32 flag = 1 << by;
865 
866 	if (dso__data(dso)->status_seen & flag)
867 		return true;
868 
869 	dso__data(dso)->status_seen |= flag;
870 
871 	return false;
872 }
873 
874 #ifdef HAVE_LIBBPF_SUPPORT
875 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
876 {
877 	struct bpf_prog_info_node *node;
878 	ssize_t size = DSO__DATA_CACHE_SIZE;
879 	struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso);
880 	u64 len;
881 	u8 *buf;
882 
883 	node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id);
884 	if (!node || !node->info_linear) {
885 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
886 		return -1;
887 	}
888 
889 	/* jited_prog_insns is only valid if bpil_offs_to_addr() converted it */
890 	if (!(node->info_linear->arrays & (1UL << PERF_BPIL_JITED_INSNS))) {
891 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
892 		return -1;
893 	}
894 
895 	len = node->info_linear->info.jited_prog_len;
896 	buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
897 
898 	if (offset >= len)
899 		return -1;
900 
901 	size = (ssize_t)min(len - offset, (u64)size);
902 	memcpy(data, buf + offset, size);
903 	return size;
904 }
905 
906 static int bpf_size(struct dso *dso)
907 {
908 	struct bpf_prog_info_node *node;
909 	struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso);
910 
911 	node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id);
912 	if (!node || !node->info_linear) {
913 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
914 		return -1;
915 	}
916 
917 	dso__data(dso)->file_size = node->info_linear->info.jited_prog_len;
918 	return 0;
919 }
920 #endif // HAVE_LIBBPF_SUPPORT
921 
922 static void
923 dso_cache__free(struct dso *dso)
924 {
925 	struct rb_root *root = &dso__data(dso)->cache;
926 	struct rb_node *next = rb_first(root);
927 
928 	mutex_lock(dso__lock(dso));
929 	while (next) {
930 		struct dso_cache *cache;
931 
932 		cache = rb_entry(next, struct dso_cache, rb_node);
933 		next = rb_next(&cache->rb_node);
934 		rb_erase(&cache->rb_node, root);
935 		free(cache);
936 	}
937 	mutex_unlock(dso__lock(dso));
938 }
939 
940 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
941 {
942 	const struct rb_root *root = &dso__data(dso)->cache;
943 	struct rb_node * const *p = &root->rb_node;
944 	const struct rb_node *parent = NULL;
945 	struct dso_cache *cache;
946 
947 	while (*p != NULL) {
948 		u64 end;
949 
950 		parent = *p;
951 		cache = rb_entry(parent, struct dso_cache, rb_node);
952 		end = cache->offset + DSO__DATA_CACHE_SIZE;
953 
954 		if (offset < cache->offset)
955 			p = &(*p)->rb_left;
956 		else if (offset >= end)
957 			p = &(*p)->rb_right;
958 		else
959 			return cache;
960 	}
961 
962 	return NULL;
963 }
964 
965 static struct dso_cache *
966 dso_cache__insert(struct dso *dso, struct dso_cache *new)
967 {
968 	struct rb_root *root = &dso__data(dso)->cache;
969 	struct rb_node **p = &root->rb_node;
970 	struct rb_node *parent = NULL;
971 	struct dso_cache *cache;
972 	u64 offset = new->offset;
973 
974 	mutex_lock(dso__lock(dso));
975 	while (*p != NULL) {
976 		u64 end;
977 
978 		parent = *p;
979 		cache = rb_entry(parent, struct dso_cache, rb_node);
980 		end = cache->offset + DSO__DATA_CACHE_SIZE;
981 
982 		if (offset < cache->offset)
983 			p = &(*p)->rb_left;
984 		else if (offset >= end)
985 			p = &(*p)->rb_right;
986 		else
987 			goto out;
988 	}
989 
990 	rb_link_node(&new->rb_node, parent, p);
991 	rb_insert_color(&new->rb_node, root);
992 
993 	cache = NULL;
994 out:
995 	mutex_unlock(dso__lock(dso));
996 	return cache;
997 }
998 
999 static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
1000 				 u64 size, bool out)
1001 {
1002 	u64 cache_offset = offset - cache->offset;
1003 	u64 cache_size   = min(cache->size - cache_offset, size);
1004 
1005 	if (out)
1006 		memcpy(data, cache->data + cache_offset, cache_size);
1007 	else
1008 		memcpy(cache->data + cache_offset, data, cache_size);
1009 	return cache_size;
1010 }
1011 
1012 static ssize_t file_read(struct dso *dso, struct machine *machine,
1013 			 u64 offset, char *data)
1014 {
1015 	ssize_t ret;
1016 
1017 	mutex_lock(dso__data_open_lock());
1018 
1019 	/*
1020 	 * dso__data(dso)->fd might be closed if other thread opened another
1021 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
1022 	 */
1023 	try_to_open_dso(dso, machine);
1024 
1025 	if (dso__data(dso)->fd < 0) {
1026 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
1027 		ret = -errno;
1028 		goto out;
1029 	}
1030 
1031 	ret = pread(dso__data(dso)->fd, data, DSO__DATA_CACHE_SIZE, offset);
1032 out:
1033 	mutex_unlock(dso__data_open_lock());
1034 	return ret;
1035 }
1036 
1037 static struct dso_cache *dso_cache__populate(struct dso *dso,
1038 					     struct machine *machine,
1039 					     u64 offset, ssize_t *ret)
1040 {
1041 	u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
1042 	struct dso_cache *cache;
1043 	struct dso_cache *old;
1044 
1045 	cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
1046 	if (!cache) {
1047 		*ret = -ENOMEM;
1048 		return NULL;
1049 	}
1050 #ifdef HAVE_LIBBPF_SUPPORT
1051 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
1052 		*ret = bpf_read(dso, cache_offset, cache->data);
1053 	else
1054 #endif
1055 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__OOL)
1056 		*ret = DSO__DATA_CACHE_SIZE;
1057 	else
1058 		*ret = file_read(dso, machine, cache_offset, cache->data);
1059 
1060 	if (*ret <= 0) {
1061 		free(cache);
1062 		return NULL;
1063 	}
1064 
1065 	cache->offset = cache_offset;
1066 	cache->size   = *ret;
1067 
1068 	old = dso_cache__insert(dso, cache);
1069 	if (old) {
1070 		/* we lose the race */
1071 		free(cache);
1072 		cache = old;
1073 	}
1074 
1075 	return cache;
1076 }
1077 
1078 static struct dso_cache *dso_cache__find(struct dso *dso,
1079 					 struct machine *machine,
1080 					 u64 offset,
1081 					 ssize_t *ret)
1082 {
1083 	struct dso_cache *cache = __dso_cache__find(dso, offset);
1084 
1085 	return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
1086 }
1087 
1088 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
1089 			    u64 offset, u8 *data, ssize_t size, bool out)
1090 {
1091 	struct dso_cache *cache;
1092 	ssize_t ret = 0;
1093 
1094 	cache = dso_cache__find(dso, machine, offset, &ret);
1095 	if (!cache)
1096 		return ret;
1097 
1098 	return dso_cache__memcpy(cache, offset, data, size, out);
1099 }
1100 
1101 /*
1102  * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
1103  * in the rb_tree. Any read to already cached data is served
1104  * by cached data. Writes update the cache only, not the backing file.
1105  */
1106 static ssize_t cached_io(struct dso *dso, struct machine *machine,
1107 			 u64 offset, u8 *data, ssize_t size, bool out)
1108 {
1109 	ssize_t r = 0;
1110 	u8 *p = data;
1111 
1112 	do {
1113 		ssize_t ret;
1114 
1115 		ret = dso_cache_io(dso, machine, offset, p, size, out);
1116 		if (ret < 0)
1117 			return ret;
1118 
1119 		/* Reached EOF, return what we have. */
1120 		if (!ret)
1121 			break;
1122 
1123 		BUG_ON(ret > size);
1124 
1125 		r      += ret;
1126 		p      += ret;
1127 		offset += ret;
1128 		size   -= ret;
1129 
1130 	} while (size);
1131 
1132 	return r;
1133 }
1134 
1135 static int file_size(struct dso *dso, struct machine *machine)
1136 {
1137 	int ret = 0;
1138 	struct stat st;
1139 
1140 	mutex_lock(dso__data_open_lock());
1141 
1142 	/*
1143 	 * dso__data(dso)->fd might be closed if other thread opened another
1144 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
1145 	 */
1146 	try_to_open_dso(dso, machine);
1147 
1148 	if (dso__data(dso)->fd < 0) {
1149 		ret = -errno;
1150 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
1151 		goto out;
1152 	}
1153 
1154 	if (fstat(dso__data(dso)->fd, &st) < 0) {
1155 		ret = -errno;
1156 		pr_err("dso cache fstat failed: %m\n");
1157 		dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
1158 		goto out;
1159 	}
1160 	dso__data(dso)->file_size = st.st_size;
1161 
1162 out:
1163 	mutex_unlock(dso__data_open_lock());
1164 	return ret;
1165 }
1166 
1167 int dso__data_file_size(struct dso *dso, struct machine *machine)
1168 {
1169 	if (dso__data(dso)->file_size)
1170 		return 0;
1171 
1172 	if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
1173 		return -1;
1174 #ifdef HAVE_LIBBPF_SUPPORT
1175 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
1176 		return bpf_size(dso);
1177 #endif
1178 	return file_size(dso, machine);
1179 }
1180 
1181 /**
1182  * dso__data_size - Return dso data size
1183  * @dso: dso object
1184  * @machine: machine object
1185  *
1186  * Return: dso data size
1187  */
1188 off_t dso__data_size(struct dso *dso, struct machine *machine)
1189 {
1190 	if (dso__data_file_size(dso, machine))
1191 		return -1;
1192 
1193 	/* For now just estimate dso data size is close to file size */
1194 	return dso__data(dso)->file_size;
1195 }
1196 
1197 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1198 				      u64 offset, u8 *data, ssize_t size,
1199 				      bool out)
1200 {
1201 	if (dso__data_file_size(dso, machine))
1202 		return -1;
1203 
1204 	/* Check the offset sanity. */
1205 	if (offset > dso__data(dso)->file_size)
1206 		return -1;
1207 
1208 	if (offset + size < offset)
1209 		return -1;
1210 
1211 	return cached_io(dso, machine, offset, data, size, out);
1212 }
1213 
1214 /**
1215  * dso__data_read_offset - Read data from dso file offset
1216  * @dso: dso object
1217  * @machine: machine object
1218  * @offset: file offset
1219  * @data: buffer to store data
1220  * @size: size of the @data buffer
1221  *
1222  * External interface to read data from dso file offset. Open
1223  * dso data file and use cached_read to get the data.
1224  */
1225 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1226 			      u64 offset, u8 *data, ssize_t size)
1227 {
1228 	if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
1229 		return -1;
1230 
1231 	return data_read_write_offset(dso, machine, offset, data, size, true);
1232 }
1233 
1234 static enum dso_swap_type dso_swap_type__from_elf_data(unsigned char eidata)
1235 {
1236 	static const unsigned int endian = 1;
1237 
1238 	switch (eidata) {
1239 	case ELFDATA2LSB:
1240 		/* We are big endian, DSO is little endian. */
1241 		return (*(unsigned char const *)&endian != 1) ? DSO_SWAP__YES : DSO_SWAP__NO;
1242 	case ELFDATA2MSB:
1243 		/* We are little endian, DSO is big endian. */
1244 		return (*(unsigned char const *)&endian != 0) ? DSO_SWAP__YES : DSO_SWAP__NO;
1245 	default:
1246 		return DSO_SWAP__UNSET;
1247 	}
1248 }
1249 
1250 /* Reads e_machine from fd, optionally caching data in dso. */
1251 uint16_t dso__read_e_machine_endian(struct dso *optional_dso, int fd, uint32_t *e_flags,
1252 				    bool *is_big_endian)
1253 {
1254 	uint16_t e_machine = EM_NONE;
1255 	unsigned char e_ident[EI_NIDENT];
1256 	enum dso_swap_type swap_type;
1257 	bool need_e_flags;
1258 
1259 	if (e_flags)
1260 		*e_flags = 0;
1261 
1262 	{
1263 		_Static_assert(offsetof(Elf32_Ehdr, e_ident) == 0, "Unexpected offset");
1264 		_Static_assert(offsetof(Elf64_Ehdr, e_ident) == 0, "Unexpected offset");
1265 	}
1266 	if (pread(fd, &e_ident, sizeof(e_ident), 0) != sizeof(e_ident))
1267 		return EM_NONE; // Read failed.
1268 
1269 	if (memcmp(e_ident, ELFMAG, SELFMAG) != 0)
1270 		return EM_NONE; // Not an ELF file.
1271 
1272 	if (e_ident[EI_CLASS] == ELFCLASSNONE || e_ident[EI_CLASS] >= ELFCLASSNUM)
1273 		return EM_NONE; // Bad ELF class (32 or 64-bit objects).
1274 
1275 	if (e_ident[EI_VERSION] != EV_CURRENT)
1276 		return EM_NONE; // Bad ELF version.
1277 
1278 	swap_type = dso_swap_type__from_elf_data(e_ident[EI_DATA]);
1279 	if (swap_type == DSO_SWAP__UNSET)
1280 		return EM_NONE; // Bad ELF data encoding.
1281 
1282 	if (is_big_endian)
1283 		*is_big_endian = (e_ident[EI_DATA] == ELFDATA2MSB);
1284 
1285 	/* Cache the need for swapping. */
1286 	if (optional_dso) {
1287 		assert(dso__needs_swap(optional_dso) == DSO_SWAP__UNSET ||
1288 		       dso__needs_swap(optional_dso) == swap_type);
1289 		dso__set_needs_swap(optional_dso, swap_type);
1290 	}
1291 
1292 	{
1293 		_Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
1294 		_Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
1295 	}
1296 	if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine))
1297 		return EM_NONE; // e_machine read failed.
1298 
1299 	e_machine = DSO_SWAP_TYPE__SWAP(swap_type, uint16_t, e_machine);
1300 	if (e_machine >= EM_NUM)
1301 		return EM_NONE; // Bad ELF machine number.
1302 
1303 #ifdef NDEBUG
1304 	/* In production code the e_flags are only needed on CSKY. */
1305 	need_e_flags = e_flags && e_machine == EM_CSKY;
1306 #else
1307 	/* Debug code will always read the e_flags. */
1308 	need_e_flags = e_flags != NULL;
1309 #endif
1310 	if (need_e_flags) {
1311 		off_t offset = e_ident[EI_CLASS] == ELFCLASS32
1312 			? offsetof(Elf32_Ehdr, e_flags)
1313 			: offsetof(Elf64_Ehdr, e_flags);
1314 
1315 		if (pread(fd, e_flags, sizeof(*e_flags), offset) != sizeof(*e_flags)) {
1316 			*e_flags = 0;
1317 			return EM_NONE; // e_flags read failed.
1318 		}
1319 	}
1320 	return e_machine;
1321 }
1322 
1323 uint16_t dso__e_machine_endian(struct dso *dso, struct machine *machine, uint32_t *e_flags,
1324 			       bool *is_big_endian)
1325 {
1326 	uint16_t e_machine = EM_NONE;
1327 	int fd;
1328 
1329 	switch (dso__binary_type(dso)) {
1330 	case DSO_BINARY_TYPE__KALLSYMS:
1331 	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1332 	case DSO_BINARY_TYPE__VMLINUX:
1333 	case DSO_BINARY_TYPE__GUEST_VMLINUX:
1334 	case DSO_BINARY_TYPE__GUEST_KMODULE:
1335 	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1336 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1337 	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1338 	case DSO_BINARY_TYPE__KCORE:
1339 	case DSO_BINARY_TYPE__GUEST_KCORE:
1340 	case DSO_BINARY_TYPE__BPF_PROG_INFO:
1341 	case DSO_BINARY_TYPE__BPF_IMAGE:
1342 	case DSO_BINARY_TYPE__OOL:
1343 	case DSO_BINARY_TYPE__JAVA_JIT:
1344 		if (is_big_endian) {
1345 			*is_big_endian = perf_arch_is_big_endian(
1346 				machine && machine->env ? perf_env__arch(machine->env) : NULL);
1347 		}
1348 		return perf_env__e_machine(machine ? machine->env : NULL, e_flags);
1349 	case DSO_BINARY_TYPE__DEBUGLINK:
1350 	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1351 	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1352 	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
1353 	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1354 	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1355 	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1356 	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1357 	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1358 	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1359 		break;
1360 	case DSO_BINARY_TYPE__NOT_FOUND:
1361 	default:
1362 		if (e_flags)
1363 			*e_flags = 0;
1364 		return EM_NONE;
1365 	}
1366 
1367 	mutex_lock(dso__data_open_lock());
1368 
1369 	/*
1370 	 * dso__data(dso)->fd might be closed if other thread opened another
1371 	 * file (dso) due to open file limit (RLIMIT_NOFILE).
1372 	 */
1373 	try_to_open_dso(dso, machine);
1374 	fd = dso__data(dso)->fd;
1375 	if (fd >= 0)
1376 		e_machine = dso__read_e_machine_endian(dso, fd, e_flags, is_big_endian);
1377 	else if (e_flags)
1378 		*e_flags = 0;
1379 
1380 	mutex_unlock(dso__data_open_lock());
1381 	return e_machine;
1382 }
1383 
1384 /**
1385  * dso__data_read_addr - Read data from dso address
1386  * @dso: dso object
1387  * @machine: machine object
1388  * @add: virtual memory address
1389  * @data: buffer to store data
1390  * @size: size of the @data buffer
1391  *
1392  * External interface to read data from dso address.
1393  */
1394 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1395 			    struct machine *machine, u64 addr,
1396 			    u8 *data, ssize_t size)
1397 {
1398 	u64 offset = map__map_ip(map, addr);
1399 
1400 	return dso__data_read_offset(dso, machine, offset, data, size);
1401 }
1402 
1403 /**
1404  * dso__data_write_cache_offs - Write data to dso data cache at file offset
1405  * @dso: dso object
1406  * @machine: machine object
1407  * @offset: file offset
1408  * @data: buffer to write
1409  * @size: size of the @data buffer
1410  *
1411  * Write into the dso file data cache, but do not change the file itself.
1412  */
1413 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1414 				   u64 offset, const u8 *data_in, ssize_t size)
1415 {
1416 	u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
1417 
1418 	if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
1419 		return -1;
1420 
1421 	return data_read_write_offset(dso, machine, offset, data, size, false);
1422 }
1423 
1424 /**
1425  * dso__data_write_cache_addr - Write data to dso data cache at dso address
1426  * @dso: dso object
1427  * @machine: machine object
1428  * @add: virtual memory address
1429  * @data: buffer to write
1430  * @size: size of the @data buffer
1431  *
1432  * External interface to write into the dso file data cache, but do not change
1433  * the file itself.
1434  */
1435 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1436 				   struct machine *machine, u64 addr,
1437 				   const u8 *data, ssize_t size)
1438 {
1439 	u64 offset = map__map_ip(map, addr);
1440 
1441 	return dso__data_write_cache_offs(dso, machine, offset, data, size);
1442 }
1443 
1444 struct map *dso__new_map(const char *name)
1445 {
1446 	struct map *map = NULL;
1447 	struct dso *dso = dso__new(name);
1448 
1449 	if (dso) {
1450 		map = map__new2(0, dso);
1451 		dso__put(dso);
1452 	}
1453 
1454 	return map;
1455 }
1456 
1457 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1458 				    const char *short_name, int dso_type)
1459 {
1460 	/*
1461 	 * The kernel dso could be created by build_id processing.
1462 	 */
1463 	struct dso *dso = machine__findnew_dso(machine, name);
1464 
1465 	/*
1466 	 * We need to run this in all cases, since during the build_id
1467 	 * processing we had no idea this was the kernel dso.
1468 	 */
1469 	if (dso != NULL) {
1470 		dso__set_short_name(dso, short_name, false);
1471 		dso__set_kernel(dso, dso_type);
1472 	}
1473 
1474 	return dso;
1475 }
1476 
1477 static void __dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated)
1478 {
1479 	if (dso__long_name_allocated(dso))
1480 		free((char *)dso__long_name(dso));
1481 
1482 	RC_CHK_ACCESS(dso)->long_name = name;
1483 	RC_CHK_ACCESS(dso)->long_name_len = strlen(name);
1484 	dso__set_long_name_allocated(dso, name_allocated);
1485 }
1486 
1487 static void dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated)
1488 {
1489 	struct dsos *dsos = dso__dsos(dso);
1490 
1491 	if (name == NULL)
1492 		return;
1493 
1494 	if (dsos) {
1495 		/*
1496 		 * Need to avoid re-sorting the dsos breaking by non-atomically
1497 		 * renaming the dso.
1498 		 */
1499 		down_write(&dsos->lock);
1500 		__dso__set_long_name_id(dso, name, name_allocated);
1501 		dsos->sorted = false;
1502 		up_write(&dsos->lock);
1503 	} else {
1504 		__dso__set_long_name_id(dso, name, name_allocated);
1505 	}
1506 }
1507 
1508 static int __dso_id__cmp(const struct dso_id *a, const struct dso_id *b)
1509 {
1510 	if (a->mmap2_valid && b->mmap2_valid) {
1511 		if (a->maj > b->maj) return -1;
1512 		if (a->maj < b->maj) return 1;
1513 
1514 		if (a->min > b->min) return -1;
1515 		if (a->min < b->min) return 1;
1516 
1517 		if (a->ino > b->ino) return -1;
1518 		if (a->ino < b->ino) return 1;
1519 	}
1520 	if (a->mmap2_ino_generation_valid && b->mmap2_ino_generation_valid) {
1521 		if (a->ino_generation > b->ino_generation) return -1;
1522 		if (a->ino_generation < b->ino_generation) return 1;
1523 	}
1524 	if (build_id__is_defined(&a->build_id) && build_id__is_defined(&b->build_id)) {
1525 		if (a->build_id.size != b->build_id.size)
1526 			return a->build_id.size < b->build_id.size ? -1 : 1;
1527 		return memcmp(a->build_id.data, b->build_id.data, a->build_id.size);
1528 	}
1529 	return 0;
1530 }
1531 
1532 const struct dso_id dso_id_empty = {
1533 	{
1534 		.maj = 0,
1535 		.min = 0,
1536 		.ino = 0,
1537 		.ino_generation = 0,
1538 	},
1539 	.mmap2_valid = false,
1540 	.mmap2_ino_generation_valid = false,
1541 	{
1542 		.size = 0,
1543 	}
1544 };
1545 
1546 void __dso__improve_id(struct dso *dso, const struct dso_id *id)
1547 {
1548 	struct dsos *dsos = dso__dsos(dso);
1549 	struct dso_id *dso_id = dso__id(dso);
1550 	bool changed = false;
1551 
1552 	/* dsos write lock held by caller. */
1553 
1554 	if (id->mmap2_valid && !dso_id->mmap2_valid) {
1555 		dso_id->maj = id->maj;
1556 		dso_id->min = id->min;
1557 		dso_id->ino = id->ino;
1558 		dso_id->mmap2_valid = true;
1559 		changed = true;
1560 	}
1561 	if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) {
1562 		dso_id->ino_generation = id->ino_generation;
1563 		dso_id->mmap2_ino_generation_valid = true;
1564 		changed = true;
1565 	}
1566 	if (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso_id->build_id)) {
1567 		dso_id->build_id = id->build_id;
1568 		changed = true;
1569 	}
1570 	if (changed && dsos)
1571 		dsos->sorted = false;
1572 }
1573 
1574 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b)
1575 {
1576 	if (a == &dso_id_empty || b == &dso_id_empty) {
1577 		/* There is no valid data to compare so the comparison always returns identical. */
1578 		return 0;
1579 	}
1580 
1581 	return __dso_id__cmp(a, b);
1582 }
1583 
1584 int dso__cmp_id(struct dso *a, struct dso *b)
1585 {
1586 	return __dso_id__cmp(dso__id(a), dso__id(b));
1587 }
1588 
1589 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1590 {
1591 	dso__set_long_name_id(dso, name, name_allocated);
1592 }
1593 
1594 static void __dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1595 {
1596 	if (dso__short_name_allocated(dso))
1597 		free((char *)dso__short_name(dso));
1598 
1599 	RC_CHK_ACCESS(dso)->short_name		  = name;
1600 	RC_CHK_ACCESS(dso)->short_name_len	  = strlen(name);
1601 	dso__set_short_name_allocated(dso, name_allocated);
1602 }
1603 
1604 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1605 {
1606 	struct dsos *dsos = dso__dsos(dso);
1607 
1608 	if (name == NULL)
1609 		return;
1610 
1611 	if (dsos) {
1612 		/*
1613 		 * Need to avoid re-sorting the dsos breaking by non-atomically
1614 		 * renaming the dso.
1615 		 */
1616 		down_write(&dsos->lock);
1617 		__dso__set_short_name(dso, name, name_allocated);
1618 		dsos->sorted = false;
1619 		up_write(&dsos->lock);
1620 	} else {
1621 		__dso__set_short_name(dso, name, name_allocated);
1622 	}
1623 }
1624 
1625 int dso__name_len(const struct dso *dso)
1626 {
1627 	if (!dso)
1628 		return strlen("[unknown]");
1629 	if (verbose > 0)
1630 		return dso__long_name_len(dso);
1631 
1632 	return dso__short_name_len(dso);
1633 }
1634 
1635 bool dso__loaded(const struct dso *dso)
1636 {
1637 	return RC_CHK_ACCESS(dso)->loaded;
1638 }
1639 
1640 bool dso__sorted_by_name(const struct dso *dso)
1641 {
1642 	return RC_CHK_ACCESS(dso)->sorted_by_name;
1643 }
1644 
1645 void dso__set_sorted_by_name(struct dso *dso)
1646 {
1647 	RC_CHK_ACCESS(dso)->sorted_by_name = true;
1648 }
1649 
1650 struct dso *dso__new_id(const char *name, const struct dso_id *id)
1651 {
1652 	RC_STRUCT(dso) *dso = zalloc(sizeof(*dso) + strlen(name) + 1);
1653 	struct dso *res;
1654 	struct dso_data *data;
1655 
1656 	if (!dso)
1657 		return NULL;
1658 
1659 	if (ADD_RC_CHK(res, dso)) {
1660 		strcpy(dso->name, name);
1661 		if (id)
1662 			dso->id = *id;
1663 		dso__set_long_name_id(res, dso->name, false);
1664 		dso__set_short_name(res, dso->name, false);
1665 		dso->symbols = RB_ROOT_CACHED;
1666 		dso->symbol_names = NULL;
1667 		dso->symbol_names_len = 0;
1668 		dso->inlined_nodes = RB_ROOT_CACHED;
1669 		dso->srclines = RB_ROOT_CACHED;
1670 		dso->data_types = RB_ROOT;
1671 		dso->global_vars = RB_ROOT;
1672 		dso->data.fd = -1;
1673 		dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1674 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1675 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1676 		dso->is_64_bit = (sizeof(void *) == 8);
1677 		dso->loaded = 0;
1678 		dso->rel = 0;
1679 		dso->sorted_by_name = 0;
1680 		dso->has_srcline = 1;
1681 		dso->a2l_fails = 1;
1682 		dso->kernel = DSO_SPACE__USER;
1683 		dso->is_kmod = 0;
1684 		dso->needs_swap = DSO_SWAP__UNSET;
1685 		dso->comp = COMP_ID__NONE;
1686 		mutex_init(&dso->lock);
1687 		refcount_set(&dso->refcnt, 1);
1688 		data = &dso->data;
1689 		data->cache = RB_ROOT;
1690 		data->fd = -1;
1691 		data->status = DSO_DATA_STATUS_UNKNOWN;
1692 		INIT_LIST_HEAD(&data->open_entry);
1693 #ifdef REFCNT_CHECKING
1694 		data->dso = NULL; /* Set when on the open_entry list. */
1695 #endif
1696 	}
1697 	return res;
1698 }
1699 
1700 struct dso *dso__new(const char *name)
1701 {
1702 	return dso__new_id(name, NULL);
1703 }
1704 
1705 void dso__delete(struct dso *dso)
1706 {
1707 	if (dso__dsos(dso))
1708 		pr_err("DSO %s is still in rbtree when being deleted!\n", dso__long_name(dso));
1709 
1710 	/* free inlines first, as they reference symbols */
1711 	inlines__tree_delete(&RC_CHK_ACCESS(dso)->inlined_nodes);
1712 	srcline__tree_delete(&RC_CHK_ACCESS(dso)->srclines);
1713 	symbols__delete(&RC_CHK_ACCESS(dso)->symbols);
1714 	RC_CHK_ACCESS(dso)->symbol_names_len = 0;
1715 	zfree(&RC_CHK_ACCESS(dso)->symbol_names);
1716 	annotated_data_type__tree_delete(dso__data_types(dso));
1717 	global_var_type__tree_delete(dso__global_vars(dso));
1718 
1719 	if (RC_CHK_ACCESS(dso)->short_name_allocated) {
1720 		zfree((char **)&RC_CHK_ACCESS(dso)->short_name);
1721 		RC_CHK_ACCESS(dso)->short_name_allocated = false;
1722 	}
1723 
1724 	if (RC_CHK_ACCESS(dso)->long_name_allocated) {
1725 		zfree((char **)&RC_CHK_ACCESS(dso)->long_name);
1726 		RC_CHK_ACCESS(dso)->long_name_allocated = false;
1727 	}
1728 
1729 	dso__data_close(dso);
1730 	auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
1731 	dso_cache__free(dso);
1732 	dso__free_a2l(dso);
1733 	dso__free_libdw(dso);
1734 	dso__free_symsrc_filename(dso);
1735 	nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo);
1736 	mutex_destroy(dso__lock(dso));
1737 	RC_CHK_FREE(dso);
1738 }
1739 
1740 struct dso *dso__get(struct dso *dso)
1741 {
1742 	struct dso *result;
1743 
1744 	if (RC_CHK_GET(result, dso))
1745 		refcount_inc(&RC_CHK_ACCESS(dso)->refcnt);
1746 
1747 	return result;
1748 }
1749 
1750 void dso__put(struct dso *dso)
1751 {
1752 #ifdef REFCNT_CHECKING
1753 	if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
1754 		dso__data_close(dso);
1755 #endif
1756 	if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
1757 		dso__delete(dso);
1758 	else
1759 		RC_CHK_PUT(dso);
1760 }
1761 
1762 int dso__swap_init(struct dso *dso, unsigned char eidata)
1763 {
1764 	enum dso_swap_type type = dso_swap_type__from_elf_data(eidata);
1765 
1766 	dso__set_needs_swap(dso, type);
1767 	if (type == DSO_SWAP__UNSET) {
1768 		pr_err("unrecognized DSO data encoding %d\n", eidata);
1769 		return -EINVAL;
1770 	}
1771 	return 0;
1772 }
1773 
1774 void dso__set_build_id(struct dso *dso, const struct build_id *bid)
1775 {
1776 	dso__id(dso)->build_id = *bid;
1777 }
1778 
1779 bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid)
1780 {
1781 	const struct build_id *dso_bid = dso__bid(dso);
1782 
1783 	if (dso_bid->size > bid->size && dso_bid->size == BUILD_ID_SIZE) {
1784 		/*
1785 		 * For the backward compatibility, it allows a build-id has
1786 		 * trailing zeros.
1787 		 */
1788 		return !memcmp(dso_bid->data, bid->data, bid->size) &&
1789 			!memchr_inv(&dso_bid->data[bid->size], 0,
1790 				    dso_bid->size - bid->size);
1791 	}
1792 
1793 	return dso_bid->size == bid->size &&
1794 	       memcmp(dso_bid->data, bid->data, dso_bid->size) == 0;
1795 }
1796 
1797 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1798 {
1799 	char path[PATH_MAX];
1800 	struct build_id bid = { .size = 0, };
1801 
1802 	if (machine__is_default_guest(machine))
1803 		return;
1804 	snprintf(path, sizeof(path), "%s/sys/kernel/notes", machine->root_dir);
1805 	sysfs__read_build_id(path, &bid);
1806 	dso__set_build_id(dso, &bid);
1807 }
1808 
1809 int dso__kernel_module_get_build_id(struct dso *dso,
1810 				    const char *root_dir)
1811 {
1812 	char filename[PATH_MAX];
1813 	struct build_id bid = { .size = 0, };
1814 	/*
1815 	 * kernel module short names are of the form "[module]" and
1816 	 * we need just "module" here.
1817 	 */
1818 	const char *name = dso__short_name(dso) + 1;
1819 
1820 	snprintf(filename, sizeof(filename),
1821 		 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1822 		 root_dir, (int)strlen(name) - 1, name);
1823 
1824 	sysfs__read_build_id(filename, &bid);
1825 	dso__set_build_id(dso, &bid);
1826 	return 0;
1827 }
1828 
1829 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1830 {
1831 	char sbuild_id[SBUILD_ID_SIZE];
1832 
1833 	build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id));
1834 	return fprintf(fp, "%s", sbuild_id);
1835 }
1836 
1837 size_t dso__fprintf(struct dso *dso, FILE *fp)
1838 {
1839 	struct rb_node *nd;
1840 	size_t ret = fprintf(fp, "dso: %s (", dso__short_name(dso));
1841 
1842 	if (dso__short_name(dso) != dso__long_name(dso))
1843 		ret += fprintf(fp, "%s, ", dso__long_name(dso));
1844 	ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1845 	ret += dso__fprintf_buildid(dso, fp);
1846 	ret += fprintf(fp, ")\n");
1847 	for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd)) {
1848 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1849 		ret += symbol__fprintf(pos, fp);
1850 	}
1851 
1852 	return ret;
1853 }
1854 
1855 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1856 {
1857 	int fd = -1;
1858 	enum dso_type type = DSO__TYPE_UNKNOWN;
1859 
1860 	if (dso__data_get_fd(dso, machine, &fd)) {
1861 		type = dso__type_fd(fd);
1862 		dso__data_put_fd(dso);
1863 	}
1864 
1865 	return type;
1866 }
1867 
1868 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1869 {
1870 	int idx, errnum = *dso__load_errno(dso);
1871 	/*
1872 	 * This must have a same ordering as the enum dso_load_errno.
1873 	 */
1874 	static const char *dso_load__error_str[] = {
1875 	"Internal tools/perf/ library error",
1876 	"Invalid ELF file",
1877 	"Can not read build id",
1878 	"Mismatching build id",
1879 	"Decompression failure",
1880 	};
1881 
1882 	BUG_ON(buflen == 0);
1883 
1884 	if (errnum >= 0) {
1885 		errno = errnum;
1886 		scnprintf(buf, buflen, "%m");
1887 
1888 		return 0;
1889 	}
1890 
1891 	if (errnum <  __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1892 		return -1;
1893 
1894 	idx = errnum - __DSO_LOAD_ERRNO__START;
1895 	scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1896 	return 0;
1897 }
1898 
1899 bool perf_pid_map_tid(const char *dso_name, int *tid)
1900 {
1901 	return sscanf(dso_name, "/tmp/perf-%d.map", tid) == 1;
1902 }
1903 
1904 bool is_perf_pid_map_name(const char *dso_name)
1905 {
1906 	int tid;
1907 
1908 	return perf_pid_map_tid(dso_name, &tid);
1909 }
1910 
1911 struct find_file_offset_data {
1912 	u64 ip;
1913 	u64 offset;
1914 };
1915 
1916 /* This will be called for each PHDR in an ELF binary */
1917 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1918 {
1919 	struct find_file_offset_data *data = arg;
1920 
1921 	if (start <= data->ip && data->ip < start + len) {
1922 		data->offset = pgoff + data->ip - start;
1923 		return 1;
1924 	}
1925 	return 0;
1926 }
1927 
1928 static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename,
1929 				    u64 start, size_t len,
1930 				    u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
1931 {
1932 	struct nscookie nsc;
1933 	int fd;
1934 	ssize_t count;
1935 	struct find_file_offset_data data = {
1936 		.ip = start,
1937 	};
1938 	u8 *code_buf = NULL;
1939 	int saved_errno;
1940 
1941 	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1942 	fd = open(symfs_filename, O_RDONLY | O_CLOEXEC);
1943 	saved_errno = errno;
1944 	nsinfo__mountns_exit(&nsc);
1945 	if (fd < 0) {
1946 		errno = saved_errno;
1947 		return NULL;
1948 	}
1949 	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) <= 0) {
1950 		close(fd);
1951 		errno = ENOENT;
1952 		return NULL;
1953 	}
1954 	code_buf = malloc(len);
1955 	if (code_buf == NULL) {
1956 		close(fd);
1957 		errno = ENOMEM;
1958 		return NULL;
1959 	}
1960 	count = pread(fd, code_buf, len, data.offset);
1961 	saved_errno = errno;
1962 	close(fd);
1963 	if ((u64)count != len) {
1964 		free(code_buf);
1965 		errno = saved_errno;
1966 		return NULL;
1967 	}
1968 	*out_buf = code_buf;
1969 	*out_buf_len = len;
1970 	return code_buf;
1971 }
1972 
1973 /*
1974  * Read a symbol into memory for disassembly by a library like capstone of
1975  * libLLVM. If memory is allocated out_buf holds it.
1976  */
1977 const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
1978 			   const struct map *map, const struct symbol *sym,
1979 			   u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
1980 {
1981 	u64 start = map__rip_2objdump(map, sym->start);
1982 	u64 end = map__rip_2objdump(map, sym->end);
1983 	size_t len = end - start;
1984 
1985 	*out_buf = NULL;
1986 	*out_buf_len = 0;
1987 	*is_64bit = false;
1988 
1989 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
1990 		/*
1991 		 * Note, there is fallback BPF image disassembly in the objdump
1992 		 * version but it currently does nothing.
1993 		 */
1994 		errno = EOPNOTSUPP;
1995 		return NULL;
1996 	}
1997 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
1998 #ifdef HAVE_LIBBPF_SUPPORT
1999 		struct bpf_prog_info_node *info_node;
2000 		struct perf_bpil *info_linear;
2001 
2002 		*is_64bit = sizeof(void *) == sizeof(u64);
2003 		info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
2004 							 dso__bpf_prog(dso)->id);
2005 		if (!info_node) {
2006 			errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
2007 			return NULL;
2008 		}
2009 		info_linear = info_node->info_linear;
2010 		if (!(info_linear->arrays & (1UL << PERF_BPIL_JITED_INSNS))) {
2011 			errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
2012 			return NULL;
2013 		}
2014 		assert(len <= info_linear->info.jited_prog_len);
2015 		*out_buf_len = len;
2016 		return (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns);
2017 #else
2018 		pr_debug("No BPF program disassembly support\n");
2019 		errno = EOPNOTSUPP;
2020 		return NULL;
2021 #endif
2022 	}
2023 	return __dso__read_symbol(dso, symfs_filename, start, len,
2024 				  out_buf, out_buf_len, is_64bit);
2025 }
2026 
2027 struct debuginfo *dso__debuginfo(struct dso *dso)
2028 {
2029 	char *name;
2030 	bool decomp = false;
2031 	struct debuginfo *dinfo = NULL;
2032 
2033 	mutex_lock(dso__lock(dso));
2034 
2035 	name = dso__get_filename(dso, "", &decomp);
2036 	if (name)
2037 		dinfo = debuginfo__new(name);
2038 
2039 	if (decomp)
2040 		unlink(name);
2041 
2042 	mutex_unlock(dso__lock(dso));
2043 	free(name);
2044 	return dinfo;
2045 }
2046