binutils-gdb/libctf/libctf.ver
Nick Alcock f4f60336da libctf, include: find types of symbols by name
The existing ctf_lookup_by_symbol and ctf_arc_lookup_symbol functions
suffice to look up the types of symbols if the caller already has a
symbol number.  But the caller often doesn't have one of those and only
knows the name of the symbol: also, in object files, the caller might
not have a useful symbol number in any sense (and neither does libctf:
the 'symbol number' we use in that case literally starts at 0 for the
lexicographically first-sorted symbol in the symtypetab and counts those
symbols, so it corresponds to nothing useful).

This means that even though object files have a symtypetab (generated by
the compiler or by ld -r), the only way we can look up anything in it is
to iterate over all symbols in turn with ctf_symbol_next until we find
the one we want.

This is unhelpful and pointlessly inefficient.

So add a pair of functions to look up symbols by name in a dict and in a
whole archive: ctf_lookup_by_symbol_name and ctf_arc_lookup_symbol_name.
These are identical to the existing functions except that they take
symbol names rather than symbol numbers.

To avoid insane repetition, we do some refactoring in the process, so
that both ctf_lookup_by_symbol and ctf_arc_lookup_symbol turn into thin
wrappers around internal functions that do both lookup by symbol index
and lookup by name.  This massively reduces code duplication because
even the existing lookup-by-index stuff wants to use a name sometimes
(when looking up in indexed sections), and the new lookup-by-name stuff
has to turn it into an index sometimes (when looking up in non-indexed
sections): doing it this way lets us share most of that.

The actual name->index lookup is done by ctf_lookup_symbol_idx.  We do
not anticipate this lookup to be as heavily used as ld.so symbol lookup
by many orders of magnitude, so using the ELF symbol hashes would
probably take more time to read them than is saved by using the hashes,
and it adds a lot of complexity.  Instead, do a linear search for the
symbol name, caching all the name -> index mappings as we go, so that
future searches are likely to hit in the cache.  To avoid having to
repeat this search over and over in a CTF archive when
ctf_arc_lookup_symbol_name is used, have cached archive lookups (the
sort done by ctf_arc_lookup_symbol* and the ctf_archive_next iterator)
pick out the first dict they cache in a given archive and store it in a
new ctf_archive field, ctfi_crossdict_cache.  This can be used to store
cross-dictionary cached state that depends on things like the ELF symbol
table rather than the contents of any one dict.  ctf_lookup_symbol_idx
then caches its name->index mappings in the dictionary named in the
crossdict cache, if any, so that ctf_lookup_symbol_idx in other dicts
in the same archive benefit from the previous linear search, and the
symtab only needs to be scanned at most once.

(Note that if you call ctf_lookup_by_symbol_name in one specific dict,
and then follow it with a ctf_arc_lookup_symbol_name, the former will
not use the crossdict cache because it's only populated by the dict
opens in ctf_arc_lookup_symbol_name. This is harmless except for a small
one-off waste of memory and time: it's only a cache, after all.  We can
fix this later by using the archive caching machinery more
aggressively.)

In ctf-archive, we do similar things, turning ctf_arc_lookup_symbol into
a wrapper around a new function that does both index -> ID and name ->
ID lookups across all dicts in an archive.  We add a new
ctfi_symnamedicts cache that maps symbol names to the ctf_dict_t * that
it was found in (so that linear searches for symbols don't need to be
repeated): but we also *remove* a cache, the ctfi_syms cache that was
memoizing the actual ctf_id_t returned from every call to
ctf_arc_lookup_symbol.  This is pointless: all it saves is one call to
ctf_lookup_by_symbol, and that's basically an array lookup and nothing
more so isn't worth caching.  (Equally, given that symbol -> index
mappings are cached by ctf_lookup_by_symbol_name, those calls are nearly
free after the first call, so there's no point caching the ctf_id_t in
that case either.)

We fix up one test that was doing manual symbol lookup to use
ctf_arc_lookup_symbol instead, and enhance it to check that the caching
layer is not totally broken: we also add a new test to do lookups in a
.o file, and another to do lookups in an archive with conflicted types
and make sure that sort of multi-dict lookup is actually working.

include/ChangeLog
2021-02-17  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-api.h (ctf_arc_lookup_symbol_name): New.
	(ctf_lookup_by_symbol_name): Likewise.

libctf/ChangeLog
2021-02-17  Nick Alcock  <nick.alcock@oracle.com>

	* ctf-impl.h (ctf_dict_t) <ctf_symhash>: New.
	<ctf_symhash_latest>: Likewise.
	(struct ctf_archive_internal) <ctfi_crossdict_cache>: New.
	<ctfi_symnamedicts>: New.
	<ctfi_syms>: Remove.
	(ctf_lookup_symbol_name): Remove.
	* ctf-lookup.c (ctf_lookup_symbol_name): Propagate errors from
	parent properly.  Make static.
	(ctf_lookup_symbol_idx): New, linear search for the symbol name,
	cached in the crossdict cache's ctf_symhash (if available), or
	this dict's (otherwise).
	(ctf_try_lookup_indexed): Allow the symname to be passed in.
	(ctf_lookup_by_symbol): Turn into a wrapper around...
	(ctf_lookup_by_sym_or_name): ... this, supporting name lookup too,
	using ctf_lookup_symbol_idx in non-writable dicts.  Special-case
	name lookup in dynamic dicts without reported symbols, which have
	no symtab or dynsymidx but where name lookup should still work.
	(ctf_lookup_by_symbol_name): New, another wrapper.
	* ctf-archive.c (enosym): Note that this is present in
	ctfi_symnamedicts too.
	(ctf_arc_close): Adjust for removal of ctfi_syms.  Free the
	ctfi_symnamedicts.
	(ctf_arc_flush_caches): Likewise.
	(ctf_dict_open_cached): Memoize the first cached dict in the
	crossdict cache.
	(ctf_arc_lookup_symbol): Turn into a wrapper around...
	(ctf_arc_lookup_sym_or_name): ... this.  No longer cache
	ctf_id_t lookups: just call ctf_lookup_by_symbol as needed (but
	still cache the dicts those lookups succeed in).  Add
	lookup-by-name support, with dicts of successful lookups cached in
	ctfi_symnamedicts.  Refactor the caching code a bit.
	(ctf_arc_lookup_symbol_name): New, another wrapper.
	* ctf-open.c (ctf_dict_close): Free the ctf_symhash.
	* libctf.ver (LIBCTF_1.2): New version.  Add
	ctf_lookup_by_symbol_name, ctf_arc_lookup_symbol_name.
	* testsuite/libctf-lookup/enum-symbol.c (main): Use
	ctf_arc_lookup_symbol rather than looking up the name ourselves.
	Fish it out repeatedly, to make sure that symbol caching isn't
	broken.
	(symidx_64): Remove.
	(symidx_32): Remove.
	* testsuite/libctf-lookup/enum-symbol-obj.lk: Test symbol lookup
	in an unlinked object file (indexed symtypetab sections only).
	* testsuite/libctf-writable/symtypetab-nonlinker-writeout.c
	(try_maybe_reporting): Check symbol types via
	ctf_lookup_by_symbol_name as well as ctf_symbol_next.
	* testsuite/libctf-lookup/conflicting-type-syms.*: New test of
	lookups in a multi-dict archive.
2021-02-20 16:37:08 +00:00

205 lines
3.5 KiB
Plaintext

