Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <elf.h> |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 30 | #include <string.h> |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 31 | #include <sys/auxv.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <link.h> |
| 34 | |
| 35 | /* ld provides this to us in the default link script */ |
| 36 | extern "C" void* __executable_start; |
| 37 | |
| 38 | int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) { |
| 39 | ElfW(Ehdr)* ehdr = reinterpret_cast<ElfW(Ehdr)*>(&__executable_start); |
| 40 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 41 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) { |
| 42 | return -1; |
| 43 | } |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 44 | |
| 45 | // Dynamic binaries get their dl_iterate_phdr from the dynamic linker, but |
| 46 | // static binaries get this. We don't have a list of shared objects to |
| 47 | // iterate over, since there's really only a single monolithic blob of |
| 48 | // code/data, plus optionally a VDSO. |
| 49 | |
| 50 | struct dl_phdr_info exe_info; |
| 51 | exe_info.dlpi_addr = 0; |
| 52 | exe_info.dlpi_name = NULL; |
| 53 | exe_info.dlpi_phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(ehdr) + ehdr->e_phoff); |
| 54 | exe_info.dlpi_phnum = ehdr->e_phnum; |
| 55 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 56 | #if defined(AT_SYSINFO_EHDR) |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 57 | // Try the executable first. |
| 58 | int rc = cb(&exe_info, sizeof(exe_info), data); |
| 59 | if (rc != 0) { |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | // Try the VDSO if that didn't work. |
| 64 | ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR)); |
Elliott Hughes | 36f451a | 2014-09-10 15:20:40 -0700 | [diff] [blame] | 65 | if (ehdr_vdso == nullptr) { |
| 66 | // There is no VDSO, so there's nowhere left to look. |
| 67 | return rc; |
| 68 | } |
| 69 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 70 | struct dl_phdr_info vdso_info; |
| 71 | vdso_info.dlpi_addr = 0; |
| 72 | vdso_info.dlpi_name = NULL; |
| 73 | vdso_info.dlpi_phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); |
| 74 | vdso_info.dlpi_phnum = ehdr_vdso->e_phnum; |
| 75 | for (size_t i = 0; i < vdso_info.dlpi_phnum; ++i) { |
| 76 | if (vdso_info.dlpi_phdr[i].p_type == PT_LOAD) { |
| 77 | vdso_info.dlpi_addr = (ElfW(Addr)) ehdr_vdso - vdso_info.dlpi_phdr[i].p_vaddr; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | return cb(&vdso_info, sizeof(vdso_info), data); |
| 82 | #else |
| 83 | // There's only the executable to try. |
| 84 | return cb(&exe_info, sizeof(exe_info), data); |
| 85 | #endif |
| 86 | } |