1998-03-07 19:24:35 +00:00
|
|
|
/*-
|
2023-05-10 15:40:58 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2017-11-27 15:25:02 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2011-08-24 20:05:13 +00:00
|
|
|
#include <stdarg.h>
|
2019-12-31 05:41:47 +00:00
|
|
|
#include <stdbool.h>
|
2010-12-25 08:51:20 +00:00
|
|
|
#include <setjmp.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"
|
|
|
|
|
1998-03-07 19:24:35 +00:00
|
|
|
#define NEW(type) ((type *) xmalloc(sizeof(type)))
|
2012-03-22 14:20:51 +00:00
|
|
|
#define CNEW(type) ((type *) xcalloc(1, sizeof(type)))
|
1998-03-07 19:24:35 +00:00
|
|
|
|
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;
|
2018-10-29 21:08:11 +00:00
|
|
|
extern Elf_Addr tls_dtv_generation;
|
2004-08-03 08:51:00 +00:00
|
|
|
extern int tls_max_index;
|
|
|
|
|
Before calling mmap() on a shared library's text and data sections, rtld
first calls mmap() with the arguments PROT_NONE and MAP_ANON to reserve a
single, contiguous range of virtual addresses for the entire shared library.
Later, rtld calls mmap() with the the shared library's file descriptor
and the argument MAP_FIXED to place the text and data sections within the
reserved range. The rationale for mapping shared libraries in this way is
explained in the commit message for Revision 190885. However, this approach
does have an unintended, negative consequence. Since the first call to
mmap() specifies MAP_ANON and not the shared library's file descriptor, the
kernel has no idea what alignment the vm object backing the file prefers.
As a result, the reserved range's alignment is unlikely to be the same as
the vm object's, and so mapping with superpages becomes impossible. To
address this problem, this revision adds the argument MAP_ALIGNED_SUPER to
the first call to mmap() if the text section is larger than the smallest
superpage size.
To determine if the text section is larger than the smallest superpage
size, rtld must always fetch the page size information. As a result, the
private code for fetching the base page size in rtld's builtin malloc is
redundant. Eliminate it. Requested by: kib
Tested by: zbb (on arm)
Reviewed by: kib (an earlier version)
Discussed with: jhb
2014-04-11 16:55:25 +00:00
|
|
|
extern int npagesizes;
|
|
|
|
extern size_t *pagesizes;
|
2022-04-04 15:05:40 +00:00
|
|
|
extern size_t page_size;
|
Before calling mmap() on a shared library's text and data sections, rtld
first calls mmap() with the arguments PROT_NONE and MAP_ANON to reserve a
single, contiguous range of virtual addresses for the entire shared library.
Later, rtld calls mmap() with the the shared library's file descriptor
and the argument MAP_FIXED to place the text and data sections within the
reserved range. The rationale for mapping shared libraries in this way is
explained in the commit message for Revision 190885. However, this approach
does have an unintended, negative consequence. Since the first call to
mmap() specifies MAP_ANON and not the shared library's file descriptor, the
kernel has no idea what alignment the vm object backing the file prefers.
As a result, the reserved range's alignment is unlikely to be the same as
the vm object's, and so mapping with superpages becomes impossible. To
address this problem, this revision adds the argument MAP_ALIGNED_SUPER to
the first call to mmap() if the text section is larger than the smallest
superpage size.
To determine if the text section is larger than the smallest superpage
size, rtld must always fetch the page size information. As a result, the
private code for fetching the base page size in rtld's builtin malloc is
redundant. Eliminate it. Requested by: kib
Tested by: zbb (on arm)
Reviewed by: kib (an earlier version)
Discussed with: jhb
2014-04-11 16:55:25 +00:00
|
|
|
|
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
|
|
|
extern int main_argc;
|
|
|
|
extern char **main_argv;
|
|
|
|
extern char **environ;
|
|
|
|
|
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);
|
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
|
|
|
typedef void (*InitArrFunc)(int, char **, char **);
|
2000-01-09 21:13:48 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
typedef struct Struct_Name_Entry {
|
|
|
|
STAILQ_ENTRY(Struct_Name_Entry) link;
|
|
|
|
char name[1];
|
|
|
|
} Name_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;
|
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
typedef struct Struct_Ver_Entry {
|
|
|
|
Elf_Word hash;
|
|
|
|
unsigned int flags;
|
|
|
|
const char *name;
|
|
|
|
const char *file;
|
|
|
|
} Ver_Entry;
|
|
|
|
|
2012-04-30 13:29:21 +00:00
|
|
|
typedef struct Struct_Sym_Match_Result {
|
|
|
|
const Elf_Sym *sym_out;
|
|
|
|
const Elf_Sym *vsymp;
|
|
|
|
int vcount;
|
|
|
|
} Sym_Match_Result;
|
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
#define VER_INFO_HIDDEN 0x01
|
|
|
|
|
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.
|
|
|
|
*/
|
2005-12-18 04:52:37 +00:00
|
|
|
Elf_Size magic; /* Magic number (sanity check) */
|
|
|
|
Elf_Size version; /* Version number of struct format */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2016-01-20 07:21:33 +00:00
|
|
|
TAILQ_ENTRY(Struct_Obj_Entry) next;
|
1998-03-07 19:24:35 +00:00
|
|
|
char *path; /* Pathname of underlying file (%) */
|
2003-06-18 03:34:29 +00:00
|
|
|
char *origin_path; /* Directory path of origin file */
|
2016-12-22 17:37:39 +00:00
|
|
|
int refcount; /* DAG references */
|
|
|
|
int holdcount; /* Count of transient references */
|
1998-03-07 19:24:35 +00:00
|
|
|
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 */
|
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 */
|
2011-01-08 17:11:49 +00:00
|
|
|
Elf_Word stack_flags;
|
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 */
|
2020-04-04 22:37:50 +00:00
|
|
|
size_t tlspoffset; /* p_offset of the static TLS block */
|
2004-08-03 08:51:00 +00:00
|
|
|
|
2012-01-30 19:52:17 +00:00
|
|
|
caddr_t relro_page;
|
|
|
|
size_t relro_size;
|
|
|
|
|
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 */
|
2021-10-16 21:34:37 +00:00
|
|
|
const Elf_Relr *relr; /* RELR relocation entries */
|
|
|
|
unsigned long relrsize; /* Size in bytes of RELR relocations */
|
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 */
|
2019-06-25 00:40:44 +00:00
|
|
|
#ifdef __powerpc__
|
2015-12-03 00:10:57 +00:00
|
|
|
#ifdef __powerpc64__
|
|
|
|
Elf_Addr glink; /* GLINK PLT call stub section */
|
2019-06-25 00:40:44 +00:00
|
|
|
#else
|
|
|
|
Elf_Addr *gotptr; /* GOT pointer (secure-plt only) */
|
|
|
|
#endif
|
2015-12-03 00:10:57 +00:00
|
|
|
#endif
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
const Elf_Verneed *verneed; /* Required versions. */
|
|
|
|
Elf_Word verneednum; /* Number of entries in verneed table */
|
|
|
|
const Elf_Verdef *verdef; /* Provided versions. */
|
|
|
|
Elf_Word verdefnum; /* Number of entries in verdef table */
|
|
|
|
const Elf_Versym *versyms; /* Symbol versions 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 */
|
2012-04-30 13:31:10 +00:00
|
|
|
unsigned long nchains; /* Number of entries in chain array */
|
|
|
|
|
|
|
|
Elf32_Word nbuckets_gnu; /* Number of GNU hash buckets*/
|
|
|
|
Elf32_Word symndx_gnu; /* 1st accessible symbol on dynsym table */
|
|
|
|
Elf32_Word maskwords_bm_gnu; /* Bloom filter words - 1 (bitmask) */
|
|
|
|
Elf32_Word shift2_gnu; /* Bloom filter shift count */
|
|
|
|
Elf32_Word dynsymcount; /* Total entries in dynsym table */
|
2018-10-29 21:08:19 +00:00
|
|
|
const Elf_Addr *bloom_gnu; /* Bloom filter used by GNU hash func */
|
2012-04-30 13:31:10 +00:00
|
|
|
const Elf_Hashelt *buckets_gnu; /* GNU hash table bucket array */
|
|
|
|
const Elf_Hashelt *chain_zero_gnu; /* GNU hash table value array (Zeroed) */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2018-10-29 21:08:19 +00:00
|
|
|
const char *rpath; /* Search path specified in object */
|
|
|
|
const char *runpath; /* Search path with different priority */
|
1998-03-07 19:24:35 +00:00
|
|
|
Needed_Entry *needed; /* Shared objects needed by this one (%) */
|
2010-12-25 08:51:20 +00:00
|
|
|
Needed_Entry *needed_filtees;
|
|
|
|
Needed_Entry *needed_aux_filtees;
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we
|
|
|
|
know about. */
|
|
|
|
Ver_Entry *vertab; /* Versions required /defined by this object */
|
|
|
|
int vernum; /* Number of entries in vertab */
|
|
|
|
|
2001-10-29 10:10:10 +00:00
|
|
|
Elf_Addr init; /* Initialization function to call */
|
|
|
|
Elf_Addr fini; /* Termination function to call */
|
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
|
|
|
Elf_Addr preinit_array; /* Pre-initialization array of functions */
|
|
|
|
Elf_Addr init_array; /* Initialization array of functions */
|
|
|
|
Elf_Addr fini_array; /* Termination array of functions */
|
|
|
|
int preinit_array_num; /* Number of entries in preinit_array */
|
|
|
|
int init_array_num; /* Number of entries in init_array */
|
|
|
|
int fini_array_num; /* Number of entries in fini_array */
|
|
|
|
|
|
|
|
int32_t osrel; /* OSREL note value */
|
2018-11-23 22:37:35 +00:00
|
|
|
uint32_t fctl0; /* FEATURE_CONTROL note desc[0] value */
|
1998-03-07 19:24:35 +00:00
|
|
|
|
2007-04-03 18:31:20 +00:00
|
|
|
bool mainprog : 1; /* True if this is the main program */
|
|
|
|
bool rtld : 1; /* True if this is the dynamic linker */
|
2012-03-20 13:20:49 +00:00
|
|
|
bool relocated : 1; /* True if processed by relocate_objects() */
|
2012-03-27 14:10:15 +00:00
|
|
|
bool ver_checked : 1; /* True if processed by rtld_verify_object_versions */
|
2007-04-03 18:31:20 +00:00
|
|
|
bool textrel : 1; /* True if there are relocations to text seg */
|
|
|
|
bool symbolic : 1; /* True if generated with "-Bsymbolic" */
|
|
|
|
bool bind_now : 1; /* True if all relocations should be made first */
|
|
|
|
bool traced : 1; /* Already printed in ldd trace output */
|
|
|
|
bool jmpslots_done : 1; /* Already have relocated the jump slots */
|
|
|
|
bool init_done : 1; /* Already have added object to init list */
|
2023-06-05 00:43:55 +00:00
|
|
|
bool tls_static : 1; /* Already allocated offset for static TLS */
|
2023-06-05 00:54:46 +00:00
|
|
|
bool tls_dynamic : 1; /* A non-static DTV entry has been allocated */
|
2007-04-03 18:31:20 +00:00
|
|
|
bool phdr_alloc : 1; /* Phdr is allocated and needs to be freed. */
|
2009-03-18 13:40:37 +00:00
|
|
|
bool z_origin : 1; /* Process rpath and soname tokens */
|
2009-03-30 08:47:28 +00:00
|
|
|
bool z_nodelete : 1; /* Do not unload the object and dependencies */
|
2009-11-26 13:57:20 +00:00
|
|
|
bool z_noopen : 1; /* Do not load on dlopen */
|
2010-12-25 08:51:20 +00:00
|
|
|
bool z_loadfltr : 1; /* Immediately load filtees */
|
2013-10-07 08:19:30 +00:00
|
|
|
bool z_interpose : 1; /* Interpose all objects but main */
|
2012-07-15 10:53:48 +00:00
|
|
|
bool z_nodeflib : 1; /* Don't search default library path */
|
2015-04-15 08:16:34 +00:00
|
|
|
bool z_global : 1; /* Make the object global */
|
2020-06-02 16:20:58 +00:00
|
|
|
bool z_pie : 1; /* Object proclaimed itself PIE executable */
|
2019-03-29 17:52:57 +00:00
|
|
|
bool static_tls : 1; /* Needs static TLS allocation */
|
|
|
|
bool static_tls_copied : 1; /* Needs static TLS copying */
|
2009-06-20 14:16:41 +00:00
|
|
|
bool ref_nodel : 1; /* Refcount increased to prevent dlclose */
|
|
|
|
bool init_scanned: 1; /* Object is already on init list. */
|
|
|
|
bool on_fini_list: 1; /* Object is already on fini list. */
|
If dlopen() is called for the dso that has been already loaded as a
dependency, then the dso never has its DAG initialized. Empty DAG
makes ref_dag() call in dlopen() a nop, and the dso refcount is off
by one.
Initialize the DAG on the first dlopen() call, using a boolean flag
to prevent double initialization.
From the PR (edited):
Assume we have a library liba.so, containing a function a(), and a
library libb.so, containing function b(). liba.so needs functionality
from libb.so, so liba.so links in libb.so.
An application doesn't know about the relation between these libraries,
but needs to call a() and b(). It dlopen()s liba.so and obtains a
pointer to a(), then it dlopen()s libb.so and obtains a pointer to b().
As soon as the application doesn't need a() anymore, it dlclose()s liba.so.
Expected result: the pointer to b() is still valid and can be called
Actual result: the pointer to b() has become invalid, even though the
application did not dlclose() the handle to libb.so. On calling b(), the
application crashes with a segmentation fault.
PR: misc/151861
Based on patch by: jh
Reviewed by: kan
Tested by: Arjan van Leeuwen <freebsd-maintainer opera com>
MFC after: 1 week
2010-11-03 09:23:08 +00:00
|
|
|
bool dag_inited : 1; /* Object has its DAG initialized. */
|
2010-12-25 08:51:20 +00:00
|
|
|
bool filtees_loaded : 1; /* Filtees loaded */
|
2011-12-12 11:03:14 +00:00
|
|
|
bool irelative : 1; /* Object has R_MACHDEP_IRELATIVE relocs */
|
2020-02-13 23:42:09 +00:00
|
|
|
bool irelative_nonplt : 1; /* Object has R_MACHDEP_IRELATIVE non-plt relocs */
|
2011-12-12 11:03:14 +00:00
|
|
|
bool gnu_ifunc : 1; /* Object has references to STT_GNU_IFUNC */
|
2014-08-29 10:43:56 +00:00
|
|
|
bool non_plt_gnu_ifunc : 1; /* Object has non-plt IFUNC references */
|
2018-12-03 20:03:43 +00:00
|
|
|
bool ifuncs_resolved : 1; /* Object ifuncs were already resolved */
|
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
|
|
|
bool crt_no_init : 1; /* Object' crt does not call _init/_fini */
|
2012-04-30 13:31:10 +00:00
|
|
|
bool valid_hash_sysv : 1; /* A valid System V hash hash tag is available */
|
|
|
|
bool valid_hash_gnu : 1; /* A valid GNU hash tag is available */
|
2015-01-03 18:09:53 +00:00
|
|
|
bool dlopened : 1; /* dlopen()-ed (vs. load statically) */
|
2016-01-20 07:21:33 +00:00
|
|
|
bool marker : 1; /* marker on the global obj list */
|
2016-12-22 17:37:39 +00:00
|
|
|
bool unholdfree : 1; /* unmap upon last unhold */
|
2016-12-22 17:41:32 +00:00
|
|
|
bool doomed : 1; /* Object cannot be referenced */
|
1998-04-30 07:48:02 +00:00
|
|
|
|
2009-06-20 14:16:41 +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 */
|
2012-01-07 16:09:54 +00:00
|
|
|
void *priv; /* Platform-dependent */
|
1998-03-07 19:24:35 +00:00
|
|
|
} Obj_Entry;
|
|
|
|
|
|
|
|
#define RTLD_MAGIC 0xd550b87a
|
|
|
|
#define RTLD_VERSION 1
|
|
|
|
|
2016-01-20 07:21:33 +00:00
|
|
|
TAILQ_HEAD(obj_entry_q, Struct_Obj_Entry);
|
|
|
|
|
2009-05-27 18:54:31 +00:00
|
|
|
#define RTLD_STATIC_TLS_EXTRA 128
|
2004-08-03 08:51:00 +00:00
|
|
|
|
2005-12-18 19:43:33 +00:00
|
|
|
/* Flags to be passed into symlook_ family of functions. */
|
|
|
|
#define SYMLOOK_IN_PLT 0x01 /* Lookup for PLT symbol */
|
2011-12-09 20:40:24 +00:00
|
|
|
#define SYMLOOK_DLSYM 0x02 /* Return newest versioned symbol. Used by
|
2005-12-18 19:43:33 +00:00
|
|
|
dlsym. */
|
2012-03-20 13:20:49 +00:00
|
|
|
#define SYMLOOK_EARLY 0x04 /* Symlook is done during initialization. */
|
2014-08-29 09:29:10 +00:00
|
|
|
#define SYMLOOK_IFUNC 0x08 /* Allow IFUNC processing in
|
|
|
|
reloc_non_plt(). */
|
2005-12-18 19:43:33 +00:00
|
|
|
|
2009-11-26 13:57:20 +00:00
|
|
|
/* Flags for load_object(). */
|
2009-11-28 14:29:32 +00:00
|
|
|
#define RTLD_LO_NOLOAD 0x01 /* dlopen() specified RTLD_NOLOAD. */
|
|
|
|
#define RTLD_LO_DLOPEN 0x02 /* Load_object() called from dlopen(). */
|
|
|
|
#define RTLD_LO_TRACE 0x04 /* Only tracing. */
|
2010-12-25 08:51:20 +00:00
|
|
|
#define RTLD_LO_NODELETE 0x08 /* Loaded object cannot be closed. */
|
|
|
|
#define RTLD_LO_FILTEES 0x10 /* Loading filtee. */
|
2012-03-20 13:20:49 +00:00
|
|
|
#define RTLD_LO_EARLY 0x20 /* Do not call ctors, postpone it to the
|
|
|
|
initialization during the image start. */
|
2020-04-22 18:39:45 +00:00
|
|
|
#define RTLD_LO_IGNSTLS 0x40 /* Do not allocate static TLS */
|
2020-05-15 11:58:01 +00:00
|
|
|
#define RTLD_LO_DEEPBIND 0x80 /* Force symbolic for this object */
|
2009-11-26 13:57:20 +00:00
|
|
|
|
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;
|
|
|
|
|
2010-12-25 08:51:20 +00:00
|
|
|
/*
|
|
|
|
* This structure provides a reentrant way to keep a list of objects and
|
|
|
|
* check which ones have already been processed in some way.
|
|
|
|
*/
|
|
|
|
typedef struct Struct_DoneList {
|
|
|
|
const Obj_Entry **objs; /* Array of object pointers */
|
|
|
|
unsigned int num_alloc; /* Allocated size of the array */
|
|
|
|
unsigned int num_used; /* Number of array slots used */
|
|
|
|
} DoneList;
|
|
|
|
|
|
|
|
struct Struct_RtldLockState {
|
|
|
|
int lockstate;
|
2011-02-09 09:20:27 +00:00
|
|
|
sigjmp_buf env;
|
2010-12-25 08:51:20 +00:00
|
|
|
};
|
|
|
|
|
2012-07-15 10:53:48 +00:00
|
|
|
struct fill_search_info_args {
|
|
|
|
int request;
|
|
|
|
unsigned int flags;
|
|
|
|
struct dl_serinfo *serinfo;
|
|
|
|
struct dl_serpath *serpath;
|
|
|
|
char *strspace;
|
|
|
|
};
|
|
|
|
|
2010-12-25 08:51:20 +00:00
|
|
|
/*
|
|
|
|
* The pack of arguments and results for the symbol lookup functions.
|
|
|
|
*/
|
|
|
|
typedef struct Struct_SymLook {
|
|
|
|
const char *name;
|
|
|
|
unsigned long hash;
|
2012-04-30 13:31:10 +00:00
|
|
|
uint32_t hash_gnu;
|
2010-12-25 08:51:20 +00:00
|
|
|
const Ver_Entry *ventry;
|
|
|
|
int flags;
|
|
|
|
const Obj_Entry *defobj_out;
|
|
|
|
const Elf_Sym *sym_out;
|
|
|
|
struct Struct_RtldLockState *lockstate;
|
|
|
|
} SymLook;
|
|
|
|
|
2015-03-29 18:53:21 +00:00
|
|
|
void _rtld_error(const char *, ...) __printflike(1, 2) __exported;
|
2015-04-02 21:35:36 +00:00
|
|
|
void rtld_die(void) __dead2;
|
2012-03-23 12:13:31 +00:00
|
|
|
const char *rtld_strerror(int);
|
|
|
|
Obj_Entry *map_object(int, const char *, const struct stat *);
|
|
|
|
void *xcalloc(size_t, size_t);
|
|
|
|
void *xmalloc(size_t);
|
|
|
|
char *xstrdup(const char *);
|
2023-07-30 01:51:52 +00:00
|
|
|
void *xmalloc_aligned(size_t size, size_t align, size_t offset);
|
1998-09-04 19:03:57 +00:00
|
|
|
extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
|
2010-09-12 17:04:51 +00:00
|
|
|
extern Elf_Sym sym_zero; /* For resolving undefined weak refs. */
|
2017-03-15 21:11:57 +00:00
|
|
|
extern bool ld_bind_not;
|
2020-02-09 12:22:43 +00:00
|
|
|
extern bool ld_fast_sigblock;
|
1998-09-04 19:03:57 +00:00
|
|
|
|
2012-03-23 12:13:31 +00:00
|
|
|
void dump_relocations(Obj_Entry *);
|
|
|
|
void dump_obj_relocations(Obj_Entry *);
|
|
|
|
void dump_Elf_Rel(Obj_Entry *, const Elf_Rel *, u_long);
|
|
|
|
void dump_Elf_Rela(Obj_Entry *, const Elf_Rela *, u_long);
|
2003-06-19 03:55:38 +00:00
|
|
|
|
1998-09-04 19:03:57 +00:00
|
|
|
/*
|
|
|
|
* Function declarations.
|
|
|
|
*/
|
2022-04-04 15:05:40 +00:00
|
|
|
uintptr_t rtld_round_page(uintptr_t);
|
|
|
|
uintptr_t rtld_trunc_page(uintptr_t);
|
2023-04-12 15:07:26 +00:00
|
|
|
Elf32_Word elf_hash(const char *);
|
2000-09-19 04:27:16 +00:00
|
|
|
const Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
|
2010-12-25 08:51:20 +00:00
|
|
|
const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *);
|
2003-06-19 02:39:37 +00:00
|
|
|
void lockdflt_init(void);
|
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
|
|
|
void digest_notes(Obj_Entry *, Elf_Addr, Elf_Addr);
|
2016-01-20 07:21:33 +00:00
|
|
|
Obj_Entry *globallist_curr(const Obj_Entry *obj);
|
|
|
|
Obj_Entry *globallist_next(const Obj_Entry *obj);
|
1999-08-30 01:48:19 +00:00
|
|
|
void obj_free(Obj_Entry *);
|
|
|
|
Obj_Entry *obj_new(void);
|
2021-04-10 12:30:59 +00:00
|
|
|
Obj_Entry *obj_from_addr(const void *);
|
1999-04-09 00:28:43 +00:00
|
|
|
void _rtld_bind_start(void);
|
2011-12-12 11:03:14 +00:00
|
|
|
void *rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def);
|
2010-12-25 08:51:20 +00:00
|
|
|
void symlook_init(SymLook *, const char *);
|
|
|
|
int symlook_obj(SymLook *, const Obj_Entry *);
|
2021-12-09 21:17:54 +00:00
|
|
|
void *tls_get_addr_common(uintptr_t **dtvp, int index, size_t offset);
|
2004-08-03 08:51:00 +00:00
|
|
|
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);
|
2005-12-18 19:43:33 +00:00
|
|
|
const Ver_Entry *fetch_ventry(const Obj_Entry *obj, unsigned long);
|
2016-03-02 16:36:24 +00:00
|
|
|
int convert_prot(int elfflags);
|
2021-11-11 17:51:26 +00:00
|
|
|
bool check_elf_headers(const Elf_Ehdr *hdr, const char *path);
|
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 *);
|
2012-03-20 13:20:49 +00:00
|
|
|
int reloc_non_plt(Obj_Entry *, Obj_Entry *, int flags,
|
|
|
|
struct Struct_RtldLockState *);
|
2018-12-15 10:38:07 +00:00
|
|
|
int reloc_plt(Obj_Entry *, int flags, struct Struct_RtldLockState *);
|
2012-03-20 13:20:49 +00:00
|
|
|
int reloc_jmpslots(Obj_Entry *, int flags, struct Struct_RtldLockState *);
|
2011-12-12 11:03:14 +00:00
|
|
|
int reloc_iresolve(Obj_Entry *, struct Struct_RtldLockState *);
|
2020-02-13 23:42:09 +00:00
|
|
|
int reloc_iresolve_nonplt(Obj_Entry *, struct Struct_RtldLockState *);
|
2012-03-20 13:20:49 +00:00
|
|
|
int reloc_gnu_ifunc(Obj_Entry *, int flags, struct Struct_RtldLockState *);
|
2018-02-03 23:14:11 +00:00
|
|
|
void ifunc_init(Elf_Auxinfo[__min_size(AT_COUNT)]);
|
|
|
|
void init_pltgot(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 /* } */
|