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.
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.
11 #ifndef _RUNTIME_LOADER_H
12 #define _RUNTIME_LOADER_H
18 #include <elf_private.h>
21 // #pragma mark - runtime loader libroot interface
24 struct user_space_program_args
;
25 struct SymbolLookupInfo
;
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
,
40 status_t (*get_nearest_symbol_at_address
)(void* address
,
41 image_id
* _imageID
, char** _imagePath
, char** _imageName
,
42 char** _symbolName
, int32
* _type
, void** _location
, bool* _exactMatch
);
43 status_t (*test_executable
)(const char *path
, char *interpreter
);
44 status_t (*get_executable_architecture
)(const char *path
,
45 const char** _architecture
);
46 status_t (*get_next_image_dependency
)(image_id id
, uint32
*cookie
,
48 void* (*get_tls_address
)(unsigned dso
, addr_t offset
);
49 void (*destroy_thread_tls
)();
51 status_t (*reinit_after_fork
)();
53 void (*call_atexit_hooks_for_range
)(addr_t start
, addr_t size
);
55 void (*call_termination_hooks
)();
57 const struct user_space_program_args
*program_args
;
58 const void* commpage_address
;
62 extern struct rld_export
*__gRuntimeLoader
;
65 // #pragma mark - runtime loader debugger interface
68 struct RuntimeLoaderSymbolPatcher
;
70 typedef struct elf_region_t
{
82 typedef struct elf_version_info
{
83 uint32 hash
; // version name hash
84 const char *name
; // version name
85 const char *file_name
; // dependency file name (needed versions only)
88 typedef struct image_t
{
89 // image identification
90 char path
[B_PATH_NAME_LENGTH
];
91 char name
[B_FILE_NAME_LENGTH
];
106 addr_t dynamic_ptr
; // pointer to the dynamic section
108 // pointer to symbol participation data structures
120 addr_t
*preinit_array
;
121 int preinit_array_len
;
128 struct image_t
**needed
;
130 // versioning related structures
131 uint32 num_version_definitions
;
132 elf_verdef
*version_definitions
;
133 uint32 num_needed_versions
;
134 elf_verneed
*needed_versions
;
135 elf_versym
*symbol_versions
;
136 elf_version_info
*versions
;
140 elf_sym
* (*find_undefined_symbol
)(struct image_t
* rootImage
,
141 struct image_t
* image
,
142 const SymbolLookupInfo
& lookupInfo
,
143 struct image_t
** foundInImage
);
145 elf_sym
* (*find_undefined_symbol
)(struct image_t
* rootImage
,
146 struct image_t
* image
,
147 const struct SymbolLookupInfo
* lookupInfo
,
148 struct image_t
** foundInImage
);
151 // Singly-linked list of symbol patchers for symbols defined respectively
152 // referenced by this image.
153 struct RuntimeLoaderSymbolPatcher
*defined_symbol_patchers
;
154 struct RuntimeLoaderSymbolPatcher
*undefined_symbol_patchers
;
156 // describes the text and data regions
158 elf_region_t regions
[1];
161 typedef struct image_queue_t
{
167 #define IMAGE_FLAG_RTLD_MASK 0x03
168 // RTLD_{LAZY,NOW} | RTLD_{LOCAL,GLOBAL}
170 #define STRING(image, offset) ((char*)(&(image)->strtab[(offset)]))
171 #define SYMNAME(image, sym) STRING(image, (sym)->st_name)
172 #define SYMBOL(image, num) (&(image)->syms[num])
173 #define HASHTABSIZE(image) ((image)->symhash[0])
174 #define HASHBUCKETS(image) ((unsigned int*)&(image)->symhash[2])
175 #define HASHCHAINS(image) ((unsigned int*)&(image)->symhash[2+HASHTABSIZE(image)])
178 // The name of the area the runtime loader creates for debugging purposes.
179 #define RUNTIME_LOADER_DEBUG_AREA_NAME "_rld_debug_"
181 // The contents of the runtime loader debug area.
182 typedef struct runtime_loader_debug_area
{
183 image_queue_t
*loaded_images
;
184 } runtime_loader_debug_area
;
187 // #pragma mark - runtime loader add-on interface
190 // symbol patcher callback
191 typedef void runtime_loader_symbol_patcher(void* cookie
,
192 struct image_t
* rootImage
, struct image_t
* image
, const char* name
,
193 struct image_t
** foundInImage
, void** symbol
, int32
* type
);
195 // interface provided to add-ons
196 struct runtime_loader_add_on_export
{
197 status_t (*register_defined_symbol_patcher
)(struct image_t
* image
,
198 runtime_loader_symbol_patcher
* patcher
, void* cookie
);
199 void (*unregister_defined_symbol_patcher
)(struct image_t
* image
,
200 runtime_loader_symbol_patcher
* patcher
, void* cookie
);
201 status_t (*register_undefined_symbol_patcher
)(struct image_t
* image
,
202 runtime_loader_symbol_patcher
* patcher
, void* cookie
);
203 void (*unregister_undefined_symbol_patcher
)(struct image_t
* image
,
204 runtime_loader_symbol_patcher
* patcher
, void* cookie
);
208 #define RUNTIME_LOADER_ADD_ON_VERSION 1
210 typedef struct runtime_loader_add_on
{
214 // called after the add-on image has been loaded
215 void (*init
)(struct rld_export
* standardInterface
,
216 struct runtime_loader_add_on_export
* addOnInterface
);
218 // called whenever the respective image event occurs
219 void (*image_loaded
)(struct image_t
* image
);
220 void (*image_relocated
)(struct image_t
* image
);
221 void (*image_initialized
)(struct image_t
* image
);
222 void (*image_uninitializing
)(struct image_t
* image
);
223 void (*image_unloading
)(struct image_t
* image
);
224 } runtime_loader_add_on
;
226 // This is the variable a preloaded shared object has to export to get picked up
227 // by the runtime loader as an add-on.
228 extern runtime_loader_add_on __gRuntimeLoaderAddOn
;
230 #endif // _RUNTIME_LOADER_H