xref: /linux/tools/perf/util/symbol.h (revision 19a9ed115fda95317c98bef0c716ea8412cd8ce0)
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 <linux/list.h>
10 #include <linux/rbtree.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include "addr_location.h"
14 #include "path.h"
15 #include "symbol_conf.h"
16 #include "spark.h"
17 #include "util.h"
18 
19 #ifdef HAVE_LIBELF_SUPPORT
20 #include <libelf.h>
21 #include <gelf.h>
22 #endif
23 #include <elf.h>
24 
25 struct dso;
26 struct map;
27 struct maps;
28 struct option;
29 struct build_id;
30 
31 /*
32  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
33  * for newer versions we can use mmap to reduce memory usage:
34  */
35 #ifdef ELF_C_READ_MMAP
36 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
37 #else
38 # define PERF_ELF_C_READ_MMAP ELF_C_READ
39 #endif
40 
41 #ifdef HAVE_LIBELF_SUPPORT
42 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
43 			     GElf_Shdr *shp, const char *name, size_t *idx);
44 #endif
45 
46 /**
47  * A symtab entry. When allocated this may be preceded by an annotation (see
48  * symbol__annotation) and/or a browser_index (see symbol__browser_index).
49  */
50 struct symbol {
51 	struct rb_node	rb_node;
52 	/** Range of symbol [start, end). */
53 	u64		start;
54 	u64		end;
55 	/** Length of the string name. */
56 	u16		namelen;
57 	/** ELF symbol type as defined for st_info. E.g STT_OBJECT or STT_FUNC. */
58 	u8		type:4;
59 	/** ELF binding type as defined for st_info. E.g. STB_WEAK or STB_GLOBAL. */
60 	u8		binding:4;
61 	/** Set true for kernel symbols of idle routines. */
62 	u8		idle:1;
63 	/** Resolvable but tools ignore it (e.g. idle routines). */
64 	u8		ignore:1;
65 	/** Symbol for an inlined function. */
66 	u8		inlined:1;
67 	/** Has symbol__annotate2 been performed. */
68 	u8		annotate2:1;
69 	/** Symbol is an alias of an STT_GNU_IFUNC */
70 	u8		ifunc_alias:1;
71 	/** Architecture specific. Unused except on PPC where it holds st_other. */
72 	u8		arch_sym;
73 	/** The name of length namelen associated with the symbol. */
74 	char		name[];
75 };
76 
77 void symbol__delete(struct symbol *sym);
78 void symbols__delete(struct rb_root_cached *symbols);
79 
80 /* symbols__for_each_entry - iterate over symbols (rb_root)
81  *
82  * @symbols: the rb_root of symbols
83  * @pos: the 'struct symbol *' to use as a loop cursor
84  * @nd: the 'struct rb_node *' to use as a temporary storage
85  */
86 #define symbols__for_each_entry(symbols, pos, nd)			\
87 	for (nd = rb_first_cached(symbols);					\
88 	     nd && (pos = rb_entry(nd, struct symbol, rb_node));	\
89 	     nd = rb_next(nd))
90 
91 static inline size_t symbol__size(const struct symbol *sym)
92 {
93 	return sym->end - sym->start;
94 }
95 
96 struct strlist;
97 struct intlist;
98 
99 static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
100 {
101 	if (symbol_conf.symfs_layout_flat)
102 		return path__join(bf, size, symbol_conf.symfs, perf_basename(path));
103 
104 	return path__join(bf, size, symbol_conf.symfs, path);
105 }
106 
107 #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
108 
109 extern int vmlinux_path__nr_entries;
110 extern char **vmlinux_path;
111 
112 static inline void *symbol__priv(struct symbol *sym)
113 {
114 	return ((void *)sym) - symbol_conf.priv_size;
115 }
116 
117 struct ref_reloc_sym {
118 	const char	*name;
119 	u64		addr;
120 	u64		unrelocated_addr;
121 };
122 
123 int dso__load(struct dso *dso, struct map *map);
124 int dso__load_vmlinux(struct dso *dso, struct map *map,
125 		      const char *vmlinux, bool vmlinux_allocated);
126 int dso__load_vmlinux_path(struct dso *dso, struct map *map);
127 int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
128 			 bool no_kcore);
129 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
130 
131 void dso__insert_symbol(struct dso *dso,
132 			struct symbol *sym);
133 void dso__delete_symbol(struct dso *dso,
134 			struct symbol *sym);
135 
136 struct symbol *dso__find_symbol(struct dso *dso, u64 addr);
137 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr);
138 
139 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx);
140 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx);
141 
142 struct symbol *dso__first_symbol(struct dso *dso);
143 struct symbol *dso__last_symbol(struct dso *dso);
144 struct symbol *dso__next_symbol(struct symbol *sym);
145 
146 enum dso_type dso__type_fd(int fd);
147 
148 int filename__read_build_id(const char *filename, struct build_id *id);
149 int sysfs__read_build_id(const char *filename, struct build_id *bid);
150 int modules__parse(const char *filename, void *arg,
151 		   int (*process_module)(void *arg, const char *name,
152 					 u64 start, u64 size));
153 int filename__read_debuglink(const char *filename, char *debuglink,
154 			     size_t size);
155 bool filename__has_section(const char *filename, const char *sec);
156 
157 struct perf_env;
158 int symbol__init(struct perf_env *env);
159 void symbol__exit(void);
160 void symbol__elf_init(void);
161 int symbol__annotation_init(void);
162 
163 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
164 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
165 				      const struct addr_location *al,
166 				      bool unknown_as_addr,
167 				      bool print_offsets, FILE *fp);
168 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
169 				    const struct addr_location *al, FILE *fp);
170 size_t __symbol__fprintf_symname(const struct symbol *sym,
171 				 const struct addr_location *al,
172 				 bool unknown_as_addr, FILE *fp);
173 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
174 size_t symbol__fprintf(struct symbol *sym, FILE *fp);
175 bool symbol__restricted_filename(const char *filename,
176 				 const char *restricted_filename);
177 
178 #define SYMFS_HELP "setup root directory which contains debug files:\n" \
179 	"\t\t\t\t" "directory:\tLook for files with symbols relative to this directory.\n" \
180 	"\t\t\t\t" "layout:   \tLayout of files, 'hierarchy' matches full path (default), 'flat' only matches base name.\n"
181 
182 int symbol__config_symfs(const struct option *opt __maybe_unused,
183 			 const char *dir, int unset __maybe_unused);
184 
185 struct symsrc;
186 
187 #ifdef HAVE_LIBBFD_SUPPORT
188 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
189 #endif
190 
191 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
192 		  struct symsrc *runtime_ss, int kmodule);
193 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
194 
195 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
196 
197 void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym,
198 		       bool kernel);
199 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
200 void symbols__fixup_duplicate(struct rb_root_cached *symbols);
201 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms);
202 
203 typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
204 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
205 		    bool *is_64_bit);
206 
207 #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
208 
209 struct kcore_extract {
210 	char *kcore_filename;
211 	u64 addr;
212 	u64 offs;
213 	u64 len;
214 	char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
215 	int fd;
216 };
217 
218 int kcore_extract__create(struct kcore_extract *kce);
219 void kcore_extract__delete(struct kcore_extract *kce);
220 
221 int kcore_copy(const char *from_dir, const char *to_dir);
222 int compare_proc_modules(const char *from, const char *to);
223 
224 int setup_list(struct strlist **list, const char *list_str,
225 	       const char *list_name);
226 int setup_intlist(struct intlist **list, const char *list_str,
227 		  const char *list_name);
228 
229 #ifdef HAVE_LIBELF_SUPPORT
230 void arch__sym_update(struct symbol *s, GElf_Sym *sym);
231 #endif
232 
233 const char *arch__normalize_symbol_name(const char *name);
234 #define SYMBOL_A 0
235 #define SYMBOL_B 1
236 
237 int arch__compare_symbol_names(const char *namea, const char *nameb);
238 int arch__compare_symbol_names_n(const char *namea, const char *nameb,
239 				 unsigned int n);
240 int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
241 
242 enum symbol_tag_include {
243 	SYMBOL_TAG_INCLUDE__NONE = 0,
244 	SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
245 };
246 
247 int symbol__match_symbol_name(const char *namea, const char *nameb,
248 			      enum symbol_tag_include includes);
249 
250 /* structure containing an SDT note's info */
251 struct sdt_note {
252 	char *name;			/* name of the note*/
253 	char *provider;			/* provider name */
254 	char *args;
255 	bool bit32;			/* whether the location is 32 bits? */
256 	union {				/* location, base and semaphore addrs */
257 		Elf64_Addr a64[3];
258 		Elf32_Addr a32[3];
259 	} addr;
260 	struct list_head note_list;	/* SDT notes' list */
261 };
262 
263 int get_sdt_note_list(struct list_head *head, const char *target);
264 int cleanup_sdt_note_list(struct list_head *sdt_notes);
265 int sdt_notes__get_count(struct list_head *start);
266 
267 #define SDT_PROBES_SCN ".probes"
268 #define SDT_BASE_SCN ".stapsdt.base"
269 #define SDT_NOTE_SCN  ".note.stapsdt"
270 #define SDT_NOTE_TYPE 3
271 #define SDT_NOTE_NAME "stapsdt"
272 #define NR_ADDR 3
273 
274 enum {
275 	SDT_NOTE_IDX_LOC = 0,
276 	SDT_NOTE_IDX_BASE,
277 	SDT_NOTE_IDX_REFCTR,
278 };
279 
280 int symbol__validate_sym_arguments(void);
281 
282 #endif /* __PERF_SYMBOL */
283