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