xref: /haiku/src/system/runtime_loader/export.cpp (revision c237c4ce593ee823d9867fd997e51e4c447f5623)
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, NULL, &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* caller,
34 	void **_handle)
35 {
36 	return load_library(name, flags, false, caller, _handle);
37 }
38 
39 
40 static status_t
41 export_unload_library(void* handle)
42 {
43 	return unload_library(handle, -1, false);
44 }
45 
46 
47 status_t
48 reinit_after_fork()
49 {
50 	status_t returnstatus = B_OK;
51 	if (status_t status = elf_reinit_after_fork())
52 		returnstatus = status;
53 	if (status_t status = heap_reinit_after_fork())
54 		returnstatus = status;
55 	return returnstatus;
56 }
57 
58 
59 struct rld_export gRuntimeLoader = {
60 	// dynamic loading support API
61 	export_load_add_on,
62 	export_unload_add_on,
63 	export_load_library,
64 	export_unload_library,
65 	get_symbol,
66 	get_library_symbol,
67 	get_nth_symbol,
68 	get_nearest_symbol_at_address,
69 	test_executable,
70 	get_executable_architecture,
71 	get_next_image_dependency,
72 	get_tls_address,
73 	destroy_thread_tls,
74 
75 	reinit_after_fork,
76 	NULL, // call_atexit_hooks_for_range
77 	terminate_program,
78 
79 	// the following values will be set later
80 	NULL,	// program_args
81 	NULL,	// commpage_address
82 	0		// ABI version
83 };
84 
85 rld_export* __gRuntimeLoader = &gRuntimeLoader;
86 
87 
88 void
89 rldexport_init(void)
90 {
91 	gRuntimeLoader.program_args = gProgramArgs;
92 	gRuntimeLoader.commpage_address = __gCommPageAddress;
93 }
94 
95 
96 /*!	Is called for all images, and sets the minimum ABI version found to the
97 	gRuntimeLoader.abi_version field and the minimum API version found to the
98 	gRuntimeLoader.api_version field.
99 */
100 void
101 set_abi_api_version(int abi_version, int api_version)
102 {
103 	if (gRuntimeLoader.abi_version == 0
104 		|| gRuntimeLoader.abi_version > abi_version) {
105 		gRuntimeLoader.abi_version = abi_version;
106 	}
107 	if (gRuntimeLoader.api_version == 0
108 		|| gRuntimeLoader.api_version > api_version) {
109 		gRuntimeLoader.api_version = api_version;
110 	}
111 }
112