Print this page
10075 make usr/src/tools smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/ctf/cvt/stabs.c
+++ new/usr/src/tools/ctf/cvt/stabs.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.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 + * Copyright (c) 2018, Joyent, Inc.
28 + */
29 +
30 +/*
27 31 * Routines used to read stabs data from a file, and to build a tdata structure
28 32 * based on the interesting parts of that data.
29 33 */
30 34
31 35 #include <stdio.h>
32 36 #include <stdlib.h>
33 37 #include <fcntl.h>
34 38 #include <unistd.h>
35 39 #include <assert.h>
36 40 #include <string.h>
37 41 #include <libgen.h>
38 42 #include <errno.h>
39 43 #include <sys/types.h>
40 44 #include <sys/param.h>
41 45
42 46 #include "ctftools.h"
43 47 #include "list.h"
44 48 #include "stack.h"
45 49 #include "memory.h"
46 50 #include "traverse.h"
47 51
48 52 const char *curhdr;
49 53
50 54 /*
51 55 * The stabs generator will sometimes reference types before they've been
52 56 * defined. If this is the case, a TYPEDEF_UNRES tdesc will be generated.
53 57 * Note that this is different from a forward declaration, in which the
54 58 * stab is defined, but is defined as something that doesn't exist yet.
55 59 * When we have read all of the stabs from the file, we can go back and
56 60 * fix up all of the unresolved types. We should be able to fix all of them.
57 61 */
58 62 /*ARGSUSED2*/
59 63 static int
60 64 resolve_tou_node(tdesc_t *node, tdesc_t **nodep, void *private)
61 65 {
62 66 tdesc_t *new;
63 67
64 68 debug(3, "Trying to resolve %s (%d)\n", tdesc_name(node), node->t_id);
65 69 new = lookup(node->t_id);
66 70
67 71 if (new == NULL) {
68 72 terminate("Couldn't resolve type %d\n", node->t_id);
69 73 }
70 74
71 75 debug(3, " Resolving to %d\n", new->t_id);
72 76
73 77 *nodep = new;
74 78
75 79 return (1);
76 80 }
77 81
78 82 /*ARGSUSED*/
79 83 static int
80 84 resolve_fwd_node(tdesc_t *node, tdesc_t **nodep, void *private)
81 85 {
82 86 tdesc_t *new = lookupname(node->t_name);
83 87
84 88 debug(3, "Trying to unforward %s (%d)\n", tdesc_name(node), node->t_id);
85 89
86 90 if (!new || (new->t_type != STRUCT && new->t_type != UNION))
87 91 return (0);
88 92
89 93 debug(3, " Unforwarded to %d\n", new->t_id);
90 94
91 95 *nodep = new;
92 96
93 97 return (1);
94 98 }
95 99
96 100 static tdtrav_cb_f resolve_cbs[] = {
97 101 NULL,
98 102 NULL, /* intrinsic */
99 103 NULL, /* pointer */
100 104 NULL, /* array */
101 105 NULL, /* function */
102 106 NULL, /* struct */
103 107 NULL, /* union */
104 108 NULL, /* enum */
105 109 resolve_fwd_node, /* forward */
106 110 NULL, /* typedef */
107 111 resolve_tou_node, /* typedef unres */
108 112 NULL, /* volatile */
109 113 NULL, /* const */
110 114 NULL, /* restrict */
111 115 };
112 116
113 117 static void
114 118 resolve_nodes(tdata_t *td)
115 119 {
116 120 debug(2, "Resolving unresolved stabs\n");
117 121
118 122 (void) iitraverse_hash(td->td_iihash, &td->td_curvgen, resolve_cbs,
119 123 NULL, NULL, td);
120 124 }
121 125
122 126 static char *
123 127 concat(char *s1, char *s2, int s2strip)
124 128 {
125 129 int savelen = strlen(s2) - s2strip;
126 130 int newlen = (s1 ? strlen(s1) : 0) + savelen + 1;
127 131 char *out;
128 132
129 133 out = xrealloc(s1, newlen);
130 134 if (s1)
131 135 strncpy(out + strlen(out), s2, savelen);
132 136 else
133 137 strncpy(out, s2, savelen);
134 138
135 139 out[newlen - 1] = '\0';
136 140
137 141 return (out);
138 142 }
139 143
140 144 /*
141 145 * N_FUN stabs come with their arguments in promoted form. In order to get the
142 146 * actual arguments, we need to wait for the N_PSYM stabs that will come towards
143 147 * the end of the function. These routines free the arguments (fnarg_free) we
144 148 * got from the N_FUN stab and add (fnarg_add) the ones from the N_PSYM stabs.
145 149 */
146 150 static void
147 151 fnarg_add(iidesc_t *curfun, iidesc_t *arg)
148 152 {
149 153 curfun->ii_nargs++;
150 154
151 155 if (curfun->ii_nargs == 1)
152 156 curfun->ii_args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
153 157 else if (curfun->ii_nargs > FUNCARG_DEF) {
154 158 curfun->ii_args = xrealloc(curfun->ii_args,
155 159 sizeof (tdesc_t *) * curfun->ii_nargs);
156 160 }
157 161
158 162 curfun->ii_args[curfun->ii_nargs - 1] = arg->ii_dtype;
159 163 arg->ii_dtype = NULL;
160 164 }
161 165
162 166 static void
163 167 fnarg_free(iidesc_t *ii)
164 168 {
165 169 ii->ii_nargs = 0;
166 170 free(ii->ii_args);
167 171 ii->ii_args = NULL;
168 172 }
169 173
170 174 /*
171 175 * Read the stabs from the stab ELF section, and turn them into a tdesc tree,
172 176 * assembled under an iidesc list.
173 177 */
174 178 int
175 179 stabs_read(tdata_t *td, Elf *elf, const char *file)
176 180 {
177 181 Elf_Scn *scn;
178 182 Elf_Data *data;
179 183 stab_t *stab;
180 184 stk_t *file_stack;
181 185 iidesc_t *iidescp;
182 186 iidesc_t *curfun = NULL;
183 187 char curpath[MAXPATHLEN];
184 188 char *curfile = NULL;
185 189 char *str;
186 190 char *fstr = NULL, *ofstr = NULL;
187 191 int stabidx, stabstridx;
188 192 int nstabs, rc, i;
189 193 int scope = 0;
190 194
191 195 if (!((stabidx = findelfsecidx(elf, file, ".stab.excl")) >= 0 &&
192 196 (stabstridx = findelfsecidx(elf, file, ".stab.exclstr")) >= 0) &&
193 197 !((stabidx = findelfsecidx(elf, file, ".stab")) >= 0 &&
194 198 (stabstridx = findelfsecidx(elf, file, ".stabstr")) >= 0)) {
195 199 errno = ENOENT;
196 200 return (-1);
197 201 }
198 202
199 203 file_stack = stack_new(free);
200 204
201 205 stack_push(file_stack, (void *)file);
202 206 curhdr = file;
203 207
204 208 debug(3, "Found stabs in %d, strings in %d\n", stabidx, stabstridx);
205 209
206 210 scn = elf_getscn(elf, stabidx);
207 211 data = elf_rawdata(scn, NULL);
208 212 nstabs = data->d_size / sizeof (stab_t);
209 213
210 214 parse_init(td);
211 215 for (i = 0; i < nstabs; i++) {
212 216 stab = &((stab_t *)data->d_buf)[i];
213 217
214 218 /* We don't want any local definitions */
215 219 if (stab->n_type == N_LBRAC) {
216 220 scope++;
217 221 debug(3, "stab %d: opening scope (%d)\n", i + 1, scope);
218 222 continue;
219 223 } else if (stab->n_type == N_RBRAC) {
220 224 scope--;
221 225 debug(3, "stab %d: closing scope (%d)\n", i + 1, scope);
222 226 continue;
223 227 } else if (stab->n_type == N_EINCL) {
224 228 /*
225 229 * There's a bug in the 5.2 (Taz) compilers that causes
226 230 * them to emit an extra N_EINCL if there's no actual
227 231 * text in the file being compiled. To work around this
228 232 * bug, we explicitly check to make sure we're not
229 233 * trying to pop a stack that only has the outer scope
230 234 * on it.
231 235 */
232 236 if (stack_level(file_stack) != 1) {
233 237 str = (char *)stack_pop(file_stack);
234 238 free(str);
235 239 curhdr = (char *)stack_peek(file_stack);
236 240 }
237 241 }
238 242
239 243 /* We only care about a subset of the stabs */
240 244 if (!(stab->n_type == N_FUN || stab->n_type == N_GSYM ||
241 245 stab->n_type == N_LCSYM || stab->n_type == N_LSYM ||
242 246 stab->n_type == N_PSYM || stab->n_type == N_ROSYM ||
243 247 stab->n_type == N_RSYM ||
244 248 stab->n_type == N_STSYM || stab->n_type == N_BINCL ||
245 249 stab->n_type == N_SO || stab->n_type == N_OPT))
246 250 continue;
247 251
248 252 if ((str = elf_strptr(elf, stabstridx,
249 253 (size_t)stab->n_strx)) == NULL) {
250 254 terminate("%s: Can't find string at %u for stab %d\n",
251 255 file, stab->n_strx, i);
252 256 }
253 257
254 258 if (stab->n_type == N_BINCL) {
255 259 curhdr = xstrdup(str);
256 260 stack_push(file_stack, (void *)curhdr);
257 261 continue;
258 262 } else if (stab->n_type == N_SO) {
259 263 if (str[strlen(str) - 1] != '/') {
260 264 strcpy(curpath, str);
261 265 curfile = basename(curpath);
262 266 }
263 267 continue;
264 268 } else if (stab->n_type == N_OPT) {
265 269 if (strcmp(str, "gcc2_compiled.") == 0) {
266 270 terminate("%s: GCC-generated stabs are "
267 271 "unsupported. Use DWARF instead.\n", file);
268 272 }
269 273 continue;
270 274 }
271 275
272 276 if (str[strlen(str) - 1] == '\\') {
273 277 int offset = 1;
274 278 /*
275 279 * There's a bug in the compilers that causes them to
276 280 * generate \ for continuations with just -g (this is
277 281 * ok), and \\ for continuations with -g -O (this is
278 282 * broken). This bug is "fixed" in the 6.2 compilers
279 283 * via the elimination of continuation stabs.
280 284 */
281 285 if (str[strlen(str) - 2] == '\\')
282 286 offset = 2;
283 287 fstr = concat(fstr, str, offset);
284 288 continue;
285 289 } else
286 290 fstr = concat(fstr, str, 0);
287 291
288 292 debug(3, "%4d: .stabs \"%s\", %#x, %d, %hd, %d (from %s)\n", i,
289 293 fstr, stab->n_type, 0, stab->n_desc,
290 294 stab->n_value, curhdr);
291 295
292 296 if (debug_level >= 3)
293 297 check_hash();
294 298
295 299 /*
296 300 * Sometimes the compiler stutters, and emits the same stab
297 301 * twice. This is bad for the parser, which will attempt to
298 302 * redefine the type IDs indicated in the stabs. This is
299 303 * compiler bug 4433511.
300 304 */
301 305 if (ofstr && strcmp(fstr, ofstr) == 0) {
302 306 debug(3, "Stutter stab\n");
303 307 free(fstr);
304 308 fstr = NULL;
305 309 continue;
306 310 }
307 311
308 312 if (ofstr)
309 313 free(ofstr);
310 314 ofstr = fstr;
311 315
312 316 iidescp = NULL;
313 317
314 318 if ((rc = parse_stab(stab, fstr, &iidescp)) < 0) {
315 319 terminate("%s: Couldn't parse stab \"%s\" "
316 320 "(source file %s)\n", file, str, curhdr);
317 321 }
318 322
319 323 if (rc == 0)
320 324 goto parse_loop_end;
↓ open down ↓ |
284 lines elided |
↑ open up ↑ |
321 325
322 326 /* Make sure the scope tracking is working correctly */
323 327 assert(stab->n_type != N_FUN || (iidescp->ii_type != II_GFUN &&
324 328 iidescp->ii_type != II_SFUN) || scope == 0);
325 329
326 330 /*
327 331 * The only things we care about that are in local scope are
328 332 * the N_PSYM stabs.
329 333 */
330 334 if (scope && stab->n_type != N_PSYM) {
331 - if (iidescp)
332 - iidesc_free(iidescp);
335 + iidesc_free(iidescp);
333 336 goto parse_loop_end;
334 337 }
335 338
336 339 switch (iidescp->ii_type) {
337 340 case II_SFUN:
338 341 iidescp->ii_owner = xstrdup(curfile);
339 342 /*FALLTHROUGH*/
340 343 case II_GFUN:
341 344 curfun = iidescp;
342 345 fnarg_free(iidescp);
343 346 iidesc_add(td->td_iihash, iidescp);
344 347 break;
345 348
346 349 case II_SVAR:
347 350 iidescp->ii_owner = xstrdup(curfile);
348 351 /*FALLTHROUGH*/
349 352 case II_GVAR:
350 353 case II_TYPE:
351 354 case II_SOU:
352 355 iidesc_add(td->td_iihash, iidescp);
353 356 break;
354 357
355 358 case II_PSYM:
356 359 fnarg_add(curfun, iidescp);
357 360 iidesc_free(iidescp);
358 361 break;
359 362 default:
360 363 aborterr("invalid ii_type %d for stab type %d",
361 364 iidescp->ii_type, stab->n_type);
362 365 }
363 366
364 367 parse_loop_end:
365 368 fstr = NULL;
366 369 }
367 370
368 371 if (ofstr)
369 372 free(ofstr);
370 373
371 374 resolve_nodes(td);
372 375 resolve_typed_bitfields();
373 376 parse_finish(td);
374 377
375 378 cvt_fixstabs(td);
376 379 cvt_fixups(td, elf_ptrsz(elf));
377 380
378 381 return (0);
379 382 }
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX