Alexei Starovoitov | 1bc38b8 | 2018-10-05 16:40:00 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2018 Facebook */ |
| 3 | |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 4 | #include <endian.h> |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 5 | #include <stdio.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6 | #include <stdlib.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 7 | #include <string.h> |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/btf.h> |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 13 | #include <gelf.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 14 | #include "btf.h" |
| 15 | #include "bpf.h" |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 16 | #include "libbpf.h" |
Andrii Nakryiko | d72386f | 2019-05-15 20:39:27 -0700 | [diff] [blame] | 17 | #include "libbpf_internal.h" |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 18 | #include "hashmap.h" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 19 | |
Andrii Nakryiko | 1d1a3bc | 2020-01-10 10:19:16 -0800 | [diff] [blame^] | 20 | /* make sure libbpf doesn't use kernel-only integer typedefs */ |
| 21 | #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 |
| 22 | |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 23 | #define BTF_MAX_NR_TYPES 0x7fffffff |
| 24 | #define BTF_MAX_STR_OFFSET 0x7fffffff |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 25 | |
| 26 | static struct btf_type btf_void; |
| 27 | |
| 28 | struct btf { |
| 29 | union { |
| 30 | struct btf_header *hdr; |
| 31 | void *data; |
| 32 | }; |
| 33 | struct btf_type **types; |
| 34 | const char *strings; |
| 35 | void *nohdr_data; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 36 | __u32 nr_types; |
| 37 | __u32 types_size; |
| 38 | __u32 data_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 39 | int fd; |
| 40 | }; |
| 41 | |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 42 | static inline __u64 ptr_to_u64(const void *ptr) |
| 43 | { |
| 44 | return (__u64) (unsigned long) ptr; |
| 45 | } |
| 46 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 47 | static int btf_add_type(struct btf *btf, struct btf_type *t) |
| 48 | { |
| 49 | if (btf->types_size - btf->nr_types < 2) { |
| 50 | struct btf_type **new_types; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 51 | __u32 expand_by, new_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 52 | |
| 53 | if (btf->types_size == BTF_MAX_NR_TYPES) |
| 54 | return -E2BIG; |
| 55 | |
| 56 | expand_by = max(btf->types_size >> 2, 16); |
| 57 | new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by); |
| 58 | |
| 59 | new_types = realloc(btf->types, sizeof(*new_types) * new_size); |
| 60 | if (!new_types) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | if (btf->nr_types == 0) |
| 64 | new_types[0] = &btf_void; |
| 65 | |
| 66 | btf->types = new_types; |
| 67 | btf->types_size = new_size; |
| 68 | } |
| 69 | |
| 70 | btf->types[++(btf->nr_types)] = t; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 75 | static int btf_parse_hdr(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 76 | { |
| 77 | const struct btf_header *hdr = btf->hdr; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 78 | __u32 meta_left; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 79 | |
| 80 | if (btf->data_size < sizeof(struct btf_header)) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 81 | pr_debug("BTF header not found\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 82 | return -EINVAL; |
| 83 | } |
| 84 | |
| 85 | if (hdr->magic != BTF_MAGIC) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 86 | pr_debug("Invalid BTF magic:%x\n", hdr->magic); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | if (hdr->version != BTF_VERSION) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 91 | pr_debug("Unsupported BTF version:%u\n", hdr->version); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 92 | return -ENOTSUP; |
| 93 | } |
| 94 | |
| 95 | if (hdr->flags) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 96 | pr_debug("Unsupported BTF flags:%x\n", hdr->flags); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 97 | return -ENOTSUP; |
| 98 | } |
| 99 | |
| 100 | meta_left = btf->data_size - sizeof(*hdr); |
| 101 | if (!meta_left) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 102 | pr_debug("BTF has no data\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | if (meta_left < hdr->type_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 107 | pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | if (meta_left < hdr->str_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 112 | pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | if (hdr->type_off >= hdr->str_off) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 117 | pr_debug("BTF type section offset >= string section offset. No type?\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | if (hdr->type_off & 0x02) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 122 | pr_debug("BTF type section is not aligned to 4 bytes\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | } |
| 125 | |
| 126 | btf->nohdr_data = btf->hdr + 1; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 131 | static int btf_parse_str_sec(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 132 | { |
| 133 | const struct btf_header *hdr = btf->hdr; |
| 134 | const char *start = btf->nohdr_data + hdr->str_off; |
| 135 | const char *end = start + btf->hdr->str_len; |
| 136 | |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 137 | if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 138 | start[0] || end[-1]) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 139 | pr_debug("Invalid BTF string section\n"); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | btf->strings = start; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 148 | static int btf_type_size(struct btf_type *t) |
| 149 | { |
| 150 | int base_size = sizeof(struct btf_type); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 151 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 152 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 153 | switch (btf_kind(t)) { |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 154 | case BTF_KIND_FWD: |
| 155 | case BTF_KIND_CONST: |
| 156 | case BTF_KIND_VOLATILE: |
| 157 | case BTF_KIND_RESTRICT: |
| 158 | case BTF_KIND_PTR: |
| 159 | case BTF_KIND_TYPEDEF: |
| 160 | case BTF_KIND_FUNC: |
| 161 | return base_size; |
| 162 | case BTF_KIND_INT: |
| 163 | return base_size + sizeof(__u32); |
| 164 | case BTF_KIND_ENUM: |
| 165 | return base_size + vlen * sizeof(struct btf_enum); |
| 166 | case BTF_KIND_ARRAY: |
| 167 | return base_size + sizeof(struct btf_array); |
| 168 | case BTF_KIND_STRUCT: |
| 169 | case BTF_KIND_UNION: |
| 170 | return base_size + vlen * sizeof(struct btf_member); |
| 171 | case BTF_KIND_FUNC_PROTO: |
| 172 | return base_size + vlen * sizeof(struct btf_param); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 173 | case BTF_KIND_VAR: |
| 174 | return base_size + sizeof(struct btf_var); |
| 175 | case BTF_KIND_DATASEC: |
| 176 | return base_size + vlen * sizeof(struct btf_var_secinfo); |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 177 | default: |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 178 | pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t)); |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | } |
| 181 | } |
| 182 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 183 | static int btf_parse_type_sec(struct btf *btf) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 184 | { |
| 185 | struct btf_header *hdr = btf->hdr; |
| 186 | void *nohdr_data = btf->nohdr_data; |
| 187 | void *next_type = nohdr_data + hdr->type_off; |
| 188 | void *end_type = nohdr_data + hdr->str_off; |
| 189 | |
| 190 | while (next_type < end_type) { |
| 191 | struct btf_type *t = next_type; |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 192 | int type_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 193 | int err; |
| 194 | |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 195 | type_size = btf_type_size(t); |
| 196 | if (type_size < 0) |
| 197 | return type_size; |
| 198 | next_type += type_size; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 199 | err = btf_add_type(btf, t); |
| 200 | if (err) |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Andrii Nakryiko | 9c65112 | 2019-02-04 17:29:46 -0800 | [diff] [blame] | 207 | __u32 btf__get_nr_types(const struct btf *btf) |
| 208 | { |
| 209 | return btf->nr_types; |
| 210 | } |
| 211 | |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 212 | const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 213 | { |
| 214 | if (type_id > btf->nr_types) |
| 215 | return NULL; |
| 216 | |
| 217 | return btf->types[type_id]; |
| 218 | } |
| 219 | |
| 220 | static bool btf_type_is_void(const struct btf_type *t) |
| 221 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 222 | return t == &btf_void || btf_is_fwd(t); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static bool btf_type_is_void_or_null(const struct btf_type *t) |
| 226 | { |
| 227 | return !t || btf_type_is_void(t); |
| 228 | } |
| 229 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 230 | #define MAX_RESOLVE_DEPTH 32 |
| 231 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 232 | __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 233 | { |
| 234 | const struct btf_array *array; |
| 235 | const struct btf_type *t; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 236 | __u32 nelems = 1; |
| 237 | __s64 size = -1; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 238 | int i; |
| 239 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 240 | t = btf__type_by_id(btf, type_id); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 241 | for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); |
| 242 | i++) { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 243 | switch (btf_kind(t)) { |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 244 | case BTF_KIND_INT: |
| 245 | case BTF_KIND_STRUCT: |
| 246 | case BTF_KIND_UNION: |
| 247 | case BTF_KIND_ENUM: |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 248 | case BTF_KIND_DATASEC: |
Andrii Nakryiko | 69eaab04 | 2019-02-04 17:29:44 -0800 | [diff] [blame] | 249 | size = t->size; |
| 250 | goto done; |
| 251 | case BTF_KIND_PTR: |
| 252 | size = sizeof(void *); |
| 253 | goto done; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 254 | case BTF_KIND_TYPEDEF: |
| 255 | case BTF_KIND_VOLATILE: |
| 256 | case BTF_KIND_CONST: |
| 257 | case BTF_KIND_RESTRICT: |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 258 | case BTF_KIND_VAR: |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 259 | type_id = t->type; |
| 260 | break; |
| 261 | case BTF_KIND_ARRAY: |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 262 | array = btf_array(t); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 263 | if (nelems && array->nelems > UINT32_MAX / nelems) |
| 264 | return -E2BIG; |
| 265 | nelems *= array->nelems; |
| 266 | type_id = array->type; |
| 267 | break; |
| 268 | default: |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 272 | t = btf__type_by_id(btf, type_id); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Andrii Nakryiko | 994021a | 2019-11-06 18:08:54 -0800 | [diff] [blame] | 275 | done: |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 276 | if (size < 0) |
| 277 | return -EINVAL; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 278 | if (nelems && size > UINT32_MAX / nelems) |
| 279 | return -E2BIG; |
| 280 | |
| 281 | return nelems * size; |
| 282 | } |
| 283 | |
Andrii Nakryiko | 3d208f4 | 2019-12-13 17:43:30 -0800 | [diff] [blame] | 284 | int btf__align_of(const struct btf *btf, __u32 id) |
| 285 | { |
| 286 | const struct btf_type *t = btf__type_by_id(btf, id); |
| 287 | __u16 kind = btf_kind(t); |
| 288 | |
| 289 | switch (kind) { |
| 290 | case BTF_KIND_INT: |
| 291 | case BTF_KIND_ENUM: |
| 292 | return min(sizeof(void *), t->size); |
| 293 | case BTF_KIND_PTR: |
| 294 | return sizeof(void *); |
| 295 | case BTF_KIND_TYPEDEF: |
| 296 | case BTF_KIND_VOLATILE: |
| 297 | case BTF_KIND_CONST: |
| 298 | case BTF_KIND_RESTRICT: |
| 299 | return btf__align_of(btf, t->type); |
| 300 | case BTF_KIND_ARRAY: |
| 301 | return btf__align_of(btf, btf_array(t)->type); |
| 302 | case BTF_KIND_STRUCT: |
| 303 | case BTF_KIND_UNION: { |
| 304 | const struct btf_member *m = btf_members(t); |
| 305 | __u16 vlen = btf_vlen(t); |
Prashant Bhole | a79ac2d | 2019-12-16 17:27:38 +0900 | [diff] [blame] | 306 | int i, max_align = 1, align; |
Andrii Nakryiko | 3d208f4 | 2019-12-13 17:43:30 -0800 | [diff] [blame] | 307 | |
| 308 | for (i = 0; i < vlen; i++, m++) { |
Prashant Bhole | a79ac2d | 2019-12-16 17:27:38 +0900 | [diff] [blame] | 309 | align = btf__align_of(btf, m->type); |
| 310 | if (align <= 0) |
| 311 | return align; |
| 312 | max_align = max(max_align, align); |
Andrii Nakryiko | 3d208f4 | 2019-12-13 17:43:30 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Prashant Bhole | a79ac2d | 2019-12-16 17:27:38 +0900 | [diff] [blame] | 315 | return max_align; |
Andrii Nakryiko | 3d208f4 | 2019-12-13 17:43:30 -0800 | [diff] [blame] | 316 | } |
| 317 | default: |
| 318 | pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t)); |
| 319 | return 0; |
| 320 | } |
| 321 | } |
| 322 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 323 | int btf__resolve_type(const struct btf *btf, __u32 type_id) |
| 324 | { |
| 325 | const struct btf_type *t; |
| 326 | int depth = 0; |
| 327 | |
| 328 | t = btf__type_by_id(btf, type_id); |
| 329 | while (depth < MAX_RESOLVE_DEPTH && |
| 330 | !btf_type_is_void_or_null(t) && |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 331 | (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) { |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 332 | type_id = t->type; |
| 333 | t = btf__type_by_id(btf, type_id); |
| 334 | depth++; |
| 335 | } |
| 336 | |
| 337 | if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t)) |
| 338 | return -EINVAL; |
| 339 | |
| 340 | return type_id; |
| 341 | } |
| 342 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 343 | __s32 btf__find_by_name(const struct btf *btf, const char *type_name) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 344 | { |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 345 | __u32 i; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 346 | |
| 347 | if (!strcmp(type_name, "void")) |
| 348 | return 0; |
| 349 | |
| 350 | for (i = 1; i <= btf->nr_types; i++) { |
| 351 | const struct btf_type *t = btf->types[i]; |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 352 | const char *name = btf__name_by_offset(btf, t->name_off); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 353 | |
| 354 | if (name && !strcmp(type_name, name)) |
| 355 | return i; |
| 356 | } |
| 357 | |
| 358 | return -ENOENT; |
| 359 | } |
| 360 | |
Alexei Starovoitov | 1442e28 | 2019-11-14 10:57:05 -0800 | [diff] [blame] | 361 | __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name, |
| 362 | __u32 kind) |
| 363 | { |
| 364 | __u32 i; |
| 365 | |
| 366 | if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void")) |
| 367 | return 0; |
| 368 | |
| 369 | for (i = 1; i <= btf->nr_types; i++) { |
| 370 | const struct btf_type *t = btf->types[i]; |
| 371 | const char *name; |
| 372 | |
| 373 | if (btf_kind(t) != kind) |
| 374 | continue; |
| 375 | name = btf__name_by_offset(btf, t->name_off); |
| 376 | if (name && !strcmp(type_name, name)) |
| 377 | return i; |
| 378 | } |
| 379 | |
| 380 | return -ENOENT; |
| 381 | } |
| 382 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 383 | void btf__free(struct btf *btf) |
| 384 | { |
| 385 | if (!btf) |
| 386 | return; |
| 387 | |
| 388 | if (btf->fd != -1) |
| 389 | close(btf->fd); |
| 390 | |
| 391 | free(btf->data); |
| 392 | free(btf->types); |
| 393 | free(btf); |
| 394 | } |
| 395 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 396 | struct btf *btf__new(__u8 *data, __u32 size) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 397 | { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 398 | struct btf *btf; |
| 399 | int err; |
| 400 | |
| 401 | btf = calloc(1, sizeof(struct btf)); |
| 402 | if (!btf) |
| 403 | return ERR_PTR(-ENOMEM); |
| 404 | |
| 405 | btf->fd = -1; |
| 406 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 407 | btf->data = malloc(size); |
| 408 | if (!btf->data) { |
| 409 | err = -ENOMEM; |
| 410 | goto done; |
| 411 | } |
| 412 | |
| 413 | memcpy(btf->data, data, size); |
| 414 | btf->data_size = size; |
| 415 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 416 | err = btf_parse_hdr(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 417 | if (err) |
| 418 | goto done; |
| 419 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 420 | err = btf_parse_str_sec(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 421 | if (err) |
| 422 | goto done; |
| 423 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 424 | err = btf_parse_type_sec(btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 425 | |
| 426 | done: |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 427 | if (err) { |
| 428 | btf__free(btf); |
| 429 | return ERR_PTR(err); |
| 430 | } |
| 431 | |
| 432 | return btf; |
| 433 | } |
| 434 | |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 435 | static bool btf_check_endianness(const GElf_Ehdr *ehdr) |
| 436 | { |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 437 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 438 | return ehdr->e_ident[EI_DATA] == ELFDATA2LSB; |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 439 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 440 | return ehdr->e_ident[EI_DATA] == ELFDATA2MSB; |
| 441 | #else |
| 442 | # error "Unrecognized __BYTE_ORDER__" |
| 443 | #endif |
| 444 | } |
| 445 | |
| 446 | struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext) |
| 447 | { |
| 448 | Elf_Data *btf_data = NULL, *btf_ext_data = NULL; |
| 449 | int err = 0, fd = -1, idx = 0; |
| 450 | struct btf *btf = NULL; |
| 451 | Elf_Scn *scn = NULL; |
| 452 | Elf *elf = NULL; |
| 453 | GElf_Ehdr ehdr; |
| 454 | |
| 455 | if (elf_version(EV_CURRENT) == EV_NONE) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 456 | pr_warn("failed to init libelf for %s\n", path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 457 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
| 458 | } |
| 459 | |
| 460 | fd = open(path, O_RDONLY); |
| 461 | if (fd < 0) { |
| 462 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 463 | pr_warn("failed to open %s: %s\n", path, strerror(errno)); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 464 | return ERR_PTR(err); |
| 465 | } |
| 466 | |
| 467 | err = -LIBBPF_ERRNO__FORMAT; |
| 468 | |
| 469 | elf = elf_begin(fd, ELF_C_READ, NULL); |
| 470 | if (!elf) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 471 | pr_warn("failed to open %s as ELF file\n", path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 472 | goto done; |
| 473 | } |
| 474 | if (!gelf_getehdr(elf, &ehdr)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 475 | pr_warn("failed to get EHDR from %s\n", path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 476 | goto done; |
| 477 | } |
| 478 | if (!btf_check_endianness(&ehdr)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 479 | pr_warn("non-native ELF endianness is not supported\n"); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 480 | goto done; |
| 481 | } |
| 482 | if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 483 | pr_warn("failed to get e_shstrndx from %s\n", path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 484 | goto done; |
| 485 | } |
| 486 | |
| 487 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 488 | GElf_Shdr sh; |
| 489 | char *name; |
| 490 | |
| 491 | idx++; |
| 492 | if (gelf_getshdr(scn, &sh) != &sh) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 493 | pr_warn("failed to get section(%d) header from %s\n", |
| 494 | idx, path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 495 | goto done; |
| 496 | } |
| 497 | name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name); |
| 498 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 499 | pr_warn("failed to get section(%d) name from %s\n", |
| 500 | idx, path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 501 | goto done; |
| 502 | } |
| 503 | if (strcmp(name, BTF_ELF_SEC) == 0) { |
| 504 | btf_data = elf_getdata(scn, 0); |
| 505 | if (!btf_data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 506 | pr_warn("failed to get section(%d, %s) data from %s\n", |
| 507 | idx, name, path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 508 | goto done; |
| 509 | } |
| 510 | continue; |
| 511 | } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
| 512 | btf_ext_data = elf_getdata(scn, 0); |
| 513 | if (!btf_ext_data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 514 | pr_warn("failed to get section(%d, %s) data from %s\n", |
| 515 | idx, name, path); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 516 | goto done; |
| 517 | } |
| 518 | continue; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | err = 0; |
| 523 | |
| 524 | if (!btf_data) { |
| 525 | err = -ENOENT; |
| 526 | goto done; |
| 527 | } |
| 528 | btf = btf__new(btf_data->d_buf, btf_data->d_size); |
| 529 | if (IS_ERR(btf)) |
| 530 | goto done; |
| 531 | |
| 532 | if (btf_ext && btf_ext_data) { |
| 533 | *btf_ext = btf_ext__new(btf_ext_data->d_buf, |
| 534 | btf_ext_data->d_size); |
| 535 | if (IS_ERR(*btf_ext)) |
| 536 | goto done; |
| 537 | } else if (btf_ext) { |
| 538 | *btf_ext = NULL; |
| 539 | } |
| 540 | done: |
| 541 | if (elf) |
| 542 | elf_end(elf); |
| 543 | close(fd); |
| 544 | |
| 545 | if (err) |
| 546 | return ERR_PTR(err); |
| 547 | /* |
| 548 | * btf is always parsed before btf_ext, so no need to clean up |
| 549 | * btf_ext, if btf loading failed |
| 550 | */ |
| 551 | if (IS_ERR(btf)) |
| 552 | return btf; |
| 553 | if (btf_ext && IS_ERR(*btf_ext)) { |
| 554 | btf__free(btf); |
| 555 | err = PTR_ERR(*btf_ext); |
| 556 | return ERR_PTR(err); |
| 557 | } |
| 558 | return btf; |
| 559 | } |
| 560 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 561 | static int compare_vsi_off(const void *_a, const void *_b) |
| 562 | { |
| 563 | const struct btf_var_secinfo *a = _a; |
| 564 | const struct btf_var_secinfo *b = _b; |
| 565 | |
| 566 | return a->offset - b->offset; |
| 567 | } |
| 568 | |
| 569 | static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, |
| 570 | struct btf_type *t) |
| 571 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 572 | __u32 size = 0, off = 0, i, vars = btf_vlen(t); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 573 | const char *name = btf__name_by_offset(btf, t->name_off); |
| 574 | const struct btf_type *t_var; |
| 575 | struct btf_var_secinfo *vsi; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 576 | const struct btf_var *var; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 577 | int ret; |
| 578 | |
| 579 | if (!name) { |
| 580 | pr_debug("No name found in string section for DATASEC kind.\n"); |
| 581 | return -ENOENT; |
| 582 | } |
| 583 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 584 | /* .extern datasec size and var offsets were set correctly during |
| 585 | * extern collection step, so just skip straight to sorting variables |
| 586 | */ |
| 587 | if (t->size) |
| 588 | goto sort_vars; |
| 589 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 590 | ret = bpf_object__section_size(obj, name, &size); |
| 591 | if (ret || !size || (t->size && t->size != size)) { |
| 592 | pr_debug("Invalid size for section %s: %u bytes\n", name, size); |
| 593 | return -ENOENT; |
| 594 | } |
| 595 | |
| 596 | t->size = size; |
| 597 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 598 | for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 599 | t_var = btf__type_by_id(btf, vsi->type); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 600 | var = btf_var(t_var); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 601 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 602 | if (!btf_is_var(t_var)) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 603 | pr_debug("Non-VAR type seen in section %s\n", name); |
| 604 | return -EINVAL; |
| 605 | } |
| 606 | |
| 607 | if (var->linkage == BTF_VAR_STATIC) |
| 608 | continue; |
| 609 | |
| 610 | name = btf__name_by_offset(btf, t_var->name_off); |
| 611 | if (!name) { |
| 612 | pr_debug("No name found in string section for VAR kind\n"); |
| 613 | return -ENOENT; |
| 614 | } |
| 615 | |
| 616 | ret = bpf_object__variable_offset(obj, name, &off); |
| 617 | if (ret) { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 618 | pr_debug("No offset found in symbol table for VAR %s\n", |
| 619 | name); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 620 | return -ENOENT; |
| 621 | } |
| 622 | |
| 623 | vsi->offset = off; |
| 624 | } |
| 625 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 626 | sort_vars: |
| 627 | qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | int btf__finalize_data(struct bpf_object *obj, struct btf *btf) |
| 632 | { |
| 633 | int err = 0; |
| 634 | __u32 i; |
| 635 | |
| 636 | for (i = 1; i <= btf->nr_types; i++) { |
| 637 | struct btf_type *t = btf->types[i]; |
| 638 | |
| 639 | /* Loader needs to fix up some of the things compiler |
| 640 | * couldn't get its hands on while emitting BTF. This |
| 641 | * is section size and global variable offset. We use |
| 642 | * the info from the ELF itself for this purpose. |
| 643 | */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 644 | if (btf_is_datasec(t)) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 645 | err = btf_fixup_datasec(obj, btf, t); |
| 646 | if (err) |
| 647 | break; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return err; |
| 652 | } |
| 653 | |
Andrii Nakryiko | d29d87f | 2019-02-08 11:19:36 -0800 | [diff] [blame] | 654 | int btf__load(struct btf *btf) |
| 655 | { |
| 656 | __u32 log_buf_size = BPF_LOG_BUF_SIZE; |
| 657 | char *log_buf = NULL; |
| 658 | int err = 0; |
| 659 | |
| 660 | if (btf->fd >= 0) |
| 661 | return -EEXIST; |
| 662 | |
| 663 | log_buf = malloc(log_buf_size); |
| 664 | if (!log_buf) |
| 665 | return -ENOMEM; |
| 666 | |
| 667 | *log_buf = 0; |
| 668 | |
| 669 | btf->fd = bpf_load_btf(btf->data, btf->data_size, |
| 670 | log_buf, log_buf_size, false); |
| 671 | if (btf->fd < 0) { |
| 672 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 673 | pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno); |
Andrii Nakryiko | d29d87f | 2019-02-08 11:19:36 -0800 | [diff] [blame] | 674 | if (*log_buf) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 675 | pr_warn("%s\n", log_buf); |
Andrii Nakryiko | d29d87f | 2019-02-08 11:19:36 -0800 | [diff] [blame] | 676 | goto done; |
| 677 | } |
| 678 | |
| 679 | done: |
| 680 | free(log_buf); |
| 681 | return err; |
| 682 | } |
| 683 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 684 | int btf__fd(const struct btf *btf) |
| 685 | { |
| 686 | return btf->fd; |
| 687 | } |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 688 | |
Andrii Nakryiko | 02c8744 | 2019-02-08 11:19:37 -0800 | [diff] [blame] | 689 | const void *btf__get_raw_data(const struct btf *btf, __u32 *size) |
| 690 | { |
| 691 | *size = btf->data_size; |
| 692 | return btf->data; |
| 693 | } |
| 694 | |
Okash Khawaja | 92b5712 | 2018-07-13 21:57:02 -0700 | [diff] [blame] | 695 | const char *btf__name_by_offset(const struct btf *btf, __u32 offset) |
| 696 | { |
| 697 | if (offset < btf->hdr->str_len) |
| 698 | return &btf->strings[offset]; |
| 699 | else |
| 700 | return NULL; |
| 701 | } |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 702 | |
Martin KaFai Lau | 1d2f44c | 2018-11-23 16:44:32 -0800 | [diff] [blame] | 703 | int btf__get_from_id(__u32 id, struct btf **btf) |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 704 | { |
| 705 | struct bpf_btf_info btf_info = { 0 }; |
| 706 | __u32 len = sizeof(btf_info); |
| 707 | __u32 last_size; |
| 708 | int btf_fd; |
| 709 | void *ptr; |
| 710 | int err; |
| 711 | |
| 712 | err = 0; |
| 713 | *btf = NULL; |
| 714 | btf_fd = bpf_btf_get_fd_by_id(id); |
| 715 | if (btf_fd < 0) |
| 716 | return 0; |
| 717 | |
| 718 | /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so |
| 719 | * let's start with a sane default - 4KiB here - and resize it only if |
| 720 | * bpf_obj_get_info_by_fd() needs a bigger buffer. |
| 721 | */ |
| 722 | btf_info.btf_size = 4096; |
| 723 | last_size = btf_info.btf_size; |
| 724 | ptr = malloc(last_size); |
| 725 | if (!ptr) { |
| 726 | err = -ENOMEM; |
| 727 | goto exit_free; |
| 728 | } |
| 729 | |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 730 | memset(ptr, 0, last_size); |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 731 | btf_info.btf = ptr_to_u64(ptr); |
| 732 | err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len); |
| 733 | |
| 734 | if (!err && btf_info.btf_size > last_size) { |
| 735 | void *temp_ptr; |
| 736 | |
| 737 | last_size = btf_info.btf_size; |
| 738 | temp_ptr = realloc(ptr, last_size); |
| 739 | if (!temp_ptr) { |
| 740 | err = -ENOMEM; |
| 741 | goto exit_free; |
| 742 | } |
| 743 | ptr = temp_ptr; |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 744 | memset(ptr, 0, last_size); |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 745 | btf_info.btf = ptr_to_u64(ptr); |
| 746 | err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len); |
| 747 | } |
| 748 | |
| 749 | if (err || btf_info.btf_size > last_size) { |
| 750 | err = errno; |
| 751 | goto exit_free; |
| 752 | } |
| 753 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 754 | *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size); |
Yonghong Song | d7f5b5e | 2018-11-19 15:29:18 -0800 | [diff] [blame] | 755 | if (IS_ERR(*btf)) { |
| 756 | err = PTR_ERR(*btf); |
| 757 | *btf = NULL; |
| 758 | } |
| 759 | |
| 760 | exit_free: |
| 761 | close(btf_fd); |
| 762 | free(ptr); |
| 763 | |
| 764 | return err; |
| 765 | } |
| 766 | |
Yonghong Song | a6c109a | 2019-02-05 11:48:22 -0800 | [diff] [blame] | 767 | int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 768 | __u32 expected_key_size, __u32 expected_value_size, |
| 769 | __u32 *key_type_id, __u32 *value_type_id) |
| 770 | { |
| 771 | const struct btf_type *container_type; |
| 772 | const struct btf_member *key, *value; |
| 773 | const size_t max_name = 256; |
| 774 | char container_name[max_name]; |
| 775 | __s64 key_size, value_size; |
| 776 | __s32 container_id; |
| 777 | |
| 778 | if (snprintf(container_name, max_name, "____btf_map_%s", map_name) == |
| 779 | max_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 780 | pr_warn("map:%s length of '____btf_map_%s' is too long\n", |
| 781 | map_name, map_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 782 | return -EINVAL; |
| 783 | } |
| 784 | |
| 785 | container_id = btf__find_by_name(btf, container_name); |
| 786 | if (container_id < 0) { |
Yonghong Song | f7748e2 | 2019-02-05 21:38:30 -0800 | [diff] [blame] | 787 | pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n", |
| 788 | map_name, container_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 789 | return container_id; |
| 790 | } |
| 791 | |
| 792 | container_type = btf__type_by_id(btf, container_id); |
| 793 | if (!container_type) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 794 | pr_warn("map:%s cannot find BTF type for container_id:%u\n", |
| 795 | map_name, container_id); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | } |
| 798 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 799 | if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 800 | pr_warn("map:%s container_name:%s is an invalid container struct\n", |
| 801 | map_name, container_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 802 | return -EINVAL; |
| 803 | } |
| 804 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 805 | key = btf_members(container_type); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 806 | value = key + 1; |
| 807 | |
| 808 | key_size = btf__resolve_size(btf, key->type); |
| 809 | if (key_size < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 810 | pr_warn("map:%s invalid BTF key_type_size\n", map_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 811 | return key_size; |
| 812 | } |
| 813 | |
| 814 | if (expected_key_size != key_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 815 | pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n", |
| 816 | map_name, (__u32)key_size, expected_key_size); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 817 | return -EINVAL; |
| 818 | } |
| 819 | |
| 820 | value_size = btf__resolve_size(btf, value->type); |
| 821 | if (value_size < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 822 | pr_warn("map:%s invalid BTF value_type_size\n", map_name); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 823 | return value_size; |
| 824 | } |
| 825 | |
| 826 | if (expected_value_size != value_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 827 | pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n", |
| 828 | map_name, (__u32)value_size, expected_value_size); |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | } |
| 831 | |
| 832 | *key_type_id = key->type; |
| 833 | *value_type_id = value->type; |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 838 | struct btf_ext_sec_setup_param { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 839 | __u32 off; |
| 840 | __u32 len; |
| 841 | __u32 min_rec_size; |
| 842 | struct btf_ext_info *ext_info; |
| 843 | const char *desc; |
| 844 | }; |
| 845 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 846 | static int btf_ext_setup_info(struct btf_ext *btf_ext, |
| 847 | struct btf_ext_sec_setup_param *ext_sec) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 848 | { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 849 | const struct btf_ext_info_sec *sinfo; |
| 850 | struct btf_ext_info *ext_info; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 851 | __u32 info_left, record_size; |
| 852 | /* The start of the info sec (including the __u32 record_size). */ |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 853 | void *info; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 854 | |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 855 | if (ext_sec->len == 0) |
| 856 | return 0; |
| 857 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 858 | if (ext_sec->off & 0x03) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 859 | pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 860 | ext_sec->desc); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 861 | return -EINVAL; |
| 862 | } |
| 863 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 864 | info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off; |
| 865 | info_left = ext_sec->len; |
| 866 | |
| 867 | if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 868 | pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n", |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 869 | ext_sec->desc, ext_sec->off, ext_sec->len); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 870 | return -EINVAL; |
| 871 | } |
| 872 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 873 | /* At least a record size */ |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 874 | if (info_left < sizeof(__u32)) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 875 | pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 876 | return -EINVAL; |
| 877 | } |
| 878 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 879 | /* The record size needs to meet the minimum standard */ |
| 880 | record_size = *(__u32 *)info; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 881 | if (record_size < ext_sec->min_rec_size || |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 882 | record_size & 0x03) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 883 | pr_debug("%s section in .BTF.ext has invalid record size %u\n", |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 884 | ext_sec->desc, record_size); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 885 | return -EINVAL; |
| 886 | } |
| 887 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 888 | sinfo = info + sizeof(__u32); |
| 889 | info_left -= sizeof(__u32); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 890 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 891 | /* If no records, return failure now so .BTF.ext won't be used. */ |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 892 | if (!info_left) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 893 | pr_debug("%s section in .BTF.ext has no records", ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 894 | return -EINVAL; |
| 895 | } |
| 896 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 897 | while (info_left) { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 898 | unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 899 | __u64 total_record_size; |
| 900 | __u32 num_records; |
| 901 | |
| 902 | if (info_left < sec_hdrlen) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 903 | pr_debug("%s section header is not found in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 904 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 905 | return -EINVAL; |
| 906 | } |
| 907 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 908 | num_records = sinfo->num_info; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 909 | if (num_records == 0) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 910 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 911 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 912 | return -EINVAL; |
| 913 | } |
| 914 | |
| 915 | total_record_size = sec_hdrlen + |
| 916 | (__u64)num_records * record_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 917 | if (info_left < total_record_size) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 918 | pr_debug("%s section has incorrect num_records in .BTF.ext\n", |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 919 | ext_sec->desc); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 920 | return -EINVAL; |
| 921 | } |
| 922 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 923 | info_left -= total_record_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 924 | sinfo = (void *)sinfo + total_record_size; |
| 925 | } |
| 926 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 927 | ext_info = ext_sec->ext_info; |
| 928 | ext_info->len = ext_sec->len - sizeof(__u32); |
| 929 | ext_info->rec_size = record_size; |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 930 | ext_info->info = info + sizeof(__u32); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 931 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 932 | return 0; |
| 933 | } |
| 934 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 935 | static int btf_ext_setup_func_info(struct btf_ext *btf_ext) |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 936 | { |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 937 | struct btf_ext_sec_setup_param param = { |
| 938 | .off = btf_ext->hdr->func_info_off, |
| 939 | .len = btf_ext->hdr->func_info_len, |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 940 | .min_rec_size = sizeof(struct bpf_func_info_min), |
| 941 | .ext_info = &btf_ext->func_info, |
| 942 | .desc = "func_info" |
| 943 | }; |
| 944 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 945 | return btf_ext_setup_info(btf_ext, ¶m); |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 948 | static int btf_ext_setup_line_info(struct btf_ext *btf_ext) |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 949 | { |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 950 | struct btf_ext_sec_setup_param param = { |
| 951 | .off = btf_ext->hdr->line_info_off, |
| 952 | .len = btf_ext->hdr->line_info_len, |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 953 | .min_rec_size = sizeof(struct bpf_line_info_min), |
| 954 | .ext_info = &btf_ext->line_info, |
| 955 | .desc = "line_info", |
| 956 | }; |
| 957 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 958 | return btf_ext_setup_info(btf_ext, ¶m); |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 961 | static int btf_ext_setup_field_reloc(struct btf_ext *btf_ext) |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 962 | { |
| 963 | struct btf_ext_sec_setup_param param = { |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 964 | .off = btf_ext->hdr->field_reloc_off, |
| 965 | .len = btf_ext->hdr->field_reloc_len, |
| 966 | .min_rec_size = sizeof(struct bpf_field_reloc), |
| 967 | .ext_info = &btf_ext->field_reloc_info, |
| 968 | .desc = "field_reloc", |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | return btf_ext_setup_info(btf_ext, ¶m); |
| 972 | } |
| 973 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 974 | static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 975 | { |
| 976 | const struct btf_ext_header *hdr = (struct btf_ext_header *)data; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 977 | |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 978 | if (data_size < offsetofend(struct btf_ext_header, hdr_len) || |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 979 | data_size < hdr->hdr_len) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 980 | pr_debug("BTF.ext header not found"); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 981 | return -EINVAL; |
| 982 | } |
| 983 | |
| 984 | if (hdr->magic != BTF_MAGIC) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 985 | pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 986 | return -EINVAL; |
| 987 | } |
| 988 | |
| 989 | if (hdr->version != BTF_VERSION) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 990 | pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 991 | return -ENOTSUP; |
| 992 | } |
| 993 | |
| 994 | if (hdr->flags) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 995 | pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 996 | return -ENOTSUP; |
| 997 | } |
| 998 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 999 | if (data_size == hdr->hdr_len) { |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 1000 | pr_debug("BTF.ext has no data\n"); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1001 | return -EINVAL; |
| 1002 | } |
| 1003 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 1004 | return 0; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | void btf_ext__free(struct btf_ext *btf_ext) |
| 1008 | { |
| 1009 | if (!btf_ext) |
| 1010 | return; |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1011 | free(btf_ext->data); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1012 | free(btf_ext); |
| 1013 | } |
| 1014 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 1015 | struct btf_ext *btf_ext__new(__u8 *data, __u32 size) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1016 | { |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1017 | struct btf_ext *btf_ext; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1018 | int err; |
| 1019 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 1020 | err = btf_ext_parse_hdr(data, size); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1021 | if (err) |
| 1022 | return ERR_PTR(err); |
| 1023 | |
| 1024 | btf_ext = calloc(1, sizeof(struct btf_ext)); |
| 1025 | if (!btf_ext) |
| 1026 | return ERR_PTR(-ENOMEM); |
| 1027 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1028 | btf_ext->data_size = size; |
| 1029 | btf_ext->data = malloc(size); |
| 1030 | if (!btf_ext->data) { |
| 1031 | err = -ENOMEM; |
| 1032 | goto done; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1033 | } |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1034 | memcpy(btf_ext->data, data, size); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1035 | |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 1036 | if (btf_ext->hdr->hdr_len < |
| 1037 | offsetofend(struct btf_ext_header, line_info_len)) |
| 1038 | goto done; |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1039 | err = btf_ext_setup_func_info(btf_ext); |
| 1040 | if (err) |
| 1041 | goto done; |
| 1042 | |
| 1043 | err = btf_ext_setup_line_info(btf_ext); |
| 1044 | if (err) |
| 1045 | goto done; |
| 1046 | |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 1047 | if (btf_ext->hdr->hdr_len < |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 1048 | offsetofend(struct btf_ext_header, field_reloc_len)) |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 1049 | goto done; |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 1050 | err = btf_ext_setup_field_reloc(btf_ext); |
Andrii Nakryiko | 4cedc0d | 2019-08-07 14:39:50 -0700 | [diff] [blame] | 1051 | if (err) |
| 1052 | goto done; |
| 1053 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1054 | done: |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1055 | if (err) { |
| 1056 | btf_ext__free(btf_ext); |
| 1057 | return ERR_PTR(err); |
| 1058 | } |
| 1059 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1060 | return btf_ext; |
| 1061 | } |
| 1062 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1063 | const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size) |
| 1064 | { |
| 1065 | *size = btf_ext->data_size; |
| 1066 | return btf_ext->data; |
| 1067 | } |
| 1068 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1069 | static int btf_ext_reloc_info(const struct btf *btf, |
| 1070 | const struct btf_ext_info *ext_info, |
| 1071 | const char *sec_name, __u32 insns_cnt, |
| 1072 | void **info, __u32 *cnt) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1073 | { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1074 | __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec); |
| 1075 | __u32 i, record_size, existing_len, records_len; |
| 1076 | struct btf_ext_info_sec *sinfo; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1077 | const char *info_sec_name; |
| 1078 | __u64 remain_len; |
| 1079 | void *data; |
| 1080 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1081 | record_size = ext_info->rec_size; |
| 1082 | sinfo = ext_info->info; |
| 1083 | remain_len = ext_info->len; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1084 | while (remain_len > 0) { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1085 | records_len = sinfo->num_info * record_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1086 | info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off); |
| 1087 | if (strcmp(info_sec_name, sec_name)) { |
| 1088 | remain_len -= sec_hdrlen + records_len; |
| 1089 | sinfo = (void *)sinfo + sec_hdrlen + records_len; |
| 1090 | continue; |
| 1091 | } |
| 1092 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1093 | existing_len = (*cnt) * record_size; |
| 1094 | data = realloc(*info, existing_len + records_len); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1095 | if (!data) |
| 1096 | return -ENOMEM; |
| 1097 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1098 | memcpy(data + existing_len, sinfo->data, records_len); |
Martin KaFai Lau | 84ecc1f | 2018-12-05 17:35:47 -0800 | [diff] [blame] | 1099 | /* adjust insn_off only, the rest data will be passed |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1100 | * to the kernel. |
| 1101 | */ |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1102 | for (i = 0; i < sinfo->num_info; i++) { |
| 1103 | __u32 *insn_off; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1104 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1105 | insn_off = data + existing_len + (i * record_size); |
| 1106 | *insn_off = *insn_off / sizeof(struct bpf_insn) + |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1107 | insns_cnt; |
| 1108 | } |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1109 | *info = data; |
| 1110 | *cnt += sinfo->num_info; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 1114 | return -ENOENT; |
| 1115 | } |
| 1116 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1117 | int btf_ext__reloc_func_info(const struct btf *btf, |
| 1118 | const struct btf_ext *btf_ext, |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1119 | const char *sec_name, __u32 insns_cnt, |
| 1120 | void **func_info, __u32 *cnt) |
| 1121 | { |
| 1122 | return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name, |
| 1123 | insns_cnt, func_info, cnt); |
| 1124 | } |
| 1125 | |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 1126 | int btf_ext__reloc_line_info(const struct btf *btf, |
| 1127 | const struct btf_ext *btf_ext, |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1128 | const char *sec_name, __u32 insns_cnt, |
| 1129 | void **line_info, __u32 *cnt) |
| 1130 | { |
| 1131 | return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name, |
| 1132 | insns_cnt, line_info, cnt); |
| 1133 | } |
| 1134 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 1135 | __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext) |
| 1136 | { |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 1137 | return btf_ext->func_info.rec_size; |
| 1138 | } |
| 1139 | |
| 1140 | __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext) |
| 1141 | { |
| 1142 | return btf_ext->line_info.rec_size; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1143 | } |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1144 | |
| 1145 | struct btf_dedup; |
| 1146 | |
| 1147 | static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext, |
| 1148 | const struct btf_dedup_opts *opts); |
| 1149 | static void btf_dedup_free(struct btf_dedup *d); |
| 1150 | static int btf_dedup_strings(struct btf_dedup *d); |
| 1151 | static int btf_dedup_prim_types(struct btf_dedup *d); |
| 1152 | static int btf_dedup_struct_types(struct btf_dedup *d); |
| 1153 | static int btf_dedup_ref_types(struct btf_dedup *d); |
| 1154 | static int btf_dedup_compact_types(struct btf_dedup *d); |
| 1155 | static int btf_dedup_remap_types(struct btf_dedup *d); |
| 1156 | |
| 1157 | /* |
| 1158 | * Deduplicate BTF types and strings. |
| 1159 | * |
| 1160 | * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF |
| 1161 | * section with all BTF type descriptors and string data. It overwrites that |
| 1162 | * memory in-place with deduplicated types and strings without any loss of |
| 1163 | * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section |
| 1164 | * is provided, all the strings referenced from .BTF.ext section are honored |
| 1165 | * and updated to point to the right offsets after deduplication. |
| 1166 | * |
| 1167 | * If function returns with error, type/string data might be garbled and should |
| 1168 | * be discarded. |
| 1169 | * |
| 1170 | * More verbose and detailed description of both problem btf_dedup is solving, |
| 1171 | * as well as solution could be found at: |
| 1172 | * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html |
| 1173 | * |
| 1174 | * Problem description and justification |
| 1175 | * ===================================== |
| 1176 | * |
| 1177 | * BTF type information is typically emitted either as a result of conversion |
| 1178 | * from DWARF to BTF or directly by compiler. In both cases, each compilation |
| 1179 | * unit contains information about a subset of all the types that are used |
| 1180 | * in an application. These subsets are frequently overlapping and contain a lot |
| 1181 | * of duplicated information when later concatenated together into a single |
| 1182 | * binary. This algorithm ensures that each unique type is represented by single |
| 1183 | * BTF type descriptor, greatly reducing resulting size of BTF data. |
| 1184 | * |
| 1185 | * Compilation unit isolation and subsequent duplication of data is not the only |
| 1186 | * problem. The same type hierarchy (e.g., struct and all the type that struct |
| 1187 | * references) in different compilation units can be represented in BTF to |
| 1188 | * various degrees of completeness (or, rather, incompleteness) due to |
| 1189 | * struct/union forward declarations. |
| 1190 | * |
| 1191 | * Let's take a look at an example, that we'll use to better understand the |
| 1192 | * problem (and solution). Suppose we have two compilation units, each using |
| 1193 | * same `struct S`, but each of them having incomplete type information about |
| 1194 | * struct's fields: |
| 1195 | * |
| 1196 | * // CU #1: |
| 1197 | * struct S; |
| 1198 | * struct A { |
| 1199 | * int a; |
| 1200 | * struct A* self; |
| 1201 | * struct S* parent; |
| 1202 | * }; |
| 1203 | * struct B; |
| 1204 | * struct S { |
| 1205 | * struct A* a_ptr; |
| 1206 | * struct B* b_ptr; |
| 1207 | * }; |
| 1208 | * |
| 1209 | * // CU #2: |
| 1210 | * struct S; |
| 1211 | * struct A; |
| 1212 | * struct B { |
| 1213 | * int b; |
| 1214 | * struct B* self; |
| 1215 | * struct S* parent; |
| 1216 | * }; |
| 1217 | * struct S { |
| 1218 | * struct A* a_ptr; |
| 1219 | * struct B* b_ptr; |
| 1220 | * }; |
| 1221 | * |
| 1222 | * In case of CU #1, BTF data will know only that `struct B` exist (but no |
| 1223 | * more), but will know the complete type information about `struct A`. While |
| 1224 | * for CU #2, it will know full type information about `struct B`, but will |
| 1225 | * only know about forward declaration of `struct A` (in BTF terms, it will |
| 1226 | * have `BTF_KIND_FWD` type descriptor with name `B`). |
| 1227 | * |
| 1228 | * This compilation unit isolation means that it's possible that there is no |
| 1229 | * single CU with complete type information describing structs `S`, `A`, and |
| 1230 | * `B`. Also, we might get tons of duplicated and redundant type information. |
| 1231 | * |
| 1232 | * Additional complication we need to keep in mind comes from the fact that |
| 1233 | * types, in general, can form graphs containing cycles, not just DAGs. |
| 1234 | * |
| 1235 | * While algorithm does deduplication, it also merges and resolves type |
| 1236 | * information (unless disabled throught `struct btf_opts`), whenever possible. |
| 1237 | * E.g., in the example above with two compilation units having partial type |
| 1238 | * information for structs `A` and `B`, the output of algorithm will emit |
| 1239 | * a single copy of each BTF type that describes structs `A`, `B`, and `S` |
| 1240 | * (as well as type information for `int` and pointers), as if they were defined |
| 1241 | * in a single compilation unit as: |
| 1242 | * |
| 1243 | * struct A { |
| 1244 | * int a; |
| 1245 | * struct A* self; |
| 1246 | * struct S* parent; |
| 1247 | * }; |
| 1248 | * struct B { |
| 1249 | * int b; |
| 1250 | * struct B* self; |
| 1251 | * struct S* parent; |
| 1252 | * }; |
| 1253 | * struct S { |
| 1254 | * struct A* a_ptr; |
| 1255 | * struct B* b_ptr; |
| 1256 | * }; |
| 1257 | * |
| 1258 | * Algorithm summary |
| 1259 | * ================= |
| 1260 | * |
| 1261 | * Algorithm completes its work in 6 separate passes: |
| 1262 | * |
| 1263 | * 1. Strings deduplication. |
| 1264 | * 2. Primitive types deduplication (int, enum, fwd). |
| 1265 | * 3. Struct/union types deduplication. |
| 1266 | * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func |
| 1267 | * protos, and const/volatile/restrict modifiers). |
| 1268 | * 5. Types compaction. |
| 1269 | * 6. Types remapping. |
| 1270 | * |
| 1271 | * Algorithm determines canonical type descriptor, which is a single |
| 1272 | * representative type for each truly unique type. This canonical type is the |
| 1273 | * one that will go into final deduplicated BTF type information. For |
| 1274 | * struct/unions, it is also the type that algorithm will merge additional type |
| 1275 | * information into (while resolving FWDs), as it discovers it from data in |
| 1276 | * other CUs. Each input BTF type eventually gets either mapped to itself, if |
| 1277 | * that type is canonical, or to some other type, if that type is equivalent |
| 1278 | * and was chosen as canonical representative. This mapping is stored in |
| 1279 | * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that |
| 1280 | * FWD type got resolved to. |
| 1281 | * |
| 1282 | * To facilitate fast discovery of canonical types, we also maintain canonical |
| 1283 | * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash |
| 1284 | * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types |
| 1285 | * that match that signature. With sufficiently good choice of type signature |
| 1286 | * hashing function, we can limit number of canonical types for each unique type |
| 1287 | * signature to a very small number, allowing to find canonical type for any |
| 1288 | * duplicated type very quickly. |
| 1289 | * |
| 1290 | * Struct/union deduplication is the most critical part and algorithm for |
| 1291 | * deduplicating structs/unions is described in greater details in comments for |
| 1292 | * `btf_dedup_is_equiv` function. |
| 1293 | */ |
| 1294 | int btf__dedup(struct btf *btf, struct btf_ext *btf_ext, |
| 1295 | const struct btf_dedup_opts *opts) |
| 1296 | { |
| 1297 | struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts); |
| 1298 | int err; |
| 1299 | |
| 1300 | if (IS_ERR(d)) { |
| 1301 | pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d)); |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | |
| 1305 | err = btf_dedup_strings(d); |
| 1306 | if (err < 0) { |
| 1307 | pr_debug("btf_dedup_strings failed:%d\n", err); |
| 1308 | goto done; |
| 1309 | } |
| 1310 | err = btf_dedup_prim_types(d); |
| 1311 | if (err < 0) { |
| 1312 | pr_debug("btf_dedup_prim_types failed:%d\n", err); |
| 1313 | goto done; |
| 1314 | } |
| 1315 | err = btf_dedup_struct_types(d); |
| 1316 | if (err < 0) { |
| 1317 | pr_debug("btf_dedup_struct_types failed:%d\n", err); |
| 1318 | goto done; |
| 1319 | } |
| 1320 | err = btf_dedup_ref_types(d); |
| 1321 | if (err < 0) { |
| 1322 | pr_debug("btf_dedup_ref_types failed:%d\n", err); |
| 1323 | goto done; |
| 1324 | } |
| 1325 | err = btf_dedup_compact_types(d); |
| 1326 | if (err < 0) { |
| 1327 | pr_debug("btf_dedup_compact_types failed:%d\n", err); |
| 1328 | goto done; |
| 1329 | } |
| 1330 | err = btf_dedup_remap_types(d); |
| 1331 | if (err < 0) { |
| 1332 | pr_debug("btf_dedup_remap_types failed:%d\n", err); |
| 1333 | goto done; |
| 1334 | } |
| 1335 | |
| 1336 | done: |
| 1337 | btf_dedup_free(d); |
| 1338 | return err; |
| 1339 | } |
| 1340 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1341 | #define BTF_UNPROCESSED_ID ((__u32)-1) |
| 1342 | #define BTF_IN_PROGRESS_ID ((__u32)-2) |
| 1343 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1344 | struct btf_dedup { |
| 1345 | /* .BTF section to be deduped in-place */ |
| 1346 | struct btf *btf; |
| 1347 | /* |
| 1348 | * Optional .BTF.ext section. When provided, any strings referenced |
| 1349 | * from it will be taken into account when deduping strings |
| 1350 | */ |
| 1351 | struct btf_ext *btf_ext; |
| 1352 | /* |
| 1353 | * This is a map from any type's signature hash to a list of possible |
| 1354 | * canonical representative type candidates. Hash collisions are |
| 1355 | * ignored, so even types of various kinds can share same list of |
| 1356 | * candidates, which is fine because we rely on subsequent |
| 1357 | * btf_xxx_equal() checks to authoritatively verify type equality. |
| 1358 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1359 | struct hashmap *dedup_table; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1360 | /* Canonical types map */ |
| 1361 | __u32 *map; |
| 1362 | /* Hypothetical mapping, used during type graph equivalence checks */ |
| 1363 | __u32 *hypot_map; |
| 1364 | __u32 *hypot_list; |
| 1365 | size_t hypot_cnt; |
| 1366 | size_t hypot_cap; |
| 1367 | /* Various option modifying behavior of algorithm */ |
| 1368 | struct btf_dedup_opts opts; |
| 1369 | }; |
| 1370 | |
| 1371 | struct btf_str_ptr { |
| 1372 | const char *str; |
| 1373 | __u32 new_off; |
| 1374 | bool used; |
| 1375 | }; |
| 1376 | |
| 1377 | struct btf_str_ptrs { |
| 1378 | struct btf_str_ptr *ptrs; |
| 1379 | const char *data; |
| 1380 | __u32 cnt; |
| 1381 | __u32 cap; |
| 1382 | }; |
| 1383 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1384 | static long hash_combine(long h, long value) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1385 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1386 | return h * 31 + value; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1389 | #define for_each_dedup_cand(d, node, hash) \ |
| 1390 | hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1391 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1392 | static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1393 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1394 | return hashmap__append(d->dedup_table, |
| 1395 | (void *)hash, (void *)(long)type_id); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static int btf_dedup_hypot_map_add(struct btf_dedup *d, |
| 1399 | __u32 from_id, __u32 to_id) |
| 1400 | { |
| 1401 | if (d->hypot_cnt == d->hypot_cap) { |
| 1402 | __u32 *new_list; |
| 1403 | |
| 1404 | d->hypot_cap += max(16, d->hypot_cap / 2); |
| 1405 | new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap); |
| 1406 | if (!new_list) |
| 1407 | return -ENOMEM; |
| 1408 | d->hypot_list = new_list; |
| 1409 | } |
| 1410 | d->hypot_list[d->hypot_cnt++] = from_id; |
| 1411 | d->hypot_map[from_id] = to_id; |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | static void btf_dedup_clear_hypot_map(struct btf_dedup *d) |
| 1416 | { |
| 1417 | int i; |
| 1418 | |
| 1419 | for (i = 0; i < d->hypot_cnt; i++) |
| 1420 | d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; |
| 1421 | d->hypot_cnt = 0; |
| 1422 | } |
| 1423 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1424 | static void btf_dedup_free(struct btf_dedup *d) |
| 1425 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1426 | hashmap__free(d->dedup_table); |
| 1427 | d->dedup_table = NULL; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1428 | |
| 1429 | free(d->map); |
| 1430 | d->map = NULL; |
| 1431 | |
| 1432 | free(d->hypot_map); |
| 1433 | d->hypot_map = NULL; |
| 1434 | |
| 1435 | free(d->hypot_list); |
| 1436 | d->hypot_list = NULL; |
| 1437 | |
| 1438 | free(d); |
| 1439 | } |
| 1440 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1441 | static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx) |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 1442 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1443 | return (size_t)key; |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1446 | static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx) |
| 1447 | { |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx) |
| 1452 | { |
| 1453 | return k1 == k2; |
| 1454 | } |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 1455 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1456 | static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext, |
| 1457 | const struct btf_dedup_opts *opts) |
| 1458 | { |
| 1459 | struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup)); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1460 | hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1461 | int i, err = 0; |
| 1462 | |
| 1463 | if (!d) |
| 1464 | return ERR_PTR(-ENOMEM); |
| 1465 | |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 1466 | d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1467 | /* dedup_table_size is now used only to force collisions in tests */ |
| 1468 | if (opts && opts->dedup_table_size == 1) |
| 1469 | hash_fn = btf_dedup_collision_hash_fn; |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 1470 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1471 | d->btf = btf; |
| 1472 | d->btf_ext = btf_ext; |
| 1473 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1474 | d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL); |
| 1475 | if (IS_ERR(d->dedup_table)) { |
| 1476 | err = PTR_ERR(d->dedup_table); |
| 1477 | d->dedup_table = NULL; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1478 | goto done; |
| 1479 | } |
| 1480 | |
| 1481 | d->map = malloc(sizeof(__u32) * (1 + btf->nr_types)); |
| 1482 | if (!d->map) { |
| 1483 | err = -ENOMEM; |
| 1484 | goto done; |
| 1485 | } |
| 1486 | /* special BTF "void" type is made canonical immediately */ |
| 1487 | d->map[0] = 0; |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 1488 | for (i = 1; i <= btf->nr_types; i++) { |
| 1489 | struct btf_type *t = d->btf->types[i]; |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 1490 | |
| 1491 | /* VAR and DATASEC are never deduped and are self-canonical */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1492 | if (btf_is_var(t) || btf_is_datasec(t)) |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 1493 | d->map[i] = i; |
| 1494 | else |
| 1495 | d->map[i] = BTF_UNPROCESSED_ID; |
| 1496 | } |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1497 | |
| 1498 | d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types)); |
| 1499 | if (!d->hypot_map) { |
| 1500 | err = -ENOMEM; |
| 1501 | goto done; |
| 1502 | } |
| 1503 | for (i = 0; i <= btf->nr_types; i++) |
| 1504 | d->hypot_map[i] = BTF_UNPROCESSED_ID; |
| 1505 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1506 | done: |
| 1507 | if (err) { |
| 1508 | btf_dedup_free(d); |
| 1509 | return ERR_PTR(err); |
| 1510 | } |
| 1511 | |
| 1512 | return d; |
| 1513 | } |
| 1514 | |
| 1515 | typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx); |
| 1516 | |
| 1517 | /* |
| 1518 | * Iterate over all possible places in .BTF and .BTF.ext that can reference |
| 1519 | * string and pass pointer to it to a provided callback `fn`. |
| 1520 | */ |
| 1521 | static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx) |
| 1522 | { |
| 1523 | void *line_data_cur, *line_data_end; |
| 1524 | int i, j, r, rec_size; |
| 1525 | struct btf_type *t; |
| 1526 | |
| 1527 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 1528 | t = d->btf->types[i]; |
| 1529 | r = fn(&t->name_off, ctx); |
| 1530 | if (r) |
| 1531 | return r; |
| 1532 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1533 | switch (btf_kind(t)) { |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1534 | case BTF_KIND_STRUCT: |
| 1535 | case BTF_KIND_UNION: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1536 | struct btf_member *m = btf_members(t); |
| 1537 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1538 | |
| 1539 | for (j = 0; j < vlen; j++) { |
| 1540 | r = fn(&m->name_off, ctx); |
| 1541 | if (r) |
| 1542 | return r; |
| 1543 | m++; |
| 1544 | } |
| 1545 | break; |
| 1546 | } |
| 1547 | case BTF_KIND_ENUM: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1548 | struct btf_enum *m = btf_enum(t); |
| 1549 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1550 | |
| 1551 | for (j = 0; j < vlen; j++) { |
| 1552 | r = fn(&m->name_off, ctx); |
| 1553 | if (r) |
| 1554 | return r; |
| 1555 | m++; |
| 1556 | } |
| 1557 | break; |
| 1558 | } |
| 1559 | case BTF_KIND_FUNC_PROTO: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1560 | struct btf_param *m = btf_params(t); |
| 1561 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1562 | |
| 1563 | for (j = 0; j < vlen; j++) { |
| 1564 | r = fn(&m->name_off, ctx); |
| 1565 | if (r) |
| 1566 | return r; |
| 1567 | m++; |
| 1568 | } |
| 1569 | break; |
| 1570 | } |
| 1571 | default: |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | if (!d->btf_ext) |
| 1577 | return 0; |
| 1578 | |
| 1579 | line_data_cur = d->btf_ext->line_info.info; |
| 1580 | line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len; |
| 1581 | rec_size = d->btf_ext->line_info.rec_size; |
| 1582 | |
| 1583 | while (line_data_cur < line_data_end) { |
| 1584 | struct btf_ext_info_sec *sec = line_data_cur; |
| 1585 | struct bpf_line_info_min *line_info; |
| 1586 | __u32 num_info = sec->num_info; |
| 1587 | |
| 1588 | r = fn(&sec->sec_name_off, ctx); |
| 1589 | if (r) |
| 1590 | return r; |
| 1591 | |
| 1592 | line_data_cur += sizeof(struct btf_ext_info_sec); |
| 1593 | for (i = 0; i < num_info; i++) { |
| 1594 | line_info = line_data_cur; |
| 1595 | r = fn(&line_info->file_name_off, ctx); |
| 1596 | if (r) |
| 1597 | return r; |
| 1598 | r = fn(&line_info->line_off, ctx); |
| 1599 | if (r) |
| 1600 | return r; |
| 1601 | line_data_cur += rec_size; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | static int str_sort_by_content(const void *a1, const void *a2) |
| 1609 | { |
| 1610 | const struct btf_str_ptr *p1 = a1; |
| 1611 | const struct btf_str_ptr *p2 = a2; |
| 1612 | |
| 1613 | return strcmp(p1->str, p2->str); |
| 1614 | } |
| 1615 | |
| 1616 | static int str_sort_by_offset(const void *a1, const void *a2) |
| 1617 | { |
| 1618 | const struct btf_str_ptr *p1 = a1; |
| 1619 | const struct btf_str_ptr *p2 = a2; |
| 1620 | |
| 1621 | if (p1->str != p2->str) |
| 1622 | return p1->str < p2->str ? -1 : 1; |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem) |
| 1627 | { |
| 1628 | const struct btf_str_ptr *p = pelem; |
| 1629 | |
| 1630 | if (str_ptr != p->str) |
| 1631 | return (const char *)str_ptr < p->str ? -1 : 1; |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx) |
| 1636 | { |
| 1637 | struct btf_str_ptrs *strs; |
| 1638 | struct btf_str_ptr *s; |
| 1639 | |
| 1640 | if (*str_off_ptr == 0) |
| 1641 | return 0; |
| 1642 | |
| 1643 | strs = ctx; |
| 1644 | s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt, |
| 1645 | sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp); |
| 1646 | if (!s) |
| 1647 | return -EINVAL; |
| 1648 | s->used = true; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
| 1652 | static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx) |
| 1653 | { |
| 1654 | struct btf_str_ptrs *strs; |
| 1655 | struct btf_str_ptr *s; |
| 1656 | |
| 1657 | if (*str_off_ptr == 0) |
| 1658 | return 0; |
| 1659 | |
| 1660 | strs = ctx; |
| 1661 | s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt, |
| 1662 | sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp); |
| 1663 | if (!s) |
| 1664 | return -EINVAL; |
| 1665 | *str_off_ptr = s->new_off; |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Dedup string and filter out those that are not referenced from either .BTF |
| 1671 | * or .BTF.ext (if provided) sections. |
| 1672 | * |
| 1673 | * This is done by building index of all strings in BTF's string section, |
| 1674 | * then iterating over all entities that can reference strings (e.g., type |
| 1675 | * names, struct field names, .BTF.ext line info, etc) and marking corresponding |
| 1676 | * strings as used. After that all used strings are deduped and compacted into |
| 1677 | * sequential blob of memory and new offsets are calculated. Then all the string |
| 1678 | * references are iterated again and rewritten using new offsets. |
| 1679 | */ |
| 1680 | static int btf_dedup_strings(struct btf_dedup *d) |
| 1681 | { |
| 1682 | const struct btf_header *hdr = d->btf->hdr; |
| 1683 | char *start = (char *)d->btf->nohdr_data + hdr->str_off; |
| 1684 | char *end = start + d->btf->hdr->str_len; |
| 1685 | char *p = start, *tmp_strs = NULL; |
| 1686 | struct btf_str_ptrs strs = { |
| 1687 | .cnt = 0, |
| 1688 | .cap = 0, |
| 1689 | .ptrs = NULL, |
| 1690 | .data = start, |
| 1691 | }; |
| 1692 | int i, j, err = 0, grp_idx; |
| 1693 | bool grp_used; |
| 1694 | |
| 1695 | /* build index of all strings */ |
| 1696 | while (p < end) { |
| 1697 | if (strs.cnt + 1 > strs.cap) { |
| 1698 | struct btf_str_ptr *new_ptrs; |
| 1699 | |
| 1700 | strs.cap += max(strs.cnt / 2, 16); |
| 1701 | new_ptrs = realloc(strs.ptrs, |
| 1702 | sizeof(strs.ptrs[0]) * strs.cap); |
| 1703 | if (!new_ptrs) { |
| 1704 | err = -ENOMEM; |
| 1705 | goto done; |
| 1706 | } |
| 1707 | strs.ptrs = new_ptrs; |
| 1708 | } |
| 1709 | |
| 1710 | strs.ptrs[strs.cnt].str = p; |
| 1711 | strs.ptrs[strs.cnt].used = false; |
| 1712 | |
| 1713 | p += strlen(p) + 1; |
| 1714 | strs.cnt++; |
| 1715 | } |
| 1716 | |
| 1717 | /* temporary storage for deduplicated strings */ |
| 1718 | tmp_strs = malloc(d->btf->hdr->str_len); |
| 1719 | if (!tmp_strs) { |
| 1720 | err = -ENOMEM; |
| 1721 | goto done; |
| 1722 | } |
| 1723 | |
| 1724 | /* mark all used strings */ |
| 1725 | strs.ptrs[0].used = true; |
| 1726 | err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs); |
| 1727 | if (err) |
| 1728 | goto done; |
| 1729 | |
| 1730 | /* sort strings by context, so that we can identify duplicates */ |
| 1731 | qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content); |
| 1732 | |
| 1733 | /* |
| 1734 | * iterate groups of equal strings and if any instance in a group was |
| 1735 | * referenced, emit single instance and remember new offset |
| 1736 | */ |
| 1737 | p = tmp_strs; |
| 1738 | grp_idx = 0; |
| 1739 | grp_used = strs.ptrs[0].used; |
| 1740 | /* iterate past end to avoid code duplication after loop */ |
| 1741 | for (i = 1; i <= strs.cnt; i++) { |
| 1742 | /* |
| 1743 | * when i == strs.cnt, we want to skip string comparison and go |
| 1744 | * straight to handling last group of strings (otherwise we'd |
| 1745 | * need to handle last group after the loop w/ duplicated code) |
| 1746 | */ |
| 1747 | if (i < strs.cnt && |
| 1748 | !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) { |
| 1749 | grp_used = grp_used || strs.ptrs[i].used; |
| 1750 | continue; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * this check would have been required after the loop to handle |
| 1755 | * last group of strings, but due to <= condition in a loop |
| 1756 | * we avoid that duplication |
| 1757 | */ |
| 1758 | if (grp_used) { |
| 1759 | int new_off = p - tmp_strs; |
| 1760 | __u32 len = strlen(strs.ptrs[grp_idx].str); |
| 1761 | |
| 1762 | memmove(p, strs.ptrs[grp_idx].str, len + 1); |
| 1763 | for (j = grp_idx; j < i; j++) |
| 1764 | strs.ptrs[j].new_off = new_off; |
| 1765 | p += len + 1; |
| 1766 | } |
| 1767 | |
| 1768 | if (i < strs.cnt) { |
| 1769 | grp_idx = i; |
| 1770 | grp_used = strs.ptrs[i].used; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | /* replace original strings with deduped ones */ |
| 1775 | d->btf->hdr->str_len = p - tmp_strs; |
| 1776 | memmove(start, tmp_strs, d->btf->hdr->str_len); |
| 1777 | end = start + d->btf->hdr->str_len; |
| 1778 | |
| 1779 | /* restore original order for further binary search lookups */ |
| 1780 | qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset); |
| 1781 | |
| 1782 | /* remap string offsets */ |
| 1783 | err = btf_for_each_str_off(d, btf_str_remap_offset, &strs); |
| 1784 | if (err) |
| 1785 | goto done; |
| 1786 | |
| 1787 | d->btf->hdr->str_len = end - start; |
| 1788 | |
| 1789 | done: |
| 1790 | free(tmp_strs); |
| 1791 | free(strs.ptrs); |
| 1792 | return err; |
| 1793 | } |
| 1794 | |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1795 | static long btf_hash_common(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1796 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1797 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1798 | |
| 1799 | h = hash_combine(0, t->name_off); |
| 1800 | h = hash_combine(h, t->info); |
| 1801 | h = hash_combine(h, t->size); |
| 1802 | return h; |
| 1803 | } |
| 1804 | |
| 1805 | static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2) |
| 1806 | { |
| 1807 | return t1->name_off == t2->name_off && |
| 1808 | t1->info == t2->info && |
| 1809 | t1->size == t2->size; |
| 1810 | } |
| 1811 | |
| 1812 | /* Calculate type signature hash of INT. */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1813 | static long btf_hash_int(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1814 | { |
| 1815 | __u32 info = *(__u32 *)(t + 1); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1816 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1817 | |
| 1818 | h = btf_hash_common(t); |
| 1819 | h = hash_combine(h, info); |
| 1820 | return h; |
| 1821 | } |
| 1822 | |
| 1823 | /* Check structural equality of two INTs. */ |
| 1824 | static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2) |
| 1825 | { |
| 1826 | __u32 info1, info2; |
| 1827 | |
| 1828 | if (!btf_equal_common(t1, t2)) |
| 1829 | return false; |
| 1830 | info1 = *(__u32 *)(t1 + 1); |
| 1831 | info2 = *(__u32 *)(t2 + 1); |
| 1832 | return info1 == info2; |
| 1833 | } |
| 1834 | |
| 1835 | /* Calculate type signature hash of ENUM. */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1836 | static long btf_hash_enum(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1837 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1838 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1839 | |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 1840 | /* don't hash vlen and enum members to support enum fwd resolving */ |
| 1841 | h = hash_combine(0, t->name_off); |
| 1842 | h = hash_combine(h, t->info & ~0xffff); |
| 1843 | h = hash_combine(h, t->size); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1844 | return h; |
| 1845 | } |
| 1846 | |
| 1847 | /* Check structural equality of two ENUMs. */ |
| 1848 | static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2) |
| 1849 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1850 | const struct btf_enum *m1, *m2; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1851 | __u16 vlen; |
| 1852 | int i; |
| 1853 | |
| 1854 | if (!btf_equal_common(t1, t2)) |
| 1855 | return false; |
| 1856 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1857 | vlen = btf_vlen(t1); |
| 1858 | m1 = btf_enum(t1); |
| 1859 | m2 = btf_enum(t2); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1860 | for (i = 0; i < vlen; i++) { |
| 1861 | if (m1->name_off != m2->name_off || m1->val != m2->val) |
| 1862 | return false; |
| 1863 | m1++; |
| 1864 | m2++; |
| 1865 | } |
| 1866 | return true; |
| 1867 | } |
| 1868 | |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 1869 | static inline bool btf_is_enum_fwd(struct btf_type *t) |
| 1870 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1871 | return btf_is_enum(t) && btf_vlen(t) == 0; |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2) |
| 1875 | { |
| 1876 | if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2)) |
| 1877 | return btf_equal_enum(t1, t2); |
| 1878 | /* ignore vlen when comparing */ |
| 1879 | return t1->name_off == t2->name_off && |
| 1880 | (t1->info & ~0xffff) == (t2->info & ~0xffff) && |
| 1881 | t1->size == t2->size; |
| 1882 | } |
| 1883 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1884 | /* |
| 1885 | * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs, |
| 1886 | * as referenced type IDs equivalence is established separately during type |
| 1887 | * graph equivalence check algorithm. |
| 1888 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1889 | static long btf_hash_struct(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1890 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1891 | const struct btf_member *member = btf_members(t); |
| 1892 | __u32 vlen = btf_vlen(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1893 | long h = btf_hash_common(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1894 | int i; |
| 1895 | |
| 1896 | for (i = 0; i < vlen; i++) { |
| 1897 | h = hash_combine(h, member->name_off); |
| 1898 | h = hash_combine(h, member->offset); |
| 1899 | /* no hashing of referenced type ID, it can be unresolved yet */ |
| 1900 | member++; |
| 1901 | } |
| 1902 | return h; |
| 1903 | } |
| 1904 | |
| 1905 | /* |
| 1906 | * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type |
| 1907 | * IDs. This check is performed during type graph equivalence check and |
| 1908 | * referenced types equivalence is checked separately. |
| 1909 | */ |
Andrii Nakryiko | 91097fb | 2019-02-28 15:31:24 -0800 | [diff] [blame] | 1910 | static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1911 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1912 | const struct btf_member *m1, *m2; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1913 | __u16 vlen; |
| 1914 | int i; |
| 1915 | |
| 1916 | if (!btf_equal_common(t1, t2)) |
| 1917 | return false; |
| 1918 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1919 | vlen = btf_vlen(t1); |
| 1920 | m1 = btf_members(t1); |
| 1921 | m2 = btf_members(t2); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1922 | for (i = 0; i < vlen; i++) { |
| 1923 | if (m1->name_off != m2->name_off || m1->offset != m2->offset) |
| 1924 | return false; |
| 1925 | m1++; |
| 1926 | m2++; |
| 1927 | } |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
| 1932 | * Calculate type signature hash of ARRAY, including referenced type IDs, |
| 1933 | * under assumption that they were already resolved to canonical type IDs and |
| 1934 | * are not going to change. |
| 1935 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1936 | static long btf_hash_array(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1937 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1938 | const struct btf_array *info = btf_array(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1939 | long h = btf_hash_common(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1940 | |
| 1941 | h = hash_combine(h, info->type); |
| 1942 | h = hash_combine(h, info->index_type); |
| 1943 | h = hash_combine(h, info->nelems); |
| 1944 | return h; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * Check exact equality of two ARRAYs, taking into account referenced |
| 1949 | * type IDs, under assumption that they were already resolved to canonical |
| 1950 | * type IDs and are not going to change. |
| 1951 | * This function is called during reference types deduplication to compare |
| 1952 | * ARRAY to potential canonical representative. |
| 1953 | */ |
| 1954 | static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2) |
| 1955 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1956 | const struct btf_array *info1, *info2; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1957 | |
| 1958 | if (!btf_equal_common(t1, t2)) |
| 1959 | return false; |
| 1960 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1961 | info1 = btf_array(t1); |
| 1962 | info2 = btf_array(t2); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1963 | return info1->type == info2->type && |
| 1964 | info1->index_type == info2->index_type && |
| 1965 | info1->nelems == info2->nelems; |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * Check structural compatibility of two ARRAYs, ignoring referenced type |
| 1970 | * IDs. This check is performed during type graph equivalence check and |
| 1971 | * referenced types equivalence is checked separately. |
| 1972 | */ |
| 1973 | static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2) |
| 1974 | { |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1975 | if (!btf_equal_common(t1, t2)) |
| 1976 | return false; |
| 1977 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1978 | return btf_array(t1)->nelems == btf_array(t2)->nelems; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * Calculate type signature hash of FUNC_PROTO, including referenced type IDs, |
| 1983 | * under assumption that they were already resolved to canonical type IDs and |
| 1984 | * are not going to change. |
| 1985 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1986 | static long btf_hash_fnproto(struct btf_type *t) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1987 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1988 | const struct btf_param *member = btf_params(t); |
| 1989 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 1990 | long h = btf_hash_common(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 1991 | int i; |
| 1992 | |
| 1993 | for (i = 0; i < vlen; i++) { |
| 1994 | h = hash_combine(h, member->name_off); |
| 1995 | h = hash_combine(h, member->type); |
| 1996 | member++; |
| 1997 | } |
| 1998 | return h; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Check exact equality of two FUNC_PROTOs, taking into account referenced |
| 2003 | * type IDs, under assumption that they were already resolved to canonical |
| 2004 | * type IDs and are not going to change. |
| 2005 | * This function is called during reference types deduplication to compare |
| 2006 | * FUNC_PROTO to potential canonical representative. |
| 2007 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2008 | static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2009 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2010 | const struct btf_param *m1, *m2; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2011 | __u16 vlen; |
| 2012 | int i; |
| 2013 | |
| 2014 | if (!btf_equal_common(t1, t2)) |
| 2015 | return false; |
| 2016 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2017 | vlen = btf_vlen(t1); |
| 2018 | m1 = btf_params(t1); |
| 2019 | m2 = btf_params(t2); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2020 | for (i = 0; i < vlen; i++) { |
| 2021 | if (m1->name_off != m2->name_off || m1->type != m2->type) |
| 2022 | return false; |
| 2023 | m1++; |
| 2024 | m2++; |
| 2025 | } |
| 2026 | return true; |
| 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type |
| 2031 | * IDs. This check is performed during type graph equivalence check and |
| 2032 | * referenced types equivalence is checked separately. |
| 2033 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2034 | static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2035 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2036 | const struct btf_param *m1, *m2; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2037 | __u16 vlen; |
| 2038 | int i; |
| 2039 | |
| 2040 | /* skip return type ID */ |
| 2041 | if (t1->name_off != t2->name_off || t1->info != t2->info) |
| 2042 | return false; |
| 2043 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2044 | vlen = btf_vlen(t1); |
| 2045 | m1 = btf_params(t1); |
| 2046 | m2 = btf_params(t2); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2047 | for (i = 0; i < vlen; i++) { |
| 2048 | if (m1->name_off != m2->name_off) |
| 2049 | return false; |
| 2050 | m1++; |
| 2051 | m2++; |
| 2052 | } |
| 2053 | return true; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Deduplicate primitive types, that can't reference other types, by calculating |
| 2058 | * their type signature hash and comparing them with any possible canonical |
| 2059 | * candidate. If no canonical candidate matches, type itself is marked as |
| 2060 | * canonical and is added into `btf_dedup->dedup_table` as another candidate. |
| 2061 | */ |
| 2062 | static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) |
| 2063 | { |
| 2064 | struct btf_type *t = d->btf->types[type_id]; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2065 | struct hashmap_entry *hash_entry; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2066 | struct btf_type *cand; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2067 | /* if we don't find equivalent type, then we are canonical */ |
| 2068 | __u32 new_id = type_id; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2069 | __u32 cand_id; |
| 2070 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2071 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2072 | switch (btf_kind(t)) { |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2073 | case BTF_KIND_CONST: |
| 2074 | case BTF_KIND_VOLATILE: |
| 2075 | case BTF_KIND_RESTRICT: |
| 2076 | case BTF_KIND_PTR: |
| 2077 | case BTF_KIND_TYPEDEF: |
| 2078 | case BTF_KIND_ARRAY: |
| 2079 | case BTF_KIND_STRUCT: |
| 2080 | case BTF_KIND_UNION: |
| 2081 | case BTF_KIND_FUNC: |
| 2082 | case BTF_KIND_FUNC_PROTO: |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 2083 | case BTF_KIND_VAR: |
| 2084 | case BTF_KIND_DATASEC: |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2085 | return 0; |
| 2086 | |
| 2087 | case BTF_KIND_INT: |
| 2088 | h = btf_hash_int(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2089 | for_each_dedup_cand(d, hash_entry, h) { |
| 2090 | cand_id = (__u32)(long)hash_entry->value; |
| 2091 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2092 | if (btf_equal_int(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2093 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | break; |
| 2098 | |
| 2099 | case BTF_KIND_ENUM: |
| 2100 | h = btf_hash_enum(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2101 | for_each_dedup_cand(d, hash_entry, h) { |
| 2102 | cand_id = (__u32)(long)hash_entry->value; |
| 2103 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2104 | if (btf_equal_enum(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2105 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2106 | break; |
| 2107 | } |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 2108 | if (d->opts.dont_resolve_fwds) |
| 2109 | continue; |
| 2110 | if (btf_compat_enum(t, cand)) { |
| 2111 | if (btf_is_enum_fwd(t)) { |
| 2112 | /* resolve fwd to full enum */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2113 | new_id = cand_id; |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 2114 | break; |
| 2115 | } |
| 2116 | /* resolve canonical enum fwd to full enum */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2117 | d->map[cand_id] = type_id; |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 2118 | } |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2119 | } |
| 2120 | break; |
| 2121 | |
| 2122 | case BTF_KIND_FWD: |
| 2123 | h = btf_hash_common(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2124 | for_each_dedup_cand(d, hash_entry, h) { |
| 2125 | cand_id = (__u32)(long)hash_entry->value; |
| 2126 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2127 | if (btf_equal_common(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2128 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2129 | break; |
| 2130 | } |
| 2131 | } |
| 2132 | break; |
| 2133 | |
| 2134 | default: |
| 2135 | return -EINVAL; |
| 2136 | } |
| 2137 | |
| 2138 | d->map[type_id] = new_id; |
| 2139 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 2140 | return -ENOMEM; |
| 2141 | |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | static int btf_dedup_prim_types(struct btf_dedup *d) |
| 2146 | { |
| 2147 | int i, err; |
| 2148 | |
| 2149 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2150 | err = btf_dedup_prim_type(d, i); |
| 2151 | if (err) |
| 2152 | return err; |
| 2153 | } |
| 2154 | return 0; |
| 2155 | } |
| 2156 | |
| 2157 | /* |
| 2158 | * Check whether type is already mapped into canonical one (could be to itself). |
| 2159 | */ |
| 2160 | static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id) |
| 2161 | { |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 2162 | return d->map[type_id] <= BTF_MAX_NR_TYPES; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | /* |
| 2166 | * Resolve type ID into its canonical type ID, if any; otherwise return original |
| 2167 | * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow |
| 2168 | * STRUCT/UNION link and resolve it into canonical type ID as well. |
| 2169 | */ |
| 2170 | static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id) |
| 2171 | { |
| 2172 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
| 2173 | type_id = d->map[type_id]; |
| 2174 | return type_id; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original |
| 2179 | * type ID. |
| 2180 | */ |
| 2181 | static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id) |
| 2182 | { |
| 2183 | __u32 orig_type_id = type_id; |
| 2184 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2185 | if (!btf_is_fwd(d->btf->types[type_id])) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2186 | return type_id; |
| 2187 | |
| 2188 | while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) |
| 2189 | type_id = d->map[type_id]; |
| 2190 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2191 | if (!btf_is_fwd(d->btf->types[type_id])) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2192 | return type_id; |
| 2193 | |
| 2194 | return orig_type_id; |
| 2195 | } |
| 2196 | |
| 2197 | |
| 2198 | static inline __u16 btf_fwd_kind(struct btf_type *t) |
| 2199 | { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2200 | return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Check equivalence of BTF type graph formed by candidate struct/union (we'll |
| 2205 | * call it "candidate graph" in this description for brevity) to a type graph |
| 2206 | * formed by (potential) canonical struct/union ("canonical graph" for brevity |
| 2207 | * here, though keep in mind that not all types in canonical graph are |
| 2208 | * necessarily canonical representatives themselves, some of them might be |
| 2209 | * duplicates or its uniqueness might not have been established yet). |
| 2210 | * Returns: |
| 2211 | * - >0, if type graphs are equivalent; |
| 2212 | * - 0, if not equivalent; |
| 2213 | * - <0, on error. |
| 2214 | * |
| 2215 | * Algorithm performs side-by-side DFS traversal of both type graphs and checks |
| 2216 | * equivalence of BTF types at each step. If at any point BTF types in candidate |
| 2217 | * and canonical graphs are not compatible structurally, whole graphs are |
| 2218 | * incompatible. If types are structurally equivalent (i.e., all information |
| 2219 | * except referenced type IDs is exactly the same), a mapping from `canon_id` to |
| 2220 | * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`). |
| 2221 | * If a type references other types, then those referenced types are checked |
| 2222 | * for equivalence recursively. |
| 2223 | * |
| 2224 | * During DFS traversal, if we find that for current `canon_id` type we |
| 2225 | * already have some mapping in hypothetical map, we check for two possible |
| 2226 | * situations: |
| 2227 | * - `canon_id` is mapped to exactly the same type as `cand_id`. This will |
| 2228 | * happen when type graphs have cycles. In this case we assume those two |
| 2229 | * types are equivalent. |
| 2230 | * - `canon_id` is mapped to different type. This is contradiction in our |
| 2231 | * hypothetical mapping, because same graph in canonical graph corresponds |
| 2232 | * to two different types in candidate graph, which for equivalent type |
| 2233 | * graphs shouldn't happen. This condition terminates equivalence check |
| 2234 | * with negative result. |
| 2235 | * |
| 2236 | * If type graphs traversal exhausts types to check and find no contradiction, |
| 2237 | * then type graphs are equivalent. |
| 2238 | * |
| 2239 | * When checking types for equivalence, there is one special case: FWD types. |
| 2240 | * If FWD type resolution is allowed and one of the types (either from canonical |
| 2241 | * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind |
| 2242 | * flag) and their names match, hypothetical mapping is updated to point from |
| 2243 | * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully, |
| 2244 | * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently. |
| 2245 | * |
| 2246 | * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution, |
| 2247 | * if there are two exactly named (or anonymous) structs/unions that are |
| 2248 | * compatible structurally, one of which has FWD field, while other is concrete |
| 2249 | * STRUCT/UNION, but according to C sources they are different structs/unions |
| 2250 | * that are referencing different types with the same name. This is extremely |
| 2251 | * unlikely to happen, but btf_dedup API allows to disable FWD resolution if |
| 2252 | * this logic is causing problems. |
| 2253 | * |
| 2254 | * Doing FWD resolution means that both candidate and/or canonical graphs can |
| 2255 | * consists of portions of the graph that come from multiple compilation units. |
| 2256 | * This is due to the fact that types within single compilation unit are always |
| 2257 | * deduplicated and FWDs are already resolved, if referenced struct/union |
| 2258 | * definiton is available. So, if we had unresolved FWD and found corresponding |
| 2259 | * STRUCT/UNION, they will be from different compilation units. This |
| 2260 | * consequently means that when we "link" FWD to corresponding STRUCT/UNION, |
| 2261 | * type graph will likely have at least two different BTF types that describe |
| 2262 | * same type (e.g., most probably there will be two different BTF types for the |
| 2263 | * same 'int' primitive type) and could even have "overlapping" parts of type |
| 2264 | * graph that describe same subset of types. |
| 2265 | * |
| 2266 | * This in turn means that our assumption that each type in canonical graph |
| 2267 | * must correspond to exactly one type in candidate graph might not hold |
| 2268 | * anymore and will make it harder to detect contradictions using hypothetical |
| 2269 | * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION |
| 2270 | * resolution only in canonical graph. FWDs in candidate graphs are never |
| 2271 | * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs |
| 2272 | * that can occur: |
| 2273 | * - Both types in canonical and candidate graphs are FWDs. If they are |
| 2274 | * structurally equivalent, then they can either be both resolved to the |
| 2275 | * same STRUCT/UNION or not resolved at all. In both cases they are |
| 2276 | * equivalent and there is no need to resolve FWD on candidate side. |
| 2277 | * - Both types in canonical and candidate graphs are concrete STRUCT/UNION, |
| 2278 | * so nothing to resolve as well, algorithm will check equivalence anyway. |
| 2279 | * - Type in canonical graph is FWD, while type in candidate is concrete |
| 2280 | * STRUCT/UNION. In this case candidate graph comes from single compilation |
| 2281 | * unit, so there is exactly one BTF type for each unique C type. After |
| 2282 | * resolving FWD into STRUCT/UNION, there might be more than one BTF type |
| 2283 | * in canonical graph mapping to single BTF type in candidate graph, but |
| 2284 | * because hypothetical mapping maps from canonical to candidate types, it's |
| 2285 | * alright, and we still maintain the property of having single `canon_id` |
| 2286 | * mapping to single `cand_id` (there could be two different `canon_id` |
| 2287 | * mapped to the same `cand_id`, but it's not contradictory). |
| 2288 | * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate |
| 2289 | * graph is FWD. In this case we are just going to check compatibility of |
| 2290 | * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll |
| 2291 | * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to |
| 2292 | * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs |
| 2293 | * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from |
| 2294 | * canonical graph. |
| 2295 | */ |
| 2296 | static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, |
| 2297 | __u32 canon_id) |
| 2298 | { |
| 2299 | struct btf_type *cand_type; |
| 2300 | struct btf_type *canon_type; |
| 2301 | __u32 hypot_type_id; |
| 2302 | __u16 cand_kind; |
| 2303 | __u16 canon_kind; |
| 2304 | int i, eq; |
| 2305 | |
| 2306 | /* if both resolve to the same canonical, they must be equivalent */ |
| 2307 | if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id)) |
| 2308 | return 1; |
| 2309 | |
| 2310 | canon_id = resolve_fwd_id(d, canon_id); |
| 2311 | |
| 2312 | hypot_type_id = d->hypot_map[canon_id]; |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 2313 | if (hypot_type_id <= BTF_MAX_NR_TYPES) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2314 | return hypot_type_id == cand_id; |
| 2315 | |
| 2316 | if (btf_dedup_hypot_map_add(d, canon_id, cand_id)) |
| 2317 | return -ENOMEM; |
| 2318 | |
| 2319 | cand_type = d->btf->types[cand_id]; |
| 2320 | canon_type = d->btf->types[canon_id]; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2321 | cand_kind = btf_kind(cand_type); |
| 2322 | canon_kind = btf_kind(canon_type); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2323 | |
| 2324 | if (cand_type->name_off != canon_type->name_off) |
| 2325 | return 0; |
| 2326 | |
| 2327 | /* FWD <--> STRUCT/UNION equivalence check, if enabled */ |
| 2328 | if (!d->opts.dont_resolve_fwds |
| 2329 | && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD) |
| 2330 | && cand_kind != canon_kind) { |
| 2331 | __u16 real_kind; |
| 2332 | __u16 fwd_kind; |
| 2333 | |
| 2334 | if (cand_kind == BTF_KIND_FWD) { |
| 2335 | real_kind = canon_kind; |
| 2336 | fwd_kind = btf_fwd_kind(cand_type); |
| 2337 | } else { |
| 2338 | real_kind = cand_kind; |
| 2339 | fwd_kind = btf_fwd_kind(canon_type); |
| 2340 | } |
| 2341 | return fwd_kind == real_kind; |
| 2342 | } |
| 2343 | |
Andrii Nakryiko | 9ec71c1 | 2019-03-26 22:00:06 -0700 | [diff] [blame] | 2344 | if (cand_kind != canon_kind) |
| 2345 | return 0; |
| 2346 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2347 | switch (cand_kind) { |
| 2348 | case BTF_KIND_INT: |
| 2349 | return btf_equal_int(cand_type, canon_type); |
| 2350 | |
| 2351 | case BTF_KIND_ENUM: |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 2352 | if (d->opts.dont_resolve_fwds) |
| 2353 | return btf_equal_enum(cand_type, canon_type); |
| 2354 | else |
| 2355 | return btf_compat_enum(cand_type, canon_type); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2356 | |
| 2357 | case BTF_KIND_FWD: |
| 2358 | return btf_equal_common(cand_type, canon_type); |
| 2359 | |
| 2360 | case BTF_KIND_CONST: |
| 2361 | case BTF_KIND_VOLATILE: |
| 2362 | case BTF_KIND_RESTRICT: |
| 2363 | case BTF_KIND_PTR: |
| 2364 | case BTF_KIND_TYPEDEF: |
| 2365 | case BTF_KIND_FUNC: |
Andrii Nakryiko | 9768095 | 2019-03-10 17:44:09 -0700 | [diff] [blame] | 2366 | if (cand_type->info != canon_type->info) |
| 2367 | return 0; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2368 | return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
| 2369 | |
| 2370 | case BTF_KIND_ARRAY: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2371 | const struct btf_array *cand_arr, *canon_arr; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2372 | |
| 2373 | if (!btf_compat_array(cand_type, canon_type)) |
| 2374 | return 0; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2375 | cand_arr = btf_array(cand_type); |
| 2376 | canon_arr = btf_array(canon_type); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2377 | eq = btf_dedup_is_equiv(d, |
| 2378 | cand_arr->index_type, canon_arr->index_type); |
| 2379 | if (eq <= 0) |
| 2380 | return eq; |
| 2381 | return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); |
| 2382 | } |
| 2383 | |
| 2384 | case BTF_KIND_STRUCT: |
| 2385 | case BTF_KIND_UNION: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2386 | const struct btf_member *cand_m, *canon_m; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2387 | __u16 vlen; |
| 2388 | |
Andrii Nakryiko | 91097fb | 2019-02-28 15:31:24 -0800 | [diff] [blame] | 2389 | if (!btf_shallow_equal_struct(cand_type, canon_type)) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2390 | return 0; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2391 | vlen = btf_vlen(cand_type); |
| 2392 | cand_m = btf_members(cand_type); |
| 2393 | canon_m = btf_members(canon_type); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2394 | for (i = 0; i < vlen; i++) { |
| 2395 | eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); |
| 2396 | if (eq <= 0) |
| 2397 | return eq; |
| 2398 | cand_m++; |
| 2399 | canon_m++; |
| 2400 | } |
| 2401 | |
| 2402 | return 1; |
| 2403 | } |
| 2404 | |
| 2405 | case BTF_KIND_FUNC_PROTO: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2406 | const struct btf_param *cand_p, *canon_p; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2407 | __u16 vlen; |
| 2408 | |
| 2409 | if (!btf_compat_fnproto(cand_type, canon_type)) |
| 2410 | return 0; |
| 2411 | eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); |
| 2412 | if (eq <= 0) |
| 2413 | return eq; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2414 | vlen = btf_vlen(cand_type); |
| 2415 | cand_p = btf_params(cand_type); |
| 2416 | canon_p = btf_params(canon_type); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2417 | for (i = 0; i < vlen; i++) { |
| 2418 | eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); |
| 2419 | if (eq <= 0) |
| 2420 | return eq; |
| 2421 | cand_p++; |
| 2422 | canon_p++; |
| 2423 | } |
| 2424 | return 1; |
| 2425 | } |
| 2426 | |
| 2427 | default: |
| 2428 | return -EINVAL; |
| 2429 | } |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * Use hypothetical mapping, produced by successful type graph equivalence |
| 2435 | * check, to augment existing struct/union canonical mapping, where possible. |
| 2436 | * |
| 2437 | * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record |
| 2438 | * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional: |
| 2439 | * it doesn't matter if FWD type was part of canonical graph or candidate one, |
| 2440 | * we are recording the mapping anyway. As opposed to carefulness required |
| 2441 | * for struct/union correspondence mapping (described below), for FWD resolution |
| 2442 | * it's not important, as by the time that FWD type (reference type) will be |
| 2443 | * deduplicated all structs/unions will be deduped already anyway. |
| 2444 | * |
| 2445 | * Recording STRUCT/UNION mapping is purely a performance optimization and is |
| 2446 | * not required for correctness. It needs to be done carefully to ensure that |
| 2447 | * struct/union from candidate's type graph is not mapped into corresponding |
| 2448 | * struct/union from canonical type graph that itself hasn't been resolved into |
| 2449 | * canonical representative. The only guarantee we have is that canonical |
| 2450 | * struct/union was determined as canonical and that won't change. But any |
| 2451 | * types referenced through that struct/union fields could have been not yet |
| 2452 | * resolved, so in case like that it's too early to establish any kind of |
| 2453 | * correspondence between structs/unions. |
| 2454 | * |
| 2455 | * No canonical correspondence is derived for primitive types (they are already |
| 2456 | * deduplicated completely already anyway) or reference types (they rely on |
| 2457 | * stability of struct/union canonical relationship for equivalence checks). |
| 2458 | */ |
| 2459 | static void btf_dedup_merge_hypot_map(struct btf_dedup *d) |
| 2460 | { |
| 2461 | __u32 cand_type_id, targ_type_id; |
| 2462 | __u16 t_kind, c_kind; |
| 2463 | __u32 t_id, c_id; |
| 2464 | int i; |
| 2465 | |
| 2466 | for (i = 0; i < d->hypot_cnt; i++) { |
| 2467 | cand_type_id = d->hypot_list[i]; |
| 2468 | targ_type_id = d->hypot_map[cand_type_id]; |
| 2469 | t_id = resolve_type_id(d, targ_type_id); |
| 2470 | c_id = resolve_type_id(d, cand_type_id); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2471 | t_kind = btf_kind(d->btf->types[t_id]); |
| 2472 | c_kind = btf_kind(d->btf->types[c_id]); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2473 | /* |
| 2474 | * Resolve FWD into STRUCT/UNION. |
| 2475 | * It's ok to resolve FWD into STRUCT/UNION that's not yet |
| 2476 | * mapped to canonical representative (as opposed to |
| 2477 | * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because |
| 2478 | * eventually that struct is going to be mapped and all resolved |
| 2479 | * FWDs will automatically resolve to correct canonical |
| 2480 | * representative. This will happen before ref type deduping, |
| 2481 | * which critically depends on stability of these mapping. This |
| 2482 | * stability is not a requirement for STRUCT/UNION equivalence |
| 2483 | * checks, though. |
| 2484 | */ |
| 2485 | if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD) |
| 2486 | d->map[c_id] = t_id; |
| 2487 | else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD) |
| 2488 | d->map[t_id] = c_id; |
| 2489 | |
| 2490 | if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) && |
| 2491 | c_kind != BTF_KIND_FWD && |
| 2492 | is_type_mapped(d, c_id) && |
| 2493 | !is_type_mapped(d, t_id)) { |
| 2494 | /* |
| 2495 | * as a perf optimization, we can map struct/union |
| 2496 | * that's part of type graph we just verified for |
| 2497 | * equivalence. We can do that for struct/union that has |
| 2498 | * canonical representative only, though. |
| 2499 | */ |
| 2500 | d->map[t_id] = c_id; |
| 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | * Deduplicate struct/union types. |
| 2507 | * |
| 2508 | * For each struct/union type its type signature hash is calculated, taking |
| 2509 | * into account type's name, size, number, order and names of fields, but |
| 2510 | * ignoring type ID's referenced from fields, because they might not be deduped |
| 2511 | * completely until after reference types deduplication phase. This type hash |
| 2512 | * is used to iterate over all potential canonical types, sharing same hash. |
| 2513 | * For each canonical candidate we check whether type graphs that they form |
| 2514 | * (through referenced types in fields and so on) are equivalent using algorithm |
| 2515 | * implemented in `btf_dedup_is_equiv`. If such equivalence is found and |
| 2516 | * BTF_KIND_FWD resolution is allowed, then hypothetical mapping |
| 2517 | * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence |
| 2518 | * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to |
| 2519 | * potentially map other structs/unions to their canonical representatives, |
| 2520 | * if such relationship hasn't yet been established. This speeds up algorithm |
| 2521 | * by eliminating some of the duplicate work. |
| 2522 | * |
| 2523 | * If no matching canonical representative was found, struct/union is marked |
| 2524 | * as canonical for itself and is added into btf_dedup->dedup_table hash map |
| 2525 | * for further look ups. |
| 2526 | */ |
| 2527 | static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id) |
| 2528 | { |
Andrii Nakryiko | 91097fb | 2019-02-28 15:31:24 -0800 | [diff] [blame] | 2529 | struct btf_type *cand_type, *t; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2530 | struct hashmap_entry *hash_entry; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2531 | /* if we don't find equivalent type, then we are canonical */ |
| 2532 | __u32 new_id = type_id; |
| 2533 | __u16 kind; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2534 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2535 | |
| 2536 | /* already deduped or is in process of deduping (loop detected) */ |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 2537 | if (d->map[type_id] <= BTF_MAX_NR_TYPES) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2538 | return 0; |
| 2539 | |
| 2540 | t = d->btf->types[type_id]; |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2541 | kind = btf_kind(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2542 | |
| 2543 | if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION) |
| 2544 | return 0; |
| 2545 | |
| 2546 | h = btf_hash_struct(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2547 | for_each_dedup_cand(d, hash_entry, h) { |
| 2548 | __u32 cand_id = (__u32)(long)hash_entry->value; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2549 | int eq; |
| 2550 | |
Andrii Nakryiko | 91097fb | 2019-02-28 15:31:24 -0800 | [diff] [blame] | 2551 | /* |
| 2552 | * Even though btf_dedup_is_equiv() checks for |
| 2553 | * btf_shallow_equal_struct() internally when checking two |
| 2554 | * structs (unions) for equivalence, we need to guard here |
| 2555 | * from picking matching FWD type as a dedup candidate. |
| 2556 | * This can happen due to hash collision. In such case just |
| 2557 | * relying on btf_dedup_is_equiv() would lead to potentially |
| 2558 | * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because |
| 2559 | * FWD and compatible STRUCT/UNION are considered equivalent. |
| 2560 | */ |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2561 | cand_type = d->btf->types[cand_id]; |
Andrii Nakryiko | 91097fb | 2019-02-28 15:31:24 -0800 | [diff] [blame] | 2562 | if (!btf_shallow_equal_struct(t, cand_type)) |
| 2563 | continue; |
| 2564 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2565 | btf_dedup_clear_hypot_map(d); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2566 | eq = btf_dedup_is_equiv(d, type_id, cand_id); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2567 | if (eq < 0) |
| 2568 | return eq; |
| 2569 | if (!eq) |
| 2570 | continue; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2571 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2572 | btf_dedup_merge_hypot_map(d); |
| 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | d->map[type_id] = new_id; |
| 2577 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 2578 | return -ENOMEM; |
| 2579 | |
| 2580 | return 0; |
| 2581 | } |
| 2582 | |
| 2583 | static int btf_dedup_struct_types(struct btf_dedup *d) |
| 2584 | { |
| 2585 | int i, err; |
| 2586 | |
| 2587 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2588 | err = btf_dedup_struct_type(d, i); |
| 2589 | if (err) |
| 2590 | return err; |
| 2591 | } |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
| 2595 | /* |
| 2596 | * Deduplicate reference type. |
| 2597 | * |
| 2598 | * Once all primitive and struct/union types got deduplicated, we can easily |
| 2599 | * deduplicate all other (reference) BTF types. This is done in two steps: |
| 2600 | * |
| 2601 | * 1. Resolve all referenced type IDs into their canonical type IDs. This |
| 2602 | * resolution can be done either immediately for primitive or struct/union types |
| 2603 | * (because they were deduped in previous two phases) or recursively for |
| 2604 | * reference types. Recursion will always terminate at either primitive or |
| 2605 | * struct/union type, at which point we can "unwind" chain of reference types |
| 2606 | * one by one. There is no danger of encountering cycles because in C type |
| 2607 | * system the only way to form type cycle is through struct/union, so any chain |
| 2608 | * of reference types, even those taking part in a type cycle, will inevitably |
| 2609 | * reach struct/union at some point. |
| 2610 | * |
| 2611 | * 2. Once all referenced type IDs are resolved into canonical ones, BTF type |
| 2612 | * becomes "stable", in the sense that no further deduplication will cause |
| 2613 | * any changes to it. With that, it's now possible to calculate type's signature |
| 2614 | * hash (this time taking into account referenced type IDs) and loop over all |
| 2615 | * potential canonical representatives. If no match was found, current type |
| 2616 | * will become canonical representative of itself and will be added into |
| 2617 | * btf_dedup->dedup_table as another possible canonical representative. |
| 2618 | */ |
| 2619 | static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id) |
| 2620 | { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2621 | struct hashmap_entry *hash_entry; |
| 2622 | __u32 new_id = type_id, cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2623 | struct btf_type *t, *cand; |
| 2624 | /* if we don't find equivalent type, then we are representative type */ |
Dan Carpenter | 3d8669e | 2019-02-28 21:06:47 +0300 | [diff] [blame] | 2625 | int ref_type_id; |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2626 | long h; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2627 | |
| 2628 | if (d->map[type_id] == BTF_IN_PROGRESS_ID) |
| 2629 | return -ELOOP; |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 2630 | if (d->map[type_id] <= BTF_MAX_NR_TYPES) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2631 | return resolve_type_id(d, type_id); |
| 2632 | |
| 2633 | t = d->btf->types[type_id]; |
| 2634 | d->map[type_id] = BTF_IN_PROGRESS_ID; |
| 2635 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2636 | switch (btf_kind(t)) { |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2637 | case BTF_KIND_CONST: |
| 2638 | case BTF_KIND_VOLATILE: |
| 2639 | case BTF_KIND_RESTRICT: |
| 2640 | case BTF_KIND_PTR: |
| 2641 | case BTF_KIND_TYPEDEF: |
| 2642 | case BTF_KIND_FUNC: |
| 2643 | ref_type_id = btf_dedup_ref_type(d, t->type); |
| 2644 | if (ref_type_id < 0) |
| 2645 | return ref_type_id; |
| 2646 | t->type = ref_type_id; |
| 2647 | |
| 2648 | h = btf_hash_common(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2649 | for_each_dedup_cand(d, hash_entry, h) { |
| 2650 | cand_id = (__u32)(long)hash_entry->value; |
| 2651 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2652 | if (btf_equal_common(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2653 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2654 | break; |
| 2655 | } |
| 2656 | } |
| 2657 | break; |
| 2658 | |
| 2659 | case BTF_KIND_ARRAY: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2660 | struct btf_array *info = btf_array(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2661 | |
| 2662 | ref_type_id = btf_dedup_ref_type(d, info->type); |
| 2663 | if (ref_type_id < 0) |
| 2664 | return ref_type_id; |
| 2665 | info->type = ref_type_id; |
| 2666 | |
| 2667 | ref_type_id = btf_dedup_ref_type(d, info->index_type); |
| 2668 | if (ref_type_id < 0) |
| 2669 | return ref_type_id; |
| 2670 | info->index_type = ref_type_id; |
| 2671 | |
| 2672 | h = btf_hash_array(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2673 | for_each_dedup_cand(d, hash_entry, h) { |
| 2674 | cand_id = (__u32)(long)hash_entry->value; |
| 2675 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2676 | if (btf_equal_array(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2677 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2678 | break; |
| 2679 | } |
| 2680 | } |
| 2681 | break; |
| 2682 | } |
| 2683 | |
| 2684 | case BTF_KIND_FUNC_PROTO: { |
| 2685 | struct btf_param *param; |
| 2686 | __u16 vlen; |
| 2687 | int i; |
| 2688 | |
| 2689 | ref_type_id = btf_dedup_ref_type(d, t->type); |
| 2690 | if (ref_type_id < 0) |
| 2691 | return ref_type_id; |
| 2692 | t->type = ref_type_id; |
| 2693 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2694 | vlen = btf_vlen(t); |
| 2695 | param = btf_params(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2696 | for (i = 0; i < vlen; i++) { |
| 2697 | ref_type_id = btf_dedup_ref_type(d, param->type); |
| 2698 | if (ref_type_id < 0) |
| 2699 | return ref_type_id; |
| 2700 | param->type = ref_type_id; |
| 2701 | param++; |
| 2702 | } |
| 2703 | |
| 2704 | h = btf_hash_fnproto(t); |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2705 | for_each_dedup_cand(d, hash_entry, h) { |
| 2706 | cand_id = (__u32)(long)hash_entry->value; |
| 2707 | cand = d->btf->types[cand_id]; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2708 | if (btf_equal_fnproto(t, cand)) { |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2709 | new_id = cand_id; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2710 | break; |
| 2711 | } |
| 2712 | } |
| 2713 | break; |
| 2714 | } |
| 2715 | |
| 2716 | default: |
| 2717 | return -EINVAL; |
| 2718 | } |
| 2719 | |
| 2720 | d->map[type_id] = new_id; |
| 2721 | if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) |
| 2722 | return -ENOMEM; |
| 2723 | |
| 2724 | return new_id; |
| 2725 | } |
| 2726 | |
| 2727 | static int btf_dedup_ref_types(struct btf_dedup *d) |
| 2728 | { |
| 2729 | int i, err; |
| 2730 | |
| 2731 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2732 | err = btf_dedup_ref_type(d, i); |
| 2733 | if (err < 0) |
| 2734 | return err; |
| 2735 | } |
Andrii Nakryiko | 2fc3fc0 | 2019-05-24 11:59:02 -0700 | [diff] [blame] | 2736 | /* we won't need d->dedup_table anymore */ |
| 2737 | hashmap__free(d->dedup_table); |
| 2738 | d->dedup_table = NULL; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | * Compact types. |
| 2744 | * |
| 2745 | * After we established for each type its corresponding canonical representative |
| 2746 | * type, we now can eliminate types that are not canonical and leave only |
| 2747 | * canonical ones layed out sequentially in memory by copying them over |
| 2748 | * duplicates. During compaction btf_dedup->hypot_map array is reused to store |
| 2749 | * a map from original type ID to a new compacted type ID, which will be used |
| 2750 | * during next phase to "fix up" type IDs, referenced from struct/union and |
| 2751 | * reference types. |
| 2752 | */ |
| 2753 | static int btf_dedup_compact_types(struct btf_dedup *d) |
| 2754 | { |
| 2755 | struct btf_type **new_types; |
| 2756 | __u32 next_type_id = 1; |
| 2757 | char *types_start, *p; |
| 2758 | int i, len; |
| 2759 | |
| 2760 | /* we are going to reuse hypot_map to store compaction remapping */ |
| 2761 | d->hypot_map[0] = 0; |
| 2762 | for (i = 1; i <= d->btf->nr_types; i++) |
| 2763 | d->hypot_map[i] = BTF_UNPROCESSED_ID; |
| 2764 | |
| 2765 | types_start = d->btf->nohdr_data + d->btf->hdr->type_off; |
| 2766 | p = types_start; |
| 2767 | |
| 2768 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2769 | if (d->map[i] != i) |
| 2770 | continue; |
| 2771 | |
| 2772 | len = btf_type_size(d->btf->types[i]); |
| 2773 | if (len < 0) |
| 2774 | return len; |
| 2775 | |
| 2776 | memmove(p, d->btf->types[i], len); |
| 2777 | d->hypot_map[i] = next_type_id; |
| 2778 | d->btf->types[next_type_id] = (struct btf_type *)p; |
| 2779 | p += len; |
| 2780 | next_type_id++; |
| 2781 | } |
| 2782 | |
| 2783 | /* shrink struct btf's internal types index and update btf_header */ |
| 2784 | d->btf->nr_types = next_type_id - 1; |
| 2785 | d->btf->types_size = d->btf->nr_types; |
| 2786 | d->btf->hdr->type_len = p - types_start; |
| 2787 | new_types = realloc(d->btf->types, |
| 2788 | (1 + d->btf->nr_types) * sizeof(struct btf_type *)); |
| 2789 | if (!new_types) |
| 2790 | return -ENOMEM; |
| 2791 | d->btf->types = new_types; |
| 2792 | |
| 2793 | /* make sure string section follows type information without gaps */ |
| 2794 | d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data; |
| 2795 | memmove(p, d->btf->strings, d->btf->hdr->str_len); |
| 2796 | d->btf->strings = p; |
| 2797 | p += d->btf->hdr->str_len; |
| 2798 | |
| 2799 | d->btf->data_size = p - (char *)d->btf->data; |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | /* |
| 2804 | * Figure out final (deduplicated and compacted) type ID for provided original |
| 2805 | * `type_id` by first resolving it into corresponding canonical type ID and |
| 2806 | * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map, |
| 2807 | * which is populated during compaction phase. |
| 2808 | */ |
| 2809 | static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id) |
| 2810 | { |
| 2811 | __u32 resolved_type_id, new_type_id; |
| 2812 | |
| 2813 | resolved_type_id = resolve_type_id(d, type_id); |
| 2814 | new_type_id = d->hypot_map[resolved_type_id]; |
Andrii Nakryiko | 5aab392 | 2019-02-15 19:52:18 -0800 | [diff] [blame] | 2815 | if (new_type_id > BTF_MAX_NR_TYPES) |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2816 | return -EINVAL; |
| 2817 | return new_type_id; |
| 2818 | } |
| 2819 | |
| 2820 | /* |
| 2821 | * Remap referenced type IDs into deduped type IDs. |
| 2822 | * |
| 2823 | * After BTF types are deduplicated and compacted, their final type IDs may |
| 2824 | * differ from original ones. The map from original to a corresponding |
| 2825 | * deduped type ID is stored in btf_dedup->hypot_map and is populated during |
| 2826 | * compaction phase. During remapping phase we are rewriting all type IDs |
| 2827 | * referenced from any BTF type (e.g., struct fields, func proto args, etc) to |
| 2828 | * their final deduped type IDs. |
| 2829 | */ |
| 2830 | static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id) |
| 2831 | { |
| 2832 | struct btf_type *t = d->btf->types[type_id]; |
| 2833 | int i, r; |
| 2834 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2835 | switch (btf_kind(t)) { |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2836 | case BTF_KIND_INT: |
| 2837 | case BTF_KIND_ENUM: |
| 2838 | break; |
| 2839 | |
| 2840 | case BTF_KIND_FWD: |
| 2841 | case BTF_KIND_CONST: |
| 2842 | case BTF_KIND_VOLATILE: |
| 2843 | case BTF_KIND_RESTRICT: |
| 2844 | case BTF_KIND_PTR: |
| 2845 | case BTF_KIND_TYPEDEF: |
| 2846 | case BTF_KIND_FUNC: |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 2847 | case BTF_KIND_VAR: |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2848 | r = btf_dedup_remap_type_id(d, t->type); |
| 2849 | if (r < 0) |
| 2850 | return r; |
| 2851 | t->type = r; |
| 2852 | break; |
| 2853 | |
| 2854 | case BTF_KIND_ARRAY: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2855 | struct btf_array *arr_info = btf_array(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2856 | |
| 2857 | r = btf_dedup_remap_type_id(d, arr_info->type); |
| 2858 | if (r < 0) |
| 2859 | return r; |
| 2860 | arr_info->type = r; |
| 2861 | r = btf_dedup_remap_type_id(d, arr_info->index_type); |
| 2862 | if (r < 0) |
| 2863 | return r; |
| 2864 | arr_info->index_type = r; |
| 2865 | break; |
| 2866 | } |
| 2867 | |
| 2868 | case BTF_KIND_STRUCT: |
| 2869 | case BTF_KIND_UNION: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2870 | struct btf_member *member = btf_members(t); |
| 2871 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2872 | |
| 2873 | for (i = 0; i < vlen; i++) { |
| 2874 | r = btf_dedup_remap_type_id(d, member->type); |
| 2875 | if (r < 0) |
| 2876 | return r; |
| 2877 | member->type = r; |
| 2878 | member++; |
| 2879 | } |
| 2880 | break; |
| 2881 | } |
| 2882 | |
| 2883 | case BTF_KIND_FUNC_PROTO: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2884 | struct btf_param *param = btf_params(t); |
| 2885 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2886 | |
| 2887 | r = btf_dedup_remap_type_id(d, t->type); |
| 2888 | if (r < 0) |
| 2889 | return r; |
| 2890 | t->type = r; |
| 2891 | |
| 2892 | for (i = 0; i < vlen; i++) { |
| 2893 | r = btf_dedup_remap_type_id(d, param->type); |
| 2894 | if (r < 0) |
| 2895 | return r; |
| 2896 | param->type = r; |
| 2897 | param++; |
| 2898 | } |
| 2899 | break; |
| 2900 | } |
| 2901 | |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 2902 | case BTF_KIND_DATASEC: { |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2903 | struct btf_var_secinfo *var = btf_var_secinfos(t); |
| 2904 | __u16 vlen = btf_vlen(t); |
Andrii Nakryiko | 189cf5a | 2019-04-15 16:48:07 -0700 | [diff] [blame] | 2905 | |
| 2906 | for (i = 0; i < vlen; i++) { |
| 2907 | r = btf_dedup_remap_type_id(d, var->type); |
| 2908 | if (r < 0) |
| 2909 | return r; |
| 2910 | var->type = r; |
| 2911 | var++; |
| 2912 | } |
| 2913 | break; |
| 2914 | } |
| 2915 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 2916 | default: |
| 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
| 2923 | static int btf_dedup_remap_types(struct btf_dedup *d) |
| 2924 | { |
| 2925 | int i, r; |
| 2926 | |
| 2927 | for (i = 1; i <= d->btf->nr_types; i++) { |
| 2928 | r = btf_dedup_remap_type(d, i); |
| 2929 | if (r < 0) |
| 2930 | return r; |
| 2931 | } |
| 2932 | return 0; |
| 2933 | } |