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