From a2ceba11321c89dc9cc3e874a8e1f5bb98d28b10 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 3 Sep 2020 17:34:16 +0100 Subject: [PATCH] Add ar support for dependencies Record -L/-l dependencies of a static library in a __.LIBDEP entry. Options should be passed literally as a single argument, e.g. ar rL "-L/usr/local/lib -lfoo -lbar" libnew.a obj1.o obj2.o --- binutils/ar.c | 59 +++++++++++++++++++++++++++++++++++++++------ binutils/binemul.c | 53 ++++++++++++++++++++++++++++------------ binutils/binemul.h | 15 +++++++----- binutils/emul_aix.c | 16 +++--------- 4 files changed, 101 insertions(+), 42 deletions(-) diff --git a/binutils/ar.c b/binutils/ar.c index 85b342a650..af554acdba 100644 --- a/binutils/ar.c +++ b/binutils/ar.c @@ -138,6 +138,11 @@ static bfd_boolean full_pathname = FALSE; /* Whether to create a "thin" archive (symbol index only -- no files). */ static bfd_boolean make_thin_archive = FALSE; +#define LIBDEPS "__.LIBDEP" +/* Dependencies to store in __.LIBDEP for linker to use */ +char *Libdeps; +static bfd * Libdeps_bfd; + static int show_version = 0; static int show_help = 0; @@ -487,7 +492,7 @@ decode_options (int argc, char **argv) argv = new_argv; } - while ((c = getopt_long (argc, argv, "hdmpqrtxlcoOVsSuvabiMNfPTDU", + while ((c = getopt_long (argc, argv, "hdmpqrtxlL:coOVsSuvabiMNfPTDU", long_options, NULL)) != EOF) { switch (c) @@ -536,6 +541,9 @@ decode_options (int argc, char **argv) break; case 'l': break; + case 'L': + Libdeps = optarg; + break; case 'c': silent_create = 1; break; @@ -847,6 +855,27 @@ main (int argc, char **argv) if (operation == extract && bfd_is_thin_archive (arch)) fatal (_("`x' cannot be used on thin archives.")); + if (Libdeps) + { + char **new_files; + + /* create a bfd to contain the arguments */ + Libdeps_bfd = bfd_create(LIBDEPS, arch); + bfd_find_target("binary", Libdeps_bfd); + bfd_set_format(Libdeps_bfd, bfd_object); + bfd_make_writable(Libdeps_bfd); + bfd_bwrite(Libdeps, strlen(Libdeps)+1, Libdeps_bfd); + bfd_make_readable(Libdeps_bfd); + bfd_find_target("plugin", Libdeps_bfd); + new_files = xmalloc((file_count+2) * sizeof(char *)); + for (i=0; iarchive_next, pos_after, bfd_get_filename (current)); - if (ar_emul_replace (after_bfd, *files_to_move, - target, verbose)) + if (Libdeps && FILENAME_CMP (normalize (*files_to_move, arch), + LIBDEPS) == 0) + { + changed |= ar_emul_replace_bfd (after_bfd, Libdeps_bfd, + verbose); + } + else + { + changed |= ar_emul_replace (after_bfd, *files_to_move, + target, verbose); + } + if (changed) { /* Snip out this entry from the chain. */ *current_ptr = (*current_ptr)->archive_next; - changed = TRUE; } goto next_file; @@ -1470,9 +1508,16 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick) /* Add to the end of the archive. */ after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL); - if (ar_emul_append (after_bfd, *files_to_move, target, - verbose, make_thin_archive)) - changed = TRUE; + if (Libdeps && FILENAME_CMP (normalize (*files_to_move, arch), LIBDEPS) == 0) + { + changed |= ar_emul_append_bfd (after_bfd, Libdeps_bfd, + verbose, make_thin_archive); + } + else + { + changed |= ar_emul_append (after_bfd, *files_to_move, target, + verbose, make_thin_archive); + } next_file:; diff --git a/binutils/binemul.c b/binutils/binemul.c index 7c71b5b78c..8a0512ebd9 100644 --- a/binutils/binemul.c +++ b/binutils/binemul.c @@ -41,9 +41,24 @@ ar_emul_default_usage (FILE *fp) bfd_boolean ar_emul_append (bfd **after_bfd, char *file_name, const char *target, bfd_boolean verbose, bfd_boolean flatten) +{ + bfd *new_bfd; + + new_bfd = bfd_openr (file_name, target); + AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); + if (bin_dummy_emulation.ar_append) + return bin_dummy_emulation.ar_append (after_bfd, new_bfd, + verbose, flatten); + + return FALSE; +} + +bfd_boolean +ar_emul_append_bfd (bfd **after_bfd, bfd *new_bfd, + bfd_boolean verbose, bfd_boolean flatten) { if (bin_dummy_emulation.ar_append) - return bin_dummy_emulation.ar_append (after_bfd, file_name, target, + return bin_dummy_emulation.ar_append (after_bfd, new_bfd, verbose, flatten); return FALSE; @@ -93,14 +108,9 @@ do_ar_emul_append (bfd **after_bfd, bfd *new_bfd, } bfd_boolean -ar_emul_default_append (bfd **after_bfd, char *file_name, - const char *target, bfd_boolean verbose, - bfd_boolean flatten) +ar_emul_default_append (bfd **after_bfd, bfd *new_bfd, + bfd_boolean verbose, bfd_boolean flatten) { - bfd *new_bfd; - - new_bfd = bfd_openr (file_name, target); - AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, any_ok); } @@ -108,23 +118,34 @@ bfd_boolean ar_emul_replace (bfd **after_bfd, char *file_name, const char *target, bfd_boolean verbose) { + bfd *new_bfd; + + new_bfd = bfd_openr (file_name, target); + AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); + if (bin_dummy_emulation.ar_replace) - return bin_dummy_emulation.ar_replace (after_bfd, file_name, - target, verbose); + return bin_dummy_emulation.ar_replace (after_bfd, new_bfd, + verbose); return FALSE; } bfd_boolean -ar_emul_default_replace (bfd **after_bfd, char *file_name, - const char *target, bfd_boolean verbose) +ar_emul_replace_bfd (bfd **after_bfd, bfd *new_bfd, + bfd_boolean verbose) { - bfd *new_bfd; + if (bin_dummy_emulation.ar_replace) + return bin_dummy_emulation.ar_replace (after_bfd, new_bfd, + verbose); - new_bfd = bfd_openr (file_name, target); - AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); + return FALSE; +} - AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, file_name); +bfd_boolean +ar_emul_default_replace (bfd **after_bfd, bfd *new_bfd, + bfd_boolean verbose) +{ + AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd)); new_bfd->archive_next = *after_bfd; *after_bfd = new_bfd; diff --git a/binutils/binemul.h b/binutils/binemul.h index d4a14edfeb..16c1f89062 100644 --- a/binutils/binemul.h +++ b/binutils/binemul.h @@ -30,15 +30,19 @@ extern void ar_emul_usage (FILE *); extern void ar_emul_default_usage (FILE *); extern bfd_boolean ar_emul_append (bfd **, char *, const char *, bfd_boolean, bfd_boolean); -extern bfd_boolean ar_emul_default_append (bfd **, char *, const char *, +extern bfd_boolean ar_emul_append_bfd (bfd **, bfd *, + bfd_boolean, bfd_boolean); +extern bfd_boolean ar_emul_default_append (bfd **, bfd *, bfd_boolean, bfd_boolean); extern bfd_boolean do_ar_emul_append (bfd **, bfd *, bfd_boolean, bfd_boolean, bfd_boolean (*)(bfd *)); extern bfd_boolean ar_emul_replace (bfd **, char *, const char *, bfd_boolean); -extern bfd_boolean ar_emul_default_replace (bfd **, char *, - const char *, bfd_boolean); +extern bfd_boolean ar_emul_replace_bfd (bfd **, bfd *, + bfd_boolean); +extern bfd_boolean ar_emul_default_replace (bfd **, bfd *, + bfd_boolean); extern bfd_boolean ar_emul_parse_arg (char *); extern bfd_boolean ar_emul_default_parse_arg (char *); @@ -61,9 +65,8 @@ typedef struct bin_emulation_xfer_struct { /* Print out the extra options. */ void (* ar_usage) (FILE *fp); - bfd_boolean (* ar_append) (bfd **, char *, const char *, bfd_boolean, - bfd_boolean); - bfd_boolean (* ar_replace) (bfd **, char *, const char *, bfd_boolean); + bfd_boolean (* ar_append) (bfd **, bfd *, bfd_boolean, bfd_boolean); + bfd_boolean (* ar_replace) (bfd **, bfd *, bfd_boolean); bfd_boolean (* ar_parse_arg) (char *); } bin_emulation_xfer_type; diff --git a/binutils/emul_aix.c b/binutils/emul_aix.c index c54e1083e5..726cb3b70c 100644 --- a/binutils/emul_aix.c +++ b/binutils/emul_aix.c @@ -67,30 +67,20 @@ check_aix (bfd *try_bfd) } static bfd_boolean -ar_emul_aix_append (bfd **after_bfd, char *file_name, const char *target, +ar_emul_aix_append (bfd **after_bfd, bfd *new_bfd, bfd_boolean verbose, bfd_boolean flatten) { - bfd *new_bfd; - - new_bfd = bfd_openr (file_name, target); - AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); - return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, check_aix); } static bfd_boolean -ar_emul_aix_replace (bfd **after_bfd, char *file_name, const char *target, +ar_emul_aix_replace (bfd **after_bfd, bfd *new_bfd, bfd_boolean verbose) { - bfd *new_bfd; - - new_bfd = bfd_openr (file_name, target); - AR_EMUL_ELEMENT_CHECK (new_bfd, file_name); - if (!check_aix (new_bfd)) return FALSE; - AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, file_name); + AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd)); new_bfd->archive_next = *after_bfd; *after_bfd = new_bfd; -- 2.20.1