blob: ae1ae3c6a80e3f24fa8d6fe8f30907a6d4de3eed [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
29#ifndef _LINKER_H_
30#define _LINKER_H_
31
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -080032#include <dlfcn.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070033#include <android/dlext.h>
Nick Kralevich9ec0f032012-02-28 10:40:00 -080034#include <elf.h>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070035#include <inttypes.h>
Pavel Chupinb7beb692012-08-17 12:53:29 +040036#include <link.h>
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070037#include <sys/stat.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070038#include <unistd.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039
Elliott Hughesafab3ff2015-07-28 14:58:37 -070040#include "private/bionic_page.h"
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070041#include "linked_list.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070042#include "linker_common_types.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070043#include "linker_logger.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070044#include "linker_soinfo.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080045
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070046#include <string>
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070047#include <vector>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070048
Elliott Hughes0266ae52014-02-10 17:46:57 -080049#if defined(__LP64__)
50#define ELFW(what) ELF64_ ## what
51#else
52#define ELFW(what) ELF32_ ## what
53#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080054
Chris Dearman99186652014-02-06 20:36:51 -080055// mips64 interprets Elf64_Rel structures' r_info field differently.
56// bionic (like other C libraries) has macros that assume regular ELF files,
57// but the dynamic linker needs to be able to load mips64 ELF files.
58#if defined(__mips__) && defined(__LP64__)
59#undef ELF64_R_SYM
60#undef ELF64_R_TYPE
61#undef ELF64_R_INFO
62#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
63#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
64#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
65#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
66#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
67#endif
68
Bernhard Rosenkränzer59ba65e2016-08-19 21:57:12 +020069#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE)
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070070
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070071// Class used construct version dependency graph.
72class VersionTracker {
73 public:
74 VersionTracker() = default;
75 bool init(const soinfo* si_from);
76
77 const version_info* get_version_info(ElfW(Versym) source_symver) const;
78 private:
79 bool init_verneed(const soinfo* si_from);
80 bool init_verdef(const soinfo* si_from);
81 void add_version_info(size_t source_index, ElfW(Word) elf_hash,
82 const char* ver_name, const soinfo* target_si);
83
84 std::vector<version_info> version_infos;
85
86 DISALLOW_COPY_AND_ASSIGN(VersionTracker);
87};
88
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070089bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -070090 soinfo** si_found_in, const soinfo_list_t& global_group,
91 const soinfo_list_t& local_group, const ElfW(Sym)** symbol);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080092
93enum RelocationKind {
94 kRelocAbsolute = 0,
95 kRelocRelative,
96 kRelocCopy,
97 kRelocSymbol,
98 kRelocMax
99};
100
101void count_relocation(RelocationKind kind);
102
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800103soinfo* get_libdl_info(const char* linker_path);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700105soinfo* find_containing_library(const void* p);
106
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800107void do_android_get_LD_LIBRARY_PATH(char*, size_t);
Elliott Hughescade4c32012-12-20 14:42:14 -0800108void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800109void* do_dlopen(const char* name,
110 int flags,
111 const android_dlextinfo* extinfo,
112 const void* caller_addr);
113
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700114int do_dlclose(void* handle);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200115
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700116int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
117
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800118#if defined(__arm__)
119_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
120#endif
121
122bool do_dlsym(void* handle, const char* sym_name,
123 const char* sym_ver,
124 const void* caller_addr,
125 void** symbol);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700126
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800127int do_dladdr(const void* addr, Dl_info* info);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700128
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700129// void ___cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr, void *Ret);
130// void ___cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData, void *Ret);
131void ___cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *Ret);
132
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700133void set_application_target_sdk_version(uint32_t target);
134uint32_t get_application_target_sdk_version();
135
Dimitry Ivanov41fd2952016-05-09 17:37:39 -0700136enum {
137 /* A regular namespace is the namespace with a custom search path that does
138 * not impose any restrictions on the location of native libraries.
139 */
140 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
141
142 /* An isolated namespace requires all the libraries to be on the search path
143 * or under permitted_when_isolated_path. The search path is the union of
144 * ld_library_path and default_library_path.
145 */
146 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
147
148 /* The shared namespace clones the list of libraries of the caller namespace upon creation
149 * which means that they are shared between namespaces - the caller namespace and the new one
150 * will use the same copy of a library if it was loaded prior to android_create_namespace call.
151 *
152 * Note that libraries loaded after the namespace is created will not be shared.
153 *
154 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
155 * permitted_path from the caller's namespace.
156 */
157 ANDROID_NAMESPACE_TYPE_SHARED = 2,
Jiyong Park37b91af2017-05-05 22:07:05 +0900158
159 /* This flag instructs linker to enable grey-list workaround for the namespace.
160 * See http://b/26394120 for details.
161 */
162 ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED = 0x08000000,
163
Dimitry Ivanov41fd2952016-05-09 17:37:39 -0700164 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
165 ANDROID_NAMESPACE_TYPE_ISOLATED,
166};
167
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800168bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path);
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700169android_namespace_t* create_namespace(const void* caller_addr,
170 const char* name,
171 const char* ld_library_path,
172 const char* default_library_path,
173 uint64_t type,
174 const char* permitted_when_isolated_path,
175 android_namespace_t* parent_namespace);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700176
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800177bool link_namespaces(android_namespace_t* namespace_from,
178 android_namespace_t* namespace_to,
179 const char* shared_lib_sonames);
180
Jiyong Park01de74e2017-04-03 23:10:37 +0900181android_namespace_t* get_exported_namespace(const char* name);
182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183#endif