xref: /freebsd/lib/libproc/proc_rtld.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Rui Paulo under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include <rtld_db.h>
38 
39 #include "_libproc.h"
40 
41 static void	rdl2prmap(const rd_loadobj_t *, prmap_t *);
42 
43 static int
44 map_iter(const rd_loadobj_t *lop, void *arg)
45 {
46 	struct file_info *file;
47 	struct map_info *mapping, *tmp;
48 	struct proc_handle *phdl;
49 	size_t i;
50 
51 	phdl = arg;
52 	if (phdl->nmappings >= phdl->maparrsz) {
53 		phdl->maparrsz *= 2;
54 		tmp = reallocarray(phdl->mappings, phdl->maparrsz,
55 		    sizeof(*phdl->mappings));
56 		if (tmp == NULL)
57 			return (-1);
58 		phdl->mappings = tmp;
59 	}
60 
61 	mapping = &phdl->mappings[phdl->nmappings];
62 	rdl2prmap(lop, &mapping->map);
63 	if (strcmp(lop->rdl_path, phdl->execpath) == 0 &&
64 	    (lop->rdl_prot & RD_RDL_X) != 0)
65 		phdl->exec_map = &mapping->map;
66 
67 	file = NULL;
68 	if (lop->rdl_path[0] != '\0') {
69 		/* Look for an existing mapping of the same file. */
70 		for (i = 0; i < phdl->nmappings; i++)
71 			if (strcmp(mapping->map.pr_mapname,
72 			    phdl->mappings[i].map.pr_mapname) == 0) {
73 				file = phdl->mappings[i].file;
74 				break;
75 			}
76 
77 		if (file == NULL) {
78 			file = malloc(sizeof(*file));
79 			if (file == NULL)
80 				return (-1);
81 			file->elf = NULL;
82 			file->fd = -1;
83 			file->refs = 1;
84 		} else
85 			file->refs++;
86 	}
87 	mapping->file = file;
88 	phdl->nmappings++;
89 	return (0);
90 }
91 
92 static void
93 rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map)
94 {
95 
96 	map->pr_vaddr = rdl->rdl_saddr;
97 	map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
98 	map->pr_offset = rdl->rdl_offset;
99 	map->pr_mflags = 0;
100 	if (rdl->rdl_prot & RD_RDL_R)
101 		map->pr_mflags |= MA_READ;
102 	if (rdl->rdl_prot & RD_RDL_W)
103 		map->pr_mflags |= MA_WRITE;
104 	if (rdl->rdl_prot & RD_RDL_X)
105 		map->pr_mflags |= MA_EXEC;
106 	(void)strlcpy(map->pr_mapname, rdl->rdl_path,
107 	    sizeof(map->pr_mapname));
108 }
109 
110 rd_agent_t *
111 proc_rdagent(struct proc_handle *phdl)
112 {
113 
114 	if (phdl->rdap == NULL && phdl->status != PS_UNDEAD &&
115 	    phdl->status != PS_IDLE) {
116 		if ((phdl->rdap = rd_new(phdl)) == NULL)
117 			return (NULL);
118 
119 		phdl->maparrsz = 64;
120 		phdl->mappings = calloc(phdl->maparrsz,
121 		    sizeof(*phdl->mappings));
122 		if (phdl->mappings == NULL)
123 			return (phdl->rdap);
124 		if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK)
125 			return (NULL);
126 	}
127 	return (phdl->rdap);
128 }
129 
130 void
131 proc_updatesyms(struct proc_handle *phdl)
132 {
133 
134 	memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz);
135 	rd_loadobj_iter(phdl->rdap, map_iter, phdl);
136 }
137