]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/pldd-xx.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / elf / pldd-xx.c
CommitLineData
dff8da6b 1/* Copyright (C) 2011-2024 Free Software Foundation, Inc.
11988f8f 2 This file is part of the GNU C Library.
11988f8f
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
11988f8f
UD
17
18#define E(name) E_(name, CLASS)
19#define E_(name, cl) E__(name, cl)
20#define E__(name, cl) name##cl
21#define EW(type) EW_(Elf, CLASS, type)
22#define EW_(e, w, t) EW__(e, w, _##t)
23#define EW__(e, w, t) e##w##t
24
3fb18fd8
L
25#include <dl-r_debug.h>
26
11988f8f
UD
27struct E(link_map)
28{
29 EW(Addr) l_addr;
30 EW(Addr) l_name;
31 EW(Addr) l_ld;
32 EW(Addr) l_next;
33 EW(Addr) l_prev;
34 EW(Addr) l_real;
35 Lmid_t l_ns;
36 EW(Addr) l_libname;
37};
38#if CLASS == __ELF_NATIVE_CLASS
1a4c2735
AZ
39_Static_assert (offsetof (struct link_map, l_addr)
40 == offsetof (struct E(link_map), l_addr), "l_addr");
41_Static_assert (offsetof (struct link_map, l_name)
42 == offsetof (struct E(link_map), l_name), "l_name");
43_Static_assert (offsetof (struct link_map, l_next)
44 == offsetof (struct E(link_map), l_next), "l_next");
11988f8f
UD
45#endif
46
47
48struct E(libname_list)
49{
50 EW(Addr) name;
51 EW(Addr) next;
52};
53#if CLASS == __ELF_NATIVE_CLASS
1a4c2735
AZ
54_Static_assert (offsetof (struct libname_list, name)
55 == offsetof (struct E(libname_list), name), "name");
56_Static_assert (offsetof (struct libname_list, next)
57 == offsetof (struct E(libname_list), next), "next");
11988f8f
UD
58#endif
59
60struct E(r_debug)
61{
62 int r_version;
c5305d88
UD
63#if CLASS == 64
64 int pad;
65#endif
11988f8f
UD
66 EW(Addr) r_map;
67};
68#if CLASS == __ELF_NATIVE_CLASS
1a4c2735
AZ
69_Static_assert (offsetof (struct r_debug, r_version)
70 == offsetof (struct E(r_debug), r_version), "r_version");
71_Static_assert (offsetof (struct r_debug, r_map)
72 == offsetof (struct E(r_debug), r_map), "r_map");
11988f8f
UD
73#endif
74
75
76static int
c5305d88 77
1a4c2735
AZ
78E(find_maps) (const char *exe, int memfd, pid_t pid, void *auxv,
79 size_t auxv_size)
11988f8f
UD
80{
81 EW(Addr) phdr = 0;
82 unsigned int phnum = 0;
83 unsigned int phent = 0;
84
85 EW(auxv_t) *auxvXX = (EW(auxv_t) *) auxv;
86 for (int i = 0; i < auxv_size / sizeof (EW(auxv_t)); ++i)
87 switch (auxvXX[i].a_type)
88 {
89 case AT_PHDR:
90 phdr = auxvXX[i].a_un.a_val;
91 break;
92 case AT_PHNUM:
93 phnum = auxvXX[i].a_un.a_val;
94 break;
95 case AT_PHENT:
96 phent = auxvXX[i].a_un.a_val;
97 break;
98 default:
99 break;
100 }
101
102 if (phdr == 0 || phnum == 0 || phent == 0)
103 error (EXIT_FAILURE, 0, gettext ("cannot find program header of process"));
104
1a4c2735
AZ
105 EW(Phdr) *p = xmalloc (phnum * phent);
106 if (pread (memfd, p, phnum * phent, phdr) != phnum * phent)
107 error (EXIT_FAILURE, 0, gettext ("cannot read program header"));
11988f8f
UD
108
109 /* Determine the load offset. We need this for interpreting the
110 other program header entries so we do this in a separate loop.
111 Fortunately it is the first time unless someone does something
112 stupid when linking the application. */
113 EW(Addr) offset = 0;
114 for (unsigned int i = 0; i < phnum; ++i)
115 if (p[i].p_type == PT_PHDR)
116 {
117 offset = phdr - p[i].p_vaddr;
118 break;
119 }
120
121 EW(Addr) list = 0;
122 char *interp = NULL;
123 for (unsigned int i = 0; i < phnum; ++i)
124 if (p[i].p_type == PT_DYNAMIC)
125 {
126 EW(Dyn) *dyn = xmalloc (p[i].p_filesz);
1a4c2735 127 if (pread (memfd, dyn, p[i].p_filesz, offset + p[i].p_vaddr)
11988f8f 128 != p[i].p_filesz)
1a4c2735 129 error (EXIT_FAILURE, 0, gettext ("cannot read dynamic section"));
11988f8f 130
3fb18fd8 131 /* Search for the struct r_debug. */
11988f8f 132 for (unsigned int j = 0; j < p[i].p_filesz / sizeof (EW(Dyn)); ++j)
3fb18fd8
L
133 {
134 EW(Addr) off = offset + p[i].p_vaddr + sizeof (EW(Dyn)) * j;
135 off = E(r_debug_offset) (&dyn[j], memfd, off);
136 if (off != 0)
137 {
138 struct E(r_debug) r;
139 if (pread (memfd, &r, sizeof (r), off)
140 != sizeof (r))
141 error (EXIT_FAILURE, 0, gettext ("cannot read r_debug"));
142
143 if (r.r_map != 0)
144 {
145 list = r.r_map;
146 break;
147 }
148 }
149 }
11988f8f
UD
150
151 free (dyn);
152 break;
153 }
154 else if (p[i].p_type == PT_INTERP)
155 {
1a4c2735
AZ
156 interp = xmalloc (p[i].p_filesz);
157 if (pread (memfd, interp, p[i].p_filesz, offset + p[i].p_vaddr)
11988f8f 158 != p[i].p_filesz)
1a4c2735 159 error (EXIT_FAILURE, 0, gettext ("cannot read program interpreter"));
11988f8f
UD
160 }
161
162 if (list == 0)
163 {
164 if (interp == NULL)
165 {
166 // XXX check whether the executable itself is the loader
1a4c2735 167 exit (EXIT_FAILURE);
11988f8f
UD
168 }
169
170 // XXX perhaps try finding ld.so and _r_debug in it
1a4c2735 171 exit (EXIT_FAILURE);
11988f8f
UD
172 }
173
1a4c2735
AZ
174 free (p);
175 free (interp);
176
11988f8f
UD
177 /* Print the PID and program name first. */
178 printf ("%lu:\t%s\n", (unsigned long int) pid, exe);
179
180 /* Iterate over the list of objects and print the information. */
7b8399f4
FW
181 struct scratch_buffer tmpbuf;
182 scratch_buffer_init (&tmpbuf);
183 int status = 0;
11988f8f
UD
184 do
185 {
186 struct E(link_map) m;
1a4c2735
AZ
187 if (pread (memfd, &m, sizeof (m), list) != sizeof (m))
188 error (EXIT_FAILURE, 0, gettext ("cannot read link map"));
11988f8f
UD
189
190 EW(Addr) name_offset = m.l_name;
11988f8f
UD
191 while (1)
192 {
1a4c2735 193 ssize_t n = pread (memfd, tmpbuf.data, tmpbuf.length, name_offset);
11988f8f 194 if (n == -1)
1a4c2735 195 error (EXIT_FAILURE, 0, gettext ("cannot read object name"));
11988f8f 196
7b8399f4 197 if (memchr (tmpbuf.data, '\0', n) != NULL)
11988f8f
UD
198 break;
199
7b8399f4 200 if (!scratch_buffer_grow (&tmpbuf))
1a4c2735
AZ
201 error (EXIT_FAILURE, 0,
202 gettext ("cannot allocate buffer for object name"));
11988f8f
UD
203 }
204
1a4c2735
AZ
205 /* The m.l_name and m.l_libname.name for loader linkmap points to same
206 values (since BZ#387 fix). Trying to use l_libname name as the
207 shared object name might lead to an infinite loop (BZ#18035). */
11988f8f
UD
208
209 /* Skip over the executable. */
7b8399f4
FW
210 if (((char *)tmpbuf.data)[0] != '\0')
211 printf ("%s\n", (char *)tmpbuf.data);
11988f8f
UD
212
213 list = m.l_next;
214 }
215 while (list != 0);
216
7b8399f4
FW
217 scratch_buffer_free (&tmpbuf);
218 return status;
11988f8f
UD
219}
220
221
222#undef CLASS