Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / third_party / android_crazy_linker / src / include / crazy_linker.h
blob347cd7ca642fdc106d256f91b9c41e51033892b7
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CRAZY_LINKER_H
6 #define CRAZY_LINKER_H
8 // This is the crazy linker, a custom dynamic linker that can be used
9 // by NDK applications to load shared libraries (not executables) with
10 // a twist.
12 // Compared to the dynamic linker, the crazy one has the following
13 // features:
15 // - It can use an arbitrary search path.
17 // - It can load a library at a memory fixed address, or from a fixed
18 // file offset (both must be page-aligned).
20 // - It can share the RELRO section between two libraries
21 // loaded at the same address in two distinct processes.
23 #include <dlfcn.h>
24 #include <stdbool.h>
25 #include <stddef.h>
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
31 // Function attribute to indicate that it needs to be exported by
32 // the library.
33 #define _CRAZY_PUBLIC __attribute__((__visibility__("default")))
35 // Maximum path length of a file in a zip file.
36 const size_t kMaxFilePathLengthInZip = 256;
38 // Status values returned by crazy linker functions to indicate
39 // success or failure. They were chosen to match boolean values,
40 // this allows one to test for failures with:
42 // if (!crazy_linker_function(....)) {
43 // ... an error occured.
44 // }
46 // If the function called used a crazy_context_t, it is possible to
47 // retrieve the error details with crazy_context_get_error().
48 typedef enum {
49 CRAZY_STATUS_FAILURE = 0,
50 CRAZY_STATUS_SUCCESS = 1
51 } crazy_status_t;
53 // Opaque handle to a context object that will hold parameters
54 // for the crazy linker's operations. For example, this is where
55 // you would set the explicit load address, and other user-provided
56 // values before calling functions like crazy_library_open().
58 // The context holds a list of library search paths, initialized to
59 // the content of the LD_LIBRARY_PATH variable on creation.
61 // The context also holds a string buffer to hold error messages that
62 // can be queried with crazy_context_get_error().
63 typedef struct crazy_context_t crazy_context_t;
65 // Create a new context object.
66 // Note that this calls crazy_context_reset_search_paths().
67 crazy_context_t* crazy_context_create(void) _CRAZY_PUBLIC;
69 // Return current error string, or NULL if there was no error.
70 const char* crazy_context_get_error(crazy_context_t* context) _CRAZY_PUBLIC;
72 // Clear error in a given context.
73 void crazy_context_clear_error(crazy_context_t* context) _CRAZY_PUBLIC;
75 // Set the explicit load address in a context object. Value 0 means
76 // the address is randomized.
77 void crazy_context_set_load_address(crazy_context_t* context,
78 size_t load_address) _CRAZY_PUBLIC;
80 // Return the current load address in a context.
81 size_t crazy_context_get_load_address(crazy_context_t* context) _CRAZY_PUBLIC;
83 // Set the explicit file offset in a context object. The value should
84 // always page-aligned, or the load will fail.
85 // Note you can not use the same file with multiple offsets. See crbug/388223.
86 void crazy_context_set_file_offset(crazy_context_t* context,
87 size_t file_offset) _CRAZY_PUBLIC;
89 // Return the current file offset in a context object.
90 size_t crazy_context_get_file_offset(crazy_context_t* context);
92 // Add one or more paths to the list of library search paths held
93 // by a given context. |path| is a string using a column (:) as a
94 // list separator. As with the PATH variable, an empty list item
95 // is equivalent to '.', the current directory.
96 // This can fail if too many paths are added to the context.
98 // NOTE: Calling this function appends new paths to the search list,
99 // but all paths added with this function will be searched before
100 // the ones listed in LD_LIBRARY_PATH.
101 crazy_status_t crazy_context_add_search_path(
102 crazy_context_t* context,
103 const char* file_path) _CRAZY_PUBLIC;
105 // Find the ELF binary that contains |address|, and add its directory
106 // path to the context's list of search directories. This is useful to
107 // load libraries in the same directory than the current program itself.
108 crazy_status_t crazy_context_add_search_path_for_address(
109 crazy_context_t* context,
110 void* address) _CRAZY_PUBLIC;
112 // Reset the search paths to the value of the LD_LIBRARY_PATH
113 // environment variable. This essentially removes any paths
114 // that were added with crazy_context_add_search_path() or
115 // crazy_context_add_search_path_for_address().
116 void crazy_context_reset_search_paths(crazy_context_t* context) _CRAZY_PUBLIC;
118 // Record the value of |java_vm| inside of |context|. If this is not NULL,
119 // which is the default, then after loading any library, the crazy linker
120 // will look for a "JNI_OnLoad" symbol within it, and, if it exists, will call
121 // it, passing the value of |java_vm| to it. If the function returns with
122 // a jni version number that is smaller than |minimum_jni_version|, then
123 // the library load will fail with an error.
125 // The |java_vm| field is also saved in the crazy_library_t object, and
126 // used at unload time to call JNI_OnUnload() if it exists.
127 void crazy_context_set_java_vm(crazy_context_t* context,
128 void* java_vm,
129 int minimum_jni_version);
131 // Retrieves the last values set with crazy_context_set_java_vm().
132 // A value of NUMM in |*java_vm| means the feature is disabled.
133 void crazy_context_get_java_vm(crazy_context_t* context,
134 void** java_vm,
135 int* minimum_jni_version);
137 // Destroy a given context object.
138 void crazy_context_destroy(crazy_context_t* context) _CRAZY_PUBLIC;
140 // Some operations performed by the crazy linker might conflict with the
141 // system linker if they are used concurrently in different threads
142 // (e.g. modifying the list of shared libraries seen by GDB). To work
143 // around this, the crazy linker provides a way to delay these conflicting
144 // operations for a later time.
146 // This works by wrapping each of these operations in a small data structure
147 // (crazy_callback_t), which can later be passed to crazy_callback_run()
148 // to execute it.
150 // The user must provide a function to record these callbacks during
151 // library loading, by calling crazy_linker_set_callback_poster().
153 // Once all libraries are loaded, the callbacks can be later called either
154 // in a different thread, or when it is safe to assume the system linker
155 // cannot be running in parallel.
157 // Callback handler.
158 typedef void (*crazy_callback_handler_t)(void* opaque);
160 // A small structure used to model a callback provided by the crazy linker.
161 // Use crazy_callback_run() to run the callback.
162 typedef struct {
163 crazy_callback_handler_t handler;
164 void* opaque;
165 } crazy_callback_t;
167 // Function to call to enable a callback into the crazy linker when delayed
168 // operations are enabled (see crazy_context_set_callback_poster). A call
169 // to crazy_callback_poster_t returns true if the callback was successfully
170 // set up and will occur later, false if callback could not be set up (and
171 // so will never occur).
172 typedef bool (*crazy_callback_poster_t)(
173 crazy_callback_t* callback, void* poster_opaque);
175 // Enable delayed operation, by passing the address of a
176 // |crazy_callback_poster_t| function, that will be called during library
177 // loading to let the user record callbacks for delayed operations.
178 // Callers must copy the |crazy_callback_t| passed to |poster|.
180 // Note: If client code calls this function to supply a callback poster,
181 // it must guarantee to invoke any callback requested through the poster.
182 // The call will be (typically) on another thread, but may instead be
183 // immediate from the poster. However, the callback must be invoked,
184 // otherwise if it is a blocking callback the crazy linker will deadlock
185 // waiting for it.
187 // |poster_opaque| is an opaque value for client code use, passed back
188 // on each call to |poster|.
189 // |poster| can be NULL to disable the feature.
190 void crazy_context_set_callback_poster(crazy_context_t* context,
191 crazy_callback_poster_t poster,
192 void* poster_opaque);
194 // Return the address of the function that the crazy linker can use to
195 // request callbacks, and the |poster_opaque| passed back on each call
196 // to |poster|. |poster| is NULL if the feature is disabled.
197 void crazy_context_get_callback_poster(crazy_context_t* context,
198 crazy_callback_poster_t* poster,
199 void** poster_opaque);
201 // Run a given |callback| in the current thread. Must only be called once
202 // per callback.
203 void crazy_callback_run(crazy_callback_t* callback);
205 // Pass the platform's SDK build version to the crazy linker. The value is
206 // from android.os.Build.VERSION.SDK_INT.
207 void crazy_set_sdk_build_version(int sdk_build_version);
209 // Opaque handle to a library as seen/loaded by the crazy linker.
210 typedef struct crazy_library_t crazy_library_t;
212 // Try to open or load a library with the crazy linker.
213 // |lib_name| if the library name or path. If it contains a directory
214 // separator (/), this is treated as a explicit file path, otherwise
215 // it is treated as a base name, and the context's search path list
216 // will be used to locate the corresponding file.
217 // |context| is a linker context handle. Can be NULL for defaults.
218 // On success, return CRAZY_STATUS_SUCCESS and sets |*library|.
219 // Libraries are reference-counted, trying to open the same library
220 // twice will return the same library handle.
222 // NOTE: The load address and file offset from the context only apply
223 // to the library being loaded (when not already in the process). If the
224 // operations needs to load any dependency libraries, these will use
225 // offset and address values of 0 to do so.
227 // NOTE: It is possible to open NDK system libraries (e.g. "liblog.so")
228 // with this function, but they will be loaded with the system dlopen().
229 crazy_status_t crazy_library_open(crazy_library_t** library,
230 const char* lib_name,
231 crazy_context_t* context) _CRAZY_PUBLIC;
233 // Try to open or load a library with the crazy linker. The
234 // library is in a zip file with the name |zipfile_name|. Within the zip
235 // file the library must be uncompressed and page aligned. |zipfile_name|
236 // should be an absolute path name and |lib_name| should be a relative
237 // pathname. The library in the zip file is expected to have the name
238 // lib/<abi_tag>/crazy.<lib_name> where abi_tag is the abi directory matching
239 // the ABI for which the crazy linker was compiled. Note this does not support
240 // opening multiple libraries in the same zipfile, see crbug/388223.
241 crazy_status_t crazy_library_open_in_zip_file(crazy_library_t** library,
242 const char* zipfile_name,
243 const char* lib_name,
244 crazy_context_t* context)
245 _CRAZY_PUBLIC;
247 // A structure used to hold information about a given library.
248 // |load_address| is the library's actual (page-aligned) load address.
249 // |load_size| is the library's actual (page-aligned) size.
250 // |relro_start| is the address of the library's RELRO section in memory.
251 // |relso_size| is the size of the library's RELRO section (or 0 if none).
252 // |relro_fd| is the ashmem file descriptor for the shared section, if one
253 // was created with crazy_library_enable_relro_sharing(), -1 otherwise.
254 typedef struct {
255 size_t load_address;
256 size_t load_size;
257 size_t relro_start;
258 size_t relro_size;
259 } crazy_library_info_t;
261 // Retrieve information about a given library.
262 // |library| is a library handle.
263 // |context| will get an error message on failure.
264 // On success, return true and sets |*info|.
265 // Note that this function will fail for system libraries.
266 crazy_status_t crazy_library_get_info(crazy_library_t* library,
267 crazy_context_t* context,
268 crazy_library_info_t* info);
270 // Create an ashmem region containing a copy of the RELRO section for a given
271 // |library|. This can be used with crazy_library_use_shared_relro().
272 // |load_address| can be specified as non-0 to ensure that the content of the
273 // ashmem region corresponds to a RELRO relocated for a new load address.
274 // on success, return CRAZY_STATUS_SUCCESS and sets |*relro_start| to the
275 // start of the RELRO section in memory, |*relro_size| to its size in bytes
276 // and |*relro_fd| to a file descriptor to a read-only ashmem region containing
277 // the data. On failure, return false and set error message in |context|.
278 // NOTE: On success, the caller becomes the owner of |*relro_fd| and is shall
279 // close it appropriately.
280 crazy_status_t crazy_library_create_shared_relro(crazy_library_t* library,
281 crazy_context_t* context,
282 size_t load_address,
283 size_t* relro_start,
284 size_t* relro_size,
285 int* relro_fd) _CRAZY_PUBLIC;
287 // Use the shared RELRO section of the same library loaded in a different
288 // address space. On success, return CRAZY_STATUS_SUCCESS and owns |relro_fd|.
289 // On failure, return CRAZY_STATUS_FAILURE and sets error message in |context|.
290 // |library| is the library handle.
291 // |relro_start| is the address of the RELRO section in memory.
292 // |relro_size| is the size of the RELRO section.
293 // |relro_fd| is the file descriptor for the shared RELRO ashmem region.
294 // |context| will receive an error in case of failure.
295 // NOTE: This will fail if this is a system library, or if the RELRO
296 // parameters do not match the library's actual load address.
297 // NOTE: The caller is responsible for closing the file descriptor after this
298 // call.
299 crazy_status_t crazy_library_use_shared_relro(crazy_library_t* library,
300 crazy_context_t* context,
301 size_t relro_start,
302 size_t relro_size,
303 int relro_fd) _CRAZY_PUBLIC;
305 // Look for a library named |library_name| in the set of currently
306 // loaded libraries, and return a handle for it in |*library| on success.
307 // Note that this increments the reference count on the library, thus
308 // the caller shall call crazy_library_close() when it's done with it.
309 crazy_status_t crazy_library_find_by_name(const char* library_name,
310 crazy_library_t** library);
312 // Find the library that contains a given |address| in memory.
313 // On success, return CRAZY_STATUS_SUCCESS and sets |*library|.
314 crazy_status_t crazy_linker_find_library_from_address(
315 void* address,
316 crazy_library_t** library) _CRAZY_PUBLIC;
318 // Lookup a symbol's address by its |symbol_name| in a given library.
319 // This only looks at the symbols in |library|.
320 // On success, returns CRAZY_STATUS_SUCCESS and sets |*symbol_address|,
321 // which could be NULL for some symbols.
322 crazy_status_t crazy_library_find_symbol(crazy_library_t* library,
323 const char* symbol_name,
324 void** symbol_address) _CRAZY_PUBLIC;
326 // Lookup a symbol's address in all libraries known by the crazy linker.
327 // |symbol_name| is the symbol name. On success, returns CRAZY_STATUS_SUCCESS
328 // and sets |*symbol_address|.
329 // NOTE: This will _not_ look into system libraries that were not opened
330 // with the crazy linker.
331 crazy_status_t crazy_linker_find_symbol(const char* symbol_name,
332 void** symbol_address) _CRAZY_PUBLIC;
334 // Find the in-process library that contains a given memory address.
335 // Note that this works even if the memory is inside a system library that
336 // was not previously opened with crazy_library_open().
337 // |address| is the memory address.
338 // On success, returns CRAZY_STATUS_SUCCESS and sets |*library|.
339 // The caller muyst call crazy_library_close() once it's done with the
340 // library.
341 crazy_status_t crazy_library_find_from_address(
342 void* address,
343 crazy_library_t** library) _CRAZY_PUBLIC;
345 // Close a library. This decrements its reference count. If it reaches
346 // zero, the library be unloaded from the process.
347 void crazy_library_close(crazy_library_t* library) _CRAZY_PUBLIC;
349 // Close a library, with associated context to support delayed operations.
350 void crazy_library_close_with_context(crazy_library_t* library,
351 crazy_context_t* context) _CRAZY_PUBLIC;
353 #ifdef __cplusplus
354 } /* extern "C" */
355 #endif
357 #endif /* CRAZY_LINKER_H */