forked from Imagelibrary/binutils-gdb
libctf: string: refs rework
This commit moves provisional (not-yet-serialized) string refs towards the
scheme to be used for CTF IDs in the future. In particular
- provisional string offsets now count downwards from just under the
external string offset space (all bits on but the high bit). This makes
it possible to detect an overflowing strtab, and also makes it trivial to
determine whether any string offset (ref) updates were missed -- where
before we might get a slightly corrupted or incorrect string, we now get
a huge high strtab offset corresponding to no string, and an error is
emitted at read time.
- refs are emitted at serialization time during the pass through the types.
They are strictly associated with the newly-written-out buffer: the
existing opened CTF dict is not changed, though it does still get the new
strtab so that new refs to the same string can just refer directly to it.
The provisional strtab hash table that contains these strings is not
deleted after serialization (because we might serialize again): instead,
we keep track in the parent of the lowest-yet-used ("latest") provisional
strtab offset, and any strtab offset above that, but not external
(high-bit-on) is considered provisional.
This is sort-of-enforced by moving most of the ref-addition function
declarations (including ctf_str_add_ref) to a new ctf-ref.h, which is
not included by ctf-create.c or ctf-open.c.
- because we don't add refs when adding types, we don't need to handle the
case where we add things to expanding vlens (enums, struct members) and
have to realloc() them. So the entire painful movable refs system can
just be deleted, along with the ability to remove refs piecemeal at all
(purging all of them is still possible). Strings added during type
addition are added via ctf_str_add(), which adds no refs: the strings are
picked up at serialization time and refs to their final, serialized
resting place added. The DTDs never have any refs in them, and their
provisional strtab offsets are never updated by the ref system.
This caused several bugs to fall out of the earlier work and get fixed.
In particular, attempts to look up a string in a child dict now search
the parent's provisional strtab too: we add some extra special casing
for the null string so we don't need to worry about deduplication
moving it somewhere other than offset zero.
Finally, the optimization that removes an unreferenced synthetic external
strtab (the record of the strings the linker has told us about, kept around
internally for lookup during late serialization) is faulty: references to a
strtab entry will only produce CTF-level refs if their value might change,
and an external string's offset won't change, so it produces no refs: worse
yet, even if we did get a ref (say, if the string was originally believed
to be internal and only later were we told that the linker knew about it
too), when we serialize a strtab, all its refs are dropped (since they've
been updated and can no longer change); so if we serialized it a second
time, its synthetic external strtab would be considered empty and dropped,
even though the same external strings as before still exist, referencing
it. We must keep the synthetic external strtab around as long as external
strings exist that reference it, i.e. for the life of the dict.
One benefit of all this: now we're emitting provisional string offsets at
a really high value, it's out of the way of the consecutive, deduplicated
string offsets in child dicts. So we can drop the constraint that you
cannot add strings to a dict with children, which allows us to add types
freely to parent dicts again. What you can't do is write that dict out
again: when we serialize, we currently update the dict being serialized
with the updated strtabs: when you write a dict out, its provisional
strings become real strings, and suddenly the offsets would overlap once
more. But opening a dict and its children, adding to it, and then
writing it out again is rare indeed, and we have a workaround: anyone
wanting to do this can just use ctf_link instead.
This commit is contained in:
@@ -221,7 +221,6 @@ void
|
||||
ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd)
|
||||
{
|
||||
int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
|
||||
size_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
|
||||
int name_kind = kind;
|
||||
const char *name;
|
||||
|
||||
@@ -229,27 +228,6 @@ ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd)
|
||||
|
||||
switch (kind)
|
||||
{
|
||||
case CTF_K_STRUCT:
|
||||
case CTF_K_UNION:
|
||||
{
|
||||
ctf_lmember_t *memb = (ctf_lmember_t *) dtd->dtd_vlen;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < vlen; i++)
|
||||
ctf_str_remove_ref (fp, ctf_strraw (fp, memb[i].ctlm_name),
|
||||
&memb[i].ctlm_name);
|
||||
}
|
||||
break;
|
||||
case CTF_K_ENUM:
|
||||
{
|
||||
ctf_enum_t *en = (ctf_enum_t *) dtd->dtd_vlen;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < vlen; i++)
|
||||
ctf_str_remove_ref (fp, ctf_strraw (fp, en[i].cte_name),
|
||||
&en[i].cte_name);
|
||||
}
|
||||
break;
|
||||
case CTF_K_FORWARD:
|
||||
name_kind = dtd->dtd_data.ctt_type;
|
||||
break;
|
||||
@@ -262,7 +240,6 @@ ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd)
|
||||
{
|
||||
if (LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
|
||||
ctf_dynhash_remove (ctf_name_table (fp, name_kind), name);
|
||||
ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
|
||||
}
|
||||
|
||||
ctf_list_delete (&fp->ctf_dtdefs, dtd);
|
||||
@@ -388,10 +365,7 @@ ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id)
|
||||
if (dtd->dtd_data.ctt_name
|
||||
&& (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
|
||||
&& LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
|
||||
{
|
||||
ctf_dynhash_remove (ctf_name_table (fp, kind), name);
|
||||
ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
|
||||
}
|
||||
|
||||
ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
|
||||
ctf_dtd_delete (fp, dtd);
|
||||
@@ -467,7 +441,7 @@ ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind,
|
||||
type = ++fp->ctf_typemax;
|
||||
type = ctf_index_to_type (fp, type);
|
||||
|
||||
dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
|
||||
dtd->dtd_data.ctt_name = ctf_str_add (fp, name);
|
||||
dtd->dtd_type = type;
|
||||
|
||||
if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
|
||||
@@ -1063,7 +1037,6 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
|
||||
{
|
||||
ctf_dict_t *ofp = fp;
|
||||
ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
|
||||
unsigned char *old_vlen;
|
||||
ctf_enum_t *en;
|
||||
|
||||
uint32_t kind, vlen, root;
|
||||
@@ -1104,17 +1077,11 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
|
||||
if (vlen == CTF_MAX_VLEN)
|
||||
return (ctf_set_errno (ofp, ECTF_DTFULL));
|
||||
|
||||
old_vlen = dtd->dtd_vlen;
|
||||
|
||||
if (ctf_grow_vlen (fp, dtd, sizeof (ctf_enum_t) * (vlen + 1)) < 0)
|
||||
return -1; /* errno is set for us. */
|
||||
|
||||
en = (ctf_enum_t *) dtd->dtd_vlen;
|
||||
|
||||
/* Remove refs in the old vlen region and reapply them. */
|
||||
|
||||
ctf_move_refs (fp, old_vlen, sizeof (ctf_enum_t) * vlen, dtd->dtd_vlen);
|
||||
|
||||
/* Check for constant duplication within any given enum: only needed for
|
||||
non-root-visible types, since the duplicate detection above does the job
|
||||
for root-visible types just fine. */
|
||||
@@ -1128,7 +1095,7 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
|
||||
return (ctf_set_errno (ofp, ECTF_DUPLICATE));
|
||||
}
|
||||
|
||||
en[vlen].cte_name = ctf_str_add_movable_ref (fp, name, &en[vlen].cte_name);
|
||||
en[vlen].cte_name = ctf_str_add (fp, name);
|
||||
en[vlen].cte_value = value;
|
||||
|
||||
if (en[vlen].cte_name == 0 && name != NULL && name[0] != '\0')
|
||||
@@ -1161,7 +1128,6 @@ ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name,
|
||||
uint32_t kind, vlen, root;
|
||||
size_t i;
|
||||
int is_incomplete = 0;
|
||||
unsigned char *old_vlen;
|
||||
ctf_lmember_t *memb;
|
||||
|
||||
if (fp->ctf_flags & LCTF_NO_STR)
|
||||
@@ -1197,15 +1163,10 @@ ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name,
|
||||
if (vlen == CTF_MAX_VLEN)
|
||||
return (ctf_set_errno (ofp, ECTF_DTFULL));
|
||||
|
||||
old_vlen = dtd->dtd_vlen;
|
||||
if (ctf_grow_vlen (fp, dtd, sizeof (ctf_lmember_t) * (vlen + 1)) < 0)
|
||||
return (ctf_set_errno (ofp, ctf_errno (fp)));
|
||||
memb = (ctf_lmember_t *) dtd->dtd_vlen;
|
||||
|
||||
/* Remove refs in the old vlen region and reapply them. */
|
||||
|
||||
ctf_move_refs (fp, old_vlen, sizeof (ctf_lmember_t) * vlen, dtd->dtd_vlen);
|
||||
|
||||
if (name != NULL)
|
||||
{
|
||||
for (i = 0; i < vlen; i++)
|
||||
@@ -1235,7 +1196,7 @@ ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name,
|
||||
return -1; /* errno is set for us. */
|
||||
}
|
||||
|
||||
memb[vlen].ctlm_name = ctf_str_add_movable_ref (fp, name, &memb[vlen].ctlm_name);
|
||||
memb[vlen].ctlm_name = ctf_str_add (fp, name);
|
||||
memb[vlen].ctlm_type = type;
|
||||
if (memb[vlen].ctlm_name == 0 && name != NULL && name[0] != '\0')
|
||||
return -1; /* errno is set for us. */
|
||||
|
||||
@@ -228,14 +228,6 @@ typedef struct ctf_str_atom
|
||||
int csa_flags; /* CTF_STR_ATOM_* flags. */
|
||||
} ctf_str_atom_t;
|
||||
|
||||
/* A single ref. */
|
||||
|
||||
typedef struct ctf_ref
|
||||
{
|
||||
ctf_list_t cre_list; /* List forward/back pointers. */
|
||||
uint32_t *cre_ref; /* A single ref to this string. */
|
||||
} ctf_ref_t;
|
||||
|
||||
/* A single linker-provided symbol, during symbol addition, possibly before we
|
||||
have been given external strtab refs. */
|
||||
typedef struct ctf_in_flight_dynsym
|
||||
@@ -392,8 +384,9 @@ struct ctf_dict
|
||||
ctf_strs_t ctf_str[2]; /* Array of string table base and bounds. */
|
||||
ctf_strs_writable_t *ctf_dynstrtab; /* Dynamically allocated string table, if any. */
|
||||
ctf_dynhash_t *ctf_str_atoms; /* Hash table of ctf_str_atoms_t. */
|
||||
ctf_dynhash_t *ctf_movable_refs; /* Hash table of void * -> ctf_ref_t. */
|
||||
uint32_t ctf_str_prov_offset; /* Latest provisional offset assigned so far. */
|
||||
uint32_t ctf_str_prov_offset; /* Latest provisional offset assigned so far.
|
||||
Kept in the parent. Counts down. */
|
||||
size_t ctf_str_prov_len; /* Length of all unwritten provisional strings. */
|
||||
unsigned char *ctf_base; /* CTF file pointer. */
|
||||
unsigned char *ctf_dynbase; /* Freeable CTF file pointer. */
|
||||
unsigned char *ctf_buf; /* Uncompressed CTF data buffer. */
|
||||
@@ -756,26 +749,11 @@ extern int ctf_str_create_atoms (ctf_dict_t *);
|
||||
extern void ctf_str_free_atoms (ctf_dict_t *);
|
||||
extern uint32_t ctf_str_add (ctf_dict_t *, const char *);
|
||||
extern uint32_t ctf_str_add_copy (ctf_dict_t *, const char *);
|
||||
extern uint32_t ctf_str_add_ref (ctf_dict_t *, const char *, uint32_t *ref);
|
||||
extern uint32_t ctf_str_add_no_dedup_ref (ctf_dict_t *, const char *,
|
||||
uint32_t *ref);
|
||||
extern uint32_t ctf_str_add_movable_ref (ctf_dict_t *, const char *,
|
||||
uint32_t *ref);
|
||||
extern int ctf_str_add_external (ctf_dict_t *, const char *, uint32_t offset);
|
||||
extern void ctf_str_remove_ref (ctf_dict_t *, const char *, uint32_t *ref);
|
||||
extern void ctf_str_purge_refs (ctf_dict_t *fp);
|
||||
extern void ctf_str_rollback (ctf_dict_t *, ctf_snapshot_id_t);
|
||||
extern const ctf_strs_writable_t *ctf_str_write_strtab (ctf_dict_t *);
|
||||
|
||||
extern int ctf_init_refs (ctf_dict_t *);
|
||||
extern void ctf_free_refs (ctf_dict_t *);
|
||||
extern ctf_ref_t *ctf_create_ref (ctf_dict_t *, ctf_list_t *, uint32_t *ref,
|
||||
int movable);
|
||||
extern void ctf_remove_ref (ctf_dict_t *fp, ctf_list_t *, uint32_t *ref);
|
||||
extern int ctf_move_refs (ctf_dict_t *fp, void *src, size_t len, void *dest);
|
||||
extern void ctf_purge_ref_list (ctf_dict_t *, ctf_list_t *);
|
||||
extern void ctf_update_refs (ctf_list_t *, uint32_t value);
|
||||
|
||||
extern int ctf_preserialize (ctf_dict_t *fp);
|
||||
extern void ctf_depreserialize (ctf_dict_t *fp);
|
||||
|
||||
|
||||
@@ -2140,8 +2140,10 @@ ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
|
||||
ctf_dprintf ("Deduplicating strings.\n");
|
||||
|
||||
for (i = 0; i < arg.i; i++)
|
||||
all_strlens += arg.files[i]->ctf_str_prov_offset;
|
||||
old_parent_strlen = arg.files[0]->ctf_str_prov_offset;
|
||||
all_strlens += arg.files[i]->ctf_str[0].cts_len
|
||||
+ arg.files[i]->ctf_str_prov_len;
|
||||
old_parent_strlen = arg.files[0]->ctf_str[0].cts_len
|
||||
+ arg.files[0]->ctf_str_prov_len;
|
||||
|
||||
if (ctf_dedup_strings (fp) < 0)
|
||||
{
|
||||
@@ -2154,7 +2156,7 @@ ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
|
||||
ctf_dprintf ("Deduplicated strings: original parent strlen: %zu; "
|
||||
"original lengths: %zu; final length: %zu.\n",
|
||||
(size_t) old_parent_strlen, (size_t) all_strlens,
|
||||
(size_t) arg.files[0]->ctf_str_prov_offset);
|
||||
(size_t) arg.files[0]->ctf_str_prov_len);
|
||||
|
||||
if ((f = tmpfile ()) == NULL)
|
||||
{
|
||||
|
||||
@@ -1798,7 +1798,7 @@ ctf_bufopen (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_init_refs (fp) < 0 || ctf_str_create_atoms (fp) < 0)
|
||||
if (ctf_str_create_atoms (fp) < 0)
|
||||
{
|
||||
err = ENOMEM;
|
||||
goto bad;
|
||||
@@ -2021,9 +2021,8 @@ ctf_dict_close (ctf_dict_t *fp)
|
||||
the atoms (since in a link the outputs contain references to the parent's
|
||||
atoms), but we must destroy the inputs after that (since many type strings
|
||||
ultimately come from the inputs). In addition, if there are
|
||||
ctf_link_outputs, the parent dict's atoms table may have movable refs that
|
||||
refer to the outputs: so purge the refs first, including the movable
|
||||
ones. */
|
||||
ctf_link_outputs, the parent dict's atoms table may have refs that refer to
|
||||
the outputs: so purge the refs first. */
|
||||
|
||||
if (fp->ctf_link_outputs && ctf_dynhash_elements (fp->ctf_link_outputs) > 0)
|
||||
ctf_str_purge_refs (fp);
|
||||
@@ -2032,7 +2031,6 @@ ctf_dict_close (ctf_dict_t *fp)
|
||||
ctf_dynhash_destroy (fp->ctf_link_out_cu_mapping);
|
||||
|
||||
ctf_str_free_atoms (fp);
|
||||
ctf_free_refs (fp);
|
||||
free (fp->ctf_tmp_typeslice);
|
||||
|
||||
if (fp->ctf_data.cts_name != _CTF_NULLSTR)
|
||||
@@ -2048,7 +2046,6 @@ ctf_dict_close (ctf_dict_t *fp)
|
||||
|
||||
free (fp->ctf_dynbase);
|
||||
|
||||
ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
|
||||
ctf_dynhash_destroy (fp->ctf_link_inputs);
|
||||
ctf_dynhash_destroy (fp->ctf_link_type_mapping);
|
||||
ctf_dynhash_destroy (fp->ctf_link_in_cu_mapping);
|
||||
@@ -2223,6 +2220,17 @@ ctf_import_internal (ctf_dict_t *fp, ctf_dict_t *pfp, int unreffed)
|
||||
return (ctf_set_errno (fp, ECTF_WRONGPARENT));
|
||||
}
|
||||
|
||||
/* We might in time be able to lift this restriction, but it is unlikely to be
|
||||
something anyone would want to do, so let's not bother for now. */
|
||||
|
||||
if (ctf_dynhash_elements (fp->ctf_prov_strtab) != 0)
|
||||
{
|
||||
ctf_err_warn (fp, 0, EINVAL,
|
||||
_("ctf_import: child dict already has %zi bytes of strings, cannot import"),
|
||||
ctf_dynhash_elements (fp->ctf_prov_strtab));
|
||||
return (ctf_set_errno (fp, EINVAL));
|
||||
}
|
||||
|
||||
fp->ctf_parent = NULL;
|
||||
free (fp->ctf_pptrtab);
|
||||
fp->ctf_pptrtab = NULL;
|
||||
|
||||
43
libctf/ctf-ref.h
Normal file
43
libctf/ctf-ref.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/* CTF ref system.
|
||||
Copyright (C) 2019-2025 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of libctf.
|
||||
|
||||
libctf is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free
|
||||
Software Foundation; either version 3, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; see the file COPYING. If not see
|
||||
<http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef _CTF_REF_H
|
||||
#define _CTF_REF_H
|
||||
|
||||
#include "ctf-impl.h"
|
||||
|
||||
/* This is in a separate header because nothing but ctf-string.c and
|
||||
ctf-serialize.c should use functions herein (and ctf-util.c, which defines
|
||||
them). */
|
||||
|
||||
typedef struct ctf_ref
|
||||
{
|
||||
ctf_list_t cre_list; /* List forward/back pointers. */
|
||||
uint32_t *cre_ref; /* A single ref to this string. */
|
||||
} ctf_ref_t;
|
||||
|
||||
extern uint32_t ctf_str_add_ref (ctf_dict_t *, const char *, uint32_t *ref);
|
||||
extern uint32_t ctf_str_add_no_dedup_ref (ctf_dict_t *, const char *,
|
||||
uint32_t *ref);
|
||||
|
||||
extern ctf_ref_t *ctf_create_ref (ctf_dict_t *, ctf_list_t *, uint32_t *ref);
|
||||
extern void ctf_purge_ref_list (ctf_dict_t *, ctf_list_t *);
|
||||
extern void ctf_update_refs (ctf_list_t *, uint32_t value);
|
||||
|
||||
#endif /* _CTF_REF_H */
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <elf.h>
|
||||
#include "elf-bfd.h"
|
||||
|
||||
#include <ctf-ref.h>
|
||||
|
||||
/* Symtypetab sections. */
|
||||
|
||||
/* Symtypetab emission flags. */
|
||||
@@ -898,7 +900,6 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
|
||||
const char *name = ctf_strraw (fp, dtd_vlen[i].cte_name);
|
||||
|
||||
ctf_str_add_ref (fp, name, &t_vlen[i].cte_name);
|
||||
ctf_str_add_ref (fp, name, &dtd_vlen[i].cte_name);
|
||||
}
|
||||
t += sizeof (struct ctf_enum) * vlen;
|
||||
|
||||
@@ -963,8 +964,24 @@ ctf_preserialize (ctf_dict_t *fp)
|
||||
this applies only to CTFv1 dicts, which have a different parent/child type
|
||||
offset to v2 and higher, and nowhere to record this in CTFv4. */
|
||||
|
||||
if (fp->ctf_flags & LCTF_NO_SERIALIZE)
|
||||
return (ctf_set_errno (fp, ECTF_CTFVERS_NO_SERIALIZE));
|
||||
if (!fp->ctf_parent)
|
||||
{
|
||||
/* Prohibit serialization of a parent dict which has already been
|
||||
serialized, has children, and has had strings added since the last
|
||||
serialization: because we update strtabs in the dict itself, not just
|
||||
the serialized copy, this would cause overlapping strtabs.
|
||||
|
||||
TODO: lift this restriction. */
|
||||
|
||||
if (fp->ctf_str[CTF_STRTAB_0].cts_len != 0
|
||||
&& fp->ctf_max_children > 0
|
||||
&& fp->ctf_str_prov_len != 0)
|
||||
{
|
||||
ctf_set_errno (fp, EINVAL);
|
||||
ctf_err_warn (fp, 0, 0, _("cannot write out already-written dict with children and newly-added strings"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Fill in an initial CTF header. The type section begins at a 4-byte aligned
|
||||
boundary past the CTF header itself (at relative offset zero). The flag
|
||||
|
||||
@@ -19,12 +19,33 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctf-impl.h>
|
||||
#include <ctf-ref.h>
|
||||
#include <string.h>
|
||||
|
||||
static ctf_str_atom_t *
|
||||
ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
|
||||
int flags, uint32_t *ref);
|
||||
|
||||
/* Get the provisional offset, possibly climbing to the parent to do so. */
|
||||
static uint32_t
|
||||
get_prov_offset (ctf_dict_t *fp)
|
||||
{
|
||||
if (fp->ctf_parent)
|
||||
return fp->ctf_parent->ctf_str_prov_offset;
|
||||
else
|
||||
return fp->ctf_str_prov_offset;
|
||||
}
|
||||
|
||||
/* Similarly, set it. */
|
||||
static void
|
||||
set_prov_offset (ctf_dict_t *fp, uint32_t prov_offset)
|
||||
{
|
||||
if (fp->ctf_parent)
|
||||
fp->ctf_parent->ctf_str_prov_offset = prov_offset;
|
||||
else
|
||||
fp->ctf_str_prov_offset = prov_offset;
|
||||
}
|
||||
|
||||
/* Convert an encoded CTF string name into a pointer to a C string, possibly
|
||||
using an explicit internal provisional strtab rather than the fp-based
|
||||
one. */
|
||||
@@ -33,14 +54,52 @@ ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab)
|
||||
{
|
||||
int stid_tab = CTF_NAME_STID (name);
|
||||
ctf_strs_t *ctsp = &fp->ctf_str[stid_tab];
|
||||
uint32_t prov_offset;
|
||||
|
||||
/* For dicts in a parent/child relationship, there are two phases to string
|
||||
/* Special case: "" is at position zero. */
|
||||
|
||||
if (name == 0)
|
||||
return "";
|
||||
|
||||
/* If the name (adjusted to allow for names in the parent) is in the internal
|
||||
strtab, and the name offset is at least 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. This value is not
|
||||
adjusted to account for parent lengths or anything, it just descends from
|
||||
the top of the non-external string offset space, intermingling parent and
|
||||
child strings. */
|
||||
|
||||
prov_offset = get_prov_offset (fp);
|
||||
|
||||
if (prov_offset < fp->ctf_str[CTF_STRTAB_0].cts_len)
|
||||
{
|
||||
ctf_set_errno (fp, ECTF_INTERNAL);
|
||||
ctf_err_warn (fp, 0, 0, _("internal error: overlapping strtabs!"));
|
||||
}
|
||||
|
||||
/* Provisional strings may be in the parent as well as the child: check
|
||||
both. (Provisional offsets cannot appear in both.) */
|
||||
|
||||
if (stid_tab == CTF_STRTAB_0 && name >= prov_offset)
|
||||
{
|
||||
const char *str;
|
||||
|
||||
str = ctf_dynhash_lookup (fp->ctf_prov_strtab,
|
||||
(void *) (uintptr_t) name);
|
||||
if (!str && fp->ctf_parent)
|
||||
str = ctf_dynhash_lookup (fp->ctf_parent->ctf_prov_strtab,
|
||||
(void *) (uintptr_t) name);
|
||||
return str;
|
||||
}
|
||||
|
||||
/* Nonprovisional string.
|
||||
|
||||
For dicts in a parent/child relationship, there are two phases to string
|
||||
lookup: before writeout, fp->ctf_parent->cts_len is 0, and the parent and
|
||||
child are uncorrelated and lookups start at offset 0; and after writeout,
|
||||
the parent's strings are incorporated into the child and further
|
||||
modification of the parent's strtab (even the addition of new strings) is
|
||||
prohibited. This prohibition means that ctf_prov_strtab is safe to use:
|
||||
the "start" of the child strtab will never be observed changing. */
|
||||
prohibited. */
|
||||
|
||||
if (stid_tab == CTF_STRTAB_0)
|
||||
{
|
||||
@@ -87,22 +146,11 @@ ctf_strraw_explicit (ctf_dict_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 (adjusted to allow for names in the parent) is in the internal
|
||||
strtab, and the name 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 (stid_tab == 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));
|
||||
|
||||
ctf_err_warn (fp, 1, 0, _("offset %x: strtab not found or corrupt offset: cts_len is %zx, parent strlen is %u, cts_strs is %p"),
|
||||
CTF_NAME_OFFSET (name), ctsp->cts_len, fp->ctf_header->cth_parent_strlen, ctsp->cts_strs);
|
||||
ctf_err_warn (fp, 1, 0, _("offset %x: strtab not found or corrupt offset: cts_len is %zx, parent strlen is %u, cts_strs is %p, prov offset is %x, stid_tab is %u"),
|
||||
CTF_NAME_OFFSET (name), ctsp->cts_len, fp->ctf_header->cth_parent_strlen, ctsp->cts_strs, prov_offset, stid_tab);
|
||||
|
||||
/* String table not loaded or corrupt offset. */
|
||||
return NULL;
|
||||
@@ -197,17 +245,12 @@ ctf_str_create_atoms (ctf_dict_t *fp)
|
||||
if (!fp->ctf_prov_strtab)
|
||||
goto oom_prov_strtab;
|
||||
|
||||
errno = 0;
|
||||
ctf_str_add (fp, "");
|
||||
if (errno == ENOMEM)
|
||||
goto oom_str_add;
|
||||
|
||||
/* 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. The
|
||||
offsets are not parent-relative, so we don't need to have imported any
|
||||
dicts at this stage, and the parent need not be considered. */
|
||||
/* 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
|
||||
need not be populated. The offsets are not parent-relative, so we don't
|
||||
need to have imported any dicts at this stage, and the parent need not be
|
||||
considered. */
|
||||
|
||||
for (i = 0; i < fp->ctf_str[CTF_STRTAB_0].cts_len;
|
||||
i += strlen (&fp->ctf_str[CTF_STRTAB_0].cts_strs[i]) + 1)
|
||||
@@ -226,7 +269,10 @@ ctf_str_create_atoms (ctf_dict_t *fp)
|
||||
atom->csa_offset = i;
|
||||
}
|
||||
|
||||
fp->ctf_str_prov_offset = fp->ctf_str[CTF_STRTAB_0].cts_len + 1;
|
||||
/* Provisional offsets start from the offset before the STID-1 range and count
|
||||
down. */
|
||||
fp->ctf_str_prov_offset = (1U << 31) - 1;
|
||||
fp->ctf_str_prov_len = 0;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -244,6 +290,7 @@ void
|
||||
ctf_str_free_atoms (ctf_dict_t *fp)
|
||||
{
|
||||
ctf_dynhash_destroy (fp->ctf_prov_strtab);
|
||||
ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
|
||||
ctf_dynhash_destroy (fp->ctf_str_atoms);
|
||||
if (fp->ctf_dynstrtab)
|
||||
{
|
||||
@@ -254,9 +301,8 @@ ctf_str_free_atoms (ctf_dict_t *fp)
|
||||
|
||||
#define CTF_STR_ADD_REF 0x1
|
||||
#define CTF_STR_PROVISIONAL 0x2
|
||||
#define CTF_STR_MOVABLE 0x4
|
||||
#define CTF_STR_COPY 0x8
|
||||
#define CTF_STR_NO_DEDUP 0x10
|
||||
#define CTF_STR_COPY 0x4
|
||||
#define CTF_STR_NO_DEDUP 0x8
|
||||
|
||||
/* Add a string to the atoms table, copying the passed-in string if
|
||||
necessary. Return the atom added. Return NULL only when out of memory
|
||||
@@ -274,9 +320,18 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
|
||||
char *newstr = NULL;
|
||||
ctf_str_atom_t *atom = NULL;
|
||||
int added = 0;
|
||||
ctf_dict_t *lookup_fp = fp;
|
||||
|
||||
/* Check for existing atoms in the parent as well. */
|
||||
|
||||
atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
|
||||
|
||||
if (!atom && fp->ctf_parent)
|
||||
{
|
||||
lookup_fp = fp->ctf_parent;
|
||||
atom = ctf_dynhash_lookup (lookup_fp->ctf_str_atoms, str);
|
||||
}
|
||||
|
||||
/* Existing atoms get refs added only if they are provisional:
|
||||
non-provisional strings already have a fixed strtab offset, and just
|
||||
get their ref updated immediately, since its value cannot change. */
|
||||
@@ -286,23 +341,22 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
|
||||
if (flags & CTF_STR_NO_DEDUP)
|
||||
atom->csa_flags |= CTF_STR_ATOM_NO_DEDUP;
|
||||
|
||||
if (!ctf_dynhash_lookup (fp->ctf_prov_strtab, (void *) (uintptr_t)
|
||||
atom->csa_offset))
|
||||
if (atom->csa_offset < get_prov_offset (fp)
|
||||
|| atom->csa_external_offset != 0)
|
||||
{
|
||||
if (flags & CTF_STR_ADD_REF)
|
||||
{
|
||||
if (atom->csa_external_offset)
|
||||
*ref = atom->csa_external_offset;
|
||||
else
|
||||
*ref = atom->csa_offset;
|
||||
*ref = atom->csa_offset + lookup_fp->ctf_header->cth_parent_strlen;
|
||||
}
|
||||
return atom;
|
||||
}
|
||||
|
||||
if (flags & CTF_STR_ADD_REF)
|
||||
{
|
||||
if (!ctf_create_ref (fp, &atom->csa_refs, ref,
|
||||
flags & CTF_STR_MOVABLE))
|
||||
if (!ctf_create_ref (lookup_fp, &atom->csa_refs, ref))
|
||||
{
|
||||
ctf_set_errno (fp, ENOMEM);
|
||||
return NULL;
|
||||
@@ -312,17 +366,29 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
|
||||
return atom;
|
||||
}
|
||||
|
||||
/* New atom. Prohibited if this is a parent dict with children and a
|
||||
non-empty existing strtab. */
|
||||
/* New atom. */
|
||||
|
||||
if (fp->ctf_str[CTF_STRTAB_0].cts_len != 0
|
||||
&& fp->ctf_max_children != 0)
|
||||
&& fp->ctf_max_children != 0
|
||||
&& !(flags & CTF_STR_PROVISIONAL))
|
||||
{
|
||||
ctf_set_errno (fp, ECTF_RDONLY);
|
||||
ctf_err_warn (fp, 0, 0, _("attempt to add strings to a serialized parent dict"));
|
||||
ctf_err_warn (fp, 0, 0, _("attempt to add non-provisional strings to an "
|
||||
"already-serialized parent dict"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (flags & CTF_STR_PROVISIONAL)
|
||||
{
|
||||
if (get_prov_offset (fp) < fp->ctf_header->cth_parent_strlen
|
||||
+ fp->ctf_str[CTF_STRTAB_0].cts_len)
|
||||
{
|
||||
ctf_set_errno (fp, ECTF_FULL);
|
||||
ctf_err_warn (fp, 0, 0, _("strtab is full: cannot add more strings"));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if ((atom = malloc (sizeof (struct ctf_str_atom))) == NULL)
|
||||
goto oom;
|
||||
memset (atom, 0, sizeof (struct ctf_str_atom));
|
||||
@@ -358,23 +424,31 @@ ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
|
||||
atom->csa_snapshot_id = fp->ctf_snapshots;
|
||||
|
||||
/* New atoms marked provisional go into the provisional strtab, and get a ref
|
||||
added. The offset starts at 1, so may overlap with values in the parent:
|
||||
offsets are always adjusted by the size of the parent strtab before lookup
|
||||
to compensate for this. */
|
||||
added. Provisional offsets are shared among the parent and all children.
|
||||
|
||||
Special-case "" again: it gets a real offset of zero, not a high
|
||||
provisional one. This atom's offset is never returned (see the special
|
||||
case in ctf_strraw_explicit) and mostly exists for the sake of the
|
||||
deduplicator. */
|
||||
|
||||
if (flags & CTF_STR_PROVISIONAL)
|
||||
{
|
||||
atom->csa_offset = fp->ctf_str_prov_offset;
|
||||
if (str[0] == 0)
|
||||
atom->csa_offset = 0;
|
||||
else
|
||||
{
|
||||
set_prov_offset (fp, get_prov_offset (fp) - strlen (atom->csa_str) - 1);
|
||||
atom->csa_offset = get_prov_offset (fp);
|
||||
fp->ctf_str_prov_len += strlen (atom->csa_str) + 1;
|
||||
}
|
||||
|
||||
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 (flags & CTF_STR_ADD_REF)
|
||||
{
|
||||
if (!ctf_create_ref (fp, &atom->csa_refs, ref, flags & CTF_STR_MOVABLE))
|
||||
if (!ctf_create_ref (fp, &atom->csa_refs, ref))
|
||||
goto oom;
|
||||
}
|
||||
}
|
||||
@@ -401,23 +475,23 @@ ctf_str_add_flagged (ctf_dict_t *fp, const char *str, uint32_t *ref,
|
||||
str = "";
|
||||
|
||||
atom = ctf_str_add_ref_internal (fp, str, flags, ref);
|
||||
/* TODO handle failure better */
|
||||
if (!atom)
|
||||
return 0;
|
||||
|
||||
offset = atom->csa_offset + fp->ctf_header->cth_parent_strlen;
|
||||
|
||||
if (atom->csa_external_offset)
|
||||
offset = atom->csa_external_offset;
|
||||
else
|
||||
offset = atom->csa_offset;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
||||
/* 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.)
|
||||
string: if the string is not already known, 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.)
|
||||
|
||||
If this atom is already known to have an external offset, the external offset
|
||||
is simply returned unchanged. */
|
||||
@@ -455,15 +529,6 @@ ctf_str_add_no_dedup_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
|
||||
| CTF_STR_NO_DEDUP);
|
||||
}
|
||||
|
||||
/* Like ctf_str_add_ref(), but note that the ref may be moved later on. */
|
||||
uint32_t
|
||||
ctf_str_add_movable_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
|
||||
{
|
||||
return ctf_str_add_flagged (fp, str, ref,
|
||||
CTF_STR_ADD_REF | CTF_STR_PROVISIONAL
|
||||
| CTF_STR_MOVABLE);
|
||||
}
|
||||
|
||||
/* Add an external strtab reference at OFFSET. Returns zero if the addition
|
||||
failed, nonzero otherwise. */
|
||||
int
|
||||
@@ -480,6 +545,11 @@ ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
|
||||
|
||||
atom->csa_external_offset = CTF_SET_STID (offset, CTF_STRTAB_1);
|
||||
|
||||
/* The "synthetic external strtab" contains all strings that the linker has
|
||||
told us about, kept around so that we can look them up by external offset
|
||||
even in situations in which no ELF information is available, such as
|
||||
during late serialization. */
|
||||
|
||||
if (!fp->ctf_syn_ext_strtab)
|
||||
fp->ctf_syn_ext_strtab = ctf_dynhash_create (ctf_hash_integer,
|
||||
ctf_hash_eq_integer,
|
||||
@@ -495,8 +565,9 @@ ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
|
||||
atom->csa_external_offset,
|
||||
(void *) atom->csa_str) < 0)
|
||||
{
|
||||
/* No need to bother freeing the syn_ext_strtab: it will get freed at
|
||||
ctf_str_write_strtab time if unreferenced. */
|
||||
ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
|
||||
fp->ctf_syn_ext_strtab = NULL;
|
||||
|
||||
ctf_set_errno (fp, ENOMEM);
|
||||
return 0;
|
||||
}
|
||||
@@ -504,19 +575,6 @@ ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Remove a single ref to a string. */
|
||||
void
|
||||
ctf_str_remove_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
|
||||
{
|
||||
ctf_str_atom_t *atom = NULL;
|
||||
|
||||
atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
|
||||
if (!atom)
|
||||
return;
|
||||
|
||||
ctf_remove_ref (fp, &atom->csa_refs, ref);
|
||||
}
|
||||
|
||||
/* A ctf_dynhash_iter_remove() callback that removes atoms later than a given
|
||||
snapshot ID. External atoms are never removed, because they came from the
|
||||
linker string table and are still present even if you roll back type
|
||||
@@ -579,6 +637,8 @@ ctf_str_sort_strtab (const void *a, const void *b)
|
||||
serialization does not change the dict passed in, because the alternative
|
||||
is to copy the entire atoms table on every reserialization just to avoid
|
||||
modifying the original, which is excessively costly for minimal gain.
|
||||
There can be no references to the strings in the newly-added portion
|
||||
of the strtab on return, though some may appear at a later date.
|
||||
|
||||
We use the lazy man's approach and double memory costs by always storing
|
||||
atoms as individually allocated entities whenever they come from anywhere
|
||||
@@ -608,14 +668,14 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
void *v;
|
||||
int err;
|
||||
int new_strtab = 0;
|
||||
int any_external = 0;
|
||||
uint32_t prov_offset;
|
||||
|
||||
/* Writing a full v4 shared-with-parent child strtab is possible only if the
|
||||
parent has already been written out. */
|
||||
|
||||
if (fp->ctf_parent && fp->ctf_header->cth_parent_strlen != 0)
|
||||
{
|
||||
if (ctf_dynhash_elements (fp->ctf_parent->ctf_prov_strtab) != 0)
|
||||
if (fp->ctf_parent->ctf_str_prov_len != 0)
|
||||
{
|
||||
ctf_set_errno (fp, ECTF_NOTSERIALIZED);
|
||||
ctf_err_warn (fp, 0, 0, _("attempt to write strtab with unserialized parent"));
|
||||
@@ -655,9 +715,11 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
strtab->cts_len++; /* For the \0. */
|
||||
}
|
||||
|
||||
/* Count new entries in the strtab: i.e. entries in the provisional
|
||||
strtab. Ignore any entry for \0, entries which ended up in the
|
||||
external strtab, and unreferenced entries. */
|
||||
/* Count new entries in the strtab: i.e. entries in the provisional strtab, in
|
||||
the provisional range. Ignore any entry for \0, entries which ended up in
|
||||
the external strtab, and unreferenced entries. */
|
||||
|
||||
prov_offset = get_prov_offset (fp);
|
||||
|
||||
while ((err = ctf_dynhash_next (fp->ctf_prov_strtab, &it, NULL, &v)) == 0)
|
||||
{
|
||||
@@ -669,6 +731,7 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
goto err_strtab;
|
||||
|
||||
if (atom->csa_str[0] == 0 || atom->csa_external_offset
|
||||
|| atom->csa_offset < prov_offset
|
||||
|| ctf_list_empty_p (&atom->csa_refs))
|
||||
continue;
|
||||
|
||||
@@ -705,6 +768,7 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
goto err_sorttab;
|
||||
|
||||
if (atom->csa_str[0] == 0 || atom->csa_external_offset
|
||||
|| atom->csa_offset < prov_offset
|
||||
|| ctf_list_empty_p (&atom->csa_refs))
|
||||
continue;
|
||||
|
||||
@@ -758,10 +822,7 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
continue;
|
||||
|
||||
if (atom->csa_external_offset)
|
||||
{
|
||||
any_external = 1;
|
||||
offset = atom->csa_external_offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (atom->csa_flags & CTF_STR_ATOM_IN_PARENT
|
||||
@@ -782,6 +843,9 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
offset = atom->csa_offset + fp->ctf_header->cth_parent_strlen;
|
||||
}
|
||||
|
||||
if (!ctf_assert (fp, offset < prov_offset))
|
||||
goto err_strtab;
|
||||
|
||||
ctf_update_refs (&atom->csa_refs, offset);
|
||||
}
|
||||
if (err != ECTF_NEXT_END)
|
||||
@@ -792,12 +856,6 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
}
|
||||
ctf_str_purge_refs (fp);
|
||||
|
||||
if (!any_external)
|
||||
{
|
||||
ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
|
||||
fp->ctf_syn_ext_strtab = NULL;
|
||||
}
|
||||
|
||||
/* Replace the old strtab with the new one in this dict. */
|
||||
|
||||
if (fp->ctf_dynstrtab)
|
||||
@@ -810,12 +868,9 @@ ctf_str_write_strtab (ctf_dict_t *fp)
|
||||
fp->ctf_str[CTF_STRTAB_0].cts_strs = strtab->cts_strs;
|
||||
fp->ctf_str[CTF_STRTAB_0].cts_len = strtab->cts_len;
|
||||
|
||||
/* 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. */
|
||||
/* Note that all strings have been written out. */
|
||||
fp->ctf_str_prov_len = 0;
|
||||
|
||||
ctf_dynhash_empty (fp->ctf_prov_strtab);
|
||||
fp->ctf_str_prov_offset = strtab->cts_len + 1;
|
||||
return strtab;
|
||||
|
||||
err_sorttab:
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <ctf-impl.h>
|
||||
#include <string.h>
|
||||
#include "ctf-ref.h"
|
||||
#include "ctf-endian.h"
|
||||
|
||||
/* Simple doubly-linked list append routine. This implementation assumes that
|
||||
@@ -146,29 +147,12 @@ ctf_str_append_noerr (char *s, const char *append)
|
||||
return new_s;
|
||||
}
|
||||
|
||||
/* Initialize the ref system. */
|
||||
int
|
||||
ctf_init_refs (ctf_dict_t *fp)
|
||||
{
|
||||
fp->ctf_movable_refs = ctf_dynhash_create (ctf_hash_integer,
|
||||
ctf_hash_eq_integer,
|
||||
NULL, NULL);
|
||||
if (!fp->ctf_movable_refs)
|
||||
return -ENOMEM;
|
||||
return 0;
|
||||
}
|
||||
/* Destroy the ref system. */
|
||||
void
|
||||
ctf_free_refs (ctf_dict_t *fp)
|
||||
{
|
||||
ctf_dynhash_destroy (fp->ctf_movable_refs);
|
||||
}
|
||||
|
||||
/* Allocate a ref and bind it into a ref list. Does not actually
|
||||
initialize anything through the ref: the caller must do that. */
|
||||
|
||||
ctf_ref_t *
|
||||
ctf_create_ref (ctf_dict_t *fp, ctf_list_t *reflist, uint32_t *ref, int movable)
|
||||
ctf_create_ref (ctf_dict_t *fp _libctf_unused_, ctf_list_t *reflist,
|
||||
uint32_t *ref)
|
||||
{
|
||||
ctf_ref_t *aref;
|
||||
|
||||
@@ -178,80 +162,14 @@ ctf_create_ref (ctf_dict_t *fp, ctf_list_t *reflist, uint32_t *ref, int movable)
|
||||
return NULL;
|
||||
|
||||
aref->cre_ref = ref;
|
||||
|
||||
/* Movable refs get a backpointer to them in ctf_movable_refs: they can be
|
||||
moved later in batches via a call to ctf_move_refs. */
|
||||
|
||||
if (movable)
|
||||
{
|
||||
if (ctf_dynhash_insert (fp->ctf_movable_refs, ref, aref) < 0)
|
||||
{
|
||||
free (aref);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ctf_list_append (reflist, aref);
|
||||
|
||||
return aref;
|
||||
}
|
||||
|
||||
/* Note that refs have moved from (SRC, LEN) to DEST. We use the movable
|
||||
refs backpointer for this, because it is done an amortized-constant
|
||||
number of times during structure member and enumerand addition, and if we
|
||||
did a linear search this would turn such addition into an O(n^2)
|
||||
operation. */
|
||||
int
|
||||
ctf_move_refs (ctf_dict_t *fp, void *src, size_t len, void *dest)
|
||||
{
|
||||
uintptr_t p;
|
||||
|
||||
if (src == dest)
|
||||
return 0;
|
||||
|
||||
for (p = (uintptr_t) src; p - (uintptr_t) src < len; p++)
|
||||
{
|
||||
ctf_ref_t *ref;
|
||||
|
||||
if ((ref = ctf_dynhash_lookup (fp->ctf_movable_refs,
|
||||
(ctf_ref_t *) p)) != NULL)
|
||||
{
|
||||
int out_of_memory;
|
||||
|
||||
ref->cre_ref = (uint32_t *) (((uintptr_t) ref->cre_ref +
|
||||
(uintptr_t) dest - (uintptr_t) src));
|
||||
ctf_dynhash_remove (fp->ctf_movable_refs, (ctf_ref_t *) p);
|
||||
out_of_memory = ctf_dynhash_insert (fp->ctf_movable_refs,
|
||||
ref->cre_ref, ref);
|
||||
assert (out_of_memory == 0);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Remove a single ref. */
|
||||
void
|
||||
ctf_remove_ref (ctf_dict_t *fp, ctf_list_t *reflist, uint32_t *ref)
|
||||
{
|
||||
ctf_ref_t *aref, *anext;
|
||||
|
||||
for (aref = ctf_list_next (reflist); aref != NULL; aref = anext)
|
||||
{
|
||||
anext = ctf_list_next (aref);
|
||||
if (aref->cre_ref == ref)
|
||||
{
|
||||
ctf_list_delete (reflist, aref);
|
||||
ctf_dynhash_remove (fp->ctf_movable_refs, ref);
|
||||
free (aref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Remove all refs to a given entity. */
|
||||
void
|
||||
ctf_purge_ref_list (ctf_dict_t *fp, ctf_list_t *reflist)
|
||||
ctf_purge_ref_list (ctf_dict_t *fp _libctf_unused_, ctf_list_t *reflist)
|
||||
{
|
||||
ctf_ref_t *ref, *next;
|
||||
|
||||
@@ -259,13 +177,12 @@ ctf_purge_ref_list (ctf_dict_t *fp, ctf_list_t *reflist)
|
||||
{
|
||||
next = ctf_list_next (ref);
|
||||
ctf_list_delete (reflist, ref);
|
||||
ctf_dynhash_remove (fp->ctf_movable_refs, ref);
|
||||
free (ref);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Update a list of refs to the specified value. */
|
||||
|
||||
void
|
||||
ctf_update_refs (ctf_list_t *reflist, uint32_t value)
|
||||
{
|
||||
@@ -344,3 +261,4 @@ ctf_next_copy (ctf_next_t *i)
|
||||
ctf_next_destroy (i2);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,23 +37,24 @@ no_prop_err (void)
|
||||
int main (void)
|
||||
{
|
||||
ctf_dict_t *parent;
|
||||
ctf_dict_t *blank;
|
||||
ctf_dict_t *child;
|
||||
ctf_dict_t *wrong;
|
||||
ctf_id_t void_id;
|
||||
ctf_id_t wrong_id;
|
||||
ctf_id_t base;
|
||||
ctf_id_t slice;
|
||||
ctf_id_t function;
|
||||
ctf_id_t ptr;
|
||||
ctf_encoding_t long_encoding = { CTF_INT_SIGNED, 0, sizeof (long) };
|
||||
ctf_encoding_t void_encoding = { CTF_INT_SIGNED, 0, 0 };
|
||||
ctf_encoding_t foo;
|
||||
ctf_funcinfo_t fi;
|
||||
ctf_id_t bar;
|
||||
char *funcname;
|
||||
int err;
|
||||
|
||||
if ((parent = ctf_create (&err)) == NULL
|
||||
|| (child = ctf_create (&err)) == NULL
|
||||
|| (blank = ctf_create (&err)) == NULL)
|
||||
|| (wrong = ctf_create (&err)) == NULL)
|
||||
{
|
||||
fprintf (stderr, "Cannot create dicts: %s\n", ctf_errmsg (err));
|
||||
return 1;
|
||||
@@ -65,11 +66,49 @@ int main (void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((void_id = ctf_add_integer (parent, CTF_ADD_ROOT, "void", &void_encoding))
|
||||
/* Populate two dicts, one with the same types in a different order. This
|
||||
passes all ctf_import checks (type and strtab count), but will still
|
||||
induce errors due to type mismatches with the child. In particular, base
|
||||
in the right parent is a non-integral type (a pointer) in the wrong one,
|
||||
and "void" in the parent is an unknown type in the wrong one. */
|
||||
|
||||
if ((ctf_add_unknown (parent, CTF_ADD_ROOT, "spacer")) /* 1 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((base = ctf_add_integer (parent, CTF_ADD_ROOT, "long int", &long_encoding))
|
||||
if ((ctf_add_unknown (parent, CTF_ADD_ROOT, "spacer2")) /* 2 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((void_id = ctf_add_integer (parent, CTF_ADD_ROOT, "void", &void_encoding)) /* 3 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((base = ctf_add_integer (parent, CTF_ADD_ROOT, "long int", &long_encoding)) /* 4 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((ptr = ctf_add_pointer (parent, CTF_ADD_ROOT, void_id)) /* 5 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((ctf_add_integer (wrong, CTF_ADD_ROOT, "long int", &long_encoding)) /* 1 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((wrong_id = ctf_add_integer (wrong, CTF_ADD_ROOT, "void", &void_encoding)) /* 2 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((ctf_add_unknown (parent, CTF_ADD_ROOT, "spacer")) /* 3 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((ptr = ctf_add_pointer (wrong, CTF_ADD_ROOT, wrong_id)) /* 4 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if ((ctf_add_unknown (wrong, CTF_ADD_ROOT, "spacer2")) /* 5 */
|
||||
== CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
@@ -79,7 +118,8 @@ int main (void)
|
||||
if ((slice = ctf_add_slice (child, CTF_ADD_ROOT, base, &foo)) == CTF_ERR)
|
||||
goto parent_err;
|
||||
|
||||
if (ctf_add_variable (parent, "foo", base) < 0)
|
||||
/* Same name as a type: no change in strtab.strlen. */
|
||||
if (ctf_add_variable (parent, "base", base) < 0)
|
||||
goto child_err;
|
||||
|
||||
fi.ctc_return = void_id;
|
||||
@@ -98,58 +138,51 @@ int main (void)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_NOTFUNC);
|
||||
|
||||
if ((ctf_import (child, blank)) < 0)
|
||||
/* Write out and reopen to get a child with no parent. */
|
||||
if ((ctf_import (child, wrong)) < 0)
|
||||
{
|
||||
fprintf (stderr, "cannot reimport: %s\n", ctf_errmsg (ctf_errno (child)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* This is testing ctf_type_resolve_unsliced(), which is called by the enum
|
||||
functions (which are not themselves buggy). This typea isn't an enum, but
|
||||
functions (which are not themselves buggy). This type isn't an enum, but
|
||||
that's OK: we're after an error, after all, and the type we're slicing is
|
||||
not visible any longer, so nothing can tell it's not an enum. */
|
||||
|
||||
desc = "child slice resolution";
|
||||
if ((ctf_enum_value (child, slice, "foo", NULL)) != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_NONREPRESENTABLE);
|
||||
|
||||
desc = "child slice encoding lookup";
|
||||
if ((ctf_type_encoding (child, slice, &foo)) != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_BADID);
|
||||
|
||||
desc = "func info lookup of non-function";
|
||||
desc = "func info lookup of nonrepresentable function";
|
||||
if ((ctf_func_type_info (child, base, &fi)) != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_NONREPRESENTABLE);
|
||||
|
||||
desc = "func args lookup of non-function";
|
||||
desc = "func args lookup of nonrepresentable function";
|
||||
if ((ctf_func_type_args (child, base, 0, &bar)) != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_NONREPRESENTABLE);
|
||||
|
||||
desc = "child slice addition";
|
||||
if ((slice = ctf_add_slice (child, CTF_ADD_ROOT, base, &foo)) != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_NOTINTFP);
|
||||
|
||||
desc = "variable lookup";
|
||||
if (ctf_lookup_variable (child, "foo") != CTF_ERR)
|
||||
if (ctf_lookup_variable (child, "base") != CTF_ERR)
|
||||
no_prop_err ();
|
||||
check_prop_err (child, parent, ECTF_NOTYPEDAT);
|
||||
|
||||
desc = "function lookup via ctf_type_aname";
|
||||
if ((funcname = ctf_type_aname (child, function)) != NULL)
|
||||
{
|
||||
no_prop_err ();
|
||||
free (funcname);
|
||||
}
|
||||
check_prop_err (child, parent, ECTF_BADID);
|
||||
check_prop_err (child, wrong, ECTF_NOTYPEDAT);
|
||||
|
||||
ctf_dict_close (child);
|
||||
ctf_dict_close (parent);
|
||||
ctf_dict_close (blank);
|
||||
ctf_dict_close (wrong);
|
||||
fprintf (stderr, "All done.\n");
|
||||
return 0;
|
||||
|
||||
|
||||
@@ -52,6 +52,11 @@ main (int argc, char *argv[])
|
||||
else
|
||||
printf ("zygal's name is %s\n", name);
|
||||
|
||||
if (ctf_type_name (fp, autoschediastic, name, sizeof (name)) == NULL)
|
||||
fprintf (stderr, "Can't get name of autoschediastic: %s\n", ctf_errmsg (ctf_errno (fp)));
|
||||
else
|
||||
printf ("autoschediastic's name is %s after serialization\n", name);
|
||||
|
||||
/* Add another new name, roll back, and make sure the strings are
|
||||
uncorrupted. */
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
zygal's name is struct zygal
|
||||
zygal's name is struct zygal
|
||||
zygal's name is struct zygal
|
||||
autoschediastic's name is enum autoschediastic after serialization
|
||||
zygal's name is struct zygal after first rollback
|
||||
autoschediastic's name is enum autoschediastic after first rollback
|
||||
|
||||
Reference in New Issue
Block a user