1fcf9fc10SMark Johnston /*- 28eb20f36SRui Paulo * Copyright (c) 2010 The FreeBSD Foundation 38eb20f36SRui Paulo * All rights reserved. 48eb20f36SRui Paulo * 58eb20f36SRui Paulo * This software was developed by Rui Paulo under sponsorship from the 68eb20f36SRui Paulo * FreeBSD Foundation. 78eb20f36SRui Paulo * 88eb20f36SRui Paulo * Redistribution and use in source and binary forms, with or without 98eb20f36SRui Paulo * modification, are permitted provided that the following conditions 108eb20f36SRui Paulo * are met: 118eb20f36SRui Paulo * 1. Redistributions of source code must retain the above copyright 128eb20f36SRui Paulo * notice, this list of conditions and the following disclaimer. 138eb20f36SRui Paulo * 2. Redistributions in binary form must reproduce the above copyright 148eb20f36SRui Paulo * notice, this list of conditions and the following disclaimer in the 158eb20f36SRui Paulo * documentation and/or other materials provided with the distribution. 168eb20f36SRui Paulo * 178eb20f36SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 188eb20f36SRui Paulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198eb20f36SRui Paulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208eb20f36SRui Paulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 218eb20f36SRui Paulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228eb20f36SRui Paulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238eb20f36SRui Paulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248eb20f36SRui Paulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258eb20f36SRui Paulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268eb20f36SRui Paulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278eb20f36SRui Paulo * SUCH DAMAGE. 288eb20f36SRui Paulo */ 298eb20f36SRui Paulo 308eb20f36SRui Paulo #include <sys/cdefs.h> 318eb20f36SRui Paulo __FBSDID("$FreeBSD$"); 328eb20f36SRui Paulo 338eb20f36SRui Paulo #include <stdio.h> 348eb20f36SRui Paulo #include <stdlib.h> 35*07a9c2e6SMark Johnston #include <string.h> 36*07a9c2e6SMark Johnston 378eb20f36SRui Paulo #include <rtld_db.h> 38fcf9fc10SMark Johnston 398eb20f36SRui Paulo #include "_libproc.h" 408eb20f36SRui Paulo 41*07a9c2e6SMark Johnston static void rdl2prmap(const rd_loadobj_t *, prmap_t *); 42*07a9c2e6SMark Johnston 438eb20f36SRui Paulo static int 448eb20f36SRui Paulo map_iter(const rd_loadobj_t *lop, void *arg) 458eb20f36SRui Paulo { 46*07a9c2e6SMark Johnston struct file_info *file; 47*07a9c2e6SMark Johnston struct map_info *mapping, *tmp; 48*07a9c2e6SMark Johnston struct proc_handle *phdl; 49*07a9c2e6SMark Johnston size_t i; 508eb20f36SRui Paulo 51*07a9c2e6SMark Johnston phdl = arg; 52*07a9c2e6SMark Johnston if (phdl->nmappings >= phdl->maparrsz) { 53*07a9c2e6SMark Johnston phdl->maparrsz *= 2; 54*07a9c2e6SMark Johnston tmp = reallocarray(phdl->mappings, phdl->maparrsz, 55*07a9c2e6SMark Johnston sizeof(*phdl->mappings)); 56*07a9c2e6SMark Johnston if (tmp == NULL) 578eb20f36SRui Paulo return (-1); 58*07a9c2e6SMark Johnston phdl->mappings = tmp; 598eb20f36SRui Paulo } 60*07a9c2e6SMark Johnston 61*07a9c2e6SMark Johnston mapping = &phdl->mappings[phdl->nmappings]; 62*07a9c2e6SMark Johnston rdl2prmap(lop, &mapping->map); 634808a678SMark Johnston if (strcmp(lop->rdl_path, phdl->execpath) == 0 && 64acc0eea6SMark Johnston (lop->rdl_prot & RD_RDL_X) != 0) 65*07a9c2e6SMark Johnston phdl->exec_map = &mapping->map; 668eb20f36SRui Paulo 67*07a9c2e6SMark Johnston file = NULL; 68*07a9c2e6SMark Johnston if (lop->rdl_path[0] != '\0') { 69*07a9c2e6SMark Johnston /* Look for an existing mapping of the same file. */ 70*07a9c2e6SMark Johnston for (i = 0; i < phdl->nmappings; i++) 71*07a9c2e6SMark Johnston if (strcmp(mapping->map.pr_mapname, 72*07a9c2e6SMark Johnston phdl->mappings[i].map.pr_mapname) == 0) { 73*07a9c2e6SMark Johnston file = phdl->mappings[i].file; 74*07a9c2e6SMark Johnston break; 75*07a9c2e6SMark Johnston } 76*07a9c2e6SMark Johnston 77*07a9c2e6SMark Johnston if (file == NULL) { 78*07a9c2e6SMark Johnston file = malloc(sizeof(*file)); 79*07a9c2e6SMark Johnston if (file == NULL) 80*07a9c2e6SMark Johnston return (-1); 81*07a9c2e6SMark Johnston file->elf = NULL; 82*07a9c2e6SMark Johnston file->fd = -1; 83*07a9c2e6SMark Johnston file->refs = 1; 84*07a9c2e6SMark Johnston } else 85*07a9c2e6SMark Johnston file->refs++; 86*07a9c2e6SMark Johnston } 87*07a9c2e6SMark Johnston mapping->file = file; 88*07a9c2e6SMark Johnston phdl->nmappings++; 898eb20f36SRui Paulo return (0); 908eb20f36SRui Paulo } 918eb20f36SRui Paulo 92*07a9c2e6SMark Johnston static void 93*07a9c2e6SMark Johnston rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map) 94*07a9c2e6SMark Johnston { 95*07a9c2e6SMark Johnston 96*07a9c2e6SMark Johnston map->pr_vaddr = rdl->rdl_saddr; 97*07a9c2e6SMark Johnston map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr; 98*07a9c2e6SMark Johnston map->pr_offset = rdl->rdl_offset; 99*07a9c2e6SMark Johnston map->pr_mflags = 0; 100*07a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_R) 101*07a9c2e6SMark Johnston map->pr_mflags |= MA_READ; 102*07a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_W) 103*07a9c2e6SMark Johnston map->pr_mflags |= MA_WRITE; 104*07a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_X) 105*07a9c2e6SMark Johnston map->pr_mflags |= MA_EXEC; 106*07a9c2e6SMark Johnston (void)strlcpy(map->pr_mapname, rdl->rdl_path, 107*07a9c2e6SMark Johnston sizeof(map->pr_mapname)); 108*07a9c2e6SMark Johnston } 109*07a9c2e6SMark Johnston 1108eb20f36SRui Paulo rd_agent_t * 1118eb20f36SRui Paulo proc_rdagent(struct proc_handle *phdl) 1128eb20f36SRui Paulo { 113*07a9c2e6SMark Johnston 1148eb20f36SRui Paulo if (phdl->rdap == NULL && phdl->status != PS_UNDEAD && 1158eb20f36SRui Paulo phdl->status != PS_IDLE) { 116*07a9c2e6SMark Johnston if ((phdl->rdap = rd_new(phdl)) == NULL) 117*07a9c2e6SMark Johnston return (NULL); 1188eb20f36SRui Paulo 119*07a9c2e6SMark Johnston phdl->maparrsz = 64; 120*07a9c2e6SMark Johnston phdl->mappings = calloc(phdl->maparrsz, 121*07a9c2e6SMark Johnston sizeof(*phdl->mappings)); 122*07a9c2e6SMark Johnston if (phdl->mappings == NULL) 123*07a9c2e6SMark Johnston return (phdl->rdap); 124*07a9c2e6SMark Johnston if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK) 125*07a9c2e6SMark Johnston return (NULL); 126*07a9c2e6SMark Johnston } 1278eb20f36SRui Paulo return (phdl->rdap); 1288eb20f36SRui Paulo } 1298eb20f36SRui Paulo 1308eb20f36SRui Paulo void 1318eb20f36SRui Paulo proc_updatesyms(struct proc_handle *phdl) 1328eb20f36SRui Paulo { 1334c74b245SRui Paulo 134*07a9c2e6SMark Johnston memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz); 1358eb20f36SRui Paulo rd_loadobj_iter(phdl->rdap, map_iter, phdl); 1368eb20f36SRui Paulo } 137