Commit graph

1018 commits

Author SHA1 Message Date
Konstantin Belousov
69b6dc0b92 libc/gen: split user-visible opendir()-like functions into separate source files
(cherry picked from commit d40daefca64750c1076822bdbd3c409a9519f513)
2025-03-04 06:19:08 +02:00
Konstantin Belousov
068de5be49 rtld: clear any holes in the struct utrace_rtld passed to kernel logger
PR:	284563

(cherry picked from commit e917958c36670131ab42e8f2c849b708a3216e37)
2025-02-08 02:26:55 +02:00
Konstantin Belousov
f10a06556f rtld-elf/{amd64,i386}/reloc.c: remove unneeded #ifdef dbg
(cherry picked from commit e3035c52f25356299abc8e08be9221032967554d)
2025-01-23 11:46:58 +02:00
Konstantin Belousov
57820508a0 rtld-elf/rtld.c: fix typo in comment
(cherry picked from commit dd1d72961b8d343b29480b74afcfe4292fd166af)
2025-01-23 11:46:57 +02:00
Konstantin Belousov
73cc71607d rtld: drop RTLD_INIT_PAGESIZES_EARLY
(cherry picked from commit 5bc24fd7a21ffd98fe978b4eb09a383c09edbe37)
2024-11-27 13:31:55 +02:00
Kyle Evans
708106fb80 rtld: implement _dl_iterate_phdr_locked
Some sanitizers need to be able to use dl_iterate_phdr() after stopping
the rest of the process, but it's very hard to do so reliably as a
non-participant in the main logic of the program.

Introduce _dl_iterate_phdr_locked to bypass the locking that's normally
required for dl_iterate_phdr() and slap some scary warning on it.  It
will remain undocumented and probably shouldn't be used for anything
else.

Reviewed by:	kib

(cherry picked from commit 1426fd6cff0603f0ee275b99f2ba35dc36f3d0c2)
2024-11-26 19:15:56 -06:00
R. Christian McDonald
d39e0bdc6b rtld/arm: fix initial-exec (IE) thread-local storage relocation
net/frr[89] revealed an interesting edge-case on arm when dynamically
linking a shared library that declares more than one static TLS variable
with at least one  using the "initial-exec" TLS model. In the case
of frr[89], this library was libfrr.so which essentially does the
following:

	#include <stdio.h>

	#include "lib.h"

	static __thread int *a
		__attribute__((tls_model("initial-exec")));

	void lib_test()
	{
		static __thread int b = -1;

		printf("&a = %p\n", &a);
		printf(" a = %p\n", a);

		printf("\n");

		printf("&b = %p\n", &b);
		printf(" b = %d\n", b);
	}

Allocates a file scoped `static __thread` pointer with
tls_model("initial-exec") and later a block scoped TLS int. Notice in
the above minimal reproducer, `b == -1`. The relocation process does
the wrong thing and ends up pointing both `a` and `b` at the same place
in memory.

The output of the above in the broken state is:

	&a = 0x4009c018
	 a = 0xffffffff

	&b = 0x4009c018
	 b = -1

With the patch applied, the output becomes:

	&a = 0x4009c01c
	 a = 0x0

	&b = 0x4009c018
	 b = -1

Reviewed by:	kib
Approved by:    kp (mentor)
Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D42415/

(cherry picked from commit 98fd69f0090da73d9d0451bd769d7752468284c6)
2024-11-22 10:35:06 -05:00
Konstantin Belousov
dcdc0d7167 rtld.1: cross-reference rtld_get_var.3
(cherry picked from commit 16d000bdac54d7153f33a0d050bcc4307b702751)
2024-11-14 02:42:17 +02:00
Konstantin Belousov
86a2abab0a rtld: add rtld_{get,set}_var
(cherry picked from commit c56df6ce71ae96f00b088790d3ad2e0ebebdd59a)
2024-11-14 02:42:17 +02:00
Konstantin Belousov
db21e6980b rtld: make it easier to add sparce non-default members to ld_env_var_desc
(cherry picked from commit d7214577ff00221ac84a85e61794a5e126b952dd)
2024-11-14 02:42:17 +02:00
Konstantin Belousov
8ee3da8e73 rtld: 1-bit for bool is enough
(cherry picked from commit 450e684e0fea2785b5770241f51e897070c86726)
2024-11-14 02:42:17 +02:00
Graham Percival
cdffbea57c manuals: Fix dates
These were reported by `mandoc -T lint ...` as warnings.

Signed-off-by:	Graham Percival <gperciva@tarsnap.com>
Reviewed by:	mhorne
MFC after:	3 days
Sponsored by:	Tarsnap Backup Inc.
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1475

(cherry picked from commit b74aaa1a2199261f9078247d29481a994b6b5e42)
2024-11-04 12:16:22 -04:00
Ed Maste
f06d322e9d Remove "All Rights Reserved" from FreeBSD Foundation copyrights
These ones were unambiguous cases where the Foundation was the only
listed copyright holder.

Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 5c2bc3db201a4fe8d7911cf816bea104d5dc2138)
2024-08-06 15:39:53 -04:00
Konstantin Belousov
279e543dc7 rtld: add LD_NO_DL_ITERATE_PHDR_AFTER_FORK env var
PR:	280318

(cherry picked from commit 860c4d94ac46cee35a678cf3c9cdbd437dfed75e)
2024-08-05 03:32:11 +03:00
Konstantin Belousov
da32a0616b rtld: make ld_get_env_var() usable for all rtld source files
(cherry picked from commit 47315d6d3562111883142bb09320d35f05d34a58)
2024-08-05 03:32:11 +03:00
Konstantin Belousov
f0d4329dc2 rtld(1): Spell value as VALUE, not NAME
(cherry picked from commit bc25bc6850e512c4c45effdff6178d20b5eca558)
2024-08-05 03:32:11 +03:00
Andrew Turner
c2e0dea93f rtld: Add MD_OBJ_ENTRY to extend Struct_Obj_Entry
Add a macro the architectures can use to add per-arch fields to
Struct_Obj_Entry.

Reviewed by:	kib
Sponsored by:	Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D45116

(cherry picked from commit 06db20ffeca9898e5802d63f3b06caaa37c3a4ed)
2024-07-15 12:24:58 +00:00
Konstantin Belousov
0d47501246 rtld.1: clarify interaction between -u and -o
(cherry picked from commit 6a7819e43f938992304472054c83f4a6602a9e19)
2024-05-06 07:42:19 +03:00
Konstantin Belousov
6c85003139 rtld direct exec: make -u behavior match the description
(cherry picked from commit ef2694f368cee5957ee44b0298da88ff8360d561)
2024-05-06 07:42:19 +03:00
Konstantin Belousov
d63c680d39 rtld: add direct-exec option -o
(cherry picked from commit d1cd0cc32b53c09e72e33116b94a5b0b9781a183)
2024-05-06 07:42:19 +03:00
Konstantin Belousov
44debee9fc rtld snprintf: do not erronously skip a char at the buffer boundary
(cherry picked from commit 56ee5fc43c40479e2651b21862e299bcf73017ae)
2024-05-03 03:25:33 +03:00
Konstantin Belousov
2cd1939e2e rtld(1): minor clarification for LD_STATIC_TLS_EXTRA
(cherry picked from commit 8d7473703520f315c1bcd3b3d12498eee10d563e)
2024-04-23 03:32:21 +03:00
Stephen J. Kiernan
0b56acfc49 rtld: introduce STATIC_TLS_EXTRA
(cherry picked from commit 95335dd3c19e0ade161bb4dc8462fc3d045ce4f8)
2024-04-20 16:52:16 +03:00
Stefan Eßer
32364f99e1 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

(cherry picked from commit 7b77d37a561b47db093a2528b8032dbfe5791698)

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..

(cherry picked from commit 173953182af060dcab43990e179ee91e9f2d1e54)

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:		7b77d37a561b ("rtld-elf: support either byte-order of hints")
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D44472

(cherry picked from commit da2d6e2815d7694e3ccbd561508074c547b02dd6)

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

(cherry picked from commit c44bf7d2e9d2292867f2e23f291266af26762354)
2024-03-29 07:34:44 +01:00
Kyle Evans
a7b90458a2 rtld: add some dlopen tests
dlopen_basic just tests that libthr.so can be dlopen()ed, which will
just serve as a sanity check that "libthr.so" is a thing that can be
dlopened in case we get a weird failure in dlopen_recursing.

dlopen_recursing tests a regression reported after the libsys split,
where some dlopen() may cause infinite recursion and a resulting crash.
This case is inspired by bdrewery's description of what seemed to be
causing his issue.

The corresponding fix landed in commit
968a18975ad ("rtld: ignore load_filtees() calls if we already [...]")

Reviewed by:	kib

(cherry picked from commit c5796f1572c82b88e8b6a2810c92f30e5ac3e118)
2024-03-18 10:52:57 -05:00
Konstantin Belousov
3cce838c60 rtld: use generated map file to check for some leaks from libc into rtld
(cherry picked from commit 5db5c6c87a75f8b1871f021726fc4697253ae5cf)
2024-03-07 02:34:34 +02:00
Konstantin Belousov
5a98473132 rtld: unconditionally generate map file during build
(cherry picked from commit 799940154c70597a46627deb4d0b98e174b27b98)
2024-03-07 02:34:34 +02:00
Konstantin Belousov
11137153ab fdlopen(3): do not create a new object mapping if already loaded
PR:	277169

