1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3 /* 4 * BTF-to-C type converter. 5 * 6 * Copyright (c) 2019 Facebook 7 */ 8 9 #include <stdbool.h> 10 #include <stddef.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <errno.h> 14 #include <linux/err.h> 15 #include <linux/btf.h> 16 #include "btf.h" 17 #include "hashmap.h" 18 #include "libbpf.h" 19 #include "libbpf_internal.h" 20 21 #define min(x, y) ((x) < (y) ? (x) : (y)) 22 #define max(x, y) ((x) < (y) ? (y) : (x)) 23 24 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t"; 25 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1; 26 27 static const char *pfx(int lvl) 28 { 29 return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl]; 30 } 31 32 enum btf_dump_type_order_state { 33 NOT_ORDERED, 34 ORDERING, 35 ORDERED, 36 }; 37 38 enum btf_dump_type_emit_state { 39 NOT_EMITTED, 40 EMITTING, 41 EMITTED, 42 }; 43 44 /* per-type auxiliary state */ 45 struct btf_dump_type_aux_state { 46 /* topological sorting state */ 47 enum btf_dump_type_order_state order_state: 2; 48 /* emitting state used to determine the need for forward declaration */ 49 enum btf_dump_type_emit_state emit_state: 2; 50 /* whether forward declaration was already emitted */ 51 __u8 fwd_emitted: 1; 52 /* whether unique non-duplicate name was already assigned */ 53 __u8 name_resolved: 1; 54 }; 55 56 struct btf_dump { 57 const struct btf *btf; 58 const struct btf_ext *btf_ext; 59 btf_dump_printf_fn_t printf_fn; 60 struct btf_dump_opts opts; 61 62 /* per-type auxiliary state */ 63 struct btf_dump_type_aux_state *type_states; 64 /* per-type optional cached unique name, must be freed, if present */ 65 const char **cached_names; 66 67 /* topo-sorted list of dependent type definitions */ 68 __u32 *emit_queue; 69 int emit_queue_cap; 70 int emit_queue_cnt; 71 72 /* 73 * stack of type declarations (e.g., chain of modifiers, arrays, 74 * funcs, etc) 75 */ 76 __u32 *decl_stack; 77 int decl_stack_cap; 78 int decl_stack_cnt; 79 80 /* maps struct/union/enum name to a number of name occurrences */ 81 struct hashmap *type_names; 82 /* 83 * maps typedef identifiers and enum value names to a number of such 84 * name occurrences 85 */ 86 struct hashmap *ident_names; 87 }; 88 89 static size_t str_hash_fn(const void *key, void *ctx) 90 { 91 const char *s = key; 92 size_t h = 0; 93 94 while (*s) { 95 h = h * 31 + *s; 96 s++; 97 } 98 return h; 99 } 100 101 static bool str_equal_fn(const void *a, const void *b, void *ctx) 102 { 103 return strcmp(a, b) == 0; 104 } 105 106 static __u16 btf_kind_of(const struct btf_type *t) 107 { 108 return BTF_INFO_KIND(t->info); 109 } 110 111 static __u16 btf_vlen_of(const struct btf_type *t) 112 { 113 return BTF_INFO_VLEN(t->info); 114 } 115 116 static bool btf_kflag_of(const struct btf_type *t) 117 { 118 return BTF_INFO_KFLAG(t->info); 119 } 120 121 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off) 122 { 123 return btf__name_by_offset(d->btf, name_off); 124 } 125 126 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...) 127 { 128 va_list args; 129 130 va_start(args, fmt); 131 d->printf_fn(d->opts.ctx, fmt, args); 132 va_end(args); 133 } 134 135 struct btf_dump *btf_dump__new(const struct btf *btf, 136 const struct btf_ext *btf_ext, 137 const struct btf_dump_opts *opts, 138 btf_dump_printf_fn_t printf_fn) 139 { 140 struct btf_dump *d; 141 int err; 142 143 d = calloc(1, sizeof(struct btf_dump)); 144 if (!d) 145 return ERR_PTR(-ENOMEM); 146 147 d->btf = btf; 148 d->btf_ext = btf_ext; 149 d->printf_fn = printf_fn; 150 d->opts.ctx = opts ? opts->ctx : NULL; 151 152 d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL); 153 if (IS_ERR(d->type_names)) { 154 err = PTR_ERR(d->type_names); 155 d->type_names = NULL; 156 btf_dump__free(d); 157 return ERR_PTR(err); 158 } 159 d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL); 160 if (IS_ERR(d->ident_names)) { 161 err = PTR_ERR(d->ident_names); 162 d->ident_names = NULL; 163 btf_dump__free(d); 164 return ERR_PTR(err); 165 } 166 167 return d; 168 } 169 170 void btf_dump__free(struct btf_dump *d) 171 { 172 int i, cnt; 173 174 if (!d) 175 return; 176 177 free(d->type_states); 178 if (d->cached_names) { 179 /* any set cached name is owned by us and should be freed */ 180 for (i = 0, cnt = btf__get_nr_types(d->btf); i <= cnt; i++) { 181 if (d->cached_names[i]) 182 free((void *)d->cached_names[i]); 183 } 184 } 185 free(d->cached_names); 186 free(d->emit_queue); 187 free(d->decl_stack); 188 hashmap__free(d->type_names); 189 hashmap__free(d->ident_names); 190 191 free(d); 192 } 193 194 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr); 195 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id); 196 197 /* 198 * Dump BTF type in a compilable C syntax, including all the necessary 199 * dependent types, necessary for compilation. If some of the dependent types 200 * were already emitted as part of previous btf_dump__dump_type() invocation 201 * for another type, they won't be emitted again. This API allows callers to 202 * filter out BTF types according to user-defined criterias and emitted only 203 * minimal subset of types, necessary to compile everything. Full struct/union 204 * definitions will still be emitted, even if the only usage is through 205 * pointer and could be satisfied with just a forward declaration. 206 * 207 * Dumping is done in two high-level passes: 208 * 1. Topologically sort type definitions to satisfy C rules of compilation. 209 * 2. Emit type definitions in C syntax. 210 * 211 * Returns 0 on success; <0, otherwise. 212 */ 213 int btf_dump__dump_type(struct btf_dump *d, __u32 id) 214 { 215 int err, i; 216 217 if (id > btf__get_nr_types(d->btf)) 218 return -EINVAL; 219 220 /* type states are lazily allocated, as they might not be needed */ 221 if (!d->type_states) { 222 d->type_states = calloc(1 + btf__get_nr_types(d->btf), 223 sizeof(d->type_states[0])); 224 if (!d->type_states) 225 return -ENOMEM; 226 d->cached_names = calloc(1 + btf__get_nr_types(d->btf), 227 sizeof(d->cached_names[0])); 228 if (!d->cached_names) 229 return -ENOMEM; 230 231 /* VOID is special */ 232 d->type_states[0].order_state = ORDERED; 233 d->type_states[0].emit_state = EMITTED; 234 } 235 236 d->emit_queue_cnt = 0; 237 err = btf_dump_order_type(d, id, false); 238 if (err < 0) 239 return err; 240 241 for (i = 0; i < d->emit_queue_cnt; i++) 242 btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/); 243 244 return 0; 245 } 246 247 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id) 248 { 249 __u32 *new_queue; 250 size_t new_cap; 251 252 if (d->emit_queue_cnt >= d->emit_queue_cap) { 253 new_cap = max(16, d->emit_queue_cap * 3 / 2); 254 new_queue = realloc(d->emit_queue, 255 new_cap * sizeof(new_queue[0])); 256 if (!new_queue) 257 return -ENOMEM; 258 d->emit_queue = new_queue; 259 d->emit_queue_cap = new_cap; 260 } 261 262 d->emit_queue[d->emit_queue_cnt++] = id; 263 return 0; 264 } 265 266 /* 267 * Determine order of emitting dependent types and specified type to satisfy 268 * C compilation rules. This is done through topological sorting with an 269 * additional complication which comes from C rules. The main idea for C is 270 * that if some type is "embedded" into a struct/union, it's size needs to be 271 * known at the time of definition of containing type. E.g., for: 272 * 273 * struct A {}; 274 * struct B { struct A x; } 275 * 276 * struct A *HAS* to be defined before struct B, because it's "embedded", 277 * i.e., it is part of struct B layout. But in the following case: 278 * 279 * struct A; 280 * struct B { struct A *x; } 281 * struct A {}; 282 * 283 * it's enough to just have a forward declaration of struct A at the time of 284 * struct B definition, as struct B has a pointer to struct A, so the size of 285 * field x is known without knowing struct A size: it's sizeof(void *). 286 * 287 * Unfortunately, there are some trickier cases we need to handle, e.g.: 288 * 289 * struct A {}; // if this was forward-declaration: compilation error 290 * struct B { 291 * struct { // anonymous struct 292 * struct A y; 293 * } *x; 294 * }; 295 * 296 * In this case, struct B's field x is a pointer, so it's size is known 297 * regardless of the size of (anonymous) struct it points to. But because this 298 * struct is anonymous and thus defined inline inside struct B, *and* it 299 * embeds struct A, compiler requires full definition of struct A to be known 300 * before struct B can be defined. This creates a transitive dependency 301 * between struct A and struct B. If struct A was forward-declared before 302 * struct B definition and fully defined after struct B definition, that would 303 * trigger compilation error. 304 * 305 * All this means that while we are doing topological sorting on BTF type 306 * graph, we need to determine relationships between different types (graph 307 * nodes): 308 * - weak link (relationship) between X and Y, if Y *CAN* be 309 * forward-declared at the point of X definition; 310 * - strong link, if Y *HAS* to be fully-defined before X can be defined. 311 * 312 * The rule is as follows. Given a chain of BTF types from X to Y, if there is 313 * BTF_KIND_PTR type in the chain and at least one non-anonymous type 314 * Z (excluding X, including Y), then link is weak. Otherwise, it's strong. 315 * Weak/strong relationship is determined recursively during DFS traversal and 316 * is returned as a result from btf_dump_order_type(). 317 * 318 * btf_dump_order_type() is trying to avoid unnecessary forward declarations, 319 * but it is not guaranteeing that no extraneous forward declarations will be 320 * emitted. 321 * 322 * To avoid extra work, algorithm marks some of BTF types as ORDERED, when 323 * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT, 324 * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the 325 * entire graph path, so depending where from one came to that BTF type, it 326 * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM, 327 * once they are processed, there is no need to do it again, so they are 328 * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces 329 * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But 330 * in any case, once those are processed, no need to do it again, as the 331 * result won't change. 332 * 333 * Returns: 334 * - 1, if type is part of strong link (so there is strong topological 335 * ordering requirements); 336 * - 0, if type is part of weak link (so can be satisfied through forward 337 * declaration); 338 * - <0, on error (e.g., unsatisfiable type loop detected). 339 */ 340 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr) 341 { 342 /* 343 * Order state is used to detect strong link cycles, but only for BTF 344 * kinds that are or could be an independent definition (i.e., 345 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays, 346 * func_protos, modifiers are just means to get to these definitions. 347 * Int/void don't need definitions, they are assumed to be always 348 * properly defined. We also ignore datasec, var, and funcs for now. 349 * So for all non-defining kinds, we never even set ordering state, 350 * for defining kinds we set ORDERING and subsequently ORDERED if it 351 * forms a strong link. 352 */ 353 struct btf_dump_type_aux_state *tstate = &d->type_states[id]; 354 const struct btf_type *t; 355 __u16 kind, vlen; 356 int err, i; 357 358 /* return true, letting typedefs know that it's ok to be emitted */ 359 if (tstate->order_state == ORDERED) 360 return 1; 361 362 t = btf__type_by_id(d->btf, id); 363 kind = btf_kind_of(t); 364 365 if (tstate->order_state == ORDERING) { 366 /* type loop, but resolvable through fwd declaration */ 367 if ((kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION) && 368 through_ptr && t->name_off != 0) 369 return 0; 370 pr_warning("unsatisfiable type cycle, id:[%u]\n", id); 371 return -ELOOP; 372 } 373 374 switch (kind) { 375 case BTF_KIND_INT: 376 tstate->order_state = ORDERED; 377 return 0; 378 379 case BTF_KIND_PTR: 380 err = btf_dump_order_type(d, t->type, true); 381 tstate->order_state = ORDERED; 382 return err; 383 384 case BTF_KIND_ARRAY: { 385 const struct btf_array *a = (void *)(t + 1); 386 387 return btf_dump_order_type(d, a->type, through_ptr); 388 } 389 case BTF_KIND_STRUCT: 390 case BTF_KIND_UNION: { 391 const struct btf_member *m = (void *)(t + 1); 392 /* 393 * struct/union is part of strong link, only if it's embedded 394 * (so no ptr in a path) or it's anonymous (so has to be 395 * defined inline, even if declared through ptr) 396 */ 397 if (through_ptr && t->name_off != 0) 398 return 0; 399 400 tstate->order_state = ORDERING; 401 402 vlen = btf_vlen_of(t); 403 for (i = 0; i < vlen; i++, m++) { 404 err = btf_dump_order_type(d, m->type, false); 405 if (err < 0) 406 return err; 407 } 408 409 if (t->name_off != 0) { 410 err = btf_dump_add_emit_queue_id(d, id); 411 if (err < 0) 412 return err; 413 } 414 415 tstate->order_state = ORDERED; 416 return 1; 417 } 418 case BTF_KIND_ENUM: 419 case BTF_KIND_FWD: 420 if (t->name_off != 0) { 421 err = btf_dump_add_emit_queue_id(d, id); 422 if (err) 423 return err; 424 } 425 tstate->order_state = ORDERED; 426 return 1; 427 428 case BTF_KIND_TYPEDEF: { 429 int is_strong; 430 431 is_strong = btf_dump_order_type(d, t->type, through_ptr); 432 if (is_strong < 0) 433 return is_strong; 434 435 /* typedef is similar to struct/union w.r.t. fwd-decls */ 436 if (through_ptr && !is_strong) 437 return 0; 438 439 /* typedef is always a named definition */ 440 err = btf_dump_add_emit_queue_id(d, id); 441 if (err) 442 return err; 443 444 d->type_states[id].order_state = ORDERED; 445 return 1; 446 } 447 case BTF_KIND_VOLATILE: 448 case BTF_KIND_CONST: 449 case BTF_KIND_RESTRICT: 450 return btf_dump_order_type(d, t->type, through_ptr); 451 452 case BTF_KIND_FUNC_PROTO: { 453 const struct btf_param *p = (void *)(t + 1); 454 bool is_strong; 455 456 err = btf_dump_order_type(d, t->type, through_ptr); 457 if (err < 0) 458 return err; 459 is_strong = err > 0; 460 461 vlen = btf_vlen_of(t); 462 for (i = 0; i < vlen; i++, p++) { 463 err = btf_dump_order_type(d, p->type, through_ptr); 464 if (err < 0) 465 return err; 466 if (err > 0) 467 is_strong = true; 468 } 469 return is_strong; 470 } 471 case BTF_KIND_FUNC: 472 case BTF_KIND_VAR: 473 case BTF_KIND_DATASEC: 474 d->type_states[id].order_state = ORDERED; 475 return 0; 476 477 default: 478 return -EINVAL; 479 } 480 } 481 482 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id, 483 const struct btf_type *t); 484 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id, 485 const struct btf_type *t, int lvl); 486 487 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, 488 const struct btf_type *t); 489 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id, 490 const struct btf_type *t, int lvl); 491 492 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id, 493 const struct btf_type *t); 494 495 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id, 496 const struct btf_type *t, int lvl); 497 498 /* a local view into a shared stack */ 499 struct id_stack { 500 const __u32 *ids; 501 int cnt; 502 }; 503 504 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id, 505 const char *fname, int lvl); 506 static void btf_dump_emit_type_chain(struct btf_dump *d, 507 struct id_stack *decl_stack, 508 const char *fname, int lvl); 509 510 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id); 511 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id); 512 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map, 513 const char *orig_name); 514 515 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id) 516 { 517 const struct btf_type *t = btf__type_by_id(d->btf, id); 518 519 /* __builtin_va_list is a compiler built-in, which causes compilation 520 * errors, when compiling w/ different compiler, then used to compile 521 * original code (e.g., GCC to compile kernel, Clang to use generated 522 * C header from BTF). As it is built-in, it should be already defined 523 * properly internally in compiler. 524 */ 525 if (t->name_off == 0) 526 return false; 527 return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0; 528 } 529 530 /* 531 * Emit C-syntax definitions of types from chains of BTF types. 532 * 533 * High-level handling of determining necessary forward declarations are handled 534 * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type 535 * declarations/definitions in C syntax are handled by a combo of 536 * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to 537 * corresponding btf_dump_emit_*_{def,fwd}() functions. 538 * 539 * We also keep track of "containing struct/union type ID" to determine when 540 * we reference it from inside and thus can avoid emitting unnecessary forward 541 * declaration. 542 * 543 * This algorithm is designed in such a way, that even if some error occurs 544 * (either technical, e.g., out of memory, or logical, i.e., malformed BTF 545 * that doesn't comply to C rules completely), algorithm will try to proceed 546 * and produce as much meaningful output as possible. 547 */ 548 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) 549 { 550 struct btf_dump_type_aux_state *tstate = &d->type_states[id]; 551 bool top_level_def = cont_id == 0; 552 const struct btf_type *t; 553 __u16 kind; 554 555 if (tstate->emit_state == EMITTED) 556 return; 557 558 t = btf__type_by_id(d->btf, id); 559 kind = btf_kind_of(t); 560 561 if (top_level_def && t->name_off == 0) { 562 pr_warning("unexpected nameless definition, id:[%u]\n", id); 563 return; 564 } 565 566 if (tstate->emit_state == EMITTING) { 567 if (tstate->fwd_emitted) 568 return; 569 570 switch (kind) { 571 case BTF_KIND_STRUCT: 572 case BTF_KIND_UNION: 573 /* 574 * if we are referencing a struct/union that we are 575 * part of - then no need for fwd declaration 576 */ 577 if (id == cont_id) 578 return; 579 if (t->name_off == 0) { 580 pr_warning("anonymous struct/union loop, id:[%u]\n", 581 id); 582 return; 583 } 584 btf_dump_emit_struct_fwd(d, id, t); 585 btf_dump_printf(d, ";\n\n"); 586 tstate->fwd_emitted = 1; 587 break; 588 case BTF_KIND_TYPEDEF: 589 /* 590 * for typedef fwd_emitted means typedef definition 591 * was emitted, but it can be used only for "weak" 592 * references through pointer only, not for embedding 593 */ 594 if (!btf_dump_is_blacklisted(d, id)) { 595 btf_dump_emit_typedef_def(d, id, t, 0); 596 btf_dump_printf(d, ";\n\n"); 597 }; 598 tstate->fwd_emitted = 1; 599 break; 600 default: 601 break; 602 } 603 604 return; 605 } 606 607 switch (kind) { 608 case BTF_KIND_INT: 609 tstate->emit_state = EMITTED; 610 break; 611 case BTF_KIND_ENUM: 612 if (top_level_def) { 613 btf_dump_emit_enum_def(d, id, t, 0); 614 btf_dump_printf(d, ";\n\n"); 615 } 616 tstate->emit_state = EMITTED; 617 break; 618 case BTF_KIND_PTR: 619 case BTF_KIND_VOLATILE: 620 case BTF_KIND_CONST: 621 case BTF_KIND_RESTRICT: 622 btf_dump_emit_type(d, t->type, cont_id); 623 break; 624 case BTF_KIND_ARRAY: { 625 const struct btf_array *a = (void *)(t + 1); 626 627 btf_dump_emit_type(d, a->type, cont_id); 628 break; 629 } 630 case BTF_KIND_FWD: 631 btf_dump_emit_fwd_def(d, id, t); 632 btf_dump_printf(d, ";\n\n"); 633 tstate->emit_state = EMITTED; 634 break; 635 case BTF_KIND_TYPEDEF: 636 tstate->emit_state = EMITTING; 637 btf_dump_emit_type(d, t->type, id); 638 /* 639 * typedef can server as both definition and forward 640 * declaration; at this stage someone depends on 641 * typedef as a forward declaration (refers to it 642 * through pointer), so unless we already did it, 643 * emit typedef as a forward declaration 644 */ 645 if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) { 646 btf_dump_emit_typedef_def(d, id, t, 0); 647 btf_dump_printf(d, ";\n\n"); 648 } 649 tstate->emit_state = EMITTED; 650 break; 651 case BTF_KIND_STRUCT: 652 case BTF_KIND_UNION: 653 tstate->emit_state = EMITTING; 654 /* if it's a top-level struct/union definition or struct/union 655 * is anonymous, then in C we'll be emitting all fields and 656 * their types (as opposed to just `struct X`), so we need to 657 * make sure that all types, referenced from struct/union 658 * members have necessary forward-declarations, where 659 * applicable 660 */ 661 if (top_level_def || t->name_off == 0) { 662 const struct btf_member *m = (void *)(t + 1); 663 __u16 vlen = btf_vlen_of(t); 664 int i, new_cont_id; 665 666 new_cont_id = t->name_off == 0 ? cont_id : id; 667 for (i = 0; i < vlen; i++, m++) 668 btf_dump_emit_type(d, m->type, new_cont_id); 669 } else if (!tstate->fwd_emitted && id != cont_id) { 670 btf_dump_emit_struct_fwd(d, id, t); 671 btf_dump_printf(d, ";\n\n"); 672 tstate->fwd_emitted = 1; 673 } 674 675 if (top_level_def) { 676 btf_dump_emit_struct_def(d, id, t, 0); 677 btf_dump_printf(d, ";\n\n"); 678 tstate->emit_state = EMITTED; 679 } else { 680 tstate->emit_state = NOT_EMITTED; 681 } 682 break; 683 case BTF_KIND_FUNC_PROTO: { 684 const struct btf_param *p = (void *)(t + 1); 685 __u16 vlen = btf_vlen_of(t); 686 int i; 687 688 btf_dump_emit_type(d, t->type, cont_id); 689 for (i = 0; i < vlen; i++, p++) 690 btf_dump_emit_type(d, p->type, cont_id); 691 692 break; 693 } 694 default: 695 break; 696 } 697 } 698 699 static int btf_align_of(const struct btf *btf, __u32 id) 700 { 701 const struct btf_type *t = btf__type_by_id(btf, id); 702 __u16 kind = btf_kind_of(t); 703 704 switch (kind) { 705 case BTF_KIND_INT: 706 case BTF_KIND_ENUM: 707 return min(sizeof(void *), t->size); 708 case BTF_KIND_PTR: 709 return sizeof(void *); 710 case BTF_KIND_TYPEDEF: 711 case BTF_KIND_VOLATILE: 712 case BTF_KIND_CONST: 713 case BTF_KIND_RESTRICT: 714 return btf_align_of(btf, t->type); 715 case BTF_KIND_ARRAY: { 716 const struct btf_array *a = (void *)(t + 1); 717 718 return btf_align_of(btf, a->type); 719 } 720 case BTF_KIND_STRUCT: 721 case BTF_KIND_UNION: { 722 const struct btf_member *m = (void *)(t + 1); 723 __u16 vlen = btf_vlen_of(t); 724 int i, align = 1; 725 726 for (i = 0; i < vlen; i++, m++) 727 align = max(align, btf_align_of(btf, m->type)); 728 729 return align; 730 } 731 default: 732 pr_warning("unsupported BTF_KIND:%u\n", btf_kind_of(t)); 733 return 1; 734 } 735 } 736 737 static bool btf_is_struct_packed(const struct btf *btf, __u32 id, 738 const struct btf_type *t) 739 { 740 const struct btf_member *m; 741 int align, i, bit_sz; 742 __u16 vlen; 743 bool kflag; 744 745 align = btf_align_of(btf, id); 746 /* size of a non-packed struct has to be a multiple of its alignment*/ 747 if (t->size % align) 748 return true; 749 750 m = (void *)(t + 1); 751 kflag = btf_kflag_of(t); 752 vlen = btf_vlen_of(t); 753 /* all non-bitfield fields have to be naturally aligned */ 754 for (i = 0; i < vlen; i++, m++) { 755 align = btf_align_of(btf, m->type); 756 bit_sz = kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0; 757 if (bit_sz == 0 && m->offset % (8 * align) != 0) 758 return true; 759 } 760 761 /* 762 * if original struct was marked as packed, but its layout is 763 * naturally aligned, we'll detect that it's not packed 764 */ 765 return false; 766 } 767 768 static int chip_away_bits(int total, int at_most) 769 { 770 return total % at_most ? : at_most; 771 } 772 773 static void btf_dump_emit_bit_padding(const struct btf_dump *d, 774 int cur_off, int m_off, int m_bit_sz, 775 int align, int lvl) 776 { 777 int off_diff = m_off - cur_off; 778 int ptr_bits = sizeof(void *) * 8; 779 780 if (off_diff <= 0) 781 /* no gap */ 782 return; 783 if (m_bit_sz == 0 && off_diff < align * 8) 784 /* natural padding will take care of a gap */ 785 return; 786 787 while (off_diff > 0) { 788 const char *pad_type; 789 int pad_bits; 790 791 if (ptr_bits > 32 && off_diff > 32) { 792 pad_type = "long"; 793 pad_bits = chip_away_bits(off_diff, ptr_bits); 794 } else if (off_diff > 16) { 795 pad_type = "int"; 796 pad_bits = chip_away_bits(off_diff, 32); 797 } else if (off_diff > 8) { 798 pad_type = "short"; 799 pad_bits = chip_away_bits(off_diff, 16); 800 } else { 801 pad_type = "char"; 802 pad_bits = chip_away_bits(off_diff, 8); 803 } 804 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits); 805 off_diff -= pad_bits; 806 } 807 } 808 809 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id, 810 const struct btf_type *t) 811 { 812 btf_dump_printf(d, "%s %s", 813 btf_kind_of(t) == BTF_KIND_STRUCT ? "struct" : "union", 814 btf_dump_type_name(d, id)); 815 } 816 817 static void btf_dump_emit_struct_def(struct btf_dump *d, 818 __u32 id, 819 const struct btf_type *t, 820 int lvl) 821 { 822 const struct btf_member *m = (void *)(t + 1); 823 bool kflag = btf_kflag_of(t), is_struct; 824 int align, i, packed, off = 0; 825 __u16 vlen = btf_vlen_of(t); 826 827 is_struct = btf_kind_of(t) == BTF_KIND_STRUCT; 828 packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0; 829 align = packed ? 1 : btf_align_of(d->btf, id); 830 831 btf_dump_printf(d, "%s%s%s {", 832 is_struct ? "struct" : "union", 833 t->name_off ? " " : "", 834 btf_dump_type_name(d, id)); 835 836 for (i = 0; i < vlen; i++, m++) { 837 const char *fname; 838 int m_off, m_sz; 839 840 fname = btf_name_of(d, m->name_off); 841 m_sz = kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0; 842 m_off = kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset; 843 align = packed ? 1 : btf_align_of(d->btf, m->type); 844 845 btf_dump_emit_bit_padding(d, off, m_off, m_sz, align, lvl + 1); 846 btf_dump_printf(d, "\n%s", pfx(lvl + 1)); 847 btf_dump_emit_type_decl(d, m->type, fname, lvl + 1); 848 849 if (m_sz) { 850 btf_dump_printf(d, ": %d", m_sz); 851 off = m_off + m_sz; 852 } else { 853 m_sz = max(0, btf__resolve_size(d->btf, m->type)); 854 off = m_off + m_sz * 8; 855 } 856 btf_dump_printf(d, ";"); 857 } 858 859 if (vlen) 860 btf_dump_printf(d, "\n"); 861 btf_dump_printf(d, "%s}", pfx(lvl)); 862 if (packed) 863 btf_dump_printf(d, " __attribute__((packed))"); 864 } 865 866 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id, 867 const struct btf_type *t) 868 { 869 btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id)); 870 } 871 872 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id, 873 const struct btf_type *t, 874 int lvl) 875 { 876 const struct btf_enum *v = (void *)(t+1); 877 __u16 vlen = btf_vlen_of(t); 878 const char *name; 879 size_t dup_cnt; 880 int i; 881 882 btf_dump_printf(d, "enum%s%s", 883 t->name_off ? " " : "", 884 btf_dump_type_name(d, id)); 885 886 if (vlen) { 887 btf_dump_printf(d, " {"); 888 for (i = 0; i < vlen; i++, v++) { 889 name = btf_name_of(d, v->name_off); 890 /* enumerators share namespace with typedef idents */ 891 dup_cnt = btf_dump_name_dups(d, d->ident_names, name); 892 if (dup_cnt > 1) { 893 btf_dump_printf(d, "\n%s%s___%zu = %d,", 894 pfx(lvl + 1), name, dup_cnt, 895 (__s32)v->val); 896 } else { 897 btf_dump_printf(d, "\n%s%s = %d,", 898 pfx(lvl + 1), name, 899 (__s32)v->val); 900 } 901 } 902 btf_dump_printf(d, "\n%s}", pfx(lvl)); 903 } 904 } 905 906 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id, 907 const struct btf_type *t) 908 { 909 const char *name = btf_dump_type_name(d, id); 910 911 if (btf_kflag_of(t)) 912 btf_dump_printf(d, "union %s", name); 913 else 914 btf_dump_printf(d, "struct %s", name); 915 } 916 917 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id, 918 const struct btf_type *t, int lvl) 919 { 920 const char *name = btf_dump_ident_name(d, id); 921 922 btf_dump_printf(d, "typedef "); 923 btf_dump_emit_type_decl(d, t->type, name, lvl); 924 } 925 926 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id) 927 { 928 __u32 *new_stack; 929 size_t new_cap; 930 931 if (d->decl_stack_cnt >= d->decl_stack_cap) { 932 new_cap = max(16, d->decl_stack_cap * 3 / 2); 933 new_stack = realloc(d->decl_stack, 934 new_cap * sizeof(new_stack[0])); 935 if (!new_stack) 936 return -ENOMEM; 937 d->decl_stack = new_stack; 938 d->decl_stack_cap = new_cap; 939 } 940 941 d->decl_stack[d->decl_stack_cnt++] = id; 942 943 return 0; 944 } 945 946 /* 947 * Emit type declaration (e.g., field type declaration in a struct or argument 948 * declaration in function prototype) in correct C syntax. 949 * 950 * For most types it's trivial, but there are few quirky type declaration 951 * cases worth mentioning: 952 * - function prototypes (especially nesting of function prototypes); 953 * - arrays; 954 * - const/volatile/restrict for pointers vs other types. 955 * 956 * For a good discussion of *PARSING* C syntax (as a human), see 957 * Peter van der Linden's "Expert C Programming: Deep C Secrets", 958 * Ch.3 "Unscrambling Declarations in C". 959 * 960 * It won't help with BTF to C conversion much, though, as it's an opposite 961 * problem. So we came up with this algorithm in reverse to van der Linden's 962 * parsing algorithm. It goes from structured BTF representation of type 963 * declaration to a valid compilable C syntax. 964 * 965 * For instance, consider this C typedef: 966 * typedef const int * const * arr[10] arr_t; 967 * It will be represented in BTF with this chain of BTF types: 968 * [typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int] 969 * 970 * Notice how [const] modifier always goes before type it modifies in BTF type 971 * graph, but in C syntax, const/volatile/restrict modifiers are written to 972 * the right of pointers, but to the left of other types. There are also other 973 * quirks, like function pointers, arrays of them, functions returning other 974 * functions, etc. 975 * 976 * We handle that by pushing all the types to a stack, until we hit "terminal" 977 * type (int/enum/struct/union/fwd). Then depending on the kind of a type on 978 * top of a stack, modifiers are handled differently. Array/function pointers 979 * have also wildly different syntax and how nesting of them are done. See 980 * code for authoritative definition. 981 * 982 * To avoid allocating new stack for each independent chain of BTF types, we 983 * share one bigger stack, with each chain working only on its own local view 984 * of a stack frame. Some care is required to "pop" stack frames after 985 * processing type declaration chain. 986 */ 987 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id, 988 const char *fname, int lvl) 989 { 990 struct id_stack decl_stack; 991 const struct btf_type *t; 992 int err, stack_start; 993 __u16 kind; 994 995 stack_start = d->decl_stack_cnt; 996 for (;;) { 997 err = btf_dump_push_decl_stack_id(d, id); 998 if (err < 0) { 999 /* 1000 * if we don't have enough memory for entire type decl 1001 * chain, restore stack, emit warning, and try to 1002 * proceed nevertheless 1003 */ 1004 pr_warning("not enough memory for decl stack:%d", err); 1005 d->decl_stack_cnt = stack_start; 1006 return; 1007 } 1008 1009 /* VOID */ 1010 if (id == 0) 1011 break; 1012 1013 t = btf__type_by_id(d->btf, id); 1014 kind = btf_kind_of(t); 1015 switch (kind) { 1016 case BTF_KIND_PTR: 1017 case BTF_KIND_VOLATILE: 1018 case BTF_KIND_CONST: 1019 case BTF_KIND_RESTRICT: 1020 case BTF_KIND_FUNC_PROTO: 1021 id = t->type; 1022 break; 1023 case BTF_KIND_ARRAY: { 1024 const struct btf_array *a = (void *)(t + 1); 1025 1026 id = a->type; 1027 break; 1028 } 1029 case BTF_KIND_INT: 1030 case BTF_KIND_ENUM: 1031 case BTF_KIND_FWD: 1032 case BTF_KIND_STRUCT: 1033 case BTF_KIND_UNION: 1034 case BTF_KIND_TYPEDEF: 1035 goto done; 1036 default: 1037 pr_warning("unexpected type in decl chain, kind:%u, id:[%u]\n", 1038 kind, id); 1039 goto done; 1040 } 1041 } 1042 done: 1043 /* 1044 * We might be inside a chain of declarations (e.g., array of function 1045 * pointers returning anonymous (so inlined) structs, having another 1046 * array field). Each of those needs its own "stack frame" to handle 1047 * emitting of declarations. Those stack frames are non-overlapping 1048 * portions of shared btf_dump->decl_stack. To make it a bit nicer to 1049 * handle this set of nested stacks, we create a view corresponding to 1050 * our own "stack frame" and work with it as an independent stack. 1051 * We'll need to clean up after emit_type_chain() returns, though. 1052 */ 1053 decl_stack.ids = d->decl_stack + stack_start; 1054 decl_stack.cnt = d->decl_stack_cnt - stack_start; 1055 btf_dump_emit_type_chain(d, &decl_stack, fname, lvl); 1056 /* 1057 * emit_type_chain() guarantees that it will pop its entire decl_stack 1058 * frame before returning. But it works with a read-only view into 1059 * decl_stack, so it doesn't actually pop anything from the 1060 * perspective of shared btf_dump->decl_stack, per se. We need to 1061 * reset decl_stack state to how it was before us to avoid it growing 1062 * all the time. 1063 */ 1064 d->decl_stack_cnt = stack_start; 1065 } 1066 1067 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack) 1068 { 1069 const struct btf_type *t; 1070 __u32 id; 1071 1072 while (decl_stack->cnt) { 1073 id = decl_stack->ids[decl_stack->cnt - 1]; 1074 t = btf__type_by_id(d->btf, id); 1075 1076 switch (btf_kind_of(t)) { 1077 case BTF_KIND_VOLATILE: 1078 btf_dump_printf(d, "volatile "); 1079 break; 1080 case BTF_KIND_CONST: 1081 btf_dump_printf(d, "const "); 1082 break; 1083 case BTF_KIND_RESTRICT: 1084 btf_dump_printf(d, "restrict "); 1085 break; 1086 default: 1087 return; 1088 } 1089 decl_stack->cnt--; 1090 } 1091 } 1092 1093 static bool btf_is_mod_kind(const struct btf *btf, __u32 id) 1094 { 1095 const struct btf_type *t = btf__type_by_id(btf, id); 1096 1097 switch (btf_kind_of(t)) { 1098 case BTF_KIND_VOLATILE: 1099 case BTF_KIND_CONST: 1100 case BTF_KIND_RESTRICT: 1101 return true; 1102 default: 1103 return false; 1104 } 1105 } 1106 1107 static void btf_dump_emit_name(const struct btf_dump *d, 1108 const char *name, bool last_was_ptr) 1109 { 1110 bool separate = name[0] && !last_was_ptr; 1111 1112 btf_dump_printf(d, "%s%s", separate ? " " : "", name); 1113 } 1114 1115 static void btf_dump_emit_type_chain(struct btf_dump *d, 1116 struct id_stack *decls, 1117 const char *fname, int lvl) 1118 { 1119 /* 1120 * last_was_ptr is used to determine if we need to separate pointer 1121 * asterisk (*) from previous part of type signature with space, so 1122 * that we get `int ***`, instead of `int * * *`. We default to true 1123 * for cases where we have single pointer in a chain. E.g., in ptr -> 1124 * func_proto case. func_proto will start a new emit_type_chain call 1125 * with just ptr, which should be emitted as (*) or (*<fname>), so we 1126 * don't want to prepend space for that last pointer. 1127 */ 1128 bool last_was_ptr = true; 1129 const struct btf_type *t; 1130 const char *name; 1131 __u16 kind; 1132 __u32 id; 1133 1134 while (decls->cnt) { 1135 id = decls->ids[--decls->cnt]; 1136 if (id == 0) { 1137 /* VOID is a special snowflake */ 1138 btf_dump_emit_mods(d, decls); 1139 btf_dump_printf(d, "void"); 1140 last_was_ptr = false; 1141 continue; 1142 } 1143 1144 t = btf__type_by_id(d->btf, id); 1145 kind = btf_kind_of(t); 1146 1147 switch (kind) { 1148 case BTF_KIND_INT: 1149 btf_dump_emit_mods(d, decls); 1150 name = btf_name_of(d, t->name_off); 1151 btf_dump_printf(d, "%s", name); 1152 break; 1153 case BTF_KIND_STRUCT: 1154 case BTF_KIND_UNION: 1155 btf_dump_emit_mods(d, decls); 1156 /* inline anonymous struct/union */ 1157 if (t->name_off == 0) 1158 btf_dump_emit_struct_def(d, id, t, lvl); 1159 else 1160 btf_dump_emit_struct_fwd(d, id, t); 1161 break; 1162 case BTF_KIND_ENUM: 1163 btf_dump_emit_mods(d, decls); 1164 /* inline anonymous enum */ 1165 if (t->name_off == 0) 1166 btf_dump_emit_enum_def(d, id, t, lvl); 1167 else 1168 btf_dump_emit_enum_fwd(d, id, t); 1169 break; 1170 case BTF_KIND_FWD: 1171 btf_dump_emit_mods(d, decls); 1172 btf_dump_emit_fwd_def(d, id, t); 1173 break; 1174 case BTF_KIND_TYPEDEF: 1175 btf_dump_emit_mods(d, decls); 1176 btf_dump_printf(d, "%s", btf_dump_ident_name(d, id)); 1177 break; 1178 case BTF_KIND_PTR: 1179 btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *"); 1180 break; 1181 case BTF_KIND_VOLATILE: 1182 btf_dump_printf(d, " volatile"); 1183 break; 1184 case BTF_KIND_CONST: 1185 btf_dump_printf(d, " const"); 1186 break; 1187 case BTF_KIND_RESTRICT: 1188 btf_dump_printf(d, " restrict"); 1189 break; 1190 case BTF_KIND_ARRAY: { 1191 const struct btf_array *a = (void *)(t + 1); 1192 const struct btf_type *next_t; 1193 __u32 next_id; 1194 bool multidim; 1195 /* 1196 * GCC has a bug 1197 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354) 1198 * which causes it to emit extra const/volatile 1199 * modifiers for an array, if array's element type has 1200 * const/volatile modifiers. Clang doesn't do that. 1201 * In general, it doesn't seem very meaningful to have 1202 * a const/volatile modifier for array, so we are 1203 * going to silently skip them here. 1204 */ 1205 while (decls->cnt) { 1206 next_id = decls->ids[decls->cnt - 1]; 1207 if (btf_is_mod_kind(d->btf, next_id)) 1208 decls->cnt--; 1209 else 1210 break; 1211 } 1212 1213 if (decls->cnt == 0) { 1214 btf_dump_emit_name(d, fname, last_was_ptr); 1215 btf_dump_printf(d, "[%u]", a->nelems); 1216 return; 1217 } 1218 1219 next_t = btf__type_by_id(d->btf, next_id); 1220 multidim = btf_kind_of(next_t) == BTF_KIND_ARRAY; 1221 /* we need space if we have named non-pointer */ 1222 if (fname[0] && !last_was_ptr) 1223 btf_dump_printf(d, " "); 1224 /* no parentheses for multi-dimensional array */ 1225 if (!multidim) 1226 btf_dump_printf(d, "("); 1227 btf_dump_emit_type_chain(d, decls, fname, lvl); 1228 if (!multidim) 1229 btf_dump_printf(d, ")"); 1230 btf_dump_printf(d, "[%u]", a->nelems); 1231 return; 1232 } 1233 case BTF_KIND_FUNC_PROTO: { 1234 const struct btf_param *p = (void *)(t + 1); 1235 __u16 vlen = btf_vlen_of(t); 1236 int i; 1237 1238 btf_dump_emit_mods(d, decls); 1239 if (decls->cnt) { 1240 btf_dump_printf(d, " ("); 1241 btf_dump_emit_type_chain(d, decls, fname, lvl); 1242 btf_dump_printf(d, ")"); 1243 } else { 1244 btf_dump_emit_name(d, fname, last_was_ptr); 1245 } 1246 btf_dump_printf(d, "("); 1247 /* 1248 * Clang for BPF target generates func_proto with no 1249 * args as a func_proto with a single void arg (e.g., 1250 * `int (*f)(void)` vs just `int (*f)()`). We are 1251 * going to pretend there are no args for such case. 1252 */ 1253 if (vlen == 1 && p->type == 0) { 1254 btf_dump_printf(d, ")"); 1255 return; 1256 } 1257 1258 for (i = 0; i < vlen; i++, p++) { 1259 if (i > 0) 1260 btf_dump_printf(d, ", "); 1261 1262 /* last arg of type void is vararg */ 1263 if (i == vlen - 1 && p->type == 0) { 1264 btf_dump_printf(d, "..."); 1265 break; 1266 } 1267 1268 name = btf_name_of(d, p->name_off); 1269 btf_dump_emit_type_decl(d, p->type, name, lvl); 1270 } 1271 1272 btf_dump_printf(d, ")"); 1273 return; 1274 } 1275 default: 1276 pr_warning("unexpected type in decl chain, kind:%u, id:[%u]\n", 1277 kind, id); 1278 return; 1279 } 1280 1281 last_was_ptr = kind == BTF_KIND_PTR; 1282 } 1283 1284 btf_dump_emit_name(d, fname, last_was_ptr); 1285 } 1286 1287 /* return number of duplicates (occurrences) of a given name */ 1288 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map, 1289 const char *orig_name) 1290 { 1291 size_t dup_cnt = 0; 1292 1293 hashmap__find(name_map, orig_name, (void **)&dup_cnt); 1294 dup_cnt++; 1295 hashmap__set(name_map, orig_name, (void *)dup_cnt, NULL, NULL); 1296 1297 return dup_cnt; 1298 } 1299 1300 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id, 1301 struct hashmap *name_map) 1302 { 1303 struct btf_dump_type_aux_state *s = &d->type_states[id]; 1304 const struct btf_type *t = btf__type_by_id(d->btf, id); 1305 const char *orig_name = btf_name_of(d, t->name_off); 1306 const char **cached_name = &d->cached_names[id]; 1307 size_t dup_cnt; 1308 1309 if (t->name_off == 0) 1310 return ""; 1311 1312 if (s->name_resolved) 1313 return *cached_name ? *cached_name : orig_name; 1314 1315 dup_cnt = btf_dump_name_dups(d, name_map, orig_name); 1316 if (dup_cnt > 1) { 1317 const size_t max_len = 256; 1318 char new_name[max_len]; 1319 1320 snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt); 1321 *cached_name = strdup(new_name); 1322 } 1323 1324 s->name_resolved = 1; 1325 return *cached_name ? *cached_name : orig_name; 1326 } 1327 1328 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id) 1329 { 1330 return btf_dump_resolve_name(d, id, d->type_names); 1331 } 1332 1333 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id) 1334 { 1335 return btf_dump_resolve_name(d, id, d->ident_names); 1336 } 1337