diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2020-11-20 13:34:04 +0000 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2020-11-20 13:34:08 +0000 |
commit | 1136c379718cb9f6a82e71029f86cd8cf70fa6be (patch) | |
tree | cf8b731e9ac18ffe88e28d7aedf87cbd9ea24ce6 /libctf/ctf-link.c | |
parent | 3d16b64e28ab2cd7e69c0b31bc3ab1601891c969 (diff) | |
download | fsf-binutils-gdb-1136c379718cb9f6a82e71029f86cd8cf70fa6be.zip fsf-binutils-gdb-1136c379718cb9f6a82e71029f86cd8cf70fa6be.tar.gz fsf-binutils-gdb-1136c379718cb9f6a82e71029f86cd8cf70fa6be.tar.bz2 |
libctf: symbol type linking support
This adds facilities to write out the function info and data object
sections, which efficiently map from entries in the symbol table to
types. The write-side code is entirely new: the read-side code was
merely significantly changed and support for indexed tables added
(pointed to by the no-longer-unused cth_objtidxoff and cth_funcidxoff
header fields).
With this in place, you can use ctf_lookup_by_symbol to look up the
types of symbols of function and object type (and, as before, you can
use ctf_lookup_variable to look up types of file-scope variables not
present in the symbol table, as long as you know their name: but
variables that are also data objects are now found in the data object
section instead.)
(Compatible) file format change:
The CTF spec has always said that the function info section looks much
like the CTF_K_FUNCTIONs in the type section: an info word (including an
argument count) followed by a return type and N argument types. This
format is suboptimal: it means function symbols cannot be deduplicated
and it causes a lot of ugly code duplication in libctf. But
conveniently the compiler has never emitted this! Because it has always
emitted a rather different format that libctf has never accepted, we can
be sure that there are no instances of this function info section in the
wild, and can freely change its format without compatibility concerns or
a file format version bump. (And since it has never been emitted in any
code that generated any older file format version, either, we need keep
no code to read the format as specified at all!)
So the function info section is now specified as an array of uint32_t,
exactly like the object data section: each entry is a type ID in the
type section which must be of kind CTF_K_FUNCTION, the prototype of
this function.
This allows function types to be deduplicated and also correctly encodes
the fact that all functions declared in C really are types available to
the program: so they should be stored in the type section like all other
types. (In format v4, we will be able to represent the types of static
functions as well, but that really does require a file format change.)
We introduce a new header flag, CTF_F_NEWFUNCINFO, which is set if the
new function info format is in use. A sufficiently new compiler will
always set this flag. New libctf will always set this flag: old libctf
will refuse to open any CTF dicts that have this flag set. If the flag
is not set on a dict being read in, new libctf will disregard the
function info section. Format v4 will remove this flag (or, rather, the
flag has no meaning there and the bit position may be recycled for some
other purpose).
New API:
Symbol addition:
ctf_add_func_sym: Add a symbol with a given name and type. The
type must be of kind CTF_K_FUNCTION (a function
pointer). Internally this adds a name -> type
mapping to the ctf_funchash in the ctf_dict.
ctf_add_objt_sym: Add a symbol with a given name and type. The type
kind can be anything, including function pointers.
This adds to ctf_objthash.
These both treat symbols as name -> type mappings: the linker associates
symbol names with symbol indexes via the ctf_link_shuffle_syms callback,
which sets up the ctf_dynsyms/ctf_dynsymidx/ctf_dynsymmax fields in the
ctf_dict. Repeated relinks can add more symbols.
Variables that are also exposed as symbols are removed from the variable
section at serialization time.
CTF symbol type sections which have enough pads, defined by
CTF_INDEX_PAD_THRESHOLD (whether because they are in dicts with symbols
where most types are unknown, or in archive where most types are defined
in some child or parent dict, not in this specific dict) are sorted by
name rather than symidx and accompanied by an index which associates
each symbol type entry with a name: the existing ctf_lookup_by_symbol
will map symbol indexes to symbol names and look the names up in the
index automatically. (This is currently ELF-symbol-table-dependent, but
there is almost nothing specific to ELF in here and we can add support
for other symbol table formats easily).
The compiler also uses index sections to communicate the contents of
object file symbol tables without relying on any specific ordering of
symbols: it doesn't need to sort them, and libctf will detect an
unsorted index section via the absence of the new CTF_F_IDXSORTED header
flag, and sort it if needed.
Iteration:
ctf_symbol_next: Iterator which returns the types and names of symbols
one by one, either for function or data symbols.
This does not require any sorting: the ctf_link machinery uses it to
pull in all the compiler-provided symbols cheaply, but it is not
restricted to that use.
(Compatible) changes in API:
ctf_lookup_by_symbol: can now be called for object and function
symbols: never returns ECTF_NOTDATA (which is
now not thrown by anything, but is kept for
compatibility and because it is a plausible
error that we might start throwing again at some
later date).
Internally we also have changes to the ctf-string functionality so that
"external" strings (those where we track a string -> offset mapping, but
only write out an offset) can be consulted via the usual means
(ctf_strptr) before the strtab is written out. This is important
because ctf_link_add_linker_symbol can now be handed symbols named via
strtab offsets, and ctf_link_shuffle_syms must figure out their actual
names by looking in the external symtab we have just been fed by the
ctf_link_add_strtab callback, long before that strtab is written out.
include/ChangeLog
2020-11-20 Nick Alcock <nick.alcock@oracle.com>
* ctf-api.h (ctf_symbol_next): New.
(ctf_add_objt_sym): Likewise.
(ctf_add_func_sym): Likewise.
* ctf.h: Document new function info section format.
(CTF_F_NEWFUNCINFO): New.
(CTF_F_IDXSORTED): New.
(CTF_F_MAX): Adjust accordingly.
libctf/ChangeLog
2020-11-20 Nick Alcock <nick.alcock@oracle.com>
* ctf-impl.h (CTF_INDEX_PAD_THRESHOLD): New.
(_libctf_nonnull_): Likewise.
(ctf_in_flight_dynsym_t): New.
(ctf_dict_t) <ctf_funcidx_names>: Likewise.
<ctf_objtidx_names>: Likewise.
<ctf_nfuncidx>: Likewise.
<ctf_nobjtidx>: Likewise.
<ctf_funcidx_sxlate>: Likewise.
<ctf_objtidx_sxlate>: Likewise.
<ctf_objthash>: Likewise.
<ctf_funchash>: Likewise.
<ctf_dynsyms>: Likewise.
<ctf_dynsymidx>: Likewise.
<ctf_dynsymmax>: Likewise.
<ctf_in_flight_dynsym>: Likewise.
(struct ctf_next) <u.ctn_next>: Likewise.
(ctf_symtab_skippable): New prototype.
(ctf_add_funcobjt_sym): Likewise.
(ctf_dynhash_sort_by_name): Likewise.
(ctf_sym_to_elf64): Rename to...
(ctf_elf32_to_link_sym): ... this, and...
(ctf_elf64_to_link_sym): ... this.
* ctf-open.c (init_symtab): Check for lack of CTF_F_NEWFUNCINFO
flag, and presence of index sections. Refactor out
ctf_symtab_skippable and ctf_elf*_to_link_sym, and use them. Use
ctf_link_sym_t, not Elf64_Sym. Skip initializing objt or func
sxlate sections if corresponding index section is present. Adjust
for new func info section format.
(ctf_bufopen_internal): Add ctf_err_warn to corrupt-file error
handling. Report incorrect-length index sections. Always do an
init_symtab, even if there is no symtab section (there may be index
sections still).
(flip_objts): Adjust comment: func and objt sections are actually
identical in structure now, no need to caveat.
(ctf_dict_close): Free newly-added data structures.
* ctf-create.c (ctf_create): Initialize them.
(ctf_symtab_skippable): New, refactored out of
init_symtab, with st_nameidx_set check added.
(ctf_add_funcobjt_sym): New, add a function or object symbol to the
ctf_objthash or ctf_funchash, by name.
(ctf_add_objt_sym): Call it.
(ctf_add_func_sym): Likewise.
(symtypetab_delete_nonstatic_vars): New, delete vars also present as
data objects.
(CTF_SYMTYPETAB_EMIT_FUNCTION): New flag to symtypetab emitters:
this is a function emission, not a data object emission.
(CTF_SYMTYPETAB_EMIT_PAD): New flag to symtypetab emitters: emit
pads for symbols with no type (only set for unindexed sections).
(CTF_SYMTYPETAB_FORCE_INDEXED): New flag to symtypetab emitters:
always emit indexed.
(symtypetab_density): New, figure out section sizes.
(emit_symtypetab): New, emit a symtypetab.
(emit_symtypetab_index): New, emit a symtypetab index.
(ctf_serialize): Call them, emitting suitably sorted symtypetab
sections and indexes. Set suitable header flags. Copy over new
fields.
* ctf-hash.c (ctf_dynhash_sort_by_name): New, used to impose an
order on symtypetab index sections.
* ctf-link.c (ctf_add_type_mapping): Delete erroneous comment
relating to code that was never committed.
(ctf_link_one_variable): Improve variable name.
(check_sym): New, symtypetab analogue of check_variable.
(ctf_link_deduplicating_one_symtypetab): New.
(ctf_link_deduplicating_syms): Likewise.
(ctf_link_deduplicating): Call them.
(ctf_link_deduplicating_per_cu): Note that we don't call them in
this case (yet).
(ctf_link_add_strtab): Set the error on the fp correctly.
(ctf_link_add_linker_symbol): New (no longer a do-nothing stub), add
a linker symbol to the in-flight list.
(ctf_link_shuffle_syms): New (no longer a do-nothing stub), turn the
in-flight list into a mapping we can use, now its names are
resolvable in the external strtab.
* ctf-string.c (ctf_str_rollback_atom): Don't roll back atoms with
external strtab offsets.
(ctf_str_rollback): Adjust comment.
(ctf_str_write_strtab): Migrate ctf_syn_ext_strtab population from
writeout time...
(ctf_str_add_external): ... to string addition time.
* ctf-lookup.c (ctf_lookup_var_key_t): Rename to...
(ctf_lookup_idx_key_t): ... this, now we use it for syms too.
<clik_names>: New member, a name table.
(ctf_lookup_var): Adjust accordingly.
(ctf_lookup_variable): Likewise.
(ctf_lookup_by_id): Shuffle further up in the file.
(ctf_symidx_sort_arg_cb): New, callback for...
(sort_symidx_by_name): ... this new function to sort a symidx
found to be unsorted (likely originating from the compiler).
(ctf_symidx_sort): New, sort a symidx.
(ctf_lookup_symbol_name): Support dynamic symbols with indexes
provided by the linker. Use ctf_link_sym_t, not Elf64_Sym.
Check the parent if a child lookup fails.
(ctf_lookup_by_symbol): Likewise. Work for function symbols too.
(ctf_symbol_next): New, iterate over symbols with types (without
sorting).
(ctf_lookup_idx_name): New, bsearch for symbol names in indexes.
(ctf_try_lookup_indexed): New, attempt an indexed lookup.
(ctf_func_info): Reimplement in terms of ctf_lookup_by_symbol.
(ctf_func_args): Likewise.
(ctf_get_dict): Move...
* ctf-types.c (ctf_get_dict): ... here.
* ctf-util.c (ctf_sym_to_elf64): Re-express as...
(ctf_elf64_to_link_sym): ... this. Add new st_symidx field, and
st_nameidx_set (always 0, so st_nameidx can be ignored). Look in
the ELF strtab for names.
(ctf_elf32_to_link_sym): Likewise, for Elf32_Sym.
(ctf_next_destroy): Destroy ctf_next_t.u.ctn_next if need be.
* libctf.ver: Add ctf_symbol_next, ctf_add_objt_sym and
ctf_add_func_sym.
Diffstat (limited to 'libctf/ctf-link.c')
-rw-r--r-- | libctf/ctf-link.c | 346 |
1 files changed, 331 insertions, 15 deletions
diff --git a/libctf/ctf-link.c b/libctf/ctf-link.c index 3f4f2ee..4b86ca9 100644 --- a/libctf/ctf-link.c +++ b/libctf/ctf-link.c @@ -51,10 +51,6 @@ ctf_add_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type); - /* This dynhash is a bit tricky: it has a multivalued (structural) key, so we - need to use the sized-hash machinery to generate key hashing and equality - functions. */ - if (dst_fp->ctf_link_type_mapping == NULL) { ctf_hash_fun f = ctf_hash_type_key; @@ -574,7 +570,7 @@ ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_) ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_; ctf_dict_t *per_cu_out_fp; ctf_id_t dst_type = 0; - ctf_dict_t *check_fp; + ctf_dict_t *insert_fp; ctf_dvdef_t *dvd; /* See if this variable is filtered out. */ @@ -590,18 +586,18 @@ ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_) dict, we want to try to add to that first: if it reports a duplicate, or if the type is in a child already, add straight to the child. */ - check_fp = arg->out_fp; + insert_fp = arg->out_fp; - dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp); + dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp); if (dst_type != 0) { - if (check_fp == arg->out_fp) + if (insert_fp == arg->out_fp) { - if (check_variable (name, check_fp, dst_type, &dvd)) + if (check_variable (name, insert_fp, dst_type, &dvd)) { /* No variable here: we can add it. */ - if (ctf_add_variable (check_fp, name, dst_type) < 0) - return (ctf_set_errno (arg->out_fp, ctf_errno (check_fp))); + if (ctf_add_variable (insert_fp, name, dst_type) < 0) + return (ctf_set_errno (arg->out_fp, ctf_errno (insert_fp))); return 0; } @@ -628,11 +624,11 @@ ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_) arg->cu_name)) == NULL) return -1; /* Errno is set for us. */ - /* If the type was not found, check for it in the child too. */ + /* If the type was not found, check for it in the child too. */ if (dst_type == 0) { - check_fp = per_cu_out_fp; - dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp); + insert_fp = per_cu_out_fp; + dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp); if (dst_type == 0) { @@ -1153,6 +1149,169 @@ ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs, return 0; } +/* Check for symbol conflicts during linking. Three possibilities: already + exists, conflicting, or nonexistent. We don't have a dvd structure we can + use as a flag like check_variable does, so we use a tristate return + value instead: -1: conflicting; 1: nonexistent: 0: already exists. */ + +static int +check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions) +{ + ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash; + ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash; + void *value; + + /* Wrong type (function when object is wanted, etc). */ + if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL)) + return -1; + + /* Not present at all yet. */ + if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value)) + return 1; + + /* Already present. */ + if ((ctf_id_t) (uintptr_t) value == type) + return 0; + + /* Wrong type. */ + return -1; +} + +/* Do a deduplicating link of one symtypetab (function info or data object) in + one input dict. */ + +static int +ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input, + int cu_mapped, int functions) +{ + ctf_next_t *it = NULL; + const char *name; + ctf_id_t type; + const char *in_file_name; + + if (ctf_cuname (input) != NULL) + in_file_name = ctf_cuname (input); + else + in_file_name = "unnamed-CU"; + + while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR) + { + ctf_id_t dst_type; + ctf_dict_t *per_cu_out_fp; + ctf_dict_t *insert_fp = fp; + int sym; + + /* Look in the parent first. */ + + dst_type = ctf_type_mapping (input, type, &insert_fp); + if (dst_type != 0) + { + if (insert_fp == fp) + { + sym = check_sym (fp, name, dst_type, functions); + + /* Already present: next symbol. */ + if (sym == 0) + continue; + /* Not present: add it. */ + else if (sym > 0) + { + if (ctf_add_funcobjt_sym (fp, functions, + name, dst_type) < 0) + return -1; /* errno is set for us. */ + continue; + } + } + } + + /* Can't add to the parent due to a name clash (most unlikely), or because + it references a type only present in the child. Try adding to the + child, creating if need be. If we can't do that, skip it. Don't add + to a child if we're doing a CU-mapped link, since that has only one + output. */ + if (cu_mapped) + { + ctf_dprintf ("Symbol %s in input file %s depends on a type %lx " + "hidden due to conflicts: skipped.\n", name, + in_file_name, type); + continue; + } + + if ((per_cu_out_fp = ctf_create_per_cu (fp, in_file_name, + in_file_name)) == NULL) + return -1; /* errno is set for us. */ + + /* If the type was not found, check for it in the child too. */ + if (dst_type == 0) + { + insert_fp = per_cu_out_fp; + dst_type = ctf_type_mapping (input, type, &insert_fp); + + if (dst_type == 0) + { + ctf_err_warn (fp, 1, 0, + _("type %lx for symbol %s in input file %s " + "not found: skipped"), type, name, in_file_name); + continue; + } + } + + sym = check_sym (per_cu_out_fp, name, dst_type, functions); + + /* Already present: next symbol. */ + if (sym == 0) + continue; + /* Not present: add it. */ + else if (sym > 0) + { + if (ctf_add_funcobjt_sym (per_cu_out_fp, functions, + name, dst_type) < 0) + return -1; /* errno is set for us. */ + } + else + { + /* Perhaps this should be an assertion failure. */ + ctf_err_warn (fp, 0, ECTF_DUPLICATE, + _("symbol %s in input file %s found conflicting " + "even when trying in per-CU dict."), name, + in_file_name); + return (ctf_set_errno (fp, ECTF_DUPLICATE)); + } + } + if (ctf_errno (input) != ECTF_NEXT_END) + { + ctf_set_errno (fp, ctf_errno (input)); + ctf_err_warn (fp, 0, ctf_errno (input), + functions ? _("iterating over function symbols") : + _("iterating over data symbols")); + return -1; + } + + return 0; +} + +/* Do a deduplicating link of the function info and data objects + in the inputs. */ +static int +ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs, + size_t ninputs, int cu_mapped) +{ + size_t i; + + for (i = 0; i < ninputs; i++) + { + if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i], + cu_mapped, 0) < 0) + return -1; /* errno is set for us. */ + + if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i], + cu_mapped, 1) < 0) + return -1; /* errno is set for us. */ + } + + return 0; +} + /* Do the per-CU part of a deduplicating link. */ static int ctf_link_deduplicating_per_cu (ctf_dict_t *fp) @@ -1305,6 +1464,11 @@ ctf_link_deduplicating_per_cu (ctf_dict_t *fp) goto err_inputs_outputs; } + /* For now, we omit symbol section linking for CU-mapped links, until it + is clear how to unify the symbol table across such links. (Perhaps we + should emit an unconditionally indexed symtab, like the compiler + does.) */ + if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0) { free (inputs); @@ -1457,6 +1621,15 @@ ctf_link_deduplicating (ctf_dict_t *fp) goto err; } + if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0) + { + ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for " + "%s"), ctf_link_input_name (fp)); + for (i = 1; i < noutputs; i++) + ctf_dict_close (outputs[i]); + goto err; + } + /* Now close all the inputs, including per-CU intermediates. */ if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0) @@ -1591,19 +1764,162 @@ ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string, err = iter_arg.err; } + if (err) + ctf_set_errno (fp, err); + return -err; } -/* Not yet implemented. */ +/* Inform the ctf-link machinery of a new symbol in the target symbol table + (which must be some symtab that is not usually stripped, and which + is in agreement with ctf_bfdopen_ctfsect). May be called either before or + after ctf_link_add_strtab. */ int ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym) { + ctf_in_flight_dynsym_t *cid; + + /* Cheat a little: if there is already an ENOMEM error code recorded against + this dict, we shouldn't even try to add symbols because there will be no + memory to do so: probably we failed to add some previous symbol. This + makes out-of-memory exits 'sticky' across calls to this function, so the + caller doesn't need to worry about error conditions. */ + + if (ctf_errno (fp) == ENOMEM) + return -ENOMEM; /* errno is set for us. */ + + if (ctf_symtab_skippable (sym)) + return 0; + + if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC) + return 0; + + /* Add the symbol to the in-flight list. */ + + if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL) + goto oom; + + cid->cid_sym = *sym; + ctf_list_append (&fp->ctf_in_flight_dynsyms, cid); + return 0; + + oom: + ctf_dynhash_destroy (fp->ctf_dynsyms); + fp->ctf_dynsyms = NULL; + ctf_set_errno (fp, ENOMEM); + return -ENOMEM; } + +/* Impose an ordering on symbols. The ordering takes effect immediately, but + since the ordering info does not include type IDs, lookups may return nothing + until such IDs are added by calls to ctf_add_*_sym. Must be called after + ctf_link_add_strtab and ctf_link_add_linker_symbol. */ int ctf_link_shuffle_syms (ctf_dict_t *fp) { + ctf_in_flight_dynsym_t *did, *nid; + ctf_next_t *i = NULL; + int err = ENOMEM; + void *name_, *sym_; + + if (!fp->ctf_dynsyms) + { + fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string, + ctf_hash_eq_string, + NULL, free); + if (!fp->ctf_dynsyms) + { + ctf_set_errno (fp, ENOMEM); + return -ENOMEM; + } + } + + /* Add all the symbols, excluding only those we already know are prohibited + from appearing in symtypetabs. */ + + for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid) + { + ctf_link_sym_t *new_sym; + + nid = ctf_list_next (did); + ctf_list_delete (&fp->ctf_in_flight_dynsyms, did); + + /* We might get a name or an external strtab offset. The strtab offset is + guaranteed resolvable at this point, so turn it into a string. */ + + if (did->cid_sym.st_name == NULL) + { + uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1); + + did->cid_sym.st_name = ctf_strraw (fp, off); + did->cid_sym.st_nameidx_set = 0; + if (!ctf_assert (fp, did->cid_sym.st_name != NULL)) + return -ECTF_INTERNAL; /* errno is set for us. */ + } + + /* The symbol might have turned out to be nameless, so we have to recheck + for skippability here. */ + if (!ctf_symtab_skippable (&did->cid_sym)) + { + ctf_dprintf ("symbol name from linker: %s\n", did->cid_sym.st_name); + + if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL) + goto local_oom; + + memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t)); + if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0) + goto local_oom; + + if (fp->ctf_dynsymmax < new_sym->st_symidx) + fp->ctf_dynsymmax = new_sym->st_symidx; + } + + free (did); + continue; + + local_oom: + free (did); + free (new_sym); + goto err; + } + + /* Construct a mapping from shndx to the symbol info. */ + free (fp->ctf_dynsymidx); + if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1, + sizeof (ctf_link_sym_t *))) == NULL) + goto err; + + while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0) + { + const char *name = (const char *) name; + ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_; + + if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax)) + { + ctf_next_destroy (i); + err = ctf_errno (fp); + goto err; + } + fp->ctf_dynsymidx[symp->st_symidx] = symp; + } + if (err != ECTF_NEXT_END) + { + ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols")); + goto err; + } return 0; + + err: + /* Leave the in-flight symbols around: they'll be freed at + dict close time regardless. */ + ctf_dynhash_destroy (fp->ctf_dynsyms); + fp->ctf_dynsyms = NULL; + free (fp->ctf_dynsymidx); + fp->ctf_dynsymidx = NULL; + fp->ctf_dynsymmax = 0; + ctf_set_errno (fp, err); + return -err; } typedef struct ctf_name_list_accum_cb_arg |