]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - linker/linker.h
Remove has_DT_SYMBOLIC flag
[android-sdk/platform-bionic.git] / linker / linker.h
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  */
29 #ifndef _LINKER_H_
30 #define _LINKER_H_
32 #include <elf.h>
33 #include <inttypes.h>
34 #include <link.h>
35 #include <unistd.h>
36 #include <android/dlext.h>
37 #include <sys/stat.h>
39 #include "private/libc_logging.h"
40 #include "linked_list.h"
42 #define DL_ERR(fmt, x...) \
43     do { \
44       __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
45       /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
46       DEBUG("%s\n", linker_get_error_buffer()); \
47     } while (false)
49 #define DL_WARN(fmt, x...) \
50     do { \
51       __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
52       __libc_format_fd(2, "WARNING: linker: "); \
53       __libc_format_fd(2, fmt, ##x); \
54       __libc_format_fd(2, "\n"); \
55     } while (false)
57 #if defined(__LP64__)
58 #define ELFW(what) ELF64_ ## what
59 #else
60 #define ELFW(what) ELF32_ ## what
61 #endif
63 // mips64 interprets Elf64_Rel structures' r_info field differently.
64 // bionic (like other C libraries) has macros that assume regular ELF files,
65 // but the dynamic linker needs to be able to load mips64 ELF files.
66 #if defined(__mips__) && defined(__LP64__)
67 #undef ELF64_R_SYM
68 #undef ELF64_R_TYPE
69 #undef ELF64_R_INFO
70 #define ELF64_R_SYM(info)   (((info) >> 0) & 0xffffffff)
71 #define ELF64_R_SSYM(info)  (((info) >> 32) & 0xff)
72 #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
73 #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
74 #define ELF64_R_TYPE(info)  (((info) >> 56) & 0xff)
75 #endif
77 // Returns the address of the page containing address 'x'.
78 #define PAGE_START(x)  ((x) & PAGE_MASK)
80 // Returns the offset of address 'x' in its page.
81 #define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
83 // Returns the address of the next page after address 'x', unless 'x' is
84 // itself at the start of a page.
85 #define PAGE_END(x)    PAGE_START((x) + (PAGE_SIZE-1))
87 #define FLAG_LINKED     0x00000001
88 #define FLAG_EXE        0x00000004 // The main executable
89 #define FLAG_LINKER     0x00000010 // The linker itself
90 #define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
92 #define SOINFO_VERSION 1
94 #define SOINFO_NAME_LEN 128
96 typedef void (*linker_function_t)();
98 // Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
99 #if defined(__aarch64__) || defined(__x86_64__)
100 #define USE_RELA 1
101 #endif
103 struct soinfo;
105 class SoinfoListAllocator {
106 public:
107   static LinkedListEntry<soinfo>* alloc();
108   static void free(LinkedListEntry<soinfo>* entry);
109 private:
110   // unconstructable
111   DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
112 };
114 struct soinfo {
115  public:
116   typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
117  public:
118   char name[SOINFO_NAME_LEN];
119   const ElfW(Phdr)* phdr;
120   size_t phnum;
121   ElfW(Addr) entry;
122   ElfW(Addr) base;
123   size_t size;
125 #ifndef __LP64__
126   uint32_t unused1;  // DO NOT USE, maintained for compatibility.
127 #endif
129   ElfW(Dyn)* dynamic;
131 #ifndef __LP64__
132   uint32_t unused2; // DO NOT USE, maintained for compatibility
133   uint32_t unused3; // DO NOT USE, maintained for compatibility
134 #endif
136   soinfo* next;
137   unsigned flags;
139   const char* strtab;
140   ElfW(Sym)* symtab;
142   size_t nbucket;
143   size_t nchain;
144   unsigned* bucket;
145   unsigned* chain;
147 #if defined(__mips__) || !defined(__LP64__)
148   // This is only used by mips and mips64, but needs to be here for
149   // all 32-bit architectures to preserve binary compatibility.
150   ElfW(Addr)** plt_got;
151 #endif
153 #if defined(USE_RELA)
154   ElfW(Rela)* plt_rela;
155   size_t plt_rela_count;
157   ElfW(Rela)* rela;
158   size_t rela_count;
159 #else
160   ElfW(Rel)* plt_rel;
161   size_t plt_rel_count;
163   ElfW(Rel)* rel;
164   size_t rel_count;
165 #endif
167   linker_function_t* preinit_array;
168   size_t preinit_array_count;
170   linker_function_t* init_array;
171   size_t init_array_count;
172   linker_function_t* fini_array;
173   size_t fini_array_count;
175   linker_function_t init_func;
176   linker_function_t fini_func;
178 #if defined(__arm__)
179   // ARM EABI section used for stack unwinding.
180   unsigned* ARM_exidx;
181   size_t ARM_exidx_count;
182 #elif defined(__mips__)
183   unsigned mips_symtabno;
184   unsigned mips_local_gotno;
185   unsigned mips_gotsym;
186 #endif
188   size_t ref_count;
189   link_map link_map_head;
191   bool constructors_called;
193   // When you read a virtual address from the ELF file, add this
194   // value to get the corresponding address in the process' address space.
195   ElfW(Addr) load_bias;
197 #if !defined(__LP64__)
198   bool has_text_relocations;
199 #endif
200   bool unused4; // DO NOT USE, maintained for compatibility
202   soinfo(const char* name, const struct stat* file_stat, int rtld_flags);
204   void CallConstructors();
205   void CallDestructors();
206   void CallPreInitConstructors();
207   bool PrelinkImage();
208   bool LinkImage(const android_dlextinfo* extinfo);
210   void add_child(soinfo* child);
211   void remove_all_links();
213   void set_st_dev(dev_t st_dev);
214   void set_st_ino(ino_t st_ino);
215   ino_t get_st_ino();
216   dev_t get_st_dev();
218   int get_rtld_flags();
220   soinfo_list_t& get_children();
221   soinfo_list_t& get_parents();
223   ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
225   bool inline has_min_version(uint32_t min_version) {
226     return (flags & FLAG_NEW_SOINFO) != 0 && version >= min_version;
227   }
228  private:
229   void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
230   void CallFunction(const char* function_name, linker_function_t function);
231 #if defined(USE_RELA)
232   int Relocate(ElfW(Rela)* rela, unsigned count);
233 #else
234   int Relocate(ElfW(Rel)* rel, unsigned count);
235 #endif
237  private:
238   // This part of the structure is only available
239   // when FLAG_NEW_SOINFO is set in this->flags.
240   uint32_t version;
242   // version >= 0
243   dev_t st_dev;
244   ino_t st_ino;
246   // dependency graph
247   soinfo_list_t children;
248   soinfo_list_t parents;
250   // version >= 1
251   int rtld_flags;
252 };
254 extern soinfo* get_libdl_info();
256 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
257 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
258 soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
259 void do_dlclose(soinfo* si);
261 ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
262 soinfo* find_containing_library(const void* addr);
264 ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
265 ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
267 void debuggerd_init();
268 extern "C" abort_msg_t* g_abort_message;
269 extern "C" void notify_gdb_of_libraries();
271 char* linker_get_error_buffer();
272 size_t linker_get_error_buffer_size();
274 #endif