btf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Facebook */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <linux/err.h>
  9. #include <linux/btf.h>
  10. #include "btf.h"
  11. #include "bpf.h"
  12. #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
  13. #define max(a, b) ((a) > (b) ? (a) : (b))
  14. #define min(a, b) ((a) < (b) ? (a) : (b))
  15. #define BTF_MAX_NR_TYPES 65535
  16. static struct btf_type btf_void;
  17. struct btf {
  18. union {
  19. struct btf_header *hdr;
  20. void *data;
  21. };
  22. struct btf_type **types;
  23. const char *strings;
  24. void *nohdr_data;
  25. uint32_t nr_types;
  26. uint32_t types_size;
  27. uint32_t data_size;
  28. int fd;
  29. };
  30. static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
  31. {
  32. if (offset < btf->hdr->str_len)
  33. return &btf->strings[offset];
  34. else
  35. return NULL;
  36. }
  37. static int btf_add_type(struct btf *btf, struct btf_type *t)
  38. {
  39. if (btf->types_size - btf->nr_types < 2) {
  40. struct btf_type **new_types;
  41. u32 expand_by, new_size;
  42. if (btf->types_size == BTF_MAX_NR_TYPES)
  43. return -E2BIG;
  44. expand_by = max(btf->types_size >> 2, 16);
  45. new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
  46. new_types = realloc(btf->types, sizeof(*new_types) * new_size);
  47. if (!new_types)
  48. return -ENOMEM;
  49. if (btf->nr_types == 0)
  50. new_types[0] = &btf_void;
  51. btf->types = new_types;
  52. btf->types_size = new_size;
  53. }
  54. btf->types[++(btf->nr_types)] = t;
  55. return 0;
  56. }
  57. static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
  58. {
  59. const struct btf_header *hdr = btf->hdr;
  60. u32 meta_left;
  61. if (btf->data_size < sizeof(struct btf_header)) {
  62. elog("BTF header not found\n");
  63. return -EINVAL;
  64. }
  65. if (hdr->magic != BTF_MAGIC) {
  66. elog("Invalid BTF magic:%x\n", hdr->magic);
  67. return -EINVAL;
  68. }
  69. if (hdr->version != BTF_VERSION) {
  70. elog("Unsupported BTF version:%u\n", hdr->version);
  71. return -ENOTSUP;
  72. }
  73. if (hdr->flags) {
  74. elog("Unsupported BTF flags:%x\n", hdr->flags);
  75. return -ENOTSUP;
  76. }
  77. meta_left = btf->data_size - sizeof(*hdr);
  78. if (!meta_left) {
  79. elog("BTF has no data\n");
  80. return -EINVAL;
  81. }
  82. if (meta_left < hdr->type_off) {
  83. elog("Invalid BTF type section offset:%u\n", hdr->type_off);
  84. return -EINVAL;
  85. }
  86. if (meta_left < hdr->str_off) {
  87. elog("Invalid BTF string section offset:%u\n", hdr->str_off);
  88. return -EINVAL;
  89. }
  90. if (hdr->type_off >= hdr->str_off) {
  91. elog("BTF type section offset >= string section offset. No type?\n");
  92. return -EINVAL;
  93. }
  94. if (hdr->type_off & 0x02) {
  95. elog("BTF type section is not aligned to 4 bytes\n");
  96. return -EINVAL;
  97. }
  98. btf->nohdr_data = btf->hdr + 1;
  99. return 0;
  100. }
  101. static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
  102. {
  103. const struct btf_header *hdr = btf->hdr;
  104. const char *start = btf->nohdr_data + hdr->str_off;
  105. const char *end = start + btf->hdr->str_len;
  106. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
  107. start[0] || end[-1]) {
  108. elog("Invalid BTF string section\n");
  109. return -EINVAL;
  110. }
  111. btf->strings = start;
  112. return 0;
  113. }
  114. static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
  115. {
  116. struct btf_header *hdr = btf->hdr;
  117. void *nohdr_data = btf->nohdr_data;
  118. void *next_type = nohdr_data + hdr->type_off;
  119. void *end_type = nohdr_data + hdr->str_off;
  120. while (next_type < end_type) {
  121. struct btf_type *t = next_type;
  122. uint16_t vlen = BTF_INFO_VLEN(t->info);
  123. int err;
  124. next_type += sizeof(*t);
  125. switch (BTF_INFO_KIND(t->info)) {
  126. case BTF_KIND_INT:
  127. next_type += sizeof(int);
  128. break;
  129. case BTF_KIND_ARRAY:
  130. next_type += sizeof(struct btf_array);
  131. break;
  132. case BTF_KIND_STRUCT:
  133. case BTF_KIND_UNION:
  134. next_type += vlen * sizeof(struct btf_member);
  135. break;
  136. case BTF_KIND_ENUM:
  137. next_type += vlen * sizeof(struct btf_enum);
  138. break;
  139. case BTF_KIND_TYPEDEF:
  140. case BTF_KIND_PTR:
  141. case BTF_KIND_FWD:
  142. case BTF_KIND_VOLATILE:
  143. case BTF_KIND_CONST:
  144. case BTF_KIND_RESTRICT:
  145. break;
  146. default:
  147. elog("Unsupported BTF_KIND:%u\n",
  148. BTF_INFO_KIND(t->info));
  149. return -EINVAL;
  150. }
  151. err = btf_add_type(btf, t);
  152. if (err)
  153. return err;
  154. }
  155. return 0;
  156. }
  157. static const struct btf_type *btf_type_by_id(const struct btf *btf,
  158. uint32_t type_id)
  159. {
  160. if (type_id > btf->nr_types)
  161. return NULL;
  162. return btf->types[type_id];
  163. }
  164. static bool btf_type_is_void(const struct btf_type *t)
  165. {
  166. return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
  167. }
  168. static bool btf_type_is_void_or_null(const struct btf_type *t)
  169. {
  170. return !t || btf_type_is_void(t);
  171. }
  172. static int64_t btf_type_size(const struct btf_type *t)
  173. {
  174. switch (BTF_INFO_KIND(t->info)) {
  175. case BTF_KIND_INT:
  176. case BTF_KIND_STRUCT:
  177. case BTF_KIND_UNION:
  178. case BTF_KIND_ENUM:
  179. return t->size;
  180. case BTF_KIND_PTR:
  181. return sizeof(void *);
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. #define MAX_RESOLVE_DEPTH 32
  187. int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)
  188. {
  189. const struct btf_array *array;
  190. const struct btf_type *t;
  191. uint32_t nelems = 1;
  192. int64_t size = -1;
  193. int i;
  194. t = btf_type_by_id(btf, type_id);
  195. for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
  196. i++) {
  197. size = btf_type_size(t);
  198. if (size >= 0)
  199. break;
  200. switch (BTF_INFO_KIND(t->info)) {
  201. case BTF_KIND_TYPEDEF:
  202. case BTF_KIND_VOLATILE:
  203. case BTF_KIND_CONST:
  204. case BTF_KIND_RESTRICT:
  205. type_id = t->type;
  206. break;
  207. case BTF_KIND_ARRAY:
  208. array = (const struct btf_array *)(t + 1);
  209. if (nelems && array->nelems > UINT32_MAX / nelems)
  210. return -E2BIG;
  211. nelems *= array->nelems;
  212. type_id = array->type;
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. t = btf_type_by_id(btf, type_id);
  218. }
  219. if (size < 0)
  220. return -EINVAL;
  221. if (nelems && size > UINT32_MAX / nelems)
  222. return -E2BIG;
  223. return nelems * size;
  224. }
  225. int32_t btf__find_by_name(const struct btf *btf, const char *type_name)
  226. {
  227. uint32_t i;
  228. if (!strcmp(type_name, "void"))
  229. return 0;
  230. for (i = 1; i <= btf->nr_types; i++) {
  231. const struct btf_type *t = btf->types[i];
  232. const char *name = btf_name_by_offset(btf, t->name_off);
  233. if (name && !strcmp(type_name, name))
  234. return i;
  235. }
  236. return -ENOENT;
  237. }
  238. void btf__free(struct btf *btf)
  239. {
  240. if (!btf)
  241. return;
  242. if (btf->fd != -1)
  243. close(btf->fd);
  244. free(btf->data);
  245. free(btf->types);
  246. free(btf);
  247. }
  248. struct btf *btf__new(uint8_t *data, uint32_t size,
  249. btf_print_fn_t err_log)
  250. {
  251. uint32_t log_buf_size = 0;
  252. char *log_buf = NULL;
  253. struct btf *btf;
  254. int err;
  255. btf = calloc(1, sizeof(struct btf));
  256. if (!btf)
  257. return ERR_PTR(-ENOMEM);
  258. btf->fd = -1;
  259. if (err_log) {
  260. log_buf = malloc(BPF_LOG_BUF_SIZE);
  261. if (!log_buf) {
  262. err = -ENOMEM;
  263. goto done;
  264. }
  265. *log_buf = 0;
  266. log_buf_size = BPF_LOG_BUF_SIZE;
  267. }
  268. btf->data = malloc(size);
  269. if (!btf->data) {
  270. err = -ENOMEM;
  271. goto done;
  272. }
  273. memcpy(btf->data, data, size);
  274. btf->data_size = size;
  275. btf->fd = bpf_load_btf(btf->data, btf->data_size,
  276. log_buf, log_buf_size, false);
  277. if (btf->fd == -1) {
  278. err = -errno;
  279. elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
  280. if (log_buf && *log_buf)
  281. elog("%s\n", log_buf);
  282. goto done;
  283. }
  284. err = btf_parse_hdr(btf, err_log);
  285. if (err)
  286. goto done;
  287. err = btf_parse_str_sec(btf, err_log);
  288. if (err)
  289. goto done;
  290. err = btf_parse_type_sec(btf, err_log);
  291. done:
  292. free(log_buf);
  293. if (err) {
  294. btf__free(btf);
  295. return ERR_PTR(err);
  296. }
  297. return btf;
  298. }
  299. int btf__fd(const struct btf *btf)
  300. {
  301. return btf->fd;
  302. }