Commit Graph

99181 Commits

Author SHA1 Message Date
Nick Alcock de07e349be libctf: remove ctf_malloc, ctf_free and ctf_strdup
These just get in the way of auditing for erroneous usage of strdup and
add a huge irregular surface of "ctf_malloc or malloc? ctf_free or free?
ctf_strdup or strdup?"

ctf_malloc and ctf_free usage has not reliably matched up for many
years, if ever, making the whole game pointless.

Go back to malloc, free, and strdup like everyone else: while we're at
it, fix a bunch of places where we weren't properly checking for OOM.
This changes the interface of ctf_cuname_set and ctf_parent_name_set,
which could strdup but could not return errors (like ENOMEM).

New in v4.

include/
	* ctf-api.h (ctf_cuname_set): Can now fail, returning int.
	(ctf_parent_name_set): Likewise.
libctf/
	* ctf-impl.h (ctf_alloc): Remove.
	(ctf_free): Likewise.
	(ctf_strdup): Likewise.
	* ctf-subr.c (ctf_alloc): Remove.
	(ctf_free): Likewise.
	* ctf-util.c (ctf_strdup): Remove.

	* ctf-create.c (ctf_serialize): Use malloc, not ctf_alloc; free, not
	ctf_free; strdup, not ctf_strdup.
	(ctf_dtd_delete): Likewise.
	(ctf_dvd_delete): Likewise.
	(ctf_add_generic): Likewise.
	(ctf_add_function): Likewise.
	(ctf_add_enumerator): Likewise.
	(ctf_add_member_offset): Likewise.
	(ctf_add_variable): Likewise.
	(membadd): Likewise.
	(ctf_compress_write): Likewise.
	(ctf_write_mem): Likewise.
	* ctf-decl.c (ctf_decl_push): Likewise.
	(ctf_decl_fini): Likewise.
	(ctf_decl_sprintf): Likewise.  Check for OOM.
	* ctf-dump.c (ctf_dump_append): Use malloc, not ctf_alloc; free, not
	ctf_free; strdup, not ctf_strdup.
	(ctf_dump_free): Likewise.
	(ctf_dump): Likewise.
	* ctf-open.c (upgrade_types_v1): Likewise.
	(init_types): Likewise.
	(ctf_file_close): Likewise.
	(ctf_bufopen_internal): Likewise.  Check for OOM.
	(ctf_parent_name_set): Likewise: report the OOM to the caller.
	(ctf_cuname_set): Likewise.
	(ctf_import): Likewise.
	* ctf-string.c (ctf_str_purge_atom_refs): Use malloc, not ctf_alloc;
	free, not ctf_free; strdup, not ctf_strdup.
	(ctf_str_free_atom): Likewise.
	(ctf_str_create_atoms): Likewise.
	(ctf_str_add_ref_internal): Likewise.
	(ctf_str_remove_ref): Likewise.
	(ctf_str_write_strtab): Likewise.
2019-10-03 17:04:56 +01:00
Nick Alcock 9c1a2295e8 libctf: get the encoding of non-ints/fps in the dynamic space right
If you call ctf_type_encoding() on a slice, you are meant to get the
encoding of the slice with the format of the underlying type.  If
you call it on a non-int, non-fp, non-slice, you're meant to get the
error ECTF_INTNOTFP.

None of this was implemented for types in the dynamic space (which, now,
is *all* types in writable containers).  Instead, we were always
returning the encoding as if it were a float, which for all other types
consulted the wrong part of a discriminated union and returned garbage.
(Curiously, existing users were more disturbed by the lack of an error
in the non-int/fp/slice case than they were about getting garbage back.)

libctf/
	* ctf-types.c (ctf_type_encoding): Fix the dynamic case to
	work right for non-int/fps.
2019-10-03 17:04:56 +01:00
Nick Alcock 1a6ab13e71 libctf: allow ctf_type_lname of a null pointer.
The code was meant to handle this, but accidentally dereferenced the
null pointer before checking it for nullity.

v5: fix tabdamage.

libctf/
	* ctf-types.c (ctf_type_name): Don't strlen a potentially-
	null pointer.
2019-10-03 17:04:56 +01:00
Nick Alcock 99dc3ebdff libctf: properly handle ctf_add_type of forwards and self-reffing structs
The code to handle structures (and unions) that refer to themselves in
ctf_add_type is extremely dodgy.  It works by looking through the list
of not-yet-committed types for a structure with the same name as the
structure in question and assuming, if it finds it, that this must be a
reference to the same type.  This is a linear search that gets ever
slower as the dictionary grows, requiring you to call ctf_update at
intervals to keep performance tolerable: but if you do that, you run
into the problem that if a forward declared before the ctf_update is
changed to a structure afterwards, ctf_update explodes.

The last commit fixed most of this: this commit can use it, adding a new
ctf_add_processing hash that tracks source type IDs that are currently
being processed and uses it to avoid infinite recursion rather than the
dynamic type list: we split ctf_add_type into a ctf_add_type_internal,
so that ctf_add_type itself can become a wrapper that empties out this
being-processed hash once the entire recursive type addition is over.
Structure additions themselves avoid adding their dependent types
quite so much by checking the type mapping and avoiding re-adding types
we already know we have added.

We also add support for adding forwards to dictionaries that already
contain the thing they are a forward to: we just silently return the
original type.

v4: return existing struct/union/enum types properly, rather than using
    an uninitialized variable: shrinks sizes of CTF sections back down
    to roughly where they were in v1/v2 of this patch series.
v5: fix tabdamage.

libctf/
	* ctf-impl.h (ctf_file_t) <ctf_add_processing>: New.
	* ctf-open.c (ctf_file_close): Free it.
	* ctf-create.c (ctf_serialize): Adjust.
	(membcmp): When reporting a conflict due to an error, report the
	error.
	(ctf_add_type): Turn into a ctf_add_processing wrapper.  Rename to...
	(ctf_add_type_internal): ... this.  Hand back types we are already
	in the middle of adding immediately.  Hand back structs/unions with
	the same number of members immediately.  Do not walk the dynamic
	list.  Call ctf_add_type_internal, not ctf_add_type.  Handle
	forwards promoted to other types and the inverse case identically.
	Add structs to the mapping as soon as we intern them, before they
	gain any members.
2019-10-03 17:04:56 +01:00
Nick Alcock 676c3ecbad 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.
2019-10-03 17:04:56 +01:00
Nick Alcock 791915db42 libctf: handle nonrepresentable types at link time
GCC can emit references to type 0 to indicate that this type is one that
is not representable in the version of CTF it emits (for instance,
version 3 cannot encode vector types).  Type 0 is already used in the
function section to indicate padding inserted to skip functions we do
not want to encode the type of, so using zero in this way is a good
extension of the format: but libctf reports such types as ECTF_BADID,
which is indistinguishable from file corruption via links to truly
nonexistent types with IDs like 0xDEADBEEF etc, which we really do want
to stop for.

In particular, this stops all traversals of types dead at this point,
preventing us from even dumping CTF files containing unrepresentable
types to see what's going on!

So add a new error, ECTF_NONREPRESENTABLE, which is returned by
recursive type resolution when a reference to a zero type is found.  (No
zero type is ever emitted into the CTF file by GCC, only references to
one).  We can't do much with types that are ultimately nonrepresentable,
but we can do enough to keep functioning.

Adjust ctf_add_type to ensure that top-level types of type zero and
structure and union members of ultimate type zero are simply skipped
without reporting an error, so we can copy structures and unions that
contain nonrepresentable members (skipping them and leaving a hole where
they would be, so no consumers downstream of the linker need to worry
about this): adjust the dumper so that we dump members of
nonrepresentable types in a simple form that indicates
nonrepresentability rather than terminating the dump, and do not falsely
assume all errors to be -ENOMEM: adjust the linker so that types that
fail to get added are simply skipped, so that both nonrepresentable
types and outright errors do not terminate the type addition, which
could skip many valid types and cause further errors when variables of
those types are added.

In future, when we gain the ability to call back to the linker to report
link-time type resolution errors, we should report failures to add all
but nonrepresentable types.  But we can't do that yet.

v5: Fix tabdamage.

include/
	* ctf-api.h (ECTF_NONREPRESENTABLE): New.
libctf/
	* ctf-types.c (ctf_type_resolve): Return ECTF_NONREPRESENTABLE on
	type zero.
	* ctf-create.c (ctf_add_type): Detect and skip nonrepresentable
	members and types.
	(ctf_add_variable): Likewise for variables pointing to them.
	* ctf-link.c (ctf_link_one_type): Do not warn for nonrepresentable
	type link failure, but do warn for others.
	* ctf-dump.c (ctf_dump_format_type): Likewise.  Do not assume all
	errors to be ENOMEM.
	(ctf_dump_member): Likewise.
	(ctf_dump_type): Likewise.
	(ctf_dump_header_strfield): Do not assume all errors to be ENOMEM.
	(ctf_dump_header_sectfield): Do not assume all errors to be ENOMEM.
	(ctf_dump_header): Likewise.
	(ctf_dump_label): likewise.
	(ctf_dump_objts): likewise.
	(ctf_dump_funcs): likewise.
	(ctf_dump_var): likewise.
	(ctf_dump_str): Likewise.
2019-10-03 17:04:56 +01:00
Nick Alcock fd86991bc5 objdump: get CTF parent importing right
The linker emits CTF into a single section named .ctf, which is a CTF
archive where the default member (itself named ".ctf", or simply NULL)
is the parent of all other members.  Teach objdump to look for this by
default, rather than only trying to do it if a specific CTF parent
section was specified.  (If no parent name is specified, we get the .ctf
member from the same section as everything else, which matches what the
linker generates.)

