blob: b9e12377ec6e39266e2c3a6e9a65a3afcf554535 [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
32#include <unistd.h>
33#include <sys/types.h>
Nick Kralevich9ec0f032012-02-28 10:40:00 -080034#include <elf.h>
35#include <sys/exec_elf.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
Pavel Chupinb7beb692012-08-17 12:53:29 +040037#include <link.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070039#include "private/libc_logging.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080040
41#define DL_ERR(fmt, x...) \
42 do { \
43 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
44 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
45 DEBUG("%s\n", linker_get_error_buffer()); \
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -070046 } while (false)
47
48#define DL_WARN(fmt, x...) \
49 do { \
50 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
51 __libc_format_fd(2, "WARNING: linker: "); \
52 __libc_format_fd(2, fmt, ##x); \
53 __libc_format_fd(2, "\n"); \
54 } while (false)
55
Elliott Hughes650be4e2013-03-05 18:47:58 -080056
Elliott Hughes1a696162012-11-01 13:49:32 -070057// Returns the address of the page containing address 'x'.
58#define PAGE_START(x) ((x) & PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020059
Elliott Hughes1a696162012-11-01 13:49:32 -070060// Returns the offset of address 'x' in its page.
61#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020062
Elliott Hughes1a696162012-11-01 13:49:32 -070063// Returns the address of the next page after address 'x', unless 'x' is
64// itself at the start of a page.
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020065#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
Elliott Hughesd23736e2012-11-01 15:16:56 -070067// Magic shared structures that GDB knows about.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Elliott Hughesca0c11b2013-03-12 10:40:45 -070069struct link_map_t {
Elliott Hughesd23736e2012-11-01 15:16:56 -070070 uintptr_t l_addr;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080071 char* l_name;
Elliott Hughesd23736e2012-11-01 15:16:56 -070072 uintptr_t l_ld;
Elliott Hughesca0c11b2013-03-12 10:40:45 -070073 link_map_t* l_next;
74 link_map_t* l_prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075};
76
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077// Values for r_debug->state
78enum {
Elliott Hughesd23736e2012-11-01 15:16:56 -070079 RT_CONSISTENT,
80 RT_ADD,
81 RT_DELETE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082};
83
Elliott Hughesd23736e2012-11-01 15:16:56 -070084struct r_debug {
85 int32_t r_version;
Elliott Hughesca0c11b2013-03-12 10:40:45 -070086 link_map_t* r_map;
Elliott Hughesd23736e2012-11-01 15:16:56 -070087 void (*r_brk)(void);
88 int32_t r_state;
89 uintptr_t r_ldbase;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090};
91
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092#define FLAG_LINKED 0x00000001
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080094#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
96#define SOINFO_NAME_LEN 128
97
Elliott Hughesca0c11b2013-03-12 10:40:45 -070098typedef void (*linker_function_t)();
99
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700100// Android uses REL for 32-bit but only uses RELA for 64-bit.
101#if defined(__LP64__)
102#define USE_RELA 1
103#endif
104
Elliott Hughes18a206c2012-10-29 17:37:13 -0700105struct soinfo {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700106 public:
Elliott Hughesd23736e2012-11-01 15:16:56 -0700107 char name[SOINFO_NAME_LEN];
Elliott Hughesc6200592013-09-30 18:43:46 -0700108 const Elf_Phdr* phdr;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700109 size_t phnum;
Elliott Hughesc6200592013-09-30 18:43:46 -0700110 Elf_Addr entry;
111 Elf_Addr base;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700112 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Elliott Hughesc6200592013-09-30 18:43:46 -0700114#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700115 uint32_t unused1; // DO NOT USE, maintained for compatibility.
Elliott Hughesc6200592013-09-30 18:43:46 -0700116#endif
Nick Kralevich38bccb22011-08-29 13:49:22 -0700117
Elliott Hughesc6200592013-09-30 18:43:46 -0700118 Elf_Dyn* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
Elliott Hughesc6200592013-09-30 18:43:46 -0700120#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700121 uint32_t unused2; // DO NOT USE, maintained for compatibility
122 uint32_t unused3; // DO NOT USE, maintained for compatibility
Elliott Hughesc6200592013-09-30 18:43:46 -0700123#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughesd23736e2012-11-01 15:16:56 -0700125 soinfo* next;
126 unsigned flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Elliott Hughesd23736e2012-11-01 15:16:56 -0700128 const char* strtab;
Elliott Hughesc6200592013-09-30 18:43:46 -0700129 Elf_Sym* symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700131 size_t nbucket;
132 size_t nchain;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700133 unsigned* bucket;
134 unsigned* chain;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700136#if !defined(__LP64__)
137 // This is only used by 32-bit MIPS, but needs to be here for
138 // all 32-bit architectures to preserve binary compatibility.
139 unsigned* plt_got;
140#endif
141
142#if defined(USE_RELA)
143 Elf_Rela* plt_rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700144 size_t plt_rela_count;
145
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700146 Elf_Rela* rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700147 size_t rela_count;
148#else
Elliott Hughesc6200592013-09-30 18:43:46 -0700149 Elf_Rel* plt_rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700150 size_t plt_rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151
Elliott Hughesc6200592013-09-30 18:43:46 -0700152 Elf_Rel* rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700153 size_t rel_count;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700154#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700156 linker_function_t* preinit_array;
157 size_t preinit_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700159 linker_function_t* init_array;
160 size_t init_array_count;
161 linker_function_t* fini_array;
162 size_t fini_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700164 linker_function_t init_func;
165 linker_function_t fini_func;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700167#if defined(__arm__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700168 // ARM EABI section used for stack unwinding.
169 unsigned* ARM_exidx;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700170 size_t ARM_exidx_count;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700171#elif defined(__mips__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700172 unsigned mips_symtabno;
173 unsigned mips_local_gotno;
174 unsigned mips_gotsym;
175#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700177 size_t ref_count;
178 link_map_t link_map;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400179
Elliott Hughesd23736e2012-11-01 15:16:56 -0700180 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800181
Elliott Hughesd23736e2012-11-01 15:16:56 -0700182 // When you read a virtual address from the ELF file, add this
183 // value to get the corresponding address in the process' address space.
Elliott Hughesc6200592013-09-30 18:43:46 -0700184 Elf_Addr load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200185
Elliott Hughese4d792a2013-10-28 14:19:05 -0700186#if !defined(__LP64__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700187 bool has_text_relocations;
Elliott Hughese4d792a2013-10-28 14:19:05 -0700188#endif
Elliott Hughesd23736e2012-11-01 15:16:56 -0700189 bool has_DT_SYMBOLIC;
190
191 void CallConstructors();
192 void CallDestructors();
193 void CallPreInitConstructors();
194
195 private:
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700196 void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
197 void CallFunction(const char* function_name, linker_function_t function);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198};
199
Elliott Hughese4d792a2013-10-28 14:19:05 -0700200// The possible DT_FLAGS bits are in
201// http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#dynamic_section
202// but not in the upstream NetBSD <sys/exec_elf.h> header file ours is based on.
203#define DF_ORIGIN 0x1
204#define DF_SYMBOLIC 0x2
205#define DF_TEXTREL 0x4
206#define DF_BIND_NOW 0x8
207#define DF_STATIC_TLS 0x10
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
Elliott Hughese4d792a2013-10-28 14:19:05 -0700209extern soinfo libdl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
Elliott Hughescade4c32012-12-20 14:42:14 -0800211void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Elliott Hughese66190d2012-12-18 15:57:55 -0800212soinfo* do_dlopen(const char* name, int flags);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700213int do_dlclose(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200214
Elliott Hughesc6200592013-09-30 18:43:46 -0700215Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700216soinfo* find_containing_library(const void* addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Elliott Hughesc6200592013-09-30 18:43:46 -0700218Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr);
219Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700220
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800221void debuggerd_init();
Elliott Hughes0d787c12013-04-04 13:46:46 -0700222extern "C" abort_msg_t* gAbortMessage;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700223extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700224
Elliott Hughes650be4e2013-03-05 18:47:58 -0800225char* linker_get_error_buffer();
226size_t linker_get_error_buffer_size();
227
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228#endif