xref: /netbsd/external/bsd/librtld_db/dist/rtld_db.c (revision 911f56e1)
1a9d541a3Schristos /*
2a9d541a3Schristos  * Copyright (c) 2010 The FreeBSD Foundation
3a9d541a3Schristos  * All rights reserved.
4a9d541a3Schristos  *
5a9d541a3Schristos  * This software was developed by Rui Paulo under sponsorship from the
6a9d541a3Schristos  * FreeBSD Foundation.
7a9d541a3Schristos  *
8a9d541a3Schristos  * Redistribution and use in source and binary forms, with or without
9a9d541a3Schristos  * modification, are permitted provided that the following conditions
10a9d541a3Schristos  * are met:
11a9d541a3Schristos  * 1. Redistributions of source code must retain the above copyright
12a9d541a3Schristos  *    notice, this list of conditions and the following disclaimer.
13a9d541a3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
14a9d541a3Schristos  *    notice, this list of conditions and the following disclaimer in the
15a9d541a3Schristos  *    documentation and/or other materials provided with the distribution.
16a9d541a3Schristos  *
17a9d541a3Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a9d541a3Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a9d541a3Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a9d541a3Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a9d541a3Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a9d541a3Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a9d541a3Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a9d541a3Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a9d541a3Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a9d541a3Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a9d541a3Schristos  * SUCH DAMAGE.
28a9d541a3Schristos  */
29a9d541a3Schristos #include <sys/cdefs.h>
306bf6036dSchristos #ifdef __FBSDID
31a9d541a3Schristos __FBSDID("$FreeBSD: head/lib/librtld_db/rtld_db.c 272488 2014-10-03 23:20:37Z markj $");
326bf6036dSchristos #else
33*911f56e1Schs __RCSID("$NetBSD: rtld_db.c,v 1.3 2016/04/26 14:26:49 chs Exp $");
346bf6036dSchristos #endif
35a9d541a3Schristos 
36a9d541a3Schristos #include <sys/types.h>
376bf6036dSchristos #include <sys/sysctl.h>
38a9d541a3Schristos 
396bf6036dSchristos #include <inttypes.h>
40a9d541a3Schristos #include <err.h>
41a9d541a3Schristos #include <stdio.h>
42a9d541a3Schristos #include <stdlib.h>
43a9d541a3Schristos #include <string.h>
44a9d541a3Schristos #include <limits.h>
45a9d541a3Schristos #include <libproc.h>
466bf6036dSchristos #include <util.h>
47a9d541a3Schristos 
48a9d541a3Schristos #include "rtld_db.h"
49a9d541a3Schristos 
50a9d541a3Schristos static int _librtld_db_debug = 0;
51a9d541a3Schristos #define DPRINTF(...) do {				\
52a9d541a3Schristos 	if (_librtld_db_debug) {			\
53a9d541a3Schristos 		fprintf(stderr, "librtld_db: DEBUG: ");	\
54a9d541a3Schristos 		fprintf(stderr, __VA_ARGS__);		\
55a9d541a3Schristos 	}						\
56a9d541a3Schristos } while (0)
57a9d541a3Schristos 
58a9d541a3Schristos void
rd_delete(rd_agent_t * rdap)59a9d541a3Schristos rd_delete(rd_agent_t *rdap)
60a9d541a3Schristos {
61a9d541a3Schristos 
62a9d541a3Schristos 	free(rdap);
63a9d541a3Schristos }
64a9d541a3Schristos 
65a9d541a3Schristos const char *
rd_errstr(rd_err_e rderr)66a9d541a3Schristos rd_errstr(rd_err_e rderr)
67a9d541a3Schristos {
68a9d541a3Schristos 
69a9d541a3Schristos 	switch (rderr) {
70a9d541a3Schristos 	case RD_ERR:
71a9d541a3Schristos 		return "generic error";
72a9d541a3Schristos 	case RD_OK:
73a9d541a3Schristos 		return "no error";
74a9d541a3Schristos 	case RD_NOCAPAB:
75a9d541a3Schristos 		return "capability not supported";
76a9d541a3Schristos 	case RD_DBERR:
77a9d541a3Schristos 		return "database error";
78a9d541a3Schristos 	case RD_NOBASE:
79a9d541a3Schristos 		return "NOBASE";
80a9d541a3Schristos 	case RD_NOMAPS:
81a9d541a3Schristos 		return "NOMAPS";
82a9d541a3Schristos 	default:
83a9d541a3Schristos 		return "unknown error";
84a9d541a3Schristos 	}
85a9d541a3Schristos }
86a9d541a3Schristos 
87a9d541a3Schristos rd_err_e
rd_event_addr(rd_agent_t * rdap,rd_event_e event,rd_notify_t * notify)88a9d541a3Schristos rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
89a9d541a3Schristos {
90a9d541a3Schristos 	rd_err_e ret;
91a9d541a3Schristos 
92a9d541a3Schristos 	DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
93a9d541a3Schristos 	    notify);
94a9d541a3Schristos 
95a9d541a3Schristos 	ret = RD_OK;
96a9d541a3Schristos 	switch (event) {
97a9d541a3Schristos 	case RD_NONE:
98a9d541a3Schristos 		break;
99a9d541a3Schristos 	case RD_PREINIT:
100a9d541a3Schristos 		notify->type = RD_NOTIFY_BPT;
101a9d541a3Schristos 		notify->u.bptaddr = rdap->rda_preinit_addr;
102a9d541a3Schristos 		break;
103a9d541a3Schristos 	case RD_POSTINIT:
104a9d541a3Schristos 		notify->type = RD_NOTIFY_BPT;
105a9d541a3Schristos 		notify->u.bptaddr = rdap->rda_postinit_addr;
106a9d541a3Schristos 		break;
107a9d541a3Schristos 	case RD_DLACTIVITY:
108a9d541a3Schristos 		notify->type = RD_NOTIFY_BPT;
109a9d541a3Schristos 		notify->u.bptaddr = rdap->rda_dlactivity_addr;
110a9d541a3Schristos 		break;
111a9d541a3Schristos 	default:
112a9d541a3Schristos 		ret = RD_ERR;
113a9d541a3Schristos 		break;
114a9d541a3Schristos 	}
115a9d541a3Schristos 	return (ret);
116a9d541a3Schristos }
117a9d541a3Schristos 
118a9d541a3Schristos rd_err_e
rd_event_enable(rd_agent_t * rdap __unused,int onoff)119a9d541a3Schristos rd_event_enable(rd_agent_t *rdap __unused, int onoff)
120a9d541a3Schristos {
121a9d541a3Schristos 	DPRINTF("%s onoff %d\n", __func__, onoff);
122a9d541a3Schristos 
123a9d541a3Schristos 	return (RD_OK);
124a9d541a3Schristos }
125a9d541a3Schristos 
126a9d541a3Schristos rd_err_e
rd_event_getmsg(rd_agent_t * rdap __unused,rd_event_msg_t * msg)127a9d541a3Schristos rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
128a9d541a3Schristos {
129a9d541a3Schristos 	DPRINTF("%s\n", __func__);
130a9d541a3Schristos 
131a9d541a3Schristos 	msg->type = RD_POSTINIT;
132a9d541a3Schristos 	msg->u.state = RD_CONSISTENT;
133a9d541a3Schristos 
134a9d541a3Schristos 	return (RD_OK);
135a9d541a3Schristos }
136a9d541a3Schristos 
137a9d541a3Schristos rd_err_e
rd_init(int version)138a9d541a3Schristos rd_init(int version)
139a9d541a3Schristos {
140a9d541a3Schristos 	char *debug = NULL;
141a9d541a3Schristos 
142a9d541a3Schristos 	if (version == RD_VERSION) {
143a9d541a3Schristos 		debug = getenv("LIBRTLD_DB_DEBUG");
144a9d541a3Schristos 		_librtld_db_debug = debug ? atoi(debug) : 0;
145a9d541a3Schristos 		return (RD_OK);
146a9d541a3Schristos 	} else
147a9d541a3Schristos 		return (RD_NOCAPAB);
148a9d541a3Schristos }
149a9d541a3Schristos 
150a9d541a3Schristos rd_err_e
rd_loadobj_iter(rd_agent_t * rdap,rl_iter_f * cb,void * clnt_data)151a9d541a3Schristos rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
152a9d541a3Schristos {
1536bf6036dSchristos 	size_t cnt, i, lastvn = 0;
154a9d541a3Schristos 	rd_loadobj_t rdl;
155a9d541a3Schristos 	struct kinfo_vmentry *kves, *kve;
156a9d541a3Schristos 
157a9d541a3Schristos 	DPRINTF("%s\n", __func__);
158a9d541a3Schristos 
159a9d541a3Schristos         if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
160a9d541a3Schristos 		warn("ERROR: kinfo_getvmmap() failed");
161a9d541a3Schristos 		return (RD_ERR);
162a9d541a3Schristos 	}
163a9d541a3Schristos 	for (i = 0; i < cnt; i++) {
164a9d541a3Schristos 		kve = kves + i;
165a9d541a3Schristos 		if (kve->kve_type == KVME_TYPE_VNODE)
166a9d541a3Schristos 			lastvn = i;
167a9d541a3Schristos 		memset(&rdl, 0, sizeof(rdl));
168a9d541a3Schristos 		/*
169a9d541a3Schristos 		 * Map the kinfo_vmentry struct to the rd_loadobj structure.
170a9d541a3Schristos 		 */
171a9d541a3Schristos 		rdl.rdl_saddr = kve->kve_start;
172a9d541a3Schristos 		rdl.rdl_eaddr = kve->kve_end;
173a9d541a3Schristos 		rdl.rdl_offset = kve->kve_offset;
174a9d541a3Schristos 		if (kve->kve_protection & KVME_PROT_READ)
175a9d541a3Schristos 			rdl.rdl_prot |= RD_RDL_R;
176a9d541a3Schristos 		if (kve->kve_protection & KVME_PROT_WRITE)
177a9d541a3Schristos 			rdl.rdl_prot |= RD_RDL_W;
178a9d541a3Schristos 		if (kve->kve_protection & KVME_PROT_EXEC)
179a9d541a3Schristos 			rdl.rdl_prot |= RD_RDL_X;
180a9d541a3Schristos 		strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
181a9d541a3Schristos 			sizeof(rdl.rdl_path));
182a9d541a3Schristos 		(*cb)(&rdl, clnt_data);
183a9d541a3Schristos 	}
184a9d541a3Schristos 	free(kves);
185a9d541a3Schristos 
186a9d541a3Schristos 	return (RD_OK);
187a9d541a3Schristos }
188a9d541a3Schristos 
189a9d541a3Schristos void
rd_log(const int onoff)190a9d541a3Schristos rd_log(const int onoff)
191a9d541a3Schristos {
192a9d541a3Schristos 	DPRINTF("%s\n", __func__);
193a9d541a3Schristos 
194a9d541a3Schristos 	(void)onoff;
195a9d541a3Schristos }
196a9d541a3Schristos 
197a9d541a3Schristos rd_agent_t *
rd_new(struct proc_handle * php)198a9d541a3Schristos rd_new(struct proc_handle *php)
199a9d541a3Schristos {
200a9d541a3Schristos 	rd_agent_t *rdap;
201a9d541a3Schristos 
202a9d541a3Schristos 	rdap = malloc(sizeof(rd_agent_t));
203a9d541a3Schristos 	if (rdap) {
204a9d541a3Schristos 		memset(rdap, 0, sizeof(rd_agent_t));
205a9d541a3Schristos 		rdap->rda_php = php;
206a9d541a3Schristos 		rd_reset(rdap);
207a9d541a3Schristos 	}
208a9d541a3Schristos 
209a9d541a3Schristos 	return (rdap);
210a9d541a3Schristos }
211a9d541a3Schristos 
212a9d541a3Schristos rd_err_e
rd_objpad_enable(rd_agent_t * rdap,size_t padsize)213a9d541a3Schristos rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
214a9d541a3Schristos {
215a9d541a3Schristos 	DPRINTF("%s\n", __func__);
216a9d541a3Schristos 
217a9d541a3Schristos 	(void)rdap;
218a9d541a3Schristos 	(void)padsize;
219a9d541a3Schristos 
220a9d541a3Schristos 	return (RD_ERR);
221a9d541a3Schristos }
222a9d541a3Schristos 
223a9d541a3Schristos rd_err_e
rd_plt_resolution(rd_agent_t * rdap,uintptr_t pc,struct proc * proc,uintptr_t plt_base,rd_plt_info_t * rpi)224a9d541a3Schristos rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
225a9d541a3Schristos     uintptr_t plt_base, rd_plt_info_t *rpi)
226a9d541a3Schristos {
227a9d541a3Schristos 	DPRINTF("%s\n", __func__);
228a9d541a3Schristos 
229a9d541a3Schristos 	(void)rdap;
230a9d541a3Schristos 	(void)pc;
231a9d541a3Schristos 	(void)proc;
232a9d541a3Schristos 	(void)plt_base;
233a9d541a3Schristos 	(void)rpi;
234a9d541a3Schristos 
235a9d541a3Schristos 	return (RD_ERR);
236a9d541a3Schristos }
237a9d541a3Schristos 
238a9d541a3Schristos rd_err_e
rd_reset(rd_agent_t * rdap)239a9d541a3Schristos rd_reset(rd_agent_t *rdap)
240a9d541a3Schristos {
241a9d541a3Schristos 	GElf_Sym sym;
242a9d541a3Schristos 
243*911f56e1Schs 	/*
244*911f56e1Schs 	 * preinit and dlactivity events are not supported yet.
245*911f56e1Schs 	 */
246a9d541a3Schristos 
247*911f56e1Schs 	rdap->rda_preinit_addr = (uintptr_t)-1;
248*911f56e1Schs 	rdap->rda_dlactivity_addr = (uintptr_t)-1;
249*911f56e1Schs 
250*911f56e1Schs 	if (proc_name2sym(rdap->rda_php, "ld.elf_so", "_rtld_debug_state",
251a9d541a3Schristos 	    &sym, NULL) < 0)
252a9d541a3Schristos 		return (RD_ERR);
253*911f56e1Schs 	DPRINTF("found _rtld_debug_state at 0x%lx\n",
254a9d541a3Schristos 	    (unsigned long)sym.st_value);
255a9d541a3Schristos 	rdap->rda_postinit_addr = sym.st_value;
256a9d541a3Schristos 
257a9d541a3Schristos 	return (RD_OK);
258a9d541a3Schristos }
259