binutils/
	* objdump.c (dump_ctf): Use the default CTF archive member as the
	parent even when no parent section is specified.
	(dump_ctf_archive_member): Only import from the parent
	if this is not the default ".ctf" member.
2019-10-03 17:04:56 +01:00
Nick Alcock 87279e3cef libctf: installable libctf as a shared library
This lets other programs read and write CTF-format data.

Two versioned shared libraries are created: libctf.so and
libctf-nobfd.so.  They contain identical content except that
libctf-nobfd.so contains no references to libbfd and does not implement
ctf_open, ctf_fdopen, ctf_bfdopen or ctf_bfdopen_ctfsect, so it can be
used by programs that cannot use BFD, like readelf.

The soname major version is presently .0 until the linker API
stabilizes, when it will flip to .1 and hopefully never change again.

New in v3.
v4: libtoolize and turn into a pair of shared libraries.  Drop
    --enable-install-ctf: now controlled by --enable-shared and
    --enable-install-libbfd, like everything else.
v5: Add ../bfd to ACLOCAL_AMFLAGS and AC_CONFIG_MACRO_DIR.  Fix tabdamage.

	* Makefile.def (host_modules): libctf is no longer no_install.
	* Makefile.in: Regenerated.
libctf/
	* configure.ac (AC_DISABLE_SHARED): New, like opcodes/.
	(LT_INIT): Likewise.
	(AM_INSTALL_LIBBFD): Likewise.
	(dlopen): Note why this is necessary in a comment.
	(SHARED_LIBADD): Initialize for possibly-PIC libiberty: derived from
	opcodes/.
	(SHARED_LDFLAGS): Likewise.
	(BFD_LIBADD): Likewise, for libbfd.
	(BFD_DEPENDENCIES): Likewise.
	(VERSION_FLAGS): Initialize, using a version script if ld supports
	one, or libtool -export-symbols-regex otherwise.
	(AC_CONFIG_MACRO_DIR): Add ../BFD.
	* Makefile.am (ACLOCAL_AMFLAGS): Likewise.
	(INCDIR): New.
	(AM_CPPFLAGS): Use $(srcdir), not $(top_srcdir).
	(noinst_LIBRARIES): Replace with...
	[INSTALL_LIBBFD] (lib_LTLIBRARIES): This, or...
	[!INSTALL_LIBBFD] (noinst_LTLIBRARIES): ... this, mentioning new
	libctf-nobfd.la as well.
	[INSTALL_LIBCTF] (include_HEADERS): Add the CTF headers.
	[!INSTALL_LIBCTF] (include_HEADERS): New, empty.
	(libctf_a_SOURCES): Rename to...
	(libctf_nobfd_la_SOURCES): ... this, all of libctf other than
	ctf-open-bfd.c.
	(libctf_la_SOURCES): Now derived from libctf_nobfd_la_SOURCES,
	with ctf-open-bfd.c added.
	(libctf_nobfd_la_LIBADD): New, using @SHARED_LIBADD@.
	(libctf_la_LIBADD): New, using @BFD_LIBADD@ as well.
	(libctf_la_DEPENDENCIES): New, using @BFD_DEPENDENCIES@.
	* Makefile.am [INSTALL_LIBCTF]: Use it.
	* aclocal.m4: Add ../bfd/acinclude.m4, ../config/acx.m4, and the
	libtool macros.
	* libctf.ver: New, everything is version LIBCTF_1.0 currently (even
	the unstable components).
	* Makefile.in: Regenerated.
	* config.h.in: Likewise.
	* configure: Likewise.
binutils/
	* Makefile.am (LIBCTF): Mention the .la file.
	(LIBCTF_NOBFD): New.
	(readelf_DEPENDENCIES): Use it.
	(readelf_LDADD): Likewise.
	* Makefile.in: Regenerated.
ld/
	* configure.ac (TESTCTFLIB): Set to the .so or .a, like TESTBFDLIB.
	* Makefile.am (TESTCTFLIB): Use it.
	(LIBCTF): Use the .la file.
	(check-DEJAGNU): Use it.
	* Makefile.in: Regenerated.
	* configure: Likewise.
include/
	* ctf-api.h: Note the instability of the ctf_link interfaces.
2019-10-03 17:04:56 +01:00
Nick Alcock 1ff6de0312 bfd, ld: add CTF section linking
This is quite complicated because the CTF section's contents depend on
the final contents of the symtab and strtab, because it has two sections
whose contents are shuffled to be in 1:1 correspondence with the symtab,
and an internal strtab that gets deduplicated against the ELF strtab
(with offsets adjusted to point into the ELF strtab instead).  It is
also compressed if large enough, so its size depends on its contents!

So we cannot construct it as early as most sections: we cannot even
*begin* construction until after the symtab and strtab are finalized.
Thankfully there is already one section treated similarly: compressed
debugging sections: the only differences are that compressed debugging
sections have extra handling to deal with their changing name if
compressed (CTF sections are always called ".ctf" for now, though we
have reserved ".ctf.*" against future use), and that compressed
debugging sections have previously-uncompressed content which has to be
stashed away for later compression, while CTF sections have no content
at all until we generate it (very late).

BFD also cannot do the link itself: libctf knows how to do it, and BFD
cannot call libctf directly because libctf already depends on bfd for
file I/O.  So we have to use a pair of callbacks, one, examine_strtab,
which allows a caller to examine the symtab and strtab after
finalization (called from elf_link_swap_symbols_out(), right before the
symtabs are written, and after the strtab has been finalized), and one
which actually does the emission (called emit_ctf simply because it is
grouped with a bunch of section-specific late-emission function calls at
the bottom of bfd_elf_final_link, and a section-specific name seems best
for that).  emit_ctf is actually called *twice*: once from lang_process
if the emulation suggests that this bfd target does not examine the
symtab or strtab, and once via a bfd callback if it does.  (This means
that non-ELF targets still get CTF emitted, even though the late CTF
emission stage is never called for them).

v2: merged with non-ELF support patch: slight commit message
    adjustments.
v3: do not spend time merging CTF, or crash, if the CTF section is
    explicitly discarded.  Do not try to merge or compress CTF unless
    linking.
v4: add CTF_COMPRESSION_THRESHOLD.  Annul the freed input ctf_file_t's
    after writeout: set SEC_IN_MEMORY on the output contents so a future
    bfd enhancement knows it could free it.  Add SEC_LINKER_CREATED |
    SEC_KEEP to avoid having to add .ctf to the linker script.  Drop
    now-unnecessary ldlang.h-level elf-bfd.h include and hackery around
    it.  Adapt to elf32.em->elf.em and elf-generic.em->ldelf*.c
    changes.
v5: fix tabdamage.  Drop #inclusions in .h files: include in .c files,
    .em files, and use struct forwards instead.  Use bfd_section_is_ctf
    inline function rather than SECTION_IS_CTF macro.  Move a few
    comments.

	* Makefile.def (dependencies): all-ld depends on all-libctf.
	* Makefile.in: Regenerated.

include/
	* bfdlink.h (elf_strtab_hash): New forward.
	(elf_sym_strtab): Likewise.
	(struct bfd_link_callbacks <examine_strtab>): New.
	(struct bfd_link_callbacks <emit_ctf>): Likewise.

bfd/
	* elf-bfd.h (bfd_section_is_ctf): New inline function.
	* elf.c (special_sections_c): Add ".ctf".
	(assign_file_positions_for_non_load_sections): Note that
	compressed debugging sections etc are not assigned here.  Treat
	CTF sections like SEC_ELF_COMPRESS sections when is_linker_output:
	sh_offset -1.
	(assign_file_positions_except_relocs): Likewise.
	(find_section_in_list): Note that debugging and CTF sections, as
	well as reloc sections, are assigned later.
	(_bfd_elf_assign_file_positions_for_non_load): CTF sections get
	their size and contents updated.
	(_bfd_elf_set_section_contents): Skip CTF sections: unlike
	compressed sections, they have no uncompressed content to copy at
	this stage.
	* elflink.c (elf_link_swap_symbols_out): Call the examine_strtab
	callback right before the strtab is written out.
	(bfd_elf_final_link): Don't cache the section contents of CTF
	sections: they are not populated yet.  Call the emit_ctf callback
	right at the end, after all the symbols and strings are flushed
	out.

