dwarf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Google LLC
  4. */
  5. #define _GNU_SOURCE
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdarg.h>
  9. #include "gendwarfksyms.h"
  10. /* See get_union_kabi_status */
  11. #define KABI_PREFIX "__kabi_"
  12. #define KABI_PREFIX_LEN (sizeof(KABI_PREFIX) - 1)
  13. #define KABI_RESERVED_PREFIX "reserved"
  14. #define KABI_RESERVED_PREFIX_LEN (sizeof(KABI_RESERVED_PREFIX) - 1)
  15. #define KABI_RENAMED_PREFIX "renamed"
  16. #define KABI_RENAMED_PREFIX_LEN (sizeof(KABI_RENAMED_PREFIX) - 1)
  17. #define KABI_IGNORED_PREFIX "ignored"
  18. #define KABI_IGNORED_PREFIX_LEN (sizeof(KABI_IGNORED_PREFIX) - 1)
  19. static inline bool is_kabi_prefix(const char *name)
  20. {
  21. return name && !strncmp(name, KABI_PREFIX, KABI_PREFIX_LEN);
  22. }
  23. enum kabi_status {
  24. /* >0 to stop DIE processing */
  25. KABI_NORMAL = 1,
  26. KABI_RESERVED,
  27. KABI_IGNORED,
  28. };
  29. static bool do_linebreak;
  30. static int indentation_level;
  31. /* Line breaks and indentation for pretty-printing */
  32. static void process_linebreak(struct die *cache, int n)
  33. {
  34. indentation_level += n;
  35. do_linebreak = true;
  36. die_map_add_linebreak(cache, n);
  37. }
  38. #define DEFINE_GET_ATTR(attr, type) \
  39. static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \
  40. type *value) \
  41. { \
  42. Dwarf_Attribute da; \
  43. return dwarf_attr(die, id, &da) && \
  44. !dwarf_form##attr(&da, value); \
  45. }
  46. DEFINE_GET_ATTR(flag, bool)
  47. DEFINE_GET_ATTR(udata, Dwarf_Word)
  48. static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value)
  49. {
  50. Dwarf_Attribute da;
  51. /* dwarf_formref_die returns a pointer instead of an error value. */
  52. return dwarf_attr(die, id, &da) && dwarf_formref_die(&da, value);
  53. }
  54. #define DEFINE_GET_STRING_ATTR(attr) \
  55. static const char *get_##attr##_attr(Dwarf_Die *die) \
  56. { \
  57. Dwarf_Attribute da; \
  58. if (dwarf_attr(die, DW_AT_##attr, &da)) \
  59. return dwarf_formstring(&da); \
  60. return NULL; \
  61. }
  62. DEFINE_GET_STRING_ATTR(name)
  63. DEFINE_GET_STRING_ATTR(linkage_name)
  64. static const char *get_symbol_name(Dwarf_Die *die)
  65. {
  66. const char *name;
  67. /* rustc uses DW_AT_linkage_name for exported symbols */
  68. name = get_linkage_name_attr(die);
  69. if (!name)
  70. name = get_name_attr(die);
  71. return name;
  72. }
  73. static bool match_export_symbol(struct state *state, Dwarf_Die *die)
  74. {
  75. Dwarf_Die *source = die;
  76. Dwarf_Die origin;
  77. /* If the DIE has an abstract origin, use it for type information. */
  78. if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin))
  79. source = &origin;
  80. state->sym = symbol_get(get_symbol_name(die));
  81. /* Look up using the origin name if there are no matches. */
  82. if (!state->sym && source != die)
  83. state->sym = symbol_get(get_symbol_name(source));
  84. state->die = *source;
  85. return !!state->sym;
  86. }
  87. /* DW_AT_decl_file -> struct srcfile */
  88. static struct cache srcfile_cache;
  89. static bool is_definition_private(Dwarf_Die *die)
  90. {
  91. Dwarf_Word filenum;
  92. Dwarf_Files *files;
  93. Dwarf_Die cudie;
  94. const char *s;
  95. int res;
  96. /*
  97. * Definitions in .c files cannot change the public ABI,
  98. * so consider them private.
  99. */
  100. if (!get_udata_attr(die, DW_AT_decl_file, &filenum))
  101. return false;
  102. res = cache_get(&srcfile_cache, filenum);
  103. if (res >= 0)
  104. return !!res;
  105. if (!dwarf_cu_die(die->cu, &cudie, NULL, NULL, NULL, NULL, NULL, NULL))
  106. error("dwarf_cu_die failed: '%s'", dwarf_errmsg(-1));
  107. if (dwarf_getsrcfiles(&cudie, &files, NULL))
  108. error("dwarf_getsrcfiles failed: '%s'", dwarf_errmsg(-1));
  109. s = dwarf_filesrc(files, filenum, NULL, NULL);
  110. if (!s)
  111. error("dwarf_filesrc failed: '%s'", dwarf_errmsg(-1));
  112. s = strrchr(s, '.');
  113. res = s && !strcmp(s, ".c");
  114. cache_set(&srcfile_cache, filenum, res);
  115. return !!res;
  116. }
  117. static bool is_kabi_definition(struct die *cache, Dwarf_Die *die)
  118. {
  119. bool value;
  120. if (get_flag_attr(die, DW_AT_declaration, &value) && value)
  121. return false;
  122. if (kabi_is_declonly(cache->fqn))
  123. return false;
  124. return !is_definition_private(die);
  125. }
  126. /*
  127. * Type string processing
  128. */
  129. static void process(struct die *cache, const char *s)
  130. {
  131. s = s ?: "<null>";
  132. if (dump_dies && do_linebreak) {
  133. fputs("\n", stderr);
  134. for (int i = 0; i < indentation_level; i++)
  135. fputs(" ", stderr);
  136. do_linebreak = false;
  137. }
  138. if (dump_dies)
  139. fputs(s, stderr);
  140. if (cache)
  141. die_debug_r("cache %p string '%s'", cache, s);
  142. die_map_add_string(cache, s);
  143. }
  144. #define MAX_FMT_BUFFER_SIZE 128
  145. static void process_fmt(struct die *cache, const char *fmt, ...)
  146. {
  147. char buf[MAX_FMT_BUFFER_SIZE];
  148. va_list args;
  149. va_start(args, fmt);
  150. if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >= sizeof(buf))
  151. error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE");
  152. process(cache, buf);
  153. va_end(args);
  154. }
  155. static void update_fqn(struct die *cache, Dwarf_Die *die)
  156. {
  157. struct die *fqn;
  158. if (!cache->fqn) {
  159. if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &fqn) &&
  160. *fqn->fqn)
  161. cache->fqn = xstrdup(fqn->fqn);
  162. else
  163. cache->fqn = "";
  164. }
  165. }
  166. static void process_fqn(struct die *cache, Dwarf_Die *die)
  167. {
  168. update_fqn(cache, die);
  169. if (*cache->fqn)
  170. process(cache, " ");
  171. process(cache, cache->fqn);
  172. }
  173. #define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \
  174. static void process_##attribute##_attr(struct die *cache, \
  175. Dwarf_Die *die) \
  176. { \
  177. Dwarf_Word value; \
  178. if (get_udata_attr(die, DW_AT_##attribute, &value)) \
  179. process_fmt(cache, " " #attribute "(%" PRIu64 ")", \
  180. value); \
  181. }
  182. DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility)
  183. DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment)
  184. DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size)
  185. DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
  186. DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset)
  187. DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location)
  188. DEFINE_PROCESS_UDATA_ATTRIBUTE(discr_value)
  189. static void process_byte_size_attr(struct die *cache, Dwarf_Die *die)
  190. {
  191. Dwarf_Word value;
  192. unsigned long override;
  193. if (get_udata_attr(die, DW_AT_byte_size, &value)) {
  194. if (stable && kabi_get_byte_size(cache->fqn, &override))
  195. value = override;
  196. process_fmt(cache, " byte_size(%" PRIu64 ")", value);
  197. }
  198. }
  199. /* Match functions -- die_match_callback_t */
  200. #define DEFINE_MATCH(type) \
  201. static bool match_##type##_type(Dwarf_Die *die) \
  202. { \
  203. return dwarf_tag(die) == DW_TAG_##type##_type; \
  204. }
  205. DEFINE_MATCH(enumerator)
  206. DEFINE_MATCH(formal_parameter)
  207. DEFINE_MATCH(member)
  208. DEFINE_MATCH(subrange)
  209. bool match_all(Dwarf_Die *die)
  210. {
  211. return true;
  212. }
  213. int process_die_container(struct state *state, struct die *cache,
  214. Dwarf_Die *die, die_callback_t func,
  215. die_match_callback_t match)
  216. {
  217. Dwarf_Die current;
  218. int res;
  219. /* Track the first item in lists. */
  220. if (state)
  221. state->first_list_item = true;
  222. res = checkp(dwarf_child(die, &current));
  223. while (!res) {
  224. if (match(&current)) {
  225. /* <0 = error, 0 = continue, >0 = stop */
  226. res = checkp(func(state, cache, &current));
  227. if (res)
  228. goto out;
  229. }
  230. res = checkp(dwarf_siblingof(&current, &current));
  231. }
  232. res = 0;
  233. out:
  234. if (state)
  235. state->first_list_item = false;
  236. return res;
  237. }
  238. static int process_type(struct state *state, struct die *parent,
  239. Dwarf_Die *die);
  240. static void process_type_attr(struct state *state, struct die *cache,
  241. Dwarf_Die *die)
  242. {
  243. Dwarf_Die type;
  244. if (get_ref_die_attr(die, DW_AT_type, &type)) {
  245. check(process_type(state, cache, &type));
  246. return;
  247. }
  248. /* Compilers can omit DW_AT_type -- print out 'void' to clarify */
  249. process(cache, "base_type void");
  250. }
  251. static void process_list_comma(struct state *state, struct die *cache)
  252. {
  253. if (state->first_list_item) {
  254. state->first_list_item = false;
  255. } else {
  256. process(cache, " ,");
  257. process_linebreak(cache, 0);
  258. }
  259. }
  260. /* Comma-separated with DW_AT_type */
  261. static void __process_list_type(struct state *state, struct die *cache,
  262. Dwarf_Die *die, const char *type)
  263. {
  264. const char *name = get_name_attr(die);
  265. if (stable) {
  266. if (is_kabi_prefix(name))
  267. name = NULL;
  268. state->kabi.orig_name = NULL;
  269. }
  270. process_list_comma(state, cache);
  271. process(cache, type);
  272. process_type_attr(state, cache, die);
  273. if (stable && state->kabi.orig_name)
  274. name = state->kabi.orig_name;
  275. if (name) {
  276. process(cache, " ");
  277. process(cache, name);
  278. }
  279. process_accessibility_attr(cache, die);
  280. process_bit_size_attr(cache, die);
  281. process_data_bit_offset_attr(cache, die);
  282. process_data_member_location_attr(cache, die);
  283. }
  284. #define DEFINE_PROCESS_LIST_TYPE(type) \
  285. static void process_##type##_type(struct state *state, \
  286. struct die *cache, Dwarf_Die *die) \
  287. { \
  288. __process_list_type(state, cache, die, #type " "); \
  289. }
  290. DEFINE_PROCESS_LIST_TYPE(formal_parameter)
  291. DEFINE_PROCESS_LIST_TYPE(member)
  292. /* Container types with DW_AT_type */
  293. static void __process_type(struct state *state, struct die *cache,
  294. Dwarf_Die *die, const char *type)
  295. {
  296. process(cache, type);
  297. process_fqn(cache, die);
  298. process(cache, " {");
  299. process_linebreak(cache, 1);
  300. process_type_attr(state, cache, die);
  301. process_linebreak(cache, -1);
  302. process(cache, "}");
  303. process_byte_size_attr(cache, die);
  304. process_alignment_attr(cache, die);
  305. }
  306. #define DEFINE_PROCESS_TYPE(type) \
  307. static void process_##type##_type(struct state *state, \
  308. struct die *cache, Dwarf_Die *die) \
  309. { \
  310. __process_type(state, cache, die, #type "_type"); \
  311. }
  312. DEFINE_PROCESS_TYPE(atomic)
  313. DEFINE_PROCESS_TYPE(const)
  314. DEFINE_PROCESS_TYPE(immutable)
  315. DEFINE_PROCESS_TYPE(packed)
  316. DEFINE_PROCESS_TYPE(pointer)
  317. DEFINE_PROCESS_TYPE(reference)
  318. DEFINE_PROCESS_TYPE(restrict)
  319. DEFINE_PROCESS_TYPE(rvalue_reference)
  320. DEFINE_PROCESS_TYPE(shared)
  321. DEFINE_PROCESS_TYPE(template_type_parameter)
  322. DEFINE_PROCESS_TYPE(volatile)
  323. DEFINE_PROCESS_TYPE(typedef)
  324. static void process_subrange_type(struct state *state, struct die *cache,
  325. Dwarf_Die *die)
  326. {
  327. Dwarf_Word count = 0;
  328. if (get_udata_attr(die, DW_AT_count, &count))
  329. process_fmt(cache, "[%" PRIu64 "]", count);
  330. else if (get_udata_attr(die, DW_AT_upper_bound, &count))
  331. process_fmt(cache, "[%" PRIu64 "]", count + 1);
  332. else
  333. process(cache, "[]");
  334. }
  335. static void process_array_type(struct state *state, struct die *cache,
  336. Dwarf_Die *die)
  337. {
  338. process(cache, "array_type");
  339. /* Array size */
  340. check(process_die_container(state, cache, die, process_type,
  341. match_subrange_type));
  342. process(cache, " {");
  343. process_linebreak(cache, 1);
  344. process_type_attr(state, cache, die);
  345. process_linebreak(cache, -1);
  346. process(cache, "}");
  347. }
  348. static void __process_subroutine_type(struct state *state, struct die *cache,
  349. Dwarf_Die *die, const char *type)
  350. {
  351. process(cache, type);
  352. process(cache, " (");
  353. process_linebreak(cache, 1);
  354. /* Parameters */
  355. check(process_die_container(state, cache, die, process_type,
  356. match_formal_parameter_type));
  357. process_linebreak(cache, -1);
  358. process(cache, ")");
  359. process_linebreak(cache, 0);
  360. /* Return type */
  361. process(cache, "-> ");
  362. process_type_attr(state, cache, die);
  363. }
  364. static void process_subroutine_type(struct state *state, struct die *cache,
  365. Dwarf_Die *die)
  366. {
  367. __process_subroutine_type(state, cache, die, "subroutine_type");
  368. }
  369. static void process_variant_type(struct state *state, struct die *cache,
  370. Dwarf_Die *die)
  371. {
  372. process_list_comma(state, cache);
  373. process(cache, "variant {");
  374. process_linebreak(cache, 1);
  375. check(process_die_container(state, cache, die, process_type,
  376. match_member_type));
  377. process_linebreak(cache, -1);
  378. process(cache, "}");
  379. process_discr_value_attr(cache, die);
  380. }
  381. static void process_variant_part_type(struct state *state, struct die *cache,
  382. Dwarf_Die *die)
  383. {
  384. process_list_comma(state, cache);
  385. process(cache, "variant_part {");
  386. process_linebreak(cache, 1);
  387. check(process_die_container(state, cache, die, process_type,
  388. match_all));
  389. process_linebreak(cache, -1);
  390. process(cache, "}");
  391. }
  392. static int get_kabi_status(Dwarf_Die *die, const char **suffix)
  393. {
  394. const char *name = get_name_attr(die);
  395. if (suffix)
  396. *suffix = NULL;
  397. if (is_kabi_prefix(name)) {
  398. name += KABI_PREFIX_LEN;
  399. if (!strncmp(name, KABI_RESERVED_PREFIX,
  400. KABI_RESERVED_PREFIX_LEN))
  401. return KABI_RESERVED;
  402. if (!strncmp(name, KABI_IGNORED_PREFIX,
  403. KABI_IGNORED_PREFIX_LEN))
  404. return KABI_IGNORED;
  405. if (!strncmp(name, KABI_RENAMED_PREFIX,
  406. KABI_RENAMED_PREFIX_LEN)) {
  407. if (suffix) {
  408. name += KABI_RENAMED_PREFIX_LEN;
  409. *suffix = name;
  410. }
  411. return KABI_RESERVED;
  412. }
  413. }
  414. return KABI_NORMAL;
  415. }
  416. static int check_struct_member_kabi_status(struct state *state,
  417. struct die *__unused, Dwarf_Die *die)
  418. {
  419. int res;
  420. assert(dwarf_tag(die) == DW_TAG_member_type);
  421. /*
  422. * If the union member is a struct, expect the __kabi field to
  423. * be the first member of the structure, i.e..:
  424. *
  425. * union {
  426. * type new_member;
  427. * struct {
  428. * type __kabi_field;
  429. * }
  430. * };
  431. */
  432. res = get_kabi_status(die, &state->kabi.orig_name);
  433. if (res == KABI_RESERVED &&
  434. !get_ref_die_attr(die, DW_AT_type, &state->kabi.placeholder))
  435. error("structure member missing a type?");
  436. return res;
  437. }
  438. static int check_union_member_kabi_status(struct state *state,
  439. struct die *__unused, Dwarf_Die *die)
  440. {
  441. Dwarf_Die type;
  442. int res;
  443. assert(dwarf_tag(die) == DW_TAG_member_type);
  444. if (!get_ref_die_attr(die, DW_AT_type, &type))
  445. error("union member missing a type?");
  446. /*
  447. * We expect a union with two members. Check if either of them
  448. * has a __kabi name prefix, i.e.:
  449. *
  450. * union {
  451. * ...
  452. * type memberN; // <- type, N = {0,1}
  453. * ...
  454. * };
  455. *
  456. * The member can also be a structure type, in which case we'll
  457. * check the first structure member.
  458. *
  459. * In any case, stop processing after we've seen two members.
  460. */
  461. res = get_kabi_status(die, &state->kabi.orig_name);
  462. if (res == KABI_RESERVED)
  463. state->kabi.placeholder = type;
  464. if (res != KABI_NORMAL)
  465. return res;
  466. if (dwarf_tag(&type) == DW_TAG_structure_type)
  467. res = checkp(process_die_container(
  468. state, NULL, &type, check_struct_member_kabi_status,
  469. match_member_type));
  470. if (res <= KABI_NORMAL && ++state->kabi.members < 2)
  471. return 0; /* Continue */
  472. return res;
  473. }
  474. static int get_union_kabi_status(Dwarf_Die *die, Dwarf_Die *placeholder,
  475. const char **orig_name)
  476. {
  477. struct state state;
  478. int res;
  479. if (!stable)
  480. return KABI_NORMAL;
  481. /*
  482. * To maintain a stable kABI, distributions may choose to reserve
  483. * space in structs for later use by adding placeholder members,
  484. * for example:
  485. *
  486. * struct s {
  487. * u32 a;
  488. * // an 8-byte placeholder for future use
  489. * u64 __kabi_reserved_0;
  490. * };
  491. *
  492. * When the reserved member is taken into use, the type change
  493. * would normally cause the symbol version to change as well, but
  494. * if the replacement uses the following convention, gendwarfksyms
  495. * continues to use the placeholder type for versioning instead,
  496. * thus maintaining the same symbol version:
  497. *
  498. * struct s {
  499. * u32 a;
  500. * union {
  501. * // placeholder replaced with a new member `b`
  502. * struct t b;
  503. * struct {
  504. * // the placeholder type that is still
  505. * // used for versioning
  506. * u64 __kabi_reserved_0;
  507. * };
  508. * };
  509. * };
  510. *
  511. * I.e., as long as the replaced member is in a union, and the
  512. * placeholder has a __kabi_reserved name prefix, we'll continue
  513. * to use the placeholder type (here u64) for version calculation
  514. * instead of the union type.
  515. *
  516. * It's also possible to ignore new members from versioning if
  517. * they've been added to alignment holes, for example, by
  518. * including them in a union with another member that uses the
  519. * __kabi_ignored name prefix:
  520. *
  521. * struct s {
  522. * u32 a;
  523. * // an alignment hole is used to add `n`
  524. * union {
  525. * u32 n;
  526. * // hide the entire union member from versioning
  527. * u8 __kabi_ignored_0;
  528. * };
  529. * u64 b;
  530. * };
  531. *
  532. * Note that the user of this feature is responsible for ensuring
  533. * that the structure actually remains ABI compatible.
  534. */
  535. memset(&state.kabi, 0, sizeof(state.kabi));
  536. res = checkp(process_die_container(&state, NULL, die,
  537. check_union_member_kabi_status,
  538. match_member_type));
  539. if (res == KABI_RESERVED) {
  540. if (placeholder)
  541. *placeholder = state.kabi.placeholder;
  542. if (orig_name)
  543. *orig_name = state.kabi.orig_name;
  544. }
  545. return res;
  546. }
  547. static bool is_kabi_ignored(Dwarf_Die *die)
  548. {
  549. Dwarf_Die type;
  550. if (!stable)
  551. return false;
  552. if (!get_ref_die_attr(die, DW_AT_type, &type))
  553. error("member missing a type?");
  554. return dwarf_tag(&type) == DW_TAG_union_type &&
  555. checkp(get_union_kabi_status(&type, NULL, NULL)) == KABI_IGNORED;
  556. }
  557. static int ___process_structure_type(struct state *state, struct die *cache,
  558. Dwarf_Die *die)
  559. {
  560. switch (dwarf_tag(die)) {
  561. case DW_TAG_member:
  562. if (is_kabi_ignored(die))
  563. return 0;
  564. return check(process_type(state, cache, die));
  565. case DW_TAG_variant_part:
  566. return check(process_type(state, cache, die));
  567. case DW_TAG_class_type:
  568. case DW_TAG_enumeration_type:
  569. case DW_TAG_structure_type:
  570. case DW_TAG_template_type_parameter:
  571. case DW_TAG_union_type:
  572. case DW_TAG_subprogram:
  573. /* Skip non-member types, including member functions */
  574. return 0;
  575. default:
  576. error("unexpected structure_type child: %x", dwarf_tag(die));
  577. }
  578. }
  579. static void __process_structure_type(struct state *state, struct die *cache,
  580. Dwarf_Die *die, const char *type,
  581. die_callback_t process_func,
  582. die_match_callback_t match_func)
  583. {
  584. bool expand;
  585. process(cache, type);
  586. process_fqn(cache, die);
  587. process(cache, " {");
  588. process_linebreak(cache, 1);
  589. expand = state->expand.expand && is_kabi_definition(cache, die);
  590. if (expand) {
  591. state->expand.current_fqn = cache->fqn;
  592. check(process_die_container(state, cache, die, process_func,
  593. match_func));
  594. }
  595. process_linebreak(cache, -1);
  596. process(cache, "}");
  597. if (expand) {
  598. process_byte_size_attr(cache, die);
  599. process_alignment_attr(cache, die);
  600. }
  601. }
  602. #define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \
  603. static void process_##structure##_type( \
  604. struct state *state, struct die *cache, Dwarf_Die *die) \
  605. { \
  606. __process_structure_type(state, cache, die, \
  607. #structure "_type", \
  608. ___process_structure_type, \
  609. match_all); \
  610. }
  611. DEFINE_PROCESS_STRUCTURE_TYPE(class)
  612. DEFINE_PROCESS_STRUCTURE_TYPE(structure)
  613. static void process_union_type(struct state *state, struct die *cache,
  614. Dwarf_Die *die)
  615. {
  616. Dwarf_Die placeholder;
  617. int res = checkp(get_union_kabi_status(die, &placeholder,
  618. &state->kabi.orig_name));
  619. if (res == KABI_RESERVED)
  620. check(process_type(state, cache, &placeholder));
  621. if (res > KABI_NORMAL)
  622. return;
  623. __process_structure_type(state, cache, die, "union_type",
  624. ___process_structure_type, match_all);
  625. }
  626. static void process_enumerator_type(struct state *state, struct die *cache,
  627. Dwarf_Die *die)
  628. {
  629. bool overridden = false;
  630. unsigned long override;
  631. Dwarf_Word value;
  632. if (stable) {
  633. /* Get the fqn before we process anything */
  634. update_fqn(cache, die);
  635. if (kabi_is_enumerator_ignored(state->expand.current_fqn,
  636. cache->fqn))
  637. return;
  638. overridden = kabi_get_enumerator_value(
  639. state->expand.current_fqn, cache->fqn, &override);
  640. value = override;
  641. }
  642. process_list_comma(state, cache);
  643. process(cache, "enumerator");
  644. process_fqn(cache, die);
  645. if (overridden || get_udata_attr(die, DW_AT_const_value, &value)) {
  646. process(cache, " = ");
  647. process_fmt(cache, "%" PRIu64, value);
  648. }
  649. }
  650. static void process_enumeration_type(struct state *state, struct die *cache,
  651. Dwarf_Die *die)
  652. {
  653. __process_structure_type(state, cache, die, "enumeration_type",
  654. process_type, match_enumerator_type);
  655. }
  656. static void process_base_type(struct state *state, struct die *cache,
  657. Dwarf_Die *die)
  658. {
  659. process(cache, "base_type");
  660. process_fqn(cache, die);
  661. process_byte_size_attr(cache, die);
  662. process_encoding_attr(cache, die);
  663. process_alignment_attr(cache, die);
  664. }
  665. static void process_unspecified_type(struct state *state, struct die *cache,
  666. Dwarf_Die *die)
  667. {
  668. /*
  669. * These can be emitted for stand-alone assembly code, which means we
  670. * might run into them in vmlinux.o.
  671. */
  672. process(cache, "unspecified_type");
  673. }
  674. static void process_cached(struct state *state, struct die *cache,
  675. Dwarf_Die *die)
  676. {
  677. struct die_fragment *df;
  678. Dwarf_Die child;
  679. list_for_each_entry(df, &cache->fragments, list) {
  680. switch (df->type) {
  681. case FRAGMENT_STRING:
  682. die_debug_b("cache %p STRING '%s'", cache,
  683. df->data.str);
  684. process(NULL, df->data.str);
  685. break;
  686. case FRAGMENT_LINEBREAK:
  687. process_linebreak(NULL, df->data.linebreak);
  688. break;
  689. case FRAGMENT_DIE:
  690. if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu),
  691. (void *)df->data.addr, &child))
  692. error("dwarf_die_addr_die failed");
  693. die_debug_b("cache %p DIE addr %" PRIxPTR " tag %x",
  694. cache, df->data.addr, dwarf_tag(&child));
  695. check(process_type(state, NULL, &child));
  696. break;
  697. default:
  698. error("empty die_fragment");
  699. }
  700. }
  701. }
  702. static void state_init(struct state *state)
  703. {
  704. state->expand.expand = true;
  705. state->expand.current_fqn = NULL;
  706. cache_init(&state->expansion_cache);
  707. }
  708. static void expansion_state_restore(struct expansion_state *state,
  709. struct expansion_state *saved)
  710. {
  711. state->expand = saved->expand;
  712. state->current_fqn = saved->current_fqn;
  713. }
  714. static void expansion_state_save(struct expansion_state *state,
  715. struct expansion_state *saved)
  716. {
  717. expansion_state_restore(saved, state);
  718. }
  719. static bool is_expanded_type(int tag)
  720. {
  721. return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
  722. tag == DW_TAG_union_type || tag == DW_TAG_enumeration_type;
  723. }
  724. #define PROCESS_TYPE(type) \
  725. case DW_TAG_##type##_type: \
  726. process_##type##_type(state, cache, die); \
  727. break;
  728. static int process_type(struct state *state, struct die *parent, Dwarf_Die *die)
  729. {
  730. enum die_state want_state = DIE_COMPLETE;
  731. struct die *cache;
  732. struct expansion_state saved;
  733. int tag = dwarf_tag(die);
  734. expansion_state_save(&state->expand, &saved);
  735. /*
  736. * Structures and enumeration types are expanded only once per
  737. * exported symbol. This is sufficient for detecting ABI changes
  738. * within the structure.
  739. */
  740. if (is_expanded_type(tag)) {
  741. if (cache_was_expanded(&state->expansion_cache, die->addr))
  742. state->expand.expand = false;
  743. if (state->expand.expand)
  744. cache_mark_expanded(&state->expansion_cache, die->addr);
  745. else
  746. want_state = DIE_UNEXPANDED;
  747. }
  748. /*
  749. * If we have want_state already cached, use it instead of walking
  750. * through DWARF.
  751. */
  752. cache = die_map_get(die, want_state);
  753. if (cache->state == want_state) {
  754. die_debug_g("cached addr %p tag %x -- %s", die->addr, tag,
  755. die_state_name(cache->state));
  756. process_cached(state, cache, die);
  757. die_map_add_die(parent, cache);
  758. expansion_state_restore(&state->expand, &saved);
  759. return 0;
  760. }
  761. die_debug_g("addr %p tag %x -- %s -> %s", die->addr, tag,
  762. die_state_name(cache->state), die_state_name(want_state));
  763. switch (tag) {
  764. /* Type modifiers */
  765. PROCESS_TYPE(atomic)
  766. PROCESS_TYPE(const)
  767. PROCESS_TYPE(immutable)
  768. PROCESS_TYPE(packed)
  769. PROCESS_TYPE(pointer)
  770. PROCESS_TYPE(reference)
  771. PROCESS_TYPE(restrict)
  772. PROCESS_TYPE(rvalue_reference)
  773. PROCESS_TYPE(shared)
  774. PROCESS_TYPE(volatile)
  775. /* Container types */
  776. PROCESS_TYPE(class)
  777. PROCESS_TYPE(structure)
  778. PROCESS_TYPE(union)
  779. PROCESS_TYPE(enumeration)
  780. /* Subtypes */
  781. PROCESS_TYPE(enumerator)
  782. PROCESS_TYPE(formal_parameter)
  783. PROCESS_TYPE(member)
  784. PROCESS_TYPE(subrange)
  785. PROCESS_TYPE(template_type_parameter)
  786. PROCESS_TYPE(variant)
  787. PROCESS_TYPE(variant_part)
  788. /* Other types */
  789. PROCESS_TYPE(array)
  790. PROCESS_TYPE(base)
  791. PROCESS_TYPE(subroutine)
  792. PROCESS_TYPE(typedef)
  793. PROCESS_TYPE(unspecified)
  794. default:
  795. error("unexpected type: %x", tag);
  796. }
  797. die_debug_r("parent %p cache %p die addr %p tag %x", parent, cache,
  798. die->addr, tag);
  799. /* Update cache state and append to the parent (if any) */
  800. cache->tag = tag;
  801. cache->state = want_state;
  802. die_map_add_die(parent, cache);
  803. expansion_state_restore(&state->expand, &saved);
  804. return 0;
  805. }
  806. /*
  807. * Exported symbol processing
  808. */
  809. static struct die *get_symbol_cache(struct state *state, Dwarf_Die *die)
  810. {
  811. struct die *cache;
  812. cache = die_map_get(die, DIE_SYMBOL);
  813. if (cache->state != DIE_INCOMPLETE)
  814. return NULL; /* We already processed a symbol for this DIE */
  815. cache->tag = dwarf_tag(die);
  816. return cache;
  817. }
  818. static void process_symbol(struct state *state, Dwarf_Die *die,
  819. die_callback_t process_func)
  820. {
  821. struct die *cache;
  822. symbol_set_die(state->sym, die);
  823. cache = get_symbol_cache(state, die);
  824. if (!cache)
  825. return;
  826. debug("%s", state->sym->name);
  827. check(process_func(state, cache, die));
  828. cache->state = DIE_SYMBOL;
  829. if (dump_dies)
  830. fputs("\n", stderr);
  831. }
  832. static int __process_subprogram(struct state *state, struct die *cache,
  833. Dwarf_Die *die)
  834. {
  835. __process_subroutine_type(state, cache, die, "subprogram");
  836. return 0;
  837. }
  838. static void process_subprogram(struct state *state, Dwarf_Die *die)
  839. {
  840. process_symbol(state, die, __process_subprogram);
  841. }
  842. static int __process_variable(struct state *state, struct die *cache,
  843. Dwarf_Die *die)
  844. {
  845. process(cache, "variable ");
  846. process_type_attr(state, cache, die);
  847. return 0;
  848. }
  849. static void process_variable(struct state *state, Dwarf_Die *die)
  850. {
  851. process_symbol(state, die, __process_variable);
  852. }
  853. static void save_symbol_ptr(struct state *state)
  854. {
  855. Dwarf_Die ptr_type;
  856. Dwarf_Die type;
  857. if (!get_ref_die_attr(&state->die, DW_AT_type, &ptr_type) ||
  858. dwarf_tag(&ptr_type) != DW_TAG_pointer_type)
  859. error("%s must be a pointer type!",
  860. get_symbol_name(&state->die));
  861. if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type))
  862. error("%s pointer missing a type attribute?",
  863. get_symbol_name(&state->die));
  864. /*
  865. * Save the symbol pointer DIE in case the actual symbol is
  866. * missing from the DWARF. Clang, for example, intentionally
  867. * omits external symbols from the debugging information.
  868. */
  869. if (dwarf_tag(&type) == DW_TAG_subroutine_type)
  870. symbol_set_ptr(state->sym, &type);
  871. else
  872. symbol_set_ptr(state->sym, &ptr_type);
  873. }
  874. static int process_exported_symbols(struct state *unused, struct die *cache,
  875. Dwarf_Die *die)
  876. {
  877. int tag = dwarf_tag(die);
  878. switch (tag) {
  879. /* Possible containers of exported symbols */
  880. case DW_TAG_namespace:
  881. case DW_TAG_class_type:
  882. case DW_TAG_structure_type:
  883. return check(process_die_container(
  884. NULL, cache, die, process_exported_symbols, match_all));
  885. /* Possible exported symbols */
  886. case DW_TAG_subprogram:
  887. case DW_TAG_variable: {
  888. struct state state;
  889. if (!match_export_symbol(&state, die))
  890. return 0;
  891. state_init(&state);
  892. if (is_symbol_ptr(get_symbol_name(&state.die)))
  893. save_symbol_ptr(&state);
  894. else if (tag == DW_TAG_subprogram)
  895. process_subprogram(&state, &state.die);
  896. else
  897. process_variable(&state, &state.die);
  898. cache_free(&state.expansion_cache);
  899. return 0;
  900. }
  901. default:
  902. return 0;
  903. }
  904. }
  905. static void process_symbol_ptr(struct symbol *sym, void *arg)
  906. {
  907. struct state state;
  908. Dwarf *dwarf = arg;
  909. if (sym->state != SYMBOL_UNPROCESSED || !sym->ptr_die_addr)
  910. return;
  911. debug("%s", sym->name);
  912. state_init(&state);
  913. state.sym = sym;
  914. if (!dwarf_die_addr_die(dwarf, (void *)sym->ptr_die_addr, &state.die))
  915. error("dwarf_die_addr_die failed for symbol ptr: '%s'",
  916. sym->name);
  917. if (dwarf_tag(&state.die) == DW_TAG_subroutine_type)
  918. process_subprogram(&state, &state.die);
  919. else
  920. process_variable(&state, &state.die);
  921. cache_free(&state.expansion_cache);
  922. }
  923. static int resolve_fqns(struct state *parent, struct die *unused,
  924. Dwarf_Die *die)
  925. {
  926. struct state state;
  927. struct die *cache;
  928. const char *name;
  929. bool use_prefix;
  930. char *prefix = NULL;
  931. char *fqn = "";
  932. int tag;
  933. if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &cache))
  934. return 0;
  935. tag = dwarf_tag(die);
  936. /*
  937. * Only namespaces and structures need to pass a prefix to the next
  938. * scope.
  939. */
  940. use_prefix = tag == DW_TAG_namespace || tag == DW_TAG_class_type ||
  941. tag == DW_TAG_structure_type;
  942. state.expand.current_fqn = NULL;
  943. name = get_name_attr(die);
  944. if (parent && parent->expand.current_fqn && (use_prefix || name)) {
  945. /*
  946. * The fqn for the current DIE, and if needed, a prefix for the
  947. * next scope.
  948. */
  949. if (asprintf(&prefix, "%s::%s", parent->expand.current_fqn,
  950. name ? name : "<anonymous>") < 0)
  951. error("asprintf failed");
  952. if (use_prefix)
  953. state.expand.current_fqn = prefix;
  954. /*
  955. * Use fqn only if the DIE has a name. Otherwise fqn will
  956. * remain empty.
  957. */
  958. if (name) {
  959. fqn = prefix;
  960. /* prefix will be freed by die_map. */
  961. prefix = NULL;
  962. }
  963. } else if (name) {
  964. /* No prefix from the previous scope. Use only the name. */
  965. fqn = xstrdup(name);
  966. if (use_prefix)
  967. state.expand.current_fqn = fqn;
  968. }
  969. /* If the DIE has a non-empty name, cache it. */
  970. if (*fqn) {
  971. cache = die_map_get(die, DIE_FQN);
  972. /* Move ownership of fqn to die_map. */
  973. cache->fqn = fqn;
  974. cache->state = DIE_FQN;
  975. }
  976. check(process_die_container(&state, NULL, die, resolve_fqns,
  977. match_all));
  978. free(prefix);
  979. return 0;
  980. }
  981. void process_cu(Dwarf_Die *cudie)
  982. {
  983. check(process_die_container(NULL, NULL, cudie, resolve_fqns,
  984. match_all));
  985. check(process_die_container(NULL, NULL, cudie, process_exported_symbols,
  986. match_all));
  987. symbol_for_each(process_symbol_ptr, dwarf_cu_getdwarf(cudie->cu));
  988. cache_free(&srcfile_cache);
  989. }