aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-string.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2024-03-26 13:04:20 +0000
committerNick Alcock <nick.alcock@oracle.com>2024-04-19 16:14:47 +0100
commit483546ce4f36a93d3fb711f2261b15b75871f3f9 (patch)
tree7022abc55beff5b441d1c959ae52cbc18bafaf82 /libctf/ctf-string.c
parentcf9da3b0b6a6ae9d71ba36898a5e39710150f85e (diff)
downloadbinutils-483546ce4f36a93d3fb711f2261b15b75871f3f9.zip
binutils-483546ce4f36a93d3fb711f2261b15b75871f3f9.tar.gz
binutils-483546ce4f36a93d3fb711f2261b15b75871f3f9.tar.bz2
libctf: make ctf_serialize() actually serialize
ctf_serialize() evolved from the old ctf_update(), which mutated the in-memory CTF dict to make all the dynamic in-memory types into static, unchanging written-to-the-dict types (by deserializing and reserializing it): back in the days when you could only do type lookups on static types, this meant you could see all the types you added recently, at the small, small cost of making it impossible to change those older types ever again and inducing an amortized O(n^2) cost if you actually wanted to add references to types you added at arbitrary times to later types. It also reset things so that ctf_discard() would throw away only types you added after the most recent ctf_update() call. Some time ago this was all changed so that you could look up dynamic types just as easily as static types: ctf_update() changed so that only its visible side-effect of affecting ctf_discard() remained: the old ctf_update() was renamed to ctf_serialize(), made internal to libctf, and called from the various functions that wrote files out. ... but it was still working by serializing and deserializing the entire dict, swapping out its guts with the newly-serialized copy in an invasive and horrible fashion that coupled ctf_serialize() to almost every field in the ctf_dict_t. This is totally useless, and fixing it is easy: just rip all that code out and have ctf_serialize return a serialized representation, and let everything use that directly. This simplifies most of its callers significantly. (It also points up another bug: ctf_gzwrite() failed to call ctf_serialize() at all, so it would only ever work for a dict you just ctf_write_mem()ed yourself, just for its invisible side-effect of serializing the dict!) This lets us simplify away a bunch of internal-only open-side functionality for overriding the syn_ext_strtab and some just-added functionality for forcing in an existing atoms table, without loss of functionality, and lets us lift the restriction on reserializing a dict that was ctf_open()ed rather than being ctf_create()d: it's now perfectly OK to open a dict, modify it (except for adding members to existing structs, unions, or enums, which fails with -ECTF_RDONLY), and write it out again, just as one would expect. libctf/ * ctf-serialize.c (ctf_symtypetab_sect_sizes): Fix typos. (ctf_type_sect_size): Add static type sizes too. (ctf_serialize): Return the new dict rather than updating the existing dict. No longer fail for dicts with static types; copy them onto the start of the new types table. (ctf_gzwrite): Actually serialize before gzwriting. (ctf_write_mem): Improve forced (test-mode) endian-flipping: flip dicts even if they are too small to be compressed. Improve confusing variable naming. * ctf-archive.c (arc_write_one_ctf): Don't bother to call ctf_serialize: both the functions we call do so. * ctf-string.c (ctf_str_create_atoms): Drop serializing case (atoms arg). * ctf-open.c (ctf_simple_open): Call ctf_bufopen directly. (ctf_simple_open_internal): Delete. (ctf_bufopen_internal): Delete/rename to ctf_bufopen: no longer bother with syn_ext_strtab or forced atoms table, serialization no longer needs them. * ctf-create.c (ctf_create): Call ctf_bufopen directly. * ctf-impl.h (ctf_str_create_atoms): Drop atoms arg. (ctf_simple_open_internal): Delete. (ctf_bufopen_internal): Likewise. (ctf_serialize): Adjust. * testsuite/libctf-lookup/add-to-opened.c: Adjust now that this is supposed to work.
Diffstat (limited to 'libctf/ctf-string.c')
-rw-r--r--libctf/ctf-string.c73
1 files changed, 16 insertions, 57 deletions
diff --git a/libctf/ctf-string.c b/libctf/ctf-string.c
index 46b984b..0c5bd58 100644
--- a/libctf/ctf-string.c
+++ b/libctf/ctf-string.c
@@ -144,15 +144,9 @@ ctf_str_free_atom (void *a)
calls to ctf_str_add_external to populate external strtab entries, since
these are often not quite the same as what appears in any external
strtab, and the external strtab is often huge and best not aggressively
- pulled in.)
-
- Alternatively, if passed, populate atoms from the passed-in table, but do
- not propagate their flags or refs: they are all non-freeable and
- non-movable. (This is used when serializing a dict: this entire atoms
- table will be thrown away shortly, so it is important that we not create
- any new strings.) */
+ pulled in.) */
int
-ctf_str_create_atoms (ctf_dict_t *fp, ctf_dynhash_t *atoms)
+ctf_str_create_atoms (ctf_dict_t *fp)
{
size_t i;
@@ -179,61 +173,26 @@ ctf_str_create_atoms (ctf_dict_t *fp, ctf_dynhash_t *atoms)
if (errno == ENOMEM)
goto oom_str_add;
- /* Serializing. We have existing strings in an existing atoms table with
- possibly-live pointers to them which must be used unchanged. Import
- them into this atoms table. */
-
- if (atoms)
- {
- ctf_next_t *it = NULL;
- void *k, *v;
- int err;
-
- while ((err = ctf_dynhash_next (atoms, &it, &k, &v)) == 0)
- {
- ctf_str_atom_t *existing = v;
- ctf_str_atom_t *atom;
-
- if (existing->csa_str[0] == 0)
- continue;
+ /* Pull in all the strings in the strtab as new atoms. The provisional
+ strtab must be empty at this point, so there is no need to populate
+ atoms from it as well. Types in this subset are frozen and readonly,
+ so the refs list and movable refs list need not be populated. */
- if ((atom = malloc (sizeof (struct ctf_str_atom))) == NULL)
- goto oom_str_add;
- memcpy (atom, existing, sizeof (struct ctf_str_atom));
- memset (&atom->csa_refs, 0, sizeof(ctf_list_t));
- atom->csa_flags = 0;
-
- if (ctf_dynhash_insert (fp->ctf_str_atoms, atom->csa_str, atom) < 0)
- {
- free (atom);
- goto oom_str_add;
- }
- }
- }
- else
+ for (i = 0; i < fp->ctf_str[CTF_STRTAB_0].cts_len;
+ i += strlen (&fp->ctf_str[CTF_STRTAB_0].cts_strs[i]) + 1)
{
- /* Not serializing. Pull in all the strings in the strtab as new
- atoms. The provisional strtab must be empty at this point, so
- there is no need to populate atoms from it as well. Types in this
- subset are frozen and readonly, so the refs list and movable refs
- list need not be populated. */
-
- for (i = 0; i < fp->ctf_str[CTF_STRTAB_0].cts_len;
- i += strlen (&fp->ctf_str[CTF_STRTAB_0].cts_strs[i]) + 1)
- {
- ctf_str_atom_t *atom;
+ ctf_str_atom_t *atom;
- if (fp->ctf_str[CTF_STRTAB_0].cts_strs[i] == 0)
- continue;
+ if (fp->ctf_str[CTF_STRTAB_0].cts_strs[i] == 0)
+ continue;
- atom = ctf_str_add_ref_internal (fp, &fp->ctf_str[CTF_STRTAB_0].cts_strs[i],
- 0, 0);
+ atom = ctf_str_add_ref_internal (fp, &fp->ctf_str[CTF_STRTAB_0].cts_strs[i],
+ 0, 0);
- if (!atom)
- goto oom_str_add;
+ if (!atom)
+ goto oom_str_add;
- atom->csa_offset = i;
- }
+ atom->csa_offset = i;
}
return 0;