aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-string.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2019-08-07 17:55:09 +0100
committerNick Alcock <nick.alcock@oracle.com>2019-10-03 17:04:56 +0100
commit676c3ecbad6e9c41b906b0f882ef2ce23f49976a (patch)
tree3fac550f8df05fe8726316fa17fd5d99ad59f4e8 /libctf/ctf-string.c
parent791915db4273edbfb1952897cbf4c9745101c0e4 (diff)
downloadbinutils-676c3ecbad6e9c41b906b0f882ef2ce23f49976a.zip
binutils-676c3ecbad6e9c41b906b0f882ef2ce23f49976a.tar.gz
binutils-676c3ecbad6e9c41b906b0f882ef2ce23f49976a.tar.bz2
libctf: avoid the need to ever use ctf_update
The method of operation of libctf when the dictionary is writable has before now been that types that are added land in the dynamic type section, which is a linked list and hash of IDs -> dynamic type definitions (and, recently a hash of names): the DTDs are a bit of CTF representing the ctf_type_t and ad hoc C structures representing the vlen. Historically, libctf was unable to do anything with these types, not even look them up by ID, let alone by name: if you wanted to do that say if you were adding a type that depended on one you just added) you called ctf_update, which serializes all the DTDs into a CTF file and reopens it, copying its guts over the fp it's called with. The ctf_updated types are then frozen in amber and unchangeable: all lookups will return the types in the static portion in preference to the dynamic portion, and we will refuse to re-add things that already exist in the static portion (and, of late, in the dynamic portion too). The libctf machinery remembers the boundary between static and dynamic types and looks in the right portion for each type. Lots of things still don't quite work with dynamic types (e.g. getting their size), but enough works to do a bunch of additions and then a ctf_update, most of the time. Except it doesn't, because ctf_add_type finds it necessary to walk the full dynamic type definition list looking for types with matching names, so it gets slower and slower with every type you add: fixing this requires calling ctf_update periodically for no other reason than to avoid massively slowing things down. This is all clunky and very slow but kind of works, until you consider that it is in fact possible and indeed necessary to modify one sort of type after it has been added: forwards. These are necessarily promoted to structs, unions or enums, and when they do so *their type ID does not change*. So all of a sudden we are changing types that already exist in the static portion. ctf_update gets massively confused by this and allocates space enough for the forward (with no members), but then emits the new dynamic type (with all the members) into it. You get an assertion failure after that, if you're lucky, or a coredump. So this commit rejigs things a bit and arranges to exclusively use the dynamic type definitions in writable dictionaries, and the static type definitions in readable dictionaries: we don't at any time have a mixture of static and dynamic types, and you don't need to call ctf_update to make things "appear". The ctf_dtbyname hash I introduced a few months ago, which maps things like "struct foo" to DTDs, is removed, replaced instead by a change of type of the four dictionaries which track names. Rather than just being (unresizable) ctf_hash_t's populated only at ctf_bufopen time, they are now a ctf_names_t structure, which is a pair of ctf_hash_t and ctf_dynhash_t, with the ctf_hash_t portion being used in readonly dictionaries, and the ctf_dynhash_t being used in writable ones. The decision as to which to use is centralized in the new functions ctf_lookup_by_rawname (which takes a type kind) and ctf_lookup_by_rawhash, which it calls (which takes a ctf_names_t *.) This change lets us switch from using static to dynamic name hashes on the fly across the entirety of libctf without complexifying anything: in fact, because we now centralize the knowledge about how to map from type kind to name hash, it actually simplifies things and lets us throw out quite a lot of now-unnecessary complexity, from ctf_dtnyname (replaced by the dynamic half of the name tables), through to ctf_dtnextid (now that a dictionary's static portion is never referenced if the dictionary is writable, we can just use ctf_typemax to indicate the maximum type: dynamic or non-dynamic does not matter, and we no longer need to track the boundary between the types). You can now ctf_rollback() as far as you like, even past a ctf_update or for that matter a full writeout; all the iteration functions work just as well on writable as on read-only dictionaries; ctf_add_type no longer needs expensive duplicated code to run over the dynamic types hunting for ones it might be interested in; and the linker no longer needs a hack to call ctf_update so that calling ctf_add_type is not impossibly expensive. There is still a bit more complexity: some new code paths in ctf-types.c need to know how to extract information from dynamic types. This complexity will go away again in a few months when libctf acquires a proper intermediate representation. You can still call ctf_update if you like (it's public API, after all), but its only effect now is to set the point to which ctf_discard rolls back. Obviously *something* still needs to serialize the CTF file before writeout, and this job is done by ctf_serialize, which does everything ctf_update used to except set the counter used by ctf_discard. It is automatically called by the various functions that do CTF writeout: nobody else ever needs to call it. With this in place, forwards that are promoted to non-forwards no longer crash the link, even if it happens tens of thousands of types later. v5: fix tabdamage. libctf/ * ctf-impl.h (ctf_names_t): New. (ctf_lookup_t) <ctf_hash>: Now a ctf_names_t, not a ctf_hash_t. (ctf_file_t) <ctf_structs>: Likewise. <ctf_unions>: Likewise. <ctf_enums>: Likewise. <ctf_names>: Likewise. <ctf_lookups>: Improve comment. <ctf_ptrtab_len>: New. <ctf_prov_strtab>: New. <ctf_str_prov_offset>: New. <ctf_dtbyname>: Remove, redundant to the names hashes. <ctf_dtnextid>: Remove, redundant to ctf_typemax. (ctf_dtdef_t) <dtd_name>: Remove. <dtd_data>: Note that the ctt_name is now populated. (ctf_str_atom_t) <csa_offset>: This is now the strtab offset for internal strings too. <csa_external_offset>: New, the external strtab offset. (CTF_INDEX_TO_TYPEPTR): Handle the LCTF_RDWR case. (ctf_name_table): New declaration. (ctf_lookup_by_rawname): Likewise. (ctf_lookup_by_rawhash): Likewise. (ctf_set_ctl_hashes): Likewise. (ctf_serialize): Likewise. (ctf_dtd_insert): Adjust. (ctf_simple_open_internal): Likewise. (ctf_bufopen_internal): Likewise. (ctf_list_empty_p): Likewise. (ctf_str_remove_ref): Likewise. (ctf_str_add): Returns uint32_t now. (ctf_str_add_ref): Likewise. (ctf_str_add_external): Now returns a boolean (int). * ctf-string.c (ctf_strraw_explicit): Check the ctf_prov_strtab for strings in the appropriate range. (ctf_str_create_atoms): Create the ctf_prov_strtab. Detect OOM when adding the null string to the new strtab. (ctf_str_free_atoms): Destroy the ctf_prov_strtab. (ctf_str_add_ref_internal): Add make_provisional argument. If make_provisional, populate the offset and fill in the ctf_prov_strtab accordingly. (ctf_str_add): Return the offset, not the string. (ctf_str_add_ref): Likewise. (ctf_str_add_external): Return a success integer. (ctf_str_remove_ref): New, remove a single ref. (ctf_str_count_strtab): Do not count the initial null string's length or the existence or length of any unreferenced internal atoms. (ctf_str_populate_sorttab): Skip atoms with no refs. (ctf_str_write_strtab): Populate the nullstr earlier. Add one to the cts_len for the null string, since it is no longer done in ctf_str_count_strtab. Adjust for csa_external_offset rename. Populate the csa_offset for both internal and external cases. Flush the ctf_prov_strtab afterwards, and reset the ctf_str_prov_offset. * ctf-create.c (ctf_grow_ptrtab): New. (ctf_create): Call it. Initialize new fields rather than old ones. Tell ctf_bufopen_internal that this is a writable dictionary. Set the ctl hashes and data model. (ctf_update): Rename to... (ctf_serialize): ... this. Leave a compatibility function behind. Tell ctf_simple_open_internal that this is a writable dictionary. Pass the new fields along from the old dictionary. Drop ctf_dtnextid and ctf_dtbyname. Use ctf_strraw, not dtd_name. Do not zero out the DTD's ctt_name. (ctf_prefixed_name): Rename to... (ctf_name_table): ... this. No longer return a prefixed name: return the applicable name table instead. (ctf_dtd_insert): Use it, and use the right name table. Pass in the kind we're adding. Migrate away from dtd_name. (ctf_dtd_delete): Adjust similarly. Remove the ref to the deleted ctt_name. (ctf_dtd_lookup_type_by_name): Remove. (ctf_dynamic_type): Always return NULL on read-only dictionaries. No longer check ctf_dtnextid: check ctf_typemax instead. (ctf_snapshot): No longer use ctf_dtnextid: use ctf_typemax instead. (ctf_rollback): Likewise. No longer fail with ECTF_OVERROLLBACK. Use ctf_name_table and the right name table, and migrate away from dtd_name as in ctf_dtd_delete. (ctf_add_generic): Pass in the kind explicitly and pass it to ctf_dtd_insert. Use ctf_typemax, not ctf_dtnextid. Migrate away from dtd_name to using ctf_str_add_ref to populate the ctt_name. Grow the ptrtab if needed. (ctf_add_encoded): Pass in the kind. (ctf_add_slice): Likewise. (ctf_add_array): Likewise. (ctf_add_function): Likewise. (ctf_add_typedef): Likewise. (ctf_add_reftype): Likewise. Initialize the ctf_ptrtab, checking ctt_name rather than dtd_name. (ctf_add_struct_sized): Pass in the kind. Use ctf_lookup_by_rawname, not ctf_hash_lookup_type / ctf_dtd_lookup_type_by_name. (ctf_add_union_sized): Likewise. (ctf_add_enum): Likewise. (ctf_add_enum_encoded): Likewise. (ctf_add_forward): Likewise. (ctf_add_type): Likewise. (ctf_compress_write): Call ctf_serialize: adjust for ctf_size not being initialized until after the call. (ctf_write_mem): Likewise. (ctf_write): Likewise. * ctf-archive.c (arc_write_one_ctf): Likewise. * ctf-lookup.c (ctf_lookup_by_name): Use ctf_lookuup_by_rawhash, not ctf_hash_lookup_type. (ctf_lookup_by_id): No longer check the readonly types if the dictionary is writable. * ctf-open.c (init_types): Assert that this dictionary is not writable. Adjust to use the new name hashes, ctf_name_table, and ctf_ptrtab_len. GNU style fix for the final ptrtab scan. (ctf_bufopen_internal): New 'writable' parameter. Flip on LCTF_RDWR if set. Drop out early when dictionary is writable. Split the ctf_lookups initialization into... (ctf_set_cth_hashes): ... this new function. (ctf_simple_open_internal): Adjust. New 'writable' parameter. (ctf_simple_open): Adjust accordingly. (ctf_bufopen): Likewise. (ctf_file_close): Destroy the appropriate name hashes. No longer destroy ctf_dtbyname, which is gone. (ctf_getdatasect): Remove spurious "extern". * ctf-types.c (ctf_lookup_by_rawname): New, look up types in the specified name table, given a kind. (ctf_lookup_by_rawhash): Likewise, given a ctf_names_t *. (ctf_member_iter): Add support for iterating over the dynamic type list. (ctf_enum_iter): Likewise. (ctf_variable_iter): Likewise. (ctf_type_rvisit): Likewise. (ctf_member_info): Add support for types in the dynamic type list. (ctf_enum_name): Likewise. (ctf_enum_value): Likewise. (ctf_func_type_info): Likewise. (ctf_func_type_args): Likewise. * ctf-link.c (ctf_accumulate_archive_names): No longer call ctf_update. (ctf_link_write): Likewise. (ctf_link_intern_extern_string): Adjust for new ctf_str_add_external return value. (ctf_link_add_strtab): Likewise. * ctf-util.c (ctf_list_empty_p): New.
Diffstat (limited to 'libctf/ctf-string.c')
-rw-r--r--libctf/ctf-string.c163
1 files changed, 130 insertions, 33 deletions
diff --git a/libctf/ctf-string.c b/libctf/ctf-string.c
index 44cd447..243e1ac 100644
--- a/libctf/ctf-string.c
+++ b/libctf/ctf-string.c
@@ -38,6 +38,16 @@ ctf_strraw_explicit (ctf_file_t *fp, uint32_t name, ctf_strs_t *strtab)
return ctf_dynhash_lookup (fp->ctf_syn_ext_strtab,
(void *) (uintptr_t) name);
+ /* If the name is in the internal strtab, and the offset is beyond the end of
+ the ctsp->cts_len but below the ctf_str_prov_offset, this is a provisional
+ string added by ctf_str_add*() but not yet built into a real strtab: get
+ the value out of the ctf_prov_strtab. */
+
+ if (CTF_NAME_STID (name) == CTF_STRTAB_0
+ && name >= ctsp->cts_len && name < fp->ctf_str_prov_offset)
+ return ctf_dynhash_lookup (fp->ctf_prov_strtab,
+ (void *) (uintptr_t) name);
+
if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET (name) < ctsp->cts_len)
return (ctsp->cts_strs + CTF_NAME_OFFSET (name));
@@ -96,24 +106,45 @@ ctf_str_create_atoms (ctf_file_t *fp)
if (fp->ctf_str_atoms == NULL)
return -ENOMEM;
+ if (!fp->ctf_prov_strtab)
+ fp->ctf_prov_strtab = ctf_dynhash_create (ctf_hash_integer,
+ ctf_hash_eq_integer,
+ NULL, NULL);
+ if (!fp->ctf_prov_strtab)
+ goto oom_prov_strtab;
+
+ errno = 0;
ctf_str_add (fp, "");
+ if (errno == ENOMEM)
+ goto oom_str_add;
+
return 0;
+
+ oom_str_add:
+ ctf_dynhash_destroy (fp->ctf_prov_strtab);
+ fp->ctf_prov_strtab = NULL;
+ oom_prov_strtab:
+ ctf_dynhash_destroy (fp->ctf_str_atoms);
+ fp->ctf_str_atoms = NULL;
+ return -ENOMEM;
}
/* Destroy the atoms table. */
void
ctf_str_free_atoms (ctf_file_t *fp)
{
+ ctf_dynhash_destroy (fp->ctf_prov_strtab);
ctf_dynhash_destroy (fp->ctf_str_atoms);
}
/* Add a string to the atoms table, copying the passed-in string. Return the
atom added. Return NULL only when out of memory (and do not touch the
passed-in string in that case). Possibly augment the ref list with the
- passed-in ref. */
+ passed-in ref. Possibly add a provisional entry for this string to the
+ provisional strtab. */
static ctf_str_atom_t *
ctf_str_add_ref_internal (ctf_file_t *fp, const char *str,
- int add_ref, uint32_t *ref)
+ int add_ref, int make_provisional, uint32_t *ref)
{
char *newstr = NULL;
ctf_str_atom_t *atom = NULL;
@@ -150,6 +181,18 @@ ctf_str_add_ref_internal (ctf_file_t *fp, const char *str,
atom->csa_str = newstr;
atom->csa_snapshot_id = fp->ctf_snapshots;
+
+ if (make_provisional)
+ {
+ atom->csa_offset = fp->ctf_str_prov_offset;
+
+ if (ctf_dynhash_insert (fp->ctf_prov_strtab, (void *) (uintptr_t)
+ atom->csa_offset, (void *) atom->csa_str) < 0)
+ goto oom;
+
+ fp->ctf_str_prov_offset += strlen (atom->csa_str) + 1;
+ }
+
if (add_ref)
{
ctf_list_append (&atom->csa_refs, aref);
@@ -158,59 +201,87 @@ ctf_str_add_ref_internal (ctf_file_t *fp, const char *str,
return atom;
oom:
+ if (newstr)
+ ctf_dynhash_remove (fp->ctf_str_atoms, newstr);
ctf_free (atom);
ctf_free (aref);
ctf_free (newstr);
return NULL;
}
-/* Add a string to the atoms table and return it, without augmenting the ref
- list for this string. */
-const char *
+/* Add a string to the atoms table, without augmenting the ref list for this
+ string: return a 'provisional offset' which can be used to return this string
+ until ctf_str_write_strtab is called, or 0 on failure. (Everywhere the
+ provisional offset is assigned to should be added as a ref using
+ ctf_str_add_ref() as well.) */
+uint32_t
ctf_str_add (ctf_file_t *fp, const char *str)
{
ctf_str_atom_t *atom;
if (!str)
- return NULL;
+ return 0;
- atom = ctf_str_add_ref_internal (fp, str, FALSE, 0);
+ atom = ctf_str_add_ref_internal (fp, str, FALSE, TRUE, 0);
if (!atom)
- return NULL;
+ return 0;
- return atom->csa_str;
+ return atom->csa_offset;
}
/* Like ctf_str_add(), but additionally augment the atom's refs list with the
passed-in ref, whether or not the string is already present. There is no
attempt to deduplicate the refs list (but duplicates are harmless). */
-const char *
+uint32_t
ctf_str_add_ref (ctf_file_t *fp, const char *str, uint32_t *ref)
{
ctf_str_atom_t *atom;
if (!str)
- return NULL;
+ return 0;
- atom = ctf_str_add_ref_internal (fp, str, TRUE, ref);
+ atom = ctf_str_add_ref_internal (fp, str, TRUE, TRUE, ref);
if (!atom)
- return NULL;
+ return 0;
- return atom->csa_str;
+ return atom->csa_offset;
}
-/* Add an external strtab reference at OFFSET. */
-const char *
+/* Add an external strtab reference at OFFSET. Returns zero if the addition
+ failed, nonzero otherwise. */
+int
ctf_str_add_external (ctf_file_t *fp, const char *str, uint32_t offset)
{
ctf_str_atom_t *atom;
if (!str)
- return NULL;
+ return 0;
+
+ atom = ctf_str_add_ref_internal (fp, str, FALSE, FALSE, 0);
+ if (!atom)
+ return 0;
+
+ atom->csa_external_offset = CTF_SET_STID (offset, CTF_STRTAB_1);
+ return 1;
+}
- atom = ctf_str_add_ref_internal (fp, str, FALSE, 0);
+/* Remove a single ref. */
+void
+ctf_str_remove_ref (ctf_file_t *fp, const char *str, uint32_t *ref)
+{
+ ctf_str_atom_ref_t *aref, *anext;
+ ctf_str_atom_t *atom = NULL;
+
+ atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
if (!atom)
- return NULL;
+ return;
- atom->csa_offset = CTF_SET_STID (offset, CTF_STRTAB_1);
- return atom->csa_str;
+ for (aref = ctf_list_next (&atom->csa_refs); aref != NULL; aref = anext)
+ {
+ anext = ctf_list_next (aref);
+ if (aref->caf_ref == ref)
+ {
+ ctf_list_delete (&atom->csa_refs, aref);
+ ctf_free (aref);
+ }
+ }
}
/* A ctf_dynhash_iter_remove() callback that removes atoms later than a given
@@ -285,12 +356,25 @@ ctf_str_count_strtab (void *key _libctf_unused_, void *value,
ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
ctf_strtab_write_state_t *s = (ctf_strtab_write_state_t *) arg;
- /* We only factor in the length of items that have no offset:
- other items are in the external strtab. They still contribute to the
- total count, though, because we still have to sort them. */
- if (!atom->csa_offset)
- s->strtab->cts_len += strlen (atom->csa_str) + 1;
- s->strtab_count++;
+ /* We only factor in the length of items that have no offset and have refs:
+ other items are in the external strtab, or will simply not be written out
+ at all. They still contribute to the total count, though, because we still
+ have to sort them. We add in the null string's length explicitly, outside
+ this function, since it is explicitly written out even if it has no refs at
+ all. */
+
+ if (s->nullstr == atom)
+ {
+ s->strtab_count++;
+ return;
+ }
+
+ if (!ctf_list_empty_p (&atom->csa_refs))
+ {
+ if (!atom->csa_external_offset)
+ s->strtab->cts_len += strlen (atom->csa_str) + 1;
+ s->strtab_count++;
+ }
}
/* Populate the sorttab with pointers to the strtab atoms. */
@@ -305,7 +389,9 @@ ctf_str_populate_sorttab (void *key _libctf_unused_, void *value,
if (s->nullstr == atom)
return;
- s->sorttab[s->i++] = atom;
+ /* Skip atoms with no refs. */
+ if (!ctf_list_empty_p (&atom->csa_refs))
+ s->sorttab[s->i++] = atom;
}
/* Sort the strtab. */
@@ -346,7 +432,9 @@ ctf_str_write_strtab (ctf_file_t *fp)
return strtab;
}
+ s.nullstr = nullstr;
ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_count_strtab, &s);
+ strtab.cts_len++; /* For the null string. */
ctf_dprintf ("%lu bytes of strings in strtab.\n",
(unsigned long) strtab.cts_len);
@@ -359,7 +447,6 @@ ctf_str_write_strtab (ctf_file_t *fp)
sorttab[0] = nullstr;
s.i = 1;
s.sorttab = sorttab;
- s.nullstr = nullstr;
ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_populate_sorttab, &s);
qsort (&sorttab[1], s.strtab_count - 1, sizeof (ctf_str_atom_t *),
@@ -378,7 +465,7 @@ ctf_str_write_strtab (ctf_file_t *fp)
/* Update all refs: also update the strtab appropriately. */
for (i = 0; i < s.strtab_count; i++)
{
- if (sorttab[i]->csa_offset)
+ if (sorttab[i]->csa_external_offset)
{
/* External strtab entry: populate the synthetic external strtab.
@@ -388,17 +475,21 @@ ctf_str_write_strtab (ctf_file_t *fp)
until ctf_file_close. */
any_external = 1;
- ctf_str_update_refs (sorttab[i], sorttab[i]->csa_offset);
+ ctf_str_update_refs (sorttab[i], sorttab[i]->csa_external_offset);
if (ctf_dynhash_insert (fp->ctf_syn_ext_strtab,
- (void *) (uintptr_t) sorttab[i]->csa_offset,
+ (void *) (uintptr_t)
+ sorttab[i]->csa_external_offset,
(void *) sorttab[i]->csa_str) < 0)
goto oom_strtab;
+ sorttab[i]->csa_offset = sorttab[i]->csa_external_offset;
}
else
{
- /* Internal strtab entry: actually add to the string table. */
+ /* Internal strtab entry with refs: actually add to the string
+ table. */
ctf_str_update_refs (sorttab[i], cur_stroff);
+ sorttab[i]->csa_offset = cur_stroff;
strcpy (&strtab.cts_strs[cur_stroff], sorttab[i]->csa_str);
cur_stroff += strlen (sorttab[i]->csa_str) + 1;
}
@@ -411,6 +502,12 @@ ctf_str_write_strtab (ctf_file_t *fp)
fp->ctf_syn_ext_strtab = NULL;
}
+ /* All the provisional strtab entries are now real strtab entries, and
+ ctf_strptr() will find them there. The provisional offset now starts right
+ beyond the new end of the strtab. */
+
+ ctf_dynhash_empty (fp->ctf_prov_strtab);
+ fp->ctf_str_prov_offset = strtab.cts_len + 1;
return strtab;
oom_strtab: