[PATCH v3 1/2] ld.so: Target-specific info in ldd and ld.so output

Yury Khrustalev yury.khrustalev@arm.com
Thu Nov 19 13:36:15 GMT 2026


Add new '--target-info' flag for 'ld.so': it only affects
tracing of loaded objects and results in printing extra
target-specific information for each module.

The ldd script gets a similar new option that changes its
behaviour in the following two ways:

 - LD_TRACE_LOADED_OBJECTS=2 (instead of usual 1) to include
   main program binary in the output
 - ld.so gets new flag '--target-info'

On some targets the result of dynamic loader may depend on
target-specific binary markings or other properties of the
loaded objects.

To make this information easier to access, we add support to
print this information: targets may implement new function
'_dl_trace_print_target_info()' that should print any target
specific properties on one line without any new line at the
end using _dl_printf.

Behaviour of the 'LD_VERBOSE' environment variable and the
'-v' option of the 'ldd' tool is changed: new target-specific
output in included in the verbose output ("verbose" means
"print all").

Target-specific info will be printed for static binaries too.

This patch implements printing of target-specific properties
for AArch64 and currently covers BTI, PAC and GCS but more
features can be added in the future.

On aarch64, the properties of ld.so and linux-vdso are not
printed because properties of these modules are not processed.
---
 elf/dl-main.h             |  3 +++
 elf/ldd.bash.in           | 18 +++++++++++++--
 elf/rtld.c                | 48 ++++++++++++++++++++++++++++++---------
 sysdeps/aarch64/dl-prop.h | 24 +++++++++++++++++++-
 sysdeps/aarch64/linkmap.h |  1 +
 sysdeps/generic/dl-prop.h |  5 ++++
 sysdeps/x86/dl-prop.h     |  5 ++++
 7 files changed, 90 insertions(+), 14 deletions(-)

diff --git a/elf/dl-main.h b/elf/dl-main.h
index 8f3063ce1d..d85b2781e9 100644
--- a/elf/dl-main.h
+++ b/elf/dl-main.h
@@ -99,6 +99,9 @@ struct dl_main_state
 
   /* True if information about versions has to be printed.  */
   bool version_info;
+
+  /* True if target-specific information needs to be printed.  */
+  bool target_info;
 };
 
 /* Helper function to invoke _dl_init_paths with the right arguments
diff --git a/elf/ldd.bash.in b/elf/ldd.bash.in
index bfc40f6505..3e4cbecc1b 100644
--- a/elf/ldd.bash.in
+++ b/elf/ldd.bash.in
@@ -29,8 +29,10 @@ TEXTDOMAINDIR=@TEXTDOMAINDIR@
 RTLDLIST=@RTLD@
 warn=
 bind_now=
+targetinfo=
 verbose=
 unused=
+ld_so_options=
 
 while test $# -gt 0; do
   case "$1" in
@@ -52,6 +54,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   -r, --function-relocs   process data and function relocations
   -u, --unused            print unused direct dependencies
   -v, --verbose           print all information
+  -t, --target-info       print target-specific properties
 "
     printf $"For bug reporting instructions, please see:\\n%s.\\n" \
       "@REPORT_BUGS_TO@"
@@ -69,6 +72,10 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     bind_now=yes
     shift
     ;;
+  -t | --target | --target-info)
+    targetinfo=yes
+    shift
+    ;;
   -v | --verb | --verbo | --verbos | --verbose)
     verbose=yes
     shift
@@ -101,7 +108,14 @@ nonelf ()
   return 1;
 }
 
-add_env="LD_TRACE_LOADED_OBJECTS=1 LD_WARN=$warn LD_BIND_NOW=$bind_now"
+if test "$targetinfo" = yes; then
+  ld_so_options="--target-info"
+  level=2
+else
+  level=1
+fi
+
+add_env="LD_TRACE_LOADED_OBJECTS=$level LD_WARN=$warn LD_BIND_NOW=$bind_now"
 add_env="$add_env LD_VERBOSE=$verbose"
 if test "$unused" = yes; then
   add_env="$add_env LD_DEBUG=\"$LD_DEBUG${LD_DEBUG:+,}unused\""
@@ -173,7 +187,7 @@ warning: you do not have execution permission for" "\`$file'" >&2
       }
       ;;
     0|2)
-      try_trace "$RTLD" "$file" || result=1
+      try_trace "$RTLD" $ld_so_options "$file" || result=1
       ;;
     *)
       echo 'ldd:' ${RTLD} $"exited with unknown exit code" "($ret)" >&2
diff --git a/elf/rtld.c b/elf/rtld.c
index 65c10edff2..ccbf9e450a 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -298,6 +298,7 @@ dl_main_state_init (struct dl_main_state *state)
   state->glibc_hwcaps_mask = NULL;
   state->mode = rtld_mode_normal;
   state->version_info = false;
+  state->target_info = false;
 }
 
 #ifndef HAVE_INLINED_SYSCALLS
@@ -1480,6 +1481,12 @@ dl_main (const ElfW(Phdr) *phdr,
 	  {
 	    state.mode = rtld_mode_list_diagnostics;
 
+	    --_dl_argc;
+	    ++_dl_argv;
+	  }
+	else if (! strcmp (_dl_argv[1], "--target-info"))
+	  {
+	    state.target_info = true;
 	    --_dl_argc;
 	    ++_dl_argv;
 	  }
@@ -2074,7 +2081,12 @@ dl_main (const ElfW(Phdr) *phdr,
 	  _exit (first != true);
 	}
       else if (! main_map->l_info[DT_NEEDED])
-	_dl_printf ("\tstatically linked\n");
+	{
+	  _dl_printf ("\tstatically linked");
+	  if (state.target_info)
+	    _dl_trace_print_target_info (main_map);
+	  _dl_printf ("\n");
+	}
       else
 	{
 	  for (l = state.mode_trace_program ? main_map : main_map->l_next;
@@ -2085,16 +2097,26 @@ dl_main (const ElfW(Phdr) *phdr,
 	    else if (strcmp (l->l_libname->name, l->l_name) == 0)
 	      /* Print vDSO like libraries without duplicate name.  Some
 		 consumers depend of this format.  */