/* Copyright (C) 2019-2021 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/>. */
LIBCTF_1.0 {
global:
/* In libctf and libctf-nobfd. */
ctf_bufopen;
ctf_simple_open;
ctf_create;
ctf_close;
ctf_ref;
ctf_file_close;
ctf_cuname;
ctf_cuname_set;
ctf_parent_file;
ctf_parent_name;
ctf_parent_name_set;
ctf_type_isparent;
ctf_type_ischild;
ctf_import;
ctf_setmodel;
ctf_getmodel;
ctf_setspecific;
ctf_getspecific;
ctf_errno;
ctf_errmsg;
ctf_version;
ctf_func_info;
ctf_func_args;
ctf_func_type_info;
ctf_func_type_args;
ctf_lookup_by_name;
ctf_lookup_by_symbol;
ctf_lookup_variable;
ctf_type_resolve;
ctf_type_lname;
ctf_type_name;
ctf_type_name_raw;
ctf_type_aname;
ctf_type_aname_raw;
ctf_type_size;
ctf_type_align;
ctf_type_kind;
ctf_type_kind_forwarded;
ctf_type_reference;
ctf_type_pointer;
ctf_type_encoding;
ctf_type_visit;
ctf_type_cmp;
ctf_type_compat;
ctf_member_info;
ctf_member_next;
ctf_array_info;
ctf_member_count;
ctf_enum_name;
ctf_enum_value;
ctf_label_set;
ctf_label_get;
ctf_label_topmost;
ctf_label_info;
ctf_member_iter;
ctf_enum_iter;
ctf_enum_next;
ctf_type_iter;
ctf_type_next;
ctf_type_iter_all;
ctf_label_iter;
ctf_variable_iter;
ctf_variable_next;
ctf_next_create;
ctf_next_destroy;
ctf_next_copy;
ctf_add_array;
ctf_add_const;
ctf_add_enum;
ctf_add_enum_encoded;
ctf_add_float;
ctf_add_forward;
ctf_add_function;
ctf_add_integer;
ctf_add_pointer;
ctf_add_type;
ctf_add_typedef;
ctf_add_restrict;
ctf_add_slice;
ctf_add_struct;
ctf_add_union;
ctf_add_struct_sized;
ctf_add_union_sized;
ctf_add_volatile;
ctf_add_enumerator;
ctf_add_member;
ctf_add_member_offset;
ctf_add_member_encoded;
ctf_add_variable;
ctf_set_array;
ctf_update;
ctf_discard;
ctf_snapshot;
ctf_rollback;
ctf_write;
ctf_write_mem;
ctf_gzwrite;
ctf_compress_write;
ctf_getdatasect;
ctf_arc_write;
ctf_arc_write_fd;
ctf_arc_open;
ctf_arc_bufopen;
ctf_arc_close;
ctf_arc_open_by_name;
ctf_arc_open_by_name_sections;
ctf_archive_count;
ctf_archive_iter;
ctf_archive_next;
ctf_archive_raw_iter;
ctf_get_arc;
ctf_dump;
ctf_setdebug;
ctf_getdebug;
ctf_errwarning_next;
ctf_link_add_ctf;
ctf_link_add_cu_mapping;
ctf_link_set_memb_name_changer;
ctf_link_set_variable_filter;
ctf_link;
ctf_link_add_strtab;
ctf_link_shuffle_syms;
ctf_link_write;
/* In libctf alone. */
ctf_fdopen;
ctf_open;
ctf_bfdopen;
ctf_bfdopen_ctfsect;
local:
*;
};
LIBCTF_1.1 {
global:
ctf_dict_open;
ctf_dict_open_sections;
ctf_dict_close;
ctf_parent_dict;
ctf_symbol_next;
ctf_add_objt_sym;
ctf_add_func_sym;
ctf_link_add_linker_symbol;
ctf_arc_lookup_symbol;
ctf_arc_flush_caches;
ctf_getsymsect;
ctf_getstrsect;
ctf_symsect_endianness;
ctf_arc_symsect_endianness;
} LIBCTF_1.0;
LIBCTF_1.2 {
global:
ctf_lookup_by_symbol_name;
ctf_arc_lookup_symbol_name;
} LIBCTF_1.1;