aboutsummaryrefslogtreecommitdiff
path: root/libexec/rtld-elf/rtld_lock.c
Commit message (Collapse)AuthorAgeFilesLines
* rtld: add lockstate_wlocked()Konstantin Belousov2025-05-021-0/+6
| | | | | | | querying the lockstate for write-locked state. Sponsored by: The FreeBSD Foundation MFC after: 1 week
* rtld: some style in rtld_lock.cKonstantin Belousov2025-05-021-2/+2
|
* rtld: add LD_NO_DL_ITERATE_PHDR_AFTER_FORK env varKonstantin Belousov2024-07-291-2/+5
| | | | | | | | | | which makes threaded fork ignore the phdr rtld lock, in particular allowing the dl_iterate_phdr() to block in callback. The cost is that the image started in this mode cannot use dl_iterate_phdr() after fork. PR: 280318 Sponsored by: The FreeBSD Foundation MFC after: 1 week
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-121-1/+1
| | | | | | | | | 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
* rtld: style the rest of rtld_lock.cKonstantin Belousov2022-09-041-30/+31
| | | | | | | Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36396
* rtld: mask signals for default read locksKonstantin Belousov2022-09-041-24/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* rtld: initialize default dlerror_seen_val as trueKonstantin Belousov2021-05-121-1/+1
| | | | | | | | | There should be no error after startup. PR: 255698 Reported by: Eugene M. Kim <astralblue@gmail.com> Sponsored by: The FreeBSD Foundation MFC after: 1 week
* rtld: make dlerror() thread-localKonstantin Belousov2021-04-101-0/+33
| | | | | | | | PR: 95339 Discussed with: arichardson Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29633
* rtld: workaround for broken ABIKonstantin Belousov2021-04-101-1/+16
| | | | | | | | | | | | | | | | Right now, libthr does not initialize RtldLockInfo.rtli_version when calling _rtld_thread_init(), which makes versioning the interface troublesome. Add a workaround: if the calling object of _rtld_thread_init() exports the "_pli_rtli_version" symbol, then consider rtli_version initialized. Otherwise, forcibly set it to RTLI_VERSION_ONE, currently defined as RTLI_VERSION. Export "_pli_rtli_version" from libthr and properly initialize rtli_version. Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29633
* rtld: unstaticise lockinfo and obj_from_addr()Konstantin Belousov2021-04-101-1/+1
| | | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29633
* Use sigfastblock(2) in rtld.Konstantin Belousov2020-02-091-41/+65
| | | | | | | | | | | | | | | | | | | | | | | | | | This allows for rtld to not issue two sigprocmask(2) syscalls for each symbol binding operation in single-threaded processes. Rtld needs to block signals as part of locking to ensure signal safety of the bind process, because signal handlers might need to lazily resolve symbol references. As result, number of syscalls issued on startup by simple programs not using libthr, is typically reduced 2x. For instance, for hello world, I see: non-sigfastblock # (truss ./hello > /dev/null) |& wc -l 63 sigfastblock # (truss ./hello > /dev/null) |& wc -l 37 Tested by: pho Disscussed with: cem, emaste, jilles Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D12773 Notes: svn path=/head/; revision=357697
* Reduce size of rtld by 22% by pulling in less code from libcAlex Richardson2019-06-301-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently RTLD is linked against libc_nossp_pic which means that any libc symbol used in rtld can pull in a lot of depedencies. This was causing symbol such as __libc_interposing and all the pthread stubs to be included in RTLD even though they are not required. It turns out most of these dependencies can easily be avoided by providing overrides inside of rtld. This change is motivated by CHERI, where we have an experimental ABI that requires additional relocation processing to allow the use of function pointers inside of rtld. Instead of adding this self-relocation code to RTLD I attempted to remove most function pointers from RTLD and discovered that most of them came from the libc dependencies instead of being actually used inside rtld. A nice side-effect of this change is that rtld is now 22% smaller on amd64. text data bss dec hex filename 0x21eb6 0xce0 0xe60 145910 239f6 /home/alr48/ld-elf-x86.before.so.1 0x1a6ed 0x728 0xdd8 113645 1bbed /home/alr48/ld-elf-x86.after.so.1 The number of R_X86_64_RELATIVE relocations that need to be processed on startup has also gone down from 368 to 187 (almost 50% less). Reviewed By: kib Differential Revision: https://reviews.freebsd.org/D20663 Notes: svn path=/head/; revision=349554
* rtld-elf: fix more warnings to allow compiling with WARNS=6Alex Richardson2018-10-291-1/+1
| | | | | | | | | Reviewed By: kib Approved By: brooks (mentor) Differential Revision: https://reviews.freebsd.org/D17154 Notes: svn path=/head/; revision=339879
* libexec: adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-271-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 2-Clause license, however the tool I was using misidentified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. No functional change intended. Notes: svn path=/head/; revision=326274
* Fix acquisition of nested write compat rtld locks.Konstantin Belousov2017-01-101-24/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Obtaining compat rtld lock in write mode sets process signal mask to block all signals. Previous mask is stored in the global variable oldsigmask. If a lock is write-locked while another lock is already write-locked, oldsigmask is overwritten by the total mask and on the last unlock, all signals except traps appear to be blocked. Fix this by counting the write-lock nested level, and only storing to oldsigmask/restoring from it at the outermost level. Masking signals disables involuntary preemption for libc_r, and there could be no voluntary context switches in the locked code (dl_iterate_phdr(3) keeps a lock around user callback, but it was added long after libc_r was renounced). Due to this, remembering the level in the global variable after the lock is obtained should be safe, because no two libc_r threads can acquire different write locks in parallel. PR: 215826 Reported by: kami Tested by: yamagi@yamagi.org (previous version) To be reviewed by: kan Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=311886
* Use ANSI C definitions, update comment.Konstantin Belousov2017-01-101-4/+4
| | | | | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=311879
* Change compiler setting to make default visibility of the symbols forKonstantin Belousov2015-03-291-0/+4
| | | | | | | | | | | | | | rtld on x86 to be hidden. This is a micro-optimization, which allows intrinsic references inside rtld to be handled without indirection through PLT. The visibility of rtld symbols for other objects in the symbol namespace is controlled by a version script. Reviewed by: kan, jilles Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=280816
* Right now, the rtld prefork hook locks the rtld bind lock in the readKonstantin Belousov2014-05-241-1/+15
| | | | | | | | | | | | | | | | | | | | | mode. This allows the binder to be functional in the child after the fork (assuming no lazy loading of a filter is needed), but other rtld services which require write lock on rtld_bind_lock cause deadlock, if called by child. Change the _rtld_atfork() to lock the bind lock in write mode, making the rtld fully functional after the fork. Pre-resolve the symbols which are called by the libthr' fork() interposer, since dynamic resolution causes deadlock due to the rtld_bind_lock already owned in the write mode. Reported and tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=266609
* Rtld links with the specially built pic static libc library to get someKonstantin Belousov2011-08-241-2/+0
| | | | | | | | | | | | | | | | | | | | C runtime services, like printf(). Unfortunately, the multithread-safeness measures in the libc do not work in rtld environment. Rip the kernel printf() implementation and use it in the rtld instead of libc version. This printf does not require any shared global data and thus is mt-safe. Systematically use rtld_printf() and related functions, remove the calls to err(3). Note that stdio is still pulled from libc due to libmap implementaion using fopen(). This is safe but unoptimal, and can be changed later. Reported and tested by: pgj Diagnosed and reviewed by: kan (previous version) Approved by: re (bz) Notes: svn path=/head/; revision=225152
* Use sigsetjmp/siglongjmp with disabled signal mask access forKonstantin Belousov2011-02-091-1/+1
| | | | | | | | | lock upgrade in rtld. There is no need to care about the mask, which causes a lot of unneeded sigprocmask(2) calls during each symbol lookup. Notes: svn path=/head/; revision=218476
* Implement support for ELF filters in rtld. Both normal and auxillaryKonstantin Belousov2010-12-251-25/+77
| | | | | | | | | | | | | | | | | | | | filters are implemented. Filtees are loaded on demand, unless LD_LOADFLTR environment variable is set or -z loadfltr was specified during the linking. This forces rtld to upgrade read-locked rtld_bind_lock to write lock when it encounters an object with filter during symbol lookup. Consolidate common arguments of the symbol lookup functions in the SymLook structure. Track the state of the rtld locks in the RtldLockState structure. Pass local RtldLockState through the rtld symbol lookup calls to allow lock upgrades. Reviewed by: kan Tested by: Mykola Dzham <i levsha me>, nwhitehorn (powerpc) Notes: svn path=/head/; revision=216695
* Prefer <sys/param.h> to <machine/param.h> for the definition ofRobert Watson2009-04-201-1/+1
| | | | | | | | | | CACHE_LINE_SIZE. Submitted by: bde MFC after: 2 weeks Notes: svn path=/head/; revision=191303
* Explicitly include machine/param.h for CACHE_LINE_SIZE.Robert Watson2009-04-191-0/+2
| | | | | | | MFC after: 2 weeks Notes: svn path=/head/; revision=191292
* Add two rtld exported symbols, _rtld_atfork_pre and _rtld_atfork_post.Konstantin Belousov2008-11-271-0/+16
| | | | | | | | | | | | | | | | | Threading library calls _pre before the fork, allowing the rtld to lock itself to ensure that other threads of the process are out of dynamic linker. _post releases the locks. This allows the rtld to have consistent state in the child. Although child may legitimately call only async-safe functions, the call may need plt relocation resolution, and this requires working rtld. Reported and debugging help by: rink Reviewed by: kan, davidxu MFC after: 1 month (anyway, not before 7.1 is out) Notes: svn path=/head/; revision=185369
* Allow multiple locks to be acquired by detecting correspondingDavid Xu2008-09-161-2/+2
| | | | | | | | | | | bit flag, otherwise if a thread acquired a lock, another thread or the current thread itself can no longer acquire another lock because thread_mask_set() return whole flag word, this results bit leaking in the word and misbehavior in later locking and unlocking. Notes: svn path=/head/; revision=183061
* Fix the problem with the C++ exception handling for the multithreadedKonstantin Belousov2008-05-061-1/+2
| | | | | | | | | | | | | | | | | | | | | | | programs. From the PR description: The gcc runtime's _Unwind_Find_FDE function, invoked during exception handling's stack unwinding, is not safe to execute from within multiple threads. FreeBSD' s dl_iterate_phdr() however permits multiple threads to pass through it though. The result is surprisingly reliable infinite looping of one or more threads if they just happen to be unwinding at the same time. Introduce the new lock that is write locked around the dl_iterate_pdr, thus providing required exclusion for the stack unwinders. PR: threads/123062 Submitted by: Andy Newman <an at atrn org> Reviewed by: kan MFC after: 2 weeks Notes: svn path=/head/; revision=178807
* Use u_int for variable manipulated by atomic ops to match atomicAlexander Kabaev2007-04-031-1/+1
| | | | | | | ops function prototypes. Notes: svn path=/head/; revision=168311
* Remove 80386 support from the ELF run time linker.John Baldwin2004-11-161-19/+0
| | | | Notes: svn path=/head/; revision=137785
* Consistently use __inline instead of __inline__ as the former is an empty macroStefan Farfeleder2004-07-041-2/+2
| | | | | | | in <sys/cdefs.h> for compilers without support for inline. Notes: svn path=/head/; revision=131575
* Allow threading libraries to register their own lockingAlexander Kabaev2003-05-291-0/+336
implementation in case default one provided by rtld is not suitable. Consolidate various identical MD lock implementation into a single file using appropriate machine/atomic.h. Approved by: re (scottl) Notes: svn path=/head/; revision=115396