ld.so: Support showing target info in ldd output
Checks
| Context |
Check |
Description |
| redhat-pt-bot/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
| linaro-tcwg-bot/tcwg_glibc_build--master-arm |
success
|
Build passed
|
| redhat-pt-bot/TryBot-32bit |
success
|
Build for i686
|
| linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_glibc_check--master-arm |
success
|
Test passed
|
| linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 |
success
|
Test passed
|
Commit Message
The ldd script calls ld.so with a non empty LD_TRACE_LOADED_OBJECTS
env variable which prints dependencies of a program.
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 provide a macro that resolves to a
static inline function returning a string with the target specific
information that is printed after each object shown in the ldd output.
Also fix printing main executable name (currently an empty string is
printed with LD_TRACE_LOADED_OBJECTS=2.
---
elf/rtld.c | 15 ++++++++++-----
sysdeps/aarch64/dl-prop.h | 14 ++++++++++++++
2 files changed, 24 insertions(+), 5 deletions(-)
Comments
On 19/10/26 07:37, Yury Khrustalev wrote:
> The ldd script calls ld.so with a non empty LD_TRACE_LOADED_OBJECTS
> env variable which prints dependencies of a program.
>
> 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 provide a macro that resolves to a
> static inline function returning a string with the target specific
> information that is printed after each object shown in the ldd output.
>
> Also fix printing main executable name (currently an empty string is
> printed with LD_TRACE_LOADED_OBJECTS=2.
The main program name change issue is real, but it should be a separate patch.
There are some issues with this approach. With the following example running
on qemu (but native can work as well, it is just that I only have a debian
VM configured and it has not CONFIG_ARM64_BTI_KERNEL):
$ cat > lib.c <<'EOF'
int fn (void) { return 0; }
EOF
$ cat > prog.c <<'EOF'
int fn_bti (void);
int fn_gcs (void);
int fn_none (void);
int main (void) { return 0; }
EOF
# A dynamic PIE without DT_NEEDED: takes rtld's "statically linked" path.
$ cat > nodeps.c <<'EOF'
void
_start (void)
{
register long x8 __asm__ ("x8") = 93; /* __NR_exit */
register long x0 __asm__ ("x0") = 0;
__asm__ volatile ("svc #0" : : "r" (x8), "r" (x0));
}
EOF
$ aarch64-linux-gnu-gcc -mbranch-protection=pac-ret+bti -fPIC -shared -o libbti.so lib.c
$ aarch64-linux-gnu-gcc -mbranch-protection=pac-ret+bti -fPIC -shared -Wl,-z,gcs=always,-z,gcs-report=none -o libgcs.so lib.c
$ aarch64-linux-gnu-gcc -mbranch-protection=none -fPIC -shared -o libnone.so lib.c
$ aarch64-linux-gnu-gcc -mbranch-protection=pac-ret+bti -o prog prog.c -Wl,--no-as-needed -L. -lbti -lgcs -lnone
$ aarch64-linux-gnu-gcc -mbranch-protection=pac-ret+bti -nostdlib -pie -o nodeps nodeps.c
Expected GNU attributes:
Object Marking
libbti.so BTI, PAC
libgcs.so BTI, PAC, GCS
libnone.so none
prog BTI, PAC
nodeps BTI, PAC
ld.so BTI, PAC
vDSO BTI, PAC
$ export BUILDDIR=/path/to/glibc/build/
$ export LDSO=$BUILDDIR/elf/ld.so
The issues are:
1. (bti) depends on the CPU, (gcs) does not, PAC is never shown:
$ qemu-aarch64 -cpu max $LDSO --library-path $BUILDDIR:. --list ./prog
linux-vdso.so.1 (0x00007e0a8c72d000)
libbti.so => ./libbti.so (0x00007e0a8aed0000) (bti)
libgcs.so => ./libgcs.so (0x00007e0a8aea0000) (bti,gcs)
libnone.so => ./libnone.so (0x00007e0a8ae70000)
libc.so.6 => /home/azanella/Projects/glibc/build/aarch64-linux-gnu-review/libc.so.6 (0x00007e0a8ac70000) (bti)
/lib/ld-linux-aarch64.so.1 => /home/azanella/Projects/glibc/build/aarch64-linux-gnu-review/elf/ld.so (0x00007e0a8b730000)
$ qemu-aarch64 -cpu cortex-a72 $LDSO --library-path $BUILDDIR:. --list ./prog
linux-vdso.so.1 (0x000074f7bd384000)
libbti.so => ./libbti.so (0x000074f7bc3a0000)
libgcs.so => ./libgcs.so (0x000074f7bc370000) (gcs)
libnone.so => ./libnone.so (0x000074f7bc340000)
libc.so.6 => /home/azanella/Projects/glibc/build/aarch64-linux-gnu-review/libc.so.6 (0x000074f7bc140000)
/lib/ld-linux-aarch64.so.1 => /home/azanella/Projects/glibc/build/aarch64-linux-gnu-review/elf/ld.so (0x000074f7bd340000)
I would suggested store the raw feature_1 word in link_map_machine from
_dl_process_gnu_property, as x86 already does with l_x86_feature_1_and,
and print from that. This also covers PAC for free.
2. both ld.so and the vDSO never get a suffix
linux-vdso.so.1 (0x...)
libc.so.6 => .../libc.so.6 (0x...) (bti)
.../elf/ld.so (0x...)
And both are marked BTI, PAC in the table above.
3. ldd never shows the main program and fr BTI and GCS, the main program is the object
that matters most.
4. The ldd output format changes, which can be dangerous. At minimum it needs a NEWS
entry.
> ---
> elf/rtld.c | 15 ++++++++++-----
> sysdeps/aarch64/dl-prop.h | 14 ++++++++++++++
> 2 files changed, 24 insertions(+), 5 deletions(-)
>
> diff --git a/elf/rtld.c b/elf/rtld.c
> index 324393d606..9bbb15fb44 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -1338,6 +1338,10 @@ _dl_start_args_adjust (int skip_args, int skip_env)
> #endif
> }
>
> +#ifndef LD_TRACE_MACH_INFO
> +#define LD_TRACE_MACH_INFO(x) ""
> +#endif
Add a static inline stub to sysdeps/generic/dl-prop.h, next to _rtld_main_check
and _dl_open_check. The sysdeps/x86/dl-prop.h replaces the generic header entirely,
so it needs its own version.
That's also a good place for IBT/SHSTK from l_x86_feature_1_and, if x86 wants
the same output.
Also, LD_TRACE_MACH_INFO resembles LD_* environment variable. Something like
_dl_trace_print_props (const struct link_map *) is clearer.
> +
> static void
> dl_main (const ElfW(Phdr) *phdr,
> ElfW(Word) phnum,
> @@ -2074,7 +2078,7 @@ 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%s\n", LD_TRACE_MACH_INFO(main_map));
> else
> {
> for (l = state.mode_trace_program ? main_map : main_map->l_next;
> @@ -2085,15 +2089,16 @@ 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", l->l_libname->name,
> + _dl_printf ("\t%s (0x%0*zx)%s\n",
> + DSO_FILENAME (l->l_libname->name),
> (int) sizeof l->l_map_start * 2,
> - (size_t) l->l_map_start);
> + (size_t) l->l_map_start, LD_TRACE_MACH_INFO(l));
> else
> - _dl_printf ("\t%s => %s (0x%0*zx)\n",
> + _dl_printf ("\t%s => %s (0x%0*zx)%s\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);
> + (size_t) l->l_map_start, LD_TRACE_MACH_INFO(l));
> }
> }
>
> diff --git a/sysdeps/aarch64/dl-prop.h b/sysdeps/aarch64/dl-prop.h
> index 26fc639405..17a1b1f899 100644
> --- a/sysdeps/aarch64/dl-prop.h
> +++ b/sysdeps/aarch64/dl-prop.h
> @@ -70,4 +70,18 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
> return 1;
> }
>
> +#define LD_TRACE_MACH_INFO(l) _dl_get_mach_info_props(l)
> +
> +static inline const char *
> +_dl_get_mach_info_props(struct link_map *l)
> +{
> + if (l->l_mach.bti && l->l_mach.gcs)
> + return " (bti,gcs)";
> + if (l->l_mach.bti)
> + return " (bti)";
> + if (l->l_mach.gcs)
> + return " (gcs)";
> + return "";
> +}
Static strings don't scale, and returning one fixed string per combination needs 2^n
strings (4 today, 8 once PAC is added). A loop over a {bit, "name"} table that prints
through _dl_printf avoids that.
Also, I think it would be good to add some a minimal test.
> +
> #endif /* _DL_PROP_H */
Hello Adhemerval,
On Mon, Oct 26, 2026 at 02:29:04PM -0300, Adhemerval Zanella Netto wrote:
>
> On 19/10/26 07:37, Yury Khrustalev wrote:
> > The ldd script calls ld.so with a non empty LD_TRACE_LOADED_OBJECTS
> > env variable which prints dependencies of a program.
> >
> > 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 provide a macro that resolves to a
> > static inline function returning a string with the target specific
> > information that is printed after each object shown in the ldd output.
> >
> > Also fix printing main executable name (currently an empty string is
> > printed with LD_TRACE_LOADED_OBJECTS=2.
>
> The main program name change issue is real, but it should be a separate patch.
I will send a separate patch for this part.
> There are some issues with this approach. With the following example running
> on qemu (but native can work as well, it is just that I only have a debian
> VM configured and it has not CONFIG_ARM64_BTI_KERNEL):
Thanks for your comments, I'll prepare v2 with fixes.
Kind regards,
Yury
@@ -1338,6 +1338,10 @@ _dl_start_args_adjust (int skip_args, int skip_env)
#endif
}
+#ifndef LD_TRACE_MACH_INFO
+#define LD_TRACE_MACH_INFO(x) ""
+#endif
+
static void
dl_main (const ElfW(Phdr) *phdr,
ElfW(Word) phnum,
@@ -2074,7 +2078,7 @@ 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%s\n", LD_TRACE_MACH_INFO(main_map));
else
{
for (l = state.mode_trace_program ? main_map : main_map->l_next;
@@ -2085,15 +2089,16 @@ 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", l->l_libname->name,
+ _dl_printf ("\t%s (0x%0*zx)%s\n",
+ DSO_FILENAME (l->l_libname->name),
(int) sizeof l->l_map_start * 2,
- (size_t) l->l_map_start);
+ (size_t) l->l_map_start, LD_TRACE_MACH_INFO(l));
else
- _dl_printf ("\t%s => %s (0x%0*zx)\n",
+ _dl_printf ("\t%s => %s (0x%0*zx)%s\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);
+ (size_t) l->l_map_start, LD_TRACE_MACH_INFO(l));
}
}
@@ -70,4 +70,18 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
return 1;
}
+#define LD_TRACE_MACH_INFO(l) _dl_get_mach_info_props(l)
+
+static inline const char *
+_dl_get_mach_info_props(struct link_map *l)
+{
+ if (l->l_mach.bti && l->l_mach.gcs)
+ return " (bti,gcs)";
+ if (l->l_mach.bti)
+ return " (bti)";
+ if (l->l_mach.gcs)
+ return " (gcs)";
+ return "";
+}
+
#endif /* _DL_PROP_H */