resolve.h revision 1.92
1/* $OpenBSD: resolve.h,v 1.92 2019/05/11 21:02:35 guenther Exp $ */ 2 3/* 4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29#ifndef _RESOLVE_H_ 30#define _RESOLVE_H_ 31 32#include <sys/queue.h> 33#include <dlfcn.h> 34#include <link.h> 35#include <tib.h> 36 37#define __relro __attribute__((section(".data.rel.ro"))) 38 39#ifndef __boot 40# if DO_CLEAN_BOOT 41# define __boot __attribute__((section(".boot.text"))) 42# define __boot_data __attribute__((section(".boot.data"))) 43# else 44# define __boot 45# define __boot_data 46# endif 47#endif 48 49/* Number of low tags that are used saved internally (0 .. DT_NUM-1) */ 50#define DT_NUM (DT_PREINIT_ARRAYSZ + 1) 51 52struct load_list { 53 struct load_list *next; 54 void *start; 55 size_t size; 56 int prot; 57 Elf_Addr moff; 58 long foff; 59}; 60 61typedef void initarrayfunc(int, const char **, char **, dl_cb_cb *); 62typedef void initfunc(void); /* also fini and fini_array functions */ 63 64/* Alpha uses 8byte entries for DT_HASH */ 65#ifdef __alpha__ 66typedef uint64_t Elf_Hash_Word; 67#else 68typedef uint32_t Elf_Hash_Word; 69#endif 70 71/* 72 * Structure describing a loaded object. 73 * The head of this struct must be compatible 74 * with struct link_map in sys/link.h 75 */ 76typedef struct elf_object elf_object_t; 77struct elf_object { 78 Elf_Addr obj_base; /* object's address '0' base */ 79 char *load_name; /* Pointer to object name */ 80 Elf_Dyn *load_dyn; /* Pointer to object dynamic data */ 81 struct elf_object *next; 82 struct elf_object *prev; 83/* End struct link_map compatible */ 84 Elf_Addr load_base; /* Base address of loadable segments */ 85 86 struct load_list *load_list; 87 88 u_int32_t load_size; 89 90 union { 91 u_long info[DT_NUM + DT_PROCNUM]; 92 struct { 93 Elf_Addr null; /* Not used */ 94 Elf_Addr needed; /* Not used */ 95 Elf_Addr pltrelsz; 96 Elf_Addr *pltgot; 97 Elf_Addr *hash; 98 const char *strtab; 99 const Elf_Sym *symtab; 100 Elf_RelA *rela; 101 Elf_Addr relasz; 102 Elf_Addr relaent; 103 Elf_Addr strsz; 104 Elf_Addr syment; 105 initfunc *init; 106 initfunc *fini; 107 const char *soname; 108 const char *rpath; 109 Elf_Addr symbolic; 110 Elf_Rel *rel; 111 Elf_Addr relsz; 112 Elf_Addr relent; 113 Elf_Addr pltrel; 114 Elf_Addr debug; 115 Elf_Addr textrel; 116 Elf_Addr jmprel; 117 Elf_Addr bind_now; 118 initarrayfunc **init_array; 119 initfunc **fini_array; 120 Elf_Addr init_arraysz; 121 Elf_Addr fini_arraysz; 122 const char *runpath; 123 Elf_Addr flags; 124 Elf_Addr encoding; 125 initarrayfunc **preinit_array; 126 Elf_Addr preinit_arraysz; 127 } u; 128 } Dyn; 129#define dyn Dyn.u 130 131 Elf_Addr relacount; /* DT_RELACOUNT */ 132 Elf_Addr relcount; /* DT_RELCOUNT */ 133 134 int status; 135#define STAT_RELOC_DONE 0x001 136#define STAT_GOT_DONE 0x002 137#define STAT_INIT_DONE 0x004 138#define STAT_FINI_DONE 0x008 139#define STAT_FINI_READY 0x010 140#define STAT_UNLOADED 0x020 141#define STAT_NODELETE 0x040 142#define STAT_GNU_HASH 0x080 143#define STAT_VISIT_INITFIRST 0x100 144#define STAT_VISIT_INIT 0x200 145 146 Elf_Phdr *phdrp; 147 int phdrc; 148 149 int obj_type; 150#define OBJTYPE_LDR 1 151#define OBJTYPE_EXE 2 152#define OBJTYPE_LIB 3 153#define OBJTYPE_DLO 4 154 int obj_flags; /* c.f. <sys/exec_elf.h> DF_1_* */ 155 156 /* shared by ELF and GNU hash */ 157 u_int32_t nbuckets; 158 u_int32_t nchains; /* really, number of symbols */ 159 union { 160 struct { 161 /* specific to ELF hash */ 162 const Elf_Hash_Word *buckets; 163 const Elf_Hash_Word *chains; 164 } u_elf; 165 struct { 166 /* specific to GNU hash */ 167 const Elf_Word *buckets; 168 const Elf_Word *chains; 169 const Elf_Addr *bloom; 170 Elf_Word mask_bm; 171 Elf_Word shift2; 172 Elf_Word symndx; 173 } u_gnu; 174 } hash_u; 175#define buckets_elf hash_u.u_elf.buckets 176#define chains_elf hash_u.u_elf.chains 177#define buckets_gnu hash_u.u_gnu.buckets 178#define chains_gnu hash_u.u_gnu.chains 179#define bloom_gnu hash_u.u_gnu.bloom 180#define mask_bm_gnu hash_u.u_gnu.mask_bm 181#define shift2_gnu hash_u.u_gnu.shift2 182#define symndx_gnu hash_u.u_gnu.symndx 183 184 TAILQ_HEAD(,dep_node) child_list; /* direct dep libs of object */ 185 TAILQ_HEAD(,dep_node) grpsym_list; /* ordered complete dep list */ 186 TAILQ_HEAD(,dep_node) grpref_list; /* refs to other load groups */ 187 188 int refcount; /* dep libs only */ 189 int opencount; /* # dlopen() & exe */ 190 int grprefcount; /* load group refs */ 191#define OBJECT_REF_CNT(object) \ 192 ((object->refcount + object->opencount + object->grprefcount)) 193#define OBJECT_DLREF_CNT(object) \ 194 ((object->opencount + object->grprefcount)) 195 196 /* object that caused this module to be loaded, used in symbol lookup */ 197 elf_object_t *load_object; 198 struct sod sod; 199 200 /* for object confirmation */ 201 dev_t dev; 202 ino_t inode; 203 204 /* thread local storage info */ 205 Elf_Addr tls_fsize; 206 Elf_Addr tls_msize; 207 Elf_Addr tls_align; 208 const void *tls_static_data; 209 int tls_offset; 210 211 /* relro bits */ 212 Elf_Addr relro_addr; 213 Elf_Addr relro_size; 214 215 /* generation number of last grpsym insert on this object */ 216 unsigned int grpsym_gen; 217 218 char **rpath; 219 char **runpath; 220 221 /* nonzero if trace enabled for this object */ 222 int traced; 223}; 224 225struct dep_node { 226 TAILQ_ENTRY(dep_node) next_sib; 227 elf_object_t *data; 228}; 229 230 231/* Please don't rename or make hidden; gdb(1) knows about these. */ 232Elf_Addr _dl_bind(elf_object_t *object, int index); 233void _dl_debug_state(void); 234 235/* exported to the application */ 236extern char *__progname; 237 238__BEGIN_HIDDEN_DECLS 239void _dl_add_object(elf_object_t *object); 240elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp, 241 Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase, 242 const long obase); 243void _dl_remove_object(elf_object_t *object); 244void _dl_cleanup_objects(void); 245 246elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int); 247elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags); 248 249int _dl_md_reloc(elf_object_t *object, int rel, int relsz); 250int _dl_md_reloc_got(elf_object_t *object, int lazy); 251 252Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this, 253 int flags, const Elf_Sym *ref_sym, elf_object_t *object, 254 const elf_object_t **pobj); 255Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx, 256 const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym, 257 const elf_object_t **pobj); 258/* 259 * defines for _dl_find_symbol() flag field, three bits of meaning 260 * myself - clear: search all objects, set: search only this object 261 * warnnotfound - clear: no warning, set: warn if not found 262 * inplt - clear: possible plt ref set: real matching function. 263 * 264 * inplt - due to how ELF handles function addresses in shared libraries 265 * &func may actually refer to the plt entry in the main program 266 * rather than the actual function address in the .so file. 267 * This rather bizarre behavior is documented in the SVR4 ABI. 268 * when getting the function address to relocate a PLT entry 269 * the 'real' function address is necessary, not the possible PLT address. 270 */ 271/* myself */ 272#define SYM_SEARCH_ALL 0x00 273#define SYM_SEARCH_SELF 0x01 274#define SYM_SEARCH_OTHER 0x02 275#define SYM_SEARCH_NEXT 0x04 276/* warnnotfound */ 277#define SYM_NOWARNNOTFOUND 0x00 278#define SYM_WARNNOTFOUND 0x10 279/* inplt */ 280#define SYM_NOTPLT 0x00 281#define SYM_PLT 0x20 282 283#define SYM_DLSYM 0x40 284 285int _dl_load_dep_libs(elf_object_t *object, int flags, int booting); 286int _dl_rtld(elf_object_t *object); 287void _dl_call_init(elf_object_t *object); 288void _dl_link_child(elf_object_t *dep, elf_object_t *p); 289void _dl_link_grpsym(elf_object_t *object, int checklist); 290void _dl_cache_grpsym_list(elf_object_t *object); 291void _dl_cache_grpsym_list_setup(elf_object_t *object); 292void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object); 293void _dl_link_dlopen(elf_object_t *dep); 294void _dl_unlink_dlopen(elf_object_t *dep); 295void _dl_notify_unload_shlib(elf_object_t *object); 296void _dl_unload_shlib(elf_object_t *object); 297void _dl_unload_dlopen(void); 298 299void _dl_run_all_dtors(void); 300 301int _dl_match_file(struct sod *sodp, const char *name, int namelen); 302char *_dl_find_shlib(struct sod *sodp, char **searchpath, int nohints); 303void _dl_load_list_free(struct load_list *load_list); 304 305typedef void lock_cb(int); 306void _dl_thread_kern_go(lock_cb *); 307lock_cb *_dl_thread_kern_stop(void); 308 309char *_dl_getenv(const char *, char **) __boot; 310void _dl_unsetenv(const char *, char **) __boot; 311 312void _dl_trace_setup(char **) __boot; 313void _dl_trace_object_setup(elf_object_t *); 314int _dl_trace_plt(const elf_object_t *, const char *); 315 316/* tib.c */ 317void _dl_allocate_tls_offsets(void) __boot; 318void _dl_allocate_first_tib(void) __boot; 319void _dl_set_tls(elf_object_t *_object, Elf_Phdr *_ptls, Elf_Addr _libaddr, 320 const char *_libname); 321extern int _dl_tib_static_done; 322 323extern elf_object_t *_dl_objects; 324extern elf_object_t *_dl_last_object; 325 326extern elf_object_t *_dl_loading_object; 327 328extern struct r_debug *_dl_debug_map; 329 330extern int _dl_pagesz; 331extern int _dl_errno; 332 333extern char **_dl_libpath; 334 335extern int _dl_bindnow; 336extern int _dl_traceld; 337extern int _dl_debug; 338 339extern char *_dl_preload; 340extern char *_dl_tracefmt1; 341extern char *_dl_tracefmt2; 342extern char *_dl_traceprog; 343 344extern int _dl_trust; 345 346#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0) 347 348#define DL_NOT_FOUND 1 349#define DL_CANT_OPEN 2 350#define DL_NOT_ELF 3 351#define DL_CANT_OPEN_REF 4 352#define DL_CANT_MMAP 5 353#define DL_NO_SYMBOL 6 354#define DL_INVALID_HANDLE 7 355#define DL_INVALID_CTL 8 356#define DL_NO_OBJECT 9 357#define DL_CANT_FIND_OBJ 10 358#define DL_CANT_LOAD_OBJ 11 359#define DL_INVALID_MODE 12 360 361#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1)) 362#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1)) 363 364/* symbol lookup cache */ 365typedef struct sym_cache { 366 const elf_object_t *obj; 367 const Elf_Sym *sym; 368 int flags; 369} sym_cache; 370 371extern sym_cache *_dl_symcache; 372extern int _dl_symcachestat_hits; 373extern int _dl_symcachestat_lookups; 374TAILQ_HEAD(dlochld, dep_node); 375extern struct dlochld _dlopened_child_list; 376__END_HIDDEN_DECLS 377 378#endif /* _RESOLVE_H_ */ 379