Commit graph

461 commits

Author SHA1 Message Date
Andrew Turner 1cd90a2c16 rtld: Move powerpc specific code to powerpc files
There are two variables set by dynamic tags in the powerpc runtime
linker. Now we have a way to split out architecture-specific dynamic
tags use it to handle these.

Reviewed by:	kib, jhibbits
Obtained from:	jhibbits (earlier version)
Sponsored by:	Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D45182
2024-05-17 09:37:23 +00:00
Andrew Turner dd4155bec7 rtld: Add arch_digest_dynamic
This will be used to handle the DT_AARCH64_VARIANT_PCS tag.

Reviewed by:	kib
Sponsored by:	Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D45117
2024-05-17 09:37:12 +00:00
Konstantin Belousov ef2694f368 rtld direct exec: make -u behavior match the description
Instead of only ignoring insecure env vars, clear them all.

Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44999
2024-04-30 03:26:49 +03:00
Konstantin Belousov d1cd0cc32b rtld: add direct-exec option -o
allowing to set any known LD_ parameter for the current rtld invocation,
but without polluting the activated' binary environment.  In other
words, the set parameter is not exported into the environment.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44988
2024-04-30 03:16:05 +03:00
Andrew Turner d8925a5f42 Support BTI in rtld
Read the elf note to decide when to set the guard page on arm64.

Reviewed by:	kib
Sponsored by:	Arm Ltd
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D39452
2024-04-12 14:30:44 +00:00
Stefan Eßer c44bf7d2e9 rtld: reduce debug messages after fix on big-endian hosts
Remove a debug message that had been added to support the debugging
of a mis-detection of the hint files endianness on powerpc64.

MFC after:	3 days
2024-03-22 21:54:11 +01:00
Michael Tuexen da2d6e2815 rtld: fix check for endianess of elf hints file
Don't check if the elf hints file is in host byte order, but check
if it is in little endian by looking at the magic number.
This fixes rtld on big endian platforms.
Reviewed by:	se, kib (prior version of the patch)
Fixes:		7b77d37a56 ("rtld-elf: support either byte-order of hints")
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D44472
2024-03-22 14:50:25 +01:00
Stefan Eßer 173953182a rtld-elf: add some debug print statements
The byte-order independent code has been reported to fail on powerpc64.
Add some more debug statements to help identify the parametrs used and
to verify the correct operation of the byte-swap macros used..
2024-03-21 16:31:49 +01:00
Stefan Eßer 7b77d37a56 rtld-elf: support either byte-order of hints file
Accept either little-endian or big-endian representation of the ELF
hints magic number in the header of a hints file and convert the
parameters to the native byte-order of the repsective system.

This is a pre-requisite for a planned change to always write the byte
order in little-endian format on all architectures. The only relvant
architecture that uses big-endian data is powerpc64, and it is not
likely that new architectures will choose that representation of data
in memory.

When all supported architectures use little-endian data in the hints
file, the byte swap logic can be enabled for big-endian CPUs at
compile time. Up to that point, there is a very small run-time penalty
that is paid on all systems to check the byte-order of the hints file
and to provide the option to byte-swap the parameters read from the
hints file header.

This commit contains the changes from review D44080 (which had been
split off from this patch for easier review),

Reviewed by:	kib
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D44053
2024-02-26 23:18:12 +01:00
Konstantin Belousov 452c5e9995 fdlopen(3): do not create a new object mapping if already loaded
This is expected behavior for both dlopen(3) and fdlopen(3).

PR:	277169
Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44019
2024-02-22 03:27:09 +02:00
Konstantin Belousov 968a18975a rtld: ignore load_filtees() calls if we already loading filtees for the obj
in addition to avoiding it for already loaded filtees. Issue is that
during load, rtld needs to resolve some special ABI symbols, like
executable stack fixer and static TLS initializer, which might trigger
recursion.

Example is libthr which is filter for libsys, and which exports
__pthread_distribute_static_tls.

Tested by:	kevans, krion
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D43858
2024-02-13 16:24:01 +02:00
Konstantin Belousov 30b5f6b33b rtld load_filtees(): reindent and reduce block nesting
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D43858
2024-02-13 16:23:55 +02:00
Konstantin Belousov 9ea864b54b rtld symlook_obj: move common code to check filtees into helper
Revieved by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D43858
2024-02-13 16:23:41 +02:00
Mark Johnston e7951d0b04 rtld-elf: Avoid unnecessary lock_restart_for_upgrade() calls
In order to atomically upgrade the rtld bind lock, load_filtees() may
trigger a longjmp back to _rtld_bind() so that the binding can be done
with the write lock held.  However, the write lock is only needed when
filtee objects haven't already been loaded, so move the
lock_restart_for_upgrade() call to avoid unnecessary lock upgrades when
a filtee is defined.

Reviewed by:	kib
Tested by:	brooks
MFC after:	1 week
Sponsored by:	Innovate UK
2024-02-02 09:39:54 -05:00
Konstantin Belousov 9daf6cd0f4 RTLD_DEEPBIND: make lookup not just symbolic, but walk all refobj' DAGs
before starting the walk over the global list.  Effectively we visit
needed objects first as well, instead of just the object itself.
This seems to better match the semantic offered by the glibc flag.

Reported by:	kevans
PR:	275393
Reviewed by:	kevans
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D42841
2023-11-30 21:34:36 +02:00
Warner Losh a6fe717c2a libexec: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:23:59 -07:00
Stephen J. Kiernan 95335dd3c1 rtld: introduce STATIC_TLS_EXTRA
The new STATIC_TLS_EXTRA variable provides a means for applications
to increases the size of the extra static TLS space allocated by
rtld beyond the default of '128'. This extra static TLS space is used
for objects loaded with dlopen.

The value specified in the variable must be no less than the default
value and no greater than the maximum allowed value for size_t type.

If an invalid value is specified, rtld will ignore it and just use
the default value.

The rtld(1) man page is updated to document this new option.

Obtained from:  Juniper Networks, Inc.
Differential Revision:  https://reviews.freebsd.org/D42025
2023-10-30 13:42:05 -04:00
Konstantin Belousov 72d97e1dd9 rtld: output rtld errors into the dbg channel
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2023-09-15 22:40:41 +03:00
Konstantin Belousov feaae6ba1a rtld: switch from malloc_aligned() to __crt_aligned_alloc()
Use regular free(), since it works now.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41150
2023-08-21 17:16:42 +03:00
Konstantin Belousov b1d3e2b771 rtld: unlock bind lock when calling into crt __pthread_distribute_static_tls method
The method might require resolving and binding symbols, which means
recursing on the bind lock. It is safe to unlock the bind lock,
since we operate on the private object list, and user attempting to
unload an object from the list of not yet fully loaded objects caused
self-inflicted race.

It is similar to how we treat user' init/fini methods.

Reported by:	stevek
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2023-08-20 15:30:13 +03:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Konstantin Belousov 1005d3d053 rtld: fix dlopen() for an object that is already mapped but not yet initialized
For instance, dso might be mapped as needed but not yet initialized from
the other subtree of needed objects, while current object' constructor
does dlopen() for the dso.  Right now rtld does relocations and other
processing based on the arrival of new objects in the global list, which
is not happens there.  Directly check for the initialization state of
the object, for which we would return the handle.

One practical use case of this support is e.g. dlopen("libthr.so",
RTLD_NOLOAD) by libraries that are threading-aware but happy to live
with libc pthread shims if the program is not multithreaded.

Reviewed by:	tijl
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
2023-07-16 15:34:34 +03:00
Jessica Clarke f940929d41 rtld-elf: Migrate from COMPAT_32BIT to generic COMPAT_libcompat/LIBCOMPAT
We still have a tiny amount of libcompat-specific code in rtld_paths.h,
but it's been deduplicated as much as possible, and in future we may
wish to just push these variables down to the few consumers of them and
make them use the double-underscore variants with a libcompat argument
rather than give them names here.

See commit 8fad2cda93 ("bsd.compat.mk: Provide new CPP and sub-make
variables") for the context behind this change.

Reviewed by:	kib, brooks, jhb
Differential Revision:	https://reviews.freebsd.org/D40925
2023-07-09 18:49:07 +01:00
John Baldwin 38efd4df23 rtld-elf: Mark tls_init_align __unused in free_tls for Variant I TLS.
Some architectures (powerpc and RISC-V) always use 0 for the post TLS
size in which case tls_init_align isn't used by
calculate_tls_post_size.  Use __unused to quiet the warning for these
platforms.

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D40673
2023-06-20 12:38:48 -07:00
Konstantin Belousov e9a38ed2fa rtld: fix allocate_module_tls() variant I fallback to static allocation
Submitted by:	Joerg Sonnenberger
Fixes:	91880e07f605edb90339685bc934699a4344de3bESC
MFC after:	1 week
2023-06-09 15:13:46 +03:00
Konstantin Belousov 91880e07f6 rtld: do not allow both dynamic DTV index and static TLS offset
If we are allocating static offset for an object with dynamic index,
return failure.  In the opposite case, if dynamic index is requested for
statically allocated TLS area, directly use the offset instead of
setting the index.

Taken from NetBSD Joerg Sonnenberger change for src/libexec/ld.elf_so/tls.c
rev. 1.18.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2023-06-05 22:33:18 +03:00
Konstantin Belousov 283a4f4097 rtld: rename tls_done to tls_static
The meaning of the flag is that static TLS allocation was done.

Taken from NetBSD Joerg Sonnenberger change for src/libexec/ld.elf_so/tls.c
rev. 1.18.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2023-06-05 22:33:17 +03:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix
2023-05-12 10:44:03 -06:00
Konstantin Belousov 7cdfe51f30 rtld: fixes for handling of the grouped options
Do not terminate scanning group when 'd' is encountered.
The 'b' option must be last in the group, same as 'f'.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D39544
2023-04-13 17:37:33 +03:00
Ed Maste 29e3a06510 rtld: fix SysV hash function overflow
Quoting from https://maskray.me/blog/2023-04-12-elf-hash-function:

The System V Application Binary Interface (generic ABI) specifies the
ELF object file format. When producing an output executable or shared
object needing a dynamic symbol table (.dynsym), a linker generates a
.hash section with type SHT_HASH to hold a symbol hash table. A DT_HASH
tag is produced to hold the address of .hash.

The function is supposed to return a value no larger than 0x0fffffff.
Unfortunately, there is a bug. When unsigned long consists of more than
32 bits, the return value may be larger than UINT32_MAX. For instance,
elf_hash((const unsigned char *)"\xff\x0f\x0f\x0f\x0f\x0f\x12") returns
0x100000002, which is clearly unintended, as the function should behave
the same way regardless of whether long represents a 32-bit integer or
a 64-bit integer.

Reviewed by:	kib, Fangrui Song
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39517
2023-04-12 12:44:34 -04:00
Ed Maste 87443cb6c6 rtld: reindent and style(9) elf_hash()
In preparation for an upcoming change (D39517).

Sponsored by:	The FreeBSD Foundation
2023-04-12 12:43:36 -04:00
John Baldwin b069d3e019 rtld: Revert "When loading dso without PT_GNU_STACK phdr, only call"
After the removal of ia64 and sparc64, all current architectures
support executable stacks at an architectural level.

This reverts commit 1290d38ac5.

Reviewed by:	kib
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D37904
2023-01-04 14:55:00 -08:00
Konstantin Belousov 8cc44a1e59 rtld: add support for the $LIB token
similar to the same token in glibc.

Requested and reviewed by:	bapt
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37329
2022-11-11 12:06:30 +02:00
Konstantin Belousov f585d13dd6 rtld: remove unused macro FPTR_TARGET
It is a remnant from the ia64 removal

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
2022-10-22 05:15:06 +03:00
Konstantin Belousov 2f72ee987d 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
2022-09-22 14:20:13 +03:00
Konstantin Belousov daa85548d5 rtld: teach LD_SHOW_AUXV about AT_USRSTACK*
Reviewed by:	brooks, imp (previous version)
Discussed with:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D36540
2022-09-16 23:24:18 +03:00
Konstantin Belousov 832b40f7ff rtld.c: remove extra blank line
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D36396
2022-09-04 07:28:02 +03:00
Konstantin Belousov 008475d3c8 rtld: fix display of the mapbase for the traced objects
Commit 24d0c9c1f5 introduced the following regression:
% 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)
Note that the base address is the same for all displayed libraries.

