1210688Srpaulo/* 2210688Srpaulo * Copyright (c) 2010 The FreeBSD Foundation 3210688Srpaulo * All rights reserved. 4210688Srpaulo * 5210688Srpaulo * This software was developed by Rui Paulo under sponsorship from the 6210688Srpaulo * FreeBSD Foundation. 7210688Srpaulo * 8210688Srpaulo * Redistribution and use in source and binary forms, with or without 9210688Srpaulo * modification, are permitted provided that the following conditions 10210688Srpaulo * are met: 11210688Srpaulo * 1. Redistributions of source code must retain the above copyright 12210688Srpaulo * notice, this list of conditions and the following disclaimer. 13210688Srpaulo * 2. Redistributions in binary form must reproduce the above copyright 14210688Srpaulo * notice, this list of conditions and the following disclaimer in the 15210688Srpaulo * documentation and/or other materials provided with the distribution. 16210688Srpaulo * 17210688Srpaulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18210688Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19210688Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20210688Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21210688Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22210688Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23210688Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24210688Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25210688Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26210688Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27210688Srpaulo * SUCH DAMAGE. 28210688Srpaulo */ 29210688Srpaulo 30210688Srpaulo#include <sys/cdefs.h> 31210688Srpaulo__FBSDID("$FreeBSD: releng/10.3/lib/libproc/proc_rtld.c 270731 2014-08-27 19:51:42Z markj $"); 32210688Srpaulo 33210688Srpaulo#include <stdio.h> 34210688Srpaulo#include <string.h> 35210688Srpaulo#include <stdlib.h> 36210688Srpaulo#include <rtld_db.h> 37210688Srpaulo#include "libproc.h" 38210688Srpaulo#include "_libproc.h" 39210688Srpaulo 40210688Srpaulostatic int 41210688Srpaulomap_iter(const rd_loadobj_t *lop, void *arg) 42210688Srpaulo{ 43210688Srpaulo struct proc_handle *phdl = arg; 44210688Srpaulo 45210688Srpaulo if (phdl->nobjs >= phdl->rdobjsz) { 46210688Srpaulo phdl->rdobjsz *= 2; 47246035Sjhb phdl->rdobjs = reallocf(phdl->rdobjs, sizeof(*phdl->rdobjs) * 48246035Sjhb phdl->rdobjsz); 49210688Srpaulo if (phdl->rdobjs == NULL) 50210688Srpaulo return (-1); 51210688Srpaulo } 52270731Smarkj if (strcmp(lop->rdl_path, phdl->execname) == 0 && 53270731Smarkj (lop->rdl_prot & RD_RDL_X) != 0) 54270731Smarkj phdl->rdexec = &phdl->rdobjs[phdl->nobjs]; 55211184Srpaulo memcpy(&phdl->rdobjs[phdl->nobjs++], lop, sizeof(*lop)); 56210688Srpaulo 57210688Srpaulo return (0); 58210688Srpaulo} 59210688Srpaulo 60210688Srpaulord_agent_t * 61210688Srpauloproc_rdagent(struct proc_handle *phdl) 62210688Srpaulo{ 63210688Srpaulo if (phdl->rdap == NULL && phdl->status != PS_UNDEAD && 64210688Srpaulo phdl->status != PS_IDLE) { 65210688Srpaulo if ((phdl->rdap = rd_new(phdl)) != NULL) { 66210688Srpaulo phdl->rdobjs = malloc(sizeof(*phdl->rdobjs) * 64); 67211184Srpaulo phdl->rdobjsz = 64; 68210688Srpaulo if (phdl->rdobjs == NULL) 69210688Srpaulo return (phdl->rdap); 70210688Srpaulo rd_loadobj_iter(phdl->rdap, map_iter, phdl); 71210688Srpaulo } 72210688Srpaulo } 73210688Srpaulo 74210688Srpaulo return (phdl->rdap); 75210688Srpaulo} 76210688Srpaulo 77210688Srpaulovoid 78210688Srpauloproc_updatesyms(struct proc_handle *phdl) 79210688Srpaulo{ 80211184Srpaulo 81211184Srpaulo memset(phdl->rdobjs, 0, sizeof(*phdl->rdobjs) * phdl->rdobjsz); 82210688Srpaulo phdl->nobjs = 0; 83210688Srpaulo rd_loadobj_iter(phdl->rdap, map_iter, phdl); 84210688Srpaulo} 85