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