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_create_array_range(ctf_cu_t *cup, Dwarf_Die range, ctf_id_t *idp, 1460 ctf_id_t base, int isroot) 1461 { 1462 int ret; 1463 Dwarf_Die sib; 1464 Dwarf_Unsigned val; 1465 Dwarf_Signed sval; 1466 ctf_arinfo_t ar; 1467 1468 ctf_dprintf("creating array range\n"); 1469 1470 if ((ret = ctf_dwarf_sib(cup, range, &sib)) != 0) 1471 return (ret); 1472 if (sib != NULL) { 1473 ctf_id_t id; 1474 if ((ret = ctf_dwarf_create_array_range(cup, sib, &id, 1475 base, CTF_ADD_NONROOT)) != 0) 1476 return (ret); 1477 ar.ctr_contents = id; 1478 } else { 1479 ar.ctr_contents = base; 1480 } 1481 1482 if ((ar.ctr_index = ctf_dwarf_long(cup)) == CTF_ERR) 1483 return (ctf_errno(cup->cu_ctfp)); 1484 1485 /* 1486 * Array bounds can be signed or unsigned, but there are several kinds 1487 * of signless forms (data1, data2, etc) that take their sign from the 1488 * routine that is trying to interpret them. That is, data1 can be 1489 * either signed or unsigned, depending on whether you use the signed or 1490 * unsigned accessor function. GCC will use the signless forms to store 1491 * unsigned values which have their high bit set, so we need to try to 1492 * read them first as unsigned to get positive values. We could also 1493 * try signed first, falling back to unsigned if we got a negative 1494 * value. 1495 */ 1496 if ((ret = ctf_dwarf_unsigned(cup, range, DW_AT_upper_bound, 1497 &val)) == 0) { 1498 ar.ctr_nelems = val + 1; 1499 } else if (ret != ENOENT) { 1500 return (ret); 1501 } else if ((ret = ctf_dwarf_signed(cup, range, DW_AT_upper_bound, 1502 &sval)) == 0) { 1503 ar.ctr_nelems = sval + 1; 1504 } else if (ret != ENOENT) { 1505 return (ret); 1506 } else { 1507 ar.ctr_nelems = 0; 1508 } 1509 1510 if ((*idp = ctf_add_array(cup->cu_ctfp, isroot, &ar)) == CTF_ERR) 1511 return (ctf_errno(cup->cu_ctfp)); 1512 1513 return (0); 1514 } 1515 1516 /* 1517 * Try and create an array type. First, the kind of the array is specified in 1518 * the DW_AT_type entry. Next, the number of entries is stored in a more 1519 * complicated form, we should have a child that has the DW_TAG_subrange type. 1520 */ 1521 static int 1522 ctf_dwarf_create_array(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot) 1523 { 1524 int ret; 1525 Dwarf_Die tdie, rdie; 1526 ctf_id_t tid; 1527 Dwarf_Half rtag; 1528 1529 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) 1530 return (ret); 1531 if ((ret = ctf_dwarf_convert_type(cup, tdie, &tid, 1532 CTF_ADD_NONROOT)) != 0) 1533 return (ret); 1534 1535 if ((ret = ctf_dwarf_child(cup, die, &rdie)) != 0) 1536 return (ret); 1537 if ((ret = ctf_dwarf_tag(cup, rdie, &rtag)) != 0) 1538 return (ret); 1539 if (rtag != DW_TAG_subrange_type) { 1540 (void) snprintf(cup->cu_errbuf, cup->cu_errlen, 1541 "encountered array without DW_TAG_subrange_type child\n"); 1542 return (ECTF_CONVBKERR); 1543 } 1544 1545 /* 1546 * The compiler may opt to describe a multi-dimensional array as one 1547 * giant array or it may opt to instead encode it as a series of 1548 * subranges. If it's the latter, then for each subrange we introduce a 1549 * type. We can always use the base type. 1550 */ 1551 if ((ret = ctf_dwarf_create_array_range(cup, rdie, idp, tid, 1552 isroot)) != 0) 1553 return (ret); 1554 ctf_dprintf("Got back id %d\n", *idp); 1555 return (ctf_dwmap_add(cup, *idp, die, B_FALSE)); 1556 } 1557 1558 /* 1559 * Given "const int const_array3[11]", GCC7 at least will create a DIE tree of 1560 * DW_TAG_const_type:DW_TAG_array_type:DW_Tag_const_type:<member_type>. 1561 * 1562 * Given C's syntax, this renders out as "const const int const_array3[11]". To 1563 * get closer to round-tripping (and make the unit tests work), we'll peek for 1564 * this case, and avoid adding the extraneous qualifier if we see that the 1565 * underlying array referent already has the same qualifier. 1566 * 1567 * This is unfortunately less trivial than it could be: this issue applies to 1568 * qualifier sets like "const volatile", as well as multi-dimensional arrays, so 1569 * we need to descend down those. 1570 * 1571 * Returns CTF_ERR on error, or a boolean value otherwise. 1572 */ 1573 static int 1574 needed_array_qualifier(ctf_cu_t *cup, int kind, ctf_id_t ref_id) 1575 { 1576 const ctf_type_t *t; 1577 ctf_arinfo_t arinfo; 1578 int akind; 1579 1580 if (kind != CTF_K_CONST && kind != CTF_K_VOLATILE && 1581 kind != CTF_K_RESTRICT) 1582 return (1); 1583 1584 if ((t = ctf_dyn_lookup_by_id(cup->cu_ctfp, ref_id)) == NULL) 1585 return (CTF_ERR); 1586 1587 if (LCTF_INFO_KIND(cup->cu_ctfp, t->ctt_info) != CTF_K_ARRAY) 1588 return (1); 1589 1590 if (ctf_dyn_array_info(cup->cu_ctfp, ref_id, &arinfo) != 0) 1591 return (CTF_ERR); 1592 1593 ctf_id_t id = arinfo.ctr_contents; 1594 1595 for (;;) { 1596 if ((t = ctf_dyn_lookup_by_id(cup->cu_ctfp, id)) == NULL) 1597 return (CTF_ERR); 1598 1599 akind = LCTF_INFO_KIND(cup->cu_ctfp, t->ctt_info); 1600 1601 if (akind == kind) 1602 break; 1603 1604 if (akind == CTF_K_ARRAY) { 1605 if (ctf_dyn_array_info(cup->cu_ctfp, 1606 id, &arinfo) != 0) 1607 return (CTF_ERR); 1608 id = arinfo.ctr_contents; 1609 continue; 1610 } 1611 1612 if (akind != CTF_K_CONST && akind != CTF_K_VOLATILE && 1613 akind != CTF_K_RESTRICT) 1614 break; 1615 1616 id = t->ctt_type; 1617 } 1618 1619 if (kind == akind) { 1620 ctf_dprintf("ignoring extraneous %s qualifier for array %d\n", 1621 ctf_kind_name(cup->cu_ctfp, kind), ref_id); 1622 } 1623 1624 return (kind != akind); 1625 } 1626 1627 static int 1628 ctf_dwarf_create_reference(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, 1629 int kind, int isroot) 1630 { 1631 int ret; 1632 ctf_id_t id; 1633 Dwarf_Die tdie; 1634 char *name; 1635 size_t namelen; 1636 1637 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 && 1638 ret != ENOENT) 1639 return (ret); 1640 if (ret == ENOENT) { 1641 name = NULL; 1642 namelen = 0; 1643 } else { 1644 namelen = strlen(name); 1645 } 1646 1647 ctf_dprintf("reference kind %d %s\n", kind, name != NULL ? name : "<>"); 1648 1649 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) { 1650 if (ret != ENOENT) { 1651 ctf_free(name, namelen); 1652 return (ret); 1653 } 1654 if ((id = ctf_dwarf_void(cup)) == CTF_ERR) { 1655 ctf_free(name, namelen); 1656 return (ctf_errno(cup->cu_ctfp)); 1657 } 1658 } else { 1659 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id, 1660 CTF_ADD_NONROOT)) != 0) { 1661 ctf_free(name, namelen); 1662 return (ret); 1663 } 1664 } 1665 1666 if ((ret = needed_array_qualifier(cup, kind, id)) <= 0) { 1667 if (ret != 0) { 1668 ret = (ctf_errno(cup->cu_ctfp)); 1669 } else { 1670 *idp = id; 1671 } 1672 1673 ctf_free(name, namelen); 1674 return (ret); 1675 } 1676 1677 if ((*idp = ctf_add_reftype(cup->cu_ctfp, isroot, name, id, kind)) == 1678 CTF_ERR) { 1679 ctf_free(name, namelen); 1680 return (ctf_errno(cup->cu_ctfp)); 1681 } 1682 1683 ctf_free(name, namelen); 1684 return (ctf_dwmap_add(cup, *idp, die, B_FALSE)); 1685 } 1686 1687 /* 1688 * Get the size of the type of a particular die. Note that this is a simple 1689 * version that doesn't attempt to traverse further than expecting a single 1690 * sized type reference (so no qualifiers etc.). Nor does it attempt to do as 1691 * much as ctf_type_size() - which we cannot use here as that doesn't look up 1692 * dynamic types, and we don't yet want to do a ctf_update(). 1693 */ 1694 static int 1695 ctf_dwarf_get_type_size(ctf_cu_t *cup, Dwarf_Die die, size_t *sizep) 1696 { 1697 const ctf_type_t *t; 1698 Dwarf_Die tdie; 1699 ctf_id_t tid; 1700 int ret; 1701 1702 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) 1703 return (ret); 1704 1705 if ((ret = ctf_dwarf_convert_type(cup, tdie, &tid, 1706 CTF_ADD_NONROOT)) != 0) 1707 return (ret); 1708 1709 if ((t = ctf_dyn_lookup_by_id(cup->cu_ctfp, tid)) == NULL) 1710 return (ENOENT); 1711 1712 *sizep = ctf_get_ctt_size(cup->cu_ctfp, t, NULL, NULL); 1713 return (0); 1714 } 1715 1716 static int 1717 ctf_dwarf_create_enum(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot) 1718 { 1719 size_t size = 0; 1720 Dwarf_Die child; 1721 ctf_id_t id; 1722 char *name; 1723 int ret; 1724 1725 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 && 1726 ret != ENOENT) 1727 return (ret); 1728 if (ret == ENOENT) 1729 name = NULL; 1730 1731 (void) ctf_dwarf_get_type_size(cup, die, &size); 1732 1733 id = ctf_add_enum(cup->cu_ctfp, isroot, name, size); 1734 ctf_dprintf("added enum %s (%d)\n", name, id); 1735 if (name != NULL) 1736 ctf_free(name, strlen(name) + 1); 1737 if (id == CTF_ERR) 1738 return (ctf_errno(cup->cu_ctfp)); 1739 *idp = id; 1740 if ((ret = ctf_dwmap_add(cup, id, die, B_FALSE)) != 0) 1741 return (ret); 1742 1743 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) { 1744 if (ret == ENOENT) 1745 ret = 0; 1746 return (ret); 1747 } 1748 1749 while (child != NULL) { 1750 Dwarf_Half tag; 1751 Dwarf_Signed sval; 1752 Dwarf_Unsigned uval; 1753 Dwarf_Die arg = child; 1754 int eval; 1755 1756 if ((ret = ctf_dwarf_sib(cup, arg, &child)) != 0) 1757 return (ret); 1758 1759 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0) 1760 return (ret); 1761 1762 if (tag != DW_TAG_enumerator) { 1763 if ((ret = ctf_dwarf_convert_type(cup, arg, NULL, 1764 CTF_ADD_NONROOT)) != 0) 1765 return (ret); 1766 continue; 1767 } 1768 1769 /* 1770 * DWARF v4 section 5.7 tells us we'll always have names. 1771 */ 1772 if ((ret = ctf_dwarf_string(cup, arg, DW_AT_name, &name)) != 0) 1773 return (ret); 1774 1775 /* 1776 * We have to be careful here: newer GCCs generate DWARF where 1777 * an unsigned value will happily pass ctf_dwarf_signed(). 1778 * Since negative values will fail ctf_dwarf_unsigned(), we try 1779 * that first to make sure we get the right value. 1780 */ 1781 if ((ret = ctf_dwarf_unsigned(cup, arg, DW_AT_const_value, 1782 &uval)) == 0) { 1783 eval = (int)uval; 1784 } else if ((ret = ctf_dwarf_signed(cup, arg, DW_AT_const_value, 1785 &sval)) == 0) { 1786 eval = sval; 1787 } 1788 1789 if (ret != 0) { 1790 if (ret != ENOENT) 1791 return (ret); 1792 1793 (void) snprintf(cup->cu_errbuf, cup->cu_errlen, 1794 "encountered enumeration without constant value\n"); 1795 return (ECTF_CONVBKERR); 1796 } 1797 1798 ret = ctf_add_enumerator(cup->cu_ctfp, id, name, eval); 1799 if (ret == CTF_ERR) { 1800 (void) snprintf(cup->cu_errbuf, cup->cu_errlen, 1801 "failed to add enumarator %s (%d) to %d\n", 1802 name, eval, id); 1803 ctf_free(name, strlen(name) + 1); 1804 return (ctf_errno(cup->cu_ctfp)); 1805 } 1806 ctf_free(name, strlen(name) + 1); 1807 } 1808 1809 return (0); 1810 } 1811 1812 /* 1813 * For a function pointer, walk over and process all of its children, unless we 1814 * encounter one that's just a declaration. In which case, we error on it. 1815 */ 1816 static int 1817 ctf_dwarf_create_fptr(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, int isroot) 1818 { 1819 int ret; 1820 Dwarf_Bool b; 1821 ctf_funcinfo_t fi; 1822 Dwarf_Die retdie; 1823 ctf_id_t *argv = NULL; 1824 1825 bzero(&fi, sizeof (ctf_funcinfo_t)); 1826 1827 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) != 0) { 1828 if (ret != ENOENT) 1829 return (ret); 1830 } else { 1831 if (b != 0) 1832 return (EPROTOTYPE); 1833 } 1834 1835 /* 1836 * Return type is in DW_AT_type, if none, it returns void. 1837 */ 1838 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &retdie)) != 0) { 1839 if (ret != ENOENT) 1840 return (ret); 1841 if ((fi.ctc_return = ctf_dwarf_void(cup)) == CTF_ERR) 1842 return (ctf_errno(cup->cu_ctfp)); 1843 } else { 1844 if ((ret = ctf_dwarf_convert_type(cup, retdie, &fi.ctc_return, 1845 CTF_ADD_NONROOT)) != 0) 1846 return (ret); 1847 } 1848 1849 if ((ret = ctf_dwarf_function_count(cup, die, &fi, B_TRUE)) != 0) { 1850 return (ret); 1851 } 1852 1853 if (fi.ctc_argc != 0) { 1854 argv = ctf_alloc(sizeof (ctf_id_t) * fi.ctc_argc); 1855 if (argv == NULL) 1856 return (ENOMEM); 1857 1858 if ((ret = ctf_dwarf_convert_fargs(cup, die, &fi, argv)) != 0) { 1859 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc); 1860 return (ret); 1861 } 1862 } 1863 1864 if ((*idp = ctf_add_funcptr(cup->cu_ctfp, isroot, &fi, argv)) == 1865 CTF_ERR) { 1866 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc); 1867 return (ctf_errno(cup->cu_ctfp)); 1868 } 1869 1870 ctf_free(argv, sizeof (ctf_id_t) * fi.ctc_argc); 1871 return (ctf_dwmap_add(cup, *idp, die, B_FALSE)); 1872 } 1873 1874 static int 1875 ctf_dwarf_convert_type(ctf_cu_t *cup, Dwarf_Die die, ctf_id_t *idp, 1876 int isroot) 1877 { 1878 int ret; 1879 Dwarf_Off offset; 1880 Dwarf_Half tag; 1881 ctf_dwmap_t lookup, *map; 1882 ctf_id_t id; 1883 1884 if (idp == NULL) 1885 idp = &id; 1886 1887 if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0) 1888 return (ret); 1889 1890 if (offset > cup->cu_maxoff) { 1891 (void) snprintf(cup->cu_errbuf, cup->cu_errlen, 1892 "die offset %llu beyond maximum for header %llu\n", 1893 offset, cup->cu_maxoff); 1894 return (ECTF_CONVBKERR); 1895 } 1896 1897 /* 1898 * If we've already added an entry for this offset, then we're done. 1899 */ 1900 lookup.cdm_off = offset; 1901 if ((map = avl_find(&cup->cu_map, &lookup, NULL)) != NULL) { 1902 *idp = map->cdm_id; 1903 return (0); 1904 } 1905 1906 if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0) 1907 return (ret); 1908 1909 ret = ENOTSUP; 1910 switch (tag) { 1911 case DW_TAG_base_type: 1912 ctf_dprintf("base\n"); 1913 ret = ctf_dwarf_create_base(cup, die, idp, isroot, offset); 1914 break; 1915 case DW_TAG_array_type: 1916 ctf_dprintf("array\n"); 1917 ret = ctf_dwarf_create_array(cup, die, idp, isroot); 1918 break; 1919 case DW_TAG_enumeration_type: 1920 ctf_dprintf("enum\n"); 1921 ret = ctf_dwarf_create_enum(cup, die, idp, isroot); 1922 break; 1923 case DW_TAG_pointer_type: 1924 ctf_dprintf("pointer\n"); 1925 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_POINTER, 1926 isroot); 1927 break; 1928 case DW_TAG_structure_type: 1929 ctf_dprintf("struct\n"); 1930 ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_STRUCT, 1931 isroot); 1932 break; 1933 case DW_TAG_subroutine_type: 1934 ctf_dprintf("fptr\n"); 1935 ret = ctf_dwarf_create_fptr(cup, die, idp, isroot); 1936 break; 1937 case DW_TAG_typedef: 1938 ctf_dprintf("typedef\n"); 1939 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_TYPEDEF, 1940 isroot); 1941 break; 1942 case DW_TAG_union_type: 1943 ctf_dprintf("union\n"); 1944 ret = ctf_dwarf_create_sou(cup, die, idp, CTF_K_UNION, 1945 isroot); 1946 break; 1947 case DW_TAG_const_type: 1948 ctf_dprintf("const\n"); 1949 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_CONST, 1950 isroot); 1951 break; 1952 case DW_TAG_volatile_type: 1953 ctf_dprintf("volatile\n"); 1954 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_VOLATILE, 1955 isroot); 1956 break; 1957 case DW_TAG_restrict_type: 1958 ctf_dprintf("restrict\n"); 1959 ret = ctf_dwarf_create_reference(cup, die, idp, CTF_K_RESTRICT, 1960 isroot); 1961 break; 1962 default: 1963 ctf_dprintf("ignoring tag type %x\n", tag); 1964 *idp = CTF_ERR; 1965 ret = 0; 1966 break; 1967 } 1968 ctf_dprintf("ctf_dwarf_convert_type tag specific handler returned %d\n", 1969 ret); 1970 1971 return (ret); 1972 } 1973 1974 static int 1975 ctf_dwarf_walk_lexical(ctf_cu_t *cup, Dwarf_Die die) 1976 { 1977 int ret; 1978 Dwarf_Die child; 1979 1980 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) 1981 return (ret); 1982 1983 if (child == NULL) 1984 return (0); 1985 1986 return (ctf_dwarf_convert_die(cup, die)); 1987 } 1988 1989 static int 1990 ctf_dwarf_function_count(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip, 1991 boolean_t fptr) 1992 { 1993 int ret; 1994 Dwarf_Die child, sib, arg; 1995 1996 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) 1997 return (ret); 1998 1999 arg = child; 2000 while (arg != NULL) { 2001 Dwarf_Half tag; 2002 2003 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0) 2004 return (ret); 2005 2006 /* 2007 * We have to check for a varargs type declaration. This will 2008 * happen in one of two ways. If we have a function pointer 2009 * type, then it'll be done with a tag of type 2010 * DW_TAG_unspecified_parameters. However, it only means we have 2011 * a variable number of arguments, if we have more than one 2012 * argument found so far. Otherwise, when we have a function 2013 * type, it instead uses a formal parameter whose name is '...' 2014 * to indicate a variable arguments member. 2015 * 2016 * Also, if we have a function pointer, then we have to expect 2017 * that we might not get a name at all. 2018 */ 2019 if (tag == DW_TAG_formal_parameter && fptr == B_FALSE) { 2020 char *name; 2021 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, 2022 &name)) != 0) 2023 return (ret); 2024 if (strcmp(name, DWARF_VARARGS_NAME) == 0) 2025 fip->ctc_flags |= CTF_FUNC_VARARG; 2026 else 2027 fip->ctc_argc++; 2028 ctf_free(name, strlen(name) + 1); 2029 } else if (tag == DW_TAG_formal_parameter) { 2030 fip->ctc_argc++; 2031 } else if (tag == DW_TAG_unspecified_parameters && 2032 fip->ctc_argc > 0) { 2033 fip->ctc_flags |= CTF_FUNC_VARARG; 2034 } 2035 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0) 2036 return (ret); 2037 arg = sib; 2038 } 2039 2040 return (0); 2041 } 2042 2043 static int 2044 ctf_dwarf_convert_fargs(ctf_cu_t *cup, Dwarf_Die die, ctf_funcinfo_t *fip, 2045 ctf_id_t *argv) 2046 { 2047 int ret; 2048 int i = 0; 2049 Dwarf_Die child, sib, arg; 2050 2051 if ((ret = ctf_dwarf_child(cup, die, &child)) != 0) 2052 return (ret); 2053 2054 arg = child; 2055 while (arg != NULL) { 2056 Dwarf_Half tag; 2057 2058 if ((ret = ctf_dwarf_tag(cup, arg, &tag)) != 0) 2059 return (ret); 2060 if (tag == DW_TAG_formal_parameter) { 2061 Dwarf_Die tdie; 2062 2063 if ((ret = ctf_dwarf_refdie(cup, arg, DW_AT_type, 2064 &tdie)) != 0) 2065 return (ret); 2066 2067 if ((ret = ctf_dwarf_convert_type(cup, tdie, &argv[i], 2068 CTF_ADD_ROOT)) != 0) 2069 return (ret); 2070 i++; 2071 2072 /* 2073 * Once we hit argc entries, we're done. This ensures we 2074 * don't accidentally hit a varargs which should be the 2075 * last entry. 2076 */ 2077 if (i == fip->ctc_argc) 2078 break; 2079 } 2080 2081 if ((ret = ctf_dwarf_sib(cup, arg, &sib)) != 0) 2082 return (ret); 2083 arg = sib; 2084 } 2085 2086 return (0); 2087 } 2088 2089 static int 2090 ctf_dwarf_convert_function(ctf_cu_t *cup, Dwarf_Die die) 2091 { 2092 ctf_dwfunc_t *cdf; 2093 Dwarf_Die tdie; 2094 Dwarf_Bool b; 2095 char *name; 2096 int ret; 2097 2098 /* 2099 * Functions that don't have a name are generally functions that have 2100 * been inlined and thus most information about them has been lost. If 2101 * we can't get a name, then instead of returning ENOENT, we silently 2102 * swallow the error. 2103 */ 2104 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0) { 2105 if (ret == ENOENT) 2106 return (0); 2107 return (ret); 2108 } 2109 2110 ctf_dprintf("beginning work on function %s (die %llx)\n", 2111 name, ctf_die_offset(die)); 2112 2113 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) != 0) { 2114 if (ret != ENOENT) 2115 return (ret); 2116 } else if (b != 0) { 2117 /* 2118 * GCC7 at least creates empty DW_AT_declarations for functions 2119 * defined in headers. As they lack details on the function 2120 * prototype, we need to ignore them. If we later actually 2121 * see the relevant function's definition, we will see another 2122 * DW_TAG_subprogram that is more complete. 2123 */ 2124 ctf_dprintf("ignoring declaration of function %s (die %llx)\n", 2125 name, ctf_die_offset(die)); 2126 return (0); 2127 } 2128 2129 if ((cdf = ctf_alloc(sizeof (ctf_dwfunc_t))) == NULL) { 2130 ctf_free(name, strlen(name) + 1); 2131 return (ENOMEM); 2132 } 2133 bzero(cdf, sizeof (ctf_dwfunc_t)); 2134 cdf->cdf_name = name; 2135 2136 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) == 0) { 2137 if ((ret = ctf_dwarf_convert_type(cup, tdie, 2138 &(cdf->cdf_fip.ctc_return), CTF_ADD_ROOT)) != 0) { 2139 ctf_free(name, strlen(name) + 1); 2140 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2141 return (ret); 2142 } 2143 } else if (ret != ENOENT) { 2144 ctf_free(name, strlen(name) + 1); 2145 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2146 return (ret); 2147 } else { 2148 if ((cdf->cdf_fip.ctc_return = ctf_dwarf_void(cup)) == 2149 CTF_ERR) { 2150 ctf_free(name, strlen(name) + 1); 2151 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2152 return (ctf_errno(cup->cu_ctfp)); 2153 } 2154 } 2155 2156 /* 2157 * A function has a number of children, some of which may not be ones we 2158 * care about. Children that we care about have a type of 2159 * DW_TAG_formal_parameter. We're going to do two passes, the first to 2160 * count the arguments, the second to process them. Afterwards, we 2161 * should be good to go ahead and add this function. 2162 * 2163 * Note, we already got the return type by going in and grabbing it out 2164 * of the DW_AT_type. 2165 */ 2166 if ((ret = ctf_dwarf_function_count(cup, die, &cdf->cdf_fip, 2167 B_FALSE)) != 0) { 2168 ctf_free(name, strlen(name) + 1); 2169 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2170 return (ret); 2171 } 2172 2173 ctf_dprintf("beginning to convert function arguments %s\n", name); 2174 if (cdf->cdf_fip.ctc_argc != 0) { 2175 uint_t argc = cdf->cdf_fip.ctc_argc; 2176 cdf->cdf_argv = ctf_alloc(sizeof (ctf_id_t) * argc); 2177 if (cdf->cdf_argv == NULL) { 2178 ctf_free(name, strlen(name) + 1); 2179 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2180 return (ENOMEM); 2181 } 2182 if ((ret = ctf_dwarf_convert_fargs(cup, die, 2183 &cdf->cdf_fip, cdf->cdf_argv)) != 0) { 2184 ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) * argc); 2185 ctf_free(name, strlen(name) + 1); 2186 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2187 return (ret); 2188 } 2189 } else { 2190 cdf->cdf_argv = NULL; 2191 } 2192 2193 if ((ret = ctf_dwarf_isglobal(cup, die, &cdf->cdf_global)) != 0) { 2194 ctf_free(cdf->cdf_argv, sizeof (ctf_id_t) * 2195 cdf->cdf_fip.ctc_argc); 2196 ctf_free(name, strlen(name) + 1); 2197 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2198 return (ret); 2199 } 2200 2201 ctf_list_append(&cup->cu_funcs, cdf); 2202 return (ret); 2203 } 2204 2205 /* 2206 * Convert variables, but only if they're not prototypes and have names. 2207 */ 2208 static int 2209 ctf_dwarf_convert_variable(ctf_cu_t *cup, Dwarf_Die die) 2210 { 2211 int ret; 2212 char *name; 2213 Dwarf_Bool b; 2214 Dwarf_Die tdie; 2215 ctf_id_t id; 2216 ctf_dwvar_t *cdv; 2217 2218 /* Skip "Non-Defining Declarations" */ 2219 if ((ret = ctf_dwarf_boolean(cup, die, DW_AT_declaration, &b)) == 0) { 2220 if (b != 0) 2221 return (0); 2222 } else if (ret != ENOENT) { 2223 return (ret); 2224 } 2225 2226 /* 2227 * If we find a DIE of "Declarations Completing Non-Defining 2228 * Declarations", we will use the referenced type's DIE. This isn't 2229 * quite correct, e.g. DW_AT_decl_line will be the forward declaration 2230 * not this site. It's sufficient for what we need, however: in 2231 * particular, we should find DW_AT_external as needed there. 2232 */ 2233 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_specification, 2234 &tdie)) == 0) { 2235 Dwarf_Off offset; 2236 if ((ret = ctf_dwarf_offset(cup, tdie, &offset)) != 0) 2237 return (ret); 2238 ctf_dprintf("die 0x%llx DW_AT_specification -> die 0x%llx\n", 2239 ctf_die_offset(die), ctf_die_offset(tdie)); 2240 die = tdie; 2241 } else if (ret != ENOENT) { 2242 return (ret); 2243 } 2244 2245 if ((ret = ctf_dwarf_string(cup, die, DW_AT_name, &name)) != 0 && 2246 ret != ENOENT) 2247 return (ret); 2248 if (ret == ENOENT) 2249 return (0); 2250 2251 if ((ret = ctf_dwarf_refdie(cup, die, DW_AT_type, &tdie)) != 0) { 2252 ctf_free(name, strlen(name) + 1); 2253 return (ret); 2254 } 2255 2256 if ((ret = ctf_dwarf_convert_type(cup, tdie, &id, 2257 CTF_ADD_ROOT)) != 0) 2258 return (ret); 2259 2260 if ((cdv = ctf_alloc(sizeof (ctf_dwvar_t))) == NULL) { 2261 ctf_free(name, strlen(name) + 1); 2262 return (ENOMEM); 2263 } 2264 2265 cdv->cdv_name = name; 2266 cdv->cdv_type = id; 2267 2268 if ((ret = ctf_dwarf_isglobal(cup, die, &cdv->cdv_global)) != 0) { 2269 ctf_free(cdv, sizeof (ctf_dwvar_t)); 2270 ctf_free(name, strlen(name) + 1); 2271 return (ret); 2272 } 2273 2274 ctf_list_append(&cup->cu_vars, cdv); 2275 return (0); 2276 } 2277 2278 /* 2279 * Walk through our set of top-level types and process them. 2280 */ 2281 static int 2282 ctf_dwarf_walk_toplevel(ctf_cu_t *cup, Dwarf_Die die) 2283 { 2284 int ret; 2285 Dwarf_Off offset; 2286 Dwarf_Half tag; 2287 2288 if ((ret = ctf_dwarf_offset(cup, die, &offset)) != 0) 2289 return (ret); 2290 2291 if (offset > cup->cu_maxoff) { 2292 (void) snprintf(cup->cu_errbuf, cup->cu_errlen, 2293 "die offset %llu beyond maximum for header %llu\n", 2294 offset, cup->cu_maxoff); 2295 return (ECTF_CONVBKERR); 2296 } 2297 2298 if ((ret = ctf_dwarf_tag(cup, die, &tag)) != 0) 2299 return (ret); 2300 2301 ret = 0; 2302 switch (tag) { 2303 case DW_TAG_subprogram: 2304 ctf_dprintf("top level func\n"); 2305 ret = ctf_dwarf_convert_function(cup, die); 2306 break; 2307 case DW_TAG_variable: 2308 ctf_dprintf("top level var\n"); 2309 ret = ctf_dwarf_convert_variable(cup, die); 2310 break; 2311 case DW_TAG_lexical_block: 2312 ctf_dprintf("top level block\n"); 2313 ret = ctf_dwarf_walk_lexical(cup, die); 2314 break; 2315 case DW_TAG_enumeration_type: 2316 case DW_TAG_structure_type: 2317 case DW_TAG_typedef: 2318 case DW_TAG_union_type: 2319 ctf_dprintf("top level type\n"); 2320 ret = ctf_dwarf_convert_type(cup, die, NULL, B_TRUE); 2321 break; 2322 default: 2323 break; 2324 } 2325 2326 return (ret); 2327 } 2328 2329 2330 /* 2331 * We're given a node. At this node we need to convert it and then proceed to 2332 * convert any siblings that are associaed with this die. 2333 */ 2334 static int 2335 ctf_dwarf_convert_die(ctf_cu_t *cup, Dwarf_Die die) 2336 { 2337 while (die != NULL) { 2338 int ret; 2339 Dwarf_Die sib; 2340 2341 if ((ret = ctf_dwarf_walk_toplevel(cup, die)) != 0) 2342 return (ret); 2343 2344 if ((ret = ctf_dwarf_sib(cup, die, &sib)) != 0) 2345 return (ret); 2346 die = sib; 2347 } 2348 return (0); 2349 } 2350 2351 static int 2352 ctf_dwarf_fixup_die(ctf_cu_t *cup, boolean_t addpass) 2353 { 2354 ctf_dwmap_t *map; 2355 2356 for (map = avl_first(&cup->cu_map); map != NULL; 2357 map = AVL_NEXT(&cup->cu_map, map)) { 2358 int ret; 2359 if (map->cdm_fix == B_FALSE) 2360 continue; 2361 if ((ret = ctf_dwarf_fixup_sou(cup, map->cdm_die, map->cdm_id, 2362 addpass)) != 0) 2363 return (ret); 2364 } 2365 2366 return (0); 2367 } 2368 2369 /* 2370 * The DWARF information about a symbol and the information in the symbol table 2371 * may not be the same due to symbol reduction that is performed by ld due to a 2372 * mapfile or other such directive. We process weak symbols at a later time. 2373 * 2374 * The following are the rules that we employ: 2375 * 2376 * 1. A DWARF function that is considered exported matches STB_GLOBAL entries 2377 * with the same name. 2378 * 2379 * 2. A DWARF function that is considered exported matches STB_LOCAL entries 2380 * with the same name and the same file. This case may happen due to mapfile 2381 * reduction. 2382 * 2383 * 3. A DWARF function that is not considered exported matches STB_LOCAL entries 2384 * with the same name and the same file. 2385 * 2386 * 4. A DWARF function that has the same name as the symbol table entry, but the 2387 * files do not match. This is considered a 'fuzzy' match. This may also happen 2388 * due to a mapfile reduction. Fuzzy matching is only used when we know that the 2389 * file in question refers to the primary object. This is because when a symbol 2390 * is reduced in a mapfile, it's always going to be tagged as a local value in 2391 * the generated output and it is considered as to belong to the primary file 2392 * which is the first STT_FILE symbol we see. 2393 */ 2394 static boolean_t 2395 ctf_dwarf_symbol_match(const char *symtab_file, const char *symtab_name, 2396 uint_t symtab_bind, const char *dwarf_file, const char *dwarf_name, 2397 boolean_t dwarf_global, boolean_t *is_fuzzy) 2398 { 2399 *is_fuzzy = B_FALSE; 2400 2401 if (symtab_bind != STB_LOCAL && symtab_bind != STB_GLOBAL) { 2402 return (B_FALSE); 2403 } 2404 2405 if (strcmp(symtab_name, dwarf_name) != 0) { 2406 return (B_FALSE); 2407 } 2408 2409 if (symtab_bind == STB_GLOBAL) { 2410 return (dwarf_global); 2411 } 2412 2413 if (strcmp(symtab_file, dwarf_file) == 0) { 2414 return (B_TRUE); 2415 } 2416 2417 if (dwarf_global) { 2418 *is_fuzzy = B_TRUE; 2419 return (B_TRUE); 2420 } 2421 2422 return (B_FALSE); 2423 } 2424 2425 static ctf_dwfunc_t * 2426 ctf_dwarf_match_func(ctf_cu_t *cup, const char *file, const char *name, 2427 uint_t bind, boolean_t primary) 2428 { 2429 ctf_dwfunc_t *cdf, *fuzzy = NULL; 2430 2431 if (bind == STB_WEAK) 2432 return (NULL); 2433 2434 if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL)) 2435 return (NULL); 2436 2437 for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL; 2438 cdf = ctf_list_next(cdf)) { 2439 boolean_t is_fuzzy = B_FALSE; 2440 2441 if (ctf_dwarf_symbol_match(file, name, bind, cup->cu_name, 2442 cdf->cdf_name, cdf->cdf_global, &is_fuzzy)) { 2443 if (is_fuzzy) { 2444 if (primary) { 2445 fuzzy = cdf; 2446 } 2447 continue; 2448 } else { 2449 return (cdf); 2450 } 2451 } 2452 } 2453 2454 return (fuzzy); 2455 } 2456 2457 static ctf_dwvar_t * 2458 ctf_dwarf_match_var(ctf_cu_t *cup, const char *file, const char *name, 2459 uint_t bind, boolean_t primary) 2460 { 2461 ctf_dwvar_t *cdv, *fuzzy = NULL; 2462 2463 if (bind == STB_WEAK) 2464 return (NULL); 2465 2466 if (bind == STB_LOCAL && (file == NULL || cup->cu_name == NULL)) 2467 return (NULL); 2468 2469 for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL; 2470 cdv = ctf_list_next(cdv)) { 2471 boolean_t is_fuzzy = B_FALSE; 2472 2473 if (ctf_dwarf_symbol_match(file, name, bind, cup->cu_name, 2474 cdv->cdv_name, cdv->cdv_global, &is_fuzzy)) { 2475 if (is_fuzzy) { 2476 if (primary) { 2477 fuzzy = cdv; 2478 } 2479 } else { 2480 return (cdv); 2481 } 2482 } 2483 } 2484 2485 return (fuzzy); 2486 } 2487 2488 static int 2489 ctf_dwarf_conv_funcvars_cb(const Elf64_Sym *symp, ulong_t idx, 2490 const char *file, const char *name, boolean_t primary, void *arg) 2491 { 2492 int ret; 2493 uint_t bind, type; 2494 ctf_cu_t *cup = arg; 2495 2496 bind = GELF_ST_BIND(symp->st_info); 2497 type = GELF_ST_TYPE(symp->st_info); 2498 2499 /* 2500 * Come back to weak symbols in another pass 2501 */ 2502 if (bind == STB_WEAK) 2503 return (0); 2504 2505 if (type == STT_OBJECT) { 2506 ctf_dwvar_t *cdv = ctf_dwarf_match_var(cup, file, name, 2507 bind, primary); 2508 if (cdv == NULL) 2509 return (0); 2510 ret = ctf_add_object(cup->cu_ctfp, idx, cdv->cdv_type); 2511 ctf_dprintf("added object %s->%ld\n", name, cdv->cdv_type); 2512 } else { 2513 ctf_dwfunc_t *cdf = ctf_dwarf_match_func(cup, file, name, 2514 bind, primary); 2515 if (cdf == NULL) 2516 return (0); 2517 ret = ctf_add_function(cup->cu_ctfp, idx, &cdf->cdf_fip, 2518 cdf->cdf_argv); 2519 ctf_dprintf("added function %s\n", name); 2520 } 2521 2522 if (ret == CTF_ERR) { 2523 return (ctf_errno(cup->cu_ctfp)); 2524 } 2525 2526 return (0); 2527 } 2528 2529 static int 2530 ctf_dwarf_conv_funcvars(ctf_cu_t *cup) 2531 { 2532 return (ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_funcvars_cb, cup)); 2533 } 2534 2535 /* 2536 * If we have a weak symbol, attempt to find the strong symbol it will resolve 2537 * to. Note: the code where this actually happens is in sym_process() in 2538 * cmd/sgs/libld/common/syms.c 2539 * 2540 * Finding the matching symbol is unfortunately not trivial. For a symbol to be 2541 * a candidate, it must: 2542 * 2543 * - have the same type (function, object) 2544 * - have the same value (address) 2545 * - have the same size 2546 * - not be another weak symbol 2547 * - belong to the same section (checked via section index) 2548 * 2549 * To perform this check, we first iterate over the symbol table. For each weak 2550 * symbol that we encounter, we then do a second walk over the symbol table, 2551 * calling ctf_dwarf_conv_check_weak(). If a symbol matches the above, then it's 2552 * either a local or global symbol. If we find a global symbol then we go with 2553 * it and stop searching for additional matches. 2554 * 2555 * If instead, we find a local symbol, things are more complicated. The first 2556 * thing we do is to try and see if we have file information about both symbols 2557 * (STT_FILE). If they both have file information and it matches, then we treat 2558 * that as a good match and stop searching for additional matches. 2559 * 2560 * Otherwise, this means we have a non-matching file and a local symbol. We 2561 * treat this as a candidate and if we find a better match (one of the two cases 2562 * above), use that instead. There are two different ways this can happen. 2563 * Either this is a completely different symbol, or it's a once-global symbol 2564 * that was scoped to local via a mapfile. In the former case, curfile is 2565 * likely inaccurate since the linker does not preserve the needed curfile in 2566 * the order of the symbol table (see the comments about locally scoped symbols 2567 * in libld's update_osym()). As we can't tell this case from the former one, 2568 * we use this symbol iff no other matching symbol is found. 2569 * 2570 * What we really need here is a SUNW section containing weak<->strong mappings 2571 * that we can consume. 2572 */ 2573 typedef struct ctf_dwarf_weak_arg { 2574 const Elf64_Sym *cweak_symp; 2575 const char *cweak_file; 2576 boolean_t cweak_candidate; 2577 ulong_t cweak_idx; 2578 } ctf_dwarf_weak_arg_t; 2579 2580 static int 2581 ctf_dwarf_conv_check_weak(const Elf64_Sym *symp, ulong_t idx, const char *file, 2582 const char *name, boolean_t primary, void *arg) 2583 { 2584 ctf_dwarf_weak_arg_t *cweak = arg; 2585 2586 const Elf64_Sym *wsymp = cweak->cweak_symp; 2587 2588 ctf_dprintf("comparing weak to %s\n", name); 2589 2590 if (GELF_ST_BIND(symp->st_info) == STB_WEAK) { 2591 return (0); 2592 } 2593 2594 if (GELF_ST_TYPE(wsymp->st_info) != GELF_ST_TYPE(symp->st_info)) { 2595 return (0); 2596 } 2597 2598 if (wsymp->st_value != symp->st_value) { 2599 return (0); 2600 } 2601 2602 if (wsymp->st_size != symp->st_size) { 2603 return (0); 2604 } 2605 2606 if (wsymp->st_shndx != symp->st_shndx) { 2607 return (0); 2608 } 2609 2610 /* 2611 * Check if it's a weak candidate. 2612 */ 2613 if (GELF_ST_BIND(symp->st_info) == STB_LOCAL && 2614 (file == NULL || cweak->cweak_file == NULL || 2615 strcmp(file, cweak->cweak_file) != 0)) { 2616 cweak->cweak_candidate = B_TRUE; 2617 cweak->cweak_idx = idx; 2618 return (0); 2619 } 2620 2621 /* 2622 * Found a match, break. 2623 */ 2624 cweak->cweak_idx = idx; 2625 return (1); 2626 } 2627 2628 static int 2629 ctf_dwarf_duplicate_sym(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx) 2630 { 2631 ctf_id_t id = ctf_lookup_by_symbol(cup->cu_ctfp, matchidx); 2632 2633 /* 2634 * If we matched something that for some reason didn't have type data, 2635 * we don't consider that a fatal error and silently swallow it. 2636 */ 2637 if (id == CTF_ERR) { 2638 if (ctf_errno(cup->cu_ctfp) == ECTF_NOTYPEDAT) 2639 return (0); 2640 else 2641 return (ctf_errno(cup->cu_ctfp)); 2642 } 2643 2644 if (ctf_add_object(cup->cu_ctfp, idx, id) == CTF_ERR) 2645 return (ctf_errno(cup->cu_ctfp)); 2646 2647 return (0); 2648 } 2649 2650 static int 2651 ctf_dwarf_duplicate_func(ctf_cu_t *cup, ulong_t idx, ulong_t matchidx) 2652 { 2653 int ret; 2654 ctf_funcinfo_t fip; 2655 ctf_id_t *args = NULL; 2656 2657 if (ctf_func_info(cup->cu_ctfp, matchidx, &fip) == CTF_ERR) { 2658 if (ctf_errno(cup->cu_ctfp) == ECTF_NOFUNCDAT) 2659 return (0); 2660 else 2661 return (ctf_errno(cup->cu_ctfp)); 2662 } 2663 2664 if (fip.ctc_argc != 0) { 2665 args = ctf_alloc(sizeof (ctf_id_t) * fip.ctc_argc); 2666 if (args == NULL) 2667 return (ENOMEM); 2668 2669 if (ctf_func_args(cup->cu_ctfp, matchidx, fip.ctc_argc, args) == 2670 CTF_ERR) { 2671 ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc); 2672 return (ctf_errno(cup->cu_ctfp)); 2673 } 2674 } 2675 2676 ret = ctf_add_function(cup->cu_ctfp, idx, &fip, args); 2677 if (args != NULL) 2678 ctf_free(args, sizeof (ctf_id_t) * fip.ctc_argc); 2679 if (ret == CTF_ERR) 2680 return (ctf_errno(cup->cu_ctfp)); 2681 2682 return (0); 2683 } 2684 2685 static int 2686 ctf_dwarf_conv_weaks_cb(const Elf64_Sym *symp, ulong_t idx, const char *file, 2687 const char *name, boolean_t primary, void *arg) 2688 { 2689 int ret, type; 2690 ctf_dwarf_weak_arg_t cweak; 2691 ctf_cu_t *cup = arg; 2692 2693 /* 2694 * We only care about weak symbols. 2695 */ 2696 if (GELF_ST_BIND(symp->st_info) != STB_WEAK) 2697 return (0); 2698 2699 type = GELF_ST_TYPE(symp->st_info); 2700 ASSERT(type == STT_OBJECT || type == STT_FUNC); 2701 2702 /* 2703 * For each weak symbol we encounter, we need to do a second iteration 2704 * to try and find a match. We should probably think about other 2705 * techniques to try and save us time in the future. 2706 */ 2707 cweak.cweak_symp = symp; 2708 cweak.cweak_file = file; 2709 cweak.cweak_candidate = B_FALSE; 2710 cweak.cweak_idx = 0; 2711 2712 ctf_dprintf("Trying to find weak equiv for %s\n", name); 2713 2714 ret = ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_check_weak, &cweak); 2715 VERIFY(ret == 0 || ret == 1); 2716 2717 /* 2718 * Nothing was ever found, we're not going to add anything for this 2719 * entry. 2720 */ 2721 if (ret == 0 && cweak.cweak_candidate == B_FALSE) { 2722 ctf_dprintf("found no weak match for %s\n", name); 2723 return (0); 2724 } 2725 2726 /* 2727 * Now, finally go and add the type based on the match. 2728 */ 2729 ctf_dprintf("matched weak symbol %lu to %lu\n", idx, cweak.cweak_idx); 2730 if (type == STT_OBJECT) { 2731 ret = ctf_dwarf_duplicate_sym(cup, idx, cweak.cweak_idx); 2732 } else { 2733 ret = ctf_dwarf_duplicate_func(cup, idx, cweak.cweak_idx); 2734 } 2735 2736 return (ret); 2737 } 2738 2739 static int 2740 ctf_dwarf_conv_weaks(ctf_cu_t *cup) 2741 { 2742 return (ctf_symtab_iter(cup->cu_ctfp, ctf_dwarf_conv_weaks_cb, cup)); 2743 } 2744 2745 /* ARGSUSED */ 2746 static int 2747 ctf_dwarf_convert_one(void *arg, void *unused) 2748 { 2749 int ret; 2750 ctf_file_t *dedup; 2751 ctf_cu_t *cup = arg; 2752 2753 ctf_dprintf("converting die: %s\n", cup->cu_name); 2754 ctf_dprintf("max offset: %x\n", cup->cu_maxoff); 2755 VERIFY(cup != NULL); 2756 2757 ret = ctf_dwarf_convert_die(cup, cup->cu_cu); 2758 ctf_dprintf("ctf_dwarf_convert_die (%s) returned %d\n", cup->cu_name, 2759 ret); 2760 if (ret != 0) { 2761 return (ret); 2762 } 2763 if (ctf_update(cup->cu_ctfp) != 0) { 2764 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0, 2765 "failed to update output ctf container")); 2766 } 2767 2768 ret = ctf_dwarf_fixup_die(cup, B_FALSE); 2769 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name, 2770 ret); 2771 if (ret != 0) { 2772 return (ret); 2773 } 2774 if (ctf_update(cup->cu_ctfp) != 0) { 2775 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0, 2776 "failed to update output ctf container")); 2777 } 2778 2779 ret = ctf_dwarf_fixup_die(cup, B_TRUE); 2780 ctf_dprintf("ctf_dwarf_fixup_die (%s) returned %d\n", cup->cu_name, 2781 ret); 2782 if (ret != 0) { 2783 return (ret); 2784 } 2785 if (ctf_update(cup->cu_ctfp) != 0) { 2786 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0, 2787 "failed to update output ctf container")); 2788 } 2789 2790 2791 if ((ret = ctf_dwarf_conv_funcvars(cup)) != 0) { 2792 return (ctf_dwarf_error(cup, NULL, ret, 2793 "failed to convert strong functions and variables")); 2794 } 2795 2796 if (ctf_update(cup->cu_ctfp) != 0) { 2797 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0, 2798 "failed to update output ctf container")); 2799 } 2800 2801 if (cup->cu_doweaks == B_TRUE) { 2802 if ((ret = ctf_dwarf_conv_weaks(cup)) != 0) { 2803 return (ctf_dwarf_error(cup, NULL, ret, 2804 "failed to convert weak functions and variables")); 2805 } 2806 2807 if (ctf_update(cup->cu_ctfp) != 0) { 2808 return (ctf_dwarf_error(cup, cup->cu_ctfp, 0, 2809 "failed to update output ctf container")); 2810 } 2811 } 2812 2813 ctf_phase_dump(cup->cu_ctfp, "pre-dwarf-dedup", cup->cu_name); 2814 ctf_dprintf("adding inputs for dedup\n"); 2815 if ((ret = ctf_merge_add(cup->cu_cmh, cup->cu_ctfp)) != 0) { 2816 return (ctf_dwarf_error(cup, NULL, ret, 2817 "failed to add inputs for merge")); 2818 } 2819 2820 ctf_dprintf("starting dedup of %s\n", cup->cu_name); 2821 if ((ret = ctf_merge_dedup(cup->cu_cmh, &dedup)) != 0) { 2822 return (ctf_dwarf_error(cup, NULL, ret, 2823 "failed to deduplicate die")); 2824 } 2825 ctf_close(cup->cu_ctfp); 2826 cup->cu_ctfp = dedup; 2827 ctf_phase_dump(cup->cu_ctfp, "post-dwarf-dedup", cup->cu_name); 2828 2829 return (0); 2830 } 2831 2832 /* 2833 * Note, we expect that if we're returning a ctf_file_t from one of the dies, 2834 * say in the single node case, it's been saved and the entry here has been set 2835 * to NULL, which ctf_close happily ignores. 2836 */ 2837 static void 2838 ctf_dwarf_free_die(ctf_cu_t *cup) 2839 { 2840 ctf_dwfunc_t *cdf, *ndf; 2841 ctf_dwvar_t *cdv, *ndv; 2842 ctf_dwbitf_t *cdb, *ndb; 2843 ctf_dwmap_t *map; 2844 void *cookie; 2845 Dwarf_Error derr; 2846 2847 ctf_dprintf("Beginning to free die: %p\n", cup); 2848 cup->cu_elf = NULL; 2849 ctf_dprintf("Trying to free name: %p\n", cup->cu_name); 2850 if (cup->cu_name != NULL) 2851 ctf_free(cup->cu_name, strlen(cup->cu_name) + 1); 2852 ctf_dprintf("Trying to free merge handle: %p\n", cup->cu_cmh); 2853 if (cup->cu_cmh != NULL) { 2854 ctf_merge_fini(cup->cu_cmh); 2855 cup->cu_cmh = NULL; 2856 } 2857 2858 ctf_dprintf("Trying to free functions\n"); 2859 for (cdf = ctf_list_next(&cup->cu_funcs); cdf != NULL; cdf = ndf) { 2860 ndf = ctf_list_next(cdf); 2861 ctf_free(cdf->cdf_name, strlen(cdf->cdf_name) + 1); 2862 if (cdf->cdf_fip.ctc_argc != 0) { 2863 ctf_free(cdf->cdf_argv, 2864 sizeof (ctf_id_t) * cdf->cdf_fip.ctc_argc); 2865 } 2866 ctf_free(cdf, sizeof (ctf_dwfunc_t)); 2867 } 2868 2869 ctf_dprintf("Trying to free variables\n"); 2870 for (cdv = ctf_list_next(&cup->cu_vars); cdv != NULL; cdv = ndv) { 2871 ndv = ctf_list_next(cdv); 2872 ctf_free(cdv->cdv_name, strlen(cdv->cdv_name) + 1); 2873 ctf_free(cdv, sizeof (ctf_dwvar_t)); 2874 } 2875 2876 ctf_dprintf("Trying to free bitfields\n"); 2877 for (cdb = ctf_list_next(&cup->cu_bitfields); cdb != NULL; cdb = ndb) { 2878 ndb = ctf_list_next(cdb); 2879 ctf_free(cdb, sizeof (ctf_dwbitf_t)); 2880 } 2881 2882 ctf_dprintf("Trying to clean up dwarf_t: %p\n", cup->cu_dwarf); 2883 if (cup->cu_dwarf != NULL) 2884 (void) dwarf_finish(cup->cu_dwarf, &derr); 2885 cup->cu_dwarf = NULL; 2886 ctf_close(cup->cu_ctfp); 2887 2888 cookie = NULL; 2889 while ((map = avl_destroy_nodes(&cup->cu_map, &cookie)) != NULL) { 2890 ctf_free(map, sizeof (ctf_dwmap_t)); 2891 } 2892 avl_destroy(&cup->cu_map); 2893 cup->cu_errbuf = NULL; 2894 } 2895 2896 static void 2897 ctf_dwarf_free_dies(ctf_cu_t *cdies, int ndies) 2898 { 2899 int i; 2900 2901 ctf_dprintf("Beginning to free dies\n"); 2902 for (i = 0; i < ndies; i++) { 2903 ctf_dwarf_free_die(&cdies[i]); 2904 } 2905 2906 ctf_free(cdies, sizeof (ctf_cu_t) * ndies); 2907 } 2908 2909 static int 2910 ctf_dwarf_count_dies(Dwarf_Debug dw, Dwarf_Error *derr, int *ndies, 2911 char *errbuf, size_t errlen) 2912 { 2913 int ret; 2914 Dwarf_Half vers; 2915 Dwarf_Unsigned nexthdr; 2916 2917 while ((ret = dwarf_next_cu_header(dw, NULL, &vers, NULL, NULL, 2918 &nexthdr, derr)) != DW_DLV_NO_ENTRY) { 2919 if (ret != DW_DLV_OK) { 2920 (void) snprintf(errbuf, errlen, 2921 "file does not contain valid DWARF data: %s\n", 2922 dwarf_errmsg(*derr)); 2923 return (ECTF_CONVBKERR); 2924 } 2925 2926 if (vers != DWARF_VERSION_TWO) { 2927 (void) snprintf(errbuf, errlen, 2928 "unsupported DWARF version: %d\n", vers); 2929 return (ECTF_CONVBKERR); 2930 } 2931 *ndies = *ndies + 1; 2932 } 2933 2934 return (0); 2935 } 2936 2937 static int 2938 ctf_dwarf_init_die(int fd, Elf *elf, ctf_cu_t *cup, int ndie, char *errbuf, 2939 size_t errlen) 2940 { 2941 int ret; 2942 Dwarf_Unsigned hdrlen, abboff, nexthdr; 2943 Dwarf_Half addrsz; 2944 Dwarf_Unsigned offset = 0; 2945 Dwarf_Error derr; 2946 2947 while ((ret = dwarf_next_cu_header(cup->cu_dwarf, &hdrlen, NULL, 2948 &abboff, &addrsz, &nexthdr, &derr)) != DW_DLV_NO_ENTRY) { 2949 char *name; 2950 Dwarf_Die cu, child; 2951 2952 /* Based on the counting above, we should be good to go */ 2953 VERIFY(ret == DW_DLV_OK); 2954 if (ndie > 0) { 2955 ndie--; 2956 offset = nexthdr; 2957 continue; 2958 } 2959 2960 /* 2961 * Compilers are apparently inconsistent. Some emit no DWARF for 2962 * empty files and others emit empty compilation unit. 2963 */ 2964 cup->cu_voidtid = CTF_ERR; 2965 cup->cu_longtid = CTF_ERR; 2966 cup->cu_elf = elf; 2967 cup->cu_maxoff = nexthdr - 1; 2968 cup->cu_ctfp = ctf_fdcreate(fd, &ret); 2969 if (cup->cu_ctfp == NULL) 2970 return (ret); 2971 2972 avl_create(&cup->cu_map, ctf_dwmap_comp, sizeof (ctf_dwmap_t), 2973 offsetof(ctf_dwmap_t, cdm_avl)); 2974 cup->cu_errbuf = errbuf; 2975 cup->cu_errlen = errlen; 2976 bzero(&cup->cu_vars, sizeof (ctf_list_t)); 2977 bzero(&cup->cu_funcs, sizeof (ctf_list_t)); 2978 bzero(&cup->cu_bitfields, sizeof (ctf_list_t)); 2979 2980 if ((ret = ctf_dwarf_die_elfenc(elf, cup, errbuf, 2981 errlen)) != 0) 2982 return (ret); 2983 2984 if ((ret = ctf_dwarf_sib(cup, NULL, &cu)) != 0) 2985 return (ret); 2986 2987 if (cu == NULL) { 2988 (void) snprintf(errbuf, errlen, 2989 "file does not contain DWARF data"); 2990 return (ECTF_CONVNODEBUG); 2991 } 2992 2993 if ((ret = ctf_dwarf_child(cup, cu, &child)) != 0) 2994 return (ret); 2995 2996 if (child == NULL) { 2997 (void) snprintf(errbuf, errlen, 2998 "file does not contain DWARF data"); 2999 return (ECTF_CONVNODEBUG); 3000 } 3001 3002 cup->cu_cuoff = offset; 3003 cup->cu_cu = child; 3004 3005 if ((cup->cu_cmh = ctf_merge_init(fd, &ret)) == NULL) 3006 return (ret); 3007 3008 if (ctf_dwarf_string(cup, cu, DW_AT_name, &name) == 0) { 3009 size_t len = strlen(name) + 1; 3010 char *b = basename(name); 3011 cup->cu_name = strdup(b); 3012 ctf_free(name, len); 3013 } 3014 break; 3015 } 3016 3017 return (0); 3018 } 3019 3020 /* 3021 * This is our only recourse to identify a C source file that is missing debug 3022 * info: it will be mentioned as an STT_FILE, but not have a compile unit entry. 3023 * (A traditional ctfmerge works on individual files, so can identify missing 3024 * DWARF more directly, via ctf_has_c_source() on the .o file.) 3025 * 3026 * As we operate on basenames, this can of course miss some cases, but it's 3027 * better than not checking at all. 3028 * 3029 * We explicitly whitelist some CRT components. Failing that, there's always 3030 * the -m option. 3031 */ 3032 static boolean_t 3033 c_source_has_debug(const char *file, ctf_cu_t *cus, size_t nr_cus) 3034 { 3035 const char *basename = strrchr(file, '/'); 3036 3037 if (basename == NULL) 3038 basename = file; 3039 else 3040 basename++; 3041 3042 if (strcmp(basename, "common-crt.c") == 0 || 3043 strcmp(basename, "gmon.c") == 0 || 3044 strcmp(basename, "dlink_init.c") == 0 || 3045 strcmp(basename, "dlink_common.c") == 0 || 3046 strncmp(basename, "crt", strlen("crt")) == 0 || 3047 strncmp(basename, "values-", strlen("values-")) == 0) 3048 return (B_TRUE); 3049 3050 for (size_t i = 0; i < nr_cus; i++) { 3051 if (strcmp(basename, cus[i].cu_name) == 0) 3052 return (B_TRUE); 3053 } 3054 3055 return (B_FALSE); 3056 } 3057 3058 static int 3059 ctf_dwarf_check_missing(ctf_cu_t *cus, size_t nr_cus, Elf *elf, 3060 char *errmsg, size_t errlen) 3061 { 3062 Elf_Scn *scn, *strscn; 3063 Elf_Data *data, *strdata; 3064 GElf_Shdr shdr; 3065 ulong_t i; 3066 3067 scn = NULL; 3068 while ((scn = elf_nextscn(elf, scn)) != NULL) { 3069 if (gelf_getshdr(scn, &shdr) == NULL) { 3070 (void) snprintf(errmsg, errlen, 3071 "failed to get section header: %s\n", 3072 elf_errmsg(elf_errno())); 3073 return (EINVAL); 3074 } 3075 3076 if (shdr.sh_type == SHT_SYMTAB) 3077 break; 3078 } 3079 3080 if (scn == NULL) 3081 return (0); 3082 3083 if ((strscn = elf_getscn(elf, shdr.sh_link)) == NULL) { 3084 (void) snprintf(errmsg, errlen, 3085 "failed to get str section: %s\n", 3086 elf_errmsg(elf_errno())); 3087 return (EINVAL); 3088 } 3089 3090 if ((data = elf_getdata(scn, NULL)) == NULL) { 3091 (void) snprintf(errmsg, errlen, "failed to read section: %s\n", 3092 elf_errmsg(elf_errno())); 3093 return (EINVAL); 3094 } 3095 3096 if ((strdata = elf_getdata(strscn, NULL)) == NULL) { 3097 (void) snprintf(errmsg, errlen, 3098 "failed to read string table: %s\n", 3099 elf_errmsg(elf_errno())); 3100 return (EINVAL); 3101 } 3102 3103 for (i = 0; i < shdr.sh_size / shdr.sh_entsize; i++) { 3104 GElf_Sym sym; 3105 const char *file; 3106 size_t len; 3107 3108 if (gelf_getsym(data, i, &sym) == NULL) { 3109 (void) snprintf(errmsg, errlen, 3110 "failed to read sym %lu: %s\n", 3111 i, elf_errmsg(elf_errno())); 3112 return (EINVAL); 3113 } 3114 3115 if (GELF_ST_TYPE(sym.st_info) != STT_FILE) 3116 continue; 3117 3118 file = (const char *)((uintptr_t)strdata->d_buf + sym.st_name); 3119 len = strlen(file); 3120 if (len < 2 || strncmp(".c", &file[len - 2], 2) != 0) 3121 continue; 3122 3123 if (!c_source_has_debug(file, cus, nr_cus)) { 3124 (void) snprintf(errmsg, errlen, 3125 "file %s is missing debug info\n", file); 3126 return (ECTF_CONVNODEBUG); 3127 } 3128 } 3129 3130 return (0); 3131 } 3132 3133 int 3134 ctf_dwarf_convert(int fd, Elf *elf, uint_t nthrs, uint_t flags, 3135 ctf_file_t **fpp, char *errbuf, size_t errlen) 3136 { 3137 int err, ret, ndies, i; 3138 Dwarf_Debug dw; 3139 Dwarf_Error derr; 3140 ctf_cu_t *cdies = NULL, *cup; 3141 workq_t *wqp = NULL; 3142 3143 *fpp = NULL; 3144 3145 ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dw, &derr); 3146 if (ret != DW_DLV_OK) { 3147 if (ret == DW_DLV_NO_ENTRY || 3148 dwarf_errno(derr) == DW_DLE_DEBUG_INFO_NULL) { 3149 (void) snprintf(errbuf, errlen, 3150 "file does not contain DWARF data\n"); 3151 return (ECTF_CONVNODEBUG); 3152 } 3153 3154 (void) snprintf(errbuf, errlen, 3155 "dwarf_elf_init() failed: %s\n", dwarf_errmsg(derr)); 3156 return (ECTF_CONVBKERR); 3157 } 3158 3159 /* 3160 * Iterate over all of the compilation units and create a ctf_cu_t for 3161 * each of them. This is used to determine if we have zero, one, or 3162 * multiple dies to convert. If we have zero, that's an error. If 3163 * there's only one die, that's the simple case. No merge needed and 3164 * only a single Dwarf_Debug as well. 3165 */ 3166 ndies = 0; 3167 err = ctf_dwarf_count_dies(dw, &derr, &ndies, errbuf, errlen); 3168 3169 ctf_dprintf("found %d DWARF CUs\n", ndies); 3170 3171 if (ndies == 0) { 3172 (void) snprintf(errbuf, errlen, 3173 "file does not contain DWARF data\n"); 3174 return (ECTF_CONVNODEBUG); 3175 } 3176 3177 (void) dwarf_finish(dw, &derr); 3178 cdies = ctf_alloc(sizeof (ctf_cu_t) * ndies); 3179 if (cdies == NULL) { 3180 return (ENOMEM); 3181 } 3182 3183 bzero(cdies, sizeof (ctf_cu_t) * ndies); 3184 3185 for (i = 0; i < ndies; i++) { 3186 cup = &cdies[i]; 3187 ret = dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, 3188 &cup->cu_dwarf, &derr); 3189 if (ret != 0) { 3190 ctf_free(cdies, sizeof (ctf_cu_t) * ndies); 3191 (void) snprintf(errbuf, errlen, 3192 "failed to initialize DWARF: %s\n", 3193 dwarf_errmsg(derr)); 3194 return (ECTF_CONVBKERR); 3195 } 3196 3197 err = ctf_dwarf_init_die(fd, elf, cup, i, errbuf, errlen); 3198 if (err != 0) 3199 goto out; 3200 3201 cup->cu_doweaks = ndies > 1 ? B_FALSE : B_TRUE; 3202 } 3203 3204 if (!(flags & CTF_ALLOW_MISSING_DEBUG) && 3205 (err = ctf_dwarf_check_missing(cdies, ndies, 3206 elf, errbuf, errlen)) != 0) 3207 goto out; 3208 3209 /* 3210 * If we only have one compilation unit, there's no reason to use 3211 * multiple threads, even if the user requested them. After all, they 3212 * just gave us an upper bound. 3213 */ 3214 if (ndies == 1) 3215 nthrs = 1; 3216 3217 if (workq_init(&wqp, nthrs) == -1) { 3218 err = errno; 3219 goto out; 3220 } 3221 3222 for (i = 0; i < ndies; i++) { 3223 cup = &cdies[i]; 3224 ctf_dprintf("adding cu %s: %p, %x %x\n", cup->cu_name, 3225 cup->cu_cu, cup->cu_cuoff, cup->cu_maxoff); 3226 if (workq_add(wqp, cup) == -1) { 3227 err = errno; 3228 goto out; 3229 } 3230 } 3231 3232 ret = workq_work(wqp, ctf_dwarf_convert_one, NULL, &err); 3233 if (ret == WORKQ_ERROR) { 3234 err = errno; 3235 goto out; 3236 } else if (ret == WORKQ_UERROR) { 3237 ctf_dprintf("internal convert failed: %s\n", 3238 ctf_errmsg(err)); 3239 goto out; 3240 } 3241 3242 ctf_dprintf("Determining next phase: have %d CUs\n", ndies); 3243 if (ndies != 1) { 3244 ctf_merge_t *cmp; 3245 3246 cmp = ctf_merge_init(fd, &err); 3247 if (cmp == NULL) 3248 goto out; 3249 3250 ctf_dprintf("setting threads\n"); 3251 if ((err = ctf_merge_set_nthreads(cmp, nthrs)) != 0) { 3252 ctf_merge_fini(cmp); 3253 goto out; 3254 } 3255 3256 for (i = 0; i < ndies; i++) { 3257 cup = &cdies[i]; 3258 if ((err = ctf_merge_add(cmp, cup->cu_ctfp)) != 0) { 3259 ctf_merge_fini(cmp); 3260 goto out; 3261 } 3262 } 3263 3264 ctf_dprintf("performing merge\n"); 3265 err = ctf_merge_merge(cmp, fpp); 3266 if (err != 0) { 3267 ctf_dprintf("failed merge!\n"); 3268 *fpp = NULL; 3269 ctf_merge_fini(cmp); 3270 goto out; 3271 } 3272 ctf_merge_fini(cmp); 3273 err = 0; 3274 ctf_dprintf("successfully converted!\n"); 3275 } else { 3276 err = 0; 3277 *fpp = cdies->cu_ctfp; 3278 cdies->cu_ctfp = NULL; 3279 ctf_dprintf("successfully converted!\n"); 3280 } 3281 3282 out: 3283 workq_fini(wqp); 3284 ctf_dwarf_free_dies(cdies, ndies); 3285 return (err); 3286 } 3287