1 /* Copyright (C) 2011-2020 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 <https://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
37 #if CLASS == __ELF_NATIVE_CLASS
38 _Static_assert (offsetof (struct link_map
, l_addr
)
39 == offsetof (struct E(link_map
), l_addr
), "l_addr");
40 _Static_assert (offsetof (struct link_map
, l_name
)
41 == offsetof (struct E(link_map
), l_name
), "l_name");
42 _Static_assert (offsetof (struct link_map
, l_next
)
43 == offsetof (struct E(link_map
), l_next
), "l_next");
47 struct E(libname_list
)
52 #if CLASS == __ELF_NATIVE_CLASS
53 _Static_assert (offsetof (struct libname_list
, name
)
54 == offsetof (struct E(libname_list
), name
), "name");
55 _Static_assert (offsetof (struct libname_list
, next
)
56 == offsetof (struct E(libname_list
), next
), "next");
67 #if CLASS == __ELF_NATIVE_CLASS
68 _Static_assert (offsetof (struct r_debug
, r_version
)
69 == offsetof (struct E(r_debug
), r_version
), "r_version");
70 _Static_assert (offsetof (struct r_debug
, r_map
)
71 == offsetof (struct E(r_debug
), r_map
), "r_map");
77 E(find_maps
) (const char *exe
, int memfd
, pid_t pid
, void *auxv
,
81 unsigned int phnum
= 0;
82 unsigned int phent
= 0;
84 EW(auxv_t
) *auxvXX
= (EW(auxv_t
) *) auxv
;
85 for (int i
= 0; i
< auxv_size
/ sizeof (EW(auxv_t
)); ++i
)
86 switch (auxvXX
[i
].a_type
)
89 phdr
= auxvXX
[i
].a_un
.a_val
;
92 phnum
= auxvXX
[i
].a_un
.a_val
;
95 phent
= auxvXX
[i
].a_un
.a_val
;
101 if (phdr
== 0 || phnum
== 0 || phent
== 0)
102 error (EXIT_FAILURE
, 0, gettext ("cannot find program header of process"));
104 EW(Phdr
) *p
= xmalloc (phnum
* phent
);
105 if (pread (memfd
, p
, phnum
* phent
, phdr
) != phnum
* phent
)
106 error (EXIT_FAILURE
, 0, gettext ("cannot read program header"));
108 /* Determine the load offset. We need this for interpreting the
109 other program header entries so we do this in a separate loop.
110 Fortunately it is the first time unless someone does something
111 stupid when linking the application. */
113 for (unsigned int i
= 0; i
< phnum
; ++i
)
114 if (p
[i
].p_type
== PT_PHDR
)
116 offset
= phdr
- p
[i
].p_vaddr
;
122 for (unsigned int i
= 0; i
< phnum
; ++i
)
123 if (p
[i
].p_type
== PT_DYNAMIC
)
125 EW(Dyn
) *dyn
= xmalloc (p
[i
].p_filesz
);
126 if (pread (memfd
, dyn
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
128 error (EXIT_FAILURE
, 0, gettext ("cannot read dynamic section"));
130 /* Search for the DT_DEBUG entry. */
131 for (unsigned int j
= 0; j
< p
[i
].p_filesz
/ sizeof (EW(Dyn
)); ++j
)
132 if (dyn
[j
].d_tag
== DT_DEBUG
&& dyn
[j
].d_un
.d_ptr
!= 0)
135 if (pread (memfd
, &r
, sizeof (r
), dyn
[j
].d_un
.d_ptr
)
137 error (EXIT_FAILURE
, 0, gettext ("cannot read r_debug"));
149 else if (p
[i
].p_type
== PT_INTERP
)
151 interp
= xmalloc (p
[i
].p_filesz
);
152 if (pread (memfd
, interp
, p
[i
].p_filesz
, offset
+ p
[i
].p_vaddr
)
154 error (EXIT_FAILURE
, 0, gettext ("cannot read program interpreter"));
161 // XXX check whether the executable itself is the loader
165 // XXX perhaps try finding ld.so and _r_debug in it
172 /* Print the PID and program name first. */
173 printf ("%lu:\t%s\n", (unsigned long int) pid
, exe
);
175 /* Iterate over the list of objects and print the information. */
176 struct scratch_buffer tmpbuf
;
177 scratch_buffer_init (&tmpbuf
);
181 struct E(link_map
) m
;
182 if (pread (memfd
, &m
, sizeof (m
), list
) != sizeof (m
))
183 error (EXIT_FAILURE
, 0, gettext ("cannot read link map"));
185 EW(Addr
) name_offset
= m
.l_name
;
188 ssize_t n
= pread (memfd
, tmpbuf
.data
, tmpbuf
.length
, name_offset
);
190 error (EXIT_FAILURE
, 0, gettext ("cannot read object name"));
192 if (memchr (tmpbuf
.data
, '\0', n
) != NULL
)
195 if (!scratch_buffer_grow (&tmpbuf
))
196 error (EXIT_FAILURE
, 0,
197 gettext ("cannot allocate buffer for object name"));
200 /* The m.l_name and m.l_libname.name for loader linkmap points to same
201 values (since BZ#387 fix). Trying to use l_libname name as the
202 shared object name might lead to an infinite loop (BZ#18035). */
204 /* Skip over the executable. */
205 if (((char *)tmpbuf
.data
)[0] != '\0')
206 printf ("%s\n", (char *)tmpbuf
.data
);
212 scratch_buffer_free (&tmpbuf
);