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

Message ID 20190517221002.408822-1-nick.alcock@oracle.com
Headers show
Series
  • libctf, and CTF support for objdump and readelf
Related show

Message

Nick Alcock May 17, 2019, 10:09 p.m.
This is the second posting of libctf, containing fixes for every review comment
received in the last cycle, and introduction of backward-compatibility code for
the old v1 format found in some enterprise Linux kernel packages, and nothing
else: in particular, the various format changes and prototype changes planned
have not landed yet.  (The backward-compatibility code is preparation for this,
since it is that code which will be enhanced and disentangled when the format
changes land.)

(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 -- 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 15 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: ELF file opening via BFD
  libctf: core type lookup
  libctf: lookups by name and symbol
  libctf: type copying
  libctf: library version enforcement
  libctf: mmappable archives
  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      |   12 +
 binutils/doc/ctf.options.texi   |   19 +
 binutils/objdump.c              |  156 +-
 binutils/readelf.c              |  206 +
 configure                       |    2 +-
 configure.ac                    |    2 +-
 include/ctf-api.h               |  363 ++
 include/ctf.h                   |  561 +++
 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            |  617 +++
 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               |  374 ++
 libctf/ctf-labels.c             |  138 +
 libctf/ctf-lookup.c             |  427 ++
 libctf/ctf-open-bfd.c           |  264 ++
 libctf/ctf-open.c               | 1687 +++++++
 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, 19107 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

Comments

Joseph Myers May 20, 2019, 7:27 p.m. | #1
On Fri, 17 May 2019, Nick Alcock wrote:

> Joseph: you'll probably be interested in patches 3, 9, 10, and 15 in particular,

> for their BFDization and portability fixes.


Thanks.  A quick question would be what hosts you've tested this builds 
for.  At least some GNU/Linux distributions package cross-compilers for 
MinGW; if you install such a compiler and make sure that binutils, with 
these patches, can build using it, --host=x86_64-w64-mingw32 
--target=<some ELF target> (adjust host as needed if the host triplet of 
the packaged compiler is different), then there shouldn't be major 
problems getting it to build on any other likely host either.

-- 
Joseph S. Myers
joseph@codesourcery.com
Nick Alcock May 20, 2019, 8:50 p.m. | #2
On 20 May 2019, Joseph Myers uttered the following:

> On Fri, 17 May 2019, Nick Alcock wrote:

>

>> Joseph: you'll probably be interested in patches 3, 9, 10, and 15 in particular,

>> for their BFDization and portability fixes.

>

> Thanks.  A quick question would be what hosts you've tested this builds 

> for.  At least some GNU/Linux distributions package cross-compilers for 

> MinGW; if you install such a compiler and make sure that binutils, with 

> these patches, can build using it, --host=x86_64-w64-mingw32 

> --target=<some ELF target> (adjust host as needed if the host triplet of 

> the packaged compiler is different), then there shouldn't be major 

> problems getting it to build on any other likely host either.


Oh blast I knew I forgot to mention something.

This has mostly been tested on Linux (x86_64, arm64, and sparc64): I did
do a native mingw build a while back (right after doing the mmap and
pread stuff) but I probably need to do another one. I'm tempted to do a
FreeBSD build too simply because it's easy.

I did most of the no-mmap and no-pread() testing on Linux, by just
hacking the config.h to turn it off and making sure it wasn't making the
relevant syscalls any more, and still worked. (Which it does, but it's
slower when doing archive opens, unsurprisingly, since it's reading the
whole archive in even if only a bit is needed.)

I suspect the bfd side of things is still broken: a teammate has just
tested it and reported that things aren't working there yet. I'll look
at that tomorrow.
Nick Clifton May 28, 2019, 8:33 a.m. | #3
Hi Nick,

  Sorry for the delay in reviewing.
 
> 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: ELF file opening via BFD

>   libctf: core type lookup

>   libctf: lookups by name and symbol

>   libctf: type copying

>   libctf: library version enforcement

>   libctf: mmappable archives

>   libctf: labels

>   libctf: debug dumping

>   libctf: build system

>   binutils: CTF support for objdump and readelf


Patch series approved (all of it) - please apply.

I would however ask that you make two small additions:

  * Please could you add an entry to the binutils/NEWS file,
    mentioning this new feature of the binutils.

  * Please could you add an entry to the binutils/MAINTAINERS
    file, listing yourself, or someone else at Oracle, as the
    maintainer for the CTF code.

Thanks very much for contributing this code to the binutils project.

Cheers
  Nick
Nick Alcock May 28, 2019, 9:56 a.m. | #4
On 28 May 2019, Nick Clifton verbalised:

> Hi Nick,

>

>   Sorry for the delay in reviewing.


No problem! Review bandwidth is scarce: I'm happy to get reviews at all.

>> 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: ELF file opening via BFD

>>   libctf: core type lookup

>>   libctf: lookups by name and symbol

>>   libctf: type copying

>>   libctf: library version enforcement

>>   libctf: mmappable archives

>>   libctf: labels

>>   libctf: debug dumping

>>   libctf: build system

>>   binutils: CTF support for objdump and readelf

>

> Patch series approved (all of it) - please apply.


Thank you! -- though I don't have commit rights yet, so until someone
sees fit to grant me that someone else will have to do the actual push.
I also don't really know how one transforms the ChangeLogs in the commit
messages into actual ChangeLog files in the commits: do you really edit
every commit and add the ChangeLog changes to it by hand?

(I should of course rebase it first so it's not based on a months-old
checkout, and make sure that it still works after the rebase.)


On related matters: I presume nobody minds if I submit further
improvements that change the format to provide size reductions (quite
radical ones)? i.e. nobody minds if it's not set in stone yet? It's
still young in this form, after all.

For backward-compatibility, I propose to use the usual GNU rules, and
provide backward-compat for all formats "in the world" in any official
release version: so for now I'll keep the format version at v3 and any
format changes go into that, updating the v1/v2 -> v3 compat code at the
same time, until a release of GCC or the not-yet-written linker plugin
or *something* that generates CTF, whereupon the format is bumped to v4
and backward-compat code for v3 -> all future changes in v4 need to be
added as part of any future changes. The backward-compat stuff also
means that most things that generate CTF don't need to change when the
format changes: the linker plugin will bring the format up to date as
needed.

(As for something that generates CTF, the linker plugin is my next major
thing to attack. I'm just trying to shrink things a little first. It's
amazing how many obvious improvements show up when you get your code
properly reviewed.)

> I would however ask that you make two small additions:

>

>   * Please could you add an entry to the binutils/NEWS file,

>     mentioning this new feature of the binutils.


How's about

 * Add support for dumping types encoded in the Compact Type Format
   to objdump and readelf.

(Should we mention that this is a *new* CTF, not format-compatible with
existing ones? Should we mention that we haven't committed tools that
emit CTF yet? Should we mention the existence of the library itself
anywhere, even though it's not -- yet -- user-visible? I'm not sure.)

>   * Please could you add an entry to the binutils/MAINTAINERS

>     file, listing yourself, or someone else at Oracle, as the

>     maintainer for the CTF code.


Of course! Though, again, without commit access all I can do is propose
an entry for the top level and an entry for binutils/MAINTAINERS, which
may change if GCC portions of this get in.

The top-level entry gets added to

bfd/; binutils/; elfcpp/; gas/; gold/; gprof/; ld/; opcodes/; cpu/;

I think. For MAINTAINERS, I am happy to have added

CTF		Nick Alcock <nick.alcock@oracle.com>

> Thanks very much for contributing this code to the binutils project.


I'm not done yet! Though the linker plugin won't turn up immediately
since I still have to write it, it will turn up, and this stuff will
actually be useful. (I'll be doing a simple linker plugin without
deduplication first, and then adding deduplication.)

But this is the foundation. Nothing else is possible without it. So
thank you for the excellent review!
Jose E. Marchesi May 28, 2019, 4:39 p.m. | #5
> 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: ELF file opening via BFD

    >   libctf: core type lookup

    >   libctf: lookups by name and symbol

    >   libctf: type copying

    >   libctf: library version enforcement

    >   libctf: mmappable archives

    >   libctf: labels

    >   libctf: debug dumping

    >   libctf: build system

    >   binutils: CTF support for objdump and readelf

    
    Patch series approved (all of it) - please apply.

Just pushed it in behalf of Nix, with the additions you suggested.
Thanks!
Christophe Lyon May 28, 2019, 7:14 p.m. | #6
Hi,


On Tue, 28 May 2019 at 18:39, Jose E. Marchesi <jose.marchesi@oracle.com> wrote:
>

>

>     > 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: ELF file opening via BFD

>     >   libctf: core type lookup

>     >   libctf: lookups by name and symbol

>     >   libctf: type copying

>     >   libctf: library version enforcement

>     >   libctf: mmappable archives

>     >   libctf: labels

>     >   libctf: debug dumping

>     >   libctf: build system

>     >   binutils: CTF support for objdump and readelf

>

>     Patch series approved (all of it) - please apply.

>

> Just pushed it in behalf of Nix, with the additions you suggested.

> Thanks!



One of the patches in the series broke the build for --target arm-pe:
../libctf/libctf.a(ctf-open-bfd.o): In function `ctf_bfdopen_ctfsect':
/home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:136:
undefined reference to `bfd_section_from_elf_index'
/home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:145:
undefined reference to `bfd_section_from_elf_index'

Can you check & fix?

Thanks

Christophe
Nix May 28, 2019, 9:36 p.m. | #7
On 28 May 2019, Christophe Lyon stated:

> On Tue, 28 May 2019 at 18:39, Jose E. Marchesi <jose.marchesi@oracle.com> wrote:

>>

>>

>>     > 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: ELF file opening via BFD

>>     >   libctf: core type lookup

>>     >   libctf: lookups by name and symbol

>>     >   libctf: type copying

>>     >   libctf: library version enforcement

>>     >   libctf: mmappable archives

>>     >   libctf: labels

>>     >   libctf: debug dumping

>>     >   libctf: build system

>>     >   binutils: CTF support for objdump and readelf

>>

>>     Patch series approved (all of it) - please apply.

>>

>> Just pushed it in behalf of Nix, with the additions you suggested.

>> Thanks!

>

>

> One of the patches in the series broke the build for --target arm-pe:

> ../libctf/libctf.a(ctf-open-bfd.o): In function `ctf_bfdopen_ctfsect':

> /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:136:

> undefined reference to `bfd_section_from_elf_index'

> /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:145:

> undefined reference to `bfd_section_from_elf_index'

>

> Can you check & fix?


Very sorry about that. My non-ELF platform of choice was mingw, which I
now see pulls in the ELF bfd stuff! Ironically I had all the necessary
ifdefs in place for the non-ELF builds but was intentionally trimming
them out of the copy synced to binutils...

I am testing the fix below (on lots more targets, this time, so it might
be a few hours). I hope the build system fiddling is right. It was... a
bit counterintuitive, although all the changes made sense in hindsight.

I tested mingw, x86_64-pc-linux-gnu and arm-pe by hand and it seems to
work on all three.

commit e23cfedad4f85235337d485b5c51bdcda92565ef
Author: Nick Alcock <nick.alcock@oracle.com>
Date:   Tue May 28 22:24:01 2019 +0100

    Fix libctf build on non-ELF targets.
    
    All machinery works as on ELF, except for automatic loading of ELF
    string and symbol tables in the BFD-style open machinery.
    
            * Makefile.def (dependencies): configure-libctf depends on all-bfd
            and all its deps.
            * Makefile.in: Regenerated.
    
    libctf/
            * configure.in: Check for bfd_section_from_elf_index.
            * configure: Regenerate.
            * config.h.in [HAVE_BFD_ELF]: Likewise.
            * libctf/ctf_open_bfd (ctf_bfdopen_ctfsect): Use it.
            abfd is potentially unused now.

diff --git a/ChangeLog b/ChangeLog
index f57f4fe2f9..cbfb2155c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-05-28  Nick Alcock  <nick.alcock@oracle.com>
+
+	* Makefile.def (dependencies): configure-libctf depends on all-bfd
+	and all its deps.
+	* Makefile.in: Regenerated.
+
 2019-05-28  Nick Alcock  <nick.alcock@oracle.com>
 
 	* MAINTAINERS: Add libctf.
diff --git a/Makefile.def b/Makefile.def
index 3aa03b45d3..e3a94f2c7d 100644
--- a/Makefile.def
+++ b/Makefile.def
@@ -522,6 +522,11 @@ dependencies = { module=all-fastjar; on=all-libiberty; };
 dependencies = { module=all-libctf; on=all-libiberty; hard=true; };
 dependencies = { module=all-libctf; on=all-bfd; };
 dependencies = { module=all-libctf; on=all-zlib; };
+// So that checking for ELF support in BFD from libctf configure is possible.
+dependencies = { module=configure-libctf; on=all-bfd; };
+dependencies = { module=configure-libctf; on=all-intl; };
+dependencies = { module=configure-libctf; on=all-zlib; };
+dependencies = { module=configure-libctf; on=all-libiconv; };
 
 // Warning, these are not well tested.
 dependencies = { module=all-bison; on=all-intl; };
diff --git a/Makefile.in b/Makefile.in
index 68675f0479..e4b6051b1a 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -50930,6 +50930,34 @@ all-stage3-libctf: maybe-all-stage3-zlib
 all-stage4-libctf: maybe-all-stage4-zlib
 all-stageprofile-libctf: maybe-all-stageprofile-zlib
 all-stagefeedback-libctf: maybe-all-stagefeedback-zlib
+configure-libctf: maybe-all-bfd
+configure-stage1-libctf: maybe-all-stage1-bfd
+configure-stage2-libctf: maybe-all-stage2-bfd
+configure-stage3-libctf: maybe-all-stage3-bfd
+configure-stage4-libctf: maybe-all-stage4-bfd
+configure-stageprofile-libctf: maybe-all-stageprofile-bfd
+configure-stagefeedback-libctf: maybe-all-stagefeedback-bfd
+configure-libctf: maybe-all-intl
+configure-stage1-libctf: maybe-all-stage1-intl
+configure-stage2-libctf: maybe-all-stage2-intl
+configure-stage3-libctf: maybe-all-stage3-intl
+configure-stage4-libctf: maybe-all-stage4-intl
+configure-stageprofile-libctf: maybe-all-stageprofile-intl
+configure-stagefeedback-libctf: maybe-all-stagefeedback-intl
+configure-libctf: maybe-all-zlib
+configure-stage1-libctf: maybe-all-stage1-zlib
+configure-stage2-libctf: maybe-all-stage2-zlib
+configure-stage3-libctf: maybe-all-stage3-zlib
+configure-stage4-libctf: maybe-all-stage4-zlib
+configure-stageprofile-libctf: maybe-all-stageprofile-zlib
+configure-stagefeedback-libctf: maybe-all-stagefeedback-zlib
+configure-libctf: maybe-all-libiconv
+configure-stage1-libctf: maybe-all-stage1-libiconv
+configure-stage2-libctf: maybe-all-stage2-libiconv
+configure-stage3-libctf: maybe-all-stage3-libiconv
+configure-stage4-libctf: maybe-all-stage4-libiconv
+configure-stageprofile-libctf: maybe-all-stageprofile-libiconv
+configure-stagefeedback-libctf: maybe-all-stagefeedback-libiconv
 all-bison: maybe-all-build-texinfo
 all-flex: maybe-all-build-bison
 all-flex: maybe-all-m4
diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index a4fd3b17ba..879aeed38f 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,11 @@
+2019-05-28  Nick Alcock  <nick.alcock@oracle.com>
+
+	* configure.in: Check for bfd_section_from_elf_index.
+	* configure: Regenerate.
+	* config.h.in [HAVE_BFD_ELF]: Likewise.
+	* libctf/ctf_open_bfd (ctf_bfdopen_ctfsect): Use it.
+	abfd is potentially unused now.
+
 2019-05-28  Nick Alcock  <nick.alcock@oracle.com>
 
 	* Makefile.am: New.
diff --git a/libctf/config.h.in b/libctf/config.h.in
index 8d87b3d08a..829201033e 100644
--- a/libctf/config.h.in
+++ b/libctf/config.h.in
@@ -1,5 +1,8 @@
 /* config.h.in.  Generated from configure.ac by autoheader.  */
 
+/* Whether libbfd was configured for an ELF target. */
+#undef HAVE_BFD_ELF
+
 /* Define to 1 if you have the <byteswap.h> header file. */
 #undef HAVE_BYTESWAP_H
 
diff --git a/libctf/configure b/libctf/configure
index 2213393673..1c0340125a 100755
--- a/libctf/configure
+++ b/libctf/configure
@@ -5974,6 +5974,114 @@ $as_echo "#define HAVE_MMAP 1" >>confdefs.h
 fi
 rm -f conftest.mmap conftest.txt
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing dlopen" >&5
+$as_echo_n "checking for library containing dlopen... " >&6; }
+if ${ac_cv_search_dlopen+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_func_search_save_LIBS=$LIBS
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char dlopen ();
+int
+main ()
+{
+return dlopen ();
+  ;
+  return 0;
+}
+_ACEOF
+for ac_lib in '' dl; do
+  if test -z "$ac_lib"; then
+    ac_res="none required"
+  else
+    ac_res=-l$ac_lib
+    LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
+  fi
+  if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_search_dlopen=$ac_res
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext
+  if ${ac_cv_search_dlopen+:} false; then :
+  break
+fi
+done
+if ${ac_cv_search_dlopen+:} false; then :
+
+else
+  ac_cv_search_dlopen=no
+fi
+rm conftest.$ac_ext
+LIBS=$ac_func_search_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_dlopen" >&5
+$as_echo "$ac_cv_search_dlopen" >&6; }
+ac_res=$ac_cv_search_dlopen
+if test "$ac_res" != no; then :
+  test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
+
+fi
+
+
+# Similar to GDB_AC_CHECK_BFD.
+OLD_CFLAGS=$CFLAGS
+OLD_LDFLAGS=$LDFLAGS
+OLD_LIBS=$LIBS
+# Put the old CFLAGS/LDFLAGS last, in case the user's (C|LD)FLAGS
+# points somewhere with bfd, with -I/foo/lib and -L/foo/lib.  We
+# always want our bfd.
+CFLAGS="-I${srcdir}/../include -I../bfd -I${srcdir}/../bfd $CFLAGS"
+ZLIBDIR=`echo $zlibdir | sed 's,\$(top_builddir)/,,g'`
+LDFLAGS="-L../bfd -L../libiberty $ZLIBDIR $LDFLAGS"
+intl=`echo $LIBINTL | sed 's,${top_builddir}/,,g'`
+LIBS="-lbfd -liberty -lz $intl $LIBS"
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ELF support in BFD" >&5
+$as_echo_n "checking for ELF support in BFD... " >&6; }
+if ${ac_cv_libctf_bfd_elf+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <stdlib.h>
+	     #include "bfd.h"
+	     #include "elf-bfd.h"
+int
+main ()
+{
+(void) bfd_section_from_elf_index (NULL, 0);
+	     return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_libctf_bfd_elf=yes
+else
+  ac_cv_libctf_bfd_elf=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_libctf_bfd_elf" >&5
+$as_echo "$ac_cv_libctf_bfd_elf" >&6; }
+CFLAGS=$OLD_CFLAGS
+LDFLAGS=$OLD_LDFLAGS
+LIBS=$OLD_LIBS
+
+if test $ac_cv_libctf_bfd_elf = yes; then
+
+$as_echo "#define HAVE_BFD_ELF 1" >>confdefs.h
+
+fi
+
 for ac_header in byteswap.h
 do :
   ac_fn_c_check_header_mongrel "$LINENO" "byteswap.h" "ac_cv_header_byteswap_h" "$ac_includes_default"
diff --git a/libctf/configure.ac b/libctf/configure.ac
index 2d0cf86bb7..2df10935c0 100644
--- a/libctf/configure.ac
+++ b/libctf/configure.ac
@@ -55,6 +55,37 @@ AM_MAINTAINER_MODE
 ACX_PROG_CC_WARNING_OPTS([-Wall], [ac_libctf_warn_cflags])
 
 AC_FUNC_MMAP
+AC_SEARCH_LIBS(dlopen, dl)
+
+# Similar to GDB_AC_CHECK_BFD.
+OLD_CFLAGS=$CFLAGS
+OLD_LDFLAGS=$LDFLAGS
+OLD_LIBS=$LIBS
+# Put the old CFLAGS/LDFLAGS last, in case the user's (C|LD)FLAGS
+# points somewhere with bfd, with -I/foo/lib and -L/foo/lib.  We
+# always want our bfd.
+CFLAGS="-I${srcdir}/../include -I../bfd -I${srcdir}/../bfd $CFLAGS"
+ZLIBDIR=`echo $zlibdir | sed 's,\$(top_builddir)/,,g'`
+LDFLAGS="-L../bfd -L../libiberty $ZLIBDIR $LDFLAGS"
+intl=`echo $LIBINTL | sed 's,${top_builddir}/,,g'`
+LIBS="-lbfd -liberty -lz $intl $LIBS"
+AC_CACHE_CHECK([for ELF support in BFD], ac_cv_libctf_bfd_elf,
+[AC_TRY_LINK([#include <stdlib.h>
+	     #include "bfd.h"
+	     #include "elf-bfd.h"],
+	    [(void) bfd_section_from_elf_index (NULL, 0);
+	     return 0;],
+	    [ac_cv_libctf_bfd_elf=yes],
+	    [ac_cv_libctf_bfd_elf=no])])
+CFLAGS=$OLD_CFLAGS
+LDFLAGS=$OLD_LDFLAGS
+LIBS=$OLD_LIBS
+
+if test $ac_cv_libctf_bfd_elf = yes; then
+  AC_DEFINE([HAVE_BFD_ELF], 1,
+	    [Whether libbfd was configured for an ELF target.])
+fi
+
 AC_CHECK_HEADERS(byteswap.h)
 AC_CHECK_FUNCS(pread)
 
diff --git a/libctf/ctf-open-bfd.c b/libctf/ctf-open-bfd.c
index d6d442ea42..5e34d12369 100644
--- a/libctf/ctf-open-bfd.c
+++ b/libctf/ctf-open-bfd.c
@@ -119,7 +119,8 @@ ctf_bfdopen (struct bfd *abfd, int *errp)
    later.  */
 
 ctf_archive_t *
-ctf_bfdopen_ctfsect (struct bfd *abfd, const ctf_sect_t *ctfsect, int *errp)
+ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
+		     const ctf_sect_t *ctfsect, int *errp)
 {
   struct ctf_archive *arc = NULL;
   ctf_archive_t *arci;
@@ -129,6 +130,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd, const ctf_sect_t *ctfsect, int *errp)
   const char *bfderrstr = NULL;
   int is_archive;
 
+#ifdef HAVE_BFD_ELF
   asection *sym_asect;
   ctf_sect_t symsect, strsect;
   /* TODO: handle SYMTAB_SHNDX.  */
@@ -178,6 +180,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd, const ctf_sect_t *ctfsect, int *errp)
 	  symsectp = &symsect;
 	}
     }
