xref: /freebsd/lib/libproc/proc_rtld.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1fcf9fc10SMark Johnston /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
48eb20f36SRui Paulo  * Copyright (c) 2010 The FreeBSD Foundation
58eb20f36SRui Paulo  * All rights reserved.
68eb20f36SRui Paulo  *
78eb20f36SRui Paulo  * This software was developed by Rui Paulo under sponsorship from the
88eb20f36SRui Paulo  * FreeBSD Foundation.
98eb20f36SRui Paulo  *
108eb20f36SRui Paulo  * Redistribution and use in source and binary forms, with or without
118eb20f36SRui Paulo  * modification, are permitted provided that the following conditions
128eb20f36SRui Paulo  * are met:
138eb20f36SRui Paulo  * 1. Redistributions of source code must retain the above copyright
148eb20f36SRui Paulo  *    notice, this list of conditions and the following disclaimer.
158eb20f36SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
168eb20f36SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
178eb20f36SRui Paulo  *    documentation and/or other materials provided with the distribution.
188eb20f36SRui Paulo  *
198eb20f36SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
208eb20f36SRui Paulo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218eb20f36SRui Paulo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228eb20f36SRui Paulo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
238eb20f36SRui Paulo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248eb20f36SRui Paulo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258eb20f36SRui Paulo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268eb20f36SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278eb20f36SRui Paulo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288eb20f36SRui Paulo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298eb20f36SRui Paulo  * SUCH DAMAGE.
308eb20f36SRui Paulo  */
318eb20f36SRui Paulo 
328eb20f36SRui Paulo #include <sys/cdefs.h>
338eb20f36SRui Paulo __FBSDID("$FreeBSD$");
348eb20f36SRui Paulo 
358eb20f36SRui Paulo #include <stdio.h>
368eb20f36SRui Paulo #include <stdlib.h>
3707a9c2e6SMark Johnston #include <string.h>
3807a9c2e6SMark Johnston 
398eb20f36SRui Paulo #include <rtld_db.h>
40fcf9fc10SMark Johnston 
418eb20f36SRui Paulo #include "_libproc.h"
428eb20f36SRui Paulo 
4307a9c2e6SMark Johnston static void	rdl2prmap(const rd_loadobj_t *, prmap_t *);
4407a9c2e6SMark Johnston 
458eb20f36SRui Paulo static int
468eb20f36SRui Paulo map_iter(const rd_loadobj_t *lop, void *arg)
478eb20f36SRui Paulo {
4807a9c2e6SMark Johnston 	struct file_info *file;
4907a9c2e6SMark Johnston 	struct map_info *mapping, *tmp;
5007a9c2e6SMark Johnston 	struct proc_handle *phdl;
5107a9c2e6SMark Johnston 	size_t i;
528eb20f36SRui Paulo 
5307a9c2e6SMark Johnston 	phdl = arg;
5407a9c2e6SMark Johnston 	if (phdl->nmappings >= phdl->maparrsz) {
5507a9c2e6SMark Johnston 		phdl->maparrsz *= 2;
5607a9c2e6SMark Johnston 		tmp = reallocarray(phdl->mappings, phdl->maparrsz,
5707a9c2e6SMark Johnston 		    sizeof(*phdl->mappings));
5807a9c2e6SMark Johnston 		if (tmp == NULL)
598eb20f36SRui Paulo 			return (-1);
6007a9c2e6SMark Johnston 		phdl->mappings = tmp;
618eb20f36SRui Paulo 	}
6207a9c2e6SMark Johnston 
6307a9c2e6SMark Johnston 	mapping = &phdl->mappings[phdl->nmappings];
6407a9c2e6SMark Johnston 	rdl2prmap(lop, &mapping->map);
654808a678SMark Johnston 	if (strcmp(lop->rdl_path, phdl->execpath) == 0 &&
66acc0eea6SMark Johnston 	    (lop->rdl_prot & RD_RDL_X) != 0)
672c73c414SMark Johnston 		phdl->exec_map = phdl->nmappings;
688eb20f36SRui Paulo 
6907a9c2e6SMark Johnston 	file = NULL;
7007a9c2e6SMark Johnston 	if (lop->rdl_path[0] != '\0') {
7107a9c2e6SMark Johnston 		/* Look for an existing mapping of the same file. */
7207a9c2e6SMark Johnston 		for (i = 0; i < phdl->nmappings; i++)
7307a9c2e6SMark Johnston 			if (strcmp(mapping->map.pr_mapname,
7407a9c2e6SMark Johnston 			    phdl->mappings[i].map.pr_mapname) == 0) {
7507a9c2e6SMark Johnston 				file = phdl->mappings[i].file;
7607a9c2e6SMark Johnston 				break;
7707a9c2e6SMark Johnston 			}
7807a9c2e6SMark Johnston 
7907a9c2e6SMark Johnston 		if (file == NULL) {
8007a9c2e6SMark Johnston 			file = malloc(sizeof(*file));
8107a9c2e6SMark Johnston 			if (file == NULL)
8207a9c2e6SMark Johnston 				return (-1);
8307a9c2e6SMark Johnston 			file->elf = NULL;
8407a9c2e6SMark Johnston 			file->fd = -1;
8507a9c2e6SMark Johnston 			file->refs = 1;
8607a9c2e6SMark Johnston 		} else
8707a9c2e6SMark Johnston 			file->refs++;
8807a9c2e6SMark Johnston 	}
8907a9c2e6SMark Johnston 	mapping->file = file;
9007a9c2e6SMark Johnston 	phdl->nmappings++;
918eb20f36SRui Paulo 	return (0);
928eb20f36SRui Paulo }
938eb20f36SRui Paulo 
9407a9c2e6SMark Johnston static void
9507a9c2e6SMark Johnston rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map)
9607a9c2e6SMark Johnston {
9707a9c2e6SMark Johnston 
9807a9c2e6SMark Johnston 	map->pr_vaddr = rdl->rdl_saddr;
9907a9c2e6SMark Johnston 	map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
10007a9c2e6SMark Johnston 	map->pr_offset = rdl->rdl_offset;
10107a9c2e6SMark Johnston 	map->pr_mflags = 0;
10207a9c2e6SMark Johnston 	if (rdl->rdl_prot & RD_RDL_R)
10307a9c2e6SMark Johnston 		map->pr_mflags |= MA_READ;
10407a9c2e6SMark Johnston 	if (rdl->rdl_prot & RD_RDL_W)
10507a9c2e6SMark Johnston 		map->pr_mflags |= MA_WRITE;
10607a9c2e6SMark Johnston 	if (rdl->rdl_prot & RD_RDL_X)
10707a9c2e6SMark Johnston 		map->pr_mflags |= MA_EXEC;
10807a9c2e6SMark Johnston 	(void)strlcpy(map->pr_mapname, rdl->rdl_path,
10907a9c2e6SMark Johnston 	    sizeof(map->pr_mapname));
11007a9c2e6SMark Johnston }
11107a9c2e6SMark Johnston 
1128eb20f36SRui Paulo rd_agent_t *
1138eb20f36SRui Paulo proc_rdagent(struct proc_handle *phdl)
1148eb20f36SRui Paulo {
11507a9c2e6SMark Johnston 
1168eb20f36SRui Paulo 	if (phdl->rdap == NULL && phdl->status != PS_UNDEAD &&
1178eb20f36SRui Paulo 	    phdl->status != PS_IDLE) {
11807a9c2e6SMark Johnston 		if ((phdl->rdap = rd_new(phdl)) == NULL)
11907a9c2e6SMark Johnston 			return (NULL);
1208eb20f36SRui Paulo 
12107a9c2e6SMark Johnston 		phdl->maparrsz = 64;
12207a9c2e6SMark Johnston 		phdl->mappings = calloc(phdl->maparrsz,
12307a9c2e6SMark Johnston 		    sizeof(*phdl->mappings));
12407a9c2e6SMark Johnston 		if (phdl->mappings == NULL)
12507a9c2e6SMark Johnston 			return (phdl->rdap);
12607a9c2e6SMark Johnston 		if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK)
12707a9c2e6SMark Johnston 			return (NULL);
12807a9c2e6SMark Johnston 	}
1298eb20f36SRui Paulo 	return (phdl->rdap);
1308eb20f36SRui Paulo }
1318eb20f36SRui Paulo 
1328eb20f36SRui Paulo void
1338eb20f36SRui Paulo proc_updatesyms(struct proc_handle *phdl)
1348eb20f36SRui Paulo {
1354c74b245SRui Paulo 
13607a9c2e6SMark Johnston 	memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz);
1378eb20f36SRui Paulo 	rd_loadobj_iter(phdl->rdap, map_iter, phdl);
1388eb20f36SRui Paulo }
139