(cherry picked from commit 452c5e9995ab4cd6c7ea230cffe0c53bfa65c1ab)
2024-02-29 02:24:06 +02:00
Konstantin Belousov
0a246310f7 rtld: remove pointless "extern"
(cherry picked from commit 180df764c4ee570850deb4a910a11780ec3316df)
2024-02-29 02:24:06 +02:00
Andrew Turner
d416325740 rtld: Teach rtld about the BTI elf note
Add the Branch Target Identification (BTI) note to libc assembly
sources. As all obect files need the note for rtld to have it we need
to insert it in all asm files.

Reviewed by:	markj, emaste
Sponsored by:	Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D42228

(cherry picked from commit 5270cc9757fb4e0c1303ec44c2602b75acf3806d)
2024-02-19 13:12:18 +00:00
Konstantin Belousov
25bd875397 rtld: ignore load_filtees() calls if we already loading filtees for the obj
(cherry picked from commit 968a18975adc9c2a619bb52aa2f009de99fc9e24)
2024-02-18 12:01:47 +02:00
Konstantin Belousov
e43855c6f1 rtld load_filtees(): reindent and reduce block nesting
(cherry picked from commit 30b5f6b33b35623e6b6aa1d27a78311e199fa602)
2024-02-18 12:01:47 +02:00
Konstantin Belousov
c6d4339208 rtld symlook_obj: move common code to check filtees into helper
(cherry picked from commit 9ea864b54b57f2d0125860fb923f8db52b20eac2)
2024-02-18 12:01:46 +02:00
Mark Johnston
323226829a 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

(cherry picked from commit e7951d0b04e6464b37264b8166b1e9ce368a9f1d)
2024-02-09 09:55:56 -05:00
John Baldwin
a697425ccc Stop #defining FREEBSD_ELF
This was originally used (along with FREEBSD_AOUT) to prefer the use
of ELF in various tools instead of a.out as part of the a.out to ELF
transition in the 3.x days.  The last use of it was removed from
<link.h> in commit 66422f5b7a back in
2002, but various files still #define it.

Reviewed by:	kevans, imp, emaste
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42964

(cherry picked from commit 4a3cf5f329d69076aa9d093d596eb0ee82d917f5)
2024-01-04 16:28:48 -08:00
Kyle Evans
2ef9079ece rtld: add a test for RTLD_DEEPBIND
This tests that with RTLD_DEEPBIND, symbols are looked up in all of the
object's needed objects before the global object.

PR:		275393
Reviewed by:	kib
Sponsored by:	NetApp, Inc.
Sponsored by:	Klara, Inc.

(cherry picked from commit d9c543b6b0cabea6e6679d70b4e701018e7eab80)
2023-12-14 18:58:36 -06:00
Konstantin Belousov
8d0740e8b9 RTLD_DEEPBIND: make lookup not just symbolic, but walk all refobj' DAGs
PR:	275393

(cherry picked from commit 9daf6cd0f46416d9c6eb0411ea6042cd42b8a9bc)
2023-12-07 02:27:19 +02:00
John Baldwin
260bab9f1e Purge more stray embedded $FreeBSD$ strings
These do not use __FBSDID but instead use bare char arrays.

Reviewed by:	imp, emaste
Differential Revision:	https://reviews.freebsd.org/D41957

(cherry picked from commit eba230afba4932f02a1ca44efc797cf7499a5cb0)
2023-10-24 10:10:12 -07:00
Konstantin Belousov
3f3c631da0 rtld: output rtld errors into the dbg channel
(cherry picked from commit 72d97e1dd9cc72a2acb96e35e56db6f3f51bca7d)
2023-09-19 11:31:24 +03:00
Konstantin Belousov
cbf1bbe5c8 __crt_aligned_alloc_offset(): fix ov_index for backing allocation address
(cherry picked from commit 03a7c36ddbc0ddb1063d2c8a37c64d83e1519c55)
2023-09-09 11:26:33 +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
c29ee08204 rtld_malloc: add __crt_aligned_alloc_offset()
It is modelled after aligned_alloc(3).  Most importantly, to free the
allocation, __crt_free() can be used.  Additionally, caller may specify
offset into the aligned allocation, so that we return offset-ed from
alignment pointer.

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
d60130bf32 rtld_malloc: increase overhead index to uint16
Reorder it with magic, to keep alignment.

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
6bb7f05850 rtld_malloc.c: change return type of cp2op() to void
for it to be useful to return unaligned pointer.

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
fa9896e082 Remove $FreeBSD$: two-line nroff pattern
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:10 -06:00
Warner Losh
d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh
1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Warner Losh
42b388439b Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:23 -06:00
Warner Losh
b3e7694832 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:16 -06:00