• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
30 #include <linux/elf.h>
31 #include <link.h>
32 
33 /* Dynamic binaries get this from the dynamic linker (system/linker), which
34  * we don't pull in for static bins. We also don't have a list of so's to
35  * iterate over, since there's really only a single monolithic blob of
36  * code/data.
37  *
38  * All we need to do is to find where the executable is in memory, and grab the
39  * phdr and phnum from there.
40  */
41 
42 /* ld provides this to us in the default link script */
43 extern void *__executable_start;
44 
45 int
dl_iterate_phdr(int (* cb)(struct dl_phdr_info * info,size_t size,void * data),void * data)46 dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
47                 void *data)
48 {
49     struct dl_phdr_info dl_info;
50     Elf32_Ehdr *ehdr = (Elf32_Ehdr *) &__executable_start;
51     Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned long)ehdr + ehdr->e_phoff);
52 
53     /* TODO: again, copied from linker.c. Find a better home for this
54      * later. */
55     if (ehdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
56     if (ehdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
57     if (ehdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
58     if (ehdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
59 
60     dl_info.dlpi_addr = 0;
61     dl_info.dlpi_name = NULL;
62     dl_info.dlpi_phdr = phdr;
63     dl_info.dlpi_phnum = ehdr->e_phnum;
64     return cb(&dl_info, sizeof (struct dl_phdr_info), data);
65 }
66