blob: a9ed116267d8154ee4201938e5a5cee772b1ba8b [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 Hughes650be4e2013-03-05 18:47:58 -080039#include <private/debug_format.h>
40
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()); \
46 } while(0)
47
Elliott Hughes1a696162012-11-01 13:49:32 -070048// Returns the address of the page containing address 'x'.
49#define PAGE_START(x) ((x) & PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020050
Elliott Hughes1a696162012-11-01 13:49:32 -070051// Returns the offset of address 'x' in its page.
52#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020053
Elliott Hughes1a696162012-11-01 13:49:32 -070054// Returns the address of the next page after address 'x', unless 'x' is
55// itself at the start of a page.
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020056#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
Elliott Hughesd23736e2012-11-01 15:16:56 -070058// Magic shared structures that GDB knows about.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Elliott Hughesd23736e2012-11-01 15:16:56 -070060struct link_map {
61 uintptr_t l_addr;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080062 char* l_name;
Elliott Hughesd23736e2012-11-01 15:16:56 -070063 uintptr_t l_ld;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080064 struct link_map* l_next;
65 struct link_map* l_prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066};
67
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068// Values for r_debug->state
69enum {
Elliott Hughesd23736e2012-11-01 15:16:56 -070070 RT_CONSISTENT,
71 RT_ADD,
72 RT_DELETE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073};
74
Elliott Hughesd23736e2012-11-01 15:16:56 -070075struct r_debug {
76 int32_t r_version;
77 struct link_map* r_map;
78 void (*r_brk)(void);
79 int32_t r_state;
80 uintptr_t r_ldbase;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081};
82
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083#define FLAG_LINKED 0x00000001
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080085#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
87#define SOINFO_NAME_LEN 128
88
Elliott Hughes18a206c2012-10-29 17:37:13 -070089struct soinfo {
Elliott Hughesd23736e2012-11-01 15:16:56 -070090 char name[SOINFO_NAME_LEN];
91 const Elf32_Phdr* phdr;
92 int phnum;
Kito Chengfa8c05d2013-03-12 14:58:06 +080093 Elf32_Addr entry;
94 Elf32_Addr base;
Elliott Hughesd23736e2012-11-01 15:16:56 -070095 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096
Elliott Hughesd23736e2012-11-01 15:16:56 -070097 int unused; // DO NOT USE, maintained for compatibility.
Nick Kralevich38bccb22011-08-29 13:49:22 -070098
Brian Carlstromd4ee82d2013-02-28 15:58:45 -080099 Elf32_Dyn* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100
Elliott Hughesd23736e2012-11-01 15:16:56 -0700101 unsigned unused2; // DO NOT USE, maintained for compatibility
102 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Elliott Hughesd23736e2012-11-01 15:16:56 -0700104 soinfo* next;
105 unsigned flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughesd23736e2012-11-01 15:16:56 -0700107 const char* strtab;
108 Elf32_Sym* symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
Elliott Hughesd23736e2012-11-01 15:16:56 -0700110 unsigned nbucket;
111 unsigned nchain;
112 unsigned* bucket;
113 unsigned* chain;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughesd23736e2012-11-01 15:16:56 -0700115 unsigned* plt_got;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
Elliott Hughesd23736e2012-11-01 15:16:56 -0700117 Elf32_Rel* plt_rel;
118 unsigned plt_rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
Elliott Hughesd23736e2012-11-01 15:16:56 -0700120 Elf32_Rel* rel;
121 unsigned rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Elliott Hughesd23736e2012-11-01 15:16:56 -0700123 unsigned* preinit_array;
124 unsigned preinit_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
Elliott Hughesd23736e2012-11-01 15:16:56 -0700126 unsigned* init_array;
127 unsigned init_array_count;
128 unsigned* fini_array;
129 unsigned fini_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Elliott Hughesd23736e2012-11-01 15:16:56 -0700131 void (*init_func)();
132 void (*fini_func)();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700134#if defined(ANDROID_ARM_LINKER)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700135 // ARM EABI section used for stack unwinding.
136 unsigned* ARM_exidx;
137 unsigned ARM_exidx_count;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700138#elif defined(ANDROID_MIPS_LINKER)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700139 unsigned mips_symtabno;
140 unsigned mips_local_gotno;
141 unsigned mips_gotsym;
142#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
Elliott Hughesd23736e2012-11-01 15:16:56 -0700144 unsigned refcount;
145 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400146
Elliott Hughesd23736e2012-11-01 15:16:56 -0700147 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800148
Elliott Hughesd23736e2012-11-01 15:16:56 -0700149 // When you read a virtual address from the ELF file, add this
150 // value to get the corresponding address in the process' address space.
151 Elf32_Addr load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200152
Elliott Hughesd23736e2012-11-01 15:16:56 -0700153 bool has_text_relocations;
154 bool has_DT_SYMBOLIC;
155
156 void CallConstructors();
157 void CallDestructors();
158 void CallPreInitConstructors();
159
160 private:
161 void CallArray(const char* array_name, unsigned* array, int count, bool reverse);
162 void CallFunction(const char* function_name, void (*function)());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163};
164
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165extern soinfo libdl_info;
166
Elliott Hughesa6a3ac52013-01-29 15:02:50 -0800167// These aren't defined in <sys/exec_elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168#ifndef DT_PREINIT_ARRAY
169#define DT_PREINIT_ARRAY 32
170#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171#ifndef DT_PREINIT_ARRAYSZ
172#define DT_PREINIT_ARRAYSZ 33
173#endif
174
Elliott Hughescade4c32012-12-20 14:42:14 -0800175void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Elliott Hughese66190d2012-12-18 15:57:55 -0800176soinfo* do_dlopen(const char* name, int flags);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700177int do_dlclose(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200178
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800179Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700180soinfo* find_containing_library(const void* addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800182Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr);
183Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700184
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800185void debuggerd_init();
Elliott Hughes18a206c2012-10-29 17:37:13 -0700186extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700187
Elliott Hughes650be4e2013-03-05 18:47:58 -0800188char* linker_get_error_buffer();
189size_t linker_get_error_buffer_size();
190
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191#endif