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