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