elf32.c revision 1.5 1 1.5 ad /* $NetBSD: elf32.c,v 1.5 2006/12/25 11:57:40 ad Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad /*
40 1.1 ad * Copyright (c) 1996 Christopher G. Demetriou
41 1.1 ad * All rights reserved.
42 1.1 ad *
43 1.1 ad * Redistribution and use in source and binary forms, with or without
44 1.1 ad * modification, are permitted provided that the following conditions
45 1.1 ad * are met:
46 1.1 ad * 1. Redistributions of source code must retain the above copyright
47 1.1 ad * notice, this list of conditions and the following disclaimer.
48 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 ad * notice, this list of conditions and the following disclaimer in the
50 1.1 ad * documentation and/or other materials provided with the distribution.
51 1.1 ad * 3. All advertising materials mentioning features or use of this software
52 1.1 ad * must display the following acknowledgement:
53 1.1 ad * This product includes software developed for the
54 1.1 ad * NetBSD Project. See http://www.NetBSD.org/ for
55 1.1 ad * information about NetBSD.
56 1.1 ad * 4. The name of the author may not be used to endorse or promote products
57 1.1 ad * derived from this software without specific prior written permission.
58 1.1 ad *
59 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 1.1 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 1.1 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 1.1 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 1.1 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 1.1 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 1.1 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 1.1 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 1.1 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 1.1 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 1.1 ad *
70 1.1 ad * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
71 1.1 ad */
72 1.1 ad
73 1.1 ad #include <sys/cdefs.h>
74 1.1 ad #if !defined(lint)
75 1.5 ad __RCSID("$NetBSD: elf32.c,v 1.5 2006/12/25 11:57:40 ad Exp $");
76 1.1 ad #endif
77 1.1 ad
78 1.1 ad #ifndef ELFSIZE
79 1.1 ad #define ELFSIZE 32
80 1.1 ad #endif
81 1.1 ad
82 1.1 ad #include <sys/param.h>
83 1.1 ad #include <sys/exec_elf.h>
84 1.1 ad #include <sys/queue.h>
85 1.2 ad
86 1.2 ad #include <dev/lockstat.h>
87 1.1 ad
88 1.1 ad #include <stdio.h>
89 1.1 ad #include <stdlib.h>
90 1.1 ad #include <string.h>
91 1.1 ad #include <unistd.h>
92 1.1 ad #include <err.h>
93 1.1 ad
94 1.1 ad #include "extern.h"
95 1.1 ad
96 1.1 ad #if (ELFSIZE == 32)
97 1.1 ad #define NAME(x) x##32
98 1.1 ad #elif (ELFSIZE == 64)
99 1.1 ad #define NAME(x) x##64
100 1.1 ad #endif
101 1.1 ad
102 1.1 ad static int nsyms;
103 1.1 ad static Elf_Sym *symp;
104 1.1 ad static char *strp;
105 1.1 ad
106 1.1 ad int
107 1.1 ad NAME(loadsym)(int fd)
108 1.1 ad {
109 1.1 ad Elf_Shdr symhdr, strhdr;
110 1.1 ad Elf_Ehdr ehdr;
111 1.1 ad size_t sz;
112 1.1 ad off_t off;
113 1.1 ad int i;
114 1.1 ad
115 1.1 ad /*
116 1.1 ad * Read the ELF header and make sure it's OK.
117 1.1 ad */
118 1.1 ad if (pread(fd, &ehdr, sizeof(ehdr), 0) != sizeof(ehdr))
119 1.1 ad return -1;
120 1.1 ad
121 1.1 ad if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
122 1.1 ad ehdr.e_ident[EI_CLASS] != ELFCLASS)
123 1.1 ad return -1;
124 1.1 ad
125 1.1 ad switch (ehdr.e_machine) {
126 1.1 ad ELFDEFNNAME(MACHDEP_ID_CASES)
127 1.1 ad default:
128 1.1 ad return -1;
129 1.1 ad }
130 1.1 ad
131 1.1 ad /*
132 1.1 ad * Find the symbol table header, and make sure the binary isn't
133 1.1 ad * stripped.
134 1.1 ad */
135 1.1 ad off = ehdr.e_shoff;
136 1.1 ad for (i = 0; i < ehdr.e_shnum; i++, off += sizeof(symhdr)) {
137 1.1 ad sz = pread(fd, &symhdr, sizeof(symhdr), off);
138 1.1 ad if (sz != sizeof(symhdr))
139 1.1 ad err(EXIT_FAILURE, "pread (section headers)");
140 1.1 ad if (symhdr.sh_type == SHT_SYMTAB)
141 1.1 ad break;
142 1.1 ad }
143 1.1 ad if (i == ehdr.e_shnum || symhdr.sh_offset == 0)
144 1.1 ad err(EXIT_FAILURE, "namelist is stripped");
145 1.1 ad
146 1.1 ad /*
147 1.1 ad * Pull in the string table header, and then read in both the symbol
148 1.1 ad * table and string table proper.
149 1.1 ad *
150 1.1 ad * XXX We can't use mmap(), as /dev/ksyms doesn't support mmap yet.
151 1.1 ad */
152 1.1 ad off = ehdr.e_shoff + symhdr.sh_link * sizeof(symhdr);
153 1.1 ad if (pread(fd, &strhdr, sizeof(strhdr), off) != sizeof(strhdr))
154 1.1 ad err(EXIT_FAILURE, "pread");
155 1.1 ad
156 1.1 ad if ((symp = malloc(symhdr.sh_size)) == NULL)
157 1.1 ad err(EXIT_FAILURE, "malloc (symbol table)");
158 1.1 ad sz = pread(fd, symp, symhdr.sh_size, symhdr.sh_offset );
159 1.1 ad if (sz != symhdr.sh_size)
160 1.1 ad err(EXIT_FAILURE, "pread (symbol table)");
161 1.1 ad
162 1.1 ad if ((strp = malloc(strhdr.sh_size)) == NULL)
163 1.1 ad err(EXIT_FAILURE, "malloc (string table)");
164 1.1 ad sz = pread(fd, strp, strhdr.sh_size, strhdr.sh_offset);
165 1.1 ad if (sz != strhdr.sh_size)
166 1.1 ad err(EXIT_FAILURE, "pread (string table)");
167 1.1 ad
168 1.1 ad nsyms = (int)(symhdr.sh_size / sizeof(Elf_Sym));
169 1.1 ad
170 1.1 ad return 0;
171 1.1 ad }
172 1.1 ad
173 1.1 ad int
174 1.1 ad NAME(findsym)(findsym_t find, char *name, uintptr_t *start, uintptr_t *end)
175 1.1 ad {
176 1.1 ad static int lastptr[FIND_MAX];
177 1.1 ad uintptr_t sa, ea;
178 1.3 ad int i, rv, st, off;
179 1.3 ad
180 1.3 ad switch (find) {
181 1.3 ad case LOCK_BYNAME:
182 1.3 ad case LOCK_BYADDR:
183 1.3 ad st = STT_OBJECT;
184 1.3 ad break;
185 1.3 ad case FUNC_BYNAME:
186 1.3 ad case FUNC_BYADDR:
187 1.3 ad st = STT_FUNC;
188 1.3 ad break;
189 1.3 ad default:
190 1.3 ad return -1;
191 1.3 ad }
192 1.1 ad
193 1.1 ad rv = -1;
194 1.1 ad
195 1.1 ad #ifdef dump_core
196 1.1 ad for (i = lastptr[find];;) {
197 1.1 ad #else
198 1.1 ad for (i = 0; i < nsyms; i++) {
199 1.1 ad #endif
200 1.1 ad switch (find) {
201 1.1 ad case LOCK_BYNAME:
202 1.3 ad case FUNC_BYNAME:
203 1.3 ad if (ELF_ST_TYPE(symp[i].st_info) != st)
204 1.1 ad break;
205 1.1 ad if (strcmp(&strp[symp[i].st_name], name) != 0)
206 1.1 ad break;
207 1.1 ad *start = (uintptr_t)symp[i].st_value;
208 1.1 ad *end = *start + (uintptr_t)symp[i].st_size;
209 1.1 ad goto found;
210 1.1 ad
211 1.1 ad case LOCK_BYADDR:
212 1.1 ad case FUNC_BYADDR:
213 1.3 ad if (ELF_ST_TYPE(symp[i].st_info) != st)
214 1.1 ad break;
215 1.1 ad sa = (uintptr_t)symp[i].st_value;
216 1.1 ad ea = sa + (uintptr_t)symp[i].st_size - 1;
217 1.1 ad if (*start < sa || *start > ea)
218 1.1 ad break;
219 1.3 ad off = (int)(*start - sa);
220 1.3 ad if (off == 0)
221 1.5 ad strlcpy(name, &strp[symp[i].st_name],
222 1.5 ad NAME_SIZE);
223 1.3 ad else
224 1.5 ad snprintf(name, NAME_SIZE, "%s+%x",
225 1.3 ad &strp[symp[i].st_name], off);
226 1.1 ad goto found;
227 1.1 ad
228 1.1 ad default:
229 1.1 ad break;
230 1.1 ad }
231 1.1 ad
232 1.1 ad #ifdef dump_core
233 1.1 ad if (++i >= nsyms)
234 1.1 ad i = 0;
235 1.1 ad if (i == lastptr[find])
236 1.1 ad return -1;
237 1.1 ad #endif
238 1.1 ad }
239 1.1 ad
240 1.1 ad return -1;
241 1.1 ad
242 1.1 ad found:
243 1.1 ad lastptr[find] = i;
244 1.1 ad return 0;
245 1.1 ad }
246