+#endif
 
   if (ctfsect->cts_size > sizeof (uint64_t) &&
       ((*(uint64_t *) ctfsect->cts_data) == CTFA_MAGIC))
@@ -203,9 +206,11 @@ ctf_bfdopen_ctfsect (struct bfd *abfd, const ctf_sect_t *ctfsect, int *errp)
   if (arci)
     return arci;
  err_free_sym:
+#ifdef HAVE_BFD_ELF
   free ((void *) symsect.cts_data);
 err_free_str:
   free ((void *) strsect.cts_data);
+#endif
 err: _libctf_unused_;
   if (bfderrstr)
     {
Alan Modra May 29, 2019, 3:25 a.m. | #8
On Tue, May 28, 2019 at 10:36:17PM +0100, Nix wrote:
> On 28 May 2019, Christophe Lyon stated:

> > One of the patches in the series broke the build for --target arm-pe:

> > ../libctf/libctf.a(ctf-open-bfd.o): In function `ctf_bfdopen_ctfsect':

> > /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:136:

> > undefined reference to `bfd_section_from_elf_index'

> > /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:145:

> > undefined reference to `bfd_section_from_elf_index'

> >

> > Can you check & fix?

> 

> Very sorry about that. My non-ELF platform of choice was mingw, which I

> now see pulls in the ELF bfd stuff! Ironically I had all the necessary

> ifdefs in place for the non-ELF builds but was intentionally trimming

> them out of the copy synced to binutils...

> 

> I am testing the fix below (on lots more targets, this time, so it might

> be a few hours). I hope the build system fiddling is right. It was... a

> bit counterintuitive, although all the changes made sense in hindsight.


It looks OK to me and fixes build failures on these targets that I test:
alpha-dec-vms
arm-pe
arm-wince-pe
hppa-hp-hpux10
i386-darwin
mcore-pe
ns32k-netbsd
ns32k-pc532-mach
pdp11-dec-aout
powerpc-aix5.1
powerpc-aix5.2
powerpcle-cygwin
rs6000-aix4.3.3
rs6000-aix5.1
rs6000-aix5.2
sh-pe
tic30-unknown-aout
tic30-unknown-coff
tic4x-coff
tic54x-coff
z80-coff
z8k-coff

-- 
Alan Modra
Australia Development Lab, IBM
Jose E. Marchesi May 29, 2019, 9:13 a.m. | #9
> On 28 May 2019, Christophe Lyon stated:

    > > One of the patches in the series broke the build for --target arm-pe:

    > > ../libctf/libctf.a(ctf-open-bfd.o): In function `ctf_bfdopen_ctfsect':

    > > /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:136:

    > > undefined reference to `bfd_section_from_elf_index'

    > > /home/tcwg-buildslave/workspace/tcwg-binutils/bfd/--enable-64-bit-bfd=no/label/tcwg-x86_64-cam/target/arm-pe/build-arm-pe/libctf/../../libctf/ctf-open-bfd.c:145:

    > > undefined reference to `bfd_section_from_elf_index'

    > >

    > > Can you check & fix?

    > 

    > Very sorry about that. My non-ELF platform of choice was mingw, which I

    > now see pulls in the ELF bfd stuff! Ironically I had all the necessary

    > ifdefs in place for the non-ELF builds but was intentionally trimming

    > them out of the copy synced to binutils...

    > 

    > I am testing the fix below (on lots more targets, this time, so it might

    > be a few hours). I hope the build system fiddling is right. It was... a

    > bit counterintuitive, although all the changes made sense in hindsight.

    
    It looks OK to me and fixes build failures on these targets that I
    test:
    alpha-dec-vms
    arm-pe
    arm-wince-pe
    hppa-hp-hpux10
    i386-darwin
    mcore-pe
    ns32k-netbsd
    ns32k-pc532-mach
    pdp11-dec-aout
    powerpc-aix5.1
    powerpc-aix5.2
    powerpcle-cygwin
    rs6000-aix4.3.3
    rs6000-aix5.1
    rs6000-aix5.2
    sh-pe
    tic30-unknown-aout
    tic30-unknown-coff
    tic4x-coff
    tic54x-coff
    z80-coff
    z8k-coff

I just pushed the fix in Nix's behalf.