1998-03-07 19:24:35 +00:00
|
|
|
/*-
|
2000-01-09 21:13:48 +00:00
|
|
|
* Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
|
1998-03-07 19:24:35 +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 ``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 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$
|
1998-03-07 19:24:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RTLD_H /* { */
|
|
|
|
#define RTLD_H 1
|
|
|
|
|
2001-05-02 23:56:21 +00:00
|
|
|
#include <machine/elf.h>
|
1998-03-07 19:24:35 +00:00
|
|
|
#include <sys/types.h>
|
1999-08-30 01:48:19 +00:00
|
|
|
#include <sys/queue.h>
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2001-05-02 23:56:21 +00:00
|
|
|
#include <elf-hints.h>
|
1998-04-30 07:48:02 +00:00
|
|
|
#include <link.h>
|
1998-03-07 19:24:35 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2003-05-29 22:58:26 +00:00
|
|
|
#include "rtld_lock.h"
|
1999-04-09 00:28:43 +00:00
|
|
|
#include "rtld_machdep.h"
|
|
|
|
|
2004-03-21 01:21:26 +00:00
|
|
|
#ifdef COMPAT_32BIT
|
|
|
|
#undef STANDARD_LIBRARY_PATH
|
|
|
|
#undef _PATH_ELF_HINTS
|
|
|
|
#define _PATH_ELF_HINTS "/var/run/ld-elf32.so.hints"
|
|
|
|
/* For running 32 bit binaries */
|
|
|
|
#define STANDARD_LIBRARY_PATH "/lib32:/usr/lib32"
|
|
|
|
#define LD_ "LD_32_"
|
|
|
|
#endif
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
#ifndef STANDARD_LIBRARY_PATH
|
2003-08-17 07:55:17 +00:00
|
|
|
#define STANDARD_LIBRARY_PATH "/lib:/usr/lib"
|
1998-03-07 19:24:35 +00:00
|
|
|
#endif
|
2004-03-21 01:21:26 +00:00
|
|
|
#ifndef LD_
|
|
|
|
#define LD_ "LD_"
|
|
|
|
#endif
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
#define NEW(type) ((type *) xmalloc(sizeof(type)))
|
|
|
|
#define CNEW(type) ((type *) xcalloc(sizeof(type)))
|
|
|
|
|
|
|
|
/* We might as well do booleans like C++. */
|
|
|
|
typedef unsigned char bool;
|
|
|
|
#define false 0
|
|
|
|
#define true 1
|
|
|
|
|
2004-08-03 08:51:00 +00:00
|
|
|
extern size_t tls_last_offset;
|
|
|
|
extern size_t tls_last_size;
|
|
|
|
extern size_t tls_static_space;
|
|
|
|
extern int tls_dtv_generation;
|
|
|
|
extern int tls_max_index;
|
|
|
|
|
1999-08-30 01:50:41 +00:00
|
|
|
struct stat;
|
1998-03-07 19:24:35 +00:00
|
|
|
struct Struct_Obj_Entry;
|
|
|
|
|
2000-01-09 21:13:48 +00:00
|
|
|
/* Lists of shared objects */
|
1999-08-30 01:48:19 +00:00
|
|
|
typedef struct Struct_Objlist_Entry {
|
2000-05-26 02:09:24 +00:00
|
|
|
STAILQ_ENTRY(Struct_Objlist_Entry) link;
|
1999-08-30 01:48:19 +00:00
|
|
|
struct Struct_Obj_Entry *obj;
|
|
|
|
} Objlist_Entry;
|
|
|
|
|
2000-05-26 02:09:24 +00:00
|
|
|
typedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
|
1999-08-30 01:48:19 +00:00
|
|
|
|
2000-07-26 04:24:40 +00:00
|
|
|
/* Types of init and fini functions */
|
2000-01-09 21:13:48 +00:00
|
|
|
typedef void (*InitFunc)(void);
|
|
|
|
|
|
|
|
/* Lists of shared object dependencies */
|
1998-03-07 19:24:35 +00:00
|
|
|
typedef struct Struct_Needed_Entry {
|
|
|
|
struct Struct_Needed_Entry *next;
|
|
|
|
struct Struct_Obj_Entry *obj;
|
|
|
|
unsigned long name; /* Offset of name in string table */
|
|
|
|
} Needed_Entry;
|
|
|
|
|
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
|
|
|
/* Lock object */
|
|
|
|
typedef struct Struct_LockInfo {
|
|
|
|
void *context; /* Client context for creating locks */
|
|
|
|
void *thelock; /* The one big lock */
|
|
|
|
/* Debugging aids. */
|
|
|
|
volatile int rcount; /* Number of readers holding lock */
|
|
|
|
volatile int wcount; /* Number of writers holding lock */
|
|
|
|
/* Methods */
|
|
|
|
void *(*lock_create)(void *context);
|
|
|
|
void (*rlock_acquire)(void *lock);
|
|
|
|
void (*wlock_acquire)(void *lock);
|
|
|
|
void (*rlock_release)(void *lock);
|
|
|
|
void (*wlock_release)(void *lock);
|
|
|
|
void (*lock_destroy)(void *lock);
|
|
|
|
void (*context_destroy)(void *context);
|
|
|
|
} LockInfo;
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
/*
|
|
|
|
* Shared object descriptor.
|
|
|
|
*
|
|
|
|
* Items marked with "(%)" are dynamically allocated, and must be freed
|
|
|
|
* when the structure is destroyed.
|
1999-09-05 21:12:53 +00:00
|
|
|
*
|
|
|
|
* CAUTION: It appears that the JDK port peeks into these structures.
|
|
|
|
* It looks at "next" and "mapbase" at least. Don't add new members
|
|
|
|
* near the front, until this can be straightened out.
|
1998-03-07 19:24:35 +00:00
|
|
|
*/
|
|
|
|
typedef struct Struct_Obj_Entry {
|
|
|
|
/*
|
|
|
|
* These two items have to be set right for compatibility with the
|
|
|
|
* original ElfKit crt1.o.
|
|
|
|
*/
|
1998-08-21 03:29:40 +00:00
|
|
|
Elf_Word magic; /* Magic number (sanity check) */
|
|
|
|
Elf_Word version; /* Version number of struct format */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
struct Struct_Obj_Entry *next;
|
|
|
|
char *path; /* Pathname of underlying file (%) */
|
2003-06-18 03:34:29 +00:00
|
|
|
char *origin_path; /* Directory path of origin file */
|
1998-03-07 19:24:35 +00:00
|
|
|
int refcount;
|
|
|
|
int dl_refcount; /* Number of times loaded by dlopen */
|
|
|
|
|
|
|
|
/* These items are computed by map_object() or by digest_phdr(). */
|
|
|
|
caddr_t mapbase; /* Base address of mapped region */
|
|
|
|
size_t mapsize; /* Size of mapped region in bytes */
|
|
|
|
size_t textsize; /* Size of text segment in bytes */
|
1998-08-21 03:29:40 +00:00
|
|
|
Elf_Addr vaddrbase; /* Base address in shared object file */
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t relocbase; /* Relocation constant = mapbase - vaddrbase */
|
1998-08-21 03:29:40 +00:00
|
|
|
const Elf_Dyn *dynamic; /* Dynamic section */
|
1998-03-07 19:24:35 +00:00
|
|
|
caddr_t entry; /* Entry point */
|
1998-08-21 03:29:40 +00:00
|
|
|
const Elf_Phdr *phdr; /* Program header if it is mapped, else NULL */
|
1998-03-07 19:24:35 +00:00
|
|
|
size_t phsize; /* Size of program header in bytes */
|
1999-08-30 01:54:13 +00:00
|
|
|
const char *interp; /* Pathname of the interpreter, if any */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2004-08-03 08:51:00 +00:00
|
|
|
/* TLS information */
|
|
|
|
int tlsindex; /* Index in DTV for this module */
|
|
|
|
void *tlsinit; /* Base address of TLS init block */
|
|
|
|
size_t tlsinitsize; /* Size of TLS init block for this module */
|
|
|
|
size_t tlssize; /* Size of TLS block for this module */
|
|
|
|
size_t tlsoffset; /* Offset of static TLS block for this module */
|
|
|
|
size_t tlsalign; /* Alignment of static TLS block */
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
/* Items from the dynamic section. */
|
1999-04-09 00:28:43 +00:00
|
|
|
Elf_Addr *pltgot; /* PLT or GOT, depending on architecture */
|
1998-08-21 03:29:40 +00:00
|
|
|
const Elf_Rel *rel; /* Relocation entries */
|
1998-03-07 19:24:35 +00:00
|
|
|
unsigned long relsize; /* Size in bytes of relocation info */
|
1998-09-04 19:03:57 +00:00
|
|
|
const Elf_Rela *rela; /* Relocation entries with addend */
|
|
|
|
unsigned long relasize; /* Size in bytes of addend relocation info */
|
1998-08-21 03:29:40 +00:00
|
|
|
const Elf_Rel *pltrel; /* PLT relocation entries */
|
1998-03-07 19:24:35 +00:00
|
|
|
unsigned long pltrelsize; /* Size in bytes of PLT relocation info */
|
1998-09-04 19:03:57 +00:00
|
|
|
const Elf_Rela *pltrela; /* PLT relocation entries with addend */
|
|
|
|
unsigned long pltrelasize; /* Size in bytes of PLT addend reloc info */
|
1998-08-21 03:29:40 +00:00
|
|
|
const Elf_Sym *symtab; /* Symbol table */
|
1998-03-07 19:24:35 +00:00
|
|
|
const char *strtab; /* String table */
|
|
|
|
unsigned long strsize; /* Size in bytes of string table */
|
|
|
|
|
2001-10-15 18:48:42 +00:00
|
|
|
const Elf_Hashelt *buckets; /* Hash table buckets array */
|
1998-03-07 19:24:35 +00:00
|
|
|
unsigned long nbuckets; /* Number of buckets */
|
2001-10-15 18:48:42 +00:00
|
|
|
const Elf_Hashelt *chains; /* Hash table chain array */
|
1998-03-07 19:24:35 +00:00
|
|
|
unsigned long nchains; /* Number of chains */
|
|
|
|
|
|
|
|
const char *rpath; /* Search path specified in object */
|
|
|
|
Needed_Entry *needed; /* Shared objects needed by this one (%) */
|
|
|
|
|
2001-10-29 10:10:10 +00:00
|
|
|
Elf_Addr init; /* Initialization function to call */
|
|
|
|
Elf_Addr fini; /* Termination function to call */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
|
|
|
bool mainprog; /* True if this is the main program */
|
|
|
|
bool rtld; /* True if this is the dynamic linker */
|
|
|
|
bool textrel; /* True if there are relocations to text seg */
|
|
|
|
bool symbolic; /* True if generated with "-Bsymbolic" */
|
2003-06-18 03:34:29 +00:00
|
|
|
bool bind_now; /* True if all relocations should be made first */
|
1998-09-02 02:51:12 +00:00
|
|
|
bool traced; /* Already printed in ldd trace output */
|
2000-01-29 01:27:04 +00:00
|
|
|
bool jmpslots_done; /* Already have relocated the jump slots */
|
2000-07-26 04:24:40 +00:00
|
|
|
bool init_done; /* Already have added object to init list */
|
2004-08-03 08:51:00 +00:00
|
|
|
bool tls_done; /* Already allocated offset for static TLS */
|
1998-04-30 07:48:02 +00:00
|
|
|
|
2003-02-13 17:47:44 +00:00
|
|
|
struct link_map linkmap; /* for GDB and dlinfo() */
|
1999-09-05 21:12:53 +00:00
|
|
|
Objlist dldags; /* Object belongs to these dlopened DAGs (%) */
|
|
|
|
Objlist dagmembers; /* DAG has these members (%) */
|
|
|
|
dev_t dev; /* Object's filesystem's device */
|
|
|
|
ino_t ino; /* Object's inode number */
|
2001-10-15 18:48:42 +00:00
|
|
|
void *priv; /* Platform-dependant */
|
1998-03-07 19:24:35 +00:00
|
|
|
} Obj_Entry;
|
|
|
|
|
|
|
|
#define RTLD_MAGIC 0xd550b87a
|
|
|
|
#define RTLD_VERSION 1
|
|
|
|
|
2004-08-03 08:51:00 +00:00
|
|
|
#define RTLD_STATIC_TLS_EXTRA 64
|
|
|
|
|
2001-05-05 23:21:05 +00:00
|
|
|
/*
|
|
|
|
* Symbol cache entry used during relocation to avoid multiple lookups
|
|
|
|
* of the same symbol.
|
|
|
|
*/
|
|
|
|
typedef struct Struct_SymCache {
|
|
|
|
const Elf_Sym *sym; /* Symbol table entry */
|
|
|
|
const Obj_Entry *obj; /* Shared object which defines it */
|
|
|
|
} SymCache;
|
|
|
|
|
1999-07-18 00:02:19 +00:00
|
|
|
extern void _rtld_error(const char *, ...) __printflike(1, 2);
|
1999-08-30 01:50:41 +00:00
|
|
|
extern Obj_Entry *map_object(int, const char *, const struct stat *);
|
1998-03-07 19:24:35 +00:00
|
|
|
extern void *xcalloc(size_t);
|
|
|
|
extern void *xmalloc(size_t);
|
|
|
|
extern char *xstrdup(const char *);
|
1998-09-04 19:03:57 +00:00
|
|
|
extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
|
|
|
|
|
2003-06-19 03:55:38 +00:00
|
|
|
extern void dump_relocations (Obj_Entry *);
|
|
|
|
extern void dump_obj_relocations (Obj_Entry *);
|
|
|
|
extern void dump_Elf_Rel (Obj_Entry *, const Elf_Rel *, u_long);
|
|
|
|
extern void dump_Elf_Rela (Obj_Entry *, const Elf_Rela *, u_long);
|
|
|
|
|
1998-09-04 19:03:57 +00:00
|
|
|
/*
|
|
|
|
* Function declarations.
|
|
|
|
*/
|
|
|
|
unsigned long elf_hash(const char *);
|
2000-09-19 04:27:16 +00:00
|
|
|
const Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
|
2001-05-05 23:21:05 +00:00
|
|
|
const Obj_Entry **, bool, SymCache *);
|
1999-04-09 00:28:43 +00:00
|
|
|
void init_pltgot(Obj_Entry *);
|
2003-06-19 02:39:37 +00:00
|
|
|
void lockdflt_init(void);
|
1999-08-30 01:48:19 +00:00
|
|
|
void obj_free(Obj_Entry *);
|
|
|
|
Obj_Entry *obj_new(void);
|
1999-04-09 00:28:43 +00:00
|
|
|
void _rtld_bind_start(void);
|
1998-09-04 19:03:57 +00:00
|
|
|
const Elf_Sym *symlook_obj(const char *, unsigned long,
|
|
|
|
const Obj_Entry *, bool);
|
2004-08-03 08:51:00 +00:00
|
|
|
void *tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset);
|
|
|
|
void *allocate_tls(Obj_Entry *, void *, size_t, size_t);
|
|
|
|
void free_tls(void *, size_t, size_t);
|
|
|
|
void *allocate_module_tls(int index);
|
|
|
|
bool allocate_tls_offset(Obj_Entry *obj);
|
2005-02-27 12:55:40 +00:00
|
|
|
void free_tls_offset(Obj_Entry *obj);
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2003-06-19 02:42:04 +00:00
|
|
|
/*
|
|
|
|
* MD function declarations.
|
|
|
|
*/
|
|
|
|
int do_copy_relocations(Obj_Entry *);
|
|
|
|
int reloc_non_plt(Obj_Entry *, Obj_Entry *);
|
|
|
|
int reloc_plt(Obj_Entry *);
|
|
|
|
int reloc_jmpslots(Obj_Entry *);
|
2004-08-03 08:51:00 +00:00
|
|
|
void allocate_initial_tls(Obj_Entry *);
|
2003-06-19 02:42:04 +00:00
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
#endif /* } */
|