blob: 44d81d5ae8cf7c761ce7697c512be04af56dc78d [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03003#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03004#include "symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03005#include "thread.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03006
Frederic Weisbecker8f288272009-08-16 22:05:48 +02007#include "debug.h"
8
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03009#include <libelf.h>
10#include <gelf.h>
11#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020012#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030013#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020014
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020015#ifndef NT_GNU_BUILD_ID
16#define NT_GNU_BUILD_ID 3
17#endif
18
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030019enum dso_origin {
20 DSO__ORIG_KERNEL = 0,
21 DSO__ORIG_JAVA_JIT,
22 DSO__ORIG_FEDORA,
23 DSO__ORIG_UBUNTU,
24 DSO__ORIG_BUILDID,
25 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030026 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030027 DSO__ORIG_NOT_FOUND,
28};
29
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030030static void dsos__add(struct dso *dso);
31static struct dso *dsos__find(const char *name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030032static struct map *map__new2(u64 start, struct dso *dso);
33static void kernel_maps__insert(struct map *map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020034static int dso__load_kernel_sym(struct dso *self, struct map *map,
35 symbol_filter_t filter);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020036unsigned int symbol__priv_size;
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 Meloaf427bf2009-10-05 14:26:17 -030040static struct rb_root kernel_maps;
41
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030042static void dso__fixup_sym_end(struct dso *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030043{
44 struct rb_node *nd, *prevnd = rb_first(&self->syms);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030045 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030046
47 if (prevnd == NULL)
48 return;
49
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030050 curr = rb_entry(prevnd, struct symbol, rb_node);
51
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030052 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030053 prev = curr;
54 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030055
56 if (prev->end == prev->start)
57 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030058 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030059
60 /* Last entry */
61 if (curr->end == curr->start)
62 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030063}
64
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030065static void kernel_maps__fixup_end(void)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030066{
67 struct map *prev, *curr;
68 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
69
70 if (prevnd == NULL)
71 return;
72
73 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030074
75 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
76 prev = curr;
77 curr = rb_entry(nd, struct map, rb_node);
78 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030079 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -020080
81 /*
82 * We still haven't the actual symbols, so guess the
83 * last map final address.
84 */
85 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030086}
87
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020088static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030089{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030090 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020091 struct symbol *self = calloc(1, (symbol__priv_size +
92 sizeof(*self) + namelen));
Ingo Molnar0b73da32009-06-06 15:48:52 +020093 if (!self)
94 return NULL;
95
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020096 if (symbol__priv_size) {
97 memset(self, 0, symbol__priv_size);
98 self = ((void *)self) + symbol__priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030099 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200100 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200101 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200102
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200103 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200104
Ingo Molnar0b73da32009-06-06 15:48:52 +0200105 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300106
107 return self;
108}
109
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200110static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300111{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200112 free(((void *)self) - symbol__priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300113}
114
115static size_t symbol__fprintf(struct symbol *self, FILE *fp)
116{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300117 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300118 self->start, self->end, self->name);
119}
120
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200121static void dso__set_long_name(struct dso *self, char *name)
122{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200123 if (name == NULL)
124 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200125 self->long_name = name;
126 self->long_name_len = strlen(name);
127}
128
129static void dso__set_basename(struct dso *self)
130{
131 self->short_name = basename(self->long_name);
132}
133
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200134struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300135{
136 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
137
138 if (self != NULL) {
139 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200140 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300141 self->short_name = self->name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300142 self->syms = RB_ROOT;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200143 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300144 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300145 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200146 self->loaded = 0;
147 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300148 }
149
150 return self;
151}
152
153static void dso__delete_symbols(struct dso *self)
154{
155 struct symbol *pos;
156 struct rb_node *next = rb_first(&self->syms);
157
158 while (next) {
159 pos = rb_entry(next, struct symbol, rb_node);
160 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -0300161 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200162 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300163 }
164}
165
166void dso__delete(struct dso *self)
167{
168 dso__delete_symbols(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300169 if (self->long_name != self->name)
170 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300171 free(self);
172}
173
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200174void dso__set_build_id(struct dso *self, void *build_id)
175{
176 memcpy(self->build_id, build_id, sizeof(self->build_id));
177 self->has_build_id = 1;
178}
179
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300180static void dso__insert_symbol(struct dso *self, struct symbol *sym)
181{
182 struct rb_node **p = &self->syms.rb_node;
183 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000184 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300185 struct symbol *s;
186
187 while (*p != NULL) {
188 parent = *p;
189 s = rb_entry(parent, struct symbol, rb_node);
190 if (ip < s->start)
191 p = &(*p)->rb_left;
192 else
193 p = &(*p)->rb_right;
194 }
195 rb_link_node(&sym->rb_node, parent, p);
196 rb_insert_color(&sym->rb_node, &self->syms);
197}
198
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000199struct symbol *dso__find_symbol(struct dso *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300200{
201 struct rb_node *n;
202
203 if (self == NULL)
204 return NULL;
205
206 n = self->syms.rb_node;
207
208 while (n) {
209 struct symbol *s = rb_entry(n, struct symbol, rb_node);
210
211 if (ip < s->start)
212 n = n->rb_left;
213 else if (ip > s->end)
214 n = n->rb_right;
215 else
216 return s;
217 }
218
219 return NULL;
220}
221
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200222int build_id__sprintf(u8 *self, int len, char *bf)
223{
224 char *bid = bf;
225 u8 *raw = self;
226 int i;
227
228 for (i = 0; i < len; ++i) {
229 sprintf(bid, "%02x", *raw);
230 ++raw;
231 bid += 2;
232 }
233
234 return raw - self;
235}
236
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200237size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300238{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200239 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200240
241 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200242 return fprintf(fp, "%s", sbuild_id);
243}
244
245size_t dso__fprintf(struct dso *self, FILE *fp)
246{
247 struct rb_node *nd;
248 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
249
250 ret += dso__fprintf_buildid(self, fp);
251 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200252
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300253 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
254 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
255 ret += symbol__fprintf(pos, fp);
256 }
257
258 return ret;
259}
260
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300261/*
262 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
263 * so that we can in the next step set the symbol ->end address and then
264 * call kernel_maps__split_kallsyms.
265 */
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200266static int kernel_maps__load_all_kallsyms(void)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300267{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300268 char *line = NULL;
269 size_t n;
270 FILE *file = fopen("/proc/kallsyms", "r");
271
272 if (file == NULL)
273 goto out_failure;
274
275 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000276 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300277 struct symbol *sym;
278 int line_len, len;
279 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300280 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300281
282 line_len = getline(&line, &n, file);
283 if (line_len < 0)
284 break;
285
286 if (!line)
287 goto out_failure;
288
289 line[--line_len] = '\0'; /* \n */
290
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300291 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300292
293 len++;
294 if (len + 2 >= line_len)
295 continue;
296
297 symbol_type = toupper(line[len]);
298 /*
299 * We're interested only in code ('T'ext)
300 */
301 if (symbol_type != 'T' && symbol_type != 'W')
302 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300303
304 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300305 /*
306 * Will fix up the end later, when we have all symbols sorted.
307 */
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200308 sym = symbol__new(start, 0, symbol_name);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300309
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300310 if (sym == NULL)
311 goto out_delete_line;
312
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200313 /*
314 * We will pass the symbols to the filter later, in
315 * kernel_maps__split_kallsyms, when we have split the
316 * maps per module
317 */
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300318 dso__insert_symbol(kernel_map->dso, sym);
319 }
320
321 free(line);
322 fclose(file);
323
324 return 0;
325
326out_delete_line:
327 free(line);
328out_failure:
329 return -1;
330}
331
332/*
333 * Split the symbols into maps, making sure there are no overlaps, i.e. the
334 * kernel range is broken in several maps, named [kernel].N, as we don't have
335 * the original ELF section names vmlinux have.
336 */
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200337static int kernel_maps__split_kallsyms(symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300338{
339 struct map *map = kernel_map;
340 struct symbol *pos;
341 int count = 0;
342 struct rb_node *next = rb_first(&kernel_map->dso->syms);
343 int kernel_range = 0;
344
345 while (next) {
346 char *module;
347
348 pos = rb_entry(next, struct symbol, rb_node);
349 next = rb_next(&pos->rb_node);
350
351 module = strchr(pos->name, '\t');
352 if (module) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300353 *module++ = '\0';
354
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300355 if (strcmp(map->dso->name, module)) {
356 map = kernel_maps__find_by_dso_name(module);
357 if (!map) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200358 pr_err("/proc/{kallsyms,modules} "
359 "inconsistency!\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300360 return -1;
361 }
362 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300363 /*
364 * So that we look just like we get from .ko files,
365 * i.e. not prelinked, relative to map->start.
366 */
367 pos->start = map->map_ip(map, pos->start);
368 pos->end = map->map_ip(map, pos->end);
369 } else if (map != kernel_map) {
370 char dso_name[PATH_MAX];
371 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300372
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300373 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
374 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300375
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200376 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300377 if (dso == NULL)
378 return -1;
379
380 map = map__new2(pos->start, dso);
381 if (map == NULL) {
382 dso__delete(dso);
383 return -1;
384 }
385
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200386 map->map_ip = map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300387 kernel_maps__insert(map);
388 ++kernel_range;
389 }
390
391 if (filter && filter(map, pos)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300392 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200393 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300394 } else {
395 if (map != kernel_map) {
396 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
397 dso__insert_symbol(map->dso, pos);
398 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200399 count++;
400 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300401 }
402
Mike Galbraith9974f492009-07-02 08:05:58 +0200403 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300404}
405
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300406
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200407static int kernel_maps__load_kallsyms(symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300408{
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200409 if (kernel_maps__load_all_kallsyms())
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300410 return -1;
411
412 dso__fixup_sym_end(kernel_map->dso);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200413 kernel_map->dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300414
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200415 return kernel_maps__split_kallsyms(filter);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300416}
417
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200418size_t kernel_maps__fprintf(FILE *fp)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300419{
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200420 size_t printed = fprintf(fp, "Kernel maps:\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300421 struct rb_node *nd;
422
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300423 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
424 struct map *pos = rb_entry(nd, struct map, rb_node);
425
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300426 printed += fprintf(fp, "Map:");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300427 printed += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200428 if (verbose > 1) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300429 printed += dso__fprintf(pos->dso, fp);
430 printed += fprintf(fp, "--\n");
431 }
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300432 }
433
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200434 return printed + fprintf(fp, "END kernel maps\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300435}
436
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300437static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200438 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300439{
440 char *line = NULL;
441 size_t n;
442 FILE *file;
443 int nr_syms = 0;
444
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300445 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300446 if (file == NULL)
447 goto out_failure;
448
449 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000450 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300451 struct symbol *sym;
452 int line_len, len;
453
454 line_len = getline(&line, &n, file);
455 if (line_len < 0)
456 break;
457
458 if (!line)
459 goto out_failure;
460
461 line[--line_len] = '\0'; /* \n */
462
463 len = hex2u64(line, &start);
464
465 len++;
466 if (len + 2 >= line_len)
467 continue;
468
469 len += hex2u64(line + len, &size);
470
471 len++;
472 if (len + 2 >= line_len)
473 continue;
474
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200475 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300476
477 if (sym == NULL)
478 goto out_delete_line;
479
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300480 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200481 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300482 else {
483 dso__insert_symbol(self, sym);
484 nr_syms++;
485 }
486 }
487
488 free(line);
489 fclose(file);
490
491 return nr_syms;
492
493out_delete_line:
494 free(line);
495out_failure:
496 return -1;
497}
498
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300499/**
500 * elf_symtab__for_each_symbol - iterate thru all the symbols
501 *
502 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200503 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300504 * @sym: GElf_Sym iterator
505 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200506#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
507 for (idx = 0, gelf_getsym(syms, idx, &sym);\
508 idx < nr_syms; \
509 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300510
511static inline uint8_t elf_sym__type(const GElf_Sym *sym)
512{
513 return GELF_ST_TYPE(sym->st_info);
514}
515
516static inline int elf_sym__is_function(const GElf_Sym *sym)
517{
518 return elf_sym__type(sym) == STT_FUNC &&
519 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300520 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300521}
522
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200523static inline int elf_sym__is_label(const GElf_Sym *sym)
524{
525 return elf_sym__type(sym) == STT_NOTYPE &&
526 sym->st_name != 0 &&
527 sym->st_shndx != SHN_UNDEF &&
528 sym->st_shndx != SHN_ABS;
529}
530
531static inline const char *elf_sec__name(const GElf_Shdr *shdr,
532 const Elf_Data *secstrs)
533{
534 return secstrs->d_buf + shdr->sh_name;
535}
536
537static inline int elf_sec__is_text(const GElf_Shdr *shdr,
538 const Elf_Data *secstrs)
539{
540 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
541}
542
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300543static inline const char *elf_sym__name(const GElf_Sym *sym,
544 const Elf_Data *symstrs)
545{
546 return symstrs->d_buf + sym->st_name;
547}
548
549static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
550 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200551 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300552{
553 Elf_Scn *sec = NULL;
554 size_t cnt = 1;
555
556 while ((sec = elf_nextscn(elf, sec)) != NULL) {
557 char *str;
558
559 gelf_getshdr(sec, shp);
560 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
561 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200562 if (idx)
563 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300564 break;
565 }
566 ++cnt;
567 }
568
569 return sec;
570}
571
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300572#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
573 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
574 idx < nr_entries; \
575 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
576
577#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
578 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
579 idx < nr_entries; \
580 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
581
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300582/*
583 * We need to check if we have a .dynsym, so that we can handle the
584 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
585 * .dynsym or .symtab).
586 * And always look at the original dso, not at debuginfo packages, that
587 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
588 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200589static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
590 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300591{
592 uint32_t nr_rel_entries, idx;
593 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000594 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300595 GElf_Shdr shdr_plt;
596 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300597 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300598 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300599 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
600 size_t dynsym_idx;
601 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300602 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300603 Elf *elf;
604 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300605
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300606 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300607 if (fd < 0)
608 goto out;
609
Marti Raudsepp84087122009-10-24 19:10:36 +0300610 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300611 if (elf == NULL)
612 goto out_close;
613
614 if (gelf_getehdr(elf, &ehdr) == NULL)
615 goto out_elf_end;
616
617 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
618 ".dynsym", &dynsym_idx);
619 if (scn_dynsym == NULL)
620 goto out_elf_end;
621
622 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300623 ".rela.plt", NULL);
624 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300625 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300626 ".rel.plt", NULL);
627 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300628 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300629 }
630
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300631 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300632
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300633 if (shdr_rel_plt.sh_link != dynsym_idx)
634 goto out_elf_end;
635
636 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
637 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300638
639 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200640 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300641 * and the symbols in the .dynsym they refer to.
642 */
643 reldata = elf_getdata(scn_plt_rel, NULL);
644 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300645 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300646
647 syms = elf_getdata(scn_dynsym, NULL);
648 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300649 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300650
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300651 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300652 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300653 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300654
655 symstrs = elf_getdata(scn_symstrs, NULL);
656 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300657 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300658
659 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
660 plt_offset = shdr_plt.sh_offset;
661
662 if (shdr_rel_plt.sh_type == SHT_RELA) {
663 GElf_Rela pos_mem, *pos;
664
665 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
666 nr_rel_entries) {
667 symidx = GELF_R_SYM(pos->r_info);
668 plt_offset += shdr_plt.sh_entsize;
669 gelf_getsym(syms, symidx, &sym);
670 snprintf(sympltname, sizeof(sympltname),
671 "%s@plt", elf_sym__name(&sym, symstrs));
672
673 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200674 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300675 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300676 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300677
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200678 if (filter && filter(map, f))
679 symbol__delete(f);
680 else {
681 dso__insert_symbol(self, f);
682 ++nr;
683 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300684 }
685 } else if (shdr_rel_plt.sh_type == SHT_REL) {
686 GElf_Rel pos_mem, *pos;
687 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
688 nr_rel_entries) {
689 symidx = GELF_R_SYM(pos->r_info);
690 plt_offset += shdr_plt.sh_entsize;
691 gelf_getsym(syms, symidx, &sym);
692 snprintf(sympltname, sizeof(sympltname),
693 "%s@plt", elf_sym__name(&sym, symstrs));
694
695 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200696 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300697 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300698 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300699
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200700 if (filter && filter(map, f))
701 symbol__delete(f);
702 else {
703 dso__insert_symbol(self, f);
704 ++nr;
705 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300706 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300707 }
708
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300709 err = 0;
710out_elf_end:
711 elf_end(elf);
712out_close:
713 close(fd);
714
715 if (err == 0)
716 return nr;
717out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200718 pr_warning("%s: problems reading %s PLT info.\n",
719 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300720 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300721}
722
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300723static int dso__load_sym(struct dso *self, struct map *map, const char *name,
724 int fd, symbol_filter_t filter, int kernel,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200725 int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300726{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300727 struct map *curr_map = map;
728 struct dso *curr_dso = self;
729 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200730 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300731 uint32_t nr_syms;
732 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200733 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300734 GElf_Ehdr ehdr;
735 GElf_Shdr shdr;
736 Elf_Data *syms;
737 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300738 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300739 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300740 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300741
Marti Raudsepp84087122009-10-24 19:10:36 +0300742 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300743 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200744 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300745 goto out_close;
746 }
747
748 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200749 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300750 goto out_elf_end;
751 }
752
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300753 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
754 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300755 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
756 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300757 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300758 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300759
760 syms = elf_getdata(sec, NULL);
761 if (syms == NULL)
762 goto out_elf_end;
763
764 sec = elf_getscn(elf, shdr.sh_link);
765 if (sec == NULL)
766 goto out_elf_end;
767
768 symstrs = elf_getdata(sec, NULL);
769 if (symstrs == NULL)
770 goto out_elf_end;
771
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200772 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
773 if (sec_strndx == NULL)
774 goto out_elf_end;
775
776 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500777 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200778 goto out_elf_end;
779
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300780 nr_syms = shdr.sh_size / shdr.sh_entsize;
781
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200782 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200783 if (!kernel) {
784 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300785 elf_section_by_name(elf, &ehdr, &shdr,
786 ".gnu.prelink_undo",
787 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200788 } else self->adjust_symbols = 0;
789
Ingo Molnar83a09442009-08-15 12:26:57 +0200790 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300791 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200792 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300793 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200794 int is_label = elf_sym__is_label(&sym);
795 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300796
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200797 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300798 continue;
799
800 sec = elf_getscn(elf, sym.st_shndx);
801 if (!sec)
802 goto out_elf_end;
803
804 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200805
806 if (is_label && !elf_sec__is_text(&shdr, secstrs))
807 continue;
808
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300809 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200810 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200811
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300812 if (kernel || kmodule) {
813 char dso_name[PATH_MAX];
814
815 if (strcmp(section_name,
816 curr_dso->short_name + dso_name_len) == 0)
817 goto new_symbol;
818
819 if (strcmp(section_name, ".text") == 0) {
820 curr_map = map;
821 curr_dso = self;
822 goto new_symbol;
823 }
824
825 snprintf(dso_name, sizeof(dso_name),
826 "%s%s", self->short_name, section_name);
827
828 curr_map = kernel_maps__find_by_dso_name(dso_name);
829 if (curr_map == NULL) {
830 u64 start = sym.st_value;
831
832 if (kmodule)
833 start += map->start + shdr.sh_offset;
834
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200835 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300836 if (curr_dso == NULL)
837 goto out_elf_end;
838 curr_map = map__new2(start, curr_dso);
839 if (curr_map == NULL) {
840 dso__delete(curr_dso);
841 goto out_elf_end;
842 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200843 curr_map->map_ip = identity__map_ip;
844 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300845 curr_dso->origin = DSO__ORIG_KERNEL;
846 kernel_maps__insert(curr_map);
847 dsos__add(curr_dso);
848 } else
849 curr_dso = curr_map->dso;
850
851 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300852 }
853
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300854 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200855 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
856 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
857 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300858 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300859 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300860 /*
861 * We need to figure out if the object was created from C++ sources
862 * DWARF DW_compile_unit has this, but we don't always have access
863 * to it...
864 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200865 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300866 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +0200867 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300868new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200869 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300870 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300871 if (!f)
872 goto out_elf_end;
873
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300874 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200875 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300876 else {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300877 dso__insert_symbol(curr_dso, f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300878 nr++;
879 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300880 }
881
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300882 /*
883 * For misannotated, zeroed, ASM function sizes.
884 */
885 if (nr > 0)
886 dso__fixup_sym_end(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300887 err = nr;
888out_elf_end:
889 elf_end(elf);
890out_close:
891 return err;
892}
893
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200894static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
895{
896 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
897}
898
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200899bool dsos__read_build_ids(void)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100900{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200901 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100902 struct dso *pos;
903
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200904 list_for_each_entry(pos, &dsos, node)
905 if (filename__read_build_id(pos->long_name, pos->build_id,
906 sizeof(pos->build_id)) > 0) {
907 have_build_id = true;
908 pos->has_build_id = true;
909 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100910
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200911 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100912}
913
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200914/*
915 * Align offset to 4 bytes as needed for note name and descriptor data.
916 */
917#define NOTE_ALIGN(n) (((n) + 3) & -4U)
918
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200919int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300920{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200921 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300922 GElf_Ehdr ehdr;
923 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200924 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300925 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +0200926 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200927 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300928 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300929
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200930 if (size < BUILD_ID_SIZE)
931 goto out;
932
933 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300934 if (fd < 0)
935 goto out;
936
Marti Raudsepp84087122009-10-24 19:10:36 +0300937 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300938 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200939 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300940 goto out_close;
941 }
942
Pekka Enberge57cfcd2009-11-22 12:29:44 +0200943 ek = elf_kind(elf);
944 if (ek != ELF_K_ELF)
945 goto out_elf_end;
946
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300947 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200948 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300949 goto out_elf_end;
950 }
951
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200952 sec = elf_section_by_name(elf, &ehdr, &shdr,
953 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200954 if (sec == NULL) {
955 sec = elf_section_by_name(elf, &ehdr, &shdr,
956 ".notes", NULL);
957 if (sec == NULL)
958 goto out_elf_end;
959 }
960
961 data = elf_getdata(sec, NULL);
962 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300963 goto out_elf_end;
964
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200965 ptr = data->d_buf;
966 while (ptr < (data->d_buf + data->d_size)) {
967 GElf_Nhdr *nhdr = ptr;
968 int namesz = NOTE_ALIGN(nhdr->n_namesz),
969 descsz = NOTE_ALIGN(nhdr->n_descsz);
970 const char *name;
971
972 ptr += sizeof(*nhdr);
973 name = ptr;
974 ptr += namesz;
975 if (nhdr->n_type == NT_GNU_BUILD_ID &&
976 nhdr->n_namesz == sizeof("GNU")) {
977 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
978 memcpy(bf, ptr, BUILD_ID_SIZE);
979 err = BUILD_ID_SIZE;
980 break;
981 }
982 }
983 ptr += descsz;
984 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200985out_elf_end:
986 elf_end(elf);
987out_close:
988 close(fd);
989out:
990 return err;
991}
992
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200993int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
994{
995 int fd, err = -1;
996
997 if (size < BUILD_ID_SIZE)
998 goto out;
999
1000 fd = open(filename, O_RDONLY);
1001 if (fd < 0)
1002 goto out;
1003
1004 while (1) {
1005 char bf[BUFSIZ];
1006 GElf_Nhdr nhdr;
1007 int namesz, descsz;
1008
1009 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1010 break;
1011
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001012 namesz = NOTE_ALIGN(nhdr.n_namesz);
1013 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001014 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1015 nhdr.n_namesz == sizeof("GNU")) {
1016 if (read(fd, bf, namesz) != namesz)
1017 break;
1018 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1019 if (read(fd, build_id,
1020 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1021 err = 0;
1022 break;
1023 }
1024 } else if (read(fd, bf, descsz) != descsz)
1025 break;
1026 } else {
1027 int n = namesz + descsz;
1028 if (read(fd, bf, n) != n)
1029 break;
1030 }
1031 }
1032 close(fd);
1033out:
1034 return err;
1035}
1036
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001037char dso__symtab_origin(const struct dso *self)
1038{
1039 static const char origin[] = {
1040 [DSO__ORIG_KERNEL] = 'k',
1041 [DSO__ORIG_JAVA_JIT] = 'j',
1042 [DSO__ORIG_FEDORA] = 'f',
1043 [DSO__ORIG_UBUNTU] = 'u',
1044 [DSO__ORIG_BUILDID] = 'b',
1045 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001046 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001047 };
1048
1049 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1050 return '!';
1051 return origin[self->origin];
1052}
1053
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001054int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001055{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001056 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001057 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001058 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001059 int ret = -1;
1060 int fd;
1061
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001062 self->loaded = 1;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001063
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001064 if (self->kernel)
1065 return dso__load_kernel_sym(self, map, filter);
1066
1067 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001068 if (!name)
1069 return -1;
1070
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001071 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001072
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001073 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001074 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001075 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1076 DSO__ORIG_NOT_FOUND;
1077 return ret;
1078 }
1079
1080 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001081
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001082more:
1083 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001084 self->origin++;
1085 switch (self->origin) {
1086 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001087 snprintf(name, size, "/usr/lib/debug%s.debug",
1088 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001089 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001090 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001091 snprintf(name, size, "/usr/lib/debug%s",
1092 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001093 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001094 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001095 if (filename__read_build_id(self->long_name, build_id,
1096 sizeof(build_id))) {
1097 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1098
1099 build_id__sprintf(build_id, sizeof(build_id),
1100 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001101 snprintf(name, size,
1102 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001103 build_id_hex, build_id_hex + 2);
1104 if (self->has_build_id)
1105 goto compare_build_id;
1106 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001107 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001108 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001109 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001110 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001111 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001112 break;
1113
1114 default:
1115 goto out;
1116 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001117
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001118 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001119 if (filename__read_build_id(name, build_id,
1120 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001121 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001122compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001123 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001124 goto more;
1125 }
1126
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001127 fd = open(name, O_RDONLY);
1128 } while (fd < 0);
1129
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001130 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001131 close(fd);
1132
1133 /*
1134 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1135 */
1136 if (!ret)
1137 goto more;
1138
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001139 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001140 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001141 if (nr_plt > 0)
1142 ret += nr_plt;
1143 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001144out:
1145 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001146 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1147 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001148 return ret;
1149}
1150
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001151struct map *kernel_map;
1152
1153static void kernel_maps__insert(struct map *map)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001154{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001155 maps__insert(&kernel_maps, map);
1156}
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001157
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001158struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp,
1159 symbol_filter_t filter)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001160{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001161 struct map *map = maps__find(&kernel_maps, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001162
1163 if (mapp)
1164 *mapp = map;
1165
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001166 if (map) {
1167 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001168 return map__find_symbol(map, ip, filter);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001169 }
1170
1171 return NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001172}
1173
1174struct map *kernel_maps__find_by_dso_name(const char *name)
1175{
1176 struct rb_node *nd;
1177
1178 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1179 struct map *map = rb_entry(nd, struct map, rb_node);
1180
1181 if (map->dso && strcmp(map->dso->name, name) == 0)
1182 return map;
1183 }
1184
1185 return NULL;
1186}
1187
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001188static int dsos__set_modules_path_dir(char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001189{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001190 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001191 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001192
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001193 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001194 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001195 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001196 }
1197
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001198 while ((dent = readdir(dir)) != NULL) {
1199 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001200
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001201 if (dent->d_type == DT_DIR) {
1202 if (!strcmp(dent->d_name, ".") ||
1203 !strcmp(dent->d_name, ".."))
1204 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001205
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001206 snprintf(path, sizeof(path), "%s/%s",
1207 dirname, dent->d_name);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001208 if (dsos__set_modules_path_dir(path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001209 goto failure;
1210 } else {
1211 char *dot = strrchr(dent->d_name, '.'),
1212 dso_name[PATH_MAX];
1213 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001214 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001215
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001216 if (dot == NULL || strcmp(dot, ".ko"))
1217 continue;
1218 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1219 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001220
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001221 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001222 map = kernel_maps__find_by_dso_name(dso_name);
1223 if (map == NULL)
1224 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001225
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001226 snprintf(path, sizeof(path), "%s/%s",
1227 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001228
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001229 long_name = strdup(path);
1230 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001231 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001232 dso__set_long_name(map->dso, long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001233 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001234 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001235
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001236 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001237failure:
1238 closedir(dir);
1239 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001240}
1241
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001242static int dsos__set_modules_path(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001243{
1244 struct utsname uts;
1245 char modules_path[PATH_MAX];
1246
1247 if (uname(&uts) < 0)
1248 return -1;
1249
1250 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1251 uts.release);
1252
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001253 return dsos__set_modules_path_dir(modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001254}
1255
1256/*
1257 * Constructor variant for modules (where we know from /proc/modules where
1258 * they are loaded) and for vmlinux, where only after we load all the
1259 * symbols we'll know where it starts and ends.
1260 */
1261static struct map *map__new2(u64 start, struct dso *dso)
1262{
1263 struct map *self = malloc(sizeof(*self));
1264
1265 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001266 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001267 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001268 */
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001269 map__init(self, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001270 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001271
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001272 return self;
1273}
1274
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001275static int kernel_maps__create_module_maps(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001276{
1277 char *line = NULL;
1278 size_t n;
1279 FILE *file = fopen("/proc/modules", "r");
1280 struct map *map;
1281
1282 if (file == NULL)
1283 return -1;
1284
1285 while (!feof(file)) {
1286 char name[PATH_MAX];
1287 u64 start;
1288 struct dso *dso;
1289 char *sep;
1290 int line_len;
1291
1292 line_len = getline(&line, &n, file);
1293 if (line_len < 0)
1294 break;
1295
1296 if (!line)
1297 goto out_failure;
1298
1299 line[--line_len] = '\0'; /* \n */
1300
1301 sep = strrchr(line, 'x');
1302 if (sep == NULL)
1303 continue;
1304
1305 hex2u64(sep + 1, &start);
1306
1307 sep = strchr(line, ' ');
1308 if (sep == NULL)
1309 continue;
1310
1311 *sep = '\0';
1312
1313 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001314 dso = dso__new(name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001315
1316 if (dso == NULL)
1317 goto out_delete_line;
1318
1319 map = map__new2(start, dso);
1320 if (map == NULL) {
1321 dso__delete(dso);
1322 goto out_delete_line;
1323 }
1324
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001325 snprintf(name, sizeof(name),
1326 "/sys/module/%s/notes/.note.gnu.build-id", line);
1327 if (sysfs__read_build_id(name, dso->build_id,
1328 sizeof(dso->build_id)) == 0)
1329 dso->has_build_id = true;
1330
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001331 dso->origin = DSO__ORIG_KMODULE;
1332 kernel_maps__insert(map);
1333 dsos__add(dso);
1334 }
1335
1336 free(line);
1337 fclose(file);
1338
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001339 return dsos__set_modules_path();
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001340
1341out_delete_line:
1342 free(line);
1343out_failure:
1344 return -1;
1345}
1346
1347static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001348 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001349{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001350 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001351
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001352 if (self->has_build_id) {
1353 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001354
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001355 if (filename__read_build_id(vmlinux, build_id,
1356 sizeof(build_id)) < 0) {
1357 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1358 return -1;
1359 }
1360 if (!dso__build_id_equal(self, build_id)) {
1361 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1362 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1363
1364 build_id__sprintf(self->build_id,
1365 sizeof(self->build_id),
1366 expected_build_id);
1367 build_id__sprintf(build_id, sizeof(build_id),
1368 vmlinux_build_id);
1369 pr_debug("build_id in %s is %s while expected is %s, "
1370 "ignoring it\n", vmlinux, vmlinux_build_id,
1371 expected_build_id);
1372 return -1;
1373 }
1374 }
1375
1376 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001377 if (fd < 0)
1378 return -1;
1379
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001380 self->loaded = 1;
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001381 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001382
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001383 close(fd);
1384
1385 return err;
1386}
1387
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001388static int dso__load_kernel_sym(struct dso *self, struct map *map,
1389 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001390{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001391 int err;
1392 bool is_kallsyms;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001393
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001394 if (vmlinux_path != NULL) {
1395 int i;
1396 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1397 vmlinux_path__nr_entries);
1398 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1399 err = dso__load_vmlinux(self, map, vmlinux_path[i],
1400 filter);
1401 if (err > 0) {
1402 pr_debug("Using %s for symbols\n",
1403 vmlinux_path[i]);
1404 dso__set_long_name(self,
1405 strdup(vmlinux_path[i]));
1406 goto out_fixup;
1407 }
1408 }
1409 }
1410
1411 is_kallsyms = self->long_name[0] == '[';
1412 if (is_kallsyms)
1413 goto do_kallsyms;
1414
1415 err = dso__load_vmlinux(self, map, self->long_name, filter);
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001416 if (err <= 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001417 pr_info("The file %s cannot be used, "
1418 "trying to use /proc/kallsyms...", self->long_name);
1419 sleep(2);
1420do_kallsyms:
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001421 err = kernel_maps__load_kallsyms(filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001422 if (err > 0 && !is_kallsyms)
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001423 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1424 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001425
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001426 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001427out_fixup:
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001428 map__fixup_start(map);
1429 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001430 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001431
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001432 return err;
1433}
1434
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001435LIST_HEAD(dsos);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001436struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001437
1438static void dsos__add(struct dso *dso)
1439{
1440 list_add_tail(&dso->node, &dsos);
1441}
1442
1443static struct dso *dsos__find(const char *name)
1444{
1445 struct dso *pos;
1446
1447 list_for_each_entry(pos, &dsos, node)
1448 if (strcmp(pos->name, name) == 0)
1449 return pos;
1450 return NULL;
1451}
1452
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001453struct dso *dsos__findnew(const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001454{
1455 struct dso *dso = dsos__find(name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001456
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001457 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001458 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001459 if (dso != NULL) {
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001460 dsos__add(dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001461 dso__set_basename(dso);
1462 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001463 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001464
1465 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001466}
1467
1468void dsos__fprintf(FILE *fp)
1469{
1470 struct dso *pos;
1471
1472 list_for_each_entry(pos, &dsos, node)
1473 dso__fprintf(pos, fp);
1474}
1475
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001476size_t dsos__fprintf_buildid(FILE *fp)
1477{
1478 struct dso *pos;
1479 size_t ret = 0;
1480
1481 list_for_each_entry(pos, &dsos, node) {
1482 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001483 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001484 }
1485 return ret;
1486}
1487
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001488static int kernel_maps__create_kernel_map(const char *vmlinux_name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001489{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001490 struct dso *kernel = dso__new(vmlinux_name ?: "[kernel.kallsyms]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001491
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001492 if (kernel == NULL)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001493 return -1;
1494
1495 kernel_map = map__new2(0, kernel);
1496 if (kernel_map == NULL)
1497 goto out_delete_kernel_dso;
1498
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001499 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1500 kernel->short_name = "[kernel]";
1501 kernel->kernel = 1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001502
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001503 vdso = dso__new("[vdso]");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001504 if (vdso == NULL)
1505 goto out_delete_kernel_map;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001506
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001507 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1508 sizeof(kernel->build_id)) == 0)
1509 kernel->has_build_id = true;
1510
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001511 kernel_maps__insert(kernel_map);
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001512 dsos__add(kernel);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001513 dsos__add(vdso);
1514
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001515 return 0;
1516
1517out_delete_kernel_map:
1518 map__delete(kernel_map);
1519 kernel_map = NULL;
1520out_delete_kernel_dso:
1521 dso__delete(kernel);
1522 return -1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001523}
1524
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001525static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001526{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001527 while (--vmlinux_path__nr_entries >= 0) {
1528 free(vmlinux_path[vmlinux_path__nr_entries]);
1529 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1530 }
1531
1532 free(vmlinux_path);
1533 vmlinux_path = NULL;
1534}
1535
1536static int vmlinux_path__init(void)
1537{
1538 struct utsname uts;
1539 char bf[PATH_MAX];
1540
1541 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001542 return -1;
1543
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001544 vmlinux_path = malloc(sizeof(char *) * 5);
1545 if (vmlinux_path == NULL)
1546 return -1;
1547
1548 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1549 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1550 goto out_fail;
1551 ++vmlinux_path__nr_entries;
1552 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1553 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1554 goto out_fail;
1555 ++vmlinux_path__nr_entries;
1556 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1557 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1558 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1559 goto out_fail;
1560 ++vmlinux_path__nr_entries;
1561 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1562 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1563 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1564 goto out_fail;
1565 ++vmlinux_path__nr_entries;
1566 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1567 uts.release);
1568 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1569 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1570 goto out_fail;
1571 ++vmlinux_path__nr_entries;
1572
1573 return 0;
1574
1575out_fail:
1576 vmlinux_path__exit();
1577 return -1;
1578}
1579
1580int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
1581 bool use_modules)
1582{
1583 if (try_vmlinux_path && vmlinux_path__init() < 0)
1584 return -1;
1585
1586 if (kernel_maps__create_kernel_map(vmlinux_name) < 0) {
1587 vmlinux_path__exit();
1588 return -1;
1589 }
1590
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001591 if (use_modules && kernel_maps__create_module_maps() < 0)
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001592 pr_debug("Failed to load list of modules in use, "
1593 "continuing...\n");
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001594 /*
1595 * Now that we have all the maps created, just set the ->end of them:
1596 */
1597 kernel_maps__fixup_end();
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001598 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001599}
1600
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001601void symbol__init(unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001602{
1603 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001604 symbol__priv_size = priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001605}