symbol.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678
  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 <inttypes.h>
  15. #include "build-id.h"
  16. #include "debug.h"
  17. #include "symbol.h"
  18. #include "strlist.h"
  19. #include <libelf.h>
  20. #include <gelf.h>
  21. #include <elf.h>
  22. #include <limits.h>
  23. #include <sys/utsname.h>
  24. #ifndef KSYM_NAME_LEN
  25. #define KSYM_NAME_LEN 128
  26. #endif
  27. #ifndef NT_GNU_BUILD_ID
  28. #define NT_GNU_BUILD_ID 3
  29. #endif
  30. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  31. static int elf_read_build_id(Elf *elf, void *bf, size_t size);
  32. static void dsos__add(struct list_head *head, struct dso *dso);
  33. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  34. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  35. symbol_filter_t filter);
  36. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  37. symbol_filter_t filter);
  38. static int vmlinux_path__nr_entries;
  39. static char **vmlinux_path;
  40. struct symbol_conf symbol_conf = {
  41. .exclude_other = true,
  42. .use_modules = true,
  43. .try_vmlinux_path = true,
  44. .symfs = "",
  45. };
  46. int dso__name_len(const struct dso *dso)
  47. {
  48. if (verbose)
  49. return dso->long_name_len;
  50. return dso->short_name_len;
  51. }
  52. bool dso__loaded(const struct dso *dso, enum map_type type)
  53. {
  54. return dso->loaded & (1 << type);
  55. }
  56. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  57. {
  58. return dso->sorted_by_name & (1 << type);
  59. }
  60. static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  61. {
  62. dso->sorted_by_name |= (1 << type);
  63. }
  64. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  65. {
  66. switch (map_type) {
  67. case MAP__FUNCTION:
  68. return symbol_type == 'T' || symbol_type == 'W';
  69. case MAP__VARIABLE:
  70. return symbol_type == 'D' || symbol_type == 'd';
  71. default:
  72. return false;
  73. }
  74. }
  75. static void symbols__fixup_end(struct rb_root *symbols)
  76. {
  77. struct rb_node *nd, *prevnd = rb_first(symbols);
  78. struct symbol *curr, *prev;
  79. if (prevnd == NULL)
  80. return;
  81. curr = rb_entry(prevnd, struct symbol, rb_node);
  82. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  83. prev = curr;
  84. curr = rb_entry(nd, struct symbol, rb_node);
  85. if (prev->end == prev->start && prev->end != curr->start)
  86. prev->end = curr->start - 1;
  87. }
  88. /* Last entry */
  89. if (curr->end == curr->start)
  90. curr->end = roundup(curr->start, 4096);
  91. }
  92. static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  93. {
  94. struct map *prev, *curr;
  95. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  96. if (prevnd == NULL)
  97. return;
  98. curr = rb_entry(prevnd, struct map, rb_node);
  99. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  100. prev = curr;
  101. curr = rb_entry(nd, struct map, rb_node);
  102. prev->end = curr->start - 1;
  103. }
  104. /*
  105. * We still haven't the actual symbols, so guess the
  106. * last map final address.
  107. */
  108. curr->end = ~0ULL;
  109. }
  110. static void map_groups__fixup_end(struct map_groups *mg)
  111. {
  112. int i;
  113. for (i = 0; i < MAP__NR_TYPES; ++i)
  114. __map_groups__fixup_end(mg, i);
  115. }
  116. static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
  117. const char *name)
  118. {
  119. size_t namelen = strlen(name) + 1;
  120. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  121. sizeof(*sym) + namelen));
  122. if (sym == NULL)
  123. return NULL;
  124. if (symbol_conf.priv_size)
  125. sym = ((void *)sym) + symbol_conf.priv_size;
  126. sym->start = start;
  127. sym->end = len ? start + len - 1 : start;
  128. sym->binding = binding;
  129. sym->namelen = namelen - 1;
  130. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  131. __func__, name, start, sym->end);
  132. memcpy(sym->name, name, namelen);
  133. return sym;
  134. }
  135. void symbol__delete(struct symbol *sym)
  136. {
  137. free(((void *)sym) - symbol_conf.priv_size);
  138. }
  139. static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  140. {
  141. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  142. sym->start, sym->end,
  143. sym->binding == STB_GLOBAL ? 'g' :
  144. sym->binding == STB_LOCAL ? 'l' : 'w',
  145. sym->name);
  146. }
  147. void dso__set_long_name(struct dso *dso, char *name)
  148. {
  149. if (name == NULL)
  150. return;
  151. dso->long_name = name;
  152. dso->long_name_len = strlen(name);
  153. }
  154. static void dso__set_short_name(struct dso *dso, const char *name)
  155. {
  156. if (name == NULL)
  157. return;
  158. dso->short_name = name;
  159. dso->short_name_len = strlen(name);
  160. }
  161. static void dso__set_basename(struct dso *dso)
  162. {
  163. dso__set_short_name(dso, basename(dso->long_name));
  164. }
  165. struct dso *dso__new(const char *name)
  166. {
  167. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  168. if (dso != NULL) {
  169. int i;
  170. strcpy(dso->name, name);
  171. dso__set_long_name(dso, dso->name);
  172. dso__set_short_name(dso, dso->name);
  173. for (i = 0; i < MAP__NR_TYPES; ++i)
  174. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  175. dso->symtab_type = SYMTAB__NOT_FOUND;
  176. dso->loaded = 0;
  177. dso->sorted_by_name = 0;
  178. dso->has_build_id = 0;
  179. dso->kernel = DSO_TYPE_USER;
  180. INIT_LIST_HEAD(&dso->node);
  181. }
  182. return dso;
  183. }
  184. static void symbols__delete(struct rb_root *symbols)
  185. {
  186. struct symbol *pos;
  187. struct rb_node *next = rb_first(symbols);
  188. while (next) {
  189. pos = rb_entry(next, struct symbol, rb_node);
  190. next = rb_next(&pos->rb_node);
  191. rb_erase(&pos->rb_node, symbols);
  192. symbol__delete(pos);
  193. }
  194. }
  195. void dso__delete(struct dso *dso)
  196. {
  197. int i;
  198. for (i = 0; i < MAP__NR_TYPES; ++i)
  199. symbols__delete(&dso->symbols[i]);
  200. if (dso->sname_alloc)
  201. free((char *)dso->short_name);
  202. if (dso->lname_alloc)
  203. free(dso->long_name);
  204. free(dso);
  205. }
  206. void dso__set_build_id(struct dso *dso, void *build_id)
  207. {
  208. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  209. dso->has_build_id = 1;
  210. }
  211. static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  212. {
  213. struct rb_node **p = &symbols->rb_node;
  214. struct rb_node *parent = NULL;
  215. const u64 ip = sym->start;
  216. struct symbol *s;
  217. while (*p != NULL) {
  218. parent = *p;
  219. s = rb_entry(parent, struct symbol, rb_node);
  220. if (ip < s->start)
  221. p = &(*p)->rb_left;
  222. else
  223. p = &(*p)->rb_right;
  224. }
  225. rb_link_node(&sym->rb_node, parent, p);
  226. rb_insert_color(&sym->rb_node, symbols);
  227. }
  228. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  229. {
  230. struct rb_node *n;
  231. if (symbols == NULL)
  232. return NULL;
  233. n = symbols->rb_node;
  234. while (n) {
  235. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  236. if (ip < s->start)
  237. n = n->rb_left;
  238. else if (ip > s->end)
  239. n = n->rb_right;
  240. else
  241. return s;
  242. }
  243. return NULL;
  244. }
  245. struct symbol_name_rb_node {
  246. struct rb_node rb_node;
  247. struct symbol sym;
  248. };
  249. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  250. {
  251. struct rb_node **p = &symbols->rb_node;
  252. struct rb_node *parent = NULL;
  253. struct symbol_name_rb_node *symn, *s;
  254. symn = container_of(sym, struct symbol_name_rb_node, sym);
  255. while (*p != NULL) {
  256. parent = *p;
  257. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  258. if (strcmp(sym->name, s->sym.name) < 0)
  259. p = &(*p)->rb_left;
  260. else
  261. p = &(*p)->rb_right;
  262. }
  263. rb_link_node(&symn->rb_node, parent, p);
  264. rb_insert_color(&symn->rb_node, symbols);
  265. }
  266. static void symbols__sort_by_name(struct rb_root *symbols,
  267. struct rb_root *source)
  268. {
  269. struct rb_node *nd;
  270. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  271. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  272. symbols__insert_by_name(symbols, pos);
  273. }
  274. }
  275. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  276. const char *name)
  277. {
  278. struct rb_node *n;
  279. if (symbols == NULL)
  280. return NULL;
  281. n = symbols->rb_node;
  282. while (n) {
  283. struct symbol_name_rb_node *s;
  284. int cmp;
  285. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  286. cmp = strcmp(name, s->sym.name);
  287. if (cmp < 0)
  288. n = n->rb_left;
  289. else if (cmp > 0)
  290. n = n->rb_right;
  291. else
  292. return &s->sym;
  293. }
  294. return NULL;
  295. }
  296. struct symbol *dso__find_symbol(struct dso *dso,
  297. enum map_type type, u64 addr)
  298. {
  299. return symbols__find(&dso->symbols[type], addr);
  300. }
  301. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  302. const char *name)
  303. {
  304. return symbols__find_by_name(&dso->symbol_names[type], name);
  305. }
  306. void dso__sort_by_name(struct dso *dso, enum map_type type)
  307. {
  308. dso__set_sorted_by_name(dso, type);
  309. return symbols__sort_by_name(&dso->symbol_names[type],
  310. &dso->symbols[type]);
  311. }
  312. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  313. {
  314. char *bid = bf;
  315. const u8 *raw = build_id;
  316. int i;
  317. for (i = 0; i < len; ++i) {
  318. sprintf(bid, "%02x", *raw);
  319. ++raw;
  320. bid += 2;
  321. }
  322. return raw - build_id;
  323. }
  324. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  325. {
  326. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  327. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  328. return fprintf(fp, "%s", sbuild_id);
  329. }
  330. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  331. enum map_type type, FILE *fp)
  332. {
  333. size_t ret = 0;
  334. struct rb_node *nd;
  335. struct symbol_name_rb_node *pos;
  336. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  337. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  338. fprintf(fp, "%s\n", pos->sym.name);
  339. }
  340. return ret;
  341. }
  342. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  343. {
  344. struct rb_node *nd;
  345. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  346. if (dso->short_name != dso->long_name)
  347. ret += fprintf(fp, "%s, ", dso->long_name);
  348. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  349. dso->loaded ? "" : "NOT ");
  350. ret += dso__fprintf_buildid(dso, fp);
  351. ret += fprintf(fp, ")\n");
  352. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  353. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  354. ret += symbol__fprintf(pos, fp);
  355. }
  356. return ret;
  357. }
  358. int kallsyms__parse(const char *filename, void *arg,
  359. int (*process_symbol)(void *arg, const char *name,
  360. char type, u64 start, u64 end))
  361. {
  362. char *line = NULL;
  363. size_t n;
  364. int err = -1;
  365. u64 prev_start = 0;
  366. char prev_symbol_type = 0;
  367. char *prev_symbol_name;
  368. FILE *file = fopen(filename, "r");
  369. if (file == NULL)
  370. goto out_failure;
  371. prev_symbol_name = malloc(KSYM_NAME_LEN);
  372. if (prev_symbol_name == NULL)
  373. goto out_close;
  374. err = 0;
  375. while (!feof(file)) {
  376. u64 start;
  377. int line_len, len;
  378. char symbol_type;
  379. char *symbol_name;
  380. line_len = getline(&line, &n, file);
  381. if (line_len < 0 || !line)
  382. break;
  383. line[--line_len] = '\0'; /* \n */
  384. len = hex2u64(line, &start);
  385. len++;
  386. if (len + 2 >= line_len)
  387. continue;
  388. symbol_type = toupper(line[len]);
  389. len += 2;
  390. symbol_name = line + len;
  391. len = line_len - len;
  392. if (len >= KSYM_NAME_LEN) {
  393. err = -1;
  394. break;
  395. }
  396. if (prev_symbol_type) {
  397. u64 end = start;
  398. if (end != prev_start)
  399. --end;
  400. err = process_symbol(arg, prev_symbol_name,
  401. prev_symbol_type, prev_start, end);
  402. if (err)
  403. break;
  404. }
  405. memcpy(prev_symbol_name, symbol_name, len + 1);
  406. prev_symbol_type = symbol_type;
  407. prev_start = start;
  408. }
  409. free(prev_symbol_name);
  410. free(line);
  411. out_close:
  412. fclose(file);
  413. return err;
  414. out_failure:
  415. return -1;
  416. }
  417. struct process_kallsyms_args {
  418. struct map *map;
  419. struct dso *dso;
  420. };
  421. static u8 kallsyms2elf_type(char type)
  422. {
  423. if (type == 'W')
  424. return STB_WEAK;
  425. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  426. }
  427. static int map__process_kallsym_symbol(void *arg, const char *name,
  428. char type, u64 start, u64 end)
  429. {
  430. struct symbol *sym;
  431. struct process_kallsyms_args *a = arg;
  432. struct rb_root *root = &a->dso->symbols[a->map->type];
  433. if (!symbol_type__is_a(type, a->map->type))
  434. return 0;
  435. sym = symbol__new(start, end - start + 1,
  436. kallsyms2elf_type(type), name);
  437. if (sym == NULL)
  438. return -ENOMEM;
  439. /*
  440. * We will pass the symbols to the filter later, in
  441. * map__split_kallsyms, when we have split the maps per module
  442. */
  443. symbols__insert(root, sym);
  444. return 0;
  445. }
  446. /*
  447. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  448. * so that we can in the next step set the symbol ->end address and then
  449. * call kernel_maps__split_kallsyms.
  450. */
  451. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  452. struct map *map)
  453. {
  454. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  455. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  456. }
  457. /*
  458. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  459. * kernel range is broken in several maps, named [kernel].N, as we don't have
  460. * the original ELF section names vmlinux have.
  461. */
  462. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  463. symbol_filter_t filter)
  464. {
  465. struct map_groups *kmaps = map__kmap(map)->kmaps;
  466. struct machine *machine = kmaps->machine;
  467. struct map *curr_map = map;
  468. struct symbol *pos;
  469. int count = 0, moved = 0;
  470. struct rb_root *root = &dso->symbols[map->type];
  471. struct rb_node *next = rb_first(root);
  472. int kernel_range = 0;
  473. while (next) {
  474. char *module;
  475. pos = rb_entry(next, struct symbol, rb_node);
  476. next = rb_next(&pos->rb_node);
  477. module = strchr(pos->name, '\t');
  478. if (module) {
  479. if (!symbol_conf.use_modules)
  480. goto discard_symbol;
  481. *module++ = '\0';
  482. if (strcmp(curr_map->dso->short_name, module)) {
  483. if (curr_map != map &&
  484. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  485. machine__is_default_guest(machine)) {
  486. /*
  487. * We assume all symbols of a module are
  488. * continuous in * kallsyms, so curr_map
  489. * points to a module and all its
  490. * symbols are in its kmap. Mark it as
  491. * loaded.
  492. */
  493. dso__set_loaded(curr_map->dso,
  494. curr_map->type);
  495. }
  496. curr_map = map_groups__find_by_name(kmaps,
  497. map->type, module);
  498. if (curr_map == NULL) {
  499. pr_debug("%s/proc/{kallsyms,modules} "
  500. "inconsistency while looking "
  501. "for \"%s\" module!\n",
  502. machine->root_dir, module);
  503. curr_map = map;
  504. goto discard_symbol;
  505. }
  506. if (curr_map->dso->loaded &&
  507. !machine__is_default_guest(machine))
  508. goto discard_symbol;
  509. }
  510. /*
  511. * So that we look just like we get from .ko files,
  512. * i.e. not prelinked, relative to map->start.
  513. */
  514. pos->start = curr_map->map_ip(curr_map, pos->start);
  515. pos->end = curr_map->map_ip(curr_map, pos->end);
  516. } else if (curr_map != map) {
  517. char dso_name[PATH_MAX];
  518. struct dso *ndso;
  519. if (count == 0) {
  520. curr_map = map;
  521. goto filter_symbol;
  522. }
  523. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  524. snprintf(dso_name, sizeof(dso_name),
  525. "[guest.kernel].%d",
  526. kernel_range++);
  527. else
  528. snprintf(dso_name, sizeof(dso_name),
  529. "[kernel].%d",
  530. kernel_range++);
  531. ndso = dso__new(dso_name);
  532. if (ndso == NULL)
  533. return -1;
  534. ndso->kernel = dso->kernel;
  535. curr_map = map__new2(pos->start, ndso, map->type);
  536. if (curr_map == NULL) {
  537. dso__delete(ndso);
  538. return -1;
  539. }
  540. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  541. map_groups__insert(kmaps, curr_map);
  542. ++kernel_range;
  543. }
  544. filter_symbol:
  545. if (filter && filter(curr_map, pos)) {
  546. discard_symbol: rb_erase(&pos->rb_node, root);
  547. symbol__delete(pos);
  548. } else {
  549. if (curr_map != map) {
  550. rb_erase(&pos->rb_node, root);
  551. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  552. ++moved;
  553. } else
  554. ++count;
  555. }
  556. }
  557. if (curr_map != map &&
  558. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  559. machine__is_default_guest(kmaps->machine)) {
  560. dso__set_loaded(curr_map->dso, curr_map->type);
  561. }
  562. return count + moved;
  563. }
  564. static bool symbol__restricted_filename(const char *filename,
  565. const char *restricted_filename)
  566. {
  567. bool restricted = false;
  568. if (symbol_conf.kptr_restrict) {
  569. char *r = realpath(filename, NULL);
  570. if (r != NULL) {
  571. restricted = strcmp(r, restricted_filename) == 0;
  572. free(r);
  573. return restricted;
  574. }
  575. }
  576. return restricted;
  577. }
  578. int dso__load_kallsyms(struct dso *dso, const char *filename,
  579. struct map *map, symbol_filter_t filter)
  580. {
  581. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  582. return -1;
  583. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  584. return -1;
  585. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  586. dso->symtab_type = SYMTAB__GUEST_KALLSYMS;
  587. else
  588. dso->symtab_type = SYMTAB__KALLSYMS;
  589. return dso__split_kallsyms(dso, map, filter);
  590. }
  591. static int dso__load_perf_map(struct dso *dso, struct map *map,
  592. symbol_filter_t filter)
  593. {
  594. char *line = NULL;
  595. size_t n;
  596. FILE *file;
  597. int nr_syms = 0;
  598. file = fopen(dso->long_name, "r");
  599. if (file == NULL)
  600. goto out_failure;
  601. while (!feof(file)) {
  602. u64 start, size;
  603. struct symbol *sym;
  604. int line_len, len;
  605. line_len = getline(&line, &n, file);
  606. if (line_len < 0)
  607. break;
  608. if (!line)
  609. goto out_failure;
  610. line[--line_len] = '\0'; /* \n */
  611. len = hex2u64(line, &start);
  612. len++;
  613. if (len + 2 >= line_len)
  614. continue;
  615. len += hex2u64(line + len, &size);
  616. len++;
  617. if (len + 2 >= line_len)
  618. continue;
  619. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  620. if (sym == NULL)
  621. goto out_delete_line;
  622. if (filter && filter(map, sym))
  623. symbol__delete(sym);
  624. else {
  625. symbols__insert(&dso->symbols[map->type], sym);
  626. nr_syms++;
  627. }
  628. }
  629. free(line);
  630. fclose(file);
  631. return nr_syms;
  632. out_delete_line:
  633. free(line);
  634. out_failure:
  635. return -1;
  636. }
  637. /**
  638. * elf_symtab__for_each_symbol - iterate thru all the symbols
  639. *
  640. * @syms: struct elf_symtab instance to iterate
  641. * @idx: uint32_t idx
  642. * @sym: GElf_Sym iterator
  643. */
  644. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  645. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  646. idx < nr_syms; \
  647. idx++, gelf_getsym(syms, idx, &sym))
  648. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  649. {
  650. return GELF_ST_TYPE(sym->st_info);
  651. }
  652. static inline int elf_sym__is_function(const GElf_Sym *sym)
  653. {
  654. return elf_sym__type(sym) == STT_FUNC &&
  655. sym->st_name != 0 &&
  656. sym->st_shndx != SHN_UNDEF;
  657. }
  658. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  659. {
  660. return elf_sym__type(sym) == STT_OBJECT &&
  661. sym->st_name != 0 &&
  662. sym->st_shndx != SHN_UNDEF;
  663. }
  664. static inline int elf_sym__is_label(const GElf_Sym *sym)
  665. {
  666. return elf_sym__type(sym) == STT_NOTYPE &&
  667. sym->st_name != 0 &&
  668. sym->st_shndx != SHN_UNDEF &&
  669. sym->st_shndx != SHN_ABS;
  670. }
  671. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  672. const Elf_Data *secstrs)
  673. {
  674. return secstrs->d_buf + shdr->sh_name;
  675. }
  676. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  677. const Elf_Data *secstrs)
  678. {
  679. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  680. }
  681. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  682. const Elf_Data *secstrs)
  683. {
  684. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  685. }
  686. static inline const char *elf_sym__name(const GElf_Sym *sym,
  687. const Elf_Data *symstrs)
  688. {
  689. return symstrs->d_buf + sym->st_name;
  690. }
  691. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  692. GElf_Shdr *shp, const char *name,
  693. size_t *idx)
  694. {
  695. Elf_Scn *sec = NULL;
  696. size_t cnt = 1;
  697. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  698. char *str;
  699. gelf_getshdr(sec, shp);
  700. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  701. if (!strcmp(name, str)) {
  702. if (idx)
  703. *idx = cnt;
  704. break;
  705. }
  706. ++cnt;
  707. }
  708. return sec;
  709. }
  710. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  711. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  712. idx < nr_entries; \
  713. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  714. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  715. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  716. idx < nr_entries; \
  717. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  718. /*
  719. * We need to check if we have a .dynsym, so that we can handle the
  720. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  721. * .dynsym or .symtab).
  722. * And always look at the original dso, not at debuginfo packages, that
  723. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  724. */
  725. static int dso__synthesize_plt_symbols(struct dso *dso, struct map *map,
  726. symbol_filter_t filter)
  727. {
  728. uint32_t nr_rel_entries, idx;
  729. GElf_Sym sym;
  730. u64 plt_offset;
  731. GElf_Shdr shdr_plt;
  732. struct symbol *f;
  733. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  734. Elf_Data *reldata, *syms, *symstrs;
  735. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  736. size_t dynsym_idx;
  737. GElf_Ehdr ehdr;
  738. char sympltname[1024];
  739. Elf *elf;
  740. int nr = 0, symidx, fd, err = 0;
  741. char name[PATH_MAX];
  742. snprintf(name, sizeof(name), "%s%s",
  743. symbol_conf.symfs, dso->long_name);
  744. fd = open(name, O_RDONLY);
  745. if (fd < 0)
  746. goto out;
  747. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  748. if (elf == NULL)
  749. goto out_close;
  750. if (gelf_getehdr(elf, &ehdr) == NULL)
  751. goto out_elf_end;
  752. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  753. ".dynsym", &dynsym_idx);
  754. if (scn_dynsym == NULL)
  755. goto out_elf_end;
  756. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  757. ".rela.plt", NULL);
  758. if (scn_plt_rel == NULL) {
  759. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  760. ".rel.plt", NULL);
  761. if (scn_plt_rel == NULL)
  762. goto out_elf_end;
  763. }
  764. err = -1;
  765. if (shdr_rel_plt.sh_link != dynsym_idx)
  766. goto out_elf_end;
  767. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  768. goto out_elf_end;
  769. /*
  770. * Fetch the relocation section to find the idxes to the GOT
  771. * and the symbols in the .dynsym they refer to.
  772. */
  773. reldata = elf_getdata(scn_plt_rel, NULL);
  774. if (reldata == NULL)
  775. goto out_elf_end;
  776. syms = elf_getdata(scn_dynsym, NULL);
  777. if (syms == NULL)
  778. goto out_elf_end;
  779. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  780. if (scn_symstrs == NULL)
  781. goto out_elf_end;
  782. symstrs = elf_getdata(scn_symstrs, NULL);
  783. if (symstrs == NULL)
  784. goto out_elf_end;
  785. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  786. plt_offset = shdr_plt.sh_offset;
  787. if (shdr_rel_plt.sh_type == SHT_RELA) {
  788. GElf_Rela pos_mem, *pos;
  789. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  790. nr_rel_entries) {
  791. symidx = GELF_R_SYM(pos->r_info);
  792. plt_offset += shdr_plt.sh_entsize;
  793. gelf_getsym(syms, symidx, &sym);
  794. snprintf(sympltname, sizeof(sympltname),
  795. "%s@plt", elf_sym__name(&sym, symstrs));
  796. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  797. STB_GLOBAL, sympltname);
  798. if (!f)
  799. goto out_elf_end;
  800. if (filter && filter(map, f))
  801. symbol__delete(f);
  802. else {
  803. symbols__insert(&dso->symbols[map->type], f);
  804. ++nr;
  805. }
  806. }
  807. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  808. GElf_Rel pos_mem, *pos;
  809. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  810. nr_rel_entries) {
  811. symidx = GELF_R_SYM(pos->r_info);
  812. plt_offset += shdr_plt.sh_entsize;
  813. gelf_getsym(syms, symidx, &sym);
  814. snprintf(sympltname, sizeof(sympltname),
  815. "%s@plt", elf_sym__name(&sym, symstrs));
  816. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  817. STB_GLOBAL, sympltname);
  818. if (!f)
  819. goto out_elf_end;
  820. if (filter && filter(map, f))
  821. symbol__delete(f);
  822. else {
  823. symbols__insert(&dso->symbols[map->type], f);
  824. ++nr;
  825. }
  826. }
  827. }
  828. err = 0;
  829. out_elf_end:
  830. elf_end(elf);
  831. out_close:
  832. close(fd);
  833. if (err == 0)
  834. return nr;
  835. out:
  836. pr_debug("%s: problems reading %s PLT info.\n",
  837. __func__, dso->long_name);
  838. return 0;
  839. }
  840. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  841. {
  842. switch (type) {
  843. case MAP__FUNCTION:
  844. return elf_sym__is_function(sym);
  845. case MAP__VARIABLE:
  846. return elf_sym__is_object(sym);
  847. default:
  848. return false;
  849. }
  850. }
  851. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  852. enum map_type type)
  853. {
  854. switch (type) {
  855. case MAP__FUNCTION:
  856. return elf_sec__is_text(shdr, secstrs);
  857. case MAP__VARIABLE:
  858. return elf_sec__is_data(shdr, secstrs);
  859. default:
  860. return false;
  861. }
  862. }
  863. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  864. {
  865. Elf_Scn *sec = NULL;
  866. GElf_Shdr shdr;
  867. size_t cnt = 1;
  868. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  869. gelf_getshdr(sec, &shdr);
  870. if ((addr >= shdr.sh_addr) &&
  871. (addr < (shdr.sh_addr + shdr.sh_size)))
  872. return cnt;
  873. ++cnt;
  874. }
  875. return -1;
  876. }
  877. static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
  878. int fd, symbol_filter_t filter, int kmodule,
  879. int want_symtab)
  880. {
  881. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  882. struct map *curr_map = map;
  883. struct dso *curr_dso = dso;
  884. Elf_Data *symstrs, *secstrs;
  885. uint32_t nr_syms;
  886. int err = -1;
  887. uint32_t idx;
  888. GElf_Ehdr ehdr;
  889. GElf_Shdr shdr, opdshdr;
  890. Elf_Data *syms, *opddata = NULL;
  891. GElf_Sym sym;
  892. Elf_Scn *sec, *sec_strndx, *opdsec;
  893. Elf *elf;
  894. int nr = 0;
  895. size_t opdidx = 0;
  896. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  897. if (elf == NULL) {
  898. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  899. goto out_close;
  900. }
  901. if (gelf_getehdr(elf, &ehdr) == NULL) {
  902. pr_debug("%s: cannot get elf header.\n", __func__);
  903. goto out_elf_end;
  904. }
  905. /* Always reject images with a mismatched build-id: */
  906. if (dso->has_build_id) {
  907. u8 build_id[BUILD_ID_SIZE];
  908. if (elf_read_build_id(elf, build_id,
  909. BUILD_ID_SIZE) != BUILD_ID_SIZE)
  910. goto out_elf_end;
  911. if (!dso__build_id_equal(dso, build_id))
  912. goto out_elf_end;
  913. }
  914. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  915. if (sec == NULL) {
  916. if (want_symtab)
  917. goto out_elf_end;
  918. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  919. if (sec == NULL)
  920. goto out_elf_end;
  921. }
  922. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  923. if (opdsec)
  924. opddata = elf_rawdata(opdsec, NULL);
  925. syms = elf_getdata(sec, NULL);
  926. if (syms == NULL)
  927. goto out_elf_end;
  928. sec = elf_getscn(elf, shdr.sh_link);
  929. if (sec == NULL)
  930. goto out_elf_end;
  931. symstrs = elf_getdata(sec, NULL);
  932. if (symstrs == NULL)
  933. goto out_elf_end;
  934. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  935. if (sec_strndx == NULL)
  936. goto out_elf_end;
  937. secstrs = elf_getdata(sec_strndx, NULL);
  938. if (secstrs == NULL)
  939. goto out_elf_end;
  940. nr_syms = shdr.sh_size / shdr.sh_entsize;
  941. memset(&sym, 0, sizeof(sym));
  942. if (dso->kernel == DSO_TYPE_USER) {
  943. dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  944. elf_section_by_name(elf, &ehdr, &shdr,
  945. ".gnu.prelink_undo",
  946. NULL) != NULL);
  947. } else {
  948. dso->adjust_symbols = 0;
  949. }
  950. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  951. struct symbol *f;
  952. const char *elf_name = elf_sym__name(&sym, symstrs);
  953. char *demangled = NULL;
  954. int is_label = elf_sym__is_label(&sym);
  955. const char *section_name;
  956. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  957. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  958. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  959. if (!is_label && !elf_sym__is_a(&sym, map->type))
  960. continue;
  961. /* Reject ARM ELF "mapping symbols": these aren't unique and
  962. * don't identify functions, so will confuse the profile
  963. * output: */
  964. if (ehdr.e_machine == EM_ARM) {
  965. if (!strcmp(elf_name, "$a") ||
  966. !strcmp(elf_name, "$d") ||
  967. !strcmp(elf_name, "$t"))
  968. continue;
  969. }
  970. if (opdsec && sym.st_shndx == opdidx) {
  971. u32 offset = sym.st_value - opdshdr.sh_addr;
  972. u64 *opd = opddata->d_buf + offset;
  973. sym.st_value = *opd;
  974. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  975. }
  976. sec = elf_getscn(elf, sym.st_shndx);
  977. if (!sec)
  978. goto out_elf_end;
  979. gelf_getshdr(sec, &shdr);
  980. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  981. continue;
  982. section_name = elf_sec__name(&shdr, secstrs);
  983. /* On ARM, symbols for thumb functions have 1 added to
  984. * the symbol address as a flag - remove it */
  985. if ((ehdr.e_machine == EM_ARM) &&
  986. (map->type == MAP__FUNCTION) &&
  987. (sym.st_value & 1))
  988. --sym.st_value;
  989. if (dso->kernel != DSO_TYPE_USER || kmodule) {
  990. char dso_name[PATH_MAX];
  991. if (strcmp(section_name,
  992. (curr_dso->short_name +
  993. dso->short_name_len)) == 0)
  994. goto new_symbol;
  995. if (strcmp(section_name, ".text") == 0) {
  996. curr_map = map;
  997. curr_dso = dso;
  998. goto new_symbol;
  999. }
  1000. snprintf(dso_name, sizeof(dso_name),
  1001. "%s%s", dso->short_name, section_name);
  1002. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  1003. if (curr_map == NULL) {
  1004. u64 start = sym.st_value;
  1005. if (kmodule)
  1006. start += map->start + shdr.sh_offset;
  1007. curr_dso = dso__new(dso_name);
  1008. if (curr_dso == NULL)
  1009. goto out_elf_end;
  1010. curr_dso->kernel = dso->kernel;
  1011. curr_dso->long_name = dso->long_name;
  1012. curr_dso->long_name_len = dso->long_name_len;
  1013. curr_map = map__new2(start, curr_dso,
  1014. map->type);
  1015. if (curr_map == NULL) {
  1016. dso__delete(curr_dso);
  1017. goto out_elf_end;
  1018. }
  1019. curr_map->map_ip = identity__map_ip;
  1020. curr_map->unmap_ip = identity__map_ip;
  1021. curr_dso->symtab_type = dso->symtab_type;
  1022. map_groups__insert(kmap->kmaps, curr_map);
  1023. dsos__add(&dso->node, curr_dso);
  1024. dso__set_loaded(curr_dso, map->type);
  1025. } else
  1026. curr_dso = curr_map->dso;
  1027. goto new_symbol;
  1028. }
  1029. if (curr_dso->adjust_symbols) {
  1030. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1031. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1032. (u64)sym.st_value, (u64)shdr.sh_addr,
  1033. (u64)shdr.sh_offset);
  1034. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1035. }
  1036. /*
  1037. * We need to figure out if the object was created from C++ sources
  1038. * DWARF DW_compile_unit has this, but we don't always have access
  1039. * to it...
  1040. */
  1041. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  1042. if (demangled != NULL)
  1043. elf_name = demangled;
  1044. new_symbol:
  1045. f = symbol__new(sym.st_value, sym.st_size,
  1046. GELF_ST_BIND(sym.st_info), elf_name);
  1047. free(demangled);
  1048. if (!f)
  1049. goto out_elf_end;
  1050. if (filter && filter(curr_map, f))
  1051. symbol__delete(f);
  1052. else {
  1053. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  1054. nr++;
  1055. }
  1056. }
  1057. /*
  1058. * For misannotated, zeroed, ASM function sizes.
  1059. */
  1060. if (nr > 0) {
  1061. symbols__fixup_end(&dso->symbols[map->type]);
  1062. if (kmap) {
  1063. /*
  1064. * We need to fixup this here too because we create new
  1065. * maps here, for things like vsyscall sections.
  1066. */
  1067. __map_groups__fixup_end(kmap->kmaps, map->type);
  1068. }
  1069. }
  1070. err = nr;
  1071. out_elf_end:
  1072. elf_end(elf);
  1073. out_close:
  1074. return err;
  1075. }
  1076. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  1077. {
  1078. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  1079. }
  1080. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1081. {
  1082. bool have_build_id = false;
  1083. struct dso *pos;
  1084. list_for_each_entry(pos, head, node) {
  1085. if (with_hits && !pos->hit)
  1086. continue;
  1087. if (pos->has_build_id) {
  1088. have_build_id = true;
  1089. continue;
  1090. }
  1091. if (filename__read_build_id(pos->long_name, pos->build_id,
  1092. sizeof(pos->build_id)) > 0) {
  1093. have_build_id = true;
  1094. pos->has_build_id = true;
  1095. }
  1096. }
  1097. return have_build_id;
  1098. }
  1099. /*
  1100. * Align offset to 4 bytes as needed for note name and descriptor data.
  1101. */
  1102. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1103. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1104. {
  1105. int err = -1;
  1106. GElf_Ehdr ehdr;
  1107. GElf_Shdr shdr;
  1108. Elf_Data *data;
  1109. Elf_Scn *sec;
  1110. Elf_Kind ek;
  1111. void *ptr;
  1112. if (size < BUILD_ID_SIZE)
  1113. goto out;
  1114. ek = elf_kind(elf);
  1115. if (ek != ELF_K_ELF)
  1116. goto out;
  1117. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1118. pr_err("%s: cannot get elf header.\n", __func__);
  1119. goto out;
  1120. }
  1121. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1122. ".note.gnu.build-id", NULL);
  1123. if (sec == NULL) {
  1124. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1125. ".notes", NULL);
  1126. if (sec == NULL)
  1127. goto out;
  1128. }
  1129. data = elf_getdata(sec, NULL);
  1130. if (data == NULL)
  1131. goto out;
  1132. ptr = data->d_buf;
  1133. while (ptr < (data->d_buf + data->d_size)) {
  1134. GElf_Nhdr *nhdr = ptr;
  1135. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  1136. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1137. const char *name;
  1138. ptr += sizeof(*nhdr);
  1139. name = ptr;
  1140. ptr += namesz;
  1141. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1142. nhdr->n_namesz == sizeof("GNU")) {
  1143. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1144. memcpy(bf, ptr, BUILD_ID_SIZE);
  1145. err = BUILD_ID_SIZE;
  1146. break;
  1147. }
  1148. }
  1149. ptr += descsz;
  1150. }
  1151. out:
  1152. return err;
  1153. }
  1154. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1155. {
  1156. int fd, err = -1;
  1157. Elf *elf;
  1158. if (size < BUILD_ID_SIZE)
  1159. goto out;
  1160. fd = open(filename, O_RDONLY);
  1161. if (fd < 0)
  1162. goto out;
  1163. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1164. if (elf == NULL) {
  1165. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1166. goto out_close;
  1167. }
  1168. err = elf_read_build_id(elf, bf, size);
  1169. elf_end(elf);
  1170. out_close:
  1171. close(fd);
  1172. out:
  1173. return err;
  1174. }
  1175. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1176. {
  1177. int fd, err = -1;
  1178. if (size < BUILD_ID_SIZE)
  1179. goto out;
  1180. fd = open(filename, O_RDONLY);
  1181. if (fd < 0)
  1182. goto out;
  1183. while (1) {
  1184. char bf[BUFSIZ];
  1185. GElf_Nhdr nhdr;
  1186. int namesz, descsz;
  1187. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1188. break;
  1189. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1190. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1191. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1192. nhdr.n_namesz == sizeof("GNU")) {
  1193. if (read(fd, bf, namesz) != namesz)
  1194. break;
  1195. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1196. if (read(fd, build_id,
  1197. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  1198. err = 0;
  1199. break;
  1200. }
  1201. } else if (read(fd, bf, descsz) != descsz)
  1202. break;
  1203. } else {
  1204. int n = namesz + descsz;
  1205. if (read(fd, bf, n) != n)
  1206. break;
  1207. }
  1208. }
  1209. close(fd);
  1210. out:
  1211. return err;
  1212. }
  1213. char dso__symtab_origin(const struct dso *dso)
  1214. {
  1215. static const char origin[] = {
  1216. [SYMTAB__KALLSYMS] = 'k',
  1217. [SYMTAB__JAVA_JIT] = 'j',
  1218. [SYMTAB__BUILD_ID_CACHE] = 'B',
  1219. [SYMTAB__FEDORA_DEBUGINFO] = 'f',
  1220. [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
  1221. [SYMTAB__BUILDID_DEBUGINFO] = 'b',
  1222. [SYMTAB__SYSTEM_PATH_DSO] = 'd',
  1223. [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
  1224. [SYMTAB__GUEST_KALLSYMS] = 'g',
  1225. [SYMTAB__GUEST_KMODULE] = 'G',
  1226. };
  1227. if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
  1228. return '!';
  1229. return origin[dso->symtab_type];
  1230. }
  1231. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1232. {
  1233. int size = PATH_MAX;
  1234. char *name;
  1235. int ret = -1;
  1236. int fd;
  1237. struct machine *machine;
  1238. const char *root_dir;
  1239. int want_symtab;
  1240. dso__set_loaded(dso, map->type);
  1241. if (dso->kernel == DSO_TYPE_KERNEL)
  1242. return dso__load_kernel_sym(dso, map, filter);
  1243. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1244. return dso__load_guest_kernel_sym(dso, map, filter);
  1245. if (map->groups && map->groups->machine)
  1246. machine = map->groups->machine;
  1247. else
  1248. machine = NULL;
  1249. name = malloc(size);
  1250. if (!name)
  1251. return -1;
  1252. dso->adjust_symbols = 0;
  1253. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1254. struct stat st;
  1255. if (stat(dso->name, &st) < 0)
  1256. return -1;
  1257. if (st.st_uid && (st.st_uid != geteuid())) {
  1258. pr_warning("File %s not owned by current user or root, "
  1259. "ignoring it.\n", dso->name);
  1260. return -1;
  1261. }
  1262. ret = dso__load_perf_map(dso, map, filter);
  1263. dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
  1264. SYMTAB__NOT_FOUND;
  1265. return ret;
  1266. }
  1267. /* Iterate over candidate debug images.
  1268. * On the first pass, only load images if they have a full symtab.
  1269. * Failing that, do a second pass where we accept .dynsym also
  1270. */
  1271. want_symtab = 1;
  1272. restart:
  1273. for (dso->symtab_type = SYMTAB__BUILD_ID_CACHE;
  1274. dso->symtab_type != SYMTAB__NOT_FOUND;
  1275. dso->symtab_type++) {
  1276. switch (dso->symtab_type) {
  1277. case SYMTAB__BUILD_ID_CACHE:
  1278. /* skip the locally configured cache if a symfs is given */
  1279. if (symbol_conf.symfs[0] ||
  1280. (dso__build_id_filename(dso, name, size) == NULL)) {
  1281. continue;
  1282. }
  1283. break;
  1284. case SYMTAB__FEDORA_DEBUGINFO:
  1285. snprintf(name, size, "%s/usr/lib/debug%s.debug",
  1286. symbol_conf.symfs, dso->long_name);
  1287. break;
  1288. case SYMTAB__UBUNTU_DEBUGINFO:
  1289. snprintf(name, size, "%s/usr/lib/debug%s",
  1290. symbol_conf.symfs, dso->long_name);
  1291. break;
  1292. case SYMTAB__BUILDID_DEBUGINFO: {
  1293. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1294. if (!dso->has_build_id)
  1295. continue;
  1296. build_id__sprintf(dso->build_id,
  1297. sizeof(dso->build_id),
  1298. build_id_hex);
  1299. snprintf(name, size,
  1300. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1301. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1302. }
  1303. break;
  1304. case SYMTAB__SYSTEM_PATH_DSO:
  1305. snprintf(name, size, "%s%s",
  1306. symbol_conf.symfs, dso->long_name);
  1307. break;
  1308. case SYMTAB__GUEST_KMODULE:
  1309. if (map->groups && machine)
  1310. root_dir = machine->root_dir;
  1311. else
  1312. root_dir = "";
  1313. snprintf(name, size, "%s%s%s", symbol_conf.symfs,
  1314. root_dir, dso->long_name);
  1315. break;
  1316. case SYMTAB__SYSTEM_PATH_KMODULE:
  1317. snprintf(name, size, "%s%s", symbol_conf.symfs,
  1318. dso->long_name);
  1319. break;
  1320. default:;
  1321. }
  1322. /* Name is now the name of the next image to try */
  1323. fd = open(name, O_RDONLY);
  1324. if (fd < 0)
  1325. continue;
  1326. ret = dso__load_sym(dso, map, name, fd, filter, 0,
  1327. want_symtab);
  1328. close(fd);
  1329. /*
  1330. * Some people seem to have debuginfo files _WITHOUT_ debug
  1331. * info!?!?
  1332. */
  1333. if (!ret)
  1334. continue;
  1335. if (ret > 0) {
  1336. int nr_plt = dso__synthesize_plt_symbols(dso, map,
  1337. filter);
  1338. if (nr_plt > 0)
  1339. ret += nr_plt;
  1340. break;
  1341. }
  1342. }
  1343. /*
  1344. * If we wanted a full symtab but no image had one,
  1345. * relax our requirements and repeat the search.
  1346. */
  1347. if (ret <= 0 && want_symtab) {
  1348. want_symtab = 0;
  1349. goto restart;
  1350. }
  1351. free(name);
  1352. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1353. return 0;
  1354. return ret;
  1355. }
  1356. struct map *map_groups__find_by_name(struct map_groups *mg,
  1357. enum map_type type, const char *name)
  1358. {
  1359. struct rb_node *nd;
  1360. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1361. struct map *map = rb_entry(nd, struct map, rb_node);
  1362. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1363. return map;
  1364. }
  1365. return NULL;
  1366. }
  1367. static int dso__kernel_module_get_build_id(struct dso *dso,
  1368. const char *root_dir)
  1369. {
  1370. char filename[PATH_MAX];
  1371. /*
  1372. * kernel module short names are of the form "[module]" and
  1373. * we need just "module" here.
  1374. */
  1375. const char *name = dso->short_name + 1;
  1376. snprintf(filename, sizeof(filename),
  1377. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1378. root_dir, (int)strlen(name) - 1, name);
  1379. if (sysfs__read_build_id(filename, dso->build_id,
  1380. sizeof(dso->build_id)) == 0)
  1381. dso->has_build_id = true;
  1382. return 0;
  1383. }
  1384. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  1385. const char *dir_name)
  1386. {
  1387. struct dirent *dent;
  1388. DIR *dir = opendir(dir_name);
  1389. int ret = 0;
  1390. if (!dir) {
  1391. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1392. return -1;
  1393. }
  1394. while ((dent = readdir(dir)) != NULL) {
  1395. char path[PATH_MAX];
  1396. struct stat st;
  1397. /*sshfs might return bad dent->d_type, so we have to stat*/
  1398. sprintf(path, "%s/%s", dir_name, dent->d_name);
  1399. if (stat(path, &st))
  1400. continue;
  1401. if (S_ISDIR(st.st_mode)) {
  1402. if (!strcmp(dent->d_name, ".") ||
  1403. !strcmp(dent->d_name, ".."))
  1404. continue;
  1405. snprintf(path, sizeof(path), "%s/%s",
  1406. dir_name, dent->d_name);
  1407. ret = map_groups__set_modules_path_dir(mg, path);
  1408. if (ret < 0)
  1409. goto out;
  1410. } else {
  1411. char *dot = strrchr(dent->d_name, '.'),
  1412. dso_name[PATH_MAX];
  1413. struct map *map;
  1414. char *long_name;
  1415. if (dot == NULL || strcmp(dot, ".ko"))
  1416. continue;
  1417. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1418. (int)(dot - dent->d_name), dent->d_name);
  1419. strxfrchar(dso_name, '-', '_');
  1420. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  1421. dso_name);
  1422. if (map == NULL)
  1423. continue;
  1424. snprintf(path, sizeof(path), "%s/%s",
  1425. dir_name, dent->d_name);
  1426. long_name = strdup(path);
  1427. if (long_name == NULL) {
  1428. ret = -1;
  1429. goto out;
  1430. }
  1431. dso__set_long_name(map->dso, long_name);
  1432. map->dso->lname_alloc = 1;
  1433. dso__kernel_module_get_build_id(map->dso, "");
  1434. }
  1435. }
  1436. out:
  1437. closedir(dir);
  1438. return ret;
  1439. }
  1440. static char *get_kernel_version(const char *root_dir)
  1441. {
  1442. char version[PATH_MAX];
  1443. FILE *file;
  1444. char *name, *tmp;
  1445. const char *prefix = "Linux version ";
  1446. sprintf(version, "%s/proc/version", root_dir);
  1447. file = fopen(version, "r");
  1448. if (!file)
  1449. return NULL;
  1450. version[0] = '\0';
  1451. tmp = fgets(version, sizeof(version), file);
  1452. fclose(file);
  1453. name = strstr(version, prefix);
  1454. if (!name)
  1455. return NULL;
  1456. name += strlen(prefix);
  1457. tmp = strchr(name, ' ');
  1458. if (tmp)
  1459. *tmp = '\0';
  1460. return strdup(name);
  1461. }
  1462. static int machine__set_modules_path(struct machine *machine)
  1463. {
  1464. char *version;
  1465. char modules_path[PATH_MAX];
  1466. version = get_kernel_version(machine->root_dir);
  1467. if (!version)
  1468. return -1;
  1469. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1470. machine->root_dir, version);
  1471. free(version);
  1472. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1473. }
  1474. /*
  1475. * Constructor variant for modules (where we know from /proc/modules where
  1476. * they are loaded) and for vmlinux, where only after we load all the
  1477. * symbols we'll know where it starts and ends.
  1478. */
  1479. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1480. {
  1481. struct map *map = calloc(1, (sizeof(*map) +
  1482. (dso->kernel ? sizeof(struct kmap) : 0)));
  1483. if (map != NULL) {
  1484. /*
  1485. * ->end will be filled after we load all the symbols
  1486. */
  1487. map__init(map, type, start, 0, 0, dso);
  1488. }
  1489. return map;
  1490. }
  1491. struct map *machine__new_module(struct machine *machine, u64 start,
  1492. const char *filename)
  1493. {
  1494. struct map *map;
  1495. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1496. if (dso == NULL)
  1497. return NULL;
  1498. map = map__new2(start, dso, MAP__FUNCTION);
  1499. if (map == NULL)
  1500. return NULL;
  1501. if (machine__is_host(machine))
  1502. dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
  1503. else
  1504. dso->symtab_type = SYMTAB__GUEST_KMODULE;
  1505. map_groups__insert(&machine->kmaps, map);
  1506. return map;
  1507. }
  1508. static int machine__create_modules(struct machine *machine)
  1509. {
  1510. char *line = NULL;
  1511. size_t n;
  1512. FILE *file;
  1513. struct map *map;
  1514. const char *modules;
  1515. char path[PATH_MAX];
  1516. if (machine__is_default_guest(machine))
  1517. modules = symbol_conf.default_guest_modules;
  1518. else {
  1519. sprintf(path, "%s/proc/modules", machine->root_dir);
  1520. modules = path;
  1521. }
  1522. if (symbol__restricted_filename(path, "/proc/modules"))
  1523. return -1;
  1524. file = fopen(modules, "r");
  1525. if (file == NULL)
  1526. return -1;
  1527. while (!feof(file)) {
  1528. char name[PATH_MAX];
  1529. u64 start;
  1530. char *sep;
  1531. int line_len;
  1532. line_len = getline(&line, &n, file);
  1533. if (line_len < 0)
  1534. break;
  1535. if (!line)
  1536. goto out_failure;
  1537. line[--line_len] = '\0'; /* \n */
  1538. sep = strrchr(line, 'x');
  1539. if (sep == NULL)
  1540. continue;
  1541. hex2u64(sep + 1, &start);
  1542. sep = strchr(line, ' ');
  1543. if (sep == NULL)
  1544. continue;
  1545. *sep = '\0';
  1546. snprintf(name, sizeof(name), "[%s]", line);
  1547. map = machine__new_module(machine, start, name);
  1548. if (map == NULL)
  1549. goto out_delete_line;
  1550. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1551. }
  1552. free(line);
  1553. fclose(file);
  1554. return machine__set_modules_path(machine);
  1555. out_delete_line:
  1556. free(line);
  1557. out_failure:
  1558. return -1;
  1559. }
  1560. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1561. const char *vmlinux, symbol_filter_t filter)
  1562. {
  1563. int err = -1, fd;
  1564. char symfs_vmlinux[PATH_MAX];
  1565. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1566. symbol_conf.symfs, vmlinux);
  1567. fd = open(symfs_vmlinux, O_RDONLY);
  1568. if (fd < 0)
  1569. return -1;
  1570. dso__set_long_name(dso, (char *)vmlinux);
  1571. dso__set_loaded(dso, map->type);
  1572. err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
  1573. close(fd);
  1574. if (err > 0)
  1575. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1576. return err;
  1577. }
  1578. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1579. symbol_filter_t filter)
  1580. {
  1581. int i, err = 0;
  1582. char *filename;
  1583. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1584. vmlinux_path__nr_entries + 1);
  1585. filename = dso__build_id_filename(dso, NULL, 0);
  1586. if (filename != NULL) {
  1587. err = dso__load_vmlinux(dso, map, filename, filter);
  1588. if (err > 0) {
  1589. dso__set_long_name(dso, filename);
  1590. goto out;
  1591. }
  1592. free(filename);
  1593. }
  1594. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1595. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1596. if (err > 0) {
  1597. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1598. break;
  1599. }
  1600. }
  1601. out:
  1602. return err;
  1603. }
  1604. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1605. symbol_filter_t filter)
  1606. {
  1607. int err;
  1608. const char *kallsyms_filename = NULL;
  1609. char *kallsyms_allocated_filename = NULL;
  1610. /*
  1611. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1612. * it and only it, reporting errors to the user if it cannot be used.
  1613. *
  1614. * For instance, try to analyse an ARM perf.data file _without_ a
  1615. * build-id, or if the user specifies the wrong path to the right
  1616. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1617. * x86_86 one, on the machine where analysis is being performed, say),
  1618. * or worse, /proc/kallsyms.
  1619. *
  1620. * If the specified file _has_ a build-id and there is a build-id
  1621. * section in the perf.data file, we will still do the expected
  1622. * validation in dso__load_vmlinux and will bail out if they don't
  1623. * match.
  1624. */
  1625. if (symbol_conf.kallsyms_name != NULL) {
  1626. kallsyms_filename = symbol_conf.kallsyms_name;
  1627. goto do_kallsyms;
  1628. }
  1629. if (symbol_conf.vmlinux_name != NULL) {
  1630. err = dso__load_vmlinux(dso, map,
  1631. symbol_conf.vmlinux_name, filter);
  1632. if (err > 0) {
  1633. dso__set_long_name(dso,
  1634. strdup(symbol_conf.vmlinux_name));
  1635. goto out_fixup;
  1636. }
  1637. return err;
  1638. }
  1639. if (vmlinux_path != NULL) {
  1640. err = dso__load_vmlinux_path(dso, map, filter);
  1641. if (err > 0)
  1642. goto out_fixup;
  1643. }
  1644. /* do not try local files if a symfs was given */
  1645. if (symbol_conf.symfs[0] != 0)
  1646. return -1;
  1647. /*
  1648. * Say the kernel DSO was created when processing the build-id header table,
  1649. * we have a build-id, so check if it is the same as the running kernel,
  1650. * using it if it is.
  1651. */
  1652. if (dso->has_build_id) {
  1653. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1654. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1655. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1656. sizeof(kallsyms_build_id)) == 0) {
  1657. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1658. kallsyms_filename = "/proc/kallsyms";
  1659. goto do_kallsyms;
  1660. }
  1661. }
  1662. /*
  1663. * Now look if we have it on the build-id cache in
  1664. * $HOME/.debug/[kernel.kallsyms].
  1665. */
  1666. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1667. sbuild_id);
  1668. if (asprintf(&kallsyms_allocated_filename,
  1669. "%s/.debug/[kernel.kallsyms]/%s",
  1670. getenv("HOME"), sbuild_id) == -1) {
  1671. pr_err("Not enough memory for kallsyms file lookup\n");
  1672. return -1;
  1673. }
  1674. kallsyms_filename = kallsyms_allocated_filename;
  1675. if (access(kallsyms_filename, F_OK)) {
  1676. pr_err("No kallsyms or vmlinux with build-id %s "
  1677. "was found\n", sbuild_id);
  1678. free(kallsyms_allocated_filename);
  1679. return -1;
  1680. }
  1681. } else {
  1682. /*
  1683. * Last resort, if we don't have a build-id and couldn't find
  1684. * any vmlinux file, try the running kernel kallsyms table.
  1685. */
  1686. kallsyms_filename = "/proc/kallsyms";
  1687. }
  1688. do_kallsyms:
  1689. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1690. if (err > 0)
  1691. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1692. free(kallsyms_allocated_filename);
  1693. if (err > 0) {
  1694. out_fixup:
  1695. if (kallsyms_filename != NULL)
  1696. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1697. map__fixup_start(map);
  1698. map__fixup_end(map);
  1699. }
  1700. return err;
  1701. }
  1702. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1703. symbol_filter_t filter)
  1704. {
  1705. int err;
  1706. const char *kallsyms_filename = NULL;
  1707. struct machine *machine;
  1708. char path[PATH_MAX];
  1709. if (!map->groups) {
  1710. pr_debug("Guest kernel map hasn't the point to groups\n");
  1711. return -1;
  1712. }
  1713. machine = map->groups->machine;
  1714. if (machine__is_default_guest(machine)) {
  1715. /*
  1716. * if the user specified a vmlinux filename, use it and only
  1717. * it, reporting errors to the user if it cannot be used.
  1718. * Or use file guest_kallsyms inputted by user on commandline
  1719. */
  1720. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1721. err = dso__load_vmlinux(dso, map,
  1722. symbol_conf.default_guest_vmlinux_name, filter);
  1723. goto out_try_fixup;
  1724. }
  1725. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1726. if (!kallsyms_filename)
  1727. return -1;
  1728. } else {
  1729. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1730. kallsyms_filename = path;
  1731. }
  1732. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1733. if (err > 0)
  1734. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1735. out_try_fixup:
  1736. if (err > 0) {
  1737. if (kallsyms_filename != NULL) {
  1738. machine__mmap_name(machine, path, sizeof(path));
  1739. dso__set_long_name(dso, strdup(path));
  1740. }
  1741. map__fixup_start(map);
  1742. map__fixup_end(map);
  1743. }
  1744. return err;
  1745. }
  1746. static void dsos__add(struct list_head *head, struct dso *dso)
  1747. {
  1748. list_add_tail(&dso->node, head);
  1749. }
  1750. static struct dso *dsos__find(struct list_head *head, const char *name)
  1751. {
  1752. struct dso *pos;
  1753. list_for_each_entry(pos, head, node)
  1754. if (strcmp(pos->long_name, name) == 0)
  1755. return pos;
  1756. return NULL;
  1757. }
  1758. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1759. {
  1760. struct dso *dso = dsos__find(head, name);
  1761. if (!dso) {
  1762. dso = dso__new(name);
  1763. if (dso != NULL) {
  1764. dsos__add(head, dso);
  1765. dso__set_basename(dso);
  1766. }
  1767. }
  1768. return dso;
  1769. }
  1770. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1771. {
  1772. struct dso *pos;
  1773. size_t ret = 0;
  1774. list_for_each_entry(pos, head, node) {
  1775. int i;
  1776. for (i = 0; i < MAP__NR_TYPES; ++i)
  1777. ret += dso__fprintf(pos, i, fp);
  1778. }
  1779. return ret;
  1780. }
  1781. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1782. {
  1783. struct rb_node *nd;
  1784. size_t ret = 0;
  1785. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1786. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1787. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1788. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1789. }
  1790. return ret;
  1791. }
  1792. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1793. bool with_hits)
  1794. {
  1795. struct dso *pos;
  1796. size_t ret = 0;
  1797. list_for_each_entry(pos, head, node) {
  1798. if (with_hits && !pos->hit)
  1799. continue;
  1800. ret += dso__fprintf_buildid(pos, fp);
  1801. ret += fprintf(fp, " %s\n", pos->long_name);
  1802. }
  1803. return ret;
  1804. }
  1805. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  1806. bool with_hits)
  1807. {
  1808. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  1809. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  1810. }
  1811. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  1812. FILE *fp, bool with_hits)
  1813. {
  1814. struct rb_node *nd;
  1815. size_t ret = 0;
  1816. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1817. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1818. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  1819. }
  1820. return ret;
  1821. }
  1822. struct dso *dso__new_kernel(const char *name)
  1823. {
  1824. struct dso *dso = dso__new(name ?: "[kernel.kallsyms]");
  1825. if (dso != NULL) {
  1826. dso__set_short_name(dso, "[kernel]");
  1827. dso->kernel = DSO_TYPE_KERNEL;
  1828. }
  1829. return dso;
  1830. }
  1831. static struct dso *dso__new_guest_kernel(struct machine *machine,
  1832. const char *name)
  1833. {
  1834. char bf[PATH_MAX];
  1835. struct dso *dso = dso__new(name ?: machine__mmap_name(machine, bf,
  1836. sizeof(bf)));
  1837. if (dso != NULL) {
  1838. dso__set_short_name(dso, "[guest.kernel]");
  1839. dso->kernel = DSO_TYPE_GUEST_KERNEL;
  1840. }
  1841. return dso;
  1842. }
  1843. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1844. {
  1845. char path[PATH_MAX];
  1846. if (machine__is_default_guest(machine))
  1847. return;
  1848. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1849. if (sysfs__read_build_id(path, dso->build_id,
  1850. sizeof(dso->build_id)) == 0)
  1851. dso->has_build_id = true;
  1852. }
  1853. static struct dso *machine__create_kernel(struct machine *machine)
  1854. {
  1855. const char *vmlinux_name = NULL;
  1856. struct dso *kernel;
  1857. if (machine__is_host(machine)) {
  1858. vmlinux_name = symbol_conf.vmlinux_name;
  1859. kernel = dso__new_kernel(vmlinux_name);
  1860. } else {
  1861. if (machine__is_default_guest(machine))
  1862. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  1863. kernel = dso__new_guest_kernel(machine, vmlinux_name);
  1864. }
  1865. if (kernel != NULL) {
  1866. dso__read_running_kernel_build_id(kernel, machine);
  1867. dsos__add(&machine->kernel_dsos, kernel);
  1868. }
  1869. return kernel;
  1870. }
  1871. struct process_args {
  1872. u64 start;
  1873. };
  1874. static int symbol__in_kernel(void *arg, const char *name,
  1875. char type __used, u64 start, u64 end __used)
  1876. {
  1877. struct process_args *args = arg;
  1878. if (strchr(name, '['))
  1879. return 0;
  1880. args->start = start;
  1881. return 1;
  1882. }
  1883. /* Figure out the start address of kernel map from /proc/kallsyms */
  1884. static u64 machine__get_kernel_start_addr(struct machine *machine)
  1885. {
  1886. const char *filename;
  1887. char path[PATH_MAX];
  1888. struct process_args args;
  1889. if (machine__is_host(machine)) {
  1890. filename = "/proc/kallsyms";
  1891. } else {
  1892. if (machine__is_default_guest(machine))
  1893. filename = (char *)symbol_conf.default_guest_kallsyms;
  1894. else {
  1895. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1896. filename = path;
  1897. }
  1898. }
  1899. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  1900. return 0;
  1901. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  1902. return 0;
  1903. return args.start;
  1904. }
  1905. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  1906. {
  1907. enum map_type type;
  1908. u64 start = machine__get_kernel_start_addr(machine);
  1909. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1910. struct kmap *kmap;
  1911. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  1912. if (machine->vmlinux_maps[type] == NULL)
  1913. return -1;
  1914. machine->vmlinux_maps[type]->map_ip =
  1915. machine->vmlinux_maps[type]->unmap_ip =
  1916. identity__map_ip;
  1917. kmap = map__kmap(machine->vmlinux_maps[type]);
  1918. kmap->kmaps = &machine->kmaps;
  1919. map_groups__insert(&machine->kmaps,
  1920. machine->vmlinux_maps[type]);
  1921. }
  1922. return 0;
  1923. }
  1924. void machine__destroy_kernel_maps(struct machine *machine)
  1925. {
  1926. enum map_type type;
  1927. for (type = 0; type < MAP__NR_TYPES; ++type) {
  1928. struct kmap *kmap;
  1929. if (machine->vmlinux_maps[type] == NULL)
  1930. continue;
  1931. kmap = map__kmap(machine->vmlinux_maps[type]);
  1932. map_groups__remove(&machine->kmaps,
  1933. machine->vmlinux_maps[type]);
  1934. if (kmap->ref_reloc_sym) {
  1935. /*
  1936. * ref_reloc_sym is shared among all maps, so free just
  1937. * on one of them.
  1938. */
  1939. if (type == MAP__FUNCTION) {
  1940. free((char *)kmap->ref_reloc_sym->name);
  1941. kmap->ref_reloc_sym->name = NULL;
  1942. free(kmap->ref_reloc_sym);
  1943. }
  1944. kmap->ref_reloc_sym = NULL;
  1945. }
  1946. map__delete(machine->vmlinux_maps[type]);
  1947. machine->vmlinux_maps[type] = NULL;
  1948. }
  1949. }
  1950. int machine__create_kernel_maps(struct machine *machine)
  1951. {
  1952. struct dso *kernel = machine__create_kernel(machine);
  1953. if (kernel == NULL ||
  1954. __machine__create_kernel_maps(machine, kernel) < 0)
  1955. return -1;
  1956. if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
  1957. pr_debug("Problems creating module maps, continuing anyway...\n");
  1958. /*
  1959. * Now that we have all the maps created, just set the ->end of them:
  1960. */
  1961. map_groups__fixup_end(&machine->kmaps);
  1962. return 0;
  1963. }
  1964. static void vmlinux_path__exit(void)
  1965. {
  1966. while (--vmlinux_path__nr_entries >= 0) {
  1967. free(vmlinux_path[vmlinux_path__nr_entries]);
  1968. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1969. }
  1970. free(vmlinux_path);
  1971. vmlinux_path = NULL;
  1972. }
  1973. static int vmlinux_path__init(void)
  1974. {
  1975. struct utsname uts;
  1976. char bf[PATH_MAX];
  1977. vmlinux_path = malloc(sizeof(char *) * 5);
  1978. if (vmlinux_path == NULL)
  1979. return -1;
  1980. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1981. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1982. goto out_fail;
  1983. ++vmlinux_path__nr_entries;
  1984. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1985. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1986. goto out_fail;
  1987. ++vmlinux_path__nr_entries;
  1988. /* only try running kernel version if no symfs was given */
  1989. if (symbol_conf.symfs[0] != 0)
  1990. return 0;
  1991. if (uname(&uts) < 0)
  1992. return -1;
  1993. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1994. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1995. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1996. goto out_fail;
  1997. ++vmlinux_path__nr_entries;
  1998. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1999. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2000. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2001. goto out_fail;
  2002. ++vmlinux_path__nr_entries;
  2003. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  2004. uts.release);
  2005. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2006. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2007. goto out_fail;
  2008. ++vmlinux_path__nr_entries;
  2009. return 0;
  2010. out_fail:
  2011. vmlinux_path__exit();
  2012. return -1;
  2013. }
  2014. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  2015. {
  2016. int i;
  2017. size_t printed = 0;
  2018. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  2019. if (kdso->has_build_id) {
  2020. char filename[PATH_MAX];
  2021. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  2022. printed += fprintf(fp, "[0] %s\n", filename);
  2023. }
  2024. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  2025. printed += fprintf(fp, "[%d] %s\n",
  2026. i + kdso->has_build_id, vmlinux_path[i]);
  2027. return printed;
  2028. }
  2029. static int setup_list(struct strlist **list, const char *list_str,
  2030. const char *list_name)
  2031. {
  2032. if (list_str == NULL)
  2033. return 0;
  2034. *list = strlist__new(true, list_str);
  2035. if (!*list) {
  2036. pr_err("problems parsing %s list\n", list_name);
  2037. return -1;
  2038. }
  2039. return 0;
  2040. }
  2041. static bool symbol__read_kptr_restrict(void)
  2042. {
  2043. bool value = false;
  2044. if (geteuid() != 0) {
  2045. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  2046. if (fp != NULL) {
  2047. char line[8];
  2048. if (fgets(line, sizeof(line), fp) != NULL)
  2049. value = atoi(line) != 0;
  2050. fclose(fp);
  2051. }
  2052. }
  2053. return value;
  2054. }
  2055. int symbol__init(void)
  2056. {
  2057. const char *symfs;
  2058. if (symbol_conf.initialized)
  2059. return 0;
  2060. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2061. elf_version(EV_CURRENT);
  2062. if (symbol_conf.sort_by_name)
  2063. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2064. sizeof(struct symbol));
  2065. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2066. return -1;
  2067. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2068. pr_err("'.' is the only non valid --field-separator argument\n");
  2069. return -1;
  2070. }
  2071. if (setup_list(&symbol_conf.dso_list,
  2072. symbol_conf.dso_list_str, "dso") < 0)
  2073. return -1;
  2074. if (setup_list(&symbol_conf.comm_list,
  2075. symbol_conf.comm_list_str, "comm") < 0)
  2076. goto out_free_dso_list;
  2077. if (setup_list(&symbol_conf.sym_list,
  2078. symbol_conf.sym_list_str, "symbol") < 0)
  2079. goto out_free_comm_list;
  2080. /*
  2081. * A path to symbols of "/" is identical to ""
  2082. * reset here for simplicity.
  2083. */
  2084. symfs = realpath(symbol_conf.symfs, NULL);
  2085. if (symfs == NULL)
  2086. symfs = symbol_conf.symfs;
  2087. if (strcmp(symfs, "/") == 0)
  2088. symbol_conf.symfs = "";
  2089. if (symfs != symbol_conf.symfs)
  2090. free((void *)symfs);
  2091. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  2092. symbol_conf.initialized = true;
  2093. return 0;
  2094. out_free_dso_list:
  2095. strlist__delete(symbol_conf.dso_list);
  2096. out_free_comm_list:
  2097. strlist__delete(symbol_conf.comm_list);
  2098. return -1;
  2099. }
  2100. void symbol__exit(void)
  2101. {
  2102. if (!symbol_conf.initialized)
  2103. return;
  2104. strlist__delete(symbol_conf.sym_list);
  2105. strlist__delete(symbol_conf.dso_list);
  2106. strlist__delete(symbol_conf.comm_list);
  2107. vmlinux_path__exit();
  2108. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2109. symbol_conf.initialized = false;
  2110. }
  2111. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  2112. {
  2113. struct machine *machine = machines__findnew(machines, pid);
  2114. if (machine == NULL)
  2115. return -1;
  2116. return machine__create_kernel_maps(machine);
  2117. }
  2118. static int hex(char ch)
  2119. {
  2120. if ((ch >= '0') && (ch <= '9'))
  2121. return ch - '0';
  2122. if ((ch >= 'a') && (ch <= 'f'))
  2123. return ch - 'a' + 10;
  2124. if ((ch >= 'A') && (ch <= 'F'))
  2125. return ch - 'A' + 10;
  2126. return -1;
  2127. }
  2128. /*
  2129. * While we find nice hex chars, build a long_val.
  2130. * Return number of chars processed.
  2131. */
  2132. int hex2u64(const char *ptr, u64 *long_val)
  2133. {
  2134. const char *p = ptr;
  2135. *long_val = 0;
  2136. while (*p) {
  2137. const int hex_val = hex(*p);
  2138. if (hex_val < 0)
  2139. break;
  2140. *long_val = (*long_val << 4) | hex_val;
  2141. p++;
  2142. }
  2143. return p - ptr;
  2144. }
  2145. char *strxfrchar(char *s, char from, char to)
  2146. {
  2147. char *p = s;
  2148. while ((p = strchr(p, from)) != NULL)
  2149. *p++ = to;
  2150. return s;
  2151. }
  2152. int machines__create_guest_kernel_maps(struct rb_root *machines)
  2153. {
  2154. int ret = 0;
  2155. struct dirent **namelist = NULL;
  2156. int i, items = 0;
  2157. char path[PATH_MAX];
  2158. pid_t pid;
  2159. if (symbol_conf.default_guest_vmlinux_name ||
  2160. symbol_conf.default_guest_modules ||
  2161. symbol_conf.default_guest_kallsyms) {
  2162. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  2163. }
  2164. if (symbol_conf.guestmount) {
  2165. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2166. if (items <= 0)
  2167. return -ENOENT;
  2168. for (i = 0; i < items; i++) {
  2169. if (!isdigit(namelist[i]->d_name[0])) {
  2170. /* Filter out . and .. */
  2171. continue;
  2172. }
  2173. pid = atoi(namelist[i]->d_name);
  2174. sprintf(path, "%s/%s/proc/kallsyms",
  2175. symbol_conf.guestmount,
  2176. namelist[i]->d_name);
  2177. ret = access(path, R_OK);
  2178. if (ret) {
  2179. pr_debug("Can't access file %s\n", path);
  2180. goto failure;
  2181. }
  2182. machines__create_kernel_maps(machines, pid);
  2183. }
  2184. failure:
  2185. free(namelist);
  2186. }
  2187. return ret;
  2188. }
  2189. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  2190. {
  2191. struct rb_node *next = rb_first(machines);
  2192. while (next) {
  2193. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2194. next = rb_next(&pos->rb_node);
  2195. rb_erase(&pos->rb_node, machines);
  2196. machine__delete(pos);
  2197. }
  2198. }
  2199. int machine__load_kallsyms(struct machine *machine, const char *filename,
  2200. enum map_type type, symbol_filter_t filter)
  2201. {
  2202. struct map *map = machine->vmlinux_maps[type];
  2203. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2204. if (ret > 0) {
  2205. dso__set_loaded(map->dso, type);
  2206. /*
  2207. * Since /proc/kallsyms will have multiple sessions for the
  2208. * kernel, with modules between them, fixup the end of all
  2209. * sections.
  2210. */
  2211. __map_groups__fixup_end(&machine->kmaps, type);
  2212. }
  2213. return ret;
  2214. }
  2215. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  2216. symbol_filter_t filter)
  2217. {
  2218. struct map *map = machine->vmlinux_maps[type];
  2219. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2220. if (ret > 0) {
  2221. dso__set_loaded(map->dso, type);
  2222. map__reloc_vmlinux(map);
  2223. }
  2224. return ret;
  2225. }