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