Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.h revision 1.117
      1 /*	$NetBSD: rtld.h,v 1.117 2014/03/06 19:19:40 matt 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 #ifdef __ARM_EABI__
     52 #include "unwind.h"
     53 #endif
     54 
     55 #ifndef	RTLD_DEFAULT_LIBRARY_PATH
     56 #define	RTLD_DEFAULT_LIBRARY_PATH	"/usr/lib"
     57 #endif
     58 #define _PATH_LD_HINTS			"/etc/ld.so.conf"
     59 
     60 extern size_t _rtld_pagesz;
     61 
     62 #define round_down(x)	((x) & ~(_rtld_pagesz - 1))
     63 #define round_up(x)	round_down((x) + _rtld_pagesz - 1)
     64 
     65 #define NEW(type)	((type *) xmalloc(sizeof(type)))
     66 #define CNEW(type)	((type *) xcalloc(sizeof(type)))
     67 
     68 /*
     69  * Fill in a DoneList with an allocation large enough to hold all of
     70  * the currently-loaded objects. Keep this in a macro since it calls
     71  * alloca and we want that to occur within the scope of the caller.
     72  */
     73 #define _rtld_donelist_init(dlp)					\
     74     ((dlp)->num_alloc = _rtld_objcount,					\
     75     (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])),	\
     76     assert((dlp)->objs != NULL),					\
     77     (dlp)->num_used = 0)
     78 
     79 #endif /* _RTLD_SOURCE */
     80 
     81 /*
     82  * C++ has mandated the use of the following keywords for its new boolean
     83  * type.  We might as well follow their lead.
     84  */
     85 struct Struct_Obj_Entry;
     86 
     87 typedef struct Struct_Objlist_Entry {
     88 	SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
     89 	struct Struct_Obj_Entry *obj;
     90 } Objlist_Entry;
     91 
     92 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
     93 
     94 typedef struct Struct_Name_Entry {
     95 	SIMPLEQ_ENTRY(Struct_Name_Entry) link;
     96 	char	name[1];
     97 } Name_Entry;
     98 
     99 typedef struct Struct_Needed_Entry {
    100 	struct Struct_Needed_Entry *next;
    101 	struct Struct_Obj_Entry *obj;
    102 	unsigned long   name;	/* Offset of name in string table */
    103 } Needed_Entry;
    104 
    105 typedef struct _rtld_search_path_t {
    106 	struct _rtld_search_path_t *sp_next;
    107 	const char     *sp_path;
    108 	size_t          sp_pathlen;
    109 } Search_Path;
    110 
    111 typedef struct Struct_Ver_Entry {
    112 	Elf_Word        hash;
    113 	u_int           flags;
    114 	const char     *name;
    115 	const char     *file;
    116 } Ver_Entry;
    117 
    118 /* Ver_Entry.flags */
    119 #define VER_INFO_HIDDEN	0x01
    120 
    121 #define RTLD_MAX_ENTRY 10
    122 #define RTLD_MAX_LIBRARY 4
    123 #define RTLD_MAX_CTL 2
    124 typedef struct _rtld_library_xform_t {
    125 	struct _rtld_library_xform_t *next;
    126 	char *name;
    127 	const char *ctlname;
    128 	struct {
    129 		char *value;
    130 		char *library[RTLD_MAX_LIBRARY];
    131 	} entry[RTLD_MAX_ENTRY];
    132 } Library_Xform;
    133 
    134 /*
    135  * Shared object descriptor.
    136  *
    137  * Items marked with "(%)" are dynamically allocated, and must be freed
    138  * when the structure is destroyed.
    139  *
    140  * The layout of this structure needs to be preserved because pre-2.0 binaries
    141  * hard-coded the location of dlopen() and friends.
    142  */
    143 
    144 #define RTLD_MAGIC	0xd550b87a
    145 #define RTLD_VERSION	1
    146 
    147 typedef void (*fptr_t)(void);
    148 
    149 typedef struct Struct_Obj_Entry {
    150 	Elf32_Word      magic;		/* Magic number (sanity check) */
    151 	Elf32_Word      version;	/* Version number of struct format */
    152 
    153 	struct Struct_Obj_Entry *next;
    154 	char           *path;		/* Pathname of underlying file (%) */
    155 	int             refcount;
    156 	int             dl_refcount;	/* Number of times loaded by dlopen */
    157 
    158 	/* These items are computed by map_object() or by digest_phdr(). */
    159 	caddr_t         mapbase;	/* Base address of mapped region */
    160 	size_t          mapsize;	/* Size of mapped region in bytes */
    161 	size_t          textsize;	/* Size of text segment in bytes */
    162 	Elf_Addr        vaddrbase;	/* Base address in shared object file */
    163 	caddr_t         relocbase;	/* Reloc const = mapbase - *vaddrbase */
    164 	Elf_Dyn        *dynamic;	/* Dynamic section */
    165 	caddr_t         entry;		/* Entry point */
    166 	const Elf_Phdr *phdr;		/* Program header (may be xmalloc'ed) */
    167 	size_t		phsize;		/* Size of program header in bytes */
    168 
    169 	/* Items from the dynamic section. */
    170 	Elf_Addr       *pltgot;		/* PLTGOT table */
    171 	const Elf_Rel  *rel;		/* Relocation entries */
    172 	const Elf_Rel  *rellim;		/* Limit of Relocation entries */
    173 	const Elf_Rela *rela;		/* Relocation entries */
    174 	const Elf_Rela *relalim;	/* Limit of Relocation entries */
    175 	const Elf_Rel  *pltrel;		/* PLT relocation entries */
    176 	const Elf_Rel  *pltrellim;	/* Limit of PLT relocation entries */
    177 	const Elf_Rela *pltrela;	/* PLT relocation entries */
    178 	const Elf_Rela *pltrelalim;	/* Limit of PLT relocation entries */
    179 	const Elf_Sym  *symtab;		/* Symbol table */
    180 	const char     *strtab;		/* String table */
    181 	unsigned long   strsize;	/* Size in bytes of string table */
    182 #ifdef __mips__
    183 	Elf_Word        local_gotno;	/* Number of local GOT entries */
    184 	Elf_Word        symtabno;	/* Number of dynamic symbols */
    185 	Elf_Word        gotsym;		/* First dynamic symbol in GOT */
    186 #endif
    187 
    188 	const Elf_Symindx *buckets;	/* Hash table buckets array */
    189 	unsigned long	unused1;	/* Used to be nbuckets */
    190 	const Elf_Symindx *chains;	/* Hash table chain array */
    191 	unsigned long   nchains;	/* Number of chains */
    192 
    193 	Search_Path    *rpaths;		/* Search path specified in object */
    194 	Needed_Entry   *needed;		/* Shared objects needed by this (%) */
    195 
    196 	fptr_t		init;		/* Initialization function to call */
    197 	fptr_t		fini;		/* Termination function to call */
    198 
    199 	/*
    200 	 * BACKWARDS COMPAT Entry points for dlopen() and friends.
    201 	 *
    202 	 * DO NOT MOVE OR ADD TO THE LIST
    203 	 *
    204 	 */
    205 	void           *(*dlopen)(const char *, int);
    206 	void           *(*dlsym)(void *, const char *);
    207 	char           *(*dlerror)(void);
    208 	int             (*dlclose)(void *);
    209 	int             (*dladdr)(const void *, Dl_info *);
    210 
    211 	u_int32_t	mainprog:1,	/* True if this is the main program */
    212 	        	rtld:1,		/* True if this is the dynamic linker */
    213 			textrel:1,	/* True if there are relocations to
    214 					 * text seg */
    215 			symbolic:1,	/* True if generated with
    216 					 * "-Bsymbolic" */
    217 			printed:1,	/* True if ldd has printed it */
    218 			isdynamic:1,	/* True if this is a pure PIC object */
    219 			mainref:1,	/* True if on _rtld_list_main */
    220 			globalref:1,	/* True if on _rtld_list_global */
    221 			init_done:1,	/* True if .init has been added */
    222 			init_called:1,	/* True if .init function has been
    223 					 * called */
    224 			fini_called:1,	/* True if .fini function has been
    225 					 * called */
    226 			z_now:1,	/* True if object's symbols should be
    227 					   bound immediately */
    228 			z_nodelete:1,	/* True if object should never be
    229 					   unloaded */
    230 			z_initfirst:1,	/* True if object's .init/.fini take
    231 					 * priority over others */
    232 			z_noopen:1,	/* True if object should never be
    233 					   dlopen'ed */
    234 			phdr_loaded:1,	/* Phdr is loaded and doesn't need to
    235 					 * be freed. */
    236 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    237 			tls_done:1,	/* True if static TLS offset
    238 					 * has been allocated */
    239 #endif
    240 			ref_nodel:1;	/* Refcount increased to prevent dlclose */
    241 
    242 	struct link_map linkmap;	/* for GDB */
    243 
    244 	/* These items are computed by map_object() or by digest_phdr(). */
    245 	const char     *interp;	/* Pathname of the interpreter, if any */
    246 	Objlist         dldags;	/* Object belongs to these dlopened DAGs (%) */
    247 	Objlist         dagmembers;	/* DAG has these members (%) */
    248 	dev_t           dev;		/* Object's filesystem's device */
    249 	ino_t           ino;		/* Object's inode number */
    250 
    251 	void		*ehdr;
    252 
    253 	uint32_t        nbuckets;	/* Number of buckets */
    254 	uint32_t        nbuckets_m;	/* Precomputed for fast remainder */
    255 	uint8_t         nbuckets_s1;
    256 	uint8_t         nbuckets_s2;
    257 	size_t		pathlen;	/* Pathname length */
    258 	SIMPLEQ_HEAD(, Struct_Name_Entry) names; /* List of names for this
    259 						  * object we know about. */
    260 
    261 #ifdef __powerpc__
    262 #ifdef _LP64
    263 	Elf_Addr	glink;		/* global linkage */
    264 #else
    265 	Elf_Addr       *gotptr;		/* GOT table (secure-plt only) */
    266 #endif
    267 #endif
    268 
    269 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    270 	/* Thread Local Storage support for this module */
    271 	size_t		tlsindex;	/* Index in DTV */
    272 	void		*tlsinit;	/* Base address of TLS init block */
    273 	size_t		tlsinitsize;	/* Size of TLS init block */
    274 	size_t		tlssize;	/* Size of TLS block */
    275 	size_t		tlsoffset;	/* Offset in the static TLS block */
    276 	size_t		tlsalign;	/* Needed alignment for static TLS */
    277 #endif
    278 
    279 	/* symbol versioning */
    280 	const Elf_Verneed *verneed;	/* Required versions. */
    281 	Elf_Word	verneednum;	/* Number of entries in verneed table */
    282 	const Elf_Verdef  *verdef;	/* Provided versions. */
    283 	Elf_Word	verdefnum;	/* Number of entries in verdef table */
    284 	const Elf_Versym *versyms;	/* Symbol versions table */
    285 
    286 	Ver_Entry	*vertab;	/* Versions required/defined by this
    287 					 * object */
    288 	int		vertabnum;	/* Number of entries in vertab */
    289 
    290 	/* init_array/fini_array */
    291 	fptr_t		*init_array;	/* start of init array */
    292 	size_t		init_arraysz;	/* # of entries in it */
    293 	fptr_t		*fini_array;	/* start of fini array */
    294 	size_t		fini_arraysz;	/* # of entries in it */
    295 #ifdef __ARM_EABI__
    296 	void		*exidx_start;
    297 	size_t		exidx_sz;
    298 #endif
    299 } Obj_Entry;
    300 
    301 typedef struct Struct_DoneList {
    302 	const Obj_Entry **objs;		/* Array of object pointers */
    303 	unsigned int num_alloc;		/* Allocated size of the array */
    304 	unsigned int num_used;		/* Number of array slots used */
    305 } DoneList;
    306 
    307 
    308 #if defined(_RTLD_SOURCE)
    309 
    310 extern struct r_debug _rtld_debug;
    311 extern Search_Path *_rtld_default_paths;
    312 extern Obj_Entry *_rtld_objlist;
    313 extern Obj_Entry **_rtld_objtail;
    314 extern u_int _rtld_objcount;
    315 extern u_int _rtld_objloads;
    316 extern Obj_Entry *_rtld_objmain;
    317 extern Obj_Entry _rtld_objself;
    318 extern Search_Path *_rtld_paths;
    319 extern Library_Xform *_rtld_xforms;
    320 extern bool _rtld_trust;
    321 extern Objlist _rtld_list_global;
    322 extern Objlist _rtld_list_main;
    323 extern Elf_Sym _rtld_sym_zero;
    324 
    325 #define	RTLD_MODEMASK 0x3
    326 
    327 /* Flags to be passed into _rtld_symlook_ family of functions. */
    328 #define SYMLOOK_IN_PLT	0x01	/* Lookup for PLT symbol */
    329 #define SYMLOOK_DLSYM	0x02	/* Return newes versioned symbol.
    330 				   Used by dlsym. */
    331 
    332 /* Flags for _rtld_load_object() and friends. */
    333 #define	_RTLD_GLOBAL	0x01	/* Add object to global DAG. */
    334 #define	_RTLD_MAIN	0x02
    335 #define	_RTLD_NOLOAD	0x04	/* dlopen() specified RTLD_NOLOAD. */
    336 #define	_RTLD_DLOPEN	0x08	/* Load_object() called from dlopen(). */
    337 
    338 /* Preallocation for static TLS model */
    339 #define	RTLD_STATIC_TLS_RESERVATION	64
    340 
    341 /* rtld.c */
    342 
    343 /* We export these symbols using _rtld_symbol_lookup and is_exported. */
    344 __dso_public char *dlerror(void);
    345 __dso_public void *dlopen(const char *, int);
    346 __dso_public void *dlsym(void *, const char *);
    347 __dso_public int dlclose(void *);
    348 __dso_public int dladdr(const void *, Dl_info *);
    349 __dso_public int dlinfo(void *, int, void *);
    350 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
    351     void *);
    352 
    353 __dso_public void *_dlauxinfo(void) __pure;
    354 
    355 #ifdef __ARM_EABI__
    356 /*
    357  * This is used by libgcc to find the start and length of the exception table
    358  * associated with a PC.
    359  */
    360 __dso_public _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr, int *);
    361 #endif
    362 
    363 /* These aren't exported */
    364 void _rtld_error(const char *, ...) __printflike(1,2);
    365 void _rtld_die(void) __dead;
    366 void *_rtld_objmain_sym(const char *);
    367 __dso_public void _rtld_debug_state(void) __noinline;
    368 void _rtld_linkmap_add(Obj_Entry *);
    369 void _rtld_linkmap_delete(Obj_Entry *);
    370 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
    371 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
    372 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
    373 void _rtld_ref_dag(Obj_Entry *);
    374 
    375 void _rtld_shared_enter(void);
    376 void _rtld_shared_exit(void);
    377 void _rtld_exclusive_enter(sigset_t *);
    378 void _rtld_exclusive_exit(sigset_t *);
    379 
    380 /* expand.c */
    381 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
    382     const char *);
    383 
    384 /* headers.c */
    385 void _rtld_digest_dynamic(const char *, Obj_Entry *);
    386 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
    387 
    388 /* load.c */
    389 Obj_Entry *_rtld_load_object(const char *, int);
    390 int _rtld_load_needed_objects(Obj_Entry *, int);
    391 int _rtld_preload(const char *);
    392 
    393 #define	OBJ_ERR	(Obj_Entry *)(-1)
    394 /* path.c */
    395 void _rtld_add_paths(const char *, Search_Path **, const char *);
    396 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
    397     const char *);
    398 int _rtld_sysctl(const char *, void *, size_t *);
    399 
    400 /* reloc.c */
    401 int _rtld_do_copy_relocations(const Obj_Entry *);
    402 int _rtld_relocate_objects(Obj_Entry *, bool);
    403 int _rtld_relocate_nonplt_objects(Obj_Entry *);
    404 int _rtld_relocate_plt_lazy(const Obj_Entry *);
    405 int _rtld_relocate_plt_objects(const Obj_Entry *);
    406 void _rtld_setup_pltgot(const Obj_Entry *);
    407 
    408 /* search.c */
    409 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
    410 
    411 /* symbol.c */
    412 unsigned long _rtld_elf_hash(const char *);
    413 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
    414     const Obj_Entry *, u_int, const Ver_Entry *);
    415 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
    416     const Obj_Entry **, u_int);
    417 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
    418     const Obj_Entry **, bool);
    419 
    420 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
    421     const Objlist *, const Obj_Entry **, u_int, const Ver_Entry *, DoneList *);
    422 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
    423     const Obj_Entry *, const Obj_Entry **, u_int, const Ver_Entry *);
    424 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
    425     const Needed_Entry *, const Obj_Entry **, u_int, const Ver_Entry *,
    426     DoneList *, DoneList *);
    427 #ifdef COMBRELOC
    428 void _rtld_combreloc_reset(const Obj_Entry *);
    429 #endif
    430 
    431 /* symver.c */
    432 void _rtld_object_add_name(Obj_Entry *, const char *);
    433 int _rtld_object_match_name(const Obj_Entry *, const char *);
    434 int _rtld_verify_object_versions(Obj_Entry *);
    435 
    436 static __inline const Ver_Entry *
    437 _rtld_fetch_ventry(const Obj_Entry *obj, unsigned long symnum)
    438 {
    439 	Elf_Half vernum;
    440 
    441 	if (obj->vertab) {
    442 		vernum = VER_NDX(obj->versyms[symnum].vs_vers);
    443 		if (vernum >= obj->vertabnum) {
    444 			_rtld_error("%s: symbol %s has wrong verneed value %d",
    445 			    obj->path, &obj->strtab[symnum], vernum);
    446 		} else if (obj->vertab[vernum].hash) {
    447 			return &obj->vertab[vernum];
    448 		}
    449 	}
    450 	return NULL;
    451 }
    452 
    453 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
    454 /* tls.c */
    455 void *_rtld_tls_get_addr(void *, size_t, size_t);
    456 void _rtld_tls_initial_allocation(void);
    457 void *_rtld_tls_module_allocate(size_t index);
    458 int _rtld_tls_offset_allocate(Obj_Entry *);
    459 void _rtld_tls_offset_free(Obj_Entry *);
    460 
    461 extern size_t _rtld_tls_dtv_generation;
    462 extern size_t _rtld_tls_max_index;
    463 
    464 __dso_public extern void *__tls_get_addr(void *);
    465 #ifdef __i386__
    466 __dso_public extern void *___tls_get_addr(void *)
    467     __attribute__((__regparm__(1)));
    468 #endif
    469 #endif
    470 
    471 /* map_object.c */
    472 struct stat;
    473 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
    474 void _rtld_obj_free(Obj_Entry *);
    475 Obj_Entry *_rtld_obj_new(void);
    476 
    477 /* function descriptors */
    478 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    479 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
    480     const Elf_Sym *, Elf_Addr);
    481 const void *_rtld_function_descriptor_function(const void *);
    482 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    483 
    484 #endif /* _RTLD_SOURCE */
    485 
    486 #endif /* RTLD_H */
    487