Fix it by passing correct object to trace_print_obj().

Fixes:	24d0c9c1f5
Reviewed by:	jrtc27
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Differential revision:	https://reviews.freebsd.org/D36198
2022-08-14 18:11:06 +03:00
Jessica Clarke becd9908be rtld-elf: Fix leaks and wild frees in origin_subst
55abf23dd3 inverted the value passed to origin_subst_one when rolling
up the existing code into a loop. If the first token is found ($ORIGIN),
this results in a wild free of part of strtab. Processing the second
token works fine and will act how the first should have regardless of
whether found, allocating memory for the string without freeing.
Processing subsequent tokens however will then leak, regardless of
whether found, as they will also believe they need to allocate memory
and can't free the string.

Found by:	CHERI
Reviewed by:	kib, markj
Fixes:		55abf23dd3 ("rtld: make token substitution table-driven")
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D35792
2022-07-12 17:47:47 +01:00
Andrew Turner e85eaa9308 Have rtld query the page size from the kernel
To allow for a dynamic page size on arm64 have the runtime linker
query the kernel for the currentl page size.

Reviewed by:	kib
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D34765
2022-04-07 15:37:37 +01:00
Konstantin Belousov 0913953c9e rtld: trace preloaded objects
Before, preloaded objects, if not listed as needed for any normally
linked objects, were silently ignored.

Preloaded objects are printed with the `[preloaded]` herald. The list
includes the objects not listed explicitly as recursive dependencies of
the main object, effectively dsos loaded by LD_PRELOAD mechanism.
vdso is listed as well, since it is not needed by anything.

Since there is no DT_NEEDED entry for LD_PRELOADed objects, they are
usually printed using LD_TRACE_LOADED_OBJECTS_FTM2 format due to the
failure of the heuristic based on the presence of the 'lib' prefix.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34716
2022-04-01 03:51:22 +03:00
Konstantin Belousov db0372808a rtld trace_loaded_objects(): use bool for the list_containers variable
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34716
2022-04-01 03:51:22 +03:00
Konstantin Belousov b0bc8cc705 rtld: use style(9) for trace_loaded_objects()
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34716
2022-04-01 03:51:22 +03:00
Konstantin Belousov 24d0c9c1f5 rtld: extract printer for a single traced object into a helper
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34716
2022-04-01 03:51:21 +03:00
Konstantin Belousov c25f36f630 rtld: extract calculation of the format strings for trace into a helper
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34716
2022-04-01 03:51:21 +03:00
Konstantin Belousov b1b5174123 rtld: Add ${TOKEN} aliases to $TOKEN
it seems that glibc supports them, and such spelling is mentioned in the
ld.bfd manual. Idea seems to auto-correct some quoting/makefile sytnax
errors on linker command line.

Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34247
2022-02-11 20:01:20 +02:00
Konstantin Belousov 55abf23dd3 rtld: make token substitution table-driven
Reviewed by:	emaste, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D34247
2022-02-11 20:00:47 +02:00
John Baldwin d33d8f7ef4 rtld: Fix assertion I broke.
textrel is a bool, not a pointer.

Pointy hat to:	jhb
Reported by:	kevans
Fixes:		6a2d75d248 rtld: Trim a few more MIPS #ifdefs.
2022-01-27 14:42:40 -08:00
John Baldwin 6a2d75d248 rtld: Trim a few more MIPS #ifdefs.
Reviewed by:	imp, kib
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D34052
2022-01-27 11:00:24 -08:00
Konstantin Belousov 2bf21b0e8e Restore DT_DEBUG processing
Pointed out by:	kevans
Fixes:	292cba9b49
Sponsored by:	The FreeBSD Foundation
2022-01-06 09:14:09 +02:00