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