linker.h revision a799b53f10e5a6fd51fef4436cfb7ec99836a516
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 <linux/elf.h>
35
36#undef PAGE_MASK
37#undef PAGE_SIZE
38#define PAGE_SIZE 4096
39#define PAGE_MASK 4095
40
41void debugger_init();
42const char *addr_to_name(unsigned addr);
43
44/* magic shared structures that GDB knows about */
45
46struct link_map
47{
48    uintptr_t l_addr;
49    char * l_name;
50    uintptr_t l_ld;
51    struct link_map * l_next;
52    struct link_map * l_prev;
53};
54
55/* needed for dl_iterate_phdr to be passed to the callbacks provided */
56struct dl_phdr_info
57{
58    Elf32_Addr dlpi_addr;
59    const char *dlpi_name;
60    const Elf32_Phdr *dlpi_phdr;
61    Elf32_Half dlpi_phnum;
62};
63
64
65// Values for r_debug->state
66enum {
67    RT_CONSISTENT,
68    RT_ADD,
69    RT_DELETE
70};
71
72struct r_debug
73{
74    int32_t r_version;
75    struct link_map * r_map;
76    void (*r_brk)(void);
77    int32_t r_state;
78    uintptr_t r_ldbase;
79};
80
81typedef struct soinfo soinfo;
82
83#define FLAG_LINKED     0x00000001
84#define FLAG_ERROR      0x00000002
85#define FLAG_EXE        0x00000004 // The main executable
86#define FLAG_PRELINKED  0x00000008 // This is a pre-linked lib
87
88#define SOINFO_NAME_LEN 128
89
90struct soinfo
91{
92    const char name[SOINFO_NAME_LEN];
93    Elf32_Phdr *phdr;
94    int phnum;
95    unsigned entry;
96    unsigned base;
97    unsigned size;
98
99    unsigned *dynamic;
100
101    unsigned wrprotect_start;
102    unsigned wrprotect_end;
103
104    soinfo *next;
105    unsigned flags;
106
107    const char *strtab;
108    Elf32_Sym *symtab;
109
110    unsigned nbucket;
111    unsigned nchain;
112    unsigned *bucket;
113    unsigned *chain;
114
115    unsigned *plt_got;
116
117    Elf32_Rel *plt_rel;
118    unsigned plt_rel_count;
119
120    Elf32_Rel *rel;
121    unsigned rel_count;
122
123    unsigned *preinit_array;
124    unsigned preinit_array_count;
125
126    unsigned *init_array;
127    unsigned init_array_count;
128    unsigned *fini_array;
129    unsigned fini_array_count;
130
131    void (*init_func)(void);
132    void (*fini_func)(void);
133
134#ifdef ANDROID_ARM_LINKER
135    /* ARM EABI section used for stack unwinding. */
136    unsigned *ARM_exidx;
137    unsigned ARM_exidx_count;
138#endif
139
140    unsigned refcount;
141    struct link_map linkmap;
142};
143
144
145extern soinfo libdl_info;
146
147/* these must all be powers of two */
148#define LIBBASE 0x80000000
149#define LIBLAST 0x90000000
150#define LIBINC  0x00100000
151
152
153#ifdef ANDROID_ARM_LINKER
154
155#define R_ARM_COPY       20
156#define R_ARM_GLOB_DAT   21
157#define R_ARM_JUMP_SLOT  22
158#define R_ARM_RELATIVE   23
159
160#elif defined(ANDROID_X86_LINKER)
161
162#define R_386_32         1
163#define R_386_PC32       2
164#define R_386_GLOB_DAT   6
165#define R_386_JUMP_SLOT  7
166#define R_386_RELATIVE   8
167
168#endif /* ANDROID_*_LINKER */
169
170
171#ifndef DT_INIT_ARRAY
172#define DT_INIT_ARRAY      25
173#endif
174
175#ifndef DT_FINI_ARRAY
176#define DT_FINI_ARRAY      26
177#endif
178
179#ifndef DT_INIT_ARRAYSZ
180#define DT_INIT_ARRAYSZ    27
181#endif
182
183#ifndef DT_FINI_ARRAYSZ
184#define DT_FINI_ARRAYSZ    28
185#endif
186
187#ifndef DT_PREINIT_ARRAY
188#define DT_PREINIT_ARRAY   32
189#endif
190
191#ifndef DT_PREINIT_ARRAYSZ
192#define DT_PREINIT_ARRAYSZ 33
193#endif
194
195/* in theory we only need the above relative relocations,
196   but in practice the following one turns up from time
197   to time.  fushigi na.
198*/
199#define R_ARM_ABS32      2
200
201soinfo *find_library(const char *name);
202unsigned unload_library(soinfo *si);
203Elf32_Sym *lookup_in_library(soinfo *si, const char *name);
204Elf32_Sym *lookup(const char *name, unsigned *base);
205
206#ifdef ANDROID_ARM_LINKER
207typedef long unsigned int *_Unwind_Ptr;
208_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
209#elif defined(ANDROID_X86_LINKER)
210int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, size_t, void *), void *);
211#endif
212
213#endif
214