blob: 4eecff6fc90d089e281884212bbe7e08e85e680f [file] [log] [blame]
Raghu Gandham405b8022012-07-25 18:16:42 -07001/*
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 <sys/types.h>
30#include <linux/elf.h>
31
32/* TODO: Move this into a header that linker.h can also pull it in.
33 * Silly to have same struct in 2 places. This is temporary. */
34struct dl_phdr_info
35{
36 Elf32_Addr dlpi_addr;
37 const char *dlpi_name;
38 const Elf32_Phdr *dlpi_phdr;
39 Elf32_Half dlpi_phnum;
40};
41
42/* Dynamic binaries get this from the dynamic linker (system/linker), which
43 * we don't pull in for static bins. We also don't have a list of so's to
44 * iterate over, since there's really only a single monolithic blob of
45 * code/data.
46 *
47 * All we need to do is to find where the executable is in memory, and grab the
48 * phdr and phnum from there.
49 */
50
51/* ld provides this to us in the default link script */
52extern void *__executable_start;
53
54int
55dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
56 void *data)
57{
58 struct dl_phdr_info dl_info;
59 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) &__executable_start;
60 Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned long)ehdr + ehdr->e_phoff);
61
62 /* TODO: again, copied from linker.c. Find a better home for this
63 * later. */
64 if (ehdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
65 if (ehdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
66 if (ehdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
67 if (ehdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
68
69 dl_info.dlpi_addr = 0;
70 dl_info.dlpi_name = NULL;
71 dl_info.dlpi_phdr = phdr;
72 dl_info.dlpi_phnum = ehdr->e_phnum;
73 return cb(&dl_info, sizeof (struct dl_phdr_info), data);
74}