blob: c324bdf8baba1bd4bac10fd752672c065c8fb781 [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 Melo94cb9e32009-08-06 14:43:17 -030015enum dso_origin {
16 DSO__ORIG_KERNEL = 0,
17 DSO__ORIG_JAVA_JIT,
18 DSO__ORIG_FEDORA,
19 DSO__ORIG_UBUNTU,
20 DSO__ORIG_BUILDID,
21 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030022 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030023 DSO__ORIG_NOT_FOUND,
24};
25
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030026static void dsos__add(struct dso *dso);
27static struct dso *dsos__find(const char *name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030028static struct map *map__new2(u64 start, struct dso *dso);
29static void kernel_maps__insert(struct map *map);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020030unsigned int symbol__priv_size;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030031
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030032static struct rb_root kernel_maps;
33
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030034static void dso__fixup_sym_end(struct dso *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030035{
36 struct rb_node *nd, *prevnd = rb_first(&self->syms);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030037 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030038
39 if (prevnd == NULL)
40 return;
41
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030042 curr = rb_entry(prevnd, struct symbol, rb_node);
43
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030044 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030045 prev = curr;
46 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030047
48 if (prev->end == prev->start)
49 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030050 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030051
52 /* Last entry */
53 if (curr->end == curr->start)
54 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030055}
56
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030057static void kernel_maps__fixup_end(void)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030058{
59 struct map *prev, *curr;
60 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
61
62 if (prevnd == NULL)
63 return;
64
65 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030066
67 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
68 prev = curr;
69 curr = rb_entry(nd, struct map, rb_node);
70 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030071 }
72
73 nd = rb_last(&curr->dso->syms);
74 if (nd) {
75 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
76 curr->end = sym->end;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030077 }
78}
79
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020080static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030081{
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -030082 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020083 struct symbol *self = calloc(1, (symbol__priv_size +
84 sizeof(*self) + namelen));
Ingo Molnar0b73da32009-06-06 15:48:52 +020085 if (!self)
86 return NULL;
87
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020088 if (symbol__priv_size) {
89 memset(self, 0, symbol__priv_size);
90 self = ((void *)self) + symbol__priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030091 }
Ingo Molnar0b73da32009-06-06 15:48:52 +020092 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +020093 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020094
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020095 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020096
Ingo Molnar0b73da32009-06-06 15:48:52 +020097 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030098
99 return self;
100}
101
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200102static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300103{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200104 free(((void *)self) - symbol__priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300105}
106
107static size_t symbol__fprintf(struct symbol *self, FILE *fp)
108{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300109 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300110 self->start, self->end, self->name);
111}
112
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200113static void dso__set_long_name(struct dso *self, char *name)
114{
115 self->long_name = name;
116 self->long_name_len = strlen(name);
117}
118
119static void dso__set_basename(struct dso *self)
120{
121 self->short_name = basename(self->long_name);
122}
123
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200124struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300125{
126 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
127
128 if (self != NULL) {
129 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200130 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300131 self->short_name = self->name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300132 self->syms = RB_ROOT;
Peter Zijlstrafc54db52009-06-05 14:04:59 +0200133 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300134 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300135 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200136 self->loaded = 0;
137 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300138 }
139
140 return self;
141}
142
143static void dso__delete_symbols(struct dso *self)
144{
145 struct symbol *pos;
146 struct rb_node *next = rb_first(&self->syms);
147
148 while (next) {
149 pos = rb_entry(next, struct symbol, rb_node);
150 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -0300151 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200152 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300153 }
154}
155
156void dso__delete(struct dso *self)
157{
158 dso__delete_symbols(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300159 if (self->long_name != self->name)
160 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300161 free(self);
162}
163
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200164void dso__set_build_id(struct dso *self, void *build_id)
165{
166 memcpy(self->build_id, build_id, sizeof(self->build_id));
167 self->has_build_id = 1;
168}
169
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300170static void dso__insert_symbol(struct dso *self, struct symbol *sym)
171{
172 struct rb_node **p = &self->syms.rb_node;
173 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000174 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300175 struct symbol *s;
176
177 while (*p != NULL) {
178 parent = *p;
179 s = rb_entry(parent, struct symbol, rb_node);
180 if (ip < s->start)
181 p = &(*p)->rb_left;
182 else
183 p = &(*p)->rb_right;
184 }
185 rb_link_node(&sym->rb_node, parent, p);
186 rb_insert_color(&sym->rb_node, &self->syms);
187}
188
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000189struct symbol *dso__find_symbol(struct dso *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300190{
191 struct rb_node *n;
192
193 if (self == NULL)
194 return NULL;
195
196 n = self->syms.rb_node;
197
198 while (n) {
199 struct symbol *s = rb_entry(n, struct symbol, rb_node);
200
201 if (ip < s->start)
202 n = n->rb_left;
203 else if (ip > s->end)
204 n = n->rb_right;
205 else
206 return s;
207 }
208
209 return NULL;
210}
211
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200212int build_id__sprintf(u8 *self, int len, char *bf)
213{
214 char *bid = bf;
215 u8 *raw = self;
216 int i;
217
218 for (i = 0; i < len; ++i) {
219 sprintf(bid, "%02x", *raw);
220 ++raw;
221 bid += 2;
222 }
223
224 return raw - self;
225}
226
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200227size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300228{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200229 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200230
231 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200232 return fprintf(fp, "%s", sbuild_id);
233}
234
235size_t dso__fprintf(struct dso *self, FILE *fp)
236{
237 struct rb_node *nd;
238 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
239
240 ret += dso__fprintf_buildid(self, fp);
241 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200242
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300243 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
244 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
245 ret += symbol__fprintf(pos, fp);
246 }
247
248 return ret;
249}
250
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300251/*
252 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
253 * so that we can in the next step set the symbol ->end address and then
254 * call kernel_maps__split_kallsyms.
255 */
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200256static int kernel_maps__load_all_kallsyms(void)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300257{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300258 char *line = NULL;
259 size_t n;
260 FILE *file = fopen("/proc/kallsyms", "r");
261
262 if (file == NULL)
263 goto out_failure;
264
265 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000266 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300267 struct symbol *sym;
268 int line_len, len;
269 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300270 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300271
272 line_len = getline(&line, &n, file);
273 if (line_len < 0)
274 break;
275
276 if (!line)
277 goto out_failure;
278
279 line[--line_len] = '\0'; /* \n */
280
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300281 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300282
283 len++;
284 if (len + 2 >= line_len)
285 continue;
286
287 symbol_type = toupper(line[len]);
288 /*
289 * We're interested only in code ('T'ext)
290 */
291 if (symbol_type != 'T' && symbol_type != 'W')
292 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300293
294 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300295 /*
296 * Will fix up the end later, when we have all symbols sorted.
297 */
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200298 sym = symbol__new(start, 0, symbol_name);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300299
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300300 if (sym == NULL)
301 goto out_delete_line;
302
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200303 /*
304 * We will pass the symbols to the filter later, in
305 * kernel_maps__split_kallsyms, when we have split the
306 * maps per module
307 */
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300308 dso__insert_symbol(kernel_map->dso, sym);
309 }
310
311 free(line);
312 fclose(file);
313
314 return 0;
315
316out_delete_line:
317 free(line);
318out_failure:
319 return -1;
320}
321
322/*
323 * Split the symbols into maps, making sure there are no overlaps, i.e. the
324 * kernel range is broken in several maps, named [kernel].N, as we don't have
325 * the original ELF section names vmlinux have.
326 */
327static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
328{
329 struct map *map = kernel_map;
330 struct symbol *pos;
331 int count = 0;
332 struct rb_node *next = rb_first(&kernel_map->dso->syms);
333 int kernel_range = 0;
334
335 while (next) {
336 char *module;
337
338 pos = rb_entry(next, struct symbol, rb_node);
339 next = rb_next(&pos->rb_node);
340
341 module = strchr(pos->name, '\t');
342 if (module) {
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300343 if (!use_modules)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300344 goto delete_symbol;
345
346 *module++ = '\0';
347
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300348 if (strcmp(map->dso->name, module)) {
349 map = kernel_maps__find_by_dso_name(module);
350 if (!map) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200351 pr_err("/proc/{kallsyms,modules} "
352 "inconsistency!\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300353 return -1;
354 }
355 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300356 /*
357 * So that we look just like we get from .ko files,
358 * i.e. not prelinked, relative to map->start.
359 */
360 pos->start = map->map_ip(map, pos->start);
361 pos->end = map->map_ip(map, pos->end);
362 } else if (map != kernel_map) {
363 char dso_name[PATH_MAX];
364 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300365
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300366 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
367 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300368
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200369 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300370 if (dso == NULL)
371 return -1;
372
373 map = map__new2(pos->start, dso);
374 if (map == NULL) {
375 dso__delete(dso);
376 return -1;
377 }
378
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200379 map->map_ip = map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300380 kernel_maps__insert(map);
381 ++kernel_range;
382 }
383
384 if (filter && filter(map, pos)) {
385delete_symbol:
386 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200387 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300388 } else {
389 if (map != kernel_map) {
390 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
391 dso__insert_symbol(map->dso, pos);
392 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200393 count++;
394 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300395 }
396
Mike Galbraith9974f492009-07-02 08:05:58 +0200397 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300398}
399
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300400
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200401static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300402{
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200403 if (kernel_maps__load_all_kallsyms())
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300404 return -1;
405
406 dso__fixup_sym_end(kernel_map->dso);
407
408 return kernel_maps__split_kallsyms(filter, use_modules);
409}
410
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200411static size_t kernel_maps__fprintf(FILE *fp)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300412{
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200413 size_t printed = fprintf(fp, "Kernel maps:\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300414 struct rb_node *nd;
415
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300416 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
417 struct map *pos = rb_entry(nd, struct map, rb_node);
418
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300419 printed += fprintf(fp, "Map:");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300420 printed += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200421 if (verbose > 1) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300422 printed += dso__fprintf(pos->dso, fp);
423 printed += fprintf(fp, "--\n");
424 }
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300425 }
426
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200427 return printed + fprintf(fp, "END kernel maps\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300428}
429
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300430static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200431 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300432{
433 char *line = NULL;
434 size_t n;
435 FILE *file;
436 int nr_syms = 0;
437
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300438 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300439 if (file == NULL)
440 goto out_failure;
441
442 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000443 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300444 struct symbol *sym;
445 int line_len, len;
446
447 line_len = getline(&line, &n, file);
448 if (line_len < 0)
449 break;
450
451 if (!line)
452 goto out_failure;
453
454 line[--line_len] = '\0'; /* \n */
455
456 len = hex2u64(line, &start);
457
458 len++;
459 if (len + 2 >= line_len)
460 continue;
461
462 len += hex2u64(line + len, &size);
463
464 len++;
465 if (len + 2 >= line_len)
466 continue;
467
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200468 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300469
470 if (sym == NULL)
471 goto out_delete_line;
472
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300473 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200474 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300475 else {
476 dso__insert_symbol(self, sym);
477 nr_syms++;
478 }
479 }
480
481 free(line);
482 fclose(file);
483
484 return nr_syms;
485
486out_delete_line:
487 free(line);
488out_failure:
489 return -1;
490}
491
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300492/**
493 * elf_symtab__for_each_symbol - iterate thru all the symbols
494 *
495 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200496 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300497 * @sym: GElf_Sym iterator
498 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200499#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
500 for (idx = 0, gelf_getsym(syms, idx, &sym);\
501 idx < nr_syms; \
502 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300503
504static inline uint8_t elf_sym__type(const GElf_Sym *sym)
505{
506 return GELF_ST_TYPE(sym->st_info);
507}
508
509static inline int elf_sym__is_function(const GElf_Sym *sym)
510{
511 return elf_sym__type(sym) == STT_FUNC &&
512 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300513 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300514}
515
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200516static inline int elf_sym__is_label(const GElf_Sym *sym)
517{
518 return elf_sym__type(sym) == STT_NOTYPE &&
519 sym->st_name != 0 &&
520 sym->st_shndx != SHN_UNDEF &&
521 sym->st_shndx != SHN_ABS;
522}
523
524static inline const char *elf_sec__name(const GElf_Shdr *shdr,
525 const Elf_Data *secstrs)
526{
527 return secstrs->d_buf + shdr->sh_name;
528}
529
530static inline int elf_sec__is_text(const GElf_Shdr *shdr,
531 const Elf_Data *secstrs)
532{
533 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
534}
535
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300536static inline const char *elf_sym__name(const GElf_Sym *sym,
537 const Elf_Data *symstrs)
538{
539 return symstrs->d_buf + sym->st_name;
540}
541
542static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
543 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200544 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300545{
546 Elf_Scn *sec = NULL;
547 size_t cnt = 1;
548
549 while ((sec = elf_nextscn(elf, sec)) != NULL) {
550 char *str;
551
552 gelf_getshdr(sec, shp);
553 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
554 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200555 if (idx)
556 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300557 break;
558 }
559 ++cnt;
560 }
561
562 return sec;
563}
564
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300565#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
566 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
567 idx < nr_entries; \
568 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
569
570#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
571 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
572 idx < nr_entries; \
573 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
574
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300575/*
576 * We need to check if we have a .dynsym, so that we can handle the
577 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
578 * .dynsym or .symtab).
579 * And always look at the original dso, not at debuginfo packages, that
580 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
581 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200582static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
583 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300584{
585 uint32_t nr_rel_entries, idx;
586 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000587 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300588 GElf_Shdr shdr_plt;
589 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300590 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300591 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300592 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
593 size_t dynsym_idx;
594 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300595 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300596 Elf *elf;
597 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300598
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300599 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300600 if (fd < 0)
601 goto out;
602
Marti Raudsepp84087122009-10-24 19:10:36 +0300603 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300604 if (elf == NULL)
605 goto out_close;
606
607 if (gelf_getehdr(elf, &ehdr) == NULL)
608 goto out_elf_end;
609
610 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
611 ".dynsym", &dynsym_idx);
612 if (scn_dynsym == NULL)
613 goto out_elf_end;
614
615 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300616 ".rela.plt", NULL);
617 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300618 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300619 ".rel.plt", NULL);
620 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300621 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300622 }
623
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300624 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300625
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300626 if (shdr_rel_plt.sh_link != dynsym_idx)
627 goto out_elf_end;
628
629 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
630 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300631
632 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200633 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300634 * and the symbols in the .dynsym they refer to.
635 */
636 reldata = elf_getdata(scn_plt_rel, NULL);
637 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300638 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300639
640 syms = elf_getdata(scn_dynsym, NULL);
641 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300642 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300643
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300644 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300645 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300646 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300647
648 symstrs = elf_getdata(scn_symstrs, NULL);
649 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300650 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300651
652 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
653 plt_offset = shdr_plt.sh_offset;
654
655 if (shdr_rel_plt.sh_type == SHT_RELA) {
656 GElf_Rela pos_mem, *pos;
657
658 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
659 nr_rel_entries) {
660 symidx = GELF_R_SYM(pos->r_info);
661 plt_offset += shdr_plt.sh_entsize;
662 gelf_getsym(syms, symidx, &sym);
663 snprintf(sympltname, sizeof(sympltname),
664 "%s@plt", elf_sym__name(&sym, symstrs));
665
666 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200667 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300668 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300669 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300670
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200671 if (filter && filter(map, f))
672 symbol__delete(f);
673 else {
674 dso__insert_symbol(self, f);
675 ++nr;
676 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300677 }
678 } else if (shdr_rel_plt.sh_type == SHT_REL) {
679 GElf_Rel pos_mem, *pos;
680 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
681 nr_rel_entries) {
682 symidx = GELF_R_SYM(pos->r_info);
683 plt_offset += shdr_plt.sh_entsize;
684 gelf_getsym(syms, symidx, &sym);
685 snprintf(sympltname, sizeof(sympltname),
686 "%s@plt", elf_sym__name(&sym, symstrs));
687
688 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200689 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300690 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300691 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300692
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200693 if (filter && filter(map, f))
694 symbol__delete(f);
695 else {
696 dso__insert_symbol(self, f);
697 ++nr;
698 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300699 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300700 }
701
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300702 err = 0;
703out_elf_end:
704 elf_end(elf);
705out_close:
706 close(fd);
707
708 if (err == 0)
709 return nr;
710out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200711 pr_warning("%s: problems reading %s PLT info.\n",
712 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300713 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300714}
715
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300716static int dso__load_sym(struct dso *self, struct map *map, const char *name,
717 int fd, symbol_filter_t filter, int kernel,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200718 int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300719{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300720 struct map *curr_map = map;
721 struct dso *curr_dso = self;
722 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200723 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300724 uint32_t nr_syms;
725 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200726 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300727 GElf_Ehdr ehdr;
728 GElf_Shdr shdr;
729 Elf_Data *syms;
730 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300731 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300732 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300733 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300734
Marti Raudsepp84087122009-10-24 19:10:36 +0300735 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300736 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200737 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300738 goto out_close;
739 }
740
741 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200742 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300743 goto out_elf_end;
744 }
745
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300746 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
747 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300748 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
749 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300750 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300751 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300752
753 syms = elf_getdata(sec, NULL);
754 if (syms == NULL)
755 goto out_elf_end;
756
757 sec = elf_getscn(elf, shdr.sh_link);
758 if (sec == NULL)
759 goto out_elf_end;
760
761 symstrs = elf_getdata(sec, NULL);
762 if (symstrs == NULL)
763 goto out_elf_end;
764
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200765 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
766 if (sec_strndx == NULL)
767 goto out_elf_end;
768
769 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500770 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200771 goto out_elf_end;
772
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300773 nr_syms = shdr.sh_size / shdr.sh_entsize;
774
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200775 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200776 if (!kernel) {
777 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300778 elf_section_by_name(elf, &ehdr, &shdr,
779 ".gnu.prelink_undo",
780 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200781 } else self->adjust_symbols = 0;
782
Ingo Molnar83a09442009-08-15 12:26:57 +0200783 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300784 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200785 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300786 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200787 int is_label = elf_sym__is_label(&sym);
788 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300789
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200790 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300791 continue;
792
793 sec = elf_getscn(elf, sym.st_shndx);
794 if (!sec)
795 goto out_elf_end;
796
797 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200798
799 if (is_label && !elf_sec__is_text(&shdr, secstrs))
800 continue;
801
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300802 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200803 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200804
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300805 if (kernel || kmodule) {
806 char dso_name[PATH_MAX];
807
808 if (strcmp(section_name,
809 curr_dso->short_name + dso_name_len) == 0)
810 goto new_symbol;
811
812 if (strcmp(section_name, ".text") == 0) {
813 curr_map = map;
814 curr_dso = self;
815 goto new_symbol;
816 }
817
818 snprintf(dso_name, sizeof(dso_name),
819 "%s%s", self->short_name, section_name);
820
821 curr_map = kernel_maps__find_by_dso_name(dso_name);
822 if (curr_map == NULL) {
823 u64 start = sym.st_value;
824
825 if (kmodule)
826 start += map->start + shdr.sh_offset;
827
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200828 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300829 if (curr_dso == NULL)
830 goto out_elf_end;
831 curr_map = map__new2(start, curr_dso);
832 if (curr_map == NULL) {
833 dso__delete(curr_dso);
834 goto out_elf_end;
835 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200836 curr_map->map_ip = identity__map_ip;
837 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300838 curr_dso->origin = DSO__ORIG_KERNEL;
839 kernel_maps__insert(curr_map);
840 dsos__add(curr_dso);
841 } else
842 curr_dso = curr_map->dso;
843
844 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300845 }
846
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300847 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200848 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
849 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
850 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300851 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300852 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300853 /*
854 * We need to figure out if the object was created from C++ sources
855 * DWARF DW_compile_unit has this, but we don't always have access
856 * to it...
857 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200858 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300859 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +0200860 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300861new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200862 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300863 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300864 if (!f)
865 goto out_elf_end;
866
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300867 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200868 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300869 else {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300870 dso__insert_symbol(curr_dso, f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300871 nr++;
872 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300873 }
874
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300875 /*
876 * For misannotated, zeroed, ASM function sizes.
877 */
878 if (nr > 0)
879 dso__fixup_sym_end(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300880 err = nr;
881out_elf_end:
882 elf_end(elf);
883out_close:
884 return err;
885}
886
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200887static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
888{
889 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
890}
891
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200892bool dsos__read_build_ids(void)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100893{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200894 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100895 struct dso *pos;
896
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200897 list_for_each_entry(pos, &dsos, node)
898 if (filename__read_build_id(pos->long_name, pos->build_id,
899 sizeof(pos->build_id)) > 0) {
900 have_build_id = true;
901 pos->has_build_id = true;
902 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100903
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200904 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100905}
906
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200907/*
908 * Align offset to 4 bytes as needed for note name and descriptor data.
909 */
910#define NOTE_ALIGN(n) (((n) + 3) & -4U)
911
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200912int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300913{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200914 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300915 GElf_Ehdr ehdr;
916 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200917 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300918 Elf_Scn *sec;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200919 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300920 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300921
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200922 if (size < BUILD_ID_SIZE)
923 goto out;
924
925 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300926 if (fd < 0)
927 goto out;
928
Marti Raudsepp84087122009-10-24 19:10:36 +0300929 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300930 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200931 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300932 goto out_close;
933 }
934
935 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200936 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300937 goto out_elf_end;
938 }
939
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200940 sec = elf_section_by_name(elf, &ehdr, &shdr,
941 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200942 if (sec == NULL) {
943 sec = elf_section_by_name(elf, &ehdr, &shdr,
944 ".notes", NULL);
945 if (sec == NULL)
946 goto out_elf_end;
947 }
948
949 data = elf_getdata(sec, NULL);
950 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300951 goto out_elf_end;
952
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200953 ptr = data->d_buf;
954 while (ptr < (data->d_buf + data->d_size)) {
955 GElf_Nhdr *nhdr = ptr;
956 int namesz = NOTE_ALIGN(nhdr->n_namesz),
957 descsz = NOTE_ALIGN(nhdr->n_descsz);
958 const char *name;
959
960 ptr += sizeof(*nhdr);
961 name = ptr;
962 ptr += namesz;
963 if (nhdr->n_type == NT_GNU_BUILD_ID &&
964 nhdr->n_namesz == sizeof("GNU")) {
965 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
966 memcpy(bf, ptr, BUILD_ID_SIZE);
967 err = BUILD_ID_SIZE;
968 break;
969 }
970 }
971 ptr += descsz;
972 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200973out_elf_end:
974 elf_end(elf);
975out_close:
976 close(fd);
977out:
978 return err;
979}
980
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200981int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
982{
983 int fd, err = -1;
984
985 if (size < BUILD_ID_SIZE)
986 goto out;
987
988 fd = open(filename, O_RDONLY);
989 if (fd < 0)
990 goto out;
991
992 while (1) {
993 char bf[BUFSIZ];
994 GElf_Nhdr nhdr;
995 int namesz, descsz;
996
997 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
998 break;
999
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001000 namesz = NOTE_ALIGN(nhdr.n_namesz);
1001 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001002 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1003 nhdr.n_namesz == sizeof("GNU")) {
1004 if (read(fd, bf, namesz) != namesz)
1005 break;
1006 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1007 if (read(fd, build_id,
1008 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1009 err = 0;
1010 break;
1011 }
1012 } else if (read(fd, bf, descsz) != descsz)
1013 break;
1014 } else {
1015 int n = namesz + descsz;
1016 if (read(fd, bf, n) != n)
1017 break;
1018 }
1019 }
1020 close(fd);
1021out:
1022 return err;
1023}
1024
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001025char dso__symtab_origin(const struct dso *self)
1026{
1027 static const char origin[] = {
1028 [DSO__ORIG_KERNEL] = 'k',
1029 [DSO__ORIG_JAVA_JIT] = 'j',
1030 [DSO__ORIG_FEDORA] = 'f',
1031 [DSO__ORIG_UBUNTU] = 'u',
1032 [DSO__ORIG_BUILDID] = 'b',
1033 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001034 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001035 };
1036
1037 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1038 return '!';
1039 return origin[self->origin];
1040}
1041
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001042int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001043{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001044 int size = PATH_MAX;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001045 char *name = malloc(size);
1046 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001047 int ret = -1;
1048 int fd;
1049
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001050 self->loaded = 1;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001051
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001052 if (!name)
1053 return -1;
1054
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001055 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001056
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001057 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001058 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001059 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1060 DSO__ORIG_NOT_FOUND;
1061 return ret;
1062 }
1063
1064 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001065
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001066more:
1067 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001068 self->origin++;
1069 switch (self->origin) {
1070 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001071 snprintf(name, size, "/usr/lib/debug%s.debug",
1072 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001073 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001074 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001075 snprintf(name, size, "/usr/lib/debug%s",
1076 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001077 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001078 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001079 if (filename__read_build_id(self->long_name, build_id,
1080 sizeof(build_id))) {
1081 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1082
1083 build_id__sprintf(build_id, sizeof(build_id),
1084 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001085 snprintf(name, size,
1086 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001087 build_id_hex, build_id_hex + 2);
1088 if (self->has_build_id)
1089 goto compare_build_id;
1090 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001091 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001092 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001093 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001094 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001095 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001096 break;
1097
1098 default:
1099 goto out;
1100 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001101
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001102 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001103 if (filename__read_build_id(name, build_id,
1104 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001105 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001106compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001107 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001108 goto more;
1109 }
1110
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001111 fd = open(name, O_RDONLY);
1112 } while (fd < 0);
1113
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001114 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001115 close(fd);
1116
1117 /*
1118 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1119 */
1120 if (!ret)
1121 goto more;
1122
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001123 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001124 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001125 if (nr_plt > 0)
1126 ret += nr_plt;
1127 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001128out:
1129 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001130 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1131 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001132 return ret;
1133}
1134
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001135struct map *kernel_map;
1136
1137static void kernel_maps__insert(struct map *map)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001138{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001139 maps__insert(&kernel_maps, map);
1140}
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001141
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001142struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1143{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001144 struct map *map = maps__find(&kernel_maps, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001145
1146 if (mapp)
1147 *mapp = map;
1148
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001149 if (map) {
1150 ip = map->map_ip(map, ip);
1151 return map->dso->find_symbol(map->dso, ip);
1152 }
1153
1154 return NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001155}
1156
1157struct map *kernel_maps__find_by_dso_name(const char *name)
1158{
1159 struct rb_node *nd;
1160
1161 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1162 struct map *map = rb_entry(nd, struct map, rb_node);
1163
1164 if (map->dso && strcmp(map->dso->name, name) == 0)
1165 return map;
1166 }
1167
1168 return NULL;
1169}
1170
1171static int dso__load_module_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001172 symbol_filter_t filter)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001173{
1174 int err = 0, fd = open(self->long_name, O_RDONLY);
1175
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001176 self->loaded = 1;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001177
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001178 if (fd < 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001179 pr_err("%s: cannot open %s\n", __func__, self->long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001180 return err;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001181 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001182
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001183 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001184 close(fd);
1185
1186 return err;
1187}
1188
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001189static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001190{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001191 struct dirent *dent;
1192 int nr_symbols = 0, err;
1193 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001194
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001195 if (!dir) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001196 pr_err("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001197 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001198 }
1199
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001200 while ((dent = readdir(dir)) != NULL) {
1201 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001202
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001203 if (dent->d_type == DT_DIR) {
1204 if (!strcmp(dent->d_name, ".") ||
1205 !strcmp(dent->d_name, ".."))
1206 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001207
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001208 snprintf(path, sizeof(path), "%s/%s",
1209 dirname, dent->d_name);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001210 err = dsos__load_modules_sym_dir(path, filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001211 if (err < 0)
1212 goto failure;
1213 } else {
1214 char *dot = strrchr(dent->d_name, '.'),
1215 dso_name[PATH_MAX];
1216 struct map *map;
1217 struct rb_node *last;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001218 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001219
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001220 if (dot == NULL || strcmp(dot, ".ko"))
1221 continue;
1222 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1223 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001224
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001225 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001226 map = kernel_maps__find_by_dso_name(dso_name);
1227 if (map == NULL)
1228 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001229
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001230 snprintf(path, sizeof(path), "%s/%s",
1231 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001232
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001233 long_name = strdup(path);
1234 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001235 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001236 dso__set_long_name(map->dso, long_name);
1237 dso__set_basename(map->dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001238
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001239 err = dso__load_module_sym(map->dso, map, filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001240 if (err < 0)
1241 goto failure;
1242 last = rb_last(&map->dso->syms);
1243 if (last) {
1244 struct symbol *sym;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001245 /*
1246 * We do this here as well, even having the
1247 * symbol size found in the symtab because
1248 * misannotated ASM symbols may have the size
1249 * set to zero.
1250 */
1251 dso__fixup_sym_end(map->dso);
1252
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001253 sym = rb_entry(last, struct symbol, rb_node);
1254 map->end = map->start + sym->end;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001255 }
1256 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001257 nr_symbols += err;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001258 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001259
1260 return nr_symbols;
1261failure:
1262 closedir(dir);
1263 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001264}
1265
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001266int dsos__load_modules_sym(symbol_filter_t filter)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001267{
1268 struct utsname uts;
1269 char modules_path[PATH_MAX];
1270
1271 if (uname(&uts) < 0)
1272 return -1;
1273
1274 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1275 uts.release);
1276
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001277 return dsos__load_modules_sym_dir(modules_path, filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001278}
1279
1280/*
1281 * Constructor variant for modules (where we know from /proc/modules where
1282 * they are loaded) and for vmlinux, where only after we load all the
1283 * symbols we'll know where it starts and ends.
1284 */
1285static struct map *map__new2(u64 start, struct dso *dso)
1286{
1287 struct map *self = malloc(sizeof(*self));
1288
1289 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001290 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001291 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001292 */
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001293 map__init(self, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001294 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001295
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001296 return self;
1297}
1298
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001299int dsos__load_modules(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001300{
1301 char *line = NULL;
1302 size_t n;
1303 FILE *file = fopen("/proc/modules", "r");
1304 struct map *map;
1305
1306 if (file == NULL)
1307 return -1;
1308
1309 while (!feof(file)) {
1310 char name[PATH_MAX];
1311 u64 start;
1312 struct dso *dso;
1313 char *sep;
1314 int line_len;
1315
1316 line_len = getline(&line, &n, file);
1317 if (line_len < 0)
1318 break;
1319
1320 if (!line)
1321 goto out_failure;
1322
1323 line[--line_len] = '\0'; /* \n */
1324
1325 sep = strrchr(line, 'x');
1326 if (sep == NULL)
1327 continue;
1328
1329 hex2u64(sep + 1, &start);
1330
1331 sep = strchr(line, ' ');
1332 if (sep == NULL)
1333 continue;
1334
1335 *sep = '\0';
1336
1337 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001338 dso = dso__new(name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001339
1340 if (dso == NULL)
1341 goto out_delete_line;
1342
1343 map = map__new2(start, dso);
1344 if (map == NULL) {
1345 dso__delete(dso);
1346 goto out_delete_line;
1347 }
1348
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001349 snprintf(name, sizeof(name),
1350 "/sys/module/%s/notes/.note.gnu.build-id", line);
1351 if (sysfs__read_build_id(name, dso->build_id,
1352 sizeof(dso->build_id)) == 0)
1353 dso->has_build_id = true;
1354
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001355 dso->origin = DSO__ORIG_KMODULE;
1356 kernel_maps__insert(map);
1357 dsos__add(dso);
1358 }
1359
1360 free(line);
1361 fclose(file);
1362
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001363 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001364
1365out_delete_line:
1366 free(line);
1367out_failure:
1368 return -1;
1369}
1370
1371static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001372 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001373{
1374 int err, fd = open(vmlinux, O_RDONLY);
1375
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001376 self->loaded = 1;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001377
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001378 if (fd < 0)
1379 return -1;
1380
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 Melo6671cb12009-11-20 20:51:24 -02001388int dso__load_kernel_sym(struct dso *self, symbol_filter_t filter,
1389 int use_modules)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001390{
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001391 int err;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001392
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001393 kernel_map = map__new2(0, self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001394 if (kernel_map == NULL)
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001395 return -1;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001396
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001397 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001398
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001399 err = dso__load_vmlinux(self, kernel_map, self->name, filter);
Mike Galbraith9974f492009-07-02 08:05:58 +02001400 if (err <= 0)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001401 err = kernel_maps__load_kallsyms(filter, use_modules);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001402
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001403 if (err > 0) {
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001404 struct rb_node *node = rb_first(&self->syms);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001405 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1406
1407 kernel_map->start = sym->start;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001408 node = rb_last(&self->syms);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001409 sym = rb_entry(node, struct symbol, rb_node);
1410 kernel_map->end = sym->end;
1411
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001412 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001413 kernel_maps__insert(kernel_map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001414 /*
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001415 * Now that we have all sorted out, just set the ->end of all
1416 * maps:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001417 */
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001418 kernel_maps__fixup_end();
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001419
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001420 if (verbose)
1421 kernel_maps__fprintf(stderr);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001422 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001423
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001424 return err;
1425}
1426
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001427LIST_HEAD(dsos);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001428struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001429
Ingo Molnar83a09442009-08-15 12:26:57 +02001430const char *vmlinux_name = "vmlinux";
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001431
1432static void dsos__add(struct dso *dso)
1433{
1434 list_add_tail(&dso->node, &dsos);
1435}
1436
1437static struct dso *dsos__find(const char *name)
1438{
1439 struct dso *pos;
1440
1441 list_for_each_entry(pos, &dsos, node)
1442 if (strcmp(pos->name, name) == 0)
1443 return pos;
1444 return NULL;
1445}
1446
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001447struct dso *dsos__findnew(const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001448{
1449 struct dso *dso = dsos__find(name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001450
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001451 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001452 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001453 if (dso != NULL) {
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001454 dsos__add(dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001455 dso__set_basename(dso);
1456 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001457 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001458
1459 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001460}
1461
1462void dsos__fprintf(FILE *fp)
1463{
1464 struct dso *pos;
1465
1466 list_for_each_entry(pos, &dsos, node)
1467 dso__fprintf(pos, fp);
1468}
1469
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001470size_t dsos__fprintf_buildid(FILE *fp)
1471{
1472 struct dso *pos;
1473 size_t ret = 0;
1474
1475 list_for_each_entry(pos, &dsos, node) {
1476 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001477 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001478 }
1479 return ret;
1480}
1481
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001482struct dso *dsos__load_kernel(void)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001483{
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001484 struct dso *kernel = dso__new(vmlinux_name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001485
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001486 if (kernel == NULL)
1487 return NULL;
1488
1489 kernel->short_name = "[kernel]";
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001490 vdso = dso__new("[vdso]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001491 if (!vdso)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001492 return NULL;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001493
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001494 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1495 sizeof(kernel->build_id)) == 0)
1496 kernel->has_build_id = true;
1497
1498 dsos__add(kernel);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001499 dsos__add(vdso);
1500
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001501 return kernel;
1502}
1503
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001504int load_kernel(symbol_filter_t filter, bool use_modules)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001505{
1506 struct dso *kernel = dsos__load_kernel();
1507
1508 if (kernel == NULL)
1509 return -1;
1510
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001511 if (use_modules) {
1512 if (dsos__load_modules() < 0)
1513 pr_warning("Failed to load list of modules in use, "
1514 "continuing...\n");
1515 else if (dsos__load_modules_sym(filter) < 0)
1516 pr_warning("Failed to read module symbols, "
1517 "continuing...\n");
1518 }
1519
1520 if (dso__load_kernel_sym(kernel, filter, use_modules) < 0)
1521 pr_warning("Failed to read kernel symbols, continuing...\n");
1522
1523 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001524}
1525
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001526void symbol__init(unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001527{
1528 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001529 symbol__priv_size = priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001530}