xref: /freebsd/lib/librtld_db/rtld_db.c (revision a2aef24aa3c8458e4036735dd6928b4ef77294e5)
1 /*-
2  * Copyright (c) 2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Rui Paulo under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <sys/user.h>
36 
37 #include <assert.h>
38 #include <err.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <machine/elf.h>
47 
48 #include <libelf.h>
49 #include <libproc.h>
50 #include <libprocstat.h>
51 #include <libutil.h>
52 
53 #include "rtld_db.h"
54 
55 static int _librtld_db_debug = 0;
56 #define DPRINTF(...) do {				\
57 	if (_librtld_db_debug) {			\
58 		fprintf(stderr, "librtld_db: DEBUG: ");	\
59 		fprintf(stderr, __VA_ARGS__);		\
60 	}						\
61 } while (0)
62 
63 void
64 rd_delete(rd_agent_t *rdap)
65 {
66 
67 	if (rdap->rda_procstat != NULL)
68 		procstat_close(rdap->rda_procstat);
69 	free(rdap);
70 }
71 
72 const char *
73 rd_errstr(rd_err_e rderr)
74 {
75 
76 	switch (rderr) {
77 	case RD_ERR:
78 		return "generic error";
79 	case RD_OK:
80 		return "no error";
81 	case RD_NOCAPAB:
82 		return "capability not supported";
83 	case RD_DBERR:
84 		return "database error";
85 	case RD_NOBASE:
86 		return "NOBASE";
87 	case RD_NOMAPS:
88 		return "NOMAPS";
89 	default:
90 		return "unknown error";
91 	}
92 }
93 
94 rd_err_e
95 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
96 {
97 	rd_err_e ret;
98 
99 	DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
100 	    notify);
101 
102 	ret = RD_OK;
103 	switch (event) {
104 	case RD_NONE:
105 		break;
106 	case RD_PREINIT:
107 		notify->type = RD_NOTIFY_BPT;
108 		notify->u.bptaddr = rdap->rda_preinit_addr;
109 		break;
110 	case RD_POSTINIT:
111 		notify->type = RD_NOTIFY_BPT;
112 		notify->u.bptaddr = rdap->rda_postinit_addr;
113 		break;
114 	case RD_DLACTIVITY:
115 		notify->type = RD_NOTIFY_BPT;
116 		notify->u.bptaddr = rdap->rda_dlactivity_addr;
117 		break;
118 	default:
119 		ret = RD_ERR;
120 		break;
121 	}
122 	return (ret);
123 }
124 
125 rd_err_e
126 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
127 {
128 	DPRINTF("%s onoff %d\n", __func__, onoff);
129 
130 	return (RD_OK);
131 }
132 
133 rd_err_e
134 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
135 {
136 	DPRINTF("%s\n", __func__);
137 
138 	msg->type = RD_POSTINIT;
139 	msg->u.state = RD_CONSISTENT;
140 
141 	return (RD_OK);
142 }
143 
144 rd_err_e
145 rd_init(int version)
146 {
147 	char *debug = NULL;
148 
149 	if (version == RD_VERSION) {
150 		debug = getenv("LIBRTLD_DB_DEBUG");
151 		_librtld_db_debug = debug ? atoi(debug) : 0;
152 		return (RD_OK);
153 	} else
154 		return (RD_NOCAPAB);
155 }
156 
157 rd_err_e
158 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
159 {
160 	struct kinfo_vmentry *kves, *kve;
161 	rd_loadobj_t rdl;
162 	rd_err_e ret;
163 	int cnt, i, lastvn;
164 
165 	DPRINTF("%s\n", __func__);
166 
167 	if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
168 		warn("ERROR: kinfo_getvmmap() failed");
169 		return (RD_ERR);
170 	}
171 
172 	ret = RD_OK;
173 	lastvn = 0;
174 	for (i = 0; i < cnt; i++) {
175 		kve = kves + i;
176 		if (kve->kve_type == KVME_TYPE_VNODE)
177 			lastvn = i;
178 		memset(&rdl, 0, sizeof(rdl));
179 		/*
180 		 * Map the kinfo_vmentry struct to the rd_loadobj structure.
181 		 */
182 		rdl.rdl_saddr = kve->kve_start;
183 		rdl.rdl_eaddr = kve->kve_end;
184 		rdl.rdl_offset = kve->kve_offset;
185 		if (kve->kve_protection & KVME_PROT_READ)
186 			rdl.rdl_prot |= RD_RDL_R;
187 		if (kve->kve_protection & KVME_PROT_WRITE)
188 			rdl.rdl_prot |= RD_RDL_W;
189 		if (kve->kve_protection & KVME_PROT_EXEC)
190 			rdl.rdl_prot |= RD_RDL_X;
191 		strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
192 		    sizeof(rdl.rdl_path));
193 		if ((*cb)(&rdl, clnt_data) != 0) {
194 			ret = RD_ERR;
195 			break;
196 		}
197 	}
198 	free(kves);
199 	return (ret);
200 }
201 
202 void
203 rd_log(const int onoff)
204 {
205 	DPRINTF("%s\n", __func__);
206 
207 	(void)onoff;
208 }
209 
210 rd_agent_t *
211 rd_new(struct proc_handle *php)
212 {
213 	rd_agent_t *rdap;
214 
215 	rdap = malloc(sizeof(*rdap));
216 	if (rdap == NULL)
217 		return (NULL);
218 
219 	memset(rdap, 0, sizeof(rd_agent_t));
220 	rdap->rda_php = php;
221 	rdap->rda_procstat = procstat_open_sysctl();
222 
223 	if (rd_reset(rdap) != RD_OK) {
224 		rd_delete(rdap);
225 		rdap = NULL;
226 	}
227 	return (rdap);
228 }
229 
230 rd_err_e
231 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
232 {
233 	DPRINTF("%s\n", __func__);
234 
235 	(void)rdap;
236 	(void)padsize;
237 
238 	return (RD_ERR);
239 }
240 
241 rd_err_e
242 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
243     uintptr_t plt_base, rd_plt_info_t *rpi)
244 {
245 	DPRINTF("%s\n", __func__);
246 
247 	(void)rdap;
248 	(void)pc;
249 	(void)proc;
250 	(void)plt_base;
251 	(void)rpi;
252 
253 	return (RD_ERR);
254 }
255 
256 static int
257 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
258 {
259 	GElf_Shdr shdr;
260 	GElf_Sym sym;
261 	Elf *e;
262 	Elf_Data *data;
263 	Elf_Scn *scn;
264 	const char *symname;
265 	Elf64_Word strscnidx;
266 	int fd, i, ret;
267 
268 	ret = 1;
269 	e = NULL;
270 
271 	fd = open(rtldpath, O_RDONLY);
272 	if (fd < 0)
273 		goto err;
274 
275 	if (elf_version(EV_CURRENT) == EV_NONE)
276 		goto err;
277 	e = elf_begin(fd, ELF_C_READ, NULL);
278 	if (e == NULL)
279 		goto err;
280 
281 	scn = NULL;
282 	while ((scn = elf_nextscn(e, scn)) != NULL) {
283 		gelf_getshdr(scn, &shdr);
284 		if (shdr.sh_type == SHT_DYNSYM)
285 			break;
286 	}
287 	if (scn == NULL)
288 		goto err;
289 
290 	strscnidx = shdr.sh_link;
291 	data = elf_getdata(scn, NULL);
292 	if (data == NULL)
293 		goto err;
294 
295 	for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
296 		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
297 		    GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
298 			continue;
299 		symname = elf_strptr(e, strscnidx, sym.st_name);
300 		if (symname == NULL)
301 			continue;
302 
303 		if (strcmp(symname, "r_debug_state") == 0) {
304 			rdap->rda_preinit_addr = sym.st_value + base;
305 			rdap->rda_dlactivity_addr = sym.st_value + base;
306 		} else if (strcmp(symname, "_r_debug_postinit") == 0) {
307 			rdap->rda_postinit_addr = sym.st_value + base;
308 		}
309 	}
310 
311 	if (rdap->rda_preinit_addr != 0 &&
312 	    rdap->rda_postinit_addr != 0 &&
313 	    rdap->rda_dlactivity_addr != 0)
314 		ret = 0;
315 
316 err:
317 	if (e != NULL)
318 		(void)elf_end(e);
319 	if (fd >= 0)
320 		(void)close(fd);
321 	return (ret);
322 }
323 
324 rd_err_e
325 rd_reset(rd_agent_t *rdap)
326 {
327 	struct kinfo_proc *kp;
328 	struct kinfo_vmentry *kve;
329 	Elf_Auxinfo *auxv;
330 	const char *rtldpath;
331 	u_long base;
332 	rd_err_e rderr;
333 	int count, i;
334 
335 	kp = NULL;
336 	auxv = NULL;
337 	kve = NULL;
338 	rderr = RD_ERR;
339 
340 	kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
341 	    proc_getpid(rdap->rda_php), &count);
342 	if (kp == NULL)
343 		return (RD_ERR);
344 	assert(count == 1);
345 
346 	auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
347 	if (auxv == NULL)
348 		goto err;
349 
350 	base = 0;
351 	for (i = 0; i < count; i++) {
352 		if (auxv[i].a_type == AT_BASE) {
353 			base = auxv[i].a_un.a_val;
354 			break;
355 		}
356 	}
357 	if (i == count)
358 		goto err;
359 
360 	rtldpath = NULL;
361 	kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
362 	if (kve == NULL)
363 		goto err;
364 	for (i = 0; i < count; i++) {
365 		if (kve[i].kve_start == base) {
366 			rtldpath = kve[i].kve_path;
367 			break;
368 		}
369 	}
370 	if (i == count)
371 		goto err;
372 
373 	if (rtld_syms(rdap, rtldpath, base) != 0)
374 		goto err;
375 
376 	rderr = RD_OK;
377 
378 err:
379 	if (kve != NULL)
380 		procstat_freevmmap(rdap->rda_procstat, kve);
381 	if (auxv != NULL)
382 		procstat_freeauxv(rdap->rda_procstat, auxv);
383 	if (kp != NULL)
384 		procstat_freeprocs(rdap->rda_procstat, kp);
385 	return (rderr);
386 }
387