1 /* CTF string table management.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
24 /* Convert an encoded CTF string name into a pointer to a C string, using an
25 explicit internal strtab rather than the fp-based one. */
27 ctf_strraw_explicit (ctf_dict_t
*fp
, uint32_t name
, ctf_strs_t
*strtab
)
29 ctf_strs_t
*ctsp
= &fp
->ctf_str
[CTF_NAME_STID (name
)];
31 if ((CTF_NAME_STID (name
) == CTF_STRTAB_0
) && (strtab
!= NULL
))
34 /* If this name is in the external strtab, and there is a synthetic strtab,
35 use it in preference. */
37 if (CTF_NAME_STID (name
) == CTF_STRTAB_1
38 && fp
->ctf_syn_ext_strtab
!= NULL
)
39 return ctf_dynhash_lookup (fp
->ctf_syn_ext_strtab
,
40 (void *) (uintptr_t) name
);
42 /* If the name is in the internal strtab, and the offset is beyond the end of
43 the ctsp->cts_len but below the ctf_str_prov_offset, this is a provisional
44 string added by ctf_str_add*() but not yet built into a real strtab: get
45 the value out of the ctf_prov_strtab. */
47 if (CTF_NAME_STID (name
) == CTF_STRTAB_0
48 && name
>= ctsp
->cts_len
&& name
< fp
->ctf_str_prov_offset
)
49 return ctf_dynhash_lookup (fp
->ctf_prov_strtab
,
50 (void *) (uintptr_t) name
);
52 if (ctsp
->cts_strs
!= NULL
&& CTF_NAME_OFFSET (name
) < ctsp
->cts_len
)
53 return (ctsp
->cts_strs
+ CTF_NAME_OFFSET (name
));
55 /* String table not loaded or corrupt offset. */
59 /* Convert an encoded CTF string name into a pointer to a C string by looking
60 up the appropriate string table buffer and then adding the offset. */
62 ctf_strraw (ctf_dict_t
*fp
, uint32_t name
)
64 return ctf_strraw_explicit (fp
, name
, NULL
);
67 /* Return a guaranteed-non-NULL pointer to the string with the given CTF
70 ctf_strptr (ctf_dict_t
*fp
, uint32_t name
)
72 const char *s
= ctf_strraw (fp
, name
);
73 return (s
!= NULL
? s
: "(?)");
76 /* Remove all refs to a given atom. */
78 ctf_str_purge_atom_refs (ctf_str_atom_t
*atom
)
80 ctf_str_atom_ref_t
*ref
, *next
;
82 for (ref
= ctf_list_next (&atom
->csa_refs
); ref
!= NULL
; ref
= next
)
84 next
= ctf_list_next (ref
);
85 ctf_list_delete (&atom
->csa_refs
, ref
);
90 /* Free an atom (only called on ctf_close().) */
92 ctf_str_free_atom (void *a
)
94 ctf_str_atom_t
*atom
= a
;
96 ctf_str_purge_atom_refs (atom
);
100 /* Create the atoms table. There is always at least one atom in it, the null
103 ctf_str_create_atoms (ctf_dict_t
*fp
)
105 fp
->ctf_str_atoms
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
106 free
, ctf_str_free_atom
);
107 if (!fp
->ctf_str_atoms
)
110 if (!fp
->ctf_prov_strtab
)
111 fp
->ctf_prov_strtab
= ctf_dynhash_create (ctf_hash_integer
,
114 if (!fp
->ctf_prov_strtab
)
115 goto oom_prov_strtab
;
117 if (!fp
->ctf_str_pending_ref
)
118 fp
->ctf_str_pending_ref
= ctf_dynset_create (htab_hash_pointer
,
121 if (!fp
->ctf_str_pending_ref
)
122 goto oom_str_pending_ref
;
125 ctf_str_add (fp
, "");
132 ctf_dynhash_destroy (fp
->ctf_prov_strtab
);
133 fp
->ctf_prov_strtab
= NULL
;
135 ctf_dynset_destroy (fp
->ctf_str_pending_ref
);
136 fp
->ctf_str_pending_ref
= NULL
;
138 ctf_dynhash_destroy (fp
->ctf_str_atoms
);
139 fp
->ctf_str_atoms
= NULL
;
143 /* Destroy the atoms table. */
145 ctf_str_free_atoms (ctf_dict_t
*fp
)
147 ctf_dynhash_destroy (fp
->ctf_prov_strtab
);
148 ctf_dynhash_destroy (fp
->ctf_str_atoms
);
149 ctf_dynset_destroy (fp
->ctf_str_pending_ref
);
152 #define CTF_STR_ADD_REF 0x1
153 #define CTF_STR_MAKE_PROVISIONAL 0x2
154 #define CTF_STR_PENDING_REF 0x4
156 /* Add a string to the atoms table, copying the passed-in string. Return the
157 atom added. Return NULL only when out of memory (and do not touch the
158 passed-in string in that case). Possibly augment the ref list with the
159 passed-in ref. Possibly add a provisional entry for this string to the
160 provisional strtab. */
161 static ctf_str_atom_t
*
162 ctf_str_add_ref_internal (ctf_dict_t
*fp
, const char *str
,
163 int flags
, uint32_t *ref
)
166 ctf_str_atom_t
*atom
= NULL
;
167 ctf_str_atom_ref_t
*aref
= NULL
;
169 atom
= ctf_dynhash_lookup (fp
->ctf_str_atoms
, str
);
171 if (flags
& CTF_STR_ADD_REF
)
173 if ((aref
= malloc (sizeof (struct ctf_str_atom_ref
))) == NULL
)
180 if (flags
& CTF_STR_ADD_REF
)
182 ctf_dynset_remove (fp
->ctf_str_pending_ref
, (void *) ref
);
183 ctf_list_append (&atom
->csa_refs
, aref
);
184 fp
->ctf_str_num_refs
++;
189 if ((atom
= malloc (sizeof (struct ctf_str_atom
))) == NULL
)
191 memset (atom
, 0, sizeof (struct ctf_str_atom
));
193 if ((newstr
= strdup (str
)) == NULL
)
196 if (ctf_dynhash_insert (fp
->ctf_str_atoms
, newstr
, atom
) < 0)
199 atom
->csa_str
= newstr
;
200 atom
->csa_snapshot_id
= fp
->ctf_snapshots
;
202 if (flags
& CTF_STR_MAKE_PROVISIONAL
)
204 atom
->csa_offset
= fp
->ctf_str_prov_offset
;
206 if (ctf_dynhash_insert (fp
->ctf_prov_strtab
, (void *) (uintptr_t)
207 atom
->csa_offset
, (void *) atom
->csa_str
) < 0)
210 fp
->ctf_str_prov_offset
+= strlen (atom
->csa_str
) + 1;
213 if (flags
& CTF_STR_PENDING_REF
)
215 if (ctf_dynset_insert (fp
->ctf_str_pending_ref
, (void *) ref
) < 0)
218 else if (flags
& CTF_STR_ADD_REF
)
220 ctf_dynset_remove (fp
->ctf_str_pending_ref
, (void *) ref
);
221 ctf_list_append (&atom
->csa_refs
, aref
);
222 fp
->ctf_str_num_refs
++;
228 ctf_dynhash_remove (fp
->ctf_str_atoms
, newstr
);
232 ctf_set_errno (fp
, ENOMEM
);
236 /* Add a string to the atoms table, without augmenting the ref list for this
237 string: return a 'provisional offset' which can be used to return this string
238 until ctf_str_write_strtab is called, or 0 on failure. (Everywhere the
239 provisional offset is assigned to should be added as a ref using
240 ctf_str_add_ref() as well.) */
242 ctf_str_add (ctf_dict_t
*fp
, const char *str
)
244 ctf_str_atom_t
*atom
;
249 atom
= ctf_str_add_ref_internal (fp
, str
, CTF_STR_MAKE_PROVISIONAL
, 0);
253 return atom
->csa_offset
;
256 /* Like ctf_str_add(), but additionally augment the atom's refs list with the
257 passed-in ref, whether or not the string is already present. There is no
258 attempt to deduplicate the refs list (but duplicates are harmless). */
260 ctf_str_add_ref (ctf_dict_t
*fp
, const char *str
, uint32_t *ref
)
262 ctf_str_atom_t
*atom
;
267 atom
= ctf_str_add_ref_internal (fp
, str
, CTF_STR_ADD_REF
268 | CTF_STR_MAKE_PROVISIONAL
, ref
);
272 return atom
->csa_offset
;
275 /* Like ctf_str_add_ref(), but notes that this memory location must be added as
276 a ref by a later serialization phase, rather than adding it itself. */
278 ctf_str_add_pending (ctf_dict_t
*fp
, const char *str
, uint32_t *ref
)
280 ctf_str_atom_t
*atom
;
285 atom
= ctf_str_add_ref_internal (fp
, str
, CTF_STR_PENDING_REF
286 | CTF_STR_MAKE_PROVISIONAL
, ref
);
290 return atom
->csa_offset
;
293 /* Note that a pending ref now located at NEW_REF has moved by BYTES bytes. */
295 ctf_str_move_pending (ctf_dict_t
*fp
, uint32_t *new_ref
, ptrdiff_t bytes
)
300 if (ctf_dynset_insert (fp
->ctf_str_pending_ref
, (void *) new_ref
) < 0)
301 return (ctf_set_errno (fp
, ENOMEM
));
303 ctf_dynset_remove (fp
->ctf_str_pending_ref
,
304 (void *) ((signed char *) new_ref
- bytes
));
308 /* Add an external strtab reference at OFFSET. Returns zero if the addition
309 failed, nonzero otherwise. */
311 ctf_str_add_external (ctf_dict_t
*fp
, const char *str
, uint32_t offset
)
313 ctf_str_atom_t
*atom
;
318 atom
= ctf_str_add_ref_internal (fp
, str
, 0, 0);
322 atom
->csa_external_offset
= CTF_SET_STID (offset
, CTF_STRTAB_1
);
324 if (!fp
->ctf_syn_ext_strtab
)
325 fp
->ctf_syn_ext_strtab
= ctf_dynhash_create (ctf_hash_integer
,
328 if (!fp
->ctf_syn_ext_strtab
)
330 ctf_set_errno (fp
, ENOMEM
);
334 if (ctf_dynhash_insert (fp
->ctf_syn_ext_strtab
,
336 atom
->csa_external_offset
,
337 (void *) atom
->csa_str
) < 0)
339 /* No need to bother freeing the syn_ext_strtab: it will get freed at
340 ctf_str_write_strtab time if unreferenced. */
341 ctf_set_errno (fp
, ENOMEM
);
348 /* Remove a single ref. */
350 ctf_str_remove_ref (ctf_dict_t
*fp
, const char *str
, uint32_t *ref
)
352 ctf_str_atom_ref_t
*aref
, *anext
;
353 ctf_str_atom_t
*atom
= NULL
;
355 atom
= ctf_dynhash_lookup (fp
->ctf_str_atoms
, str
);
359 for (aref
= ctf_list_next (&atom
->csa_refs
); aref
!= NULL
; aref
= anext
)
361 anext
= ctf_list_next (aref
);
362 if (aref
->caf_ref
== ref
)
364 ctf_list_delete (&atom
->csa_refs
, aref
);
369 ctf_dynset_remove (fp
->ctf_str_pending_ref
, (void *) ref
);
372 /* A ctf_dynhash_iter_remove() callback that removes atoms later than a given
373 snapshot ID. External atoms are never removed, because they came from the
374 linker string table and are still present even if you roll back type
377 ctf_str_rollback_atom (void *key _libctf_unused_
, void *value
, void *arg
)
379 ctf_str_atom_t
*atom
= (ctf_str_atom_t
*) value
;
380 ctf_snapshot_id_t
*id
= (ctf_snapshot_id_t
*) arg
;
382 return (atom
->csa_snapshot_id
> id
->snapshot_id
)
383 && (atom
->csa_external_offset
== 0);
386 /* Roll back, deleting all (internal) atoms created after a particular ID. */
388 ctf_str_rollback (ctf_dict_t
*fp
, ctf_snapshot_id_t id
)
390 ctf_dynhash_iter_remove (fp
->ctf_str_atoms
, ctf_str_rollback_atom
, &id
);
393 /* An adaptor around ctf_purge_atom_refs. */
395 ctf_str_purge_one_atom_refs (void *key _libctf_unused_
, void *value
,
396 void *arg _libctf_unused_
)
398 ctf_str_atom_t
*atom
= (ctf_str_atom_t
*) value
;
399 ctf_str_purge_atom_refs (atom
);
402 /* Remove all the recorded refs from the atoms table. */
404 ctf_str_purge_refs (ctf_dict_t
*fp
)
406 if (fp
->ctf_str_num_refs
> 0)
407 ctf_dynhash_iter (fp
->ctf_str_atoms
, ctf_str_purge_one_atom_refs
, NULL
);
408 fp
->ctf_str_num_refs
= 0;
411 /* Update a list of refs to the specified value. */
413 ctf_str_update_refs (ctf_str_atom_t
*refs
, uint32_t value
)
415 ctf_str_atom_ref_t
*ref
;
417 for (ref
= ctf_list_next (&refs
->csa_refs
); ref
!= NULL
;
418 ref
= ctf_list_next (ref
))
419 *(ref
->caf_ref
) = value
;
422 /* State shared across the strtab write process. */
423 typedef struct ctf_strtab_write_state
425 /* Strtab we are writing, and the number of strings in it. */
426 ctf_strs_writable_t
*strtab
;
429 /* Pointers to (existing) atoms in the atoms table, for qsorting. */
430 ctf_str_atom_t
**sorttab
;
432 /* Loop counter for sorttab population. */
435 /* The null-string atom (skipped during population). */
436 ctf_str_atom_t
*nullstr
;
437 } ctf_strtab_write_state_t
;
439 /* Count the number of entries in the strtab, and its length. */
441 ctf_str_count_strtab (void *key _libctf_unused_
, void *value
,
444 ctf_str_atom_t
*atom
= (ctf_str_atom_t
*) value
;
445 ctf_strtab_write_state_t
*s
= (ctf_strtab_write_state_t
*) arg
;
447 /* We only factor in the length of items that have no offset and have refs:
448 other items are in the external strtab, or will simply not be written out
449 at all. They still contribute to the total count, though, because we still
450 have to sort them. We add in the null string's length explicitly, outside
451 this function, since it is explicitly written out even if it has no refs at
454 if (s
->nullstr
== atom
)
460 if (!ctf_list_empty_p (&atom
->csa_refs
))
462 if (!atom
->csa_external_offset
)
463 s
->strtab
->cts_len
+= strlen (atom
->csa_str
) + 1;
468 /* Populate the sorttab with pointers to the strtab atoms. */
470 ctf_str_populate_sorttab (void *key _libctf_unused_
, void *value
,
473 ctf_str_atom_t
*atom
= (ctf_str_atom_t
*) value
;
474 ctf_strtab_write_state_t
*s
= (ctf_strtab_write_state_t
*) arg
;
476 /* Skip the null string. */
477 if (s
->nullstr
== atom
)
480 /* Skip atoms with no refs. */
481 if (!ctf_list_empty_p (&atom
->csa_refs
))
482 s
->sorttab
[s
->i
++] = atom
;
485 /* Sort the strtab. */
487 ctf_str_sort_strtab (const void *a
, const void *b
)
489 ctf_str_atom_t
**one
= (ctf_str_atom_t
**) a
;
490 ctf_str_atom_t
**two
= (ctf_str_atom_t
**) b
;
492 return (strcmp ((*one
)->csa_str
, (*two
)->csa_str
));
495 /* Write out and return a strtab containing all strings with recorded refs,
496 adjusting the refs to refer to the corresponding string. The returned strtab
497 may be NULL on error. Also populate the synthetic strtab with mappings from
498 external strtab offsets to names, so we can look them up with ctf_strptr().
499 Only external strtab offsets with references are added. */
501 ctf_str_write_strtab (ctf_dict_t
*fp
)
503 ctf_strs_writable_t strtab
;
504 ctf_str_atom_t
*nullstr
;
505 uint32_t cur_stroff
= 0;
506 ctf_strtab_write_state_t s
;
507 ctf_str_atom_t
**sorttab
;
509 int any_external
= 0;
511 memset (&strtab
, 0, sizeof (struct ctf_strs_writable
));
512 memset (&s
, 0, sizeof (struct ctf_strtab_write_state
));
515 nullstr
= ctf_dynhash_lookup (fp
->ctf_str_atoms
, "");
518 ctf_err_warn (fp
, 0, ECTF_INTERNAL
, _("null string not found in strtab"));
519 strtab
.cts_strs
= NULL
;
524 ctf_dynhash_iter (fp
->ctf_str_atoms
, ctf_str_count_strtab
, &s
);
525 strtab
.cts_len
++; /* For the null string. */
527 ctf_dprintf ("%lu bytes of strings in strtab.\n",
528 (unsigned long) strtab
.cts_len
);
530 /* Sort the strtab. Force the null string to be first. */
531 sorttab
= calloc (s
.strtab_count
, sizeof (ctf_str_atom_t
*));
535 sorttab
[0] = nullstr
;
538 ctf_dynhash_iter (fp
->ctf_str_atoms
, ctf_str_populate_sorttab
, &s
);
540 qsort (&sorttab
[1], s
.strtab_count
- 1, sizeof (ctf_str_atom_t
*),
541 ctf_str_sort_strtab
);
543 if ((strtab
.cts_strs
= malloc (strtab
.cts_len
)) == NULL
)
546 /* Update all refs: also update the strtab appropriately. */
547 for (i
= 0; i
< s
.strtab_count
; i
++)
549 if (sorttab
[i
]->csa_external_offset
)
551 /* External strtab entry. */
554 ctf_str_update_refs (sorttab
[i
], sorttab
[i
]->csa_external_offset
);
555 sorttab
[i
]->csa_offset
= sorttab
[i
]->csa_external_offset
;
559 /* Internal strtab entry with refs: actually add to the string
562 ctf_str_update_refs (sorttab
[i
], cur_stroff
);
563 sorttab
[i
]->csa_offset
= cur_stroff
;
564 strcpy (&strtab
.cts_strs
[cur_stroff
], sorttab
[i
]->csa_str
);
565 cur_stroff
+= strlen (sorttab
[i
]->csa_str
) + 1;
572 ctf_dynhash_destroy (fp
->ctf_syn_ext_strtab
);
573 fp
->ctf_syn_ext_strtab
= NULL
;
576 /* All the provisional strtab entries are now real strtab entries, and
577 ctf_strptr() will find them there. The provisional offset now starts right
578 beyond the new end of the strtab. */
580 ctf_dynhash_empty (fp
->ctf_prov_strtab
);
581 fp
->ctf_str_prov_offset
= strtab
.cts_len
+ 1;