Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.h revision 1.105
      1 /*	$NetBSD: rtld.h,v 1.105 2011/03/29 20:56:35 joerg Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #ifndef RTLD_H
     35 #define RTLD_H
     36 
     37 #include <dlfcn.h>
     38 #include <signal.h>
     39 #include <stdbool.h>
     40 #include <stddef.h>
     41 #include <sys/param.h>
     42 #include <sys/types.h>
     43 #include <sys/queue.h>
     44 #include <sys/exec_elf.h>
     45 #include <sys/tls.h>
     46 #include "rtldenv.h"
     47 #include "link.h"
     48 
     49 #if defined(_RTLD_SOURCE)
     50 
     51 #ifndef	RTLD_DEFAULT_LIBRARY_PATH
     52 #define	RTLD_DEFAULT_LIBRARY_PATH	"/usr/lib"
     53 #endif
     54 #define _PATH_LD_HINTS			"/etc/ld.so.conf"
     55 
     56 extern size_t _rtld_pagesz;
     57 
     58 #define round_down(x)	((x) & ~(_rtld_pagesz - 1))
     59 #define round_up(x)	round_down((x) + _rtld_pagesz - 1)
     60 
     61 #define NEW(type)	((type *) xmalloc(sizeof(type)))
     62 #define CNEW(type)	((type *) xcalloc(sizeof(type)))
     63 
     64 /*
     65  * Fill in a DoneList with an allocation large enough to hold all of
     66  * the currently-loaded objects. Keep this in a macro since it calls
     67  * alloca and we want that to occur within the scope of the caller.
     68  */
     69 #define _rtld_donelist_init(dlp)					\
     70     ((dlp)->num_alloc = _rtld_objcount,					\
     71     (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])),	\
     72     assert((dlp)->objs != NULL),					\
     73     (dlp)->num_used = 0)
     74 
     75 #endif /* _RTLD_SOURCE */
     76 
     77 /*
     78  * C++ has mandated the use of the following keywords for its new boolean
     79  * type.  We might as well follow their lead.
     80  */
     81 struct Struct_Obj_Entry;
     82 
     83 typedef struct Struct_Objlist_Entry {
     84 	SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
     85 	struct Struct_Obj_Entry *obj;
     86 } Objlist_Entry;
     87 
     88 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
     89 
     90 typedef struct Struct_Name_Entry {
     91 	STAILQ_ENTRY(Struct_Name_Entry)	link;
     92 	char	name[1];
     93 } Name_Entry;
     94 
     95 typedef struct Struct_Needed_Entry {
     96 	struct Struct_Needed_Entry *next;
     97 	struct Struct_Obj_Entry *obj;
     98 	unsigned long   name;	/* Offset of name in string table */
     99 } Needed_Entry;
    100 
    101 typedef struct _rtld_search_path_t {
    102 	struct _rtld_search_path_t *sp_next;
    103 	const char     *sp_path;
    104 	size_t          sp_pathlen;
    105 } Search_Path;
    106 
    107 
    108 #define RTLD_MAX_ENTRY 10
    109 #define RTLD_MAX_LIBRARY 4
    110 #define RTLD_MAX_CTL 2
    111 typedef struct _rtld_library_xform_t {
    112 	struct _rtld_library_xform_t *next;
    113 	char *name;
    114 	const char *ctlname;
    115 	struct {
    116 		char *value;
    117 		char *library[RTLD_MAX_LIBRARY];
    118 	} entry[RTLD_MAX_ENTRY];
    119 } Library_Xform;
    120 
    121 /*
    122  * Shared object descriptor.
    123  *
    124  * Items marked with "(%)" are dynamically allocated, and must be freed
    125  * when the structure is destroyed.
    126  *
    127  * The layout of this structure needs to be preserved because pre-2.0 binaries
    128  * hard-coded the location of dlopen() and friends.
    129  */
    130 
    131 #define RTLD_MAGIC	0xd550b87a
    132 #define RTLD_VERSION	1
    133 
    134 typedef struct Struct_Obj_Entry {
    135 	Elf32_Word      magic;		/* Magic number (sanity check) */
    136 	Elf32_Word      version;	/* Version number of struct format */
    137 
    138 	struct Struct_Obj_Entry *next;
    139 	char           *path;		/* Pathname of underlying file (%) */
    140 	int             refcount;
    141 	int             dl_refcount;	/* Number of times loaded by dlopen */
    142 
    143 	/* These items are computed by map_object() or by digest_phdr(). */
    144 	caddr_t         mapbase;	/* Base address of mapped region */
    145 	size_t          mapsize;	/* Size of mapped region in bytes */
    146 	size_t          textsize;	/* Size of text segment in bytes */
    147 	Elf_Addr        vaddrbase;	/* Base address in shared object file */
    148 	caddr_t         relocbase;	/* Reloc const = mapbase - *vaddrbase */
    149 	Elf_Dyn        *dynamic;	/* Dynamic section */
    150 	caddr_t         entry;		/* Entry point */
    151 	const Elf_Phdr *phdr;		/* Program header (may be xmalloc'ed) */
    152 	size_t		phsize;		/* Size of program header in bytes */
    153 
    154 	/* Items from the dynamic section. */
    155 	Elf_Addr       *pltgot;		/* PLTGOT table */
    156 	const Elf_Rel  *rel;		/* Relocation entries */
    157 	const Elf_Rel  *rellim;		/* Limit of Relocation entries */
    158 	const Elf_Rela *rela;		/* Relocation entries */
    159 	const Elf_Rela *relalim;	/* Limit of Relocation entries */
    160 	const Elf_Rel  *pltrel;		/* PLT relocation entries */
    161 	const Elf_Rel  *pltrellim;	/* Limit of PLT relocation entries */
    162 	const Elf_Rela *pltrela;	/* PLT relocation entries */
    163 	const Elf_Rela *pltrelalim;	/* Limit of PLT relocation entries */
    164 	const Elf_Sym  *symtab;		/* Symbol table */
    165 	const char     *strtab;		/* String table */
    166 	unsigned long   strsize;	/* Size in bytes of string table */
    167 #ifdef __mips__
    168 	Elf_Word        local_gotno;	/* Number of local GOT entries */
    169 	Elf_Word        symtabno;	/* Number of dynamic symbols */
    170 	Elf_Word        gotsym;		/* First dynamic symbol in GOT */
    171 #endif
    172 
    173 	const Elf_Symindx *buckets;	/* Hash table buckets array */
    174 	unsigned long	unused1;	/* Used to be nbuckets */
    175 	const Elf_Symindx *chains;	/* Hash table chain array */
    176 	unsigned long   nchains;	/* Number of chains */
    177 
    178 	Search_Path    *rpaths;		/* Search path specified in object */
    179 	Needed_Entry   *needed;		/* Shared objects needed by this (%) */
    180 
    181 	void            (*init)(void); 	/* Initialization function to call */
    182 	void            (*fini)(void);	/* Termination function to call */
    183 
    184 	/* Entry points for dlopen() and friends. */
    185 	void           *(*dlopen)(const char *, int);
    186 	void           *(*dlsym)(void *, const char *);
    187 	char           *(*dlerror)(void);
    188 	int             (*dlclose)(void *);
    189 	int             (*dladdr)(const void *, Dl_info *);
    190 
    191 	u_int32_t	mainprog:1,	/* True if this is the main program */
    192 	        	rtld:1,		/* True if this is the dynamic linker */
    193 			textrel:1,	/* True if there are relocations to
    194 					 * text seg */
    195 			symbolic:1,	/* True if generated with
    196 					 * "-Bsymbolic" */
    197 			printed:1,	/* True if ldd has printed it */
    198 			isdynamic:1,	/* True if this is a pure PIC object */
    199 			mainref:1,	/* True if on _rtld_list_main */
    200 			globalref:1,	/* True if on _rtld_list_global */
    201 			init_done:1,	/* True if .init has been added */
    202 			init_called:1,	/* True if .init function has been
    203 					 * called */
    204 			fini_called:1,	/* True if .fini function has been
    205 					 * called */
    206 			z_now:1,	/* True if object's symbols should be
    207 					   bound immediately */
    208 			z_nodelete:1,	/* True if object should never be
    209 					   unloaded */
    210 			z_initfirst:1,	/* True if object's .init/.fini take
    211 					 * priority over others */
    212 			z_noopen:1,	/* True if object should never be
    213 					   dlopen'ed */
    214 			phdr_loaded:1,	/* Phdr is loaded and doesn't need to
    215 					 * be freed. */
    216 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    217 			tls_done:1,	/* True if static TLS offset
    218 					 * has been allocated */
    219 #endif
    220 			ref_nodel:1;	/* Refcount increased to prevent dlclose */
    221 
    222 	struct link_map linkmap;	/* for GDB */
    223 
    224 	/* These items are computed by map_object() or by digest_phdr(). */
    225 	const char     *interp;	/* Pathname of the interpreter, if any */
    226 	Objlist         dldags;	/* Object belongs to these dlopened DAGs (%) */
    227 	Objlist         dagmembers;	/* DAG has these members (%) */
    228 	dev_t           dev;		/* Object's filesystem's device */
    229 	ino_t           ino;		/* Object's inode number */
    230 
    231 	void		*ehdr;
    232 
    233 	uint32_t        nbuckets;	/* Number of buckets */
    234 	uint32_t        nbuckets_m;	/* Precomputed for fast remainder */
    235 	uint8_t         nbuckets_s1;
    236 	uint8_t         nbuckets_s2;
    237 	size_t		pathlen;	/* Pathname length */
    238 	STAILQ_HEAD(, Struct_Name_Entry) names;	/* List of names for this object we
    239 						   know about. */
    240 
    241 #ifdef __powerpc__
    242 	Elf_Addr       *gotptr;		/* GOT table (secure-plt only) */
    243 #endif
    244 
    245 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    246 	/* Thread Local Storage support for this module */
    247 	size_t		tlsindex;	/* Index in DTV */
    248 	void		*tlsinit;	/* Base address of TLS init block */
    249 	size_t		tlsinitsize;	/* Size of TLS init block */
    250 	size_t		tlssize;	/* Size of TLS block */
    251 	size_t		tlsoffset;	/* Offset in the static TLS block */
    252 	size_t		tlsalign;	/* Needed alignment for static TLS */
    253 #endif
    254 } Obj_Entry;
    255 
    256 typedef struct Struct_DoneList {
    257 	const Obj_Entry **objs;		/* Array of object pointers */
    258 	unsigned int num_alloc;		/* Allocated size of the array */
    259 	unsigned int num_used;		/* Number of array slots used */
    260 } DoneList;
    261 
    262 
    263 #if defined(_RTLD_SOURCE)
    264 
    265 extern struct r_debug _rtld_debug;
    266 extern Search_Path *_rtld_default_paths;
    267 extern Obj_Entry *_rtld_objlist;
    268 extern Obj_Entry **_rtld_objtail;
    269 extern u_int _rtld_objcount;
    270 extern u_int _rtld_objloads;
    271 extern Obj_Entry *_rtld_objmain;
    272 extern Obj_Entry _rtld_objself;
    273 extern Search_Path *_rtld_paths;
    274 extern Library_Xform *_rtld_xforms;
    275 extern bool _rtld_trust;
    276 extern Objlist _rtld_list_global;
    277 extern Objlist _rtld_list_main;
    278 extern Elf_Sym _rtld_sym_zero;
    279 
    280 #define	RTLD_MODEMASK 0x3
    281 
    282 /* Flags for _rtld_load_object() and friends. */
    283 #define	_RTLD_GLOBAL	0x01	/* Add object to global DAG. */
    284 #define	_RTLD_MAIN	0x02
    285 #define	_RTLD_NOLOAD	0x04	/* dlopen() specified RTLD_NOLOAD. */
    286 #define	_RTLD_DLOPEN	0x08	/* Load_object() called from dlopen(). */
    287 
    288 /* Preallocation for static TLS model */
    289 #define	RTLD_STATIC_TLS_RESERVATION	64
    290 
    291 /* rtld.c */
    292 
    293 /* We export these symbols using _rtld_symbol_lookup and is_exported. */
    294 __dso_public char *dlerror(void);
    295 __dso_public void *dlopen(const char *, int);
    296 __dso_public void *dlsym(void *, const char *);
    297 __dso_public int dlclose(void *);
    298 __dso_public int dladdr(const void *, Dl_info *);
    299 __dso_public int dlinfo(void *, int, void *);
    300 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
    301     void *);
    302 
    303 /* These aren't exported */
    304 void _rtld_error(const char *, ...)
    305      __attribute__((__format__(__printf__,1,2)));
    306 void _rtld_die(void) __attribute__((__noreturn__));
    307 void *_rtld_objmain_sym(const char *);
    308 __dso_public void _rtld_debug_state(void);
    309 void _rtld_linkmap_add(Obj_Entry *);
    310 void _rtld_linkmap_delete(Obj_Entry *);
    311 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
    312 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
    313 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
    314 void _rtld_ref_dag(Obj_Entry *);
    315 
    316 void _rtld_shared_enter(void);
    317 void _rtld_shared_exit(void);
    318 void _rtld_exclusive_enter(sigset_t *);
    319 void _rtld_exclusive_exit(sigset_t *);
    320 
    321 /* expand.c */
    322 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
    323     const char *);
    324 
    325 /* headers.c */
    326 void _rtld_digest_dynamic(const char *, Obj_Entry *);
    327 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
    328 
    329 /* load.c */
    330 Obj_Entry *_rtld_load_object(const char *, int);
    331 int _rtld_load_needed_objects(Obj_Entry *, int);
    332 int _rtld_preload(const char *);
    333 
    334 #define	OBJ_ERR	(Obj_Entry *)(-1)
    335 /* path.c */
    336 void _rtld_add_paths(const char *, Search_Path **, const char *);
    337 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
    338     const char *);
    339 int _rtld_sysctl(const char *, void *, size_t *);
    340 
    341 /* reloc.c */
    342 int _rtld_do_copy_relocations(const Obj_Entry *);
    343 int _rtld_relocate_objects(Obj_Entry *, bool);
    344 int _rtld_relocate_nonplt_objects(Obj_Entry *);
    345 int _rtld_relocate_plt_lazy(const Obj_Entry *);
    346 int _rtld_relocate_plt_objects(const Obj_Entry *);
    347 void _rtld_setup_pltgot(const Obj_Entry *);
    348 
    349 /* search.c */
    350 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
    351 
    352 /* symbol.c */
    353 unsigned long _rtld_elf_hash(const char *);
    354 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
    355     const Obj_Entry *, bool);
    356 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
    357     const Obj_Entry **, bool);
    358 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
    359     const Obj_Entry **, bool);
    360 
    361 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
    362     const Objlist *, const Obj_Entry **, bool, DoneList *);
    363 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
    364     const Obj_Entry *, const Obj_Entry **, bool);
    365 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
    366     const Needed_Entry *, const Obj_Entry **, bool,
    367     DoneList *, DoneList *);
    368 #ifdef COMBRELOC
    369 void _rtld_combreloc_reset(const Obj_Entry *);
    370 #endif
    371 
    372 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    373 /* tls.c */
    374 void *_rtld_tls_get_addr(void *, size_t, size_t);
    375 void _rtld_tls_initial_allocation(void);
    376 void *_rtld_tls_module_allocate(size_t index);
    377 int _rtld_tls_offset_allocate(Obj_Entry *);
    378 void _rtld_tls_offset_free(Obj_Entry *);
    379 
    380 extern size_t _rtld_tls_dtv_generation;
    381 extern size_t _rtld_tls_max_index;
    382 
    383 __dso_public extern void *__tls_get_addr(void *);
    384 #ifdef __i386__
    385 __dso_public extern void *___tls_get_addr(void *)
    386     __attribute__((__regparm__(1)));
    387 #endif
    388 #endif
    389 
    390 /* map_object.c */
    391 struct stat;
    392 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
    393 void _rtld_obj_free(Obj_Entry *);
    394 Obj_Entry *_rtld_obj_new(void);
    395 
    396 /* function descriptors */
    397 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    398 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
    399     const Elf_Sym *, Elf_Addr);
    400 const void *_rtld_function_descriptor_function(const void *);
    401 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    402 
    403 #endif /* _RTLD_SOURCE */
    404 
    405 #endif /* RTLD_H */
    406