diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2024-03-26 13:04:20 +0000 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2024-04-19 16:14:47 +0100 |
commit | 483546ce4f36a93d3fb711f2261b15b75871f3f9 (patch) | |
tree | 7022abc55beff5b441d1c959ae52cbc18bafaf82 /libctf/ctf-open.c | |
parent | cf9da3b0b6a6ae9d71ba36898a5e39710150f85e (diff) | |
download | binutils-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-open.c')
-rw-r--r-- | libctf/ctf-open.c | 39 |
1 files changed, 5 insertions, 34 deletions
diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c index 6d7a276..9cbf076 100644 --- a/libctf/ctf-open.c +++ b/libctf/ctf-open.c @@ -1233,9 +1233,8 @@ flip_types (ctf_dict_t *fp, void *start, size_t len, int to_foreign) return 0; } -/* Flip the endianness of BUF, given the offsets in the (already endian- - converted) CTH. If TO_FOREIGN is set, flip to foreign-endianness; if not, - flip away. +/* Flip the endianness of BUF, given the offsets in the (native-endianness) CTH. + If TO_FOREIGN is set, flip to foreign-endianness; if not, flip away. All of this stuff happens before the header is fully initialized, so the LCTF_*() macros cannot be used yet. Since we do not try to endian-convert v1 @@ -1288,21 +1287,6 @@ ctf_dict_t *ctf_simple_open (const char *ctfsect, size_t ctfsect_size, const char *strsect, size_t strsect_size, int *errp) { - return ctf_simple_open_internal (ctfsect, ctfsect_size, symsect, symsect_size, - symsect_entsize, strsect, strsect_size, NULL, - NULL, errp); -} - -/* Open a CTF file, mocking up a suitable ctf_sect and overriding the external - strtab with a synthetic one. */ - -ctf_dict_t *ctf_simple_open_internal (const char *ctfsect, size_t ctfsect_size, - const char *symsect, size_t symsect_size, - size_t symsect_entsize, - const char *strsect, size_t strsect_size, - ctf_dynhash_t *syn_strtab, - ctf_dynhash_t *atoms, int *errp) -{ ctf_sect_t skeleton; ctf_sect_t ctf_sect, sym_sect, str_sect; @@ -1338,8 +1322,7 @@ ctf_dict_t *ctf_simple_open_internal (const char *ctfsect, size_t ctfsect_size, strsectp = &str_sect; } - return ctf_bufopen_internal (ctfsectp, symsectp, strsectp, syn_strtab, - atoms, errp); + return ctf_bufopen (ctfsectp, symsectp, strsectp, errp); } /* Decode the specified CTF buffer and optional symbol table, and create a new @@ -1351,16 +1334,6 @@ ctf_dict_t * ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, const ctf_sect_t *strsect, int *errp) { - return ctf_bufopen_internal (ctfsect, symsect, strsect, NULL, NULL, errp); -} - -/* Like ctf_bufopen, but overriding the external strtab with a synthetic one. */ - -ctf_dict_t * -ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, - const ctf_sect_t *strsect, ctf_dynhash_t *syn_strtab, - ctf_dynhash_t *atoms, int *errp) -{ const ctf_preamble_t *pp; size_t hdrsz = sizeof (ctf_header_t); ctf_header_t *hp; @@ -1370,8 +1343,7 @@ ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, libctf_init_debug(); - if ((ctfsect == NULL) || ((symsect != NULL) && - ((strsect == NULL) && syn_strtab == NULL))) + if ((ctfsect == NULL) || ((symsect != NULL) && (strsect == NULL))) return (ctf_set_open_errno (errp, EINVAL)); if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) && @@ -1623,7 +1595,7 @@ ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *) fp->ctf_buf + hp->cth_stroff; fp->ctf_str[CTF_STRTAB_0].cts_len = hp->cth_strlen; - if (ctf_str_create_atoms (fp, atoms) < 0) + if (ctf_str_create_atoms (fp) < 0) { err = ENOMEM; goto bad; @@ -1669,7 +1641,6 @@ ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect, fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data; fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size; } - fp->ctf_syn_ext_strtab = syn_strtab; /* Dynamic state, for dynamic addition to this dict after loading. */ |