1 /* Get loaded objects program headers.
2
3 Based on GNU C library (file: libc/elf/dl-iteratephdr.c)
4
5 Copyright (C) 2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
6 Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
7
8 Copyright (C) 2008 STMicroelectronics Ltd.
9 Author: Carmelo Amoroso <carmelo.amoroso@st.com>
10
11 Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
12 */
13
14
15 #include <link.h>
16 #include <ldso.h>
17
18 /* we want this in libc but nowhere else */
19 #ifdef __USE_GNU
20
21 static int
__dl_iterate_phdr(int (* callback)(struct dl_phdr_info * info,size_t size,void * data),void * data)22 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
23 {
24 int ret = 0;
25 #ifndef __ARCH_HAS_NO_SHARED__
26 struct elf_resolve *l;
27 struct dl_phdr_info info;
28
29 for (l = _dl_loaded_modules; l != NULL; l = l->next) {
30 info.dlpi_addr = l->loadaddr;
31 info.dlpi_name = l->libname;
32 info.dlpi_phdr = l->ppnt;
33 info.dlpi_phnum = l->n_phent;
34 ret = callback (&info, sizeof (struct dl_phdr_info), data);
35 if (ret)
36 break;
37 }
38 #endif
39 return ret;
40 }
41
42 # ifdef SHARED
43
44 weak_alias(__dl_iterate_phdr, dl_iterate_phdr)
45
46 # else
47
48 /* dl-support.c defines these and initializes them early on. */
49 extern ElfW(Phdr) *_dl_phdr;
50 extern size_t _dl_phnum;
51
52 int
53 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
54 size_t size, void *data), void *data)
55 {
56 if (_dl_phnum != 0)
57 {
58 /* This entry describes this statically-linked program itself. */
59 struct dl_phdr_info info;
60 int ret;
61 #if defined(__FRV_FDPIC__) || defined(__BFIN_FDPIC__) || defined(__FDPIC__)
62 info.dlpi_addr.map = NULL;
63 info.dlpi_addr.got_value = NULL;
64 #elif defined(__DSBT__)
65 info.dlpi_addr.map = NULL;
66 #else
67 info.dlpi_addr = 0;
68 #endif
69 info.dlpi_name = "";
70 info.dlpi_phdr = _dl_phdr;
71 info.dlpi_phnum = _dl_phnum;
72 ret = (*callback) (&info, sizeof (struct dl_phdr_info), data);
73 if (ret)
74 return ret;
75 }
76 /* Then invoke callback on loaded modules, if any */
77 return __dl_iterate_phdr (callback, data);
78 }
79
80 # endif
81 #endif
82