2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINKER_H_
|
|
|
|
#define _LINKER_H_
|
|
|
|
|
2015-12-11 01:08:14 +01:00
|
|
|
#include <dlfcn.h>
|
2015-07-28 23:58:37 +02:00
|
|
|
#include <android/dlext.h>
|
2012-02-28 19:40:00 +01:00
|
|
|
#include <elf.h>
|
2014-08-22 21:25:04 +02:00
|
|
|
#include <inttypes.h>
|
2012-08-17 10:53:29 +02:00
|
|
|
#include <link.h>
|
2014-05-09 18:10:14 +02:00
|
|
|
#include <sys/stat.h>
|
2015-07-28 23:58:37 +02:00
|
|
|
#include <unistd.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2015-07-28 23:58:37 +02:00
|
|
|
#include "private/bionic_page.h"
|
2013-03-15 23:30:25 +01:00
|
|
|
#include "private/libc_logging.h"
|
2014-05-09 18:10:14 +02:00
|
|
|
#include "linked_list.h"
|
2016-08-04 01:00:10 +02:00
|
|
|
#include "linker_common_types.h"
|
2016-07-12 03:11:39 +02:00
|
|
|
#include "linker_logger.h"
|
2016-08-04 20:50:36 +02:00
|
|
|
#include "linker_soinfo.h"
|
2013-03-06 03:47:58 +01:00
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
#include <string>
|
2015-04-09 22:42:33 +02:00
|
|
|
#include <vector>
|
2015-03-31 20:14:03 +02:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define ELFW(what) ELF64_ ## what
|
|
|
|
#else
|
|
|
|
#define ELFW(what) ELF32_ ## what
|
|
|
|
#endif
|
2013-03-06 03:47:58 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
// mips64 interprets Elf64_Rel structures' r_info field differently.
|
|
|
|
// bionic (like other C libraries) has macros that assume regular ELF files,
|
|
|
|
// but the dynamic linker needs to be able to load mips64 ELF files.
|
|
|
|
#if defined(__mips__) && defined(__LP64__)
|
|
|
|
#undef ELF64_R_SYM
|
|
|
|
#undef ELF64_R_TYPE
|
|
|
|
#undef ELF64_R_INFO
|
|
|
|
#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
|
|
|
|
#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
|
|
|
|
#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
|
|
|
|
#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
|
|
|
|
#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
|
|
|
|
#endif
|
|
|
|
|
2016-08-19 21:57:12 +02:00
|
|
|
#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE)
|
2014-08-28 23:12:12 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
// Class used construct version dependency graph.
|
|
|
|
class VersionTracker {
|
|
|
|
public:
|
|
|
|
VersionTracker() = default;
|
|
|
|
bool init(const soinfo* si_from);
|
|
|
|
|
|
|
|
const version_info* get_version_info(ElfW(Versym) source_symver) const;
|
|
|
|
private:
|
|
|
|
bool init_verneed(const soinfo* si_from);
|
|
|
|
bool init_verdef(const soinfo* si_from);
|
|
|
|
void add_version_info(size_t source_index, ElfW(Word) elf_hash,
|
|
|
|
const char* ver_name, const soinfo* target_si);
|
|
|
|
|
|
|
|
std::vector<version_info> version_infos;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(VersionTracker);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
|
2016-08-04 01:00:10 +02:00
|
|
|
soinfo** si_found_in, const soinfo_list_t& global_group,
|
|
|
|
const soinfo_list_t& local_group, const ElfW(Sym)** symbol);
|
2015-01-14 20:36:38 +01:00
|
|
|
|
|
|
|
enum RelocationKind {
|
|
|
|
kRelocAbsolute = 0,
|
|
|
|
kRelocRelative,
|
|
|
|
kRelocCopy,
|
|
|
|
kRelocSymbol,
|
|
|
|
kRelocMax
|
|
|
|
};
|
|
|
|
|
|
|
|
void count_relocation(RelocationKind kind);
|
|
|
|
|
2016-11-23 01:55:25 +01:00
|
|
|
soinfo* get_libdl_info(const char* linker_path);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-01-14 01:37:47 +01:00
|
|
|
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
|
2012-12-20 23:42:14 +01:00
|
|
|
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
|
2016-11-23 01:55:25 +01:00
|
|
|
void* do_dlopen(const char* name,
|
|
|
|
int flags,
|
|
|
|
const android_dlextinfo* extinfo,
|
|
|
|
const void* caller_addr);
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
int do_dlclose(void* handle);
|
2012-06-12 16:25:37 +02:00
|
|
|
|
2015-06-29 23:48:25 +02:00
|
|
|
int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
|
|
|
|
|
2016-11-23 01:55:25 +01:00
|
|
|
#if defined(__arm__)
|
|
|
|
_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool do_dlsym(void* handle, const char* sym_name,
|
|
|
|
const char* sym_ver,
|
|
|
|
const void* caller_addr,
|
|
|
|
void** symbol);
|
2015-10-30 01:01:24 +01:00
|
|
|
|
2015-12-11 01:08:14 +01:00
|
|
|
int do_dladdr(const void* addr, Dl_info* info);
|
2012-11-01 23:16:56 +01:00
|
|
|
|
2015-05-22 02:43:49 +02:00
|
|
|
void set_application_target_sdk_version(uint32_t target);
|
|
|
|
uint32_t get_application_target_sdk_version();
|
|
|
|
|
2016-05-10 02:37:39 +02:00
|
|
|
enum {
|
|
|
|
/* A regular namespace is the namespace with a custom search path that does
|
|
|
|
* not impose any restrictions on the location of native libraries.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_REGULAR = 0,
|
|
|
|
|
|
|
|
/* An isolated namespace requires all the libraries to be on the search path
|
|
|
|
* or under permitted_when_isolated_path. The search path is the union of
|
|
|
|
* ld_library_path and default_library_path.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
|
|
|
|
|
|
|
|
/* The shared namespace clones the list of libraries of the caller namespace upon creation
|
|
|
|
* which means that they are shared between namespaces - the caller namespace and the new one
|
|
|
|
* will use the same copy of a library if it was loaded prior to android_create_namespace call.
|
|
|
|
*
|
|
|
|
* Note that libraries loaded after the namespace is created will not be shared.
|
|
|
|
*
|
|
|
|
* Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
|
|
|
|
* permitted_path from the caller's namespace.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_SHARED = 2,
|
|
|
|
ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
|
|
|
|
ANDROID_NAMESPACE_TYPE_ISOLATED,
|
|
|
|
};
|
|
|
|
|
2015-11-23 20:26:35 +01:00
|
|
|
bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path);
|
2016-05-13 00:20:21 +02:00
|
|
|
android_namespace_t* create_namespace(const void* caller_addr,
|
|
|
|
const char* name,
|
|
|
|
const char* ld_library_path,
|
|
|
|
const char* default_library_path,
|
|
|
|
uint64_t type,
|
|
|
|
const char* permitted_when_isolated_path,
|
|
|
|
android_namespace_t* parent_namespace);
|
2015-10-30 01:01:24 +01:00
|
|
|
|
Align-up and randomize shared libraries.
This change implements the following property:
Any 2**N aligned memory region on size 2**N contains no more than one DSO.
The value N can be configured, with 16 or 18 looking like a good choice.
Additionally, DSOs are loaded at random page-aligned address inside these large
regions.
This change has dual purpose:
1. Larger values of N allow a lot more compact CFI shadow implementation.
See change I14dfea630de468eb5620e7f55f92b1397ba06217.
For example, CFI shadow for the system_server process has the following size (RSS, KB):
152 for N = 12, 32 for N = 16, 16 for N = 18.
2. Extra randomization is good for security.
This change does not result in extra RAM usage, because everything is still page-aligned.
It does result in a bit more VM fragmentation because of the gaps between shared libraries.
As it turns out, this fragmentation is barely noticeable because the kernel creates new mapping
at the highest possible address, and we do enough small mappings to almost completely fill the
gaps (ex. in the Zygote the gaps are filled with .ttf file mappings and thread stacks).
I've measured VM fragmentation as the sum of all VM gaps (unmapped regions) that are larger
than 1MB according to /proc/$PID/maps. On aosp_angler-userdebug, the numbers are (in GB):
| N = 12 | N = 18
system_server | 521.9 | 521.1
zygote64 | 522.1 | 521.3
zygote32 | 2.55 | 2.55
mediaserver | 4.00 | 4.00
Change-Id: Ia6df840dd409c82837efd1f263be420d9723c84a
2016-07-16 01:31:42 +02:00
|
|
|
constexpr unsigned kLibraryAlignmentBits = 18;
|
|
|
|
constexpr size_t kLibraryAlignment = 1UL << kLibraryAlignmentBits;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|