[PATCH v3 00/19] libctf, and CTF support for objdump and readelf

Nick Alcock nick.alcock@oracle.com
Fri May 24 20:11:00 GMT 2019


This is the third posting of libctf.

Compared to v2, this is mostly a rethinking of the ctf_open / ctf_fdopen /
ctf_bfdopen public interfaces: they'd not been used in anger for a decade, and
the first attempts to use them revealed some deficiencies.  I took the
opportunity to improve them and integrate them better with the CTF archive
machinery, so that you can now do everything with CTF files contained in CTF
archives that you can do with CTF files on their own: in particular, you can
associate ELF string and symbol tables with them, and it's easy to do so (see
the example below).

We also support multiple BFD targets, and diagnose ambiguous formats etc
properly.  The BFD code has now been tested and works, at least for the simple
cases it's been tried with.  I've also tried a cross build (x86-pc-linux-gnu ->
mingw) and they seemed to work: at least objdump worked.  If I can find a SPARC
anywhere I'll try a cross to there and see if that works, but I haven't done
that yet.


Of the changes in this version (noted in detail in the individual patch mails),
the change to the public interface is probably most interesting.  ctf_open,
ctf_fdopen and ctf_bufopen now return ctf_archive_t's, not ctf_file_t's.
ctf_open and ctf_fdopen can be called on CTF files in raw form on disk, raw CTF
archives (.ctfa files), or ELF files containing any of these in the .ctf
section.  If a CTF archive is found (raw on disk or in an ELF section), it's
returned and can be iterated over, opened etc as usual; if a CTF file is found,
it is returned to the user as if it were a CTF archive containing a single
member named ".ctf".  (ctf_arc_open_by_name() accepts a name of NULL to mean
".ctf" to make this easier for users who don't care about the details of CTF
archives).  If called on an ELF file, the archive returned has the ELF file's
symbol and string tables associated with it automatically.  ctf_close() is now
the converse of ctf_open(): it closes a CTF archive, not a CTF file.  The new
ctf_file_close() closes a CTF file (usually obtained from
ctf_arc_open_by_name()).

To avoid the need to drag around an extra ctf_archive_t everywhere, there is a
new function ctf_get_arc() that returns the CTF archive that a given CTF file
came from.  Normal usage for a CTF file now changes from

ctf_file_t *fp = ctf_open ("foo", &err);
/* error handling */
/* do stuff */
ctf_close (fp);

to this:

ctf_archive_t *arc = ctf_open ("foo", optional_BFD_target_name, &err);
/* error handling */
ctf_file_t *fp = ctf_arc_open_by_name (arc, NULL, &err);
/* error handling */
/* do stuff */
ctf_file_close (fp);
ctf_close (arc);

Or (if you don't want to drag the archive variable around):

ctf_archive_t *tmp = ctf_open ("foo", &err);
/* error handling */
ctf_file_t *fp = ctf_arc_open_by_name (arc, NULL, &err);
/* error handling */

/* do stuff... then, later: */
ctf_archive_t *arc = ctf_get_arc (fp);
ctf_file_close (fp);
ctf_close (arc);

(Of course, since this is an archive, you can also iterate over it and pull more
than one ctf_file_t out of it if you need to.  You couldn't do that with the old
interface.)


objdump has been revamped to use this machinery: it's much simpler now, and
acquires the string and symbol tables automatically, with no need for extra
command-line options to tell it to do so.  It also contains an example of archive
iteration :)


Interface musing: I am entirely amenable to adding a shorter name for
ctf_arc_open_by_name() now that it's called so often.  ctf_get_file()? I'm not
sure.  It might also be nice to have a convenience function that wraps up the
ctf_open / ctf_arc_open_by_name (arc, NULL, &err) thing into a single function
call for those who know they'll never be dealing with archives, but that might
also be providing people with too much of a gun to shoot themselves in the foot
with.

If someone could think of a better name for "struct ctf_archive", so we don't
have the enormously confusing situation where ctf_archive_t is not a typedef for
struct ctf_archive, I'd be happy to hear one -- but fundamentally the "struct
ctf_archive" *is* the underlying archive format and it's defined in
ctf-archive.c and giving it another name just seems wrong.  Maybe leaving "struct
ctf_archive" as it is and renaming ctf_archive_t would be a better idea, but
again I can't think of a better name.  The ctf_archive_t does appear to be an
archive, after all, and it *is* an abstraction over, among other things, the
"struct ctf_archive".


