/src/binutils-gdb/libctf/ctf-string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* CTF string table management. |
2 | | Copyright (C) 2019-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of libctf. |
5 | | |
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 |
9 | | version. |
10 | | |
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. |
15 | | |
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/>. */ |
19 | | |
20 | | #include <assert.h> |
21 | | #include <ctf-impl.h> |
22 | | #include <string.h> |
23 | | |
24 | | static ctf_str_atom_t * |
25 | | ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str, |
26 | | int flags, uint32_t *ref); |
27 | | |
28 | | /* Convert an encoded CTF string name into a pointer to a C string, possibly |
29 | | using an explicit internal provisional strtab rather than the fp-based |
30 | | one. */ |
31 | | const char * |
32 | | ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab) |
33 | 0 | { |
34 | 0 | ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID (name)]; |
35 | |
|
36 | 0 | if ((CTF_NAME_STID (name) == CTF_STRTAB_0) && (strtab != NULL)) |
37 | 0 | ctsp = strtab; |
38 | | |
39 | | /* If this name is in the external strtab, and there is a synthetic |
40 | | strtab, use it in preference. (This is used to add the set of strings |
41 | | -- symbol names, etc -- the linker knows about before the strtab is |
42 | | written out.) */ |
43 | |
|
44 | 0 | if (CTF_NAME_STID (name) == CTF_STRTAB_1 |
45 | 0 | && fp->ctf_syn_ext_strtab != NULL) |
46 | 0 | return ctf_dynhash_lookup (fp->ctf_syn_ext_strtab, |
47 | 0 | (void *) (uintptr_t) name); |
48 | | |
49 | | /* If the name is in the internal strtab, and the name offset is beyond |
50 | | the end of the ctsp->cts_len but below the ctf_str_prov_offset, this is |
51 | | a provisional string added by ctf_str_add*() but not yet built into a |
52 | | real strtab: get the value out of the ctf_prov_strtab. */ |
53 | | |
54 | 0 | if (CTF_NAME_STID (name) == CTF_STRTAB_0 |
55 | 0 | && name >= ctsp->cts_len && name < fp->ctf_str_prov_offset) |
56 | 0 | return ctf_dynhash_lookup (fp->ctf_prov_strtab, |
57 | 0 | (void *) (uintptr_t) name); |
58 | | |
59 | 0 | if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET (name) < ctsp->cts_len) |
60 | 0 | return (ctsp->cts_strs + CTF_NAME_OFFSET (name)); |
61 | | |
62 | | /* String table not loaded or corrupt offset. */ |
63 | 0 | return NULL; |
64 | 0 | } |
65 | | |
66 | | /* Convert an encoded CTF string name into a pointer to a C string by looking |
67 | | up the appropriate string table buffer and then adding the offset. */ |
68 | | const char * |
69 | | ctf_strraw (ctf_dict_t *fp, uint32_t name) |
70 | 0 | { |
71 | 0 | return ctf_strraw_explicit (fp, name, NULL); |
72 | 0 | } |
73 | | |
74 | | /* Return a guaranteed-non-NULL pointer to the string with the given CTF |
75 | | name. */ |
76 | | const char * |
77 | | ctf_strptr (ctf_dict_t *fp, uint32_t name) |
78 | 0 | { |
79 | 0 | const char *s = ctf_strraw (fp, name); |
80 | 0 | return (s != NULL ? s : "(?)"); |
81 | 0 | } |
82 | | |
83 | | /* As above, but return info on what is wrong in more detail. |
84 | | (Used for type lookups.) */ |
85 | | |
86 | | const char * |
87 | | ctf_strptr_validate (ctf_dict_t *fp, uint32_t name) |
88 | 0 | { |
89 | 0 | const char *str = ctf_strraw (fp, name); |
90 | |
|
91 | 0 | if (str == NULL) |
92 | 0 | { |
93 | 0 | if (CTF_NAME_STID (name) == CTF_STRTAB_1 |
94 | 0 | && fp->ctf_syn_ext_strtab == NULL |
95 | 0 | && fp->ctf_str[CTF_NAME_STID (name)].cts_strs == NULL) |
96 | 0 | { |
97 | 0 | ctf_set_errno (fp, ECTF_STRTAB); |
98 | 0 | return NULL; |
99 | 0 | } |
100 | | |
101 | 0 | ctf_set_errno (fp, ECTF_BADNAME); |
102 | 0 | return NULL; |
103 | 0 | } |
104 | 0 | return str; |
105 | 0 | } |
106 | | |
107 | | /* Remove all refs to a given atom. */ |
108 | | static void |
109 | | ctf_str_purge_atom_refs (ctf_str_atom_t *atom) |
110 | 0 | { |
111 | 0 | ctf_str_atom_ref_t *ref, *next; |
112 | 0 | ctf_str_atom_ref_movable_t *movref, *movnext; |
113 | |
|
114 | 0 | for (ref = ctf_list_next (&atom->csa_refs); ref != NULL; ref = next) |
115 | 0 | { |
116 | 0 | next = ctf_list_next (ref); |
117 | 0 | ctf_list_delete (&atom->csa_refs, ref); |
118 | 0 | free (ref); |
119 | 0 | } |
120 | |
|
121 | 0 | for (movref = ctf_list_next (&atom->csa_movable_refs); |
122 | 0 | movref != NULL; movref = movnext) |
123 | 0 | { |
124 | 0 | movnext = ctf_list_next (movref); |
125 | 0 | ctf_list_delete (&atom->csa_movable_refs, movref); |
126 | |
|
127 | 0 | ctf_dynhash_remove (movref->caf_movable_refs, movref); |
128 | |
|
129 | 0 | free (movref); |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | | /* Free an atom. */ |
134 | | static void |
135 | | ctf_str_free_atom (void *a) |
136 | 0 | { |
137 | 0 | ctf_str_atom_t *atom = a; |
138 | |
|
139 | 0 | ctf_str_purge_atom_refs (atom); |
140 | |
|
141 | 0 | if (atom->csa_flags & CTF_STR_ATOM_FREEABLE) |
142 | 0 | free (atom->csa_str); |
143 | |
|
144 | 0 | free (atom); |
145 | 0 | } |
146 | | |
147 | | /* Create the atoms table. There is always at least one atom in it, the null |
148 | | string: but also pull in atoms from the internal strtab. (We rely on |
149 | | calls to ctf_str_add_external to populate external strtab entries, since |
150 | | these are often not quite the same as what appears in any external |
151 | | strtab, and the external strtab is often huge and best not aggressively |
152 | | pulled in.) */ |
153 | | int |
154 | | ctf_str_create_atoms (ctf_dict_t *fp) |
155 | 0 | { |
156 | 0 | size_t i; |
157 | |
|
158 | 0 | fp->ctf_str_atoms = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, |
159 | 0 | NULL, ctf_str_free_atom); |
160 | 0 | if (!fp->ctf_str_atoms) |
161 | 0 | return -ENOMEM; |
162 | | |
163 | 0 | if (!fp->ctf_prov_strtab) |
164 | 0 | fp->ctf_prov_strtab = ctf_dynhash_create (ctf_hash_integer, |
165 | 0 | ctf_hash_eq_integer, |
166 | 0 | NULL, NULL); |
167 | 0 | if (!fp->ctf_prov_strtab) |
168 | 0 | goto oom_prov_strtab; |
169 | | |
170 | 0 | fp->ctf_str_movable_refs = ctf_dynhash_create (ctf_hash_integer, |
171 | 0 | ctf_hash_eq_integer, |
172 | 0 | NULL, NULL); |
173 | 0 | if (!fp->ctf_str_movable_refs) |
174 | 0 | goto oom_movable_refs; |
175 | | |
176 | 0 | errno = 0; |
177 | 0 | ctf_str_add (fp, ""); |
178 | 0 | if (errno == ENOMEM) |
179 | 0 | goto oom_str_add; |
180 | | |
181 | | /* Pull in all the strings in the strtab as new atoms. The provisional |
182 | | strtab must be empty at this point, so there is no need to populate |
183 | | atoms from it as well. Types in this subset are frozen and readonly, |
184 | | so the refs list and movable refs list need not be populated. */ |
185 | | |
186 | 0 | for (i = 0; i < fp->ctf_str[CTF_STRTAB_0].cts_len; |
187 | 0 | i += strlen (&fp->ctf_str[CTF_STRTAB_0].cts_strs[i]) + 1) |
188 | 0 | { |
189 | 0 | ctf_str_atom_t *atom; |
190 | |
|
191 | 0 | if (fp->ctf_str[CTF_STRTAB_0].cts_strs[i] == 0) |
192 | 0 | continue; |
193 | | |
194 | 0 | atom = ctf_str_add_ref_internal (fp, &fp->ctf_str[CTF_STRTAB_0].cts_strs[i], |
195 | 0 | 0, 0); |
196 | |
|
197 | 0 | if (!atom) |
198 | 0 | goto oom_str_add; |
199 | | |
200 | 0 | atom->csa_offset = i; |
201 | 0 | } |
202 | | |
203 | 0 | fp->ctf_str_prov_offset = fp->ctf_str[CTF_STRTAB_0].cts_len + 1; |
204 | |
|
205 | 0 | return 0; |
206 | | |
207 | 0 | oom_str_add: |
208 | 0 | ctf_dynhash_destroy (fp->ctf_str_movable_refs); |
209 | 0 | fp->ctf_str_movable_refs = NULL; |
210 | 0 | oom_movable_refs: |
211 | 0 | ctf_dynhash_destroy (fp->ctf_prov_strtab); |
212 | 0 | fp->ctf_prov_strtab = NULL; |
213 | 0 | oom_prov_strtab: |
214 | 0 | ctf_dynhash_destroy (fp->ctf_str_atoms); |
215 | 0 | fp->ctf_str_atoms = NULL; |
216 | 0 | return -ENOMEM; |
217 | 0 | } |
218 | | |
219 | | /* Destroy the atoms table and associated refs. */ |
220 | | void |
221 | | ctf_str_free_atoms (ctf_dict_t *fp) |
222 | 0 | { |
223 | 0 | ctf_dynhash_destroy (fp->ctf_prov_strtab); |
224 | 0 | ctf_dynhash_destroy (fp->ctf_str_atoms); |
225 | 0 | ctf_dynhash_destroy (fp->ctf_str_movable_refs); |
226 | 0 | if (fp->ctf_dynstrtab) |
227 | 0 | { |
228 | 0 | free (fp->ctf_dynstrtab->cts_strs); |
229 | 0 | free (fp->ctf_dynstrtab); |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | 0 | #define CTF_STR_ADD_REF 0x1 |
234 | 0 | #define CTF_STR_PROVISIONAL 0x2 |
235 | 0 | #define CTF_STR_MOVABLE 0x4 |
236 | | |
237 | | /* Allocate a ref and bind it into a ref list. */ |
238 | | |
239 | | static ctf_str_atom_ref_t * |
240 | | aref_create (ctf_dict_t *fp, ctf_str_atom_t *atom, uint32_t *ref, int flags) |
241 | 0 | { |
242 | 0 | ctf_str_atom_ref_t *aref; |
243 | 0 | size_t s = sizeof (struct ctf_str_atom_ref); |
244 | |
|
245 | 0 | if (flags & CTF_STR_MOVABLE) |
246 | 0 | s = sizeof (struct ctf_str_atom_ref_movable); |
247 | |
|
248 | 0 | aref = malloc (s); |
249 | |
|
250 | 0 | if (!aref) |
251 | 0 | return NULL; |
252 | | |
253 | 0 | aref->caf_ref = ref; |
254 | | |
255 | | /* Movable refs get a backpointer to them in ctf_str_movable_refs, and a |
256 | | pointer to ctf_str_movable_refs itself in the ref, for use when freeing |
257 | | refs: they can be moved later in batches via a call to |
258 | | ctf_str_move_refs. */ |
259 | |
|
260 | 0 | if (flags & CTF_STR_MOVABLE) |
261 | 0 | { |
262 | 0 | ctf_str_atom_ref_movable_t *movref = (ctf_str_atom_ref_movable_t *) aref; |
263 | |
|
264 | 0 | movref->caf_movable_refs = fp->ctf_str_movable_refs; |
265 | |
|
266 | 0 | if (ctf_dynhash_insert (fp->ctf_str_movable_refs, ref, aref) < 0) |
267 | 0 | { |
268 | 0 | free (aref); |
269 | 0 | return NULL; |
270 | 0 | } |
271 | 0 | ctf_list_append (&atom->csa_movable_refs, movref); |
272 | 0 | } |
273 | 0 | else |
274 | 0 | ctf_list_append (&atom->csa_refs, aref); |
275 | | |
276 | 0 | return aref; |
277 | 0 | } |
278 | | |
279 | | /* Add a string to the atoms table, copying the passed-in string if |
280 | | necessary. Return the atom added. Return NULL only when out of memory |
281 | | (and do not touch the passed-in string in that case). |
282 | | |
283 | | Possibly add a provisional entry for this string to the provisional |
284 | | strtab. If the string is in the provisional strtab, update its ref list |
285 | | with the passed-in ref, causing the ref to be updated when the strtab is |
286 | | written out. */ |
287 | | |
288 | | static ctf_str_atom_t * |
289 | | ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str, |
290 | | int flags, uint32_t *ref) |
291 | 0 | { |
292 | 0 | char *newstr = NULL; |
293 | 0 | ctf_str_atom_t *atom = NULL; |
294 | 0 | int added = 0; |
295 | |
|
296 | 0 | atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str); |
297 | | |
298 | | /* Existing atoms get refs added only if they are provisional: |
299 | | non-provisional strings already have a fixed strtab offset, and just |
300 | | get their ref updated immediately, since its value cannot change. */ |
301 | |
|
302 | 0 | if (atom) |
303 | 0 | { |
304 | 0 | if (!ctf_dynhash_lookup (fp->ctf_prov_strtab, (void *) (uintptr_t) |
305 | 0 | atom->csa_offset)) |
306 | 0 | { |
307 | 0 | if (flags & CTF_STR_ADD_REF) |
308 | 0 | { |
309 | 0 | if (atom->csa_external_offset) |
310 | 0 | *ref = atom->csa_external_offset; |
311 | 0 | else |
312 | 0 | *ref = atom->csa_offset; |
313 | 0 | } |
314 | 0 | return atom; |
315 | 0 | } |
316 | | |
317 | 0 | if (flags & CTF_STR_ADD_REF) |
318 | 0 | { |
319 | 0 | if (!aref_create (fp, atom, ref, flags)) |
320 | 0 | { |
321 | 0 | ctf_set_errno (fp, ENOMEM); |
322 | 0 | return NULL; |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | 0 | return atom; |
327 | 0 | } |
328 | | |
329 | | /* New atom. */ |
330 | | |
331 | 0 | if ((atom = malloc (sizeof (struct ctf_str_atom))) == NULL) |
332 | 0 | goto oom; |
333 | 0 | memset (atom, 0, sizeof (struct ctf_str_atom)); |
334 | | |
335 | | /* Don't allocate new strings if this string is within an mmapped |
336 | | strtab. */ |
337 | |
|
338 | 0 | if ((unsigned char *) str < (unsigned char *) fp->ctf_data_mmapped |
339 | 0 | || (unsigned char *) str > (unsigned char *) fp->ctf_data_mmapped + fp->ctf_data_mmapped_len) |
340 | 0 | { |
341 | 0 | if ((newstr = strdup (str)) == NULL) |
342 | 0 | goto oom; |
343 | 0 | atom->csa_flags |= CTF_STR_ATOM_FREEABLE; |
344 | 0 | atom->csa_str = newstr; |
345 | 0 | } |
346 | 0 | else |
347 | 0 | atom->csa_str = (char *) str; |
348 | | |
349 | 0 | if (ctf_dynhash_insert (fp->ctf_str_atoms, atom->csa_str, atom) < 0) |
350 | 0 | goto oom; |
351 | 0 | added = 1; |
352 | |
|
353 | 0 | atom->csa_snapshot_id = fp->ctf_snapshots; |
354 | | |
355 | | /* New atoms marked provisional go into the provisional strtab, and get a |
356 | | ref added. */ |
357 | |
|
358 | 0 | if (flags & CTF_STR_PROVISIONAL) |
359 | 0 | { |
360 | 0 | atom->csa_offset = fp->ctf_str_prov_offset; |
361 | |
|
362 | 0 | if (ctf_dynhash_insert (fp->ctf_prov_strtab, (void *) (uintptr_t) |
363 | 0 | atom->csa_offset, (void *) atom->csa_str) < 0) |
364 | 0 | goto oom; |
365 | | |
366 | 0 | fp->ctf_str_prov_offset += strlen (atom->csa_str) + 1; |
367 | |
|
368 | 0 | if (flags & CTF_STR_ADD_REF) |
369 | 0 | { |
370 | 0 | if (!aref_create (fp, atom, ref, flags)) |
371 | 0 | goto oom; |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | return atom; |
376 | | |
377 | 0 | oom: |
378 | 0 | if (added) |
379 | 0 | ctf_dynhash_remove (fp->ctf_str_atoms, atom->csa_str); |
380 | 0 | free (atom); |
381 | 0 | free (newstr); |
382 | 0 | ctf_set_errno (fp, ENOMEM); |
383 | 0 | return NULL; |
384 | 0 | } |
385 | | |
386 | | /* Add a string to the atoms table, without augmenting the ref list for this |
387 | | string: return a 'provisional offset' which can be used to return this string |
388 | | until ctf_str_write_strtab is called, or 0 on failure. (Everywhere the |
389 | | provisional offset is assigned to should be added as a ref using |
390 | | ctf_str_add_ref() as well.) */ |
391 | | uint32_t |
392 | | ctf_str_add (ctf_dict_t *fp, const char *str) |
393 | 0 | { |
394 | 0 | ctf_str_atom_t *atom; |
395 | |
|
396 | 0 | if (!str) |
397 | 0 | str = ""; |
398 | |
|
399 | 0 | atom = ctf_str_add_ref_internal (fp, str, CTF_STR_PROVISIONAL, 0); |
400 | 0 | if (!atom) |
401 | 0 | return 0; |
402 | | |
403 | 0 | return atom->csa_offset; |
404 | 0 | } |
405 | | |
406 | | /* Like ctf_str_add(), but additionally augment the atom's refs list with the |
407 | | passed-in ref, whether or not the string is already present. There is no |
408 | | attempt to deduplicate the refs list (but duplicates are harmless). */ |
409 | | uint32_t |
410 | | ctf_str_add_ref (ctf_dict_t *fp, const char *str, uint32_t *ref) |
411 | 0 | { |
412 | 0 | ctf_str_atom_t *atom; |
413 | |
|
414 | 0 | if (!str) |
415 | 0 | str = ""; |
416 | |
|
417 | 0 | atom = ctf_str_add_ref_internal (fp, str, CTF_STR_ADD_REF |
418 | 0 | | CTF_STR_PROVISIONAL, ref); |
419 | 0 | if (!atom) |
420 | 0 | return 0; |
421 | | |
422 | 0 | return atom->csa_offset; |
423 | 0 | } |
424 | | |
425 | | /* Like ctf_str_add_ref(), but note that the ref may be moved later on. */ |
426 | | uint32_t |
427 | | ctf_str_add_movable_ref (ctf_dict_t *fp, const char *str, uint32_t *ref) |
428 | 0 | { |
429 | 0 | ctf_str_atom_t *atom; |
430 | |
|
431 | 0 | if (!str) |
432 | 0 | str = ""; |
433 | |
|
434 | 0 | atom = ctf_str_add_ref_internal (fp, str, CTF_STR_ADD_REF |
435 | 0 | | CTF_STR_PROVISIONAL |
436 | 0 | | CTF_STR_MOVABLE, ref); |
437 | 0 | if (!atom) |
438 | 0 | return 0; |
439 | | |
440 | 0 | return atom->csa_offset; |
441 | 0 | } |
442 | | |
443 | | /* Add an external strtab reference at OFFSET. Returns zero if the addition |
444 | | failed, nonzero otherwise. */ |
445 | | int |
446 | | ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset) |
447 | 0 | { |
448 | 0 | ctf_str_atom_t *atom; |
449 | |
|
450 | 0 | if (!str) |
451 | 0 | str = ""; |
452 | |
|
453 | 0 | atom = ctf_str_add_ref_internal (fp, str, 0, 0); |
454 | 0 | if (!atom) |
455 | 0 | return 0; |
456 | | |
457 | 0 | atom->csa_external_offset = CTF_SET_STID (offset, CTF_STRTAB_1); |
458 | |
|
459 | 0 | if (!fp->ctf_syn_ext_strtab) |
460 | 0 | fp->ctf_syn_ext_strtab = ctf_dynhash_create (ctf_hash_integer, |
461 | 0 | ctf_hash_eq_integer, |
462 | 0 | NULL, NULL); |
463 | 0 | if (!fp->ctf_syn_ext_strtab) |
464 | 0 | { |
465 | 0 | ctf_set_errno (fp, ENOMEM); |
466 | 0 | return 0; |
467 | 0 | } |
468 | | |
469 | 0 | if (ctf_dynhash_insert (fp->ctf_syn_ext_strtab, |
470 | 0 | (void *) (uintptr_t) |
471 | 0 | atom->csa_external_offset, |
472 | 0 | (void *) atom->csa_str) < 0) |
473 | 0 | { |
474 | | /* No need to bother freeing the syn_ext_strtab: it will get freed at |
475 | | ctf_str_write_strtab time if unreferenced. */ |
476 | 0 | ctf_set_errno (fp, ENOMEM); |
477 | 0 | return 0; |
478 | 0 | } |
479 | | |
480 | 0 | return 1; |
481 | 0 | } |
482 | | |
483 | | /* Note that refs have moved from (SRC, LEN) to DEST. We use the movable |
484 | | refs backpointer for this, because it is done an amortized-constant |
485 | | number of times during structure member and enumerand addition, and if we |
486 | | did a linear search this would turn such addition into an O(n^2) |
487 | | operation. Even this is not linear, but it's better than that. */ |
488 | | int |
489 | | ctf_str_move_refs (ctf_dict_t *fp, void *src, size_t len, void *dest) |
490 | 0 | { |
491 | 0 | uintptr_t p; |
492 | |
|
493 | 0 | if (src == dest) |
494 | 0 | return 0; |
495 | | |
496 | 0 | for (p = (uintptr_t) src; p - (uintptr_t) src < len; p++) |
497 | 0 | { |
498 | 0 | ctf_str_atom_ref_movable_t *ref; |
499 | |
|
500 | 0 | if ((ref = ctf_dynhash_lookup (fp->ctf_str_movable_refs, |
501 | 0 | (ctf_str_atom_ref_t *) p)) != NULL) |
502 | 0 | { |
503 | 0 | int out_of_memory; |
504 | |
|
505 | 0 | ref->caf_ref = (uint32_t *) (((uintptr_t) ref->caf_ref + |
506 | 0 | (uintptr_t) dest - (uintptr_t) src)); |
507 | 0 | ctf_dynhash_remove (fp->ctf_str_movable_refs, |
508 | 0 | (ctf_str_atom_ref_t *) p); |
509 | 0 | out_of_memory = ctf_dynhash_insert (fp->ctf_str_movable_refs, |
510 | 0 | ref->caf_ref, ref); |
511 | 0 | assert (out_of_memory == 0); |
512 | 0 | } |
513 | 0 | } |
514 | | |
515 | 0 | return 0; |
516 | 0 | } |
517 | | |
518 | | /* Remove a single ref. */ |
519 | | void |
520 | | ctf_str_remove_ref (ctf_dict_t *fp, const char *str, uint32_t *ref) |
521 | 0 | { |
522 | 0 | ctf_str_atom_ref_t *aref, *anext; |
523 | 0 | ctf_str_atom_ref_movable_t *amovref, *amovnext; |
524 | 0 | ctf_str_atom_t *atom = NULL; |
525 | |
|
526 | 0 | atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str); |
527 | 0 | if (!atom) |
528 | 0 | return; |
529 | | |
530 | 0 | for (aref = ctf_list_next (&atom->csa_refs); aref != NULL; aref = anext) |
531 | 0 | { |
532 | 0 | anext = ctf_list_next (aref); |
533 | 0 | if (aref->caf_ref == ref) |
534 | 0 | { |
535 | 0 | ctf_list_delete (&atom->csa_refs, aref); |
536 | 0 | free (aref); |
537 | 0 | } |
538 | 0 | } |
539 | |
|
540 | 0 | for (amovref = ctf_list_next (&atom->csa_movable_refs); |
541 | 0 | amovref != NULL; amovref = amovnext) |
542 | 0 | { |
543 | 0 | amovnext = ctf_list_next (amovref); |
544 | 0 | if (amovref->caf_ref == ref) |
545 | 0 | { |
546 | 0 | ctf_list_delete (&atom->csa_movable_refs, amovref); |
547 | 0 | ctf_dynhash_remove (fp->ctf_str_movable_refs, ref); |
548 | 0 | free (amovref); |
549 | 0 | } |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | | /* A ctf_dynhash_iter_remove() callback that removes atoms later than a given |
554 | | snapshot ID. External atoms are never removed, because they came from the |
555 | | linker string table and are still present even if you roll back type |
556 | | additions. */ |
557 | | static int |
558 | | ctf_str_rollback_atom (void *key _libctf_unused_, void *value, void *arg) |
559 | 0 | { |
560 | 0 | ctf_str_atom_t *atom = (ctf_str_atom_t *) value; |
561 | 0 | ctf_snapshot_id_t *id = (ctf_snapshot_id_t *) arg; |
562 | |
|
563 | 0 | return (atom->csa_snapshot_id > id->snapshot_id) |
564 | 0 | && (atom->csa_external_offset == 0); |
565 | 0 | } |
566 | | |
567 | | /* Roll back, deleting all (internal) atoms created after a particular ID. */ |
568 | | void |
569 | | ctf_str_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id) |
570 | 0 | { |
571 | 0 | ctf_dynhash_iter_remove (fp->ctf_str_atoms, ctf_str_rollback_atom, &id); |
572 | 0 | } |
573 | | |
574 | | /* An adaptor around ctf_purge_atom_refs. */ |
575 | | static void |
576 | | ctf_str_purge_one_atom_refs (void *key _libctf_unused_, void *value, |
577 | | void *arg _libctf_unused_) |
578 | 0 | { |
579 | 0 | ctf_str_atom_t *atom = (ctf_str_atom_t *) value; |
580 | |
|
581 | 0 | ctf_str_purge_atom_refs (atom); |
582 | 0 | } |
583 | | |
584 | | /* Remove all the recorded refs from the atoms table. */ |
585 | | static void |
586 | | ctf_str_purge_refs (ctf_dict_t *fp) |
587 | 0 | { |
588 | 0 | ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_purge_one_atom_refs, NULL); |
589 | 0 | } |
590 | | |
591 | | /* Update a list of refs to the specified value. */ |
592 | | static void |
593 | | ctf_str_update_refs (ctf_str_atom_t *refs, uint32_t value) |
594 | 0 | { |
595 | 0 | ctf_str_atom_ref_t *ref; |
596 | 0 | ctf_str_atom_ref_movable_t *movref; |
597 | |
|
598 | 0 | for (ref = ctf_list_next (&refs->csa_refs); ref != NULL; |
599 | 0 | ref = ctf_list_next (ref)) |
600 | 0 | *(ref->caf_ref) = value; |
601 | |
|
602 | 0 | for (movref = ctf_list_next (&refs->csa_movable_refs); |
603 | 0 | movref != NULL; movref = ctf_list_next (movref)) |
604 | 0 | *(movref->caf_ref) = value; |
605 | 0 | } |
606 | | |
607 | | /* Sort the strtab. */ |
608 | | static int |
609 | | ctf_str_sort_strtab (const void *a, const void *b) |
610 | 0 | { |
611 | 0 | ctf_str_atom_t **one = (ctf_str_atom_t **) a; |
612 | 0 | ctf_str_atom_t **two = (ctf_str_atom_t **) b; |
613 | |
|
614 | 0 | return (strcmp ((*one)->csa_str, (*two)->csa_str)); |
615 | 0 | } |
616 | | |
617 | | /* Write out and return a strtab containing all strings with recorded refs, |
618 | | adjusting the refs to refer to the corresponding string. The returned |
619 | | strtab is already assigned to strtab 0 in this dict, is owned by this |
620 | | dict, and may be NULL on error. Also populate the synthetic strtab with |
621 | | mappings from external strtab offsets to names, so we can look them up |
622 | | with ctf_strptr(). Only external strtab offsets with references are |
623 | | added. |
624 | | |
625 | | As a side effect, replaces the strtab of the current dict with the newly- |
626 | | generated strtab. This is an exception to the general rule that |
627 | | serialization does not change the dict passed in, because the alternative |
628 | | is to copy the entire atoms table on every reserialization just to avoid |
629 | | modifying the original, which is excessively costly for minimal gain. |
630 | | |
631 | | We use the lazy man's approach and double memory costs by always storing |
632 | | atoms as individually allocated entities whenever they come from anywhere |
633 | | but a freshly-opened, mmapped dict, even though after serialization there |
634 | | is another copy in the strtab; this ensures that ctf_strptr()-returned |
635 | | pointers to them remain valid for the lifetime of the dict. |
636 | | |
637 | | This is all rendered more complex because if a dict is ctf_open()ed it |
638 | | will have a bunch of strings in its strtab already, and their strtab |
639 | | offsets can never change (without piles of complexity to rescan the |
640 | | entire dict just to get all the offsets to all of them into the atoms |
641 | | table). Entries below the existing strtab limit are just copied into the |
642 | | new dict: entries above it are new, and are are sorted first, then |
643 | | appended to it. The sorting is purely a compression-efficiency |
644 | | improvement, and we get nearly as good an improvement from sorting big |
645 | | chunks like this as we would from sorting the whole thing. */ |
646 | | |
647 | | const ctf_strs_writable_t * |
648 | | ctf_str_write_strtab (ctf_dict_t *fp) |
649 | 0 | { |
650 | 0 | ctf_strs_writable_t *strtab; |
651 | 0 | size_t strtab_count = 0; |
652 | 0 | uint32_t cur_stroff = 0; |
653 | 0 | ctf_str_atom_t **sorttab; |
654 | 0 | ctf_next_t *it = NULL; |
655 | 0 | size_t i; |
656 | 0 | void *v; |
657 | 0 | int err; |
658 | 0 | int new_strtab = 0; |
659 | 0 | int any_external = 0; |
660 | |
|
661 | 0 | strtab = calloc (1, sizeof (ctf_strs_writable_t)); |
662 | 0 | if (!strtab) |
663 | 0 | return NULL; |
664 | | |
665 | | /* The strtab contains the existing string table at its start: figure out |
666 | | how many new strings we need to add. We only need to add new strings |
667 | | that have no external offset, that have refs, and that are found in the |
668 | | provisional strtab. If the existing strtab is empty we also need to |
669 | | add the null string at its start. */ |
670 | | |
671 | 0 | strtab->cts_len = fp->ctf_str[CTF_STRTAB_0].cts_len; |
672 | |
|
673 | 0 | if (strtab->cts_len == 0) |
674 | 0 | { |
675 | 0 | new_strtab = 1; |
676 | 0 | strtab->cts_len++; /* For the \0. */ |
677 | 0 | } |
678 | | |
679 | | /* Count new entries in the strtab: i.e. entries in the provisional |
680 | | strtab. Ignore any entry for \0, entries which ended up in the |
681 | | external strtab, and unreferenced entries. */ |
682 | |
|
683 | 0 | while ((err = ctf_dynhash_next (fp->ctf_prov_strtab, &it, NULL, &v)) == 0) |
684 | 0 | { |
685 | 0 | const char *str = (const char *) v; |
686 | 0 | ctf_str_atom_t *atom; |
687 | |
|
688 | 0 | atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str); |
689 | 0 | if (!ctf_assert (fp, atom)) |
690 | 0 | goto err_strtab; |
691 | | |
692 | 0 | if (atom->csa_str[0] == 0 || atom->csa_external_offset |
693 | 0 | || (ctf_list_empty_p (&atom->csa_refs) |
694 | 0 | && ctf_list_empty_p (&atom->csa_movable_refs))) |
695 | 0 | continue; |
696 | | |
697 | 0 | strtab->cts_len += strlen (atom->csa_str) + 1; |
698 | 0 | strtab_count++; |
699 | 0 | } |
700 | 0 | if (err != ECTF_NEXT_END) |
701 | 0 | { |
702 | 0 | ctf_dprintf ("ctf_str_write_strtab: error counting strtab entries: %s\n", |
703 | 0 | ctf_errmsg (err)); |
704 | 0 | goto err_strtab; |
705 | 0 | } |
706 | | |
707 | 0 | ctf_dprintf ("%lu bytes of strings in strtab: %lu pre-existing.\n", |
708 | 0 | (unsigned long) strtab->cts_len, |
709 | 0 | (unsigned long) fp->ctf_str[CTF_STRTAB_0].cts_len); |
710 | | |
711 | | /* Sort the new part of the strtab. */ |
712 | |
|
713 | 0 | sorttab = calloc (strtab_count, sizeof (ctf_str_atom_t *)); |
714 | 0 | if (!sorttab) |
715 | 0 | { |
716 | 0 | ctf_set_errno (fp, ENOMEM); |
717 | 0 | goto err_strtab; |
718 | 0 | } |
719 | | |
720 | 0 | i = 0; |
721 | 0 | while ((err = ctf_dynhash_next (fp->ctf_prov_strtab, &it, NULL, &v)) == 0) |
722 | 0 | { |
723 | 0 | ctf_str_atom_t *atom; |
724 | |
|
725 | 0 | atom = ctf_dynhash_lookup (fp->ctf_str_atoms, v); |
726 | 0 | if (!ctf_assert (fp, atom)) |
727 | 0 | goto err_sorttab; |
728 | | |
729 | 0 | if (atom->csa_str[0] == 0 || atom->csa_external_offset |
730 | 0 | || (ctf_list_empty_p (&atom->csa_refs) |
731 | 0 | && ctf_list_empty_p (&atom->csa_movable_refs))) |
732 | 0 | continue; |
733 | | |
734 | 0 | sorttab[i++] = atom; |
735 | 0 | } |
736 | | |
737 | 0 | qsort (sorttab, strtab_count, sizeof (ctf_str_atom_t *), |
738 | 0 | ctf_str_sort_strtab); |
739 | |
|
740 | 0 | if ((strtab->cts_strs = malloc (strtab->cts_len)) == NULL) |
741 | 0 | goto err_sorttab; |
742 | | |
743 | 0 | cur_stroff = fp->ctf_str[CTF_STRTAB_0].cts_len; |
744 | |
|
745 | 0 | if (new_strtab) |
746 | 0 | { |
747 | 0 | strtab->cts_strs[0] = 0; |
748 | 0 | cur_stroff++; |
749 | 0 | } |
750 | 0 | else |
751 | 0 | memcpy (strtab->cts_strs, fp->ctf_str[CTF_STRTAB_0].cts_strs, |
752 | 0 | fp->ctf_str[CTF_STRTAB_0].cts_len); |
753 | | |
754 | | /* Work over the sorttab, add its strings to the strtab, and remember |
755 | | where they are in the csa_offset for the appropriate atom. No ref |
756 | | updating is done at this point, because refs might well relate to |
757 | | already-existing strings, or external strings, which do not need adding |
758 | | to the strtab and may not be in the sorttab. */ |
759 | |
|
760 | 0 | for (i = 0; i < strtab_count; i++) |
761 | 0 | { |
762 | 0 | sorttab[i]->csa_offset = cur_stroff; |
763 | 0 | strcpy (&strtab->cts_strs[cur_stroff], sorttab[i]->csa_str); |
764 | 0 | cur_stroff += strlen (sorttab[i]->csa_str) + 1; |
765 | 0 | } |
766 | 0 | free (sorttab); |
767 | 0 | sorttab = NULL; |
768 | | |
769 | | /* Update all refs, then purge them as no longer necessary: also update |
770 | | the strtab appropriately. */ |
771 | |
|
772 | 0 | while ((err = ctf_dynhash_next (fp->ctf_str_atoms, &it, NULL, &v)) == 0) |
773 | 0 | { |
774 | 0 | ctf_str_atom_t *atom = (ctf_str_atom_t *) v; |
775 | 0 | uint32_t offset; |
776 | |
|
777 | 0 | if (ctf_list_empty_p (&atom->csa_refs) && |
778 | 0 | ctf_list_empty_p (&atom->csa_movable_refs)) |
779 | 0 | continue; |
780 | | |
781 | 0 | if (atom->csa_external_offset) |
782 | 0 | { |
783 | 0 | any_external = 1; |
784 | 0 | offset = atom->csa_external_offset; |
785 | 0 | } |
786 | 0 | else |
787 | 0 | offset = atom->csa_offset; |
788 | 0 | ctf_str_update_refs (atom, offset); |
789 | 0 | } |
790 | 0 | if (err != ECTF_NEXT_END) |
791 | 0 | { |
792 | 0 | ctf_dprintf ("ctf_str_write_strtab: error iterating over atoms while updating refs: %s\n", |
793 | 0 | ctf_errmsg (err)); |
794 | 0 | goto err_strtab; |
795 | 0 | } |
796 | 0 | ctf_str_purge_refs (fp); |
797 | |
|
798 | 0 | if (!any_external) |
799 | 0 | { |
800 | 0 | ctf_dynhash_destroy (fp->ctf_syn_ext_strtab); |
801 | 0 | fp->ctf_syn_ext_strtab = NULL; |
802 | 0 | } |
803 | | |
804 | | /* Replace the old strtab with the new one in this dict. */ |
805 | |
|
806 | 0 | if (fp->ctf_dynstrtab) |
807 | 0 | { |
808 | 0 | free (fp->ctf_dynstrtab->cts_strs); |
809 | 0 | free (fp->ctf_dynstrtab); |
810 | 0 | } |
811 | |
|
812 | 0 | fp->ctf_dynstrtab = strtab; |
813 | 0 | fp->ctf_str[CTF_STRTAB_0].cts_strs = strtab->cts_strs; |
814 | 0 | fp->ctf_str[CTF_STRTAB_0].cts_len = strtab->cts_len; |
815 | | |
816 | | /* All the provisional strtab entries are now real strtab entries, and |
817 | | ctf_strptr() will find them there. The provisional offset now starts right |
818 | | beyond the new end of the strtab. */ |
819 | |
|
820 | 0 | ctf_dynhash_empty (fp->ctf_prov_strtab); |
821 | 0 | fp->ctf_str_prov_offset = strtab->cts_len + 1; |
822 | 0 | return strtab; |
823 | | |
824 | 0 | err_sorttab: |
825 | 0 | free (sorttab); |
826 | 0 | err_strtab: |
827 | 0 | free (strtab); |
828 | 0 | return NULL; |
829 | 0 | } |