xref: /linux/tools/perf/util/symbol.h (revision db91da8f05185706f492841d96864ad78893dd62)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_SYMBOL
3 #define __PERF_SYMBOL 1
4 
5 #include <linux/types.h>
6 #include <linux/refcount.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdatomic.h>
10 #include <linux/list.h>
11 #include <linux/livepatch_external.h>
12 #include <linux/rbtree.h>
13 #include <linux/string.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include "addr_location.h"
17 #include "path.h"
18 #include "symbol_conf.h"
19 #include "spark.h"
20 #include "util.h"
21 
22 #ifdef HAVE_LIBELF_SUPPORT
23 #include <libelf.h>
24 #include <gelf.h>
25 #endif
26 #include <elf.h>
27 
28 struct dso;
29 struct map;
30 struct maps;
31 struct option;
32 struct build_id;
33 struct perf_env;
34 
35 /*
36  * Ignore local labels and mapping symbols, matching kernel is_ignored_kernel_symbol() logic.
37  * Only use this for kernel symbols (kallsyms, ksymbol events, kernel ELF DSOs).
38  */
39 static inline bool is_ignored_kernel_symbol(const char *str)
40 {
41 	if (str[0] == '.' && str[1] == 'L')
42 		return true;
43 	if (str[0] == 'L' && str[1] == '0')
44 		return true;
45 	return str[0] == '$';
46 }
47 
48 /*
49  * Livepatch symbols (.klp.sym.*) are relocation placeholders whose resolved
50  * addresses alias existing kernel symbols.  They carry a [module] tag which
51  * confuses module boundary tracking and symbol table lookups.
52  */
53 static inline bool is_livepatch_symbol(const char *str)
54 {
55 	return strstarts(str, KLP_SYM_PREFIX);
56 }
57 
58 /*
59  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
60  * for newer versions we can use mmap to reduce memory usage:
61  */
62 #ifdef ELF_C_READ_MMAP
63 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
64 #else
65 # define PERF_ELF_C_READ_MMAP ELF_C_READ
66 #endif
67 
68 #ifdef HAVE_LIBELF_SUPPORT
69 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
70 			     GElf_Shdr *shp, const char *name, size_t *idx);
71 #endif
72 
73 enum symbol_idle_kind {
74 	SYMBOL_IDLE__UNKNOWN = 0,
75 	SYMBOL_IDLE__NOT_IDLE = 1,
76 	SYMBOL_IDLE__IDLE = 2,
77 };
78 
79 #define SYMBOL_FLAG_TYPE_SHIFT      0
80 #define SYMBOL_FLAG_TYPE_MASK       (0xF << SYMBOL_FLAG_TYPE_SHIFT)
81 #define SYMBOL_FLAG_BINDING_SHIFT   4
82 #define SYMBOL_FLAG_BINDING_MASK    (0xF << SYMBOL_FLAG_BINDING_SHIFT)
83 #define SYMBOL_FLAG_IDLE_SHIFT      8
84 #define SYMBOL_FLAG_IDLE_MASK       (0x3 << SYMBOL_FLAG_IDLE_SHIFT)
85 #define SYMBOL_FLAG_IGNORE          (1 << 10)
86 #define SYMBOL_FLAG_INLINED         (1 << 11)
87 #define SYMBOL_FLAG_ANNOTATE2       (1 << 12)
88 #define SYMBOL_FLAG_IFUNC_ALIAS     (1 << 13)
89 /**
90  * A symtab entry. When allocated this may be preceded by an annotation (see
91  * symbol__annotation) and/or a browser_index (see symbol__browser_index).
92  */
93 struct symbol {
94 	struct rb_node	rb_node;
95 	/** Range of symbol [start, end). */
96 	u64		start;
97 	u64		end;
98 	/** Length of the string name. */
99 	u16		namelen;
100 	_Atomic uint16_t flags;
101 	/** Architecture specific. Unused except on PPC where it holds st_other. */
102 	u8		arch_sym;
103 	/** The name of length namelen associated with the symbol. */
104 	char		name[];
105 };
106 
107 void symbol__delete(struct symbol *sym);
108 void symbols__delete(struct rb_root_cached *symbols);
109 
110 static inline u8 symbol__type(const struct symbol *sym)
111 {
112 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
113 		SYMBOL_FLAG_TYPE_MASK) >> SYMBOL_FLAG_TYPE_SHIFT;
114 }
115 
116 static inline u8 symbol__binding(const struct symbol *sym)
117 {
118 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
119 		SYMBOL_FLAG_BINDING_MASK) >> SYMBOL_FLAG_BINDING_SHIFT;
120 }
121 
122 static inline bool symbol__ignore(const struct symbol *sym)
123 {
124 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
125 		SYMBOL_FLAG_IGNORE) != 0;
126 }
127 
128 static inline bool symbol__inlined(const struct symbol *sym)
129 {
130 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
131 		SYMBOL_FLAG_INLINED) != 0;
132 }
133 
134 static inline bool symbol__is_annotate2(const struct symbol *sym)
135 {
136 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
137 		SYMBOL_FLAG_ANNOTATE2) != 0;
138 }
139 
140 static inline bool symbol__ifunc_alias(const struct symbol *sym)
141 {
142 	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
143 		SYMBOL_FLAG_IFUNC_ALIAS) != 0;
144 }
145 
146 bool symbol__is_idle(struct symbol *sym, const struct dso *dso, struct perf_env *env);
147 
148 void symbol__set_ignore(struct symbol *sym, bool ignore);
149 void symbol__set_annotate2(struct symbol *sym, bool annotate2);
150 void symbol__set_inlined(struct symbol *sym, bool inlined);
151 void symbol__set_ifunc_alias(struct symbol *sym, bool ifunc_alias);
152 
153 /* symbols__for_each_entry - iterate over symbols (rb_root)
154  *
155  * @symbols: the rb_root of symbols
156  * @pos: the 'struct symbol *' to use as a loop cursor
157  * @nd: the 'struct rb_node *' to use as a temporary storage
158  */
159 #define symbols__for_each_entry(symbols, pos, nd)			\
160 	for (nd = rb_first_cached(symbols);					\
161 	     nd && (pos = rb_entry(nd, struct symbol, rb_node));	\
162 	     nd = rb_next(nd))
163 
164 static inline size_t symbol__size(const struct symbol *sym)
165 {
166 	return sym->end - sym->start;
167 }
168 
169 struct strlist;
170 struct intlist;
171 
172 static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
173 {
174 	if (symbol_conf.symfs_layout_flat)
175 		return path__join(bf, size, symbol_conf.symfs, perf_basename(path));
176 
177 	return path__join(bf, size, symbol_conf.symfs, path);
178 }
179 
180 #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
181 
182 extern int vmlinux_path__nr_entries;
183 extern char **vmlinux_path;
184 
185 static inline void *symbol__priv(struct symbol *sym)
186 {
187 	return ((void *)sym) - symbol_conf.priv_size;
188 }
189 
190 struct ref_reloc_sym {
191 	const char	*name;
192 	u64		addr;
193 	u64		unrelocated_addr;
194 };
195 
196 int dso__load(struct dso *dso, struct map *map);
197 int dso__load_vmlinux(struct dso *dso, struct map *map,
198 		      const char *vmlinux, bool vmlinux_allocated);
199 int dso__load_vmlinux_path(struct dso *dso, struct map *map);
200 int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
201 			 bool no_kcore);
202 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
203 
204 void dso__insert_symbol(struct dso *dso,
205 			struct symbol *sym);
206 void dso__delete_symbol(struct dso *dso,
207 			struct symbol *sym);
208 
209 struct symbol *dso__find_symbol(struct dso *dso, u64 addr);
210 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr);
211 
212 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx);
213 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx);
214 
215 struct symbol *dso__first_symbol(struct dso *dso);
216 struct symbol *dso__last_symbol(struct dso *dso);
217 struct symbol *dso__next_symbol(struct symbol *sym);
218 
219 enum dso_type dso__type_fd(int fd);
220 
221 int filename__read_build_id(const char *filename, struct build_id *id);
222 int sysfs__read_build_id(const char *filename, struct build_id *bid);
223 int modules__parse(const char *filename, void *arg,
224 		   int (*process_module)(void *arg, const char *name,
225 					 u64 start, u64 size));
226 int filename__read_debuglink(const char *filename, char *debuglink,
227 			     size_t size);
228 bool filename__has_section(const char *filename, const char *sec);
229 
230 int symbol__init(struct perf_env *env);
231 void symbol__exit(void);
232 void symbol__elf_init(void);
233 int symbol__annotation_init(void);
234 
235 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
236 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
237 				      const struct addr_location *al,
238 				      bool unknown_as_addr,
239 				      bool print_offsets, FILE *fp);
240 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
241 				    const struct addr_location *al, FILE *fp);
242 size_t __symbol__fprintf_symname(const struct symbol *sym,
243 				 const struct addr_location *al,
244 				 bool unknown_as_addr, FILE *fp);
245 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
246 size_t symbol__fprintf(struct symbol *sym, FILE *fp);
247 bool symbol__restricted_filename(const char *filename,
248 				 const char *restricted_filename);
249 
250 #define SYMFS_HELP "setup root directory which contains debug files:\n" \
251 	"\t\t\t\t" "directory:\tLook for files with symbols relative to this directory.\n" \
252 	"\t\t\t\t" "layout:   \tLayout of files, 'hierarchy' matches full path (default), 'flat' only matches base name.\n"
253 
254 int symbol__config_symfs(const struct option *opt __maybe_unused,
255 			 const char *dir, int unset __maybe_unused);
256 
257 struct symsrc;
258 
259 #ifdef HAVE_LIBBFD_SUPPORT
260 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
261 #endif
262 
263 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
264 		  struct symsrc *runtime_ss, int kmodule);
265 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
266 
267 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
268 
269 void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
270 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
271 void symbols__fixup_duplicate(struct rb_root_cached *symbols);
272 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms);
273 
274 typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
275 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
276 		    bool *is_64_bit);
277 
278 #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
279 
280 struct kcore_extract {
281 	char *kcore_filename;
282 	u64 addr;
283 	u64 offs;
284 	u64 len;
285 	char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
286 	int fd;
287 };
288 
289 int kcore_extract__create(struct kcore_extract *kce);
290 void kcore_extract__delete(struct kcore_extract *kce);
291 
292 int kcore_copy(const char *from_dir, const char *to_dir);
293 int compare_proc_modules(const char *from, const char *to);
294 
295 int setup_list(struct strlist **list, const char *list_str,
296 	       const char *list_name);
297 int setup_intlist(struct intlist **list, const char *list_str,
298 		  const char *list_name);
299 
300 #ifdef HAVE_LIBELF_SUPPORT
301 void arch__sym_update(struct symbol *s, GElf_Sym *sym);
302 #endif
303 
304 const char *arch__normalize_symbol_name(const char *name);
305 #define SYMBOL_A 0
306 #define SYMBOL_B 1
307 
308 int arch__compare_symbol_names(const char *namea, const char *nameb);
309 int arch__compare_symbol_names_n(const char *namea, const char *nameb,
310 				 unsigned int n);
311 int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
312 
313 enum symbol_tag_include {
314 	SYMBOL_TAG_INCLUDE__NONE = 0,
315 	SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
316 };
317 
318 int symbol__match_symbol_name(const char *namea, const char *nameb,
319 			      enum symbol_tag_include includes);
320 
321 /* structure containing an SDT note's info */
322 struct sdt_note {
323 	char *name;			/* name of the note*/
324 	char *provider;			/* provider name */
325 	char *args;
326 	bool bit32;			/* whether the location is 32 bits? */
327 	union {				/* location, base and semaphore addrs */
328 		Elf64_Addr a64[3];
329 		Elf32_Addr a32[3];
330 	} addr;
331 	struct list_head note_list;	/* SDT notes' list */
332 };
333 
334 int get_sdt_note_list(struct list_head *head, const char *target);
335 int cleanup_sdt_note_list(struct list_head *sdt_notes);
336 int sdt_notes__get_count(struct list_head *start);
337 
338 #define SDT_PROBES_SCN ".probes"
339 #define SDT_BASE_SCN ".stapsdt.base"
340 #define SDT_NOTE_SCN  ".note.stapsdt"
341 #define SDT_NOTE_TYPE 3
342 #define SDT_NOTE_NAME "stapsdt"
343 #define NR_ADDR 3
344 
345 enum {
346 	SDT_NOTE_IDX_LOC = 0,
347 	SDT_NOTE_IDX_BASE,
348 	SDT_NOTE_IDX_REFCTR,
349 };
350 
351 int symbol__validate_sym_arguments(void);
352 
353 #endif /* __PERF_SYMBOL */
354