1 /* 2 * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2003-2011, Axel Dörfler, axeld@pinc-software.de. 4 * Distributed under the terms of the MIT License. 5 * 6 * Copyright 2002, Manuel J. Petit. All rights reserved. 7 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 8 * Distributed under the terms of the NewOS License. 9 */ 10 11 #ifndef _RUNTIME_LOADER_H 12 #define _RUNTIME_LOADER_H 13 14 #include <image.h> 15 #include <OS.h> 16 17 #include <elf32.h> 18 #include <elf64.h> 19 20 21 // #pragma mark - runtime loader libroot interface 22 23 24 struct user_space_program_args; 25 struct SymbolLookupInfo; 26 27 struct rld_export { 28 // runtime loader API export 29 image_id (*load_add_on)(char const *path, uint32 flags); 30 status_t (*unload_add_on)(image_id imageID); 31 image_id (*load_library)(char const *path, uint32 flags, void **_handle); 32 status_t (*unload_library)(void* handle); 33 status_t (*get_image_symbol)(image_id imageID, char const *symbolName, 34 int32 symbolType, bool recursive, image_id *_inImage, void **_location); 35 status_t (*get_library_symbol)(void* handle, void* caller, 36 const char* symbolName, void **_location); 37 status_t (*get_nth_image_symbol)(image_id imageID, int32 num, 38 char *symbolName, int32 *nameLength, int32 *symbolType, 39 void **_location); 40 status_t (*get_nearest_symbol_at_address)(void* address, 41 image_id* _imageID, char** _imagePath, char** _symbolName, 42 int32* _type, void** _location); 43 status_t (*test_executable)(const char *path, char *interpreter); 44 status_t (*get_next_image_dependency)(image_id id, uint32 *cookie, 45 const char **_name); 46 47 status_t (*reinit_after_fork)(); 48 49 void (*call_atexit_hooks_for_range)(addr_t start, addr_t size); 50 51 void (*call_termination_hooks)(); 52 53 const struct user_space_program_args *program_args; 54 const void* commpage_address; 55 int abi_version; 56 }; 57 58 extern struct rld_export *__gRuntimeLoader; 59 60 61 // #pragma mark - runtime loader debugger interface 62 63 64 struct RuntimeLoaderSymbolPatcher; 65 66 typedef struct elf_region_t { 67 area_id id; 68 addr_t start; 69 addr_t size; 70 addr_t vmstart; 71 addr_t vmsize; 72 addr_t fdstart; 73 addr_t fdsize; 74 long delta; 75 uint32 flags; 76 } elf_region_t; 77 78 typedef struct elf_version_info { 79 uint32 hash; // version name hash 80 const char *name; // version name 81 const char *file_name; // dependency file name (needed versions only) 82 } elf_version_info; 83 84 typedef struct image_t { 85 // image identification 86 char path[B_PATH_NAME_LENGTH]; 87 char name[B_OS_NAME_LENGTH]; 88 image_id id; 89 image_type type; 90 91 struct image_t *next; 92 struct image_t *prev; 93 int32 ref_count; 94 uint32 flags; 95 96 uint32 api_version; 97 uint32 abi; 98 99 addr_t entry_point; 100 addr_t init_routine; 101 addr_t term_routine; 102 addr_t dynamic_ptr; // pointer to the dynamic section 103 104 // pointer to symbol participation data structures 105 uint32 *symhash; 106 elf_sym *syms; 107 char *strtab; 108 elf_rel *rel; 109 int rel_len; 110 elf_rela *rela; 111 int rela_len; 112 elf_rel *pltrel; 113 int pltrel_len; 114 115 uint32 num_needed; 116 struct image_t **needed; 117 118 // versioning related structures 119 uint32 num_version_definitions; 120 elf_verdef *version_definitions; 121 uint32 num_needed_versions; 122 elf_verneed *needed_versions; 123 elf_versym *symbol_versions; 124 elf_version_info *versions; 125 uint32 num_versions; 126 127 #ifdef __cplusplus 128 elf_sym* (*find_undefined_symbol)(struct image_t* rootImage, 129 struct image_t* image, 130 const SymbolLookupInfo& lookupInfo, 131 struct image_t** foundInImage); 132 #else 133 elf_sym* (*find_undefined_symbol)(struct image_t* rootImage, 134 struct image_t* image, 135 const struct SymbolLookupInfo* lookupInfo, 136 struct image_t** foundInImage); 137 #endif 138 139 // Singly-linked list of symbol patchers for symbols defined respectively 140 // referenced by this image. 141 struct RuntimeLoaderSymbolPatcher *defined_symbol_patchers; 142 struct RuntimeLoaderSymbolPatcher *undefined_symbol_patchers; 143 144 // describes the text and data regions 145 uint32 num_regions; 146 elf_region_t regions[1]; 147 } image_t; 148 149 typedef struct image_queue_t { 150 image_t *head; 151 image_t *tail; 152 } image_queue_t; 153 154 // image_t::flags 155 #define IMAGE_FLAG_RTLD_MASK 0x03 156 // RTLD_{LAZY,NOW} | RTLD_{LOCAL,GLOBAL} 157 158 #define STRING(image, offset) ((char*)(&(image)->strtab[(offset)])) 159 #define SYMNAME(image, sym) STRING(image, (sym)->st_name) 160 #define SYMBOL(image, num) (&(image)->syms[num]) 161 #define HASHTABSIZE(image) ((image)->symhash[0]) 162 #define HASHBUCKETS(image) ((unsigned int*)&(image)->symhash[2]) 163 #define HASHCHAINS(image) ((unsigned int*)&(image)->symhash[2+HASHTABSIZE(image)]) 164 165 166 // The name of the area the runtime loader creates for debugging purposes. 167 #define RUNTIME_LOADER_DEBUG_AREA_NAME "_rld_debug_" 168 169 // The contents of the runtime loader debug area. 170 typedef struct runtime_loader_debug_area { 171 image_queue_t *loaded_images; 172 } runtime_loader_debug_area; 173 174 175 // #pragma mark - runtime loader add-on interface 176 177 178 // symbol patcher callback 179 typedef void runtime_loader_symbol_patcher(void* cookie, 180 struct image_t* rootImage, struct image_t* image, const char* name, 181 struct image_t** foundInImage, void** symbol, int32* type); 182 183 // interface provided to add-ons 184 struct runtime_loader_add_on_export { 185 status_t (*register_defined_symbol_patcher)(struct image_t* image, 186 runtime_loader_symbol_patcher* patcher, void* cookie); 187 void (*unregister_defined_symbol_patcher)(struct image_t* image, 188 runtime_loader_symbol_patcher* patcher, void* cookie); 189 status_t (*register_undefined_symbol_patcher)(struct image_t* image, 190 runtime_loader_symbol_patcher* patcher, void* cookie); 191 void (*unregister_undefined_symbol_patcher)(struct image_t* image, 192 runtime_loader_symbol_patcher* patcher, void* cookie); 193 }; 194 195 196 #define RUNTIME_LOADER_ADD_ON_VERSION 1 197 198 typedef struct runtime_loader_add_on { 199 uint32 version; 200 uint32 flags; 201 202 // called after the add-on image has been loaded 203 void (*init)(struct rld_export* standardInterface, 204 struct runtime_loader_add_on_export* addOnInterface); 205 206 // called whenever the respective image event occurs 207 void (*image_loaded)(struct image_t* image); 208 void (*image_relocated)(struct image_t* image); 209 void (*image_initialized)(struct image_t* image); 210 void (*image_uninitializing)(struct image_t* image); 211 void (*image_unloading)(struct image_t* image); 212 } runtime_loader_add_on; 213 214 // This is the variable a preloaded shared object has to export to get picked up 215 // by the runtime loader as an add-on. 216 extern runtime_loader_add_on __gRuntimeLoaderAddOn; 217 218 #endif // _RUNTIME_LOADER_H 219