ctf-string.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /* CTF string table management.
  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 <string.h>
  17. #include <assert.h>
  18. /* Convert an encoded CTF string name into a pointer to a C string, using an
  19. explicit internal strtab rather than the fp-based one. */
  20. const char *
  21. ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab)
  22. {
  23. ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID (name)];
  24. if ((CTF_NAME_STID (name) == CTF_STRTAB_0) && (strtab != NULL))
  25. ctsp = strtab;
  26. /* If this name is in the external strtab, and there is a synthetic strtab,
  27. use it in preference. */
  28. if (CTF_NAME_STID (name) == CTF_STRTAB_1
  29. && fp->ctf_syn_ext_strtab != NULL)
  30. return ctf_dynhash_lookup (fp->ctf_syn_ext_strtab,
  31. (void *) (uintptr_t) name);
  32. /* If the name is in the internal strtab, and the offset is beyond the end of
  33. the ctsp->cts_len but below the ctf_str_prov_offset, this is a provisional
  34. string added by ctf_str_add*() but not yet built into a real strtab: get
  35. the value out of the ctf_prov_strtab. */
  36. if (CTF_NAME_STID (name) == CTF_STRTAB_0
  37. && name >= ctsp->cts_len && name < fp->ctf_str_prov_offset)
  38. return ctf_dynhash_lookup (fp->ctf_prov_strtab,
  39. (void *) (uintptr_t) name);
  40. if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET (name) < ctsp->cts_len)
  41. return (ctsp->cts_strs + CTF_NAME_OFFSET (name));
  42. /* String table not loaded or corrupt offset. */
  43. return NULL;
  44. }
  45. /* Convert an encoded CTF string name into a pointer to a C string by looking
  46. up the appropriate string table buffer and then adding the offset. */
  47. const char *
  48. ctf_strraw (ctf_dict_t *fp, uint32_t name)
  49. {
  50. return ctf_strraw_explicit (fp, name, NULL);
  51. }
  52. /* Return a guaranteed-non-NULL pointer to the string with the given CTF
  53. name. */
  54. const char *
  55. ctf_strptr (ctf_dict_t *fp, uint32_t name)
  56. {
  57. const char *s = ctf_strraw (fp, name);
  58. return (s != NULL ? s : "(?)");
  59. }
  60. /* Remove all refs to a given atom. */
  61. static void
  62. ctf_str_purge_atom_refs (ctf_str_atom_t *atom)
  63. {
  64. ctf_str_atom_ref_t *ref, *next;
  65. for (ref = ctf_list_next (&atom->csa_refs); ref != NULL; ref = next)
  66. {
  67. next = ctf_list_next (ref);
  68. ctf_list_delete (&atom->csa_refs, ref);
  69. free (ref);
  70. }
  71. }
  72. /* Free an atom (only called on ctf_close().) */
  73. static void
  74. ctf_str_free_atom (void *a)
  75. {
  76. ctf_str_atom_t *atom = a;
  77. ctf_str_purge_atom_refs (atom);
  78. free (atom);
  79. }
  80. /* Create the atoms table. There is always at least one atom in it, the null
  81. string. */
  82. int
  83. ctf_str_create_atoms (ctf_dict_t *fp)
  84. {
  85. fp->ctf_str_atoms = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
  86. free, ctf_str_free_atom);
  87. if (!fp->ctf_str_atoms)
  88. return -ENOMEM;
  89. if (!fp->ctf_prov_strtab)
  90. fp->ctf_prov_strtab = ctf_dynhash_create (ctf_hash_integer,
  91. ctf_hash_eq_integer,
  92. NULL, NULL);
  93. if (!fp->ctf_prov_strtab)
  94. goto oom_prov_strtab;
  95. if (!fp->ctf_str_pending_ref)
  96. fp->ctf_str_pending_ref = ctf_dynset_create (htab_hash_pointer,
  97. htab_eq_pointer,
  98. NULL);
  99. if (!fp->ctf_str_pending_ref)
  100. goto oom_str_pending_ref;
  101. errno = 0;
  102. ctf_str_add (fp, "");
  103. if (errno == ENOMEM)
  104. goto oom_str_add;
  105. return 0;
  106. oom_str_add:
  107. ctf_dynhash_destroy (fp->ctf_prov_strtab);
  108. fp->ctf_prov_strtab = NULL;
  109. oom_str_pending_ref:
  110. ctf_dynset_destroy (fp->ctf_str_pending_ref);
  111. fp->ctf_str_pending_ref = NULL;
  112. oom_prov_strtab:
  113. ctf_dynhash_destroy (fp->ctf_str_atoms);
  114. fp->ctf_str_atoms = NULL;
  115. return -ENOMEM;
  116. }
  117. /* Destroy the atoms table. */
  118. void
  119. ctf_str_free_atoms (ctf_dict_t *fp)
  120. {
  121. ctf_dynhash_destroy (fp->ctf_prov_strtab);
  122. ctf_dynhash_destroy (fp->ctf_str_atoms);
  123. ctf_dynset_destroy (fp->ctf_str_pending_ref);
  124. }
  125. #define CTF_STR_ADD_REF 0x1
  126. #define CTF_STR_MAKE_PROVISIONAL 0x2
  127. #define CTF_STR_PENDING_REF 0x4
  128. /* Add a string to the atoms table, copying the passed-in string. Return the
  129. atom added. Return NULL only when out of memory (and do not touch the
  130. passed-in string in that case). Possibly augment the ref list with the
  131. passed-in ref. Possibly add a provisional entry for this string to the
  132. provisional strtab. */
  133. static ctf_str_atom_t *
  134. ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
  135. int flags, uint32_t *ref)
  136. {
  137. char *newstr = NULL;
  138. ctf_str_atom_t *atom = NULL;
  139. ctf_str_atom_ref_t *aref = NULL;
  140. atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
  141. if (flags & CTF_STR_ADD_REF)
  142. {
  143. if ((aref = malloc (sizeof (struct ctf_str_atom_ref))) == NULL)
  144. return NULL;
  145. aref->caf_ref = ref;
  146. }
  147. if (atom)
  148. {
  149. if (flags & CTF_STR_ADD_REF)
  150. {
  151. ctf_dynset_remove (fp->ctf_str_pending_ref, (void *) ref);
  152. ctf_list_append (&atom->csa_refs, aref);
  153. fp->ctf_str_num_refs++;
  154. }
  155. return atom;
  156. }
  157. if ((atom = malloc (sizeof (struct ctf_str_atom))) == NULL)
  158. goto oom;
  159. memset (atom, 0, sizeof (struct ctf_str_atom));
  160. if ((newstr = strdup (str)) == NULL)
  161. goto oom;
  162. if (ctf_dynhash_insert (fp->ctf_str_atoms, newstr, atom) < 0)
  163. goto oom;
  164. atom->csa_str = newstr;
  165. atom->csa_snapshot_id = fp->ctf_snapshots;
  166. if (flags & CTF_STR_MAKE_PROVISIONAL)
  167. {
  168. atom->csa_offset = fp->ctf_str_prov_offset;
  169. if (ctf_dynhash_insert (fp->ctf_prov_strtab, (void *) (uintptr_t)
  170. atom->csa_offset, (void *) atom->csa_str) < 0)
  171. goto oom;
  172. fp->ctf_str_prov_offset += strlen (atom->csa_str) + 1;
  173. }
  174. if (flags & CTF_STR_PENDING_REF)
  175. {
  176. if (ctf_dynset_insert (fp->ctf_str_pending_ref, (void *) ref) < 0)
  177. goto oom;
  178. }
  179. else if (flags & CTF_STR_ADD_REF)
  180. {
  181. ctf_dynset_remove (fp->ctf_str_pending_ref, (void *) ref);
  182. ctf_list_append (&atom->csa_refs, aref);
  183. fp->ctf_str_num_refs++;
  184. }
  185. return atom;
  186. oom:
  187. if (newstr)
  188. ctf_dynhash_remove (fp->ctf_str_atoms, newstr);
  189. free (atom);
  190. free (aref);
  191. free (newstr);
  192. ctf_set_errno (fp, ENOMEM);
  193. return NULL;
  194. }
  195. /* Add a string to the atoms table, without augmenting the ref list for this
  196. string: return a 'provisional offset' which can be used to return this string
  197. until ctf_str_write_strtab is called, or 0 on failure. (Everywhere the
  198. provisional offset is assigned to should be added as a ref using
  199. ctf_str_add_ref() as well.) */
  200. uint32_t
  201. ctf_str_add (ctf_dict_t *fp, const char *str)
  202. {
  203. ctf_str_atom_t *atom;
  204. if (!str)
  205. str = "";
  206. atom = ctf_str_add_ref_internal (fp, str, CTF_STR_MAKE_PROVISIONAL, 0);
  207. if (!atom)
  208. return 0;
  209. return atom->csa_offset;
  210. }
  211. /* Like ctf_str_add(), but additionally augment the atom's refs list with the
  212. passed-in ref, whether or not the string is already present. There is no
  213. attempt to deduplicate the refs list (but duplicates are harmless). */
  214. uint32_t
  215. ctf_str_add_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
  216. {
  217. ctf_str_atom_t *atom;
  218. if (!str)
  219. str = "";
  220. atom = ctf_str_add_ref_internal (fp, str, CTF_STR_ADD_REF
  221. | CTF_STR_MAKE_PROVISIONAL, ref);
  222. if (!atom)
  223. return 0;
  224. return atom->csa_offset;
  225. }
  226. /* Like ctf_str_add_ref(), but notes that this memory location must be added as
  227. a ref by a later serialization phase, rather than adding it itself. */
  228. uint32_t
  229. ctf_str_add_pending (ctf_dict_t *fp, const char *str, uint32_t *ref)
  230. {
  231. ctf_str_atom_t *atom;
  232. if (!str)
  233. str = "";
  234. atom = ctf_str_add_ref_internal (fp, str, CTF_STR_PENDING_REF
  235. | CTF_STR_MAKE_PROVISIONAL, ref);
  236. if (!atom)
  237. return 0;
  238. return atom->csa_offset;
  239. }
  240. /* Note that a pending ref now located at NEW_REF has moved by BYTES bytes. */
  241. int
  242. ctf_str_move_pending (ctf_dict_t *fp, uint32_t *new_ref, ptrdiff_t bytes)
  243. {
  244. if (bytes == 0)
  245. return 0;
  246. if (ctf_dynset_insert (fp->ctf_str_pending_ref, (void *) new_ref) < 0)
  247. return (ctf_set_errno (fp, ENOMEM));
  248. ctf_dynset_remove (fp->ctf_str_pending_ref,
  249. (void *) ((signed char *) new_ref - bytes));
  250. return 0;
  251. }
  252. /* Add an external strtab reference at OFFSET. Returns zero if the addition
  253. failed, nonzero otherwise. */
  254. int
  255. ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
  256. {
  257. ctf_str_atom_t *atom;
  258. if (!str)
  259. str = "";
  260. atom = ctf_str_add_ref_internal (fp, str, 0, 0);
  261. if (!atom)
  262. return 0;
  263. atom->csa_external_offset = CTF_SET_STID (offset, CTF_STRTAB_1);
  264. if (!fp->ctf_syn_ext_strtab)
  265. fp->ctf_syn_ext_strtab = ctf_dynhash_create (ctf_hash_integer,
  266. ctf_hash_eq_integer,
  267. NULL, NULL);
  268. if (!fp->ctf_syn_ext_strtab)
  269. {
  270. ctf_set_errno (fp, ENOMEM);
  271. return 0;
  272. }
  273. if (ctf_dynhash_insert (fp->ctf_syn_ext_strtab,
  274. (void *) (uintptr_t)
  275. atom->csa_external_offset,
  276. (void *) atom->csa_str) < 0)
  277. {
  278. /* No need to bother freeing the syn_ext_strtab: it will get freed at
  279. ctf_str_write_strtab time if unreferenced. */
  280. ctf_set_errno (fp, ENOMEM);
  281. return 0;
  282. }
  283. return 1;
  284. }
  285. /* Remove a single ref. */
  286. void
  287. ctf_str_remove_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
  288. {
  289. ctf_str_atom_ref_t *aref, *anext;
  290. ctf_str_atom_t *atom = NULL;
  291. atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
  292. if (!atom)
  293. return;
  294. for (aref = ctf_list_next (&atom->csa_refs); aref != NULL; aref = anext)
  295. {
  296. anext = ctf_list_next (aref);
  297. if (aref->caf_ref == ref)
  298. {
  299. ctf_list_delete (&atom->csa_refs, aref);
  300. free (aref);
  301. }
  302. }
  303. ctf_dynset_remove (fp->ctf_str_pending_ref, (void *) ref);
  304. }
  305. /* A ctf_dynhash_iter_remove() callback that removes atoms later than a given
  306. snapshot ID. External atoms are never removed, because they came from the
  307. linker string table and are still present even if you roll back type
  308. additions. */
  309. static int
  310. ctf_str_rollback_atom (void *key _libctf_unused_, void *value, void *arg)
  311. {
  312. ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
  313. ctf_snapshot_id_t *id = (ctf_snapshot_id_t *) arg;
  314. return (atom->csa_snapshot_id > id->snapshot_id)
  315. && (atom->csa_external_offset == 0);
  316. }
  317. /* Roll back, deleting all (internal) atoms created after a particular ID. */
  318. void
  319. ctf_str_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id)
  320. {
  321. ctf_dynhash_iter_remove (fp->ctf_str_atoms, ctf_str_rollback_atom, &id);
  322. }
  323. /* An adaptor around ctf_purge_atom_refs. */
  324. static void
  325. ctf_str_purge_one_atom_refs (void *key _libctf_unused_, void *value,
  326. void *arg _libctf_unused_)
  327. {
  328. ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
  329. ctf_str_purge_atom_refs (atom);
  330. }
  331. /* Remove all the recorded refs from the atoms table. */
  332. void
  333. ctf_str_purge_refs (ctf_dict_t *fp)
  334. {
  335. if (fp->ctf_str_num_refs > 0)
  336. ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_purge_one_atom_refs, NULL);
  337. fp->ctf_str_num_refs = 0;
  338. }
  339. /* Update a list of refs to the specified value. */
  340. static void
  341. ctf_str_update_refs (ctf_str_atom_t *refs, uint32_t value)
  342. {
  343. ctf_str_atom_ref_t *ref;
  344. for (ref = ctf_list_next (&refs->csa_refs); ref != NULL;
  345. ref = ctf_list_next (ref))
  346. *(ref->caf_ref) = value;
  347. }
  348. /* State shared across the strtab write process. */
  349. typedef struct ctf_strtab_write_state
  350. {
  351. /* Strtab we are writing, and the number of strings in it. */
  352. ctf_strs_writable_t *strtab;
  353. size_t strtab_count;
  354. /* Pointers to (existing) atoms in the atoms table, for qsorting. */
  355. ctf_str_atom_t **sorttab;
  356. /* Loop counter for sorttab population. */
  357. size_t i;
  358. /* The null-string atom (skipped during population). */
  359. ctf_str_atom_t *nullstr;
  360. } ctf_strtab_write_state_t;
  361. /* Count the number of entries in the strtab, and its length. */
  362. static void
  363. ctf_str_count_strtab (void *key _libctf_unused_, void *value,
  364. void *arg)
  365. {
  366. ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
  367. ctf_strtab_write_state_t *s = (ctf_strtab_write_state_t *) arg;
  368. /* We only factor in the length of items that have no offset and have refs:
  369. other items are in the external strtab, or will simply not be written out
  370. at all. They still contribute to the total count, though, because we still
  371. have to sort them. We add in the null string's length explicitly, outside
  372. this function, since it is explicitly written out even if it has no refs at
  373. all. */
  374. if (s->nullstr == atom)
  375. {
  376. s->strtab_count++;
  377. return;
  378. }
  379. if (!ctf_list_empty_p (&atom->csa_refs))
  380. {
  381. if (!atom->csa_external_offset)
  382. s->strtab->cts_len += strlen (atom->csa_str) + 1;
  383. s->strtab_count++;
  384. }
  385. }
  386. /* Populate the sorttab with pointers to the strtab atoms. */
  387. static void
  388. ctf_str_populate_sorttab (void *key _libctf_unused_, void *value,
  389. void *arg)
  390. {
  391. ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
  392. ctf_strtab_write_state_t *s = (ctf_strtab_write_state_t *) arg;
  393. /* Skip the null string. */
  394. if (s->nullstr == atom)
  395. return;
  396. /* Skip atoms with no refs. */
  397. if (!ctf_list_empty_p (&atom->csa_refs))
  398. s->sorttab[s->i++] = atom;
  399. }
  400. /* Sort the strtab. */
  401. static int
  402. ctf_str_sort_strtab (const void *a, const void *b)
  403. {
  404. ctf_str_atom_t **one = (ctf_str_atom_t **) a;
  405. ctf_str_atom_t **two = (ctf_str_atom_t **) b;
  406. return (strcmp ((*one)->csa_str, (*two)->csa_str));
  407. }
  408. /* Write out and return a strtab containing all strings with recorded refs,
  409. adjusting the refs to refer to the corresponding string. The returned strtab
  410. may be NULL on error. Also populate the synthetic strtab with mappings from
  411. external strtab offsets to names, so we can look them up with ctf_strptr().
  412. Only external strtab offsets with references are added. */
  413. ctf_strs_writable_t
  414. ctf_str_write_strtab (ctf_dict_t *fp)
  415. {
  416. ctf_strs_writable_t strtab;
  417. ctf_str_atom_t *nullstr;
  418. uint32_t cur_stroff = 0;
  419. ctf_strtab_write_state_t s;
  420. ctf_str_atom_t **sorttab;
  421. size_t i;
  422. int any_external = 0;
  423. memset (&strtab, 0, sizeof (struct ctf_strs_writable));
  424. memset (&s, 0, sizeof (struct ctf_strtab_write_state));
  425. s.strtab = &strtab;
  426. nullstr = ctf_dynhash_lookup (fp->ctf_str_atoms, "");
  427. if (!nullstr)
  428. {
  429. ctf_err_warn (fp, 0, ECTF_INTERNAL, _("null string not found in strtab"));
  430. strtab.cts_strs = NULL;
  431. return strtab;
  432. }
  433. s.nullstr = nullstr;
  434. ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_count_strtab, &s);
  435. strtab.cts_len++; /* For the null string. */
  436. ctf_dprintf ("%lu bytes of strings in strtab.\n",
  437. (unsigned long) strtab.cts_len);
  438. /* Sort the strtab. Force the null string to be first. */
  439. sorttab = calloc (s.strtab_count, sizeof (ctf_str_atom_t *));
  440. if (!sorttab)
  441. goto oom;
  442. sorttab[0] = nullstr;
  443. s.i = 1;
  444. s.sorttab = sorttab;
  445. ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_populate_sorttab, &s);
  446. qsort (&sorttab[1], s.strtab_count - 1, sizeof (ctf_str_atom_t *),
  447. ctf_str_sort_strtab);
  448. if ((strtab.cts_strs = malloc (strtab.cts_len)) == NULL)
  449. goto oom_sorttab;
  450. /* Update all refs: also update the strtab appropriately. */
  451. for (i = 0; i < s.strtab_count; i++)
  452. {
  453. if (sorttab[i]->csa_external_offset)
  454. {
  455. /* External strtab entry. */
  456. any_external = 1;
  457. ctf_str_update_refs (sorttab[i], sorttab[i]->csa_external_offset);
  458. sorttab[i]->csa_offset = sorttab[i]->csa_external_offset;
  459. }
  460. else
  461. {
  462. /* Internal strtab entry with refs: actually add to the string
  463. table. */
  464. ctf_str_update_refs (sorttab[i], cur_stroff);
  465. sorttab[i]->csa_offset = cur_stroff;
  466. strcpy (&strtab.cts_strs[cur_stroff], sorttab[i]->csa_str);
  467. cur_stroff += strlen (sorttab[i]->csa_str) + 1;
  468. }
  469. }
  470. free (sorttab);
  471. if (!any_external)
  472. {
  473. ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
  474. fp->ctf_syn_ext_strtab = NULL;
  475. }
  476. /* All the provisional strtab entries are now real strtab entries, and
  477. ctf_strptr() will find them there. The provisional offset now starts right
  478. beyond the new end of the strtab. */
  479. ctf_dynhash_empty (fp->ctf_prov_strtab);
  480. fp->ctf_str_prov_offset = strtab.cts_len + 1;
  481. return strtab;
  482. oom_sorttab:
  483. free (sorttab);
  484. oom:
  485. return strtab;
  486. }