1. Oct 04, 2019
    • Andrew Burgess's avatar
      gdb/testsuite: Reduce test name duplication in gdb.python tests · 082cce05
      Andrew Burgess authored
      This commit removes some, but not all, of the test name duplication
      within the gdb.python tests.  On my local machine this takes the
      number of duplicate test names in this set of tests from 174 to 85.
      It is possible that different setups might encounter more duplicate
      tests.
      
      gdb/testsuite/ChangeLog:
      
      	* gdb.python/py-parameter.exp: Make test names unique.
      	* gdb.python/py-template.exp: Likewise.
      	* gdb.python/py-value.exp: Likewise.
      082cce05
    • Andrew Burgess's avatar
      gdb/testsuite: Reduce test name duplication in gdb.base tests · 2b74ba5a
      Andrew Burgess authored
      This commit removes some, but not all, of the test name duplication
      within the gdb.base tests.  On my local machine this takes the number
      of duplicate test names in this set of tests from 454 to 145.  It is
      possible that different setups might encounter more duplicate tests.
      
      gdb/testsuite/ChangeLog:
      
      	* gdb.base/break-interp.exp: Reduce test name duplication.
      	* gdb.base/call-sc.exp: Likewise.
      	* gdb.base/callfuncs.exp: Likewise.
      	* gdb.base/charset.exp: Likewise.
      	* gdb.base/dump.exp: Likewise.
      	* gdb.base/ena-dis-br.exp: Likewise.
      	* gdb.base/relational.exp: Likewise.
      	* gdb.base/step-over-syscall.exp: Likewise.
      	* gdb.base/structs.exp: Likewise.
      2b74ba5a
    • Andrew Burgess's avatar
      gdb/testsuite: Make test names unique in gdb.linespec tests · 58eb20d5
      Andrew Burgess authored
      Make test names unique in the gdb.linespec tests.  On my local machine
      this removed 43 duplicate test names.  It is possible that different
      setups might still encounter some duplicates.
      
      gdb/testsuite/ChangeLog:
      
      	* gdb.linespec/explicit.exp: Make test names unique.
      	* gdb.linespec/ls-errs.exp: Likewise.
      58eb20d5
    • Andrew Burgess's avatar
      gdb/testsuite: Make test names unique in gdb.reverse tests · 4d421147
      Andrew Burgess authored
      Make test names unique in the gdb.reverse tests.  On my local machine
      this removed 825 duplicate test names.  It is possible that different
      setups might still encounter some duplicates.
      
      gdb/testsuite/ChangeLog:
      
      	* gdb.reverse/break-precsave.exp: Make test names unique.
      	* gdb.reverse/break-reverse.exp: Likewise.
      	* gdb.reverse/finish-precsave.exp: Likewise.
      	* gdb.reverse/finish-reverse.exp: Likewise.
      	* gdb.reverse/machinestate-precsave.exp: Likewise.
      	* gdb.reverse/machinestate.exp: Likewise.
      	* gdb.reverse/readv-reverse.exp: Likewise.
      	* gdb.reverse/recvmsg-reverse.exp: Likewise.
      	* gdb.reverse/sigall-precsave.exp: Likewise.
      	* gdb.reverse/sigall-reverse.exp: Likewise.
      	* gdb.reverse/step-indirect-call-thunk.exp: Likewise.
      	* gdb.reverse/watch-precsave.exp: Likewise.
      	* gdb.reverse/watch-reverse.exp: Likewise.
      4d421147
    • Nick Alcock's avatar
      libctf: fix tabdamage · fa56cdcd
      Nick Alcock authored
      A little tabdamage predating the linker patch series has crept in.
      
      New in v5.
      
      libctf/
      	* ctf-open.c (ctf_bufopen_internal): Fix tabdamage.
      	* ctf-types.c (ctf_type_lname): Likewise.
      fa56cdcd
    • Nick Alcock's avatar
      binutils: spaces -> tabs in CTF parts of objdump and readelf · d344b407
      Nick Alcock authored
      For readelf particularly, this is more or less whistling in the dark:
      there are hundreds of lines where spaces are used where tabs were used
      on adjacent lines.
      
      New in v5.
      
      binutils/
      	* objdump.c (main): Fix tabdamage.
      	* readelf.c (CTF_DUMP): Likewise.
      	(options): Likewise.
      	(dump_section_as_ctf): Likewise.
      d344b407
    • Nick Alcock's avatar
      libctf: fix refcount leak in ctf_import · ad613f1d
      Nick Alcock authored
      Calling ctf_import (fp, NULL) to cancel out a pre-existing import leaked
      the refcnt increment on the parent, so it could never be freed.
      
      New in v4.
      
      libctf/
      	* ctf-open.c (ctf_import): Do not leak a ctf_file_t ref on every
      	ctf_import after the first for a given file.
      ad613f1d
    • Nick Alcock's avatar
      libctf: make ctf_dump not crash on OOM · 9323dd86
      Nick Alcock authored
      ctf_dump calls ctf_str_append extensively but never checks to see if it
      returns NULL (on OOM).  If it ever does, we truncate the string we are
      appending to and leak it!
      
      Instead, create a variant of ctf_str_append that returns the *original
      string* on OOM, and use it in ctf-dump.  It is far better to omit a tiny
      piece of a dump on OOM than to omit a bigger piece, and it is also
      better to do this in what is after all purely debugging code than it is
      to uglify ctf-dump.c with huge numbers of checks for the out-of-memory
      case.  Slightly truncated debugging output is better than no debugging
      output at all and an out-of-memory message.
      
      New in v4.
      
      libctf/
      	* ctf-impl.h (ctf_str_append_noerr): Declare.
      	* ctf-util.c (ctf_str_append_noerr): Define in terms of
      	ctf_str_append.
      	* ctf-dump.c (str_append): New, call it.
      	(ctf_dump_format_type): Use str_append, not ctf_str_append.
      	(ctf_dump_label): Likewise.
      	(ctf_dump_objts): Likewise.
      	(ctf_dump_funcs): Likewise.
      	(ctf_dump_var): Likewise.
      	(ctf_dump_member): Likewise.
      	(ctf_dump_type): Likewise.
      	(ctf_dump): Likewise.
      9323dd86
    • Nick Alcock's avatar
      libctf: remove ctf_malloc, ctf_free and ctf_strdup · de07e349
      Nick Alcock authored
      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.
      de07e349
    • Nick Alcock's avatar
      libctf: get the encoding of non-ints/fps in the dynamic space right · 9c1a2295
      Nick Alcock authored
      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.
      9c1a2295
    • Nick Alcock's avatar
      libctf: allow ctf_type_lname of a null pointer. · 1a6ab13e
      Nick Alcock authored
      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.
      1a6ab13e
    • Nick Alcock's avatar
      libctf: properly handle ctf_add_type of forwards and self-reffing structs · 99dc3ebd
      Nick Alcock authored
      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.
      99dc3ebd
    • Nick Alcock's avatar
      libctf: avoid the need to ever use ctf_update · 676c3ecb
      Nick Alcock authored
      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.
      676c3ecb
    • Nick Alcock's avatar
      libctf: handle nonrepresentable types at link time · 791915db
      Nick Alcock authored
      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.
      791915db
    • Nick Alcock's avatar
      objdump: get CTF parent importing right · fd86991b
      Nick Alcock authored
      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.
      fd86991b
    • Nick Alcock's avatar
      libctf: installable libctf as a shared library · 87279e3c
      Nick Alcock authored
      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.
      87279e3c
    • Nick Alcock's avatar
      bfd, ld: add CTF section linking · 1ff6de03
      Nick Alcock authored
      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.
      1ff6de03
    • Nick Alcock's avatar
      bfd: new functions for getting strings out of a strtab · 22ccb849
      Nick Alcock authored
      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.
      22ccb849
    • Nick Alcock's avatar
      libctf: actually close bfds we have opened · f046147d
      Nick Alcock authored
      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.
      f046147d
    • Nick Alcock's avatar
      libctf: bfd-open: mark the bfd as cacheable · edc8bbe9
      Nick Alcock authored
      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.
      edc8bbe9
    • Nick Alcock's avatar
      libctf: get rid of a disruptive public include of <sys/param.h> · 7e97445a
      Nick Alcock authored
      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.
      7e97445a
    • Nick Alcock's avatar
      libctf: eschew C99 for loop initial declarations · 5ae6af75
      Nick Alcock authored
      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.
      5ae6af75
    • Nick Alcock's avatar
      libctf: don't leak hash keys or values on value replacement · 1820745a
      Nick Alcock authored
      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.
      1820745a
    • Nick Alcock's avatar
      libctf: teach ctf_add_type how forwards work · 5de9eada
      Nick Alcock authored
      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.
      5de9eada
    • Nick Alcock's avatar
      libctf: add CU-mapping machinery · 49ea9b45
      Nick Alcock authored
      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.
      49ea9b45
    • Nick Alcock's avatar
      libctf: add linking of the variable section · eabb7154
      Nick Alcock authored
      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.
      eabb7154
    • Nick Alcock's avatar
      libctf: map from old to corresponding newly-added types in ctf_add_type · 886453cb
      Nick Alcock authored
      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.
      886453cb
    • Nick Alcock's avatar
      libctf: add the ctf_link machinery · 72c83edd
      Nick Alcock authored
      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.
      72c83edd
    • Nick Alcock's avatar
      libctf: dump: check the right error values when dumping functions · d18f9f16
      Nick Alcock authored
      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.
      d18f9f16
    • Nick Alcock's avatar
      libctf: dump: support non-root type dumping · b4f0e09c
      Nick Alcock authored
      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.
      b4f0e09c
    • Nick Alcock's avatar
      libctf: fix double-free on ctf_compress_write error path · 3dde2c91
      Nick Alcock authored
      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.
      3dde2c91
    • Nick Alcock's avatar
      libctf: write CTF files to memory, and CTF archives to fds · 5537f9b9
      Nick Alcock authored
      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.
      5537f9b9
    • Nick Alcock's avatar
      libctf: support getting strings from the ELF strtab · d851ecd3
      Nick Alcock authored
      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.
      d851ecd3
    • Nick Alcock's avatar
      libctf: Add iteration over non-root types · 0ac62312
      Nick Alcock authored
      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.
      0ac62312
    • Nick Alcock's avatar
      binutils: objdump does not take --ctf-symbols or --ctf-strings options · 6b88d7d7
      Nick Alcock authored
      libctf figures out what to load itself, with no overriding currently
      possible, so remove the documentation of these nonexistent options.
      6b88d7d7
    • Nick Alcock's avatar
      binutils: readelf: when dumping CTF, load strtab and symtab automatically · 616febde
      Nick Alcock authored
      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.
      616febde
    • Nick Alcock's avatar
      libctf: add the object index and function index sections · 2db912ba
      Nick Alcock authored
      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.
      2db912ba
    • Nick Alcock's avatar
      libctf, bfd: fix ctf_bfdopen_ctfsect opening symbol and string sections · 6d5944fc
      Nick Alcock authored
      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.
      6d5944fc
    • Nick Alcock's avatar
      libctf, binutils: dump the CTF header · 9b32cba4
      Nick Alcock authored
      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.
      9b32cba4
    • Nick Alcock's avatar
      libctf: allow the header to change between versions · fd55eae8
      Nick Alcock authored
      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.
      fd55eae8