Commit Graph

944 Commits

Author SHA1 Message Date
Ed Maste
ec1e83782d rtld: don't add extraneous -L directory when MK_TOOLCHAIN == no
rtld's Makefile used to add -L${LIBDIR} to LDFLAGS when MK_TOOLCHAIN was
no.  This was done as part of a change to fix building rtld with
MK_TOOLCHAIN == no (although I'm not sure this part was necessary).

In any case as of 5f2e84015d libc_pic.a is built independent of the
MK_TOOLCHAIN setting and the main part of the workaround has already
been removed.  Remove the rest now.

This reverts commit c0f5aeb032.

Reviewed by:	jrtc27
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D39938
2023-05-02 18:01:43 -04:00
Simon J. Gerraty
d9a4274795 Update/fix Makefile.depend for userland 2023-04-18 17:14:23 -07: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
7d8e1e8dd9 libcasper: Move helper libraries from /lib/casper to /lib.
These libraries are linked to directly by applications rather than
opened at runtime via dlopen().

Discussed with:	oshogbo
Reviewed by:	markj, emaste
Differential Revision:	https://reviews.freebsd.org/D39245
2023-03-29 15:04:28 -07:00
Konstantin Belousov
5942b4b6fd sys/param.h: Add _WANT_P_OSREL
Use it instead of defining IN_RTLD by base sources that want P_OSREL_
defines in userspace, but are not rtld.
This allows to remove abuse of IN_RTLD from userspace.

Reviewed by:	dchagin, markj, imp
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D38585
2023-02-15 02:43:18 +02: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
ca2560bd85 rtld: fix typo in comment
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
2022-10-17 17:10:03 +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
1251cf8a30 rtld-elf/libmap.c: correct comment
There is no intent to support TLS for rtld.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2022-09-19 01:49:41 +03:00
Konstantin Belousov
1fd4dec26e rtld-elf/libmap.h: style declarations
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2022-09-19 01:49:41 +03:00
Konstantin Belousov
3aed5ddb8b rtld-elf/libmap.h: add include guard
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2022-09-19 01:49:41 +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
7444f54bd3 rtld: style the rest of rtld_lock.c
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
a687683b99 rtld: mask signals for default read locks
Rtld locks from libthr defer signals delivery, which means that binding
is not possible while a signal handler is executed.

Binding might upgrade read-locked rtld_bind_lock to write-lock, if
symbol resolution requires loading filters.  If a signal would be delivered
while rtld is in read-locked section, and signal handler needs binding
which upgrades the lock, for non-threaded image that uses default rtld
locks, we get the rtld data structures modified under the top-level
active rtld frame.

To correct the problem, mask signals for read-locking of default locks
in addition to the write-locking.  It is very cheap now with
sigfastblock(2).

Note that the global state is used to track pre-locked state of either
sigfastblock(2) or signal mask (if sigfastblock(2) is administratively
disabled).  It is fine for non-threaded images since there are no other
threads.  But I believe that it is fine for threaded images using libc_r
as well, since masking signals disables preemption (I did not tested
it).

NetBSD PR:	https://gnats.netbsd.org/56979
Reported by:	tmunro
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
Gordon Bergling
3576233cd3 rtld(1): Fix a typo in a source code comment
- s/interger/integer/

MFC after:	3 days
2022-09-03 19:16:32 +02: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
John F. Carr
6c799530d8 rtld.1: Fix misplaced text
PR:		262194
Fixes:		f90218886f ("rtld: introduce PRELOAD_FDS")
MFC after:	3 days
2022-02-25 11:16:35 -05: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
Warner Losh
0d1f0898c9 rtld-elf: Remove libsoft support
Remove support for loading libsoft libraries.

Sponsored by:		Netflix
2022-01-06 22:44:07 -07: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
Konstantin Belousov
470b98e61d rtld: remove mips arch directory
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D33763
2022-01-06 06:00:39 +02:00
Konstantin Belousov
292cba9b49 rtld: remove mips-specific cases from generic code
Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D33763
2022-01-06 06:00:39 +02:00
John Baldwin
8bcdb144eb TLS: Use <machine/tls.h> for libc and rtld.
- Include <machine/tls.h> in MD rtld_machdep.h headers.

- Remove local definitions of TLS_* constants from rtld_machdep.h
  headers and libc using the values from <machine/tls.h> instead.

- Use _tcb_set() instead of inlined versions in MD
  allocate_initial_tls() routines in rtld.  The one exception is amd64
  whose _tcb_set() invokes the amd64_set_fsbase ifunc.  rtld cannot
  use ifuncs, so amd64 inlines the logic to optionally write to fsbase
  directly.

- Use _tcb_set() instead of _set_tp() in libc.

- Use '&_tcb_get()->tcb_dtv' instead of _get_tp() in both rtld and libc.
  This permits removing _get_tp.c from rtld.

- Use TLS_TCB_SIZE and TLS_TCB_ALIGN with allocate_tls() in MD
  allocate_initial_tls() routines in rtld.

Reviewed by:	kib, jrtc27 (earlier version)
Differential Revision:	https://reviews.freebsd.org/D33353
2021-12-09 13:23:05 -08:00
John Baldwin
299617496c amd64: Allocate TCB with alignment of 16 rather than 8.
This matches the TLS_TCB_ALIGN definition in libc.

Reviewed by:	kib, jrtc27
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33349
2021-12-09 13:16:45 -08:00
John Baldwin
23e0c0e9a3 mips: Add TLS_DTV_OFFSET to the result of tls_get_addr_common.
Previously TLS_DTV_OFFSET was added to the offset passed to
tls_get_addr_common; however, this approach matches powerpc and RISC-V
and better matches the intention.

Reviewed by:	kib, jrtc27
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33347
2021-12-09 13:16:19 -08:00
John Baldwin
03f6b14106 mips: Rename TLS_DTP_OFFSET to TLS_DTV_OFFSET.
This is the more standard name for the bias of dtv pointers used on
other platforms.  This also fixes a few other places that were using
the wrong bias previously on MIPS such as dlpi_tls_data in struct
dl_phdr_info and the recently added __libc_tls_get_addr().

Reviewed by:	kib, jrtc27
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33346
2021-12-09 13:16:00 -08:00
Konstantin Belousov
c210ef1347 rtld: load preloaded vdso
Reviewed by:	emaste
Discussed with:	jrtc27
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 month
Differential revision:	https://reviews.freebsd.org/D32960
2021-12-06 20:46:49 +02:00
Konstantin Belousov
63fc4e820c rtld: extract header validation into new helper check_elf_headers()
Reviewed by:	emaste
Discussed with:	jrtc27
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D32960
2021-12-06 20:46:49 +02:00
Konstantin Belousov
01c77a436e Pass vdso address to userspace
Reviewed by:	emaste
Discussed with:	jrtc27
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 month
Differential revision:	https://reviews.freebsd.org/D32960
2021-12-06 20:46:49 +02:00
Konstantin Belousov
f340188625 rtld_paths.h: Provide _PATH_ELF32_HINTS string, unconditionally
Reviewed by:	emaste
Tested by:	jbeich
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D33058
2021-11-25 01:55:03 +02:00
John Baldwin
b928e924f7 rtld-elf: Use _get_tp in __tls_get_addr for aarch64 and riscv64.
Reviewed by:	kib
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33047
2021-11-17 15:50:55 -08:00
Konstantin Belousov
b61bce17f3 rtld dump_auxv: be pedantic and distiguish between auxv union members based on format
Reviewed by:	jrtc27
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2021-11-13 22:13:10 +02:00
Konstantin Belousov
3a902ef253 rtld dump_auxv: consistently use long modifier for non-pointer auxv vals
Reviewed by:	jrtc27
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2021-11-13 22:12:33 +02:00
Konstantin Belousov
64ba1f4cf3 rtld: Implement LD_SHOW_AUXV
It dumps auxv as seen by interpreter, right before starting any user
code.

Copied from:	glibc
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2021-11-13 19:33:13 +02:00
Konstantin Belousov
77c088ab21 rtld: add rtld_fdprintfx()
It is same as rtld_fdprintf() without enforcement of the format string
to be a literal.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2021-11-13 19:33:13 +02:00