Bug 265750 - ldd(1) -f formatting polluted in 14-CURRENT, POLA violation
Summary: ldd(1) -f formatting polluted in 14-CURRENT, POLA violation
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-08-10 04:35 UTC by Michael Dexter
Modified: 2022-09-29 00:29 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Dexter freebsd_triage 2022-08-10 04:35:37 UTC
FreeBSD 13.X and previous provides highly-parsable ldd -f output such as:

ldd -f '%p\n' `which ls`
/lib/libutil.so.9
/lib/libncursesw.so.9
/lib/libc.so.7

Such output can easily be put into a shell loop or similar for processing.


On 14-CURRENT, 'ldd -f' adds additional, unactionable lines, limiting the use of formatting without additional line removal, which would require an OS version check because of the different behavior:

ldd -f '%p\n' `which ls`
/lib/libutil.so.9
/lib/libtinfow.so.9
/lib/libc.so.7
[preloaded]
        [vdso] (0x2952532395d0)

ldd -f '%p\n' `which which`
/lib/libc.so.7
[preloaded]
        [vdso] (0x1c674fa475d0)

I suggest that this additional information be removed from 14-CURRENT 'ldd -f' to restore the 13.X and previous behavior.

Thank you!
Comment 1 John Baldwin freebsd_committer freebsd_triage 2022-08-18 16:41:00 UTC
This is the same issue that Ed's recent Makefile.inc1 change had to workaround.
Linux's output does not add a separate [preloaded] section but instead just lists the vdso as a special entry without a path:

# ldd /usr/sbin/smartctl
	linux-vdso.so.1 (0x00007ffd9b9f8000)
	libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2563090000)
	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2562ec3000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2562ea9000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2562ce4000)
	libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f2562c4c000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2562c46000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f25631bf000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2562b00000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2562ade000)

Possibly one compromise would be to not emit the [preloaded] preamble.  But it also seems like the vdso output is not honoring the format string (though it seems like it should be based on the code?)
Comment 2 Ed Maste freebsd_committer freebsd_triage 2022-08-18 16:43:24 UTC
An OS version check is not necessary; you can apply the same sed/grep/whatever expression universally and it will just have no effect on earlier versions. Makefile.inc1 uses "grep -Ev '\[.*]'" for this.

That said the ability to elide the preloaded objects would be useful.
Comment 3 Ed Maste freebsd_committer freebsd_triage 2022-08-18 16:51:13 UTC
(In reply to John Baldwin from comment #1)

It's probably LD_TRACE_LOADED_OBJECTS_FMT1 vs LD_TRACE_LOADED_OBJECTS_FMT2 - perhaps -f '%p\n' -f '%p\n'
Comment 4 John Baldwin freebsd_committer freebsd_triage 2022-08-19 22:20:03 UTC
Ah, the FMT2 thing does make sense and while it's a bit odd, it's true that the existing scripts and use cases probably should be using two -f options.  I do think we should probably drop the '[preloaded]' line however.
Comment 5 Konstantin Belousov freebsd_committer freebsd_triage 2022-08-19 22:28:56 UTC
So what is the agreement there?

Add a new LD_ env var like LD_TRACE_SKIP_PRELOADED_OBJECTS and some
ldd(1) flag  to set it?

Drop the `[preloaded]` line?

Note that the options are independent.
Comment 6 John Baldwin freebsd_committer freebsd_triage 2022-08-19 23:54:44 UTC
I think I have two suggestions.  First, consider these outputs that we currently have:

> ldd /bin/ls
/bin/ls:
        libutil.so.9 => /lib/libutil.so.9 (0x1021000)
        libncursesw.so.9 => /lib/libncursesw.so.9 (0x1021000)
        libc.so.7 => /lib/libc.so.7 (0x1021000)
[preloaded]
        [vdso] (0x7ffffffff000)

> ldd -f "%o\n" -f "%o\n" /bin/ls
libutil.so.9
libncursesw.so.9
libc.so.7
[preloaded]
[vdso]

> ldd -f "%p\n" -f "%p\n" /bin/ls
/lib/libutil.so.9
/lib/libncursesw.so.9
/lib/libc.so.7
[preloaded]
[vdso]

I think probably we should omit "[preloaded]", and I would also use an empty path for the vdso so that in the '%p' case (which scripts use to find relevant libraries needed to be copied into a jail or some such) there is simply a blank line for the vdso.

I think it is fine that the vdso requires the second '-f' option given the meaning of the two options and that its name does not start with "lib".
Comment 7 Konstantin Belousov freebsd_committer freebsd_triage 2022-08-20 00:48:32 UTC
The '[preloaded]' line is not only about vdso, I added it because ldd started
printing all preloaded objects:
$ LD_PRELOAD=/lib/libthr.so.3 ldd /bin/ls
/bin/ls:
        libutil.so.9 => /lib/libutil.so.9 (0x80109a000)
        libncursesw.so.9 => /lib/libncursesw.so.9 (0x8010b1000)
        libc.so.7 => /lib/libc.so.7 (0x801124000)
[preloaded]
        [vdso] (0x7ffffffff000)
        /lib/libthr.so.3 (0x80106d000)

When I introduced the line, I thought that printing everything without
classification would cause more troubles than adding some new element into
the output.

Hm, I just checked what Linux does:
$ LD_PRELOAD=/lib64/libncurses.so.6 ldd /bin/ls
        linux-vdso.so.1 (0x00007ffc6f1ad000)
        /lib64/libncurses.so.6 (0x00007fd90f91b000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd90f8e3000)
        libcap.so.2 => /lib64/libcap.so.2 (0x00007fd90f8d9000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd90f6d7000)
        libtinfo.so.6 => /lib64/libtinfo.so.6 (0x00007fd90f6a8000)
        libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007fd90f60b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd90f96e000)
