1// SPDX-License-Identifier: GPL-2.0
2#include <inttypes.h>
3#include <limits.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <linux/string.h>
8#include <linux/zalloc.h>
9#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
10#include "debug.h"
11#include "dso.h"
12#include "map.h"
13#include "namespaces.h"
14#include "srcline.h"
15#include "symbol.h"
16#include "thread.h"
17#include "vdso.h"
18
19static inline int is_android_lib(const char *filename)
20{
21	return strstarts(filename, "/data/app-lib/") ||
22	       strstarts(filename, "/system/lib/");
23}
24
25static inline bool replace_android_lib(const char *filename, char *newfilename)
26{
27	const char *libname;
28	char *app_abi;
29	size_t app_abi_length, new_length;
30	size_t lib_length = 0;
31
32	libname  = strrchr(filename, '/');
33	if (libname)
34		lib_length = strlen(libname);
35
36	app_abi = getenv("APP_ABI");
37	if (!app_abi)
38		return false;
39
40	app_abi_length = strlen(app_abi);
41
42	if (strstarts(filename, "/data/app-lib/")) {
43		char *apk_path;
44
45		if (!app_abi_length)
46			return false;
47
48		new_length = 7 + app_abi_length + lib_length;
49
50		apk_path = getenv("APK_PATH");
51		if (apk_path) {
52			new_length += strlen(apk_path) + 1;
53			if (new_length > PATH_MAX)
54				return false;
55			snprintf(newfilename, new_length,
56				 "%s/libs/%s/%s", apk_path, app_abi, libname);
57		} else {
58			if (new_length > PATH_MAX)
59				return false;
60			snprintf(newfilename, new_length,
61				 "libs/%s/%s", app_abi, libname);
62		}
63		return true;
64	}
65
66	if (strstarts(filename, "/system/lib/")) {
67		char *ndk, *app;
68		const char *arch;
69		int ndk_length, app_length;
70
71		ndk = getenv("NDK_ROOT");
72		app = getenv("APP_PLATFORM");
73
74		if (!(ndk && app))
75			return false;
76
77		ndk_length = strlen(ndk);
78		app_length = strlen(app);
79
80		if (!(ndk_length && app_length && app_abi_length))
81			return false;
82
83		arch = !strncmp(app_abi, "arm", 3) ? "arm" :
84		       !strncmp(app_abi, "mips", 4) ? "mips" :
85		       !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
86
87		if (!arch)
88			return false;
89
90		new_length = 27 + ndk_length +
91			     app_length + lib_length
92			   + strlen(arch);
93
94		if (new_length > PATH_MAX)
95			return false;
96		snprintf(newfilename, new_length,
97			"%.*s/platforms/%.*s/arch-%s/usr/lib/%s",
98			ndk_length, ndk, app_length, app, arch, libname);
99
100		return true;
101	}
102	return false;
103}
104
105void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
106{
107	map__set_start(map, start);
108	map__set_end(map, end);
109	map__set_pgoff(map, pgoff);
110	map__set_reloc(map, 0);
111	map__set_dso(map, dso__get(dso));
112	map__set_mapping_type(map, MAPPING_TYPE__DSO);
113	map__set_erange_warned(map, false);
114	refcount_set(map__refcnt(map), 1);
115}
116
117struct map *map__new(struct machine *machine, u64 start, u64 len,
118		     u64 pgoff, struct dso_id *id,
119		     u32 prot, u32 flags, struct build_id *bid,
120		     char *filename, struct thread *thread)
121{
122	struct map *result;
123	RC_STRUCT(map) *map;
124	struct nsinfo *nsi = NULL;
125	struct nsinfo *nnsi;
126
127	map = malloc(sizeof(*map));
128	if (ADD_RC_CHK(result, map)) {
129		char newfilename[PATH_MAX];
130		struct dso *dso, *header_bid_dso;
131		int anon, no_dso, vdso, android;
132
133		android = is_android_lib(filename);
134		anon = is_anon_memory(filename) || flags & MAP_HUGETLB;
135		vdso = is_vdso_map(filename);
136		no_dso = is_no_dso_memory(filename);
137		map->prot = prot;
138		map->flags = flags;
139		nsi = nsinfo__get(thread__nsinfo(thread));
140
141		if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
142			snprintf(newfilename, sizeof(newfilename),
143				 "/tmp/perf-%d.map", nsinfo__pid(nsi));
144			filename = newfilename;
145		}
146
147		if (android) {
148			if (replace_android_lib(filename, newfilename))
149				filename = newfilename;
150		}
151
152		if (vdso) {
153			/* The vdso maps are always on the host and not the
154			 * container.  Ensure that we don't use setns to look
155			 * them up.
156			 */
157			nnsi = nsinfo__copy(nsi);
158			if (nnsi) {
159				nsinfo__put(nsi);
160				nsinfo__clear_need_setns(nnsi);
161				nsi = nnsi;
162			}
163			pgoff = 0;
164			dso = machine__findnew_vdso(machine, thread);
165		} else
166			dso = machine__findnew_dso_id(machine, filename, id);
167
168		if (dso == NULL)
169			goto out_delete;
170
171		assert(!dso->kernel);
172		map__init(result, start, start + len, pgoff, dso);
173
174		if (anon || no_dso) {
175			map->mapping_type = MAPPING_TYPE__IDENTITY;
176
177			/*
178			 * Set memory without DSO as loaded. All map__find_*
179			 * functions still return NULL, and we avoid the
180			 * unnecessary map__load warning.
181			 */
182			if (!(prot & PROT_EXEC))
183				dso__set_loaded(dso);
184		}
185		mutex_lock(&dso->lock);
186		nsinfo__put(dso->nsinfo);
187		dso->nsinfo = nsi;
188		mutex_unlock(&dso->lock);
189
190		if (build_id__is_defined(bid)) {
191			dso__set_build_id(dso, bid);
192		} else {
193			/*
194			 * If the mmap event had no build ID, search for an existing dso from the
195			 * build ID header by name. Otherwise only the dso loaded at the time of
196			 * reading the header will have the build ID set and all future mmaps will
197			 * have it missing.
198			 */
199			down_read(&machine->dsos.lock);
200			header_bid_dso = __dsos__find(&machine->dsos, filename, false);
201			up_read(&machine->dsos.lock);
202			if (header_bid_dso && header_bid_dso->header_build_id) {
203				dso__set_build_id(dso, &header_bid_dso->bid);
204				dso->header_build_id = 1;
205			}
206		}
207		dso__put(dso);
208	}
209	return result;
210out_delete:
211	nsinfo__put(nsi);
212	RC_CHK_FREE(result);
213	return NULL;
214}
215
216/*
217 * Constructor variant for modules (where we know from /proc/modules where
218 * they are loaded) and for vmlinux, where only after we load all the
219 * symbols we'll know where it starts and ends.
220 */
221struct map *map__new2(u64 start, struct dso *dso)
222{
223	struct map *result;
224	RC_STRUCT(map) *map;
225
226	map = calloc(1, sizeof(*map) + (dso->kernel ? sizeof(struct kmap) : 0));
227	if (ADD_RC_CHK(result, map)) {
228		/*
229		 * ->end will be filled after we load all the symbols
230		 */
231		map__init(result, start, 0, 0, dso);
232	}
233
234	return result;
235}
236
237bool __map__is_kernel(const struct map *map)
238{
239	if (!map__dso(map)->kernel)
240		return false;
241	return machine__kernel_map(maps__machine(map__kmaps((struct map *)map))) == map;
242}
243
244bool __map__is_extra_kernel_map(const struct map *map)
245{
246	struct kmap *kmap = __map__kmap((struct map *)map);
247
248	return kmap && kmap->name[0];
249}
250
251bool __map__is_bpf_prog(const struct map *map)
252{
253	const char *name;
254	struct dso *dso = map__dso(map);
255
256	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
257		return true;
258
259	/*
260	 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
261	 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
262	 * guess the type based on name.
263	 */
264	name = dso->short_name;
265	return name && (strstr(name, "bpf_prog_") == name);
266}
267
268bool __map__is_bpf_image(const struct map *map)
269{
270	const char *name;
271	struct dso *dso = map__dso(map);
272
273	if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE)
274		return true;
275
276	/*
277	 * If PERF_RECORD_KSYMBOL is not included, the dso will not have
278	 * type of DSO_BINARY_TYPE__BPF_IMAGE. In such cases, we can
279	 * guess the type based on name.
280	 */
281	name = dso->short_name;
282	return name && is_bpf_image(name);
283}
284
285bool __map__is_ool(const struct map *map)
286{
287	const struct dso *dso = map__dso(map);
288
289	return dso && dso->binary_type == DSO_BINARY_TYPE__OOL;
290}
291
292bool map__has_symbols(const struct map *map)
293{
294	return dso__has_symbols(map__dso(map));
295}
296
297static void map__exit(struct map *map)
298{
299	BUG_ON(refcount_read(map__refcnt(map)) != 0);
300	dso__zput(RC_CHK_ACCESS(map)->dso);
301}
302
303void map__delete(struct map *map)
304{
305	map__exit(map);
306	RC_CHK_FREE(map);
307}
308
309void map__put(struct map *map)
310{
311	if (map && refcount_dec_and_test(map__refcnt(map)))
312		map__delete(map);
313	else
314		RC_CHK_PUT(map);
315}
316
317void map__fixup_start(struct map *map)
318{
319	struct dso *dso = map__dso(map);
320	struct rb_root_cached *symbols = &dso->symbols;
321	struct rb_node *nd = rb_first_cached(symbols);
322
323	if (nd != NULL) {
324		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
325
326		map__set_start(map, sym->start);
327	}
328}
329
330void map__fixup_end(struct map *map)
331{
332	struct dso *dso = map__dso(map);
333	struct rb_root_cached *symbols = &dso->symbols;
334	struct rb_node *nd = rb_last(&symbols->rb_root);
335
336	if (nd != NULL) {
337		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
338		map__set_end(map, sym->end);
339	}
340}
341
342#define DSO__DELETED "(deleted)"
343
344int map__load(struct map *map)
345{
346	struct dso *dso = map__dso(map);
347	const char *name = dso->long_name;
348	int nr;
349
350	if (dso__loaded(dso))
351		return 0;
352
353	nr = dso__load(dso, map);
354	if (nr < 0) {
355		if (dso->has_build_id) {
356			char sbuild_id[SBUILD_ID_SIZE];
357
358			build_id__sprintf(&dso->bid, sbuild_id);
359			pr_debug("%s with build id %s not found", name, sbuild_id);
360		} else
361			pr_debug("Failed to open %s", name);
362
363		pr_debug(", continuing without symbols\n");
364		return -1;
365	} else if (nr == 0) {
366#ifdef HAVE_LIBELF_SUPPORT
367		const size_t len = strlen(name);
368		const size_t real_len = len - sizeof(DSO__DELETED);
369
370		if (len > sizeof(DSO__DELETED) &&
371		    strcmp(name + real_len + 1, DSO__DELETED) == 0) {
372			pr_debug("%.*s was updated (is prelink enabled?). "
373				"Restart the long running apps that use it!\n",
374				   (int)real_len, name);
375		} else {
376			pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
377		}
378#endif
379		return -1;
380	}
381
382	return 0;
383}
384
385struct symbol *map__find_symbol(struct map *map, u64 addr)
386{
387	if (map__load(map) < 0)
388		return NULL;
389
390	return dso__find_symbol(map__dso(map), addr);
391}
392
393struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *name, size_t *idx)
394{
395	struct dso *dso;
396
397	if (map__load(map) < 0)
398		return NULL;
399
400	dso = map__dso(map);
401	dso__sort_by_name(dso);
402
403	return dso__find_symbol_by_name(dso, name, idx);
404}
405
406struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
407{
408	size_t idx;
409
410	return map__find_symbol_by_name_idx(map, name, &idx);
411}
412
413struct map *map__clone(struct map *from)
414{
415	struct map *result;
416	RC_STRUCT(map) *map;
417	size_t size = sizeof(RC_STRUCT(map));
418	struct dso *dso = map__dso(from);
419
420	if (dso && dso->kernel)
421		size += sizeof(struct kmap);
422
423	map = memdup(RC_CHK_ACCESS(from), size);
424	if (ADD_RC_CHK(result, map)) {
425		refcount_set(&map->refcnt, 1);
426		map->dso = dso__get(dso);
427	}
428
429	return result;
430}
431
432size_t map__fprintf(struct map *map, FILE *fp)
433{
434	const struct dso *dso = map__dso(map);
435
436	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
437		       map__start(map), map__end(map), map__pgoff(map), dso->name);
438}
439
440static bool prefer_dso_long_name(const struct dso *dso, bool print_off)
441{
442	return dso->long_name &&
443	       (symbol_conf.show_kernel_path ||
444		(print_off && (dso->name[0] == '[' || dso__is_kcore(dso))));
445}
446
447static size_t __map__fprintf_dsoname(struct map *map, bool print_off, FILE *fp)
448{
449	char buf[symbol_conf.pad_output_len_dso + 1];
450	const char *dsoname = "[unknown]";
451	const struct dso *dso = map ? map__dso(map) : NULL;
452
453	if (dso) {
454		if (prefer_dso_long_name(dso, print_off))
455			dsoname = dso->long_name;
456		else
457			dsoname = dso->name;
458	}
459
460	if (symbol_conf.pad_output_len_dso) {
461		scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname);
462		dsoname = buf;
463	}
464
465	return fprintf(fp, "%s", dsoname);
466}
467
468size_t map__fprintf_dsoname(struct map *map, FILE *fp)
469{
470	return __map__fprintf_dsoname(map, false, fp);
471}
472
473size_t map__fprintf_dsoname_dsoff(struct map *map, bool print_off, u64 addr, FILE *fp)
474{
475	const struct dso *dso = map ? map__dso(map) : NULL;
476	int printed = 0;
477
478	if (print_off && (!dso || !dso__is_object_file(dso)))
479		print_off = false;
480	printed += fprintf(fp, " (");
481	printed += __map__fprintf_dsoname(map, print_off, fp);
482	if (print_off)
483		printed += fprintf(fp, "+0x%" PRIx64, addr);
484	printed += fprintf(fp, ")");
485
486	return printed;
487}
488
489char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
490{
491	if (map == NULL)
492		return SRCLINE_UNKNOWN;
493
494	return get_srcline(map__dso(map), map__rip_2objdump(map, addr), sym, true, true, addr);
495}
496
497int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
498			 FILE *fp)
499{
500	const struct dso *dso = map ? map__dso(map) : NULL;
501	int ret = 0;
502
503	if (dso) {
504		char *srcline = map__srcline(map, addr, NULL);
505		if (srcline != SRCLINE_UNKNOWN)
506			ret = fprintf(fp, "%s%s", prefix, srcline);
507		zfree_srcline(&srcline);
508	}
509	return ret;
510}
511
512void srccode_state_free(struct srccode_state *state)
513{
514	zfree(&state->srcfile);
515	state->line = 0;
516}
517
518/**
519 * map__rip_2objdump - convert symbol start address to objdump address.
520 * @map: memory map
521 * @rip: symbol start address
522 *
523 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
524 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
525 * relative to section start.
526 *
527 * Return: Address suitable for passing to "objdump --start-address="
528 */
529u64 map__rip_2objdump(struct map *map, u64 rip)
530{
531	struct kmap *kmap = __map__kmap(map);
532	const struct dso *dso = map__dso(map);
533
534	/*
535	 * vmlinux does not have program headers for PTI entry trampolines and
536	 * kcore may not either. However the trampoline object code is on the
537	 * main kernel map, so just use that instead.
538	 */
539	if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps) {
540		struct machine *machine = maps__machine(kmap->kmaps);
541
542		if (machine) {
543			struct map *kernel_map = machine__kernel_map(machine);
544
545			if (kernel_map)
546				map = kernel_map;
547		}
548	}
549
550	if (!dso->adjust_symbols)
551		return rip;
552
553	if (dso->rel)
554		return rip - map__pgoff(map);
555
556	if (dso->kernel == DSO_SPACE__USER)
557		return rip + dso->text_offset;
558
559	return map__unmap_ip(map, rip) - map__reloc(map);
560}
561
562/**
563 * map__objdump_2mem - convert objdump address to a memory address.
564 * @map: memory map
565 * @ip: objdump address
566 *
567 * Closely related to map__rip_2objdump(), this function takes an address from
568 * objdump and converts it to a memory address.  Note this assumes that @map
569 * contains the address.  To be sure the result is valid, check it forwards
570 * e.g. map__rip_2objdump(map__map_ip(map, map__objdump_2mem(map, ip))) == ip
571 *
572 * Return: Memory address.
573 */
574u64 map__objdump_2mem(struct map *map, u64 ip)
575{
576	const struct dso *dso = map__dso(map);
577
578	if (!dso->adjust_symbols)
579		return map__unmap_ip(map, ip);
580
581	if (dso->rel)
582		return map__unmap_ip(map, ip + map__pgoff(map));
583
584	if (dso->kernel == DSO_SPACE__USER)
585		return map__unmap_ip(map, ip - dso->text_offset);
586
587	return ip + map__reloc(map);
588}
589
590bool map__contains_symbol(const struct map *map, const struct symbol *sym)
591{
592	u64 ip = map__unmap_ip(map, sym->start);
593
594	return ip >= map__start(map) && ip < map__end(map);
595}
596
597struct kmap *__map__kmap(struct map *map)
598{
599	const struct dso *dso = map__dso(map);
600
601	if (!dso || !dso->kernel)
602		return NULL;
603	return (struct kmap *)(&RC_CHK_ACCESS(map)[1]);
604}
605
606struct kmap *map__kmap(struct map *map)
607{
608	struct kmap *kmap = __map__kmap(map);
609
610	if (!kmap)
611		pr_err("Internal error: map__kmap with a non-kernel map\n");
612	return kmap;
613}
614
615struct maps *map__kmaps(struct map *map)
616{
617	struct kmap *kmap = map__kmap(map);
618
619	if (!kmap || !kmap->kmaps) {
620		pr_err("Internal error: map__kmaps with a non-kernel map\n");
621		return NULL;
622	}
623	return kmap->kmaps;
624}
625