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