blob: a01f0911b3ca6f05dc39040811a79c14710214a9 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -080031#include <dlfcn.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070032#include <android/dlext.h>
Nick Kralevich9ec0f032012-02-28 10:40:00 -080033#include <elf.h>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070034#include <inttypes.h>
Pavel Chupinb7beb692012-08-17 12:53:29 +040035#include <link.h>
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070036#include <sys/stat.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070037#include <unistd.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038
Elliott Hughesafab3ff2015-07-28 14:58:37 -070039#include "private/bionic_page.h"
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070040#include "linked_list.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070041#include "linker_common_types.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070042#include "linker_logger.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070043#include "linker_soinfo.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080044
Christopher R. Palmer9dc396a2017-04-08 22:40:01 +020045#ifdef LD_SHIM_LIBS
46#include "linker_debug.h"
47#endif
48
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070049#include <string>
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070050#include <vector>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070051
Elliott Hughes0266ae52014-02-10 17:46:57 -080052#if defined(__LP64__)
53#define ELFW(what) ELF64_ ## what
54#else
55#define ELFW(what) ELF32_ ## what
56#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080057
Chris Dearman99186652014-02-06 20:36:51 -080058// mips64 interprets Elf64_Rel structures' r_info field differently.
59// bionic (like other C libraries) has macros that assume regular ELF files,
60// but the dynamic linker needs to be able to load mips64 ELF files.
61#if defined(__mips__) && defined(__LP64__)
62#undef ELF64_R_SYM
63#undef ELF64_R_TYPE
64#undef ELF64_R_INFO
65#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
66#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
67#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
68#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
69#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
70#endif
71
Bernhard Rosenkränzer59ba65e2016-08-19 21:57:12 +020072#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070073
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070074// Class used construct version dependency graph.
75class VersionTracker {
76 public:
77 VersionTracker() = default;
78 bool init(const soinfo* si_from);
79
80 const version_info* get_version_info(ElfW(Versym) source_symver) const;
81 private:
82 bool init_verneed(const soinfo* si_from);
83 bool init_verdef(const soinfo* si_from);
84 void add_version_info(size_t source_index, ElfW(Word) elf_hash,
85 const char* ver_name, const soinfo* target_si);
86
87 std::vector<version_info> version_infos;
88
89 DISALLOW_COPY_AND_ASSIGN(VersionTracker);
90};
91
Ryan Prichard04896452018-08-20 17:44:42 -070092soinfo* get_libdl_info(const char* linker_path, const soinfo& linker_si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070094soinfo* find_containing_library(const void* p);
95
Ryan Prichard8f639a42018-10-01 23:10:05 -070096int open_executable(const char* path, off64_t* file_offset, std::string* realpath);
97
Christopher R. Palmer9dc396a2017-04-08 22:40:01 +020098#ifdef LD_SHIM_LIBS
99typedef std::pair<std::string, std::string> ShimDescriptor;
100void parse_LD_SHIM_LIBS(const char* path);
101std::vector<const ShimDescriptor*> shim_matching_pairs(const char* path);
102
103template<typename F>
104void for_each_matching_shim(const char* path, F action) {
105 if (path == nullptr) return;
106 INFO("Finding shim libs for \"%s\"", path);
107 for (const auto& one_pair : shim_matching_pairs(path)) {
108 INFO("Injecting shim lib \"%s\" as needed for %s", one_pair->second.c_str(), path);
109 action(one_pair->second.c_str());
110 }
111}
112#endif
113
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800114void do_android_get_LD_LIBRARY_PATH(char*, size_t);
Elliott Hughescade4c32012-12-20 14:42:14 -0800115void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800116void* do_dlopen(const char* name,
117 int flags,
118 const android_dlextinfo* extinfo,
119 const void* caller_addr);
120
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700121int do_dlclose(void* handle);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200122
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700123int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
124
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800125#if defined(__arm__)
126_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
127#endif
128
129bool do_dlsym(void* handle, const char* sym_name,
130 const char* sym_ver,
131 const void* caller_addr,
132 void** symbol);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700133
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800134int do_dladdr(const void* addr, Dl_info* info);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700135
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700136// void ___cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr, void *Ret);
137// void ___cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData, void *Ret);
138void ___cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *Ret);
139
Elliott Hughesff1428a2018-11-12 16:01:37 -0800140void set_application_target_sdk_version(int target);
141int get_application_target_sdk_version();
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700142
Dimitry Ivanov41fd2952016-05-09 17:37:39 -0700143enum {
144 /* A regular namespace is the namespace with a custom search path that does
145 * not impose any restrictions on the location of native libraries.
146 */
147 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
148
149 /* An isolated namespace requires all the libraries to be on the search path
150 * or under permitted_when_isolated_path. The search path is the union of
151 * ld_library_path and default_library_path.
152 */
153 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
154
155 /* The shared namespace clones the list of libraries of the caller namespace upon creation
156 * which means that they are shared between namespaces - the caller namespace and the new one
157 * will use the same copy of a library if it was loaded prior to android_create_namespace call.
158 *
159 * Note that libraries loaded after the namespace is created will not be shared.
160 *
161 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
162 * permitted_path from the caller's namespace.
163 */
164 ANDROID_NAMESPACE_TYPE_SHARED = 2,
Jiyong Park37b91af2017-05-05 22:07:05 +0900165
166 /* This flag instructs linker to enable grey-list workaround for the namespace.
167 * See http://b/26394120 for details.
168 */
169 ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED = 0x08000000,
170
Dimitry Ivanov41fd2952016-05-09 17:37:39 -0700171 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
172 ANDROID_NAMESPACE_TYPE_ISOLATED,
173};
174
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800175bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path);
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700176android_namespace_t* create_namespace(const void* caller_addr,
177 const char* name,
178 const char* ld_library_path,
179 const char* default_library_path,
180 uint64_t type,
181 const char* permitted_when_isolated_path,
182 android_namespace_t* parent_namespace);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700183
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800184bool link_namespaces(android_namespace_t* namespace_from,
185 android_namespace_t* namespace_to,
186 const char* shared_lib_sonames);
187
Logan Chien9ee45912018-01-18 12:05:09 +0800188bool link_namespaces_all_libs(android_namespace_t* namespace_from,
189 android_namespace_t* namespace_to);
190
Jiyong Park01de74e2017-04-03 23:10:37 +0900191android_namespace_t* get_exported_namespace(const char* name);
192
dimitry06016f22018-01-05 11:39:28 +0100193void increment_dso_handle_reference_counter(void* dso_handle);
194void decrement_dso_handle_reference_counter(void* dso_handle);
Vic Yangbb7e1232019-01-29 20:23:16 -0800195
196void purge_unused_memory();
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400197
198struct address_space_params {
199 void* start_addr = nullptr;
200 size_t reserved_size = 0;
201 bool must_use_address = false;
202};
Ryan Prichardbbc2cb42020-01-02 14:59:11 -0800203
Ryan Prichardf56ac992020-01-02 16:36:06 -0800204int get_application_target_sdk_version();
Ryan Prichardbbc2cb42020-01-02 14:59:11 -0800205ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi);
Ryan Prichardf56ac992020-01-02 16:36:06 -0800206bool validate_verdef_section(const soinfo* si);