xref: /haiku/src/system/runtime_loader/export.cpp (revision 6a44d4c52765e4fd5ccf6a4a7c69486c9531431d)
1 /*
2  * Copyright 2003-2011, 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  * Distributed under the terms of the NewOS License.
7  */
8 
9 
10 #include "runtime_loader_private.h"
11 #include "elf_tls.h"
12 
13 
14 // exported via the rld_export structure in user space program arguments
15 
16 
17 static image_id
18 export_load_add_on(char const *name, uint32 flags)
19 {
20 	void* handle;
21 	return load_library(name, flags, true, &handle);
22 }
23 
24 
25 static status_t
26 export_unload_add_on(image_id id)
27 {
28 	return unload_library(NULL, id, true);
29 }
30 
31 
32 static image_id
33 export_load_library(char const *name, uint32 flags, void **_handle)
34 {
35 	return load_library(name, flags, false, _handle);
36 }
37 
38 
39 static status_t
40 export_unload_library(void* handle)
41 {
42 	return unload_library(handle, -1, false);
43 }
44 
45 
46 status_t
47 reinit_after_fork()
48 {
49 	status_t returnstatus = B_OK;
50 	if (status_t status = elf_reinit_after_fork())
51 		returnstatus = status;
52 	if (status_t status = heap_reinit_after_fork())
53 		returnstatus = status;
54 	return returnstatus;
55 }
56 
57 
58 struct rld_export gRuntimeLoader = {
59 	// dynamic loading support API
60 	export_load_add_on,
61 	export_unload_add_on,
62 	export_load_library,
63 	export_unload_library,
64 	get_symbol,
65 	get_library_symbol,
66 	get_nth_symbol,
67 	get_nearest_symbol_at_address,
68 	test_executable,
69 	get_executable_architecture,
70 	get_next_image_dependency,
71 	get_tls_address,
72 	destroy_thread_tls,
73 
74 	reinit_after_fork,
75 	NULL, // call_atexit_hooks_for_range
76 	terminate_program,
77 
78 	// the following values will be set later
79 	NULL,	// program_args
80 	NULL,	// commpage_address
81 	0		// ABI version
82 };
83 
84 rld_export* __gRuntimeLoader = &gRuntimeLoader;
85 
86 
87 void
88 rldexport_init(void)
89 {
90 	gRuntimeLoader.program_args = gProgramArgs;
91 	gRuntimeLoader.commpage_address = __gCommPageAddress;
92 }
93 
94 
95 /*!	Is called for all images, and sets the minimum ABI version found to the
96 	gRuntimeLoader.abi_version field and the minimum API version found to the
97 	gRuntimeLoader.api_version field.
98 */
99 void
100 set_abi_api_version(int abi_version, int api_version)
101 {
102 	if (gRuntimeLoader.abi_version == 0
103 		|| gRuntimeLoader.abi_version > abi_version) {
104 		gRuntimeLoader.abi_version = abi_version;
105 	}
106 	if (gRuntimeLoader.api_version == 0
107 		|| gRuntimeLoader.api_version > api_version) {
108 		gRuntimeLoader.api_version = api_version;
109 	}
110 }
111