ld/
	* ldlang.h: (struct lang_input_statement_struct): Add the_ctf.
	(struct elf_sym_strtab): Add forward.
	(struct elf_strtab_hash): Likewise.
	(ldlang_ctf_apply_strsym): Declare.
	(ldlang_write_ctf_late): Likewise.
	* ldemul.h (ldemul_emit_ctf_early): New.
	(ldemul_examine_strtab_for_ctf): Likewise.
	(ld_emulation_xfer_type) <emit_ctf_early>: Likewise.
	(ld_emulation_xfer_type) <examine_strtab_for_ctf>: Likewise.
	* ldemul.c (ldemul_emit_ctf_early): New.
	(ldemul_examine_strtab_for_ctf): Likewise.
	* ldlang.c: Include ctf-api.h.
	(CTF_COMPRESSION_THRESHOLD): New.
	(ctf_output): New. Initialized in...
	(ldlang_open_ctf): ... this new function.  Open all the CTF
	sections in the input files: mark them non-loaded and empty
	so as not to copy their contents to the output, but linker-created
	so the section gets created in the target.
	(ldlang_merge_ctf): New, merge types via ctf_link_add_ctf and
	ctf_link.
	(ldlang_ctf_apply_strsym): New, an examine_strtab callback: wrap
	ldemul_examine_strtab_for_ctf.
	(lang_write_ctf): New, write out the CTF section.
	(ldlang_write_ctf_late): New, late call via bfd's emit_ctf hook.
	(lang_process): Call ldlang_open_ctf, ldlang_merge_ctf, and
	lang_write_ctf.
	* ldmain.c (link_callbacks): Add ldlang_ctf_apply_strsym,
	ldlang_write_ctf_late.
	* emultempl/aix.em: Add ctf-api.h.
	* emultempl/armcoff.em: Likewise.
	* emultempl/beos.em: Likewise.
	* emultempl/elf.em: Likewise.
	* emultempl/generic.em: Likewise.
	* emultempl/linux.em: Likewise.
	* emultempl/msp430.em: Likewise.
	* emultempl/pe.em: Likewise.
	* emultempl/pep.em: Likewise.
	* emultempl/ticoff.em: Likewise.
	* emultempl/vanilla.em: Likewise.
	* ldcref.c: Likewise.
	* ldctor.c: Likewise.
	* ldelf.c: Likewise.
	* ldelfgen.c: Likewise.
	* ldemul.c: Likewise.
	* ldexp.c: Likewise.
	* ldfile.c: Likewise.
	* ldgram.c: Likewise.
	* ldlex.l: Likewise.
	* ldmain.c: Likewise.
	* ldmisc.c: Likewise.
	* ldver.c: Likewise.
	* ldwrite.c: Likewise.
	* lexsup.c: Likewise.
	* mri.c: Likewise.
	* pe-dll.c: Likewise.
	* plugin.c: Likewise.

	* ldelfgen.c (ldelf_emit_ctf_early): New.
	(ldelf_examine_strtab_for_ctf): tell libctf about the symtab and
	strtab.
	(struct ctf_strsym_iter_cb_arg): New, state to do so.
	(ldelf_ctf_strtab_iter_cb): New: tell libctf about
	each string in the strtab in turn.
	(ldelf_ctf_symbols_iter_cb): New, tell libctf
	about each symbol in the symtab in turn.
	* ldelfgen.h (struct elf_sym_strtab): Add forward.
	(struct elf_strtab_hash): Likewise.
	(struct ctf_file): Likewise.
	(ldelf_emit_ctf_early): Declare.
	(ldelf_examine_strtab_for_ctf): Likewise.
	* emultempl/elf-generic.em (LDEMUL_EMIT_CTF_EARLY): Set it.
	(LDEMUL_EXAMINE_STRTAB_FOR_CTF): Likewise.
	* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
	emit_ctf_early and examine_strtab_for_ctf, NULL by default.
	* emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/vanilla.em (ld_vanilla_emulation): Likewise.

	* Makefile.am: Pull in libctf (and zlib, a transitive requirement
	for compressed CTF section emission).  Pass it on to DejaGNU.
	* configure.ac: Add AM_ZLIB.
	* aclocal.m4: Added zlib.m4.
	* Makefile.in: Regenerated.
	* testsuite/ld-bootstrap/bootstrap.exp: Use it when relinking ld.
2019-10-03 17:04:56 +01:00
Nick Alcock 22ccb84954 bfd: new functions for getting strings out of a strtab
The CTF linking process wants to deduplicate the CTF strtab against the
ELF strtab, for which it needs to know the number of strings in the
strtab and it needs to be able to extract them one by one.

The BFD strtab functions only support returning the
size-or-section-length of the strtab (with _bfd_elf_strtab_size)
and returning the offset (but not string!) and decrementing the refcount
at the same time.

So add new functions _bfd_elf_strtab_len (that just returns the length
in strings of the strtab, never the section size) and bfd_elf_strtab_str
(which returns the string at a given strtab index, and its offset,
without touching the refcount).

It is probably a mistake to use _bfd_elf_strtab_str in particular before
_bfd_elf_strtab_finalize is called, and will not produce useful output
if you do so.

v5: fix tabdamage.

bfd/
	* elf-strtab.c (_bfd_elf_strtab_len): New.
	(_bfd_elf_strtab_str): Likewise.
	* bfd-elf.h: Declare them.
2019-10-03 17:04:55 +01:00
Nick Alcock f046147d59 libctf: actually close bfds we have opened
When we do a ctf_fdopen, we open things via bfd_fdopenr and set up a
hook to close the bfd again... but then we never actually call that hook
from anywhere, so we eventually leak every bfd we open.

Fix this by calling the hook (if set) in ctf_arc_close.

New in v3.

libctf/
	* ctf-archive.c (ctf_arc_close): Call ctfi_bfd_close if set.
	* ctf-open-bfd.c (ctf_bfdclose): Fix comment.
2019-10-03 17:04:55 +01:00
Nick Alcock edc8bbe90b libctf: bfd-open: mark the bfd as cacheable
Without this, the FD is only closed when the CTF file is, leading to
running out of fds on (e.g.) very large links.

New in v3.

libctf/
	* ctf-open-bfd.c (ctf_fdopen): Call bfd_set_cacheable.
2019-10-03 17:04:55 +01:00
Nick Alcock 7e97445a5a libctf: get rid of a disruptive public include of <sys/param.h>
This hoary old header defines things like MAX that users of libctf might
perfectly reasonably define themselves.

The CTF headers do not need it: move it into libctf/ctf-impl.h instead.

include/
	* ctf-api.h (includes): No longer include <sys/param.h>.
libctf/
	* ctf-impl.h (includes): Include <sys/param.h> here.
2019-10-03 17:04:55 +01:00
Nick Alcock 5ae6af75b5 libctf: eschew C99 for loop initial declarations
We shouldn't use these, since binutils doesn't require a C99-capable
compiler yet.

New in v3.
v5: fix tabdamage.

libctf/
	* ctf-open.c (flip_lbls): Eschew for-loop initial declarations.
	(flip_objts): Likewise.
	(flip_vars): Likewise.
	(flip_types): Likewise.
2019-10-03 17:04:55 +01:00
Nick Alcock 1820745a0a libctf: don't leak hash keys or values on value replacement
When a ctf_dynhash_insert() finds a slot already existing, it should
call the key and value free functions on the existing key and value and
move the passed-in key into place, so that the lifetime rules for hash
keys are always the same no matter whether the key existed or not but
neither are the keys or values leaked.

New in v3.
v5: fix tabdamage.

libctf/
	* ctf-hash.c (ctf_hashtab_insert): Pass in the key and value
	freeing functions: if set, free the key and value if the slot
	already exists.  Always reassign the key.
	(ctf_dynhash_insert): Adjust call appropriately.
	(ctf_hash_insert_type): Likewise.
2019-10-03 17:04:55 +01:00
Nick Alcock 5de9eada3b libctf: teach ctf_add_type how forwards work
This machinery has been broken for as long as Solaris has existed.
Forwards are meant to encode "struct foo;", "enum foo;" or "union
foo;".  Obviously these all exist in distinct namespaces, so forwards
store the type kind they forward to in their ctt_type member
(which makes conceptual sense if you squint at it).  The addition
machinery uses this to promote forwards to the appropriate type as
needed.

Unfortunately ctf_add_type does not: it checks the global namespace
(which is always wrong), and so fails with a spurious conflict if you
have, say, a typedef and then a forward comes along with the same name,
even if it's a forward to something like a struct.  (This was observed
with <libio.h>, which has "struct _IO_FILE;" and also
"typedef struct _IO_FILE _IO_FILE").  We should look at the recorded
type kind and look in the appropriate namespace.   We should also,
when creating the forward in the new container, use that type kind,
rather than just defaulting to CTF_K_STRUCT and hoping that what
eventually comes along is a struct.

This bug is as old as the first implementation of ctf_add_type in
Solaris.  But we also want a new feature for the linker, closely-related
and touching the same code so we add it here: not only do we want a
forward followed by a struct/union/enum to promote the forward, but
we want want a struct/union/enum followed by a forward to act as a NOP
and return the existing type, because when we're adding many files
in succession to a target link, there will often be already-promoted
forwards (in the shape of a struct/union/enum) that want to unify
with duplicate forwards coming from other object files.

v5: fix tabdamage.

libctf/
	* ctf-create.c (ctf_add_type): Look up and use the forwarded-to
	type kind.  Allow forwards to unify with pre-existing structs/
	unions/enums.
2019-10-03 17:04:55 +01:00
Nick Alcock 49ea9b450b libctf: add CU-mapping machinery
Once the deduplicator is capable of actually detecting conflicting types
with the same name (i.e., not yet) we will place such conflicting types,
and types that depend on them, into CTF dictionaries that are the child
of the main dictionary we usually emit: currently, this will lead to the
.ctf section becoming a CTF archive rather than a single dictionary,
with the default-named archive member (_CTF_SECTION, or NULL) being the
main shared dictionary with most of the types in it.

By default, the sections are named after the compilation unit they come
from (complete path and all), with the cuname field in the CTF header
providing further evidence of the name without requiring the caller to
engage in tiresome parsing.  But some callers may not wish the mapping
from input CU to output sub-dictionary to be purely CU-based.

The machinery here allows this to be freely changed, in two ways:

 - callers can call ctf_link_add_cu_mapping to specify that a single
   input compilation unit should have its types placed in some other CU
   if they conflict: the CU will always be created, even if empty, so
   the consuming program can depend on its existence.  You can map
   multiple input CUs to one output CU to force all their types to be
   merged together: if some of *those* types conflict, the behaviour is
   currently unspecified (the new deduplicator will specify it).

 - callers can call ctf_link_set_memb_name_changer to provide a function
   which is passed every CTF sub-dictionary name in turn (including
   _CTF_SECTION) and can return a new name, or NULL if no change is
   desired.  The mapping from input to output names should not map two
   input names to the same output name: if this happens, the two are not
   merged but will result in an archive with two members with the same
   name (technically valid, but it's hard to access the second
   same-named member: you have to do an iteration over archive members).

