1999-04-09 00:28:43 +00:00
|
|
|
/*-
|
Solve the dynamic linker's problems with multithreaded programs once
and for all (I hope). Packages such as wine, JDK, and linuxthreads
should no longer have any problems with re-entering the dynamic
linker.
This commit replaces the locking used in the dynamic linker with a
new spinlock-based reader/writer lock implementation. Brian
Fundakowski Feldman <green> argued for this from the very beginning,
but it took me a long time to come around to his point of view.
Spinlocks are the only kinds of locks that work with all thread
packages. But on uniprocessor systems they can be inefficient,
because while a contender for the lock is spinning the holder of the
lock cannot make any progress toward releasing it. To alleviate
this disadvantage I have borrowed a trick from Sleepycat's Berkeley
DB implementation. When spinning for a lock, the requester does a
nanosleep() call for 1 usec. each time around the loop. This will
generally yield the CPU to other threads, allowing the lock holder
to finish its business and release the lock. I chose 1 usec. as the
minimum sleep which would with reasonable certainty not be rounded
down to 0.
The formerly machine-independent file "lockdflt.c" has been moved
into the architecture-specific subdirectories by repository copy.
It now contains the machine-dependent spinlocking code. For the
spinlocks I used the very nifty "simple, non-scalable reader-preference
lock" which I found at
<http://www.cs.rochester.edu/u/scott/synchronization/pseudocode/rw.html>
on all CPUs except the 80386 (the specific CPU model, not the
architecture). The 80386 CPU doesn't support the necessary "cmpxchg"
instruction, so on that CPU a simple exclusive test-and-set lock
is used instead. 80386 CPUs are detected at initialization time by
trying to execute "cmpxchg" and catching the resulting SIGILL
signal.
To reduce contention for the locks, I have revamped a couple of
key data structures, permitting all common operations to be done
under non-exclusive (reader) locking. The only operations that
require exclusive locking now are the rare intrusive operations
such as dlopen() and dlclose().
The dllockinit() interface is now deprecated. It still exists,
but only as a do-nothing stub. I plan to remove it as soon as is
reasonably possible. (From the very beginning it was clearly
labeled as experimental and subject to change.) As far as I know,
only the linuxthreads port uses dllockinit(). This interface turned
out to have several problems. As one example, when the dynamic
linker called a client-supplied locking function, that function
sometimes needed lazy binding, causing re-entry into the dynamic
linker and a big looping mess. And in any case, it turned out to be
too burdensome to require threads packages to register themselves
with the dynamic linker.
2000-07-08 04:10:38 +00:00
|
|
|
* Copyright (c) 1999, 2000 John D. Polstra.
|
1999-04-09 00:28:43 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1999-04-09 00:28:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RTLD_MACHDEP_H
|
|
|
|
#define RTLD_MACHDEP_H 1
|
|
|
|
|
2003-05-29 22:58:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <machine/atomic.h>
|
|
|
|
|
2001-10-15 18:48:42 +00:00
|
|
|
struct Struct_Obj_Entry;
|
|
|
|
|
1999-04-09 00:28:43 +00:00
|
|
|
/* Return the address of the .dynamic section in the dynamic linker. */
|
|
|
|
#define rtld_dynamic(obj) \
|
|
|
|
((const Elf_Dyn *)((obj)->relocbase + (Elf_Addr)&_DYNAMIC))
|
|
|
|
|
1999-06-25 02:53:59 +00:00
|
|
|
/* Fixup the jump slot at "where" to transfer control to "target". */
|
2001-10-15 18:48:42 +00:00
|
|
|
static inline Elf_Addr
|
|
|
|
reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
|
2002-11-18 22:08:50 +00:00
|
|
|
const struct Struct_Obj_Entry *obj,
|
|
|
|
const struct Struct_Obj_Entry *refobj, const Elf_Rel *rel)
|
2001-10-15 18:48:42 +00:00
|
|
|
{
|
2006-03-28 18:28:07 +00:00
|
|
|
#ifdef dbg
|
2001-10-15 18:48:42 +00:00
|
|
|
dbg("reloc_jmpslot: *%p = %p", (void *)(where),
|
|
|
|
(void *)(target));
|
2006-03-28 18:28:07 +00:00
|
|
|
#endif
|
2001-10-15 18:48:42 +00:00
|
|
|
(*(Elf_Addr *)(where) = (Elf_Addr)(target));
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define make_function_pointer(def, defobj) \
|
|
|
|
((defobj)->relocbase + (def)->st_value)
|
1999-06-25 02:53:59 +00:00
|
|
|
|
2001-10-29 10:10:10 +00:00
|
|
|
#define call_initfini_pointer(obj, target) \
|
|
|
|
(((InitFunc)(target))())
|
|
|
|
|
Add support for preinit, init and fini arrays. Some ABIs, in
particular on ARM, do require working init arrays.
Traditional FreeBSD crt1 calls _init and _fini of the binary, instead
of allowing runtime linker to arrange the calls. This was probably
done to have the same crt code serve both statically and dynamically
linked binaries. Since ABI mandates that first is called preinit
array functions, then init, and then init array functions, the init
have to be called from rtld now.
To provide binary compatibility to old FreeBSD crt1, which calls _init
itself, rtld only calls intializers and finalizers for main binary if
binary has a note indicating that new crt was used for linking. Add
parsing of ELF notes to rtld, and cache p_osrel value since we parsed
it anyway.
The patch is inspired by init_array support for DragonflyBSD, written
by John Marino.
Reviewed by: kan
Tested by: andrew (arm, previous version), flo (sparc64, previous version)
MFC after: 3 weeks
2012-03-11 20:03:09 +00:00
|
|
|
#define call_init_pointer(obj, target) \
|
|
|
|
(((InitArrFunc)(target))(main_argc, main_argv, environ))
|
|
|
|
|
2004-08-03 08:51:00 +00:00
|
|
|
#define round(size, align) \
|
|
|
|
(((size) + (align) - 1) & ~((align) - 1))
|
|
|
|
#define calculate_first_tls_offset(size, align) \
|
|
|
|
round(size, align)
|
|
|
|
#define calculate_tls_offset(prev_offset, prev_size, size, align) \
|
|
|
|
round((prev_offset) + (size), align)
|
|
|
|
#define calculate_tls_end(off, size) (off)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned long ti_module;
|
|
|
|
unsigned long ti_offset;
|
|
|
|
} tls_index;
|
|
|
|
|
|
|
|
extern void *___tls_get_addr(tls_index *ti) __attribute__((__regparm__(1)));
|
|
|
|
extern void *__tls_get_addr(tls_index *ti);
|
|
|
|
|
2011-01-25 21:12:31 +00:00
|
|
|
#define RTLD_DEFAULT_STACK_PF_EXEC PF_X
|
|
|
|
#define RTLD_DEFAULT_STACK_EXEC PROT_EXEC
|
|
|
|
|
1999-04-09 00:28:43 +00:00
|
|
|
#endif
|