Print this page
11210 libm should be cstyle(1ONBLD) clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/__fex_sym.c
+++ new/usr/src/lib/libm/common/m9x/__fex_sym.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 +
25 26 /*
26 27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 28 * Use is subject to license terms.
28 29 */
29 30
30 31 #include <elf.h>
31 32 #include <stdio.h>
32 33 #include <stdlib.h>
33 34 #include <unistd.h>
34 35 #include <fcntl.h>
35 36 #include <procfs.h>
36 37 #include <string.h>
37 38 #include <sys/stat.h>
38 39
39 40 #if defined(__sparcv9) || defined(__amd64)
40 -
41 -#define Elf_Ehdr Elf64_Ehdr
42 -#define Elf_Phdr Elf64_Phdr
43 -#define Elf_Shdr Elf64_Shdr
44 -#define Elf_Sym Elf64_Sym
45 -#define ELF_ST_BIND ELF64_ST_BIND
46 -#define ELF_ST_TYPE ELF64_ST_TYPE
47 -
41 +#define Elf_Ehdr Elf64_Ehdr
42 +#define Elf_Phdr Elf64_Phdr
43 +#define Elf_Shdr Elf64_Shdr
44 +#define Elf_Sym Elf64_Sym
45 +#define ELF_ST_BIND ELF64_ST_BIND
46 +#define ELF_ST_TYPE ELF64_ST_TYPE
48 47 #else
49 -
50 -#define Elf_Ehdr Elf32_Ehdr
51 -#define Elf_Phdr Elf32_Phdr
52 -#define Elf_Shdr Elf32_Shdr
53 -#define Elf_Sym Elf32_Sym
54 -#define ELF_ST_BIND ELF32_ST_BIND
55 -#define ELF_ST_TYPE ELF32_ST_TYPE
56 -
57 -#endif /* __sparcv9 */
48 +#define Elf_Ehdr Elf32_Ehdr
49 +#define Elf_Phdr Elf32_Phdr
50 +#define Elf_Shdr Elf32_Shdr
51 +#define Elf_Sym Elf32_Sym
52 +#define ELF_ST_BIND ELF32_ST_BIND
53 +#define ELF_ST_TYPE ELF32_ST_TYPE
54 +#endif /* __sparcv9 */
58 55
59 56 /* semi-permanent data established by __fex_sym_init */
60 -static prmap_t *pm = NULL; /* prmap_t array */
61 -static int npm = 0; /* number of entries in pm */
57 +static prmap_t *pm = NULL; /* prmap_t array */
58 +static int npm = 0; /* number of entries in pm */
62 59
63 60 /* transient data modified by __fex_sym */
64 -static prmap_t *lpm = NULL; /* prmap_t found in last call */
65 -static Elf_Phdr *ph = NULL; /* program header array */
66 -static int phsize = 0; /* size of ph */
67 -static int nph; /* number of entries in ph */
68 -static char *stbuf = NULL; /* symbol and string table buffer */
69 -static int stbufsize = 0; /* size of stbuf */
70 -static int stoffset; /* offset of string table in stbuf */
71 -static int nsyms; /* number of symbols in stbuf */
61 +static prmap_t *lpm = NULL; /* prmap_t found in last call */
62 +static Elf_Phdr *ph = NULL; /* program header array */
63 +static int phsize = 0; /* size of ph */
64 +static int nph; /* number of entries in ph */
65 +static char *stbuf = NULL; /* symbol and string table buffer */
66 +static int stbufsize = 0; /* size of stbuf */
67 +static int stoffset; /* offset of string table in stbuf */
68 +static int nsyms; /* number of symbols in stbuf */
72 69
73 70 /* get a current prmap_t list (must call this before each stack trace) */
74 71 void
75 72 __fex_sym_init()
76 73 {
77 - struct stat statbuf;
78 - long n;
79 - int i;
74 + struct stat statbuf;
75 + long n;
76 + int i;
80 77
81 78 /* clear out the previous prmap_t list */
82 79 if (pm != NULL)
83 80 free(pm);
81 +
84 82 pm = lpm = NULL;
85 83 npm = 0;
86 84
87 85 /* get the current prmap_t list */
88 86 if (stat("/proc/self/map", &statbuf) < 0 || statbuf.st_size <= 0 ||
89 - (pm = (prmap_t*)malloc(statbuf.st_size)) == NULL)
87 + (pm = (prmap_t *)malloc(statbuf.st_size)) == NULL)
90 88 return;
91 - if ((i = open("/proc/self/map", O_RDONLY)) < 0)
92 - {
89 +
90 + if ((i = open("/proc/self/map", O_RDONLY)) < 0) {
93 91 free(pm);
94 92 pm = NULL;
95 93 return;
96 94 }
95 +
97 96 n = read(i, pm, statbuf.st_size);
98 97 close(i);
99 - if (n != statbuf.st_size)
100 - {
98 +
99 + if (n != statbuf.st_size) {
101 100 free(pm);
102 101 pm = NULL;
102 + } else {
103 + npm = (int)(n / sizeof (prmap_t));
103 104 }
104 - else
105 - npm = (int) (n / sizeof(prmap_t));
106 105 }
107 106
108 107 /* read ELF program headers and symbols; return -1 on error, 0 otherwise */
109 108 static int
110 109 __fex_read_syms(int fd)
111 110 {
112 - Elf_Ehdr h;
113 - Elf_Shdr *sh;
114 - int i, size;
111 + Elf_Ehdr h;
112 + Elf_Shdr *sh;
113 + int i, size;
115 114
116 115 /* read the ELF header */
117 - if (read(fd, &h, sizeof(h)) != sizeof(h))
118 - return -1;
119 - if (h.e_ident[EI_MAG0] != ELFMAG0 ||
120 - h.e_ident[EI_MAG1] != ELFMAG1 ||
121 - h.e_ident[EI_MAG2] != ELFMAG2 ||
122 - h.e_ident[EI_MAG3] != ELFMAG3 ||
123 - h.e_phentsize != sizeof(Elf_Phdr) ||
124 - h.e_shentsize != sizeof(Elf_Shdr))
125 - return -1;
116 + if (read(fd, &h, sizeof (h)) != sizeof (h))
117 + return (-1);
118 +
119 + if (h.e_ident[EI_MAG0] != ELFMAG0 || h.e_ident[EI_MAG1] != ELFMAG1 ||
120 + h.e_ident[EI_MAG2] != ELFMAG2 || h.e_ident[EI_MAG3] != ELFMAG3 ||
121 + h.e_phentsize != sizeof (Elf_Phdr) || h.e_shentsize !=
122 + sizeof (Elf_Shdr))
123 + return (-1);
126 124
127 125 /* get space for the program headers */
128 126 size = h.e_phnum * h.e_phentsize;
129 - if (size > phsize)
130 - {
127 +
128 + if (size > phsize) {
131 129 if (ph)
132 130 free(ph);
131 +
133 132 phsize = nph = 0;
134 - if ((ph = (Elf_Phdr*)malloc(size)) == NULL)
135 - return -1;
133 +
134 + if ((ph = (Elf_Phdr *)malloc(size)) == NULL)
135 + return (-1);
136 +
136 137 phsize = size;
137 138 }
138 139
139 140 /* read the program headers */
140 - if (lseek(fd, h.e_phoff, SEEK_SET) != h.e_phoff ||
141 - read(fd, ph, size) != (ssize_t)size)
142 - {
141 + if (lseek(fd, h.e_phoff, SEEK_SET) != h.e_phoff || read(fd, ph, size) !=
142 + (ssize_t)size) {
143 143 nph = 0;
144 - return -1;
144 + return (-1);
145 145 }
146 +
146 147 nph = h.e_phnum;
147 148
148 149 /* read the section headers */
149 150 size = h.e_shnum * h.e_shentsize;
150 - if ((sh = (Elf_Shdr*)malloc(size)) == NULL)
151 - return -1;
152 - if (lseek(fd, h.e_shoff, SEEK_SET) != h.e_shoff ||
153 - read(fd, sh, size) != (ssize_t)size)
154 - {
151 +
152 + if ((sh = (Elf_Shdr *)malloc(size)) == NULL)
153 + return (-1);
154 +
155 + if (lseek(fd, h.e_shoff, SEEK_SET) != h.e_shoff || read(fd, sh, size) !=
156 + (ssize_t)size) {
155 157 free(sh);
156 - return -1;
158 + return (-1);
157 159 }
158 160
159 161 /* find the symtab section header */
160 - for (i = 0; i < h.e_shnum; i++)
161 - {
162 + for (i = 0; i < h.e_shnum; i++) {
162 163 if (sh[i].sh_type == SHT_SYMTAB)
163 - break; /* assume there is only one */
164 + break; /* assume there is only one */
164 165 }
165 - if (i == h.e_shnum || sh[i].sh_size == 0 ||
166 - sh[i].sh_entsize != sizeof(Elf_Sym) ||
167 - sh[i].sh_link < 1 || sh[i].sh_link >= h.e_shnum ||
168 - sh[sh[i].sh_link].sh_type != SHT_STRTAB ||
169 - sh[sh[i].sh_link].sh_size == 0)
170 - {
166 +
167 + if (i == h.e_shnum || sh[i].sh_size == 0 || sh[i].sh_entsize !=
168 + sizeof (Elf_Sym) || sh[i].sh_link < 1 || sh[i].sh_link >=
169 + h.e_shnum || sh[sh[i].sh_link].sh_type != SHT_STRTAB ||
170 + sh[sh[i].sh_link].sh_size == 0) {
171 171 free(sh);
172 - return -1;
172 + return (-1);
173 173 }
174 174
175 175 /* get space for the symbol and string tables */
176 - size = (int) (sh[i].sh_size + sh[sh[i].sh_link].sh_size);
177 - if (size > stbufsize)
178 - {
176 + size = (int)(sh[i].sh_size + sh[sh[i].sh_link].sh_size);
177 +
178 + if (size > stbufsize) {
179 179 if (stbuf)
180 180 free(stbuf);
181 +
181 182 stbufsize = nsyms = 0;
182 - if ((stbuf = (char*)malloc(size)) == NULL)
183 - {
183 +
184 + if ((stbuf = (char *)malloc(size)) == NULL) {
184 185 free(sh);
185 - return -1;
186 + return (-1);
186 187 }
188 +
187 189 stbufsize = size;
188 190 }
189 191
190 192 /* read the symbol and string tables */
191 193 if (lseek(fd, sh[i].sh_offset, SEEK_SET) != sh[i].sh_offset ||
192 - read(fd, stbuf, sh[i].sh_size) != sh[i].sh_size ||
193 - lseek(fd, sh[sh[i].sh_link].sh_offset, SEEK_SET) !=
194 - sh[sh[i].sh_link].sh_offset ||
195 - read(fd, stbuf + sh[i].sh_size, sh[sh[i].sh_link].sh_size) !=
196 - sh[sh[i].sh_link].sh_size)
197 - {
194 + read(fd, stbuf, sh[i].sh_size) != sh[i].sh_size ||
195 + lseek(fd, sh[sh[i].sh_link].sh_offset, SEEK_SET) !=
196 + sh[sh[i].sh_link].sh_offset ||
197 + read(fd, stbuf + sh[i].sh_size, sh[sh[i].sh_link].sh_size) !=
198 + sh[sh[i].sh_link].sh_size) {
198 199 free(sh);
199 200 return (-1);
200 201 }
201 - nsyms = (int) (sh[i].sh_size / sh[i].sh_entsize);
202 - stoffset = (int) sh[i].sh_size;
202 +
203 + nsyms = (int)(sh[i].sh_size / sh[i].sh_entsize);
204 + stoffset = (int)sh[i].sh_size;
203 205
204 206 free(sh);
205 207 return (0);
206 208 }
207 209
208 -/* find the symbol corresponding to the given text address;
209 - return NULL on error, symbol address otherwise */
210 +/*
211 + * find the symbol corresponding to the given text address;
212 + * return NULL on error, symbol address otherwise
213 + */
210 214 char *
211 215 __fex_sym(char *a, char **name)
212 216 {
213 - Elf_Sym *s;
214 - unsigned long fo, va, value;
215 - int fd, i, j, nm;
216 - char fname[PRMAPSZ+20];
217 + Elf_Sym *s;
218 + unsigned long fo, va, value;
219 + int fd, i, j, nm;
220 + char fname[PRMAPSZ + 20];
217 221
218 222 /* see if the last prmap_t found contains the indicated address */
219 - if (lpm)
220 - {
221 - if (a >= (char*)lpm->pr_vaddr && a < (char*)lpm->pr_vaddr +
222 - lpm->pr_size)
223 + if (lpm) {
224 + if (a >= (char *)lpm->pr_vaddr && a < (char *)lpm->pr_vaddr +
225 + lpm->pr_size)
223 226 goto cont;
224 227 }
225 228
226 229 /* look for a prmap_t that contains the indicated address */
227 - for (i = 0; i < npm; i++)
228 - {
229 - if (a >= (char*)pm[i].pr_vaddr && a < (char*)pm[i].pr_vaddr +
230 - pm[i].pr_size)
230 + for (i = 0; i < npm; i++) {
231 + if (a >= (char *)pm[i].pr_vaddr && a < (char *)pm[i].pr_vaddr +
232 + pm[i].pr_size)
231 233 break;
232 234 }
235 +
233 236 if (i == npm)
234 - return NULL;
237 + return (NULL);
235 238
236 239 /* get an open file descriptor for the mapped object */
237 240 if (pm[i].pr_mapname[0] == '\0')
238 - return NULL;
241 + return (NULL);
242 +
239 243 strcpy(fname, "/proc/self/object/");
240 244 strncat(fname, pm[i].pr_mapname, PRMAPSZ);
241 245 fd = open(fname, O_RDONLY);
246 +
242 247 if (fd < 0)
243 - return NULL;
248 + return (NULL);
244 249
245 250 /* read the program headers and symbols */
246 251 lpm = NULL;
247 252 j = __fex_read_syms(fd);
248 253 close(fd);
254 +
249 255 if (j < 0)
250 - return NULL;
256 + return (NULL);
257 +
251 258 lpm = &pm[i];
252 259
253 260 cont:
254 261 /* compute the file offset corresponding to the mapped address */
255 - fo = (a - (char*)lpm->pr_vaddr) + lpm->pr_offset;
262 + fo = (a - (char *)lpm->pr_vaddr) + lpm->pr_offset;
256 263
257 264 /* find the program header containing the file offset */
258 - for (i = 0; i < nph; i++)
259 - {
260 - if (ph[i].p_type == PT_LOAD && fo >= ph[i].p_offset &&
261 - fo < ph[i].p_offset + ph[i].p_filesz)
265 + for (i = 0; i < nph; i++) {
266 + if (ph[i].p_type == PT_LOAD && fo >= ph[i].p_offset && fo <
267 + ph[i].p_offset + ph[i].p_filesz)
262 268 break;
263 269 }
270 +
264 271 if (i == nph)
265 - return NULL;
272 + return (NULL);
266 273
267 274 /* compute the virtual address corresponding to the file offset */
268 275 va = (fo - ph[i].p_offset) + ph[i].p_vaddr;
269 276
270 - /* find the symbol in this segment with the highest value
271 - less than or equal to the virtual address */
272 - s = (Elf_Sym*)stbuf;
277 + /*
278 + * find the symbol in this segment with the highest value
279 + * less than or equal to the virtual address
280 + */
281 + s = (Elf_Sym *)stbuf;
273 282 value = nm = 0;
274 - for (j = 0; j < nsyms; j++)
275 - {
283 +
284 + for (j = 0; j < nsyms; j++) {
276 285 if (s[j].st_name == 0 || s[j].st_shndx == SHN_UNDEF ||
277 - (ELF_ST_BIND(s[j].st_info) != STB_LOCAL &&
278 - ELF_ST_BIND(s[j].st_info) != STB_GLOBAL &&
279 - ELF_ST_BIND(s[j].st_info) != STB_WEAK) ||
280 - (ELF_ST_TYPE(s[j].st_info) != STT_NOTYPE &&
281 - ELF_ST_TYPE(s[j].st_info) != STT_OBJECT &&
282 - ELF_ST_TYPE(s[j].st_info) != STT_FUNC))
283 - {
286 + (ELF_ST_BIND(s[j].st_info) != STB_LOCAL && ELF_ST_BIND(
287 + s[j].st_info) != STB_GLOBAL && ELF_ST_BIND(s[j].st_info) !=
288 + STB_WEAK) || (ELF_ST_TYPE(s[j].st_info) != STT_NOTYPE &&
289 + ELF_ST_TYPE(s[j].st_info) != STT_OBJECT && ELF_ST_TYPE(
290 + s[j].st_info) != STT_FUNC))
284 291 continue;
285 - }
286 292
287 - if (s[j].st_value < ph[i].p_vaddr || s[j].st_value >= ph[i].p_vaddr
288 - + ph[i].p_memsz)
289 - {
293 + if (s[j].st_value < ph[i].p_vaddr || s[j].st_value >=
294 + ph[i].p_vaddr + ph[i].p_memsz)
290 295 continue;
291 - }
292 296
293 297 if (s[j].st_value < value || s[j].st_value > va)
294 298 continue;
295 299
296 300 value = s[j].st_value;
297 301 nm = s[j].st_name;
298 302 }
303 +
299 304 if (nm == 0)
300 - return NULL;
305 + return (NULL);
301 306
302 307 /* pass back the name and return the mapped address of the symbol */
303 308 *name = stbuf + stoffset + nm;
304 309 fo = (value - ph[i].p_vaddr) + ph[i].p_offset;
305 - return (char*)lpm->pr_vaddr + (fo - lpm->pr_offset);
310 + return ((char *)lpm->pr_vaddr + (fo - lpm->pr_offset));
306 311 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX