xref: /haiku/headers/private/runtime_loader/runtime_loader.h (revision adb0d19d561947362090081e81d90dde59142026)
1 /*
2  * Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2002, Manuel J. Petit. All rights reserved.
6  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
7  * Distributed under the terms of the NewOS License.
8  */
9 
10 #ifndef _RUNTIME_LOADER_H
11 #define _RUNTIME_LOADER_H
12 
13 #include <image.h>
14 #include <OS.h>
15 
16 #include <elf32.h>
17 
18 
19 // #pragma mark - runtime loader libroot interface
20 
21 
22 struct user_space_program_args;
23 
24 struct rld_export {
25 	// runtime loader API export
26 	image_id (*load_add_on)(char const *path, uint32 flags);
27 	status_t (*unload_add_on)(image_id imageID);
28 	image_id (*load_library)(char const *path, uint32 flags, void **_handle);
29 	status_t (*unload_library)(void* handle);
30 	status_t (*get_image_symbol)(image_id imageID, char const *symbolName,
31 		int32 symbolType, bool recursive, image_id *_inImage, void **_location);
32 	status_t (*get_library_symbol)(void* handle, void* caller,
33 		const char* symbolName, void **_location);
34 	status_t (*get_nth_image_symbol)(image_id imageID, int32 num, char *symbolName,
35 		int32 *nameLength, int32 *symbolType, void **_location);
36 	status_t (*test_executable)(const char *path, char *interpreter);
37 	status_t (*get_next_image_dependency)(image_id id, uint32 *cookie,
38 		const char **_name);
39 
40 	status_t (*reinit_after_fork)();
41 
42 	void (*call_atexit_hooks_for_range)(addr_t start, addr_t size);
43 
44 	void (*call_termination_hooks)();
45 
46 	const struct user_space_program_args *program_args;
47 };
48 
49 extern struct rld_export *__gRuntimeLoader;
50 
51 
52 // #pragma mark - runtime loader debugger interface
53 
54 
55 struct RuntimeLoaderSymbolPatcher;
56 
57 typedef struct elf_region_t {
58 	area_id		id;
59 	addr_t		start;
60 	addr_t		size;
61 	addr_t		vmstart;
62 	addr_t		vmsize;
63 	addr_t		fdstart;
64 	addr_t		fdsize;
65 	long		delta;
66 	uint32		flags;
67 } elf_region_t;
68 
69 typedef struct image_t {
70 	// image identification
71 	char				path[B_PATH_NAME_LENGTH];
72 	char				name[B_OS_NAME_LENGTH];
73 	image_id			id;
74 	image_type			type;
75 
76 	struct image_t		*next;
77 	struct image_t		*prev;
78 	int32				ref_count;
79 	uint32				flags;
80 
81 	struct {
82 		int				major;
83 		int				middle;
84 		int				minor;
85 		bool			haiku;
86 	} gcc_version;
87 
88 	addr_t 				entry_point;
89 	addr_t				init_routine;
90 	addr_t				term_routine;
91 	addr_t 				dynamic_ptr; 	// pointer to the dynamic section
92 
93 	// pointer to symbol participation data structures
94 	uint32				*symhash;
95 	struct Elf32_Sym	*syms;
96 	char				*strtab;
97 	struct Elf32_Rel	*rel;
98 	int					rel_len;
99 	struct Elf32_Rela	*rela;
100 	int					rela_len;
101 	struct Elf32_Rel	*pltrel;
102 	int					pltrel_len;
103 
104 	uint32				num_needed;
105 	struct image_t		**needed;
106 
107 	struct Elf32_Sym*	(*find_undefined_symbol)(struct image_t* rootImage,
108 							struct image_t* image, const char* name,
109 							struct image_t** foundInImage);
110 
111 	// Singly-linked list of symbol patchers for symbols defined respectively
112 	// referenced by this image.
113 	struct RuntimeLoaderSymbolPatcher	*defined_symbol_patchers;
114 	struct RuntimeLoaderSymbolPatcher	*undefined_symbol_patchers;
115 
116 	// describes the text and data regions
117 	uint32				num_regions;
118 	elf_region_t		regions[1];
119 } image_t;
120 
121 typedef struct image_queue_t {
122 	image_t *head;
123 	image_t *tail;
124 } image_queue_t;
125 
126 // image_t::flags
127 #define	IMAGE_FLAG_RTLD_MASK			0x03
128 			// RTLD_{LAZY,NOW} | RTLD_{LOCAL,GLOBAL}
129 
130 #define STRING(image, offset) ((char *)(&(image)->strtab[(offset)]))
131 #define SYMNAME(image, sym) STRING(image, (sym)->st_name)
132 #define SYMBOL(image, num) ((struct Elf32_Sym *)&(image)->syms[num])
133 #define HASHTABSIZE(image) ((image)->symhash[0])
134 #define HASHBUCKETS(image) ((unsigned int *)&(image)->symhash[2])
135 #define HASHCHAINS(image) ((unsigned int *)&(image)->symhash[2+HASHTABSIZE(image)])
136 
137 
138 // The name of the area the runtime loader creates for debugging purposes.
139 #define RUNTIME_LOADER_DEBUG_AREA_NAME	"_rld_debug_"
140 
141 // The contents of the runtime loader debug area.
142 typedef struct runtime_loader_debug_area {
143 	image_queue_t	*loaded_images;
144 } runtime_loader_debug_area;
145 
146 
147 // #pragma mark - runtime loader add-on interface
148 
149 
150 // symbol patcher callback
151 typedef void runtime_loader_symbol_patcher(void* cookie,
152 	struct image_t* rootImage, struct image_t* image, const char* name,
153 	struct image_t** foundInImage, void** symbol, int32* type);
154 
155 // interface provided to add-ons
156 struct runtime_loader_add_on_export {
157 	status_t	(*register_defined_symbol_patcher)(struct image_t* image,
158 					runtime_loader_symbol_patcher* patcher, void* cookie);
159 	void		(*unregister_defined_symbol_patcher)(struct image_t* image,
160 					runtime_loader_symbol_patcher* patcher, void* cookie);
161 	status_t	(*register_undefined_symbol_patcher)(struct image_t* image,
162 					runtime_loader_symbol_patcher* patcher, void* cookie);
163 	void		(*unregister_undefined_symbol_patcher)(struct image_t* image,
164 					runtime_loader_symbol_patcher* patcher, void* cookie);
165 };
166 
167 
168 #define RUNTIME_LOADER_ADD_ON_VERSION	1
169 
170 typedef struct runtime_loader_add_on {
171 	uint32	version;
172 	uint32	flags;
173 
174 	// called after the add-on image has been loaded
175 	void	(*init)(struct rld_export* standardInterface,
176 				struct runtime_loader_add_on_export* addOnInterface);
177 
178 	// called whenever the respective image event occurs
179 	void	(*image_loaded)(struct image_t* image);
180 	void	(*image_relocated)(struct image_t* image);
181 	void	(*image_initialized)(struct image_t* image);
182 	void	(*image_uninitializing)(struct image_t* image);
183 	void	(*image_unloading)(struct image_t* image);
184 } runtime_loader_add_on;
185 
186 // This is the variable a preloaded shared object has to export to get picked up
187 // by the runtime loader as an add-on.
188 extern runtime_loader_add_on __gRuntimeLoaderAddOn;
189 
190 #endif	// _RUNTIME_LOADER_H
191