resolve.h revision 1.65
1/* $OpenBSD: resolve.h,v 1.65 2011/11/28 20:59:03 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 <link.h> 34#include <dlfcn.h> 35#include <signal.h> 36 37struct load_list { 38 struct load_list *next; 39 void *start; 40 size_t size; 41 int prot; 42 Elf_Addr moff; 43 long foff; 44}; 45 46/* 47 * Structure describing a loaded object. 48 * The head of this struct must be compatible 49 * with struct link_map in sys/link.h 50 */ 51typedef struct elf_object elf_object_t; 52struct elf_object { 53 Elf_Addr obj_base; /* object's address '0' base */ 54 char *load_name; /* Pointer to object name */ 55 Elf_Dyn *load_dyn; /* Pointer to object dynamic data */ 56 struct elf_object *next; 57 struct elf_object *prev; 58/* End struct link_map compatible */ 59 Elf_Addr load_base; /* Base address of loadable segments */ 60 61 struct load_list *load_list; 62 63 u_int32_t load_size; 64 Elf_Addr got_addr; 65 Elf_Addr got_start; 66 size_t got_size; 67 Elf_Addr plt_start; 68 size_t plt_size; 69 70 union { 71 u_long info[DT_NUM + DT_PROCNUM]; 72 struct { 73 Elf_Addr null; /* Not used */ 74 Elf_Addr needed; /* Not used */ 75 Elf_Addr pltrelsz; 76 Elf_Addr *pltgot; 77 Elf_Addr *hash; 78 const char *strtab; 79 const Elf_Sym *symtab; 80 Elf_RelA *rela; 81 Elf_Addr relasz; 82 Elf_Addr relaent; 83 Elf_Addr strsz; 84 Elf_Addr syment; 85 void (*init)(void); 86 void (*fini)(void); 87 const char *soname; 88 const char *rpath; 89 Elf_Addr symbolic; 90 Elf_Rel *rel; 91 Elf_Addr relsz; 92 Elf_Addr relent; 93 Elf_Addr pltrel; 94 Elf_Addr debug; 95 Elf_Addr textrel; 96 Elf_Addr jmprel; 97 } u; 98 } Dyn; 99#define dyn Dyn.u 100 101 int status; 102#define STAT_RELOC_DONE 0x01 103#define STAT_GOT_DONE 0x02 104#define STAT_INIT_DONE 0x04 105#define STAT_FINI_DONE 0x08 106#define STAT_FINI_READY 0x10 107#define STAT_UNLOADED 0x20 108#define STAT_NODELETE 0x40 109#define STAT_VISITED 0x80 110 111 Elf_Phdr *phdrp; 112 int phdrc; 113 114 int obj_type; 115#define OBJTYPE_LDR 1 116#define OBJTYPE_EXE 2 117#define OBJTYPE_LIB 3 118#define OBJTYPE_DLO 4 119 int obj_flags; /* c.f. <sys/exec_elf.h> DF_1_* */ 120 121 Elf_Word *buckets; 122 u_int32_t nbuckets; 123 Elf_Word *chains; 124 u_int32_t nchains; 125 Elf_Dyn *dynamic; 126 127 TAILQ_HEAD(,dep_node) child_list; /* direct dep libs of object */ 128 TAILQ_HEAD(,dep_node) grpsym_list; /* ordered complete dep list */ 129 TAILQ_HEAD(,dep_node) grpref_list; /* refs to other load groups */ 130 131 int refcount; /* dep libs only */ 132 int opencount; /* # dlopen() & exe */ 133 int grprefcount; /* load group refs */ 134#define OBJECT_REF_CNT(object) \ 135 ((object->refcount + object->opencount + object->grprefcount)) 136#define OBJECT_DLREF_CNT(object) \ 137 ((object->opencount + object->grprefcount)) 138 139 /* object that caused this module to be loaded, used in symbol lookup */ 140 elf_object_t *load_object; 141 struct sod sod; 142 143 void *prebind_data; 144 145 /* for object confirmation */ 146 dev_t dev; 147 ino_t inode; 148 149 /* last symbol lookup on this object, to avoid mutiple searches */ 150 int lastlookup_head; 151 int lastlookup; 152}; 153 154struct dep_node { 155 TAILQ_ENTRY(dep_node) next_sib; 156 elf_object_t *data; 157}; 158 159void _dl_add_object(elf_object_t *object); 160elf_object_t *_dl_finalize_object(const char *objname, Elf_Dyn *dynp, 161 Elf_Phdr *phdrp, int phdrc, const int objtype, const long lbase, 162 const long obase); 163void _dl_remove_object(elf_object_t *object); 164void _dl_cleanup_objects(void); 165 166elf_object_t *_dl_load_shlib(const char *, elf_object_t *, int, int); 167elf_object_t *_dl_tryload_shlib(const char *libname, int type, int flags); 168 169int _dl_md_reloc(elf_object_t *object, int rel, int relsz); 170int _dl_md_reloc_got(elf_object_t *object, int lazy); 171 172Elf_Addr _dl_find_symbol(const char *name, const Elf_Sym **this, 173 int flags, const Elf_Sym *ref_sym, elf_object_t *object, 174 const elf_object_t **pobj); 175Elf_Addr _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx, 176 const Elf_Sym **ref, int flags, const Elf_Sym *ref_sym, 177 const elf_object_t **pobj); 178/* 179 * defines for _dl_find_symbol() flag field, three bits of meaning 180 * myself - clear: search all objects, set: search only this object 181 * warnnotfound - clear: no warning, set: warn if not found 182 * inplt - clear: possible plt ref set: real matching function. 183 * 184 * inplt - due to how ELF handles function addresses in shared libraries 185 * &func may actually refer to the plt entry in the main program 186 * rather than the actual function address in the .so file. 187 * This rather bizarre behavior is documented in the SVR4 ABI. 188 * when getting the function address to relocate a PLT entry 189 * the 'real' function address is necessary, not the possible PLT address. 190 */ 191/* myself */ 192#define SYM_SEARCH_ALL 0x00 193#define SYM_SEARCH_SELF 0x01 194#define SYM_SEARCH_OTHER 0x02 195#define SYM_SEARCH_NEXT 0x04 196#define SYM_SEARCH_OBJ 0x08 197/* warnnotfound */ 198#define SYM_NOWARNNOTFOUND 0x00 199#define SYM_WARNNOTFOUND 0x10 200/* inplt */ 201#define SYM_NOTPLT 0x00 202#define SYM_PLT 0x20 203 204#define SYM_DLSYM 0x40 205 206int _dl_load_dep_libs(elf_object_t *object, int flags, int booting); 207int _dl_rtld(elf_object_t *object); 208void _dl_call_init(elf_object_t *object); 209void _dl_link_child(elf_object_t *dep, elf_object_t *p); 210void _dl_link_grpsym(elf_object_t *object, int checklist); 211void _dl_cache_grpsym_list(elf_object_t *object); 212void _dl_cache_grpsym_list_setup(elf_object_t *object); 213void _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object); 214void _dl_link_dlopen(elf_object_t *dep); 215void _dl_unlink_dlopen(elf_object_t *dep); 216void _dl_notify_unload_shlib(elf_object_t *object); 217void _dl_unload_shlib(elf_object_t *object); 218void _dl_unload_dlopen(void); 219 220void _dl_run_all_dtors(void); 221 222/* Please don't rename; gdb(1) knows about this. */ 223Elf_Addr _dl_bind(elf_object_t *object, int index); 224 225int _dl_match_file(struct sod *sodp, const char *name, int namelen); 226char *_dl_find_shlib(struct sod *sodp, const char *searchpath, int nohints); 227void _dl_load_list_free(struct load_list *load_list); 228 229void _dl_thread_kern_go(void); 230void _dl_thread_kern_stop(void); 231 232void _dl_thread_bind_lock(int, sigset_t *); 233 234extern elf_object_t *_dl_objects; 235extern elf_object_t *_dl_last_object; 236 237extern elf_object_t *_dl_loading_object; 238 239extern const char *_dl_progname; 240extern struct r_debug *_dl_debug_map; 241 242extern int _dl_pagesz; 243extern int _dl_errno; 244 245extern char *_dl_libpath; 246extern char *_dl_preload; 247extern char *_dl_bindnow; 248extern char *_dl_traceld; 249extern char *_dl_tracefmt1; 250extern char *_dl_tracefmt2; 251extern char *_dl_traceprog; 252extern char *_dl_debug; 253 254#define DL_DEB(P) do { if (_dl_debug) _dl_printf P ; } while (0) 255 256#define DL_NOT_FOUND 1 257#define DL_CANT_OPEN 2 258#define DL_NOT_ELF 3 259#define DL_CANT_OPEN_REF 4 260#define DL_CANT_MMAP 5 261#define DL_NO_SYMBOL 6 262#define DL_INVALID_HANDLE 7 263#define DL_INVALID_CTL 8 264#define DL_NO_OBJECT 9 265#define DL_CANT_FIND_OBJ 10 266#define DL_CANT_LOAD_OBJ 11 267#define DL_INVALID_MODE 12 268 269#define ELF_ROUND(x,malign) (((x) + (malign)-1) & ~((malign)-1)) 270#define ELF_TRUNC(x,malign) ((x) & ~((malign)-1)) 271 272/* symbol lookup cache */ 273typedef struct sym_cache { 274 const elf_object_t *obj; 275 const Elf_Sym *sym; 276 int flags; 277} sym_cache; 278 279extern sym_cache *_dl_symcache; 280extern int _dl_symcachestat_hits; 281extern int _dl_symcachestat_lookups; 282TAILQ_HEAD(dlochld, dep_node); 283extern struct dlochld _dlopened_child_list; 284 285/* variables used to avoid duplicate node checking */ 286int _dl_searchnum; 287uint32_t _dl_skipnum; 288void _dl_newsymsearch(void); 289 290#endif /* _RESOLVE_H_ */ 291