-	      _dl_printf ("\t%s (0x%0*zx)\n",
-			  DSO_FILENAME (l->l_libname->name),
-			  (int) sizeof l->l_map_start * 2,
-			  (size_t) l->l_map_start);
+	      {
+		_dl_printf ("\t%s (0x%0*zx)",
+			    DSO_FILENAME (l->l_libname->name),
+			    (int) sizeof l->l_map_start * 2,
+			    (size_t) l->l_map_start);
+	        if (state.target_info)
+		  _dl_trace_print_target_info (l);
+		_dl_printf ("\n");
+	      }
 	    else
-	      _dl_printf ("\t%s => %s (0x%0*zx)\n",
-			  DSO_FILENAME (l->l_libname->name),
-			  DSO_FILENAME (l->l_name),
-			  (int) sizeof l->l_map_start * 2,
-			  (size_t) l->l_map_start);
+	      {
+		_dl_printf ("\t%s => %s (0x%0*zx)",
+			    DSO_FILENAME (l->l_libname->name),
+			    DSO_FILENAME (l->l_name),
+			    (int) sizeof l->l_map_start * 2,
+			    (size_t) l->l_map_start);
+	        if (state.target_info)
+		  _dl_trace_print_target_info (l);
+		_dl_printf ("\n");
+	      }
 	  }
 	}
 
@@ -2586,7 +2608,8 @@ process_envvars_secure (struct dl_main_state *state)
       || GLRO(dl_lazy) != 1
       || GLRO(dl_bind_not) != 0
       || state->mode != rtld_mode_normal
-      || state->version_info)
+      || state->version_info
+      || state->target_info)
     _exit (5);
 
   return skip_env;
@@ -2750,6 +2773,9 @@ warning: LD_PROFILE ignored because LD_PROFILE_OUTPUT not specified\n");
 	/* We use standard output if opening the file failed.  */
 	GLRO(dl_debug_fd) = STDOUT_FILENO;
     }
+  /* Verbose means "print all".  */
+  if (state->version_info)
+    state->target_info = true;
 }
 
 static int
diff --git a/sysdeps/aarch64/dl-prop.h b/sysdeps/aarch64/dl-prop.h
index 26fc639405..a8a5b40e77 100644
--- a/sysdeps/aarch64/dl-prop.h
+++ b/sysdeps/aarch64/dl-prop.h
@@ -56,12 +56,13 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
 	return 0;
 
       unsigned int feature_1 = *(unsigned int *) data;
+      l->l_mach.feature_1 = feature_1;
       if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
 	if (GLRO(dl_aarch64_cpu_features).bti)
 	  _dl_bti_protect (l, fd);
 
       if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
-	l->l_mach.gcs = 1;
+	l->l_mach.gcs = true;
 
       /* Stop if we processed the property note.  */
       return 0;
@@ -70,4 +71,25 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
   return 1;
 }
 
+/* The strings used in this function to describe each property
+   should be consistent output of readelf -n and readelf -A.
+   The list of properties is space-delimited, lower-case, and
+   should use hyphens when more than one word is required to
+   describe a property.
+  */
+static inline void
+_dl_trace_print_target_info (const struct link_map *l)
+{
+  unsigned int feature_1 = l->l_mach.feature_1;
+  if (feature_1 == 0)
+    return;
+  _dl_printf (":");
+  if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
+    _dl_printf (" bti");
+  if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
+    _dl_printf (" pac");
+  if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
+    _dl_printf (" gcs");
+}
+
 #endif /* _DL_PROP_H */
diff --git a/sysdeps/aarch64/linkmap.h b/sysdeps/aarch64/linkmap.h
index 2a1e9325ae..90460c3f94 100644
--- a/sysdeps/aarch64/linkmap.h
+++ b/sysdeps/aarch64/linkmap.h
@@ -25,4 +25,5 @@ struct link_map_machine
   bool bti_fail;	  /* Failed to enable Branch Target Identification.  */
   bool gcs;		  /* Guarded Control Stack marking.  */
   bool bti;		  /* Branch Target Identification marking.  */
+  unsigned int feature_1; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND.  */
 };
diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
index 6a1e1eeff8..767229da3a 100644
--- a/sysdeps/generic/dl-prop.h
+++ b/sysdeps/generic/dl-prop.h
@@ -36,6 +36,11 @@ _dl_open_check (struct link_map *m, int dlopen_mode)
 {
 }
 
+static inline void __attribute__ ((always_inline))
+_dl_trace_print_target_info (const struct link_map *m)
+{
+}
+
 /* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of L,
    processing of the properties continues until this returns 0.  */
 static inline int __attribute__ ((always_inline))
diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h
index a466eba331..fa9f4b52bd 100644
--- a/sysdeps/x86/dl-prop.h
+++ b/sysdeps/x86/dl-prop.h
@@ -81,6 +81,11 @@ _dl_open_check (struct link_map *m, int dlopne_mode)
 #endif
 }
 
+static inline void __attribute__ ((always_inline))
+_dl_trace_print_target_info (const struct link_map *m)
+{
+}
+
 /* Check the GNU property and return its value.  It returns:
    -1: Skip this note.
     0: Stop checking.
-- 
2.47.3



More information about the Libc-alpha mailing list