Commit Graph

8 Commits

Author SHA1 Message Date
Nick Alcock 65365aa856 libctf: drop mmap()-based CTF data allocator
This allocator has the ostensible benefit that it lets us mprotect() the
memory used for CTF storage: but in exchange for this it adds
considerable complexity, since we have to track allocation sizes
ourselves for use at freeing time, note whether the data we are storing
was ctf_data_alloc()ed or not so we know if we can safely mprotect()
it... and while the mprotect()ing has found few bugs, it *has* been the
cause of more than one due to errors in all this tracking leading to us
mprotect()ing bits of the heap and stuff like that.

We are about to start composing CTF buffers from pieces so that we can
do usage-based optimizations on the strtab.  This means we need
realloc(), which needs nonportable mremap() and *more* tracking of the
*original* allocation size, and the complexity and bureaucracy of all of
this is just too high for its negligible benefits.

Drop the whole thing and just use malloc() like everyone else.  It knows
better than we do when it is safe to use mmap() under the covers,
anyway.

While we're at it, don't leak the entire buffer if ctf_compress_write()
fails to compress it.

libctf/
	* ctf-subr.c (_PAGESIZE): Remove.
	(ctf_data_alloc): Likewise.
	(ctf_data_free): Likewise.
	(ctf_data_protect): Likewise.
	* ctf-impl.h: Remove declarations.
	* ctf-create.c (ctf_update): No longer call ctf_data_protect: use
	ctf_free, not ctf_data_free.
	(ctf_compress_write): Use ctf_data_alloc, not ctf_alloc.  Free
	the buffer again on compression error.
	* ctf-open.c (ctf_set_base): No longer track the size: call
	ctf_free, not ctf_data_free.
	(upgrade_types): Likewise.  Call ctf_alloc, not ctf_data_alloc.
	(ctf_bufopen): Likewise.  No longer call ctf_data_protect.
2019-06-21 13:04:02 +01:00
Nick Alcock 2486542803 libctf: handle errors on dynhash insertion better
We were missing several cases where dynhash insertion might fail, likely
due to OOM but possibly for other reasons.  Pass the errors on.

libctf/
	* ctf-create.c (ctf_dtd_insert): Pass on error returns from
	ctf_dynhash_insert.
	(ctf_dvd_insert): Likewise.
	(ctf_add_generic): Likewise.
	(ctf_add_variable): Likewise.
	* ctf-impl.h: Adjust declarations.
2019-06-21 13:04:01 +01:00
Nick Alcock 62d8e3b731 libctf: eschew %zi format specifier
Too many platforms don't support it, and we can always safely use %lu or
%li anyway, because the only uses are in debugging output.

libctf/
	* ctf-archive.c (ctf_arc_write): Eschew %zi format specifier.
	(ctf_arc_open_by_offset): Likewise.
	* ctf-create.c (ctf_add_type): Likewise.
2019-06-05 13:34:36 +01:00
Tom Tromey 76fad99963 Use CHAR_BIT instead of NBBY in libctf
On x86-64 Fedora 29, I tried to build a mingw-hosted gdb that targets
ppc-linux.  You can do this with:

    ../binutils-gdb/configure --host=i686-w64-mingw32 --target=ppc-linux \
        --disable-{binutils,gas,gold,gprof,ld}

The build failed with these errors in libctf:

In file included from ../../binutils-gdb/libctf/ctf-create.c:20:
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_encoded':
../../binutils-gdb/libctf/ctf-create.c:803:59: error: 'NBBY' undeclared (first use in this function)
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c:803:59: note: each undeclared identifier is reported only once for each function it appears in
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_slice':
../../binutils-gdb/libctf/ctf-create.c:862:59: error: 'NBBY' undeclared (first use in this function)
   dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, NBBY) / NBBY);
                                                           ^~~~
../../binutils-gdb/libctf/ctf-impl.h:254:42: note: in definition of macro 'P2ROUNDUP'
 #define P2ROUNDUP(x, align)  (-(-(x) & -(align)))
                                          ^~~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_member_offset':
../../binutils-gdb/libctf/ctf-create.c:1341:21: error: 'NBBY' undeclared (first use in this function)
      off += lsize * NBBY;
                     ^~~~
../../binutils-gdb/libctf/ctf-create.c: In function 'ctf_add_type':
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/libctf/ctf-create.c:1823:35: note: format string is defined here
         "union size differs, old %zi, new %zi\n",
                                   ^
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: unknown conversion type character 'z' in format [-Wformat=]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../binutils-gdb/libctf/ctf-create.c:1823:44: note: format string is defined here
         "union size differs, old %zi, new %zi\n",
                                            ^
