1fcf9fc10SMark Johnston /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 35e53a4f9SPedro F. Giffuni * 48eb20f36SRui Paulo * Copyright (c) 2010 The FreeBSD Foundation 58eb20f36SRui Paulo * 68eb20f36SRui Paulo * This software was developed by Rui Paulo under sponsorship from the 78eb20f36SRui Paulo * FreeBSD Foundation. 88eb20f36SRui Paulo * 98eb20f36SRui Paulo * Redistribution and use in source and binary forms, with or without 108eb20f36SRui Paulo * modification, are permitted provided that the following conditions 118eb20f36SRui Paulo * are met: 128eb20f36SRui Paulo * 1. Redistributions of source code must retain the above copyright 138eb20f36SRui Paulo * notice, this list of conditions and the following disclaimer. 148eb20f36SRui Paulo * 2. Redistributions in binary form must reproduce the above copyright 158eb20f36SRui Paulo * notice, this list of conditions and the following disclaimer in the 168eb20f36SRui Paulo * documentation and/or other materials provided with the distribution. 178eb20f36SRui Paulo * 188eb20f36SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 198eb20f36SRui Paulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 208eb20f36SRui Paulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 218eb20f36SRui Paulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 228eb20f36SRui Paulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 238eb20f36SRui Paulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 248eb20f36SRui Paulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 258eb20f36SRui Paulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 268eb20f36SRui Paulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 278eb20f36SRui Paulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 288eb20f36SRui Paulo * SUCH DAMAGE. 298eb20f36SRui Paulo */ 308eb20f36SRui Paulo 318eb20f36SRui Paulo #include <sys/cdefs.h> 328eb20f36SRui Paulo #include <stdio.h> 338eb20f36SRui Paulo #include <stdlib.h> 3407a9c2e6SMark Johnston #include <string.h> 3507a9c2e6SMark Johnston 368eb20f36SRui Paulo #include <rtld_db.h> 37fcf9fc10SMark Johnston 388eb20f36SRui Paulo #include "_libproc.h" 398eb20f36SRui Paulo 4007a9c2e6SMark Johnston static void rdl2prmap(const rd_loadobj_t *, prmap_t *); 4107a9c2e6SMark Johnston 428eb20f36SRui Paulo static int 438eb20f36SRui Paulo map_iter(const rd_loadobj_t *lop, void *arg) 448eb20f36SRui Paulo { 4507a9c2e6SMark Johnston struct file_info *file; 4607a9c2e6SMark Johnston struct map_info *mapping, *tmp; 4707a9c2e6SMark Johnston struct proc_handle *phdl; 4807a9c2e6SMark Johnston size_t i; 498eb20f36SRui Paulo 5007a9c2e6SMark Johnston phdl = arg; 5107a9c2e6SMark Johnston if (phdl->nmappings >= phdl->maparrsz) { 5207a9c2e6SMark Johnston phdl->maparrsz *= 2; 5307a9c2e6SMark Johnston tmp = reallocarray(phdl->mappings, phdl->maparrsz, 5407a9c2e6SMark Johnston sizeof(*phdl->mappings)); 5507a9c2e6SMark Johnston if (tmp == NULL) 568eb20f36SRui Paulo return (-1); 5707a9c2e6SMark Johnston phdl->mappings = tmp; 588eb20f36SRui Paulo } 5907a9c2e6SMark Johnston 6007a9c2e6SMark Johnston mapping = &phdl->mappings[phdl->nmappings]; 6107a9c2e6SMark Johnston rdl2prmap(lop, &mapping->map); 624808a678SMark Johnston if (strcmp(lop->rdl_path, phdl->execpath) == 0 && 63acc0eea6SMark Johnston (lop->rdl_prot & RD_RDL_X) != 0) 642c73c414SMark Johnston phdl->exec_map = phdl->nmappings; 658eb20f36SRui Paulo 6607a9c2e6SMark Johnston file = NULL; 6707a9c2e6SMark Johnston if (lop->rdl_path[0] != '\0') { 6807a9c2e6SMark Johnston /* Look for an existing mapping of the same file. */ 6907a9c2e6SMark Johnston for (i = 0; i < phdl->nmappings; i++) 7007a9c2e6SMark Johnston if (strcmp(mapping->map.pr_mapname, 7107a9c2e6SMark Johnston phdl->mappings[i].map.pr_mapname) == 0) { 7207a9c2e6SMark Johnston file = phdl->mappings[i].file; 7307a9c2e6SMark Johnston break; 7407a9c2e6SMark Johnston } 7507a9c2e6SMark Johnston 7607a9c2e6SMark Johnston if (file == NULL) { 7707a9c2e6SMark Johnston file = malloc(sizeof(*file)); 7807a9c2e6SMark Johnston if (file == NULL) 7907a9c2e6SMark Johnston return (-1); 8007a9c2e6SMark Johnston file->elf = NULL; 8107a9c2e6SMark Johnston file->fd = -1; 8207a9c2e6SMark Johnston file->refs = 1; 8307a9c2e6SMark Johnston } else 8407a9c2e6SMark Johnston file->refs++; 8507a9c2e6SMark Johnston } 8607a9c2e6SMark Johnston mapping->file = file; 8707a9c2e6SMark Johnston phdl->nmappings++; 888eb20f36SRui Paulo return (0); 898eb20f36SRui Paulo } 908eb20f36SRui Paulo 9107a9c2e6SMark Johnston static void 9207a9c2e6SMark Johnston rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map) 9307a9c2e6SMark Johnston { 9407a9c2e6SMark Johnston 9507a9c2e6SMark Johnston map->pr_vaddr = rdl->rdl_saddr; 9607a9c2e6SMark Johnston map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr; 9707a9c2e6SMark Johnston map->pr_offset = rdl->rdl_offset; 9807a9c2e6SMark Johnston map->pr_mflags = 0; 9907a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_R) 10007a9c2e6SMark Johnston map->pr_mflags |= MA_READ; 10107a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_W) 10207a9c2e6SMark Johnston map->pr_mflags |= MA_WRITE; 10307a9c2e6SMark Johnston if (rdl->rdl_prot & RD_RDL_X) 10407a9c2e6SMark Johnston map->pr_mflags |= MA_EXEC; 10507a9c2e6SMark Johnston (void)strlcpy(map->pr_mapname, rdl->rdl_path, 10607a9c2e6SMark Johnston sizeof(map->pr_mapname)); 10707a9c2e6SMark Johnston } 10807a9c2e6SMark Johnston 1098eb20f36SRui Paulo rd_agent_t * 1108eb20f36SRui Paulo proc_rdagent(struct proc_handle *phdl) 1118eb20f36SRui Paulo { 11207a9c2e6SMark Johnston 1138eb20f36SRui Paulo if (phdl->rdap == NULL && phdl->status != PS_UNDEAD && 1148eb20f36SRui Paulo phdl->status != PS_IDLE) { 11507a9c2e6SMark Johnston if ((phdl->rdap = rd_new(phdl)) == NULL) 11607a9c2e6SMark Johnston return (NULL); 1178eb20f36SRui Paulo 11807a9c2e6SMark Johnston phdl->maparrsz = 64; 11907a9c2e6SMark Johnston phdl->mappings = calloc(phdl->maparrsz, 12007a9c2e6SMark Johnston sizeof(*phdl->mappings)); 12107a9c2e6SMark Johnston if (phdl->mappings == NULL) 12207a9c2e6SMark Johnston return (phdl->rdap); 12307a9c2e6SMark Johnston if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK) 12407a9c2e6SMark Johnston return (NULL); 12507a9c2e6SMark Johnston } 1268eb20f36SRui Paulo return (phdl->rdap); 1278eb20f36SRui Paulo } 1288eb20f36SRui Paulo 1298eb20f36SRui Paulo void 1308eb20f36SRui Paulo proc_updatesyms(struct proc_handle *phdl) 1318eb20f36SRui Paulo { 1324c74b245SRui Paulo 13307a9c2e6SMark Johnston memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz); 1348eb20f36SRui Paulo rd_loadobj_iter(phdl->rdap, map_iter, phdl); 1358eb20f36SRui Paulo } 136