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