public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Howard Chu <hyc@symas.com>
Cc: binutils@sourceware.org
Subject: Re: dependency list for static libraries
Date: Thu, 3 Sep 2020 21:42:59 +0100	[thread overview]
Message-ID: <e0c00779-d9e4-100a-6cb3-72b535d9e572@symas.com> (raw)
In-Reply-To: <64fe82bd-9c00-b232-98d2-f46182fb16ba@symas.com>

[-- Attachment #1: Type: text/plain, Size: 1248 bytes --]

Howard Chu wrote:
> Joseph Myers wrote:
>> You can install a linker script under a .a file name to achieve this
>> effect.  On x86_64, glibc's libm.a now looks like:
>>
>> /* GNU ld script
>> */
>> OUTPUT_FORMAT(elf64-x86-64)
>> GROUP ( /usr/lib64/libm-2.26.90.a /usr/lib64/libmvec.a )
> 
> Thanks for the suggestion, but that still means introducing additional/auxiliary files, so it has much the same drawback as libtool files. I also find this
> particularly gross because it breaks the principle of least surprise; when I see a *.a file I expect ar, nm, and ranlib to work on it. I'm kind of shocked that
> such a solution ever flew.
> 
Here's a proposed patch to ar to implement the first half of the solution:
storing the dependencies into a static library. It looks like I may be able
to use the linker plugin facility to handle the ld side of things. But it's
not clear to me that it won't clash with other plugins. I.e., if one plugin
claims an archive file, will that prevent other plugins from being able to
process it?

-- 
  -- Howard Chu
  CTO, Symas Corp.           http://www.symas.com
  Director, Highland Sun     http://highlandsun.com/hyc/
  Chief Architect, OpenLDAP  http://www.openldap.org/project/

[-- Attachment #2: 0001-Add-ar-support-for-dependencies.patch --]
[-- Type: text/x-patch, Size: 9612 bytes --]

From a2ceba11321c89dc9cc3e874a8e1f5bb98d28b10 Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@symas.com>
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; i<file_count; i++)
+            new_files[i] = files[i];
+          new_files[i++] = LIBDEPS;
+          file_count = i;
+          files = new_files;
+          files[i] = NULL;
+        }
+
       switch (operation)
 	{
 	case print_table:
@@ -1453,12 +1482,21 @@ 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)
+		    {
+		      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


  parent reply	other threads:[~2020-09-03 20:43 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-19 15:49 Howard Chu
2017-09-19 15:52 ` Simon Richter
     [not found]   ` <WM!bae999665f49907786872b93f01ac98d53e7b97e29b4228399d8baadf9ec0ab33db74467d73c998225b250ba1d00a4c0!@mailstronghold-3.zmailcloud.com>
2017-09-19 16:04     ` Howard Chu
2017-09-20  1:42       ` R0b0t1
2017-09-19 16:54 ` Joseph Myers
     [not found]   ` <WM!83b6ad7285aa96ce69fcd1944d4eae8f20e5f19dfbf161f45313f5393bcffe1b77231520b8f4e24145a3f85eeafb39ed!@mailstronghold-1.zmailcloud.com>
2017-09-19 22:01     ` Howard Chu
2017-09-20  0:20       ` Joseph Myers
2020-09-03 20:42       ` Howard Chu [this message]
2020-09-22 10:39         ` Nick Clifton
2020-09-22 11:42           ` Howard Chu
2020-09-22 13:12             ` Nick Clifton
2020-09-22 16:23               ` [PATCH] " Howard Chu
2020-09-22 17:16                 ` Fangrui Song
2020-09-22 17:55                   ` Howard Chu
2020-09-22 20:46                 ` Howard Chu
2020-09-23 11:52                   ` Nick Clifton
2020-09-23 15:29                     ` Howard Chu
2020-09-24  5:21                       ` Fangrui Song
2020-09-24  9:19                         ` Howard Chu
2020-09-24  9:30                           ` Howard Chu
2020-09-28 11:07                           ` Howard Chu
2020-10-28 14:56                     ` Howard Chu
2020-11-03 15:14                       ` Nick Clifton
2020-11-03 15:31                         ` Howard Chu
2020-11-08  1:39                           ` Alan Modra
2020-11-08 15:07                             ` Howard Chu
2020-11-09  0:01                               ` Alan Modra
2020-11-10  2:44                                 ` Howard Chu
2020-11-10 11:07                                   ` Alan Modra
2020-11-11 14:57                                     ` Howard Chu
2020-11-11 14:59                                       ` Howard Chu
2020-11-17 14:01                                         ` Nick Clifton
2020-11-04  0:33                         ` Howard Chu
2020-11-04 11:01                           ` Nick Clifton
2020-11-04 14:50                             ` Howard Chu
2020-11-06 12:38                               ` Nick Clifton
2020-11-13 14:40                               ` Howard Chu
2020-11-24 17:49                                 ` Howard Chu
2020-11-25 11:17                                   ` Nick Clifton
2020-12-01  0:08                                     ` Howard Chu
2020-12-14 14:28                                       ` Nick Clifton
2020-12-15 16:17                                         ` Jim Wilson
2020-12-15 16:22                                           ` Jeff Law
2020-12-15 16:50                                             ` Nick Clifton
2020-12-15 19:11                                               ` Jeff Law
2020-12-15 20:04                                                 ` Jim Wilson
2020-12-15 20:22                                               ` Cary Coutant
2020-12-15 20:51                                                 ` Howard Chu
2020-12-16 11:16                                                   ` Nick Clifton
2020-12-16 14:49                                                     ` [PATCH] ld: Call plugin hooks only if they are available H.J. Lu
2020-12-16 18:34                                                       ` Howard Chu
2020-12-16 18:40                                                         ` H.J. Lu
2020-12-16 19:06                                                           ` Howard Chu
2020-12-16 19:11                                                             ` [PATCH] ld: Skip libdep plugin if not all plugin hooks " H.J. Lu
2020-12-16 21:26                                                               ` Howard Chu
2020-12-16 21:47                                                                 ` H.J. Lu
2020-12-16 18:44                                                         ` [PATCH] ld: Call plugin hooks only if they " Howard Chu
2020-12-15 20:33                             ` [PATCH] dependency list for static libraries Cary Coutant
2020-12-15 20:53                               ` Howard Chu
2020-12-16 11:18                                 ` Nick Clifton
2020-12-23 13:27                         ` Matthias Klose
2020-12-23 18:23                           ` Howard Chu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e0c00779-d9e4-100a-6cb3-72b535d9e572@symas.com \
    --to=hyc@symas.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).