../../binutils-gdb/libctf/ctf-create.c:1822:16: warning: too many arguments for format [-Wformat-extra-args]
   ctf_dprintf ("Conflict for type %s against ID %lx: "
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This patch fixes the actual errors in here.  I did not try to fix the
printf warnings, though I think someone ought to.

Ok?

libctf/ChangeLog
2019-06-04  Tom Tromey  <tromey@adacore.com>

	* ctf-create.c (ctf_add_encoded, ctf_add_slice)
	(ctf_add_member_offset): Use CHAR_BIT, not NBBY.
2019-06-04 14:11:29 -06:00
Nick Alcock 6b22174ff1 libctf: look for BSD versus GNU qsort_r signatures
We cannot just look for any declaration of qsort_r, because some
operating systems have a qsort_r that has a different prototype
but which still has a pair of pointers in the right places (the last two
args are interchanged): so use AC_LINK_IFELSE to check for both
known variants of qsort_r(), and swap their args into a consistent order
in a suitable inline function.  (The code for this is taken almost
unchanged from gnulib.)

(Now we are not using AC_LIBOBJ any more, we can use a better name for
the qsort_r replacement as well.)

libctf/
	* qsort_r.c: Rename to...
	* ctf-qsort_r.c: ... this.
	(_quicksort): Define to ctf_qsort_r.
	* ctf-decls.h (qsort_r): Remove.
	(ctf_qsort_r): Add.
	(struct ctf_qsort_arg): New, transport the real ARG and COMPAR.
	(ctf_qsort_compar_thunk): Rearrange the arguments to COMPAR.
	* Makefile.am (libctf_a_LIBADD): Remove.
	(libctf_a_SOURCES): New, add ctf-qsort_r.c.
	* ctf-archive.c (ctf_arc_write): Call ctf_qsort_r, not qsort_r.
	* ctf-create.c (ctf_update): Likewise.
	* configure.ac: Check for BSD versus GNU qsort_r signature.
	* Makefile.in: Regenerate.
	* config.h.in: Likewise.
	* configure: Likewise.
2019-06-04 17:05:08 +01:00
Jose E. Marchesi a0486bac41 libctf: fix a number of build problems found on Solaris and NetBSD
- Use of nonportable <endian.h>
- Use of qsort_r
- Use of zlib without appropriate magic to pull in the binutils zlib
- Use of off64_t without checking (fixed by dropping the unused fields
  that need off64_t entirely)
- signedness problems due to long being too short a type on 32-bit
  platforms: ctf_id_t is now 'unsigned long', and CTF_ERR must be
  used only for functions that return ctf_id_t
- One lingering use of bzero() and of <sys/errno.h>

All fixed, using code from gnulib where possible.

Relatedly, set cts_size in a couple of places it was missed
(string table and symbol table loading upon ctf_bfdopen()).

binutils/
	* objdump.c (make_ctfsect): Drop cts_type, cts_flags, and
	cts_offset.
	* readelf.c (shdr_to_ctf_sect): Likewise.
include/
	* ctf-api.h (ctf_sect_t): Drop cts_type, cts_flags, and cts_offset.
	(ctf_id_t): This is now an unsigned type.
	(CTF_ERR): Cast it to ctf_id_t.  Note that it should only be used
	for ctf_id_t-returning functions.
libctf/
	* Makefile.am (ZLIB): New.
	(ZLIBINC): Likewise.
	(AM_CFLAGS): Use them.
	(libctf_a_LIBADD): New, for LIBOBJS.
	* configure.ac: Check for zlib, endian.h, and qsort_r.
	* ctf-endian.h: New, providing htole64 and le64toh.
	* swap.h: Code style fixes.
	(bswap_identity_64): New.
	* qsort_r.c: New, from gnulib (with one added #include).
	* ctf-decls.h: New, providing a conditional qsort_r declaration,
	and unconditional definitions of MIN and MAX.
	* ctf-impl.h: Use it.  Do not use <sys/errno.h>.
	(ctf_set_errno): Now returns unsigned long.
	* ctf-util.c (ctf_set_errno): Adjust here too.
	* ctf-archive.c: Use ctf-endian.h.
	(ctf_arc_open_by_offset): Use memset, not bzero.  Drop cts_type,
	cts_flags and cts_offset.
	(ctf_arc_write): Drop debugging dependent on the size of off_t.
	* ctf-create.c: Provide a definition of roundup if not defined.
	(ctf_create): Drop cts_type, cts_flags and cts_offset.
	(ctf_add_reftype): Do not check if type IDs are below zero.
	(ctf_add_slice): Likewise.
	(ctf_add_typedef): Likewise.
	(ctf_add_member_offset): Cast error-returning ssize_t's to size_t
	when known error-free.  Drop CTF_ERR usage for functions returning
	int.
	(ctf_add_member_encoded): Drop CTF_ERR usage for functions returning
	int.
	(ctf_add_variable): Likewise.
	(enumcmp): Likewise.
	(enumadd): Likewise.
	(membcmp): Likewise.
	(ctf_add_type): Likewise.  Cast error-returning ssize_t's to size_t
	when known error-free.
	* ctf-dump.c (ctf_is_slice): Drop CTF_ERR usage for functions
	returning int: use CTF_ERR for functions returning ctf_type_id.
	(ctf_dump_label): Likewise.
	(ctf_dump_objts): Likewise.
	* ctf-labels.c (ctf_label_topmost): Likewise.
	(ctf_label_iter): Likewise.
	(ctf_label_info): Likewise.
	* ctf-lookup.c (ctf_func_args): Likewise.
	* ctf-open.c (upgrade_types): Cast to size_t where appropriate.
	(ctf_bufopen): Likewise.  Use zlib types as needed.
	* ctf-types.c (ctf_member_iter): Drop CTF_ERR usage for functions
	returning int.
	(ctf_enum_iter): Likewise.
	(ctf_type_size): Likewise.
	(ctf_type_align): Likewise.  Cast to size_t where appropriate.
	(ctf_type_kind_unsliced): Likewise.
	(ctf_type_kind): Likewise.
	(ctf_type_encoding): Likewise.
	(ctf_member_info): Likewise.
	(ctf_array_info): Likewise.
	(ctf_enum_value): Likewise.
	(ctf_type_rvisit): Likewise.
	* ctf-open-bfd.c (ctf_bfdopen): Drop cts_type, cts_flags and
	cts_offset.
	(ctf_simple_open): Likewise.
	(ctf_bfdopen_ctfsect): Likewise.  Set cts_size properly.
	* Makefile.in: Regenerate.
	* aclocal.m4: Likewise.
	* config.h: Likewise.
	* configure: Likewise.
2019-05-31 11:10:51 +02:00
Nick Alcock c499eb6896 libctf: type copying
ctf_add_type() allows you to copy types, and all the types they depend
on, from one container to another (writable) container. This lets a
program maintaining multiple distinct containers (not in a parent-child
relationship) introduce types that depend on types in one container in
another writable one, by copying the necessary types.

libctf/
	* ctf-create.c (enumcmp): New.
	(enumadd): Likewise.
	(membcmp): Likewise.
	(membadd): Likewise.
	(ctf_add_type): Likewise.
2019-05-28 17:08:26 +01:00
Nick Alcock 47d546f427 libctf: creation functions
The CTF creation process looks roughly like (error handling elided):

int err;
ctf_file_t *foo = ctf_create (&err);

ctf_id_t type = ctf_add_THING (foo, ...);
ctf_update (foo);
ctf_*write (...);

Some ctf_add_THING functions accept other type IDs as arguments,
depending on the type: cv-quals, pointers, and structure and union
members all take other types as arguments.  So do 'slices', which
let you take an existing integral type and recast it as a type
with a different bitness or offset within a byte, for bitfields.
One class of THING is not a type: "variables", which are mappings
of names (in the internal string table) to types.  These are mostly
useful when encoding variables that do not appear in a symbol table
but which some external user has some other way to figure out the
address of at runtime (dynamic symbol lookup or querying a VM
interpreter or something).

You can snapshot the creation process at any point: rolling back to a
snapshot deletes all types and variables added since that point.

You can make arbitrary type queries on the CTF container during the
creation process, but you must call ctf_update() first, which
translates the growing dynamic container into a static one (this uses
the CTF opening machinery, added in a later commit), which is quite
expensive.  This function must also be called after adding types
and before writing the container out.

Because addition of types involves looking up existing types, we add a
little of the type lookup machinery here, as well: only enough to
look up types in dynamic containers under construction.

libctf/
	* ctf-create.c: New file.
	* ctf-lookup.c: New file.

include/
	* ctf-api.h (zlib.h): New include.
	(ctf_sect_t): New.
	(ctf_sect_names_t): Likewise.
	(ctf_encoding_t): Likewise.
	(ctf_membinfo_t): Likewise.
	(ctf_arinfo_t): Likewise.
	(ctf_funcinfo_t): Likewise.
	(ctf_lblinfo_t): Likewise.
	(ctf_snapshot_id_t): Likewise.
	(CTF_FUNC_VARARG): Likewise.
	(ctf_simple_open): Likewise.
	(ctf_bufopen): Likewise.
	(ctf_create): Likewise.
	(ctf_add_array): Likewise.
	(ctf_add_const): Likewise.
	(ctf_add_enum_encoded): Likewise.
	(ctf_add_enum): Likewise.
	(ctf_add_float): Likewise.
	(ctf_add_forward): Likewise.
	(ctf_add_function): Likewise.
	(ctf_add_integer): Likewise.
	(ctf_add_slice): Likewise.
	(ctf_add_pointer): Likewise.
	(ctf_add_type): Likewise.
	(ctf_add_typedef): Likewise.
	(ctf_add_restrict): Likewise.
	(ctf_add_struct): Likewise.
	(ctf_add_union): Likewise.
	(ctf_add_struct_sized): Likewise.
	(ctf_add_union_sized): Likewise.
	(ctf_add_volatile): Likewise.
	(ctf_add_enumerator): Likewise.
	(ctf_add_member): Likewise.
	(ctf_add_member_offset): Likewise.
	(ctf_add_member_encoded): Likewise.
	(ctf_add_variable): Likewise.
	(ctf_set_array): Likewise.
	(ctf_update): Likewise.
	(ctf_snapshot): Likewise.
	(ctf_rollback): Likewise.
	(ctf_discard): Likewise.
	(ctf_write): Likewise.
	(ctf_gzwrite): Likewise.
	(ctf_compress_write): Likewise.
2019-05-28 17:07:40 +01:00