1405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham/* 2405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * Copyright (C) 2006 The Android Open Source Project 3405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * All rights reserved. 4405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * 5405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * Redistribution and use in source and binary forms, with or without 6405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * modification, are permitted provided that the following conditions 7405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * are met: 8405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * * Redistributions of source code must retain the above copyright 9405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * notice, this list of conditions and the following disclaimer. 10405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * * Redistributions in binary form must reproduce the above copyright 11405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * notice, this list of conditions and the following disclaimer in 12405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * the documentation and/or other materials provided with the 13405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * distribution. 14405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * 15405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham * SUCH DAMAGE. 27405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham */ 28405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham 29d070df4c74c654d8c37c807e7e77db616f91ecbdElliott Hughes#include <elf.h> 30c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#include <sys/auxv.h> 31405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham#include <sys/types.h> 32b7beb6907599b36809effe1b4db76039ac649518Pavel Chupin#include <link.h> 33405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham 34405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham/* ld provides this to us in the default link script */ 35c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikovextern void* __executable_start; 36405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham 37c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikovint dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) { 38c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov Elf32_Ehdr* ehdr = (Elf32_Ehdr*) &__executable_start; 39405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham 40c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // TODO: again, copied from linker.c. Find a better home for this later. 41405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham if (ehdr->e_ident[EI_MAG0] != ELFMAG0) return -1; 42405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham if (ehdr->e_ident[EI_MAG1] != ELFMAG1) return -1; 43405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham if (ehdr->e_ident[EI_MAG2] != ELFMAG2) return -1; 44405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham if (ehdr->e_ident[EI_MAG3] != ELFMAG3) return -1; 45405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham 46c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // Dynamic binaries get their dl_iterate_phdr from the dynamic linker, but 47c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // static binaries get this. We don't have a list of shared objects to 48c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // iterate over, since there's really only a single monolithic blob of 49c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // code/data, plus optionally a VDSO. 50c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 51c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov struct dl_phdr_info exe_info; 52c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov exe_info.dlpi_addr = 0; 53c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov exe_info.dlpi_name = NULL; 54c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov exe_info.dlpi_phdr = (Elf32_Phdr*) ((unsigned long) ehdr + ehdr->e_phoff); 55c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov exe_info.dlpi_phnum = ehdr->e_phnum; 56c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 57c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#ifdef AT_SYSINFO_EHDR 58c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // Try the executable first. 59c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov int rc = cb(&exe_info, sizeof(exe_info), data); 60c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov if (rc != 0) { 61c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov return rc; 62c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov } 63c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 64c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // Try the VDSO if that didn't work. 65c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov Elf32_Ehdr* ehdr_vdso = (Elf32_Ehdr*) getauxval(AT_SYSINFO_EHDR); 66c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov struct dl_phdr_info vdso_info; 67c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov vdso_info.dlpi_addr = 0; 68c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov vdso_info.dlpi_name = NULL; 69c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov vdso_info.dlpi_phdr = (Elf32_Phdr*) ((char*) ehdr_vdso + ehdr_vdso->e_phoff); 70c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov vdso_info.dlpi_phnum = ehdr_vdso->e_phnum; 71c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov for (size_t i = 0; i < vdso_info.dlpi_phnum; ++i) { 72c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov if (vdso_info.dlpi_phdr[i].p_type == PT_LOAD) { 73c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov vdso_info.dlpi_addr = (Elf32_Addr) ehdr_vdso - vdso_info.dlpi_phdr[i].p_vaddr; 74c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov break; 75c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov } 76c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov } 77c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov return cb(&vdso_info, sizeof(vdso_info), data); 78c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#else 79c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov // There's only the executable to try. 80c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov return cb(&exe_info, sizeof(exe_info), data); 81c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif 82405b8029a6888f386adf3512113a33546141d1c8Raghu Gandham} 83