Note for future enhancements: there is no longer any requirement that
ctf_archive_t be strictly a CTF archive on disk.  We already pretend that a
ctf_file_t is a one-member archive, and in future, we might acquire all CTF
sections in the ELF file by looking for sections with the right name, or section
type, or something like that: the interface doesn't need to change at all and
they'd all just appear to be multi-member ctf_archive_t's to the libctf user.
We could probably wrap the BFD archive machinery up in the same way, so that
opening a .a archive would return a ctf_archive_t full of CTF files too.  (But
this is not done yet.)


The format description is the same as it was in the last patch series: see that
series for more info.  It *will* change shortly -- though in a fashion that will
not leave it unrecognizably different, just much more compact -- and when it
changes I'll probably write something in texinfo or something to describe in a
more GNUish fashion.


Joseph: you'll probably be interested in patches 3, 9, 10 and 11 in particular,
for their BFDization and portability fixes.

Nick Alcock (19):
  include: new header ctf.h: file format description
  include: new header ctf-api.h
  libctf: lowest-level memory allocation and debug-dumping wrappers
  libctf: low-level list manipulation and helper utilities
  libctf: error handling
  libctf: hashing
  libctf: implementation definitions related to file creation
  libctf: creation functions
  libctf: opening
  libctf: mmappable archives
  libctf: ELF file opening via BFD
  libctf: core type lookup
  libctf: lookups by name and symbol
  libctf: type copying
  libctf: library version enforcement
  libctf: labels
  libctf: debug dumping
  libctf: build system
  binutils: CTF support for objdump and readelf

 Makefile.def                    |    6 +
 Makefile.in                     |  991 +++-
 binutils/Makefile.am            |   10 +-
 binutils/Makefile.in            |   18 +-
 binutils/aclocal.m4             |   10 +-
 binutils/doc/Makefile.in        |    9 +-
 binutils/doc/binutils.texi      |   19 +
 binutils/doc/ctf.options.texi   |   14 +
 binutils/objdump.c              |  169 +-
 binutils/readelf.c              |  206 +
 configure                       |    2 +-
 configure.ac                    |    2 +-
 include/ctf-api.h               |  387 ++
 include/ctf.h                   |  564 +++
 libctf/Makefile.am              |   31 +
 libctf/Makefile.in              |  767 ++++
 {binutils => libctf}/aclocal.m4 |   99 +-
 libctf/config.h.in              |  113 +
 libctf/configure                | 7462 +++++++++++++++++++++++++++++++
 libctf/configure.ac             |   63 +
 libctf/ctf-archive.c            |  756 ++++
 libctf/ctf-create.c             | 2032 +++++++++
 libctf/ctf-decl.c               |  195 +
 libctf/ctf-dump.c               |  595 +++
 libctf/ctf-error.c              |   93 +
 libctf/ctf-hash.c               |  277 ++
 libctf/ctf-impl.h               |  385 ++
 libctf/ctf-labels.c             |  138 +
 libctf/ctf-lookup.c             |  427 ++
 libctf/ctf-open-bfd.c           |  351 ++
 libctf/ctf-open.c               | 1691 +++++++
 libctf/ctf-subr.c               |  259 ++
 libctf/ctf-types.c              | 1023 +++++
 libctf/ctf-util.c               |  176 +
 libctf/elf.h                    |   61 +
 libctf/swap.h                   |   60 +
 36 files changed, 19390 insertions(+), 71 deletions(-)
 create mode 100644 binutils/doc/ctf.options.texi
 create mode 100644 include/ctf-api.h
 create mode 100644 include/ctf.h
 create mode 100644 libctf/Makefile.am
 create mode 100644 libctf/Makefile.in
 copy {binutils => libctf}/aclocal.m4 (95%)
 create mode 100644 libctf/config.h.in
 create mode 100755 libctf/configure
 create mode 100644 libctf/configure.ac
 create mode 100644 libctf/ctf-archive.c
 create mode 100644 libctf/ctf-create.c
 create mode 100644 libctf/ctf-decl.c
 create mode 100644 libctf/ctf-dump.c
 create mode 100644 libctf/ctf-error.c
 create mode 100644 libctf/ctf-hash.c
 create mode 100644 libctf/ctf-impl.h
 create mode 100644 libctf/ctf-labels.c
 create mode 100644 libctf/ctf-lookup.c
 create mode 100644 libctf/ctf-open-bfd.c
 create mode 100644 libctf/ctf-open.c
 create mode 100644 libctf/ctf-subr.c
 create mode 100644 libctf/ctf-types.c
 create mode 100644 libctf/ctf-util.c
 create mode 100644 libctf/elf.h
 create mode 100644 libctf/swap.h

-- 
2.21.0.237.gd0cfaa883d



More information about the Binutils mailing list