symbol.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. #define _GNU_SOURCE
  2. #include <ctype.h>
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <libgen.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/param.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include "symbol.h"
  15. #include "strlist.h"
  16. #include <libelf.h>
  17. #include <gelf.h>
  18. #include <elf.h>
  19. #include <limits.h>
  20. #include <sys/utsname.h>
  21. #ifndef NT_GNU_BUILD_ID
  22. #define NT_GNU_BUILD_ID 3
  23. #endif
  24. static void dsos__add(struct list_head *head, struct dso *dso);
  25. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  26. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  27. symbol_filter_t filter);
  28. static int vmlinux_path__nr_entries;
  29. static char **vmlinux_path;
  30. struct symbol_conf symbol_conf = {
  31. .exclude_other = true,
  32. .use_modules = true,
  33. .try_vmlinux_path = true,
  34. };
  35. bool dso__loaded(const struct dso *self, enum map_type type)
  36. {
  37. return self->loaded & (1 << type);
  38. }
  39. bool dso__sorted_by_name(const struct dso *self, enum map_type type)
  40. {
  41. return self->sorted_by_name & (1 << type);
  42. }
  43. static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
  44. {
  45. self->sorted_by_name |= (1 << type);
  46. }
  47. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  48. {
  49. switch (map_type) {
  50. case MAP__FUNCTION:
  51. return symbol_type == 'T' || symbol_type == 'W';
  52. case MAP__VARIABLE:
  53. return symbol_type == 'D' || symbol_type == 'd';
  54. default:
  55. return false;
  56. }
  57. }
  58. static void symbols__fixup_end(struct rb_root *self)
  59. {
  60. struct rb_node *nd, *prevnd = rb_first(self);
  61. struct symbol *curr, *prev;
  62. if (prevnd == NULL)
  63. return;
  64. curr = rb_entry(prevnd, struct symbol, rb_node);
  65. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  66. prev = curr;
  67. curr = rb_entry(nd, struct symbol, rb_node);
  68. if (prev->end == prev->start)
  69. prev->end = curr->start - 1;
  70. }
  71. /* Last entry */
  72. if (curr->end == curr->start)
  73. curr->end = roundup(curr->start, 4096);
  74. }
  75. static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
  76. {
  77. struct map *prev, *curr;
  78. struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
  79. if (prevnd == NULL)
  80. return;
  81. curr = rb_entry(prevnd, struct map, rb_node);
  82. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  83. prev = curr;
  84. curr = rb_entry(nd, struct map, rb_node);
  85. prev->end = curr->start - 1;
  86. }
  87. /*
  88. * We still haven't the actual symbols, so guess the
  89. * last map final address.
  90. */
  91. curr->end = ~0UL;
  92. }
  93. static void map_groups__fixup_end(struct map_groups *self)
  94. {
  95. int i;
  96. for (i = 0; i < MAP__NR_TYPES; ++i)
  97. __map_groups__fixup_end(self, i);
  98. }
  99. static struct symbol *symbol__new(u64 start, u64 len, const char *name)
  100. {
  101. size_t namelen = strlen(name) + 1;
  102. struct symbol *self = calloc(1, (symbol_conf.priv_size +
  103. sizeof(*self) + namelen));
  104. if (self == NULL)
  105. return NULL;
  106. if (symbol_conf.priv_size)
  107. self = ((void *)self) + symbol_conf.priv_size;
  108. self->start = start;
  109. self->end = len ? start + len - 1 : start;
  110. pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
  111. memcpy(self->name, name, namelen);
  112. return self;
  113. }
  114. void symbol__delete(struct symbol *self)
  115. {
  116. free(((void *)self) - symbol_conf.priv_size);
  117. }
  118. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  119. {
  120. return fprintf(fp, " %llx-%llx %s\n",
  121. self->start, self->end, self->name);
  122. }
  123. void dso__set_long_name(struct dso *self, char *name)
  124. {
  125. if (name == NULL)
  126. return;
  127. self->long_name = name;
  128. self->long_name_len = strlen(name);
  129. }
  130. static void dso__set_short_name(struct dso *self, const char *name)
  131. {
  132. if (name == NULL)
  133. return;
  134. self->short_name = name;
  135. self->short_name_len = strlen(name);
  136. }
  137. static void dso__set_basename(struct dso *self)
  138. {
  139. dso__set_short_name(self, basename(self->long_name));
  140. }
  141. struct dso *dso__new(const char *name)
  142. {
  143. struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
  144. if (self != NULL) {
  145. int i;
  146. strcpy(self->name, name);
  147. dso__set_long_name(self, self->name);
  148. dso__set_short_name(self, self->name);
  149. for (i = 0; i < MAP__NR_TYPES; ++i)
  150. self->symbols[i] = self->symbol_names[i] = RB_ROOT;
  151. self->slen_calculated = 0;
  152. self->origin = DSO__ORIG_NOT_FOUND;
  153. self->loaded = 0;
  154. self->sorted_by_name = 0;
  155. self->has_build_id = 0;
  156. }
  157. return self;
  158. }
  159. static void symbols__delete(struct rb_root *self)
  160. {
  161. struct symbol *pos;
  162. struct rb_node *next = rb_first(self);
  163. while (next) {
  164. pos = rb_entry(next, struct symbol, rb_node);
  165. next = rb_next(&pos->rb_node);
  166. rb_erase(&pos->rb_node, self);
  167. symbol__delete(pos);
  168. }
  169. }
  170. void dso__delete(struct dso *self)
  171. {
  172. int i;
  173. for (i = 0; i < MAP__NR_TYPES; ++i)
  174. symbols__delete(&self->symbols[i]);
  175. if (self->long_name != self->name)
  176. free(self->long_name);
  177. free(self);
  178. }
  179. void dso__set_build_id(struct dso *self, void *build_id)
  180. {
  181. memcpy(self->build_id, build_id, sizeof(self->build_id));
  182. self->has_build_id = 1;
  183. }
  184. static void symbols__insert(struct rb_root *self, struct symbol *sym)
  185. {
  186. struct rb_node **p = &self->rb_node;
  187. struct rb_node *parent = NULL;
  188. const u64 ip = sym->start;
  189. struct symbol *s;
  190. while (*p != NULL) {
  191. parent = *p;
  192. s = rb_entry(parent, struct symbol, rb_node);
  193. if (ip < s->start)
  194. p = &(*p)->rb_left;
  195. else
  196. p = &(*p)->rb_right;
  197. }
  198. rb_link_node(&sym->rb_node, parent, p);
  199. rb_insert_color(&sym->rb_node, self);
  200. }
  201. static struct symbol *symbols__find(struct rb_root *self, u64 ip)
  202. {
  203. struct rb_node *n;
  204. if (self == NULL)
  205. return NULL;
  206. n = self->rb_node;
  207. while (n) {
  208. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  209. if (ip < s->start)
  210. n = n->rb_left;
  211. else if (ip > s->end)
  212. n = n->rb_right;
  213. else
  214. return s;
  215. }
  216. return NULL;
  217. }
  218. struct symbol_name_rb_node {
  219. struct rb_node rb_node;
  220. struct symbol sym;
  221. };
  222. static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
  223. {
  224. struct rb_node **p = &self->rb_node;
  225. struct rb_node *parent = NULL;
  226. struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
  227. while (*p != NULL) {
  228. parent = *p;
  229. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  230. if (strcmp(sym->name, s->sym.name) < 0)
  231. p = &(*p)->rb_left;
  232. else
  233. p = &(*p)->rb_right;
  234. }
  235. rb_link_node(&symn->rb_node, parent, p);
  236. rb_insert_color(&symn->rb_node, self);
  237. }
  238. static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
  239. {
  240. struct rb_node *nd;
  241. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  242. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  243. symbols__insert_by_name(self, pos);
  244. }
  245. }
  246. static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
  247. {
  248. struct rb_node *n;
  249. if (self == NULL)
  250. return NULL;
  251. n = self->rb_node;
  252. while (n) {
  253. struct symbol_name_rb_node *s;
  254. int cmp;
  255. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  256. cmp = strcmp(name, s->sym.name);
  257. if (cmp < 0)
  258. n = n->rb_left;
  259. else if (cmp > 0)
  260. n = n->rb_right;
  261. else
  262. return &s->sym;
  263. }
  264. return NULL;
  265. }
  266. struct symbol *dso__find_symbol(struct dso *self,
  267. enum map_type type, u64 addr)
  268. {
  269. return symbols__find(&self->symbols[type], addr);
  270. }
  271. struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
  272. const char *name)
  273. {
  274. return symbols__find_by_name(&self->symbol_names[type], name);
  275. }
  276. void dso__sort_by_name(struct dso *self, enum map_type type)
  277. {
  278. dso__set_sorted_by_name(self, type);
  279. return symbols__sort_by_name(&self->symbol_names[type],
  280. &self->symbols[type]);
  281. }
  282. int build_id__sprintf(const u8 *self, int len, char *bf)
  283. {
  284. char *bid = bf;
  285. const u8 *raw = self;
  286. int i;
  287. for (i = 0; i < len; ++i) {
  288. sprintf(bid, "%02x", *raw);
  289. ++raw;
  290. bid += 2;
  291. }
  292. return raw - self;
  293. }
  294. size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
  295. {
  296. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  297. build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
  298. return fprintf(fp, "%s", sbuild_id);
  299. }
  300. size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
  301. {
  302. struct rb_node *nd;
  303. size_t ret = fprintf(fp, "dso: %s (", self->short_name);
  304. if (self->short_name != self->long_name)
  305. ret += fprintf(fp, "%s, ", self->long_name);
  306. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  307. self->loaded ? "" : "NOT ");
  308. ret += dso__fprintf_buildid(self, fp);
  309. ret += fprintf(fp, ")\n");
  310. for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
  311. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  312. ret += symbol__fprintf(pos, fp);
  313. }
  314. return ret;
  315. }
  316. int kallsyms__parse(const char *filename, void *arg,
  317. int (*process_symbol)(void *arg, const char *name,
  318. char type, u64 start))
  319. {
  320. char *line = NULL;
  321. size_t n;
  322. int err = 0;
  323. FILE *file = fopen(filename, "r");
  324. if (file == NULL)
  325. goto out_failure;
  326. while (!feof(file)) {
  327. u64 start;
  328. int line_len, len;
  329. char symbol_type;
  330. char *symbol_name;
  331. line_len = getline(&line, &n, file);
  332. if (line_len < 0)
  333. break;
  334. if (!line)
  335. goto out_failure;
  336. line[--line_len] = '\0'; /* \n */
  337. len = hex2u64(line, &start);
  338. len++;
  339. if (len + 2 >= line_len)
  340. continue;
  341. symbol_type = toupper(line[len]);
  342. symbol_name = line + len + 2;
  343. err = process_symbol(arg, symbol_name, symbol_type, start);
  344. if (err)
  345. break;
  346. }
  347. free(line);
  348. fclose(file);
  349. return err;
  350. out_failure:
  351. return -1;
  352. }
  353. struct process_kallsyms_args {
  354. struct map *map;
  355. struct dso *dso;
  356. };
  357. static int map__process_kallsym_symbol(void *arg, const char *name,
  358. char type, u64 start)
  359. {
  360. struct symbol *sym;
  361. struct process_kallsyms_args *a = arg;
  362. struct rb_root *root = &a->dso->symbols[a->map->type];
  363. if (!symbol_type__is_a(type, a->map->type))
  364. return 0;
  365. /*
  366. * Will fix up the end later, when we have all symbols sorted.
  367. */
  368. sym = symbol__new(start, 0, name);
  369. if (sym == NULL)
  370. return -ENOMEM;
  371. /*
  372. * We will pass the symbols to the filter later, in
  373. * map__split_kallsyms, when we have split the maps per module
  374. */
  375. symbols__insert(root, sym);
  376. return 0;
  377. }
  378. /*
  379. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  380. * so that we can in the next step set the symbol ->end address and then
  381. * call kernel_maps__split_kallsyms.
  382. */
  383. static int dso__load_all_kallsyms(struct dso *self, const char *filename,
  384. struct map *map)
  385. {
  386. struct process_kallsyms_args args = { .map = map, .dso = self, };
  387. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  388. }
  389. /*
  390. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  391. * kernel range is broken in several maps, named [kernel].N, as we don't have
  392. * the original ELF section names vmlinux have.
  393. */
  394. static int dso__split_kallsyms(struct dso *self, struct map *map,
  395. symbol_filter_t filter)
  396. {
  397. struct map_groups *kmaps = map__kmap(map)->kmaps;
  398. struct map *curr_map = map;
  399. struct symbol *pos;
  400. int count = 0;
  401. struct rb_root *root = &self->symbols[map->type];
  402. struct rb_node *next = rb_first(root);
  403. int kernel_range = 0;
  404. while (next) {
  405. char *module;
  406. pos = rb_entry(next, struct symbol, rb_node);
  407. next = rb_next(&pos->rb_node);
  408. module = strchr(pos->name, '\t');
  409. if (module) {
  410. if (!symbol_conf.use_modules)
  411. goto discard_symbol;
  412. *module++ = '\0';
  413. if (strcmp(curr_map->dso->short_name, module)) {
  414. curr_map = map_groups__find_by_name(kmaps, map->type, module);
  415. if (curr_map == NULL) {
  416. pr_debug("/proc/{kallsyms,modules} "
  417. "inconsistency while looking "
  418. "for \"%s\" module!\n", module);
  419. return -1;
  420. }
  421. if (curr_map->dso->loaded)
  422. goto discard_symbol;
  423. }
  424. /*
  425. * So that we look just like we get from .ko files,
  426. * i.e. not prelinked, relative to map->start.
  427. */
  428. pos->start = curr_map->map_ip(curr_map, pos->start);
  429. pos->end = curr_map->map_ip(curr_map, pos->end);
  430. } else if (curr_map != map) {
  431. char dso_name[PATH_MAX];
  432. struct dso *dso;
  433. snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
  434. kernel_range++);
  435. dso = dso__new(dso_name);
  436. if (dso == NULL)
  437. return -1;
  438. curr_map = map__new2(pos->start, dso, map->type);
  439. if (curr_map == NULL) {
  440. dso__delete(dso);
  441. return -1;
  442. }
  443. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  444. map_groups__insert(kmaps, curr_map);
  445. ++kernel_range;
  446. }
  447. if (filter && filter(curr_map, pos)) {
  448. discard_symbol: rb_erase(&pos->rb_node, root);
  449. symbol__delete(pos);
  450. } else {
  451. if (curr_map != map) {
  452. rb_erase(&pos->rb_node, root);
  453. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  454. }
  455. count++;
  456. }
  457. }
  458. return count;
  459. }
  460. int dso__load_kallsyms(struct dso *self, const char *filename,
  461. struct map *map, symbol_filter_t filter)
  462. {
  463. if (dso__load_all_kallsyms(self, filename, map) < 0)
  464. return -1;
  465. symbols__fixup_end(&self->symbols[map->type]);
  466. self->origin = DSO__ORIG_KERNEL;
  467. return dso__split_kallsyms(self, map, filter);
  468. }
  469. static int dso__load_perf_map(struct dso *self, struct map *map,
  470. symbol_filter_t filter)
  471. {
  472. char *line = NULL;
  473. size_t n;
  474. FILE *file;
  475. int nr_syms = 0;
  476. file = fopen(self->long_name, "r");
  477. if (file == NULL)
  478. goto out_failure;
  479. while (!feof(file)) {
  480. u64 start, size;
  481. struct symbol *sym;
  482. int line_len, len;
  483. line_len = getline(&line, &n, file);
  484. if (line_len < 0)
  485. break;
  486. if (!line)
  487. goto out_failure;
  488. line[--line_len] = '\0'; /* \n */
  489. len = hex2u64(line, &start);
  490. len++;
  491. if (len + 2 >= line_len)
  492. continue;
  493. len += hex2u64(line + len, &size);
  494. len++;
  495. if (len + 2 >= line_len)
  496. continue;
  497. sym = symbol__new(start, size, line + len);
  498. if (sym == NULL)
  499. goto out_delete_line;
  500. if (filter && filter(map, sym))
  501. symbol__delete(sym);
  502. else {
  503. symbols__insert(&self->symbols[map->type], sym);
  504. nr_syms++;
  505. }
  506. }
  507. free(line);
  508. fclose(file);
  509. return nr_syms;
  510. out_delete_line:
  511. free(line);
  512. out_failure:
  513. return -1;
  514. }
  515. /**
  516. * elf_symtab__for_each_symbol - iterate thru all the symbols
  517. *
  518. * @self: struct elf_symtab instance to iterate
  519. * @idx: uint32_t idx
  520. * @sym: GElf_Sym iterator
  521. */
  522. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  523. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  524. idx < nr_syms; \
  525. idx++, gelf_getsym(syms, idx, &sym))
  526. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  527. {
  528. return GELF_ST_TYPE(sym->st_info);
  529. }
  530. static inline int elf_sym__is_function(const GElf_Sym *sym)
  531. {
  532. return elf_sym__type(sym) == STT_FUNC &&
  533. sym->st_name != 0 &&
  534. sym->st_shndx != SHN_UNDEF;
  535. }
  536. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  537. {
  538. return elf_sym__type(sym) == STT_OBJECT &&
  539. sym->st_name != 0 &&
  540. sym->st_shndx != SHN_UNDEF;
  541. }
  542. static inline int elf_sym__is_label(const GElf_Sym *sym)
  543. {
  544. return elf_sym__type(sym) == STT_NOTYPE &&
  545. sym->st_name != 0 &&
  546. sym->st_shndx != SHN_UNDEF &&
  547. sym->st_shndx != SHN_ABS;
  548. }
  549. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  550. const Elf_Data *secstrs)
  551. {
  552. return secstrs->d_buf + shdr->sh_name;
  553. }
  554. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  555. const Elf_Data *secstrs)
  556. {
  557. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  558. }
  559. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  560. const Elf_Data *secstrs)
  561. {
  562. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  563. }
  564. static inline const char *elf_sym__name(const GElf_Sym *sym,
  565. const Elf_Data *symstrs)
  566. {
  567. return symstrs->d_buf + sym->st_name;
  568. }
  569. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  570. GElf_Shdr *shp, const char *name,
  571. size_t *idx)
  572. {
  573. Elf_Scn *sec = NULL;
  574. size_t cnt = 1;
  575. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  576. char *str;
  577. gelf_getshdr(sec, shp);
  578. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  579. if (!strcmp(name, str)) {
  580. if (idx)
  581. *idx = cnt;
  582. break;
  583. }
  584. ++cnt;
  585. }
  586. return sec;
  587. }
  588. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  589. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  590. idx < nr_entries; \
  591. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  592. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  593. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  594. idx < nr_entries; \
  595. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  596. /*
  597. * We need to check if we have a .dynsym, so that we can handle the
  598. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  599. * .dynsym or .symtab).
  600. * And always look at the original dso, not at debuginfo packages, that
  601. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  602. */
  603. static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
  604. symbol_filter_t filter)
  605. {
  606. uint32_t nr_rel_entries, idx;
  607. GElf_Sym sym;
  608. u64 plt_offset;
  609. GElf_Shdr shdr_plt;
  610. struct symbol *f;
  611. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  612. Elf_Data *reldata, *syms, *symstrs;
  613. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  614. size_t dynsym_idx;
  615. GElf_Ehdr ehdr;
  616. char sympltname[1024];
  617. Elf *elf;
  618. int nr = 0, symidx, fd, err = 0;
  619. fd = open(self->long_name, O_RDONLY);
  620. if (fd < 0)
  621. goto out;
  622. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  623. if (elf == NULL)
  624. goto out_close;
  625. if (gelf_getehdr(elf, &ehdr) == NULL)
  626. goto out_elf_end;
  627. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  628. ".dynsym", &dynsym_idx);
  629. if (scn_dynsym == NULL)
  630. goto out_elf_end;
  631. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  632. ".rela.plt", NULL);
  633. if (scn_plt_rel == NULL) {
  634. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  635. ".rel.plt", NULL);
  636. if (scn_plt_rel == NULL)
  637. goto out_elf_end;
  638. }
  639. err = -1;
  640. if (shdr_rel_plt.sh_link != dynsym_idx)
  641. goto out_elf_end;
  642. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  643. goto out_elf_end;
  644. /*
  645. * Fetch the relocation section to find the idxes to the GOT
  646. * and the symbols in the .dynsym they refer to.
  647. */
  648. reldata = elf_getdata(scn_plt_rel, NULL);
  649. if (reldata == NULL)
  650. goto out_elf_end;
  651. syms = elf_getdata(scn_dynsym, NULL);
  652. if (syms == NULL)
  653. goto out_elf_end;
  654. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  655. if (scn_symstrs == NULL)
  656. goto out_elf_end;
  657. symstrs = elf_getdata(scn_symstrs, NULL);
  658. if (symstrs == NULL)
  659. goto out_elf_end;
  660. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  661. plt_offset = shdr_plt.sh_offset;
  662. if (shdr_rel_plt.sh_type == SHT_RELA) {
  663. GElf_Rela pos_mem, *pos;
  664. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  665. nr_rel_entries) {
  666. symidx = GELF_R_SYM(pos->r_info);
  667. plt_offset += shdr_plt.sh_entsize;
  668. gelf_getsym(syms, symidx, &sym);
  669. snprintf(sympltname, sizeof(sympltname),
  670. "%s@plt", elf_sym__name(&sym, symstrs));
  671. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  672. sympltname);
  673. if (!f)
  674. goto out_elf_end;
  675. if (filter && filter(map, f))
  676. symbol__delete(f);
  677. else {
  678. symbols__insert(&self->symbols[map->type], f);
  679. ++nr;
  680. }
  681. }
  682. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  683. GElf_Rel pos_mem, *pos;
  684. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  685. nr_rel_entries) {
  686. symidx = GELF_R_SYM(pos->r_info);
  687. plt_offset += shdr_plt.sh_entsize;
  688. gelf_getsym(syms, symidx, &sym);
  689. snprintf(sympltname, sizeof(sympltname),
  690. "%s@plt", elf_sym__name(&sym, symstrs));
  691. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  692. sympltname);
  693. if (!f)
  694. goto out_elf_end;
  695. if (filter && filter(map, f))
  696. symbol__delete(f);
  697. else {
  698. symbols__insert(&self->symbols[map->type], f);
  699. ++nr;
  700. }
  701. }
  702. }
  703. err = 0;
  704. out_elf_end:
  705. elf_end(elf);
  706. out_close:
  707. close(fd);
  708. if (err == 0)
  709. return nr;
  710. out:
  711. pr_debug("%s: problems reading %s PLT info.\n",
  712. __func__, self->long_name);
  713. return 0;
  714. }
  715. static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
  716. {
  717. switch (type) {
  718. case MAP__FUNCTION:
  719. return elf_sym__is_function(self);
  720. case MAP__VARIABLE:
  721. return elf_sym__is_object(self);
  722. default:
  723. return false;
  724. }
  725. }
  726. static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
  727. {
  728. switch (type) {
  729. case MAP__FUNCTION:
  730. return elf_sec__is_text(self, secstrs);
  731. case MAP__VARIABLE:
  732. return elf_sec__is_data(self, secstrs);
  733. default:
  734. return false;
  735. }
  736. }
  737. static int dso__load_sym(struct dso *self, struct map *map, const char *name,
  738. int fd, symbol_filter_t filter, int kmodule)
  739. {
  740. struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
  741. struct map *curr_map = map;
  742. struct dso *curr_dso = self;
  743. Elf_Data *symstrs, *secstrs;
  744. uint32_t nr_syms;
  745. int err = -1;
  746. uint32_t idx;
  747. GElf_Ehdr ehdr;
  748. GElf_Shdr shdr;
  749. Elf_Data *syms;
  750. GElf_Sym sym;
  751. Elf_Scn *sec, *sec_strndx;
  752. Elf *elf;
  753. int nr = 0;
  754. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  755. if (elf == NULL) {
  756. pr_err("%s: cannot read %s ELF file.\n", __func__, name);
  757. goto out_close;
  758. }
  759. if (gelf_getehdr(elf, &ehdr) == NULL) {
  760. pr_err("%s: cannot get elf header.\n", __func__);
  761. goto out_elf_end;
  762. }
  763. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  764. if (sec == NULL) {
  765. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  766. if (sec == NULL)
  767. goto out_elf_end;
  768. }
  769. syms = elf_getdata(sec, NULL);
  770. if (syms == NULL)
  771. goto out_elf_end;
  772. sec = elf_getscn(elf, shdr.sh_link);
  773. if (sec == NULL)
  774. goto out_elf_end;
  775. symstrs = elf_getdata(sec, NULL);
  776. if (symstrs == NULL)
  777. goto out_elf_end;
  778. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  779. if (sec_strndx == NULL)
  780. goto out_elf_end;
  781. secstrs = elf_getdata(sec_strndx, NULL);
  782. if (secstrs == NULL)
  783. goto out_elf_end;
  784. nr_syms = shdr.sh_size / shdr.sh_entsize;
  785. memset(&sym, 0, sizeof(sym));
  786. if (!self->kernel) {
  787. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  788. elf_section_by_name(elf, &ehdr, &shdr,
  789. ".gnu.prelink_undo",
  790. NULL) != NULL);
  791. } else self->adjust_symbols = 0;
  792. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  793. struct symbol *f;
  794. const char *elf_name = elf_sym__name(&sym, symstrs);
  795. char *demangled = NULL;
  796. int is_label = elf_sym__is_label(&sym);
  797. const char *section_name;
  798. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  799. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  800. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  801. if (!is_label && !elf_sym__is_a(&sym, map->type))
  802. continue;
  803. sec = elf_getscn(elf, sym.st_shndx);
  804. if (!sec)
  805. goto out_elf_end;
  806. gelf_getshdr(sec, &shdr);
  807. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  808. continue;
  809. section_name = elf_sec__name(&shdr, secstrs);
  810. if (self->kernel || kmodule) {
  811. char dso_name[PATH_MAX];
  812. if (strcmp(section_name,
  813. (curr_dso->short_name +
  814. self->short_name_len)) == 0)
  815. goto new_symbol;
  816. if (strcmp(section_name, ".text") == 0) {
  817. curr_map = map;
  818. curr_dso = self;
  819. goto new_symbol;
  820. }
  821. snprintf(dso_name, sizeof(dso_name),
  822. "%s%s", self->short_name, section_name);
  823. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  824. if (curr_map == NULL) {
  825. u64 start = sym.st_value;
  826. if (kmodule)
  827. start += map->start + shdr.sh_offset;
  828. curr_dso = dso__new(dso_name);
  829. if (curr_dso == NULL)
  830. goto out_elf_end;
  831. curr_map = map__new2(start, curr_dso,
  832. map->type);
  833. if (curr_map == NULL) {
  834. dso__delete(curr_dso);
  835. goto out_elf_end;
  836. }
  837. curr_map->map_ip = identity__map_ip;
  838. curr_map->unmap_ip = identity__map_ip;
  839. curr_dso->origin = self->origin;
  840. map_groups__insert(kmap->kmaps, curr_map);
  841. dsos__add(&dsos__kernel, curr_dso);
  842. dso__set_loaded(curr_dso, map->type);
  843. } else
  844. curr_dso = curr_map->dso;
  845. goto new_symbol;
  846. }
  847. if (curr_dso->adjust_symbols) {
  848. pr_debug4("%s: adjusting symbol: st_value: %#Lx "
  849. "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
  850. (u64)sym.st_value, (u64)shdr.sh_addr,
  851. (u64)shdr.sh_offset);
  852. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  853. }
  854. /*
  855. * We need to figure out if the object was created from C++ sources
  856. * DWARF DW_compile_unit has this, but we don't always have access
  857. * to it...
  858. */
  859. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  860. if (demangled != NULL)
  861. elf_name = demangled;
  862. new_symbol:
  863. f = symbol__new(sym.st_value, sym.st_size, elf_name);
  864. free(demangled);
  865. if (!f)
  866. goto out_elf_end;
  867. if (filter && filter(curr_map, f))
  868. symbol__delete(f);
  869. else {
  870. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  871. nr++;
  872. }
  873. }
  874. /*
  875. * For misannotated, zeroed, ASM function sizes.
  876. */
  877. if (nr > 0) {
  878. symbols__fixup_end(&self->symbols[map->type]);
  879. if (kmap) {
  880. /*
  881. * We need to fixup this here too because we create new
  882. * maps here, for things like vsyscall sections.
  883. */
  884. __map_groups__fixup_end(kmap->kmaps, map->type);
  885. }
  886. }
  887. err = nr;
  888. out_elf_end:
  889. elf_end(elf);
  890. out_close:
  891. return err;
  892. }
  893. static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
  894. {
  895. return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
  896. }
  897. static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  898. {
  899. bool have_build_id = false;
  900. struct dso *pos;
  901. list_for_each_entry(pos, head, node) {
  902. if (with_hits && !pos->hit)
  903. continue;
  904. if (filename__read_build_id(pos->long_name, pos->build_id,
  905. sizeof(pos->build_id)) > 0) {
  906. have_build_id = true;
  907. pos->has_build_id = true;
  908. }
  909. }
  910. return have_build_id;
  911. }
  912. bool dsos__read_build_ids(bool with_hits)
  913. {
  914. bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
  915. ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
  916. return kbuildids || ubuildids;
  917. }
  918. /*
  919. * Align offset to 4 bytes as needed for note name and descriptor data.
  920. */
  921. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  922. int filename__read_build_id(const char *filename, void *bf, size_t size)
  923. {
  924. int fd, err = -1;
  925. GElf_Ehdr ehdr;
  926. GElf_Shdr shdr;
  927. Elf_Data *data;
  928. Elf_Scn *sec;
  929. Elf_Kind ek;
  930. void *ptr;
  931. Elf *elf;
  932. if (size < BUILD_ID_SIZE)
  933. goto out;
  934. fd = open(filename, O_RDONLY);
  935. if (fd < 0)
  936. goto out;
  937. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  938. if (elf == NULL) {
  939. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  940. goto out_close;
  941. }
  942. ek = elf_kind(elf);
  943. if (ek != ELF_K_ELF)
  944. goto out_elf_end;
  945. if (gelf_getehdr(elf, &ehdr) == NULL) {
  946. pr_err("%s: cannot get elf header.\n", __func__);
  947. goto out_elf_end;
  948. }
  949. sec = elf_section_by_name(elf, &ehdr, &shdr,
  950. ".note.gnu.build-id", NULL);
  951. if (sec == NULL) {
  952. sec = elf_section_by_name(elf, &ehdr, &shdr,
  953. ".notes", NULL);
  954. if (sec == NULL)
  955. goto out_elf_end;
  956. }
  957. data = elf_getdata(sec, NULL);
  958. if (data == NULL)
  959. goto out_elf_end;
  960. ptr = data->d_buf;
  961. while (ptr < (data->d_buf + data->d_size)) {
  962. GElf_Nhdr *nhdr = ptr;
  963. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  964. descsz = NOTE_ALIGN(nhdr->n_descsz);
  965. const char *name;
  966. ptr += sizeof(*nhdr);
  967. name = ptr;
  968. ptr += namesz;
  969. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  970. nhdr->n_namesz == sizeof("GNU")) {
  971. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  972. memcpy(bf, ptr, BUILD_ID_SIZE);
  973. err = BUILD_ID_SIZE;
  974. break;
  975. }
  976. }
  977. ptr += descsz;
  978. }
  979. out_elf_end:
  980. elf_end(elf);
  981. out_close:
  982. close(fd);
  983. out:
  984. return err;
  985. }
  986. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  987. {
  988. int fd, err = -1;
  989. if (size < BUILD_ID_SIZE)
  990. goto out;
  991. fd = open(filename, O_RDONLY);
  992. if (fd < 0)
  993. goto out;
  994. while (1) {
  995. char bf[BUFSIZ];
  996. GElf_Nhdr nhdr;
  997. int namesz, descsz;
  998. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  999. break;
  1000. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1001. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1002. 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);
  1021. out:
  1022. return err;
  1023. }
  1024. char dso__symtab_origin(const struct dso *self)
  1025. {
  1026. static const char origin[] = {
  1027. [DSO__ORIG_KERNEL] = 'k',
  1028. [DSO__ORIG_JAVA_JIT] = 'j',
  1029. [DSO__ORIG_BUILD_ID_CACHE] = 'B',
  1030. [DSO__ORIG_FEDORA] = 'f',
  1031. [DSO__ORIG_UBUNTU] = 'u',
  1032. [DSO__ORIG_BUILDID] = 'b',
  1033. [DSO__ORIG_DSO] = 'd',
  1034. [DSO__ORIG_KMODULE] = 'K',
  1035. };
  1036. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  1037. return '!';
  1038. return origin[self->origin];
  1039. }
  1040. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  1041. {
  1042. int size = PATH_MAX;
  1043. char *name;
  1044. u8 build_id[BUILD_ID_SIZE];
  1045. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1046. int ret = -1;
  1047. int fd;
  1048. dso__set_loaded(self, map->type);
  1049. if (self->kernel)
  1050. return dso__load_kernel_sym(self, map, filter);
  1051. name = malloc(size);
  1052. if (!name)
  1053. return -1;
  1054. self->adjust_symbols = 0;
  1055. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  1056. ret = dso__load_perf_map(self, map, filter);
  1057. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  1058. DSO__ORIG_NOT_FOUND;
  1059. return ret;
  1060. }
  1061. self->origin = DSO__ORIG_BUILD_ID_CACHE;
  1062. if (self->has_build_id) {
  1063. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1064. build_id_hex);
  1065. snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
  1066. getenv("HOME"), DEBUG_CACHE_DIR,
  1067. build_id_hex, build_id_hex + 2);
  1068. goto open_file;
  1069. }
  1070. more:
  1071. do {
  1072. self->origin++;
  1073. switch (self->origin) {
  1074. case DSO__ORIG_FEDORA:
  1075. snprintf(name, size, "/usr/lib/debug%s.debug",
  1076. self->long_name);
  1077. break;
  1078. case DSO__ORIG_UBUNTU:
  1079. snprintf(name, size, "/usr/lib/debug%s",
  1080. self->long_name);
  1081. break;
  1082. case DSO__ORIG_BUILDID:
  1083. if (filename__read_build_id(self->long_name, build_id,
  1084. sizeof(build_id))) {
  1085. build_id__sprintf(build_id, sizeof(build_id),
  1086. build_id_hex);
  1087. snprintf(name, size,
  1088. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  1089. build_id_hex, build_id_hex + 2);
  1090. if (self->has_build_id)
  1091. goto compare_build_id;
  1092. break;
  1093. }
  1094. self->origin++;
  1095. /* Fall thru */
  1096. case DSO__ORIG_DSO:
  1097. snprintf(name, size, "%s", self->long_name);
  1098. break;
  1099. default:
  1100. goto out;
  1101. }
  1102. if (self->has_build_id) {
  1103. if (filename__read_build_id(name, build_id,
  1104. sizeof(build_id)) < 0)
  1105. goto more;
  1106. compare_build_id:
  1107. if (!dso__build_id_equal(self, build_id))
  1108. goto more;
  1109. }
  1110. open_file:
  1111. fd = open(name, O_RDONLY);
  1112. } while (fd < 0);
  1113. ret = dso__load_sym(self, map, name, fd, filter, 0);
  1114. close(fd);
  1115. /*
  1116. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  1117. */
  1118. if (!ret)
  1119. goto more;
  1120. if (ret > 0) {
  1121. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  1122. if (nr_plt > 0)
  1123. ret += nr_plt;
  1124. }
  1125. out:
  1126. free(name);
  1127. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  1128. return 0;
  1129. return ret;
  1130. }
  1131. struct map *map_groups__find_by_name(struct map_groups *self,
  1132. enum map_type type, const char *name)
  1133. {
  1134. struct rb_node *nd;
  1135. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  1136. struct map *map = rb_entry(nd, struct map, rb_node);
  1137. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1138. return map;
  1139. }
  1140. return NULL;
  1141. }
  1142. static int dso__kernel_module_get_build_id(struct dso *self)
  1143. {
  1144. char filename[PATH_MAX];
  1145. /*
  1146. * kernel module short names are of the form "[module]" and
  1147. * we need just "module" here.
  1148. */
  1149. const char *name = self->short_name + 1;
  1150. snprintf(filename, sizeof(filename),
  1151. "/sys/module/%.*s/notes/.note.gnu.build-id",
  1152. (int)strlen(name - 1), name);
  1153. if (sysfs__read_build_id(filename, self->build_id,
  1154. sizeof(self->build_id)) == 0)
  1155. self->has_build_id = true;
  1156. return 0;
  1157. }
  1158. static int map_groups__set_modules_path_dir(struct map_groups *self, char *dir_name)
  1159. {
  1160. struct dirent *dent;
  1161. DIR *dir = opendir(dir_name);
  1162. if (!dir) {
  1163. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1164. return -1;
  1165. }
  1166. while ((dent = readdir(dir)) != NULL) {
  1167. char path[PATH_MAX];
  1168. if (dent->d_type == DT_DIR) {
  1169. if (!strcmp(dent->d_name, ".") ||
  1170. !strcmp(dent->d_name, ".."))
  1171. continue;
  1172. snprintf(path, sizeof(path), "%s/%s",
  1173. dir_name, dent->d_name);
  1174. if (map_groups__set_modules_path_dir(self, path) < 0)
  1175. goto failure;
  1176. } else {
  1177. char *dot = strrchr(dent->d_name, '.'),
  1178. dso_name[PATH_MAX];
  1179. struct map *map;
  1180. char *long_name;
  1181. if (dot == NULL || strcmp(dot, ".ko"))
  1182. continue;
  1183. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1184. (int)(dot - dent->d_name), dent->d_name);
  1185. strxfrchar(dso_name, '-', '_');
  1186. map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
  1187. if (map == NULL)
  1188. continue;
  1189. snprintf(path, sizeof(path), "%s/%s",
  1190. dir_name, dent->d_name);
  1191. long_name = strdup(path);
  1192. if (long_name == NULL)
  1193. goto failure;
  1194. dso__set_long_name(map->dso, long_name);
  1195. dso__kernel_module_get_build_id(map->dso);
  1196. }
  1197. }
  1198. return 0;
  1199. failure:
  1200. closedir(dir);
  1201. return -1;
  1202. }
  1203. static int map_groups__set_modules_path(struct map_groups *self)
  1204. {
  1205. struct utsname uts;
  1206. char modules_path[PATH_MAX];
  1207. if (uname(&uts) < 0)
  1208. return -1;
  1209. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  1210. uts.release);
  1211. return map_groups__set_modules_path_dir(self, modules_path);
  1212. }
  1213. /*
  1214. * Constructor variant for modules (where we know from /proc/modules where
  1215. * they are loaded) and for vmlinux, where only after we load all the
  1216. * symbols we'll know where it starts and ends.
  1217. */
  1218. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1219. {
  1220. struct map *self = calloc(1, (sizeof(*self) +
  1221. (dso->kernel ? sizeof(struct kmap) : 0)));
  1222. if (self != NULL) {
  1223. /*
  1224. * ->end will be filled after we load all the symbols
  1225. */
  1226. map__init(self, type, start, 0, 0, dso);
  1227. }
  1228. return self;
  1229. }
  1230. struct map *map_groups__new_module(struct map_groups *self, u64 start,
  1231. const char *filename)
  1232. {
  1233. struct map *map;
  1234. struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
  1235. if (dso == NULL)
  1236. return NULL;
  1237. map = map__new2(start, dso, MAP__FUNCTION);
  1238. if (map == NULL)
  1239. return NULL;
  1240. dso->origin = DSO__ORIG_KMODULE;
  1241. map_groups__insert(self, map);
  1242. return map;
  1243. }
  1244. static int map_groups__create_modules(struct map_groups *self)
  1245. {
  1246. char *line = NULL;
  1247. size_t n;
  1248. FILE *file = fopen("/proc/modules", "r");
  1249. struct map *map;
  1250. if (file == NULL)
  1251. return -1;
  1252. while (!feof(file)) {
  1253. char name[PATH_MAX];
  1254. u64 start;
  1255. char *sep;
  1256. int line_len;
  1257. line_len = getline(&line, &n, file);
  1258. if (line_len < 0)
  1259. break;
  1260. if (!line)
  1261. goto out_failure;
  1262. line[--line_len] = '\0'; /* \n */
  1263. sep = strrchr(line, 'x');
  1264. if (sep == NULL)
  1265. continue;
  1266. hex2u64(sep + 1, &start);
  1267. sep = strchr(line, ' ');
  1268. if (sep == NULL)
  1269. continue;
  1270. *sep = '\0';
  1271. snprintf(name, sizeof(name), "[%s]", line);
  1272. map = map_groups__new_module(self, start, name);
  1273. if (map == NULL)
  1274. goto out_delete_line;
  1275. dso__kernel_module_get_build_id(map->dso);
  1276. }
  1277. free(line);
  1278. fclose(file);
  1279. return map_groups__set_modules_path(self);
  1280. out_delete_line:
  1281. free(line);
  1282. out_failure:
  1283. return -1;
  1284. }
  1285. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1286. const char *vmlinux, symbol_filter_t filter)
  1287. {
  1288. int err = -1, fd;
  1289. if (self->has_build_id) {
  1290. u8 build_id[BUILD_ID_SIZE];
  1291. if (filename__read_build_id(vmlinux, build_id,
  1292. sizeof(build_id)) < 0) {
  1293. pr_debug("No build_id in %s, ignoring it\n", vmlinux);
  1294. return -1;
  1295. }
  1296. if (!dso__build_id_equal(self, build_id)) {
  1297. char expected_build_id[BUILD_ID_SIZE * 2 + 1],
  1298. vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
  1299. build_id__sprintf(self->build_id,
  1300. sizeof(self->build_id),
  1301. expected_build_id);
  1302. build_id__sprintf(build_id, sizeof(build_id),
  1303. vmlinux_build_id);
  1304. pr_debug("build_id in %s is %s while expected is %s, "
  1305. "ignoring it\n", vmlinux, vmlinux_build_id,
  1306. expected_build_id);
  1307. return -1;
  1308. }
  1309. }
  1310. fd = open(vmlinux, O_RDONLY);
  1311. if (fd < 0)
  1312. return -1;
  1313. dso__set_loaded(self, map->type);
  1314. err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
  1315. close(fd);
  1316. if (err > 0)
  1317. pr_debug("Using %s for symbols\n", vmlinux);
  1318. return err;
  1319. }
  1320. int dso__load_vmlinux_path(struct dso *self, struct map *map,
  1321. symbol_filter_t filter)
  1322. {
  1323. int i, err = 0;
  1324. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1325. vmlinux_path__nr_entries);
  1326. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1327. err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
  1328. if (err > 0) {
  1329. dso__set_long_name(self, strdup(vmlinux_path[i]));
  1330. break;
  1331. }
  1332. }
  1333. return err;
  1334. }
  1335. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1336. symbol_filter_t filter)
  1337. {
  1338. int err;
  1339. const char *kallsyms_filename = NULL;
  1340. char *kallsyms_allocated_filename = NULL;
  1341. /*
  1342. * Step 1: if the user specified a vmlinux filename, use it and only
  1343. * it, reporting errors to the user if it cannot be used.
  1344. *
  1345. * For instance, try to analyse an ARM perf.data file _without_ a
  1346. * build-id, or if the user specifies the wrong path to the right
  1347. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1348. * x86_86 one, on the machine where analysis is being performed, say),
  1349. * or worse, /proc/kallsyms.
  1350. *
  1351. * If the specified file _has_ a build-id and there is a build-id
  1352. * section in the perf.data file, we will still do the expected
  1353. * validation in dso__load_vmlinux and will bail out if they don't
  1354. * match.
  1355. */
  1356. if (symbol_conf.vmlinux_name != NULL) {
  1357. err = dso__load_vmlinux(self, map,
  1358. symbol_conf.vmlinux_name, filter);
  1359. goto out_try_fixup;
  1360. }
  1361. if (vmlinux_path != NULL) {
  1362. err = dso__load_vmlinux_path(self, map, filter);
  1363. if (err > 0)
  1364. goto out_fixup;
  1365. }
  1366. /*
  1367. * Say the kernel DSO was created when processing the build-id header table,
  1368. * we have a build-id, so check if it is the same as the running kernel,
  1369. * using it if it is.
  1370. */
  1371. if (self->has_build_id) {
  1372. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1373. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1374. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1375. sizeof(kallsyms_build_id)) == 0) {
  1376. if (dso__build_id_equal(self, kallsyms_build_id)) {
  1377. kallsyms_filename = "/proc/kallsyms";
  1378. goto do_kallsyms;
  1379. }
  1380. }
  1381. /*
  1382. * Now look if we have it on the build-id cache in
  1383. * $HOME/.debug/[kernel.kallsyms].
  1384. */
  1385. build_id__sprintf(self->build_id, sizeof(self->build_id),
  1386. sbuild_id);
  1387. if (asprintf(&kallsyms_allocated_filename,
  1388. "%s/.debug/[kernel.kallsyms]/%s",
  1389. getenv("HOME"), sbuild_id) == -1) {
  1390. pr_err("Not enough memory for kallsyms file lookup\n");
  1391. return -1;
  1392. }
  1393. kallsyms_filename = kallsyms_allocated_filename;
  1394. if (access(kallsyms_filename, F_OK)) {
  1395. pr_err("No kallsyms or vmlinux with build-id %s "
  1396. "was found\n", sbuild_id);
  1397. free(kallsyms_allocated_filename);
  1398. return -1;
  1399. }
  1400. } else {
  1401. /*
  1402. * Last resort, if we don't have a build-id and couldn't find
  1403. * any vmlinux file, try the running kernel kallsyms table.
  1404. */
  1405. kallsyms_filename = "/proc/kallsyms";
  1406. }
  1407. do_kallsyms:
  1408. err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
  1409. if (err > 0)
  1410. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1411. free(kallsyms_allocated_filename);
  1412. out_try_fixup:
  1413. if (err > 0) {
  1414. out_fixup:
  1415. if (kallsyms_filename != NULL)
  1416. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1417. map__fixup_start(map);
  1418. map__fixup_end(map);
  1419. }
  1420. return err;
  1421. }
  1422. LIST_HEAD(dsos__user);
  1423. LIST_HEAD(dsos__kernel);
  1424. static void dsos__add(struct list_head *head, struct dso *dso)
  1425. {
  1426. list_add_tail(&dso->node, head);
  1427. }
  1428. static struct dso *dsos__find(struct list_head *head, const char *name)
  1429. {
  1430. struct dso *pos;
  1431. list_for_each_entry(pos, head, node)
  1432. if (strcmp(pos->long_name, name) == 0)
  1433. return pos;
  1434. return NULL;
  1435. }
  1436. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1437. {
  1438. struct dso *dso = dsos__find(head, name);
  1439. if (!dso) {
  1440. dso = dso__new(name);
  1441. if (dso != NULL) {
  1442. dsos__add(head, dso);
  1443. dso__set_basename(dso);
  1444. }
  1445. }
  1446. return dso;
  1447. }
  1448. static void __dsos__fprintf(struct list_head *head, FILE *fp)
  1449. {
  1450. struct dso *pos;
  1451. list_for_each_entry(pos, head, node) {
  1452. int i;
  1453. for (i = 0; i < MAP__NR_TYPES; ++i)
  1454. dso__fprintf(pos, i, fp);
  1455. }
  1456. }
  1457. void dsos__fprintf(FILE *fp)
  1458. {
  1459. __dsos__fprintf(&dsos__kernel, fp);
  1460. __dsos__fprintf(&dsos__user, fp);
  1461. }
  1462. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1463. bool with_hits)
  1464. {
  1465. struct dso *pos;
  1466. size_t ret = 0;
  1467. list_for_each_entry(pos, head, node) {
  1468. if (with_hits && !pos->hit)
  1469. continue;
  1470. ret += dso__fprintf_buildid(pos, fp);
  1471. ret += fprintf(fp, " %s\n", pos->long_name);
  1472. }
  1473. return ret;
  1474. }
  1475. size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
  1476. {
  1477. return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
  1478. __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
  1479. }
  1480. struct dso *dso__new_kernel(const char *name)
  1481. {
  1482. struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
  1483. if (self != NULL) {
  1484. dso__set_short_name(self, "[kernel]");
  1485. self->kernel = 1;
  1486. }
  1487. return self;
  1488. }
  1489. void dso__read_running_kernel_build_id(struct dso *self)
  1490. {
  1491. if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
  1492. sizeof(self->build_id)) == 0)
  1493. self->has_build_id = true;
  1494. }
  1495. static struct dso *dsos__create_kernel(const char *vmlinux)
  1496. {
  1497. struct dso *kernel = dso__new_kernel(vmlinux);
  1498. if (kernel != NULL) {
  1499. dso__read_running_kernel_build_id(kernel);
  1500. dsos__add(&dsos__kernel, kernel);
  1501. }
  1502. return kernel;
  1503. }
  1504. int __map_groups__create_kernel_maps(struct map_groups *self,
  1505. struct map *vmlinux_maps[MAP__NR_TYPES],
  1506. struct dso *kernel)
  1507. {
  1508. enum map_type type;
  1509. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1510. struct kmap *kmap;
  1511. vmlinux_maps[type] = map__new2(0, kernel, type);
  1512. if (vmlinux_maps[type] == NULL)
  1513. return -1;
  1514. vmlinux_maps[type]->map_ip =
  1515. vmlinux_maps[type]->unmap_ip = identity__map_ip;
  1516. kmap = map__kmap(vmlinux_maps[type]);
  1517. kmap->kmaps = self;
  1518. map_groups__insert(self, vmlinux_maps[type]);
  1519. }
  1520. return 0;
  1521. }
  1522. static void vmlinux_path__exit(void)
  1523. {
  1524. while (--vmlinux_path__nr_entries >= 0) {
  1525. free(vmlinux_path[vmlinux_path__nr_entries]);
  1526. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1527. }
  1528. free(vmlinux_path);
  1529. vmlinux_path = NULL;
  1530. }
  1531. static int vmlinux_path__init(void)
  1532. {
  1533. struct utsname uts;
  1534. char bf[PATH_MAX];
  1535. if (uname(&uts) < 0)
  1536. return -1;
  1537. vmlinux_path = malloc(sizeof(char *) * 5);
  1538. if (vmlinux_path == NULL)
  1539. return -1;
  1540. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1541. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1542. goto out_fail;
  1543. ++vmlinux_path__nr_entries;
  1544. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1545. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1546. goto out_fail;
  1547. ++vmlinux_path__nr_entries;
  1548. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1549. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1550. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1551. goto out_fail;
  1552. ++vmlinux_path__nr_entries;
  1553. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1554. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1555. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1556. goto out_fail;
  1557. ++vmlinux_path__nr_entries;
  1558. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1559. uts.release);
  1560. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1561. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1562. goto out_fail;
  1563. ++vmlinux_path__nr_entries;
  1564. return 0;
  1565. out_fail:
  1566. vmlinux_path__exit();
  1567. return -1;
  1568. }
  1569. size_t vmlinux_path__fprintf(FILE *fp)
  1570. {
  1571. int i;
  1572. size_t printed = 0;
  1573. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  1574. printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
  1575. return printed;
  1576. }
  1577. static int setup_list(struct strlist **list, const char *list_str,
  1578. const char *list_name)
  1579. {
  1580. if (list_str == NULL)
  1581. return 0;
  1582. *list = strlist__new(true, list_str);
  1583. if (!*list) {
  1584. pr_err("problems parsing %s list\n", list_name);
  1585. return -1;
  1586. }
  1587. return 0;
  1588. }
  1589. int symbol__init(void)
  1590. {
  1591. elf_version(EV_CURRENT);
  1592. if (symbol_conf.sort_by_name)
  1593. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1594. sizeof(struct symbol));
  1595. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1596. return -1;
  1597. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1598. pr_err("'.' is the only non valid --field-separator argument\n");
  1599. return -1;
  1600. }
  1601. if (setup_list(&symbol_conf.dso_list,
  1602. symbol_conf.dso_list_str, "dso") < 0)
  1603. return -1;
  1604. if (setup_list(&symbol_conf.comm_list,
  1605. symbol_conf.comm_list_str, "comm") < 0)
  1606. goto out_free_dso_list;
  1607. if (setup_list(&symbol_conf.sym_list,
  1608. symbol_conf.sym_list_str, "symbol") < 0)
  1609. goto out_free_comm_list;
  1610. return 0;
  1611. out_free_dso_list:
  1612. strlist__delete(symbol_conf.dso_list);
  1613. out_free_comm_list:
  1614. strlist__delete(symbol_conf.comm_list);
  1615. return -1;
  1616. }
  1617. int map_groups__create_kernel_maps(struct map_groups *self,
  1618. struct map *vmlinux_maps[MAP__NR_TYPES])
  1619. {
  1620. struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
  1621. if (kernel == NULL)
  1622. return -1;
  1623. if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
  1624. return -1;
  1625. if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
  1626. pr_debug("Problems creating module maps, continuing anyway...\n");
  1627. /*
  1628. * Now that we have all the maps created, just set the ->end of them:
  1629. */
  1630. map_groups__fixup_end(self);
  1631. return 0;
  1632. }
  1633. static int hex(char ch)
  1634. {
  1635. if ((ch >= '0') && (ch <= '9'))
  1636. return ch - '0';
  1637. if ((ch >= 'a') && (ch <= 'f'))
  1638. return ch - 'a' + 10;
  1639. if ((ch >= 'A') && (ch <= 'F'))
  1640. return ch - 'A' + 10;
  1641. return -1;
  1642. }
  1643. /*
  1644. * While we find nice hex chars, build a long_val.
  1645. * Return number of chars processed.
  1646. */
  1647. int hex2u64(const char *ptr, u64 *long_val)
  1648. {
  1649. const char *p = ptr;
  1650. *long_val = 0;
  1651. while (*p) {
  1652. const int hex_val = hex(*p);
  1653. if (hex_val < 0)
  1654. break;
  1655. *long_val = (*long_val << 4) | hex_val;
  1656. p++;
  1657. }
  1658. return p - ptr;
  1659. }
  1660. char *strxfrchar(char *s, char from, char to)
  1661. {
  1662. char *p = s;
  1663. while ((p = strchr(p, from)) != NULL)
  1664. *p++ = to;
  1665. return s;
  1666. }