• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* bionic/arch-x86/bionic/dl_iterate_phdr_static.c
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 **     * Redistributions of source code must retain the above copyright
8 **       notice, this list of conditions and the following disclaimer.
9 **     * Redistributions in binary form must reproduce the above copyright
10 **       notice, this list of conditions and the following disclaimer in the
11 **       documentation and/or other materials provided with the distribution.
12 **     * Neither the name of Google Inc. nor the names of its contributors may
13 **       be used to endorse or promote products derived from this software
14 **       without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <sys/types.h>
29 #include <linux/elf.h>
30 
31 /* TODO: Move this into a header that linker.h can also pull it in.
32  * Silly to have same struct in 2 places. This is temporary. */
33 struct dl_phdr_info
34 {
35     Elf32_Addr dlpi_addr;
36     const char *dlpi_name;
37     const Elf32_Phdr *dlpi_phdr;
38     Elf32_Half dlpi_phnum;
39 };
40 
41 /* Dynamic binaries get this from the dynamic linker (system/linker), which
42  * we don't pull in for static bins. We also don't have a list of so's to
43  * iterate over, since there's really only a single monolithic blob of
44  * code/data.
45  *
46  * All we need to do is to find where the executable is in memory, and grab the
47  * phdr and phnum from there.
48  */
49 
50 /* ld provides this to us in the default link script */
51 extern void *__executable_start;
52 
53 int
dl_iterate_phdr(int (* cb)(struct dl_phdr_info * info,size_t size,void * data),void * data)54 dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
55                 void *data)
56 {
57     struct dl_phdr_info dl_info;
58     Elf32_Ehdr *ehdr = (Elf32_Ehdr *) &__executable_start;
59     Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned long)ehdr + ehdr->e_phoff);
60 
61     /* TODO: again, copied from linker.c. Find a better home for this
62      * later. */
63     if (ehdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
64     if (ehdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
65     if (ehdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
66     if (ehdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
67 
68     dl_info.dlpi_addr = 0;
69     dl_info.dlpi_name = NULL;
70     dl_info.dlpi_phdr = phdr;
71     dl_info.dlpi_phnum = ehdr->e_phnum;
72     return cb(&dl_info, sizeof (struct dl_phdr_info), data);
73 }
74 
75