]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ld/Linux: determine program name in a more reliable manner
authorJan Beulich <jbeulich@suse.com>
Fri, 12 Dec 2025 07:01:53 +0000 (08:01 +0100)
committerJan Beulich <jbeulich@suse.com>
Fri, 12 Dec 2025 07:01:53 +0000 (08:01 +0100)
What argv[0] holds can be pretty arbitrary. As long as it's used for just
diagnostics, that may be pretty okay to go from, but ld also uses it to
find linker scripts. For that we want to be sure we start from the real
executable name. Which on Linux we can determine from the /proc/self/exe
symbolic link target (provided of course procfs is mounted).

While there constify program_name as well.

ld/ldmain.c
ld/ldmain.h

index 157f205671efa1715f5eb119de0e3d24512fc4a0..afffdd2e43de5f59affa187734fcb3e1f8a383c4 100644 (file)
@@ -67,7 +67,7 @@ char *default_target;
 const char *output_filename = "a.out";
 
 /* Name this program was invoked by.  */
-char *program_name;
+const char *program_name;
 
 /* The prefix for system library directories.  */
 const char *ld_sysroot;
@@ -567,6 +567,19 @@ report_phases (FILE * file, time_t * start, char ** argv)
   fflush (file);
 }
 
+static void
+set_program_name (const char *argv0)
+{
+  program_name = argv0;
+
+#if defined (__linux__) && _POSIX_VERSION >= 200112L
+  char name[PATH_MAX];
+  ssize_t len = readlink ("/proc/self/exe", name, ARRAY_SIZE (name));
+  if (len > 0 && (size_t)len < ARRAY_SIZE (name))
+    program_name = xmemdup (name, len, len + 1);
+#endif
+}
+
 int
 main (int argc, char **argv)
 {
@@ -581,7 +594,7 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
-  program_name = argv[0];
+  set_program_name (argv[0]);
   xmalloc_set_program_name (program_name);
 
   /* Check the LD_STATS environment variable before parsing the command line
index 0baa3c305c3dbc45045c5b4779aaa95620bf7c10..ec33b496f868526bacfe1bd1792ea0765448cb55 100644 (file)
@@ -21,7 +21,7 @@
 #ifndef LDMAIN_H
 #define LDMAIN_H
 
-extern char *program_name;
+extern const char *program_name;
 extern const char *ld_sysroot;
 extern char *ld_canon_sysroot;
 extern int ld_canon_sysroot_len;