xref: /freebsd/lib/librtld_db/rtld_db.c (revision 12f05b84463baacfada5a79eaed061a4899d98aa)
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <machine/_inttypes.h>
33 #include <sys/types.h>
34 #include <sys/user.h>
35 
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <libproc.h>
42 #include <libutil.h>
43 
44 #include "rtld_db.h"
45 
46 static int _librtld_db_debug = 0;
47 #define DPRINTF(...) do {				\
48 	if (_librtld_db_debug) {			\
49 		fprintf(stderr, "librtld_db: DEBUG: ");	\
50 		fprintf(stderr, __VA_ARGS__);		\
51 	}						\
52 } while (0)
53 
54 void
55 rd_delete(rd_agent_t *rdap)
56 {
57 
58 	free(rdap);
59 }
60 
61 const char *
62 rd_errstr(rd_err_e rderr)
63 {
64 
65 	switch (rderr) {
66 	case RD_ERR:
67 		return "generic error";
68 	case RD_OK:
69 		return "no error";
70 	case RD_NOCAPAB:
71 		return "capability not supported";
72 	case RD_DBERR:
73 		return "database error";
74 	case RD_NOBASE:
75 		return "NOBASE";
76 	case RD_NOMAPS:
77 		return "NOMAPS";
78 	default:
79 		return "unknown error";
80 	}
81 }
82 
83 rd_err_e
84 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
85 {
86 	rd_err_e ret;
87 
88 	DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
89 	    notify);
90 
91 	ret = RD_OK;
92 	switch (event) {
93 	case RD_NONE:
94 		break;
95 	case RD_PREINIT:
96 		notify->type = RD_NOTIFY_BPT;
97 		notify->u.bptaddr = rdap->rda_preinit_addr;
98 		break;
99 	case RD_POSTINIT:
100 		notify->type = RD_NOTIFY_BPT;
101 		notify->u.bptaddr = rdap->rda_postinit_addr;
102 		break;
103 	case RD_DLACTIVITY:
104 		notify->type = RD_NOTIFY_BPT;
105 		notify->u.bptaddr = rdap->rda_dlactivity_addr;
106 		break;
107 	default:
108 		ret = RD_ERR;
109 		break;
110 	}
111 	return (ret);
112 }
113 
114 rd_err_e
115 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
116 {
117 	DPRINTF("%s onoff %d\n", __func__, onoff);
118 
119 	return (RD_OK);
120 }
121 
122 rd_err_e
123 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
124 {
125 	DPRINTF("%s\n", __func__);
126 
127 	msg->type = RD_POSTINIT;
128 	msg->u.state = RD_CONSISTENT;
129 
130 	return (RD_OK);
131 }
132 
133 rd_err_e
134 rd_init(int version)
135 {
136 	char *debug = NULL;
137 
138 	if (version == RD_VERSION) {
139 		debug = getenv("LIBRTLD_DB_DEBUG");
140 		_librtld_db_debug = debug ? atoi(debug) : 0;
141 		return (RD_OK);
142 	} else
143 		return (RD_NOCAPAB);
144 }
145 
146 rd_err_e
147 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
148 {
149 	int cnt, i, lastvn = 0;
150 	rd_loadobj_t rdl;
151 	struct kinfo_vmentry *kves, *kve;
152 
153 	DPRINTF("%s\n", __func__);
154 
155         if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
156 		warn("ERROR: kinfo_getvmmap() failed");
157 		return (RD_ERR);
158 	}
159 	for (i = 0; i < cnt; i++) {
160 		kve = kves + i;
161 		if (kve->kve_type == KVME_TYPE_VNODE)
162 			lastvn = i;
163 		memset(&rdl, 0, sizeof(rdl));
164 		/*
165 		 * Map the kinfo_vmentry struct to the rd_loadobj structure.
166 		 */
167 		rdl.rdl_saddr = kve->kve_start;
168 		rdl.rdl_eaddr = kve->kve_end;
169 		rdl.rdl_offset = kve->kve_offset;
170 		if (kve->kve_protection & KVME_PROT_READ)
171 			rdl.rdl_prot |= RD_RDL_R;
172 		if (kve->kve_protection & KVME_PROT_WRITE)
173 			rdl.rdl_prot |= RD_RDL_W;
174 		if (kve->kve_protection & KVME_PROT_EXEC)
175 			rdl.rdl_prot |= RD_RDL_X;
176 		strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
177 			sizeof(rdl.rdl_path));
178 		(*cb)(&rdl, clnt_data);
179 	}
180 	free(kves);
181 
182 	return (RD_OK);
183 }
184 
185 void
186 rd_log(const int onoff)
187 {
188 	DPRINTF("%s\n", __func__);
189 
190 	(void)onoff;
191 }
192 
193 rd_agent_t *
194 rd_new(struct proc_handle *php)
195 {
196 	rd_agent_t *rdap;
197 
198 	rdap = malloc(sizeof(rd_agent_t));
199 	if (rdap) {
200 		memset(rdap, 0, sizeof(rd_agent_t));
201 		rdap->rda_php = php;
202 		rd_reset(rdap);
203 	}
204 
205 	return (rdap);
206 }
207 
208 rd_err_e
209 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
210 {
211 	DPRINTF("%s\n", __func__);
212 
213 	(void)rdap;
214 	(void)padsize;
215 
216 	return (RD_ERR);
217 }
218 
219 rd_err_e
220 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
221     uintptr_t plt_base, rd_plt_info_t *rpi)
222 {
223 	DPRINTF("%s\n", __func__);
224 
225 	(void)rdap;
226 	(void)pc;
227 	(void)proc;
228 	(void)plt_base;
229 	(void)rpi;
230 
231 	return (RD_ERR);
232 }
233 
234 rd_err_e
235 rd_reset(rd_agent_t *rdap)
236 {
237 	GElf_Sym sym;
238 
239 	if (proc_name2sym(rdap->rda_php, "ld-elf.so.1", "r_debug_state",
240 	    &sym, NULL) < 0)
241 		return (RD_ERR);
242 	DPRINTF("found r_debug_state at 0x%lx\n", (unsigned long)sym.st_value);
243 	rdap->rda_preinit_addr = sym.st_value;
244 	rdap->rda_dlactivity_addr = sym.st_value;
245 
246 	if (proc_name2sym(rdap->rda_php, "ld-elf.so.1", "_r_debug_postinit",
247 	    &sym, NULL) < 0)
248 		return (RD_ERR);
249 	DPRINTF("found _r_debug_postinit at 0x%lx\n",
250 	    (unsigned long)sym.st_value);
251 	rdap->rda_postinit_addr = sym.st_value;
252 
253 	return (RD_OK);
254 }
255