This is used by the kernel's ctfarchive machinery (not yet upstream) to
encode CTF under member names like {module name}.ctf rather than
.ctf.CU, but it is anticipated that other large projects may wish to
have their own storage for CTF outside of .ctf sections and may wish to
have new naming schemes that suit their special-purpose consumers.

New in v3.
v4: check for strdup failure.
v5: fix tabdamage.

include/
	* ctf-api.h (ctf_link_add_cu_mapping): New.
	(ctf_link_memb_name_changer_f): New.
	(ctf_link_set_memb_name_changer): New.

libctf/
	* ctf-impl.h (ctf_file_t) <ctf_link_cu_mappping>: New.
	<ctf_link_memb_name_changer>: Likewise.
	<ctf_link_memb_name_changer_arg>: Likewise.
	* ctf-create.c (ctf_update): Update accordingly.
	* ctf-open.c (ctf_file_close): Likewise.
	* ctf-link.c (ctf_create_per_cu): Apply the cu mapping.
	(ctf_link_add_cu_mapping): New.
	(ctf_link_set_memb_name_changer): Likewise.
	(ctf_change_parent_name): New.
	(ctf_name_list_accum_cb_arg_t) <dynames>: New, storage for names
	allocated by the caller's ctf_link_memb_name_changer.
	<ndynames>: Likewise.
	(ctf_accumulate_archive_names): Call the ctf_link_memb_name_changer.
	(ctf_link_write): Likewise (for _CTF_SECTION only): also call
	ctf_change_parent_name.  Free any resulting names.
2019-10-03 17:04:55 +01:00
Nick Alcock eabb7154df libctf: add linking of the variable section
The compiler describes the name and type of all file-scope variables in
this section.  Merging it at link time requires using the type mapping
added in the previous commit to determine the appropriate type for the
variable in the output, given its type in the input: we check the shared
container first, and if the type doesn't exist there, it must be a
conflicted type in the per-CU child, and the variable should go there
too.  We also put the variable in the per-CU child if a variable with
the same name but a different type already exists in the parent: we
ignore any such conflict in the child because CTF cannot represent such
things, nor can they happen unless a third-party linking program has
overridden the mapping of CU to CTF archive member name (using machinery
added in a later commit).

v3: rewritten using an algorithm that actually works in the case of
    conflicting names.  Some code motion from the next commit.  Set
    the per-CU parent name.
v4: check for strdup failure.
v5: fix tabdamage.

include/
	* ctf-api.h (ECTF_INTERNAL): New.

libctf/
	* ctf-link.c (ctf_create_per_cu): New, refactored out of...
	(ctf_link_one_type): ... here, with parent-name setting added.
	(check_variable): New.
	(ctf_link_one_variable): Likewise.
	(ctf_link_one_input_archive_member): Call it.
	* ctf-error.c (_ctf_errlist): Updated with new errors.
2019-10-03 17:04:55 +01:00
Nick Alcock 886453cbbc libctf: map from old to corresponding newly-added types in ctf_add_type
This lets you call ctf_type_mapping (dest_fp, src_fp, src_type_id)
and get told what type ID the corresponding type has in the target
ctf_file_t.  This works even if it was added by a recursive call, and
because it is stored in the target ctf_file_t it works even if we
had to add one type to multiple ctf_file_t's as part of conflicting
type handling.

We empty out this mapping after every archive is linked: because it maps
input to output fps, and we only visit each input fp once, its contents
are rendered entirely useless every time the source fp changes.

v3: add several missing mapping additions.  Add ctf_dynhash_empty, and
    empty after every input archive.
v5: fix tabdamage.

libctf/
	* ctf-impl.h (ctf_file_t): New field ctf_link_type_mapping.
	(struct ctf_link_type_mapping_key): New.
	(ctf_hash_type_mapping_key): Likewise.
	(ctf_hash_eq_type_mapping_key): Likewise.
	(ctf_add_type_mapping): Likewise.
	(ctf_type_mapping): Likewise.
	(ctf_dynhash_empty): Likewise.
	* ctf-open.c (ctf_file_close): Update accordingly.
	* ctf-create.c (ctf_update): Likewise.
	(ctf_add_type): Populate the mapping.
	* ctf-hash.c (ctf_hash_type_mapping_key): Hash a type mapping key.
	(ctf_hash_eq_type_mapping_key): Check the key for equality.
	(ctf_dynhash_insert): Fix comment typo.
	(ctf_dynhash_empty): New.
	* ctf-link.c (ctf_add_type_mapping): New.
	(ctf_type_mapping): Likewise.
	(empty_link_type_mapping): New.
	(ctf_link_one_input_archive): Call it.
2019-10-03 17:04:55 +01:00
Nick Alcock 72c83edd92 libctf: add the ctf_link machinery
This is the start of work on the core of the linking mechanism for CTF
sections.  This commit handles the type and string sections.

The linker calls these functions in sequence:

ctf_link_add_ctf: to add each CTF section in the input in turn to a
  newly-created ctf_file_t (which will appear in the output, and which
  itself will become the shared parent that contains types that all
  TUs have in common (in all link modes) and all types that do not
  have conflicting definitions between types (by default).  Input files
  that are themselves products of ld -r are supported, though this is
  not heavily tested yet.

ctf_link: called once all input files are added to merge the types in
  all the input containers into the output container, eliminating
  duplicates.

ctf_link_add_strtab: called once the ELF string table is finalized and
  all its offsets are known, this calls a callback provided by the
  linker which returns the string content and offset of every string in
  the ELF strtab in turn: all these strings which appear in the input
  CTF strtab are eliminated from it in favour of the ELF strtab:
  equally, any strings that only appear in the input strtab will
  reappear in the internal CTF strtab of the output.

ctf_link_shuffle_syms (not yet implemented): called once the ELF symtab
  is finalized, this calls a callback provided by the linker which
  returns information on every symbol in turn as a ctf_link_sym_t.  This
  is then used to shuffle the function info and data object sections in
  the CTF section into symbol table order, eliminating the index
  sections which map those sections to symbol names before that point.
  Currently just returns ECTF_NOTYET.

ctf_link_write: Returns a buffer containing either a serialized
  ctf_file_t (if there are no types with conflicting definitions in the
  object files in the link) or a ctf_archive_t containing a large
  ctf_file_t (the common types) and a bunch of small ones named after
  individual CUs in which conflicting types are found (containing the
  conflicting types, and all types that reference them).  A threshold
  size above which compression takes place is passed as one parameter.
  (Currently, only gzip compression is supported, but I hope to add lzma
  as well.)

Lifetime rules for this are simple: don't close the input CTF files
until you've called ctf_link for the last time.  We do not assume
that symbols or strings passed in by the callback outlast the
call to ctf_link_add_strtab or ctf_link_shuffle_syms.

Right now, the duplicate elimination mechanism is the one already
present as part of the ctf_add_type function, and is not particularly
good: it misses numerous actual duplicates, and the conflicting-types
detection hardly ever reports that types conflict, even when they do
(one of them just tends to get silently dropped): it is also very slow.
This will all be fixed in the next few weeks, but the fix hardly touches
any of this code, and the linker does work without it, just not as
well as it otherwise might.  (And when no CTF section is present,
there is no effect on performance, of course.  So only people using
a trunk GCC with not-yet-committed patches will even notice.  By the
time it gets upstream, things should be better.)

v3: Fix error handling.
v4: check for strdup failure.
v5: fix tabdamage.

include/
	* ctf-api.h (struct ctf_link_sym): New, a symbol in flight to the
	libctf linking machinery.
	(CTF_LINK_SHARE_UNCONFLICTED): New.
	(CTF_LINK_SHARE_DUPLICATED): New.
	(ECTF_LINKADDEDLATE): New, replacing ECTF_UNUSED.
	(ECTF_NOTYET): New, a 'not yet implemented' message.
	(ctf_link_add_ctf): New, add an input file's CTF to the link.
	(ctf_link): New, merge the type and string sections.
	(ctf_link_strtab_string_f): New, callback for feeding strtab info.
	(ctf_link_iter_symbol_f): New, callback for feeding symtab info.
	(ctf_link_add_strtab): New, tell the CTF linker about the ELF
	strtab's strings.
	(ctf_link_shuffle_syms): New, ask the CTF linker to shuffle its
	symbols into symtab order.
	(ctf_link_write): New, ask the CTF linker to write the CTF out.

libctf/
	* ctf-link.c: New file, linking of the string and type sections.
	* Makefile.am (libctf_a_SOURCES): Add it.
	* Makefile.in: Regenerate.

	* ctf-impl.h (ctf_file_t): New fields ctf_link_inputs,
	ctf_link_outputs.
	* ctf-create.c (ctf_update): Update accordingly.
	* ctf-open.c (ctf_file_close): Likewise.
	* ctf-error.c (_ctf_errlist): Updated with new errors.
2019-10-03 17:04:55 +01:00
Nick Alcock d18f9f1629 libctf: dump: check the right error values when dumping functions
We weren't correctly detecting when there were no functions to dump in
the function info table, because we were checking for ECTF_NOTYPEDAT,
which means there are no *data objects* to dump.

Adjust accordingly.

libctf/
	* ctf-dump.c (ctf_dump_funcs): Check the right error value.
2019-10-03 17:04:55 +01:00
Nick Alcock b4f0e09cd1 libctf: dump: support non-root type dumping
Use the recently-added ctf_type_iter_all function to iterate over
non-root types, too, indicating them via {....} surrounding the type
description in the dump.

libctf/
	* ctf-dump.c (ctf_dump): Use ctf_type_iter_all to dump types, not
	ctf_type_iter.
	(ctf_dump_type): Pass down the flag from ctf_type_iter_all.
	(ctf_dump_format_type): Add non-root-type { } notation.
	Add root flag to prototype.
	(ctf_dump_label): Adjust accordingly.
	(ctf_dump_objts): Likewise.
	(ctf_dump_var): Likewise.
2019-10-03 17:04:55 +01:00
Nick Alcock 3dde2c915e libctf: fix double-free on ctf_compress_write error path
We were freeing the compressed data buffer twice if compression failed.

v4: Fix commit message.
v5: fix tabdamage.

libctf/
	* ctf-create.c (ctf_compress_write): Fix double-free.
2019-10-03 17:04:55 +01:00
Nick Alcock 5537f9b9a3 libctf: write CTF files to memory, and CTF archives to fds
Before now, we've been able to write CTF files to gzFile descriptors or
fds, and CTF archives to named files only.

Make this a bit less irregular by allowing CTF archives to be written
to fds with the new function ctf_arc_write_fd: also allow CTF
files to be written to a new memory buffer via ctf_write_mem.

(It would be nice to complete things by adding a new function to write
CTF archives to memory, but this is too difficult to do given the short
time the linker is expected to be writing them out: we will transition
to a better format in format v4, though we will always support reading
CTF archives that are stored in .ctf sections.)

include/
	* ctf-api.h (ctf_arc_write_fd): New.
	(ctf_write_mem): Likewise.
	(ctf_gzwrite): Spacing fix.

libctf/
	* ctf-archive.c (ctf_arc_write): Split off, and reimplement in terms
	of...
	(ctf_arc_write_fd): ... this new function.
	* ctf-create.c (ctf_write_mem): New.
2019-10-03 17:04:55 +01:00
Nick Alcock d851ecd373 libctf: support getting strings from the ELF strtab
The CTF file format has always supported "external strtabs", which
internally are strtab offsets with their MSB on: such refs
get their strings from the strtab passed in at CTF file open time:
this is usually intended to be the ELF strtab, and that's what this
implementation is meant to support, though in theory the external
strtab could come from anywhere.

This commit adds support for these external strings in the ctf-string.c
strtab tracking layer.  It's quite easy: we just add a field csa_offset
to the atoms table that tracks all strings: this field tracks the offset
of the string in the ELF strtab (with its MSB already on, courtesy of a
new macro CTF_SET_STID), and adds a new function that sets the
csa_offset to the specified offset (plus MSB).  Then we just need to
avoid writing out strings to the internal strtab if they have csa_offset
set, and note that the internal strtab is shorter than it might
otherwise be.

(We could in theory save a little more time here by eschewing sorting
such strings, since we never actually write the strings out anywhere,
but that would mean storing them separately and it's just not worth the
complexity cost until profiling shows it's worth doing.)

We also have to go through a bit of extra effort at variable-sorting
time.  This was previously using direct references to the internal
strtab: it couldn't use ctf_strptr or ctf_strraw because the new strtab
is not yet ready to put in its usual field (in a ctf_file_t that hasn't
even been allocated yet at this stage): but now we're using the external
strtab, this will no longer do because it'll be looking things up in the
wrong strtab, with disastrous results.  Instead, pass the new internal
strtab in to a new ctf_strraw_explicit function which is just like
ctf_strraw except you can specify a ne winternal strtab to use.

But even now that it is using a new internal strtab, this is not quite
enough: it can't look up strings in the external strtab because ld
hasn't written it out yet, and when it does will write it straight to
disk.  Instead, when we write the internal strtab, note all the offset
-> string mappings that we have noted belong in the *external* strtab to
a new "synthetic external strtab" dynhash, ctf_syn_ext_strtab, and look
in there at ctf_strraw time if it is set.  This uses minimal extra
memory (because only strings in the external strtab that we actually use
are stored, and even those come straight out of the atoms table), but
let both variable sorting and name interning when ctf_bufopen is next
called work fine.  (This also means that we don't need to filter out
spurious ECTF_STRTAB warnings from ctf_bufopen but can pass them back to
the caller, once we wrap ctf_bufopen so that we have a new internal
variant of ctf_bufopen etc that we can pass the synthetic external
strtab to. That error has been filtered out since the days of Solaris
libctf, which didn't try to handle the problem of getting external
strtabs right at construction time at all.)

v3: add the synthetic strtab and all associated machinery.
v5: fix tabdamage.

include/
	* ctf.h (CTF_SET_STID): New.

libctf/
	* ctf-impl.h (ctf_str_atom_t) <csa_offset>: New field.
	(ctf_file_t) <ctf_syn_ext_strtab>: Likewise.
	(ctf_str_add_ref): Name the last arg.
	(ctf_str_add_external) New.
	(ctf_str_add_strraw_explicit): Likewise.
	(ctf_simple_open_internal): Likewise.
	(ctf_bufopen_internal): Likewise.

	* ctf-string.c (ctf_strraw_explicit): Split from...
	(ctf_strraw): ... here, with new support for ctf_syn_ext_strtab.
	(ctf_str_add_ref_internal): Return the atom, not the
	string.
	(ctf_str_add): Adjust accordingly.
	(ctf_str_add_ref): Likewise.  Move up in the file.
	(ctf_str_add_external): New: update the csa_offset.
	(ctf_str_count_strtab): Only account for strings with no csa_offset
	in the internal strtab length.
	(ctf_str_write_strtab): If the csa_offset is set, update the
	string's refs without writing the string out, and update the
	ctf_syn_ext_strtab.  Make OOM handling less ugly.
	* ctf-create.c (struct ctf_sort_var_arg_cb): New.
	(ctf_update): Handle failure to populate the strtab.  Pass in the
	new ctf_sort_var arg.  Adjust for ctf_syn_ext_strtab addition.
	Call ctf_simple_open_internal, not ctf_simple_open.
	(ctf_sort_var): Call ctf_strraw_explicit rather than looking up
	strings by hand.
	* ctf-hash.c (ctf_hash_insert_type): Likewise (but using
	ctf_strraw).  Adjust to diagnose ECTF_STRTAB nonetheless.
	* ctf-open.c (init_types): No longer filter out ECTF_STRTAB.
	(ctf_file_close): Destroy the ctf_syn_ext_strtab.
	(ctf_simple_open): Rename to, and reimplement as a wrapper around...
	(ctf_simple_open_internal): ... this new function, which calls
	ctf_bufopen_internal.
	(ctf_bufopen): Rename to, and reimplement as a wrapper around...
	(ctf_bufopen_internal): ... this new function, which sets
	ctf_syn_ext_strtab.
2019-10-03 17:04:55 +01:00
Nick Alcock 0ac6231298 libctf: Add iteration over non-root types
The existing function ctf_type_iter lets you iterate over root-visible
types (types you can look up by name).  There is no way to iterate over
non-root-visible types, which is troublesome because both the linker
and dumper want to do that.

So add a new function that can do it: the callback it takes accepts
an extra parameter which indicates whether the type is root-visible
or not.

include/
	* ctf-api.h (ctf_type_all_f): New.
	(ctf_type_iter_all): New.

libctf/
	* ctf_types.c (ctf_type_iter_all): New.
2019-10-03 17:04:55 +01:00
Nick Alcock 6b88d7d70c binutils: objdump does not take --ctf-symbols or --ctf-strings options
libctf figures out what to load itself, with no overriding currently
possible, so remove the documentation of these nonexistent options.
2019-10-03 17:04:55 +01:00
Nick Alcock 616febde0c binutils: readelf: when dumping CTF, load strtab and symtab automatically
We were only loading them when explicitly requested, which leads to
strings that point off into empty space (into the non-loaded "external"
ELF string table).  Avoid this unfortunate consequence by loading the
strtab and symtab by default, unless a blank name is given.

binutils/
	* readelf.c (dump_ctf_symtab_name): Give default value.
	(dump_ctf_strtab_name): Likewise.
	(dump_section_as_ctf): Allow for the null string.
2019-10-03 17:04:55 +01:00
Nick Alcock 2db912ba1a libctf: add the object index and function index sections
No code handles these yet, but our latest GCC patches are generating
them, so we have to be ready for them or erroneously conclude that we
have file corruption.

(This simultaneously fixes a longstanding bug, concealed because nothing
was generating anything in the object or function info sections, where
the end of the section was being tested against the wrong thing: it
would have walked over the entire contents of the variable section and
treated them as part of the function info section.  This had to change
now anyway because the new sections have landed in between.)

include/
	* ctf.h: Add object index and function index sections.  Describe
	them. Improve the description of the variable section and clarify
	the constraints on backward-pointing type nodes.
	(ctf_header): Add cth_objtidxoff, cth_funcidxoff.

libctf/
	* ctf-open.c (init_symtab): Check for overflow against the right
	section.
	(upgrade_header): Set cth_objtidxoff, cth_funcidxoff to zero-length.
	(upgrade_types_v1): Note that these sections are not checked.
	(flip_header): Endian-swap the header fields.
	(flip_ctf): Endian-swap the sections.
	(flip_objts): Update comment.
	(ctf_bufopen): Check header offsets and alignment for validity.
2019-10-03 17:04:55 +01:00
Nick Alcock 6d5944fca6 libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections
The code in ctf_bfdopen_ctfsect (which is the ultimate place where you
end up if you use ctf_open to open a CTF file and pull in the ELF string
and symbol tables) was written before it was possible to actually test
it, since the linker was not written.  Now it is, it turns out that the
previous code was completely nonfunctional: it assumed that you could
load the symbol table via bfd_section_from_elf_index (...,elf_onesymtab())
and the string table via bfd_section_from_elf_index on the sh_link.

Unfortunately BFD loads neither of these sections in the conventional
fashion it uses for most others: the symbol table is immediately
converted into internal form (which is useless for our purposes, since
we also have to work in the absence of BFD for readelf, etc) and the
string table is loaded specially via bfd_elf_get_str_section which is
private to bfd/elf.c.

So make this function public, export it in elf-bfd.h, and use it from
libctf, which does something similar to what bfd_elf_sym_name and
bfd_elf_string_from_elf_section do.  Similarly, load the symbol table
manually using bfd_elf_get_elf_syms and throw away the internal form
it generates for us (we never use it).

BFD allocates the strtab for us via bfd_alloc, so we can leave BFD to
deallocate it: we allocate the symbol table ourselves before calling
bfd_elf_get_elf_syms, so we still have to free it.

Also change the rules around what you are allowed to provide: It is
useful to provide a string section but no symbol table, because CTF
sections can legitimately have no function info or data object sections
while relying on the ELF strtab for some of their strings.  So allow
that combination.

v4: adjust to upstream changes.  ctf_bfdopen_ctfsect's first parameter
    is potentially unused again (if BFD is not in use for this link
    due to not supporting an ELF target).
v5: fix tabdamage.

bfd/
	* elf-bfd.h (bfd_elf_get_str_section): Add.
	* elf.c (bfd_elf_get_str_section): No longer static.

libctf/
	* ctf-open-bfd.c: Add <assert.h>.
	(ctf_bfdopen_ctfsect): Open string and symbol tables using
	techniques borrowed from bfd_elf_sym_name.
	(ctf_new_archive_internal): Improve comment.
	* ctf-archive.c (ctf_arc_close): Do not free the ctfi_strsect.
	* ctf-open.c (ctf_bufopen): Allow opening with a string section but
	no symbol section, but not vice versa.
2019-10-03 17:04:55 +01:00
Nick Alcock 9b32cba44d libctf, binutils: dump the CTF header
The CTF header has before now been thrown away too soon to be dumped
using the ctf_dump() machinery used by objdump and readelf: instead, a
kludge involving debugging-priority dumps of the header offsets on every
open was used.

Replace this with proper first-class dumping machinery just like
everything else in the CTF file, and have objdump and readelf use it.
(The dumper already had an enum value in ctf_sect_names_t for this
purpose, waiting to be used.)

v5: fix tabdamage.

libctf/
	* ctf-impl.h (ctf_file_t): New field ctf_openflags.
	* ctf-open.c (ctf_bufopen): Set it.  No longer dump header offsets.
	* ctf-dump.c (dump_header): New function, dump the CTF header.
	(ctf_dump): Call it.
	(ctf_dump_header_strfield): New function.
	(ctf_dump_header_sectfield): Likewise.

binutils/
	* objdump.c (dump_ctf_archive_member): Dump the CTF header.
	* readelf.c (dump_section_as_ctf): Likewise.
2019-10-03 17:04:55 +01:00
Nick Alcock fd55eae84d libctf: allow the header to change between versions
libctf supports dynamic upgrading of the type table as file format
versions change, but before now has not supported changes to the CTF
header.  Doing this is complicated by the baroque storage method used:
the CTF header is kept prepended to the rest of the CTF data, just as
when read from the file, and written out from there, and is
endian-flipped in place.

This makes accessing it needlessly hard and makes it almost impossible
to make the header larger if we add fields.  The general storage
machinery around the malloced ctf pointer (the 'ctf_base') is also
overcomplicated: the pointer is sometimes malloced locally and sometimes
assigned from a parameter, so freeing it requires checking to see if
that parameter was used, needlessly coupling ctf_bufopen and
ctf_file_close together.

So split the header out into a new ctf_file_t.ctf_header, which is
written out explicitly: squeeze it out of the CTF buffer whenever we
reallocate it, and use ctf_file_t.ctf_buf to skip past the header when
we do not need to reallocate (when no upgrading or endian-flipping is
required).  We now track whether the CTF base can be freed explicitly
via a new ctf_dynbase pointer which is non-NULL only when freeing is
possible.

With all this done, we can upgrade the header on the fly and add new
fields as desired, via a new upgrade_header function in ctf-open.
As with other forms of upgrading, libctf upgrades older headers
automatically to the latest supported version at open time.

For a first use of this field, we add a new string field cth_cuname, and
a corresponding setter/getter pair ctf_cuname_set and ctf_cuname: this
is used by debuggers to determine whether a CTF section's types relate
to a single compilation unit, or to all compilation units in the
program.  (Types with ambiguous definitions in different CUs have only
one of these types placed in the top-level shared .ctf container: the
rest are placed in much smaller per-CU containers, which have the shared
container as their parent.  Since CTF must be useful in the absence of
DWARF, we store the names of the relevant CUs ourselves, so the debugger
can look them up.)

v5: fix tabdamage.

include/
	* ctf-api.h (ctf_cuname): New function.
	(ctf_cuname_set): Likewise.
	* ctf.h: Improve comment around upgrading, no longer
	implying that v2 is the target of upgrades (it is v3 now).
	(ctf_header_v2_t): New, old-format header for backward
	compatibility.
	(ctf_header_t): Add cth_cuname: this is the first of several
	header changes in format v3.
libctf/
	* ctf-impl.h (ctf_file_t): New fields ctf_header, ctf_dynbase,
	ctf_cuname, ctf_dyncuname: ctf_base and ctf_buf are no longer const.
	* ctf-open.c (ctf_set_base): Preserve the gap between ctf_buf and
	ctf_base: do not assume that it is always sizeof (ctf_header_t).
	Print out ctf_cuname: only print out ctf_parname if set.
	(ctf_free_base): Removed, ctf_base is no longer freed: free
	ctf_dynbase instead.
	(ctf_set_version): Fix spacing.
	(upgrade_header): New, in-place header upgrading.
	(upgrade_types): Rename to...
	(upgrade_types_v1): ... this.  Free ctf_dynbase, not ctf_base.  No
	longer track old and new headers separately.  No longer allow for
	header sizes explicitly: squeeze the headers out on upgrade (they
	are preserved in fp->ctf_header).  Set ctf_dynbase, ctf_base and
	ctf_buf explicitly.  Use ctf_free, not ctf_free_base.
	(upgrade_types): New, also handle ctf_parmax updating.
	(flip_header): Flip ctf_cuname.
	(flip_types): Flip BUF explicitly rather than deriving BUF from
	BASE.
	(ctf_bufopen): Store the header in fp->ctf_header.  Correct minimum
	required alignment of objtoff and funcoff.  No longer store it in
	the ctf_buf unless that buf is derived unmodified from the input.
	Set ctf_dynbase where ctf_base is dynamically allocated. Drop locals
	that duplicate fields in ctf_file: move allocation of ctf_file
	further up instead.  Call upgrade_header as needed.  Move
	version-specific ctf_parmax initialization into upgrade_types.  More
	concise error handling.
	(ctf_file_close): No longer test for null pointers before freeing.
	Free ctf_dyncuname, ctf_dynbase, and ctf_header.  Do not call
	ctf_free_base.
	(ctf_cuname): New.
	(ctf_cuname_set): New.
	* ctf-create.c (ctf_update): Populate ctf_cuname.
	(ctf_gzwrite): Write out the header explicitly.  Remove obsolescent
	comment.
	(ctf_write): Likewise.
	(ctf_compress_write): Get the header from ctf_header, not ctf_base.
	Fix the compression length: fp->ctf_size never counted the CTF
	header.  Simplify the compress call accordingly.
2019-10-03 17:04:55 +01:00
Nick Alcock 083114f8ba libctf, include: ChangeLog format fixes
Double-spaces before email addresses were consistently missing.
2019-10-03 17:04:55 +01:00
Hans-Peter Nilsson a2230b5e62 libctf: make it compile for old glibc
With a glibc before 2.9 (such as 2.8), there's <endian.h> but no
htole64 or le64toh, so you get, compiling binutils for any target:

libtool: link: gcc -W -Wall -Wstrict-prototypes -Wmissing-prototypes \
-Wshadow -Werror -I/x/binutils/../zlib -g -O2 -o objdump \
objdump.o dwarf.o prdbg.o rddbg.o debug.o stabs.o rdcoff.o \
bucomm.o version.o filemode.o elfcomm.o  ../opcodes/.libs/libopcodes.a \
../libctf/libctf.a ../bfd/.libs/libbfd.a -L/x/obj/b/zlib -lz ../libiberty/libiberty.a -ldl
../libctf/libctf.a(ctf-archive.o): In function `ctf_archive_raw_iter_internal':
/x/src/libctf/ctf-archive.c:543: undefined reference to `le64toh'
/x/src/libctf/ctf-archive.c:550: undefined reference to `le64toh'
/x/src/libctf/ctf-archive.c:551: undefined reference to `le64toh'
/x/src/libctf/ctf-archive.c:551: undefined reference to `le64toh'
/x/src/libctf/ctf-archive.c:554: undefined reference to `le64toh'
../libctf/libctf.a(ctf-archive.o):/x/src/libctf/ctf-archive.c:545: more undefined references to `le64toh' follow
(etc)

Also, I see no bswap_identity_64 *anywhere* except in libctf/swap.h
(including current glibc) and I don't think calling an "identity"-
function is better than just plain "#define foo(x) (x)" anyway.
(Where does the idea of a bytestap.h bswap_identity_64 come from?)

Speaking of that, I should mention that I instrumented the condition
to observe that the WORDS_BIGENDIAN case passes too for a presumed
big-endian target and glibc-2.8: there is a bswap_64 present for that
version.  Curiously, no test-case regressed with that instrumentation.

For the record, constructing binary blobs using text source to run
tests on, can be done by linking to --oformat binary (with most ELF
targets), but I guess that's seen as unnecessary roundabout perhaps
checking in binary files in the test-suite would be ok these days.
[...]

[nca: trimmed commit log slightly, updated changelog]

v5: fix tabdamage.

libctf/
	* ctf-endian.h: Don't assume htole64 and le64toh are always
	present if HAVE_ENDIAN_H; also check if htole64 is defined.
	[!WORDS_BIGENDIAN] (htole64, le64toh): Define as identity,
	not bswap_identity_64.
2019-10-03 17:04:55 +01:00
Andrew Burgess d8c06f22a3 gdb: Don't ignore all SIGSTOP when the signal handler is set to pass
It was observed that in a multi-threaded application on GNU/Linux,
that if the user has set the SIGSTOP to be pass (using GDB's handle
command) then the inferior would hang upon hitting a breakpoint.

What happens is that when a thread hits the breakpoint GDB tries to
stop all of the other threads by sending them a SIGSTOP and setting
the stop_requested flag in the target_ops structure - this can be seen
in infrun.c:stop_all_threads.

GDB then waits for all of the other threads to stop.

When the SIGSTOP event arrives we eventually end up in
linux-nat.c:linux_nat_filter_event, which has the job of deciding if
the event we're looking at (the SIGSTOP arriving in this case) is
something that should be reported back to the core of GDB.

One of the final actions of this function is to check if we stopped
due to a signal, and if we did, and the signal has been set to 'pass'
by the user then we ignore the event and resume the thread.

This code already has some conditions in place that mean the event is
reported to GDB even if the signal is in the set of signals to be
passed to the inferior.

In this commit I extend this condition such that:

  If the signal is a SIGSTOP, and the thread's stop_requested flag is
  set (indicating we're waiting for the thread to stop with a SIGSTOP)
  then we should report this SIGSTOP to GDB and not pass it to the
  inferior.

With this change in place the test now passes.  Regression tested on
x86-64 GNU/Linux with no regressions.

gdb/ChangeLog:

	* linux-nat.c (linux_nat_filter_event): Don't ignore SIGSTOP if we
	have just sent the thread a SIGSTOP and are waiting for it to
	arrive.

gdb/testsuite/ChangeLog:

	* gdb.threads/stop-with-handle.c: New file.
	* gdb.threads/stop-with-handle.exp: New file.
2019-10-03 16:12:02 +01:00
Tom de Vries 3a56ed8668 [gdb/testsuite] Fix list-missing-source.exp with gcc 4.8
With gcc 4.8.1, we see this FAIL:
...
(gdb) PASS: gdb.base/list-missing-source.exp: list
info source^M
Current source file is outputs/gdb.base/list-missing-source/main.c^M
Source language is c.^M
Producer is GNU C 4.8.5 -mtune=generic -march=x86-64 -g -fno-stack-protector.^M
Compiled with DWARF 2 debugging format.^M
Does not include preprocessor macro info.^M
(gdb) FAIL: gdb.base/list-missing-source.exp: info source
...

The problem is that a "Compilation directory is <dir>" line is expected, but
this is missing due to the fact the the compilation unit for main.c doesn't
contain a DW_AT_comp_dir in the DW_TAG_compile_unit DIE.

Fix this by allowing the "Compilation directory" line to be missing.

Tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2019-10-03  Tom de Vries  <tdevries@suse.de>

	PR testsuite/25059
	* gdb.base/list-missing-source.exp: Allowing the "Compilation
	directory" line to be missing.
2019-10-03 16:22:09 +02:00
Tom de Vries 4227504491 [gdb/testsuite] Fix info-types.exp with gcc/g++ 4.8
The gdb.base/info-types.exp test-case FAILs with gcc/g++ 4.8 because the DWARF
record for the 'unsigned int' type is missing in the executables, while it is
present for gcc/g++ 7.4.1.

For a minimal example using gcc 7.4.1:
...
$ echo "enum enum_t { AA, BB, CC }; enum enum_t var;" > enum.c
$ gcc enum.c -c -g
...
we find that the enum type has DW_AT_encoding 'unsigned':
 <1><1d>: Abbrev Number: 2 (DW_TAG_enumeration_type)
    <1e>   DW_AT_name        : (indirect string, offset: 0x1f): enum_t
    <22>   DW_AT_encoding    : 7        (unsigned)
    <23>   DW_AT_byte_size   : 4
    <24>   DW_AT_type        : <0x3e>
    <28>   DW_AT_decl_file   : 1
    <29>   DW_AT_decl_line   : 1
    <2a>   DW_AT_sibling     : <0x3e>
...
and a DW_AT_type reference to the type 'unsigned int':
...
 <1><3e>: Abbrev Number: 4 (DW_TAG_base_type)
    <3f>   DW_AT_byte_size   : 4
    <40>   DW_AT_encoding    : 7        (unsigned)
    <41>   DW_AT_name        : (indirect string, offset: 0x26): unsigned int
...

With gcc 4.8.5 however, we have no 'unsigned' encoding, and no DW_AT_type:
...
 <1><1d>: Abbrev Number: 2 (DW_TAG_enumeration_type)
    <1e>   DW_AT_name        : (indirect string, offset: 0x1f): enum_t
    <22>   DW_AT_byte_size   : 4
    <23>   DW_AT_decl_file   : 1
    <24>   DW_AT_decl_line   : 1
    <25>   DW_AT_sibling     : <0x39>
...
as well as no record for 'unsigned int'.

Make the test-case pass with gcc/g++ 4.8 by making the presence of the
'unsigned int' type optional.

Tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2019-10-03  Tom de Vries  <tdevries@suse.de>

	PR testsuite/25059
	* gdb.base/info-types.exp: Make the presence of the 'unsigned int'
	type optional.
2019-10-03 15:02:58 +02:00
Andrew Burgess a8b3b8e93e gdb: Remove whitespace in 'std::vector <...>'
In the following 3 commits:

    commit df07e2c772
    Date:   Wed Sep 25 16:10:50 2019 +0100

        gdb: Remove a use of VEC from dwarf2read.{c,h}

    commit 554ac434b0
    Date:   Thu Sep 19 13:17:59 2019 -0400

        gdb: Change a VEC to std::vector in btrace.{c,h}

    commit 46f29a9a26
    Date:   Mon Sep 16 09:12:27 2019 -0400

        gdb: Remove a VEC from gdbsupport/btrace-common.h

I incorrectly wrote 'std::vector <...>' instead of 'std::vector<...>',
this commit fixes this mistake.  There should be no user visible
changes after this commit.

gdb/ChangeLog:

	* btrace.c (btrace_add_pc): Remove whitespace before the template
	parameter in 'std::vector <...>'.
	(parse_xml_btrace_block): Likewise.
	(btrace_maint_decode_pt): Likewise.
	(btrace_maint_update_packets): Likewise.
	(btrace_maint_print_packets): Likewise.
	* btrace.h (struct btrace_maint_info): Likewise.
	* dwarf2read.c (struct type_unit_group): Likewise.
	(build_type_psymtabs_reader): Likewise.
	* gdbsupport/btrace-common.c (btrace_data_append): Likewise.
	* gdbsupport/btrace-common.h (struct btrace_data_bts): Likewise.
	* nat/linux-btrace.c (perf_event_read_bts): Likewise.
2019-10-03 09:42:42 +01:00
Tom de Vries 4d825eab2d [gdb] Fix set/show style metadata help text
There's a recent regression:
...
FAIL: gdb.gdb/unittest.exp: maintenance selftest
...

In more detail:
...
Running selftest help_doc_invariants.^M
help doc broken invariant: command 'set style metadata' help doc first line \
  is not terminated with a '.' character^M
help doc broken invariant: command 'show style metadata' help doc first line \
  is not terminated with a '.' character^M
Self test failed: self-test failed at gdb/unittests/help-doc-selftests.c:95^M
...

Fix this by adding a '.' at the end of the first line of the help text for
set/show style metadata.

Tested on x86_64-linux.

gdb/ChangeLog:

2019-10-03  Tom de Vries  <tdevries@suse.de>

	* cli/cli-style.c (_initialize_cli_style): Adding a '.' at the end of
	the first line of the help text for set/show style metadata.
2019-10-03 10:15:39 +02:00
GDB Administrator 03ba321aaa Automatic date update in version.in 2019-10-03 00:00:20 +00:00
Christian Biesinger 3e6ec53ac1 Convert boolean globals in server.c to bool
Converts the int globals to bool.

gdb/gdbserver/ChangeLog:

2019-10-02  Christian Biesinger  <cbiesinger@google.com>

	* server.c (server_waiting): Change to bool.
	(extended_protocol): Likewise.
	(response_needed): Likewise.
	(exit_requested): Likewise.
	(run_once): Likewise.
	(report_no_resumed): Likewise.
	(non_stop): Likewise.
	(disable_packet_vCont): Likewise.
	(disable_packet_Tthread): Likewise.
	(disable_packet_qC): Likewise.
	(disable_packet_qfThreadInfo): Likewise.
	(handle_general_set): Update.
	(handle_detach): Update.
	(handle_monitor_command): Update.
	(handle_query): Update.
	(captured_main): Update.
	(process_serial_event): Update.
	* server.h (server_waiting): Change to bool.
	(disable_packet_vCont): Likewise.
	(disable_packet_Tthread): Likewise.
	(disable_packet_qC): Likewise.
	(disable_packet_qfThreadInfo): Likewise.
	(run_once): Likewise.
	(non_stop): Likewise.
	* target.c (target_stop_and_wait): Update.
2019-10-02 14:48:53 -05:00
Tom Tromey 80fd282641 Fix type of startup_with_shell in gdbserver
startup_with_shell was changed to be of "bool" type, but I noticed
that the definition in gdbserver disagreed.  This disagreement caused
some regressions on a big-endian machine.

This patch removes the redundant declaration and definition of
startup_with_shell and ensures that such clashes will be diagnosed.

This moves the declaration to common-inferior.h, and introduces a new
common-inferior.c, as suggested by Pedro.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* Makefile.in (COMMON_SFILES): Add common-inferior.c.
	* gdbsupport/common-inferior.c: New file.
	* infcmd.c (startup_with_shell): Don't define.
	* nat/fork-inferior.h (startup_with_shell): Don't declare.
	* gdbsupport/common-inferior.h (startup_with_shell): Declare.
	* inferior.h (startup_with_shell): Don't declare.

gdb/gdbserver/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* Makefile.in (SFILES): Add common-inferior.c.
	(OBS): Add common-inferior.o.
	* server.c (startup_with_shell): Don't define.
2019-10-02 13:43:41 -06:00
Christian Biesinger 700545387d Add missing includes to gdb_assert.h and gdb_string_view.h
gdb::string_view uses gdb_assert, so it should include that header.
And gdb_assert uses internal_error, so it should include errors.h.

gdb/ChangeLog:

2019-10-02  Christian Biesinger  <cbiesinger@google.com>

	* gdbsupport/gdb_assert.h: Include errors.h.
	* gdbsupport/gdb_string_view.h: Include gdb_assert.h.
2019-10-02 13:33:09 -05:00
Andreas Arnez 9ef62df072 gdb/testsuite: Fix py-format-string.exp on big-endian platforms
GDB's py-format-string test case depends on endianness.  In particular it
relies on the first byte of the machine representation of 42 (as an int)
to be 42 as well.  While this is indeed the case for little-endian
machines, big-endian machines store a zero in the first byte instead.  The
wrong assumption leads to lots of FAILs on such architectures.

Fix this by filling the affected union with bytes of the same value, such
that endianness does not matter.  Use the value 42, to keep the character
in the first byte unchanged.

gdb/testsuite/ChangeLog:

	* gdb.python/py-format-string.c (string.h): New include.
	(main): Fill a_struct_with_union.the_union.an_int with bytes of
	the same value, for endianness-independence.
	* gdb.python/py-format-string.exp (default_regexp_dict)
	(test_pretty_structs, test_format): Adjust expected output to the
	changed initialization.
2019-10-02 20:01:44 +02:00
Tom Tromey 37f6a7f456 Add $_ada_exception convenience variable
This adds the $_ada_exception convenience variable.  It is set by the
Ada exception catchpoints, and holds the address of the exception
currently being thrown.  This is useful because it allows more
fine-grained filtering of exceptions than is possible using the
existing "catch" syntax.

This also simplifies Ada catchpoints somewhat; because the catchpoint
must now carry the "kind", it's possible to remove many helper
functions.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* NEWS: Add $_ada_exception entry.
	* ada-lang.c (struct ada_catchpoint): Add constructor.
	<m_kind>: New member.
	(allocate_location_exception, re_set_exception): Remove
	"ex" parameter.
	(should_stop_exception): Compute $_ada_exception.
	(check_status_exception, print_it_exception)
	(print_one_exception, print_mention_exception): Remove
	"ex" parameter.
	(allocate_location_catch_exception, re_set_catch_exception)
	(check_status_exception, print_it_catch_exception)
	(print_one_catch_exception, print_mention_catch_exception)
	(print_recreate_catch_exception)
	(allocate_location_catch_exception_unhandled)
	(re_set_catch_exception_unhandled)
	(check_status_exception, print_it_catch_exception_unhandled)
	(print_one_catch_exception_unhandled)
	(print_mention_catch_exception_unhandled)
	(print_recreate_catch_exception_unhandled)
	(allocate_location_catch_assert, re_set_catch_assert)
	(check_status_assert, print_it_catch_assert)
	(print_one_catch_assert, print_mention_catch_assert)
	(print_recreate_catch_assert)
	(allocate_location_catch_handlers, re_set_catch_handlers)
	(check_status_handlers, print_it_catch_handlers)
	(print_one_catch_handlers, print_mention_catch_handlers)
	(print_recreate_catch_handlers): Remove.
	(create_ada_exception_catchpoint): Update.
	(initialize_ada_catchpoint_ops): Update.

gdb/doc/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* gdb.texinfo (Set Catchpoints, Convenience Vars): Document
	$_ada_exception.

gdb/testsuite/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* gdb.ada/catch_ex_std.exp: Add $_ada_exception test.
2019-10-02 09:53:17 -06:00
Tom Tromey fccf9de11f Back out earlier Ada exception change
commit 2ff0a9473 (Fix "catch exception" with dynamic linking) changed
how ada-lang.c creates expressions to determine if an exception
catchpoint should stop.

That patch is no longer needed now that copy relocations are handled
more directly.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* ada-lang.c (ada_lookup_simple_minsyms): Remove.
	(create_excep_cond_exprs): Simplify exception string computation.
	(ada_exception_catchpoint_cond_string): Likewise.
2019-10-02 09:53:17 -06:00
Pedro Alves 1d58d6a26c Make print-file-var.exp test attribute visibility hidden, dlopen, and main symbol
Make gdb.base/print-file-var.exp test all combinations of:

  - attribute hidden in the this_version_id symbols or not
  - dlopen or not
  - this_version_id symbol in main file or not
  - C++

gdb/testsuite/ChangeLog
2019-10-02  Pedro Alves  <palves@redhat.com>
	    Andrew Burgess  <andrew.burgess@embecosm.com>

	* gdb.base/print-file-var-lib1.c: Include <stdio.h> and
	"print-file-var.h".
	(this_version_id) Use ATTRIBUTE_VISIBILITY.
	(get_version_1): Print this_version_id and its address.
	Add extern "C" wrappers around interface functions.
	* gdb.base/print-file-var-lib2.c: Include <stdio.h> and
	"print-file-var.h".
	(this_version_id) Use ATTRIBUTE_VISIBILITY.
	(get_version_2): Print this_version_id and its address.
	Add extern "C" wrappers around interface functions.
	* gdb.base/print-file-var-main.c: Include <dlfcn.h>, <assert.h>,
	<stddef.h> and "print-file-var.h".
	Add extern "C" wrappers around interface functions.
	[VERSION_ID_MAIN] (this_version_id): Define.
	(main): Define v0.  Use dlopen if SHLIB_NAME is defined.
	* gdb.base/print-file-var.h: Add some #defines to simplify setting
	up extern "C" blocks.
	* gdb.base/print-file-var.exp (test): New, factored out from top
	level.
	(top level): Test all combinations of attribute hidden or not,
	dlopen or not, and this_version_id symbol in main file or not.
	Compile tests as both C++ and C, make test names unique.
2019-10-02 09:53:17 -06:00
Tom Tromey 4b610737f0 Handle copy relocations
In ELF, if a data symbol is defined in a shared library and used by
the main program, it will be subject to a "copy relocation".  In this
scenario, the main program has a copy of the symbol in question, and a
relocation that tells ld.so to copy the data from the shared library.
Then the symbol in the main program is used to satisfy all references.

This patch changes gdb to handle this scenario.  Data symbols coming
from ELF shared libraries get a special flag that indicates that the
symbol's address may be subject to copy relocation.

I looked briefly into handling copy relocations by looking at the
actual relocations in the main program, but this seemed difficult to
do with BFD.

Note that no caching is done here.  Perhaps this could be changed if
need be; I wanted to avoid possible problems with either objfile
lifetimes and changes, or conflicts with the long-term (vapor-ware)
objfile splitting project.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* symmisc.c (dump_msymbols): Don't use MSYMBOL_VALUE_ADDRESS.
	* ada-lang.c (lesseq_defined_than): Handle
	LOC_STATIC.
	* dwarf2read.c (dwarf2_per_objfile): Add can_copy
	parameter.
	(dwarf2_has_info): Likewise.
	(new_symbol): Set maybe_copied on symbol when
	appropriate.
	* dwarf2read.h (dwarf2_per_objfile): Add can_copy
	parameter.
	<can_copy>: New member.
	* elfread.c (record_minimal_symbol): Set maybe_copied
	on symbol when appropriate.
	(elf_symfile_read): Update call to dwarf2_has_info.
	* minsyms.c (lookup_minimal_symbol_linkage): New
	function.
	* minsyms.h (lookup_minimal_symbol_linkage): Declare.
	* symtab.c (get_symbol_address, get_msymbol_address):
	New functions.
	* symtab.h (get_symbol_address, get_msymbol_address):
	Declare.
	(SYMBOL_VALUE_ADDRESS, MSYMBOL_VALUE_ADDRESS): Handle
	maybe_copied.
	(struct symbol, struct minimal_symbol) <maybe_copied>:
	New member.
2019-10-02 09:53:17 -06:00
Tom Tromey 1dd5885077 Make current_source_* per-program-space
This changes current_source_symtab and current_source_line to be
per-program-space.  This ensures that switching inferiors will
preserve the current "list" location for that inferior, and also
ensures that the default expression evaluation context always comes
with the current inferior.

No test case, because the latter problem crops up with an existing
gdb.multi test case once this entire series has been applied.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* source.c (struct current_source_location): New.
	(current_source_key): New global.
	(current_source_symtab, current_source_line)
	(current_source_pspace): Remove.
	(get_source_location): New function.
	(get_current_source_symtab_and_line)
	(set_default_source_symtab_and_line)
	(set_current_source_symtab_and_line)
	(clear_current_source_symtab_and_line, select_source_symtab)
	(info_source_command, print_source_lines_base)
	(info_line_command, search_command_helper, _initialize_source):
	Update.
2019-10-02 09:53:17 -06:00
Tom Tromey 5c281dbb24 Don't call decode_line_with_current_source from select_source_symtab
select_source_symtab currently calls decode_line_with_current_source.
However, this function iterates over all program spaces, and so it is
possible that it will return a "main" from some other program space.

This patch changes select_source_symtab to simply use the symbol it
already found in the current program space.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tromey@adacore.com>

	* source.c (select_source_symtab): Don't call
	decode_line_with_current_source.
2019-10-02 09:53:17 -06:00