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