2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
33 #include <sys/sysctl.h>
45 #include <machine/elf.h>
49 #include <libprocstat.h>
54 static int _librtld_db_debug = 0;
55 #define DPRINTF(...) do { \
56 if (_librtld_db_debug) { \
57 fprintf(stderr, "librtld_db: DEBUG: "); \
58 fprintf(stderr, __VA_ARGS__); \
63 rd_delete(rd_agent_t *rdap)
66 if (rdap->rda_procstat != NULL)
67 procstat_close(rdap->rda_procstat);
72 rd_errstr(rd_err_e rderr)
77 return "generic error";
81 return "capability not supported";
83 return "database error";
89 return "unknown error";
94 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
98 DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
106 notify->type = RD_NOTIFY_BPT;
107 notify->u.bptaddr = rdap->rda_preinit_addr;
110 notify->type = RD_NOTIFY_BPT;
111 notify->u.bptaddr = rdap->rda_postinit_addr;
114 notify->type = RD_NOTIFY_BPT;
115 notify->u.bptaddr = rdap->rda_dlactivity_addr;
125 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
127 DPRINTF("%s onoff %d\n", __func__, onoff);
133 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
135 DPRINTF("%s\n", __func__);
137 msg->type = RD_POSTINIT;
138 msg->u.state = RD_CONSISTENT;
148 if (version == RD_VERSION) {
149 debug = getenv("LIBRTLD_DB_DEBUG");
150 _librtld_db_debug = debug ? atoi(debug) : 0;
157 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
159 struct kinfo_vmentry *kves, *kve;
168 DPRINTF("%s\n", __func__);
170 if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
171 warn("ERROR: kinfo_getvmmap() failed");
179 for (i = 0; i < cnt; i++) {
182 * Cache the base offset of the file mapping. The kve_offset
183 * field gives the file offset of a particular mapping into the
184 * file, but we want the mapping offset relative to the base
187 if (kve->kve_type == KVME_TYPE_VNODE) {
188 if (kve->kve_vn_fileid != fileid) {
189 base = kve->kve_start;
190 fileid = kve->kve_vn_fileid;
192 path = kve->kve_path;
193 offset = kve->kve_start - base;
198 memset(&rdl, 0, sizeof(rdl));
200 * Map the kinfo_vmentry struct to the rd_loadobj structure.
202 rdl.rdl_saddr = kve->kve_start;
203 rdl.rdl_eaddr = kve->kve_end;
204 rdl.rdl_offset = offset;
205 if (kve->kve_protection & KVME_PROT_READ)
206 rdl.rdl_prot |= RD_RDL_R;
207 if (kve->kve_protection & KVME_PROT_WRITE)
208 rdl.rdl_prot |= RD_RDL_W;
209 if (kve->kve_protection & KVME_PROT_EXEC)
210 rdl.rdl_prot |= RD_RDL_X;
212 strlcpy(rdl.rdl_path, path, sizeof(rdl.rdl_path));
213 if ((*cb)(&rdl, clnt_data) != 0) {
223 rd_log(const int onoff)
225 DPRINTF("%s\n", __func__);
231 rd_new(struct proc_handle *php)
235 rdap = malloc(sizeof(*rdap));
239 memset(rdap, 0, sizeof(rd_agent_t));
241 rdap->rda_procstat = procstat_open_sysctl();
243 if (rd_reset(rdap) != RD_OK) {
251 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
253 DPRINTF("%s\n", __func__);
262 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
263 uintptr_t plt_base, rd_plt_info_t *rpi)
265 DPRINTF("%s\n", __func__);
277 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
285 Elf64_Word strscnidx;
291 fd = open(rtldpath, O_RDONLY);
295 if (elf_version(EV_CURRENT) == EV_NONE)
297 e = elf_begin(fd, ELF_C_READ, NULL);
302 while ((scn = elf_nextscn(e, scn)) != NULL) {
303 gelf_getshdr(scn, &shdr);
304 if (shdr.sh_type == SHT_DYNSYM)
310 strscnidx = shdr.sh_link;
311 data = elf_getdata(scn, NULL);
315 for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
316 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
317 GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
319 symname = elf_strptr(e, strscnidx, sym.st_name);
323 if (strcmp(symname, "r_debug_state") == 0) {
324 rdap->rda_preinit_addr = sym.st_value + base;
325 rdap->rda_dlactivity_addr = sym.st_value + base;
326 } else if (strcmp(symname, "_r_debug_postinit") == 0) {
327 rdap->rda_postinit_addr = sym.st_value + base;
331 if (rdap->rda_preinit_addr != 0 &&
332 rdap->rda_postinit_addr != 0 &&
333 rdap->rda_dlactivity_addr != 0)
345 rd_reset(rd_agent_t *rdap)
347 struct kinfo_proc *kp;
348 struct kinfo_vmentry *kve;
350 const char *rtldpath;
360 kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
361 proc_getpid(rdap->rda_php), &count);
366 auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
371 for (i = 0; i < count; i++) {
372 if (auxv[i].a_type == AT_BASE) {
373 base = auxv[i].a_un.a_val;
381 kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
384 for (i = 0; i < count; i++) {
385 if (kve[i].kve_start == base) {
386 rtldpath = kve[i].kve_path;
393 if (rtld_syms(rdap, rtldpath, base) != 0)
400 procstat_freevmmap(rdap->rda_procstat, kve);
402 procstat_freeauxv(rdap->rda_procstat, auxv);
404 procstat_freeprocs(rdap->rda_procstat, kp);