446cf2
commit 542923d949e8b2480e48bd85fea13cf5d00d30b7
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Thu Oct 8 15:33:00 2020 +0200
446cf2
446cf2
    elf: Implement ld.so --version
446cf2
    
446cf2
    This prints out version information for the dynamic loader and
446cf2
    exits immediately, without further command line processing
446cf2
    (which seems to match what some GNU tools do).
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/dl-main.h b/elf/dl-main.h
446cf2
index ac7249a580214860..b51256d3b48230b0 100644
446cf2
--- a/elf/dl-main.h
446cf2
+++ b/elf/dl-main.h
446cf2
@@ -105,6 +105,9 @@ call_init_paths (const struct dl_main_state *state)
446cf2
 _Noreturn void _dl_usage (const char *argv0, const char *wrong_option)
446cf2
   attribute_hidden;
446cf2
 
446cf2
+/* Print ld.so version information and exit.  */
446cf2
+_Noreturn void _dl_version (void) attribute_hidden;
446cf2
+
446cf2
 /* Print ld.so --help output and exit.  */
446cf2
 _Noreturn void _dl_help (const char *argv0, struct dl_main_state *state)
446cf2
   attribute_hidden;
446cf2
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
446cf2
index c1820dca2fa117ee..f3c5ac76d37f9c03 100644
446cf2
--- a/elf/dl-usage.c
446cf2
+++ b/elf/dl-usage.c
446cf2
@@ -20,6 +20,7 @@
446cf2
 #include <dl-main.h>
446cf2
 #include <ldsodefs.h>
446cf2
 #include <unistd.h>
446cf2
+#include "version.h"
446cf2
 
446cf2
 void
446cf2
 _dl_usage (const char *argv0, const char *wrong_option)
446cf2
@@ -32,6 +33,19 @@ _dl_usage (const char *argv0, const char *wrong_option)
446cf2
   _exit (EXIT_FAILURE);
446cf2
 }
446cf2
 
446cf2
+void
446cf2
+_dl_version (void)
446cf2
+{
446cf2
+  _dl_printf ("\
446cf2
+ld.so " PKGVERSION RELEASE " release version " VERSION ".\n\
446cf2
+Copyright (C) 2020 Free Software Foundation, Inc.\n\
446cf2
+This is free software; see the source for copying conditions.\n\
446cf2
+There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
446cf2
+PARTICULAR PURPOSE.\n\
446cf2
+");
446cf2
+  _exit (EXIT_SUCCESS);
446cf2
+}
446cf2
+
446cf2
 void
446cf2
 _dl_help (const char *argv0, struct dl_main_state *state)
446cf2
 {
446cf2
@@ -61,6 +75,7 @@ of this helper program; chances are you did not intend to run this program.\n\
446cf2
   --preload LIST        preload objects named in LIST\n\
446cf2
   --argv0 STRING        set argv[0] to STRING before running\n\
446cf2
   --help                display this help and exit\n\
446cf2
+  --version             output version information and exit\n\
446cf2
 ",
446cf2
               argv0);
446cf2
   _exit (EXIT_SUCCESS);
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index b92641cb1c2d99a6..da1eef108508b95f 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -1248,6 +1248,8 @@ dl_main (const ElfW(Phdr) *phdr,
446cf2
 	    --_dl_argc;
446cf2
 	    ++_dl_argv;
446cf2
 	  }
446cf2
+	else if (strcmp (_dl_argv[1], "--version") == 0)
446cf2
+	  _dl_version ();
446cf2
 	else if (_dl_argv[1][0] == '-' && _dl_argv[1][1] == '-')
446cf2
 	  {
446cf2
 	   if (_dl_argv[1][1] == '\0')