]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/librtld_db/rtld_db.c
MFV r308954:
[FreeBSD/FreeBSD.git] / lib / librtld_db / rtld_db.c
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/queue.h>
35 #include <sys/sysctl.h>
36 #include <sys/user.h>
37
38 #include <assert.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include <machine/elf.h>
48
49 #include <libelf.h>
50 #include <libproc.h>
51 #include <libprocstat.h>
52 #include <libutil.h>
53
54 #include "rtld_db.h"
55
56 static int _librtld_db_debug = 0;
57 #define DPRINTF(...) do {                               \
58         if (_librtld_db_debug) {                        \
59                 fprintf(stderr, "librtld_db: DEBUG: "); \
60                 fprintf(stderr, __VA_ARGS__);           \
61         }                                               \
62 } while (0)
63
64 void
65 rd_delete(rd_agent_t *rdap)
66 {
67
68         if (rdap->rda_procstat != NULL)
69                 procstat_close(rdap->rda_procstat);
70         free(rdap);
71 }
72
73 const char *
74 rd_errstr(rd_err_e rderr)
75 {
76
77         switch (rderr) {
78         case RD_ERR:
79                 return "generic error";
80         case RD_OK:
81                 return "no error";
82         case RD_NOCAPAB:
83                 return "capability not supported";
84         case RD_DBERR:
85                 return "database error";
86         case RD_NOBASE:
87                 return "NOBASE";
88         case RD_NOMAPS:
89                 return "NOMAPS";
90         default:
91                 return "unknown error";
92         }
93 }
94
95 rd_err_e
96 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
97 {
98         rd_err_e ret;
99
100         DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
101             notify);
102
103         ret = RD_OK;
104         switch (event) {
105         case RD_NONE:
106                 break;
107         case RD_PREINIT:
108                 notify->type = RD_NOTIFY_BPT;
109                 notify->u.bptaddr = rdap->rda_preinit_addr;
110                 break;
111         case RD_POSTINIT:
112                 notify->type = RD_NOTIFY_BPT;
113                 notify->u.bptaddr = rdap->rda_postinit_addr;
114                 break;
115         case RD_DLACTIVITY:
116                 notify->type = RD_NOTIFY_BPT;
117                 notify->u.bptaddr = rdap->rda_dlactivity_addr;
118                 break;
119         default:
120                 ret = RD_ERR;
121                 break;
122         }
123         return (ret);
124 }
125
126 rd_err_e
127 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
128 {
129         DPRINTF("%s onoff %d\n", __func__, onoff);
130
131         return (RD_OK);
132 }
133
134 rd_err_e
135 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
136 {
137         DPRINTF("%s\n", __func__);
138
139         msg->type = RD_POSTINIT;
140         msg->u.state = RD_CONSISTENT;
141
142         return (RD_OK);
143 }
144
145 rd_err_e
146 rd_init(int version)
147 {
148         char *debug = NULL;
149
150         if (version == RD_VERSION) {
151                 debug = getenv("LIBRTLD_DB_DEBUG");
152                 _librtld_db_debug = debug ? atoi(debug) : 0;
153                 return (RD_OK);
154         } else
155                 return (RD_NOCAPAB);
156 }
157
158 rd_err_e
159 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
160 {
161         struct kinfo_vmentry *kves, *kve;
162         rd_loadobj_t rdl;
163         rd_err_e ret;
164         int cnt, i, lastvn;
165
166         DPRINTF("%s\n", __func__);
167
168         if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
169                 warn("ERROR: kinfo_getvmmap() failed");
170                 return (RD_ERR);
171         }
172
173         ret = RD_OK;
174         lastvn = 0;
175         for (i = 0; i < cnt; i++) {
176                 kve = kves + i;
177                 if (kve->kve_type == KVME_TYPE_VNODE)
178                         lastvn = i;
179                 memset(&rdl, 0, sizeof(rdl));
180                 /*
181                  * Map the kinfo_vmentry struct to the rd_loadobj structure.
182                  */
183                 rdl.rdl_saddr = kve->kve_start;
184                 rdl.rdl_eaddr = kve->kve_end;
185                 rdl.rdl_offset = kve->kve_offset;
186                 if (kve->kve_protection & KVME_PROT_READ)
187                         rdl.rdl_prot |= RD_RDL_R;
188                 if (kve->kve_protection & KVME_PROT_WRITE)
189                         rdl.rdl_prot |= RD_RDL_W;
190                 if (kve->kve_protection & KVME_PROT_EXEC)
191                         rdl.rdl_prot |= RD_RDL_X;
192                 strlcpy(rdl.rdl_path, kves[lastvn].kve_path,
193                     sizeof(rdl.rdl_path));
194                 if ((*cb)(&rdl, clnt_data) != 0) {
195                         ret = RD_ERR;
196                         break;
197                 }
198         }
199         free(kves);
200         return (ret);
201 }
202
203 void
204 rd_log(const int onoff)
205 {
206         DPRINTF("%s\n", __func__);
207
208         (void)onoff;
209 }
210
211 rd_agent_t *
212 rd_new(struct proc_handle *php)
213 {
214         rd_agent_t *rdap;
215
216         rdap = malloc(sizeof(*rdap));
217         if (rdap == NULL)
218                 return (NULL);
219
220         memset(rdap, 0, sizeof(rd_agent_t));
221         rdap->rda_php = php;
222         rdap->rda_procstat = procstat_open_sysctl();
223
224         if (rd_reset(rdap) != RD_OK) {
225                 rd_delete(rdap);
226                 rdap = NULL;
227         }
228         return (rdap);
229 }
230
231 rd_err_e
232 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
233 {
234         DPRINTF("%s\n", __func__);
235
236         (void)rdap;
237         (void)padsize;
238
239         return (RD_ERR);
240 }
241
242 rd_err_e
243 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
244     uintptr_t plt_base, rd_plt_info_t *rpi)
245 {
246         DPRINTF("%s\n", __func__);
247
248         (void)rdap;
249         (void)pc;
250         (void)proc;
251         (void)plt_base;
252         (void)rpi;
253
254         return (RD_ERR);
255 }
256
257 static int
258 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
259 {
260         GElf_Shdr shdr;
261         GElf_Sym sym;
262         Elf *e;
263         Elf_Data *data;
264         Elf_Scn *scn;
265         const char *symname;
266         Elf64_Word strscnidx;
267         int fd, i, ret;
268
269         ret = 1;
270         e = NULL;
271
272         fd = open(rtldpath, O_RDONLY);
273         if (fd < 0)
274                 goto err;
275
276         if (elf_version(EV_CURRENT) == EV_NONE)
277                 goto err;
278         e = elf_begin(fd, ELF_C_READ, NULL);
279         if (e == NULL) {
280                 close(fd);
281                 goto err;
282         }
283
284         scn = NULL;
285         while ((scn = elf_nextscn(e, scn)) != NULL) {
286                 gelf_getshdr(scn, &shdr);
287                 if (shdr.sh_type == SHT_DYNSYM)
288                         break;
289         }
290         if (scn == NULL)
291                 goto err;
292
293         strscnidx = shdr.sh_link;
294         data = elf_getdata(scn, NULL);
295         if (data == NULL)
296                 goto err;
297
298         for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
299                 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
300                     GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
301                         continue;
302                 symname = elf_strptr(e, strscnidx, sym.st_name);
303                 if (symname == NULL)
304                         continue;
305
306                 if (strcmp(symname, "r_debug_state") == 0) {
307                         rdap->rda_preinit_addr = sym.st_value + base;
308                         rdap->rda_dlactivity_addr = sym.st_value + base;
309                 } else if (strcmp(symname, "_r_debug_postinit") == 0) {
310                         rdap->rda_postinit_addr = sym.st_value + base;
311                 }
312         }
313
314         if (rdap->rda_preinit_addr != 0 &&
315             rdap->rda_postinit_addr != 0 &&
316             rdap->rda_dlactivity_addr != 0)
317                 ret = 0;
318
319 err:
320         if (e != NULL)
321                 (void)elf_end(e);
322         if (fd >= 0)
323                 (void)close(fd);
324         return (ret);
325 }
326
327 rd_err_e
328 rd_reset(rd_agent_t *rdap)
329 {
330         struct kinfo_proc *kp;
331         struct kinfo_vmentry *kve;
332         Elf_Auxinfo *auxv;
333         const char *rtldpath;
334         u_long base;
335         rd_err_e rderr;
336         int count, i;
337
338         kp = NULL;
339         auxv = NULL;
340         kve = NULL;
341         rderr = RD_ERR;
342
343         kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
344             proc_getpid(rdap->rda_php), &count);
345         if (kp == NULL)
346                 return (RD_ERR);
347         assert(count == 1);
348
349         auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
350         if (auxv == NULL)
351                 goto err;
352
353         base = 0;
354         for (i = 0; i < count; i++) {
355                 if (auxv[i].a_type == AT_BASE) {
356                         base = auxv[i].a_un.a_val;
357                         break;
358                 }
359         }
360         if (i == count)
361                 goto err;
362
363         rtldpath = NULL;
364         kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
365         if (kve == NULL)
366                 goto err;
367         for (i = 0; i < count; i++) {
368                 if (kve[i].kve_start == base) {
369                         rtldpath = kve[i].kve_path;
370                         break;
371                 }
372         }
373         if (i == count)
374                 goto err;
375
376         if (rtld_syms(rdap, rtldpath, base) != 0)
377                 goto err;
378
379         rderr = RD_OK;
380
381 err:
382         if (kve != NULL)
383                 procstat_freevmmap(rdap->rda_procstat, kve);
384         if (auxv != NULL)
385                 procstat_freeauxv(rdap->rda_procstat, auxv);
386         if (kp != NULL)
387                 procstat_freeprocs(rdap->rda_procstat, kp);
388         return (rderr);
389 }