linker.h revision 0266ae5f884d72da58f33a072e865ba131234a5e
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 <elf.h>
33#include <link.h>
34#include <unistd.h>
35
36#include "private/libc_logging.h"
37
38#define DL_ERR(fmt, x...) \
39    do { \
40      __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
41      /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
42      DEBUG("%s\n", linker_get_error_buffer()); \
43    } while (false)
44
45#define DL_WARN(fmt, x...) \
46    do { \
47      __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
48      __libc_format_fd(2, "WARNING: linker: "); \
49      __libc_format_fd(2, fmt, ##x); \
50      __libc_format_fd(2, "\n"); \
51    } while (false)
52
53#if defined(__LP64__)
54#define ELFW(what) ELF64_ ## what
55#else
56#define ELFW(what) ELF32_ ## what
57#endif
58
59// Returns the address of the page containing address 'x'.
60#define PAGE_START(x)  ((x) & PAGE_MASK)
61
62// Returns the offset of address 'x' in its page.
63#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
64
65// Returns the address of the next page after address 'x', unless 'x' is
66// itself at the start of a page.
67#define PAGE_END(x)    PAGE_START((x) + (PAGE_SIZE-1))
68
69#define FLAG_LINKED     0x00000001
70#define FLAG_EXE        0x00000004 // The main executable
71#define FLAG_LINKER     0x00000010 // The linker itself
72
73#define SOINFO_NAME_LEN 128
74
75typedef void (*linker_function_t)();
76
77// Android uses REL for 32-bit but only uses RELA for 64-bit.
78#if defined(__LP64__)
79#define USE_RELA 1
80#endif
81
82struct soinfo {
83 public:
84  char name[SOINFO_NAME_LEN];
85  const ElfW(Phdr)* phdr;
86  size_t phnum;
87  ElfW(Addr) entry;
88  ElfW(Addr) base;
89  size_t size;
90
91#ifndef __LP64__
92  uint32_t unused1;  // DO NOT USE, maintained for compatibility.
93#endif
94
95  ElfW(Dyn)* dynamic;
96
97#ifndef __LP64__
98  uint32_t unused2; // DO NOT USE, maintained for compatibility
99  uint32_t unused3; // DO NOT USE, maintained for compatibility
100#endif
101
102  soinfo* next;
103  unsigned flags;
104
105  const char* strtab;
106  ElfW(Sym)* symtab;
107
108  size_t nbucket;
109  size_t nchain;
110  unsigned* bucket;
111  unsigned* chain;
112
113#if !defined(__LP64__)
114  // This is only used by 32-bit MIPS, but needs to be here for
115  // all 32-bit architectures to preserve binary compatibility.
116  unsigned* plt_got;
117#endif
118
119#if defined(USE_RELA)
120  ElfW(Rela)* plt_rela;
121  size_t plt_rela_count;
122
123  ElfW(Rela)* rela;
124  size_t rela_count;
125#else
126  ElfW(Rel)* plt_rel;
127  size_t plt_rel_count;
128
129  ElfW(Rel)* rel;
130  size_t rel_count;
131#endif
132
133  linker_function_t* preinit_array;
134  size_t preinit_array_count;
135
136  linker_function_t* init_array;
137  size_t init_array_count;
138  linker_function_t* fini_array;
139  size_t fini_array_count;
140
141  linker_function_t init_func;
142  linker_function_t fini_func;
143
144#if defined(__arm__)
145  // ARM EABI section used for stack unwinding.
146  unsigned* ARM_exidx;
147  size_t ARM_exidx_count;
148#elif defined(__mips__)
149  unsigned mips_symtabno;
150  unsigned mips_local_gotno;
151  unsigned mips_gotsym;
152#endif
153
154  size_t ref_count;
155  link_map link_map_head;
156
157  bool constructors_called;
158
159  // When you read a virtual address from the ELF file, add this
160  // value to get the corresponding address in the process' address space.
161  ElfW(Addr) load_bias;
162
163#if !defined(__LP64__)
164  bool has_text_relocations;
165#endif
166  bool has_DT_SYMBOLIC;
167
168  void CallConstructors();
169  void CallDestructors();
170  void CallPreInitConstructors();
171
172 private:
173  void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
174  void CallFunction(const char* function_name, linker_function_t function);
175};
176
177extern soinfo libdl_info;
178
179void do_android_get_LD_LIBRARY_PATH(char*, size_t);
180void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
181soinfo* do_dlopen(const char* name, int flags);
182int do_dlclose(soinfo* si);
183
184ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
185soinfo* find_containing_library(const void* addr);
186
187ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
188ElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name);
189
190void debuggerd_init();
191extern "C" abort_msg_t* gAbortMessage;
192extern "C" void notify_gdb_of_libraries();
193
194char* linker_get_error_buffer();
195size_t linker_get_error_buffer_size();
196
197#endif
198