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.
4
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.
9
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.
14
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/>. */
18
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
25
26struct E(link_map)
27{
28 EW(Addr) l_addr;
29 EW(Addr) l_name;
30 EW(Addr) l_ld;
31 EW(Addr) l_next;
32 EW(Addr) l_prev;
33 EW(Addr) l_real;
34 Lmid_t l_ns;
35 EW(Addr) l_libname;
36};
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");
44#endif
45
46
47struct E(libname_list)
48{
49 EW(Addr) name;
50 EW(Addr) next;
51};
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");
57#endif
58
59struct E(r_debug)
60{
61 int r_version;
62#if CLASS == 64
63 int pad;
64#endif
65 EW(Addr) r_map;
66};
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");
72#endif
73
74
75static int
76
77E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv,
78 size_t auxv_size)
79{
80 EW(Addr) phdr = 0;
81 unsigned int phnum = 0;
82 unsigned int phent = 0;
83
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)
87 {
88 case AT_PHDR:
89 phdr = auxvXX[i].a_un.a_val;
90 break;
91 case AT_PHNUM:
92 phnum = auxvXX[i].a_un.a_val;
93 break;
94 case AT_PHENT:
95 phent = auxvXX[i].a_un.a_val;
96 break;
97 default:
98 break;
99 }
100
101 if (phdr == 0 || phnum == 0 || phent == 0)
102 error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
103
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"));
107
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. */
112 EW(Addr) offset = 0;
113 for (unsigned int i = 0; i < phnum; ++i)
114 if (p[i].p_type == PT_PHDR)
115 {
116 offset = phdr - p[i].p_vaddr;
117 break;
118 }
119
120 EW(Addr) list = 0;
121 char *interp = NULL;
122 for (unsigned int i = 0; i < phnum; ++i)
123 if (p[i].p_type == PT_DYNAMIC)
124 {
125 EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
126 if (pread (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
127 != p[i].p_filesz)
128 error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section"));
129
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)
133 {
134 struct E(r_debug) r;
135 if (pread (memfd, &r, sizeof (r), dyn[j].d_un.d_ptr)
136 != sizeof (r))
137 error (EXIT_FAILURE, 0, gettext ("cannot read r_debug"));
138
139 if (r.r_map != 0)
140 {
141 list = r.r_map;
142 break;
143 }
144 }
145
146 free (dyn);
147 break;
148 }
149 else if (p[i].p_type == PT_INTERP)
150 {
151 interp = xmalloc (p[i].p_filesz);
152 if (pread (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
153 != p[i].p_filesz)
154 error (EXIT_FAILURE, 0, gettext ("cannot read program interpreter"));
155 }
156
157 if (list == 0)
158 {
159 if (interp == NULL)
160 {
161 // XXX check whether the executable itself is the loader
162 exit (EXIT_FAILURE);
163 }
164
165 // XXX perhaps try finding ld.so and _r_debug in it
166 exit (EXIT_FAILURE);
167 }
168
169 free (p);
170 free (interp);
171
172 /* Print the PID and program name first. */
173 printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
174
175 /* Iterate over the list of objects and print the information. */
176 struct scratch_buffer tmpbuf;
177 scratch_buffer_init (&tmpbuf);
178 int status = 0;
179 do
180 {
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"));
184
185 EW(Addr) name_offset = m.l_name;
186 while (1)
187 {
188 ssize_t n = pread (memfd, tmpbuf.data, tmpbuf.length, name_offset);
189 if (n == -1)
190 error (EXIT_FAILURE, 0, gettext ("cannot read object name"));
191
192 if (memchr (tmpbuf.data, '\0', n) != NULL)
193 break;
194
195 if (!scratch_buffer_grow (&tmpbuf))
196 error (EXIT_FAILURE, 0,
197 gettext ("cannot allocate buffer for object name"));
198 }
199
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). */
203
204 /* Skip over the executable. */
205 if (((char *)tmpbuf.data)[0] != '\0')
206 printf ("%s\n", (char *)tmpbuf.data);
207
208 list = m.l_next;
209 }
210 while (list != 0);
211
212 scratch_buffer_free (&tmpbuf);
213 return status;
214}
215
216
217#undef CLASS
218