Print this page
10812 ctf tools shouldn't add blank labels
10813 ctf symbol mapping needs work
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libctf/common/ctf_dwarf.c
+++ new/usr/src/lib/libctf/common/ctf_dwarf.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
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
21 21 /*
22 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25 /*
26 26 * Copyright 2012 Jason King. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 /*
31 - * Copyright 2018 Joyent, Inc.
31 + * Copyright 2019 Joyent, Inc.
32 32 */
33 33
34 34 /*
35 35 * CTF DWARF conversion theory.
36 36 *
37 37 * DWARF data contains a series of compilation units. Each compilation unit
38 38 * generally refers to an object file or what once was, in the case of linked
39 39 * binaries and shared objects. Each compilation unit has a series of what DWARF
40 40 * calls a DIE (Debugging Information Entry). The set of entries that we care
41 41 * about have type information stored in a series of attributes. Each DIE also
42 42 * has a tag that identifies the kind of attributes that it has.
43 43 *
44 44 * A given DIE may itself have children. For example, a DIE that represents a
45 45 * structure has children which represent members. Whenever we encounter a DIE
46 46 * that has children or other values or types associated with it, we recursively
47 47 * process those children first so that way we can then refer to the generated
48 48 * CTF type id while processing its parent. This reduces the amount of unknowns
49 49 * and fixups that we need. It also ensures that we don't accidentally add types
50 50 * that an overzealous compiler might add to the DWARF data but aren't used by
51 51 * anything in the system.
52 52 *
53 53 * Once we do a conversion, we store a mapping in an AVL tree that goes from the
54 54 * DWARF's die offset, which is relative to the given compilation unit, to a
55 55 * ctf_id_t.
56 56 *
57 57 * Unfortunately, some compilers actually will emit duplicate entries for a
58 58 * given type that look similar, but aren't quite. To that end, we go through
59 59 * and do a variant on a merge once we're done processing a single compilation
60 60 * unit which deduplicates all of the types that are in the unit.
61 61 *
62 62 * Finally, if we encounter an object that has multiple compilation units, then
63 63 * we'll convert all of the compilation units separately and then do a merge, so
64 64 * that way we can result in one single ctf_file_t that represents everything
65 65 * for the object.
66 66 *
67 67 * Conversion Steps
68 68 * ----------------
69 69 *
70 70 * Because a given object we've been given to convert may have multiple
71 71 * compilation units, we break the work into two halves. The first half
72 72 * processes each compilation unit (potentially in parallel) and then the second
73 73 * half optionally merges all of the dies in the first half. First, we'll cover
74 74 * what's involved in converting a single ctf_cu_t's dwarf to CTF. This covers
75 75 * the work done in ctf_dwarf_convert_one().
76 76 *
77 77 * An individual ctf_cu_t, which represents a compilation unit, is converted to
78 78 * CTF in a series of multiple passes.
79 79 *
80 80 * Pass 1: During the first pass we walk all of the top-level dies and if we
81 81 * find a function, variable, struct, union, enum or typedef, we recursively
82 82 * transform all of its types. We don't recurse or process everything, because
83 83 * we don't want to add some of the types that compilers may add which are
84 84 * effectively unused.
85 85 *
86 86 * During pass 1, if we encounter any structures or unions we mark them for
87 87 * fixing up later. This is necessary because we may not be able to determine
88 88 * the full size of a structure at the beginning of time. This will happen if
89 89 * the DWARF attribute DW_AT_byte_size is not present for a member. Because of
90 90 * this possibility we defer adding members to structures or even converting
91 91 * them during pass 1 and save that for pass 2. Adding all of the base
92 92 * structures without any of their members helps deal with any circular
93 93 * dependencies that we might encounter.
94 94 *
95 95 * Pass 2: This pass is used to do the first half of fixing up structures and
96 96 * unions. Rather than walk the entire type space again, we actually walk the
97 97 * list of structures and unions that we marked for later fixing up. Here, we
98 98 * iterate over every structure and add members to the underlying ctf_file_t,
99 99 * but not to the structs themselves. One might wonder why we don't, and the
100 100 * main reason is that libctf requires a ctf_update() be done before adding the
101 101 * members to structures or unions.
102 102 *
103 103 * Pass 3: This pass is used to do the second half of fixing up structures and
104 104 * unions. During this part we always go through and add members to structures
105 105 * and unions that we added to the container in the previous pass. In addition,
106 106 * we set the structure and union's actual size, which may have additional
107 107 * padding added by the compiler, it isn't simply the last offset. DWARF always
108 108 * guarantees an attribute exists for this. Importantly no ctf_id_t's change
109 109 * during pass 2.
110 110 *
111 111 * Pass 4: The next phase is to add CTF entries for all of the symbols and
112 112 * variables that are present in this die. During pass 1 we added entries to a
113 113 * map for each variable and function. During this pass, we iterate over the
114 114 * symbol table and when we encounter a symbol that we have in our lists of
115 115 * translated information which matches, we then add it to the ctf_file_t.
116 116 *
117 117 * Pass 5: Here we go and look for any weak symbols and functions and see if
118 118 * they match anything that we recognize. If so, then we add type information
119 119 * for them at this point based on the matching type.
120 120 *
121 121 * Pass 6: This pass is actually a variant on a merge. The traditional merge
122 122 * process expects there to be no duplicate types. As such, at the end of
123 123 * conversion, we do a dedup on all of the types in the system. The
124 124 * deduplication process is described in lib/libctf/common/ctf_merge.c.
125 125 *
126 126 * Once pass 6 is done, we've finished processing the individual compilation
127 127 * unit.
128 128 *
129 129 * The following steps reflect the general process of doing a conversion.
130 130 *
131 131 * 1) Walk the dwarf section and determine the number of compilation units
132 132 * 2) Create a ctf_cu_t for each compilation unit
133 133 * 3) Add all ctf_cu_t's to a workq
134 134 * 4) Have the workq process each die with ctf_dwarf_convert_one. This itself
135 135 * is comprised of several steps, which were already enumerated.
136 136 * 5) If we have multiple cu's, we do a ctf merge of all the dies. The mechanics
137 137 * of the merge are discussed in lib/libctf/common/ctf_merge.c.
138 138 * 6) Free everything up and return a ctf_file_t to the user. If we only had a
139 139 * single compilation unit, then we give that to the user. Otherwise, we
140 140 * return the merged ctf_file_t.
141 141 *
142 142 * Threading
143 143 * ---------
144 144 *
145 145 * The process has been designed to be amenable to threading. Each compilation
146 146 * unit has its own type stream, therefore the logical place to divide and
147 147 * conquer is at the compilation unit. Each ctf_cu_t has been built to be able
148 148 * to be processed independently of the others. It has its own libdwarf handle,
149 149 * as a given libdwarf handle may only be used by a single thread at a time.
150 150 * This allows the various ctf_cu_t's to be processed in parallel by different
151 151 * threads.
152 152 *
153 153 * All of the ctf_cu_t's are loaded into a workq which allows for a number of
154 154 * threads to be specified and used as a thread pool to process all of the
155 155 * queued work. We set the number of threads to use in the workq equal to the
156 156 * number of threads that the user has specified.
157 157 *
158 158 * After all of the compilation units have been drained, we use the same number
159 159 * of threads when performing a merge of multiple compilation units, if they
160 160 * exist.
161 161 *
162 162 * While all of these different parts do support and allow for multiple threads,
163 163 * it's important that when only a single thread is specified, that it be the
164 164 * calling thread. This allows the conversion routines to be used in a context
165 165 * that doesn't allow additional threads, such as rtld.
166 166 *
167 167 * Common DWARF Mechanics and Notes
168 168 * --------------------------------
169 169 *
170 170 * At this time, we really only support DWARFv2, though support for DWARFv4 is
171 171 * mostly there. There is no intent to support DWARFv3.
172 172 *
173 173 * Generally types for something are stored in the DW_AT_type attribute. For
174 174 * example, a function's return type will be stored in the local DW_AT_type
175 175 * attribute while the arguments will be in child DIEs. There are also various
176 176 * times when we don't have any DW_AT_type. In that case, the lack of a type
177 177 * implies, at least for C, that its C type is void. Because DWARF doesn't emit
178 178 * one, we have a synthetic void type that we create and manipulate instead and
179 179 * pass it off to consumers on an as-needed basis. If nothing has a void type,
180 180 * it will not be emitted.
181 181 *
182 182 * Architecture Specific Parts
183 183 * ---------------------------
184 184 *
185 185 * The CTF tooling encodes various information about the various architectures
186 186 * in the system. Importantly, the tool assumes that every architecture has a
187 187 * data model where long and pointer are the same size. This is currently the
188 188 * case, as the two data models illumos supports are ILP32 and LP64.
189 189 *
190 190 * In addition, we encode the mapping of various floating point sizes to various
191 191 * types for each architecture. If a new architecture is being added, it should
192 192 * be added to the list. The general design of the ctf conversion tools is to be
193 193 * architecture independent. eg. any of the tools here should be able to convert
194 194 * any architecture's DWARF into ctf; however, this has not been rigorously
195 195 * tested and more importantly, the ctf routines don't currently write out the
196 196 * data in an endian-aware form, they only use that of the currently running
197 197 * library.
198 198 */
199 199
200 200 #include <libctf_impl.h>
201 201 #include <sys/avl.h>
202 202 #include <sys/debug.h>
203 203 #include <gelf.h>
204 204 #include <libdwarf.h>
205 205 #include <dwarf.h>
206 206 #include <libgen.h>
207 207 #include <workq.h>
208 208 #include <errno.h>
209 209
210 210 #define DWARF_VERSION_TWO 2
211 211 #define DWARF_VARARGS_NAME "..."
212 212
213 213 /*
214 214 * Dwarf may refer recursively to other types that we've already processed. To
215 215 * see if we've already converted them, we look them up in an AVL tree that's
216 216 * sorted by the DWARF id.
217 217 */
218 218 typedef struct ctf_dwmap {
219 219 avl_node_t cdm_avl;
220 220 Dwarf_Off cdm_off;
221 221 Dwarf_Die cdm_die;
222 222 ctf_id_t cdm_id;
223 223 boolean_t cdm_fix;
224 224 } ctf_dwmap_t;
225 225
226 226 typedef struct ctf_dwvar {
227 227 ctf_list_t cdv_list;
228 228 char *cdv_name;
229 229 ctf_id_t cdv_type;
230 230 boolean_t cdv_global;
231 231 } ctf_dwvar_t;
232 232
233 233 typedef struct ctf_dwfunc {
234 234 ctf_list_t cdf_list;
235 235 char *cdf_name;
236 236 ctf_funcinfo_t cdf_fip;
237 237 ctf_id_t *cdf_argv;
238 238 boolean_t cdf_global;
239 239 } ctf_dwfunc_t;
240 240
241 241 typedef struct ctf_dwbitf {
242 242 ctf_list_t cdb_list;
243 243 ctf_id_t cdb_base;
244 244 uint_t cdb_nbits;
245 245 ctf_id_t cdb_id;
246 246 } ctf_dwbitf_t;
247 247
248 248 /*
249 249 * The ctf_cu_t represents a single top-level DWARF die unit. While generally,
250 250 * the typical object file has only a single die, if we're asked to convert
251 251 * something that's been linked from multiple sources, multiple dies will exist.
252 252 */
253 253 typedef struct ctf_die {
254 254 Elf *cu_elf; /* shared libelf handle */
255 255 char *cu_name; /* basename of the DIE */
256 256 ctf_merge_t *cu_cmh; /* merge handle */
257 257 ctf_list_t cu_vars; /* List of variables */
258 258 ctf_list_t cu_funcs; /* List of functions */
259 259 ctf_list_t cu_bitfields; /* Bit field members */
260 260 Dwarf_Debug cu_dwarf; /* libdwarf handle */
261 261 Dwarf_Die cu_cu; /* libdwarf compilation unit */
262 262 Dwarf_Off cu_cuoff; /* cu's offset */
263 263 Dwarf_Off cu_maxoff; /* maximum offset */
264 264 ctf_file_t *cu_ctfp; /* output CTF file */
265 265 avl_tree_t cu_map; /* map die offsets to CTF types */
266 266 char *cu_errbuf; /* error message buffer */
267 267 size_t cu_errlen; /* error message buffer length */
268 268 size_t cu_ptrsz; /* object's pointer size */
269 269 boolean_t cu_bigend; /* is it big endian */
270 270 boolean_t cu_doweaks; /* should we convert weak symbols? */
271 271 uint_t cu_mach; /* machine type */
272 272 ctf_id_t cu_voidtid; /* void pointer */
273 273 ctf_id_t cu_longtid; /* id for a 'long' */
274 274 } ctf_cu_t;
↓ open down ↓ |
233 lines elided |
↑ open up ↑ |
275 275
276 276 static int ctf_dwarf_offset(ctf_cu_t *, Dwarf_Die, Dwarf_Off *);
277 277 static int ctf_dwarf_convert_die(ctf_cu_t *, Dwarf_Die);
278 278 static int ctf_dwarf_convert_type(ctf_cu_t *, Dwarf_Die, ctf_id_t *, int);
279 279
280 280 static int ctf_dwarf_function_count(ctf_cu_t *, Dwarf_Die, ctf_funcinfo_t *,
281 281 boolean_t);
282 282 static int ctf_dwarf_convert_fargs(ctf_cu_t *, Dwarf_Die, ctf_funcinfo_t *,
283 283 ctf_id_t *);
284 284
285 -typedef int (ctf_dwarf_symtab_f)(ctf_cu_t *, const GElf_Sym *, ulong_t,
286 - const char *, const char *, void *);
287 -
288 285 /*
289 286 * This is a generic way to set a CTF Conversion backend error depending on what
290 287 * we were doing. Unless it was one of a specific set of errors that don't
291 288 * indicate a programming / translation bug, eg. ENOMEM, then we transform it
292 289 * into a CTF backend error and fill in the error buffer.
293 290 */
294 291 static int
295 292 ctf_dwarf_error(ctf_cu_t *cup, ctf_file_t *cfp, int err, const char *fmt, ...)
296 293 {
297 294 va_list ap;
298 295 int ret;
299 296 size_t off = 0;
300 297 ssize_t rem = cup->cu_errlen;
301 298 if (cfp != NULL)
302 299 err = ctf_errno(cfp);
303 300
304 301 if (err == ENOMEM)
305 302 return (err);
306 303
307 304 ret = snprintf(cup->cu_errbuf, rem, "die %s: ", cup->cu_name);
308 305 if (ret < 0)
309 306 goto err;
310 307 off += ret;
311 308 rem = MAX(rem - ret, 0);
312 309
313 310 va_start(ap, fmt);
314 311 ret = vsnprintf(cup->cu_errbuf + off, rem, fmt, ap);
315 312 va_end(ap);
316 313 if (ret < 0)
317 314 goto err;
318 315
319 316 off += ret;
320 317 rem = MAX(rem - ret, 0);
321 318 if (fmt[strlen(fmt) - 1] != '\n') {
322 319 (void) snprintf(cup->cu_errbuf + off, rem,
323 320 ": %s\n", ctf_errmsg(err));
324 321 }
325 322 va_end(ap);
326 323 return (ECTF_CONVBKERR);
327 324
328 325 err:
329 326 cup->cu_errbuf[0] = '\0';
330 327 return (ECTF_CONVBKERR);
331 328 }
332 329
333 330 /*
334 331 * DWARF often opts to put no explicit type to describe a void type. eg. if we
335 332 * have a reference type whose DW_AT_type member doesn't exist, then we should
336 333 * instead assume it points to void. Because this isn't represented, we
337 334 * instead cause it to come into existence.
338 335 */
339 336 static ctf_id_t
340 337 ctf_dwarf_void(ctf_cu_t *cup)
341 338 {
342 339 if (cup->cu_voidtid == CTF_ERR) {
343 340 ctf_encoding_t enc = { CTF_INT_SIGNED, 0, 0 };
344 341 cup->cu_voidtid = ctf_add_integer(cup->cu_ctfp, CTF_ADD_ROOT,
345 342 "void", &enc);
346 343 if (cup->cu_voidtid == CTF_ERR) {
347 344 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
348 345 "failed to create void type: %s\n",
349 346 ctf_errmsg(ctf_errno(cup->cu_ctfp)));
350 347 }
351 348 }
352 349
353 350 return (cup->cu_voidtid);
354 351 }
355 352
356 353 /*
357 354 * There are many different forms that an array index may take. However, we just
358 355 * always force it to be of a type long no matter what. Therefore we use this to
359 356 * have a single instance of long across everything.
360 357 */
361 358 static ctf_id_t
362 359 ctf_dwarf_long(ctf_cu_t *cup)
363 360 {
364 361 if (cup->cu_longtid == CTF_ERR) {
365 362 ctf_encoding_t enc;
366 363
367 364 enc.cte_format = CTF_INT_SIGNED;
368 365 enc.cte_offset = 0;
369 366 /* All illumos systems are LP */
370 367 enc.cte_bits = cup->cu_ptrsz * 8;
371 368 cup->cu_longtid = ctf_add_integer(cup->cu_ctfp, CTF_ADD_NONROOT,
372 369 "long", &enc);
373 370 if (cup->cu_longtid == CTF_ERR) {
374 371 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
375 372 "failed to create long type: %s\n",
376 373 ctf_errmsg(ctf_errno(cup->cu_ctfp)));
377 374 }
378 375
379 376 }
380 377
381 378 return (cup->cu_longtid);
382 379 }
383 380
384 381 static int
385 382 ctf_dwmap_comp(const void *a, const void *b)
386 383 {
387 384 const ctf_dwmap_t *ca = a;
388 385 const ctf_dwmap_t *cb = b;
389 386
390 387 if (ca->cdm_off > cb->cdm_off)
391 388 return (1);
392 389 if (ca->cdm_off < cb->cdm_off)
393 390 return (-1);
394 391 return (0);
395 392 }
396 393
397 394 static int
398 395 ctf_dwmap_add(ctf_cu_t *cup, ctf_id_t id, Dwarf_Die die, boolean_t fix)
399 396 {
400 397 int ret;
401 398 avl_index_t index;
402 399 ctf_dwmap_t *dwmap;
403 400 Dwarf_Off off;
404 401
405 402 VERIFY(id > 0 && id < CTF_MAX_TYPE);
406 403
407 404 if ((ret = ctf_dwarf_offset(cup, die, &off)) != 0)
408 405 return (ret);
409 406
410 407 if ((dwmap = ctf_alloc(sizeof (ctf_dwmap_t))) == NULL)
411 408 return (ENOMEM);
412 409
413 410 dwmap->cdm_die = die;
414 411 dwmap->cdm_off = off;
415 412 dwmap->cdm_id = id;
416 413 dwmap->cdm_fix = fix;
417 414
418 415 ctf_dprintf("dwmap: %p %" DW_PR_DUx "->%d\n", dwmap, off, id);
419 416 VERIFY(avl_find(&cup->cu_map, dwmap, &index) == NULL);
420 417 avl_insert(&cup->cu_map, dwmap, index);
421 418 return (0);
422 419 }
423 420
424 421 static int
425 422 ctf_dwarf_attribute(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
426 423 Dwarf_Attribute *attrp)
427 424 {
428 425 int ret;
429 426 Dwarf_Error derr;
430 427
431 428 if ((ret = dwarf_attr(die, name, attrp, &derr)) == DW_DLV_OK)
432 429 return (0);
433 430 if (ret == DW_DLV_NO_ENTRY) {
434 431 *attrp = NULL;
435 432 return (ENOENT);
436 433 }
437 434 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
438 435 "failed to get attribute for type: %s\n",
439 436 dwarf_errmsg(derr));
440 437 return (ECTF_CONVBKERR);
441 438 }
442 439
443 440 static int
444 441 ctf_dwarf_ref(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name, Dwarf_Off *refp)
445 442 {
446 443 int ret;
447 444 Dwarf_Attribute attr;
448 445 Dwarf_Error derr;
449 446
450 447 if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
451 448 return (ret);
452 449
453 450 if (dwarf_formref(attr, refp, &derr) == DW_DLV_OK) {
454 451 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
455 452 return (0);
456 453 }
457 454
458 455 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
459 456 "failed to get unsigned attribute for type: %s\n",
460 457 dwarf_errmsg(derr));
461 458 return (ECTF_CONVBKERR);
462 459 }
463 460
464 461 static int
465 462 ctf_dwarf_refdie(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
466 463 Dwarf_Die *diep)
467 464 {
468 465 int ret;
469 466 Dwarf_Off off;
470 467 Dwarf_Error derr;
471 468
472 469 if ((ret = ctf_dwarf_ref(cup, die, name, &off)) != 0)
473 470 return (ret);
474 471
475 472 off += cup->cu_cuoff;
476 473 if ((ret = dwarf_offdie(cup->cu_dwarf, off, diep, &derr)) !=
477 474 DW_DLV_OK) {
478 475 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
479 476 "failed to get die from offset %" DW_PR_DUu ": %s\n",
480 477 off, dwarf_errmsg(derr));
481 478 return (ECTF_CONVBKERR);
482 479 }
483 480
484 481 return (0);
485 482 }
486 483
487 484 static int
488 485 ctf_dwarf_signed(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
489 486 Dwarf_Signed *valp)
490 487 {
491 488 int ret;
492 489 Dwarf_Attribute attr;
493 490 Dwarf_Error derr;
494 491
495 492 if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
496 493 return (ret);
497 494
498 495 if (dwarf_formsdata(attr, valp, &derr) == DW_DLV_OK) {
499 496 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
500 497 return (0);
501 498 }
502 499
503 500 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
504 501 "failed to get unsigned attribute for type: %s\n",
505 502 dwarf_errmsg(derr));
506 503 return (ECTF_CONVBKERR);
507 504 }
508 505
509 506 static int
510 507 ctf_dwarf_unsigned(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
511 508 Dwarf_Unsigned *valp)
512 509 {
513 510 int ret;
514 511 Dwarf_Attribute attr;
515 512 Dwarf_Error derr;
516 513
517 514 if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
518 515 return (ret);
519 516
520 517 if (dwarf_formudata(attr, valp, &derr) == DW_DLV_OK) {
521 518 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
522 519 return (0);
523 520 }
524 521
525 522 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
526 523 "failed to get unsigned attribute for type: %s\n",
527 524 dwarf_errmsg(derr));
528 525 return (ECTF_CONVBKERR);
529 526 }
530 527
531 528 static int
532 529 ctf_dwarf_boolean(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name,
533 530 Dwarf_Bool *val)
534 531 {
535 532 int ret;
536 533 Dwarf_Attribute attr;
537 534 Dwarf_Error derr;
538 535
539 536 if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
540 537 return (ret);
541 538
542 539 if (dwarf_formflag(attr, val, &derr) == DW_DLV_OK) {
543 540 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
544 541 return (0);
545 542 }
546 543
547 544 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
548 545 "failed to get boolean attribute for type: %s\n",
549 546 dwarf_errmsg(derr));
550 547
551 548 return (ECTF_CONVBKERR);
552 549 }
553 550
554 551 static int
555 552 ctf_dwarf_string(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half name, char **strp)
556 553 {
557 554 int ret;
558 555 char *s;
559 556 Dwarf_Attribute attr;
560 557 Dwarf_Error derr;
561 558
562 559 *strp = NULL;
563 560 if ((ret = ctf_dwarf_attribute(cup, die, name, &attr)) != 0)
564 561 return (ret);
565 562
566 563 if (dwarf_formstring(attr, &s, &derr) == DW_DLV_OK) {
567 564 if ((*strp = ctf_strdup(s)) == NULL)
568 565 ret = ENOMEM;
569 566 else
570 567 ret = 0;
571 568 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
572 569 return (ret);
573 570 }
574 571
575 572 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
576 573 "failed to get string attribute for type: %s\n",
577 574 dwarf_errmsg(derr));
578 575 return (ECTF_CONVBKERR);
579 576 }
580 577
581 578 static int
582 579 ctf_dwarf_member_location(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Unsigned *valp)
583 580 {
584 581 int ret;
585 582 Dwarf_Error derr;
586 583 Dwarf_Attribute attr;
587 584 Dwarf_Locdesc *loc;
588 585 Dwarf_Signed locnum;
589 586
590 587 if ((ret = ctf_dwarf_attribute(cup, die, DW_AT_data_member_location,
591 588 &attr)) != 0)
592 589 return (ret);
593 590
594 591 if (dwarf_loclist(attr, &loc, &locnum, &derr) != DW_DLV_OK) {
595 592 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
596 593 "failed to obtain location list for member offset: %s",
597 594 dwarf_errmsg(derr));
598 595 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
599 596 return (ECTF_CONVBKERR);
600 597 }
601 598 dwarf_dealloc(cup->cu_dwarf, attr, DW_DLA_ATTR);
602 599
603 600 if (locnum != 1 || loc->ld_s->lr_atom != DW_OP_plus_uconst) {
604 601 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
605 602 "failed to parse location structure for member");
606 603 dwarf_dealloc(cup->cu_dwarf, loc->ld_s, DW_DLA_LOC_BLOCK);
607 604 dwarf_dealloc(cup->cu_dwarf, loc, DW_DLA_LOCDESC);
608 605 return (ECTF_CONVBKERR);
609 606 }
610 607
611 608 *valp = loc->ld_s->lr_number;
612 609
613 610 dwarf_dealloc(cup->cu_dwarf, loc->ld_s, DW_DLA_LOC_BLOCK);
614 611 dwarf_dealloc(cup->cu_dwarf, loc, DW_DLA_LOCDESC);
615 612 return (0);
616 613 }
617 614
618 615
619 616 static int
620 617 ctf_dwarf_offset(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Off *offsetp)
621 618 {
622 619 Dwarf_Error derr;
623 620
624 621 if (dwarf_dieoffset(die, offsetp, &derr) == DW_DLV_OK)
625 622 return (0);
626 623
627 624 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
628 625 "failed to get die offset: %s\n",
629 626 dwarf_errmsg(derr));
630 627 return (ECTF_CONVBKERR);
631 628 }
632 629
633 630 /* simpler variant for debugging output */
634 631 static Dwarf_Off
635 632 ctf_die_offset(Dwarf_Die die)
636 633 {
637 634 Dwarf_Off off = -1;
638 635 Dwarf_Error derr;
639 636
640 637 (void) dwarf_dieoffset(die, &off, &derr);
641 638 return (off);
642 639 }
643 640
644 641 static int
645 642 ctf_dwarf_tag(ctf_cu_t *cup, Dwarf_Die die, Dwarf_Half *tagp)
646 643 {
647 644 Dwarf_Error derr;
648 645
649 646 if (dwarf_tag(die, tagp, &derr) == DW_DLV_OK)
650 647 return (0);
651 648
652 649 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
653 650 "failed to get tag type: %s\n",
654 651 dwarf_errmsg(derr));
655 652 return (ECTF_CONVBKERR);
656 653 }
657 654
658 655 static int
659 656 ctf_dwarf_sib(ctf_cu_t *cup, Dwarf_Die base, Dwarf_Die *sibp)
660 657 {
661 658 Dwarf_Error derr;
662 659 int ret;
663 660
664 661 *sibp = NULL;
665 662 ret = dwarf_siblingof(cup->cu_dwarf, base, sibp, &derr);
666 663 if (ret == DW_DLV_OK || ret == DW_DLV_NO_ENTRY)
667 664 return (0);
668 665
669 666 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
670 667 "failed to sibling from die: %s\n",
671 668 dwarf_errmsg(derr));
672 669 return (ECTF_CONVBKERR);
673 670 }
674 671
675 672 static int
676 673 ctf_dwarf_child(ctf_cu_t *cup, Dwarf_Die base, Dwarf_Die *childp)
677 674 {
678 675 Dwarf_Error derr;
679 676 int ret;
680 677
681 678 *childp = NULL;
682 679 ret = dwarf_child(base, childp, &derr);
683 680 if (ret == DW_DLV_OK || ret == DW_DLV_NO_ENTRY)
684 681 return (0);
685 682
686 683 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
687 684 "failed to child from die: %s\n",
688 685 dwarf_errmsg(derr));
689 686 return (ECTF_CONVBKERR);
690 687 }
691 688
692 689 /*
693 690 * Compilers disagree on what to do to determine if something has global
694 691 * visiblity. Traditionally gcc has used DW_AT_external to indicate this while
695 692 * Studio has used DW_AT_visibility. We check DW_AT_visibility first and then
696 693 * fall back to DW_AT_external. Lack of DW_AT_external implies that it is not.
697 694 */
698 695 static int
699 696 ctf_dwarf_isglobal(ctf_cu_t *cup, Dwarf_Die die, boolean_t *igp)
700 697 {
701 698 int ret;
702 699 Dwarf_Signed vis;
703 700 Dwarf_Bool ext;
704 701
705 702 if ((ret = ctf_dwarf_signed(cup, die, DW_AT_visibility, &vis)) == 0) {
706 703 *igp = vis == DW_VIS_exported;
707 704 return (0);
708 705 } else if (ret != ENOENT) {
709 706 return (ret);
710 707 }
711 708
712 709 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_external, &ext)) != 0) {
713 710 if (ret == ENOENT) {
714 711 *igp = B_FALSE;
715 712 return (0);
716 713 }
717 714 return (ret);
718 715 }
719 716 *igp = ext != 0 ? B_TRUE : B_FALSE;
720 717 return (0);
721 718 }
722 719
723 720 static int
724 721 ctf_dwarf_die_elfenc(Elf *elf, ctf_cu_t *cup, char *errbuf, size_t errlen)
725 722 {
726 723 GElf_Ehdr ehdr;
727 724
728 725 if (gelf_getehdr(elf, &ehdr) == NULL) {
729 726 (void) snprintf(errbuf, errlen,
730 727 "failed to get ELF header: %s\n",
731 728 elf_errmsg(elf_errno()));
732 729 return (ECTF_CONVBKERR);
733 730 }
734 731
735 732 cup->cu_mach = ehdr.e_machine;
736 733
737 734 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
738 735 cup->cu_ptrsz = 4;
739 736 VERIFY(ctf_setmodel(cup->cu_ctfp, CTF_MODEL_ILP32) == 0);
740 737 } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
741 738 cup->cu_ptrsz = 8;
742 739 VERIFY(ctf_setmodel(cup->cu_ctfp, CTF_MODEL_LP64) == 0);
743 740 } else {
744 741 (void) snprintf(errbuf, errlen,
745 742 "unknown ELF class %d", ehdr.e_ident[EI_CLASS]);
746 743 return (ECTF_CONVBKERR);
747 744 }
748 745
749 746 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) {
750 747 cup->cu_bigend = B_FALSE;
751 748 } else if (ehdr.e_ident[EI_DATA] == ELFDATA2MSB) {
752 749 cup->cu_bigend = B_TRUE;
753 750 } else {
754 751 (void) snprintf(errbuf, errlen,
755 752 "unknown ELF data encoding: %hhu", ehdr.e_ident[EI_DATA]);
756 753 return (ECTF_CONVBKERR);
757 754 }
758 755
759 756 return (0);
760 757 }
761 758
762 759 typedef struct ctf_dwarf_fpent {
763 760 size_t cdfe_size;
764 761 uint_t cdfe_enc[3];
765 762 } ctf_dwarf_fpent_t;
766 763
767 764 typedef struct ctf_dwarf_fpmap {
768 765 uint_t cdf_mach;
769 766 ctf_dwarf_fpent_t cdf_ents[4];
770 767 } ctf_dwarf_fpmap_t;
771 768
772 769 static const ctf_dwarf_fpmap_t ctf_dwarf_fpmaps[] = {
773 770 { EM_SPARC, {
774 771 { 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
775 772 { 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
776 773 { 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
777 774 { 0, { 0 } }
778 775 } },
779 776 { EM_SPARC32PLUS, {
780 777 { 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
781 778 { 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
782 779 { 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
783 780 { 0, { 0 } }
784 781 } },
785 782 { EM_SPARCV9, {
786 783 { 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
787 784 { 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
788 785 { 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
789 786 { 0, { 0 } }
790 787 } },
791 788 { EM_386, {
792 789 { 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
793 790 { 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
794 791 { 12, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
795 792 { 0, { 0 } }
796 793 } },
797 794 { EM_X86_64, {
798 795 { 4, { CTF_FP_SINGLE, CTF_FP_CPLX, CTF_FP_IMAGRY } },
799 796 { 8, { CTF_FP_DOUBLE, CTF_FP_DCPLX, CTF_FP_DIMAGRY } },
800 797 { 16, { CTF_FP_LDOUBLE, CTF_FP_LDCPLX, CTF_FP_LDIMAGRY } },
801 798 { 0, { 0 } }
802 799 } },
803 800 { EM_NONE }
804 801 };
805 802
806 803 static int
807 804 ctf_dwarf_float_base(ctf_cu_t *cup, Dwarf_Signed type, ctf_encoding_t *enc)
808 805 {
809 806 const ctf_dwarf_fpmap_t *map = &ctf_dwarf_fpmaps[0];
810 807 const ctf_dwarf_fpent_t *ent;
811 808 uint_t col = 0, mult = 1;
812 809
813 810 for (map = &ctf_dwarf_fpmaps[0]; map->cdf_mach != EM_NONE; map++) {
814 811 if (map->cdf_mach == cup->cu_mach)
815 812 break;
816 813 }
817 814
818 815 if (map->cdf_mach == EM_NONE) {
819 816 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
820 817 "Unsupported machine type: %d\n", cup->cu_mach);
821 818 return (ENOTSUP);
822 819 }
823 820
824 821 if (type == DW_ATE_complex_float) {
825 822 mult = 2;
826 823 col = 1;
827 824 } else if (type == DW_ATE_imaginary_float ||
828 825 type == DW_ATE_SUN_imaginary_float) {
829 826 col = 2;
830 827 }
831 828
832 829 ent = &map->cdf_ents[0];
833 830 for (ent = &map->cdf_ents[0]; ent->cdfe_size != 0; ent++) {
834 831 if (ent->cdfe_size * mult * 8 == enc->cte_bits) {
835 832 enc->cte_format = ent->cdfe_enc[col];
836 833 return (0);
837 834 }
838 835 }
839 836
840 837 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
841 838 "failed to find valid fp mapping for encoding %d, size %d bits\n",
842 839 type, enc->cte_bits);
843 840 return (EINVAL);
844 841 }
845 842
846 843 static int
847 844 ctf_dwarf_dwarf_base(ctf_cu_t *cup, Dwarf_Die die, int *kindp,
848 845 ctf_encoding_t *enc)
849 846 {
850 847 int ret;
851 848 Dwarf_Signed type;
852 849
853 850 if ((ret = ctf_dwarf_signed(cup, die, DW_AT_encoding, &type)) != 0)
854 851 return (ret);
855 852
856 853 switch (type) {
857 854 case DW_ATE_unsigned:
858 855 case DW_ATE_address:
859 856 *kindp = CTF_K_INTEGER;
860 857 enc->cte_format = 0;
861 858 break;
862 859 case DW_ATE_unsigned_char:
863 860 *kindp = CTF_K_INTEGER;
864 861 enc->cte_format = CTF_INT_CHAR;
865 862 break;
866 863 case DW_ATE_signed:
867 864 *kindp = CTF_K_INTEGER;
868 865 enc->cte_format = CTF_INT_SIGNED;
869 866 break;
870 867 case DW_ATE_signed_char:
871 868 *kindp = CTF_K_INTEGER;
872 869 enc->cte_format = CTF_INT_SIGNED | CTF_INT_CHAR;
873 870 break;
874 871 case DW_ATE_boolean:
875 872 *kindp = CTF_K_INTEGER;
876 873 enc->cte_format = CTF_INT_SIGNED | CTF_INT_BOOL;
877 874 break;
878 875 case DW_ATE_float:
879 876 case DW_ATE_complex_float:
880 877 case DW_ATE_imaginary_float:
881 878 case DW_ATE_SUN_imaginary_float:
882 879 case DW_ATE_SUN_interval_float:
883 880 *kindp = CTF_K_FLOAT;
884 881 if ((ret = ctf_dwarf_float_base(cup, type, enc)) != 0)
885 882 return (ret);
886 883 break;
887 884 default:
888 885 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
889 886 "encountered unkown DWARF encoding: %d", type);
890 887 return (ECTF_CONVBKERR);
891 888 }
892 889
893 890 return (0);
894 891 }
895 892
896 893 /*
897 894 * Different compilers (at least GCC and Studio) use different names for types.
898 895 * This parses the types and attempts to unify them. If this fails, we just fall
899 896 * back to using the DWARF itself.
900 897 */
901 898 static int
902 899 ctf_dwarf_parse_base(const char *name, int *kindp, ctf_encoding_t *enc,
903 900 char **newnamep)
904 901 {
905 902 char buf[256];
906 903 char *base, *c, *last;
907 904 int nlong = 0, nshort = 0, nchar = 0, nint = 0;
908 905 int sign = 1;
909 906
910 907 if (strlen(name) + 1 > sizeof (buf))
911 908 return (EINVAL);
912 909
913 910 (void) strlcpy(buf, name, sizeof (buf));
914 911 for (c = strtok_r(buf, " ", &last); c != NULL;
915 912 c = strtok_r(NULL, " ", &last)) {
916 913 if (strcmp(c, "signed") == 0) {
917 914 sign = 1;
918 915 } else if (strcmp(c, "unsigned") == 0) {
919 916 sign = 0;
920 917 } else if (strcmp(c, "long") == 0) {
921 918 nlong++;
922 919 } else if (strcmp(c, "char") == 0) {
923 920 nchar++;
924 921 } else if (strcmp(c, "short") == 0) {
925 922 nshort++;
926 923 } else if (strcmp(c, "int") == 0) {
927 924 nint++;
928 925 } else {
929 926 /*
930 927 * If we don't recognize any of the tokens, we'll tell
931 928 * the caller to fall back to the dwarf-provided
932 929 * encoding information.
933 930 */
934 931 return (EINVAL);
935 932 }
936 933 }
937 934
938 935 if (nchar > 1 || nshort > 1 || nint > 1 || nlong > 2)
939 936 return (EINVAL);
940 937
941 938 if (nchar > 0) {
942 939 if (nlong > 0 || nshort > 0 || nint > 0)
943 940 return (EINVAL);
944 941 base = "char";
945 942 } else if (nshort > 0) {
946 943 if (nlong > 0)
947 944 return (EINVAL);
948 945 base = "short";
949 946 } else if (nlong > 0) {
950 947 base = "long";
951 948 } else {
952 949 base = "int";
953 950 }
954 951
955 952 if (nchar > 0)
956 953 enc->cte_format = CTF_INT_CHAR;
957 954 else
958 955 enc->cte_format = 0;
959 956
960 957 if (sign > 0)
961 958 enc->cte_format |= CTF_INT_SIGNED;
962 959
963 960 (void) snprintf(buf, sizeof (buf), "%s%s%s",
964 961 (sign ? "" : "unsigned "),
965 962 (nlong > 1 ? "long " : ""),
966 963 base);
967 964
968 965 *newnamep = ctf_strdup(buf);
969 966 if (*newnamep == NULL)
970 967 return (ENOMEM);
971 968 *kindp = CTF_K_INTEGER;
972 969 return (0);
973 970 }
974 971
975 972 static int
976 973 ctf_dwarf_create_base(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot,
977 974 Dwarf_Off off)
978 975 {
979 976 int ret;
980 977 char *name, *nname;
981 978 Dwarf_Unsigned sz;
982 979 int kind;
983 980 ctf_encoding_t enc;
984 981 ctf_id_t id;
985 982
986 983 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0)
987 984 return (ret);
988 985 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size, &sz)) != 0) {
989 986 goto out;
990 987 }
991 988 ctf_dprintf("Creating base type %s from off %llu, size: %d\n", name,
992 989 off, sz);
993 990
994 991 bzero(&enc, sizeof (ctf_encoding_t));
995 992 enc.cte_bits = sz * 8;
996 993 if ((ret = ctf_dwarf_parse_base(name, &kind, &enc, &nname)) == 0) {
997 994 ctf_free(name, strlen(name) + 1);
998 995 name = nname;
999 996 } else {
1000 997 if (ret != EINVAL)
1001 998 return (ret);
1002 999 ctf_dprintf("falling back to dwarf for base type %s\n", name);
1003 1000 if ((ret = ctf_dwarf_dwarf_base(cup, die, &kind, &enc)) != 0)
1004 1001 return (ret);
1005 1002 }
1006 1003
1007 1004 id = ctf_add_encoded(cup->cu_ctfp, isroot, name, &enc, kind);
1008 1005 if (id == CTF_ERR) {
1009 1006 ret = ctf_errno(cup->cu_ctfp);
1010 1007 } else {
1011 1008 *idp = id;
1012 1009 ret = ctf_dwmap_add(cup, id, die, B_FALSE);
1013 1010 }
1014 1011 out:
1015 1012 ctf_free(name, strlen(name) + 1);
1016 1013 return (ret);
1017 1014 }
1018 1015
1019 1016 /*
1020 1017 * Getting a member's offset is a surprisingly intricate dance. It works as
1021 1018 * follows:
1022 1019 *
1023 1020 * 1) If we're in DWARFv4, then we either have a DW_AT_data_bit_offset or we
1024 1021 * have a DW_AT_data_member_location. We won't have both. Thus we check first
1025 1022 * for DW_AT_data_bit_offset, and if it exists, we're set.
1026 1023 *
1027 1024 * Next, if we have a bitfield and we don't have a DW_AT_data_bit_offset, then
1028 1025 * we have to grab the data location and use the following dance:
1029 1026 *
1030 1027 * 2) Gather the set of DW_AT_byte_size, DW_AT_bit_offset, and DW_AT_bit_size.
1031 1028 * Of course, the DW_AT_byte_size may be omitted, even though it isn't always.
1032 1029 * When it's been omitted, we then have to say that the size is that of the
1033 1030 * underlying type, which forces that to be after a ctf_update(). Here, we have
1034 1031 * to do different things based on whether or not we're using big endian or
1035 1032 * little endian to obtain the proper offset.
1036 1033 */
1037 1034 static int
1038 1035 ctf_dwarf_member_offset(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t mid,
1039 1036 ulong_t *offp)
1040 1037 {
1041 1038 int ret;
1042 1039 Dwarf_Unsigned loc, bitsz, bytesz;
1043 1040 Dwarf_Signed bitoff;
1044 1041 size_t off;
1045 1042 ssize_t tsz;
1046 1043
1047 1044 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_data_bit_offset,
1048 1045 &loc)) == 0) {
1049 1046 *offp = loc;
1050 1047 return (0);
1051 1048 } else if (ret != ENOENT) {
1052 1049 return (ret);
1053 1050 }
1054 1051
1055 1052 if ((ret = ctf_dwarf_member_location(cup, die, &loc)) != 0)
1056 1053 return (ret);
1057 1054 off = loc * 8;
1058 1055
1059 1056 if ((ret = ctf_dwarf_signed(cup, die, DW_AT_bit_offset,
1060 1057 &bitoff)) != 0) {
1061 1058 if (ret != ENOENT)
1062 1059 return (ret);
1063 1060 *offp = off;
1064 1061 return (0);
1065 1062 }
1066 1063
1067 1064 /* At this point we have to have DW_AT_bit_size */
1068 1065 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_bit_size, &bitsz)) != 0)
1069 1066 return (ret);
1070 1067
1071 1068 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size,
1072 1069 &bytesz)) != 0) {
1073 1070 if (ret != ENOENT)
1074 1071 return (ret);
1075 1072 if ((tsz = ctf_type_size(cup->cu_ctfp, mid)) == CTF_ERR) {
1076 1073 int e = ctf_errno(cup->cu_ctfp);
1077 1074 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1078 1075 "failed to get type size: %s", ctf_errmsg(e));
1079 1076 return (ECTF_CONVBKERR);
1080 1077 }
1081 1078 } else {
1082 1079 tsz = bytesz;
1083 1080 }
1084 1081 tsz *= 8;
1085 1082 if (cup->cu_bigend == B_TRUE) {
1086 1083 *offp = off + bitoff;
1087 1084 } else {
1088 1085 *offp = off + tsz - bitoff - bitsz;
1089 1086 }
1090 1087
1091 1088 return (0);
1092 1089 }
1093 1090
1094 1091 /*
1095 1092 * We need to determine if the member in question is a bitfield. If it is, then
1096 1093 * we need to go through and create a new type that's based on the actual base
1097 1094 * type, but has a different size. We also rename the type as a result to help
1098 1095 * deal with future collisions.
1099 1096 *
1100 1097 * Here we need to look and see if we have a DW_AT_bit_size value. If we have a
1101 1098 * bit size member and it does not equal the byte size member, then we need to
1102 1099 * create a bitfield type based on this.
1103 1100 *
1104 1101 * Note: When we support DWARFv4, there may be a chance that we need to also
1105 1102 * search for the DW_AT_byte_size if we don't have a DW_AT_bit_size member.
1106 1103 */
1107 1104 static int
1108 1105 ctf_dwarf_member_bitfield(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp)
1109 1106 {
1110 1107 int ret;
1111 1108 Dwarf_Unsigned bitsz;
1112 1109 ctf_encoding_t e;
1113 1110 ctf_dwbitf_t *cdb;
1114 1111 ctf_dtdef_t *dtd;
1115 1112 ctf_id_t base = *idp;
1116 1113 int kind;
1117 1114
1118 1115 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_bit_size, &bitsz)) != 0) {
1119 1116 if (ret == ENOENT)
1120 1117 return (0);
1121 1118 return (ret);
1122 1119 }
1123 1120
1124 1121 ctf_dprintf("Trying to deal with bitfields on %d:%d\n", base, bitsz);
1125 1122 /*
1126 1123 * Given that we now have a bitsize, time to go do something about it.
1127 1124 * We're going to create a new type based on the current one, but first
1128 1125 * we need to find the base type. This means we need to traverse any
1129 1126 * typedef's, consts, and volatiles until we get to what should be
1130 1127 * something of type integer or enumeration.
1131 1128 */
1132 1129 VERIFY(bitsz < UINT32_MAX);
1133 1130 dtd = ctf_dtd_lookup(cup->cu_ctfp, base);
1134 1131 VERIFY(dtd != NULL);
1135 1132 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1136 1133 while (kind == CTF_K_TYPEDEF || kind == CTF_K_CONST ||
1137 1134 kind == CTF_K_VOLATILE) {
1138 1135 dtd = ctf_dtd_lookup(cup->cu_ctfp, dtd->dtd_data.ctt_type);
1139 1136 VERIFY(dtd != NULL);
1140 1137 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1141 1138 }
1142 1139 ctf_dprintf("got kind %d\n", kind);
1143 1140 VERIFY(kind == CTF_K_INTEGER || kind == CTF_K_ENUM);
1144 1141
1145 1142 /*
1146 1143 * As surprising as it may be, it is strictly possible to create a
1147 1144 * bitfield that is based on an enum. Of course, the C standard leaves
1148 1145 * enums sizing as an ABI concern more or less. To that effect, today on
1149 1146 * all illumos platforms the size of an enum is generally that of an
1150 1147 * int as our supported data models and ABIs all agree on that. So what
1151 1148 * we'll do is fake up a CTF encoding here to use. In this case, we'll
1152 1149 * treat it as an unsigned value of whatever size the underlying enum
1153 1150 * currently has (which is in the ctt_size member of its dynamic type
1154 1151 * data).
1155 1152 */
1156 1153 if (kind == CTF_K_INTEGER) {
1157 1154 e = dtd->dtd_u.dtu_enc;
1158 1155 } else {
1159 1156 bzero(&e, sizeof (ctf_encoding_t));
1160 1157 e.cte_bits = dtd->dtd_data.ctt_size * NBBY;
1161 1158 }
1162 1159
1163 1160 for (cdb = ctf_list_next(&cup->cu_bitfields); cdb != NULL;
1164 1161 cdb = ctf_list_next(cdb)) {
1165 1162 if (cdb->cdb_base == base && cdb->cdb_nbits == bitsz)
1166 1163 break;
1167 1164 }
1168 1165
1169 1166 /*
1170 1167 * Create a new type if none exists. We name all types in a way that is
1171 1168 * guaranteed not to conflict with the corresponding C type. We do this
1172 1169 * by using the ':' operator.
1173 1170 */
1174 1171 if (cdb == NULL) {
1175 1172 size_t namesz;
1176 1173 char *name;
1177 1174
1178 1175 e.cte_bits = bitsz;
1179 1176 namesz = snprintf(NULL, 0, "%s:%d", dtd->dtd_name,
1180 1177 (uint32_t)bitsz);
1181 1178 name = ctf_alloc(namesz + 1);
1182 1179 if (name == NULL)
1183 1180 return (ENOMEM);
1184 1181 cdb = ctf_alloc(sizeof (ctf_dwbitf_t));
1185 1182 if (cdb == NULL) {
1186 1183 ctf_free(name, namesz + 1);
1187 1184 return (ENOMEM);
1188 1185 }
1189 1186 (void) snprintf(name, namesz + 1, "%s:%d", dtd->dtd_name,
1190 1187 (uint32_t)bitsz);
1191 1188
1192 1189 cdb->cdb_base = base;
1193 1190 cdb->cdb_nbits = bitsz;
1194 1191 cdb->cdb_id = ctf_add_integer(cup->cu_ctfp, CTF_ADD_NONROOT,
1195 1192 name, &e);
1196 1193 if (cdb->cdb_id == CTF_ERR) {
1197 1194 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1198 1195 "failed to get add bitfield type %s: %s", name,
1199 1196 ctf_errmsg(ctf_errno(cup->cu_ctfp)));
1200 1197 ctf_free(name, namesz + 1);
1201 1198 ctf_free(cdb, sizeof (ctf_dwbitf_t));
1202 1199 return (ECTF_CONVBKERR);
1203 1200 }
1204 1201 ctf_free(name, namesz + 1);
1205 1202 ctf_list_append(&cup->cu_bitfields, cdb);
1206 1203 }
1207 1204
1208 1205 *idp = cdb->cdb_id;
1209 1206
1210 1207 return (0);
1211 1208 }
1212 1209
1213 1210 static int
1214 1211 ctf_dwarf_fixup_sou(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t base, boolean_t add)
1215 1212 {
1216 1213 int ret, kind;
1217 1214 Dwarf_Die child, memb;
1218 1215 Dwarf_Unsigned size;
1219 1216 ulong_t nsz;
1220 1217
1221 1218 kind = ctf_type_kind(cup->cu_ctfp, base);
1222 1219 VERIFY(kind != CTF_ERR);
1223 1220 VERIFY(kind == CTF_K_STRUCT || kind == CTF_K_UNION);
1224 1221
1225 1222 /*
1226 1223 * Members are in children. However, gcc also allows empty ones.
1227 1224 */
1228 1225 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1229 1226 return (ret);
1230 1227 if (child == NULL)
1231 1228 return (0);
1232 1229
1233 1230 memb = child;
1234 1231 while (memb != NULL) {
1235 1232 Dwarf_Die sib, tdie;
1236 1233 Dwarf_Half tag;
1237 1234 ctf_id_t mid;
1238 1235 char *mname;
1239 1236 ulong_t memboff = 0;
1240 1237
1241 1238 if ((ret = ctf_dwarf_tag(cup, memb, &tag)) != 0)
1242 1239 return (ret);
1243 1240
1244 1241 if (tag != DW_TAG_member)
1245 1242 continue;
1246 1243
1247 1244 if ((ret = ctf_dwarf_refdie(cup, memb, DW_AT_type, &tdie)) != 0)
1248 1245 return (ret);
1249 1246
1250 1247 if ((ret = ctf_dwarf_convert_type(cup, tdie, &mid,
1251 1248 CTF_ADD_NONROOT)) != 0)
1252 1249 return (ret);
1253 1250 ctf_dprintf("Got back type id: %d\n", mid);
1254 1251
1255 1252 /*
1256 1253 * If we're not adding a member, just go ahead and return.
1257 1254 */
1258 1255 if (add == B_FALSE) {
1259 1256 if ((ret = ctf_dwarf_member_bitfield(cup, memb,
1260 1257 &mid)) != 0)
1261 1258 return (ret);
1262 1259 goto next;
1263 1260 }
1264 1261
1265 1262 if ((ret = ctf_dwarf_string(cup, memb, DW_AT_name,
1266 1263 &mname)) != 0 && ret != ENOENT)
1267 1264 return (ret);
1268 1265 if (ret == ENOENT)
1269 1266 mname = NULL;
1270 1267
1271 1268 if (kind == CTF_K_UNION) {
1272 1269 memboff = 0;
1273 1270 } else if ((ret = ctf_dwarf_member_offset(cup, memb, mid,
1274 1271 &memboff)) != 0) {
1275 1272 if (mname != NULL)
1276 1273 ctf_free(mname, strlen(mname) + 1);
1277 1274 return (ret);
1278 1275 }
1279 1276
1280 1277 if ((ret = ctf_dwarf_member_bitfield(cup, memb, &mid)) != 0)
1281 1278 return (ret);
1282 1279
1283 1280 ret = ctf_add_member(cup->cu_ctfp, base, mname, mid, memboff);
1284 1281 if (ret == CTF_ERR) {
1285 1282 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1286 1283 "failed to add member %s: %s",
1287 1284 mname, ctf_errmsg(ctf_errno(cup->cu_ctfp)));
1288 1285 if (mname != NULL)
1289 1286 ctf_free(mname, strlen(mname) + 1);
1290 1287 return (ECTF_CONVBKERR);
1291 1288 }
1292 1289
1293 1290 if (mname != NULL)
1294 1291 ctf_free(mname, strlen(mname) + 1);
1295 1292
1296 1293 next:
1297 1294 if ((ret = ctf_dwarf_sib(cup, memb, &sib)) != 0)
1298 1295 return (ret);
1299 1296 memb = sib;
1300 1297 }
1301 1298
1302 1299 /*
1303 1300 * If we're not adding members, then we don't know the final size of the
1304 1301 * structure, so end here.
1305 1302 */
1306 1303 if (add == B_FALSE)
1307 1304 return (0);
1308 1305
1309 1306 /* Finally set the size of the structure to the actual byte size */
1310 1307 if ((ret = ctf_dwarf_unsigned(cup, die, DW_AT_byte_size, &size)) != 0)
1311 1308 return (ret);
1312 1309 nsz = size;
1313 1310 if ((ctf_set_size(cup->cu_ctfp, base, nsz)) == CTF_ERR) {
1314 1311 int e = ctf_errno(cup->cu_ctfp);
1315 1312 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1316 1313 "failed to set type size for %d to 0x%x: %s", base,
1317 1314 (uint32_t)size, ctf_errmsg(e));
1318 1315 return (ECTF_CONVBKERR);
1319 1316 }
1320 1317
1321 1318 return (0);
1322 1319 }
1323 1320
1324 1321 static int
1325 1322 ctf_dwarf_create_sou(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1326 1323 int kind, int isroot)
1327 1324 {
1328 1325 int ret;
1329 1326 char *name;
1330 1327 ctf_id_t base;
1331 1328 Dwarf_Die child;
1332 1329 Dwarf_Bool decl;
1333 1330
1334 1331 /*
1335 1332 * Deal with the terribly annoying case of anonymous structs and unions.
1336 1333 * If they don't have a name, set the name to the empty string.
1337 1334 */
1338 1335 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1339 1336 ret != ENOENT)
1340 1337 return (ret);
1341 1338 if (ret == ENOENT)
1342 1339 name = NULL;
1343 1340
1344 1341 /*
1345 1342 * We need to check if we just have a declaration here. If we do, then
1346 1343 * instead of creating an actual structure or union, we're just going to
1347 1344 * go ahead and create a forward. During a dedup or merge, the forward
1348 1345 * will be replaced with the real thing.
1349 1346 */
1350 1347 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration,
1351 1348 &decl)) != 0) {
1352 1349 if (ret != ENOENT)
1353 1350 return (ret);
1354 1351 decl = 0;
1355 1352 }
1356 1353
1357 1354 if (decl != 0) {
1358 1355 base = ctf_add_forward(cup->cu_ctfp, isroot, name, kind);
1359 1356 } else if (kind == CTF_K_STRUCT) {
1360 1357 base = ctf_add_struct(cup->cu_ctfp, isroot, name);
1361 1358 } else {
1362 1359 base = ctf_add_union(cup->cu_ctfp, isroot, name);
1363 1360 }
1364 1361 ctf_dprintf("added sou %s (%d) (%d)\n", name, kind, base);
1365 1362 if (name != NULL)
1366 1363 ctf_free(name, strlen(name) + 1);
1367 1364 if (base == CTF_ERR)
1368 1365 return (ctf_errno(cup->cu_ctfp));
1369 1366 *idp = base;
1370 1367
1371 1368 /*
1372 1369 * If it's just a declaration, we're not going to mark it for fix up or
1373 1370 * do anything else.
1374 1371 */
1375 1372 if (decl == B_TRUE)
1376 1373 return (ctf_dwmap_add(cup, base, die, B_FALSE));
1377 1374 if ((ret = ctf_dwmap_add(cup, base, die, B_TRUE)) != 0)
1378 1375 return (ret);
1379 1376
1380 1377 /*
1381 1378 * Members are in children. However, gcc also allows empty ones.
1382 1379 */
1383 1380 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1384 1381 return (ret);
1385 1382 if (child == NULL)
1386 1383 return (0);
1387 1384
1388 1385 return (0);
1389 1386 }
1390 1387
1391 1388 static int
1392 1389 ctf_dwarf_create_array_range(ctf_cu_t *cup, Dwarf_Die range, ctf_id_t *idp,
1393 1390 ctf_id_t base, int isroot)
1394 1391 {
1395 1392 int ret;
1396 1393 Dwarf_Die sib;
1397 1394 Dwarf_Unsigned val;
1398 1395 Dwarf_Signed sval;
1399 1396 ctf_arinfo_t ar;
1400 1397
1401 1398 ctf_dprintf("creating array range\n");
1402 1399
1403 1400 if ((ret = ctf_dwarf_sib(cup, range, &sib)) != 0)
1404 1401 return (ret);
1405 1402 if (sib != NULL) {
1406 1403 ctf_id_t id;
1407 1404 if ((ret = ctf_dwarf_create_array_range(cup, sib, &id,
1408 1405 base, CTF_ADD_NONROOT)) != 0)
1409 1406 return (ret);
1410 1407 ar.ctr_contents = id;
1411 1408 } else {
1412 1409 ar.ctr_contents = base;
1413 1410 }
1414 1411
1415 1412 if ((ar.ctr_index = ctf_dwarf_long(cup)) == CTF_ERR)
1416 1413 return (ctf_errno(cup->cu_ctfp));
1417 1414
1418 1415 /*
1419 1416 * Array bounds can be signed or unsigned, but there are several kinds
1420 1417 * of signless forms (data1, data2, etc) that take their sign from the
1421 1418 * routine that is trying to interpret them. That is, data1 can be
1422 1419 * either signed or unsigned, depending on whether you use the signed or
1423 1420 * unsigned accessor function. GCC will use the signless forms to store
1424 1421 * unsigned values which have their high bit set, so we need to try to
1425 1422 * read them first as unsigned to get positive values. We could also
1426 1423 * try signed first, falling back to unsigned if we got a negative
1427 1424 * value.
1428 1425 */
1429 1426 if ((ret = ctf_dwarf_unsigned(cup, range, DW_AT_upper_bound,
1430 1427 &val)) == 0) {
1431 1428 ar.ctr_nelems = val + 1;
1432 1429 } else if (ret != ENOENT) {
1433 1430 return (ret);
1434 1431 } else if ((ret = ctf_dwarf_signed(cup, range, DW_AT_upper_bound,
1435 1432 &sval)) == 0) {
1436 1433 ar.ctr_nelems = sval + 1;
1437 1434 } else if (ret != ENOENT) {
1438 1435 return (ret);
1439 1436 } else {
1440 1437 ar.ctr_nelems = 0;
1441 1438 }
1442 1439
1443 1440 if ((*idp = ctf_add_array(cup->cu_ctfp, isroot, &ar)) == CTF_ERR)
1444 1441 return (ctf_errno(cup->cu_ctfp));
1445 1442
1446 1443 return (0);
1447 1444 }
1448 1445
1449 1446 /*
1450 1447 * Try and create an array type. First, the kind of the array is specified in
1451 1448 * the DW_AT_type entry. Next, the number of entries is stored in a more
1452 1449 * complicated form, we should have a child that has the DW_TAG_subrange type.
1453 1450 */
1454 1451 static int
1455 1452 ctf_dwarf_create_array(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1456 1453 {
1457 1454 int ret;
1458 1455 Dwarf_Die tdie, rdie;
1459 1456 ctf_id_t tid;
1460 1457 Dwarf_Half rtag;
1461 1458
1462 1459 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0)
1463 1460 return (ret);
1464 1461 if ((ret = ctf_dwarf_convert_type(cup, tdie, &tid,
1465 1462 CTF_ADD_NONROOT)) != 0)
1466 1463 return (ret);
1467 1464
1468 1465 if ((ret = ctf_dwarf_child(cup, die, &rdie)) != 0)
1469 1466 return (ret);
1470 1467 if ((ret = ctf_dwarf_tag(cup, rdie, &rtag)) != 0)
1471 1468 return (ret);
1472 1469 if (rtag != DW_TAG_subrange_type) {
1473 1470 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1474 1471 "encountered array without DW_TAG_subrange_type child\n");
1475 1472 return (ECTF_CONVBKERR);
1476 1473 }
1477 1474
1478 1475 /*
1479 1476 * The compiler may opt to describe a multi-dimensional array as one
1480 1477 * giant array or it may opt to instead encode it as a series of
1481 1478 * subranges. If it's the latter, then for each subrange we introduce a
1482 1479 * type. We can always use the base type.
1483 1480 */
1484 1481 if ((ret = ctf_dwarf_create_array_range(cup, rdie, idp, tid,
1485 1482 isroot)) != 0)
1486 1483 return (ret);
1487 1484 ctf_dprintf("Got back id %d\n", *idp);
1488 1485 return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1489 1486 }
1490 1487
1491 1488 static int
1492 1489 ctf_dwarf_create_reference(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1493 1490 int kind, int isroot)
1494 1491 {
1495 1492 int ret;
1496 1493 ctf_id_t id;
1497 1494 Dwarf_Die tdie;
1498 1495 char *name;
1499 1496 size_t namelen;
1500 1497
1501 1498 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1502 1499 ret != ENOENT)
1503 1500 return (ret);
1504 1501 if (ret == ENOENT) {
1505 1502 name = NULL;
1506 1503 namelen = 0;
1507 1504 } else {
1508 1505 namelen = strlen(name);
1509 1506 }
1510 1507
1511 1508 ctf_dprintf("reference kind %d %s\n", kind, name != NULL ? name : "<>");
1512 1509
1513 1510 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
1514 1511 if (ret != ENOENT) {
1515 1512 ctf_free(name, namelen);
1516 1513 return (ret);
1517 1514 }
1518 1515 if ((id = ctf_dwarf_void(cup)) == CTF_ERR) {
1519 1516 ctf_free(name, namelen);
1520 1517 return (ctf_errno(cup->cu_ctfp));
1521 1518 }
1522 1519 } else {
1523 1520 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
1524 1521 CTF_ADD_NONROOT)) != 0) {
1525 1522 ctf_free(name, namelen);
1526 1523 return (ret);
1527 1524 }
1528 1525 }
1529 1526
1530 1527 if ((*idp = ctf_add_reftype(cup->cu_ctfp, isroot, name, id, kind)) ==
1531 1528 CTF_ERR) {
1532 1529 ctf_free(name, namelen);
1533 1530 return (ctf_errno(cup->cu_ctfp));
1534 1531 }
1535 1532
1536 1533 ctf_free(name, namelen);
1537 1534 return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1538 1535 }
1539 1536
1540 1537 static int
1541 1538 ctf_dwarf_create_enum(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1542 1539 {
1543 1540 int ret;
1544 1541 ctf_id_t id;
1545 1542 Dwarf_Die child;
1546 1543 char *name;
1547 1544
1548 1545 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
1549 1546 ret != ENOENT)
1550 1547 return (ret);
1551 1548 if (ret == ENOENT)
1552 1549 name = NULL;
1553 1550 id = ctf_add_enum(cup->cu_ctfp, isroot, name);
1554 1551 ctf_dprintf("added enum %s (%d)\n", name, id);
1555 1552 if (name != NULL)
1556 1553 ctf_free(name, strlen(name) + 1);
1557 1554 if (id == CTF_ERR)
1558 1555 return (ctf_errno(cup->cu_ctfp));
1559 1556 *idp = id;
1560 1557 if ((ret = ctf_dwmap_add(cup, id, die, B_FALSE)) != 0)
1561 1558 return (ret);
1562 1559
1563 1560 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) {
1564 1561 if (ret == ENOENT)
1565 1562 ret = 0;
1566 1563 return (ret);
1567 1564 }
1568 1565
1569 1566 while (child != NULL) {
1570 1567 Dwarf_Half tag;
1571 1568 Dwarf_Signed sval;
1572 1569 Dwarf_Unsigned uval;
1573 1570 Dwarf_Die arg = child;
1574 1571 int eval;
1575 1572
1576 1573 if ((ret = ctf_dwarf_sib(cup, arg, &child)) != 0)
1577 1574 return (ret);
1578 1575
1579 1576 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1580 1577 return (ret);
1581 1578
1582 1579 if (tag != DW_TAG_enumerator) {
1583 1580 if ((ret = ctf_dwarf_convert_type(cup, arg, NULL,
1584 1581 CTF_ADD_NONROOT)) != 0)
1585 1582 return (ret);
1586 1583 continue;
1587 1584 }
1588 1585
1589 1586 /*
1590 1587 * DWARF v4 section 5.7 tells us we'll always have names.
1591 1588 */
1592 1589 if ((ret = ctf_dwarf_string(cup, arg, DW_AT_name, &name)) != 0)
1593 1590 return (ret);
1594 1591
1595 1592 /*
1596 1593 * We have to be careful here: newer GCCs generate DWARF where
1597 1594 * an unsigned value will happily pass ctf_dwarf_signed().
1598 1595 * Since negative values will fail ctf_dwarf_unsigned(), we try
1599 1596 * that first to make sure we get the right value.
1600 1597 */
1601 1598 if ((ret = ctf_dwarf_unsigned(cup, arg, DW_AT_const_value,
1602 1599 &uval)) == 0) {
1603 1600 eval = (int)uval;
1604 1601 } else if ((ret = ctf_dwarf_signed(cup, arg, DW_AT_const_value,
1605 1602 &sval)) == 0) {
1606 1603 eval = sval;
1607 1604 }
1608 1605
1609 1606 if (ret != 0) {
1610 1607 if (ret != ENOENT)
1611 1608 return (ret);
1612 1609
1613 1610 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1614 1611 "encountered enumeration without constant value\n");
1615 1612 return (ECTF_CONVBKERR);
1616 1613 }
1617 1614
1618 1615 ret = ctf_add_enumerator(cup->cu_ctfp, id, name, eval);
1619 1616 if (ret == CTF_ERR) {
1620 1617 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1621 1618 "failed to add enumarator %s (%d) to %d\n",
1622 1619 name, eval, id);
1623 1620 ctf_free(name, strlen(name) + 1);
1624 1621 return (ctf_errno(cup->cu_ctfp));
1625 1622 }
1626 1623 ctf_free(name, strlen(name) + 1);
1627 1624 }
1628 1625
1629 1626 return (0);
1630 1627 }
1631 1628
1632 1629 /*
1633 1630 * For a function pointer, walk over and process all of its children, unless we
1634 1631 * encounter one that's just a declaration. In which case, we error on it.
1635 1632 */
1636 1633 static int
1637 1634 ctf_dwarf_create_fptr(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot)
1638 1635 {
1639 1636 int ret;
1640 1637 Dwarf_Bool b;
1641 1638 ctf_funcinfo_t fi;
1642 1639 Dwarf_Die retdie;
1643 1640 ctf_id_t *argv = NULL;
1644 1641
1645 1642 bzero(&fi, sizeof (ctf_funcinfo_t));
1646 1643
1647 1644 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) != 0) {
1648 1645 if (ret != ENOENT)
1649 1646 return (ret);
1650 1647 } else {
1651 1648 if (b != 0)
1652 1649 return (EPROTOTYPE);
1653 1650 }
1654 1651
1655 1652 /*
1656 1653 * Return type is in DW_AT_type, if none, it returns void.
1657 1654 */
1658 1655 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &retdie)) != 0) {
1659 1656 if (ret != ENOENT)
1660 1657 return (ret);
1661 1658 if ((fi.ctc_return = ctf_dwarf_void(cup)) == CTF_ERR)
1662 1659 return (ctf_errno(cup->cu_ctfp));
1663 1660 } else {
1664 1661 if ((ret = ctf_dwarf_convert_type(cup, retdie, &fi.ctc_return,
1665 1662 CTF_ADD_NONROOT)) != 0)
1666 1663 return (ret);
1667 1664 }
1668 1665
1669 1666 if ((ret = ctf_dwarf_function_count(cup, die, &fi, B_TRUE)) != 0) {
1670 1667 return (ret);
1671 1668 }
1672 1669
1673 1670 if (fi.ctc_argc != 0) {
1674 1671 argv = ctf_alloc(sizeof (ctf_id_t) * fi.ctc_argc);
1675 1672 if (argv == NULL)
1676 1673 return (ENOMEM);
1677 1674
1678 1675 if ((ret = ctf_dwarf_convert_fargs(cup, die, &fi, argv)) != 0) {
1679 1676 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1680 1677 return (ret);
1681 1678 }
1682 1679 }
1683 1680
1684 1681 if ((*idp = ctf_add_funcptr(cup->cu_ctfp, isroot, &fi, argv)) ==
1685 1682 CTF_ERR) {
1686 1683 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1687 1684 return (ctf_errno(cup->cu_ctfp));
1688 1685 }
1689 1686
1690 1687 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc);
1691 1688 return (ctf_dwmap_add(cup, *idp, die, B_FALSE));
1692 1689 }
1693 1690
1694 1691 static int
1695 1692 ctf_dwarf_convert_type(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp,
1696 1693 int isroot)
1697 1694 {
1698 1695 int ret;
1699 1696 Dwarf_Off offset;
1700 1697 Dwarf_Half tag;
1701 1698 ctf_dwmap_t lookup, *map;
1702 1699 ctf_id_t id;
1703 1700
1704 1701 if (idp == NULL)
1705 1702 idp = &id;
1706 1703
1707 1704 if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0)
1708 1705 return (ret);
1709 1706
1710 1707 if (offset > cup->cu_maxoff) {
1711 1708 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
1712 1709 "die offset %llu beyond maximum for header %llu\n",
1713 1710 offset, cup->cu_maxoff);
1714 1711 return (ECTF_CONVBKERR);
1715 1712 }
1716 1713
1717 1714 /*
1718 1715 * If we've already added an entry for this offset, then we're done.
1719 1716 */
1720 1717 lookup.cdm_off = offset;
1721 1718 if ((map = avl_find(&cup->cu_map, &lookup, NULL)) != NULL) {
1722 1719 *idp = map->cdm_id;
1723 1720 return (0);
1724 1721 }
1725 1722
1726 1723 if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0)
1727 1724 return (ret);
1728 1725
1729 1726 ret = ENOTSUP;
1730 1727 switch (tag) {
1731 1728 case DW_TAG_base_type:
1732 1729 ctf_dprintf("base\n");
1733 1730 ret = ctf_dwarf_create_base(cup, die, idp, isroot, offset);
1734 1731 break;
1735 1732 case DW_TAG_array_type:
1736 1733 ctf_dprintf("array\n");
1737 1734 ret = ctf_dwarf_create_array(cup, die, idp, isroot);
1738 1735 break;
1739 1736 case DW_TAG_enumeration_type:
1740 1737 ctf_dprintf("enum\n");
1741 1738 ret = ctf_dwarf_create_enum(cup, die, idp, isroot);
1742 1739 break;
1743 1740 case DW_TAG_pointer_type:
1744 1741 ctf_dprintf("pointer\n");
1745 1742 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_POINTER,
1746 1743 isroot);
1747 1744 break;
1748 1745 case DW_TAG_structure_type:
1749 1746 ctf_dprintf("struct\n");
1750 1747 ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_STRUCT,
1751 1748 isroot);
1752 1749 break;
1753 1750 case DW_TAG_subroutine_type:
1754 1751 ctf_dprintf("fptr\n");
1755 1752 ret = ctf_dwarf_create_fptr(cup, die, idp, isroot);
1756 1753 break;
1757 1754 case DW_TAG_typedef:
1758 1755 ctf_dprintf("typedef\n");
1759 1756 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_TYPEDEF,
1760 1757 isroot);
1761 1758 break;
1762 1759 case DW_TAG_union_type:
1763 1760 ctf_dprintf("union\n");
1764 1761 ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_UNION,
1765 1762 isroot);
1766 1763 break;
1767 1764 case DW_TAG_const_type:
1768 1765 ctf_dprintf("const\n");
1769 1766 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_CONST,
1770 1767 isroot);
1771 1768 break;
1772 1769 case DW_TAG_volatile_type:
1773 1770 ctf_dprintf("volatile\n");
1774 1771 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_VOLATILE,
1775 1772 isroot);
1776 1773 break;
1777 1774 case DW_TAG_restrict_type:
1778 1775 ctf_dprintf("restrict\n");
1779 1776 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_RESTRICT,
1780 1777 isroot);
1781 1778 break;
1782 1779 default:
1783 1780 ctf_dprintf("ignoring tag type %x\n", tag);
1784 1781 ret = 0;
1785 1782 break;
1786 1783 }
1787 1784 ctf_dprintf("ctf_dwarf_convert_type tag specific handler returned %d\n",
1788 1785 ret);
1789 1786
1790 1787 return (ret);
1791 1788 }
1792 1789
1793 1790 static int
1794 1791 ctf_dwarf_walk_lexical(ctf_cu_t *cup, Dwarf_Die die)
1795 1792 {
1796 1793 int ret;
1797 1794 Dwarf_Die child;
1798 1795
1799 1796 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1800 1797 return (ret);
1801 1798
1802 1799 if (child == NULL)
1803 1800 return (0);
1804 1801
1805 1802 return (ctf_dwarf_convert_die(cup, die));
1806 1803 }
1807 1804
1808 1805 static int
1809 1806 ctf_dwarf_function_count(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1810 1807 boolean_t fptr)
1811 1808 {
1812 1809 int ret;
1813 1810 Dwarf_Die child, sib, arg;
1814 1811
1815 1812 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1816 1813 return (ret);
1817 1814
1818 1815 arg = child;
1819 1816 while (arg != NULL) {
1820 1817 Dwarf_Half tag;
1821 1818
1822 1819 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1823 1820 return (ret);
1824 1821
1825 1822 /*
1826 1823 * We have to check for a varargs type decleration. This will
1827 1824 * happen in one of two ways. If we have a function pointer
1828 1825 * type, then it'll be done with a tag of type
1829 1826 * DW_TAG_unspecified_parameters. However, it only means we have
1830 1827 * a variable number of arguments, if we have more than one
1831 1828 * argument found so far. Otherwise, when we have a function
1832 1829 * type, it instead uses a formal parameter whose name is '...'
1833 1830 * to indicate a variable arguments member.
1834 1831 *
1835 1832 * Also, if we have a function pointer, then we have to expect
1836 1833 * that we might not get a name at all.
1837 1834 */
1838 1835 if (tag == DW_TAG_formal_parameter && fptr == B_FALSE) {
1839 1836 char *name;
1840 1837 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name,
1841 1838 &name)) != 0)
1842 1839 return (ret);
1843 1840 if (strcmp(name, DWARF_VARARGS_NAME) == 0)
1844 1841 fip->ctc_flags |= CTF_FUNC_VARARG;
1845 1842 else
1846 1843 fip->ctc_argc++;
1847 1844 ctf_free(name, strlen(name) + 1);
1848 1845 } else if (tag == DW_TAG_formal_parameter) {
1849 1846 fip->ctc_argc++;
1850 1847 } else if (tag == DW_TAG_unspecified_parameters &&
1851 1848 fip->ctc_argc > 0) {
1852 1849 fip->ctc_flags |= CTF_FUNC_VARARG;
1853 1850 }
1854 1851 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1855 1852 return (ret);
1856 1853 arg = sib;
1857 1854 }
1858 1855
1859 1856 return (0);
1860 1857 }
1861 1858
1862 1859 static int
1863 1860 ctf_dwarf_convert_fargs(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip,
1864 1861 ctf_id_t *argv)
1865 1862 {
1866 1863 int ret;
1867 1864 int i = 0;
1868 1865 Dwarf_Die child, sib, arg;
1869 1866
1870 1867 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0)
1871 1868 return (ret);
1872 1869
1873 1870 arg = child;
1874 1871 while (arg != NULL) {
1875 1872 Dwarf_Half tag;
1876 1873
1877 1874 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0)
1878 1875 return (ret);
1879 1876 if (tag == DW_TAG_formal_parameter) {
1880 1877 Dwarf_Die tdie;
1881 1878
1882 1879 if ((ret = ctf_dwarf_refdie(cup, arg, DW_AT_type,
1883 1880 &tdie)) != 0)
1884 1881 return (ret);
1885 1882
1886 1883 if ((ret = ctf_dwarf_convert_type(cup, tdie, &argv[i],
1887 1884 CTF_ADD_ROOT)) != 0)
1888 1885 return (ret);
1889 1886 i++;
1890 1887
1891 1888 /*
1892 1889 * Once we hit argc entries, we're done. This ensures we
1893 1890 * don't accidentally hit a varargs which should be the
1894 1891 * last entry.
1895 1892 */
1896 1893 if (i == fip->ctc_argc)
1897 1894 break;
1898 1895 }
1899 1896
1900 1897 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0)
1901 1898 return (ret);
1902 1899 arg = sib;
1903 1900 }
1904 1901
1905 1902 return (0);
1906 1903 }
1907 1904
1908 1905 static int
1909 1906 ctf_dwarf_convert_function(ctf_cu_t *cup, Dwarf_Die die)
1910 1907 {
1911 1908 int ret;
1912 1909 char *name;
1913 1910 ctf_dwfunc_t *cdf;
1914 1911 Dwarf_Die tdie;
1915 1912
1916 1913 /*
1917 1914 * Functions that don't have a name are generally functions that have
1918 1915 * been inlined and thus most information about them has been lost. If
1919 1916 * we can't get a name, then instead of returning ENOENT, we silently
1920 1917 * swallow the error.
1921 1918 */
1922 1919 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0) {
1923 1920 if (ret == ENOENT)
1924 1921 return (0);
1925 1922 return (ret);
1926 1923 }
1927 1924
1928 1925 ctf_dprintf("beginning work on function %s\n", name);
1929 1926 if ((cdf = ctf_alloc(sizeof (ctf_dwfunc_t))) == NULL) {
1930 1927 ctf_free(name, strlen(name) + 1);
1931 1928 return (ENOMEM);
1932 1929 }
1933 1930 bzero(cdf, sizeof (ctf_dwfunc_t));
1934 1931 cdf->cdf_name = name;
1935 1932
1936 1933 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) == 0) {
1937 1934 if ((ret = ctf_dwarf_convert_type(cup, tdie,
1938 1935 &(cdf->cdf_fip.ctc_return), CTF_ADD_ROOT)) != 0) {
1939 1936 ctf_free(name, strlen(name) + 1);
1940 1937 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1941 1938 return (ret);
1942 1939 }
1943 1940 } else if (ret != ENOENT) {
1944 1941 ctf_free(name, strlen(name) + 1);
1945 1942 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1946 1943 return (ret);
1947 1944 } else {
1948 1945 if ((cdf->cdf_fip.ctc_return = ctf_dwarf_void(cup)) ==
1949 1946 CTF_ERR) {
1950 1947 ctf_free(name, strlen(name) + 1);
1951 1948 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1952 1949 return (ctf_errno(cup->cu_ctfp));
1953 1950 }
1954 1951 }
1955 1952
1956 1953 /*
1957 1954 * A function has a number of children, some of which may not be ones we
1958 1955 * care about. Children that we care about have a type of
1959 1956 * DW_TAG_formal_parameter. We're going to do two passes, the first to
1960 1957 * count the arguments, the second to process them. Afterwards, we
1961 1958 * should be good to go ahead and add this function.
1962 1959 *
1963 1960 * Note, we already got the return type by going in and grabbing it out
1964 1961 * of the DW_AT_type.
1965 1962 */
1966 1963 if ((ret = ctf_dwarf_function_count(cup, die, &cdf->cdf_fip,
1967 1964 B_FALSE)) != 0) {
1968 1965 ctf_free(name, strlen(name) + 1);
1969 1966 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1970 1967 return (ret);
1971 1968 }
1972 1969
1973 1970 ctf_dprintf("beginning to convert function arguments %s\n", name);
1974 1971 if (cdf->cdf_fip.ctc_argc != 0) {
1975 1972 uint_t argc = cdf->cdf_fip.ctc_argc;
1976 1973 cdf->cdf_argv = ctf_alloc(sizeof (ctf_id_t) * argc);
1977 1974 if (cdf->cdf_argv == NULL) {
1978 1975 ctf_free(name, strlen(name) + 1);
1979 1976 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1980 1977 return (ENOMEM);
1981 1978 }
1982 1979 if ((ret = ctf_dwarf_convert_fargs(cup, die,
1983 1980 &cdf->cdf_fip, cdf->cdf_argv)) != 0) {
1984 1981 ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) * argc);
1985 1982 ctf_free(name, strlen(name) + 1);
1986 1983 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1987 1984 return (ret);
1988 1985 }
1989 1986 } else {
1990 1987 cdf->cdf_argv = NULL;
1991 1988 }
1992 1989
1993 1990 if ((ret = ctf_dwarf_isglobal(cup, die, &cdf->cdf_global)) != 0) {
1994 1991 ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) *
1995 1992 cdf->cdf_fip.ctc_argc);
1996 1993 ctf_free(name, strlen(name) + 1);
1997 1994 ctf_free(cdf, sizeof (ctf_dwfunc_t));
1998 1995 return (ret);
1999 1996 }
2000 1997
2001 1998 ctf_list_append(&cup->cu_funcs, cdf);
2002 1999 return (ret);
2003 2000 }
2004 2001
2005 2002 /*
2006 2003 * Convert variables, but only if they're not prototypes and have names.
2007 2004 */
2008 2005 static int
2009 2006 ctf_dwarf_convert_variable(ctf_cu_t *cup, Dwarf_Die die)
2010 2007 {
2011 2008 int ret;
2012 2009 char *name;
2013 2010 Dwarf_Bool b;
2014 2011 Dwarf_Die tdie;
2015 2012 ctf_id_t id;
2016 2013 ctf_dwvar_t *cdv;
2017 2014
2018 2015 /* Skip "Non-Defining Declarations" */
2019 2016 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) == 0) {
2020 2017 if (b != 0)
2021 2018 return (0);
2022 2019 } else if (ret != ENOENT) {
2023 2020 return (ret);
2024 2021 }
2025 2022
2026 2023 /*
2027 2024 * If we find a DIE of "Declarations Completing Non-Defining
2028 2025 * Declarations", we will use the referenced type's DIE. This isn't
2029 2026 * quite correct, e.g. DW_AT_decl_line will be the forward declaration
2030 2027 * not this site. It's sufficient for what we need, however: in
2031 2028 * particular, we should find DW_AT_external as needed there.
2032 2029 */
2033 2030 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_specification,
2034 2031 &tdie)) == 0) {
2035 2032 Dwarf_Off offset;
2036 2033 if ((ret = ctf_dwarf_offset(cup, tdie, &offset)) != 0)
2037 2034 return (ret);
2038 2035 ctf_dprintf("die 0x%llx DW_AT_specification -> die 0x%llx\n",
2039 2036 ctf_die_offset(die), ctf_die_offset(tdie));
2040 2037 die = tdie;
2041 2038 } else if (ret != ENOENT) {
2042 2039 return (ret);
2043 2040 }
2044 2041
2045 2042 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 &&
2046 2043 ret != ENOENT)
2047 2044 return (ret);
2048 2045 if (ret == ENOENT)
2049 2046 return (0);
2050 2047
2051 2048 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) {
2052 2049 ctf_free(name, strlen(name) + 1);
2053 2050 return (ret);
2054 2051 }
2055 2052
2056 2053 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id,
2057 2054 CTF_ADD_ROOT)) != 0)
2058 2055 return (ret);
2059 2056
2060 2057 if ((cdv = ctf_alloc(sizeof (ctf_dwvar_t))) == NULL) {
2061 2058 ctf_free(name, strlen(name) + 1);
2062 2059 return (ENOMEM);
2063 2060 }
2064 2061
2065 2062 cdv->cdv_name = name;
2066 2063 cdv->cdv_type = id;
2067 2064
2068 2065 if ((ret = ctf_dwarf_isglobal(cup, die, &cdv->cdv_global)) != 0) {
2069 2066 ctf_free(cdv, sizeof (ctf_dwvar_t));
2070 2067 ctf_free(name, strlen(name) + 1);
2071 2068 return (ret);
2072 2069 }
2073 2070
2074 2071 ctf_list_append(&cup->cu_vars, cdv);
2075 2072 return (0);
2076 2073 }
2077 2074
2078 2075 /*
2079 2076 * Walk through our set of top-level types and process them.
2080 2077 */
2081 2078 static int
2082 2079 ctf_dwarf_walk_toplevel(ctf_cu_t *cup, Dwarf_Die die)
2083 2080 {
2084 2081 int ret;
2085 2082 Dwarf_Off offset;
2086 2083 Dwarf_Half tag;
2087 2084
2088 2085 if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0)
2089 2086 return (ret);
2090 2087
2091 2088 if (offset > cup->cu_maxoff) {
2092 2089 (void) snprintf(cup->cu_errbuf, cup->cu_errlen,
2093 2090 "die offset %llu beyond maximum for header %llu\n",
2094 2091 offset, cup->cu_maxoff);
2095 2092 return (ECTF_CONVBKERR);
2096 2093 }
2097 2094
2098 2095 if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0)
2099 2096 return (ret);
2100 2097
2101 2098 ret = 0;
2102 2099 switch (tag) {
2103 2100 case DW_TAG_subprogram:
2104 2101 ctf_dprintf("top level func\n");
2105 2102 ret = ctf_dwarf_convert_function(cup, die);
2106 2103 break;
2107 2104 case DW_TAG_variable:
2108 2105 ctf_dprintf("top level var\n");
2109 2106 ret = ctf_dwarf_convert_variable(cup, die);
2110 2107 break;
2111 2108 case DW_TAG_lexical_block:
2112 2109 ctf_dprintf("top level block\n");
2113 2110 ret = ctf_dwarf_walk_lexical(cup, die);
2114 2111 break;
2115 2112 case DW_TAG_enumeration_type:
2116 2113 case DW_TAG_structure_type:
2117 2114 case DW_TAG_typedef:
2118 2115 case DW_TAG_union_type:
2119 2116 ctf_dprintf("top level type\n");
2120 2117 ret = ctf_dwarf_convert_type(cup, die, NULL, B_TRUE);
2121 2118 break;
2122 2119 default:
2123 2120 break;
2124 2121 }
2125 2122
2126 2123 return (ret);
2127 2124 }
2128 2125
2129 2126
2130 2127 /*
2131 2128 * We're given a node. At this node we need to convert it and then proceed to
2132 2129 * convert any siblings that are associaed with this die.
2133 2130 */
2134 2131 static int
2135 2132 ctf_dwarf_convert_die(ctf_cu_t *cup, Dwarf_Die die)
2136 2133 {
2137 2134 while (die != NULL) {
2138 2135 int ret;
2139 2136 Dwarf_Die sib;
2140 2137
2141 2138 if ((ret = ctf_dwarf_walk_toplevel(cup, die)) != 0)
2142 2139 return (ret);
2143 2140
2144 2141 if ((ret = ctf_dwarf_sib(cup, die, &sib)) != 0)
2145 2142 return (ret);
2146 2143 die = sib;
2147 2144 }
2148 2145 return (0);
2149 2146 }
2150 2147
2151 2148 static int
2152 2149 ctf_dwarf_fixup_die(ctf_cu_t *cup, boolean_t addpass)
2153 2150 {
2154 2151 ctf_dwmap_t *map;
2155 2152
2156 2153 for (map = avl_first(&cup->cu_map); map != NULL;
2157 2154 map = AVL_NEXT(&cup->cu_map, map)) {
2158 2155 int ret;
↓ open down ↓ |
1861 lines elided |
↑ open up ↑ |
2159 2156 if (map->cdm_fix == B_FALSE)
2160 2157 continue;
2161 2158 if ((ret = ctf_dwarf_fixup_sou(cup, map->cdm_die, map->cdm_id,
2162 2159 addpass)) != 0)
2163 2160 return (ret);
2164 2161 }
2165 2162
2166 2163 return (0);
2167 2164 }
2168 2165
2166 +/*
2167 + * The DWARF information about a symbol and the information in the symbol table
2168 + * may not be the same due to symbol reduction that is performed by ld due to a
2169 + * mapfile or other such directive. We process weak symbols at a later time.
2170 + *
2171 + * The following are the rules that we employ:
2172 + *
2173 + * 1. A DWARF function that is considered exported matches STB_GLOBAL entries
2174 + * with the same name.
2175 + *
2176 + * 2. A DWARF function that is considered exported matches STB_LOCAL entries
2177 + * with the same name and the same file. This case may happen due to mapfile
2178 + * reduction.
2179 + *
2180 + * 3. A DWARF function that is not considered exported matches STB_LOCAL entries
2181 + * with the same name and the same file.
2182 + *
2183 + * 4. A DWARF function that has the same name as the symbol table entry, but the
2184 + * files do not match. This is considered a 'fuzzy' match. This may also happen
2185 + * due to a mapfile reduction. Fuzzy matching is only used when we know that the
2186 + * file in question refers to the primary object. This is because when a symbol
2187 + * is reduced in a mapfile, it's always going to be tagged as a local value in
2188 + * the generated output and it is considered as to belong to the primary file
2189 + * which is the first STT_FILE symbol we see.
2190 + */
2191 +static boolean_t
2192 +ctf_dwarf_symbol_match(const char *symtab_file, const char *symtab_name,
2193 + uint_t symtab_bind, const char *dwarf_file, const char *dwarf_name,
2194 + boolean_t dwarf_global, boolean_t *is_fuzzy)
2195 +{
2196 + *is_fuzzy = B_FALSE;
2197 +
2198 + if (symtab_bind != STB_LOCAL && symtab_bind != STB_GLOBAL) {
2199 + return (B_FALSE);
2200 + }
2201 +
2202 + if (strcmp(symtab_name, dwarf_name) != 0) {
2203 + return (B_FALSE);
2204 + }
2205 +
2206 + if (symtab_bind == STB_GLOBAL) {
2207 + return (dwarf_global);
2208 + }
2209 +
2210 + if (strcmp(symtab_file, dwarf_file) == 0) {
2211 + return (B_TRUE);
2212 + }
2213 +
2214 + if (dwarf_global) {
2215 + *is_fuzzy = B_TRUE;
2216 + return (B_TRUE);
2217 + }
2218 +
2219 + return (B_FALSE);
2220 +}
2221 +
2169 2222 static ctf_dwfunc_t *
2170 2223 ctf_dwarf_match_func(ctf_cu_t *cup, const char *file, const char *name,
2171 - int bind)
2224 + uint_t bind, boolean_t primary)
2172 2225 {
2173 - ctf_dwfunc_t *cdf;
2226 + ctf_dwfunc_t *cdf, *fuzzy = NULL;
2174 2227
2175 2228 if (bind == STB_WEAK)
2176 2229 return (NULL);
2177 2230
2178 - /* Nothing we can do if we can't find a name to compare it to. */
2179 2231 if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL))
2180 2232 return (NULL);
2181 2233
2182 2234 for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL;
2183 2235 cdf = ctf_list_next(cdf)) {
2184 - if (bind == STB_GLOBAL && cdf->cdf_global == B_FALSE)
2185 - continue;
2186 - if (bind == STB_LOCAL && cdf->cdf_global == B_TRUE)
2187 - continue;
2188 - if (strcmp(name, cdf->cdf_name) != 0)
2189 - continue;
2190 - if (bind == STB_LOCAL && strcmp(file, cup->cu_name) != 0)
2191 - continue;
2192 - return (cdf);
2236 + boolean_t is_fuzzy = B_FALSE;
2237 +
2238 + if (ctf_dwarf_symbol_match(file, name, bind, cup->cu_name,
2239 + cdf->cdf_name, cdf->cdf_global, &is_fuzzy)) {
2240 + if (is_fuzzy) {
2241 + if (primary) {
2242 + fuzzy = cdf;
2243 + }
2244 + continue;
2245 + } else {
2246 + return (cdf);
2247 + }
2248 + }
2193 2249 }
2194 2250
2195 - return (NULL);
2251 + return (fuzzy);
2196 2252 }
2253 +
2197 2254 static ctf_dwvar_t *
2198 2255 ctf_dwarf_match_var(ctf_cu_t *cup, const char *file, const char *name,
2199 - int bind)
2256 + uint_t bind, boolean_t primary)
2200 2257 {
2201 - ctf_dwvar_t *cdv;
2258 + ctf_dwvar_t *cdv, *fuzzy = NULL;
2202 2259
2203 - /* Nothing we can do if we can't find a name to compare it to. */
2260 + if (bind == STB_WEAK)
2261 + return (NULL);
2262 +
2204 2263 if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL))
2205 2264 return (NULL);
2206 - ctf_dprintf("Still considering %s\n", name);
2207 2265
2208 2266 for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL;
2209 2267 cdv = ctf_list_next(cdv)) {
2210 - if (bind == STB_GLOBAL && cdv->cdv_global == B_FALSE)
2211 - continue;
2212 - if (bind == STB_LOCAL && cdv->cdv_global == B_TRUE)
2213 - continue;
2214 - if (strcmp(name, cdv->cdv_name) != 0)
2215 - continue;
2216 - if (bind == STB_LOCAL && strcmp(file, cup->cu_name) != 0)
2217 - continue;
2218 - return (cdv);
2219 - }
2268 + boolean_t is_fuzzy = B_FALSE;
2220 2269
2221 - return (NULL);
2222 -}
2223 -
2224 -static int
2225 -ctf_dwarf_symtab_iter(ctf_cu_t *cup, ctf_dwarf_symtab_f *func, void *arg)
2226 -{
2227 - int ret;
2228 - ulong_t i;
2229 - ctf_file_t *fp = cup->cu_ctfp;
2230 - const char *file = NULL;
2231 - uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2232 - uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data;
2233 -
2234 - for (i = 0; i < fp->ctf_nsyms; i++) {
2235 - const char *name;
2236 - int type;
2237 - GElf_Sym gsym;
2238 - const GElf_Sym *gsymp;
2239 -
2240 - if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2241 - const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
2242 - type = ELF32_ST_TYPE(symp->st_info);
2243 - if (type == STT_FILE) {
2244 - file = (char *)(strbase + symp->st_name);
2245 - continue;
2270 + if (ctf_dwarf_symbol_match(file, name, bind, cup->cu_name,
2271 + cdv->cdv_name, cdv->cdv_global, &is_fuzzy)) {
2272 + if (is_fuzzy) {
2273 + if (primary) {
2274 + fuzzy = cdv;
2275 + }
2276 + } else {
2277 + return (cdv);
2246 2278 }
2247 - if (type != STT_OBJECT && type != STT_FUNC)
2248 - continue;
2249 - if (ctf_sym_valid(strbase, type, symp->st_shndx,
2250 - symp->st_value, symp->st_name) == B_FALSE)
2251 - continue;
2252 - name = (char *)(strbase + symp->st_name);
2253 - gsym.st_name = symp->st_name;
2254 - gsym.st_value = symp->st_value;
2255 - gsym.st_size = symp->st_size;
2256 - gsym.st_info = symp->st_info;
2257 - gsym.st_other = symp->st_other;
2258 - gsym.st_shndx = symp->st_shndx;
2259 - gsymp = &gsym;
2260 - } else {
2261 - const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
2262 - type = ELF64_ST_TYPE(symp->st_info);
2263 - if (type == STT_FILE) {
2264 - file = (char *)(strbase + symp->st_name);
2265 - continue;
2266 - }
2267 - if (type != STT_OBJECT && type != STT_FUNC)
2268 - continue;
2269 - if (ctf_sym_valid(strbase, type, symp->st_shndx,
2270 - symp->st_value, symp->st_name) == B_FALSE)
2271 - continue;
2272 - name = (char *)(strbase + symp->st_name);
2273 - gsymp = symp;
2274 2279 }
2275 -
2276 - ret = func(cup, gsymp, i, file, name, arg);
2277 - if (ret != 0)
2278 - return (ret);
2279 2280 }
2280 2281
2281 - return (0);
2282 + return (fuzzy);
2282 2283 }
2283 2284
2284 2285 static int
2285 -ctf_dwarf_conv_funcvars_cb(ctf_cu_t *cup, const GElf_Sym *symp, ulong_t idx,
2286 - const char *file, const char *name, void *arg)
2286 +ctf_dwarf_conv_funcvars_cb(const Elf64_Sym *symp, ulong_t idx,
2287 + const char *file, const char *name, boolean_t primary, void *arg)
2287 2288 {
2288 - int ret, bind, type;
2289 + int ret;
2290 + uint_t bind, type;
2291 + ctf_cu_t *cup = arg;
2289 2292
2290 2293 bind = GELF_ST_BIND(symp->st_info);
2291 2294 type = GELF_ST_TYPE(symp->st_info);
2292 2295
2293 2296 /*
2294 2297 * Come back to weak symbols in another pass
2295 2298 */
2296 2299 if (bind == STB_WEAK)
2297 2300 return (0);
2298 2301
2299 2302 if (type == STT_OBJECT) {
2300 2303 ctf_dwvar_t *cdv = ctf_dwarf_match_var(cup, file, name,
2301 - bind);
2302 - ctf_dprintf("match for %s (%d): %p\n", name, idx, cdv);
2304 + bind, primary);
2303 2305 if (cdv == NULL)
2304 2306 return (0);
2305 2307 ret = ctf_add_object(cup->cu_ctfp, idx, cdv->cdv_type);
2306 - ctf_dprintf("added object %s\n", name);
2308 + ctf_dprintf("added object %s->%ld\n", name, cdv->cdv_type);
2307 2309 } else {
2308 2310 ctf_dwfunc_t *cdf = ctf_dwarf_match_func(cup, file, name,
2309 - bind);
2311 + bind, primary);
2310 2312 if (cdf == NULL)
2311 2313 return (0);
2312 2314 ret = ctf_add_function(cup->cu_ctfp, idx, &cdf->cdf_fip,
2313 2315 cdf->cdf_argv);
2316 + ctf_dprintf("added function %s\n", name);
2314 2317 }
2315 2318
2316 2319 if (ret == CTF_ERR) {
2317 2320 return (ctf_errno(cup->cu_ctfp));
2318 2321 }
2319 2322
2320 2323 return (0);
2321 2324 }
2322 2325
2323 2326 static int
2324 2327 ctf_dwarf_conv_funcvars(ctf_cu_t *cup)
2325 2328 {
2326 - return (ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_funcvars_cb, NULL));
2329 + return (ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_funcvars_cb, cup));
2327 2330 }
2328 2331
2329 2332 /*
2330 2333 * If we have a weak symbol, attempt to find the strong symbol it will resolve
2331 2334 * to. Note: the code where this actually happens is in sym_process() in
2332 2335 * cmd/sgs/libld/common/syms.c
2333 2336 *
2334 2337 * Finding the matching symbol is unfortunately not trivial. For a symbol to be
2335 2338 * a candidate, it must:
2336 2339 *
2337 2340 * - have the same type (function, object)
2338 2341 * - have the same value (address)
2339 2342 * - have the same size
2340 2343 * - not be another weak symbol
2341 2344 * - belong to the same section (checked via section index)
2342 2345 *
2343 2346 * To perform this check, we first iterate over the symbol table. For each weak
2344 2347 * symbol that we encounter, we then do a second walk over the symbol table,
2345 2348 * calling ctf_dwarf_conv_check_weak(). If a symbol matches the above, then it's
2346 2349 * either a local or global symbol. If we find a global symbol then we go with
2347 2350 * it and stop searching for additional matches.
2348 2351 *
2349 2352 * If instead, we find a local symbol, things are more complicated. The first
2350 2353 * thing we do is to try and see if we have file information about both symbols
2351 2354 * (STT_FILE). If they both have file information and it matches, then we treat
2352 2355 * that as a good match and stop searching for additional matches.
2353 2356 *
2354 2357 * Otherwise, this means we have a non-matching file and a local symbol. We
2355 2358 * treat this as a candidate and if we find a better match (one of the two cases
2356 2359 * above), use that instead. There are two different ways this can happen.
2357 2360 * Either this is a completely different symbol, or it's a once-global symbol
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
2358 2361 * that was scoped to local via a mapfile. In the former case, curfile is
2359 2362 * likely inaccurate since the linker does not preserve the needed curfile in
2360 2363 * the order of the symbol table (see the comments about locally scoped symbols
2361 2364 * in libld's update_osym()). As we can't tell this case from the former one,
2362 2365 * we use this symbol iff no other matching symbol is found.
2363 2366 *
2364 2367 * What we really need here is a SUNW section containing weak<->strong mappings
2365 2368 * that we can consume.
2366 2369 */
2367 2370 typedef struct ctf_dwarf_weak_arg {
2368 - const GElf_Sym *cweak_symp;
2371 + const Elf64_Sym *cweak_symp;
2369 2372 const char *cweak_file;
2370 2373 boolean_t cweak_candidate;
2371 2374 ulong_t cweak_idx;
2372 2375 } ctf_dwarf_weak_arg_t;
2373 2376
2374 2377 static int
2375 -ctf_dwarf_conv_check_weak(ctf_cu_t *cup, const GElf_Sym *symp,
2376 - ulong_t idx, const char *file, const char *name, void *arg)
2378 +ctf_dwarf_conv_check_weak(const Elf64_Sym *symp, ulong_t idx, const char *file,
2379 + const char *name, boolean_t primary, void *arg)
2377 2380 {
2378 2381 ctf_dwarf_weak_arg_t *cweak = arg;
2379 - const GElf_Sym *wsymp = cweak->cweak_symp;
2380 2382
2383 + const Elf64_Sym *wsymp = cweak->cweak_symp;
2384 +
2381 2385 ctf_dprintf("comparing weak to %s\n", name);
2382 2386
2383 2387 if (GELF_ST_BIND(symp->st_info) == STB_WEAK) {
2384 2388 return (0);
2385 2389 }
2386 2390
2387 2391 if (GELF_ST_TYPE(wsymp->st_info) != GELF_ST_TYPE(symp->st_info)) {
2388 2392 return (0);
2389 2393 }
2390 2394
2391 2395 if (wsymp->st_value != symp->st_value) {
2392 2396 return (0);
2393 2397 }
2394 2398
2395 2399 if (wsymp->st_size != symp->st_size) {
2396 2400 return (0);
2397 2401 }
2398 2402
2399 2403 if (wsymp->st_shndx != symp->st_shndx) {
2400 2404 return (0);
2401 2405 }
2402 2406
2403 2407 /*
2404 2408 * Check if it's a weak candidate.
2405 2409 */
2406 2410 if (GELF_ST_BIND(symp->st_info) == STB_LOCAL &&
2407 2411 (file == NULL || cweak->cweak_file == NULL ||
2408 2412 strcmp(file, cweak->cweak_file) != 0)) {
2409 2413 cweak->cweak_candidate = B_TRUE;
2410 2414 cweak->cweak_idx = idx;
2411 2415 return (0);
2412 2416 }
2413 2417
2414 2418 /*
2415 2419 * Found a match, break.
2416 2420 */
2417 2421 cweak->cweak_idx = idx;
2418 2422 return (1);
2419 2423 }
2420 2424
2421 2425 static int
2422 2426 ctf_dwarf_duplicate_sym(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx)
2423 2427 {
2424 2428 ctf_id_t id = ctf_lookup_by_symbol(cup->cu_ctfp, matchidx);
2425 2429
2426 2430 /*
2427 2431 * If we matched something that for some reason didn't have type data,
2428 2432 * we don't consider that a fatal error and silently swallow it.
2429 2433 */
2430 2434 if (id == CTF_ERR) {
2431 2435 if (ctf_errno(cup->cu_ctfp) == ECTF_NOTYPEDAT)
2432 2436 return (0);
2433 2437 else
2434 2438 return (ctf_errno(cup->cu_ctfp));
2435 2439 }
2436 2440
2437 2441 if (ctf_add_object(cup->cu_ctfp, idx, id) == CTF_ERR)
2438 2442 return (ctf_errno(cup->cu_ctfp));
2439 2443
2440 2444 return (0);
2441 2445 }
2442 2446
2443 2447 static int
2444 2448 ctf_dwarf_duplicate_func(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx)
2445 2449 {
2446 2450 int ret;
2447 2451 ctf_funcinfo_t fip;
2448 2452 ctf_id_t *args = NULL;
2449 2453
2450 2454 if (ctf_func_info(cup->cu_ctfp, matchidx, &fip) == CTF_ERR) {
2451 2455 if (ctf_errno(cup->cu_ctfp) == ECTF_NOFUNCDAT)
2452 2456 return (0);
2453 2457 else
2454 2458 return (ctf_errno(cup->cu_ctfp));
2455 2459 }
2456 2460
2457 2461 if (fip.ctc_argc != 0) {
2458 2462 args = ctf_alloc(sizeof (ctf_id_t) * fip.ctc_argc);
2459 2463 if (args == NULL)
2460 2464 return (ENOMEM);
2461 2465
2462 2466 if (ctf_func_args(cup->cu_ctfp, matchidx, fip.ctc_argc, args) ==
2463 2467 CTF_ERR) {
2464 2468 ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc);
2465 2469 return (ctf_errno(cup->cu_ctfp));
2466 2470 }
2467 2471 }
2468 2472
↓ open down ↓ |
78 lines elided |
↑ open up ↑ |
2469 2473 ret = ctf_add_function(cup->cu_ctfp, idx, &fip, args);
2470 2474 if (args != NULL)
2471 2475 ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc);
2472 2476 if (ret == CTF_ERR)
2473 2477 return (ctf_errno(cup->cu_ctfp));
2474 2478
2475 2479 return (0);
2476 2480 }
2477 2481
2478 2482 static int
2479 -ctf_dwarf_conv_weaks_cb(ctf_cu_t *cup, const GElf_Sym *symp,
2480 - ulong_t idx, const char *file, const char *name, void *arg)
2483 +ctf_dwarf_conv_weaks_cb(const Elf64_Sym *symp, ulong_t idx, const char *file,
2484 + const char *name, boolean_t primary, void *arg)
2481 2485 {
2482 2486 int ret, type;
2483 2487 ctf_dwarf_weak_arg_t cweak;
2488 + ctf_cu_t *cup = arg;
2484 2489
2485 2490 /*
2486 2491 * We only care about weak symbols.
2487 2492 */
2488 2493 if (GELF_ST_BIND(symp->st_info) != STB_WEAK)
2489 2494 return (0);
2490 2495
2491 2496 type = GELF_ST_TYPE(symp->st_info);
2492 2497 ASSERT(type == STT_OBJECT || type == STT_FUNC);
2493 2498
2494 2499 /*
2495 2500 * For each weak symbol we encounter, we need to do a second iteration
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
2496 2501 * to try and find a match. We should probably think about other
2497 2502 * techniques to try and save us time in the future.
2498 2503 */
2499 2504 cweak.cweak_symp = symp;
2500 2505 cweak.cweak_file = file;
2501 2506 cweak.cweak_candidate = B_FALSE;
2502 2507 cweak.cweak_idx = 0;
2503 2508
2504 2509 ctf_dprintf("Trying to find weak equiv for %s\n", name);
2505 2510
2506 - ret = ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_check_weak, &cweak);
2511 + ret = ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_check_weak, &cweak);
2507 2512 VERIFY(ret == 0 || ret == 1);
2508 2513
2509 2514 /*
2510 2515 * Nothing was ever found, we're not going to add anything for this
2511 2516 * entry.
2512 2517 */
2513 2518 if (ret == 0 && cweak.cweak_candidate == B_FALSE) {
2514 2519 ctf_dprintf("found no weak match for %s\n", name);
2515 2520 return (0);
2516 2521 }
2517 2522
2518 2523 /*
2519 2524 * Now, finally go and add the type based on the match.
2520 2525 */
2526 + ctf_dprintf("matched weak symbol %lu to %lu\n", idx, cweak.cweak_idx);
2521 2527 if (type == STT_OBJECT) {
2522 2528 ret = ctf_dwarf_duplicate_sym(cup, idx, cweak.cweak_idx);
2523 2529 } else {
2524 2530 ret = ctf_dwarf_duplicate_func(cup, idx, cweak.cweak_idx);
2525 2531 }
2526 2532
2527 2533 return (ret);
2528 2534 }
2529 2535
2530 2536 static int
2531 2537 ctf_dwarf_conv_weaks(ctf_cu_t *cup)
2532 2538 {
2533 - return (ctf_dwarf_symtab_iter(cup, ctf_dwarf_conv_weaks_cb, NULL));
2539 + return (ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_weaks_cb, cup));
2534 2540 }
2535 2541
2536 2542 /* ARGSUSED */
2537 2543 static int
2538 2544 ctf_dwarf_convert_one(void *arg, void *unused)
2539 2545 {
2540 2546 int ret;
2541 2547 ctf_file_t *dedup;
2542 2548 ctf_cu_t *cup = arg;
2543 2549
2544 2550 ctf_dprintf("converting die: %s\n", cup->cu_name);
2545 2551 ctf_dprintf("max offset: %x\n", cup->cu_maxoff);
2546 2552 VERIFY(cup != NULL);
2547 2553
2548 2554 ret = ctf_dwarf_convert_die(cup, cup->cu_cu);
2549 2555 ctf_dprintf("ctf_dwarf_convert_die (%s) returned %d\n", cup->cu_name,
2550 2556 ret);
2551 2557 if (ret != 0) {
2552 2558 return (ret);
2553 2559 }
2554 2560 if (ctf_update(cup->cu_ctfp) != 0) {
2555 2561 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2556 2562 "failed to update output ctf container"));
2557 2563 }
2558 2564
2559 2565 ret = ctf_dwarf_fixup_die(cup, B_FALSE);
2560 2566 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name,
2561 2567 ret);
2562 2568 if (ret != 0) {
2563 2569 return (ret);
2564 2570 }
2565 2571 if (ctf_update(cup->cu_ctfp) != 0) {
2566 2572 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2567 2573 "failed to update output ctf container"));
2568 2574 }
2569 2575
2570 2576 ret = ctf_dwarf_fixup_die(cup, B_TRUE);
2571 2577 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name,
2572 2578 ret);
2573 2579 if (ret != 0) {
2574 2580 return (ret);
2575 2581 }
2576 2582 if (ctf_update(cup->cu_ctfp) != 0) {
2577 2583 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2578 2584 "failed to update output ctf container"));
2579 2585 }
2580 2586
2581 2587
2582 2588 if ((ret = ctf_dwarf_conv_funcvars(cup)) != 0) {
2583 2589 return (ctf_dwarf_error(cup, NULL, ret,
2584 2590 "failed to convert strong functions and variables"));
2585 2591 }
2586 2592
2587 2593 if (ctf_update(cup->cu_ctfp) != 0) {
2588 2594 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2589 2595 "failed to update output ctf container"));
2590 2596 }
2591 2597
2592 2598 if (cup->cu_doweaks == B_TRUE) {
2593 2599 if ((ret = ctf_dwarf_conv_weaks(cup)) != 0) {
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
2594 2600 return (ctf_dwarf_error(cup, NULL, ret,
2595 2601 "failed to convert weak functions and variables"));
2596 2602 }
2597 2603
2598 2604 if (ctf_update(cup->cu_ctfp) != 0) {
2599 2605 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0,
2600 2606 "failed to update output ctf container"));
2601 2607 }
2602 2608 }
2603 2609
2604 - ctf_phase_dump(cup->cu_ctfp, "pre-dedup");
2610 + ctf_phase_dump(cup->cu_ctfp, "pre-dwarf-dedup", cup->cu_name);
2605 2611 ctf_dprintf("adding inputs for dedup\n");
2606 2612 if ((ret = ctf_merge_add(cup->cu_cmh, cup->cu_ctfp)) != 0) {
2607 2613 return (ctf_dwarf_error(cup, NULL, ret,
2608 2614 "failed to add inputs for merge"));
2609 2615 }
2610 2616
2611 - ctf_dprintf("starting merge\n");
2617 + ctf_dprintf("starting dedup of %s\n", cup->cu_name);
2612 2618 if ((ret = ctf_merge_dedup(cup->cu_cmh, &dedup)) != 0) {
2613 2619 return (ctf_dwarf_error(cup, NULL, ret,
2614 2620 "failed to deduplicate die"));
2615 2621 }
2616 2622 ctf_close(cup->cu_ctfp);
2617 2623 cup->cu_ctfp = dedup;
2624 + ctf_phase_dump(cup->cu_ctfp, "post-dwarf-dedup", cup->cu_name);
2618 2625
2619 2626 return (0);
2620 2627 }
2621 2628
2622 2629 /*
2623 2630 * Note, we expect that if we're returning a ctf_file_t from one of the dies,
2624 2631 * say in the single node case, it's been saved and the entry here has been set
2625 2632 * to NULL, which ctf_close happily ignores.
2626 2633 */
2627 2634 static void
2628 2635 ctf_dwarf_free_die(ctf_cu_t *cup)
2629 2636 {
2630 2637 ctf_dwfunc_t *cdf, *ndf;
2631 2638 ctf_dwvar_t *cdv, *ndv;
2632 2639 ctf_dwbitf_t *cdb, *ndb;
2633 2640 ctf_dwmap_t *map;
2634 2641 void *cookie;
2635 2642 Dwarf_Error derr;
2636 2643
2637 2644 ctf_dprintf("Beginning to free die: %p\n", cup);
2638 2645 cup->cu_elf = NULL;
2639 2646 ctf_dprintf("Trying to free name: %p\n", cup->cu_name);
2640 2647 if (cup->cu_name != NULL)
2641 2648 ctf_free(cup->cu_name, strlen(cup->cu_name) + 1);
2642 2649 ctf_dprintf("Trying to free merge handle: %p\n", cup->cu_cmh);
2643 2650 if (cup->cu_cmh != NULL) {
2644 2651 ctf_merge_fini(cup->cu_cmh);
2645 2652 cup->cu_cmh = NULL;
2646 2653 }
2647 2654
2648 2655 ctf_dprintf("Trying to free functions\n");
2649 2656 for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL; cdf = ndf) {
2650 2657 ndf = ctf_list_next(cdf);
2651 2658 ctf_free(cdf->cdf_name, strlen(cdf->cdf_name) + 1);
2652 2659 if (cdf->cdf_fip.ctc_argc != 0) {
2653 2660 ctf_free(cdf->cdf_argv,
2654 2661 sizeof (ctf_id_t) * cdf->cdf_fip.ctc_argc);
2655 2662 }
2656 2663 ctf_free(cdf, sizeof (ctf_dwfunc_t));
2657 2664 }
2658 2665
2659 2666 ctf_dprintf("Trying to free variables\n");
2660 2667 for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL; cdv = ndv) {
2661 2668 ndv = ctf_list_next(cdv);
2662 2669 ctf_free(cdv->cdv_name, strlen(cdv->cdv_name) + 1);
2663 2670 ctf_free(cdv, sizeof (ctf_dwvar_t));
2664 2671 }
2665 2672
2666 2673 ctf_dprintf("Trying to free bitfields\n");
2667 2674 for (cdb = ctf_list_next(&cup->cu_bitfields); cdb != NULL; cdb = ndb) {
2668 2675 ndb = ctf_list_next(cdb);
2669 2676 ctf_free(cdb, sizeof (ctf_dwbitf_t));
2670 2677 }
2671 2678
2672 2679 ctf_dprintf("Trying to clean up dwarf_t: %p\n", cup->cu_dwarf);
2673 2680 (void) dwarf_finish(cup->cu_dwarf, &derr);
2674 2681 cup->cu_dwarf = NULL;
2675 2682 ctf_close(cup->cu_ctfp);
2676 2683
2677 2684 cookie = NULL;
2678 2685 while ((map = avl_destroy_nodes(&cup->cu_map, &cookie)) != NULL) {
2679 2686 ctf_free(map, sizeof (ctf_dwmap_t));
2680 2687 }
2681 2688 avl_destroy(&cup->cu_map);
2682 2689 cup->cu_errbuf = NULL;
2683 2690 }
2684 2691
2685 2692 static void
2686 2693 ctf_dwarf_free_dies(ctf_cu_t *cdies, int ndies)
2687 2694 {
2688 2695 int i;
2689 2696
2690 2697 ctf_dprintf("Beginning to free dies\n");
2691 2698 for (i = 0; i < ndies; i++) {
2692 2699 ctf_dwarf_free_die(&cdies[i]);
2693 2700 }
2694 2701
2695 2702 ctf_free(cdies, sizeof (ctf_cu_t) * ndies);
2696 2703 }
2697 2704
2698 2705 static int
2699 2706 ctf_dwarf_count_dies(Dwarf_Debug dw, Dwarf_Error *derr, int *ndies,
2700 2707 char *errbuf, size_t errlen)
2701 2708 {
2702 2709 int ret;
2703 2710 Dwarf_Half vers;
2704 2711 Dwarf_Unsigned nexthdr;
2705 2712
2706 2713 while ((ret = dwarf_next_cu_header(dw, NULL, &vers, NULL, NULL,
2707 2714 &nexthdr, derr)) != DW_DLV_NO_ENTRY) {
2708 2715 if (ret != DW_DLV_OK) {
2709 2716 (void) snprintf(errbuf, errlen,
2710 2717 "file does not contain valid DWARF data: %s\n",
2711 2718 dwarf_errmsg(*derr));
2712 2719 return (ECTF_CONVBKERR);
2713 2720 }
2714 2721
2715 2722 if (vers != DWARF_VERSION_TWO) {
2716 2723 (void) snprintf(errbuf, errlen,
2717 2724 "unsupported DWARF version: %d\n", vers);
2718 2725 return (ECTF_CONVBKERR);
2719 2726 }
2720 2727 *ndies = *ndies + 1;
2721 2728 }
2722 2729
2723 2730 if (*ndies == 0) {
2724 2731 (void) snprintf(errbuf, errlen,
2725 2732 "file does not contain valid DWARF data: %s\n",
2726 2733 dwarf_errmsg(*derr));
2727 2734 return (ECTF_CONVBKERR);
2728 2735 }
2729 2736
2730 2737 return (0);
2731 2738 }
2732 2739
2733 2740 static int
2734 2741 ctf_dwarf_init_die(int fd, Elf *elf, ctf_cu_t *cup, int ndie, char *errbuf,
2735 2742 size_t errlen)
2736 2743 {
2737 2744 int ret;
2738 2745 Dwarf_Unsigned hdrlen, abboff, nexthdr;
2739 2746 Dwarf_Half addrsz;
2740 2747 Dwarf_Unsigned offset = 0;
2741 2748 Dwarf_Error derr;
2742 2749
2743 2750 while ((ret = dwarf_next_cu_header(cup->cu_dwarf, &hdrlen, NULL,
2744 2751 &abboff, &addrsz, &nexthdr, &derr)) != DW_DLV_NO_ENTRY) {
2745 2752 char *name;
2746 2753 Dwarf_Die cu, child;
2747 2754
2748 2755 /* Based on the counting above, we should be good to go */
2749 2756 VERIFY(ret == DW_DLV_OK);
2750 2757 if (ndie > 0) {
2751 2758 ndie--;
2752 2759 offset = nexthdr;
2753 2760 continue;
2754 2761 }
2755 2762
2756 2763 /*
2757 2764 * Compilers are apparently inconsistent. Some emit no DWARF for
2758 2765 * empty files and others emit empty compilation unit.
2759 2766 */
2760 2767 cup->cu_voidtid = CTF_ERR;
2761 2768 cup->cu_longtid = CTF_ERR;
2762 2769 cup->cu_elf = elf;
2763 2770 cup->cu_maxoff = nexthdr - 1;
2764 2771 cup->cu_ctfp = ctf_fdcreate(fd, &ret);
2765 2772 if (cup->cu_ctfp == NULL) {
2766 2773 ctf_free(cup, sizeof (ctf_cu_t));
2767 2774 return (ret);
2768 2775 }
2769 2776 avl_create(&cup->cu_map, ctf_dwmap_comp, sizeof (ctf_dwmap_t),
2770 2777 offsetof(ctf_dwmap_t, cdm_avl));
2771 2778 cup->cu_errbuf = errbuf;
2772 2779 cup->cu_errlen = errlen;
2773 2780 bzero(&cup->cu_vars, sizeof (ctf_list_t));
2774 2781 bzero(&cup->cu_funcs, sizeof (ctf_list_t));
2775 2782 bzero(&cup->cu_bitfields, sizeof (ctf_list_t));
2776 2783
2777 2784 if ((ret = ctf_dwarf_die_elfenc(elf, cup, errbuf,
2778 2785 errlen)) != 0) {
2779 2786 avl_destroy(&cup->cu_map);
2780 2787 ctf_free(cup, sizeof (ctf_cu_t));
2781 2788 return (ret);
2782 2789 }
2783 2790
2784 2791 if ((ret = ctf_dwarf_sib(cup, NULL, &cu)) != 0) {
2785 2792 avl_destroy(&cup->cu_map);
2786 2793 ctf_free(cup, sizeof (ctf_cu_t));
2787 2794 return (ret);
2788 2795 }
2789 2796 if (cu == NULL) {
2790 2797 (void) snprintf(errbuf, errlen,
2791 2798 "file does not contain DWARF data\n");
2792 2799 avl_destroy(&cup->cu_map);
2793 2800 ctf_free(cup, sizeof (ctf_cu_t));
2794 2801 return (ECTF_CONVBKERR);
2795 2802 }
2796 2803
2797 2804 if ((ret = ctf_dwarf_child(cup, cu, &child)) != 0) {
2798 2805 avl_destroy(&cup->cu_map);
2799 2806 ctf_free(cup, sizeof (ctf_cu_t));
2800 2807 return (ret);
2801 2808 }
2802 2809 if (child == NULL) {
2803 2810 (void) snprintf(errbuf, errlen,
2804 2811 "file does not contain DWARF data\n");
2805 2812 avl_destroy(&cup->cu_map);
2806 2813 ctf_free(cup, sizeof (ctf_cu_t));
2807 2814 return (ECTF_CONVBKERR);
2808 2815 }
2809 2816
2810 2817 cup->cu_cuoff = offset;
2811 2818 cup->cu_cu = child;
2812 2819
2813 2820 if ((cup->cu_cmh = ctf_merge_init(fd, &ret)) == NULL) {
2814 2821 avl_destroy(&cup->cu_map);
2815 2822 ctf_free(cup, sizeof (ctf_cu_t));
2816 2823 return (ret);
2817 2824 }
2818 2825
2819 2826 if (ctf_dwarf_string(cup, cu, DW_AT_name, &name) == 0) {
2820 2827 size_t len = strlen(name) + 1;
2821 2828 char *b = basename(name);
2822 2829 cup->cu_name = strdup(b);
2823 2830 ctf_free(name, len);
2824 2831 }
2825 2832 break;
2826 2833 }
2827 2834
2828 2835 return (0);
2829 2836 }
2830 2837
2831 2838
2832 2839 ctf_conv_status_t
2833 2840 ctf_dwarf_convert(int fd, Elf *elf, uint_t nthrs, int *errp, ctf_file_t **fpp,
2834 2841 char *errmsg, size_t errlen)
2835 2842 {
2836 2843 int err, ret, ndies, i;
2837 2844 Dwarf_Debug dw;
2838 2845 Dwarf_Error derr;
2839 2846 ctf_cu_t *cdies = NULL, *cup;
2840 2847 workq_t *wqp = NULL;
2841 2848
2842 2849 if (errp == NULL)
2843 2850 errp = &err;
2844 2851 *errp = 0;
2845 2852 *fpp = NULL;
2846 2853
2847 2854 ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw, &derr);
2848 2855 if (ret != DW_DLV_OK) {
2849 2856 /*
2850 2857 * We may want to expect DWARF data here and fail conversion if
2851 2858 * it's missing. In this case, if we actually have some amount
2852 2859 * of DWARF, but no section, for now, just go ahead and create
2853 2860 * an empty CTF file.
2854 2861 */
2855 2862 if (ret == DW_DLV_NO_ENTRY ||
2856 2863 dwarf_errno(derr) == DW_DLE_DEBUG_INFO_NULL) {
2857 2864 *fpp = ctf_create(errp);
2858 2865 return (*fpp != NULL ? CTF_CONV_SUCCESS :
2859 2866 CTF_CONV_ERROR);
2860 2867 }
2861 2868 (void) snprintf(errmsg, errlen,
2862 2869 "failed to initialize DWARF: %s\n",
2863 2870 dwarf_errmsg(derr));
2864 2871 *errp = ECTF_CONVBKERR;
2865 2872 return (CTF_CONV_ERROR);
2866 2873 }
2867 2874
2868 2875 /*
2869 2876 * Iterate over all of the compilation units and create a ctf_cu_t for
2870 2877 * each of them. This is used to determine if we have zero, one, or
2871 2878 * multiple dies to convert. If we have zero, that's an error. If
2872 2879 * there's only one die, that's the simple case. No merge needed and
2873 2880 * only a single Dwarf_Debug as well.
2874 2881 */
2875 2882 ndies = 0;
2876 2883 ret = ctf_dwarf_count_dies(dw, &derr, &ndies, errmsg, errlen);
2877 2884 if (ret != 0) {
2878 2885 *errp = ret;
2879 2886 goto out;
2880 2887 }
2881 2888
2882 2889 (void) dwarf_finish(dw, &derr);
2883 2890 cdies = ctf_alloc(sizeof (ctf_cu_t) * ndies);
2884 2891 if (cdies == NULL) {
2885 2892 *errp = ENOMEM;
2886 2893 return (CTF_CONV_ERROR);
2887 2894 }
2888 2895
2889 2896 for (i = 0; i < ndies; i++) {
2890 2897 cup = &cdies[i];
2891 2898 ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL,
2892 2899 &cup->cu_dwarf, &derr);
2893 2900 if (ret != 0) {
2894 2901 ctf_free(cdies, sizeof (ctf_cu_t) * ndies);
2895 2902 (void) snprintf(errmsg, errlen,
2896 2903 "failed to initialize DWARF: %s\n",
↓ open down ↓ |
269 lines elided |
↑ open up ↑ |
2897 2904 dwarf_errmsg(derr));
2898 2905 *errp = ECTF_CONVBKERR;
2899 2906 return (CTF_CONV_ERROR);
2900 2907 }
2901 2908
2902 2909 ret = ctf_dwarf_init_die(fd, elf, &cdies[i], i, errmsg, errlen);
2903 2910 if (ret != 0) {
2904 2911 *errp = ret;
2905 2912 goto out;
2906 2913 }
2914 +
2907 2915 cup->cu_doweaks = ndies > 1 ? B_FALSE : B_TRUE;
2908 2916 }
2909 2917
2910 - ctf_dprintf("found %d DWARF die(s)\n", ndies);
2918 + ctf_dprintf("found %d DWARF CUs\n", ndies);
2911 2919
2912 2920 /*
2913 2921 * If we only have one compilation unit, there's no reason to use
2914 2922 * multiple threads, even if the user requested them. After all, they
2915 2923 * just gave us an upper bound.
2916 2924 */
2917 2925 if (ndies == 1)
2918 2926 nthrs = 1;
2919 2927
2920 2928 if (workq_init(&wqp, nthrs) == -1) {
2921 2929 *errp = errno;
2922 2930 goto out;
2923 2931 }
2924 2932
2925 2933 for (i = 0; i < ndies; i++) {
2926 2934 cup = &cdies[i];
2927 - ctf_dprintf("adding die %s: %p, %x %x\n", cup->cu_name,
2935 + ctf_dprintf("adding cu %s: %p, %x %x\n", cup->cu_name,
2928 2936 cup->cu_cu, cup->cu_cuoff, cup->cu_maxoff);
2929 2937 if (workq_add(wqp, cup) == -1) {
2930 2938 *errp = errno;
2931 2939 goto out;
2932 2940 }
2933 2941 }
2934 2942
2935 2943 ret = workq_work(wqp, ctf_dwarf_convert_one, NULL, errp);
2936 2944 if (ret == WORKQ_ERROR) {
2937 2945 *errp = errno;
2938 2946 goto out;
2939 2947 } else if (ret == WORKQ_UERROR) {
2940 2948 ctf_dprintf("internal convert failed: %s\n",
2941 2949 ctf_errmsg(*errp));
2942 2950 goto out;
2943 2951 }
2944 2952
2945 - ctf_dprintf("Determining next phase: have %d dies\n", ndies);
2953 + ctf_dprintf("Determining next phase: have %d CUs\n", ndies);
2946 2954 if (ndies != 1) {
2947 2955 ctf_merge_t *cmp;
2948 2956
2949 2957 cmp = ctf_merge_init(fd, &ret);
2950 2958 if (cmp == NULL) {
2951 2959 *errp = ret;
2952 2960 goto out;
2953 2961 }
2954 2962
2955 2963 ctf_dprintf("setting threads\n");
2956 2964 if ((ret = ctf_merge_set_nthreads(cmp, nthrs)) != 0) {
2957 2965 ctf_merge_fini(cmp);
2958 2966 *errp = ret;
2959 2967 goto out;
2960 2968 }
2961 2969
2962 - ctf_dprintf("adding dies\n");
2963 2970 for (i = 0; i < ndies; i++) {
2964 2971 cup = &cdies[i];
2972 + ctf_dprintf("adding cu %s (%p)\n", cup->cu_name,
2973 + cup->cu_ctfp);
2965 2974 if ((ret = ctf_merge_add(cmp, cup->cu_ctfp)) != 0) {
2966 2975 ctf_merge_fini(cmp);
2967 2976 *errp = ret;
2968 2977 goto out;
2969 2978 }
2970 2979 }
2971 2980
2972 2981 ctf_dprintf("performing merge\n");
2973 2982 ret = ctf_merge_merge(cmp, fpp);
2974 2983 if (ret != 0) {
2975 2984 ctf_dprintf("failed merge!\n");
2976 2985 *fpp = NULL;
2977 2986 ctf_merge_fini(cmp);
2978 2987 *errp = ret;
2979 2988 goto out;
2980 2989 }
2981 2990 ctf_merge_fini(cmp);
2982 2991 *errp = 0;
2983 2992 ctf_dprintf("successfully converted!\n");
2984 2993 } else {
2985 2994 *errp = 0;
2986 2995 *fpp = cdies->cu_ctfp;
2987 2996 cdies->cu_ctfp = NULL;
2988 2997 ctf_dprintf("successfully converted!\n");
2989 2998 }
2990 2999
2991 3000 out:
2992 3001 workq_fini(wqp);
2993 3002 ctf_dwarf_free_dies(cdies, ndies);
2994 3003 return (*fpp != NULL ? CTF_CONV_SUCCESS : CTF_CONV_ERROR);
2995 3004 }
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX