xref: /haiku/headers/private/runtime_loader/runtime_loader.h (revision ffc1a5219d270b64c79d2240abcf51b7984e5dc7)
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 
15 #include <image.h>
16 #include <OS.h>
17 
18 #include <elf_private.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* caller,
32 		void **_handle);
33 	status_t (*unload_library)(void* handle);
34 	status_t (*get_image_symbol)(image_id imageID, char const *symbolName,
35 		int32 symbolType, bool recursive, image_id *_inImage, void **_location);
36 	status_t (*get_library_symbol)(void* handle, void* caller,
37 		const char* symbolName, void **_location);
38 	status_t (*get_nth_image_symbol)(image_id imageID, int32 num,
39 		char *symbolName, int32 *nameLength, int32 *symbolType,
40 		void **_location);
41 	status_t (*get_nearest_symbol_at_address)(void* address,
42 		image_id* _imageID,	char** _imagePath, char** _imageName,
43 		char** _symbolName, int32* _type, void** _location, bool* _exactMatch);
44 	status_t (*test_executable)(const char *path, char *interpreter);
45 	status_t (*get_executable_architecture)(const char *path,
46 		const char** _architecture);
47 	status_t (*get_next_image_dependency)(image_id id, uint32 *cookie,
48 		const char **_name);
49 	void* (*get_tls_address)(unsigned dso, addr_t offset);
50 	void (*destroy_thread_tls)();
51 
52 	status_t (*reinit_after_fork)();
53 
54 	void (*call_atexit_hooks_for_range)(addr_t start, addr_t size);
55 
56 	void (*call_termination_hooks)();
57 
58 	const struct user_space_program_args *program_args;
59 	const void* commpage_address;
60 	int abi_version;
61 	int api_version;
62 };
63 
64 extern struct rld_export *__gRuntimeLoader;
65 
66 
67 // #pragma mark - runtime loader debugger interface
68 
69 
70 struct RuntimeLoaderSymbolPatcher;
71 
72 typedef struct elf_region_t {
73 	area_id		id;
74 	addr_t		start;
75 	addr_t		size;
76 	addr_t		vmstart;
77 	addr_t		vmsize;
78 	addr_t		fdstart;
79 	addr_t		fdsize;
80 	long		delta;
81 	uint32		flags;
82 } elf_region_t;
83 
84 typedef struct elf_version_info {
85 	uint32		hash;			// version name hash
86 	const char	*name;			// version name
87 	const char	*file_name;		// dependency file name (needed versions only)
88 } elf_version_info;
89 
90 typedef struct image_t {
91 	// image identification
92 	char				path[B_PATH_NAME_LENGTH];
93 	char				name[B_FILE_NAME_LENGTH];
94 	image_id			id;
95 	image_type			type;
96 
97 	struct image_t		*next;
98 	struct image_t		*prev;
99 	int32				ref_count;
100 	uint32				flags;
101 
102 	uint32				api_version;
103 	uint32				abi;
104 
105 	addr_t 				entry_point;
106 	addr_t				init_routine;
107 	addr_t				term_routine;
108 	addr_t 				dynamic_ptr; 	// pointer to the dynamic section
109 
110 	// needed images
111 	uint32				num_needed;
112 	struct image_t		**needed;
113 
114 	// symbol participation data structures
115 	uint32				*symhash;
116 	elf_sym				*syms;
117 	char				*strtab;
118 	struct {
119 		uint32			mask_words_count_mask;
120 		uint32			shift2;
121 		uint32			bucket_count;
122 		elf_addr		*bloom;
123 		uint32			*buckets;
124 		uint32			*chain0;
125 	} gnuhash;
126 
127 	// relocation information
128 	elf_rel				*rel;
129 	int					rel_len;
130 	elf_rela			*rela;
131 	int					rela_len;
132 	elf_rel				*pltrel;
133 	int					pltrel_len;
134 
135 	// init/term functions
136 	addr_t				*init_array;
137 	int					init_array_len;
138 	addr_t				*preinit_array;
139 	int					preinit_array_len;
140 	addr_t				*term_array;
141 	int					term_array_len;
142 
143 	// versioning related structures
144 	uint32				num_version_definitions;
145 	elf_verdef			*version_definitions;
146 	uint32				num_needed_versions;
147 	elf_verneed			*needed_versions;
148 	elf_versym			*symbol_versions;
149 	elf_version_info	*versions;
150 	uint32				num_versions;
151 
152 	// thread-local storage
153 	unsigned			dso_tls_id;
154 
155 #ifdef __cplusplus
156 	elf_sym*			(*find_undefined_symbol)(struct image_t* rootImage,
157 							struct image_t* image,
158 							const SymbolLookupInfo& lookupInfo,
159 							struct image_t** foundInImage);
160 #else
161 	elf_sym*			(*find_undefined_symbol)(struct image_t* rootImage,
162 							struct image_t* image,
163 							const struct SymbolLookupInfo* lookupInfo,
164 							struct image_t** foundInImage);
165 #endif
166 
167 	// Singly-linked list of symbol patchers for symbols defined respectively
168 	// referenced by this image.
169 	struct RuntimeLoaderSymbolPatcher	*defined_symbol_patchers;
170 	struct RuntimeLoaderSymbolPatcher	*undefined_symbol_patchers;
171 
172 	// describes the text and data regions
173 	uint32				num_regions;
174 	elf_region_t		regions[1];
175 } image_t;
176 
177 typedef struct image_queue_t {
178 	image_t *head;
179 	image_t *tail;
180 } image_queue_t;
181 
182 // image_t::flags
183 #define	IMAGE_FLAG_RTLD_MASK			0x03
184 			// RTLD_{LAZY,NOW} | RTLD_{LOCAL,GLOBAL}
185 
186 #define STRING(image, offset) ((char*)(&(image)->strtab[(offset)]))
187 #define SYMNAME(image, sym) STRING(image, (sym)->st_name)
188 #define SYMBOL(image, num) (&(image)->syms[num])
189 #define HASHTABSIZE(image) ((image)->symhash[0])
190 #define HASHBUCKETS(image) ((unsigned int*)&(image)->symhash[2])
191 #define HASHCHAINS(image) ((unsigned int*)&(image)->symhash[2+HASHTABSIZE(image)])
192 
193 
194 // The name of the area the runtime loader creates for debugging purposes.
195 #define RUNTIME_LOADER_DEBUG_AREA_NAME	"_rld_debug_"
196 
197 // The contents of the runtime loader debug area.
198 typedef struct runtime_loader_debug_area {
199 	image_queue_t	*loaded_images;
200 } runtime_loader_debug_area;
201 
202 
203 // #pragma mark - runtime loader add-on interface
204 
205 
206 // symbol patcher callback
207 typedef void runtime_loader_symbol_patcher(void* cookie,
208 	struct image_t* rootImage, struct image_t* image, const char* name,
209 	struct image_t** foundInImage, void** symbol, int32* type);
210 
211 // interface provided to add-ons
212 struct runtime_loader_add_on_export {
213 	status_t	(*register_defined_symbol_patcher)(struct image_t* image,
214 					runtime_loader_symbol_patcher* patcher, void* cookie);
215 	void		(*unregister_defined_symbol_patcher)(struct image_t* image,
216 					runtime_loader_symbol_patcher* patcher, void* cookie);
217 	status_t	(*register_undefined_symbol_patcher)(struct image_t* image,
218 					runtime_loader_symbol_patcher* patcher, void* cookie);
219 	void		(*unregister_undefined_symbol_patcher)(struct image_t* image,
220 					runtime_loader_symbol_patcher* patcher, void* cookie);
221 };
222 
223 
224 #define RUNTIME_LOADER_ADD_ON_VERSION	1
225 
226 typedef struct runtime_loader_add_on {
227 	uint32	version;
228 	uint32	flags;
229 
230 	// called after the add-on image has been loaded
231 	void	(*init)(struct rld_export* standardInterface,
232 				struct runtime_loader_add_on_export* addOnInterface);
233 
234 	// called whenever the respective image event occurs
235 	void	(*image_loaded)(struct image_t* image);
236 	void	(*image_relocated)(struct image_t* image);
237 	void	(*image_initialized)(struct image_t* image);
238 	void	(*image_uninitializing)(struct image_t* image);
239 	void	(*image_unloading)(struct image_t* image);
240 } runtime_loader_add_on;
241 
242 // This is the variable a preloaded shared object has to export to get picked up
243 // by the runtime loader as an add-on.
244 extern runtime_loader_add_on __gRuntimeLoaderAddOn;
245 
246 #endif	// _RUNTIME_LOADER_H
247