1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #define E(name) E_(name, CLASS)
21 #define E_(name, cl) E__(name, cl)
22 #define E__(name, cl) name##cl
23 #define EW(type) EW_(Elf, CLASS, type)
24 #define EW_(e, w, t) EW__(e, w, _##t)
25 #define EW__(e, w, t) e##w##t
27 #define static_assert(name, exp) \
28 typedef int __assert_##name[((exp) != 0) - 1]
42 #if CLASS == __ELF_NATIVE_CLASS
43 static_assert (l_addr
, (offsetof (struct link_map
, l_addr
)
44 == offsetof (struct E(link_map
), l_addr
)));
45 static_assert (l_name
, (offsetof (struct link_map
, l_name
)
46 == offsetof (struct E(link_map
), l_name
)));
47 static_assert (l_next
, (offsetof (struct link_map
, l_next
)
48 == offsetof (struct E(link_map
), l_next
)));
52 struct E(libname_list
)
57 #if CLASS == __ELF_NATIVE_CLASS
58 static_assert (name
, (offsetof (struct libname_list
, name
)
59 == offsetof (struct E(libname_list
), name
)));
60 static_assert (next
, (offsetof (struct libname_list
, next
)
61 == offsetof (struct E(libname_list
), next
)));
72 #if CLASS == __ELF_NATIVE_CLASS
73 static_assert (r_version
, (offsetof (struct r_debug
, r_version
)
74 == offsetof (struct E(r_debug
), r_version
)));
75 static_assert (r_map
, (offsetof (struct r_debug
, r_map
)
76 == offsetof (struct E(r_debug
), r_map
)));
82 E(find_maps
) (pid_t pid
, void *auxv
, size_t auxv_size
)
85 unsigned int phnum
= 0;
86 unsigned int phent
= 0;
88 EW(auxv_t
) *auxvXX
= (EW(auxv_t
) *) auxv
;
89 for (int i
= 0; i
< auxv_size
/ sizeof (EW(auxv_t
)); ++i
)
90 switch (auxvXX
[i
].a_type
)
93 phdr
= auxvXX
[i
].a_un
.a_val
;
96 phnum
= auxvXX
[i
].a_un
.a_val
;
99 phent
= auxvXX
[i
].a_un
.a_val
;
105 if (phdr
== 0 || phnum
== 0 || phent
== 0)
106 error (EXIT_FAILURE
, 0, gettext ("cannot find program header of process"));
108 EW(Phdr
) *p
= alloca (phnum
* phent
);
109 if (pread64 (memfd
, p
, phnum
* phent
, phdr
) != phnum
* phent
)
111 error (0, 0, gettext ("cannot read program header"));
115 /* Determine the load offset. We need this for interpreting the
116 other program header entries so we do this in a separate loop.
117 Fortunately it is the first time unless someone does something
118 stupid when linking the application. */
120 for (unsigned int i
= 0; i
< phnum
; ++i
)
121 if (p
[i
].p_type
== PT_PHDR
)
123 offset
= phdr
- p
[i
].p_vaddr
;
129 for (unsigned int i
= 0; i
< phnum
; ++i
)
130 if (p
[i
].p_type
== PT_DYNAMIC
)
132 EW(Dyn
) *dyn
= xmalloc (p
[i
].p_filesz
);
133 if (pread64 (memfd
, dyn
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
136 error (0, 0, gettext ("cannot read dynamic section"));
140 /* Search for the DT_DEBUG entry. */
141 for (unsigned int j
= 0; j
< p
[i
].p_filesz
/ sizeof (EW(Dyn
)); ++j
)
142 if (dyn
[j
].d_tag
== DT_DEBUG
&& dyn
[j
].d_un
.d_ptr
!= 0)
145 if (pread64 (memfd
, &r
, sizeof (r
), dyn
[j
].d_un
.d_ptr
)
148 error (0, 0, gettext ("cannot read r_debug"));
162 else if (p
[i
].p_type
== PT_INTERP
)
164 interp
= alloca (p
[i
].p_filesz
);
165 if (pread64 (memfd
, interp
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
168 error (0, 0, gettext ("cannot read program interpreter"));
177 // XXX check whether the executable itself is the loader
181 // XXX perhaps try finding ld.so and _r_debug in it
186 /* Print the PID and program name first. */
187 printf ("%lu:\t%s\n", (unsigned long int) pid
, exe
);
189 /* Iterate over the list of objects and print the information. */
190 size_t strsize
= 256;
191 char *str
= alloca (strsize
);
194 struct E(link_map
) m
;
195 if (pread64 (memfd
, &m
, sizeof (m
), list
) != sizeof (m
))
197 error (0, 0, gettext ("cannot read link map"));
201 EW(Addr
) name_offset
= m
.l_name
;
205 ssize_t n
= pread64 (memfd
, str
, strsize
, name_offset
);
208 error (0, 0, gettext ("cannot read object name"));
212 if (memchr (str
, '\0', n
) != NULL
)
215 str
= extend_alloca (str
, strsize
, strsize
* 2);
218 if (str
[0] == '\0' && name_offset
== m
.l_name
221 /* Try the l_libname element. */
222 struct E(libname_list
) ln
;
223 if (pread64 (memfd
, &ln
, sizeof (ln
), m
.l_libname
) == sizeof (ln
))
225 name_offset
= ln
.name
;
230 /* Skip over the executable. */
232 printf ("%s\n", str
);