symbols.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Google LLC
  4. */
  5. #include <inttypes.h>
  6. #include "gendwarfksyms.h"
  7. #define SYMBOL_HASH_BITS 12
  8. /* struct symbol_addr -> struct symbol */
  9. static HASHTABLE_DEFINE(symbol_addrs, 1 << SYMBOL_HASH_BITS);
  10. /* name -> struct symbol */
  11. static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS);
  12. static inline unsigned int symbol_addr_hash(const struct symbol_addr *addr)
  13. {
  14. return hash_32(addr->section ^ addr_hash(addr->address));
  15. }
  16. static unsigned int __for_each_addr(struct symbol *sym, symbol_callback_t func,
  17. void *data)
  18. {
  19. struct hlist_node *tmp;
  20. struct symbol *match = NULL;
  21. unsigned int processed = 0;
  22. hash_for_each_possible_safe(symbol_addrs, match, tmp, addr_hash,
  23. symbol_addr_hash(&sym->addr)) {
  24. if (match == sym)
  25. continue; /* Already processed */
  26. if (match->addr.section == sym->addr.section &&
  27. match->addr.address == sym->addr.address) {
  28. func(match, data);
  29. ++processed;
  30. }
  31. }
  32. return processed;
  33. }
  34. /*
  35. * For symbols without debugging information (e.g. symbols defined in other
  36. * TUs), we also match __gendwarfksyms_ptr_<symbol_name> symbols, which the
  37. * kernel uses to ensure type information is present in the TU that exports
  38. * the symbol. A __gendwarfksyms_ptr pointer must have the same type as the
  39. * exported symbol, e.g.:
  40. *
  41. * typeof(symname) *__gendwarf_ptr_symname = &symname;
  42. */
  43. bool is_symbol_ptr(const char *name)
  44. {
  45. return name && !strncmp(name, SYMBOL_PTR_PREFIX, SYMBOL_PTR_PREFIX_LEN);
  46. }
  47. static unsigned int for_each(const char *name, symbol_callback_t func,
  48. void *data)
  49. {
  50. struct hlist_node *tmp;
  51. struct symbol *match;
  52. if (!name || !*name)
  53. return 0;
  54. if (is_symbol_ptr(name))
  55. name += SYMBOL_PTR_PREFIX_LEN;
  56. hash_for_each_possible_safe(symbol_names, match, tmp, name_hash,
  57. hash_str(name)) {
  58. if (strcmp(match->name, name))
  59. continue;
  60. /* Call func for the match, and all address matches */
  61. if (func)
  62. func(match, data);
  63. if (match->addr.section != SHN_UNDEF)
  64. return __for_each_addr(match, func, data) + 1;
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. static void set_crc(struct symbol *sym, void *data)
  70. {
  71. unsigned long *crc = data;
  72. if (sym->state == SYMBOL_PROCESSED && sym->crc != *crc)
  73. warn("overriding version for symbol %s (crc %lx vs. %lx)",
  74. sym->name, sym->crc, *crc);
  75. sym->state = SYMBOL_PROCESSED;
  76. sym->crc = *crc;
  77. }
  78. void symbol_set_crc(struct symbol *sym, unsigned long crc)
  79. {
  80. if (for_each(sym->name, set_crc, &crc) == 0)
  81. error("no matching symbols: '%s'", sym->name);
  82. }
  83. static void set_ptr(struct symbol *sym, void *data)
  84. {
  85. sym->ptr_die_addr = (uintptr_t)((Dwarf_Die *)data)->addr;
  86. }
  87. void symbol_set_ptr(struct symbol *sym, Dwarf_Die *ptr)
  88. {
  89. if (for_each(sym->name, set_ptr, ptr) == 0)
  90. error("no matching symbols: '%s'", sym->name);
  91. }
  92. static void set_die(struct symbol *sym, void *data)
  93. {
  94. sym->die_addr = (uintptr_t)((Dwarf_Die *)data)->addr;
  95. sym->state = SYMBOL_MAPPED;
  96. }
  97. void symbol_set_die(struct symbol *sym, Dwarf_Die *die)
  98. {
  99. if (for_each(sym->name, set_die, die) == 0)
  100. error("no matching symbols: '%s'", sym->name);
  101. }
  102. static bool is_exported(const char *name)
  103. {
  104. return for_each(name, NULL, NULL) > 0;
  105. }
  106. int symbol_read_exports(FILE *file)
  107. {
  108. struct symbol *sym;
  109. char *line = NULL;
  110. char *name = NULL;
  111. size_t size = 0;
  112. int nsym = 0;
  113. while (getline(&line, &size, file) > 0) {
  114. if (sscanf(line, "%ms\n", &name) != 1)
  115. error("malformed input line: %s", line);
  116. if (is_exported(name)) {
  117. /* Ignore duplicates */
  118. free(name);
  119. continue;
  120. }
  121. sym = xcalloc(1, sizeof(*sym));
  122. sym->name = name;
  123. sym->addr.section = SHN_UNDEF;
  124. sym->state = SYMBOL_UNPROCESSED;
  125. hash_add(symbol_names, &sym->name_hash, hash_str(sym->name));
  126. ++nsym;
  127. debug("%s", sym->name);
  128. }
  129. free(line);
  130. debug("%d exported symbols", nsym);
  131. return nsym;
  132. }
  133. static void get_symbol(struct symbol *sym, void *arg)
  134. {
  135. struct symbol **res = arg;
  136. if (sym->state == SYMBOL_UNPROCESSED)
  137. *res = sym;
  138. }
  139. struct symbol *symbol_get(const char *name)
  140. {
  141. struct symbol *sym = NULL;
  142. for_each(name, get_symbol, &sym);
  143. return sym;
  144. }
  145. void symbol_for_each(symbol_callback_t func, void *arg)
  146. {
  147. struct hlist_node *tmp;
  148. struct symbol *sym;
  149. hash_for_each_safe(symbol_names, sym, tmp, name_hash) {
  150. func(sym, arg);
  151. }
  152. }
  153. typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym,
  154. Elf32_Word xndx, void *arg);
  155. static void elf_for_each_global(int fd, elf_symbol_callback_t func, void *arg)
  156. {
  157. size_t sym_size;
  158. GElf_Shdr shdr_mem;
  159. GElf_Shdr *shdr;
  160. Elf_Data *xndx_data = NULL;
  161. Elf_Scn *scn;
  162. Elf *elf;
  163. if (elf_version(EV_CURRENT) != EV_CURRENT)
  164. error("elf_version failed: %s", elf_errmsg(-1));
  165. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  166. if (!elf)
  167. error("elf_begin failed: %s", elf_errmsg(-1));
  168. scn = elf_nextscn(elf, NULL);
  169. while (scn) {
  170. shdr = gelf_getshdr(scn, &shdr_mem);
  171. if (!shdr)
  172. error("gelf_getshdr failed: %s", elf_errmsg(-1));
  173. if (shdr->sh_type == SHT_SYMTAB_SHNDX) {
  174. xndx_data = elf_getdata(scn, NULL);
  175. if (!xndx_data)
  176. error("elf_getdata failed: %s", elf_errmsg(-1));
  177. break;
  178. }
  179. scn = elf_nextscn(elf, scn);
  180. }
  181. sym_size = gelf_fsize(elf, ELF_T_SYM, 1, EV_CURRENT);
  182. scn = elf_nextscn(elf, NULL);
  183. while (scn) {
  184. shdr = gelf_getshdr(scn, &shdr_mem);
  185. if (!shdr)
  186. error("gelf_getshdr failed: %s", elf_errmsg(-1));
  187. if (shdr->sh_type == SHT_SYMTAB) {
  188. unsigned int nsyms;
  189. unsigned int n;
  190. Elf_Data *data = elf_getdata(scn, NULL);
  191. if (!data)
  192. error("elf_getdata failed: %s", elf_errmsg(-1));
  193. if (shdr->sh_entsize != sym_size)
  194. error("expected sh_entsize (%" PRIu64 ") to be %zu",
  195. shdr->sh_entsize, sym_size);
  196. nsyms = shdr->sh_size / shdr->sh_entsize;
  197. for (n = 1; n < nsyms; ++n) {
  198. const char *name = NULL;
  199. Elf32_Word xndx = 0;
  200. GElf_Sym sym_mem;
  201. GElf_Sym *sym;
  202. sym = gelf_getsymshndx(data, xndx_data, n,
  203. &sym_mem, &xndx);
  204. if (!sym)
  205. error("gelf_getsymshndx failed: %s",
  206. elf_errmsg(-1));
  207. if (GELF_ST_BIND(sym->st_info) == STB_LOCAL)
  208. continue;
  209. if (sym->st_shndx != SHN_XINDEX)
  210. xndx = sym->st_shndx;
  211. name = elf_strptr(elf, shdr->sh_link,
  212. sym->st_name);
  213. if (!name)
  214. error("elf_strptr failed: %s",
  215. elf_errmsg(-1));
  216. /* Skip empty symbol names */
  217. if (*name)
  218. func(name, sym, xndx, arg);
  219. }
  220. }
  221. scn = elf_nextscn(elf, scn);
  222. }
  223. check(elf_end(elf));
  224. }
  225. static void set_symbol_addr(struct symbol *sym, void *arg)
  226. {
  227. struct symbol_addr *addr = arg;
  228. if (sym->addr.section == SHN_UNDEF) {
  229. sym->addr = *addr;
  230. hash_add(symbol_addrs, &sym->addr_hash,
  231. symbol_addr_hash(&sym->addr));
  232. debug("%s -> { %u, %" PRIx64 " }", sym->name, sym->addr.section,
  233. sym->addr.address);
  234. } else if (sym->addr.section != addr->section ||
  235. sym->addr.address != addr->address) {
  236. warn("multiple addresses for symbol %s?", sym->name);
  237. }
  238. }
  239. static void elf_set_symbol_addr(const char *name, GElf_Sym *sym,
  240. Elf32_Word xndx, void *arg)
  241. {
  242. struct symbol_addr addr = { .section = xndx, .address = sym->st_value };
  243. /* Set addresses for exported symbols */
  244. if (addr.section != SHN_UNDEF)
  245. for_each(name, set_symbol_addr, &addr);
  246. }
  247. void symbol_read_symtab(int fd)
  248. {
  249. elf_for_each_global(fd, elf_set_symbol_addr, NULL);
  250. }
  251. void symbol_print_versions(void)
  252. {
  253. struct hlist_node *tmp;
  254. struct symbol *sym;
  255. hash_for_each_safe(symbol_names, sym, tmp, name_hash) {
  256. if (sym->state != SYMBOL_PROCESSED)
  257. warn("no information for symbol %s", sym->name);
  258. printf("#SYMVER %s 0x%08lx\n", sym->name, sym->crc);
  259. }
  260. }
  261. void symbol_free(void)
  262. {
  263. struct hlist_node *tmp;
  264. struct symbol *sym;
  265. hash_for_each_safe(symbol_names, sym, tmp, name_hash) {
  266. free((void *)sym->name);
  267. free(sym);
  268. }
  269. hash_init(symbol_addrs);
  270. hash_init(symbol_names);
  271. }