linker.h revision 3597b8055da090ef3f1ee662e96dcb952bba2c30
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    the documentation and/or other materials provided with the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef _LINKER_H_
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _LINKER_H_
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
329ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich#include <elf.h>
330d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#include <inttypes.h>
34b7beb6907599b36809effe1b4db76039ac649518Pavel Chupin#include <link.h>
351272dbd1d76c979358fff3beae9de0c1462345afElliott Hughes#include <unistd.h>
36012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <android/dlext.h>
37d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#include <sys/stat.h>
384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
398f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h"
40d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#include "linked_list.h"
41650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
42650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes#define DL_ERR(fmt, x...) \
43650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    do { \
44650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
45650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
46650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      DEBUG("%s\n", linker_get_error_buffer()); \
477e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    } while (false)
487e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes
497e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes#define DL_WARN(fmt, x...) \
507e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    do { \
517e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes      __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
527e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes      __libc_format_fd(2, "WARNING: linker: "); \
537e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes      __libc_format_fd(2, fmt, ##x); \
547e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes      __libc_format_fd(2, "\n"); \
557e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    } while (false)
567e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes
570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#if defined(__LP64__)
580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#define ELFW(what) ELF64_ ## what
590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#else
600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#define ELFW(what) ELF32_ ## what
610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#endif
62650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
639918665a45095ad135576f005c0e5307feb366a1Chris Dearman// mips64 interprets Elf64_Rel structures' r_info field differently.
649918665a45095ad135576f005c0e5307feb366a1Chris Dearman// bionic (like other C libraries) has macros that assume regular ELF files,
659918665a45095ad135576f005c0e5307feb366a1Chris Dearman// but the dynamic linker needs to be able to load mips64 ELF files.
669918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__) && defined(__LP64__)
679918665a45095ad135576f005c0e5307feb366a1Chris Dearman#undef ELF64_R_SYM
689918665a45095ad135576f005c0e5307feb366a1Chris Dearman#undef ELF64_R_TYPE
699918665a45095ad135576f005c0e5307feb366a1Chris Dearman#undef ELF64_R_INFO
709918665a45095ad135576f005c0e5307feb366a1Chris Dearman#define ELF64_R_SYM(info)   (((info) >> 0) & 0xffffffff)
719918665a45095ad135576f005c0e5307feb366a1Chris Dearman#define ELF64_R_SSYM(info)  (((info) >> 32) & 0xff)
729918665a45095ad135576f005c0e5307feb366a1Chris Dearman#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
739918665a45095ad135576f005c0e5307feb366a1Chris Dearman#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
749918665a45095ad135576f005c0e5307feb366a1Chris Dearman#define ELF64_R_TYPE(info)  (((info) >> 56) & 0xff)
759918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
769918665a45095ad135576f005c0e5307feb366a1Chris Dearman
771a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the address of the page containing address 'x'.
781a6961650c82168864afe040dbdc05977db701dfElliott Hughes#define PAGE_START(x)  ((x) & PAGE_MASK)
79c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
801a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the offset of address 'x' in its page.
811a6961650c82168864afe040dbdc05977db701dfElliott Hughes#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
82c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
831a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the address of the next page after address 'x', unless 'x' is
841a6961650c82168864afe040dbdc05977db701dfElliott Hughes// itself at the start of a page.
85c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#define PAGE_END(x)    PAGE_START((x) + (PAGE_SIZE-1))
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FLAG_LINKED     0x00000001
881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FLAG_EXE        0x00000004 // The main executable
89468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich#define FLAG_LINKER     0x00000010 // The linker itself
90ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov#define FLAG_GNU_HASH   0x00000040 // uses gnu hash
91d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
93d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
94d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov
950d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#define SOINFO_VERSION 1
960d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define SOINFO_NAME_LEN 128
981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
99ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughestypedef void (*linker_function_t)();
100ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes
1019918665a45095ad135576f005c0e5307feb366a1Chris Dearman// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
1029918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__aarch64__) || defined(__x86_64__)
1034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#define USE_RELA 1
1044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
106d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstruct soinfo;
107d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
108d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovclass SoinfoListAllocator {
109ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov public:
110d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  static LinkedListEntry<soinfo>* alloc();
111d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  static void free(LinkedListEntry<soinfo>* entry);
112ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
113ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov private:
114d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // unconstructable
115d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
116d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov};
117d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
118ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanovclass SymbolName {
119ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov public:
120ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  explicit SymbolName(const char* name)
121ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov      : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
122ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov        elf_hash_(0), gnu_hash_(0) { }
123ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
124ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  const char* get_name() {
125ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov    return name_;
126ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  }
127ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
128ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  uint32_t elf_hash();
129ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  uint32_t gnu_hash();
130ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
131ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov private:
132ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  const char* name_;
133ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  bool has_elf_hash_;
134ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  bool has_gnu_hash_;
135ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  uint32_t elf_hash_;
136ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  uint32_t gnu_hash_;
137ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
138ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
139ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov};
140ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
14118a206c81d9743481e364384affd43306911283dElliott Hughesstruct soinfo {
142ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes public:
143d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
144d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov public:
145d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  char name[SOINFO_NAME_LEN];
1460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr;
147ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  size_t phnum;
1480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) entry;
1490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) base;
1505ceb889ac9a36e74e4e00c34c57cbb58d73e228cWeiwu Chen  size_t size;
1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
152c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#ifndef __LP64__
153ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  uint32_t unused1;  // DO NOT USE, maintained for compatibility.
154c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#endif
15538bccb271f121fc06eaa0d8fbd3c982bc44c36b7Nick Kralevich
1560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Dyn)* dynamic;
1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
158c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#ifndef __LP64__
159ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  uint32_t unused2; // DO NOT USE, maintained for compatibility
160ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  uint32_t unused3; // DO NOT USE, maintained for compatibility
161c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#endif
1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
163d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* next;
1646cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov private:
165ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  uint32_t flags_;
166ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
167047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  const char* strtab_;
168047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Sym)* symtab_;
1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
170047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t nbucket_;
171047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t nchain_;
172047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t* bucket_;
173047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t* chain_;
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1759918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__) || !defined(__LP64__)
1769918665a45095ad135576f005c0e5307feb366a1Chris Dearman  // This is only used by mips and mips64, but needs to be here for
1774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes  // all 32-bit architectures to preserve binary compatibility.
178047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Addr)** plt_got_;
1794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
1814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
182047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Rela)* plt_rela_;
183047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t plt_rela_count_;
184c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
185047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Rela)* rela_;
186047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t rela_count_;
187c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
188047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Rel)* plt_rel_;
189047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t plt_rel_count_;
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
191047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Rel)* rel_;
192047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t rel_count_;
193c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
195047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  linker_function_t* preinit_array_;
196047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t preinit_array_count_;
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
198047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  linker_function_t* init_array_;
199047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t init_array_count_;
200047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  linker_function_t* fini_array_;
201047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t fini_array_count_;
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
203047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  linker_function_t init_func_;
204047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  linker_function_t fini_func_;
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
207ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov public:
208d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // ARM EABI section used for stack unwinding.
2091b20dafdbe65e43b9f4c95057e8482380833ea91Dmitriy Ivanov  uint32_t* ARM_exidx;
210ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  size_t ARM_exidx_count;
211889409168322920ab6cddeb93f506c983d8b5878Dmitriy Ivanov private:
212ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov#elif defined(__mips__)
213047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t mips_symtabno_;
214047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t mips_local_gotno_;
215047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t mips_gotsym_;
216889409168322920ab6cddeb93f506c983d8b5878Dmitriy Ivanov  bool mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group);
217889409168322920ab6cddeb93f506c983d8b5878Dmitriy Ivanov
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
219ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  size_t ref_count_;
220047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov public:
2213a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  link_map link_map_head;
222e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
223d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  bool constructors_called;
2249ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
225d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // When you read a virtual address from the ELF file, add this
226d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // value to get the corresponding address in the process' address space.
2270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) load_bias;
2285ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
229e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
230d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  bool has_text_relocations;
231e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
23296bc37f2e1093416a432135265fd7a4db6c3df17Dmitriy Ivanov  bool has_DT_SYMBOLIC;
2330d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
234047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov public:
23507e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov  soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags);
2360d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
237047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  void call_constructors();
238047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  void call_destructors();
239047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  void call_pre_init_constructors();
240047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  bool prelink_image();
241047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo);
242d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
243d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  void add_child(soinfo* child);
244d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  void remove_all_links();
245d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
246d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  ino_t get_st_ino() const;
247d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  dev_t get_st_dev() const;
248d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  off64_t get_file_offset() const;
249c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
250d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  uint32_t get_rtld_flags() const;
251d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  uint32_t get_dt_flags_1() const;
252d225a5e65223b375a63548c4b780f04d8f3d7b60Dmitriy Ivanov  void set_dt_flags_1(uint32_t dt_flags_1);
253e8ba50fe0d51fbefee1a8f5bb62bf51d841512c8Dmitriy Ivanov
254d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  soinfo_list_t& get_children();
25514669a939d113214a4a20b9318fca0992d5453f0Dmitriy Ivanov  soinfo_list_t& get_parents();
256d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
257ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* find_symbol_by_name(SymbolName& symbol_name);
258ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* find_symbol_by_address(const void* addr);
2599aea164457c269c475592da36b4655d45f55c7bcDmitriy Ivanov  ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
2609aea164457c269c475592da36b4655d45f55c7bcDmitriy Ivanov
2616cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov  const char* get_string(ElfW(Word) index) const;
2621b20dafdbe65e43b9f4c95057e8482380833ea91Dmitriy Ivanov  bool can_unload() const;
263ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  bool is_gnu_hash() const;
2646cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov
2656cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov  bool inline has_min_version(uint32_t min_version) const {
266ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov    return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
2670d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  }
2681b20dafdbe65e43b9f4c95057e8482380833ea91Dmitriy Ivanov
269ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  bool is_linked() const;
270ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  bool is_main_executable() const;
271ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
272ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  void set_linked();
273ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  void set_linker_flag();
274ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  void set_main_executable();
275ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
276ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  void increment_ref_count();
277ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  size_t decrement_ref_count();
278ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
279ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  soinfo* get_local_group_root() const;
280ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
281d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes private:
282ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* elf_lookup(SymbolName& symbol_name);
283ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* elf_addr_lookup(const void* addr);
284ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* gnu_lookup(SymbolName& symbol_name);
285ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  ElfW(Sym)* gnu_addr_lookup(const void* addr);
286ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
287047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
288047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  void call_function(const char* function_name, linker_function_t function);
289fa26eee77685e8dee7986e62a7d263003f5bd25aDmitriy Ivanov  template<typename ElfRelIteratorT>
290fa26eee77685e8dee7986e62a7d263003f5bd25aDmitriy Ivanov  bool relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
291d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
292d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov private:
293d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // This part of the structure is only available
294d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // when FLAG_NEW_SOINFO is set in this->flags.
295047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t version_;
296c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
2970d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  // version >= 0
298047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  dev_t st_dev_;
299047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ino_t st_ino_;
300d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
301d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // dependency graph
302047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  soinfo_list_t children_;
303047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  soinfo_list_t parents_;
304d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
3050d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  // version >= 1
306047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  off64_t file_offset_;
307047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t rtld_flags_;
308047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t dt_flags_1_;
309047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  size_t strtab_size_;
3106cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov
311ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov  // version >= 2
3123597b8055da090ef3f1ee662e96dcb952bba2c30Dmitriy Ivanov
3133597b8055da090ef3f1ee662e96dcb952bba2c30Dmitriy Ivanov  size_t gnu_nbucket_;
3143597b8055da090ef3f1ee662e96dcb952bba2c30Dmitriy Ivanov  uint32_t* gnu_bucket_;
3153597b8055da090ef3f1ee662e96dcb952bba2c30Dmitriy Ivanov  uint32_t* gnu_chain_;
316047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t gnu_maskwords_;
317047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  uint32_t gnu_shift2_;
318047b5934b5f1c62502fc9262ce634529c6cc1420Dmitriy Ivanov  ElfW(Addr)* gnu_bloom_filter_;
319ec18ce06f2d007be40ad6f043058f5a4c7236573Dmitriy Ivanov
320ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov  soinfo* local_group_root_;
321ab972b9adf8789a9e1b03129cd7f0c22e6bba117Dmitriy Ivanov
32218a6956b76a071097fc658c5fe13ef010e31864aDmitriy Ivanov  uint8_t* android_relocs_;
32318a6956b76a071097fc658c5fe13ef010e31864aDmitriy Ivanov  size_t android_relocs_size_;
32418a6956b76a071097fc658c5fe13ef010e31864aDmitriy Ivanov
3256cdeb5234d7f4523fe9d83974f265d80f10512a6Dmitriy Ivanov  friend soinfo* get_libdl_info();
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project};
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
328114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy IvanovElfW(Sym)* soinfo_do_lookup(soinfo* si_from, const char* name, soinfo** si_found_in,
329114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov    const soinfo::soinfo_list_t& global_group, const soinfo::soinfo_list_t& local_group);
330114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov
331114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanovenum RelocationKind {
332114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov  kRelocAbsolute = 0,
333114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov  kRelocRelative,
334114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov  kRelocCopy,
335114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov  kRelocSymbol,
336114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov  kRelocMax
337114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov};
338114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov
339114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanovvoid count_relocation(RelocationKind kind);
340114ff69f1753c7fe4d749f8fb0c082b80e0d43f4Dmitriy Ivanov
3411b20dafdbe65e43b9f4c95057e8482380833ea91Dmitriy Ivanovsoinfo* get_libdl_info();
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
343a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char*, size_t);
344cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
3451a586293400d0e1d73e6eb82f7dfe9d2d9ed3c4bElliott Hughessoinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
346b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovvoid do_dlclose(soinfo* si);
34716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner
34802aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
349d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughessoinfo* find_containing_library(const void* addr);
3501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
35102aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
352d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
353d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromvoid debuggerd_init();
3541728b2396591853345507a063ed6075dfd251706Elliott Hughesextern "C" abort_msg_t* g_abort_message;
35518a206c81d9743481e364384affd43306911283dElliott Hughesextern "C" void notify_gdb_of_libraries();
3564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
357650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer();
358650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size();
359650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
361