blob: e752837363ee210418aa57bc9916fa6aef1c6af7 [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02003#include "sort.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03004#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03005#include "symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03006#include "thread.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03007
Frederic Weisbecker8f288272009-08-16 22:05:48 +02008#include "debug.h"
9
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020010#include <asm/bug.h>
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030011#include <libelf.h>
12#include <gelf.h>
13#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020014#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030015#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020016
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020017#ifndef NT_GNU_BUILD_ID
18#define NT_GNU_BUILD_ID 3
19#endif
20
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030021enum dso_origin {
22 DSO__ORIG_KERNEL = 0,
23 DSO__ORIG_JAVA_JIT,
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020024 DSO__ORIG_BUILD_ID_CACHE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030025 DSO__ORIG_FEDORA,
26 DSO__ORIG_UBUNTU,
27 DSO__ORIG_BUILDID,
28 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030029 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030030 DSO__ORIG_NOT_FOUND,
31};
32
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020033static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020034static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020035static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020036 symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020037static int vmlinux_path__nr_entries;
38static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030039
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020040struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020041 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020042 .use_modules = true,
43 .try_vmlinux_path = true,
44};
45
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020046bool dso__loaded(const struct dso *self, enum map_type type)
47{
48 return self->loaded & (1 << type);
49}
50
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020051bool dso__sorted_by_name(const struct dso *self, enum map_type type)
52{
53 return self->sorted_by_name & (1 << type);
54}
55
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020056static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
57{
58 self->sorted_by_name |= (1 << type);
59}
60
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020061bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020062{
63 switch (map_type) {
64 case MAP__FUNCTION:
65 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020066 case MAP__VARIABLE:
67 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020068 default:
69 return false;
70 }
71}
72
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020073static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030074{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020075 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030076 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030077
78 if (prevnd == NULL)
79 return;
80
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030081 curr = rb_entry(prevnd, struct symbol, rb_node);
82
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030083 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030084 prev = curr;
85 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030086
87 if (prev->end == prev->start)
88 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030089 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030090
91 /* Last entry */
92 if (curr->end == curr->start)
93 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030094}
95
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020096static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030097{
98 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020099 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300100
101 if (prevnd == NULL)
102 return;
103
104 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300105
106 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
107 prev = curr;
108 curr = rb_entry(nd, struct map, rb_node);
109 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300110 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200111
112 /*
113 * We still haven't the actual symbols, so guess the
114 * last map final address.
115 */
116 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300117}
118
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200119static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200120{
121 int i;
122 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200123 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200124}
125
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200126static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300127{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300128 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200129 struct symbol *self = zalloc(symbol_conf.priv_size +
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200130 sizeof(*self) + namelen);
131 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200132 return NULL;
133
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200134 if (symbol_conf.priv_size)
135 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200136
Ingo Molnar0b73da32009-06-06 15:48:52 +0200137 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200138 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200139
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200140 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200141
Ingo Molnar0b73da32009-06-06 15:48:52 +0200142 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300143
144 return self;
145}
146
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200147static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300148{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200149 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300150}
151
152static size_t symbol__fprintf(struct symbol *self, FILE *fp)
153{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300154 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300155 self->start, self->end, self->name);
156}
157
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200158void dso__set_long_name(struct dso *self, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200159{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200160 if (name == NULL)
161 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200162 self->long_name = name;
163 self->long_name_len = strlen(name);
164}
165
166static void dso__set_basename(struct dso *self)
167{
168 self->short_name = basename(self->long_name);
169}
170
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200171struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300172{
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200173 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300174
175 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200176 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300177 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200178 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300179 self->short_name = self->name;
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200180 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200181 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300182 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300183 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200184 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200185 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200186 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300187 }
188
189 return self;
190}
191
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200192static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300193{
194 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200195 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300196
197 while (next) {
198 pos = rb_entry(next, struct symbol, rb_node);
199 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200200 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200201 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300202 }
203}
204
205void dso__delete(struct dso *self)
206{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200207 int i;
208 for (i = 0; i < MAP__NR_TYPES; ++i)
209 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300210 if (self->long_name != self->name)
211 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300212 free(self);
213}
214
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200215void dso__set_build_id(struct dso *self, void *build_id)
216{
217 memcpy(self->build_id, build_id, sizeof(self->build_id));
218 self->has_build_id = 1;
219}
220
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200221static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300222{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200223 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300224 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000225 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300226 struct symbol *s;
227
228 while (*p != NULL) {
229 parent = *p;
230 s = rb_entry(parent, struct symbol, rb_node);
231 if (ip < s->start)
232 p = &(*p)->rb_left;
233 else
234 p = &(*p)->rb_right;
235 }
236 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200237 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300238}
239
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200240static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300241{
242 struct rb_node *n;
243
244 if (self == NULL)
245 return NULL;
246
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200247 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300248
249 while (n) {
250 struct symbol *s = rb_entry(n, struct symbol, rb_node);
251
252 if (ip < s->start)
253 n = n->rb_left;
254 else if (ip > s->end)
255 n = n->rb_right;
256 else
257 return s;
258 }
259
260 return NULL;
261}
262
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200263struct symbol_name_rb_node {
264 struct rb_node rb_node;
265 struct symbol sym;
266};
267
268static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
269{
270 struct rb_node **p = &self->rb_node;
271 struct rb_node *parent = NULL;
272 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
273
274 while (*p != NULL) {
275 parent = *p;
276 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
277 if (strcmp(sym->name, s->sym.name) < 0)
278 p = &(*p)->rb_left;
279 else
280 p = &(*p)->rb_right;
281 }
282 rb_link_node(&symn->rb_node, parent, p);
283 rb_insert_color(&symn->rb_node, self);
284}
285
286static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
287{
288 struct rb_node *nd;
289
290 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
291 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
292 symbols__insert_by_name(self, pos);
293 }
294}
295
296static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
297{
298 struct rb_node *n;
299
300 if (self == NULL)
301 return NULL;
302
303 n = self->rb_node;
304
305 while (n) {
306 struct symbol_name_rb_node *s;
307 int cmp;
308
309 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
310 cmp = strcmp(name, s->sym.name);
311
312 if (cmp < 0)
313 n = n->rb_left;
314 else if (cmp > 0)
315 n = n->rb_right;
316 else
317 return &s->sym;
318 }
319
320 return NULL;
321}
322
323struct symbol *dso__find_symbol(struct dso *self,
324 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200325{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200326 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200327}
328
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200329struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
330 const char *name)
331{
332 return symbols__find_by_name(&self->symbol_names[type], name);
333}
334
335void dso__sort_by_name(struct dso *self, enum map_type type)
336{
337 dso__set_sorted_by_name(self, type);
338 return symbols__sort_by_name(&self->symbol_names[type],
339 &self->symbols[type]);
340}
341
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200342int build_id__sprintf(const u8 *self, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200343{
344 char *bid = bf;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200345 const u8 *raw = self;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200346 int i;
347
348 for (i = 0; i < len; ++i) {
349 sprintf(bid, "%02x", *raw);
350 ++raw;
351 bid += 2;
352 }
353
354 return raw - self;
355}
356
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200357size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300358{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200359 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200360
361 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200362 return fprintf(fp, "%s", sbuild_id);
363}
364
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200365size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200366{
367 struct rb_node *nd;
368 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
369
370 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200371 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200372 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
373 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
374 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300375 }
376
377 return ret;
378}
379
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200380int kallsyms__parse(const char *filename, void *arg,
381 int (*process_symbol)(void *arg, const char *name,
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200382 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300383{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300384 char *line = NULL;
385 size_t n;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200386 int err = 0;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200387 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300388
389 if (file == NULL)
390 goto out_failure;
391
392 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000393 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300394 int line_len, len;
395 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300396 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300397
398 line_len = getline(&line, &n, file);
399 if (line_len < 0)
400 break;
401
402 if (!line)
403 goto out_failure;
404
405 line[--line_len] = '\0'; /* \n */
406
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300407 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300408
409 len++;
410 if (len + 2 >= line_len)
411 continue;
412
413 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300414 symbol_name = line + len + 2;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300415
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200416 err = process_symbol(arg, symbol_name, symbol_type, start);
417 if (err)
418 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300419 }
420
421 free(line);
422 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200423 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300424
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300425out_failure:
426 return -1;
427}
428
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200429struct process_kallsyms_args {
430 struct map *map;
431 struct dso *dso;
432};
433
434static int map__process_kallsym_symbol(void *arg, const char *name,
435 char type, u64 start)
436{
437 struct symbol *sym;
438 struct process_kallsyms_args *a = arg;
439 struct rb_root *root = &a->dso->symbols[a->map->type];
440
441 if (!symbol_type__is_a(type, a->map->type))
442 return 0;
443
444 /*
445 * Will fix up the end later, when we have all symbols sorted.
446 */
447 sym = symbol__new(start, 0, name);
448
449 if (sym == NULL)
450 return -ENOMEM;
451 /*
452 * We will pass the symbols to the filter later, in
453 * map__split_kallsyms, when we have split the maps per module
454 */
455 symbols__insert(root, sym);
456 return 0;
457}
458
459/*
460 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
461 * so that we can in the next step set the symbol ->end address and then
462 * call kernel_maps__split_kallsyms.
463 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200464static int dso__load_all_kallsyms(struct dso *self, const char *filename,
465 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200466{
467 struct process_kallsyms_args args = { .map = map, .dso = self, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200468 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200469}
470
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300471/*
472 * Split the symbols into maps, making sure there are no overlaps, i.e. the
473 * kernel range is broken in several maps, named [kernel].N, as we don't have
474 * the original ELF section names vmlinux have.
475 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200476static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200477 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300478{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200479 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200480 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300481 struct symbol *pos;
482 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200483 struct rb_root *root = &self->symbols[map->type];
484 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300485 int kernel_range = 0;
486
487 while (next) {
488 char *module;
489
490 pos = rb_entry(next, struct symbol, rb_node);
491 next = rb_next(&pos->rb_node);
492
493 module = strchr(pos->name, '\t');
494 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200495 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200496 goto discard_symbol;
497
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300498 *module++ = '\0';
499
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200500 if (strcmp(curr_map->dso->short_name, module)) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200501 curr_map = map_groups__find_by_name(kmaps, map->type, module);
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200502 if (curr_map == NULL) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200503 pr_debug("/proc/{kallsyms,modules} "
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200504 "inconsistency while looking "
505 "for \"%s\" module!\n", module);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300506 return -1;
507 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200508
509 if (curr_map->dso->loaded)
510 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300511 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300512 /*
513 * So that we look just like we get from .ko files,
514 * i.e. not prelinked, relative to map->start.
515 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200516 pos->start = curr_map->map_ip(curr_map, pos->start);
517 pos->end = curr_map->map_ip(curr_map, pos->end);
518 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300519 char dso_name[PATH_MAX];
520 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300521
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300522 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
523 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300524
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200525 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300526 if (dso == NULL)
527 return -1;
528
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200529 curr_map = map__new2(pos->start, dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300530 if (map == NULL) {
531 dso__delete(dso);
532 return -1;
533 }
534
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200535 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200536 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300537 ++kernel_range;
538 }
539
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200540 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200541discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200542 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300543 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200544 if (curr_map != map) {
545 rb_erase(&pos->rb_node, root);
546 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300547 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200548 count++;
549 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300550 }
551
Mike Galbraith9974f492009-07-02 08:05:58 +0200552 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300553}
554
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200555int dso__load_kallsyms(struct dso *self, const char *filename,
556 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300557{
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200558 if (dso__load_all_kallsyms(self, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300559 return -1;
560
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200561 symbols__fixup_end(&self->symbols[map->type]);
562 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300563
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200564 return dso__split_kallsyms(self, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300565}
566
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300567static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200568 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300569{
570 char *line = NULL;
571 size_t n;
572 FILE *file;
573 int nr_syms = 0;
574
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300575 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300576 if (file == NULL)
577 goto out_failure;
578
579 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000580 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300581 struct symbol *sym;
582 int line_len, len;
583
584 line_len = getline(&line, &n, file);
585 if (line_len < 0)
586 break;
587
588 if (!line)
589 goto out_failure;
590
591 line[--line_len] = '\0'; /* \n */
592
593 len = hex2u64(line, &start);
594
595 len++;
596 if (len + 2 >= line_len)
597 continue;
598
599 len += hex2u64(line + len, &size);
600
601 len++;
602 if (len + 2 >= line_len)
603 continue;
604
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200605 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300606
607 if (sym == NULL)
608 goto out_delete_line;
609
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300610 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200611 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300612 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200613 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300614 nr_syms++;
615 }
616 }
617
618 free(line);
619 fclose(file);
620
621 return nr_syms;
622
623out_delete_line:
624 free(line);
625out_failure:
626 return -1;
627}
628
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300629/**
630 * elf_symtab__for_each_symbol - iterate thru all the symbols
631 *
632 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200633 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300634 * @sym: GElf_Sym iterator
635 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200636#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
637 for (idx = 0, gelf_getsym(syms, idx, &sym);\
638 idx < nr_syms; \
639 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300640
641static inline uint8_t elf_sym__type(const GElf_Sym *sym)
642{
643 return GELF_ST_TYPE(sym->st_info);
644}
645
646static inline int elf_sym__is_function(const GElf_Sym *sym)
647{
648 return elf_sym__type(sym) == STT_FUNC &&
649 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300650 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300651}
652
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200653static inline bool elf_sym__is_object(const GElf_Sym *sym)
654{
655 return elf_sym__type(sym) == STT_OBJECT &&
656 sym->st_name != 0 &&
657 sym->st_shndx != SHN_UNDEF;
658}
659
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200660static inline int elf_sym__is_label(const GElf_Sym *sym)
661{
662 return elf_sym__type(sym) == STT_NOTYPE &&
663 sym->st_name != 0 &&
664 sym->st_shndx != SHN_UNDEF &&
665 sym->st_shndx != SHN_ABS;
666}
667
668static inline const char *elf_sec__name(const GElf_Shdr *shdr,
669 const Elf_Data *secstrs)
670{
671 return secstrs->d_buf + shdr->sh_name;
672}
673
674static inline int elf_sec__is_text(const GElf_Shdr *shdr,
675 const Elf_Data *secstrs)
676{
677 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
678}
679
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200680static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
681 const Elf_Data *secstrs)
682{
683 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
684}
685
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300686static inline const char *elf_sym__name(const GElf_Sym *sym,
687 const Elf_Data *symstrs)
688{
689 return symstrs->d_buf + sym->st_name;
690}
691
692static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
693 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200694 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300695{
696 Elf_Scn *sec = NULL;
697 size_t cnt = 1;
698
699 while ((sec = elf_nextscn(elf, sec)) != NULL) {
700 char *str;
701
702 gelf_getshdr(sec, shp);
703 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
704 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200705 if (idx)
706 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300707 break;
708 }
709 ++cnt;
710 }
711
712 return sec;
713}
714
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300715#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
716 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
717 idx < nr_entries; \
718 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
719
720#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
721 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
722 idx < nr_entries; \
723 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
724
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300725/*
726 * We need to check if we have a .dynsym, so that we can handle the
727 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
728 * .dynsym or .symtab).
729 * And always look at the original dso, not at debuginfo packages, that
730 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
731 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200732static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
733 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300734{
735 uint32_t nr_rel_entries, idx;
736 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000737 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300738 GElf_Shdr shdr_plt;
739 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300740 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300741 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300742 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
743 size_t dynsym_idx;
744 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300745 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300746 Elf *elf;
747 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300748
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300749 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300750 if (fd < 0)
751 goto out;
752
Marti Raudsepp84087122009-10-24 19:10:36 +0300753 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300754 if (elf == NULL)
755 goto out_close;
756
757 if (gelf_getehdr(elf, &ehdr) == NULL)
758 goto out_elf_end;
759
760 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
761 ".dynsym", &dynsym_idx);
762 if (scn_dynsym == NULL)
763 goto out_elf_end;
764
765 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300766 ".rela.plt", NULL);
767 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300768 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300769 ".rel.plt", NULL);
770 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300771 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300772 }
773
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300774 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300775
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300776 if (shdr_rel_plt.sh_link != dynsym_idx)
777 goto out_elf_end;
778
779 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
780 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300781
782 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200783 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300784 * and the symbols in the .dynsym they refer to.
785 */
786 reldata = elf_getdata(scn_plt_rel, NULL);
787 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300788 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300789
790 syms = elf_getdata(scn_dynsym, NULL);
791 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300792 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300793
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300794 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300795 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300796 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300797
798 symstrs = elf_getdata(scn_symstrs, NULL);
799 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300800 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300801
802 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
803 plt_offset = shdr_plt.sh_offset;
804
805 if (shdr_rel_plt.sh_type == SHT_RELA) {
806 GElf_Rela pos_mem, *pos;
807
808 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
809 nr_rel_entries) {
810 symidx = GELF_R_SYM(pos->r_info);
811 plt_offset += shdr_plt.sh_entsize;
812 gelf_getsym(syms, symidx, &sym);
813 snprintf(sympltname, sizeof(sympltname),
814 "%s@plt", elf_sym__name(&sym, symstrs));
815
816 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200817 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300818 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300819 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300820
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200821 if (filter && filter(map, f))
822 symbol__delete(f);
823 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200824 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200825 ++nr;
826 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300827 }
828 } else if (shdr_rel_plt.sh_type == SHT_REL) {
829 GElf_Rel pos_mem, *pos;
830 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
831 nr_rel_entries) {
832 symidx = GELF_R_SYM(pos->r_info);
833 plt_offset += shdr_plt.sh_entsize;
834 gelf_getsym(syms, symidx, &sym);
835 snprintf(sympltname, sizeof(sympltname),
836 "%s@plt", elf_sym__name(&sym, symstrs));
837
838 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200839 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300840 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300841 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300842
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200843 if (filter && filter(map, f))
844 symbol__delete(f);
845 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200846 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200847 ++nr;
848 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300849 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300850 }
851
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300852 err = 0;
853out_elf_end:
854 elf_end(elf);
855out_close:
856 close(fd);
857
858 if (err == 0)
859 return nr;
860out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200861 pr_warning("%s: problems reading %s PLT info.\n",
862 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300863 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300864}
865
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200866static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
867{
868 switch (type) {
869 case MAP__FUNCTION:
870 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200871 case MAP__VARIABLE:
872 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200873 default:
874 return false;
875 }
876}
877
878static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
879{
880 switch (type) {
881 case MAP__FUNCTION:
882 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200883 case MAP__VARIABLE:
884 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200885 default:
886 return false;
887 }
888}
889
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200890static int dso__load_sym(struct dso *self, struct map *map, const char *name,
891 int fd, symbol_filter_t filter, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300892{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200893 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300894 struct map *curr_map = map;
895 struct dso *curr_dso = self;
896 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200897 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300898 uint32_t nr_syms;
899 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200900 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300901 GElf_Ehdr ehdr;
902 GElf_Shdr shdr;
903 Elf_Data *syms;
904 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300905 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300906 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300907 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300908
Marti Raudsepp84087122009-10-24 19:10:36 +0300909 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300910 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200911 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300912 goto out_close;
913 }
914
915 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200916 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300917 goto out_elf_end;
918 }
919
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300920 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
921 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300922 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
923 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300924 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300925 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300926
927 syms = elf_getdata(sec, NULL);
928 if (syms == NULL)
929 goto out_elf_end;
930
931 sec = elf_getscn(elf, shdr.sh_link);
932 if (sec == NULL)
933 goto out_elf_end;
934
935 symstrs = elf_getdata(sec, NULL);
936 if (symstrs == NULL)
937 goto out_elf_end;
938
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200939 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
940 if (sec_strndx == NULL)
941 goto out_elf_end;
942
943 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500944 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200945 goto out_elf_end;
946
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300947 nr_syms = shdr.sh_size / shdr.sh_entsize;
948
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200949 memset(&sym, 0, sizeof(sym));
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200950 if (!self->kernel) {
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200951 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300952 elf_section_by_name(elf, &ehdr, &shdr,
953 ".gnu.prelink_undo",
954 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200955 } else self->adjust_symbols = 0;
956
Ingo Molnar83a09442009-08-15 12:26:57 +0200957 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300958 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200959 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300960 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200961 int is_label = elf_sym__is_label(&sym);
962 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300963
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200964 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
965 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
966 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200967
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200968 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300969 continue;
970
971 sec = elf_getscn(elf, sym.st_shndx);
972 if (!sec)
973 goto out_elf_end;
974
975 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200976
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200977 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200978 continue;
979
980 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200981
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200982 if (self->kernel || kmodule) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300983 char dso_name[PATH_MAX];
984
985 if (strcmp(section_name,
986 curr_dso->short_name + dso_name_len) == 0)
987 goto new_symbol;
988
989 if (strcmp(section_name, ".text") == 0) {
990 curr_map = map;
991 curr_dso = self;
992 goto new_symbol;
993 }
994
995 snprintf(dso_name, sizeof(dso_name),
996 "%s%s", self->short_name, section_name);
997
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200998 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300999 if (curr_map == NULL) {
1000 u64 start = sym.st_value;
1001
1002 if (kmodule)
1003 start += map->start + shdr.sh_offset;
1004
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001005 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001006 if (curr_dso == NULL)
1007 goto out_elf_end;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001008 curr_map = map__new2(start, curr_dso,
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001009 map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001010 if (curr_map == NULL) {
1011 dso__delete(curr_dso);
1012 goto out_elf_end;
1013 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001014 curr_map->map_ip = identity__map_ip;
1015 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001016 curr_dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001017 map_groups__insert(kmap->kmaps, curr_map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001018 dsos__add(&dsos__kernel, curr_dso);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001019 dso__set_loaded(curr_dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001020 } else
1021 curr_dso = curr_map->dso;
1022
1023 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001024 }
1025
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001026 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001027 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1028 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1029 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001030 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001031 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001032 /*
1033 * We need to figure out if the object was created from C++ sources
1034 * DWARF DW_compile_unit has this, but we don't always have access
1035 * to it...
1036 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001037 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001038 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001039 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001040new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001041 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001042 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001043 if (!f)
1044 goto out_elf_end;
1045
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001046 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001047 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001048 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001049 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001050 nr++;
1051 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001052 }
1053
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001054 /*
1055 * For misannotated, zeroed, ASM function sizes.
1056 */
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001057 if (nr > 0) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001058 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001059 if (kmap) {
1060 /*
1061 * We need to fixup this here too because we create new
1062 * maps here, for things like vsyscall sections.
1063 */
1064 __map_groups__fixup_end(kmap->kmaps, map->type);
1065 }
1066 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001067 err = nr;
1068out_elf_end:
1069 elf_end(elf);
1070out_close:
1071 return err;
1072}
1073
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001074static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1075{
1076 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1077}
1078
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001079static bool __dsos__read_build_ids(struct list_head *head)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001080{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001081 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001082 struct dso *pos;
1083
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001084 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001085 if (filename__read_build_id(pos->long_name, pos->build_id,
1086 sizeof(pos->build_id)) > 0) {
1087 have_build_id = true;
1088 pos->has_build_id = true;
1089 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001090
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001091 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001092}
1093
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001094bool dsos__read_build_ids(void)
1095{
Arnaldo Carvalho de Melo8b4825b2009-12-09 20:09:37 -02001096 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1097 ubuildids = __dsos__read_build_ids(&dsos__user);
1098 return kbuildids || ubuildids;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001099}
1100
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001101/*
1102 * Align offset to 4 bytes as needed for note name and descriptor data.
1103 */
1104#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1105
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001106int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001107{
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001108 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001109 GElf_Ehdr ehdr;
1110 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001111 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001112 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001113 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001114 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001115 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001116
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001117 if (size < BUILD_ID_SIZE)
1118 goto out;
1119
1120 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001121 if (fd < 0)
1122 goto out;
1123
Marti Raudsepp84087122009-10-24 19:10:36 +03001124 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001125 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001126 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001127 goto out_close;
1128 }
1129
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001130 ek = elf_kind(elf);
1131 if (ek != ELF_K_ELF)
1132 goto out_elf_end;
1133
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001134 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001135 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001136 goto out_elf_end;
1137 }
1138
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001139 sec = elf_section_by_name(elf, &ehdr, &shdr,
1140 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001141 if (sec == NULL) {
1142 sec = elf_section_by_name(elf, &ehdr, &shdr,
1143 ".notes", NULL);
1144 if (sec == NULL)
1145 goto out_elf_end;
1146 }
1147
1148 data = elf_getdata(sec, NULL);
1149 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001150 goto out_elf_end;
1151
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001152 ptr = data->d_buf;
1153 while (ptr < (data->d_buf + data->d_size)) {
1154 GElf_Nhdr *nhdr = ptr;
1155 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1156 descsz = NOTE_ALIGN(nhdr->n_descsz);
1157 const char *name;
1158
1159 ptr += sizeof(*nhdr);
1160 name = ptr;
1161 ptr += namesz;
1162 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1163 nhdr->n_namesz == sizeof("GNU")) {
1164 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1165 memcpy(bf, ptr, BUILD_ID_SIZE);
1166 err = BUILD_ID_SIZE;
1167 break;
1168 }
1169 }
1170 ptr += descsz;
1171 }
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001172out_elf_end:
1173 elf_end(elf);
1174out_close:
1175 close(fd);
1176out:
1177 return err;
1178}
1179
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001180int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1181{
1182 int fd, err = -1;
1183
1184 if (size < BUILD_ID_SIZE)
1185 goto out;
1186
1187 fd = open(filename, O_RDONLY);
1188 if (fd < 0)
1189 goto out;
1190
1191 while (1) {
1192 char bf[BUFSIZ];
1193 GElf_Nhdr nhdr;
1194 int namesz, descsz;
1195
1196 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1197 break;
1198
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001199 namesz = NOTE_ALIGN(nhdr.n_namesz);
1200 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001201 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1202 nhdr.n_namesz == sizeof("GNU")) {
1203 if (read(fd, bf, namesz) != namesz)
1204 break;
1205 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1206 if (read(fd, build_id,
1207 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1208 err = 0;
1209 break;
1210 }
1211 } else if (read(fd, bf, descsz) != descsz)
1212 break;
1213 } else {
1214 int n = namesz + descsz;
1215 if (read(fd, bf, n) != n)
1216 break;
1217 }
1218 }
1219 close(fd);
1220out:
1221 return err;
1222}
1223
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001224char dso__symtab_origin(const struct dso *self)
1225{
1226 static const char origin[] = {
1227 [DSO__ORIG_KERNEL] = 'k',
1228 [DSO__ORIG_JAVA_JIT] = 'j',
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001229 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001230 [DSO__ORIG_FEDORA] = 'f',
1231 [DSO__ORIG_UBUNTU] = 'u',
1232 [DSO__ORIG_BUILDID] = 'b',
1233 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001234 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001235 };
1236
1237 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1238 return '!';
1239 return origin[self->origin];
1240}
1241
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001242int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001243{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001244 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001245 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001246 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001247 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001248 int ret = -1;
1249 int fd;
1250
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001251 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001252
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001253 if (self->kernel)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001254 return dso__load_kernel_sym(self, map, filter);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001255
1256 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001257 if (!name)
1258 return -1;
1259
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001260 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001261
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001262 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001263 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001264 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1265 DSO__ORIG_NOT_FOUND;
1266 return ret;
1267 }
1268
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001269 self->origin = DSO__ORIG_BUILD_ID_CACHE;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001270
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001271 if (self->has_build_id) {
1272 build_id__sprintf(self->build_id, sizeof(self->build_id),
1273 build_id_hex);
1274 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1275 getenv("HOME"), DEBUG_CACHE_DIR,
1276 build_id_hex, build_id_hex + 2);
1277 goto open_file;
1278 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001279more:
1280 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001281 self->origin++;
1282 switch (self->origin) {
1283 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001284 snprintf(name, size, "/usr/lib/debug%s.debug",
1285 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001286 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001287 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001288 snprintf(name, size, "/usr/lib/debug%s",
1289 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001290 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001291 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001292 if (filename__read_build_id(self->long_name, build_id,
1293 sizeof(build_id))) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001294 build_id__sprintf(build_id, sizeof(build_id),
1295 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001296 snprintf(name, size,
1297 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001298 build_id_hex, build_id_hex + 2);
1299 if (self->has_build_id)
1300 goto compare_build_id;
1301 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001302 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001303 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001304 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001305 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001306 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001307 break;
1308
1309 default:
1310 goto out;
1311 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001312
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001313 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001314 if (filename__read_build_id(name, build_id,
1315 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001316 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001317compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001318 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001319 goto more;
1320 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001321open_file:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001322 fd = open(name, O_RDONLY);
1323 } while (fd < 0);
1324
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001325 ret = dso__load_sym(self, map, name, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001326 close(fd);
1327
1328 /*
1329 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1330 */
1331 if (!ret)
1332 goto more;
1333
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001334 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001335 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001336 if (nr_plt > 0)
1337 ret += nr_plt;
1338 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001339out:
1340 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001341 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1342 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001343 return ret;
1344}
1345
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001346struct map *map_groups__find_by_name(struct map_groups *self,
1347 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001348{
1349 struct rb_node *nd;
1350
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001351 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001352 struct map *map = rb_entry(nd, struct map, rb_node);
1353
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001354 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001355 return map;
1356 }
1357
1358 return NULL;
1359}
1360
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001361static int dso__kernel_module_get_build_id(struct dso *self)
1362{
1363 char filename[PATH_MAX];
1364 /*
1365 * kernel module short names are of the form "[module]" and
1366 * we need just "module" here.
1367 */
1368 const char *name = self->short_name + 1;
1369
1370 snprintf(filename, sizeof(filename),
1371 "/sys/module/%.*s/notes/.note.gnu.build-id",
1372 (int)strlen(name - 1), name);
1373
1374 if (sysfs__read_build_id(filename, self->build_id,
1375 sizeof(self->build_id)) == 0)
1376 self->has_build_id = true;
1377
1378 return 0;
1379}
1380
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001381static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001382{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001383 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001384 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001385
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001386 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001387 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001388 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001389 }
1390
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001391 while ((dent = readdir(dir)) != NULL) {
1392 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001393
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001394 if (dent->d_type == DT_DIR) {
1395 if (!strcmp(dent->d_name, ".") ||
1396 !strcmp(dent->d_name, ".."))
1397 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001398
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001399 snprintf(path, sizeof(path), "%s/%s",
1400 dirname, dent->d_name);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001401 if (map_groups__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001402 goto failure;
1403 } else {
1404 char *dot = strrchr(dent->d_name, '.'),
1405 dso_name[PATH_MAX];
1406 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001407 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001408
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001409 if (dot == NULL || strcmp(dot, ".ko"))
1410 continue;
1411 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1412 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001413
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001414 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001415 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001416 if (map == NULL)
1417 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001418
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001419 snprintf(path, sizeof(path), "%s/%s",
1420 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001421
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001422 long_name = strdup(path);
1423 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001424 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001425 dso__set_long_name(map->dso, long_name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001426 dso__kernel_module_get_build_id(map->dso);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001427 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001428 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001429
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001430 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001431failure:
1432 closedir(dir);
1433 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001434}
1435
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001436static int map_groups__set_modules_path(struct map_groups *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001437{
1438 struct utsname uts;
1439 char modules_path[PATH_MAX];
1440
1441 if (uname(&uts) < 0)
1442 return -1;
1443
1444 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1445 uts.release);
1446
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001447 return map_groups__set_modules_path_dir(self, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001448}
1449
1450/*
1451 * Constructor variant for modules (where we know from /proc/modules where
1452 * they are loaded) and for vmlinux, where only after we load all the
1453 * symbols we'll know where it starts and ends.
1454 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001455static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001456{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001457 struct map *self = zalloc(sizeof(*self) +
1458 (dso->kernel ? sizeof(struct kmap) : 0));
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001459 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001460 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001461 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001462 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001463 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001464 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001465
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001466 return self;
1467}
1468
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001469struct map *map_groups__new_module(struct map_groups *self, u64 start,
1470 const char *filename)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001471{
1472 struct map *map;
1473 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1474
1475 if (dso == NULL)
1476 return NULL;
1477
1478 map = map__new2(start, dso, MAP__FUNCTION);
1479 if (map == NULL)
1480 return NULL;
1481
1482 dso->origin = DSO__ORIG_KMODULE;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001483 map_groups__insert(self, map);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001484 return map;
1485}
1486
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001487static int map_groups__create_modules(struct map_groups *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001488{
1489 char *line = NULL;
1490 size_t n;
1491 FILE *file = fopen("/proc/modules", "r");
1492 struct map *map;
1493
1494 if (file == NULL)
1495 return -1;
1496
1497 while (!feof(file)) {
1498 char name[PATH_MAX];
1499 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001500 char *sep;
1501 int line_len;
1502
1503 line_len = getline(&line, &n, file);
1504 if (line_len < 0)
1505 break;
1506
1507 if (!line)
1508 goto out_failure;
1509
1510 line[--line_len] = '\0'; /* \n */
1511
1512 sep = strrchr(line, 'x');
1513 if (sep == NULL)
1514 continue;
1515
1516 hex2u64(sep + 1, &start);
1517
1518 sep = strchr(line, ' ');
1519 if (sep == NULL)
1520 continue;
1521
1522 *sep = '\0';
1523
1524 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001525 map = map_groups__new_module(self, start, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001526 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001527 goto out_delete_line;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001528 dso__kernel_module_get_build_id(map->dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001529 }
1530
1531 free(line);
1532 fclose(file);
1533
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001534 return map_groups__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001535
1536out_delete_line:
1537 free(line);
1538out_failure:
1539 return -1;
1540}
1541
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001542static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001543 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001544{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001545 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001546
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001547 if (self->has_build_id) {
1548 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001549
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001550 if (filename__read_build_id(vmlinux, build_id,
1551 sizeof(build_id)) < 0) {
1552 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1553 return -1;
1554 }
1555 if (!dso__build_id_equal(self, build_id)) {
1556 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1557 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1558
1559 build_id__sprintf(self->build_id,
1560 sizeof(self->build_id),
1561 expected_build_id);
1562 build_id__sprintf(build_id, sizeof(build_id),
1563 vmlinux_build_id);
1564 pr_debug("build_id in %s is %s while expected is %s, "
1565 "ignoring it\n", vmlinux, vmlinux_build_id,
1566 expected_build_id);
1567 return -1;
1568 }
1569 }
1570
1571 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001572 if (fd < 0)
1573 return -1;
1574
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001575 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001576 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001577 close(fd);
1578
1579 return err;
1580}
1581
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001582int dso__load_vmlinux_path(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001583 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001584{
1585 int i, err = 0;
1586
1587 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1588 vmlinux_path__nr_entries);
1589
1590 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001591 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001592 if (err > 0) {
1593 pr_debug("Using %s for symbols\n", vmlinux_path[i]);
1594 dso__set_long_name(self, strdup(vmlinux_path[i]));
1595 break;
1596 }
1597 }
1598
1599 return err;
1600}
1601
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001602static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001603 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001604{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001605 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001606 const char *kallsyms_filename = NULL;
1607 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001608 /*
1609 * Step 1: if the user specified a vmlinux filename, use it and only
1610 * it, reporting errors to the user if it cannot be used.
1611 *
1612 * For instance, try to analyse an ARM perf.data file _without_ a
1613 * build-id, or if the user specifies the wrong path to the right
1614 * vmlinux file, obviously we can't fallback to another vmlinux (a
1615 * x86_86 one, on the machine where analysis is being performed, say),
1616 * or worse, /proc/kallsyms.
1617 *
1618 * If the specified file _has_ a build-id and there is a build-id
1619 * section in the perf.data file, we will still do the expected
1620 * validation in dso__load_vmlinux and will bail out if they don't
1621 * match.
1622 */
1623 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001624 err = dso__load_vmlinux(self, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001625 symbol_conf.vmlinux_name, filter);
1626 goto out_try_fixup;
1627 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001628
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001629 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001630 err = dso__load_vmlinux_path(self, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001631 if (err > 0)
1632 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001633 }
1634
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001635 /*
1636 * Say the kernel DSO was created when processing the build-id header table,
1637 * we have a build-id, so check if it is the same as the running kernel,
1638 * using it if it is.
1639 */
1640 if (self->has_build_id) {
1641 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001642 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001643
1644 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001645 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001646 if (dso__build_id_equal(self, kallsyms_build_id)) {
1647 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001648 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001649 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001650 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001651 /*
1652 * Now look if we have it on the build-id cache in
1653 * $HOME/.debug/[kernel.kallsyms].
1654 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001655 build_id__sprintf(self->build_id, sizeof(self->build_id),
1656 sbuild_id);
1657
1658 if (asprintf(&kallsyms_allocated_filename,
1659 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001660 getenv("HOME"), sbuild_id) == -1)
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001661 return -1;
1662
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02001663 kallsyms_filename = kallsyms_allocated_filename;
1664
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001665 if (access(kallsyms_filename, F_OK)) {
1666 free(kallsyms_allocated_filename);
1667 return -1;
1668 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001669 } else {
1670 /*
1671 * Last resort, if we don't have a build-id and couldn't find
1672 * any vmlinux file, try the running kernel kallsyms table.
1673 */
1674 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001675 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001676
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001677do_kallsyms:
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001678 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001679 free(kallsyms_allocated_filename);
1680
1681out_try_fixup:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001682 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001683out_fixup:
Arnaldo Carvalho de Meloe1c7c6a2010-01-22 14:35:01 -02001684 if (kallsyms_filename != NULL)
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001685 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001686 map__fixup_start(map);
1687 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001688 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001689
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001690 return err;
1691}
1692
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001693LIST_HEAD(dsos__user);
1694LIST_HEAD(dsos__kernel);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001695
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001696static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001697{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001698 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001699}
1700
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001701static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001702{
1703 struct dso *pos;
1704
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001705 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02001706 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001707 return pos;
1708 return NULL;
1709}
1710
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001711struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001712{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001713 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001714
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001715 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001716 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001717 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001718 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001719 dso__set_basename(dso);
1720 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001721 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001722
1723 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001724}
1725
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001726static void __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001727{
1728 struct dso *pos;
1729
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001730 list_for_each_entry(pos, head, node) {
1731 int i;
1732 for (i = 0; i < MAP__NR_TYPES; ++i)
1733 dso__fprintf(pos, i, fp);
1734 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001735}
1736
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001737void dsos__fprintf(FILE *fp)
1738{
1739 __dsos__fprintf(&dsos__kernel, fp);
1740 __dsos__fprintf(&dsos__user, fp);
1741}
1742
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001743static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1744 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001745{
1746 struct dso *pos;
1747 size_t ret = 0;
1748
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001749 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001750 if (with_hits && !pos->hit)
1751 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001752 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001753 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001754 }
1755 return ret;
1756}
1757
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001758size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001759{
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001760 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1761 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001762}
1763
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001764struct dso *dso__new_kernel(const char *name)
1765{
1766 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1767
1768 if (self != NULL) {
1769 self->short_name = "[kernel]";
1770 self->kernel = 1;
1771 }
1772
1773 return self;
1774}
1775
1776void dso__read_running_kernel_build_id(struct dso *self)
1777{
1778 if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
1779 sizeof(self->build_id)) == 0)
1780 self->has_build_id = true;
1781}
1782
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001783static struct dso *dsos__create_kernel(const char *vmlinux)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001784{
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001785 struct dso *kernel = dso__new_kernel(vmlinux);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001786
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02001787 if (kernel != NULL) {
1788 dso__read_running_kernel_build_id(kernel);
1789 dsos__add(&dsos__kernel, kernel);
1790 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001791
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001792 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001793}
1794
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001795int __map_groups__create_kernel_maps(struct map_groups *self,
1796 struct map *vmlinux_maps[MAP__NR_TYPES],
1797 struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001798{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001799 enum map_type type;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001800
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001801 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001802 struct kmap *kmap;
1803
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001804 vmlinux_maps[type] = map__new2(0, kernel, type);
1805 if (vmlinux_maps[type] == NULL)
1806 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001807
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001808 vmlinux_maps[type]->map_ip =
1809 vmlinux_maps[type]->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001810
1811 kmap = map__kmap(vmlinux_maps[type]);
1812 kmap->kmaps = self;
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001813 map_groups__insert(self, vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001814 }
1815
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001816 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001817}
1818
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001819static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001820{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001821 while (--vmlinux_path__nr_entries >= 0) {
1822 free(vmlinux_path[vmlinux_path__nr_entries]);
1823 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1824 }
1825
1826 free(vmlinux_path);
1827 vmlinux_path = NULL;
1828}
1829
1830static int vmlinux_path__init(void)
1831{
1832 struct utsname uts;
1833 char bf[PATH_MAX];
1834
1835 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001836 return -1;
1837
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001838 vmlinux_path = malloc(sizeof(char *) * 5);
1839 if (vmlinux_path == NULL)
1840 return -1;
1841
1842 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1843 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1844 goto out_fail;
1845 ++vmlinux_path__nr_entries;
1846 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1847 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1848 goto out_fail;
1849 ++vmlinux_path__nr_entries;
1850 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1851 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1852 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1853 goto out_fail;
1854 ++vmlinux_path__nr_entries;
1855 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1856 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1857 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1858 goto out_fail;
1859 ++vmlinux_path__nr_entries;
1860 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1861 uts.release);
1862 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1863 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1864 goto out_fail;
1865 ++vmlinux_path__nr_entries;
1866
1867 return 0;
1868
1869out_fail:
1870 vmlinux_path__exit();
1871 return -1;
1872}
1873
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001874static int setup_list(struct strlist **list, const char *list_str,
1875 const char *list_name)
1876{
1877 if (list_str == NULL)
1878 return 0;
1879
1880 *list = strlist__new(true, list_str);
1881 if (!*list) {
1882 pr_err("problems parsing %s list\n", list_name);
1883 return -1;
1884 }
1885 return 0;
1886}
1887
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001888int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001889{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001890 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001891 if (symbol_conf.sort_by_name)
1892 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1893 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001894
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001895 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001896 return -1;
1897
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001898 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1899 pr_err("'.' is the only non valid --field-separator argument\n");
1900 return -1;
1901 }
1902
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001903 if (setup_list(&symbol_conf.dso_list,
1904 symbol_conf.dso_list_str, "dso") < 0)
1905 return -1;
1906
1907 if (setup_list(&symbol_conf.comm_list,
1908 symbol_conf.comm_list_str, "comm") < 0)
1909 goto out_free_dso_list;
1910
1911 if (setup_list(&symbol_conf.sym_list,
1912 symbol_conf.sym_list_str, "symbol") < 0)
1913 goto out_free_comm_list;
1914
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001915 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001916
1917out_free_dso_list:
1918 strlist__delete(symbol_conf.dso_list);
1919out_free_comm_list:
1920 strlist__delete(symbol_conf.comm_list);
1921 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001922}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001923
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001924int map_groups__create_kernel_maps(struct map_groups *self,
1925 struct map *vmlinux_maps[MAP__NR_TYPES])
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001926{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001927 struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
1928
1929 if (kernel == NULL)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001930 return -1;
1931
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001932 if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
1933 return -1;
1934
1935 if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
1936 return -1;
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001937 /*
1938 * Now that we have all the maps created, just set the ->end of them:
1939 */
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001940 map_groups__fixup_end(self);
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001941 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001942}