2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
7 * This software was developed by Rui Paulo under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
48 #include <machine/elf.h>
52 #include <libprocstat.h>
57 static int _librtld_db_debug = 0;
58 #define DPRINTF(...) do { \
59 if (_librtld_db_debug) { \
60 fprintf(stderr, "librtld_db: DEBUG: "); \
61 fprintf(stderr, __VA_ARGS__); \
66 rd_delete(rd_agent_t *rdap)
69 if (rdap->rda_procstat != NULL)
70 procstat_close(rdap->rda_procstat);
75 rd_errstr(rd_err_e rderr)
80 return "generic error";
84 return "capability not supported";
86 return "database error";
92 return "unknown error";
97 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
101 DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
109 notify->type = RD_NOTIFY_BPT;
110 notify->u.bptaddr = rdap->rda_preinit_addr;
113 notify->type = RD_NOTIFY_BPT;
114 notify->u.bptaddr = rdap->rda_postinit_addr;
117 notify->type = RD_NOTIFY_BPT;
118 notify->u.bptaddr = rdap->rda_dlactivity_addr;
128 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
130 DPRINTF("%s onoff %d\n", __func__, onoff);
136 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
138 DPRINTF("%s\n", __func__);
140 msg->type = RD_POSTINIT;
141 msg->u.state = RD_CONSISTENT;
151 if (version == RD_VERSION) {
152 debug = getenv("LIBRTLD_DB_DEBUG");
153 _librtld_db_debug = debug ? atoi(debug) : 0;
160 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
162 struct kinfo_vmentry *kves, *kve;
171 DPRINTF("%s\n", __func__);
173 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
174 warn("ERROR: kinfo_getvmmap() failed");
182 for (i = 0; i < cnt; i++) {
185 * Cache the base offset of the file mapping. The kve_offset
186 * field gives the file offset of a particular mapping into the
187 * file, but we want the mapping offset relative to the base
190 if (kve->kve_type == KVME_TYPE_VNODE) {
191 if (kve->kve_vn_fileid != fileid) {
192 base = kve->kve_start;
193 fileid = kve->kve_vn_fileid;
195 path = kve->kve_path;
196 offset = kve->kve_start - base;
201 memset(&rdl, 0, sizeof(rdl));
203 * Map the kinfo_vmentry struct to the rd_loadobj structure.
205 rdl.rdl_saddr = kve->kve_start;
206 rdl.rdl_eaddr = kve->kve_end;
207 rdl.rdl_offset = offset;
208 if (kve->kve_protection & KVME_PROT_READ)
209 rdl.rdl_prot |= RD_RDL_R;
210 if (kve->kve_protection & KVME_PROT_WRITE)
211 rdl.rdl_prot |= RD_RDL_W;
212 if (kve->kve_protection & KVME_PROT_EXEC)
213 rdl.rdl_prot |= RD_RDL_X;
215 strlcpy(rdl.rdl_path, path, sizeof(rdl.rdl_path));
216 if ((*cb)(&rdl, clnt_data) != 0) {
226 rd_log(const int onoff)
228 DPRINTF("%s\n", __func__);
234 rd_new(struct proc_handle *php)
238 rdap = malloc(sizeof(*rdap));
242 memset(rdap, 0, sizeof(rd_agent_t));
244 rdap->rda_procstat = procstat_open_sysctl();
246 if (rd_reset(rdap) != RD_OK) {
254 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
256 DPRINTF("%s\n", __func__);
265 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
266 uintptr_t plt_base, rd_plt_info_t *rpi)
268 DPRINTF("%s\n", __func__);
280 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
288 Elf64_Word strscnidx;
294 fd = open(rtldpath, O_RDONLY);
298 if (elf_version(EV_CURRENT) == EV_NONE)
300 e = elf_begin(fd, ELF_C_READ, NULL);
305 while ((scn = elf_nextscn(e, scn)) != NULL) {
306 gelf_getshdr(scn, &shdr);
307 if (shdr.sh_type == SHT_DYNSYM)
313 strscnidx = shdr.sh_link;
314 data = elf_getdata(scn, NULL);
318 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
319 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
320 GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
322 symname = elf_strptr(e, strscnidx, sym.st_name);
326 if (strcmp(symname, "r_debug_state") == 0) {
327 rdap->rda_preinit_addr = sym.st_value + base;
328 rdap->rda_dlactivity_addr = sym.st_value + base;
329 } else if (strcmp(symname, "_r_debug_postinit") == 0) {
330 rdap->rda_postinit_addr = sym.st_value + base;
334 if (rdap->rda_preinit_addr != 0 &&
335 rdap->rda_postinit_addr != 0 &&
336 rdap->rda_dlactivity_addr != 0)
348 rd_reset(rd_agent_t *rdap)
350 struct kinfo_proc *kp;
351 struct kinfo_vmentry *kve;
353 const char *rtldpath;
363 kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
364 proc_getpid(rdap->rda_php), &count);
369 auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
374 for (i = 0; i < count; i++) {
375 if (auxv[i].a_type == AT_BASE) {
376 base = auxv[i].a_un.a_val;
384 kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
387 for (i = 0; i < count; i++) {
388 if (kve[i].kve_start == base) {
389 rtldpath = kve[i].kve_path;
396 if (rtld_syms(rdap, rtldpath, base) != 0)
403 procstat_freevmmap(rdap->rda_procstat, kve);
405 procstat_freeauxv(rdap->rda_procstat, auxv);
407 procstat_freeprocs(rdap->rda_procstat, kp);