linker.h revision 74ce45972d88604aa759040cfd2570674cfb439d
1/*
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>
34#include <elf.h>
35#include <sys/exec_elf.h>
36
37#include <link.h>
38
39#include "private/libc_logging.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 (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
56
57// Returns the address of the page containing address 'x'.
58#define PAGE_START(x)  ((x) & PAGE_MASK)
59
60// Returns the offset of address 'x' in its page.
61#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
62
63// Returns the address of the next page after address 'x', unless 'x' is
64// itself at the start of a page.
65#define PAGE_END(x)    PAGE_START((x) + (PAGE_SIZE-1))
66
67// Magic shared structures that GDB knows about.
68
69struct link_map_t {
70  uintptr_t l_addr;
71  char*  l_name;
72  uintptr_t l_ld;
73  link_map_t* l_next;
74  link_map_t* l_prev;
75};
76
77// Values for r_debug->state
78enum {
79  RT_CONSISTENT,
80  RT_ADD,
81  RT_DELETE
82};
83
84struct r_debug {
85  int32_t r_version;
86  link_map_t* r_map;
87  void (*r_brk)(void);
88  int32_t r_state;
89  uintptr_t r_ldbase;
90};
91
92#define FLAG_LINKED     0x00000001
93#define FLAG_EXE        0x00000004 // The main executable
94#define FLAG_LINKER     0x00000010 // The linker itself
95
96#define SOINFO_NAME_LEN 128
97
98typedef void (*linker_function_t)();
99
100// 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
105struct soinfo {
106 public:
107  char name[SOINFO_NAME_LEN];
108  const Elf_Phdr* phdr;
109  size_t phnum;
110  Elf_Addr entry;
111  Elf_Addr base;
112  unsigned size;
113
114#ifndef __LP64__
115  uint32_t unused1;  // DO NOT USE, maintained for compatibility.
116#endif
117
118  Elf_Dyn* dynamic;
119
120#ifndef __LP64__
121  uint32_t unused2; // DO NOT USE, maintained for compatibility
122  uint32_t unused3; // DO NOT USE, maintained for compatibility
123#endif
124
125  soinfo* next;
126  unsigned flags;
127
128  const char* strtab;
129  Elf_Sym* symtab;
130
131  size_t nbucket;
132  size_t nchain;
133  unsigned* bucket;
134  unsigned* chain;
135
136#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;
144  size_t plt_rela_count;
145
146  Elf_Rela* rela;
147  size_t rela_count;
148#else
149  Elf_Rel* plt_rel;
150  size_t plt_rel_count;
151
152  Elf_Rel* rel;
153  size_t rel_count;
154#endif
155
156  linker_function_t* preinit_array;
157  size_t preinit_array_count;
158
159  linker_function_t* init_array;
160  size_t init_array_count;
161  linker_function_t* fini_array;
162  size_t fini_array_count;
163
164  linker_function_t init_func;
165  linker_function_t fini_func;
166
167#if defined(__arm__)
168  // ARM EABI section used for stack unwinding.
169  unsigned* ARM_exidx;
170  size_t ARM_exidx_count;
171#elif defined(__mips__)
172  unsigned mips_symtabno;
173  unsigned mips_local_gotno;
174  unsigned mips_gotsym;
175#endif
176
177  size_t ref_count;
178  link_map_t link_map;
179
180  bool constructors_called;
181
182  // When you read a virtual address from the ELF file, add this
183  // value to get the corresponding address in the process' address space.
184  Elf_Addr load_bias;
185
186#if !defined(__LP64__)
187  bool has_text_relocations;
188#endif
189  bool has_DT_SYMBOLIC;
190
191  void CallConstructors();
192  void CallDestructors();
193  void CallPreInitConstructors();
194
195 private:
196  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);
198};
199
200extern soinfo libdl_info;
201
202void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
203soinfo* do_dlopen(const char* name, int flags);
204int do_dlclose(soinfo* si);
205
206Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
207soinfo* find_containing_library(const void* addr);
208
209Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr);
210Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
211
212void debuggerd_init();
213extern "C" abort_msg_t* gAbortMessage;
214extern "C" void notify_gdb_of_libraries();
215
216char* linker_get_error_buffer();
217size_t linker_get_error_buffer_size();
218
219#endif
220