1 /* Copyright (C) 2011-2017 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, see
17 <http://www.gnu.org/licenses/>. */
19 #define E(name) E_(name, CLASS)
20 #define E_(name, cl) E__(name, cl)
21 #define E__(name, cl) name##cl
22 #define EW(type) EW_(Elf, CLASS, type)
23 #define EW_(e, w, t) EW__(e, w, _##t)
24 #define EW__(e, w, t) e##w##t
26 #define pldd_assert(name, exp) \
27 typedef int __assert_##name[((exp) != 0) - 1]
41 #if CLASS == __ELF_NATIVE_CLASS
42 pldd_assert (l_addr
, (offsetof (struct link_map
, l_addr
)
43 == offsetof (struct E(link_map
), l_addr
)));
44 pldd_assert (l_name
, (offsetof (struct link_map
, l_name
)
45 == offsetof (struct E(link_map
), l_name
)));
46 pldd_assert (l_next
, (offsetof (struct link_map
, l_next
)
47 == offsetof (struct E(link_map
), l_next
)));
51 struct E(libname_list
)
56 #if CLASS == __ELF_NATIVE_CLASS
57 pldd_assert (name
, (offsetof (struct libname_list
, name
)
58 == offsetof (struct E(libname_list
), name
)));
59 pldd_assert (next
, (offsetof (struct libname_list
, next
)
60 == offsetof (struct E(libname_list
), next
)));
71 #if CLASS == __ELF_NATIVE_CLASS
72 pldd_assert (r_version
, (offsetof (struct r_debug
, r_version
)
73 == offsetof (struct E(r_debug
), r_version
)));
74 pldd_assert (r_map
, (offsetof (struct r_debug
, r_map
)
75 == offsetof (struct E(r_debug
), r_map
)));
81 E(find_maps
) (pid_t pid
, void *auxv
, size_t auxv_size
)
84 unsigned int phnum
= 0;
85 unsigned int phent
= 0;
87 EW(auxv_t
) *auxvXX
= (EW(auxv_t
) *) auxv
;
88 for (int i
= 0; i
< auxv_size
/ sizeof (EW(auxv_t
)); ++i
)
89 switch (auxvXX
[i
].a_type
)
92 phdr
= auxvXX
[i
].a_un
.a_val
;
95 phnum
= auxvXX
[i
].a_un
.a_val
;
98 phent
= auxvXX
[i
].a_un
.a_val
;
104 if (phdr
== 0 || phnum
== 0 || phent
== 0)
105 error (EXIT_FAILURE
, 0, gettext ("cannot find program header of process"));
107 EW(Phdr
) *p
= alloca (phnum
* phent
);
108 if (pread64 (memfd
, p
, phnum
* phent
, phdr
) != phnum
* phent
)
110 error (0, 0, gettext ("cannot read program header"));
114 /* Determine the load offset. We need this for interpreting the
115 other program header entries so we do this in a separate loop.
116 Fortunately it is the first time unless someone does something
117 stupid when linking the application. */
119 for (unsigned int i
= 0; i
< phnum
; ++i
)
120 if (p
[i
].p_type
== PT_PHDR
)
122 offset
= phdr
- p
[i
].p_vaddr
;
128 for (unsigned int i
= 0; i
< phnum
; ++i
)
129 if (p
[i
].p_type
== PT_DYNAMIC
)
131 EW(Dyn
) *dyn
= xmalloc (p
[i
].p_filesz
);
132 if (pread64 (memfd
, dyn
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
135 error (0, 0, gettext ("cannot read dynamic section"));
139 /* Search for the DT_DEBUG entry. */
140 for (unsigned int j
= 0; j
< p
[i
].p_filesz
/ sizeof (EW(Dyn
)); ++j
)
141 if (dyn
[j
].d_tag
== DT_DEBUG
&& dyn
[j
].d_un
.d_ptr
!= 0)
144 if (pread64 (memfd
, &r
, sizeof (r
), dyn
[j
].d_un
.d_ptr
)
147 error (0, 0, gettext ("cannot read r_debug"));
161 else if (p
[i
].p_type
== PT_INTERP
)
163 interp
= alloca (p
[i
].p_filesz
);
164 if (pread64 (memfd
, interp
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
167 error (0, 0, gettext ("cannot read program interpreter"));
176 // XXX check whether the executable itself is the loader
180 // XXX perhaps try finding ld.so and _r_debug in it
185 /* Print the PID and program name first. */
186 printf ("%lu:\t%s\n", (unsigned long int) pid
, exe
);
188 /* Iterate over the list of objects and print the information. */
189 struct scratch_buffer tmpbuf
;
190 scratch_buffer_init (&tmpbuf
);
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"));
198 status
= EXIT_FAILURE
;
202 EW(Addr
) name_offset
= m
.l_name
;
206 ssize_t n
= pread64 (memfd
, tmpbuf
.data
, tmpbuf
.length
, name_offset
);
209 error (0, 0, gettext ("cannot read object name"));
210 status
= EXIT_FAILURE
;
214 if (memchr (tmpbuf
.data
, '\0', n
) != NULL
)
217 if (!scratch_buffer_grow (&tmpbuf
))
219 error (0, 0, gettext ("cannot allocate buffer for object name"));
220 status
= EXIT_FAILURE
;
225 if (((char *)tmpbuf
.data
)[0] == '\0' && name_offset
== m
.l_name
228 /* Try the l_libname element. */
229 struct E(libname_list
) ln
;
230 if (pread64 (memfd
, &ln
, sizeof (ln
), m
.l_libname
) == sizeof (ln
))
232 name_offset
= ln
.name
;
237 /* Skip over the executable. */
238 if (((char *)tmpbuf
.data
)[0] != '\0')
239 printf ("%s\n", (char *)tmpbuf
.data
);
246 scratch_buffer_free (&tmpbuf
);