From 9232ff64d1b2b9fa8031c8bd6785296569e8c021 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/NEWS | 5 ++ binutils/ar.c | 85 ++++++++++++++++++++++++-- binutils/binemul.c | 53 +++++++++++----- binutils/binemul.h | 15 +++-- binutils/doc/binutils.texi | 19 ++++-- binutils/emul_aix.c | 16 +---- binutils/testsuite/binutils-all/ar.exp | 41 +++++++++++++ 7 files changed, 189 insertions(+), 45 deletions(-) diff --git a/binutils/NEWS b/binutils/NEWS index 35e4e303e1..02a19ea09b 100644 --- a/binutils/NEWS +++ b/binutils/NEWS @@ -1,5 +1,10 @@ -*- text -*- +* The ar tool's previously unused l modifier is now used for specifying + dependencies of a static library. The arguments of this option + (or --record-libdeps long form option) will be stored verbatim in the + __.LIBDEP member of the archive, which the linker may read at link time. + * Readelf can now display the contents of LTO symbol table sections when asked to do so via the --lto-syms command line option. diff --git a/binutils/ar.c b/binutils/ar.c index 85b342a650..60fc51efcf 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; @@ -166,6 +171,7 @@ static struct option long_options[] = {"target", required_argument, NULL, OPTION_TARGET}, {"version", no_argument, &show_version, 1}, {"output", required_argument, NULL, OPTION_OUTPUT}, + {"record-libdeps", required_argument, NULL, 'l'}, {NULL, no_argument, NULL, 0} }; @@ -329,6 +335,7 @@ usage (int help) fprintf (s, _(" generic modifiers:\n")); fprintf (s, _(" [c] - do not warn if the library had to be created\n")); fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n")); + fprintf (s, _(" [l LIBDEPS] - specify dependencies of this library\n")); fprintf (s, _(" [S] - do not build a symbol table\n")); fprintf (s, _(" [T] - make a thin archive\n")); fprintf (s, _(" [v] - be verbose\n")); @@ -336,6 +343,7 @@ usage (int help) fprintf (s, _(" @ - read options from \n")); fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n")); fprintf (s, _(" --output=DIRNAME - specify the output directory for extraction operations\n")); + fprintf (s, _(" --record-libdeps=LIBDEPS - specify dependencies of this library\n")); #if BFD_SUPPORTS_PLUGINS fprintf (s, _(" optional:\n")); fprintf (s, _(" --plugin

- load the specified plugin\n")); @@ -487,7 +495,7 @@ decode_options (int argc, char **argv) argv = new_argv; } - while ((c = getopt_long (argc, argv, "hdmpqrtxlcoOVsSuvabiMNfPTDU", + while ((c = getopt_long (argc, argv, "hdmpqrtxl:coOVsSuvabiMNfPTDU", long_options, NULL)) != EOF) { switch (c) @@ -535,6 +543,9 @@ decode_options (int argc, char **argv) operation = extract; break; case 'l': + if (Libdeps != NULL) + fatal (_("Libdeps specified more than once")); + Libdeps = optarg; break; case 'c': silent_create = 1; @@ -847,6 +858,50 @@ 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; + bfd_size_type reclen = strlen (Libdeps)+1; + + /* Create a bfd to contain the dependencies. + It inherits its type from arch, but we must + set the type to "binary" otherwise bfd_bwrite() + will fail. After writing, we must set the type + back to "plugin" otherwise adding it to the + archive will fail. */ + Libdeps_bfd = bfd_create (LIBDEPS, arch); + if (Libdeps_bfd == NULL) + fatal (_("Cannot create Libdeps record.")); + + if (bfd_find_target ("binary", Libdeps_bfd) == NULL) + fatal (_("Cannot set Libdeps record type to binary.")); + + if (! bfd_set_format (Libdeps_bfd, bfd_object)) + fatal (_("Cannot set Libdeps object format.")); + + if (! bfd_make_writable (Libdeps_bfd)) + fatal (_("Cannot make Libdeps object writable.")); + + if (bfd_bwrite (Libdeps, reclen, Libdeps_bfd) != reclen) + fatal (_("Cannot write Libdeps record.")); + + if (! bfd_make_readable (Libdeps_bfd)) + fatal (_("Cannot make Libdeps object readable.")); + + if (bfd_find_target ("plugin", Libdeps_bfd) == NULL) + fatal (_("Cannot reset Libdeps record type.")); + + /* Append our Libdeps record to the list of files + being operated on. */ + new_files = xmalloc ((file_count+2) * sizeof(char *)); + for (i=0; iarelt_data != NULL) { + bfd_boolean replaced; if (newer_only) { struct stat fsbuf, asbuf; @@ -1453,8 +1509,18 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick) after_bfd = get_pos_bfd (&arch->archive_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) + { + replaced = ar_emul_replace_bfd (after_bfd, Libdeps_bfd, + verbose); + } + else + { + replaced = ar_emul_replace (after_bfd, *files_to_move, + target, verbose); + } + if (replaced) { /* Snip out this entry from the chain. */ *current_ptr = (*current_ptr)->archive_next; @@ -1470,9 +1536,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..57b2e20b7a 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/doc/binutils.texi b/binutils/doc/binutils.texi index 6203fde887..32a5acc3b9 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -170,7 +170,7 @@ in the section entitled ``GNU Free Documentation License''. @c man title ar create, modify, and extract from archives @smallexample -ar [-]@var{p}[@var{mod}] [@option{--plugin} @var{name}] [@option{--target} @var{bfdname}] [@option{--output} @var{dirname}] [@var{relpos}] [@var{count}] @var{archive} [@var{member}@dots{}] +ar [-]@var{p}[@var{mod}] [@option{--plugin} @var{name}] [@option{--target} @var{bfdname}] [@option{--output} @var{dirname}] [@option{--record-libdeps} @var{libdeps}] [@var{relpos}] [@var{count}] @var{archive} [@var{member}@dots{}] ar -M [ archive_next = *after_bfd; *after_bfd = new_bfd; diff --git a/binutils/testsuite/binutils-all/ar.exp b/binutils/testsuite/binutils-all/ar.exp index 5a9d27c6d0..273a0a3909 100644 --- a/binutils/testsuite/binutils-all/ar.exp +++ b/binutils/testsuite/binutils-all/ar.exp @@ -715,6 +715,46 @@ proc many_files { } { pass $testname } +proc test_add_dependencies { } { + global AR + global AS + global srcdir + global subdir + global obj + + set testname "ar adding library dependencies" + + if ![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.${obj}] { + unresolved $testname + return + } + + if [is_remote host] { + set archive artest.a + set objfile [remote_download host tmpdir/bintest.${obj}] + remote_file host delete $archive + } else { + set archive tmpdir/artest.a + set objfile tmpdir/bintest.${obj} + } + + remote_file build delete tmpdir/artest.a + + set got [binutils_run $AR "-r -c $archive --record-libdeps /foo/bar ${objfile}"] + if ![string match "" $got] { + fail $testname + return + } + + set got [binutils_run $AR "-t $archive"] + if ![string match "*bintest.${obj} __.LIBDEP*" $got] { + fail $testname + return + } + + pass $testname +} + # Run the tests. # Only run the bfdtest checks if the programs exist. Since these @@ -743,6 +783,7 @@ move_an_element empty_archive extract_an_element many_files +test_add_dependencies if { [is_elf_format] && [supports_gnu_unique] } { unique_symbol -- 2.20.1