Allow BFD to recognize macOS universal libraries
authorTom Tromey <tom@tromey.com>
Thu, 28 Jun 2018 14:02:42 +0000 (08:02 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 2 Jul 2018 14:24:32 +0000 (08:24 -0600)
commiteac61af65bcd24a48633da375527eb3f36ab47ed
treecb3dd22ee041e9f253a12838b1802c73df14cd0a
parent41823f29a811bb250ae274652281a6294fdc2530
Allow BFD to recognize macOS universal libraries

Bug #13157 is about a gdb regression, where previously it could handle
universal libraries, but now cannot.

gdb isn't working for me on macOS for other reasons, so I wrote this
small test program to show the problem:

    #include <config.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <bfd.h>

    void
    die (const char *what)
    {
      fprintf (stderr, "die: %s\n", what);
      exit (1);
    }

    int
    main (int argc, char **argv)
    {
      bfd *file = bfd_openr (argv[1], NULL);
      if (file == NULL)
die ("couldn't open");

      if (!bfd_check_format (file, bfd_archive))
die ("not an archive");

      printf ("yay\n");

      bfd_close (file);
      return 0;
    }

Then I built a simple universal binary.  With git master BFD, I get:

    $ ./doit ./universal-exe
    die: not an archive

Jeff Muizelaar tracked this down to the BFD change for PR binutils/21787.
This patch changed bfd_generic_archive_p to sometimes reset the BFD's
"format" field.

However, simply changing bfd_generic_archive_p regressed the test case
in that bug.

Debugging PR binutils/21787 again, what I saw is that the mach-o
universal binary support acts like a bfd_archive but does not provide
a _close_and_cleanup function.  However, if a BFD appears as an
archive member, it must always remove its own entry from its parent's
map.  Otherwise, when the parent is destroyed, the already-destroyed
child BFD will be referenced.  mach-o does not use the usual archive
member support, so simply using _bfd_archive_close_and_cleanup (as
other targets do) will not work.

This patch fixes the problem by introducing a new
_bfd_unlink_from_archive_parent function, then arranging for it to be
called in the mach-o case.

Ok?

bfd/ChangeLog
2018-07-02  Jeff Muizelaar  <jrmuizel@gmail.com>
    Tom Tromey  <tom@tromey.com>

PR 13157
PR 21787
* mach-o.c (bfd_mach_o_fat_close_and_cleanup): New function.
(bfd_mach_o_close_and_cleanup): Redefine.
* archive.c (_bfd_unlink_from_archive_parent): New function,
extracted from..
(_bfd_archive_close_and_cleanup): ..here.
(bfd_generic_archive_p): Do not clear archive's format.
* libbfd-in.h (_bfd_unlink_from_archive_parent): Declare.
* libbfd.h: Regenerate.
bfd/ChangeLog
bfd/archive.c
bfd/libbfd-in.h
bfd/libbfd.h
bfd/mach-o.c
This page took 0.026558 seconds and 4 git commands to generate.