| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 The Android Open Source Project | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | #include <linux/auxvec.h> | 
|  | 30 |  | 
|  | 31 | #include <stdio.h> | 
|  | 32 | #include <stdlib.h> | 
|  | 33 | #include <string.h> | 
|  | 34 | #include <unistd.h> | 
|  | 35 | #include <fcntl.h> | 
|  | 36 | #include <errno.h> | 
|  | 37 | #include <dlfcn.h> | 
|  | 38 | #include <sys/stat.h> | 
|  | 39 |  | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 40 | #include <pthread.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | #include <sys/mman.h> | 
|  | 43 |  | 
|  | 44 | #include <sys/atomics.h> | 
|  | 45 |  | 
|  | 46 | /* special private C library header - see Android.mk */ | 
|  | 47 | #include <bionic_tls.h> | 
|  | 48 |  | 
|  | 49 | #include "linker.h" | 
|  | 50 | #include "linker_debug.h" | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 51 | #include "linker_environ.h" | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 52 | #include "linker_format.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 |  | 
| Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 54 | #define ALLOW_SYMBOLS_FROM_MAIN 1 | 
| Kenny Root | 72f9a5c | 2011-02-10 17:02:21 -0800 | [diff] [blame] | 55 | #define SO_MAX 128 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 |  | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 57 | /* Assume average path length of 64 and max 8 paths */ | 
|  | 58 | #define LDPATH_BUFSIZE 512 | 
|  | 59 | #define LDPATH_MAX 8 | 
|  | 60 |  | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 61 | #define LDPRELOAD_BUFSIZE 512 | 
|  | 62 | #define LDPRELOAD_MAX 8 | 
|  | 63 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< | 
|  | 65 | * | 
|  | 66 | * Do NOT use malloc() and friends or pthread_*() code here. | 
|  | 67 | * Don't use printf() either; it's caused mysterious memory | 
|  | 68 | * corruption in the past. | 
|  | 69 | * The linker runs before we bring up libc and it's easiest | 
|  | 70 | * to make sure it does not depend on any complex libc features | 
|  | 71 | * | 
|  | 72 | * open issues / todo: | 
|  | 73 | * | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | * - are we doing everything we should for ARM_COPY relocations? | 
|  | 75 | * - cleaner error reporting | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | * - after linking, set as much stuff as possible to READONLY | 
|  | 77 | *   and NOEXEC | 
|  | 78 | * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel | 
|  | 79 | *   headers provide versions that are negative... | 
|  | 80 | * - allocate space for soinfo structs dynamically instead of | 
|  | 81 | *   having a hard limit (64) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | */ | 
|  | 83 |  | 
|  | 84 |  | 
|  | 85 | static int link_image(soinfo *si, unsigned wr_offset); | 
|  | 86 |  | 
|  | 87 | static int socount = 0; | 
|  | 88 | static soinfo sopool[SO_MAX]; | 
|  | 89 | static soinfo *freelist = NULL; | 
|  | 90 | static soinfo *solist = &libdl_info; | 
|  | 91 | static soinfo *sonext = &libdl_info; | 
| Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 92 | #if ALLOW_SYMBOLS_FROM_MAIN | 
|  | 93 | static soinfo *somain; /* main process, always the one after libdl_info */ | 
|  | 94 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 |  | 
| Iliyan Malchev | af7315a | 2009-10-16 17:50:42 -0700 | [diff] [blame] | 96 |  | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 97 | static inline int validate_soinfo(soinfo *si) | 
|  | 98 | { | 
|  | 99 | return (si >= sopool && si < sopool + SO_MAX) || | 
|  | 100 | si == &libdl_info; | 
|  | 101 | } | 
|  | 102 |  | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 103 | static char ldpaths_buf[LDPATH_BUFSIZE]; | 
|  | 104 | static const char *ldpaths[LDPATH_MAX + 1]; | 
|  | 105 |  | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 106 | static char ldpreloads_buf[LDPRELOAD_BUFSIZE]; | 
|  | 107 | static const char *ldpreload_names[LDPRELOAD_MAX + 1]; | 
|  | 108 |  | 
|  | 109 | static soinfo *preloads[LDPRELOAD_MAX + 1]; | 
|  | 110 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | int debug_verbosity; | 
|  | 112 | static int pid; | 
|  | 113 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 114 | /* This boolean is set if the program being loaded is setuid */ | 
|  | 115 | static int program_is_setuid; | 
|  | 116 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | #if STATS | 
|  | 118 | struct _link_stats linker_stats; | 
|  | 119 | #endif | 
|  | 120 |  | 
|  | 121 | #if COUNT_PAGES | 
|  | 122 | unsigned bitmask[4096]; | 
|  | 123 | #endif | 
|  | 124 |  | 
|  | 125 | #ifndef PT_ARM_EXIDX | 
|  | 126 | #define PT_ARM_EXIDX    0x70000001      /* .ARM.exidx segment */ | 
|  | 127 | #endif | 
|  | 128 |  | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 129 | #define HOODLUM(name, ret, ...)                                               \ | 
|  | 130 | ret name __VA_ARGS__                                                      \ | 
|  | 131 | {                                                                         \ | 
|  | 132 | char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \ | 
|  | 133 | write(2, errstr, sizeof(errstr));                                     \ | 
|  | 134 | abort();                                                              \ | 
|  | 135 | } | 
|  | 136 | HOODLUM(malloc, void *, (size_t size)); | 
|  | 137 | HOODLUM(free, void, (void *ptr)); | 
|  | 138 | HOODLUM(realloc, void *, (void *ptr, size_t size)); | 
|  | 139 | HOODLUM(calloc, void *, (size_t cnt, size_t size)); | 
|  | 140 |  | 
| Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 141 | static char tmp_err_buf[768]; | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 142 | static char __linker_dl_err_buf[768]; | 
|  | 143 | #define DL_ERR(fmt, x...)                                                     \ | 
|  | 144 | do {                                                                      \ | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 145 | format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf),            \ | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 146 | "%s[%d]: " fmt, __func__, __LINE__, ##x);                    \ | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 147 | ERROR(fmt "\n", ##x);                                                      \ | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 148 | } while(0) | 
|  | 149 |  | 
|  | 150 | const char *linker_get_error(void) | 
|  | 151 | { | 
|  | 152 | return (const char *)&__linker_dl_err_buf[0]; | 
|  | 153 | } | 
|  | 154 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | /* | 
|  | 156 | * This function is an empty stub where GDB locates a breakpoint to get notified | 
|  | 157 | * about linker activity. | 
|  | 158 | */ | 
|  | 159 | extern void __attribute__((noinline)) rtld_db_dlactivity(void); | 
|  | 160 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, | 
|  | 162 | RT_CONSISTENT, 0}; | 
|  | 163 | static struct link_map *r_debug_tail = 0; | 
|  | 164 |  | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 165 | static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 |  | 
|  | 167 | static void insert_soinfo_into_debug_map(soinfo * info) | 
|  | 168 | { | 
|  | 169 | struct link_map * map; | 
|  | 170 |  | 
|  | 171 | /* Copy the necessary fields into the debug structure. | 
|  | 172 | */ | 
|  | 173 | map = &(info->linkmap); | 
|  | 174 | map->l_addr = info->base; | 
|  | 175 | map->l_name = (char*) info->name; | 
| Thinker K.F Li | 5cf640c | 2009-07-03 19:40:32 +0800 | [diff] [blame] | 176 | map->l_ld = (uintptr_t)info->dynamic; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 177 |  | 
|  | 178 | /* Stick the new library at the end of the list. | 
|  | 179 | * gdb tends to care more about libc than it does | 
|  | 180 | * about leaf libraries, and ordering it this way | 
|  | 181 | * reduces the back-and-forth over the wire. | 
|  | 182 | */ | 
|  | 183 | if (r_debug_tail) { | 
|  | 184 | r_debug_tail->l_next = map; | 
|  | 185 | map->l_prev = r_debug_tail; | 
|  | 186 | map->l_next = 0; | 
|  | 187 | } else { | 
|  | 188 | _r_debug.r_map = map; | 
|  | 189 | map->l_prev = 0; | 
|  | 190 | map->l_next = 0; | 
|  | 191 | } | 
|  | 192 | r_debug_tail = map; | 
|  | 193 | } | 
|  | 194 |  | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 195 | static void remove_soinfo_from_debug_map(soinfo * info) | 
|  | 196 | { | 
|  | 197 | struct link_map * map = &(info->linkmap); | 
|  | 198 |  | 
|  | 199 | if (r_debug_tail == map) | 
|  | 200 | r_debug_tail = map->l_prev; | 
|  | 201 |  | 
|  | 202 | if (map->l_prev) map->l_prev->l_next = map->l_next; | 
|  | 203 | if (map->l_next) map->l_next->l_prev = map->l_prev; | 
|  | 204 | } | 
|  | 205 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | void notify_gdb_of_load(soinfo * info) | 
|  | 207 | { | 
|  | 208 | if (info->flags & FLAG_EXE) { | 
|  | 209 | // GDB already knows about the main executable | 
|  | 210 | return; | 
|  | 211 | } | 
|  | 212 |  | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 213 | pthread_mutex_lock(&_r_debug_lock); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 214 |  | 
|  | 215 | _r_debug.r_state = RT_ADD; | 
|  | 216 | rtld_db_dlactivity(); | 
|  | 217 |  | 
|  | 218 | insert_soinfo_into_debug_map(info); | 
|  | 219 |  | 
|  | 220 | _r_debug.r_state = RT_CONSISTENT; | 
|  | 221 | rtld_db_dlactivity(); | 
|  | 222 |  | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 223 | pthread_mutex_unlock(&_r_debug_lock); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | void notify_gdb_of_unload(soinfo * info) | 
|  | 227 | { | 
|  | 228 | if (info->flags & FLAG_EXE) { | 
|  | 229 | // GDB already knows about the main executable | 
|  | 230 | return; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | pthread_mutex_lock(&_r_debug_lock); | 
|  | 234 |  | 
|  | 235 | _r_debug.r_state = RT_DELETE; | 
|  | 236 | rtld_db_dlactivity(); | 
|  | 237 |  | 
|  | 238 | remove_soinfo_from_debug_map(info); | 
|  | 239 |  | 
|  | 240 | _r_debug.r_state = RT_CONSISTENT; | 
|  | 241 | rtld_db_dlactivity(); | 
|  | 242 |  | 
|  | 243 | pthread_mutex_unlock(&_r_debug_lock); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
|  | 246 | void notify_gdb_of_libraries() | 
|  | 247 | { | 
|  | 248 | _r_debug.r_state = RT_ADD; | 
|  | 249 | rtld_db_dlactivity(); | 
|  | 250 | _r_debug.r_state = RT_CONSISTENT; | 
|  | 251 | rtld_db_dlactivity(); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | static soinfo *alloc_info(const char *name) | 
|  | 255 | { | 
|  | 256 | soinfo *si; | 
|  | 257 |  | 
|  | 258 | if(strlen(name) >= SOINFO_NAME_LEN) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 259 | DL_ERR("%5d library name %s too long", pid, name); | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 260 | return NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
|  | 263 | /* The freelist is populated when we call free_info(), which in turn is | 
|  | 264 | done only by dlclose(), which is not likely to be used. | 
|  | 265 | */ | 
|  | 266 | if (!freelist) { | 
|  | 267 | if(socount == SO_MAX) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 268 | DL_ERR("%5d too many libraries when loading %s", pid, name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 269 | return NULL; | 
|  | 270 | } | 
|  | 271 | freelist = sopool + socount++; | 
|  | 272 | freelist->next = NULL; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | si = freelist; | 
|  | 276 | freelist = freelist->next; | 
|  | 277 |  | 
|  | 278 | /* Make sure we get a clean block of soinfo */ | 
|  | 279 | memset(si, 0, sizeof(soinfo)); | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 280 | strlcpy((char*) si->name, name, sizeof(si->name)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 281 | sonext->next = si; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 282 | si->next = NULL; | 
|  | 283 | si->refcount = 0; | 
|  | 284 | sonext = si; | 
|  | 285 |  | 
|  | 286 | TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si); | 
|  | 287 | return si; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | static void free_info(soinfo *si) | 
|  | 291 | { | 
|  | 292 | soinfo *prev = NULL, *trav; | 
|  | 293 |  | 
|  | 294 | TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si); | 
|  | 295 |  | 
|  | 296 | for(trav = solist; trav != NULL; trav = trav->next){ | 
|  | 297 | if (trav == si) | 
|  | 298 | break; | 
|  | 299 | prev = trav; | 
|  | 300 | } | 
|  | 301 | if (trav == NULL) { | 
|  | 302 | /* si was not ni solist */ | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 303 | DL_ERR("%5d name %s is not in solist!", pid, si->name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 304 | return; | 
|  | 305 | } | 
|  | 306 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 307 | /* prev will never be NULL, because the first entry in solist is | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 308 | always the static libdl_info. | 
|  | 309 | */ | 
|  | 310 | prev->next = si->next; | 
|  | 311 | if (si == sonext) sonext = prev; | 
|  | 312 | si->next = freelist; | 
|  | 313 | freelist = si; | 
|  | 314 | } | 
|  | 315 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 316 | const char *addr_to_name(unsigned addr) | 
|  | 317 | { | 
|  | 318 | soinfo *si; | 
|  | 319 |  | 
|  | 320 | for(si = solist; si != 0; si = si->next){ | 
|  | 321 | if((addr >= si->base) && (addr < (si->base + si->size))) { | 
|  | 322 | return si->name; | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | return ""; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | /* For a given PC, find the .so that it belongs to. | 
|  | 330 | * Returns the base address of the .ARM.exidx section | 
|  | 331 | * for that .so, and the number of 8-byte entries | 
|  | 332 | * in that section (via *pcount). | 
|  | 333 | * | 
|  | 334 | * Intended to be called by libc's __gnu_Unwind_Find_exidx(). | 
|  | 335 | * | 
|  | 336 | * This function is exposed via dlfcn.c and libdl.so. | 
|  | 337 | */ | 
|  | 338 | #ifdef ANDROID_ARM_LINKER | 
|  | 339 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount) | 
|  | 340 | { | 
|  | 341 | soinfo *si; | 
|  | 342 | unsigned addr = (unsigned)pc; | 
|  | 343 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 344 | for (si = solist; si != 0; si = si->next){ | 
|  | 345 | if ((addr >= si->base) && (addr < (si->base + si->size))) { | 
|  | 346 | *pcount = si->ARM_exidx_count; | 
|  | 347 | return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
|  | 350 | *pcount = 0; | 
|  | 351 | return NULL; | 
|  | 352 | } | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 353 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_LINKER) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 354 | /* Here, we only have to provide a callback to iterate across all the | 
|  | 355 | * loaded libraries. gcc_eh does the rest. */ | 
|  | 356 | int | 
|  | 357 | dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data), | 
|  | 358 | void *data) | 
|  | 359 | { | 
|  | 360 | soinfo *si; | 
|  | 361 | struct dl_phdr_info dl_info; | 
|  | 362 | int rv = 0; | 
|  | 363 |  | 
|  | 364 | for (si = solist; si != NULL; si = si->next) { | 
|  | 365 | dl_info.dlpi_addr = si->linkmap.l_addr; | 
|  | 366 | dl_info.dlpi_name = si->linkmap.l_name; | 
|  | 367 | dl_info.dlpi_phdr = si->phdr; | 
|  | 368 | dl_info.dlpi_phnum = si->phnum; | 
|  | 369 | rv = cb(&dl_info, sizeof (struct dl_phdr_info), data); | 
|  | 370 | if (rv != 0) | 
|  | 371 | break; | 
|  | 372 | } | 
|  | 373 | return rv; | 
|  | 374 | } | 
|  | 375 | #endif | 
|  | 376 |  | 
|  | 377 | static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name) | 
|  | 378 | { | 
|  | 379 | Elf32_Sym *s; | 
|  | 380 | Elf32_Sym *symtab = si->symtab; | 
|  | 381 | const char *strtab = si->strtab; | 
|  | 382 | unsigned n; | 
|  | 383 |  | 
|  | 384 | TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid, | 
|  | 385 | name, si->name, si->base, hash, hash % si->nbucket); | 
|  | 386 | n = hash % si->nbucket; | 
|  | 387 |  | 
|  | 388 | for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){ | 
|  | 389 | s = symtab + n; | 
|  | 390 | if(strcmp(strtab + s->st_name, name)) continue; | 
|  | 391 |  | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 392 | /* only concern ourselves with global and weak symbol definitions */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 393 | switch(ELF32_ST_BIND(s->st_info)){ | 
|  | 394 | case STB_GLOBAL: | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 395 | case STB_WEAK: | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 396 | /* no section == undefined */ | 
|  | 397 | if(s->st_shndx == 0) continue; | 
|  | 398 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 399 | TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid, | 
|  | 400 | name, si->name, s->st_value, s->st_size); | 
|  | 401 | return s; | 
|  | 402 | } | 
|  | 403 | } | 
|  | 404 |  | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 405 | return NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 408 | /* | 
|  | 409 | * Essentially the same method as _elf_lookup() above, but only | 
|  | 410 | * searches for LOCAL symbols | 
|  | 411 | */ | 
|  | 412 | static Elf32_Sym *_elf_lookup_local(soinfo *si, unsigned hash, const char *name) | 
|  | 413 | { | 
|  | 414 | Elf32_Sym *symtab = si->symtab; | 
|  | 415 | const char *strtab = si->strtab; | 
|  | 416 | unsigned n = hash % si->nbucket;; | 
|  | 417 |  | 
|  | 418 | TRACE_TYPE(LOOKUP, "%5d LOCAL SEARCH %s in %s@0x%08x %08x %d\n", pid, | 
|  | 419 | name, si->name, si->base, hash, hash % si->nbucket); | 
|  | 420 | for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){ | 
|  | 421 | Elf32_Sym *s = symtab + n; | 
|  | 422 | if (strcmp(strtab + s->st_name, name)) continue; | 
|  | 423 | if (ELF32_ST_BIND(s->st_info) != STB_LOCAL) continue; | 
|  | 424 | /* no section == undefined */ | 
|  | 425 | if(s->st_shndx == 0) continue; | 
|  | 426 |  | 
|  | 427 | TRACE_TYPE(LOOKUP, "%5d FOUND LOCAL %s in %s (%08x) %d\n", pid, | 
|  | 428 | name, si->name, s->st_value, s->st_size); | 
|  | 429 | return s; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | return NULL; | 
|  | 433 | } | 
|  | 434 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 435 | static unsigned elfhash(const char *_name) | 
|  | 436 | { | 
|  | 437 | const unsigned char *name = (const unsigned char *) _name; | 
|  | 438 | unsigned h = 0, g; | 
|  | 439 |  | 
|  | 440 | while(*name) { | 
|  | 441 | h = (h << 4) + *name++; | 
|  | 442 | g = h & 0xf0000000; | 
|  | 443 | h ^= g; | 
|  | 444 | h ^= g >> 24; | 
|  | 445 | } | 
|  | 446 | return h; | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | static Elf32_Sym * | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 450 | _do_lookup(soinfo *si, const char *name, unsigned *base) | 
|  | 451 | { | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 452 | unsigned elf_hash = elfhash(name); | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 453 | Elf32_Sym *s; | 
|  | 454 | unsigned *d; | 
|  | 455 | soinfo *lsi = si; | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 456 | int i; | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 457 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 458 | /* If we are trying to find a symbol for the linker itself, look | 
|  | 459 | * for LOCAL symbols first. Avoid using LOCAL symbols for other | 
|  | 460 | * shared libraries until we have a better understanding of what | 
|  | 461 | * might break by doing so. */ | 
|  | 462 | if (si->flags & FLAG_LINKER) { | 
|  | 463 | s = _elf_lookup_local(si, elf_hash, name); | 
|  | 464 | if(s != NULL) | 
|  | 465 | goto done; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | /* Look for symbols in the local scope (the object who is | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 469 | * searching). This happens with C++ templates on i386 for some | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 470 | * reason. | 
|  | 471 | * | 
|  | 472 | * Notes on weak symbols: | 
|  | 473 | * The ELF specs are ambigious about treatment of weak definitions in | 
|  | 474 | * dynamic linking.  Some systems return the first definition found | 
|  | 475 | * and some the first non-weak definition.   This is system dependent. | 
|  | 476 | * Here we return the first definition found for simplicity.  */ | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 477 |  | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 478 | s = _elf_lookup(si, elf_hash, name); | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 479 | if(s != NULL) | 
|  | 480 | goto done; | 
|  | 481 |  | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 482 | /* Next, look for it in the preloads list */ | 
|  | 483 | for(i = 0; preloads[i] != NULL; i++) { | 
|  | 484 | lsi = preloads[i]; | 
| Jean-Baptiste Queru | f439445 | 2010-05-12 10:05:59 -0700 | [diff] [blame] | 485 | s = _elf_lookup(lsi, elf_hash, name); | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 486 | if(s != NULL) | 
|  | 487 | goto done; | 
|  | 488 | } | 
|  | 489 |  | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 490 | for(d = si->dynamic; *d; d += 2) { | 
|  | 491 | if(d[0] == DT_NEEDED){ | 
|  | 492 | lsi = (soinfo *)d[1]; | 
|  | 493 | if (!validate_soinfo(lsi)) { | 
|  | 494 | DL_ERR("%5d bad DT_NEEDED pointer in %s", | 
|  | 495 | pid, si->name); | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 496 | return NULL; | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 497 | } | 
|  | 498 |  | 
|  | 499 | DEBUG("%5d %s: looking up %s in %s\n", | 
|  | 500 | pid, si->name, name, lsi->name); | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 501 | s = _elf_lookup(lsi, elf_hash, name); | 
| Min-su, Kim | 3cab22c | 2010-01-19 10:05:33 +0900 | [diff] [blame] | 502 | if ((s != NULL) && (s->st_shndx != SHN_UNDEF)) | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 503 | goto done; | 
|  | 504 | } | 
|  | 505 | } | 
|  | 506 |  | 
| Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 507 | #if ALLOW_SYMBOLS_FROM_MAIN | 
|  | 508 | /* If we are resolving relocations while dlopen()ing a library, it's OK for | 
|  | 509 | * the library to resolve a symbol that's defined in the executable itself, | 
|  | 510 | * although this is rare and is generally a bad idea. | 
|  | 511 | */ | 
|  | 512 | if (somain) { | 
|  | 513 | lsi = somain; | 
|  | 514 | DEBUG("%5d %s: looking up %s in executable %s\n", | 
|  | 515 | pid, si->name, name, lsi->name); | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 516 | s = _elf_lookup(lsi, elf_hash, name); | 
| Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 517 | } | 
|  | 518 | #endif | 
|  | 519 |  | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 520 | done: | 
|  | 521 | if(s != NULL) { | 
|  | 522 | TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, " | 
|  | 523 | "found in %s, base = 0x%08x\n", | 
|  | 524 | pid, si->name, name, s->st_value, lsi->name, lsi->base); | 
|  | 525 | *base = lsi->base; | 
|  | 526 | return s; | 
|  | 527 | } | 
|  | 528 |  | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 529 | return NULL; | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | /* This is used by dl_sym().  It performs symbol lookup only within the | 
|  | 533 | specified soinfo object and not in any of its dependencies. | 
|  | 534 | */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 535 | Elf32_Sym *lookup_in_library(soinfo *si, const char *name) | 
|  | 536 | { | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 537 | return _elf_lookup(si, elfhash(name), name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 538 | } | 
|  | 539 |  | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 540 | /* This is used by dl_sym().  It performs a global symbol lookup. | 
|  | 541 | */ | 
| Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 542 | Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 543 | { | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 544 | unsigned elf_hash = elfhash(name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 545 | Elf32_Sym *s = NULL; | 
|  | 546 | soinfo *si; | 
|  | 547 |  | 
| Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 548 | if(start == NULL) { | 
|  | 549 | start = solist; | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | for(si = start; (s == NULL) && (si != NULL); si = si->next) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 553 | { | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 554 | if(si->flags & FLAG_ERROR) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 555 | continue; | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 556 | s = _elf_lookup(si, elf_hash, name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 557 | if (s != NULL) { | 
| Iliyan Malchev | 9ea64da | 2009-09-28 18:21:30 -0700 | [diff] [blame] | 558 | *found = si; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 559 | break; | 
|  | 560 | } | 
|  | 561 | } | 
|  | 562 |  | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 563 | if(s != NULL) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 564 | TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, " | 
|  | 565 | "si->base = 0x%08x\n", pid, name, s->st_value, si->base); | 
|  | 566 | return s; | 
|  | 567 | } | 
|  | 568 |  | 
| Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 569 | return NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 570 | } | 
|  | 571 |  | 
| Mathias Agopian | bda5da0 | 2011-09-27 22:30:19 -0700 | [diff] [blame] | 572 | soinfo *find_containing_library(const void *addr) | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 573 | { | 
|  | 574 | soinfo *si; | 
|  | 575 |  | 
|  | 576 | for(si = solist; si != NULL; si = si->next) | 
|  | 577 | { | 
|  | 578 | if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) { | 
|  | 579 | return si; | 
|  | 580 | } | 
|  | 581 | } | 
|  | 582 |  | 
|  | 583 | return NULL; | 
|  | 584 | } | 
|  | 585 |  | 
| Mathias Agopian | bda5da0 | 2011-09-27 22:30:19 -0700 | [diff] [blame] | 586 | Elf32_Sym *find_containing_symbol(const void *addr, soinfo *si) | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 587 | { | 
|  | 588 | unsigned int i; | 
|  | 589 | unsigned soaddr = (unsigned)addr - si->base; | 
|  | 590 |  | 
|  | 591 | /* Search the library's symbol table for any defined symbol which | 
|  | 592 | * contains this address */ | 
|  | 593 | for(i=0; i<si->nchain; i++) { | 
|  | 594 | Elf32_Sym *sym = &si->symtab[i]; | 
|  | 595 |  | 
|  | 596 | if(sym->st_shndx != SHN_UNDEF && | 
|  | 597 | soaddr >= sym->st_value && | 
|  | 598 | soaddr < sym->st_value + sym->st_size) { | 
|  | 599 | return sym; | 
|  | 600 | } | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | return NULL; | 
|  | 604 | } | 
|  | 605 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 606 | #if 0 | 
|  | 607 | static void dump(soinfo *si) | 
|  | 608 | { | 
|  | 609 | Elf32_Sym *s = si->symtab; | 
|  | 610 | unsigned n; | 
|  | 611 |  | 
|  | 612 | for(n = 0; n < si->nchain; n++) { | 
|  | 613 | TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s, | 
|  | 614 | s->st_info, s->st_shndx, s->st_value, s->st_size, | 
|  | 615 | si->strtab + s->st_name); | 
|  | 616 | s++; | 
|  | 617 | } | 
|  | 618 | } | 
|  | 619 | #endif | 
|  | 620 |  | 
|  | 621 | static const char *sopaths[] = { | 
| Brian Swetland | fedbcde | 2010-09-19 03:39:13 -0700 | [diff] [blame] | 622 | "/vendor/lib", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 623 | "/system/lib", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 624 | 0 | 
|  | 625 | }; | 
|  | 626 |  | 
|  | 627 | static int _open_lib(const char *name) | 
|  | 628 | { | 
|  | 629 | int fd; | 
|  | 630 | struct stat filestat; | 
|  | 631 |  | 
|  | 632 | if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) { | 
|  | 633 | if ((fd = open(name, O_RDONLY)) >= 0) | 
|  | 634 | return fd; | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | return -1; | 
|  | 638 | } | 
|  | 639 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 640 | static int open_library(const char *name) | 
|  | 641 | { | 
|  | 642 | int fd; | 
|  | 643 | char buf[512]; | 
|  | 644 | const char **path; | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 645 | int n; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 646 |  | 
|  | 647 | TRACE("[ %5d opening %s ]\n", pid, name); | 
|  | 648 |  | 
|  | 649 | if(name == 0) return -1; | 
|  | 650 | if(strlen(name) > 256) return -1; | 
|  | 651 |  | 
|  | 652 | if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0)) | 
|  | 653 | return fd; | 
|  | 654 |  | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 655 | for (path = ldpaths; *path; path++) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 656 | n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name); | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 657 | if (n < 0 || n >= (int)sizeof(buf)) { | 
|  | 658 | WARN("Ignoring very long library path: %s/%s\n", *path, name); | 
|  | 659 | continue; | 
|  | 660 | } | 
|  | 661 | if ((fd = _open_lib(buf)) >= 0) | 
|  | 662 | return fd; | 
|  | 663 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 664 | for (path = sopaths; *path; path++) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 665 | n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name); | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 666 | if (n < 0 || n >= (int)sizeof(buf)) { | 
|  | 667 | WARN("Ignoring very long library path: %s/%s\n", *path, name); | 
|  | 668 | continue; | 
|  | 669 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 670 | if ((fd = _open_lib(buf)) >= 0) | 
|  | 671 | return fd; | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | return -1; | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | /* temporary space for holding the first page of the shared lib | 
|  | 678 | * which contains the elf header (with the pht). */ | 
|  | 679 | static unsigned char __header[PAGE_SIZE]; | 
|  | 680 |  | 
|  | 681 | typedef struct { | 
|  | 682 | long mmap_addr; | 
|  | 683 | char tag[4]; /* 'P', 'R', 'E', ' ' */ | 
|  | 684 | } prelink_info_t; | 
|  | 685 |  | 
|  | 686 | /* Returns the requested base address if the library is prelinked, | 
|  | 687 | * and 0 otherwise.  */ | 
|  | 688 | static unsigned long | 
|  | 689 | is_prelinked(int fd, const char *name) | 
|  | 690 | { | 
|  | 691 | off_t sz; | 
|  | 692 | prelink_info_t info; | 
|  | 693 |  | 
|  | 694 | sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END); | 
|  | 695 | if (sz < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 696 | DL_ERR("lseek() failed!"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 697 | return 0; | 
|  | 698 | } | 
|  | 699 |  | 
|  | 700 | if (read(fd, &info, sizeof(info)) != sizeof(info)) { | 
|  | 701 | WARN("Could not read prelink_info_t structure for `%s`\n", name); | 
|  | 702 | return 0; | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | if (strncmp(info.tag, "PRE ", 4)) { | 
|  | 706 | WARN("`%s` is not a prelinked library\n", name); | 
|  | 707 | return 0; | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | return (unsigned long)info.mmap_addr; | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | /* verify_elf_object | 
|  | 714 | *      Verifies if the object @ base is a valid ELF object | 
|  | 715 | * | 
|  | 716 | * Args: | 
|  | 717 | * | 
|  | 718 | * Returns: | 
|  | 719 | *       0 on success | 
|  | 720 | *      -1 if no valid ELF object is found @ base. | 
|  | 721 | */ | 
|  | 722 | static int | 
|  | 723 | verify_elf_object(void *base, const char *name) | 
|  | 724 | { | 
|  | 725 | Elf32_Ehdr *hdr = (Elf32_Ehdr *) base; | 
|  | 726 |  | 
|  | 727 | if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1; | 
|  | 728 | if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1; | 
|  | 729 | if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1; | 
|  | 730 | if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1; | 
|  | 731 |  | 
|  | 732 | /* TODO: Should we verify anything else in the header? */ | 
| Zhenghua Wang | 897815a | 2011-10-19 00:29:14 +0800 | [diff] [blame] | 733 | #ifdef ANDROID_ARM_LINKER | 
|  | 734 | if (hdr->e_machine != EM_ARM) return -1; | 
|  | 735 | #elif defined(ANDROID_X86_LINKER) | 
|  | 736 | if (hdr->e_machine != EM_386) return -1; | 
|  | 737 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 738 | return 0; | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 |  | 
|  | 742 | /* get_lib_extents | 
|  | 743 | *      Retrieves the base (*base) address where the ELF object should be | 
|  | 744 | *      mapped and its overall memory size (*total_sz). | 
|  | 745 | * | 
|  | 746 | * Args: | 
|  | 747 | *      fd: Opened file descriptor for the library | 
|  | 748 | *      name: The name of the library | 
|  | 749 | *      _hdr: Pointer to the header page of the library | 
|  | 750 | *      total_sz: Total size of the memory that should be allocated for | 
|  | 751 | *                this library | 
|  | 752 | * | 
|  | 753 | * Returns: | 
|  | 754 | *      -1 if there was an error while trying to get the lib extents. | 
|  | 755 | *         The possible reasons are: | 
|  | 756 | *             - Could not determine if the library was prelinked. | 
|  | 757 | *             - The library provided is not a valid ELF object | 
|  | 758 | *       0 if the library did not request a specific base offset (normal | 
|  | 759 | *         for non-prelinked libs) | 
|  | 760 | *     > 0 if the library requests a specific address to be mapped to. | 
|  | 761 | *         This indicates a pre-linked library. | 
|  | 762 | */ | 
|  | 763 | static unsigned | 
|  | 764 | get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz) | 
|  | 765 | { | 
|  | 766 | unsigned req_base; | 
|  | 767 | unsigned min_vaddr = 0xffffffff; | 
|  | 768 | unsigned max_vaddr = 0; | 
|  | 769 | unsigned char *_hdr = (unsigned char *)__hdr; | 
|  | 770 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr; | 
|  | 771 | Elf32_Phdr *phdr; | 
|  | 772 | int cnt; | 
|  | 773 |  | 
|  | 774 | TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name); | 
|  | 775 | if (verify_elf_object(_hdr, name) < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 776 | DL_ERR("%5d - %s is not a valid ELF object", pid, name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 777 | return (unsigned)-1; | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | req_base = (unsigned) is_prelinked(fd, name); | 
|  | 781 | if (req_base == (unsigned)-1) | 
|  | 782 | return -1; | 
|  | 783 | else if (req_base != 0) { | 
|  | 784 | TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n", | 
|  | 785 | pid, name, req_base); | 
|  | 786 | } else { | 
|  | 787 | TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name); | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff); | 
|  | 791 |  | 
|  | 792 | /* find the min/max p_vaddrs from all the PT_LOAD segments so we can | 
|  | 793 | * get the range. */ | 
|  | 794 | for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) { | 
|  | 795 | if (phdr->p_type == PT_LOAD) { | 
|  | 796 | if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr) | 
|  | 797 | max_vaddr = phdr->p_vaddr + phdr->p_memsz; | 
|  | 798 | if (phdr->p_vaddr < min_vaddr) | 
|  | 799 | min_vaddr = phdr->p_vaddr; | 
|  | 800 | } | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 804 | DL_ERR("%5d - No loadable segments found in %s.", pid, name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 805 | return (unsigned)-1; | 
|  | 806 | } | 
|  | 807 |  | 
|  | 808 | /* truncate min_vaddr down to page boundary */ | 
|  | 809 | min_vaddr &= ~PAGE_MASK; | 
|  | 810 |  | 
|  | 811 | /* round max_vaddr up to the next page */ | 
|  | 812 | max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK; | 
|  | 813 |  | 
|  | 814 | *total_sz = (max_vaddr - min_vaddr); | 
|  | 815 | return (unsigned)req_base; | 
|  | 816 | } | 
|  | 817 |  | 
|  | 818 | /* alloc_mem_region | 
|  | 819 | * | 
|  | 820 | *     This function reserves a chunk of memory to be used for mapping in | 
|  | 821 | *     the shared library. We reserve the entire memory region here, and | 
|  | 822 | *     then the rest of the linker will relocate the individual loadable | 
|  | 823 | *     segments into the correct locations within this memory range. | 
|  | 824 | * | 
|  | 825 | * Args: | 
|  | 826 | *     si->base: The requested base of the allocation. If 0, a sane one will be | 
|  | 827 | *               chosen in the range LIBBASE <= base < LIBLAST. | 
|  | 828 | *     si->size: The size of the allocation. | 
|  | 829 | * | 
|  | 830 | * Returns: | 
|  | 831 | *     -1 on failure, and 0 on success.  On success, si->base will contain | 
|  | 832 | *     the virtual address at which the library will be mapped. | 
|  | 833 | */ | 
|  | 834 |  | 
|  | 835 | static int reserve_mem_region(soinfo *si) | 
|  | 836 | { | 
|  | 837 | void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC, | 
| Chris Dearman | db4bce0 | 2011-03-10 10:48:14 -0800 | [diff] [blame] | 838 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 839 | if (base == MAP_FAILED) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 840 | DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x " | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 841 | "as requested, will try general pool: %d (%s)", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 842 | pid, (si->base ? "" : "non-"), si->name, si->base, | 
|  | 843 | errno, strerror(errno)); | 
|  | 844 | return -1; | 
|  | 845 | } else if (base != (void *)si->base) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 846 | DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, " | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 847 | "not at 0x%08x", pid, (si->base ? "" : "non-"), | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 848 | si->name, (unsigned)base, si->base); | 
|  | 849 | munmap(base, si->size); | 
|  | 850 | return -1; | 
|  | 851 | } | 
|  | 852 | return 0; | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | static int | 
|  | 856 | alloc_mem_region(soinfo *si) | 
|  | 857 | { | 
|  | 858 | if (si->base) { | 
|  | 859 | /* Attempt to mmap a prelinked library. */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 860 | return reserve_mem_region(si); | 
|  | 861 | } | 
|  | 862 |  | 
| Shih-wei Liao | 48527c3 | 2011-07-17 12:32:43 -0700 | [diff] [blame] | 863 | /* This is not a prelinked library, so we use the kernel's default | 
|  | 864 | allocator. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 865 | */ | 
| Shih-wei Liao | 48527c3 | 2011-07-17 12:32:43 -0700 | [diff] [blame] | 866 |  | 
|  | 867 | void *base = mmap(NULL, si->size, PROT_READ | PROT_EXEC, | 
|  | 868 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 
|  | 869 | if (base == MAP_FAILED) { | 
|  | 870 | DL_ERR("%5d mmap of library '%s' failed: %d (%s)\n", | 
|  | 871 | pid, si->name, | 
|  | 872 | errno, strerror(errno)); | 
|  | 873 | goto err; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 874 | } | 
| Shih-wei Liao | 48527c3 | 2011-07-17 12:32:43 -0700 | [diff] [blame] | 875 | si->base = (unsigned) base; | 
|  | 876 | PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n", | 
|  | 877 | pid, si->name, si->base); | 
|  | 878 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 879 |  | 
|  | 880 | err: | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 881 | DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 882 | pid, si->name); | 
|  | 883 | return -1; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | #define MAYBE_MAP_FLAG(x,from,to)    (((x) & (from)) ? (to) : 0) | 
|  | 887 | #define PFLAGS_TO_PROT(x)            (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \ | 
|  | 888 | MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \ | 
|  | 889 | MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE)) | 
|  | 890 | /* load_segments | 
|  | 891 | * | 
|  | 892 | *     This function loads all the loadable (PT_LOAD) segments into memory | 
|  | 893 | *     at their appropriate memory offsets off the base address. | 
|  | 894 | * | 
|  | 895 | * Args: | 
|  | 896 | *     fd: Open file descriptor to the library to load. | 
|  | 897 | *     header: Pointer to a header page that contains the ELF header. | 
|  | 898 | *             This is needed since we haven't mapped in the real file yet. | 
|  | 899 | *     si: ptr to soinfo struct describing the shared object. | 
|  | 900 | * | 
|  | 901 | * Returns: | 
|  | 902 | *     0 on success, -1 on failure. | 
|  | 903 | */ | 
|  | 904 | static int | 
|  | 905 | load_segments(int fd, void *header, soinfo *si) | 
|  | 906 | { | 
|  | 907 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header; | 
|  | 908 | Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff); | 
|  | 909 | unsigned char *base = (unsigned char *)si->base; | 
|  | 910 | int cnt; | 
|  | 911 | unsigned len; | 
|  | 912 | unsigned char *tmp; | 
|  | 913 | unsigned char *pbase; | 
|  | 914 | unsigned char *extra_base; | 
|  | 915 | unsigned extra_len; | 
|  | 916 | unsigned total_sz = 0; | 
|  | 917 |  | 
|  | 918 | si->wrprotect_start = 0xffffffff; | 
|  | 919 | si->wrprotect_end = 0; | 
|  | 920 |  | 
|  | 921 | TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n", | 
|  | 922 | pid, si->name, (unsigned)si->base); | 
|  | 923 | /* Now go through all the PT_LOAD segments and map them into memory | 
|  | 924 | * at the appropriate locations. */ | 
|  | 925 | for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) { | 
|  | 926 | if (phdr->p_type == PT_LOAD) { | 
|  | 927 | DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid); | 
|  | 928 | /* we want to map in the segment on a page boundary */ | 
|  | 929 | tmp = base + (phdr->p_vaddr & (~PAGE_MASK)); | 
|  | 930 | /* add the # of bytes we masked off above to the total length. */ | 
|  | 931 | len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK); | 
|  | 932 |  | 
|  | 933 | TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x " | 
|  | 934 | "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name, | 
|  | 935 | (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset); | 
|  | 936 | pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags), | 
|  | 937 | MAP_PRIVATE | MAP_FIXED, fd, | 
|  | 938 | phdr->p_offset & (~PAGE_MASK)); | 
|  | 939 | if (pbase == MAP_FAILED) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 940 | DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). " | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 941 | "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name, | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 942 | (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset); | 
|  | 943 | goto fail; | 
|  | 944 | } | 
|  | 945 |  | 
|  | 946 | /* If 'len' didn't end on page boundary, and it's a writable | 
|  | 947 | * segment, zero-fill the rest. */ | 
|  | 948 | if ((len & PAGE_MASK) && (phdr->p_flags & PF_W)) | 
|  | 949 | memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK)); | 
|  | 950 |  | 
|  | 951 | /* Check to see if we need to extend the map for this segment to | 
|  | 952 | * cover the diff between filesz and memsz (i.e. for bss). | 
|  | 953 | * | 
|  | 954 | *  base           _+---------------------+  page boundary | 
|  | 955 | *                  .                     . | 
|  | 956 | *                  |                     | | 
|  | 957 | *                  .                     . | 
|  | 958 | *  pbase          _+---------------------+  page boundary | 
|  | 959 | *                  |                     | | 
|  | 960 | *                  .                     . | 
|  | 961 | *  base + p_vaddr _|                     | | 
|  | 962 | *                  . \          \        . | 
|  | 963 | *                  . | filesz   |        . | 
|  | 964 | *  pbase + len    _| /          |        | | 
|  | 965 | *     <0 pad>      .            .        . | 
|  | 966 | *  extra_base     _+------------|--------+  page boundary | 
|  | 967 | *               /  .            .        . | 
|  | 968 | *               |  .            .        . | 
|  | 969 | *               |  +------------|--------+  page boundary | 
|  | 970 | *  extra_len->  |  |            |        | | 
|  | 971 | *               |  .            | memsz  . | 
|  | 972 | *               |  .            |        . | 
|  | 973 | *               \ _|            /        | | 
|  | 974 | *                  .                     . | 
|  | 975 | *                  |                     | | 
|  | 976 | *                 _+---------------------+  page boundary | 
|  | 977 | */ | 
|  | 978 | tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) & | 
|  | 979 | (~PAGE_MASK)); | 
|  | 980 | if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) { | 
|  | 981 | extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp; | 
|  | 982 | TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x " | 
|  | 983 | "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len); | 
|  | 984 | /* map in the extra page(s) as anonymous into the range. | 
|  | 985 | * This is probably not necessary as we already mapped in | 
|  | 986 | * the entire region previously, but we just want to be | 
|  | 987 | * sure. This will also set the right flags on the region | 
|  | 988 | * (though we can probably accomplish the same thing with | 
|  | 989 | * mprotect). | 
|  | 990 | */ | 
|  | 991 | extra_base = mmap((void *)tmp, extra_len, | 
|  | 992 | PFLAGS_TO_PROT(phdr->p_flags), | 
|  | 993 | MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, | 
|  | 994 | -1, 0); | 
|  | 995 | if (extra_base == MAP_FAILED) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 996 | DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x" | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 997 | " (0x%08x) ]", pid, si->name, (unsigned)tmp, | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 998 | extra_len); | 
|  | 999 | goto fail; | 
|  | 1000 | } | 
|  | 1001 | /* TODO: Check if we need to memset-0 this region. | 
|  | 1002 | * Anonymous mappings are zero-filled copy-on-writes, so we | 
|  | 1003 | * shouldn't need to. */ | 
|  | 1004 | TRACE("[ %5d - Segment from '%s' extended @ 0x%08x " | 
|  | 1005 | "(0x%08x)\n", pid, si->name, (unsigned)extra_base, | 
|  | 1006 | extra_len); | 
|  | 1007 | } | 
|  | 1008 | /* set the len here to show the full extent of the segment we | 
|  | 1009 | * just loaded, mostly for debugging */ | 
|  | 1010 | len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz + | 
|  | 1011 | PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase; | 
|  | 1012 | TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x " | 
|  | 1013 | "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name, | 
|  | 1014 | (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset); | 
|  | 1015 | total_sz += len; | 
|  | 1016 | /* Make the section writable just in case we'll have to write to | 
|  | 1017 | * it during relocation (i.e. text segment). However, we will | 
|  | 1018 | * remember what range of addresses should be write protected. | 
|  | 1019 | * | 
|  | 1020 | */ | 
|  | 1021 | if (!(phdr->p_flags & PF_W)) { | 
|  | 1022 | if ((unsigned)pbase < si->wrprotect_start) | 
|  | 1023 | si->wrprotect_start = (unsigned)pbase; | 
|  | 1024 | if (((unsigned)pbase + len) > si->wrprotect_end) | 
|  | 1025 | si->wrprotect_end = (unsigned)pbase + len; | 
|  | 1026 | mprotect(pbase, len, | 
|  | 1027 | PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE); | 
|  | 1028 | } | 
|  | 1029 | } else if (phdr->p_type == PT_DYNAMIC) { | 
|  | 1030 | DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid); | 
|  | 1031 | /* this segment contains the dynamic linking information */ | 
|  | 1032 | si->dynamic = (unsigned *)(base + phdr->p_vaddr); | 
|  | 1033 | } else { | 
|  | 1034 | #ifdef ANDROID_ARM_LINKER | 
|  | 1035 | if (phdr->p_type == PT_ARM_EXIDX) { | 
|  | 1036 | DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid); | 
|  | 1037 | /* exidx entries (used for stack unwinding) are 8 bytes each. | 
|  | 1038 | */ | 
|  | 1039 | si->ARM_exidx = (unsigned *)phdr->p_vaddr; | 
|  | 1040 | si->ARM_exidx_count = phdr->p_memsz / 8; | 
|  | 1041 | } | 
|  | 1042 | #endif | 
|  | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | } | 
|  | 1046 |  | 
|  | 1047 | /* Sanity check */ | 
|  | 1048 | if (total_sz > si->size) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1049 | DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is " | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1050 | "greater than what was allocated (0x%08x). THIS IS BAD!", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1051 | pid, total_sz, si->name, si->size); | 
|  | 1052 | goto fail; | 
|  | 1053 | } | 
|  | 1054 |  | 
|  | 1055 | TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. " | 
|  | 1056 | "Total memory footprint: 0x%08x bytes ]\n", pid, si->name, | 
|  | 1057 | (unsigned)si->base, si->size); | 
|  | 1058 | return 0; | 
|  | 1059 |  | 
|  | 1060 | fail: | 
|  | 1061 | /* We can just blindly unmap the entire region even though some things | 
|  | 1062 | * were mapped in originally with anonymous and others could have been | 
|  | 1063 | * been mapped in from the file before we failed. The kernel will unmap | 
|  | 1064 | * all the pages in the range, irrespective of how they got there. | 
|  | 1065 | */ | 
|  | 1066 | munmap((void *)si->base, si->size); | 
|  | 1067 | si->flags |= FLAG_ERROR; | 
|  | 1068 | return -1; | 
|  | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | /* TODO: Implement this to take care of the fact that Android ARM | 
|  | 1072 | * ELF objects shove everything into a single loadable segment that has the | 
|  | 1073 | * write bit set. wr_offset is then used to set non-(data|bss) pages to be | 
|  | 1074 | * non-writable. | 
|  | 1075 | */ | 
|  | 1076 | #if 0 | 
|  | 1077 | static unsigned | 
|  | 1078 | get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr) | 
|  | 1079 | { | 
|  | 1080 | Elf32_Shdr *shdr_start; | 
|  | 1081 | Elf32_Shdr *shdr; | 
|  | 1082 | int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr); | 
|  | 1083 | int cnt; | 
|  | 1084 | unsigned wr_offset = 0xffffffff; | 
|  | 1085 |  | 
|  | 1086 | shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd, | 
|  | 1087 | ehdr->e_shoff & (~PAGE_MASK)); | 
|  | 1088 | if (shdr_start == MAP_FAILED) { | 
|  | 1089 | WARN("%5d - Could not read section header info from '%s'. Will not " | 
|  | 1090 | "not be able to determine write-protect offset.\n", pid, name); | 
|  | 1091 | return (unsigned)-1; | 
|  | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) { | 
|  | 1095 | if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) && | 
|  | 1096 | (shdr->sh_addr < wr_offset)) { | 
|  | 1097 | wr_offset = shdr->sh_addr; | 
|  | 1098 | } | 
|  | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | munmap(shdr_start, shdr_sz); | 
|  | 1102 | return wr_offset; | 
|  | 1103 | } | 
|  | 1104 | #endif | 
|  | 1105 |  | 
|  | 1106 | static soinfo * | 
|  | 1107 | load_library(const char *name) | 
|  | 1108 | { | 
|  | 1109 | int fd = open_library(name); | 
|  | 1110 | int cnt; | 
|  | 1111 | unsigned ext_sz; | 
|  | 1112 | unsigned req_base; | 
| Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1113 | const char *bname; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1114 | soinfo *si = NULL; | 
|  | 1115 | Elf32_Ehdr *hdr; | 
|  | 1116 |  | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1117 | if(fd == -1) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1118 | DL_ERR("Library '%s' not found", name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1119 | return NULL; | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1120 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1121 |  | 
|  | 1122 | /* We have to read the ELF header to figure out what to do with this image | 
|  | 1123 | */ | 
|  | 1124 | if (lseek(fd, 0, SEEK_SET) < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1125 | DL_ERR("lseek() failed!"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1126 | goto fail; | 
|  | 1127 | } | 
|  | 1128 |  | 
|  | 1129 | if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1130 | DL_ERR("read() failed!"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1131 | goto fail; | 
|  | 1132 | } | 
|  | 1133 |  | 
|  | 1134 | /* Parse the ELF header and get the size of the memory footprint for | 
|  | 1135 | * the library */ | 
|  | 1136 | req_base = get_lib_extents(fd, name, &__header[0], &ext_sz); | 
|  | 1137 | if (req_base == (unsigned)-1) | 
|  | 1138 | goto fail; | 
|  | 1139 | TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name, | 
|  | 1140 | (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz); | 
|  | 1141 |  | 
|  | 1142 | /* Now configure the soinfo struct where we'll store all of our data | 
|  | 1143 | * for the ELF object. If the loading fails, we waste the entry, but | 
|  | 1144 | * same thing would happen if we failed during linking. Configuring the | 
|  | 1145 | * soinfo struct here is a lot more convenient. | 
|  | 1146 | */ | 
| Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1147 | bname = strrchr(name, '/'); | 
|  | 1148 | si = alloc_info(bname ? bname + 1 : name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1149 | if (si == NULL) | 
|  | 1150 | goto fail; | 
|  | 1151 |  | 
|  | 1152 | /* Carve out a chunk of memory where we will map in the individual | 
|  | 1153 | * segments */ | 
|  | 1154 | si->base = req_base; | 
|  | 1155 | si->size = ext_sz; | 
|  | 1156 | si->flags = 0; | 
|  | 1157 | si->entry = 0; | 
|  | 1158 | si->dynamic = (unsigned *)-1; | 
|  | 1159 | if (alloc_mem_region(si) < 0) | 
|  | 1160 | goto fail; | 
|  | 1161 |  | 
|  | 1162 | TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n", | 
|  | 1163 | pid, name, (void *)si->base, (unsigned) ext_sz); | 
|  | 1164 |  | 
|  | 1165 | /* Now actually load the library's segments into right places in memory */ | 
|  | 1166 | if (load_segments(fd, &__header[0], si) < 0) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1167 | goto fail; | 
|  | 1168 | } | 
|  | 1169 |  | 
|  | 1170 | /* this might not be right. Technically, we don't even need this info | 
|  | 1171 | * once we go through 'load_segments'. */ | 
|  | 1172 | hdr = (Elf32_Ehdr *)si->base; | 
|  | 1173 | si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff); | 
|  | 1174 | si->phnum = hdr->e_phnum; | 
|  | 1175 | /**/ | 
|  | 1176 |  | 
|  | 1177 | close(fd); | 
|  | 1178 | return si; | 
|  | 1179 |  | 
|  | 1180 | fail: | 
|  | 1181 | if (si) free_info(si); | 
|  | 1182 | close(fd); | 
|  | 1183 | return NULL; | 
|  | 1184 | } | 
|  | 1185 |  | 
|  | 1186 | static soinfo * | 
|  | 1187 | init_library(soinfo *si) | 
|  | 1188 | { | 
|  | 1189 | unsigned wr_offset = 0xffffffff; | 
|  | 1190 |  | 
|  | 1191 | /* At this point we know that whatever is loaded @ base is a valid ELF | 
|  | 1192 | * shared library whose segments are properly mapped in. */ | 
|  | 1193 | TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n", | 
|  | 1194 | pid, si->base, si->size, si->name); | 
|  | 1195 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1196 | if(link_image(si, wr_offset)) { | 
|  | 1197 | /* We failed to link.  However, we can only restore libbase | 
|  | 1198 | ** if no additional libraries have moved it since we updated it. | 
|  | 1199 | */ | 
|  | 1200 | munmap((void *)si->base, si->size); | 
|  | 1201 | return NULL; | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | return si; | 
|  | 1205 | } | 
|  | 1206 |  | 
|  | 1207 | soinfo *find_library(const char *name) | 
|  | 1208 | { | 
|  | 1209 | soinfo *si; | 
| David 'Digit' Turner | 6774809 | 2010-07-21 16:18:21 -0700 | [diff] [blame] | 1210 | const char *bname; | 
|  | 1211 |  | 
|  | 1212 | #if ALLOW_SYMBOLS_FROM_MAIN | 
|  | 1213 | if (name == NULL) | 
|  | 1214 | return somain; | 
|  | 1215 | #else | 
|  | 1216 | if (name == NULL) | 
|  | 1217 | return NULL; | 
|  | 1218 | #endif | 
|  | 1219 |  | 
|  | 1220 | bname = strrchr(name, '/'); | 
| Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1221 | bname = bname ? bname + 1 : name; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1222 |  | 
|  | 1223 | for(si = solist; si != 0; si = si->next){ | 
| Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1224 | if(!strcmp(bname, si->name)) { | 
| Erik Gilling | 30eb402 | 2009-08-13 16:05:30 -0700 | [diff] [blame] | 1225 | if(si->flags & FLAG_ERROR) { | 
|  | 1226 | DL_ERR("%5d '%s' failed to load previously", pid, bname); | 
|  | 1227 | return NULL; | 
|  | 1228 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1229 | if(si->flags & FLAG_LINKED) return si; | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1230 | DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name); | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1231 | return NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1232 | } | 
|  | 1233 | } | 
|  | 1234 |  | 
|  | 1235 | TRACE("[ %5d '%s' has not been loaded yet.  Locating...]\n", pid, name); | 
|  | 1236 | si = load_library(name); | 
|  | 1237 | if(si == NULL) | 
|  | 1238 | return NULL; | 
|  | 1239 | return init_library(si); | 
|  | 1240 | } | 
|  | 1241 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1242 | /* TODO: | 
|  | 1243 | *   notify gdb of unload | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1244 | *   for non-prelinked libraries, find a way to decrement libbase | 
|  | 1245 | */ | 
|  | 1246 | static void call_destructors(soinfo *si); | 
|  | 1247 | unsigned unload_library(soinfo *si) | 
|  | 1248 | { | 
|  | 1249 | unsigned *d; | 
|  | 1250 | if (si->refcount == 1) { | 
|  | 1251 | TRACE("%5d unloading '%s'\n", pid, si->name); | 
|  | 1252 | call_destructors(si); | 
|  | 1253 |  | 
|  | 1254 | for(d = si->dynamic; *d; d += 2) { | 
|  | 1255 | if(d[0] == DT_NEEDED){ | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1256 | soinfo *lsi = (soinfo *)d[1]; | 
|  | 1257 | d[1] = 0; | 
|  | 1258 | if (validate_soinfo(lsi)) { | 
|  | 1259 | TRACE("%5d %s needs to unload %s\n", pid, | 
|  | 1260 | si->name, lsi->name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1261 | unload_library(lsi); | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1262 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1263 | else | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1264 | DL_ERR("%5d %s: could not unload dependent library", | 
|  | 1265 | pid, si->name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1266 | } | 
|  | 1267 | } | 
|  | 1268 |  | 
|  | 1269 | munmap((char *)si->base, si->size); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 1270 | notify_gdb_of_unload(si); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1271 | free_info(si); | 
|  | 1272 | si->refcount = 0; | 
|  | 1273 | } | 
|  | 1274 | else { | 
|  | 1275 | si->refcount--; | 
|  | 1276 | PRINT("%5d not unloading '%s', decrementing refcount to %d\n", | 
|  | 1277 | pid, si->name, si->refcount); | 
|  | 1278 | } | 
|  | 1279 | return si->refcount; | 
|  | 1280 | } | 
|  | 1281 |  | 
|  | 1282 | /* TODO: don't use unsigned for addrs below. It works, but is not | 
|  | 1283 | * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned | 
|  | 1284 | * long. | 
|  | 1285 | */ | 
|  | 1286 | static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) | 
|  | 1287 | { | 
|  | 1288 | Elf32_Sym *symtab = si->symtab; | 
|  | 1289 | const char *strtab = si->strtab; | 
|  | 1290 | Elf32_Sym *s; | 
|  | 1291 | unsigned base; | 
|  | 1292 | Elf32_Rel *start = rel; | 
|  | 1293 | unsigned idx; | 
|  | 1294 |  | 
|  | 1295 | for (idx = 0; idx < count; ++idx) { | 
|  | 1296 | unsigned type = ELF32_R_TYPE(rel->r_info); | 
|  | 1297 | unsigned sym = ELF32_R_SYM(rel->r_info); | 
|  | 1298 | unsigned reloc = (unsigned)(rel->r_offset + si->base); | 
|  | 1299 | unsigned sym_addr = 0; | 
|  | 1300 | char *sym_name = NULL; | 
|  | 1301 |  | 
|  | 1302 | DEBUG("%5d Processing '%s' relocation at index %d\n", pid, | 
|  | 1303 | si->name, idx); | 
|  | 1304 | if(sym != 0) { | 
| Dima Zavin | d1b40d8 | 2009-05-12 10:59:09 -0700 | [diff] [blame] | 1305 | sym_name = (char *)(strtab + symtab[sym].st_name); | 
|  | 1306 | s = _do_lookup(si, sym_name, &base); | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1307 | if(s == NULL) { | 
|  | 1308 | /* We only allow an undefined symbol if this is a weak | 
|  | 1309 | reference..   */ | 
|  | 1310 | s = &symtab[sym]; | 
|  | 1311 | if (ELF32_ST_BIND(s->st_info) != STB_WEAK) { | 
|  | 1312 | DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name); | 
|  | 1313 | return -1; | 
|  | 1314 | } | 
|  | 1315 |  | 
|  | 1316 | /* IHI0044C AAELF 4.5.1.1: | 
|  | 1317 |  | 
|  | 1318 | Libraries are not searched to resolve weak references. | 
|  | 1319 | It is not an error for a weak reference to remain | 
|  | 1320 | unsatisfied. | 
|  | 1321 |  | 
|  | 1322 | During linking, the value of an undefined weak reference is: | 
|  | 1323 | - Zero if the relocation type is absolute | 
|  | 1324 | - The address of the place if the relocation is pc-relative | 
|  | 1325 | - The address of nominial base address if the relocation | 
|  | 1326 | type is base-relative. | 
|  | 1327 | */ | 
|  | 1328 |  | 
|  | 1329 | switch (type) { | 
|  | 1330 | #if defined(ANDROID_ARM_LINKER) | 
|  | 1331 | case R_ARM_JUMP_SLOT: | 
|  | 1332 | case R_ARM_GLOB_DAT: | 
|  | 1333 | case R_ARM_ABS32: | 
|  | 1334 | case R_ARM_RELATIVE:    /* Don't care. */ | 
|  | 1335 | case R_ARM_NONE:        /* Don't care. */ | 
|  | 1336 | #elif defined(ANDROID_X86_LINKER) | 
|  | 1337 | case R_386_JUMP_SLOT: | 
|  | 1338 | case R_386_GLOB_DAT: | 
|  | 1339 | case R_386_32: | 
|  | 1340 | case R_386_RELATIVE:    /* Dont' care. */ | 
|  | 1341 | #endif /* ANDROID_*_LINKER */ | 
|  | 1342 | /* sym_addr was initialized to be zero above or relocation | 
|  | 1343 | code below does not care about value of sym_addr. | 
|  | 1344 | No need to do anything.  */ | 
|  | 1345 | break; | 
|  | 1346 |  | 
|  | 1347 | #if defined(ANDROID_X86_LINKER) | 
|  | 1348 | case R_386_PC32: | 
|  | 1349 | sym_addr = reloc; | 
|  | 1350 | break; | 
|  | 1351 | #endif /* ANDROID_X86_LINKER */ | 
|  | 1352 |  | 
|  | 1353 | #if defined(ANDROID_ARM_LINKER) | 
|  | 1354 | case R_ARM_COPY: | 
|  | 1355 | /* Fall through.  Can't really copy if weak symbol is | 
|  | 1356 | not found in run-time.  */ | 
|  | 1357 | #endif /* ANDROID_ARM_LINKER */ | 
|  | 1358 | default: | 
|  | 1359 | DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n", | 
|  | 1360 | pid, type, rel, (int) (rel - start)); | 
|  | 1361 | return -1; | 
|  | 1362 | } | 
|  | 1363 | } else { | 
|  | 1364 | /* We got a definition.  */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1365 | #if 0 | 
|  | 1366 | if((base == 0) && (si->base != 0)){ | 
|  | 1367 | /* linking from libraries to main image is bad */ | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1368 | DL_ERR("%5d cannot locate '%s'...", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1369 | pid, strtab + symtab[sym].st_name); | 
|  | 1370 | return -1; | 
|  | 1371 | } | 
|  | 1372 | #endif | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1373 | sym_addr = (unsigned)(s->st_value + base); | 
|  | 1374 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1375 | COUNT_RELOC(RELOC_SYMBOL); | 
|  | 1376 | } else { | 
| Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1377 | s = NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1378 | } | 
|  | 1379 |  | 
|  | 1380 | /* TODO: This is ugly. Split up the relocations by arch into | 
|  | 1381 | * different files. | 
|  | 1382 | */ | 
|  | 1383 | switch(type){ | 
|  | 1384 | #if defined(ANDROID_ARM_LINKER) | 
|  | 1385 | case R_ARM_JUMP_SLOT: | 
|  | 1386 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1387 | MARK(rel->r_offset); | 
|  | 1388 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, | 
|  | 1389 | reloc, sym_addr, sym_name); | 
|  | 1390 | *((unsigned*)reloc) = sym_addr; | 
|  | 1391 | break; | 
|  | 1392 | case R_ARM_GLOB_DAT: | 
|  | 1393 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1394 | MARK(rel->r_offset); | 
|  | 1395 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, | 
|  | 1396 | reloc, sym_addr, sym_name); | 
|  | 1397 | *((unsigned*)reloc) = sym_addr; | 
|  | 1398 | break; | 
|  | 1399 | case R_ARM_ABS32: | 
|  | 1400 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1401 | MARK(rel->r_offset); | 
|  | 1402 | TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid, | 
|  | 1403 | reloc, sym_addr, sym_name); | 
|  | 1404 | *((unsigned*)reloc) += sym_addr; | 
|  | 1405 | break; | 
| David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1406 | case R_ARM_REL32: | 
|  | 1407 | COUNT_RELOC(RELOC_RELATIVE); | 
|  | 1408 | MARK(rel->r_offset); | 
|  | 1409 | TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid, | 
|  | 1410 | reloc, sym_addr, rel->r_offset, sym_name); | 
|  | 1411 | *((unsigned*)reloc) += sym_addr - rel->r_offset; | 
|  | 1412 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1413 | #elif defined(ANDROID_X86_LINKER) | 
|  | 1414 | case R_386_JUMP_SLOT: | 
|  | 1415 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1416 | MARK(rel->r_offset); | 
|  | 1417 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, | 
|  | 1418 | reloc, sym_addr, sym_name); | 
|  | 1419 | *((unsigned*)reloc) = sym_addr; | 
|  | 1420 | break; | 
|  | 1421 | case R_386_GLOB_DAT: | 
|  | 1422 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1423 | MARK(rel->r_offset); | 
|  | 1424 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, | 
|  | 1425 | reloc, sym_addr, sym_name); | 
|  | 1426 | *((unsigned*)reloc) = sym_addr; | 
|  | 1427 | break; | 
|  | 1428 | #endif /* ANDROID_*_LINKER */ | 
|  | 1429 |  | 
|  | 1430 | #if defined(ANDROID_ARM_LINKER) | 
|  | 1431 | case R_ARM_RELATIVE: | 
|  | 1432 | #elif defined(ANDROID_X86_LINKER) | 
|  | 1433 | case R_386_RELATIVE: | 
|  | 1434 | #endif /* ANDROID_*_LINKER */ | 
|  | 1435 | COUNT_RELOC(RELOC_RELATIVE); | 
|  | 1436 | MARK(rel->r_offset); | 
|  | 1437 | if(sym){ | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1438 | DL_ERR("%5d odd RELATIVE form...", pid); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1439 | return -1; | 
|  | 1440 | } | 
|  | 1441 | TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid, | 
|  | 1442 | reloc, si->base); | 
|  | 1443 | *((unsigned*)reloc) += si->base; | 
|  | 1444 | break; | 
|  | 1445 |  | 
|  | 1446 | #if defined(ANDROID_X86_LINKER) | 
|  | 1447 | case R_386_32: | 
|  | 1448 | COUNT_RELOC(RELOC_RELATIVE); | 
|  | 1449 | MARK(rel->r_offset); | 
|  | 1450 |  | 
|  | 1451 | TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid, | 
|  | 1452 | reloc, sym_addr, sym_name); | 
|  | 1453 | *((unsigned *)reloc) += (unsigned)sym_addr; | 
|  | 1454 | break; | 
|  | 1455 |  | 
|  | 1456 | case R_386_PC32: | 
|  | 1457 | COUNT_RELOC(RELOC_RELATIVE); | 
|  | 1458 | MARK(rel->r_offset); | 
|  | 1459 | TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- " | 
|  | 1460 | "+%08x (%08x - %08x) %s\n", pid, reloc, | 
|  | 1461 | (sym_addr - reloc), sym_addr, reloc, sym_name); | 
|  | 1462 | *((unsigned *)reloc) += (unsigned)(sym_addr - reloc); | 
|  | 1463 | break; | 
|  | 1464 | #endif /* ANDROID_X86_LINKER */ | 
|  | 1465 |  | 
|  | 1466 | #ifdef ANDROID_ARM_LINKER | 
|  | 1467 | case R_ARM_COPY: | 
|  | 1468 | COUNT_RELOC(RELOC_COPY); | 
|  | 1469 | MARK(rel->r_offset); | 
|  | 1470 | TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid, | 
|  | 1471 | reloc, s->st_size, sym_addr, sym_name); | 
|  | 1472 | memcpy((void*)reloc, (void*)sym_addr, s->st_size); | 
|  | 1473 | break; | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 1474 | case R_ARM_NONE: | 
|  | 1475 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1476 | #endif /* ANDROID_ARM_LINKER */ | 
|  | 1477 |  | 
|  | 1478 | default: | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1479 | DL_ERR("%5d unknown reloc type %d @ %p (%d)", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1480 | pid, type, rel, (int) (rel - start)); | 
|  | 1481 | return -1; | 
|  | 1482 | } | 
|  | 1483 | rel++; | 
|  | 1484 | } | 
|  | 1485 | return 0; | 
|  | 1486 | } | 
|  | 1487 |  | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1488 | #if defined(ANDROID_SH_LINKER) | 
|  | 1489 | static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count) | 
|  | 1490 | { | 
|  | 1491 | Elf32_Sym *symtab = si->symtab; | 
|  | 1492 | const char *strtab = si->strtab; | 
|  | 1493 | Elf32_Sym *s; | 
|  | 1494 | unsigned base; | 
|  | 1495 | Elf32_Rela *start = rela; | 
|  | 1496 | unsigned idx; | 
|  | 1497 |  | 
|  | 1498 | for (idx = 0; idx < count; ++idx) { | 
|  | 1499 | unsigned type = ELF32_R_TYPE(rela->r_info); | 
|  | 1500 | unsigned sym = ELF32_R_SYM(rela->r_info); | 
|  | 1501 | unsigned reloc = (unsigned)(rela->r_offset + si->base); | 
|  | 1502 | unsigned sym_addr = 0; | 
|  | 1503 | char *sym_name = NULL; | 
|  | 1504 |  | 
|  | 1505 | DEBUG("%5d Processing '%s' relocation at index %d\n", pid, | 
|  | 1506 | si->name, idx); | 
|  | 1507 | if(sym != 0) { | 
|  | 1508 | sym_name = (char *)(strtab + symtab[sym].st_name); | 
|  | 1509 | s = _do_lookup(si, sym_name, &base); | 
|  | 1510 | if(s == 0) { | 
|  | 1511 | DL_ERR("%5d cannot locate '%s'...", pid, sym_name); | 
|  | 1512 | return -1; | 
|  | 1513 | } | 
|  | 1514 | #if 0 | 
|  | 1515 | if((base == 0) && (si->base != 0)){ | 
|  | 1516 | /* linking from libraries to main image is bad */ | 
|  | 1517 | DL_ERR("%5d cannot locate '%s'...", | 
|  | 1518 | pid, strtab + symtab[sym].st_name); | 
|  | 1519 | return -1; | 
|  | 1520 | } | 
|  | 1521 | #endif | 
|  | 1522 | if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) { | 
|  | 1523 | DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not " | 
|  | 1524 | "handle this yet", pid, si->name, s->st_shndx, | 
|  | 1525 | s->st_value); | 
|  | 1526 | return -1; | 
|  | 1527 | } | 
|  | 1528 | sym_addr = (unsigned)(s->st_value + base); | 
|  | 1529 | COUNT_RELOC(RELOC_SYMBOL); | 
|  | 1530 | } else { | 
|  | 1531 | s = 0; | 
|  | 1532 | } | 
|  | 1533 |  | 
|  | 1534 | /* TODO: This is ugly. Split up the relocations by arch into | 
|  | 1535 | * different files. | 
|  | 1536 | */ | 
|  | 1537 | switch(type){ | 
|  | 1538 | case R_SH_JUMP_SLOT: | 
|  | 1539 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1540 | MARK(rela->r_offset); | 
|  | 1541 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, | 
|  | 1542 | reloc, sym_addr, sym_name); | 
|  | 1543 | *((unsigned*)reloc) = sym_addr; | 
|  | 1544 | break; | 
|  | 1545 | case R_SH_GLOB_DAT: | 
|  | 1546 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1547 | MARK(rela->r_offset); | 
|  | 1548 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, | 
|  | 1549 | reloc, sym_addr, sym_name); | 
|  | 1550 | *((unsigned*)reloc) = sym_addr; | 
|  | 1551 | break; | 
|  | 1552 | case R_SH_DIR32: | 
|  | 1553 | COUNT_RELOC(RELOC_ABSOLUTE); | 
|  | 1554 | MARK(rela->r_offset); | 
|  | 1555 | TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid, | 
|  | 1556 | reloc, sym_addr, sym_name); | 
|  | 1557 | *((unsigned*)reloc) += sym_addr; | 
|  | 1558 | break; | 
|  | 1559 | case R_SH_RELATIVE: | 
|  | 1560 | COUNT_RELOC(RELOC_RELATIVE); | 
|  | 1561 | MARK(rela->r_offset); | 
|  | 1562 | if(sym){ | 
|  | 1563 | DL_ERR("%5d odd RELATIVE form...", pid); | 
|  | 1564 | return -1; | 
|  | 1565 | } | 
|  | 1566 | TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid, | 
|  | 1567 | reloc, si->base); | 
|  | 1568 | *((unsigned*)reloc) += si->base; | 
|  | 1569 | break; | 
|  | 1570 |  | 
|  | 1571 | default: | 
|  | 1572 | DL_ERR("%5d unknown reloc type %d @ %p (%d)", | 
|  | 1573 | pid, type, rela, (int) (rela - start)); | 
|  | 1574 | return -1; | 
|  | 1575 | } | 
|  | 1576 | rela++; | 
|  | 1577 | } | 
|  | 1578 | return 0; | 
|  | 1579 | } | 
|  | 1580 | #endif /* ANDROID_SH_LINKER */ | 
|  | 1581 |  | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1582 |  | 
|  | 1583 | /* Please read the "Initialization and Termination functions" functions. | 
|  | 1584 | * of the linker design note in bionic/linker/README.TXT to understand | 
|  | 1585 | * what the following code is doing. | 
|  | 1586 | * | 
|  | 1587 | * The important things to remember are: | 
|  | 1588 | * | 
|  | 1589 | *   DT_PREINIT_ARRAY must be called first for executables, and should | 
|  | 1590 | *   not appear in shared libraries. | 
|  | 1591 | * | 
|  | 1592 | *   DT_INIT should be called before DT_INIT_ARRAY if both are present | 
|  | 1593 | * | 
|  | 1594 | *   DT_FINI should be called after DT_FINI_ARRAY if both are present | 
|  | 1595 | * | 
|  | 1596 | *   DT_FINI_ARRAY must be parsed in reverse order. | 
|  | 1597 | */ | 
|  | 1598 |  | 
|  | 1599 | static void call_array(unsigned *ctor, int count, int reverse) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1600 | { | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1601 | int n, inc = 1; | 
|  | 1602 |  | 
|  | 1603 | if (reverse) { | 
|  | 1604 | ctor += (count-1); | 
|  | 1605 | inc   = -1; | 
|  | 1606 | } | 
|  | 1607 |  | 
|  | 1608 | for(n = count; n > 0; n--) { | 
|  | 1609 | TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid, | 
|  | 1610 | reverse ? "dtor" : "ctor", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1611 | (unsigned)ctor, (unsigned)*ctor); | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1612 | void (*func)() = (void (*)()) *ctor; | 
|  | 1613 | ctor += inc; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1614 | if(((int) func == 0) || ((int) func == -1)) continue; | 
|  | 1615 | TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func); | 
|  | 1616 | func(); | 
|  | 1617 | } | 
|  | 1618 | } | 
|  | 1619 |  | 
|  | 1620 | static void call_constructors(soinfo *si) | 
|  | 1621 | { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1622 | if (si->flags & FLAG_EXE) { | 
|  | 1623 | TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n", | 
|  | 1624 | pid, (unsigned)si->preinit_array, si->preinit_array_count, | 
|  | 1625 | si->name); | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1626 | call_array(si->preinit_array, si->preinit_array_count, 0); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1627 | TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name); | 
|  | 1628 | } else { | 
|  | 1629 | if (si->preinit_array) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1630 | DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x." | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1631 | " This is INVALID.", pid, si->name, | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1632 | (unsigned)si->preinit_array); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1633 | } | 
|  | 1634 | } | 
|  | 1635 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1636 | if (si->init_func) { | 
|  | 1637 | TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid, | 
|  | 1638 | (unsigned)si->init_func, si->name); | 
|  | 1639 | si->init_func(); | 
|  | 1640 | TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name); | 
|  | 1641 | } | 
|  | 1642 |  | 
|  | 1643 | if (si->init_array) { | 
|  | 1644 | TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid, | 
|  | 1645 | (unsigned)si->init_array, si->init_array_count, si->name); | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1646 | call_array(si->init_array, si->init_array_count, 0); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1647 | TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name); | 
|  | 1648 | } | 
|  | 1649 | } | 
|  | 1650 |  | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1651 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1652 | static void call_destructors(soinfo *si) | 
|  | 1653 | { | 
|  | 1654 | if (si->fini_array) { | 
|  | 1655 | TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid, | 
|  | 1656 | (unsigned)si->fini_array, si->fini_array_count, si->name); | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1657 | call_array(si->fini_array, si->fini_array_count, 1); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1658 | TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name); | 
|  | 1659 | } | 
|  | 1660 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1661 | if (si->fini_func) { | 
|  | 1662 | TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid, | 
|  | 1663 | (unsigned)si->fini_func, si->name); | 
|  | 1664 | si->fini_func(); | 
|  | 1665 | TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name); | 
|  | 1666 | } | 
|  | 1667 | } | 
|  | 1668 |  | 
|  | 1669 | /* Force any of the closed stdin, stdout and stderr to be associated with | 
|  | 1670 | /dev/null. */ | 
|  | 1671 | static int nullify_closed_stdio (void) | 
|  | 1672 | { | 
|  | 1673 | int dev_null, i, status; | 
|  | 1674 | int return_value = 0; | 
|  | 1675 |  | 
|  | 1676 | dev_null = open("/dev/null", O_RDWR); | 
|  | 1677 | if (dev_null < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1678 | DL_ERR("Cannot open /dev/null."); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1679 | return -1; | 
|  | 1680 | } | 
|  | 1681 | TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null); | 
|  | 1682 |  | 
|  | 1683 | /* If any of the stdio file descriptors is valid and not associated | 
|  | 1684 | with /dev/null, dup /dev/null to it.  */ | 
|  | 1685 | for (i = 0; i < 3; i++) { | 
|  | 1686 | /* If it is /dev/null already, we are done. */ | 
|  | 1687 | if (i == dev_null) | 
|  | 1688 | continue; | 
|  | 1689 |  | 
|  | 1690 | TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i); | 
|  | 1691 | /* The man page of fcntl does not say that fcntl(..,F_GETFL) | 
|  | 1692 | can be interrupted but we do this just to be safe. */ | 
|  | 1693 | do { | 
|  | 1694 | status = fcntl(i, F_GETFL); | 
|  | 1695 | } while (status < 0 && errno == EINTR); | 
|  | 1696 |  | 
|  | 1697 | /* If file is openned, we are good. */ | 
|  | 1698 | if (status >= 0) | 
|  | 1699 | continue; | 
|  | 1700 |  | 
|  | 1701 | /* The only error we allow is that the file descriptor does not | 
|  | 1702 | exist, in which case we dup /dev/null to it. */ | 
|  | 1703 | if (errno != EBADF) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1704 | DL_ERR("nullify_stdio: unhandled error %s", strerror(errno)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1705 | return_value = -1; | 
|  | 1706 | continue; | 
|  | 1707 | } | 
|  | 1708 |  | 
|  | 1709 | /* Try dupping /dev/null to this stdio file descriptor and | 
|  | 1710 | repeat if there is a signal.  Note that any errors in closing | 
|  | 1711 | the stdio descriptor are lost.  */ | 
|  | 1712 | do { | 
|  | 1713 | status = dup2(dev_null, i); | 
|  | 1714 | } while (status < 0 && errno == EINTR); | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1715 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1716 | if (status < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1717 | DL_ERR("nullify_stdio: dup2 error %s", strerror(errno)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1718 | return_value = -1; | 
|  | 1719 | continue; | 
|  | 1720 | } | 
|  | 1721 | } | 
|  | 1722 |  | 
|  | 1723 | /* If /dev/null is not one of the stdio file descriptors, close it. */ | 
|  | 1724 | if (dev_null > 2) { | 
|  | 1725 | TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null); | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1726 | do { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1727 | status = close(dev_null); | 
|  | 1728 | } while (status < 0 && errno == EINTR); | 
|  | 1729 |  | 
|  | 1730 | if (status < 0) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1731 | DL_ERR("nullify_stdio: close error %s", strerror(errno)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1732 | return_value = -1; | 
|  | 1733 | } | 
|  | 1734 | } | 
|  | 1735 |  | 
|  | 1736 | return return_value; | 
|  | 1737 | } | 
|  | 1738 |  | 
|  | 1739 | static int link_image(soinfo *si, unsigned wr_offset) | 
|  | 1740 | { | 
|  | 1741 | unsigned *d; | 
|  | 1742 | Elf32_Phdr *phdr = si->phdr; | 
|  | 1743 | int phnum = si->phnum; | 
|  | 1744 |  | 
|  | 1745 | INFO("[ %5d linking %s ]\n", pid, si->name); | 
|  | 1746 | DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid, | 
|  | 1747 | si->base, si->flags); | 
|  | 1748 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1749 | if (si->flags & (FLAG_EXE | FLAG_LINKER)) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1750 | /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1751 | * linkage info if this is the executable or the linker itself. | 
|  | 1752 | * If this was a dynamic lib, that would have been done at load time. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1753 | * | 
|  | 1754 | * TODO: It's unfortunate that small pieces of this are | 
|  | 1755 | * repeated from the load_library routine. Refactor this just | 
|  | 1756 | * slightly to reuse these bits. | 
|  | 1757 | */ | 
|  | 1758 | si->size = 0; | 
|  | 1759 | for(; phnum > 0; --phnum, ++phdr) { | 
|  | 1760 | #ifdef ANDROID_ARM_LINKER | 
|  | 1761 | if(phdr->p_type == PT_ARM_EXIDX) { | 
|  | 1762 | /* exidx entries (used for stack unwinding) are 8 bytes each. | 
|  | 1763 | */ | 
|  | 1764 | si->ARM_exidx = (unsigned *)phdr->p_vaddr; | 
|  | 1765 | si->ARM_exidx_count = phdr->p_memsz / 8; | 
|  | 1766 | } | 
|  | 1767 | #endif | 
|  | 1768 | if (phdr->p_type == PT_LOAD) { | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1769 | /* For the executable, we use the si->size field only in | 
|  | 1770 | dl_unwind_find_exidx(), so the meaning of si->size | 
| Nick Kralevich | d9ad623 | 2011-10-20 14:57:56 -0700 | [diff] [blame] | 1771 | is not the size of the executable; it is the distance | 
|  | 1772 | between the load location of the executable and the last | 
|  | 1773 | address of the loadable part of the executable. | 
|  | 1774 | We use the range [si->base, si->base + si->size) to | 
|  | 1775 | determine whether a PC value falls within the executable | 
|  | 1776 | section. Of course, if a value is between si->base and | 
|  | 1777 | (si->base + phdr->p_vaddr), it's not in the executable | 
|  | 1778 | section, but a) we shouldn't be asking for such a value | 
|  | 1779 | anyway, and b) if we have to provide an EXIDX for such a | 
|  | 1780 | value, then the executable's EXIDX is probably the better | 
|  | 1781 | choice. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1782 | */ | 
|  | 1783 | DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid); | 
|  | 1784 | if (phdr->p_vaddr + phdr->p_memsz > si->size) | 
|  | 1785 | si->size = phdr->p_vaddr + phdr->p_memsz; | 
|  | 1786 | /* try to remember what range of addresses should be write | 
|  | 1787 | * protected */ | 
|  | 1788 | if (!(phdr->p_flags & PF_W)) { | 
|  | 1789 | unsigned _end; | 
|  | 1790 |  | 
| Nick Kralevich | d9ad623 | 2011-10-20 14:57:56 -0700 | [diff] [blame] | 1791 | if (si->base + phdr->p_vaddr < si->wrprotect_start) | 
|  | 1792 | si->wrprotect_start = si->base + phdr->p_vaddr; | 
|  | 1793 | _end = (((si->base + phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) & | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1794 | (~PAGE_MASK))); | 
|  | 1795 | if (_end > si->wrprotect_end) | 
|  | 1796 | si->wrprotect_end = _end; | 
| Nick Kralevich | d9ad623 | 2011-10-20 14:57:56 -0700 | [diff] [blame] | 1797 | /* Make the section writable just in case we'll have to | 
|  | 1798 | * write to it during relocation (i.e. text segment). | 
|  | 1799 | * However, we will remember what range of addresses | 
|  | 1800 | * should be write protected. | 
|  | 1801 | */ | 
|  | 1802 | mprotect((void *) (si->base + phdr->p_vaddr), | 
|  | 1803 | phdr->p_memsz, | 
|  | 1804 | PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1805 | } | 
|  | 1806 | } else if (phdr->p_type == PT_DYNAMIC) { | 
|  | 1807 | if (si->dynamic != (unsigned *)-1) { | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1808 | DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. " | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1809 | "Segment at 0x%08x, previously one found at 0x%08x", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1810 | pid, si->name, si->base + phdr->p_vaddr, | 
|  | 1811 | (unsigned)si->dynamic); | 
|  | 1812 | goto fail; | 
|  | 1813 | } | 
|  | 1814 | DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid); | 
|  | 1815 | si->dynamic = (unsigned *) (si->base + phdr->p_vaddr); | 
|  | 1816 | } | 
|  | 1817 | } | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | if (si->dynamic == (unsigned *)-1) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1821 | DL_ERR("%5d missing PT_DYNAMIC?!", pid); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1822 | goto fail; | 
|  | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | DEBUG("%5d dynamic = %p\n", pid, si->dynamic); | 
|  | 1826 |  | 
|  | 1827 | /* extract useful information from dynamic section */ | 
|  | 1828 | for(d = si->dynamic; *d; d++){ | 
|  | 1829 | DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]); | 
|  | 1830 | switch(*d++){ | 
|  | 1831 | case DT_HASH: | 
|  | 1832 | si->nbucket = ((unsigned *) (si->base + *d))[0]; | 
|  | 1833 | si->nchain = ((unsigned *) (si->base + *d))[1]; | 
|  | 1834 | si->bucket = (unsigned *) (si->base + *d + 8); | 
|  | 1835 | si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4); | 
|  | 1836 | break; | 
|  | 1837 | case DT_STRTAB: | 
|  | 1838 | si->strtab = (const char *) (si->base + *d); | 
|  | 1839 | break; | 
|  | 1840 | case DT_SYMTAB: | 
|  | 1841 | si->symtab = (Elf32_Sym *) (si->base + *d); | 
|  | 1842 | break; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1843 | #if !defined(ANDROID_SH_LINKER) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1844 | case DT_PLTREL: | 
|  | 1845 | if(*d != DT_REL) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1846 | DL_ERR("DT_RELA not supported"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1847 | goto fail; | 
|  | 1848 | } | 
|  | 1849 | break; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1850 | #endif | 
|  | 1851 | #ifdef ANDROID_SH_LINKER | 
|  | 1852 | case DT_JMPREL: | 
|  | 1853 | si->plt_rela = (Elf32_Rela*) (si->base + *d); | 
|  | 1854 | break; | 
|  | 1855 | case DT_PLTRELSZ: | 
|  | 1856 | si->plt_rela_count = *d / sizeof(Elf32_Rela); | 
|  | 1857 | break; | 
|  | 1858 | #else | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1859 | case DT_JMPREL: | 
|  | 1860 | si->plt_rel = (Elf32_Rel*) (si->base + *d); | 
|  | 1861 | break; | 
|  | 1862 | case DT_PLTRELSZ: | 
|  | 1863 | si->plt_rel_count = *d / 8; | 
|  | 1864 | break; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1865 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1866 | case DT_REL: | 
|  | 1867 | si->rel = (Elf32_Rel*) (si->base + *d); | 
|  | 1868 | break; | 
|  | 1869 | case DT_RELSZ: | 
|  | 1870 | si->rel_count = *d / 8; | 
|  | 1871 | break; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1872 | #ifdef ANDROID_SH_LINKER | 
|  | 1873 | case DT_RELASZ: | 
|  | 1874 | si->rela_count = *d / sizeof(Elf32_Rela); | 
|  | 1875 | break; | 
|  | 1876 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1877 | case DT_PLTGOT: | 
|  | 1878 | /* Save this in case we decide to do lazy binding. We don't yet. */ | 
|  | 1879 | si->plt_got = (unsigned *)(si->base + *d); | 
|  | 1880 | break; | 
|  | 1881 | case DT_DEBUG: | 
|  | 1882 | // Set the DT_DEBUG entry to the addres of _r_debug for GDB | 
|  | 1883 | *d = (int) &_r_debug; | 
|  | 1884 | break; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1885 | #ifdef ANDROID_SH_LINKER | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1886 | case DT_RELA: | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1887 | si->rela = (Elf32_Rela *) (si->base + *d); | 
|  | 1888 | break; | 
|  | 1889 | #else | 
|  | 1890 | case DT_RELA: | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1891 | DL_ERR("%5d DT_RELA not supported", pid); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1892 | goto fail; | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1893 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1894 | case DT_INIT: | 
|  | 1895 | si->init_func = (void (*)(void))(si->base + *d); | 
|  | 1896 | DEBUG("%5d %s constructors (init func) found at %p\n", | 
|  | 1897 | pid, si->name, si->init_func); | 
|  | 1898 | break; | 
|  | 1899 | case DT_FINI: | 
|  | 1900 | si->fini_func = (void (*)(void))(si->base + *d); | 
|  | 1901 | DEBUG("%5d %s destructors (fini func) found at %p\n", | 
|  | 1902 | pid, si->name, si->fini_func); | 
|  | 1903 | break; | 
|  | 1904 | case DT_INIT_ARRAY: | 
|  | 1905 | si->init_array = (unsigned *)(si->base + *d); | 
|  | 1906 | DEBUG("%5d %s constructors (init_array) found at %p\n", | 
|  | 1907 | pid, si->name, si->init_array); | 
|  | 1908 | break; | 
|  | 1909 | case DT_INIT_ARRAYSZ: | 
|  | 1910 | si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); | 
|  | 1911 | break; | 
|  | 1912 | case DT_FINI_ARRAY: | 
|  | 1913 | si->fini_array = (unsigned *)(si->base + *d); | 
|  | 1914 | DEBUG("%5d %s destructors (fini_array) found at %p\n", | 
|  | 1915 | pid, si->name, si->fini_array); | 
|  | 1916 | break; | 
|  | 1917 | case DT_FINI_ARRAYSZ: | 
|  | 1918 | si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); | 
|  | 1919 | break; | 
|  | 1920 | case DT_PREINIT_ARRAY: | 
|  | 1921 | si->preinit_array = (unsigned *)(si->base + *d); | 
|  | 1922 | DEBUG("%5d %s constructors (preinit_array) found at %p\n", | 
|  | 1923 | pid, si->name, si->preinit_array); | 
|  | 1924 | break; | 
|  | 1925 | case DT_PREINIT_ARRAYSZ: | 
|  | 1926 | si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); | 
|  | 1927 | break; | 
|  | 1928 | case DT_TEXTREL: | 
|  | 1929 | /* TODO: make use of this. */ | 
|  | 1930 | /* this means that we might have to write into where the text | 
|  | 1931 | * segment was loaded during relocation... Do something with | 
|  | 1932 | * it. | 
|  | 1933 | */ | 
|  | 1934 | DEBUG("%5d Text segment should be writable during relocation.\n", | 
|  | 1935 | pid); | 
|  | 1936 | break; | 
|  | 1937 | } | 
|  | 1938 | } | 
|  | 1939 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1940 | DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n", | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1941 | pid, si->base, si->strtab, si->symtab); | 
|  | 1942 |  | 
|  | 1943 | if((si->strtab == 0) || (si->symtab == 0)) { | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1944 | DL_ERR("%5d missing essential tables", pid); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1945 | goto fail; | 
|  | 1946 | } | 
|  | 1947 |  | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 1948 | /* if this is the main executable, then load all of the preloads now */ | 
|  | 1949 | if(si->flags & FLAG_EXE) { | 
|  | 1950 | int i; | 
|  | 1951 | memset(preloads, 0, sizeof(preloads)); | 
|  | 1952 | for(i = 0; ldpreload_names[i] != NULL; i++) { | 
|  | 1953 | soinfo *lsi = find_library(ldpreload_names[i]); | 
|  | 1954 | if(lsi == 0) { | 
|  | 1955 | strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); | 
|  | 1956 | DL_ERR("%5d could not load needed library '%s' for '%s' (%s)", | 
|  | 1957 | pid, ldpreload_names[i], si->name, tmp_err_buf); | 
|  | 1958 | goto fail; | 
|  | 1959 | } | 
|  | 1960 | lsi->refcount++; | 
|  | 1961 | preloads[i] = lsi; | 
|  | 1962 | } | 
|  | 1963 | } | 
|  | 1964 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1965 | for(d = si->dynamic; *d; d += 2) { | 
|  | 1966 | if(d[0] == DT_NEEDED){ | 
|  | 1967 | DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]); | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1968 | soinfo *lsi = find_library(si->strtab + d[1]); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1969 | if(lsi == 0) { | 
| Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 1970 | strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); | 
| Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1971 | DL_ERR("%5d could not load needed library '%s' for '%s' (%s)", | 
| Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 1972 | pid, si->strtab + d[1], si->name, tmp_err_buf); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1973 | goto fail; | 
|  | 1974 | } | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1975 | /* Save the soinfo of the loaded DT_NEEDED library in the payload | 
|  | 1976 | of the DT_NEEDED entry itself, so that we can retrieve the | 
|  | 1977 | soinfo directly later from the dynamic segment.  This is a hack, | 
|  | 1978 | but it allows us to map from DT_NEEDED to soinfo efficiently | 
|  | 1979 | later on when we resolve relocations, trying to look up a symgol | 
|  | 1980 | with dlsym(). | 
|  | 1981 | */ | 
|  | 1982 | d[1] = (unsigned)lsi; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1983 | lsi->refcount++; | 
|  | 1984 | } | 
|  | 1985 | } | 
|  | 1986 |  | 
|  | 1987 | if(si->plt_rel) { | 
|  | 1988 | DEBUG("[ %5d relocating %s plt ]\n", pid, si->name ); | 
|  | 1989 | if(reloc_library(si, si->plt_rel, si->plt_rel_count)) | 
|  | 1990 | goto fail; | 
|  | 1991 | } | 
|  | 1992 | if(si->rel) { | 
|  | 1993 | DEBUG("[ %5d relocating %s ]\n", pid, si->name ); | 
|  | 1994 | if(reloc_library(si, si->rel, si->rel_count)) | 
|  | 1995 | goto fail; | 
|  | 1996 | } | 
|  | 1997 |  | 
| Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1998 | #ifdef ANDROID_SH_LINKER | 
|  | 1999 | if(si->plt_rela) { | 
|  | 2000 | DEBUG("[ %5d relocating %s plt ]\n", pid, si->name ); | 
|  | 2001 | if(reloc_library_a(si, si->plt_rela, si->plt_rela_count)) | 
|  | 2002 | goto fail; | 
|  | 2003 | } | 
|  | 2004 | if(si->rela) { | 
|  | 2005 | DEBUG("[ %5d relocating %s ]\n", pid, si->name ); | 
|  | 2006 | if(reloc_library_a(si, si->rela, si->rela_count)) | 
|  | 2007 | goto fail; | 
|  | 2008 | } | 
|  | 2009 | #endif /* ANDROID_SH_LINKER */ | 
|  | 2010 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2011 | si->flags |= FLAG_LINKED; | 
|  | 2012 | DEBUG("[ %5d finished linking %s ]\n", pid, si->name); | 
|  | 2013 |  | 
|  | 2014 | #if 0 | 
|  | 2015 | /* This is the way that the old dynamic linker did protection of | 
|  | 2016 | * non-writable areas. It would scan section headers and find where | 
|  | 2017 | * .text ended (rather where .data/.bss began) and assume that this is | 
|  | 2018 | * the upper range of the non-writable area. This is too coarse, | 
|  | 2019 | * and is kept here for reference until we fully move away from single | 
|  | 2020 | * segment elf objects. See the code in get_wr_offset (also #if'd 0) | 
|  | 2021 | * that made this possible. | 
|  | 2022 | */ | 
|  | 2023 | if(wr_offset < 0xffffffff){ | 
|  | 2024 | mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC); | 
|  | 2025 | } | 
|  | 2026 | #else | 
|  | 2027 | /* TODO: Verify that this does the right thing in all cases, as it | 
|  | 2028 | * presently probably does not. It is possible that an ELF image will | 
|  | 2029 | * come with multiple read-only segments. What we ought to do is scan | 
|  | 2030 | * the program headers again and mprotect all the read-only segments. | 
|  | 2031 | * To prevent re-scanning the program header, we would have to build a | 
|  | 2032 | * list of loadable segments in si, and then scan that instead. */ | 
|  | 2033 | if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) { | 
|  | 2034 | mprotect((void *)si->wrprotect_start, | 
|  | 2035 | si->wrprotect_end - si->wrprotect_start, | 
|  | 2036 | PROT_READ | PROT_EXEC); | 
|  | 2037 | } | 
|  | 2038 | #endif | 
|  | 2039 |  | 
|  | 2040 | /* If this is a SET?ID program, dup /dev/null to opened stdin, | 
|  | 2041 | stdout and stderr to close a security hole described in: | 
|  | 2042 |  | 
|  | 2043 | ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc | 
|  | 2044 |  | 
|  | 2045 | */ | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2046 | if (program_is_setuid) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2047 | nullify_closed_stdio (); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2048 | notify_gdb_of_load(si); | 
| David 'Digit' Turner | e5ea455 | 2011-08-27 10:21:01 +0200 | [diff] [blame] | 2049 | call_constructors(si); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2050 | return 0; | 
|  | 2051 |  | 
|  | 2052 | fail: | 
| Dima Zavin | a716190 | 2010-08-17 15:56:40 -0700 | [diff] [blame] | 2053 | ERROR("failed to link %s\n", si->name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2054 | si->flags |= FLAG_ERROR; | 
|  | 2055 | return -1; | 
|  | 2056 | } | 
|  | 2057 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2058 | static void parse_library_path(const char *path, char *delim) | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2059 | { | 
|  | 2060 | size_t len; | 
|  | 2061 | char *ldpaths_bufp = ldpaths_buf; | 
|  | 2062 | int i = 0; | 
|  | 2063 |  | 
|  | 2064 | len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf)); | 
|  | 2065 |  | 
|  | 2066 | while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) { | 
|  | 2067 | if (*ldpaths[i] != '\0') | 
|  | 2068 | ++i; | 
|  | 2069 | } | 
|  | 2070 |  | 
|  | 2071 | /* Forget the last path if we had to truncate; this occurs if the 2nd to | 
|  | 2072 | * last char isn't '\0' (i.e. not originally a delim). */ | 
|  | 2073 | if (i > 0 && len >= sizeof(ldpaths_buf) && | 
|  | 2074 | ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') { | 
|  | 2075 | ldpaths[i - 1] = NULL; | 
|  | 2076 | } else { | 
|  | 2077 | ldpaths[i] = NULL; | 
|  | 2078 | } | 
|  | 2079 | } | 
|  | 2080 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2081 | static void parse_preloads(const char *path, char *delim) | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 2082 | { | 
|  | 2083 | size_t len; | 
|  | 2084 | char *ldpreloads_bufp = ldpreloads_buf; | 
|  | 2085 | int i = 0; | 
|  | 2086 |  | 
|  | 2087 | len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf)); | 
|  | 2088 |  | 
|  | 2089 | while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) { | 
|  | 2090 | if (*ldpreload_names[i] != '\0') { | 
|  | 2091 | ++i; | 
|  | 2092 | } | 
|  | 2093 | } | 
|  | 2094 |  | 
|  | 2095 | /* Forget the last path if we had to truncate; this occurs if the 2nd to | 
|  | 2096 | * last char isn't '\0' (i.e. not originally a delim). */ | 
|  | 2097 | if (i > 0 && len >= sizeof(ldpreloads_buf) && | 
|  | 2098 | ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') { | 
|  | 2099 | ldpreload_names[i - 1] = NULL; | 
|  | 2100 | } else { | 
|  | 2101 | ldpreload_names[i] = NULL; | 
|  | 2102 | } | 
|  | 2103 | } | 
|  | 2104 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2105 | int main(int argc, char **argv) | 
|  | 2106 | { | 
|  | 2107 | return 0; | 
|  | 2108 | } | 
|  | 2109 |  | 
|  | 2110 | #define ANDROID_TLS_SLOTS  BIONIC_TLS_SLOTS | 
|  | 2111 |  | 
|  | 2112 | static void * __tls_area[ANDROID_TLS_SLOTS]; | 
|  | 2113 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 2114 | /* | 
|  | 2115 | * This code is called after the linker has linked itself and | 
|  | 2116 | * fixed it's own GOT. It is safe to make references to externs | 
|  | 2117 | * and other non-local data at this point. | 
|  | 2118 | */ | 
|  | 2119 | static unsigned __linker_init_post_relocation(unsigned **elfdata) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2120 | { | 
|  | 2121 | static soinfo linker_soinfo; | 
|  | 2122 |  | 
|  | 2123 | int argc = (int) *elfdata; | 
|  | 2124 | char **argv = (char**) (elfdata + 1); | 
|  | 2125 | unsigned *vecs = (unsigned*) (argv + argc + 1); | 
|  | 2126 | soinfo *si; | 
|  | 2127 | struct link_map * map; | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2128 | const char *ldpath_env = NULL; | 
|  | 2129 | const char *ldpreload_env = NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2130 |  | 
| David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 2131 | /* Setup a temporary TLS area that is used to get a working | 
|  | 2132 | * errno for system calls. | 
|  | 2133 | */ | 
|  | 2134 | __set_tls(__tls_area); | 
|  | 2135 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2136 | pid = getpid(); | 
|  | 2137 |  | 
|  | 2138 | #if TIMING | 
|  | 2139 | struct timeval t0, t1; | 
|  | 2140 | gettimeofday(&t0, 0); | 
|  | 2141 | #endif | 
|  | 2142 |  | 
| David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 2143 | /* NOTE: we store the elfdata pointer on a special location | 
|  | 2144 | *       of the temporary TLS area in order to pass it to | 
|  | 2145 | *       the C Library's runtime initializer. | 
|  | 2146 | * | 
|  | 2147 | *       The initializer must clear the slot and reset the TLS | 
|  | 2148 | *       to point to a different location to ensure that no other | 
|  | 2149 | *       shared library constructor can access it. | 
|  | 2150 | */ | 
|  | 2151 | __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2152 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2153 | /* Are we setuid? */ | 
|  | 2154 | program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid()); | 
|  | 2155 |  | 
|  | 2156 | /* Initialize environment functions, and get to the ELF aux vectors table */ | 
|  | 2157 | vecs = linker_env_init(vecs); | 
|  | 2158 |  | 
|  | 2159 | /* Sanitize environment if we're loading a setuid program */ | 
|  | 2160 | if (program_is_setuid) | 
|  | 2161 | linker_env_secure(); | 
|  | 2162 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2163 | debugger_init(); | 
|  | 2164 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2165 | /* Get a few environment variables */ | 
|  | 2166 | { | 
|  | 2167 | const char* env; | 
|  | 2168 | env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */ | 
|  | 2169 | if (env) | 
|  | 2170 | debug_verbosity = atoi(env); | 
|  | 2171 |  | 
|  | 2172 | /* Normally, these are cleaned by linker_env_secure, but the test | 
|  | 2173 | * against program_is_setuid doesn't cost us anything */ | 
|  | 2174 | if (!program_is_setuid) { | 
|  | 2175 | ldpath_env = linker_env_get("LD_LIBRARY_PATH"); | 
|  | 2176 | ldpreload_env = linker_env_get("LD_PRELOAD"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2177 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2178 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2179 |  | 
|  | 2180 | INFO("[ android linker & debugger ]\n"); | 
|  | 2181 | DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata); | 
|  | 2182 |  | 
|  | 2183 | si = alloc_info(argv[0]); | 
|  | 2184 | if(si == 0) { | 
|  | 2185 | exit(-1); | 
|  | 2186 | } | 
|  | 2187 |  | 
|  | 2188 | /* bootstrap the link map, the main exe always needs to be first */ | 
|  | 2189 | si->flags |= FLAG_EXE; | 
|  | 2190 | map = &(si->linkmap); | 
|  | 2191 |  | 
|  | 2192 | map->l_addr = 0; | 
|  | 2193 | map->l_name = argv[0]; | 
|  | 2194 | map->l_prev = NULL; | 
|  | 2195 | map->l_next = NULL; | 
|  | 2196 |  | 
|  | 2197 | _r_debug.r_map = map; | 
|  | 2198 | r_debug_tail = map; | 
|  | 2199 |  | 
|  | 2200 | /* gdb expects the linker to be in the debug shared object list, | 
|  | 2201 | * and we need to make sure that the reported load address is zero. | 
|  | 2202 | * Without this, gdb gets the wrong idea of where rtld_db_dlactivity() | 
|  | 2203 | * is.  Don't use alloc_info(), because the linker shouldn't | 
|  | 2204 | * be on the soinfo list. | 
|  | 2205 | */ | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2206 | strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2207 | linker_soinfo.flags = 0; | 
|  | 2208 | linker_soinfo.base = 0;     // This is the important part; must be zero. | 
|  | 2209 | insert_soinfo_into_debug_map(&linker_soinfo); | 
|  | 2210 |  | 
|  | 2211 | /* extract information passed from the kernel */ | 
|  | 2212 | while(vecs[0] != 0){ | 
|  | 2213 | switch(vecs[0]){ | 
|  | 2214 | case AT_PHDR: | 
|  | 2215 | si->phdr = (Elf32_Phdr*) vecs[1]; | 
|  | 2216 | break; | 
|  | 2217 | case AT_PHNUM: | 
|  | 2218 | si->phnum = (int) vecs[1]; | 
|  | 2219 | break; | 
|  | 2220 | case AT_ENTRY: | 
|  | 2221 | si->entry = vecs[1]; | 
|  | 2222 | break; | 
|  | 2223 | } | 
|  | 2224 | vecs += 2; | 
|  | 2225 | } | 
|  | 2226 |  | 
| David 'Digit' Turner | 8180b08 | 2011-11-15 17:17:28 +0100 | [diff] [blame] | 2227 | /* Compute the value of si->base. We can't rely on the fact that | 
|  | 2228 | * the first entry is the PHDR because this will not be true | 
|  | 2229 | * for certain executables (e.g. some in the NDK unit test suite) | 
|  | 2230 | */ | 
|  | 2231 | int nn; | 
|  | 2232 | si->base = 0; | 
|  | 2233 | for ( nn = 0; nn < si->phnum; nn++ ) { | 
|  | 2234 | if (si->phdr[nn].p_type == PT_PHDR) { | 
|  | 2235 | si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_vaddr; | 
|  | 2236 | break; | 
|  | 2237 | } | 
|  | 2238 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2239 | si->dynamic = (unsigned *)-1; | 
|  | 2240 | si->wrprotect_start = 0xffffffff; | 
|  | 2241 | si->wrprotect_end = 0; | 
| David 'Digit' Turner | 6774809 | 2010-07-21 16:18:21 -0700 | [diff] [blame] | 2242 | si->refcount = 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2243 |  | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2244 | /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */ | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2245 | if (ldpath_env) | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2246 | parse_library_path(ldpath_env, ":"); | 
|  | 2247 |  | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2248 | if (ldpreload_env) { | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 2249 | parse_preloads(ldpreload_env, " :"); | 
|  | 2250 | } | 
|  | 2251 |  | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 2252 | if(link_image(si, 0)) { | 
|  | 2253 | char errmsg[] = "CANNOT LINK EXECUTABLE\n"; | 
|  | 2254 | write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); | 
|  | 2255 | write(2, errmsg, sizeof(errmsg)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2256 | exit(-1); | 
|  | 2257 | } | 
|  | 2258 |  | 
| Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 2259 | #if ALLOW_SYMBOLS_FROM_MAIN | 
|  | 2260 | /* Set somain after we've loaded all the libraries in order to prevent | 
|  | 2261 | * linking of symbols back to the main image, which is not set up at that | 
|  | 2262 | * point yet. | 
|  | 2263 | */ | 
|  | 2264 | somain = si; | 
|  | 2265 | #endif | 
|  | 2266 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2267 | #if TIMING | 
|  | 2268 | gettimeofday(&t1,NULL); | 
|  | 2269 | PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) ( | 
|  | 2270 | (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - | 
|  | 2271 | (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec) | 
|  | 2272 | )); | 
|  | 2273 | #endif | 
|  | 2274 | #if STATS | 
|  | 2275 | PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0], | 
|  | 2276 | linker_stats.reloc[RELOC_ABSOLUTE], | 
|  | 2277 | linker_stats.reloc[RELOC_RELATIVE], | 
|  | 2278 | linker_stats.reloc[RELOC_COPY], | 
|  | 2279 | linker_stats.reloc[RELOC_SYMBOL]); | 
|  | 2280 | #endif | 
|  | 2281 | #if COUNT_PAGES | 
|  | 2282 | { | 
|  | 2283 | unsigned n; | 
|  | 2284 | unsigned i; | 
|  | 2285 | unsigned count = 0; | 
|  | 2286 | for(n = 0; n < 4096; n++){ | 
|  | 2287 | if(bitmask[n]){ | 
|  | 2288 | unsigned x = bitmask[n]; | 
|  | 2289 | for(i = 0; i < 8; i++){ | 
|  | 2290 | if(x & 1) count++; | 
|  | 2291 | x >>= 1; | 
|  | 2292 | } | 
|  | 2293 | } | 
|  | 2294 | } | 
|  | 2295 | PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4); | 
|  | 2296 | } | 
|  | 2297 | #endif | 
|  | 2298 |  | 
|  | 2299 | #if TIMING || STATS || COUNT_PAGES | 
|  | 2300 | fflush(stdout); | 
|  | 2301 | #endif | 
|  | 2302 |  | 
|  | 2303 | TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name, | 
|  | 2304 | si->entry); | 
|  | 2305 | return si->entry; | 
|  | 2306 | } | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 2307 |  | 
|  | 2308 | /* | 
|  | 2309 | * Find the value of AT_BASE passed to us by the kernel. This is the load | 
|  | 2310 | * location of the linker. | 
|  | 2311 | */ | 
|  | 2312 | static unsigned find_linker_base(unsigned **elfdata) { | 
|  | 2313 | int argc = (int) *elfdata; | 
|  | 2314 | char **argv = (char**) (elfdata + 1); | 
|  | 2315 | unsigned *vecs = (unsigned*) (argv + argc + 1); | 
|  | 2316 | while (vecs[0] != 0) { | 
|  | 2317 | vecs++; | 
|  | 2318 | } | 
|  | 2319 |  | 
|  | 2320 | /* The end of the environment block is marked by two NULL pointers */ | 
|  | 2321 | vecs++; | 
|  | 2322 |  | 
|  | 2323 | while(vecs[0]) { | 
|  | 2324 | if (vecs[0] == AT_BASE) { | 
|  | 2325 | return vecs[1]; | 
|  | 2326 | } | 
|  | 2327 | vecs += 2; | 
|  | 2328 | } | 
|  | 2329 |  | 
|  | 2330 | return 0; // should never happen | 
|  | 2331 | } | 
|  | 2332 |  | 
|  | 2333 | /* | 
|  | 2334 | * This is the entry point for the linker, called from begin.S. This | 
|  | 2335 | * method is responsible for fixing the linker's own relocations, and | 
|  | 2336 | * then calling __linker_init_post_relocation(). | 
|  | 2337 | * | 
|  | 2338 | * Because this method is called before the linker has fixed it's own | 
|  | 2339 | * relocations, any attempt to reference an extern variable, extern | 
|  | 2340 | * function, or other GOT reference will generate a segfault. | 
|  | 2341 | */ | 
|  | 2342 | unsigned __linker_init(unsigned **elfdata) { | 
|  | 2343 | unsigned linker_addr = find_linker_base(elfdata); | 
|  | 2344 | Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr; | 
|  | 2345 | Elf32_Phdr *phdr = | 
|  | 2346 | (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff); | 
|  | 2347 |  | 
|  | 2348 | soinfo linker_so; | 
|  | 2349 | memset(&linker_so, 0, sizeof(soinfo)); | 
|  | 2350 |  | 
|  | 2351 | linker_so.base = linker_addr; | 
|  | 2352 | linker_so.dynamic = (unsigned *) -1; | 
|  | 2353 | linker_so.phdr = phdr; | 
|  | 2354 | linker_so.phnum = elf_hdr->e_phnum; | 
|  | 2355 | linker_so.flags |= FLAG_LINKER; | 
|  | 2356 | linker_so.wrprotect_start = 0xffffffff; | 
|  | 2357 | linker_so.wrprotect_end = 0; | 
|  | 2358 |  | 
|  | 2359 | if (link_image(&linker_so, 0)) { | 
|  | 2360 | // It would be nice to print an error message, but if the linker | 
|  | 2361 | // can't link itself, there's no guarantee that we'll be able to | 
|  | 2362 | // call write() (because it involves a GOT reference). | 
|  | 2363 | // | 
|  | 2364 | // This situation should never occur unless the linker itself | 
|  | 2365 | // is corrupt. | 
|  | 2366 | exit(-1); | 
|  | 2367 | } | 
|  | 2368 |  | 
|  | 2369 | // We have successfully fixed our own relocations. It's safe to run | 
|  | 2370 | // the main part of the linker now. | 
|  | 2371 | return __linker_init_post_relocation(elfdata); | 
|  | 2372 | } |