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