ctf-lookup.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /* Symbol, variable and name lookup.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctf-impl.h>
  16. #include <elf.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. /* Grow the pptrtab so that it is at least NEW_LEN long. */
  20. static int
  21. grow_pptrtab (ctf_dict_t *fp, size_t new_len)
  22. {
  23. uint32_t *new_pptrtab;
  24. if ((new_pptrtab = realloc (fp->ctf_pptrtab, sizeof (uint32_t)
  25. * new_len)) == NULL)
  26. return (ctf_set_errno (fp, ENOMEM));
  27. fp->ctf_pptrtab = new_pptrtab;
  28. memset (fp->ctf_pptrtab + fp->ctf_pptrtab_len, 0,
  29. sizeof (uint32_t) * (new_len - fp->ctf_pptrtab_len));
  30. fp->ctf_pptrtab_len = new_len;
  31. return 0;
  32. }
  33. /* Update entries in the pptrtab that relate to types newly added in the
  34. child. */
  35. static int
  36. refresh_pptrtab (ctf_dict_t *fp, ctf_dict_t *pfp)
  37. {
  38. uint32_t i;
  39. for (i = fp->ctf_pptrtab_typemax; i <= fp->ctf_typemax; i++)
  40. {
  41. ctf_id_t type = LCTF_INDEX_TO_TYPE (fp, i, 1);
  42. ctf_id_t reffed_type;
  43. if (ctf_type_kind (fp, type) != CTF_K_POINTER)
  44. continue;
  45. reffed_type = ctf_type_reference (fp, type);
  46. if (LCTF_TYPE_ISPARENT (fp, reffed_type))
  47. {
  48. uint32_t idx = LCTF_TYPE_TO_INDEX (fp, reffed_type);
  49. /* Guard against references to invalid types. No need to consider
  50. the CTF dict corrupt in this case: this pointer just can't be a
  51. pointer to any type we know about. */
  52. if (idx <= pfp->ctf_typemax)
  53. {
  54. if (idx >= fp->ctf_pptrtab_len
  55. && grow_pptrtab (fp, pfp->ctf_ptrtab_len) < 0)
  56. return -1; /* errno is set for us. */
  57. fp->ctf_pptrtab[idx] = i;
  58. }
  59. }
  60. }
  61. fp->ctf_pptrtab_typemax = fp->ctf_typemax;
  62. return 0;
  63. }
  64. /* Compare the given input string and length against a table of known C storage
  65. qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
  66. do this quickly, we use a pre-computed Perfect Hash Function similar to the
  67. technique originally described in the classic paper:
  68. R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
  69. Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
  70. For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
  71. for the current set of qualifiers yields a unique H in the range [0 .. 20].
  72. The hash can be modified when the keyword set changes as necessary. We also
  73. store the length of each keyword and check it prior to the final strcmp().
  74. TODO: just use gperf. */
  75. static int
  76. isqualifier (const char *s, size_t len)
  77. {
  78. static const struct qual
  79. {
  80. const char *q_name;
  81. size_t q_len;
  82. } qhash[] = {
  83. {"static", 6}, {"", 0}, {"", 0}, {"", 0},
  84. {"volatile", 8}, {"", 0}, {"", 0}, {"", 0}, {"", 0},
  85. {"", 0}, {"auto", 4}, {"extern", 6}, {"", 0}, {"", 0},
  86. {"", 0}, {"", 0}, {"const", 5}, {"register", 8},
  87. {"", 0}, {"restrict", 8}, {"_Restrict", 9}
  88. };
  89. int h = s[len - 1] + (int) len - 105;
  90. const struct qual *qp;
  91. if (h < 0 || (size_t) h >= sizeof (qhash) / sizeof (qhash[0]))
  92. return 0;
  93. qp = &qhash[h];
  94. return ((size_t) len == qp->q_len &&
  95. strncmp (qp->q_name, s, qp->q_len) == 0);
  96. }
  97. /* Attempt to convert the given C type name into the corresponding CTF type ID.
  98. It is not possible to do complete and proper conversion of type names
  99. without implementing a more full-fledged parser, which is necessary to
  100. handle things like types that are function pointers to functions that
  101. have arguments that are function pointers, and fun stuff like that.
  102. Instead, this function implements a very simple conversion algorithm that
  103. finds the things that we actually care about: structs, unions, enums,
  104. integers, floats, typedefs, and pointers to any of these named types. */
  105. static ctf_id_t
  106. ctf_lookup_by_name_internal (ctf_dict_t *fp, ctf_dict_t *child,
  107. const char *name)
  108. {
  109. static const char delimiters[] = " \t\n\r\v\f*";
  110. const ctf_lookup_t *lp;
  111. const char *p, *q, *end;
  112. ctf_id_t type = 0;
  113. ctf_id_t ntype, ptype;
  114. if (name == NULL)
  115. return (ctf_set_errno (fp, EINVAL));
  116. for (p = name, end = name + strlen (name); *p != '\0'; p = q)
  117. {
  118. while (isspace ((int) *p))
  119. p++; /* Skip leading whitespace. */
  120. if (p == end)
  121. break;
  122. if ((q = strpbrk (p + 1, delimiters)) == NULL)
  123. q = end; /* Compare until end. */
  124. if (*p == '*')
  125. {
  126. /* Find a pointer to type by looking in child->ctf_pptrtab (if child
  127. is set) and fp->ctf_ptrtab. If we can't find a pointer to the
  128. given type, see if we can compute a pointer to the type resulting
  129. from resolving the type down to its base type and use that instead.
  130. This helps with cases where the CTF data includes "struct foo *"
  131. but not "foo_t *" and the user tries to access "foo_t *" in the
  132. debugger.
  133. There is extra complexity here because uninitialized elements in
  134. the pptrtab and ptrtab are set to zero, but zero (as the type ID
  135. meaning the unimplemented type) is a valid return type from
  136. ctf_lookup_by_name. (Pointers to types are never of type 0, so
  137. this is unambiguous, just fiddly to deal with.) */
  138. uint32_t idx = LCTF_TYPE_TO_INDEX (fp, type);
  139. int in_child = 0;
  140. ntype = CTF_ERR;
  141. if (child && idx < child->ctf_pptrtab_len)
  142. {
  143. ntype = child->ctf_pptrtab[idx];
  144. if (ntype)
  145. in_child = 1;
  146. else
  147. ntype = CTF_ERR;
  148. }
  149. if (ntype == CTF_ERR)
  150. {
  151. ntype = fp->ctf_ptrtab[idx];
  152. if (ntype == 0)
  153. ntype = CTF_ERR;
  154. }
  155. /* Try resolving to its base type and check again. */
  156. if (ntype == CTF_ERR)
  157. {
  158. if (child)
  159. ntype = ctf_type_resolve_unsliced (child, type);
  160. else
  161. ntype = ctf_type_resolve_unsliced (fp, type);
  162. if (ntype == CTF_ERR)
  163. goto notype;
  164. idx = LCTF_TYPE_TO_INDEX (fp, ntype);
  165. ntype = CTF_ERR;
  166. if (child && idx < child->ctf_pptrtab_len)
  167. {
  168. ntype = child->ctf_pptrtab[idx];
  169. if (ntype)
  170. in_child = 1;
  171. else
  172. ntype = CTF_ERR;
  173. }
  174. if (ntype == CTF_ERR)
  175. {
  176. ntype = fp->ctf_ptrtab[idx];
  177. if (ntype == 0)
  178. ntype = CTF_ERR;
  179. }
  180. if (ntype == CTF_ERR)
  181. goto notype;
  182. }
  183. type = LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD)
  184. || in_child);
  185. /* We are looking up a type in the parent, but the pointed-to type is
  186. in the child. Switch to looking in the child: if we need to go
  187. back into the parent, we can recurse again. */
  188. if (in_child)
  189. {
  190. fp = child;
  191. child = NULL;
  192. }
  193. q = p + 1;
  194. continue;
  195. }
  196. if (isqualifier (p, (size_t) (q - p)))
  197. continue; /* Skip qualifier keyword. */
  198. for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++)
  199. {
  200. /* TODO: This is not MT-safe. */
  201. if ((lp->ctl_prefix[0] == '\0' ||
  202. strncmp (p, lp->ctl_prefix, (size_t) (q - p)) == 0) &&
  203. (size_t) (q - p) >= lp->ctl_len)
  204. {
  205. for (p += lp->ctl_len; isspace ((int) *p); p++)
  206. continue; /* Skip prefix and next whitespace. */
  207. if ((q = strchr (p, '*')) == NULL)
  208. q = end; /* Compare until end. */
  209. while (isspace ((int) q[-1]))
  210. q--; /* Exclude trailing whitespace. */
  211. /* Expand and/or allocate storage for a slice of the name, then
  212. copy it in. */
  213. if (fp->ctf_tmp_typeslicelen >= (size_t) (q - p) + 1)
  214. {
  215. memcpy (fp->ctf_tmp_typeslice, p, (size_t) (q - p));
  216. fp->ctf_tmp_typeslice[(size_t) (q - p)] = '\0';
  217. }
  218. else
  219. {
  220. free (fp->ctf_tmp_typeslice);
  221. fp->ctf_tmp_typeslice = xstrndup (p, (size_t) (q - p));
  222. if (fp->ctf_tmp_typeslice == NULL)
  223. {
  224. ctf_set_errno (fp, ENOMEM);
  225. return CTF_ERR;
  226. }
  227. }
  228. if ((type = ctf_lookup_by_rawhash (fp, lp->ctl_hash,
  229. fp->ctf_tmp_typeslice)) == 0)
  230. goto notype;
  231. break;
  232. }
  233. }
  234. if (lp->ctl_prefix == NULL)
  235. goto notype;
  236. }
  237. if (*p != '\0' || type == 0)
  238. return (ctf_set_errno (fp, ECTF_SYNTAX));
  239. return type;
  240. notype:
  241. ctf_set_errno (fp, ECTF_NOTYPE);
  242. if (fp->ctf_parent != NULL)
  243. {
  244. /* Need to look up in the parent, from the child's perspective.
  245. Make sure the pptrtab is up to date. */
  246. if (fp->ctf_pptrtab_typemax < fp->ctf_typemax)
  247. {
  248. if (refresh_pptrtab (fp, fp->ctf_parent) < 0)
  249. return -1; /* errno is set for us. */
  250. }
  251. if ((ptype = ctf_lookup_by_name_internal (fp->ctf_parent, fp,
  252. name)) != CTF_ERR)
  253. return ptype;
  254. return (ctf_set_errno (fp, ctf_errno (fp->ctf_parent)));
  255. }
  256. return CTF_ERR;
  257. }
  258. ctf_id_t
  259. ctf_lookup_by_name (ctf_dict_t *fp, const char *name)
  260. {
  261. return ctf_lookup_by_name_internal (fp, NULL, name);
  262. }
  263. /* Return the pointer to the internal CTF type data corresponding to the
  264. given type ID. If the ID is invalid, the function returns NULL.
  265. This function is not exported outside of the library. */
  266. const ctf_type_t *
  267. ctf_lookup_by_id (ctf_dict_t **fpp, ctf_id_t type)
  268. {
  269. ctf_dict_t *fp = *fpp; /* Caller passes in starting CTF dict. */
  270. ctf_id_t idx;
  271. if ((fp = ctf_get_dict (fp, type)) == NULL)
  272. {
  273. (void) ctf_set_errno (*fpp, ECTF_NOPARENT);
  274. return NULL;
  275. }
  276. /* If this dict is writable, check for a dynamic type. */
  277. if (fp->ctf_flags & LCTF_RDWR)
  278. {
  279. ctf_dtdef_t *dtd;
  280. if ((dtd = ctf_dynamic_type (fp, type)) != NULL)
  281. {
  282. *fpp = fp;
  283. return &dtd->dtd_data;
  284. }
  285. (void) ctf_set_errno (*fpp, ECTF_BADID);
  286. return NULL;
  287. }
  288. /* Check for a type in the static portion. */
  289. idx = LCTF_TYPE_TO_INDEX (fp, type);
  290. if (idx > 0 && (unsigned long) idx <= fp->ctf_typemax)
  291. {
  292. *fpp = fp; /* Function returns ending CTF dict. */
  293. return (LCTF_INDEX_TO_TYPEPTR (fp, idx));
  294. }
  295. (void) ctf_set_errno (*fpp, ECTF_BADID);
  296. return NULL;
  297. }
  298. typedef struct ctf_lookup_idx_key
  299. {
  300. ctf_dict_t *clik_fp;
  301. const char *clik_name;
  302. uint32_t *clik_names;
  303. } ctf_lookup_idx_key_t;
  304. /* A bsearch function for variable names. */
  305. static int
  306. ctf_lookup_var (const void *key_, const void *lookup_)
  307. {
  308. const ctf_lookup_idx_key_t *key = key_;
  309. const ctf_varent_t *lookup = lookup_;
  310. return (strcmp (key->clik_name, ctf_strptr (key->clik_fp, lookup->ctv_name)));
  311. }
  312. /* Given a variable name, return the type of the variable with that name. */
  313. ctf_id_t
  314. ctf_lookup_variable (ctf_dict_t *fp, const char *name)
  315. {
  316. ctf_varent_t *ent;
  317. ctf_lookup_idx_key_t key = { fp, name, NULL };
  318. /* This array is sorted, so we can bsearch for it. */
  319. ent = bsearch (&key, fp->ctf_vars, fp->ctf_nvars, sizeof (ctf_varent_t),
  320. ctf_lookup_var);
  321. if (ent == NULL)
  322. {
  323. if (fp->ctf_parent != NULL)
  324. return ctf_lookup_variable (fp->ctf_parent, name);
  325. return (ctf_set_errno (fp, ECTF_NOTYPEDAT));
  326. }
  327. return ent->ctv_type;
  328. }
  329. typedef struct ctf_symidx_sort_arg_cb
  330. {
  331. ctf_dict_t *fp;
  332. uint32_t *names;
  333. } ctf_symidx_sort_arg_cb_t;
  334. static int
  335. sort_symidx_by_name (const void *one_, const void *two_, void *arg_)
  336. {
  337. const uint32_t *one = one_;
  338. const uint32_t *two = two_;
  339. ctf_symidx_sort_arg_cb_t *arg = arg_;
  340. return (strcmp (ctf_strptr (arg->fp, arg->names[*one]),
  341. ctf_strptr (arg->fp, arg->names[*two])));
  342. }
  343. /* Sort a symbol index section by name. Takes a 1:1 mapping of names to the
  344. corresponding symbol table. Returns a lexicographically sorted array of idx
  345. indexes (and thus, of indexes into the corresponding func info / data object
  346. section). */
  347. static uint32_t *
  348. ctf_symidx_sort (ctf_dict_t *fp, uint32_t *idx, size_t *nidx,
  349. size_t len)
  350. {
  351. uint32_t *sorted;
  352. size_t i;
  353. if ((sorted = malloc (len)) == NULL)
  354. {
  355. ctf_set_errno (fp, ENOMEM);
  356. return NULL;
  357. }
  358. *nidx = len / sizeof (uint32_t);
  359. for (i = 0; i < *nidx; i++)
  360. sorted[i] = i;
  361. if (!(fp->ctf_header->cth_flags & CTF_F_IDXSORTED))
  362. {
  363. ctf_symidx_sort_arg_cb_t arg = { fp, idx };
  364. ctf_dprintf ("Index section unsorted: sorting.");
  365. ctf_qsort_r (sorted, *nidx, sizeof (uint32_t), sort_symidx_by_name, &arg);
  366. fp->ctf_header->cth_flags |= CTF_F_IDXSORTED;
  367. }
  368. return sorted;
  369. }
  370. /* Given a symbol index, return the name of that symbol from the table provided
  371. by ctf_link_shuffle_syms, or failing that from the secondary string table, or
  372. the null string. */
  373. static const char *
  374. ctf_lookup_symbol_name (ctf_dict_t *fp, unsigned long symidx)
  375. {
  376. const ctf_sect_t *sp = &fp->ctf_symtab;
  377. ctf_link_sym_t sym;
  378. int err;
  379. if (fp->ctf_dynsymidx)
  380. {
  381. err = EINVAL;
  382. if (symidx > fp->ctf_dynsymmax)
  383. goto try_parent;
  384. ctf_link_sym_t *symp = fp->ctf_dynsymidx[symidx];
  385. if (!symp)
  386. goto try_parent;
  387. return symp->st_name;
  388. }
  389. err = ECTF_NOSYMTAB;
  390. if (sp->cts_data == NULL)
  391. goto try_parent;
  392. if (symidx >= fp->ctf_nsyms)
  393. goto try_parent;
  394. switch (sp->cts_entsize)
  395. {
  396. case sizeof (Elf64_Sym):
  397. {
  398. const Elf64_Sym *symp = (Elf64_Sym *) sp->cts_data + symidx;
  399. ctf_elf64_to_link_sym (fp, &sym, symp, symidx);
  400. }
  401. break;
  402. case sizeof (Elf32_Sym):
  403. {
  404. const Elf32_Sym *symp = (Elf32_Sym *) sp->cts_data + symidx;
  405. ctf_elf32_to_link_sym (fp, &sym, symp, symidx);
  406. }
  407. break;
  408. default:
  409. ctf_set_errno (fp, ECTF_SYMTAB);
  410. return _CTF_NULLSTR;
  411. }
  412. assert (!sym.st_nameidx_set);
  413. return sym.st_name;
  414. try_parent:
  415. if (fp->ctf_parent)
  416. {
  417. const char *ret;
  418. ret = ctf_lookup_symbol_name (fp->ctf_parent, symidx);
  419. if (ret == NULL)
  420. ctf_set_errno (fp, ctf_errno (fp->ctf_parent));
  421. return ret;
  422. }
  423. else
  424. {
  425. ctf_set_errno (fp, err);
  426. return _CTF_NULLSTR;
  427. }
  428. }
  429. /* Given a symbol name, return the index of that symbol, or -1 on error or if
  430. not found. */
  431. static unsigned long
  432. ctf_lookup_symbol_idx (ctf_dict_t *fp, const char *symname)
  433. {
  434. const ctf_sect_t *sp = &fp->ctf_symtab;
  435. ctf_link_sym_t sym;
  436. void *known_idx;
  437. int err;
  438. ctf_dict_t *cache = fp;
  439. if (fp->ctf_dynsyms)
  440. {
  441. err = EINVAL;
  442. ctf_link_sym_t *symp;
  443. if ((symp = ctf_dynhash_lookup (fp->ctf_dynsyms, symname)) == NULL)
  444. goto try_parent;
  445. return symp->st_symidx;
  446. }
  447. err = ECTF_NOSYMTAB;
  448. if (sp->cts_data == NULL)
  449. goto try_parent;
  450. /* First, try a hash lookup to see if we have already spotted this symbol
  451. during a past iteration: create the hash first if need be. The lifespan
  452. of the strings is equal to the lifespan of the cts_data, so we don't
  453. need to strdup them. If this dict was opened as part of an archive,
  454. and this archive has designed a crossdict_cache to cache results that
  455. are the same across all dicts in an archive, use it. */
  456. if (fp->ctf_archive && fp->ctf_archive->ctfi_crossdict_cache)
  457. cache = fp->ctf_archive->ctfi_crossdict_cache;
  458. if (!cache->ctf_symhash)
  459. if ((cache->ctf_symhash = ctf_dynhash_create (ctf_hash_string,
  460. ctf_hash_eq_string,
  461. NULL, NULL)) == NULL)
  462. goto oom;
  463. if (ctf_dynhash_lookup_kv (cache->ctf_symhash, symname, NULL, &known_idx))
  464. return (unsigned long) (uintptr_t) known_idx;
  465. /* Hash lookup unsuccessful: linear search, populating the hashtab for later
  466. lookups as we go. */
  467. for (; cache->ctf_symhash_latest < sp->cts_size / sp->cts_entsize;
  468. cache->ctf_symhash_latest++)
  469. {
  470. switch (sp->cts_entsize)
  471. {
  472. case sizeof (Elf64_Sym):
  473. {
  474. Elf64_Sym *symp = (Elf64_Sym *) sp->cts_data;
  475. ctf_elf64_to_link_sym (fp, &sym, &symp[cache->ctf_symhash_latest],
  476. cache->ctf_symhash_latest);
  477. if (!ctf_dynhash_lookup_kv (cache->ctf_symhash, sym.st_name,
  478. NULL, NULL))
  479. if (ctf_dynhash_cinsert (cache->ctf_symhash, sym.st_name,
  480. (const void *) (uintptr_t)
  481. cache->ctf_symhash_latest) < 0)
  482. goto oom;
  483. if (strcmp (sym.st_name, symname) == 0)
  484. return cache->ctf_symhash_latest++;
  485. }
  486. break;
  487. case sizeof (Elf32_Sym):
  488. {
  489. Elf32_Sym *symp = (Elf32_Sym *) sp->cts_data;
  490. ctf_elf32_to_link_sym (fp, &sym, &symp[cache->ctf_symhash_latest],
  491. cache->ctf_symhash_latest);
  492. if (!ctf_dynhash_lookup_kv (cache->ctf_symhash, sym.st_name,
  493. NULL, NULL))
  494. if (ctf_dynhash_cinsert (cache->ctf_symhash, sym.st_name,
  495. (const void *) (uintptr_t)
  496. cache->ctf_symhash_latest) < 0)
  497. goto oom;
  498. if (strcmp (sym.st_name, symname) == 0)
  499. return cache->ctf_symhash_latest++;
  500. }
  501. break;
  502. default:
  503. ctf_set_errno (fp, ECTF_SYMTAB);
  504. return (unsigned long) -1;
  505. }
  506. }
  507. /* Searched everything, still not found. */
  508. return (unsigned long) -1;
  509. try_parent:
  510. if (fp->ctf_parent)
  511. return ctf_lookup_symbol_idx (fp->ctf_parent, symname);
  512. else
  513. {
  514. ctf_set_errno (fp, err);
  515. return (unsigned long) -1;
  516. }
  517. oom:
  518. ctf_set_errno (fp, ENOMEM);
  519. ctf_err_warn (fp, 0, ENOMEM, _("cannot allocate memory for symbol "
  520. "lookup hashtab"));
  521. return (unsigned long) -1;
  522. }
  523. /* Iterate over all symbols with types: if FUNC, function symbols, otherwise,
  524. data symbols. The name argument is not optional. The return order is
  525. arbitrary, though is likely to be in symbol index or name order. You can
  526. change the value of 'functions' in the middle of iteration over non-dynamic
  527. dicts, but doing so on dynamic dicts will fail. (This is probably not very
  528. useful, but there is no reason to prohibit it.) */
  529. ctf_id_t
  530. ctf_symbol_next (ctf_dict_t *fp, ctf_next_t **it, const char **name,
  531. int functions)
  532. {
  533. ctf_id_t sym;
  534. ctf_next_t *i = *it;
  535. int err;
  536. if (!i)
  537. {
  538. if ((i = ctf_next_create ()) == NULL)
  539. return ctf_set_errno (fp, ENOMEM);
  540. i->cu.ctn_fp = fp;
  541. i->ctn_iter_fun = (void (*) (void)) ctf_symbol_next;
  542. i->ctn_n = 0;
  543. *it = i;
  544. }
  545. if ((void (*) (void)) ctf_symbol_next != i->ctn_iter_fun)
  546. return (ctf_set_errno (fp, ECTF_NEXT_WRONGFUN));
  547. if (fp != i->cu.ctn_fp)
  548. return (ctf_set_errno (fp, ECTF_NEXT_WRONGFP));
  549. /* We intentionally use raw access, not ctf_lookup_by_symbol, to avoid
  550. incurring additional sorting cost for unsorted symtypetabs coming from the
  551. compiler, to allow ctf_symbol_next to work in the absence of a symtab, and
  552. finally because it's easier to work out what the name of each symbol is if
  553. we do that. */
  554. if (fp->ctf_flags & LCTF_RDWR)
  555. {
  556. ctf_dynhash_t *dynh = functions ? fp->ctf_funchash : fp->ctf_objthash;
  557. void *dyn_name = NULL, *dyn_value = NULL;
  558. if (!dynh)
  559. {
  560. ctf_next_destroy (i);
  561. return (ctf_set_errno (fp, ECTF_NEXT_END));
  562. }
  563. err = ctf_dynhash_next (dynh, &i->ctn_next, &dyn_name, &dyn_value);
  564. /* This covers errors and also end-of-iteration. */
  565. if (err != 0)
  566. {
  567. ctf_next_destroy (i);
  568. *it = NULL;
  569. return ctf_set_errno (fp, err);
  570. }
  571. *name = dyn_name;
  572. sym = (ctf_id_t) (uintptr_t) dyn_value;
  573. }
  574. else if ((!functions && fp->ctf_objtidx_names) ||
  575. (functions && fp->ctf_funcidx_names))
  576. {
  577. ctf_header_t *hp = fp->ctf_header;
  578. uint32_t *idx = functions ? fp->ctf_funcidx_names : fp->ctf_objtidx_names;
  579. uint32_t *tab;
  580. size_t len;
  581. if (functions)
  582. {
  583. len = (hp->cth_varoff - hp->cth_funcidxoff) / sizeof (uint32_t);
  584. tab = (uint32_t *) (fp->ctf_buf + hp->cth_funcoff);
  585. }
  586. else
  587. {
  588. len = (hp->cth_funcidxoff - hp->cth_objtidxoff) / sizeof (uint32_t);
  589. tab = (uint32_t *) (fp->ctf_buf + hp->cth_objtoff);
  590. }
  591. do
  592. {
  593. if (i->ctn_n >= len)
  594. goto end;
  595. *name = ctf_strptr (fp, idx[i->ctn_n]);
  596. sym = tab[i->ctn_n++];
  597. }
  598. while (sym == -1u || sym == 0);
  599. }
  600. else
  601. {
  602. /* Skip over pads in ctf_xslate, padding for typeless symbols in the
  603. symtypetab itself, and symbols in the wrong table. */
  604. for (; i->ctn_n < fp->ctf_nsyms; i->ctn_n++)
  605. {
  606. ctf_header_t *hp = fp->ctf_header;
  607. if (fp->ctf_sxlate[i->ctn_n] == -1u)
  608. continue;
  609. sym = *(uint32_t *) ((uintptr_t) fp->ctf_buf + fp->ctf_sxlate[i->ctn_n]);
  610. if (sym == 0)
  611. continue;
  612. if (functions)
  613. {
  614. if (fp->ctf_sxlate[i->ctn_n] >= hp->cth_funcoff
  615. && fp->ctf_sxlate[i->ctn_n] < hp->cth_objtidxoff)
  616. break;
  617. }
  618. else
  619. {
  620. if (fp->ctf_sxlate[i->ctn_n] >= hp->cth_objtoff
  621. && fp->ctf_sxlate[i->ctn_n] < hp->cth_funcoff)
  622. break;
  623. }
  624. }
  625. if (i->ctn_n >= fp->ctf_nsyms)
  626. goto end;
  627. *name = ctf_lookup_symbol_name (fp, i->ctn_n++);
  628. }
  629. return sym;
  630. end:
  631. ctf_next_destroy (i);
  632. *it = NULL;
  633. return (ctf_set_errno (fp, ECTF_NEXT_END));
  634. }
  635. /* A bsearch function for function and object index names. */
  636. static int
  637. ctf_lookup_idx_name (const void *key_, const void *idx_)
  638. {
  639. const ctf_lookup_idx_key_t *key = key_;
  640. const uint32_t *idx = idx_;
  641. return (strcmp (key->clik_name, ctf_strptr (key->clik_fp, key->clik_names[*idx])));
  642. }
  643. /* Given a symbol name or (failing that) number, look up that symbol in the
  644. function or object index table (which must exist). Return 0 if not found
  645. there (or pad). */
  646. static ctf_id_t
  647. ctf_try_lookup_indexed (ctf_dict_t *fp, unsigned long symidx,
  648. const char *symname, int is_function)
  649. {
  650. struct ctf_header *hp = fp->ctf_header;
  651. uint32_t *symtypetab;
  652. uint32_t *names;
  653. uint32_t *sxlate;
  654. size_t nidx;
  655. if (symname == NULL)
  656. symname = ctf_lookup_symbol_name (fp, symidx);
  657. ctf_dprintf ("Looking up type of object with symtab idx %lx or name %s in "
  658. "indexed symtypetab\n", symidx, symname);
  659. if (symname[0] == '\0')
  660. return -1; /* errno is set for us. */
  661. if (is_function)
  662. {
  663. if (!fp->ctf_funcidx_sxlate)
  664. {
  665. if ((fp->ctf_funcidx_sxlate
  666. = ctf_symidx_sort (fp, (uint32_t *)
  667. (fp->ctf_buf + hp->cth_funcidxoff),
  668. &fp->ctf_nfuncidx,
  669. hp->cth_varoff - hp->cth_funcidxoff))
  670. == NULL)
  671. {
  672. ctf_err_warn (fp, 0, 0, _("cannot sort function symidx"));
  673. return -1; /* errno is set for us. */
  674. }
  675. }
  676. symtypetab = (uint32_t *) (fp->ctf_buf + hp->cth_funcoff);
  677. sxlate = fp->ctf_funcidx_sxlate;
  678. names = fp->ctf_funcidx_names;
  679. nidx = fp->ctf_nfuncidx;
  680. }
  681. else
  682. {
  683. if (!fp->ctf_objtidx_sxlate)
  684. {
  685. if ((fp->ctf_objtidx_sxlate
  686. = ctf_symidx_sort (fp, (uint32_t *)
  687. (fp->ctf_buf + hp->cth_objtidxoff),
  688. &fp->ctf_nobjtidx,
  689. hp->cth_funcidxoff - hp->cth_objtidxoff))
  690. == NULL)
  691. {
  692. ctf_err_warn (fp, 0, 0, _("cannot sort object symidx"));
  693. return -1; /* errno is set for us. */
  694. }
  695. }
  696. symtypetab = (uint32_t *) (fp->ctf_buf + hp->cth_objtoff);
  697. sxlate = fp->ctf_objtidx_sxlate;
  698. names = fp->ctf_objtidx_names;
  699. nidx = fp->ctf_nobjtidx;
  700. }
  701. ctf_lookup_idx_key_t key = { fp, symname, names };
  702. uint32_t *idx;
  703. idx = bsearch (&key, sxlate, nidx, sizeof (uint32_t), ctf_lookup_idx_name);
  704. if (!idx)
  705. {
  706. ctf_dprintf ("%s not found in idx\n", symname);
  707. return 0;
  708. }
  709. /* Should be impossible, but be paranoid. */
  710. if ((idx - sxlate) > (ptrdiff_t) nidx)
  711. return (ctf_set_errno (fp, ECTF_CORRUPT));
  712. ctf_dprintf ("Symbol %lx (%s) is of type %x\n", symidx, symname,
  713. symtypetab[*idx]);
  714. return symtypetab[*idx];
  715. }
  716. /* Given a symbol name or (if NULL) symbol index, return the type of the
  717. function or data object described by the corresponding entry in the symbol
  718. table. We can only return symbols in read-only dicts and in dicts for which
  719. ctf_link_shuffle_syms has been called to assign symbol indexes to symbol
  720. names. */
  721. static ctf_id_t
  722. ctf_lookup_by_sym_or_name (ctf_dict_t *fp, unsigned long symidx,
  723. const char *symname)
  724. {
  725. const ctf_sect_t *sp = &fp->ctf_symtab;
  726. ctf_id_t type = 0;
  727. int err = 0;
  728. /* Shuffled dynsymidx present? Use that. */
  729. if (fp->ctf_dynsymidx)
  730. {
  731. const ctf_link_sym_t *sym;
  732. if (symname)
  733. ctf_dprintf ("Looking up type of object with symname %s in "
  734. "writable dict symtypetab\n", symname);
  735. else
  736. ctf_dprintf ("Looking up type of object with symtab idx %lx in "
  737. "writable dict symtypetab\n", symidx);
  738. /* The dict must be dynamic. */
  739. if (!ctf_assert (fp, fp->ctf_flags & LCTF_RDWR))
  740. return CTF_ERR;
  741. /* No name? Need to look it up. */
  742. if (!symname)
  743. {
  744. err = EINVAL;
  745. if (symidx > fp->ctf_dynsymmax)
  746. goto try_parent;
  747. sym = fp->ctf_dynsymidx[symidx];
  748. err = ECTF_NOTYPEDAT;
  749. if (!sym || (sym->st_shndx != STT_OBJECT && sym->st_shndx != STT_FUNC))
  750. goto try_parent;
  751. if (!ctf_assert (fp, !sym->st_nameidx_set))
  752. return CTF_ERR;
  753. symname = sym->st_name;
  754. }
  755. if (fp->ctf_objthash == NULL
  756. || ((type = (ctf_id_t) (uintptr_t)
  757. ctf_dynhash_lookup (fp->ctf_objthash, symname)) == 0))
  758. {
  759. if (fp->ctf_funchash == NULL
  760. || ((type = (ctf_id_t) (uintptr_t)
  761. ctf_dynhash_lookup (fp->ctf_funchash, symname)) == 0))
  762. goto try_parent;
  763. }
  764. return type;
  765. }
  766. /* Lookup by name in a dynamic dict: just do it directly. */
  767. if (symname && fp->ctf_flags & LCTF_RDWR)
  768. {
  769. if (fp->ctf_objthash == NULL
  770. || ((type = (ctf_id_t) (uintptr_t)
  771. ctf_dynhash_lookup (fp->ctf_objthash, symname)) == 0))
  772. {
  773. if (fp->ctf_funchash == NULL
  774. || ((type = (ctf_id_t) (uintptr_t)
  775. ctf_dynhash_lookup (fp->ctf_funchash, symname)) == 0))
  776. goto try_parent;
  777. }
  778. return type;
  779. }
  780. err = ECTF_NOSYMTAB;
  781. if (sp->cts_data == NULL)
  782. goto try_parent;
  783. /* This covers both out-of-range lookups and a dynamic dict which hasn't been
  784. shuffled yet. */
  785. err = EINVAL;
  786. if (symname == NULL && symidx >= fp->ctf_nsyms)
  787. goto try_parent;
  788. if (fp->ctf_objtidx_names)
  789. {
  790. if ((type = ctf_try_lookup_indexed (fp, symidx, symname, 0)) == CTF_ERR)
  791. return CTF_ERR; /* errno is set for us. */
  792. }
  793. if (type == 0 && fp->ctf_funcidx_names)
  794. {
  795. if ((type = ctf_try_lookup_indexed (fp, symidx, symname, 1)) == CTF_ERR)
  796. return CTF_ERR; /* errno is set for us. */
  797. }
  798. if (type != 0)
  799. return type;
  800. err = ECTF_NOTYPEDAT;
  801. if (fp->ctf_objtidx_names && fp->ctf_funcidx_names)
  802. goto try_parent;
  803. /* Table must be nonindexed. */
  804. ctf_dprintf ("Looking up object type %lx in 1:1 dict symtypetab\n", symidx);
  805. if (symname != NULL)
  806. if ((symidx = ctf_lookup_symbol_idx (fp, symname)) == (unsigned long) -1)
  807. goto try_parent;
  808. if (fp->ctf_sxlate[symidx] == -1u)
  809. goto try_parent;
  810. type = *(uint32_t *) ((uintptr_t) fp->ctf_buf + fp->ctf_sxlate[symidx]);
  811. if (type == 0)
  812. goto try_parent;
  813. return type;
  814. try_parent:
  815. if (fp->ctf_parent)
  816. {
  817. ctf_id_t ret = ctf_lookup_by_sym_or_name (fp->ctf_parent, symidx,
  818. symname);
  819. if (ret == CTF_ERR)
  820. ctf_set_errno (fp, ctf_errno (fp->ctf_parent));
  821. return ret;
  822. }
  823. else
  824. return (ctf_set_errno (fp, err));
  825. }
  826. /* Given a symbol table index, return the type of the function or data object
  827. described by the corresponding entry in the symbol table. */
  828. ctf_id_t
  829. ctf_lookup_by_symbol (ctf_dict_t *fp, unsigned long symidx)
  830. {
  831. return ctf_lookup_by_sym_or_name (fp, symidx, NULL);
  832. }
  833. /* Given a symbol name, return the type of the function or data object described
  834. by the corresponding entry in the symbol table. */
  835. ctf_id_t
  836. ctf_lookup_by_symbol_name (ctf_dict_t *fp, const char *symname)
  837. {
  838. return ctf_lookup_by_sym_or_name (fp, 0, symname);
  839. }
  840. /* Given a symbol table index, return the info for the function described
  841. by the corresponding entry in the symbol table, which may be a function
  842. symbol or may be a data symbol that happens to be a function pointer. */
  843. int
  844. ctf_func_info (ctf_dict_t *fp, unsigned long symidx, ctf_funcinfo_t *fip)
  845. {
  846. ctf_id_t type;
  847. if ((type = ctf_lookup_by_symbol (fp, symidx)) == CTF_ERR)
  848. return -1; /* errno is set for us. */
  849. if (ctf_type_kind (fp, type) != CTF_K_FUNCTION)
  850. return (ctf_set_errno (fp, ECTF_NOTFUNC));
  851. return ctf_func_type_info (fp, type, fip);
  852. }
  853. /* Given a symbol table index, return the arguments for the function described
  854. by the corresponding entry in the symbol table. */
  855. int
  856. ctf_func_args (ctf_dict_t *fp, unsigned long symidx, uint32_t argc,
  857. ctf_id_t *argv)
  858. {
  859. ctf_id_t type;
  860. if ((type = ctf_lookup_by_symbol (fp, symidx)) == CTF_ERR)
  861. return -1; /* errno is set for us. */
  862. if (ctf_type_kind (fp, type) != CTF_K_FUNCTION)
  863. return (ctf_set_errno (fp, ECTF_NOTFUNC));
  864. return ctf_func_type_args (fp, type, argc, argv);
  865. }