Comment 8 Konstantin Belousov freebsd_committer freebsd_triage 2022-09-18 00:57:12 UTC
The discussion stalled, so I implemented the best I could think of, in
https://reviews.freebsd.org/D36616
Comment 9 commit-hook freebsd_committer freebsd_triage 2022-09-22 11:21:05 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2f72ee987d492b3dc9116c5cf49777edd2474b11

commit 2f72ee987d492b3dc9116c5cf49777edd2474b11
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-18 00:49:30 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-22 11:20:13 +0000

    ldd: remove '[preloaded]' marker for the preloaded objects

    for the default output.  For '-a' (per-object needed printout) the
    [preloaded] banner is kept.

    Instead, use special format2 for printing the preloaded objects (and
    vdso), which does not include DT_NEEDED, since there is no object
    needing the printed one.

    In this way, the output is more compatible with glibc.

    Example:
    LD_PRELOAD=/lib/libthr.so.3 LD_TRACE_LOADED_OBJECTS=1 /libexec/ld-elf.so.1 /bin/ls
            libutil.so.9 => /lib/libutil.so.9 (0x801099000)
            libncursesw.so.9 => /lib/libncursesw.so.9 (0x8010b0000)
            libc.so.7 => /lib/libc.so.7 (0x801123000)
            [vdso] (0x7ffffffff000)
            /lib/libthr.so.3 (0x80106c000)
    Note the absense of the part before and including '=>' for preloaded
    libthr.so.3, and for vdso.

    PR:     265750
    Reviewed by:    jhb
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D36616

 libexec/rtld-elf/rtld.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Comment 10 Michael Dexter freebsd_triage 2022-09-22 18:25:30 UTC
Thank you for your attention to this! I will close it and invite you to re-open it if appropriate.
Comment 11 commit-hook freebsd_committer freebsd_triage 2022-09-29 00:29:52 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6958ee33b7426976bcafa3e743ae6f18e87cc3e4

commit 6958ee33b7426976bcafa3e743ae6f18e87cc3e4
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-18 00:49:30 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-29 00:29:09 +0000

    ldd: remove '[preloaded]' marker for the preloaded objects

    PR:     265750

    (cherry picked from commit 2f72ee987d492b3dc9116c5cf49777edd2474b11)

 libexec/rtld-elf/rtld.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)