Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __ANDROID_DLEXT_H__ |
| 18 | #define __ANDROID_DLEXT_H__ |
| 19 | |
| 20 | #include <stddef.h> |
David 'Digit' Turner | aad1a39 | 2014-11-18 12:21:55 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 22 | #include <sys/cdefs.h> |
David 'Digit' Turner | aad1a39 | 2014-11-18 12:21:55 +0100 | [diff] [blame] | 23 | #include <sys/types.h> /* for off64_t */ |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 24 | |
| 25 | __BEGIN_DECLS |
| 26 | |
| 27 | /* bitfield definitions for android_dlextinfo.flags */ |
| 28 | enum { |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 29 | /* When set, the reserved_addr and reserved_size fields must point to an |
| 30 | * already-reserved region of address space which will be used to load the |
| 31 | * library if it fits. If the reserved region is not large enough, the load |
| 32 | * will fail. |
| 33 | */ |
| 34 | ANDROID_DLEXT_RESERVED_ADDRESS = 0x1, |
| 35 | |
| 36 | /* As DLEXT_RESERVED_ADDRESS, but if the reserved region is not large enough, |
| 37 | * the linker will choose an available address instead. |
| 38 | */ |
| 39 | ANDROID_DLEXT_RESERVED_ADDRESS_HINT = 0x2, |
| 40 | |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 41 | /* When set, write the GNU RELRO section of the mapped library to relro_fd |
| 42 | * after relocation has been performed, to allow it to be reused by another |
| 43 | * process loading the same library at the same address. This implies |
| 44 | * ANDROID_DLEXT_USE_RELRO. |
| 45 | */ |
| 46 | ANDROID_DLEXT_WRITE_RELRO = 0x4, |
| 47 | |
| 48 | /* When set, compare the GNU RELRO section of the mapped library to relro_fd |
| 49 | * after relocation has been performed, and replace any relocated pages that |
| 50 | * are identical with a version mapped from the file. |
| 51 | */ |
| 52 | ANDROID_DLEXT_USE_RELRO = 0x8, |
| 53 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 54 | /* Instruct dlopen to use library_fd instead of opening file by name. |
| 55 | * The filename parameter is still used to identify the library. |
| 56 | */ |
| 57 | ANDROID_DLEXT_USE_LIBRARY_FD = 0x10, |
| 58 | |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 59 | /* If opening a library using library_fd read it starting at library_fd_offset. |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 60 | * This flag is only valid when ANDROID_DLEXT_USE_LIBRARY_FD is set. |
| 61 | */ |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 62 | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET = 0x20, |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 63 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 64 | /* When set, do not check if the library has already been loaded by file stat(2)s. |
| 65 | * |
| 66 | * This flag allows forced loading of the library in the case when for some |
| 67 | * reason multiple ELF files share the same filename (because the already-loaded |
| 68 | * library has been removed and overwritten, for example). |
| 69 | * |
| 70 | * Note that if the library has the same dt_soname as an old one and some other |
| 71 | * library has the soname in DT_NEEDED list, the first one will be used to resolve any |
| 72 | * dependencies. |
| 73 | */ |
| 74 | ANDROID_DLEXT_FORCE_LOAD = 0x40, |
| 75 | |
Dmitriy Ivanov | 8a11628 | 2015-06-05 22:16:23 -0700 | [diff] [blame] | 76 | /* When set, if the minimum p_vaddr of the ELF file's PT_LOAD segments is non-zero, |
| 77 | * the dynamic linker will load it at that address. |
| 78 | * |
| 79 | * This flag is for ART internal use only. |
| 80 | */ |
| 81 | ANDROID_DLEXT_FORCE_FIXED_VADDR = 0x80, |
| 82 | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 83 | /* Instructs dlopen to load the library at the address specified by reserved_addr. |
| 84 | * |
| 85 | * The difference between ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS and ANDROID_DLEXT_RESERVED_ADDRESS |
| 86 | * is that for ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS the linker reserves memory at reserved_addr |
| 87 | * whereas for ANDROID_DLEXT_RESERVED_ADDRESS the linker relies on the caller to reserve the memory. |
| 88 | * |
| 89 | * This flag can be used with ANDROID_DLEXT_FORCE_FIXED_VADDR; when ANDROID_DLEXT_FORCE_FIXED_VADDR |
| 90 | * is set and load_bias is not 0 (load_bias is min(p_vaddr) of PT_LOAD segments) this flag is ignored. |
| 91 | * This is implemented this way because the linker has to pick one address over the other and this |
| 92 | * way is more convenient for art. Note that ANDROID_DLEXT_FORCE_FIXED_VADDR does not generate |
| 93 | * an error when min(p_vaddr) is 0. |
| 94 | * |
| 95 | * Cannot be used with ANDROID_DLEXT_RESERVED_ADDRESS or ANDROID_DLEXT_RESERVED_ADDRESS_HINT. |
| 96 | * |
| 97 | * This flag is for ART internal use only. |
| 98 | */ |
| 99 | ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS = 0x100, |
| 100 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 101 | /* This flag used to load library in a different namespace. The namespace is |
| 102 | * specified in library_namespace. |
| 103 | */ |
| 104 | ANDROID_DLEXT_USE_NAMESPACE = 0x200, |
| 105 | |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 106 | /* Mask of valid bits */ |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 107 | ANDROID_DLEXT_VALID_FLAG_BITS = ANDROID_DLEXT_RESERVED_ADDRESS | |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 108 | ANDROID_DLEXT_RESERVED_ADDRESS_HINT | |
| 109 | ANDROID_DLEXT_WRITE_RELRO | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 110 | ANDROID_DLEXT_USE_RELRO | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 111 | ANDROID_DLEXT_USE_LIBRARY_FD | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 112 | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET | |
Dmitriy Ivanov | 8a11628 | 2015-06-05 22:16:23 -0700 | [diff] [blame] | 113 | ANDROID_DLEXT_FORCE_LOAD | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 114 | ANDROID_DLEXT_FORCE_FIXED_VADDR | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 115 | ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS | |
| 116 | ANDROID_DLEXT_USE_NAMESPACE, |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 119 | struct android_namespace_t; |
| 120 | |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 121 | typedef struct { |
Dmitriy Ivanov | 3a8646f | 2014-07-08 11:21:56 -0700 | [diff] [blame] | 122 | uint64_t flags; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 123 | void* reserved_addr; |
| 124 | size_t reserved_size; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 125 | int relro_fd; |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 126 | int library_fd; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 127 | off64_t library_fd_offset; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 128 | struct android_namespace_t* library_namespace; |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 129 | } android_dlextinfo; |
| 130 | |
| 131 | extern void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo); |
| 132 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 133 | /* |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame^] | 134 | * Initializes public and anonymous namespaces. The public_ns_sonames is the list of sonames |
| 135 | * to be included into public namespace separated by colon. Example: "libc.so:libm.so:libdl.so". |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 136 | * The libraries in this list should be loaded prior to this call. |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame^] | 137 | * |
| 138 | * The anon_ns_library_path is the search path for anonymous namespace. The anonymous namespace |
| 139 | * is used in the case when linker cannot identify the caller of dlopen/dlsym. This happens |
| 140 | * for the code not loaded by dynamic linker; for example calls from the mono-compiled code. |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 141 | */ |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame^] | 142 | extern bool android_init_namespaces(const char* public_ns_sonames, |
| 143 | const char* anon_ns_library_path); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Creates new linker namespace. |
| 147 | * ld_library_path and default_library_path represent the search path |
| 148 | * for the libraries in the namespace. |
| 149 | * |
| 150 | * The libraries in the namespace are searched by folowing order: |
| 151 | * 1. ld_library_path (Think of this as namespace-local LD_LIBRARY_PATH) |
| 152 | * 2. In directories specified by DT_RUNPATH of the "needed by" binary. |
| 153 | * 3. deault_library_path (This of this as namespace-local default library path) |
| 154 | * |
| 155 | * When is_isolated is true the resulted namespace requires all of the libraries |
| 156 | * to be on the search path; the search_path is ld_library_path:default_library_path. |
| 157 | * |
| 158 | * If a library or any of its dependencies are outside of the search path and not |
| 159 | * part of the public namespace dlopen will fail. |
| 160 | */ |
| 161 | extern struct android_namespace_t* android_create_namespace(const char* name, |
| 162 | const char* ld_library_path, |
| 163 | const char* default_library_path, |
| 164 | bool is_isolated); |
| 165 | |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 166 | __END_DECLS |
| 167 | |
| 168 | #endif /* __ANDROID_DLEXT_H__ */ |