Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
2 | | |
3 | | /* |
4 | | * Common eBPF ELF object loading operations. |
5 | | * |
6 | | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
7 | | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
8 | | * Copyright (C) 2015 Huawei Inc. |
9 | | * Copyright (C) 2017 Nicira, Inc. |
10 | | * Copyright (C) 2019 Isovalent, Inc. |
11 | | */ |
12 | | |
13 | | #ifndef _GNU_SOURCE |
14 | | #define _GNU_SOURCE |
15 | | #endif |
16 | | #include <stdlib.h> |
17 | | #include <stdio.h> |
18 | | #include <stdarg.h> |
19 | | #include <libgen.h> |
20 | | #include <inttypes.h> |
21 | | #include <limits.h> |
22 | | #include <string.h> |
23 | | #include <unistd.h> |
24 | | #include <endian.h> |
25 | | #include <fcntl.h> |
26 | | #include <errno.h> |
27 | | #include <ctype.h> |
28 | | #include <asm/unistd.h> |
29 | | #include <linux/err.h> |
30 | | #include <linux/kernel.h> |
31 | | #include <linux/bpf.h> |
32 | | #include <linux/btf.h> |
33 | | #include <linux/filter.h> |
34 | | #include <linux/limits.h> |
35 | | #include <linux/perf_event.h> |
36 | | #include <linux/bpf_perf_event.h> |
37 | | #include <linux/ring_buffer.h> |
38 | | #include <sys/epoll.h> |
39 | | #include <sys/ioctl.h> |
40 | | #include <sys/mman.h> |
41 | | #include <sys/stat.h> |
42 | | #include <sys/types.h> |
43 | | #include <sys/vfs.h> |
44 | | #include <sys/utsname.h> |
45 | | #include <sys/resource.h> |
46 | | #include <libelf.h> |
47 | | #include <gelf.h> |
48 | | #include <zlib.h> |
49 | | |
50 | | #include "libbpf.h" |
51 | | #include "bpf.h" |
52 | | #include "btf.h" |
53 | | #include "str_error.h" |
54 | | #include "libbpf_internal.h" |
55 | | #include "hashmap.h" |
56 | | #include "bpf_gen_internal.h" |
57 | | #include "zip.h" |
58 | | |
59 | | #ifndef BPF_FS_MAGIC |
60 | 0 | #define BPF_FS_MAGIC 0xcafe4a11 |
61 | | #endif |
62 | | |
63 | 1 | #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf" |
64 | | |
65 | 91.2k | #define BPF_INSN_SZ (sizeof(struct bpf_insn)) |
66 | | |
67 | | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
68 | | * compilation if user enables corresponding warning. Disable it explicitly. |
69 | | */ |
70 | | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
71 | | |
72 | | #define __printf(a, b) __attribute__((format(printf, a, b))) |
73 | | |
74 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj); |
75 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog); |
76 | | static int map_set_def_max_entries(struct bpf_map *map); |
77 | | |
78 | | static const char * const attach_type_name[] = { |
79 | | [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress", |
80 | | [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress", |
81 | | [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create", |
82 | | [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release", |
83 | | [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops", |
84 | | [BPF_CGROUP_DEVICE] = "cgroup_device", |
85 | | [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind", |
86 | | [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind", |
87 | | [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect", |
88 | | [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect", |
89 | | [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect", |
90 | | [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind", |
91 | | [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind", |
92 | | [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername", |
93 | | [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername", |
94 | | [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername", |
95 | | [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname", |
96 | | [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname", |
97 | | [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname", |
98 | | [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg", |
99 | | [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg", |
100 | | [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg", |
101 | | [BPF_CGROUP_SYSCTL] = "cgroup_sysctl", |
102 | | [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg", |
103 | | [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg", |
104 | | [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg", |
105 | | [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt", |
106 | | [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt", |
107 | | [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser", |
108 | | [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict", |
109 | | [BPF_SK_SKB_VERDICT] = "sk_skb_verdict", |
110 | | [BPF_SK_MSG_VERDICT] = "sk_msg_verdict", |
111 | | [BPF_LIRC_MODE2] = "lirc_mode2", |
112 | | [BPF_FLOW_DISSECTOR] = "flow_dissector", |
113 | | [BPF_TRACE_RAW_TP] = "trace_raw_tp", |
114 | | [BPF_TRACE_FENTRY] = "trace_fentry", |
115 | | [BPF_TRACE_FEXIT] = "trace_fexit", |
116 | | [BPF_MODIFY_RETURN] = "modify_return", |
117 | | [BPF_LSM_MAC] = "lsm_mac", |
118 | | [BPF_LSM_CGROUP] = "lsm_cgroup", |
119 | | [BPF_SK_LOOKUP] = "sk_lookup", |
120 | | [BPF_TRACE_ITER] = "trace_iter", |
121 | | [BPF_XDP_DEVMAP] = "xdp_devmap", |
122 | | [BPF_XDP_CPUMAP] = "xdp_cpumap", |
123 | | [BPF_XDP] = "xdp", |
124 | | [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select", |
125 | | [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate", |
126 | | [BPF_PERF_EVENT] = "perf_event", |
127 | | [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi", |
128 | | [BPF_STRUCT_OPS] = "struct_ops", |
129 | | [BPF_NETFILTER] = "netfilter", |
130 | | [BPF_TCX_INGRESS] = "tcx_ingress", |
131 | | [BPF_TCX_EGRESS] = "tcx_egress", |
132 | | [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi", |
133 | | [BPF_NETKIT_PRIMARY] = "netkit_primary", |
134 | | [BPF_NETKIT_PEER] = "netkit_peer", |
135 | | [BPF_TRACE_KPROBE_SESSION] = "trace_kprobe_session", |
136 | | }; |
137 | | |
138 | | static const char * const link_type_name[] = { |
139 | | [BPF_LINK_TYPE_UNSPEC] = "unspec", |
140 | | [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
141 | | [BPF_LINK_TYPE_TRACING] = "tracing", |
142 | | [BPF_LINK_TYPE_CGROUP] = "cgroup", |
143 | | [BPF_LINK_TYPE_ITER] = "iter", |
144 | | [BPF_LINK_TYPE_NETNS] = "netns", |
145 | | [BPF_LINK_TYPE_XDP] = "xdp", |
146 | | [BPF_LINK_TYPE_PERF_EVENT] = "perf_event", |
147 | | [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi", |
148 | | [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops", |
149 | | [BPF_LINK_TYPE_NETFILTER] = "netfilter", |
150 | | [BPF_LINK_TYPE_TCX] = "tcx", |
151 | | [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi", |
152 | | [BPF_LINK_TYPE_NETKIT] = "netkit", |
153 | | [BPF_LINK_TYPE_SOCKMAP] = "sockmap", |
154 | | }; |
155 | | |
156 | | static const char * const map_type_name[] = { |
157 | | [BPF_MAP_TYPE_UNSPEC] = "unspec", |
158 | | [BPF_MAP_TYPE_HASH] = "hash", |
159 | | [BPF_MAP_TYPE_ARRAY] = "array", |
160 | | [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", |
161 | | [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", |
162 | | [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", |
163 | | [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", |
164 | | [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", |
165 | | [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", |
166 | | [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", |
167 | | [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", |
168 | | [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", |
169 | | [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", |
170 | | [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", |
171 | | [BPF_MAP_TYPE_DEVMAP] = "devmap", |
172 | | [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash", |
173 | | [BPF_MAP_TYPE_SOCKMAP] = "sockmap", |
174 | | [BPF_MAP_TYPE_CPUMAP] = "cpumap", |
175 | | [BPF_MAP_TYPE_XSKMAP] = "xskmap", |
176 | | [BPF_MAP_TYPE_SOCKHASH] = "sockhash", |
177 | | [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage", |
178 | | [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray", |
179 | | [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage", |
180 | | [BPF_MAP_TYPE_QUEUE] = "queue", |
181 | | [BPF_MAP_TYPE_STACK] = "stack", |
182 | | [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", |
183 | | [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", |
184 | | [BPF_MAP_TYPE_RINGBUF] = "ringbuf", |
185 | | [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", |
186 | | [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", |
187 | | [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter", |
188 | | [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf", |
189 | | [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage", |
190 | | [BPF_MAP_TYPE_ARENA] = "arena", |
191 | | }; |
192 | | |
193 | | static const char * const prog_type_name[] = { |
194 | | [BPF_PROG_TYPE_UNSPEC] = "unspec", |
195 | | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", |
196 | | [BPF_PROG_TYPE_KPROBE] = "kprobe", |
197 | | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", |
198 | | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", |
199 | | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", |
200 | | [BPF_PROG_TYPE_XDP] = "xdp", |
201 | | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", |
202 | | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", |
203 | | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", |
204 | | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", |
205 | | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", |
206 | | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", |
207 | | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", |
208 | | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", |
209 | | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", |
210 | | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", |
211 | | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
212 | | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", |
213 | | [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", |
214 | | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", |
215 | | [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", |
216 | | [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", |
217 | | [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", |
218 | | [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable", |
219 | | [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", |
220 | | [BPF_PROG_TYPE_TRACING] = "tracing", |
221 | | [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops", |
222 | | [BPF_PROG_TYPE_EXT] = "ext", |
223 | | [BPF_PROG_TYPE_LSM] = "lsm", |
224 | | [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", |
225 | | [BPF_PROG_TYPE_SYSCALL] = "syscall", |
226 | | [BPF_PROG_TYPE_NETFILTER] = "netfilter", |
227 | | }; |
228 | | |
229 | | static int __base_pr(enum libbpf_print_level level, const char *format, |
230 | | va_list args) |
231 | 0 | { |
232 | 0 | const char *env_var = "LIBBPF_LOG_LEVEL"; |
233 | 0 | static enum libbpf_print_level min_level = LIBBPF_INFO; |
234 | 0 | static bool initialized; |
235 | |
|
236 | 0 | if (!initialized) { |
237 | 0 | char *verbosity; |
238 | |
|
239 | 0 | initialized = true; |
240 | 0 | verbosity = getenv(env_var); |
241 | 0 | if (verbosity) { |
242 | 0 | if (strcasecmp(verbosity, "warn") == 0) |
243 | 0 | min_level = LIBBPF_WARN; |
244 | 0 | else if (strcasecmp(verbosity, "debug") == 0) |
245 | 0 | min_level = LIBBPF_DEBUG; |
246 | 0 | else if (strcasecmp(verbosity, "info") == 0) |
247 | 0 | min_level = LIBBPF_INFO; |
248 | 0 | else |
249 | 0 | fprintf(stderr, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n", |
250 | 0 | env_var, verbosity); |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | | /* if too verbose, skip logging */ |
255 | 0 | if (level > min_level) |
256 | 0 | return 0; |
257 | | |
258 | 0 | return vfprintf(stderr, format, args); |
259 | 0 | } |
260 | | |
261 | | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
262 | | |
263 | | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
264 | 11.7k | { |
265 | 11.7k | libbpf_print_fn_t old_print_fn; |
266 | | |
267 | 11.7k | old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED); |
268 | | |
269 | 11.7k | return old_print_fn; |
270 | 11.7k | } |
271 | | |
272 | | __printf(2, 3) |
273 | | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
274 | 127k | { |
275 | 127k | va_list args; |
276 | 127k | int old_errno; |
277 | 127k | libbpf_print_fn_t print_fn; |
278 | | |
279 | 127k | print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED); |
280 | 127k | if (!print_fn) |
281 | 0 | return; |
282 | | |
283 | 127k | old_errno = errno; |
284 | | |
285 | 127k | va_start(args, format); |
286 | 127k | __libbpf_pr(level, format, args); |
287 | 127k | va_end(args); |
288 | | |
289 | 127k | errno = old_errno; |
290 | 127k | } |
291 | | |
292 | | static void pr_perm_msg(int err) |
293 | 0 | { |
294 | 0 | struct rlimit limit; |
295 | 0 | char buf[100]; |
296 | |
|
297 | 0 | if (err != -EPERM || geteuid() != 0) |
298 | 0 | return; |
299 | | |
300 | 0 | err = getrlimit(RLIMIT_MEMLOCK, &limit); |
301 | 0 | if (err) |
302 | 0 | return; |
303 | | |
304 | 0 | if (limit.rlim_cur == RLIM_INFINITY) |
305 | 0 | return; |
306 | | |
307 | 0 | if (limit.rlim_cur < 1024) |
308 | 0 | snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur); |
309 | 0 | else if (limit.rlim_cur < 1024*1024) |
310 | 0 | snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024); |
311 | 0 | else |
312 | 0 | snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024)); |
313 | |
|
314 | 0 | pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", |
315 | 0 | buf); |
316 | 0 | } |
317 | | |
318 | | #define STRERR_BUFSIZE 128 |
319 | | |
320 | | /* Copied from tools/perf/util/util.h */ |
321 | | #ifndef zfree |
322 | 207k | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
323 | | #endif |
324 | | |
325 | | #ifndef zclose |
326 | 32.8k | # define zclose(fd) ({ \ |
327 | 32.8k | int ___err = 0; \ |
328 | 32.8k | if ((fd) >= 0) \ |
329 | 32.8k | ___err = close((fd)); \ |
330 | 32.8k | fd = -1; \ |
331 | 32.8k | ___err; }) |
332 | | #endif |
333 | | |
334 | | static inline __u64 ptr_to_u64(const void *ptr) |
335 | 0 | { |
336 | 0 | return (__u64) (unsigned long) ptr; |
337 | 0 | } |
338 | | |
339 | | int libbpf_set_strict_mode(enum libbpf_strict_mode mode) |
340 | 0 | { |
341 | | /* as of v1.0 libbpf_set_strict_mode() is a no-op */ |
342 | 0 | return 0; |
343 | 0 | } |
344 | | |
345 | | __u32 libbpf_major_version(void) |
346 | 0 | { |
347 | 0 | return LIBBPF_MAJOR_VERSION; |
348 | 0 | } |
349 | | |
350 | | __u32 libbpf_minor_version(void) |
351 | 0 | { |
352 | 0 | return LIBBPF_MINOR_VERSION; |
353 | 0 | } |
354 | | |
355 | | const char *libbpf_version_string(void) |
356 | 0 | { |
357 | 0 | #define __S(X) #X |
358 | 0 | #define _S(X) __S(X) |
359 | 0 | return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION); |
360 | 0 | #undef _S |
361 | 0 | #undef __S |
362 | 0 | } |
363 | | |
364 | | enum reloc_type { |
365 | | RELO_LD64, |
366 | | RELO_CALL, |
367 | | RELO_DATA, |
368 | | RELO_EXTERN_LD64, |
369 | | RELO_EXTERN_CALL, |
370 | | RELO_SUBPROG_ADDR, |
371 | | RELO_CORE, |
372 | | }; |
373 | | |
374 | | struct reloc_desc { |
375 | | enum reloc_type type; |
376 | | int insn_idx; |
377 | | union { |
378 | | const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */ |
379 | | struct { |
380 | | int map_idx; |
381 | | int sym_off; |
382 | | int ext_idx; |
383 | | }; |
384 | | }; |
385 | | }; |
386 | | |
387 | | /* stored as sec_def->cookie for all libbpf-supported SEC()s */ |
388 | | enum sec_def_flags { |
389 | | SEC_NONE = 0, |
390 | | /* expected_attach_type is optional, if kernel doesn't support that */ |
391 | | SEC_EXP_ATTACH_OPT = 1, |
392 | | /* legacy, only used by libbpf_get_type_names() and |
393 | | * libbpf_attach_type_by_name(), not used by libbpf itself at all. |
394 | | * This used to be associated with cgroup (and few other) BPF programs |
395 | | * that were attachable through BPF_PROG_ATTACH command. Pretty |
396 | | * meaningless nowadays, though. |
397 | | */ |
398 | | SEC_ATTACHABLE = 2, |
399 | | SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT, |
400 | | /* attachment target is specified through BTF ID in either kernel or |
401 | | * other BPF program's BTF object |
402 | | */ |
403 | | SEC_ATTACH_BTF = 4, |
404 | | /* BPF program type allows sleeping/blocking in kernel */ |
405 | | SEC_SLEEPABLE = 8, |
406 | | /* BPF program support non-linear XDP buffer */ |
407 | | SEC_XDP_FRAGS = 16, |
408 | | /* Setup proper attach type for usdt probes. */ |
409 | | SEC_USDT = 32, |
410 | | }; |
411 | | |
412 | | struct bpf_sec_def { |
413 | | char *sec; |
414 | | enum bpf_prog_type prog_type; |
415 | | enum bpf_attach_type expected_attach_type; |
416 | | long cookie; |
417 | | int handler_id; |
418 | | |
419 | | libbpf_prog_setup_fn_t prog_setup_fn; |
420 | | libbpf_prog_prepare_load_fn_t prog_prepare_load_fn; |
421 | | libbpf_prog_attach_fn_t prog_attach_fn; |
422 | | }; |
423 | | |
424 | | /* |
425 | | * bpf_prog should be a better name but it has been used in |
426 | | * linux/filter.h. |
427 | | */ |
428 | | struct bpf_program { |
429 | | char *name; |
430 | | char *sec_name; |
431 | | size_t sec_idx; |
432 | | const struct bpf_sec_def *sec_def; |
433 | | /* this program's instruction offset (in number of instructions) |
434 | | * within its containing ELF section |
435 | | */ |
436 | | size_t sec_insn_off; |
437 | | /* number of original instructions in ELF section belonging to this |
438 | | * program, not taking into account subprogram instructions possible |
439 | | * appended later during relocation |
440 | | */ |
441 | | size_t sec_insn_cnt; |
442 | | /* Offset (in number of instructions) of the start of instruction |
443 | | * belonging to this BPF program within its containing main BPF |
444 | | * program. For the entry-point (main) BPF program, this is always |
445 | | * zero. For a sub-program, this gets reset before each of main BPF |
446 | | * programs are processed and relocated and is used to determined |
447 | | * whether sub-program was already appended to the main program, and |
448 | | * if yes, at which instruction offset. |
449 | | */ |
450 | | size_t sub_insn_off; |
451 | | |
452 | | /* instructions that belong to BPF program; insns[0] is located at |
453 | | * sec_insn_off instruction within its ELF section in ELF file, so |
454 | | * when mapping ELF file instruction index to the local instruction, |
455 | | * one needs to subtract sec_insn_off; and vice versa. |
456 | | */ |
457 | | struct bpf_insn *insns; |
458 | | /* actual number of instruction in this BPF program's image; for |
459 | | * entry-point BPF programs this includes the size of main program |
460 | | * itself plus all the used sub-programs, appended at the end |
461 | | */ |
462 | | size_t insns_cnt; |
463 | | |
464 | | struct reloc_desc *reloc_desc; |
465 | | int nr_reloc; |
466 | | |
467 | | /* BPF verifier log settings */ |
468 | | char *log_buf; |
469 | | size_t log_size; |
470 | | __u32 log_level; |
471 | | |
472 | | struct bpf_object *obj; |
473 | | |
474 | | int fd; |
475 | | bool autoload; |
476 | | bool autoattach; |
477 | | bool sym_global; |
478 | | bool mark_btf_static; |
479 | | enum bpf_prog_type type; |
480 | | enum bpf_attach_type expected_attach_type; |
481 | | int exception_cb_idx; |
482 | | |
483 | | int prog_ifindex; |
484 | | __u32 attach_btf_obj_fd; |
485 | | __u32 attach_btf_id; |
486 | | __u32 attach_prog_fd; |
487 | | |
488 | | void *func_info; |
489 | | __u32 func_info_rec_size; |
490 | | __u32 func_info_cnt; |
491 | | |
492 | | void *line_info; |
493 | | __u32 line_info_rec_size; |
494 | | __u32 line_info_cnt; |
495 | | __u32 prog_flags; |
496 | | }; |
497 | | |
498 | | struct bpf_struct_ops { |
499 | | struct bpf_program **progs; |
500 | | __u32 *kern_func_off; |
501 | | /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ |
502 | | void *data; |
503 | | /* e.g. struct bpf_struct_ops_tcp_congestion_ops in |
504 | | * btf_vmlinux's format. |
505 | | * struct bpf_struct_ops_tcp_congestion_ops { |
506 | | * [... some other kernel fields ...] |
507 | | * struct tcp_congestion_ops data; |
508 | | * } |
509 | | * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops) |
510 | | * bpf_map__init_kern_struct_ops() will populate the "kern_vdata" |
511 | | * from "data". |
512 | | */ |
513 | | void *kern_vdata; |
514 | | __u32 type_id; |
515 | | }; |
516 | | |
517 | 2.40k | #define DATA_SEC ".data" |
518 | 2.08k | #define BSS_SEC ".bss" |
519 | 1.74k | #define RODATA_SEC ".rodata" |
520 | 8.00k | #define KCONFIG_SEC ".kconfig" |
521 | 10.4k | #define KSYMS_SEC ".ksyms" |
522 | 4.12k | #define STRUCT_OPS_SEC ".struct_ops" |
523 | 3.83k | #define STRUCT_OPS_LINK_SEC ".struct_ops.link" |
524 | 806 | #define ARENA_SEC ".addr_space.1" |
525 | | |
526 | | enum libbpf_map_type { |
527 | | LIBBPF_MAP_UNSPEC, |
528 | | LIBBPF_MAP_DATA, |
529 | | LIBBPF_MAP_BSS, |
530 | | LIBBPF_MAP_RODATA, |
531 | | LIBBPF_MAP_KCONFIG, |
532 | | }; |
533 | | |
534 | | struct bpf_map_def { |
535 | | unsigned int type; |
536 | | unsigned int key_size; |
537 | | unsigned int value_size; |
538 | | unsigned int max_entries; |
539 | | unsigned int map_flags; |
540 | | }; |
541 | | |
542 | | struct bpf_map { |
543 | | struct bpf_object *obj; |
544 | | char *name; |
545 | | /* real_name is defined for special internal maps (.rodata*, |
546 | | * .data*, .bss, .kconfig) and preserves their original ELF section |
547 | | * name. This is important to be able to find corresponding BTF |
548 | | * DATASEC information. |
549 | | */ |
550 | | char *real_name; |
551 | | int fd; |
552 | | int sec_idx; |
553 | | size_t sec_offset; |
554 | | int map_ifindex; |
555 | | int inner_map_fd; |
556 | | struct bpf_map_def def; |
557 | | __u32 numa_node; |
558 | | __u32 btf_var_idx; |
559 | | int mod_btf_fd; |
560 | | __u32 btf_key_type_id; |
561 | | __u32 btf_value_type_id; |
562 | | __u32 btf_vmlinux_value_type_id; |
563 | | enum libbpf_map_type libbpf_type; |
564 | | void *mmaped; |
565 | | struct bpf_struct_ops *st_ops; |
566 | | struct bpf_map *inner_map; |
567 | | void **init_slots; |
568 | | int init_slots_sz; |
569 | | char *pin_path; |
570 | | bool pinned; |
571 | | bool reused; |
572 | | bool autocreate; |
573 | | bool autoattach; |
574 | | __u64 map_extra; |
575 | | }; |
576 | | |
577 | | enum extern_type { |
578 | | EXT_UNKNOWN, |
579 | | EXT_KCFG, |
580 | | EXT_KSYM, |
581 | | }; |
582 | | |
583 | | enum kcfg_type { |
584 | | KCFG_UNKNOWN, |
585 | | KCFG_CHAR, |
586 | | KCFG_BOOL, |
587 | | KCFG_INT, |
588 | | KCFG_TRISTATE, |
589 | | KCFG_CHAR_ARR, |
590 | | }; |
591 | | |
592 | | struct extern_desc { |
593 | | enum extern_type type; |
594 | | int sym_idx; |
595 | | int btf_id; |
596 | | int sec_btf_id; |
597 | | const char *name; |
598 | | char *essent_name; |
599 | | bool is_set; |
600 | | bool is_weak; |
601 | | union { |
602 | | struct { |
603 | | enum kcfg_type type; |
604 | | int sz; |
605 | | int align; |
606 | | int data_off; |
607 | | bool is_signed; |
608 | | } kcfg; |
609 | | struct { |
610 | | unsigned long long addr; |
611 | | |
612 | | /* target btf_id of the corresponding kernel var. */ |
613 | | int kernel_btf_obj_fd; |
614 | | int kernel_btf_id; |
615 | | |
616 | | /* local btf_id of the ksym extern's type. */ |
617 | | __u32 type_id; |
618 | | /* BTF fd index to be patched in for insn->off, this is |
619 | | * 0 for vmlinux BTF, index in obj->fd_array for module |
620 | | * BTF |
621 | | */ |
622 | | __s16 btf_fd_idx; |
623 | | } ksym; |
624 | | }; |
625 | | }; |
626 | | |
627 | | struct module_btf { |
628 | | struct btf *btf; |
629 | | char *name; |
630 | | __u32 id; |
631 | | int fd; |
632 | | int fd_array_idx; |
633 | | }; |
634 | | |
635 | | enum sec_type { |
636 | | SEC_UNUSED = 0, |
637 | | SEC_RELO, |
638 | | SEC_BSS, |
639 | | SEC_DATA, |
640 | | SEC_RODATA, |
641 | | SEC_ST_OPS, |
642 | | }; |
643 | | |
644 | | struct elf_sec_desc { |
645 | | enum sec_type sec_type; |
646 | | Elf64_Shdr *shdr; |
647 | | Elf_Data *data; |
648 | | }; |
649 | | |
650 | | struct elf_state { |
651 | | int fd; |
652 | | const void *obj_buf; |
653 | | size_t obj_buf_sz; |
654 | | Elf *elf; |
655 | | Elf64_Ehdr *ehdr; |
656 | | Elf_Data *symbols; |
657 | | Elf_Data *arena_data; |
658 | | size_t shstrndx; /* section index for section name strings */ |
659 | | size_t strtabidx; |
660 | | struct elf_sec_desc *secs; |
661 | | size_t sec_cnt; |
662 | | int btf_maps_shndx; |
663 | | __u32 btf_maps_sec_btf_id; |
664 | | int text_shndx; |
665 | | int symbols_shndx; |
666 | | bool has_st_ops; |
667 | | int arena_data_shndx; |
668 | | }; |
669 | | |
670 | | struct usdt_manager; |
671 | | |
672 | | struct bpf_object { |
673 | | char name[BPF_OBJ_NAME_LEN]; |
674 | | char license[64]; |
675 | | __u32 kern_version; |
676 | | |
677 | | struct bpf_program *programs; |
678 | | size_t nr_programs; |
679 | | struct bpf_map *maps; |
680 | | size_t nr_maps; |
681 | | size_t maps_cap; |
682 | | |
683 | | char *kconfig; |
684 | | struct extern_desc *externs; |
685 | | int nr_extern; |
686 | | int kconfig_map_idx; |
687 | | |
688 | | bool loaded; |
689 | | bool has_subcalls; |
690 | | bool has_rodata; |
691 | | |
692 | | struct bpf_gen *gen_loader; |
693 | | |
694 | | /* Information when doing ELF related work. Only valid if efile.elf is not NULL */ |
695 | | struct elf_state efile; |
696 | | |
697 | | struct btf *btf; |
698 | | struct btf_ext *btf_ext; |
699 | | |
700 | | /* Parse and load BTF vmlinux if any of the programs in the object need |
701 | | * it at load time. |
702 | | */ |
703 | | struct btf *btf_vmlinux; |
704 | | /* Path to the custom BTF to be used for BPF CO-RE relocations as an |
705 | | * override for vmlinux BTF. |
706 | | */ |
707 | | char *btf_custom_path; |
708 | | /* vmlinux BTF override for CO-RE relocations */ |
709 | | struct btf *btf_vmlinux_override; |
710 | | /* Lazily initialized kernel module BTFs */ |
711 | | struct module_btf *btf_modules; |
712 | | bool btf_modules_loaded; |
713 | | size_t btf_module_cnt; |
714 | | size_t btf_module_cap; |
715 | | |
716 | | /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */ |
717 | | char *log_buf; |
718 | | size_t log_size; |
719 | | __u32 log_level; |
720 | | |
721 | | int *fd_array; |
722 | | size_t fd_array_cap; |
723 | | size_t fd_array_cnt; |
724 | | |
725 | | struct usdt_manager *usdt_man; |
726 | | |
727 | | struct bpf_map *arena_map; |
728 | | void *arena_data; |
729 | | size_t arena_data_sz; |
730 | | |
731 | | struct kern_feature_cache *feat_cache; |
732 | | char *token_path; |
733 | | int token_fd; |
734 | | |
735 | | char path[]; |
736 | | }; |
737 | | |
738 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off); |
739 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off); |
740 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx); |
741 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name); |
742 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn); |
743 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn); |
744 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn); |
745 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx); |
746 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx); |
747 | | |
748 | | void bpf_program__unload(struct bpf_program *prog) |
749 | 18.3k | { |
750 | 18.3k | if (!prog) |
751 | 0 | return; |
752 | | |
753 | 18.3k | zclose(prog->fd); |
754 | | |
755 | 18.3k | zfree(&prog->func_info); |
756 | 18.3k | zfree(&prog->line_info); |
757 | 18.3k | } |
758 | | |
759 | | static void bpf_program__exit(struct bpf_program *prog) |
760 | 9.17k | { |
761 | 9.17k | if (!prog) |
762 | 0 | return; |
763 | | |
764 | 9.17k | bpf_program__unload(prog); |
765 | 9.17k | zfree(&prog->name); |
766 | 9.17k | zfree(&prog->sec_name); |
767 | 9.17k | zfree(&prog->insns); |
768 | 9.17k | zfree(&prog->reloc_desc); |
769 | | |
770 | 9.17k | prog->nr_reloc = 0; |
771 | 9.17k | prog->insns_cnt = 0; |
772 | 9.17k | prog->sec_idx = -1; |
773 | 9.17k | } |
774 | | |
775 | | static bool insn_is_subprog_call(const struct bpf_insn *insn) |
776 | 0 | { |
777 | 0 | return BPF_CLASS(insn->code) == BPF_JMP && |
778 | 0 | BPF_OP(insn->code) == BPF_CALL && |
779 | 0 | BPF_SRC(insn->code) == BPF_K && |
780 | 0 | insn->src_reg == BPF_PSEUDO_CALL && |
781 | 0 | insn->dst_reg == 0 && |
782 | 0 | insn->off == 0; |
783 | 0 | } |
784 | | |
785 | | static bool is_call_insn(const struct bpf_insn *insn) |
786 | 3.40k | { |
787 | 3.40k | return insn->code == (BPF_JMP | BPF_CALL); |
788 | 3.40k | } |
789 | | |
790 | | static bool insn_is_pseudo_func(struct bpf_insn *insn) |
791 | 0 | { |
792 | 0 | return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC; |
793 | 0 | } |
794 | | |
795 | | static int |
796 | | bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, |
797 | | const char *name, size_t sec_idx, const char *sec_name, |
798 | | size_t sec_off, void *insn_data, size_t insn_data_sz) |
799 | 9.25k | { |
800 | 9.25k | if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) { |
801 | 75 | pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n", |
802 | 75 | sec_name, name, sec_off, insn_data_sz); |
803 | 75 | return -EINVAL; |
804 | 75 | } |
805 | | |
806 | 9.17k | memset(prog, 0, sizeof(*prog)); |
807 | 9.17k | prog->obj = obj; |
808 | | |
809 | 9.17k | prog->sec_idx = sec_idx; |
810 | 9.17k | prog->sec_insn_off = sec_off / BPF_INSN_SZ; |
811 | 9.17k | prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ; |
812 | | /* insns_cnt can later be increased by appending used subprograms */ |
813 | 9.17k | prog->insns_cnt = prog->sec_insn_cnt; |
814 | | |
815 | 9.17k | prog->type = BPF_PROG_TYPE_UNSPEC; |
816 | 9.17k | prog->fd = -1; |
817 | 9.17k | prog->exception_cb_idx = -1; |
818 | | |
819 | | /* libbpf's convention for SEC("?abc...") is that it's just like |
820 | | * SEC("abc...") but the corresponding bpf_program starts out with |
821 | | * autoload set to false. |
822 | | */ |
823 | 9.17k | if (sec_name[0] == '?') { |
824 | 548 | prog->autoload = false; |
825 | | /* from now on forget there was ? in section name */ |
826 | 548 | sec_name++; |
827 | 8.62k | } else { |
828 | 8.62k | prog->autoload = true; |
829 | 8.62k | } |
830 | | |
831 | 9.17k | prog->autoattach = true; |
832 | | |
833 | | /* inherit object's log_level */ |
834 | 9.17k | prog->log_level = obj->log_level; |
835 | | |
836 | 9.17k | prog->sec_name = strdup(sec_name); |
837 | 9.17k | if (!prog->sec_name) |
838 | 0 | goto errout; |
839 | | |
840 | 9.17k | prog->name = strdup(name); |
841 | 9.17k | if (!prog->name) |
842 | 0 | goto errout; |
843 | | |
844 | 9.17k | prog->insns = malloc(insn_data_sz); |
845 | 9.17k | if (!prog->insns) |
846 | 24 | goto errout; |
847 | 9.15k | memcpy(prog->insns, insn_data, insn_data_sz); |
848 | | |
849 | 9.15k | return 0; |
850 | 24 | errout: |
851 | 24 | pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name); |
852 | 24 | bpf_program__exit(prog); |
853 | 24 | return -ENOMEM; |
854 | 9.17k | } |
855 | | |
856 | | static int |
857 | | bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data, |
858 | | const char *sec_name, int sec_idx) |
859 | 1.22k | { |
860 | 1.22k | Elf_Data *symbols = obj->efile.symbols; |
861 | 1.22k | struct bpf_program *prog, *progs; |
862 | 1.22k | void *data = sec_data->d_buf; |
863 | 1.22k | size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms; |
864 | 1.22k | int nr_progs, err, i; |
865 | 1.22k | const char *name; |
866 | 1.22k | Elf64_Sym *sym; |
867 | | |
868 | 1.22k | progs = obj->programs; |
869 | 1.22k | nr_progs = obj->nr_programs; |
870 | 1.22k | nr_syms = symbols->d_size / sizeof(Elf64_Sym); |
871 | | |
872 | 184k | for (i = 0; i < nr_syms; i++) { |
873 | 183k | sym = elf_sym_by_idx(obj, i); |
874 | | |
875 | 183k | if (sym->st_shndx != sec_idx) |
876 | 172k | continue; |
877 | 11.0k | if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) |
878 | 1.57k | continue; |
879 | | |
880 | 9.43k | prog_sz = sym->st_size; |
881 | 9.43k | sec_off = sym->st_value; |
882 | | |
883 | 9.43k | name = elf_sym_str(obj, sym->st_name); |
884 | 9.43k | if (!name) { |
885 | 60 | pr_warn("sec '%s': failed to get symbol name for offset %zu\n", |
886 | 60 | sec_name, sec_off); |
887 | 60 | return -LIBBPF_ERRNO__FORMAT; |
888 | 60 | } |
889 | | |
890 | 9.37k | if (sec_off + prog_sz > sec_sz) { |
891 | 120 | pr_warn("sec '%s': program at offset %zu crosses section boundary\n", |
892 | 120 | sec_name, sec_off); |
893 | 120 | return -LIBBPF_ERRNO__FORMAT; |
894 | 120 | } |
895 | | |
896 | 9.25k | if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) { |
897 | 1 | pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name); |
898 | 1 | return -ENOTSUP; |
899 | 1 | } |
900 | | |
901 | 9.25k | pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n", |
902 | 18.5k | sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz); |
903 | | |
904 | 9.25k | progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs)); |
905 | 9.25k | if (!progs) { |
906 | | /* |
907 | | * In this case the original obj->programs |
908 | | * is still valid, so don't need special treat for |
909 | | * bpf_close_object(). |
910 | | */ |
911 | 0 | pr_warn("sec '%s': failed to alloc memory for new program '%s'\n", |
912 | 0 | sec_name, name); |
913 | 0 | return -ENOMEM; |
914 | 0 | } |
915 | 9.25k | obj->programs = progs; |
916 | | |
917 | 9.25k | prog = &progs[nr_progs]; |
918 | | |
919 | 9.25k | err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name, |
920 | 9.25k | sec_off, data + sec_off, prog_sz); |
921 | 9.25k | if (err) |
922 | 99 | return err; |
923 | | |
924 | 9.15k | if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL) |
925 | 8.99k | prog->sym_global = true; |
926 | | |
927 | | /* if function is a global/weak symbol, but has restricted |
928 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC |
929 | | * as static to enable more permissive BPF verification mode |
930 | | * with more outside context available to BPF verifier |
931 | | */ |
932 | 9.15k | if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
933 | 8.99k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)) |
934 | 6.63k | prog->mark_btf_static = true; |
935 | | |
936 | 9.15k | nr_progs++; |
937 | 9.15k | obj->nr_programs = nr_progs; |
938 | 9.15k | } |
939 | | |
940 | 941 | return 0; |
941 | 1.22k | } |
942 | | |
943 | | static const struct btf_member * |
944 | | find_member_by_offset(const struct btf_type *t, __u32 bit_offset) |
945 | 0 | { |
946 | 0 | struct btf_member *m; |
947 | 0 | int i; |
948 | |
|
949 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
950 | 0 | if (btf_member_bit_offset(t, i) == bit_offset) |
951 | 0 | return m; |
952 | 0 | } |
953 | | |
954 | 0 | return NULL; |
955 | 0 | } |
956 | | |
957 | | static const struct btf_member * |
958 | | find_member_by_name(const struct btf *btf, const struct btf_type *t, |
959 | | const char *name) |
960 | 0 | { |
961 | 0 | struct btf_member *m; |
962 | 0 | int i; |
963 | |
|
964 | 0 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
965 | 0 | if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) |
966 | 0 | return m; |
967 | 0 | } |
968 | | |
969 | 0 | return NULL; |
970 | 0 | } |
971 | | |
972 | | static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, |
973 | | __u16 kind, struct btf **res_btf, |
974 | | struct module_btf **res_mod_btf); |
975 | | |
976 | 0 | #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" |
977 | | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
978 | | const char *name, __u32 kind); |
979 | | |
980 | | static int |
981 | | find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw, |
982 | | struct module_btf **mod_btf, |
983 | | const struct btf_type **type, __u32 *type_id, |
984 | | const struct btf_type **vtype, __u32 *vtype_id, |
985 | | const struct btf_member **data_member) |
986 | 0 | { |
987 | 0 | const struct btf_type *kern_type, *kern_vtype; |
988 | 0 | const struct btf_member *kern_data_member; |
989 | 0 | struct btf *btf; |
990 | 0 | __s32 kern_vtype_id, kern_type_id; |
991 | 0 | char tname[256]; |
992 | 0 | __u32 i; |
993 | |
|
994 | 0 | snprintf(tname, sizeof(tname), "%.*s", |
995 | 0 | (int)bpf_core_essential_name_len(tname_raw), tname_raw); |
996 | |
|
997 | 0 | kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT, |
998 | 0 | &btf, mod_btf); |
999 | 0 | if (kern_type_id < 0) { |
1000 | 0 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", |
1001 | 0 | tname); |
1002 | 0 | return kern_type_id; |
1003 | 0 | } |
1004 | 0 | kern_type = btf__type_by_id(btf, kern_type_id); |
1005 | | |
1006 | | /* Find the corresponding "map_value" type that will be used |
1007 | | * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example, |
1008 | | * find "struct bpf_struct_ops_tcp_congestion_ops" from the |
1009 | | * btf_vmlinux. |
1010 | | */ |
1011 | 0 | kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX, |
1012 | 0 | tname, BTF_KIND_STRUCT); |
1013 | 0 | if (kern_vtype_id < 0) { |
1014 | 0 | pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n", |
1015 | 0 | STRUCT_OPS_VALUE_PREFIX, tname); |
1016 | 0 | return kern_vtype_id; |
1017 | 0 | } |
1018 | 0 | kern_vtype = btf__type_by_id(btf, kern_vtype_id); |
1019 | | |
1020 | | /* Find "struct tcp_congestion_ops" from |
1021 | | * struct bpf_struct_ops_tcp_congestion_ops { |
1022 | | * [ ... ] |
1023 | | * struct tcp_congestion_ops data; |
1024 | | * } |
1025 | | */ |
1026 | 0 | kern_data_member = btf_members(kern_vtype); |
1027 | 0 | for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) { |
1028 | 0 | if (kern_data_member->type == kern_type_id) |
1029 | 0 | break; |
1030 | 0 | } |
1031 | 0 | if (i == btf_vlen(kern_vtype)) { |
1032 | 0 | pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n", |
1033 | 0 | tname, STRUCT_OPS_VALUE_PREFIX, tname); |
1034 | 0 | return -EINVAL; |
1035 | 0 | } |
1036 | | |
1037 | 0 | *type = kern_type; |
1038 | 0 | *type_id = kern_type_id; |
1039 | 0 | *vtype = kern_vtype; |
1040 | 0 | *vtype_id = kern_vtype_id; |
1041 | 0 | *data_member = kern_data_member; |
1042 | |
|
1043 | 0 | return 0; |
1044 | 0 | } |
1045 | | |
1046 | | static bool bpf_map__is_struct_ops(const struct bpf_map *map) |
1047 | 326 | { |
1048 | 326 | return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; |
1049 | 326 | } |
1050 | | |
1051 | | static bool is_valid_st_ops_program(struct bpf_object *obj, |
1052 | | const struct bpf_program *prog) |
1053 | 0 | { |
1054 | 0 | int i; |
1055 | |
|
1056 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
1057 | 0 | if (&obj->programs[i] == prog) |
1058 | 0 | return prog->type == BPF_PROG_TYPE_STRUCT_OPS; |
1059 | 0 | } |
1060 | | |
1061 | 0 | return false; |
1062 | 0 | } |
1063 | | |
1064 | | /* For each struct_ops program P, referenced from some struct_ops map M, |
1065 | | * enable P.autoload if there are Ms for which M.autocreate is true, |
1066 | | * disable P.autoload if for all Ms M.autocreate is false. |
1067 | | * Don't change P.autoload for programs that are not referenced from any maps. |
1068 | | */ |
1069 | | static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj) |
1070 | 0 | { |
1071 | 0 | struct bpf_program *prog, *slot_prog; |
1072 | 0 | struct bpf_map *map; |
1073 | 0 | int i, j, k, vlen; |
1074 | |
|
1075 | 0 | for (i = 0; i < obj->nr_programs; ++i) { |
1076 | 0 | int should_load = false; |
1077 | 0 | int use_cnt = 0; |
1078 | |
|
1079 | 0 | prog = &obj->programs[i]; |
1080 | 0 | if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) |
1081 | 0 | continue; |
1082 | | |
1083 | 0 | for (j = 0; j < obj->nr_maps; ++j) { |
1084 | 0 | const struct btf_type *type; |
1085 | |
|
1086 | 0 | map = &obj->maps[j]; |
1087 | 0 | if (!bpf_map__is_struct_ops(map)) |
1088 | 0 | continue; |
1089 | | |
1090 | 0 | type = btf__type_by_id(obj->btf, map->st_ops->type_id); |
1091 | 0 | vlen = btf_vlen(type); |
1092 | 0 | for (k = 0; k < vlen; ++k) { |
1093 | 0 | slot_prog = map->st_ops->progs[k]; |
1094 | 0 | if (prog != slot_prog) |
1095 | 0 | continue; |
1096 | | |
1097 | 0 | use_cnt++; |
1098 | 0 | if (map->autocreate) |
1099 | 0 | should_load = true; |
1100 | 0 | } |
1101 | 0 | } |
1102 | 0 | if (use_cnt) |
1103 | 0 | prog->autoload = should_load; |
1104 | 0 | } |
1105 | |
|
1106 | 0 | return 0; |
1107 | 0 | } |
1108 | | |
1109 | | /* Init the map's fields that depend on kern_btf */ |
1110 | | static int bpf_map__init_kern_struct_ops(struct bpf_map *map) |
1111 | 0 | { |
1112 | 0 | const struct btf_member *member, *kern_member, *kern_data_member; |
1113 | 0 | const struct btf_type *type, *kern_type, *kern_vtype; |
1114 | 0 | __u32 i, kern_type_id, kern_vtype_id, kern_data_off; |
1115 | 0 | struct bpf_object *obj = map->obj; |
1116 | 0 | const struct btf *btf = obj->btf; |
1117 | 0 | struct bpf_struct_ops *st_ops; |
1118 | 0 | const struct btf *kern_btf; |
1119 | 0 | struct module_btf *mod_btf; |
1120 | 0 | void *data, *kern_data; |
1121 | 0 | const char *tname; |
1122 | 0 | int err; |
1123 | |
|
1124 | 0 | st_ops = map->st_ops; |
1125 | 0 | type = btf__type_by_id(btf, st_ops->type_id); |
1126 | 0 | tname = btf__name_by_offset(btf, type->name_off); |
1127 | 0 | err = find_struct_ops_kern_types(obj, tname, &mod_btf, |
1128 | 0 | &kern_type, &kern_type_id, |
1129 | 0 | &kern_vtype, &kern_vtype_id, |
1130 | 0 | &kern_data_member); |
1131 | 0 | if (err) |
1132 | 0 | return err; |
1133 | | |
1134 | 0 | kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux; |
1135 | |
|
1136 | 0 | pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n", |
1137 | 0 | map->name, st_ops->type_id, kern_type_id, kern_vtype_id); |
1138 | |
|
1139 | 0 | map->mod_btf_fd = mod_btf ? mod_btf->fd : -1; |
1140 | 0 | map->def.value_size = kern_vtype->size; |
1141 | 0 | map->btf_vmlinux_value_type_id = kern_vtype_id; |
1142 | |
|
1143 | 0 | st_ops->kern_vdata = calloc(1, kern_vtype->size); |
1144 | 0 | if (!st_ops->kern_vdata) |
1145 | 0 | return -ENOMEM; |
1146 | | |
1147 | 0 | data = st_ops->data; |
1148 | 0 | kern_data_off = kern_data_member->offset / 8; |
1149 | 0 | kern_data = st_ops->kern_vdata + kern_data_off; |
1150 | |
|
1151 | 0 | member = btf_members(type); |
1152 | 0 | for (i = 0; i < btf_vlen(type); i++, member++) { |
1153 | 0 | const struct btf_type *mtype, *kern_mtype; |
1154 | 0 | __u32 mtype_id, kern_mtype_id; |
1155 | 0 | void *mdata, *kern_mdata; |
1156 | 0 | struct bpf_program *prog; |
1157 | 0 | __s64 msize, kern_msize; |
1158 | 0 | __u32 moff, kern_moff; |
1159 | 0 | __u32 kern_member_idx; |
1160 | 0 | const char *mname; |
1161 | |
|
1162 | 0 | mname = btf__name_by_offset(btf, member->name_off); |
1163 | 0 | moff = member->offset / 8; |
1164 | 0 | mdata = data + moff; |
1165 | 0 | msize = btf__resolve_size(btf, member->type); |
1166 | 0 | if (msize < 0) { |
1167 | 0 | pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n", |
1168 | 0 | map->name, mname); |
1169 | 0 | return msize; |
1170 | 0 | } |
1171 | | |
1172 | 0 | kern_member = find_member_by_name(kern_btf, kern_type, mname); |
1173 | 0 | if (!kern_member) { |
1174 | 0 | if (!libbpf_is_mem_zeroed(mdata, msize)) { |
1175 | 0 | pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", |
1176 | 0 | map->name, mname); |
1177 | 0 | return -ENOTSUP; |
1178 | 0 | } |
1179 | | |
1180 | 0 | if (st_ops->progs[i]) { |
1181 | | /* If we had declaratively set struct_ops callback, we need to |
1182 | | * force its autoload to false, because it doesn't have |
1183 | | * a chance of succeeding from POV of the current struct_ops map. |
1184 | | * If this program is still referenced somewhere else, though, |
1185 | | * then bpf_object_adjust_struct_ops_autoload() will update its |
1186 | | * autoload accordingly. |
1187 | | */ |
1188 | 0 | st_ops->progs[i]->autoload = false; |
1189 | 0 | st_ops->progs[i] = NULL; |
1190 | 0 | } |
1191 | | |
1192 | | /* Skip all-zero/NULL fields if they are not present in the kernel BTF */ |
1193 | 0 | pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n", |
1194 | 0 | map->name, mname); |
1195 | 0 | continue; |
1196 | 0 | } |
1197 | | |
1198 | 0 | kern_member_idx = kern_member - btf_members(kern_type); |
1199 | 0 | if (btf_member_bitfield_size(type, i) || |
1200 | 0 | btf_member_bitfield_size(kern_type, kern_member_idx)) { |
1201 | 0 | pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n", |
1202 | 0 | map->name, mname); |
1203 | 0 | return -ENOTSUP; |
1204 | 0 | } |
1205 | | |
1206 | 0 | kern_moff = kern_member->offset / 8; |
1207 | 0 | kern_mdata = kern_data + kern_moff; |
1208 | |
|
1209 | 0 | mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); |
1210 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, |
1211 | 0 | &kern_mtype_id); |
1212 | 0 | if (BTF_INFO_KIND(mtype->info) != |
1213 | 0 | BTF_INFO_KIND(kern_mtype->info)) { |
1214 | 0 | pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n", |
1215 | 0 | map->name, mname, BTF_INFO_KIND(mtype->info), |
1216 | 0 | BTF_INFO_KIND(kern_mtype->info)); |
1217 | 0 | return -ENOTSUP; |
1218 | 0 | } |
1219 | | |
1220 | 0 | if (btf_is_ptr(mtype)) { |
1221 | 0 | prog = *(void **)mdata; |
1222 | | /* just like for !kern_member case above, reset declaratively |
1223 | | * set (at compile time) program's autload to false, |
1224 | | * if user replaced it with another program or NULL |
1225 | | */ |
1226 | 0 | if (st_ops->progs[i] && st_ops->progs[i] != prog) |
1227 | 0 | st_ops->progs[i]->autoload = false; |
1228 | | |
1229 | | /* Update the value from the shadow type */ |
1230 | 0 | st_ops->progs[i] = prog; |
1231 | 0 | if (!prog) |
1232 | 0 | continue; |
1233 | | |
1234 | 0 | if (!is_valid_st_ops_program(obj, prog)) { |
1235 | 0 | pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n", |
1236 | 0 | map->name, mname); |
1237 | 0 | return -ENOTSUP; |
1238 | 0 | } |
1239 | | |
1240 | 0 | kern_mtype = skip_mods_and_typedefs(kern_btf, |
1241 | 0 | kern_mtype->type, |
1242 | 0 | &kern_mtype_id); |
1243 | | |
1244 | | /* mtype->type must be a func_proto which was |
1245 | | * guaranteed in bpf_object__collect_st_ops_relos(), |
1246 | | * so only check kern_mtype for func_proto here. |
1247 | | */ |
1248 | 0 | if (!btf_is_func_proto(kern_mtype)) { |
1249 | 0 | pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n", |
1250 | 0 | map->name, mname); |
1251 | 0 | return -ENOTSUP; |
1252 | 0 | } |
1253 | | |
1254 | 0 | if (mod_btf) |
1255 | 0 | prog->attach_btf_obj_fd = mod_btf->fd; |
1256 | | |
1257 | | /* if we haven't yet processed this BPF program, record proper |
1258 | | * attach_btf_id and member_idx |
1259 | | */ |
1260 | 0 | if (!prog->attach_btf_id) { |
1261 | 0 | prog->attach_btf_id = kern_type_id; |
1262 | 0 | prog->expected_attach_type = kern_member_idx; |
1263 | 0 | } |
1264 | | |
1265 | | /* struct_ops BPF prog can be re-used between multiple |
1266 | | * .struct_ops & .struct_ops.link as long as it's the |
1267 | | * same struct_ops struct definition and the same |
1268 | | * function pointer field |
1269 | | */ |
1270 | 0 | if (prog->attach_btf_id != kern_type_id) { |
1271 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: attach_btf_id %u != kern_type_id %u\n", |
1272 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1273 | 0 | prog->attach_btf_id, kern_type_id); |
1274 | 0 | return -EINVAL; |
1275 | 0 | } |
1276 | 0 | if (prog->expected_attach_type != kern_member_idx) { |
1277 | 0 | pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: expected_attach_type %u != kern_member_idx %u\n", |
1278 | 0 | map->name, mname, prog->name, prog->sec_name, prog->type, |
1279 | 0 | prog->expected_attach_type, kern_member_idx); |
1280 | 0 | return -EINVAL; |
1281 | 0 | } |
1282 | | |
1283 | 0 | st_ops->kern_func_off[i] = kern_data_off + kern_moff; |
1284 | |
|
1285 | 0 | pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n", |
1286 | 0 | map->name, mname, prog->name, moff, |
1287 | 0 | kern_moff); |
1288 | |
|
1289 | 0 | continue; |
1290 | 0 | } |
1291 | | |
1292 | 0 | kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); |
1293 | 0 | if (kern_msize < 0 || msize != kern_msize) { |
1294 | 0 | pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", |
1295 | 0 | map->name, mname, (ssize_t)msize, |
1296 | 0 | (ssize_t)kern_msize); |
1297 | 0 | return -ENOTSUP; |
1298 | 0 | } |
1299 | | |
1300 | 0 | pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n", |
1301 | 0 | map->name, mname, (unsigned int)msize, |
1302 | 0 | moff, kern_moff); |
1303 | 0 | memcpy(kern_mdata, mdata, msize); |
1304 | 0 | } |
1305 | | |
1306 | 0 | return 0; |
1307 | 0 | } |
1308 | | |
1309 | | static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) |
1310 | 0 | { |
1311 | 0 | struct bpf_map *map; |
1312 | 0 | size_t i; |
1313 | 0 | int err; |
1314 | |
|
1315 | 0 | for (i = 0; i < obj->nr_maps; i++) { |
1316 | 0 | map = &obj->maps[i]; |
1317 | |
|
1318 | 0 | if (!bpf_map__is_struct_ops(map)) |
1319 | 0 | continue; |
1320 | | |
1321 | 0 | if (!map->autocreate) |
1322 | 0 | continue; |
1323 | | |
1324 | 0 | err = bpf_map__init_kern_struct_ops(map); |
1325 | 0 | if (err) |
1326 | 0 | return err; |
1327 | 0 | } |
1328 | | |
1329 | 0 | return 0; |
1330 | 0 | } |
1331 | | |
1332 | | static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, |
1333 | | int shndx, Elf_Data *data) |
1334 | 170 | { |
1335 | 170 | const struct btf_type *type, *datasec; |
1336 | 170 | const struct btf_var_secinfo *vsi; |
1337 | 170 | struct bpf_struct_ops *st_ops; |
1338 | 170 | const char *tname, *var_name; |
1339 | 170 | __s32 type_id, datasec_id; |
1340 | 170 | const struct btf *btf; |
1341 | 170 | struct bpf_map *map; |
1342 | 170 | __u32 i; |
1343 | | |
1344 | 170 | if (shndx == -1) |
1345 | 0 | return 0; |
1346 | | |
1347 | 170 | btf = obj->btf; |
1348 | 170 | datasec_id = btf__find_by_name_kind(btf, sec_name, |
1349 | 170 | BTF_KIND_DATASEC); |
1350 | 170 | if (datasec_id < 0) { |
1351 | 61 | pr_warn("struct_ops init: DATASEC %s not found\n", |
1352 | 61 | sec_name); |
1353 | 61 | return -EINVAL; |
1354 | 61 | } |
1355 | | |
1356 | 109 | datasec = btf__type_by_id(btf, datasec_id); |
1357 | 109 | vsi = btf_var_secinfos(datasec); |
1358 | 162 | for (i = 0; i < btf_vlen(datasec); i++, vsi++) { |
1359 | 87 | type = btf__type_by_id(obj->btf, vsi->type); |
1360 | 87 | var_name = btf__name_by_offset(obj->btf, type->name_off); |
1361 | | |
1362 | 87 | type_id = btf__resolve_type(obj->btf, vsi->type); |
1363 | 87 | if (type_id < 0) { |
1364 | 8 | pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n", |
1365 | 8 | vsi->type, sec_name); |
1366 | 8 | return -EINVAL; |
1367 | 8 | } |
1368 | | |
1369 | 79 | type = btf__type_by_id(obj->btf, type_id); |
1370 | 79 | tname = btf__name_by_offset(obj->btf, type->name_off); |
1371 | 79 | if (!tname[0]) { |
1372 | 3 | pr_warn("struct_ops init: anonymous type is not supported\n"); |
1373 | 3 | return -ENOTSUP; |
1374 | 3 | } |
1375 | 76 | if (!btf_is_struct(type)) { |
1376 | 5 | pr_warn("struct_ops init: %s is not a struct\n", tname); |
1377 | 5 | return -EINVAL; |
1378 | 5 | } |
1379 | | |
1380 | 71 | map = bpf_object__add_map(obj); |
1381 | 71 | if (IS_ERR(map)) |
1382 | 0 | return PTR_ERR(map); |
1383 | | |
1384 | 71 | map->sec_idx = shndx; |
1385 | 71 | map->sec_offset = vsi->offset; |
1386 | 71 | map->name = strdup(var_name); |
1387 | 71 | if (!map->name) |
1388 | 0 | return -ENOMEM; |
1389 | 71 | map->btf_value_type_id = type_id; |
1390 | | |
1391 | | /* Follow same convention as for programs autoload: |
1392 | | * SEC("?.struct_ops") means map is not created by default. |
1393 | | */ |
1394 | 71 | if (sec_name[0] == '?') { |
1395 | 16 | map->autocreate = false; |
1396 | | /* from now on forget there was ? in section name */ |
1397 | 16 | sec_name++; |
1398 | 16 | } |
1399 | | |
1400 | 71 | map->def.type = BPF_MAP_TYPE_STRUCT_OPS; |
1401 | 71 | map->def.key_size = sizeof(int); |
1402 | 71 | map->def.value_size = type->size; |
1403 | 71 | map->def.max_entries = 1; |
1404 | 71 | map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0; |
1405 | 71 | map->autoattach = true; |
1406 | | |
1407 | 71 | map->st_ops = calloc(1, sizeof(*map->st_ops)); |
1408 | 71 | if (!map->st_ops) |
1409 | 0 | return -ENOMEM; |
1410 | 71 | st_ops = map->st_ops; |
1411 | 71 | st_ops->data = malloc(type->size); |
1412 | 71 | st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); |
1413 | 71 | st_ops->kern_func_off = malloc(btf_vlen(type) * |
1414 | 71 | sizeof(*st_ops->kern_func_off)); |
1415 | 71 | if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) |
1416 | 0 | return -ENOMEM; |
1417 | | |
1418 | 71 | if (vsi->offset + type->size > data->d_size) { |
1419 | 18 | pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n", |
1420 | 18 | var_name, sec_name); |
1421 | 18 | return -EINVAL; |
1422 | 18 | } |
1423 | | |
1424 | 53 | memcpy(st_ops->data, |
1425 | 53 | data->d_buf + vsi->offset, |
1426 | 53 | type->size); |
1427 | 53 | st_ops->type_id = type_id; |
1428 | | |
1429 | 53 | pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n", |
1430 | 53 | tname, type_id, var_name, vsi->offset); |
1431 | 53 | } |
1432 | | |
1433 | 75 | return 0; |
1434 | 109 | } |
1435 | | |
1436 | | static int bpf_object_init_struct_ops(struct bpf_object *obj) |
1437 | 2.39k | { |
1438 | 2.39k | const char *sec_name; |
1439 | 2.39k | int sec_idx, err; |
1440 | | |
1441 | 19.0k | for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) { |
1442 | 16.7k | struct elf_sec_desc *desc = &obj->efile.secs[sec_idx]; |
1443 | | |
1444 | 16.7k | if (desc->sec_type != SEC_ST_OPS) |
1445 | 16.5k | continue; |
1446 | | |
1447 | 170 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1448 | 170 | if (!sec_name) |
1449 | 0 | return -LIBBPF_ERRNO__FORMAT; |
1450 | | |
1451 | 170 | err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data); |
1452 | 170 | if (err) |
1453 | 95 | return err; |
1454 | 170 | } |
1455 | | |
1456 | 2.30k | return 0; |
1457 | 2.39k | } |
1458 | | |
1459 | | static struct bpf_object *bpf_object__new(const char *path, |
1460 | | const void *obj_buf, |
1461 | | size_t obj_buf_sz, |
1462 | | const char *obj_name) |
1463 | 11.7k | { |
1464 | 11.7k | struct bpf_object *obj; |
1465 | 11.7k | char *end; |
1466 | | |
1467 | 11.7k | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
1468 | 11.7k | if (!obj) { |
1469 | 0 | pr_warn("alloc memory failed for %s\n", path); |
1470 | 0 | return ERR_PTR(-ENOMEM); |
1471 | 0 | } |
1472 | | |
1473 | 11.7k | strcpy(obj->path, path); |
1474 | 11.7k | if (obj_name) { |
1475 | 11.7k | libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name)); |
1476 | 11.7k | } else { |
1477 | | /* Using basename() GNU version which doesn't modify arg. */ |
1478 | 0 | libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name)); |
1479 | 0 | end = strchr(obj->name, '.'); |
1480 | 0 | if (end) |
1481 | 0 | *end = 0; |
1482 | 0 | } |
1483 | | |
1484 | 11.7k | obj->efile.fd = -1; |
1485 | | /* |
1486 | | * Caller of this function should also call |
1487 | | * bpf_object__elf_finish() after data collection to return |
1488 | | * obj_buf to user. If not, we should duplicate the buffer to |
1489 | | * avoid user freeing them before elf finish. |
1490 | | */ |
1491 | 11.7k | obj->efile.obj_buf = obj_buf; |
1492 | 11.7k | obj->efile.obj_buf_sz = obj_buf_sz; |
1493 | 11.7k | obj->efile.btf_maps_shndx = -1; |
1494 | 11.7k | obj->kconfig_map_idx = -1; |
1495 | | |
1496 | 11.7k | obj->kern_version = get_kernel_version(); |
1497 | 11.7k | obj->loaded = false; |
1498 | | |
1499 | 11.7k | return obj; |
1500 | 11.7k | } |
1501 | | |
1502 | | static void bpf_object__elf_finish(struct bpf_object *obj) |
1503 | 15.8k | { |
1504 | 15.8k | if (!obj->efile.elf) |
1505 | 4.25k | return; |
1506 | | |
1507 | 11.6k | elf_end(obj->efile.elf); |
1508 | 11.6k | obj->efile.elf = NULL; |
1509 | 11.6k | obj->efile.symbols = NULL; |
1510 | 11.6k | obj->efile.arena_data = NULL; |
1511 | | |
1512 | 11.6k | zfree(&obj->efile.secs); |
1513 | 11.6k | obj->efile.sec_cnt = 0; |
1514 | 11.6k | zclose(obj->efile.fd); |
1515 | 11.6k | obj->efile.obj_buf = NULL; |
1516 | 11.6k | obj->efile.obj_buf_sz = 0; |
1517 | 11.6k | } |
1518 | | |
1519 | | static int bpf_object__elf_init(struct bpf_object *obj) |
1520 | 11.7k | { |
1521 | 11.7k | Elf64_Ehdr *ehdr; |
1522 | 11.7k | int err = 0; |
1523 | 11.7k | Elf *elf; |
1524 | | |
1525 | 11.7k | if (obj->efile.elf) { |
1526 | 0 | pr_warn("elf: init internal error\n"); |
1527 | 0 | return -LIBBPF_ERRNO__LIBELF; |
1528 | 0 | } |
1529 | | |
1530 | 11.7k | if (obj->efile.obj_buf_sz > 0) { |
1531 | | /* obj_buf should have been validated by bpf_object__open_mem(). */ |
1532 | 11.7k | elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); |
1533 | 11.7k | } else { |
1534 | 0 | obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); |
1535 | 0 | if (obj->efile.fd < 0) { |
1536 | 0 | char errmsg[STRERR_BUFSIZE], *cp; |
1537 | |
|
1538 | 0 | err = -errno; |
1539 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
1540 | 0 | pr_warn("elf: failed to open %s: %s\n", obj->path, cp); |
1541 | 0 | return err; |
1542 | 0 | } |
1543 | | |
1544 | 0 | elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL); |
1545 | 0 | } |
1546 | | |
1547 | 11.7k | if (!elf) { |
1548 | 93 | pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1)); |
1549 | 93 | err = -LIBBPF_ERRNO__LIBELF; |
1550 | 93 | goto errout; |
1551 | 93 | } |
1552 | | |
1553 | 11.6k | obj->efile.elf = elf; |
1554 | | |
1555 | 11.6k | if (elf_kind(elf) != ELF_K_ELF) { |
1556 | 120 | err = -LIBBPF_ERRNO__FORMAT; |
1557 | 120 | pr_warn("elf: '%s' is not a proper ELF object\n", obj->path); |
1558 | 120 | goto errout; |
1559 | 120 | } |
1560 | | |
1561 | 11.5k | if (gelf_getclass(elf) != ELFCLASS64) { |
1562 | 503 | err = -LIBBPF_ERRNO__FORMAT; |
1563 | 503 | pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path); |
1564 | 503 | goto errout; |
1565 | 503 | } |
1566 | | |
1567 | 11.0k | obj->efile.ehdr = ehdr = elf64_getehdr(elf); |
1568 | 11.0k | if (!obj->efile.ehdr) { |
1569 | 0 | pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1)); |
1570 | 0 | err = -LIBBPF_ERRNO__FORMAT; |
1571 | 0 | goto errout; |
1572 | 0 | } |
1573 | | |
1574 | 11.0k | if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { |
1575 | 23 | pr_warn("elf: failed to get section names section index for %s: %s\n", |
1576 | 23 | obj->path, elf_errmsg(-1)); |
1577 | 23 | err = -LIBBPF_ERRNO__FORMAT; |
1578 | 23 | goto errout; |
1579 | 23 | } |
1580 | | |
1581 | | /* ELF is corrupted/truncated, avoid calling elf_strptr. */ |
1582 | 10.9k | if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) { |
1583 | 1.17k | pr_warn("elf: failed to get section names strings from %s: %s\n", |
1584 | 1.17k | obj->path, elf_errmsg(-1)); |
1585 | 1.17k | err = -LIBBPF_ERRNO__FORMAT; |
1586 | 1.17k | goto errout; |
1587 | 1.17k | } |
1588 | | |
1589 | | /* Old LLVM set e_machine to EM_NONE */ |
1590 | 9.81k | if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) { |
1591 | 455 | pr_warn("elf: %s is not a valid eBPF object file\n", obj->path); |
1592 | 455 | err = -LIBBPF_ERRNO__FORMAT; |
1593 | 455 | goto errout; |
1594 | 455 | } |
1595 | | |
1596 | 9.36k | return 0; |
1597 | 2.36k | errout: |
1598 | 2.36k | bpf_object__elf_finish(obj); |
1599 | 2.36k | return err; |
1600 | 9.81k | } |
1601 | | |
1602 | | static int bpf_object__check_endianness(struct bpf_object *obj) |
1603 | 9.36k | { |
1604 | 9.36k | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
1605 | 9.36k | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) |
1606 | 9.33k | return 0; |
1607 | | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
1608 | | if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
1609 | | return 0; |
1610 | | #else |
1611 | | # error "Unrecognized __BYTE_ORDER__" |
1612 | | #endif |
1613 | 24 | pr_warn("elf: endianness mismatch in %s.\n", obj->path); |
1614 | 24 | return -LIBBPF_ERRNO__ENDIAN; |
1615 | 9.36k | } |
1616 | | |
1617 | | static int |
1618 | | bpf_object__init_license(struct bpf_object *obj, void *data, size_t size) |
1619 | 663 | { |
1620 | 663 | if (!data) { |
1621 | 1 | pr_warn("invalid license section in %s\n", obj->path); |
1622 | 1 | return -LIBBPF_ERRNO__FORMAT; |
1623 | 1 | } |
1624 | | /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't |
1625 | | * go over allowed ELF data section buffer |
1626 | | */ |
1627 | 662 | libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license))); |
1628 | 662 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
1629 | 662 | return 0; |
1630 | 663 | } |
1631 | | |
1632 | | static int |
1633 | | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
1634 | 49 | { |
1635 | 49 | __u32 kver; |
1636 | | |
1637 | 49 | if (!data || size != sizeof(kver)) { |
1638 | 13 | pr_warn("invalid kver section in %s\n", obj->path); |
1639 | 13 | return -LIBBPF_ERRNO__FORMAT; |
1640 | 13 | } |
1641 | 36 | memcpy(&kver, data, sizeof(kver)); |
1642 | 36 | obj->kern_version = kver; |
1643 | 36 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
1644 | 36 | return 0; |
1645 | 49 | } |
1646 | | |
1647 | | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
1648 | 162 | { |
1649 | 162 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
1650 | 162 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
1651 | 88 | return true; |
1652 | 74 | return false; |
1653 | 162 | } |
1654 | | |
1655 | | static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size) |
1656 | 404 | { |
1657 | 404 | Elf_Data *data; |
1658 | 404 | Elf_Scn *scn; |
1659 | | |
1660 | 404 | if (!name) |
1661 | 0 | return -EINVAL; |
1662 | | |
1663 | 404 | scn = elf_sec_by_name(obj, name); |
1664 | 404 | data = elf_sec_data(obj, scn); |
1665 | 404 | if (data) { |
1666 | 243 | *size = data->d_size; |
1667 | 243 | return 0; /* found it */ |
1668 | 243 | } |
1669 | | |
1670 | 161 | return -ENOENT; |
1671 | 404 | } |
1672 | | |
1673 | | static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name) |
1674 | 2.14k | { |
1675 | 2.14k | Elf_Data *symbols = obj->efile.symbols; |
1676 | 2.14k | const char *sname; |
1677 | 2.14k | size_t si; |
1678 | | |
1679 | 96.7k | for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) { |
1680 | 96.5k | Elf64_Sym *sym = elf_sym_by_idx(obj, si); |
1681 | | |
1682 | 96.5k | if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT) |
1683 | 87.9k | continue; |
1684 | | |
1685 | 8.61k | if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL && |
1686 | 8.61k | ELF64_ST_BIND(sym->st_info) != STB_WEAK) |
1687 | 5.71k | continue; |
1688 | | |
1689 | 2.89k | sname = elf_sym_str(obj, sym->st_name); |
1690 | 2.89k | if (!sname) { |
1691 | 28 | pr_warn("failed to get sym name string for var %s\n", name); |
1692 | 28 | return ERR_PTR(-EIO); |
1693 | 28 | } |
1694 | 2.87k | if (strcmp(name, sname) == 0) |
1695 | 1.92k | return sym; |
1696 | 2.87k | } |
1697 | | |
1698 | 200 | return ERR_PTR(-ENOENT); |
1699 | 2.14k | } |
1700 | | |
1701 | | /* Some versions of Android don't provide memfd_create() in their libc |
1702 | | * implementation, so avoid complications and just go straight to Linux |
1703 | | * syscall. |
1704 | | */ |
1705 | | static int sys_memfd_create(const char *name, unsigned flags) |
1706 | 2.92k | { |
1707 | 2.92k | return syscall(__NR_memfd_create, name, flags); |
1708 | 2.92k | } |
1709 | | |
1710 | | #ifndef MFD_CLOEXEC |
1711 | | #define MFD_CLOEXEC 0x0001U |
1712 | | #endif |
1713 | | |
1714 | | static int create_placeholder_fd(void) |
1715 | 2.92k | { |
1716 | 2.92k | int fd; |
1717 | | |
1718 | 2.92k | fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC)); |
1719 | 2.92k | if (fd < 0) |
1720 | 0 | return -errno; |
1721 | 2.92k | return fd; |
1722 | 2.92k | } |
1723 | | |
1724 | | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
1725 | 2.92k | { |
1726 | 2.92k | struct bpf_map *map; |
1727 | 2.92k | int err; |
1728 | | |
1729 | 2.92k | err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap, |
1730 | 2.92k | sizeof(*obj->maps), obj->nr_maps + 1); |
1731 | 2.92k | if (err) |
1732 | 0 | return ERR_PTR(err); |
1733 | | |
1734 | 2.92k | map = &obj->maps[obj->nr_maps++]; |
1735 | 2.92k | map->obj = obj; |
1736 | | /* Preallocate map FD without actually creating BPF map just yet. |
1737 | | * These map FD "placeholders" will be reused later without changing |
1738 | | * FD value when map is actually created in the kernel. |
1739 | | * |
1740 | | * This is useful to be able to perform BPF program relocations |
1741 | | * without having to create BPF maps before that step. This allows us |
1742 | | * to finalize and load BTF very late in BPF object's loading phase, |
1743 | | * right before BPF maps have to be created and BPF programs have to |
1744 | | * be loaded. By having these map FD placeholders we can perform all |
1745 | | * the sanitizations, relocations, and any other adjustments before we |
1746 | | * start creating actual BPF kernel objects (BTF, maps, progs). |
1747 | | */ |
1748 | 2.92k | map->fd = create_placeholder_fd(); |
1749 | 2.92k | if (map->fd < 0) |
1750 | 0 | return ERR_PTR(map->fd); |
1751 | 2.92k | map->inner_map_fd = -1; |
1752 | 2.92k | map->autocreate = true; |
1753 | | |
1754 | 2.92k | return map; |
1755 | 2.92k | } |
1756 | | |
1757 | | static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries) |
1758 | 3.28k | { |
1759 | 3.28k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1760 | 3.28k | size_t map_sz; |
1761 | | |
1762 | 3.28k | map_sz = (size_t)roundup(value_sz, 8) * max_entries; |
1763 | 3.28k | map_sz = roundup(map_sz, page_sz); |
1764 | 3.28k | return map_sz; |
1765 | 3.28k | } |
1766 | | |
1767 | | static size_t bpf_map_mmap_sz(const struct bpf_map *map) |
1768 | 3.28k | { |
1769 | 3.28k | const long page_sz = sysconf(_SC_PAGE_SIZE); |
1770 | | |
1771 | 3.28k | switch (map->def.type) { |
1772 | 3.28k | case BPF_MAP_TYPE_ARRAY: |
1773 | 3.28k | return array_map_mmap_sz(map->def.value_size, map->def.max_entries); |
1774 | 0 | case BPF_MAP_TYPE_ARENA: |
1775 | 0 | return page_sz * map->def.max_entries; |
1776 | 0 | default: |
1777 | 0 | return 0; /* not supported */ |
1778 | 3.28k | } |
1779 | 3.28k | } |
1780 | | |
1781 | | static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz) |
1782 | 0 | { |
1783 | 0 | void *mmaped; |
1784 | |
|
1785 | 0 | if (!map->mmaped) |
1786 | 0 | return -EINVAL; |
1787 | | |
1788 | 0 | if (old_sz == new_sz) |
1789 | 0 | return 0; |
1790 | | |
1791 | 0 | mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1792 | 0 | if (mmaped == MAP_FAILED) |
1793 | 0 | return -errno; |
1794 | | |
1795 | 0 | memcpy(mmaped, map->mmaped, min(old_sz, new_sz)); |
1796 | 0 | munmap(map->mmaped, old_sz); |
1797 | 0 | map->mmaped = mmaped; |
1798 | 0 | return 0; |
1799 | 0 | } |
1800 | | |
1801 | | static char *internal_map_name(struct bpf_object *obj, const char *real_name) |
1802 | 1.69k | { |
1803 | 1.69k | char map_name[BPF_OBJ_NAME_LEN], *p; |
1804 | 1.69k | int pfx_len, sfx_len = max((size_t)7, strlen(real_name)); |
1805 | | |
1806 | | /* This is one of the more confusing parts of libbpf for various |
1807 | | * reasons, some of which are historical. The original idea for naming |
1808 | | * internal names was to include as much of BPF object name prefix as |
1809 | | * possible, so that it can be distinguished from similar internal |
1810 | | * maps of a different BPF object. |
1811 | | * As an example, let's say we have bpf_object named 'my_object_name' |
1812 | | * and internal map corresponding to '.rodata' ELF section. The final |
1813 | | * map name advertised to user and to the kernel will be |
1814 | | * 'my_objec.rodata', taking first 8 characters of object name and |
1815 | | * entire 7 characters of '.rodata'. |
1816 | | * Somewhat confusingly, if internal map ELF section name is shorter |
1817 | | * than 7 characters, e.g., '.bss', we still reserve 7 characters |
1818 | | * for the suffix, even though we only have 4 actual characters, and |
1819 | | * resulting map will be called 'my_objec.bss', not even using all 15 |
1820 | | * characters allowed by the kernel. Oh well, at least the truncated |
1821 | | * object name is somewhat consistent in this case. But if the map |
1822 | | * name is '.kconfig', we'll still have entirety of '.kconfig' added |
1823 | | * (8 chars) and thus will be left with only first 7 characters of the |
1824 | | * object name ('my_obje'). Happy guessing, user, that the final map |
1825 | | * name will be "my_obje.kconfig". |
1826 | | * Now, with libbpf starting to support arbitrarily named .rodata.* |
1827 | | * and .data.* data sections, it's possible that ELF section name is |
1828 | | * longer than allowed 15 chars, so we now need to be careful to take |
1829 | | * only up to 15 first characters of ELF name, taking no BPF object |
1830 | | * name characters at all. So '.rodata.abracadabra' will result in |
1831 | | * '.rodata.abracad' kernel and user-visible name. |
1832 | | * We need to keep this convoluted logic intact for .data, .bss and |
1833 | | * .rodata maps, but for new custom .data.custom and .rodata.custom |
1834 | | * maps we use their ELF names as is, not prepending bpf_object name |
1835 | | * in front. We still need to truncate them to 15 characters for the |
1836 | | * kernel. Full name can be recovered for such maps by using DATASEC |
1837 | | * BTF type associated with such map's value type, though. |
1838 | | */ |
1839 | 1.69k | if (sfx_len >= BPF_OBJ_NAME_LEN) |
1840 | 386 | sfx_len = BPF_OBJ_NAME_LEN - 1; |
1841 | | |
1842 | | /* if there are two or more dots in map name, it's a custom dot map */ |
1843 | 1.69k | if (strchr(real_name + 1, '.') != NULL) |
1844 | 1.15k | pfx_len = 0; |
1845 | 543 | else |
1846 | 543 | pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name)); |
1847 | | |
1848 | 1.69k | snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, |
1849 | 1.69k | sfx_len, real_name); |
1850 | | |
1851 | | /* sanitise map name to characters allowed by kernel */ |
1852 | 21.6k | for (p = map_name; *p && p < map_name + sizeof(map_name); p++) |
1853 | 20.0k | if (!isalnum(*p) && *p != '_' && *p != '.') |
1854 | 2.44k | *p = '_'; |
1855 | | |
1856 | 1.69k | return strdup(map_name); |
1857 | 1.69k | } |
1858 | | |
1859 | | static int |
1860 | | map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map); |
1861 | | |
1862 | | /* Internal BPF map is mmap()'able only if at least one of corresponding |
1863 | | * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL |
1864 | | * variable and it's not marked as __hidden (which turns it into, effectively, |
1865 | | * a STATIC variable). |
1866 | | */ |
1867 | | static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map) |
1868 | 1.69k | { |
1869 | 1.69k | const struct btf_type *t, *vt; |
1870 | 1.69k | struct btf_var_secinfo *vsi; |
1871 | 1.69k | int i, n; |
1872 | | |
1873 | 1.69k | if (!map->btf_value_type_id) |
1874 | 1.52k | return false; |
1875 | | |
1876 | 169 | t = btf__type_by_id(obj->btf, map->btf_value_type_id); |
1877 | 169 | if (!btf_is_datasec(t)) |
1878 | 25 | return false; |
1879 | | |
1880 | 144 | vsi = btf_var_secinfos(t); |
1881 | 240 | for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) { |
1882 | 199 | vt = btf__type_by_id(obj->btf, vsi->type); |
1883 | 199 | if (!btf_is_var(vt)) |
1884 | 73 | continue; |
1885 | | |
1886 | 126 | if (btf_var(vt)->linkage != BTF_VAR_STATIC) |
1887 | 103 | return true; |
1888 | 126 | } |
1889 | | |
1890 | 41 | return false; |
1891 | 144 | } |
1892 | | |
1893 | | static int |
1894 | | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
1895 | | const char *real_name, int sec_idx, void *data, size_t data_sz) |
1896 | 1.69k | { |
1897 | 1.69k | struct bpf_map_def *def; |
1898 | 1.69k | struct bpf_map *map; |
1899 | 1.69k | size_t mmap_sz; |
1900 | 1.69k | int err; |
1901 | | |
1902 | 1.69k | map = bpf_object__add_map(obj); |
1903 | 1.69k | if (IS_ERR(map)) |
1904 | 0 | return PTR_ERR(map); |
1905 | | |
1906 | 1.69k | map->libbpf_type = type; |
1907 | 1.69k | map->sec_idx = sec_idx; |
1908 | 1.69k | map->sec_offset = 0; |
1909 | 1.69k | map->real_name = strdup(real_name); |
1910 | 1.69k | map->name = internal_map_name(obj, real_name); |
1911 | 1.69k | if (!map->real_name || !map->name) { |
1912 | 0 | zfree(&map->real_name); |
1913 | 0 | zfree(&map->name); |
1914 | 0 | return -ENOMEM; |
1915 | 0 | } |
1916 | | |
1917 | 1.69k | def = &map->def; |
1918 | 1.69k | def->type = BPF_MAP_TYPE_ARRAY; |
1919 | 1.69k | def->key_size = sizeof(int); |
1920 | 1.69k | def->value_size = data_sz; |
1921 | 1.69k | def->max_entries = 1; |
1922 | 1.69k | def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG |
1923 | 1.69k | ? BPF_F_RDONLY_PROG : 0; |
1924 | | |
1925 | | /* failures are fine because of maps like .rodata.str1.1 */ |
1926 | 1.69k | (void) map_fill_btf_type_info(obj, map); |
1927 | | |
1928 | 1.69k | if (map_is_mmapable(obj, map)) |
1929 | 103 | def->map_flags |= BPF_F_MMAPABLE; |
1930 | | |
1931 | 1.69k | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
1932 | 1.69k | map->name, map->sec_idx, map->sec_offset, def->map_flags); |
1933 | | |
1934 | 1.69k | mmap_sz = bpf_map_mmap_sz(map); |
1935 | 1.69k | map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, |
1936 | 1.69k | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
1937 | 1.69k | if (map->mmaped == MAP_FAILED) { |
1938 | 97 | err = -errno; |
1939 | 97 | map->mmaped = NULL; |
1940 | 97 | pr_warn("failed to alloc map '%s' content buffer: %d\n", |
1941 | 97 | map->name, err); |
1942 | 97 | zfree(&map->real_name); |
1943 | 97 | zfree(&map->name); |
1944 | 97 | return err; |
1945 | 97 | } |
1946 | | |
1947 | 1.59k | if (data) |
1948 | 976 | memcpy(map->mmaped, data, data_sz); |
1949 | | |
1950 | 1.59k | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
1951 | 1.59k | return 0; |
1952 | 1.69k | } |
1953 | | |
1954 | | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
1955 | 2.49k | { |
1956 | 2.49k | struct elf_sec_desc *sec_desc; |
1957 | 2.49k | const char *sec_name; |
1958 | 2.49k | int err = 0, sec_idx; |
1959 | | |
1960 | | /* |
1961 | | * Populate obj->maps with libbpf internal maps. |
1962 | | */ |
1963 | 17.9k | for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) { |
1964 | 15.4k | sec_desc = &obj->efile.secs[sec_idx]; |
1965 | | |
1966 | | /* Skip recognized sections with size 0. */ |
1967 | 15.4k | if (!sec_desc->data || sec_desc->data->d_size == 0) |
1968 | 12.6k | continue; |
1969 | | |
1970 | 2.83k | switch (sec_desc->sec_type) { |
1971 | 617 | case SEC_DATA: |
1972 | 617 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1973 | 617 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
1974 | 617 | sec_name, sec_idx, |
1975 | 617 | sec_desc->data->d_buf, |
1976 | 617 | sec_desc->data->d_size); |
1977 | 617 | break; |
1978 | 359 | case SEC_RODATA: |
1979 | 359 | obj->has_rodata = true; |
1980 | 359 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1981 | 359 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
1982 | 359 | sec_name, sec_idx, |
1983 | 359 | sec_desc->data->d_buf, |
1984 | 359 | sec_desc->data->d_size); |
1985 | 359 | break; |
1986 | 625 | case SEC_BSS: |
1987 | 625 | sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx)); |
1988 | 625 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
1989 | 625 | sec_name, sec_idx, |
1990 | 625 | NULL, |
1991 | 625 | sec_desc->data->d_size); |
1992 | 625 | break; |
1993 | 1.23k | default: |
1994 | | /* skip */ |
1995 | 1.23k | break; |
1996 | 2.83k | } |
1997 | 2.83k | if (err) |
1998 | 25 | return err; |
1999 | 2.83k | } |
2000 | 2.47k | return 0; |
2001 | 2.49k | } |
2002 | | |
2003 | | |
2004 | | static struct extern_desc *find_extern_by_name(const struct bpf_object *obj, |
2005 | | const void *name) |
2006 | 973 | { |
2007 | 973 | int i; |
2008 | | |
2009 | 3.60k | for (i = 0; i < obj->nr_extern; i++) { |
2010 | 3.19k | if (strcmp(obj->externs[i].name, name) == 0) |
2011 | 560 | return &obj->externs[i]; |
2012 | 3.19k | } |
2013 | 413 | return NULL; |
2014 | 973 | } |
2015 | | |
2016 | | static struct extern_desc *find_extern_by_name_with_len(const struct bpf_object *obj, |
2017 | | const void *name, int len) |
2018 | 0 | { |
2019 | 0 | const char *ext_name; |
2020 | 0 | int i; |
2021 | |
|
2022 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
2023 | 0 | ext_name = obj->externs[i].name; |
2024 | 0 | if (strlen(ext_name) == len && strncmp(ext_name, name, len) == 0) |
2025 | 0 | return &obj->externs[i]; |
2026 | 0 | } |
2027 | 0 | return NULL; |
2028 | 0 | } |
2029 | | |
2030 | | static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val, |
2031 | | char value) |
2032 | 0 | { |
2033 | 0 | switch (ext->kcfg.type) { |
2034 | 0 | case KCFG_BOOL: |
2035 | 0 | if (value == 'm') { |
2036 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n", |
2037 | 0 | ext->name, value); |
2038 | 0 | return -EINVAL; |
2039 | 0 | } |
2040 | 0 | *(bool *)ext_val = value == 'y' ? true : false; |
2041 | 0 | break; |
2042 | 0 | case KCFG_TRISTATE: |
2043 | 0 | if (value == 'y') |
2044 | 0 | *(enum libbpf_tristate *)ext_val = TRI_YES; |
2045 | 0 | else if (value == 'm') |
2046 | 0 | *(enum libbpf_tristate *)ext_val = TRI_MODULE; |
2047 | 0 | else /* value == 'n' */ |
2048 | 0 | *(enum libbpf_tristate *)ext_val = TRI_NO; |
2049 | 0 | break; |
2050 | 0 | case KCFG_CHAR: |
2051 | 0 | *(char *)ext_val = value; |
2052 | 0 | break; |
2053 | 0 | case KCFG_UNKNOWN: |
2054 | 0 | case KCFG_INT: |
2055 | 0 | case KCFG_CHAR_ARR: |
2056 | 0 | default: |
2057 | 0 | pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n", |
2058 | 0 | ext->name, value); |
2059 | 0 | return -EINVAL; |
2060 | 0 | } |
2061 | 0 | ext->is_set = true; |
2062 | 0 | return 0; |
2063 | 0 | } |
2064 | | |
2065 | | static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val, |
2066 | | const char *value) |
2067 | 0 | { |
2068 | 0 | size_t len; |
2069 | |
|
2070 | 0 | if (ext->kcfg.type != KCFG_CHAR_ARR) { |
2071 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n", |
2072 | 0 | ext->name, value); |
2073 | 0 | return -EINVAL; |
2074 | 0 | } |
2075 | | |
2076 | 0 | len = strlen(value); |
2077 | 0 | if (value[len - 1] != '"') { |
2078 | 0 | pr_warn("extern (kcfg) '%s': invalid string config '%s'\n", |
2079 | 0 | ext->name, value); |
2080 | 0 | return -EINVAL; |
2081 | 0 | } |
2082 | | |
2083 | | /* strip quotes */ |
2084 | 0 | len -= 2; |
2085 | 0 | if (len >= ext->kcfg.sz) { |
2086 | 0 | pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n", |
2087 | 0 | ext->name, value, len, ext->kcfg.sz - 1); |
2088 | 0 | len = ext->kcfg.sz - 1; |
2089 | 0 | } |
2090 | 0 | memcpy(ext_val, value + 1, len); |
2091 | 0 | ext_val[len] = '\0'; |
2092 | 0 | ext->is_set = true; |
2093 | 0 | return 0; |
2094 | 0 | } |
2095 | | |
2096 | | static int parse_u64(const char *value, __u64 *res) |
2097 | 0 | { |
2098 | 0 | char *value_end; |
2099 | 0 | int err; |
2100 | |
|
2101 | 0 | errno = 0; |
2102 | 0 | *res = strtoull(value, &value_end, 0); |
2103 | 0 | if (errno) { |
2104 | 0 | err = -errno; |
2105 | 0 | pr_warn("failed to parse '%s' as integer: %d\n", value, err); |
2106 | 0 | return err; |
2107 | 0 | } |
2108 | 0 | if (*value_end) { |
2109 | 0 | pr_warn("failed to parse '%s' as integer completely\n", value); |
2110 | 0 | return -EINVAL; |
2111 | 0 | } |
2112 | 0 | return 0; |
2113 | 0 | } |
2114 | | |
2115 | | static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v) |
2116 | 0 | { |
2117 | 0 | int bit_sz = ext->kcfg.sz * 8; |
2118 | |
|
2119 | 0 | if (ext->kcfg.sz == 8) |
2120 | 0 | return true; |
2121 | | |
2122 | | /* Validate that value stored in u64 fits in integer of `ext->sz` |
2123 | | * bytes size without any loss of information. If the target integer |
2124 | | * is signed, we rely on the following limits of integer type of |
2125 | | * Y bits and subsequent transformation: |
2126 | | * |
2127 | | * -2^(Y-1) <= X <= 2^(Y-1) - 1 |
2128 | | * 0 <= X + 2^(Y-1) <= 2^Y - 1 |
2129 | | * 0 <= X + 2^(Y-1) < 2^Y |
2130 | | * |
2131 | | * For unsigned target integer, check that all the (64 - Y) bits are |
2132 | | * zero. |
2133 | | */ |
2134 | 0 | if (ext->kcfg.is_signed) |
2135 | 0 | return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); |
2136 | 0 | else |
2137 | 0 | return (v >> bit_sz) == 0; |
2138 | 0 | } |
2139 | | |
2140 | | static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val, |
2141 | | __u64 value) |
2142 | 0 | { |
2143 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR && |
2144 | 0 | ext->kcfg.type != KCFG_BOOL) { |
2145 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n", |
2146 | 0 | ext->name, (unsigned long long)value); |
2147 | 0 | return -EINVAL; |
2148 | 0 | } |
2149 | 0 | if (ext->kcfg.type == KCFG_BOOL && value > 1) { |
2150 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n", |
2151 | 0 | ext->name, (unsigned long long)value); |
2152 | 0 | return -EINVAL; |
2153 | |
|
2154 | 0 | } |
2155 | 0 | if (!is_kcfg_value_in_range(ext, value)) { |
2156 | 0 | pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n", |
2157 | 0 | ext->name, (unsigned long long)value, ext->kcfg.sz); |
2158 | 0 | return -ERANGE; |
2159 | 0 | } |
2160 | 0 | switch (ext->kcfg.sz) { |
2161 | 0 | case 1: |
2162 | 0 | *(__u8 *)ext_val = value; |
2163 | 0 | break; |
2164 | 0 | case 2: |
2165 | 0 | *(__u16 *)ext_val = value; |
2166 | 0 | break; |
2167 | 0 | case 4: |
2168 | 0 | *(__u32 *)ext_val = value; |
2169 | 0 | break; |
2170 | 0 | case 8: |
2171 | 0 | *(__u64 *)ext_val = value; |
2172 | 0 | break; |
2173 | 0 | default: |
2174 | 0 | return -EINVAL; |
2175 | 0 | } |
2176 | 0 | ext->is_set = true; |
2177 | 0 | return 0; |
2178 | 0 | } |
2179 | | |
2180 | | static int bpf_object__process_kconfig_line(struct bpf_object *obj, |
2181 | | char *buf, void *data) |
2182 | 0 | { |
2183 | 0 | struct extern_desc *ext; |
2184 | 0 | char *sep, *value; |
2185 | 0 | int len, err = 0; |
2186 | 0 | void *ext_val; |
2187 | 0 | __u64 num; |
2188 | |
|
2189 | 0 | if (!str_has_pfx(buf, "CONFIG_")) |
2190 | 0 | return 0; |
2191 | | |
2192 | 0 | sep = strchr(buf, '='); |
2193 | 0 | if (!sep) { |
2194 | 0 | pr_warn("failed to parse '%s': no separator\n", buf); |
2195 | 0 | return -EINVAL; |
2196 | 0 | } |
2197 | | |
2198 | | /* Trim ending '\n' */ |
2199 | 0 | len = strlen(buf); |
2200 | 0 | if (buf[len - 1] == '\n') |
2201 | 0 | buf[len - 1] = '\0'; |
2202 | | /* Split on '=' and ensure that a value is present. */ |
2203 | 0 | *sep = '\0'; |
2204 | 0 | if (!sep[1]) { |
2205 | 0 | *sep = '='; |
2206 | 0 | pr_warn("failed to parse '%s': no value\n", buf); |
2207 | 0 | return -EINVAL; |
2208 | 0 | } |
2209 | | |
2210 | 0 | ext = find_extern_by_name(obj, buf); |
2211 | 0 | if (!ext || ext->is_set) |
2212 | 0 | return 0; |
2213 | | |
2214 | 0 | ext_val = data + ext->kcfg.data_off; |
2215 | 0 | value = sep + 1; |
2216 | |
|
2217 | 0 | switch (*value) { |
2218 | 0 | case 'y': case 'n': case 'm': |
2219 | 0 | err = set_kcfg_value_tri(ext, ext_val, *value); |
2220 | 0 | break; |
2221 | 0 | case '"': |
2222 | 0 | err = set_kcfg_value_str(ext, ext_val, value); |
2223 | 0 | break; |
2224 | 0 | default: |
2225 | | /* assume integer */ |
2226 | 0 | err = parse_u64(value, &num); |
2227 | 0 | if (err) { |
2228 | 0 | pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value); |
2229 | 0 | return err; |
2230 | 0 | } |
2231 | 0 | if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) { |
2232 | 0 | pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value); |
2233 | 0 | return -EINVAL; |
2234 | 0 | } |
2235 | 0 | err = set_kcfg_value_num(ext, ext_val, num); |
2236 | 0 | break; |
2237 | 0 | } |
2238 | 0 | if (err) |
2239 | 0 | return err; |
2240 | 0 | pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value); |
2241 | 0 | return 0; |
2242 | 0 | } |
2243 | | |
2244 | | static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) |
2245 | 0 | { |
2246 | 0 | char buf[PATH_MAX]; |
2247 | 0 | struct utsname uts; |
2248 | 0 | int len, err = 0; |
2249 | 0 | gzFile file; |
2250 | |
|
2251 | 0 | uname(&uts); |
2252 | 0 | len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); |
2253 | 0 | if (len < 0) |
2254 | 0 | return -EINVAL; |
2255 | 0 | else if (len >= PATH_MAX) |
2256 | 0 | return -ENAMETOOLONG; |
2257 | | |
2258 | | /* gzopen also accepts uncompressed files. */ |
2259 | 0 | file = gzopen(buf, "re"); |
2260 | 0 | if (!file) |
2261 | 0 | file = gzopen("/proc/config.gz", "re"); |
2262 | |
|
2263 | 0 | if (!file) { |
2264 | 0 | pr_warn("failed to open system Kconfig\n"); |
2265 | 0 | return -ENOENT; |
2266 | 0 | } |
2267 | | |
2268 | 0 | while (gzgets(file, buf, sizeof(buf))) { |
2269 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2270 | 0 | if (err) { |
2271 | 0 | pr_warn("error parsing system Kconfig line '%s': %d\n", |
2272 | 0 | buf, err); |
2273 | 0 | goto out; |
2274 | 0 | } |
2275 | 0 | } |
2276 | | |
2277 | 0 | out: |
2278 | 0 | gzclose(file); |
2279 | 0 | return err; |
2280 | 0 | } |
2281 | | |
2282 | | static int bpf_object__read_kconfig_mem(struct bpf_object *obj, |
2283 | | const char *config, void *data) |
2284 | 0 | { |
2285 | 0 | char buf[PATH_MAX]; |
2286 | 0 | int err = 0; |
2287 | 0 | FILE *file; |
2288 | |
|
2289 | 0 | file = fmemopen((void *)config, strlen(config), "r"); |
2290 | 0 | if (!file) { |
2291 | 0 | err = -errno; |
2292 | 0 | pr_warn("failed to open in-memory Kconfig: %d\n", err); |
2293 | 0 | return err; |
2294 | 0 | } |
2295 | | |
2296 | 0 | while (fgets(buf, sizeof(buf), file)) { |
2297 | 0 | err = bpf_object__process_kconfig_line(obj, buf, data); |
2298 | 0 | if (err) { |
2299 | 0 | pr_warn("error parsing in-memory Kconfig line '%s': %d\n", |
2300 | 0 | buf, err); |
2301 | 0 | break; |
2302 | 0 | } |
2303 | 0 | } |
2304 | |
|
2305 | 0 | fclose(file); |
2306 | 0 | return err; |
2307 | 0 | } |
2308 | | |
2309 | | static int bpf_object__init_kconfig_map(struct bpf_object *obj) |
2310 | 2.47k | { |
2311 | 2.47k | struct extern_desc *last_ext = NULL, *ext; |
2312 | 2.47k | size_t map_sz; |
2313 | 2.47k | int i, err; |
2314 | | |
2315 | 3.46k | for (i = 0; i < obj->nr_extern; i++) { |
2316 | 996 | ext = &obj->externs[i]; |
2317 | 996 | if (ext->type == EXT_KCFG) |
2318 | 246 | last_ext = ext; |
2319 | 996 | } |
2320 | | |
2321 | 2.47k | if (!last_ext) |
2322 | 2.37k | return 0; |
2323 | | |
2324 | 92 | map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz; |
2325 | 92 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG, |
2326 | 92 | ".kconfig", obj->efile.symbols_shndx, |
2327 | 92 | NULL, map_sz); |
2328 | 92 | if (err) |
2329 | 72 | return err; |
2330 | | |
2331 | 20 | obj->kconfig_map_idx = obj->nr_maps - 1; |
2332 | | |
2333 | 20 | return 0; |
2334 | 92 | } |
2335 | | |
2336 | | const struct btf_type * |
2337 | | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) |
2338 | 6.23k | { |
2339 | 6.23k | const struct btf_type *t = btf__type_by_id(btf, id); |
2340 | | |
2341 | 6.23k | if (res_id) |
2342 | 2.98k | *res_id = id; |
2343 | | |
2344 | 8.06k | while (btf_is_mod(t) || btf_is_typedef(t)) { |
2345 | 1.82k | if (res_id) |
2346 | 1.04k | *res_id = t->type; |
2347 | 1.82k | t = btf__type_by_id(btf, t->type); |
2348 | 1.82k | } |
2349 | | |
2350 | 6.23k | return t; |
2351 | 6.23k | } |
2352 | | |
2353 | | static const struct btf_type * |
2354 | | resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) |
2355 | 0 | { |
2356 | 0 | const struct btf_type *t; |
2357 | |
|
2358 | 0 | t = skip_mods_and_typedefs(btf, id, NULL); |
2359 | 0 | if (!btf_is_ptr(t)) |
2360 | 0 | return NULL; |
2361 | | |
2362 | 0 | t = skip_mods_and_typedefs(btf, t->type, res_id); |
2363 | |
|
2364 | 0 | return btf_is_func_proto(t) ? t : NULL; |
2365 | 0 | } |
2366 | | |
2367 | | static const char *__btf_kind_str(__u16 kind) |
2368 | 337 | { |
2369 | 337 | switch (kind) { |
2370 | 69 | case BTF_KIND_UNKN: return "void"; |
2371 | 10 | case BTF_KIND_INT: return "int"; |
2372 | 4 | case BTF_KIND_PTR: return "ptr"; |
2373 | 8 | case BTF_KIND_ARRAY: return "array"; |
2374 | 3 | case BTF_KIND_STRUCT: return "struct"; |
2375 | 3 | case BTF_KIND_UNION: return "union"; |
2376 | 2 | case BTF_KIND_ENUM: return "enum"; |
2377 | 9 | case BTF_KIND_FWD: return "fwd"; |
2378 | 2 | case BTF_KIND_TYPEDEF: return "typedef"; |
2379 | 3 | case BTF_KIND_VOLATILE: return "volatile"; |
2380 | 2 | case BTF_KIND_CONST: return "const"; |
2381 | 4 | case BTF_KIND_RESTRICT: return "restrict"; |
2382 | 108 | case BTF_KIND_FUNC: return "func"; |
2383 | 10 | case BTF_KIND_FUNC_PROTO: return "func_proto"; |
2384 | 29 | case BTF_KIND_VAR: return "var"; |
2385 | 52 | case BTF_KIND_DATASEC: return "datasec"; |
2386 | 7 | case BTF_KIND_FLOAT: return "float"; |
2387 | 1 | case BTF_KIND_DECL_TAG: return "decl_tag"; |
2388 | 5 | case BTF_KIND_TYPE_TAG: return "type_tag"; |
2389 | 6 | case BTF_KIND_ENUM64: return "enum64"; |
2390 | 0 | default: return "unknown"; |
2391 | 337 | } |
2392 | 337 | } |
2393 | | |
2394 | | const char *btf_kind_str(const struct btf_type *t) |
2395 | 337 | { |
2396 | 337 | return __btf_kind_str(btf_kind(t)); |
2397 | 337 | } |
2398 | | |
2399 | | /* |
2400 | | * Fetch integer attribute of BTF map definition. Such attributes are |
2401 | | * represented using a pointer to an array, in which dimensionality of array |
2402 | | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
2403 | | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
2404 | | * type definition, while using only sizeof(void *) space in ELF data section. |
2405 | | */ |
2406 | | static bool get_map_field_int(const char *map_name, const struct btf *btf, |
2407 | | const struct btf_member *m, __u32 *res) |
2408 | 786 | { |
2409 | 786 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2410 | 786 | const char *name = btf__name_by_offset(btf, m->name_off); |
2411 | 786 | const struct btf_array *arr_info; |
2412 | 786 | const struct btf_type *arr_t; |
2413 | | |
2414 | 786 | if (!btf_is_ptr(t)) { |
2415 | 20 | pr_warn("map '%s': attr '%s': expected PTR, got %s.\n", |
2416 | 20 | map_name, name, btf_kind_str(t)); |
2417 | 20 | return false; |
2418 | 20 | } |
2419 | | |
2420 | 766 | arr_t = btf__type_by_id(btf, t->type); |
2421 | 766 | if (!arr_t) { |
2422 | 0 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
2423 | 0 | map_name, name, t->type); |
2424 | 0 | return false; |
2425 | 0 | } |
2426 | 766 | if (!btf_is_array(arr_t)) { |
2427 | 5 | pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n", |
2428 | 5 | map_name, name, btf_kind_str(arr_t)); |
2429 | 5 | return false; |
2430 | 5 | } |
2431 | 761 | arr_info = btf_array(arr_t); |
2432 | 761 | *res = arr_info->nelems; |
2433 | 761 | return true; |
2434 | 766 | } |
2435 | | |
2436 | | static bool get_map_field_long(const char *map_name, const struct btf *btf, |
2437 | | const struct btf_member *m, __u64 *res) |
2438 | 65 | { |
2439 | 65 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
2440 | 65 | const char *name = btf__name_by_offset(btf, m->name_off); |
2441 | | |
2442 | 65 | if (btf_is_ptr(t)) { |
2443 | 32 | __u32 res32; |
2444 | 32 | bool ret; |
2445 | | |
2446 | 32 | ret = get_map_field_int(map_name, btf, m, &res32); |
2447 | 32 | if (ret) |
2448 | 30 | *res = (__u64)res32; |
2449 | 32 | return ret; |
2450 | 32 | } |
2451 | | |
2452 | 33 | if (!btf_is_enum(t) && !btf_is_enum64(t)) { |
2453 | 13 | pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n", |
2454 | 13 | map_name, name, btf_kind_str(t)); |
2455 | 13 | return false; |
2456 | 13 | } |
2457 | | |
2458 | 20 | if (btf_vlen(t) != 1) { |
2459 | 1 | pr_warn("map '%s': attr '%s': invalid __ulong\n", |
2460 | 1 | map_name, name); |
2461 | 1 | return false; |
2462 | 1 | } |
2463 | | |
2464 | 19 | if (btf_is_enum(t)) { |
2465 | 11 | const struct btf_enum *e = btf_enum(t); |
2466 | | |
2467 | 11 | *res = e->val; |
2468 | 11 | } else { |
2469 | 8 | const struct btf_enum64 *e = btf_enum64(t); |
2470 | | |
2471 | 8 | *res = btf_enum64_value(e); |
2472 | 8 | } |
2473 | 19 | return true; |
2474 | 20 | } |
2475 | | |
2476 | | static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) |
2477 | 1 | { |
2478 | 1 | int len; |
2479 | | |
2480 | 1 | len = snprintf(buf, buf_sz, "%s/%s", path, name); |
2481 | 1 | if (len < 0) |
2482 | 0 | return -EINVAL; |
2483 | 1 | if (len >= buf_sz) |
2484 | 0 | return -ENAMETOOLONG; |
2485 | | |
2486 | 1 | return 0; |
2487 | 1 | } |
2488 | | |
2489 | | static int build_map_pin_path(struct bpf_map *map, const char *path) |
2490 | 1 | { |
2491 | 1 | char buf[PATH_MAX]; |
2492 | 1 | int err; |
2493 | | |
2494 | 1 | if (!path) |
2495 | 1 | path = BPF_FS_DEFAULT_PATH; |
2496 | | |
2497 | 1 | err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map)); |
2498 | 1 | if (err) |
2499 | 0 | return err; |
2500 | | |
2501 | 1 | return bpf_map__set_pin_path(map, buf); |
2502 | 1 | } |
2503 | | |
2504 | | /* should match definition in bpf_helpers.h */ |
2505 | | enum libbpf_pin_type { |
2506 | | LIBBPF_PIN_NONE, |
2507 | | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ |
2508 | | LIBBPF_PIN_BY_NAME, |
2509 | | }; |
2510 | | |
2511 | | int parse_btf_map_def(const char *map_name, struct btf *btf, |
2512 | | const struct btf_type *def_t, bool strict, |
2513 | | struct btf_map_def *map_def, struct btf_map_def *inner_def) |
2514 | 1.16k | { |
2515 | 1.16k | const struct btf_type *t; |
2516 | 1.16k | const struct btf_member *m; |
2517 | 1.16k | bool is_inner = inner_def == NULL; |
2518 | 1.16k | int vlen, i; |
2519 | | |
2520 | 1.16k | vlen = btf_vlen(def_t); |
2521 | 1.16k | m = btf_members(def_t); |
2522 | 2.51k | for (i = 0; i < vlen; i++, m++) { |
2523 | 2.39k | const char *name = btf__name_by_offset(btf, m->name_off); |
2524 | | |
2525 | 2.39k | if (!name) { |
2526 | 0 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
2527 | 0 | return -EINVAL; |
2528 | 0 | } |
2529 | 2.39k | if (strcmp(name, "type") == 0) { |
2530 | 292 | if (!get_map_field_int(map_name, btf, m, &map_def->map_type)) |
2531 | 5 | return -EINVAL; |
2532 | 287 | map_def->parts |= MAP_DEF_MAP_TYPE; |
2533 | 2.10k | } else if (strcmp(name, "max_entries") == 0) { |
2534 | 15 | if (!get_map_field_int(map_name, btf, m, &map_def->max_entries)) |
2535 | 1 | return -EINVAL; |
2536 | 14 | map_def->parts |= MAP_DEF_MAX_ENTRIES; |
2537 | 2.08k | } else if (strcmp(name, "map_flags") == 0) { |
2538 | 13 | if (!get_map_field_int(map_name, btf, m, &map_def->map_flags)) |
2539 | 2 | return -EINVAL; |
2540 | 11 | map_def->parts |= MAP_DEF_MAP_FLAGS; |
2541 | 2.07k | } else if (strcmp(name, "numa_node") == 0) { |
2542 | 24 | if (!get_map_field_int(map_name, btf, m, &map_def->numa_node)) |
2543 | 3 | return -EINVAL; |
2544 | 21 | map_def->parts |= MAP_DEF_NUMA_NODE; |
2545 | 2.04k | } else if (strcmp(name, "key_size") == 0) { |
2546 | 154 | __u32 sz; |
2547 | | |
2548 | 154 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2549 | 4 | return -EINVAL; |
2550 | 150 | if (map_def->key_size && map_def->key_size != sz) { |
2551 | 64 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
2552 | 64 | map_name, map_def->key_size, sz); |
2553 | 64 | return -EINVAL; |
2554 | 64 | } |
2555 | 86 | map_def->key_size = sz; |
2556 | 86 | map_def->parts |= MAP_DEF_KEY_SIZE; |
2557 | 1.89k | } else if (strcmp(name, "key") == 0) { |
2558 | 376 | __s64 sz; |
2559 | | |
2560 | 376 | t = btf__type_by_id(btf, m->type); |
2561 | 376 | if (!t) { |
2562 | 0 | pr_warn("map '%s': key type [%d] not found.\n", |
2563 | 0 | map_name, m->type); |
2564 | 0 | return -EINVAL; |
2565 | 0 | } |
2566 | 376 | if (!btf_is_ptr(t)) { |
2567 | 8 | pr_warn("map '%s': key spec is not PTR: %s.\n", |
2568 | 8 | map_name, btf_kind_str(t)); |
2569 | 8 | return -EINVAL; |
2570 | 8 | } |
2571 | 368 | sz = btf__resolve_size(btf, t->type); |
2572 | 368 | if (sz < 0) { |
2573 | 11 | pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n", |
2574 | 11 | map_name, t->type, (ssize_t)sz); |
2575 | 11 | return sz; |
2576 | 11 | } |
2577 | 357 | if (map_def->key_size && map_def->key_size != sz) { |
2578 | 50 | pr_warn("map '%s': conflicting key size %u != %zd.\n", |
2579 | 50 | map_name, map_def->key_size, (ssize_t)sz); |
2580 | 50 | return -EINVAL; |
2581 | 50 | } |
2582 | 307 | map_def->key_size = sz; |
2583 | 307 | map_def->key_type_id = t->type; |
2584 | 307 | map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE; |
2585 | 1.51k | } else if (strcmp(name, "value_size") == 0) { |
2586 | 182 | __u32 sz; |
2587 | | |
2588 | 182 | if (!get_map_field_int(map_name, btf, m, &sz)) |
2589 | 7 | return -EINVAL; |
2590 | 175 | if (map_def->value_size && map_def->value_size != sz) { |
2591 | 65 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
2592 | 65 | map_name, map_def->value_size, sz); |
2593 | 65 | return -EINVAL; |
2594 | 65 | } |
2595 | 110 | map_def->value_size = sz; |
2596 | 110 | map_def->parts |= MAP_DEF_VALUE_SIZE; |
2597 | 1.33k | } else if (strcmp(name, "value") == 0) { |
2598 | 500 | __s64 sz; |
2599 | | |
2600 | 500 | t = btf__type_by_id(btf, m->type); |
2601 | 500 | if (!t) { |
2602 | 0 | pr_warn("map '%s': value type [%d] not found.\n", |
2603 | 0 | map_name, m->type); |
2604 | 0 | return -EINVAL; |
2605 | 0 | } |
2606 | 500 | if (!btf_is_ptr(t)) { |
2607 | 7 | pr_warn("map '%s': value spec is not PTR: %s.\n", |
2608 | 7 | map_name, btf_kind_str(t)); |
2609 | 7 | return -EINVAL; |
2610 | 7 | } |
2611 | 493 | sz = btf__resolve_size(btf, t->type); |
2612 | 493 | if (sz < 0) { |
2613 | 7 | pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n", |
2614 | 7 | map_name, t->type, (ssize_t)sz); |
2615 | 7 | return sz; |
2616 | 7 | } |
2617 | 486 | if (map_def->value_size && map_def->value_size != sz) { |
2618 | 45 | pr_warn("map '%s': conflicting value size %u != %zd.\n", |
2619 | 45 | map_name, map_def->value_size, (ssize_t)sz); |
2620 | 45 | return -EINVAL; |
2621 | 45 | } |
2622 | 441 | map_def->value_size = sz; |
2623 | 441 | map_def->value_type_id = t->type; |
2624 | 441 | map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE; |
2625 | 441 | } |
2626 | 837 | else if (strcmp(name, "values") == 0) { |
2627 | 162 | bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type); |
2628 | 162 | bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY; |
2629 | 162 | const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value"; |
2630 | 162 | char inner_map_name[128]; |
2631 | 162 | int err; |
2632 | | |
2633 | 162 | if (is_inner) { |
2634 | 2 | pr_warn("map '%s': multi-level inner maps not supported.\n", |
2635 | 2 | map_name); |
2636 | 2 | return -ENOTSUP; |
2637 | 2 | } |
2638 | 160 | if (i != vlen - 1) { |
2639 | 11 | pr_warn("map '%s': '%s' member should be last.\n", |
2640 | 11 | map_name, name); |
2641 | 11 | return -EINVAL; |
2642 | 11 | } |
2643 | 149 | if (!is_map_in_map && !is_prog_array) { |
2644 | 46 | pr_warn("map '%s': should be map-in-map or prog-array.\n", |
2645 | 46 | map_name); |
2646 | 46 | return -ENOTSUP; |
2647 | 46 | } |
2648 | 103 | if (map_def->value_size && map_def->value_size != 4) { |
2649 | 47 | pr_warn("map '%s': conflicting value size %u != 4.\n", |
2650 | 47 | map_name, map_def->value_size); |
2651 | 47 | return -EINVAL; |
2652 | 47 | } |
2653 | 56 | map_def->value_size = 4; |
2654 | 56 | t = btf__type_by_id(btf, m->type); |
2655 | 56 | if (!t) { |
2656 | 0 | pr_warn("map '%s': %s type [%d] not found.\n", |
2657 | 0 | map_name, desc, m->type); |
2658 | 0 | return -EINVAL; |
2659 | 0 | } |
2660 | 56 | if (!btf_is_array(t) || btf_array(t)->nelems) { |
2661 | 41 | pr_warn("map '%s': %s spec is not a zero-sized array.\n", |
2662 | 41 | map_name, desc); |
2663 | 41 | return -EINVAL; |
2664 | 41 | } |
2665 | 15 | t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL); |
2666 | 15 | if (!btf_is_ptr(t)) { |
2667 | 3 | pr_warn("map '%s': %s def is of unexpected kind %s.\n", |
2668 | 3 | map_name, desc, btf_kind_str(t)); |
2669 | 3 | return -EINVAL; |
2670 | 3 | } |
2671 | 12 | t = skip_mods_and_typedefs(btf, t->type, NULL); |
2672 | 12 | if (is_prog_array) { |
2673 | 4 | if (!btf_is_func_proto(t)) { |
2674 | 3 | pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n", |
2675 | 3 | map_name, btf_kind_str(t)); |
2676 | 3 | return -EINVAL; |
2677 | 3 | } |
2678 | 1 | continue; |
2679 | 4 | } |
2680 | 8 | if (!btf_is_struct(t)) { |
2681 | 5 | pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n", |
2682 | 5 | map_name, btf_kind_str(t)); |
2683 | 5 | return -EINVAL; |
2684 | 5 | } |
2685 | | |
2686 | 3 | snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name); |
2687 | 3 | err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL); |
2688 | 3 | if (err) |
2689 | 3 | return err; |
2690 | | |
2691 | 0 | map_def->parts |= MAP_DEF_INNER_MAP; |
2692 | 675 | } else if (strcmp(name, "pinning") == 0) { |
2693 | 74 | __u32 val; |
2694 | | |
2695 | 74 | if (is_inner) { |
2696 | 0 | pr_warn("map '%s': inner def can't be pinned.\n", map_name); |
2697 | 0 | return -EINVAL; |
2698 | 0 | } |
2699 | 74 | if (!get_map_field_int(map_name, btf, m, &val)) |
2700 | 1 | return -EINVAL; |
2701 | 73 | if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) { |
2702 | 51 | pr_warn("map '%s': invalid pinning value %u.\n", |
2703 | 51 | map_name, val); |
2704 | 51 | return -EINVAL; |
2705 | 51 | } |
2706 | 22 | map_def->pinning = val; |
2707 | 22 | map_def->parts |= MAP_DEF_PINNING; |
2708 | 601 | } else if (strcmp(name, "map_extra") == 0) { |
2709 | 65 | __u64 map_extra; |
2710 | | |
2711 | 65 | if (!get_map_field_long(map_name, btf, m, &map_extra)) |
2712 | 16 | return -EINVAL; |
2713 | 49 | map_def->map_extra = map_extra; |
2714 | 49 | map_def->parts |= MAP_DEF_MAP_EXTRA; |
2715 | 536 | } else { |
2716 | 536 | if (strict) { |
2717 | 536 | pr_warn("map '%s': unknown field '%s'.\n", map_name, name); |
2718 | 536 | return -ENOTSUP; |
2719 | 536 | } |
2720 | 0 | pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name); |
2721 | 0 | } |
2722 | 2.39k | } |
2723 | | |
2724 | 117 | if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) { |
2725 | 41 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
2726 | 41 | return -EINVAL; |
2727 | 41 | } |
2728 | | |
2729 | 76 | return 0; |
2730 | 117 | } |
2731 | | |
2732 | | static size_t adjust_ringbuf_sz(size_t sz) |
2733 | 3 | { |
2734 | 3 | __u32 page_sz = sysconf(_SC_PAGE_SIZE); |
2735 | 3 | __u32 mul; |
2736 | | |
2737 | | /* if user forgot to set any size, make sure they see error */ |
2738 | 3 | if (sz == 0) |
2739 | 1 | return 0; |
2740 | | /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be |
2741 | | * a power-of-2 multiple of kernel's page size. If user diligently |
2742 | | * satisified these conditions, pass the size through. |
2743 | | */ |
2744 | 2 | if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz)) |
2745 | 0 | return sz; |
2746 | | |
2747 | | /* Otherwise find closest (page_sz * power_of_2) product bigger than |
2748 | | * user-set size to satisfy both user size request and kernel |
2749 | | * requirements and substitute correct max_entries for map creation. |
2750 | | */ |
2751 | 2 | for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) { |
2752 | 2 | if (mul * page_sz > sz) |
2753 | 2 | return mul * page_sz; |
2754 | 2 | } |
2755 | | |
2756 | | /* if it's impossible to satisfy the conditions (i.e., user size is |
2757 | | * very close to UINT_MAX but is not a power-of-2 multiple of |
2758 | | * page_size) then just return original size and let kernel reject it |
2759 | | */ |
2760 | 0 | return sz; |
2761 | 2 | } |
2762 | | |
2763 | | static bool map_is_ringbuf(const struct bpf_map *map) |
2764 | 76 | { |
2765 | 76 | return map->def.type == BPF_MAP_TYPE_RINGBUF || |
2766 | 76 | map->def.type == BPF_MAP_TYPE_USER_RINGBUF; |
2767 | 76 | } |
2768 | | |
2769 | | static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def) |
2770 | 76 | { |
2771 | 76 | map->def.type = def->map_type; |
2772 | 76 | map->def.key_size = def->key_size; |
2773 | 76 | map->def.value_size = def->value_size; |
2774 | 76 | map->def.max_entries = def->max_entries; |
2775 | 76 | map->def.map_flags = def->map_flags; |
2776 | 76 | map->map_extra = def->map_extra; |
2777 | | |
2778 | 76 | map->numa_node = def->numa_node; |
2779 | 76 | map->btf_key_type_id = def->key_type_id; |
2780 | 76 | map->btf_value_type_id = def->value_type_id; |
2781 | | |
2782 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
2783 | 76 | if (map_is_ringbuf(map)) |
2784 | 3 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
2785 | | |
2786 | 76 | if (def->parts & MAP_DEF_MAP_TYPE) |
2787 | 76 | pr_debug("map '%s': found type = %u.\n", map->name, def->map_type); |
2788 | | |
2789 | 76 | if (def->parts & MAP_DEF_KEY_TYPE) |
2790 | 76 | pr_debug("map '%s': found key [%u], sz = %u.\n", |
2791 | 62 | map->name, def->key_type_id, def->key_size); |
2792 | 62 | else if (def->parts & MAP_DEF_KEY_SIZE) |
2793 | 3 | pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size); |
2794 | | |
2795 | 76 | if (def->parts & MAP_DEF_VALUE_TYPE) |
2796 | 76 | pr_debug("map '%s': found value [%u], sz = %u.\n", |
2797 | 32 | map->name, def->value_type_id, def->value_size); |
2798 | 32 | else if (def->parts & MAP_DEF_VALUE_SIZE) |
2799 | 15 | pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size); |
2800 | | |
2801 | 76 | if (def->parts & MAP_DEF_MAX_ENTRIES) |
2802 | 76 | pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries); |
2803 | 76 | if (def->parts & MAP_DEF_MAP_FLAGS) |
2804 | 76 | pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags); |
2805 | 76 | if (def->parts & MAP_DEF_MAP_EXTRA) |
2806 | 76 | pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name, |
2807 | 76 | (unsigned long long)def->map_extra); |
2808 | 76 | if (def->parts & MAP_DEF_PINNING) |
2809 | 76 | pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning); |
2810 | 76 | if (def->parts & MAP_DEF_NUMA_NODE) |
2811 | 76 | pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node); |
2812 | | |
2813 | 76 | if (def->parts & MAP_DEF_INNER_MAP) |
2814 | 76 | pr_debug("map '%s': found inner map definition.\n", map->name); |
2815 | 76 | } |
2816 | | |
2817 | | static const char *btf_var_linkage_str(__u32 linkage) |
2818 | 52 | { |
2819 | 52 | switch (linkage) { |
2820 | 1 | case BTF_VAR_STATIC: return "static"; |
2821 | 0 | case BTF_VAR_GLOBAL_ALLOCATED: return "global"; |
2822 | 1 | case BTF_VAR_GLOBAL_EXTERN: return "extern"; |
2823 | 50 | default: return "unknown"; |
2824 | 52 | } |
2825 | 52 | } |
2826 | | |
2827 | | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
2828 | | const struct btf_type *sec, |
2829 | | int var_idx, int sec_idx, |
2830 | | const Elf_Data *data, bool strict, |
2831 | | const char *pin_root_path) |
2832 | 1.23k | { |
2833 | 1.23k | struct btf_map_def map_def = {}, inner_def = {}; |
2834 | 1.23k | const struct btf_type *var, *def; |
2835 | 1.23k | const struct btf_var_secinfo *vi; |
2836 | 1.23k | const struct btf_var *var_extra; |
2837 | 1.23k | const char *map_name; |
2838 | 1.23k | struct bpf_map *map; |
2839 | 1.23k | int err; |
2840 | | |
2841 | 1.23k | vi = btf_var_secinfos(sec) + var_idx; |
2842 | 1.23k | var = btf__type_by_id(obj->btf, vi->type); |
2843 | 1.23k | var_extra = btf_var(var); |
2844 | 1.23k | map_name = btf__name_by_offset(obj->btf, var->name_off); |
2845 | | |
2846 | 1.23k | if (map_name == NULL || map_name[0] == '\0') { |
2847 | 1 | pr_warn("map #%d: empty name.\n", var_idx); |
2848 | 1 | return -EINVAL; |
2849 | 1 | } |
2850 | 1.23k | if ((__u64)vi->offset + vi->size > data->d_size) { |
2851 | 3 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
2852 | 3 | return -EINVAL; |
2853 | 3 | } |
2854 | 1.22k | if (!btf_is_var(var)) { |
2855 | 0 | pr_warn("map '%s': unexpected var kind %s.\n", |
2856 | 0 | map_name, btf_kind_str(var)); |
2857 | 0 | return -EINVAL; |
2858 | 0 | } |
2859 | 1.22k | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) { |
2860 | 52 | pr_warn("map '%s': unsupported map linkage %s.\n", |
2861 | 52 | map_name, btf_var_linkage_str(var_extra->linkage)); |
2862 | 52 | return -EOPNOTSUPP; |
2863 | 52 | } |
2864 | | |
2865 | 1.17k | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
2866 | 1.17k | if (!btf_is_struct(def)) { |
2867 | 16 | pr_warn("map '%s': unexpected def kind %s.\n", |
2868 | 16 | map_name, btf_kind_str(var)); |
2869 | 16 | return -EINVAL; |
2870 | 16 | } |
2871 | 1.15k | if (def->size > vi->size) { |
2872 | 1 | pr_warn("map '%s': invalid def size.\n", map_name); |
2873 | 1 | return -EINVAL; |
2874 | 1 | } |
2875 | | |
2876 | 1.15k | map = bpf_object__add_map(obj); |
2877 | 1.15k | if (IS_ERR(map)) |
2878 | 0 | return PTR_ERR(map); |
2879 | 1.15k | map->name = strdup(map_name); |
2880 | 1.15k | if (!map->name) { |
2881 | 0 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
2882 | 0 | return -ENOMEM; |
2883 | 0 | } |
2884 | 1.15k | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
2885 | 1.15k | map->def.type = BPF_MAP_TYPE_UNSPEC; |
2886 | 1.15k | map->sec_idx = sec_idx; |
2887 | 1.15k | map->sec_offset = vi->offset; |
2888 | 1.15k | map->btf_var_idx = var_idx; |
2889 | 1.15k | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
2890 | 1.15k | map_name, map->sec_idx, map->sec_offset); |
2891 | | |
2892 | 1.15k | err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def); |
2893 | 1.15k | if (err) |
2894 | 1.08k | return err; |
2895 | | |
2896 | 76 | fill_map_from_def(map, &map_def); |
2897 | | |
2898 | 76 | if (map_def.pinning == LIBBPF_PIN_BY_NAME) { |
2899 | 1 | err = build_map_pin_path(map, pin_root_path); |
2900 | 1 | if (err) { |
2901 | 0 | pr_warn("map '%s': couldn't build pin path.\n", map->name); |
2902 | 0 | return err; |
2903 | 0 | } |
2904 | 1 | } |
2905 | | |
2906 | 76 | if (map_def.parts & MAP_DEF_INNER_MAP) { |
2907 | 0 | map->inner_map = calloc(1, sizeof(*map->inner_map)); |
2908 | 0 | if (!map->inner_map) |
2909 | 0 | return -ENOMEM; |
2910 | 0 | map->inner_map->fd = create_placeholder_fd(); |
2911 | 0 | if (map->inner_map->fd < 0) |
2912 | 0 | return map->inner_map->fd; |
2913 | 0 | map->inner_map->sec_idx = sec_idx; |
2914 | 0 | map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1); |
2915 | 0 | if (!map->inner_map->name) |
2916 | 0 | return -ENOMEM; |
2917 | 0 | sprintf(map->inner_map->name, "%s.inner", map_name); |
2918 | |
|
2919 | 0 | fill_map_from_def(map->inner_map, &inner_def); |
2920 | 0 | } |
2921 | | |
2922 | 76 | err = map_fill_btf_type_info(obj, map); |
2923 | 76 | if (err) |
2924 | 0 | return err; |
2925 | | |
2926 | 76 | return 0; |
2927 | 76 | } |
2928 | | |
2929 | | static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map, |
2930 | | const char *sec_name, int sec_idx, |
2931 | | void *data, size_t data_sz) |
2932 | 0 | { |
2933 | 0 | const long page_sz = sysconf(_SC_PAGE_SIZE); |
2934 | 0 | size_t mmap_sz; |
2935 | |
|
2936 | 0 | mmap_sz = bpf_map_mmap_sz(obj->arena_map); |
2937 | 0 | if (roundup(data_sz, page_sz) > mmap_sz) { |
2938 | 0 | pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n", |
2939 | 0 | sec_name, mmap_sz, data_sz); |
2940 | 0 | return -E2BIG; |
2941 | 0 | } |
2942 | | |
2943 | 0 | obj->arena_data = malloc(data_sz); |
2944 | 0 | if (!obj->arena_data) |
2945 | 0 | return -ENOMEM; |
2946 | 0 | memcpy(obj->arena_data, data, data_sz); |
2947 | 0 | obj->arena_data_sz = data_sz; |
2948 | | |
2949 | | /* make bpf_map__init_value() work for ARENA maps */ |
2950 | 0 | map->mmaped = obj->arena_data; |
2951 | |
|
2952 | 0 | return 0; |
2953 | 0 | } |
2954 | | |
2955 | | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
2956 | | const char *pin_root_path) |
2957 | 3.69k | { |
2958 | 3.69k | const struct btf_type *sec = NULL; |
2959 | 3.69k | int nr_types, i, vlen, err; |
2960 | 3.69k | const struct btf_type *t; |
2961 | 3.69k | const char *name; |
2962 | 3.69k | Elf_Data *data; |
2963 | 3.69k | Elf_Scn *scn; |
2964 | | |
2965 | 3.69k | if (obj->efile.btf_maps_shndx < 0) |
2966 | 2.41k | return 0; |
2967 | | |
2968 | 1.27k | scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx); |
2969 | 1.27k | data = elf_sec_data(obj, scn); |
2970 | 1.27k | if (!scn || !data) { |
2971 | 0 | pr_warn("elf: failed to get %s map definitions for %s\n", |
2972 | 0 | MAPS_ELF_SEC, obj->path); |
2973 | 0 | return -EINVAL; |
2974 | 0 | } |
2975 | | |
2976 | 1.27k | nr_types = btf__type_cnt(obj->btf); |
2977 | 15.7k | for (i = 1; i < nr_types; i++) { |
2978 | 15.6k | t = btf__type_by_id(obj->btf, i); |
2979 | 15.6k | if (!btf_is_datasec(t)) |
2980 | 13.9k | continue; |
2981 | 1.79k | name = btf__name_by_offset(obj->btf, t->name_off); |
2982 | 1.79k | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
2983 | 1.23k | sec = t; |
2984 | 1.23k | obj->efile.btf_maps_sec_btf_id = i; |
2985 | 1.23k | break; |
2986 | 1.23k | } |
2987 | 1.79k | } |
2988 | | |
2989 | 1.27k | if (!sec) { |
2990 | 42 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
2991 | 42 | return -ENOENT; |
2992 | 42 | } |
2993 | | |
2994 | 1.23k | vlen = btf_vlen(sec); |
2995 | 1.30k | for (i = 0; i < vlen; i++) { |
2996 | 1.23k | err = bpf_object__init_user_btf_map(obj, sec, i, |
2997 | 1.23k | obj->efile.btf_maps_shndx, |
2998 | 1.23k | data, strict, |
2999 | 1.23k | pin_root_path); |
3000 | 1.23k | if (err) |
3001 | 1.15k | return err; |
3002 | 1.23k | } |
3003 | | |
3004 | 154 | for (i = 0; i < obj->nr_maps; i++) { |
3005 | 76 | struct bpf_map *map = &obj->maps[i]; |
3006 | | |
3007 | 76 | if (map->def.type != BPF_MAP_TYPE_ARENA) |
3008 | 75 | continue; |
3009 | | |
3010 | 1 | if (obj->arena_map) { |
3011 | 0 | pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n", |
3012 | 0 | map->name, obj->arena_map->name); |
3013 | 0 | return -EINVAL; |
3014 | 0 | } |
3015 | 1 | obj->arena_map = map; |
3016 | | |
3017 | 1 | if (obj->efile.arena_data) { |
3018 | 0 | err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx, |
3019 | 0 | obj->efile.arena_data->d_buf, |
3020 | 0 | obj->efile.arena_data->d_size); |
3021 | 0 | if (err) |
3022 | 0 | return err; |
3023 | 0 | } |
3024 | 1 | } |
3025 | 78 | if (obj->efile.arena_data && !obj->arena_map) { |
3026 | 1 | pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n", |
3027 | 1 | ARENA_SEC); |
3028 | 1 | return -ENOENT; |
3029 | 1 | } |
3030 | | |
3031 | 77 | return 0; |
3032 | 78 | } |
3033 | | |
3034 | | static int bpf_object__init_maps(struct bpf_object *obj, |
3035 | | const struct bpf_object_open_opts *opts) |
3036 | 3.69k | { |
3037 | 3.69k | const char *pin_root_path; |
3038 | 3.69k | bool strict; |
3039 | 3.69k | int err = 0; |
3040 | | |
3041 | 3.69k | strict = !OPTS_GET(opts, relaxed_maps, false); |
3042 | 3.69k | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
3043 | | |
3044 | 3.69k | err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
3045 | 3.69k | err = err ?: bpf_object__init_global_data_maps(obj); |
3046 | 3.69k | err = err ?: bpf_object__init_kconfig_map(obj); |
3047 | 18.4E | err = err ?: bpf_object_init_struct_ops(obj); |
3048 | | |
3049 | 18.4E | return err; |
3050 | 1.27k | } |
3051 | | |
3052 | | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
3053 | 2.04k | { |
3054 | 2.04k | Elf64_Shdr *sh; |
3055 | | |
3056 | 2.04k | sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx)); |
3057 | 2.04k | if (!sh) |
3058 | 0 | return false; |
3059 | | |
3060 | 2.04k | return sh->sh_flags & SHF_EXECINSTR; |
3061 | 2.04k | } |
3062 | | |
3063 | | static bool starts_with_qmark(const char *s) |
3064 | 0 | { |
3065 | 0 | return s && s[0] == '?'; |
3066 | 0 | } |
3067 | | |
3068 | | static bool btf_needs_sanitization(struct bpf_object *obj) |
3069 | 0 | { |
3070 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3071 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3072 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3073 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3074 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3075 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3076 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3077 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3078 | |
|
3079 | 0 | return !has_func || !has_datasec || !has_func_global || !has_float || |
3080 | 0 | !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec; |
3081 | 0 | } |
3082 | | |
3083 | | static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf) |
3084 | 0 | { |
3085 | 0 | bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC); |
3086 | 0 | bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC); |
3087 | 0 | bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT); |
3088 | 0 | bool has_func = kernel_supports(obj, FEAT_BTF_FUNC); |
3089 | 0 | bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG); |
3090 | 0 | bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG); |
3091 | 0 | bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64); |
3092 | 0 | bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC); |
3093 | 0 | int enum64_placeholder_id = 0; |
3094 | 0 | struct btf_type *t; |
3095 | 0 | int i, j, vlen; |
3096 | |
|
3097 | 0 | for (i = 1; i < btf__type_cnt(btf); i++) { |
3098 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3099 | |
|
3100 | 0 | if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) { |
3101 | | /* replace VAR/DECL_TAG with INT */ |
3102 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
3103 | | /* |
3104 | | * using size = 1 is the safest choice, 4 will be too |
3105 | | * big and cause kernel BTF validation failure if |
3106 | | * original variable took less than 4 bytes |
3107 | | */ |
3108 | 0 | t->size = 1; |
3109 | 0 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
3110 | 0 | } else if (!has_datasec && btf_is_datasec(t)) { |
3111 | | /* replace DATASEC with STRUCT */ |
3112 | 0 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
3113 | 0 | struct btf_member *m = btf_members(t); |
3114 | 0 | struct btf_type *vt; |
3115 | 0 | char *name; |
3116 | |
|
3117 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3118 | 0 | while (*name) { |
3119 | 0 | if (*name == '.' || *name == '?') |
3120 | 0 | *name = '_'; |
3121 | 0 | name++; |
3122 | 0 | } |
3123 | |
|
3124 | 0 | vlen = btf_vlen(t); |
3125 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
3126 | 0 | for (j = 0; j < vlen; j++, v++, m++) { |
3127 | | /* order of field assignments is important */ |
3128 | 0 | m->offset = v->offset * 8; |
3129 | 0 | m->type = v->type; |
3130 | | /* preserve variable name as member name */ |
3131 | 0 | vt = (void *)btf__type_by_id(btf, v->type); |
3132 | 0 | m->name_off = vt->name_off; |
3133 | 0 | } |
3134 | 0 | } else if (!has_qmark_datasec && btf_is_datasec(t) && |
3135 | 0 | starts_with_qmark(btf__name_by_offset(btf, t->name_off))) { |
3136 | | /* replace '?' prefix with '_' for DATASEC names */ |
3137 | 0 | char *name; |
3138 | |
|
3139 | 0 | name = (char *)btf__name_by_offset(btf, t->name_off); |
3140 | 0 | if (name[0] == '?') |
3141 | 0 | name[0] = '_'; |
3142 | 0 | } else if (!has_func && btf_is_func_proto(t)) { |
3143 | | /* replace FUNC_PROTO with ENUM */ |
3144 | 0 | vlen = btf_vlen(t); |
3145 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
3146 | 0 | t->size = sizeof(__u32); /* kernel enforced */ |
3147 | 0 | } else if (!has_func && btf_is_func(t)) { |
3148 | | /* replace FUNC with TYPEDEF */ |
3149 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
3150 | 0 | } else if (!has_func_global && btf_is_func(t)) { |
3151 | | /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */ |
3152 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); |
3153 | 0 | } else if (!has_float && btf_is_float(t)) { |
3154 | | /* replace FLOAT with an equally-sized empty STRUCT; |
3155 | | * since C compilers do not accept e.g. "float" as a |
3156 | | * valid struct name, make it anonymous |
3157 | | */ |
3158 | 0 | t->name_off = 0; |
3159 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0); |
3160 | 0 | } else if (!has_type_tag && btf_is_type_tag(t)) { |
3161 | | /* replace TYPE_TAG with a CONST */ |
3162 | 0 | t->name_off = 0; |
3163 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0); |
3164 | 0 | } else if (!has_enum64 && btf_is_enum(t)) { |
3165 | | /* clear the kflag */ |
3166 | 0 | t->info = btf_type_info(btf_kind(t), btf_vlen(t), false); |
3167 | 0 | } else if (!has_enum64 && btf_is_enum64(t)) { |
3168 | | /* replace ENUM64 with a union */ |
3169 | 0 | struct btf_member *m; |
3170 | |
|
3171 | 0 | if (enum64_placeholder_id == 0) { |
3172 | 0 | enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0); |
3173 | 0 | if (enum64_placeholder_id < 0) |
3174 | 0 | return enum64_placeholder_id; |
3175 | | |
3176 | 0 | t = (struct btf_type *)btf__type_by_id(btf, i); |
3177 | 0 | } |
3178 | | |
3179 | 0 | m = btf_members(t); |
3180 | 0 | vlen = btf_vlen(t); |
3181 | 0 | t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen); |
3182 | 0 | for (j = 0; j < vlen; j++, m++) { |
3183 | 0 | m->type = enum64_placeholder_id; |
3184 | 0 | m->offset = 0; |
3185 | 0 | } |
3186 | 0 | } |
3187 | 0 | } |
3188 | | |
3189 | 0 | return 0; |
3190 | 0 | } |
3191 | | |
3192 | | static bool libbpf_needs_btf(const struct bpf_object *obj) |
3193 | 3.10k | { |
3194 | 3.10k | return obj->efile.btf_maps_shndx >= 0 || |
3195 | 3.10k | obj->efile.has_st_ops || |
3196 | 3.10k | obj->nr_extern > 0; |
3197 | 3.10k | } |
3198 | | |
3199 | | static bool kernel_needs_btf(const struct bpf_object *obj) |
3200 | 0 | { |
3201 | 0 | return obj->efile.has_st_ops; |
3202 | 0 | } |
3203 | | |
3204 | | static int bpf_object__init_btf(struct bpf_object *obj, |
3205 | | Elf_Data *btf_data, |
3206 | | Elf_Data *btf_ext_data) |
3207 | 6.98k | { |
3208 | 6.98k | int err = -ENOENT; |
3209 | | |
3210 | 6.98k | if (btf_data) { |
3211 | 5.06k | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
3212 | 5.06k | err = libbpf_get_error(obj->btf); |
3213 | 5.06k | if (err) { |
3214 | 926 | obj->btf = NULL; |
3215 | 926 | pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err); |
3216 | 926 | goto out; |
3217 | 926 | } |
3218 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3219 | 4.13k | btf__set_pointer_size(obj->btf, 8); |
3220 | 4.13k | } |
3221 | 6.06k | if (btf_ext_data) { |
3222 | 301 | struct btf_ext_info *ext_segs[3]; |
3223 | 301 | int seg_num, sec_num; |
3224 | | |
3225 | 301 | if (!obj->btf) { |
3226 | 6 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
3227 | 6 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
3228 | 6 | goto out; |
3229 | 6 | } |
3230 | 295 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size); |
3231 | 295 | err = libbpf_get_error(obj->btf_ext); |
3232 | 295 | if (err) { |
3233 | 254 | pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n", |
3234 | 254 | BTF_EXT_ELF_SEC, err); |
3235 | 254 | obj->btf_ext = NULL; |
3236 | 254 | goto out; |
3237 | 254 | } |
3238 | | |
3239 | | /* setup .BTF.ext to ELF section mapping */ |
3240 | 41 | ext_segs[0] = &obj->btf_ext->func_info; |
3241 | 41 | ext_segs[1] = &obj->btf_ext->line_info; |
3242 | 41 | ext_segs[2] = &obj->btf_ext->core_relo_info; |
3243 | 164 | for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) { |
3244 | 123 | struct btf_ext_info *seg = ext_segs[seg_num]; |
3245 | 123 | const struct btf_ext_info_sec *sec; |
3246 | 123 | const char *sec_name; |
3247 | 123 | Elf_Scn *scn; |
3248 | | |
3249 | 123 | if (seg->sec_cnt == 0) |
3250 | 74 | continue; |
3251 | | |
3252 | 49 | seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs)); |
3253 | 49 | if (!seg->sec_idxs) { |
3254 | 0 | err = -ENOMEM; |
3255 | 0 | goto out; |
3256 | 0 | } |
3257 | | |
3258 | 49 | sec_num = 0; |
3259 | 184 | for_each_btf_ext_sec(seg, sec) { |
3260 | | /* preventively increment index to avoid doing |
3261 | | * this before every continue below |
3262 | | */ |
3263 | 184 | sec_num++; |
3264 | | |
3265 | 184 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
3266 | 184 | if (str_is_empty(sec_name)) |
3267 | 111 | continue; |
3268 | 73 | scn = elf_sec_by_name(obj, sec_name); |
3269 | 73 | if (!scn) |
3270 | 56 | continue; |
3271 | | |
3272 | 17 | seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn); |
3273 | 17 | } |
3274 | 49 | } |
3275 | 41 | } |
3276 | 6.98k | out: |
3277 | 6.98k | if (err && libbpf_needs_btf(obj)) { |
3278 | 48 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
3279 | 48 | return err; |
3280 | 48 | } |
3281 | 6.93k | return 0; |
3282 | 6.98k | } |
3283 | | |
3284 | | static int compare_vsi_off(const void *_a, const void *_b) |
3285 | 2.49k | { |
3286 | 2.49k | const struct btf_var_secinfo *a = _a; |
3287 | 2.49k | const struct btf_var_secinfo *b = _b; |
3288 | | |
3289 | 2.49k | return a->offset - b->offset; |
3290 | 2.49k | } |
3291 | | |
3292 | | static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, |
3293 | | struct btf_type *t) |
3294 | 3.59k | { |
3295 | 3.59k | __u32 size = 0, i, vars = btf_vlen(t); |
3296 | 3.59k | const char *sec_name = btf__name_by_offset(btf, t->name_off); |
3297 | 3.59k | struct btf_var_secinfo *vsi; |
3298 | 3.59k | bool fixup_offsets = false; |
3299 | 3.59k | int err; |
3300 | | |
3301 | 3.59k | if (!sec_name) { |
3302 | 0 | pr_debug("No name found in string section for DATASEC kind.\n"); |
3303 | 0 | return -ENOENT; |
3304 | 0 | } |
3305 | | |
3306 | | /* Extern-backing datasecs (.ksyms, .kconfig) have their size and |
3307 | | * variable offsets set at the previous step. Further, not every |
3308 | | * extern BTF VAR has corresponding ELF symbol preserved, so we skip |
3309 | | * all fixups altogether for such sections and go straight to sorting |
3310 | | * VARs within their DATASEC. |
3311 | | */ |
3312 | 3.59k | if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0) |
3313 | 544 | goto sort_vars; |
3314 | | |
3315 | | /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to |
3316 | | * fix this up. But BPF static linker already fixes this up and fills |
3317 | | * all the sizes and offsets during static linking. So this step has |
3318 | | * to be optional. But the STV_HIDDEN handling is non-optional for any |
3319 | | * non-extern DATASEC, so the variable fixup loop below handles both |
3320 | | * functions at the same time, paying the cost of BTF VAR <-> ELF |
3321 | | * symbol matching just once. |
3322 | | */ |
3323 | 3.05k | if (t->size == 0) { |
3324 | 404 | err = find_elf_sec_sz(obj, sec_name, &size); |
3325 | 404 | if (err || !size) { |
3326 | 165 | pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n", |
3327 | 165 | sec_name, size, err); |
3328 | 165 | return -ENOENT; |
3329 | 165 | } |
3330 | | |
3331 | 239 | t->size = size; |
3332 | 239 | fixup_offsets = true; |
3333 | 239 | } |
3334 | | |
3335 | 4.98k | for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { |
3336 | 2.44k | const struct btf_type *t_var; |
3337 | 2.44k | struct btf_var *var; |
3338 | 2.44k | const char *var_name; |
3339 | 2.44k | Elf64_Sym *sym; |
3340 | | |
3341 | 2.44k | t_var = btf__type_by_id(btf, vsi->type); |
3342 | 2.44k | if (!t_var || !btf_is_var(t_var)) { |
3343 | 121 | pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name); |
3344 | 121 | return -EINVAL; |
3345 | 121 | } |
3346 | | |
3347 | 2.32k | var = btf_var(t_var); |
3348 | 2.32k | if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN) |
3349 | 176 | continue; |
3350 | | |
3351 | 2.14k | var_name = btf__name_by_offset(btf, t_var->name_off); |
3352 | 2.14k | if (!var_name) { |
3353 | 0 | pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n", |
3354 | 0 | sec_name, i); |
3355 | 0 | return -ENOENT; |
3356 | 0 | } |
3357 | | |
3358 | 2.14k | sym = find_elf_var_sym(obj, var_name); |
3359 | 2.14k | if (IS_ERR(sym)) { |
3360 | 228 | pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n", |
3361 | 228 | sec_name, var_name); |
3362 | 228 | return -ENOENT; |
3363 | 228 | } |
3364 | | |
3365 | 1.92k | if (fixup_offsets) |
3366 | 106 | vsi->offset = sym->st_value; |
3367 | | |
3368 | | /* if variable is a global/weak symbol, but has restricted |
3369 | | * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR |
3370 | | * as static. This follows similar logic for functions (BPF |
3371 | | * subprogs) and influences libbpf's further decisions about |
3372 | | * whether to make global data BPF array maps as |
3373 | | * BPF_F_MMAPABLE. |
3374 | | */ |
3375 | 1.92k | if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN |
3376 | 1.92k | || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL) |
3377 | 100 | var->linkage = BTF_VAR_STATIC; |
3378 | 1.92k | } |
3379 | | |
3380 | 3.08k | sort_vars: |
3381 | 3.08k | qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off); |
3382 | 3.08k | return 0; |
3383 | 2.89k | } |
3384 | | |
3385 | | static int bpf_object_fixup_btf(struct bpf_object *obj) |
3386 | 4.20k | { |
3387 | 4.20k | int i, n, err = 0; |
3388 | | |
3389 | 4.20k | if (!obj->btf) |
3390 | 1.68k | return 0; |
3391 | | |
3392 | 2.52k | n = btf__type_cnt(obj->btf); |
3393 | 26.7k | for (i = 1; i < n; i++) { |
3394 | 24.7k | struct btf_type *t = btf_type_by_id(obj->btf, i); |
3395 | | |
3396 | | /* Loader needs to fix up some of the things compiler |
3397 | | * couldn't get its hands on while emitting BTF. This |
3398 | | * is section size and global variable offset. We use |
3399 | | * the info from the ELF itself for this purpose. |
3400 | | */ |
3401 | 24.7k | if (btf_is_datasec(t)) { |
3402 | 3.59k | err = btf_fixup_datasec(obj, obj->btf, t); |
3403 | 3.59k | if (err) |
3404 | 514 | return err; |
3405 | 3.59k | } |
3406 | 24.7k | } |
3407 | | |
3408 | 2.00k | return 0; |
3409 | 2.52k | } |
3410 | | |
3411 | | static bool prog_needs_vmlinux_btf(struct bpf_program *prog) |
3412 | 0 | { |
3413 | 0 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || |
3414 | 0 | prog->type == BPF_PROG_TYPE_LSM) |
3415 | 0 | return true; |
3416 | | |
3417 | | /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs |
3418 | | * also need vmlinux BTF |
3419 | | */ |
3420 | 0 | if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) |
3421 | 0 | return true; |
3422 | | |
3423 | 0 | return false; |
3424 | 0 | } |
3425 | | |
3426 | | static bool map_needs_vmlinux_btf(struct bpf_map *map) |
3427 | 0 | { |
3428 | 0 | return bpf_map__is_struct_ops(map); |
3429 | 0 | } |
3430 | | |
3431 | | static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) |
3432 | 0 | { |
3433 | 0 | struct bpf_program *prog; |
3434 | 0 | struct bpf_map *map; |
3435 | 0 | int i; |
3436 | | |
3437 | | /* CO-RE relocations need kernel BTF, only when btf_custom_path |
3438 | | * is not specified |
3439 | | */ |
3440 | 0 | if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path) |
3441 | 0 | return true; |
3442 | | |
3443 | | /* Support for typed ksyms needs kernel BTF */ |
3444 | 0 | for (i = 0; i < obj->nr_extern; i++) { |
3445 | 0 | const struct extern_desc *ext; |
3446 | |
|
3447 | 0 | ext = &obj->externs[i]; |
3448 | 0 | if (ext->type == EXT_KSYM && ext->ksym.type_id) |
3449 | 0 | return true; |
3450 | 0 | } |
3451 | | |
3452 | 0 | bpf_object__for_each_program(prog, obj) { |
3453 | 0 | if (!prog->autoload) |
3454 | 0 | continue; |
3455 | 0 | if (prog_needs_vmlinux_btf(prog)) |
3456 | 0 | return true; |
3457 | 0 | } |
3458 | | |
3459 | 0 | bpf_object__for_each_map(map, obj) { |
3460 | 0 | if (map_needs_vmlinux_btf(map)) |
3461 | 0 | return true; |
3462 | 0 | } |
3463 | | |
3464 | 0 | return false; |
3465 | 0 | } |
3466 | | |
3467 | | static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) |
3468 | 0 | { |
3469 | 0 | int err; |
3470 | | |
3471 | | /* btf_vmlinux could be loaded earlier */ |
3472 | 0 | if (obj->btf_vmlinux || obj->gen_loader) |
3473 | 0 | return 0; |
3474 | | |
3475 | 0 | if (!force && !obj_needs_vmlinux_btf(obj)) |
3476 | 0 | return 0; |
3477 | | |
3478 | 0 | obj->btf_vmlinux = btf__load_vmlinux_btf(); |
3479 | 0 | err = libbpf_get_error(obj->btf_vmlinux); |
3480 | 0 | if (err) { |
3481 | 0 | pr_warn("Error loading vmlinux BTF: %d\n", err); |
3482 | 0 | obj->btf_vmlinux = NULL; |
3483 | 0 | return err; |
3484 | 0 | } |
3485 | 0 | return 0; |
3486 | 0 | } |
3487 | | |
3488 | | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
3489 | 0 | { |
3490 | 0 | struct btf *kern_btf = obj->btf; |
3491 | 0 | bool btf_mandatory, sanitize; |
3492 | 0 | int i, err = 0; |
3493 | |
|
3494 | 0 | if (!obj->btf) |
3495 | 0 | return 0; |
3496 | | |
3497 | 0 | if (!kernel_supports(obj, FEAT_BTF)) { |
3498 | 0 | if (kernel_needs_btf(obj)) { |
3499 | 0 | err = -EOPNOTSUPP; |
3500 | 0 | goto report; |
3501 | 0 | } |
3502 | 0 | pr_debug("Kernel doesn't support BTF, skipping uploading it.\n"); |
3503 | 0 | return 0; |
3504 | 0 | } |
3505 | | |
3506 | | /* Even though some subprogs are global/weak, user might prefer more |
3507 | | * permissive BPF verification process that BPF verifier performs for |
3508 | | * static functions, taking into account more context from the caller |
3509 | | * functions. In such case, they need to mark such subprogs with |
3510 | | * __attribute__((visibility("hidden"))) and libbpf will adjust |
3511 | | * corresponding FUNC BTF type to be marked as static and trigger more |
3512 | | * involved BPF verification process. |
3513 | | */ |
3514 | 0 | for (i = 0; i < obj->nr_programs; i++) { |
3515 | 0 | struct bpf_program *prog = &obj->programs[i]; |
3516 | 0 | struct btf_type *t; |
3517 | 0 | const char *name; |
3518 | 0 | int j, n; |
3519 | |
|
3520 | 0 | if (!prog->mark_btf_static || !prog_is_subprog(obj, prog)) |
3521 | 0 | continue; |
3522 | | |
3523 | 0 | n = btf__type_cnt(obj->btf); |
3524 | 0 | for (j = 1; j < n; j++) { |
3525 | 0 | t = btf_type_by_id(obj->btf, j); |
3526 | 0 | if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) |
3527 | 0 | continue; |
3528 | | |
3529 | 0 | name = btf__str_by_offset(obj->btf, t->name_off); |
3530 | 0 | if (strcmp(name, prog->name) != 0) |
3531 | 0 | continue; |
3532 | | |
3533 | 0 | t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0); |
3534 | 0 | break; |
3535 | 0 | } |
3536 | 0 | } |
3537 | |
|
3538 | 0 | sanitize = btf_needs_sanitization(obj); |
3539 | 0 | if (sanitize) { |
3540 | 0 | const void *raw_data; |
3541 | 0 | __u32 sz; |
3542 | | |
3543 | | /* clone BTF to sanitize a copy and leave the original intact */ |
3544 | 0 | raw_data = btf__raw_data(obj->btf, &sz); |
3545 | 0 | kern_btf = btf__new(raw_data, sz); |
3546 | 0 | err = libbpf_get_error(kern_btf); |
3547 | 0 | if (err) |
3548 | 0 | return err; |
3549 | | |
3550 | | /* enforce 8-byte pointers for BPF-targeted BTFs */ |
3551 | 0 | btf__set_pointer_size(obj->btf, 8); |
3552 | 0 | err = bpf_object__sanitize_btf(obj, kern_btf); |
3553 | 0 | if (err) |
3554 | 0 | return err; |
3555 | 0 | } |
3556 | | |
3557 | 0 | if (obj->gen_loader) { |
3558 | 0 | __u32 raw_size = 0; |
3559 | 0 | const void *raw_data = btf__raw_data(kern_btf, &raw_size); |
3560 | |
|
3561 | 0 | if (!raw_data) |
3562 | 0 | return -ENOMEM; |
3563 | 0 | bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size); |
3564 | | /* Pretend to have valid FD to pass various fd >= 0 checks. |
3565 | | * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. |
3566 | | */ |
3567 | 0 | btf__set_fd(kern_btf, 0); |
3568 | 0 | } else { |
3569 | | /* currently BPF_BTF_LOAD only supports log_level 1 */ |
3570 | 0 | err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size, |
3571 | 0 | obj->log_level ? 1 : 0, obj->token_fd); |
3572 | 0 | } |
3573 | 0 | if (sanitize) { |
3574 | 0 | if (!err) { |
3575 | | /* move fd to libbpf's BTF */ |
3576 | 0 | btf__set_fd(obj->btf, btf__fd(kern_btf)); |
3577 | 0 | btf__set_fd(kern_btf, -1); |
3578 | 0 | } |
3579 | 0 | btf__free(kern_btf); |
3580 | 0 | } |
3581 | 0 | report: |
3582 | 0 | if (err) { |
3583 | 0 | btf_mandatory = kernel_needs_btf(obj); |
3584 | 0 | pr_warn("Error loading .BTF into kernel: %d. %s\n", err, |
3585 | 0 | btf_mandatory ? "BTF is mandatory, can't proceed." |
3586 | 0 | : "BTF is optional, ignoring."); |
3587 | 0 | if (!btf_mandatory) |
3588 | 0 | err = 0; |
3589 | 0 | } |
3590 | 0 | return err; |
3591 | 0 | } |
3592 | | |
3593 | | static const char *elf_sym_str(const struct bpf_object *obj, size_t off) |
3594 | 28.8k | { |
3595 | 28.8k | const char *name; |
3596 | | |
3597 | 28.8k | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off); |
3598 | 28.8k | if (!name) { |
3599 | 10.1k | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3600 | 10.1k | off, obj->path, elf_errmsg(-1)); |
3601 | 10.1k | return NULL; |
3602 | 10.1k | } |
3603 | | |
3604 | 18.6k | return name; |
3605 | 28.8k | } |
3606 | | |
3607 | | static const char *elf_sec_str(const struct bpf_object *obj, size_t off) |
3608 | 48.4k | { |
3609 | 48.4k | const char *name; |
3610 | | |
3611 | 48.4k | name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off); |
3612 | 48.4k | if (!name) { |
3613 | 796 | pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n", |
3614 | 796 | off, obj->path, elf_errmsg(-1)); |
3615 | 796 | return NULL; |
3616 | 796 | } |
3617 | | |
3618 | 47.6k | return name; |
3619 | 48.4k | } |
3620 | | |
3621 | | static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx) |
3622 | 14.6k | { |
3623 | 14.6k | Elf_Scn *scn; |
3624 | | |
3625 | 14.6k | scn = elf_getscn(obj->efile.elf, idx); |
3626 | 14.6k | if (!scn) { |
3627 | 0 | pr_warn("elf: failed to get section(%zu) from %s: %s\n", |
3628 | 0 | idx, obj->path, elf_errmsg(-1)); |
3629 | 0 | return NULL; |
3630 | 0 | } |
3631 | 14.6k | return scn; |
3632 | 14.6k | } |
3633 | | |
3634 | | static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name) |
3635 | 477 | { |
3636 | 477 | Elf_Scn *scn = NULL; |
3637 | 477 | Elf *elf = obj->efile.elf; |
3638 | 477 | const char *sec_name; |
3639 | | |
3640 | 2.35k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3641 | 2.14k | sec_name = elf_sec_name(obj, scn); |
3642 | 2.14k | if (!sec_name) |
3643 | 0 | return NULL; |
3644 | | |
3645 | 2.14k | if (strcmp(sec_name, name) != 0) |
3646 | 1.88k | continue; |
3647 | | |
3648 | 262 | return scn; |
3649 | 2.14k | } |
3650 | 215 | return NULL; |
3651 | 477 | } |
3652 | | |
3653 | | static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn) |
3654 | 101k | { |
3655 | 101k | Elf64_Shdr *shdr; |
3656 | | |
3657 | 101k | if (!scn) |
3658 | 0 | return NULL; |
3659 | | |
3660 | 101k | shdr = elf64_getshdr(scn); |
3661 | 101k | if (!shdr) { |
3662 | 0 | pr_warn("elf: failed to get section(%zu) header from %s: %s\n", |
3663 | 0 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3664 | 0 | return NULL; |
3665 | 0 | } |
3666 | | |
3667 | 101k | return shdr; |
3668 | 101k | } |
3669 | | |
3670 | | static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn) |
3671 | 6.95k | { |
3672 | 6.95k | const char *name; |
3673 | 6.95k | Elf64_Shdr *sh; |
3674 | | |
3675 | 6.95k | if (!scn) |
3676 | 0 | return NULL; |
3677 | | |
3678 | 6.95k | sh = elf_sec_hdr(obj, scn); |
3679 | 6.95k | if (!sh) |
3680 | 0 | return NULL; |
3681 | | |
3682 | 6.95k | name = elf_sec_str(obj, sh->sh_name); |
3683 | 6.95k | if (!name) { |
3684 | 696 | pr_warn("elf: failed to get section(%zu) name from %s: %s\n", |
3685 | 696 | elf_ndxscn(scn), obj->path, elf_errmsg(-1)); |
3686 | 696 | return NULL; |
3687 | 696 | } |
3688 | | |
3689 | 6.26k | return name; |
3690 | 6.95k | } |
3691 | | |
3692 | | static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn) |
3693 | 43.8k | { |
3694 | 43.8k | Elf_Data *data; |
3695 | | |
3696 | 43.8k | if (!scn) |
3697 | 159 | return NULL; |
3698 | | |
3699 | 43.7k | data = elf_getdata(scn, 0); |
3700 | 43.7k | if (!data) { |
3701 | 448 | pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n", |
3702 | 448 | elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>", |
3703 | 448 | obj->path, elf_errmsg(-1)); |
3704 | 448 | return NULL; |
3705 | 448 | } |
3706 | | |
3707 | 43.2k | return data; |
3708 | 43.7k | } |
3709 | | |
3710 | | static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx) |
3711 | 860k | { |
3712 | 860k | if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym)) |
3713 | 144 | return NULL; |
3714 | | |
3715 | 860k | return (Elf64_Sym *)obj->efile.symbols->d_buf + idx; |
3716 | 860k | } |
3717 | | |
3718 | | static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx) |
3719 | 8.53k | { |
3720 | 8.53k | if (idx >= data->d_size / sizeof(Elf64_Rel)) |
3721 | 0 | return NULL; |
3722 | | |
3723 | 8.53k | return (Elf64_Rel *)data->d_buf + idx; |
3724 | 8.53k | } |
3725 | | |
3726 | | static bool is_sec_name_dwarf(const char *name) |
3727 | 36.7k | { |
3728 | | /* approximation, but the actual list is too long */ |
3729 | 36.7k | return str_has_pfx(name, ".debug_"); |
3730 | 36.7k | } |
3731 | | |
3732 | | static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name) |
3733 | 40.3k | { |
3734 | | /* no special handling of .strtab */ |
3735 | 40.3k | if (hdr->sh_type == SHT_STRTAB) |
3736 | 5.53k | return true; |
3737 | | |
3738 | | /* ignore .llvm_addrsig section as well */ |
3739 | 34.8k | if (hdr->sh_type == SHT_LLVM_ADDRSIG) |
3740 | 66 | return true; |
3741 | | |
3742 | | /* no subprograms will lead to an empty .text section, ignore it */ |
3743 | 34.7k | if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 && |
3744 | 34.7k | strcmp(name, ".text") == 0) |
3745 | 39 | return true; |
3746 | | |
3747 | | /* DWARF sections */ |
3748 | 34.7k | if (is_sec_name_dwarf(name)) |
3749 | 2.17k | return true; |
3750 | | |
3751 | 32.5k | if (str_has_pfx(name, ".rel")) { |
3752 | 2.04k | name += sizeof(".rel") - 1; |
3753 | | /* DWARF section relocations */ |
3754 | 2.04k | if (is_sec_name_dwarf(name)) |
3755 | 194 | return true; |
3756 | | |
3757 | | /* .BTF and .BTF.ext don't need relocations */ |
3758 | 1.85k | if (strcmp(name, BTF_ELF_SEC) == 0 || |
3759 | 1.85k | strcmp(name, BTF_EXT_ELF_SEC) == 0) |
3760 | 416 | return true; |
3761 | 1.85k | } |
3762 | | |
3763 | 31.9k | return false; |
3764 | 32.5k | } |
3765 | | |
3766 | | static int cmp_progs(const void *_a, const void *_b) |
3767 | 44.8k | { |
3768 | 44.8k | const struct bpf_program *a = _a; |
3769 | 44.8k | const struct bpf_program *b = _b; |
3770 | | |
3771 | 44.8k | if (a->sec_idx != b->sec_idx) |
3772 | 531 | return a->sec_idx < b->sec_idx ? -1 : 1; |
3773 | | |
3774 | | /* sec_insn_off can't be the same within the section */ |
3775 | 44.3k | return a->sec_insn_off < b->sec_insn_off ? -1 : 1; |
3776 | 44.8k | } |
3777 | | |
3778 | | static int bpf_object__elf_collect(struct bpf_object *obj) |
3779 | 9.33k | { |
3780 | 9.33k | struct elf_sec_desc *sec_desc; |
3781 | 9.33k | Elf *elf = obj->efile.elf; |
3782 | 9.33k | Elf_Data *btf_ext_data = NULL; |
3783 | 9.33k | Elf_Data *btf_data = NULL; |
3784 | 9.33k | int idx = 0, err = 0; |
3785 | 9.33k | const char *name; |
3786 | 9.33k | Elf_Data *data; |
3787 | 9.33k | Elf_Scn *scn; |
3788 | 9.33k | Elf64_Shdr *sh; |
3789 | | |
3790 | | /* ELF section indices are 0-based, but sec #0 is special "invalid" |
3791 | | * section. Since section count retrieved by elf_getshdrnum() does |
3792 | | * include sec #0, it is already the necessary size of an array to keep |
3793 | | * all the sections. |
3794 | | */ |
3795 | 9.33k | if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) { |
3796 | 0 | pr_warn("elf: failed to get the number of sections for %s: %s\n", |
3797 | 0 | obj->path, elf_errmsg(-1)); |
3798 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3799 | 0 | } |
3800 | 9.33k | obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); |
3801 | 9.33k | if (!obj->efile.secs) |
3802 | 0 | return -ENOMEM; |
3803 | | |
3804 | | /* a bunch of ELF parsing functionality depends on processing symbols, |
3805 | | * so do the first pass and find the symbol table |
3806 | | */ |
3807 | 9.33k | scn = NULL; |
3808 | 54.6k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3809 | 45.5k | sh = elf_sec_hdr(obj, scn); |
3810 | 45.5k | if (!sh) |
3811 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3812 | | |
3813 | 45.5k | if (sh->sh_type == SHT_SYMTAB) { |
3814 | 9.26k | if (obj->efile.symbols) { |
3815 | 1 | pr_warn("elf: multiple symbol tables in %s\n", obj->path); |
3816 | 1 | return -LIBBPF_ERRNO__FORMAT; |
3817 | 1 | } |
3818 | | |
3819 | 9.26k | data = elf_sec_data(obj, scn); |
3820 | 9.26k | if (!data) |
3821 | 249 | return -LIBBPF_ERRNO__FORMAT; |
3822 | | |
3823 | 9.01k | idx = elf_ndxscn(scn); |
3824 | | |
3825 | 9.01k | obj->efile.symbols = data; |
3826 | 9.01k | obj->efile.symbols_shndx = idx; |
3827 | 9.01k | obj->efile.strtabidx = sh->sh_link; |
3828 | 9.01k | } |
3829 | 45.5k | } |
3830 | | |
3831 | 9.08k | if (!obj->efile.symbols) { |
3832 | 72 | pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n", |
3833 | 72 | obj->path); |
3834 | 72 | return -ENOENT; |
3835 | 72 | } |
3836 | | |
3837 | 9.01k | scn = NULL; |
3838 | 48.2k | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
3839 | 40.4k | idx = elf_ndxscn(scn); |
3840 | 40.4k | sec_desc = &obj->efile.secs[idx]; |
3841 | | |
3842 | 40.4k | sh = elf_sec_hdr(obj, scn); |
3843 | 40.4k | if (!sh) |
3844 | 0 | return -LIBBPF_ERRNO__FORMAT; |
3845 | | |
3846 | 40.4k | name = elf_sec_str(obj, sh->sh_name); |
3847 | 40.4k | if (!name) |
3848 | 100 | return -LIBBPF_ERRNO__FORMAT; |
3849 | | |
3850 | 40.3k | if (ignore_elf_section(sh, name)) |
3851 | 8.42k | continue; |
3852 | | |
3853 | 31.9k | data = elf_sec_data(obj, scn); |
3854 | 31.9k | if (!data) |
3855 | 183 | return -LIBBPF_ERRNO__FORMAT; |
3856 | | |
3857 | 31.7k | pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
3858 | 31.7k | idx, name, (unsigned long)data->d_size, |
3859 | 31.7k | (int)sh->sh_link, (unsigned long)sh->sh_flags, |
3860 | 31.7k | (int)sh->sh_type); |
3861 | | |
3862 | 31.7k | if (strcmp(name, "license") == 0) { |
3863 | 663 | err = bpf_object__init_license(obj, data->d_buf, data->d_size); |
3864 | 663 | if (err) |
3865 | 1 | return err; |
3866 | 31.0k | } else if (strcmp(name, "version") == 0) { |
3867 | 49 | err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); |
3868 | 49 | if (err) |
3869 | 13 | return err; |
3870 | 31.0k | } else if (strcmp(name, "maps") == 0) { |
3871 | 7 | pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n"); |
3872 | 7 | return -ENOTSUP; |
3873 | 31.0k | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
3874 | 1.69k | obj->efile.btf_maps_shndx = idx; |
3875 | 29.3k | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
3876 | 5.20k | if (sh->sh_type != SHT_PROGBITS) |
3877 | 55 | return -LIBBPF_ERRNO__FORMAT; |
3878 | 5.15k | btf_data = data; |
3879 | 24.1k | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
3880 | 427 | if (sh->sh_type != SHT_PROGBITS) |
3881 | 52 | return -LIBBPF_ERRNO__FORMAT; |
3882 | 375 | btf_ext_data = data; |
3883 | 23.7k | } else if (sh->sh_type == SHT_SYMTAB) { |
3884 | | /* already processed during the first pass above */ |
3885 | 16.2k | } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) { |
3886 | 3.62k | if (sh->sh_flags & SHF_EXECINSTR) { |
3887 | 1.22k | if (strcmp(name, ".text") == 0) |
3888 | 233 | obj->efile.text_shndx = idx; |
3889 | 1.22k | err = bpf_object__add_programs(obj, data, name, idx); |
3890 | 1.22k | if (err) |
3891 | 280 | return err; |
3892 | 2.40k | } else if (strcmp(name, DATA_SEC) == 0 || |
3893 | 2.40k | str_has_pfx(name, DATA_SEC ".")) { |
3894 | 661 | sec_desc->sec_type = SEC_DATA; |
3895 | 661 | sec_desc->shdr = sh; |
3896 | 661 | sec_desc->data = data; |
3897 | 1.74k | } else if (strcmp(name, RODATA_SEC) == 0 || |
3898 | 1.74k | str_has_pfx(name, RODATA_SEC ".")) { |
3899 | 399 | sec_desc->sec_type = SEC_RODATA; |
3900 | 399 | sec_desc->shdr = sh; |
3901 | 399 | sec_desc->data = data; |
3902 | 1.34k | } else if (strcmp(name, STRUCT_OPS_SEC) == 0 || |
3903 | 1.34k | strcmp(name, STRUCT_OPS_LINK_SEC) == 0 || |
3904 | 1.34k | strcmp(name, "?" STRUCT_OPS_SEC) == 0 || |
3905 | 1.34k | strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) { |
3906 | 537 | sec_desc->sec_type = SEC_ST_OPS; |
3907 | 537 | sec_desc->shdr = sh; |
3908 | 537 | sec_desc->data = data; |
3909 | 537 | obj->efile.has_st_ops = true; |
3910 | 805 | } else if (strcmp(name, ARENA_SEC) == 0) { |
3911 | 75 | obj->efile.arena_data = data; |
3912 | 75 | obj->efile.arena_data_shndx = idx; |
3913 | 730 | } else { |
3914 | 730 | pr_info("elf: skipping unrecognized data section(%d) %s\n", |
3915 | 730 | idx, name); |
3916 | 730 | } |
3917 | 12.5k | } else if (sh->sh_type == SHT_REL) { |
3918 | 2.23k | int targ_sec_idx = sh->sh_info; /* points to other section */ |
3919 | | |
3920 | 2.23k | if (sh->sh_entsize != sizeof(Elf64_Rel) || |
3921 | 2.23k | targ_sec_idx >= obj->efile.sec_cnt) |
3922 | 184 | return -LIBBPF_ERRNO__FORMAT; |
3923 | | |
3924 | | /* Only do relo for section with exec instructions */ |
3925 | 2.04k | if (!section_have_execinstr(obj, targ_sec_idx) && |
3926 | 2.04k | strcmp(name, ".rel" STRUCT_OPS_SEC) && |
3927 | 2.04k | strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) && |
3928 | 2.04k | strcmp(name, ".rel?" STRUCT_OPS_SEC) && |
3929 | 2.04k | strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) && |
3930 | 2.04k | strcmp(name, ".rel" MAPS_ELF_SEC)) { |
3931 | 572 | pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n", |
3932 | 572 | idx, name, targ_sec_idx, |
3933 | 572 | elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>"); |
3934 | 572 | continue; |
3935 | 572 | } |
3936 | | |
3937 | 1.47k | sec_desc->sec_type = SEC_RELO; |
3938 | 1.47k | sec_desc->shdr = sh; |
3939 | 1.47k | sec_desc->data = data; |
3940 | 10.3k | } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 || |
3941 | 2.08k | str_has_pfx(name, BSS_SEC "."))) { |
3942 | 1.26k | sec_desc->sec_type = SEC_BSS; |
3943 | 1.26k | sec_desc->shdr = sh; |
3944 | 1.26k | sec_desc->data = data; |
3945 | 9.09k | } else { |
3946 | 9.09k | pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name, |
3947 | 9.09k | (size_t)sh->sh_size); |
3948 | 9.09k | } |
3949 | 31.7k | } |
3950 | | |
3951 | 8.00k | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
3952 | 1.15k | pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path); |
3953 | 1.15k | return -LIBBPF_ERRNO__FORMAT; |
3954 | 1.15k | } |
3955 | | |
3956 | | /* sort BPF programs by section name and in-section instruction offset |
3957 | | * for faster search |
3958 | | */ |
3959 | 6.61k | if (obj->nr_programs) |
3960 | 632 | qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs); |
3961 | | |
3962 | 6.61k | return bpf_object__init_btf(obj, btf_data, btf_ext_data); |
3963 | 7.77k | } |
3964 | | |
3965 | | static bool sym_is_extern(const Elf64_Sym *sym) |
3966 | 572k | { |
3967 | 572k | int bind = ELF64_ST_BIND(sym->st_info); |
3968 | | /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */ |
3969 | 572k | return sym->st_shndx == SHN_UNDEF && |
3970 | 572k | (bind == STB_GLOBAL || bind == STB_WEAK) && |
3971 | 572k | ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE; |
3972 | 572k | } |
3973 | | |
3974 | | static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx) |
3975 | 1.22k | { |
3976 | 1.22k | int bind = ELF64_ST_BIND(sym->st_info); |
3977 | 1.22k | int type = ELF64_ST_TYPE(sym->st_info); |
3978 | | |
3979 | | /* in .text section */ |
3980 | 1.22k | if (sym->st_shndx != text_shndx) |
3981 | 554 | return false; |
3982 | | |
3983 | | /* local function */ |
3984 | 672 | if (bind == STB_LOCAL && type == STT_SECTION) |
3985 | 496 | return true; |
3986 | | |
3987 | | /* global function */ |
3988 | 176 | return bind == STB_GLOBAL && type == STT_FUNC; |
3989 | 672 | } |
3990 | | |
3991 | | static int find_extern_btf_id(const struct btf *btf, const char *ext_name) |
3992 | 4.71k | { |
3993 | 4.71k | const struct btf_type *t; |
3994 | 4.71k | const char *tname; |
3995 | 4.71k | int i, n; |
3996 | | |
3997 | 4.71k | if (!btf) |
3998 | 24 | return -ESRCH; |
3999 | | |
4000 | 4.69k | n = btf__type_cnt(btf); |
4001 | 27.0k | for (i = 1; i < n; i++) { |
4002 | 26.8k | t = btf__type_by_id(btf, i); |
4003 | | |
4004 | 26.8k | if (!btf_is_var(t) && !btf_is_func(t)) |
4005 | 18.7k | continue; |
4006 | | |
4007 | 8.11k | tname = btf__name_by_offset(btf, t->name_off); |
4008 | 8.11k | if (strcmp(tname, ext_name)) |
4009 | 3.60k | continue; |
4010 | | |
4011 | 4.51k | if (btf_is_var(t) && |
4012 | 4.51k | btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) |
4013 | 53 | return -EINVAL; |
4014 | | |
4015 | 4.46k | if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN) |
4016 | 16 | return -EINVAL; |
4017 | | |
4018 | 4.44k | return i; |
4019 | 4.46k | } |
4020 | | |
4021 | 179 | return -ENOENT; |
4022 | 4.69k | } |
4023 | | |
4024 | 4.44k | static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) { |
4025 | 4.44k | const struct btf_var_secinfo *vs; |
4026 | 4.44k | const struct btf_type *t; |
4027 | 4.44k | int i, j, n; |
4028 | | |
4029 | 4.44k | if (!btf) |
4030 | 0 | return -ESRCH; |
4031 | | |
4032 | 4.44k | n = btf__type_cnt(btf); |
4033 | 29.8k | for (i = 1; i < n; i++) { |
4034 | 29.7k | t = btf__type_by_id(btf, i); |
4035 | | |
4036 | 29.7k | if (!btf_is_datasec(t)) |
4037 | 19.9k | continue; |
4038 | | |
4039 | 9.80k | vs = btf_var_secinfos(t); |
4040 | 21.6k | for (j = 0; j < btf_vlen(t); j++, vs++) { |
4041 | 16.2k | if (vs->type == ext_btf_id) |
4042 | 4.40k | return i; |
4043 | 16.2k | } |
4044 | 9.80k | } |
4045 | | |
4046 | 42 | return -ENOENT; |
4047 | 4.44k | } |
4048 | | |
4049 | | static enum kcfg_type find_kcfg_type(const struct btf *btf, int id, |
4050 | | bool *is_signed) |
4051 | 1.19k | { |
4052 | 1.19k | const struct btf_type *t; |
4053 | 1.19k | const char *name; |
4054 | | |
4055 | 1.19k | t = skip_mods_and_typedefs(btf, id, NULL); |
4056 | 1.19k | name = btf__name_by_offset(btf, t->name_off); |
4057 | | |
4058 | 1.19k | if (is_signed) |
4059 | 1.08k | *is_signed = false; |
4060 | 1.19k | switch (btf_kind(t)) { |
4061 | 769 | case BTF_KIND_INT: { |
4062 | 769 | int enc = btf_int_encoding(t); |
4063 | | |
4064 | 769 | if (enc & BTF_INT_BOOL) |
4065 | 283 | return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN; |
4066 | 486 | if (is_signed) |
4067 | 409 | *is_signed = enc & BTF_INT_SIGNED; |
4068 | 486 | if (t->size == 1) |
4069 | 269 | return KCFG_CHAR; |
4070 | 217 | if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) |
4071 | 41 | return KCFG_UNKNOWN; |
4072 | 176 | return KCFG_INT; |
4073 | 217 | } |
4074 | 122 | case BTF_KIND_ENUM: |
4075 | 122 | if (t->size != 4) |
4076 | 19 | return KCFG_UNKNOWN; |
4077 | 103 | if (strcmp(name, "libbpf_tristate")) |
4078 | 92 | return KCFG_UNKNOWN; |
4079 | 11 | return KCFG_TRISTATE; |
4080 | 143 | case BTF_KIND_ENUM64: |
4081 | 143 | if (strcmp(name, "libbpf_tristate")) |
4082 | 133 | return KCFG_UNKNOWN; |
4083 | 10 | return KCFG_TRISTATE; |
4084 | 110 | case BTF_KIND_ARRAY: |
4085 | 110 | if (btf_array(t)->nelems == 0) |
4086 | 0 | return KCFG_UNKNOWN; |
4087 | 110 | if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR) |
4088 | 33 | return KCFG_UNKNOWN; |
4089 | 77 | return KCFG_CHAR_ARR; |
4090 | 53 | default: |
4091 | 53 | return KCFG_UNKNOWN; |
4092 | 1.19k | } |
4093 | 1.19k | } |
4094 | | |
4095 | | static int cmp_externs(const void *_a, const void *_b) |
4096 | 8.34k | { |
4097 | 8.34k | const struct extern_desc *a = _a; |
4098 | 8.34k | const struct extern_desc *b = _b; |
4099 | | |
4100 | 8.34k | if (a->type != b->type) |
4101 | 0 | return a->type < b->type ? -1 : 1; |
4102 | | |
4103 | 8.34k | if (a->type == EXT_KCFG) { |
4104 | | /* descending order by alignment requirements */ |
4105 | 1.15k | if (a->kcfg.align != b->kcfg.align) |
4106 | 0 | return a->kcfg.align > b->kcfg.align ? -1 : 1; |
4107 | | /* ascending order by size, within same alignment class */ |
4108 | 1.15k | if (a->kcfg.sz != b->kcfg.sz) |
4109 | 0 | return a->kcfg.sz < b->kcfg.sz ? -1 : 1; |
4110 | 1.15k | } |
4111 | | |
4112 | | /* resolve ties by name */ |
4113 | 8.34k | return strcmp(a->name, b->name); |
4114 | 8.34k | } |
4115 | | |
4116 | | static int find_int_btf_id(const struct btf *btf) |
4117 | 831 | { |
4118 | 831 | const struct btf_type *t; |
4119 | 831 | int i, n; |
4120 | | |
4121 | 831 | n = btf__type_cnt(btf); |
4122 | 7.70k | for (i = 1; i < n; i++) { |
4123 | 6.96k | t = btf__type_by_id(btf, i); |
4124 | | |
4125 | 6.96k | if (btf_is_int(t) && btf_int_bits(t) == 32) |
4126 | 89 | return i; |
4127 | 6.96k | } |
4128 | | |
4129 | 742 | return 0; |
4130 | 831 | } |
4131 | | |
4132 | | static int add_dummy_ksym_var(struct btf *btf) |
4133 | 5.55k | { |
4134 | 5.55k | int i, int_btf_id, sec_btf_id, dummy_var_btf_id; |
4135 | 5.55k | const struct btf_var_secinfo *vs; |
4136 | 5.55k | const struct btf_type *sec; |
4137 | | |
4138 | 5.55k | if (!btf) |
4139 | 1.71k | return 0; |
4140 | | |
4141 | 3.84k | sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC, |
4142 | 3.84k | BTF_KIND_DATASEC); |
4143 | 3.84k | if (sec_btf_id < 0) |
4144 | 3.11k | return 0; |
4145 | | |
4146 | 729 | sec = btf__type_by_id(btf, sec_btf_id); |
4147 | 729 | vs = btf_var_secinfos(sec); |
4148 | 1.95k | for (i = 0; i < btf_vlen(sec); i++, vs++) { |
4149 | 1.75k | const struct btf_type *vt; |
4150 | | |
4151 | 1.75k | vt = btf__type_by_id(btf, vs->type); |
4152 | 1.75k | if (btf_is_func(vt)) |
4153 | 526 | break; |
4154 | 1.75k | } |
4155 | | |
4156 | | /* No func in ksyms sec. No need to add dummy var. */ |
4157 | 729 | if (i == btf_vlen(sec)) |
4158 | 203 | return 0; |
4159 | | |
4160 | 526 | int_btf_id = find_int_btf_id(btf); |
4161 | 526 | dummy_var_btf_id = btf__add_var(btf, |
4162 | 526 | "dummy_ksym", |
4163 | 526 | BTF_VAR_GLOBAL_ALLOCATED, |
4164 | 526 | int_btf_id); |
4165 | 526 | if (dummy_var_btf_id < 0) |
4166 | 526 | pr_warn("cannot create a dummy_ksym var\n"); |
4167 | | |
4168 | 526 | return dummy_var_btf_id; |
4169 | 729 | } |
4170 | | |
4171 | | static int bpf_object__collect_externs(struct bpf_object *obj) |
4172 | 6.93k | { |
4173 | 6.93k | struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL; |
4174 | 6.93k | const struct btf_type *t; |
4175 | 6.93k | struct extern_desc *ext; |
4176 | 6.93k | int i, n, off, dummy_var_btf_id; |
4177 | 6.93k | const char *ext_name, *sec_name; |
4178 | 6.93k | size_t ext_essent_len; |
4179 | 6.93k | Elf_Scn *scn; |
4180 | 6.93k | Elf64_Shdr *sh; |
4181 | | |
4182 | 6.93k | if (!obj->efile.symbols) |
4183 | 0 | return 0; |
4184 | | |
4185 | 6.93k | scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx); |
4186 | 6.93k | sh = elf_sec_hdr(obj, scn); |
4187 | 6.93k | if (!sh || sh->sh_entsize != sizeof(Elf64_Sym)) |
4188 | 1.38k | return -LIBBPF_ERRNO__FORMAT; |
4189 | | |
4190 | 5.55k | dummy_var_btf_id = add_dummy_ksym_var(obj->btf); |
4191 | 5.55k | if (dummy_var_btf_id < 0) |
4192 | 0 | return dummy_var_btf_id; |
4193 | | |
4194 | 5.55k | n = sh->sh_size / sh->sh_entsize; |
4195 | 5.55k | pr_debug("looking for externs among %d symbols...\n", n); |
4196 | | |
4197 | 575k | for (i = 0; i < n; i++) { |
4198 | 571k | Elf64_Sym *sym = elf_sym_by_idx(obj, i); |
4199 | | |
4200 | 571k | if (!sym) |
4201 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4202 | 571k | if (!sym_is_extern(sym)) |
4203 | 562k | continue; |
4204 | 8.80k | ext_name = elf_sym_str(obj, sym->st_name); |
4205 | 8.80k | if (!ext_name || !ext_name[0]) |
4206 | 4.08k | continue; |
4207 | | |
4208 | 4.71k | ext = obj->externs; |
4209 | 4.71k | ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); |
4210 | 4.71k | if (!ext) |
4211 | 0 | return -ENOMEM; |
4212 | 4.71k | obj->externs = ext; |
4213 | 4.71k | ext = &ext[obj->nr_extern]; |
4214 | 4.71k | memset(ext, 0, sizeof(*ext)); |
4215 | 4.71k | obj->nr_extern++; |
4216 | | |
4217 | 4.71k | ext->btf_id = find_extern_btf_id(obj->btf, ext_name); |
4218 | 4.71k | if (ext->btf_id <= 0) { |
4219 | 272 | pr_warn("failed to find BTF for extern '%s': %d\n", |
4220 | 272 | ext_name, ext->btf_id); |
4221 | 272 | return ext->btf_id; |
4222 | 272 | } |
4223 | 4.44k | t = btf__type_by_id(obj->btf, ext->btf_id); |
4224 | 4.44k | ext->name = btf__name_by_offset(obj->btf, t->name_off); |
4225 | 4.44k | ext->sym_idx = i; |
4226 | 4.44k | ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK; |
4227 | | |
4228 | 4.44k | ext_essent_len = bpf_core_essential_name_len(ext->name); |
4229 | 4.44k | ext->essent_name = NULL; |
4230 | 4.44k | if (ext_essent_len != strlen(ext->name)) { |
4231 | 323 | ext->essent_name = strndup(ext->name, ext_essent_len); |
4232 | 323 | if (!ext->essent_name) |
4233 | 0 | return -ENOMEM; |
4234 | 323 | } |
4235 | | |
4236 | 4.44k | ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id); |
4237 | 4.44k | if (ext->sec_btf_id <= 0) { |
4238 | 42 | pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n", |
4239 | 42 | ext_name, ext->btf_id, ext->sec_btf_id); |
4240 | 42 | return ext->sec_btf_id; |
4241 | 42 | } |
4242 | 4.40k | sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id); |
4243 | 4.40k | sec_name = btf__name_by_offset(obj->btf, sec->name_off); |
4244 | | |
4245 | 4.40k | if (strcmp(sec_name, KCONFIG_SEC) == 0) { |
4246 | 1.32k | if (btf_is_func(t)) { |
4247 | 1 | pr_warn("extern function %s is unsupported under %s section\n", |
4248 | 1 | ext->name, KCONFIG_SEC); |
4249 | 1 | return -ENOTSUP; |
4250 | 1 | } |
4251 | 1.32k | kcfg_sec = sec; |
4252 | 1.32k | ext->type = EXT_KCFG; |
4253 | 1.32k | ext->kcfg.sz = btf__resolve_size(obj->btf, t->type); |
4254 | 1.32k | if (ext->kcfg.sz <= 0) { |
4255 | 188 | pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n", |
4256 | 188 | ext_name, ext->kcfg.sz); |
4257 | 188 | return ext->kcfg.sz; |
4258 | 188 | } |
4259 | 1.13k | ext->kcfg.align = btf__align_of(obj->btf, t->type); |
4260 | 1.13k | if (ext->kcfg.align <= 0) { |
4261 | 51 | pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n", |
4262 | 51 | ext_name, ext->kcfg.align); |
4263 | 51 | return -EINVAL; |
4264 | 51 | } |
4265 | 1.08k | ext->kcfg.type = find_kcfg_type(obj->btf, t->type, |
4266 | 1.08k | &ext->kcfg.is_signed); |
4267 | 1.08k | if (ext->kcfg.type == KCFG_UNKNOWN) { |
4268 | 366 | pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name); |
4269 | 366 | return -ENOTSUP; |
4270 | 366 | } |
4271 | 3.07k | } else if (strcmp(sec_name, KSYMS_SEC) == 0) { |
4272 | 2.98k | ksym_sec = sec; |
4273 | 2.98k | ext->type = EXT_KSYM; |
4274 | 2.98k | skip_mods_and_typedefs(obj->btf, t->type, |
4275 | 2.98k | &ext->ksym.type_id); |
4276 | 2.98k | } else { |
4277 | 89 | pr_warn("unrecognized extern section '%s'\n", sec_name); |
4278 | 89 | return -ENOTSUP; |
4279 | 89 | } |
4280 | 4.40k | } |
4281 | 4.54k | pr_debug("collected %d externs total\n", obj->nr_extern); |
4282 | | |
4283 | 4.54k | if (!obj->nr_extern) |
4284 | 4.06k | return 0; |
4285 | | |
4286 | | /* sort externs by type, for kcfg ones also by (align, size, name) */ |
4287 | 482 | qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); |
4288 | | |
4289 | | /* for .ksyms section, we need to turn all externs into allocated |
4290 | | * variables in BTF to pass kernel verification; we do this by |
4291 | | * pretending that each extern is a 8-byte variable |
4292 | | */ |
4293 | 482 | if (ksym_sec) { |
4294 | | /* find existing 4-byte integer type in BTF to use for fake |
4295 | | * extern variables in DATASEC |
4296 | | */ |
4297 | 305 | int int_btf_id = find_int_btf_id(obj->btf); |
4298 | | /* For extern function, a dummy_var added earlier |
4299 | | * will be used to replace the vs->type and |
4300 | | * its name string will be used to refill |
4301 | | * the missing param's name. |
4302 | | */ |
4303 | 305 | const struct btf_type *dummy_var; |
4304 | | |
4305 | 305 | dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id); |
4306 | 3.09k | for (i = 0; i < obj->nr_extern; i++) { |
4307 | 2.79k | ext = &obj->externs[i]; |
4308 | 2.79k | if (ext->type != EXT_KSYM) |
4309 | 0 | continue; |
4310 | 2.79k | pr_debug("extern (ksym) #%d: symbol %d, name %s\n", |
4311 | 2.79k | i, ext->sym_idx, ext->name); |
4312 | 2.79k | } |
4313 | | |
4314 | 305 | sec = ksym_sec; |
4315 | 305 | n = btf_vlen(sec); |
4316 | 649 | for (i = 0, off = 0; i < n; i++, off += sizeof(int)) { |
4317 | 601 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4318 | 601 | struct btf_type *vt; |
4319 | | |
4320 | 601 | vt = (void *)btf__type_by_id(obj->btf, vs->type); |
4321 | 601 | ext_name = btf__name_by_offset(obj->btf, vt->name_off); |
4322 | 601 | ext = find_extern_by_name(obj, ext_name); |
4323 | 601 | if (!ext) { |
4324 | 257 | pr_warn("failed to find extern definition for BTF %s '%s'\n", |
4325 | 257 | btf_kind_str(vt), ext_name); |
4326 | 257 | return -ESRCH; |
4327 | 257 | } |
4328 | 344 | if (btf_is_func(vt)) { |
4329 | 78 | const struct btf_type *func_proto; |
4330 | 78 | struct btf_param *param; |
4331 | 78 | int j; |
4332 | | |
4333 | 78 | func_proto = btf__type_by_id(obj->btf, |
4334 | 78 | vt->type); |
4335 | 78 | param = btf_params(func_proto); |
4336 | | /* Reuse the dummy_var string if the |
4337 | | * func proto does not have param name. |
4338 | | */ |
4339 | 339 | for (j = 0; j < btf_vlen(func_proto); j++) |
4340 | 261 | if (param[j].type && !param[j].name_off) |
4341 | 35 | param[j].name_off = |
4342 | 35 | dummy_var->name_off; |
4343 | 78 | vs->type = dummy_var_btf_id; |
4344 | 78 | vt->info &= ~0xffff; |
4345 | 78 | vt->info |= BTF_FUNC_GLOBAL; |
4346 | 266 | } else { |
4347 | 266 | btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4348 | 266 | vt->type = int_btf_id; |
4349 | 266 | } |
4350 | 344 | vs->offset = off; |
4351 | 344 | vs->size = sizeof(int); |
4352 | 344 | } |
4353 | 48 | sec->size = off; |
4354 | 48 | } |
4355 | | |
4356 | 225 | if (kcfg_sec) { |
4357 | 177 | sec = kcfg_sec; |
4358 | | /* for kcfg externs calculate their offsets within a .kconfig map */ |
4359 | 177 | off = 0; |
4360 | 865 | for (i = 0; i < obj->nr_extern; i++) { |
4361 | 688 | ext = &obj->externs[i]; |
4362 | 688 | if (ext->type != EXT_KCFG) |
4363 | 0 | continue; |
4364 | | |
4365 | 688 | ext->kcfg.data_off = roundup(off, ext->kcfg.align); |
4366 | 688 | off = ext->kcfg.data_off + ext->kcfg.sz; |
4367 | 688 | pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n", |
4368 | 688 | i, ext->sym_idx, ext->kcfg.data_off, ext->name); |
4369 | 688 | } |
4370 | 177 | sec->size = off; |
4371 | 177 | n = btf_vlen(sec); |
4372 | 393 | for (i = 0; i < n; i++) { |
4373 | 372 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
4374 | | |
4375 | 372 | t = btf__type_by_id(obj->btf, vs->type); |
4376 | 372 | ext_name = btf__name_by_offset(obj->btf, t->name_off); |
4377 | 372 | ext = find_extern_by_name(obj, ext_name); |
4378 | 372 | if (!ext) { |
4379 | 156 | pr_warn("failed to find extern definition for BTF var '%s'\n", |
4380 | 156 | ext_name); |
4381 | 156 | return -ESRCH; |
4382 | 156 | } |
4383 | 216 | btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
4384 | 216 | vs->offset = ext->kcfg.data_off; |
4385 | 216 | } |
4386 | 177 | } |
4387 | 69 | return 0; |
4388 | 225 | } |
4389 | | |
4390 | | static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog) |
4391 | 8.78k | { |
4392 | 8.78k | return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; |
4393 | 8.78k | } |
4394 | | |
4395 | | struct bpf_program * |
4396 | | bpf_object__find_program_by_name(const struct bpf_object *obj, |
4397 | | const char *name) |
4398 | 0 | { |
4399 | 0 | struct bpf_program *prog; |
4400 | |
|
4401 | 0 | bpf_object__for_each_program(prog, obj) { |
4402 | 0 | if (prog_is_subprog(obj, prog)) |
4403 | 0 | continue; |
4404 | 0 | if (!strcmp(prog->name, name)) |
4405 | 0 | return prog; |
4406 | 0 | } |
4407 | 0 | return errno = ENOENT, NULL; |
4408 | 0 | } |
4409 | | |
4410 | | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
4411 | | int shndx) |
4412 | 469 | { |
4413 | 469 | switch (obj->efile.secs[shndx].sec_type) { |
4414 | 98 | case SEC_BSS: |
4415 | 327 | case SEC_DATA: |
4416 | 468 | case SEC_RODATA: |
4417 | 468 | return true; |
4418 | 1 | default: |
4419 | 1 | return false; |
4420 | 469 | } |
4421 | 469 | } |
4422 | | |
4423 | | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
4424 | | int shndx) |
4425 | 21 | { |
4426 | 21 | return shndx == obj->efile.btf_maps_shndx; |
4427 | 21 | } |
4428 | | |
4429 | | static enum libbpf_map_type |
4430 | | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
4431 | 562 | { |
4432 | 562 | if (shndx == obj->efile.symbols_shndx) |
4433 | 1 | return LIBBPF_MAP_KCONFIG; |
4434 | | |
4435 | 561 | switch (obj->efile.secs[shndx].sec_type) { |
4436 | 98 | case SEC_BSS: |
4437 | 98 | return LIBBPF_MAP_BSS; |
4438 | 229 | case SEC_DATA: |
4439 | 229 | return LIBBPF_MAP_DATA; |
4440 | 141 | case SEC_RODATA: |
4441 | 141 | return LIBBPF_MAP_RODATA; |
4442 | 93 | default: |
4443 | 93 | return LIBBPF_MAP_UNSPEC; |
4444 | 561 | } |
4445 | 561 | } |
4446 | | |
4447 | | static int bpf_program__record_reloc(struct bpf_program *prog, |
4448 | | struct reloc_desc *reloc_desc, |
4449 | | __u32 insn_idx, const char *sym_name, |
4450 | | const Elf64_Sym *sym, const Elf64_Rel *rel) |
4451 | 1.72k | { |
4452 | 1.72k | struct bpf_insn *insn = &prog->insns[insn_idx]; |
4453 | 1.72k | size_t map_idx, nr_maps = prog->obj->nr_maps; |
4454 | 1.72k | struct bpf_object *obj = prog->obj; |
4455 | 1.72k | __u32 shdr_idx = sym->st_shndx; |
4456 | 1.72k | enum libbpf_map_type type; |
4457 | 1.72k | const char *sym_sec_name; |
4458 | 1.72k | struct bpf_map *map; |
4459 | | |
4460 | 1.72k | if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) { |
4461 | 49 | pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n", |
4462 | 49 | prog->name, sym_name, insn_idx, insn->code); |
4463 | 49 | return -LIBBPF_ERRNO__RELOC; |
4464 | 49 | } |
4465 | | |
4466 | 1.67k | if (sym_is_extern(sym)) { |
4467 | 1 | int sym_idx = ELF64_R_SYM(rel->r_info); |
4468 | 1 | int i, n = obj->nr_extern; |
4469 | 1 | struct extern_desc *ext; |
4470 | | |
4471 | 1 | for (i = 0; i < n; i++) { |
4472 | 0 | ext = &obj->externs[i]; |
4473 | 0 | if (ext->sym_idx == sym_idx) |
4474 | 0 | break; |
4475 | 0 | } |
4476 | 1 | if (i >= n) { |
4477 | 1 | pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n", |
4478 | 1 | prog->name, sym_name, sym_idx); |
4479 | 1 | return -LIBBPF_ERRNO__RELOC; |
4480 | 1 | } |
4481 | 0 | pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n", |
4482 | 0 | prog->name, i, ext->name, ext->sym_idx, insn_idx); |
4483 | 0 | if (insn->code == (BPF_JMP | BPF_CALL)) |
4484 | 0 | reloc_desc->type = RELO_EXTERN_CALL; |
4485 | 0 | else |
4486 | 0 | reloc_desc->type = RELO_EXTERN_LD64; |
4487 | 0 | reloc_desc->insn_idx = insn_idx; |
4488 | 0 | reloc_desc->ext_idx = i; |
4489 | 0 | return 0; |
4490 | 1 | } |
4491 | | |
4492 | | /* sub-program call relocation */ |
4493 | 1.67k | if (is_call_insn(insn)) { |
4494 | 448 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
4495 | 6 | pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name); |
4496 | 6 | return -LIBBPF_ERRNO__RELOC; |
4497 | 6 | } |
4498 | | /* text_shndx can be 0, if no default "main" program exists */ |
4499 | 442 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
4500 | 11 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4501 | 11 | pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n", |
4502 | 11 | prog->name, sym_name, sym_sec_name); |
4503 | 11 | return -LIBBPF_ERRNO__RELOC; |
4504 | 11 | } |
4505 | 431 | if (sym->st_value % BPF_INSN_SZ) { |
4506 | 1 | pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n", |
4507 | 1 | prog->name, sym_name, (size_t)sym->st_value); |
4508 | 1 | return -LIBBPF_ERRNO__RELOC; |
4509 | 1 | } |
4510 | 430 | reloc_desc->type = RELO_CALL; |
4511 | 430 | reloc_desc->insn_idx = insn_idx; |
4512 | 430 | reloc_desc->sym_off = sym->st_value; |
4513 | 430 | return 0; |
4514 | 431 | } |
4515 | | |
4516 | 1.22k | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
4517 | 2 | pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n", |
4518 | 2 | prog->name, sym_name, shdr_idx); |
4519 | 2 | return -LIBBPF_ERRNO__RELOC; |
4520 | 2 | } |
4521 | | |
4522 | | /* loading subprog addresses */ |
4523 | 1.22k | if (sym_is_subprog(sym, obj->efile.text_shndx)) { |
4524 | | /* global_func: sym->st_value = offset in the section, insn->imm = 0. |
4525 | | * local_func: sym->st_value = 0, insn->imm = offset in the section. |
4526 | | */ |
4527 | 664 | if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) { |
4528 | 5 | pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n", |
4529 | 5 | prog->name, sym_name, (size_t)sym->st_value, insn->imm); |
4530 | 5 | return -LIBBPF_ERRNO__RELOC; |
4531 | 5 | } |
4532 | | |
4533 | 659 | reloc_desc->type = RELO_SUBPROG_ADDR; |
4534 | 659 | reloc_desc->insn_idx = insn_idx; |
4535 | 659 | reloc_desc->sym_off = sym->st_value; |
4536 | 659 | return 0; |
4537 | 664 | } |
4538 | | |
4539 | 562 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
4540 | 562 | sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx)); |
4541 | | |
4542 | | /* arena data relocation */ |
4543 | 562 | if (shdr_idx == obj->efile.arena_data_shndx) { |
4544 | 72 | reloc_desc->type = RELO_DATA; |
4545 | 72 | reloc_desc->insn_idx = insn_idx; |
4546 | 72 | reloc_desc->map_idx = obj->arena_map - obj->maps; |
4547 | 72 | reloc_desc->sym_off = sym->st_value; |
4548 | 72 | return 0; |
4549 | 72 | } |
4550 | | |
4551 | | /* generic map reference relocation */ |
4552 | 490 | if (type == LIBBPF_MAP_UNSPEC) { |
4553 | 21 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
4554 | 21 | pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n", |
4555 | 21 | prog->name, sym_name, sym_sec_name); |
4556 | 21 | return -LIBBPF_ERRNO__RELOC; |
4557 | 21 | } |
4558 | 0 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4559 | 0 | map = &obj->maps[map_idx]; |
4560 | 0 | if (map->libbpf_type != type || |
4561 | 0 | map->sec_idx != sym->st_shndx || |
4562 | 0 | map->sec_offset != sym->st_value) |
4563 | 0 | continue; |
4564 | 0 | pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n", |
4565 | 0 | prog->name, map_idx, map->name, map->sec_idx, |
4566 | 0 | map->sec_offset, insn_idx); |
4567 | 0 | break; |
4568 | 0 | } |
4569 | 0 | if (map_idx >= nr_maps) { |
4570 | 0 | pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n", |
4571 | 0 | prog->name, sym_sec_name, (size_t)sym->st_value); |
4572 | 0 | return -LIBBPF_ERRNO__RELOC; |
4573 | 0 | } |
4574 | 0 | reloc_desc->type = RELO_LD64; |
4575 | 0 | reloc_desc->insn_idx = insn_idx; |
4576 | 0 | reloc_desc->map_idx = map_idx; |
4577 | 0 | reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ |
4578 | 0 | return 0; |
4579 | 0 | } |
4580 | | |
4581 | | /* global data map relocation */ |
4582 | 469 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
4583 | 1 | pr_warn("prog '%s': bad data relo against section '%s'\n", |
4584 | 1 | prog->name, sym_sec_name); |
4585 | 1 | return -LIBBPF_ERRNO__RELOC; |
4586 | 1 | } |
4587 | 704 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
4588 | 702 | map = &obj->maps[map_idx]; |
4589 | 702 | if (map->libbpf_type != type || map->sec_idx != sym->st_shndx) |
4590 | 236 | continue; |
4591 | 466 | pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n", |
4592 | 466 | prog->name, map_idx, map->name, map->sec_idx, |
4593 | 466 | map->sec_offset, insn_idx); |
4594 | 466 | break; |
4595 | 702 | } |
4596 | 468 | if (map_idx >= nr_maps) { |
4597 | 2 | pr_warn("prog '%s': data relo failed to find map for section '%s'\n", |
4598 | 2 | prog->name, sym_sec_name); |
4599 | 2 | return -LIBBPF_ERRNO__RELOC; |
4600 | 2 | } |
4601 | | |
4602 | 466 | reloc_desc->type = RELO_DATA; |
4603 | 466 | reloc_desc->insn_idx = insn_idx; |
4604 | 466 | reloc_desc->map_idx = map_idx; |
4605 | 466 | reloc_desc->sym_off = sym->st_value; |
4606 | 466 | return 0; |
4607 | 468 | } |
4608 | | |
4609 | | static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx) |
4610 | 2.22k | { |
4611 | 2.22k | return insn_idx >= prog->sec_insn_off && |
4612 | 2.22k | insn_idx < prog->sec_insn_off + prog->sec_insn_cnt; |
4613 | 2.22k | } |
4614 | | |
4615 | | static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj, |
4616 | | size_t sec_idx, size_t insn_idx) |
4617 | 8.16k | { |
4618 | 8.16k | int l = 0, r = obj->nr_programs - 1, m; |
4619 | 8.16k | struct bpf_program *prog; |
4620 | | |
4621 | 8.16k | if (!obj->nr_programs) |
4622 | 5.43k | return NULL; |
4623 | | |
4624 | 3.96k | while (l < r) { |
4625 | 1.24k | m = l + (r - l + 1) / 2; |
4626 | 1.24k | prog = &obj->programs[m]; |
4627 | | |
4628 | 1.24k | if (prog->sec_idx < sec_idx || |
4629 | 1.24k | (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx)) |
4630 | 532 | l = m; |
4631 | 710 | else |
4632 | 710 | r = m - 1; |
4633 | 1.24k | } |
4634 | | /* matching program could be at index l, but it still might be the |
4635 | | * wrong one, so we need to double check conditions for the last time |
4636 | | */ |
4637 | 2.72k | prog = &obj->programs[l]; |
4638 | 2.72k | if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx)) |
4639 | 1.72k | return prog; |
4640 | 1.00k | return NULL; |
4641 | 2.72k | } |
4642 | | |
4643 | | static int |
4644 | | bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data) |
4645 | 1.01k | { |
4646 | 1.01k | const char *relo_sec_name, *sec_name; |
4647 | 1.01k | size_t sec_idx = shdr->sh_info, sym_idx; |
4648 | 1.01k | struct bpf_program *prog; |
4649 | 1.01k | struct reloc_desc *relos; |
4650 | 1.01k | int err, i, nrels; |
4651 | 1.01k | const char *sym_name; |
4652 | 1.01k | __u32 insn_idx; |
4653 | 1.01k | Elf_Scn *scn; |
4654 | 1.01k | Elf_Data *scn_data; |
4655 | 1.01k | Elf64_Sym *sym; |
4656 | 1.01k | Elf64_Rel *rel; |
4657 | | |
4658 | 1.01k | if (sec_idx >= obj->efile.sec_cnt) |
4659 | 0 | return -EINVAL; |
4660 | | |
4661 | 1.01k | scn = elf_sec_by_idx(obj, sec_idx); |
4662 | 1.01k | scn_data = elf_sec_data(obj, scn); |
4663 | 1.01k | if (!scn_data) |
4664 | 14 | return -LIBBPF_ERRNO__FORMAT; |
4665 | | |
4666 | 997 | relo_sec_name = elf_sec_str(obj, shdr->sh_name); |
4667 | 997 | sec_name = elf_sec_name(obj, scn); |
4668 | 997 | if (!relo_sec_name || !sec_name) |
4669 | 18 | return -EINVAL; |
4670 | | |
4671 | 979 | pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n", |
4672 | 979 | relo_sec_name, sec_idx, sec_name); |
4673 | 979 | nrels = shdr->sh_size / shdr->sh_entsize; |
4674 | | |
4675 | 8.53k | for (i = 0; i < nrels; i++) { |
4676 | 8.53k | rel = elf_rel_by_idx(data, i); |
4677 | 8.53k | if (!rel) { |
4678 | 0 | pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i); |
4679 | 0 | return -LIBBPF_ERRNO__FORMAT; |
4680 | 0 | } |
4681 | | |
4682 | 8.53k | sym_idx = ELF64_R_SYM(rel->r_info); |
4683 | 8.53k | sym = elf_sym_by_idx(obj, sym_idx); |
4684 | 8.53k | if (!sym) { |
4685 | 144 | pr_warn("sec '%s': symbol #%zu not found for relo #%d\n", |
4686 | 144 | relo_sec_name, sym_idx, i); |
4687 | 144 | return -LIBBPF_ERRNO__FORMAT; |
4688 | 144 | } |
4689 | | |
4690 | 8.39k | if (sym->st_shndx >= obj->efile.sec_cnt) { |
4691 | 28 | pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n", |
4692 | 28 | relo_sec_name, sym_idx, (size_t)sym->st_shndx, i); |
4693 | 28 | return -LIBBPF_ERRNO__FORMAT; |
4694 | 28 | } |
4695 | | |
4696 | 8.36k | if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) { |
4697 | 204 | pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n", |
4698 | 204 | relo_sec_name, (size_t)rel->r_offset, i); |
4699 | 204 | return -LIBBPF_ERRNO__FORMAT; |
4700 | 204 | } |
4701 | | |
4702 | 8.16k | insn_idx = rel->r_offset / BPF_INSN_SZ; |
4703 | | /* relocations against static functions are recorded as |
4704 | | * relocations against the section that contains a function; |
4705 | | * in such case, symbol will be STT_SECTION and sym.st_name |
4706 | | * will point to empty string (0), so fetch section name |
4707 | | * instead |
4708 | | */ |
4709 | 8.16k | if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0) |
4710 | 452 | sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx)); |
4711 | 7.71k | else |
4712 | 7.71k | sym_name = elf_sym_str(obj, sym->st_name); |
4713 | 8.16k | sym_name = sym_name ?: "<?"; |
4714 | | |
4715 | 8.16k | pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n", |
4716 | 6.82k | relo_sec_name, i, insn_idx, sym_name); |
4717 | | |
4718 | 6.82k | prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx); |
4719 | 6.82k | if (!prog) { |
4720 | 6.43k | pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n", |
4721 | 6.43k | relo_sec_name, i, sec_name, insn_idx); |
4722 | 6.43k | continue; |
4723 | 6.43k | } |
4724 | | |
4725 | 385 | relos = libbpf_reallocarray(prog->reloc_desc, |
4726 | 385 | prog->nr_reloc + 1, sizeof(*relos)); |
4727 | 385 | if (!relos) |
4728 | 0 | return -ENOMEM; |
4729 | 385 | prog->reloc_desc = relos; |
4730 | | |
4731 | | /* adjust insn_idx to local BPF program frame of reference */ |
4732 | 385 | insn_idx -= prog->sec_insn_off; |
4733 | 385 | err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc], |
4734 | 385 | insn_idx, sym_name, sym, rel); |
4735 | 385 | if (err) |
4736 | 99 | return err; |
4737 | | |
4738 | 286 | prog->nr_reloc++; |
4739 | 286 | } |
4740 | 18.4E | return 0; |
4741 | 979 | } |
4742 | | |
4743 | | static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map) |
4744 | 1.76k | { |
4745 | 1.76k | int id; |
4746 | | |
4747 | 1.76k | if (!obj->btf) |
4748 | 1.36k | return -ENOENT; |
4749 | | |
4750 | | /* if it's BTF-defined map, we don't need to search for type IDs. |
4751 | | * For struct_ops map, it does not need btf_key_type_id and |
4752 | | * btf_value_type_id. |
4753 | | */ |
4754 | 402 | if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map)) |
4755 | 76 | return 0; |
4756 | | |
4757 | | /* |
4758 | | * LLVM annotates global data differently in BTF, that is, |
4759 | | * only as '.data', '.bss' or '.rodata'. |
4760 | | */ |
4761 | 326 | if (!bpf_map__is_internal(map)) |
4762 | 0 | return -ENOENT; |
4763 | | |
4764 | 326 | id = btf__find_by_name(obj->btf, map->real_name); |
4765 | 326 | if (id < 0) |
4766 | 157 | return id; |
4767 | | |
4768 | 169 | map->btf_key_type_id = 0; |
4769 | 169 | map->btf_value_type_id = id; |
4770 | 169 | return 0; |
4771 | 326 | } |
4772 | | |
4773 | | static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info) |
4774 | 0 | { |
4775 | 0 | char file[PATH_MAX], buff[4096]; |
4776 | 0 | FILE *fp; |
4777 | 0 | __u32 val; |
4778 | 0 | int err; |
4779 | |
|
4780 | 0 | snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd); |
4781 | 0 | memset(info, 0, sizeof(*info)); |
4782 | |
|
4783 | 0 | fp = fopen(file, "re"); |
4784 | 0 | if (!fp) { |
4785 | 0 | err = -errno; |
4786 | 0 | pr_warn("failed to open %s: %d. No procfs support?\n", file, |
4787 | 0 | err); |
4788 | 0 | return err; |
4789 | 0 | } |
4790 | | |
4791 | 0 | while (fgets(buff, sizeof(buff), fp)) { |
4792 | 0 | if (sscanf(buff, "map_type:\t%u", &val) == 1) |
4793 | 0 | info->type = val; |
4794 | 0 | else if (sscanf(buff, "key_size:\t%u", &val) == 1) |
4795 | 0 | info->key_size = val; |
4796 | 0 | else if (sscanf(buff, "value_size:\t%u", &val) == 1) |
4797 | 0 | info->value_size = val; |
4798 | 0 | else if (sscanf(buff, "max_entries:\t%u", &val) == 1) |
4799 | 0 | info->max_entries = val; |
4800 | 0 | else if (sscanf(buff, "map_flags:\t%i", &val) == 1) |
4801 | 0 | info->map_flags = val; |
4802 | 0 | } |
4803 | |
|
4804 | 0 | fclose(fp); |
4805 | |
|
4806 | 0 | return 0; |
4807 | 0 | } |
4808 | | |
4809 | | bool bpf_map__autocreate(const struct bpf_map *map) |
4810 | 0 | { |
4811 | 0 | return map->autocreate; |
4812 | 0 | } |
4813 | | |
4814 | | int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) |
4815 | 0 | { |
4816 | 0 | if (map->obj->loaded) |
4817 | 0 | return libbpf_err(-EBUSY); |
4818 | | |
4819 | 0 | map->autocreate = autocreate; |
4820 | 0 | return 0; |
4821 | 0 | } |
4822 | | |
4823 | | int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach) |
4824 | 0 | { |
4825 | 0 | if (!bpf_map__is_struct_ops(map)) |
4826 | 0 | return libbpf_err(-EINVAL); |
4827 | | |
4828 | 0 | map->autoattach = autoattach; |
4829 | 0 | return 0; |
4830 | 0 | } |
4831 | | |
4832 | | bool bpf_map__autoattach(const struct bpf_map *map) |
4833 | 0 | { |
4834 | 0 | return map->autoattach; |
4835 | 0 | } |
4836 | | |
4837 | | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
4838 | 0 | { |
4839 | 0 | struct bpf_map_info info; |
4840 | 0 | __u32 len = sizeof(info), name_len; |
4841 | 0 | int new_fd, err; |
4842 | 0 | char *new_name; |
4843 | |
|
4844 | 0 | memset(&info, 0, len); |
4845 | 0 | err = bpf_map_get_info_by_fd(fd, &info, &len); |
4846 | 0 | if (err && errno == EINVAL) |
4847 | 0 | err = bpf_get_map_info_from_fdinfo(fd, &info); |
4848 | 0 | if (err) |
4849 | 0 | return libbpf_err(err); |
4850 | | |
4851 | 0 | name_len = strlen(info.name); |
4852 | 0 | if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0) |
4853 | 0 | new_name = strdup(map->name); |
4854 | 0 | else |
4855 | 0 | new_name = strdup(info.name); |
4856 | |
|
4857 | 0 | if (!new_name) |
4858 | 0 | return libbpf_err(-errno); |
4859 | | |
4860 | | /* |
4861 | | * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. |
4862 | | * This is similar to what we do in ensure_good_fd(), but without |
4863 | | * closing original FD. |
4864 | | */ |
4865 | 0 | new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
4866 | 0 | if (new_fd < 0) { |
4867 | 0 | err = -errno; |
4868 | 0 | goto err_free_new_name; |
4869 | 0 | } |
4870 | | |
4871 | 0 | err = reuse_fd(map->fd, new_fd); |
4872 | 0 | if (err) |
4873 | 0 | goto err_free_new_name; |
4874 | | |
4875 | 0 | free(map->name); |
4876 | |
|
4877 | 0 | map->name = new_name; |
4878 | 0 | map->def.type = info.type; |
4879 | 0 | map->def.key_size = info.key_size; |
4880 | 0 | map->def.value_size = info.value_size; |
4881 | 0 | map->def.max_entries = info.max_entries; |
4882 | 0 | map->def.map_flags = info.map_flags; |
4883 | 0 | map->btf_key_type_id = info.btf_key_type_id; |
4884 | 0 | map->btf_value_type_id = info.btf_value_type_id; |
4885 | 0 | map->reused = true; |
4886 | 0 | map->map_extra = info.map_extra; |
4887 | |
|
4888 | 0 | return 0; |
4889 | | |
4890 | 0 | err_free_new_name: |
4891 | 0 | free(new_name); |
4892 | 0 | return libbpf_err(err); |
4893 | 0 | } |
4894 | | |
4895 | | __u32 bpf_map__max_entries(const struct bpf_map *map) |
4896 | 0 | { |
4897 | 0 | return map->def.max_entries; |
4898 | 0 | } |
4899 | | |
4900 | | struct bpf_map *bpf_map__inner_map(struct bpf_map *map) |
4901 | 0 | { |
4902 | 0 | if (!bpf_map_type__is_map_in_map(map->def.type)) |
4903 | 0 | return errno = EINVAL, NULL; |
4904 | | |
4905 | 0 | return map->inner_map; |
4906 | 0 | } |
4907 | | |
4908 | | int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries) |
4909 | 0 | { |
4910 | 0 | if (map->obj->loaded) |
4911 | 0 | return libbpf_err(-EBUSY); |
4912 | | |
4913 | 0 | map->def.max_entries = max_entries; |
4914 | | |
4915 | | /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */ |
4916 | 0 | if (map_is_ringbuf(map)) |
4917 | 0 | map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries); |
4918 | |
|
4919 | 0 | return 0; |
4920 | 0 | } |
4921 | | |
4922 | | static int bpf_object_prepare_token(struct bpf_object *obj) |
4923 | 0 | { |
4924 | 0 | const char *bpffs_path; |
4925 | 0 | int bpffs_fd = -1, token_fd, err; |
4926 | 0 | bool mandatory; |
4927 | 0 | enum libbpf_print_level level; |
4928 | | |
4929 | | /* token is explicitly prevented */ |
4930 | 0 | if (obj->token_path && obj->token_path[0] == '\0') { |
4931 | 0 | pr_debug("object '%s': token is prevented, skipping...\n", obj->name); |
4932 | 0 | return 0; |
4933 | 0 | } |
4934 | | |
4935 | 0 | mandatory = obj->token_path != NULL; |
4936 | 0 | level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG; |
4937 | |
|
4938 | 0 | bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH; |
4939 | 0 | bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR); |
4940 | 0 | if (bpffs_fd < 0) { |
4941 | 0 | err = -errno; |
4942 | 0 | __pr(level, "object '%s': failed (%d) to open BPF FS mount at '%s'%s\n", |
4943 | 0 | obj->name, err, bpffs_path, |
4944 | 0 | mandatory ? "" : ", skipping optional step..."); |
4945 | 0 | return mandatory ? err : 0; |
4946 | 0 | } |
4947 | | |
4948 | 0 | token_fd = bpf_token_create(bpffs_fd, 0); |
4949 | 0 | close(bpffs_fd); |
4950 | 0 | if (token_fd < 0) { |
4951 | 0 | if (!mandatory && token_fd == -ENOENT) { |
4952 | 0 | pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n", |
4953 | 0 | obj->name, bpffs_path); |
4954 | 0 | return 0; |
4955 | 0 | } |
4956 | 0 | __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n", |
4957 | 0 | obj->name, token_fd, bpffs_path, |
4958 | 0 | mandatory ? "" : ", skipping optional step..."); |
4959 | 0 | return mandatory ? token_fd : 0; |
4960 | 0 | } |
4961 | | |
4962 | 0 | obj->feat_cache = calloc(1, sizeof(*obj->feat_cache)); |
4963 | 0 | if (!obj->feat_cache) { |
4964 | 0 | close(token_fd); |
4965 | 0 | return -ENOMEM; |
4966 | 0 | } |
4967 | | |
4968 | 0 | obj->token_fd = token_fd; |
4969 | 0 | obj->feat_cache->token_fd = token_fd; |
4970 | |
|
4971 | 0 | return 0; |
4972 | 0 | } |
4973 | | |
4974 | | static int |
4975 | | bpf_object__probe_loading(struct bpf_object *obj) |
4976 | 0 | { |
4977 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
4978 | 0 | struct bpf_insn insns[] = { |
4979 | 0 | BPF_MOV64_IMM(BPF_REG_0, 0), |
4980 | 0 | BPF_EXIT_INSN(), |
4981 | 0 | }; |
4982 | 0 | int ret, insn_cnt = ARRAY_SIZE(insns); |
4983 | 0 | LIBBPF_OPTS(bpf_prog_load_opts, opts, |
4984 | 0 | .token_fd = obj->token_fd, |
4985 | 0 | .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0, |
4986 | 0 | ); |
4987 | |
|
4988 | 0 | if (obj->gen_loader) |
4989 | 0 | return 0; |
4990 | | |
4991 | 0 | ret = bump_rlimit_memlock(); |
4992 | 0 | if (ret) |
4993 | 0 | pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret); |
4994 | | |
4995 | | /* make sure basic loading works */ |
4996 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &opts); |
4997 | 0 | if (ret < 0) |
4998 | 0 | ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts); |
4999 | 0 | if (ret < 0) { |
5000 | 0 | ret = errno; |
5001 | 0 | cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); |
5002 | 0 | pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF " |
5003 | 0 | "program. Make sure your kernel supports BPF " |
5004 | 0 | "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is " |
5005 | 0 | "set to big enough value.\n", __func__, cp, ret); |
5006 | 0 | return -ret; |
5007 | 0 | } |
5008 | 0 | close(ret); |
5009 | |
|
5010 | 0 | return 0; |
5011 | 0 | } |
5012 | | |
5013 | | bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) |
5014 | 0 | { |
5015 | 0 | if (obj->gen_loader) |
5016 | | /* To generate loader program assume the latest kernel |
5017 | | * to avoid doing extra prog_load, map_create syscalls. |
5018 | | */ |
5019 | 0 | return true; |
5020 | | |
5021 | 0 | if (obj->token_fd) |
5022 | 0 | return feat_supported(obj->feat_cache, feat_id); |
5023 | | |
5024 | 0 | return feat_supported(NULL, feat_id); |
5025 | 0 | } |
5026 | | |
5027 | | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
5028 | 0 | { |
5029 | 0 | struct bpf_map_info map_info; |
5030 | 0 | char msg[STRERR_BUFSIZE]; |
5031 | 0 | __u32 map_info_len = sizeof(map_info); |
5032 | 0 | int err; |
5033 | |
|
5034 | 0 | memset(&map_info, 0, map_info_len); |
5035 | 0 | err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len); |
5036 | 0 | if (err && errno == EINVAL) |
5037 | 0 | err = bpf_get_map_info_from_fdinfo(map_fd, &map_info); |
5038 | 0 | if (err) { |
5039 | 0 | pr_warn("failed to get map info for map FD %d: %s\n", map_fd, |
5040 | 0 | libbpf_strerror_r(errno, msg, sizeof(msg))); |
5041 | 0 | return false; |
5042 | 0 | } |
5043 | | |
5044 | 0 | return (map_info.type == map->def.type && |
5045 | 0 | map_info.key_size == map->def.key_size && |
5046 | 0 | map_info.value_size == map->def.value_size && |
5047 | 0 | map_info.max_entries == map->def.max_entries && |
5048 | 0 | map_info.map_flags == map->def.map_flags && |
5049 | 0 | map_info.map_extra == map->map_extra); |
5050 | 0 | } |
5051 | | |
5052 | | static int |
5053 | | bpf_object__reuse_map(struct bpf_map *map) |
5054 | 0 | { |
5055 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5056 | 0 | int err, pin_fd; |
5057 | |
|
5058 | 0 | pin_fd = bpf_obj_get(map->pin_path); |
5059 | 0 | if (pin_fd < 0) { |
5060 | 0 | err = -errno; |
5061 | 0 | if (err == -ENOENT) { |
5062 | 0 | pr_debug("found no pinned map to reuse at '%s'\n", |
5063 | 0 | map->pin_path); |
5064 | 0 | return 0; |
5065 | 0 | } |
5066 | | |
5067 | 0 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
5068 | 0 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
5069 | 0 | map->pin_path, cp); |
5070 | 0 | return err; |
5071 | 0 | } |
5072 | | |
5073 | 0 | if (!map_is_reuse_compat(map, pin_fd)) { |
5074 | 0 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
5075 | 0 | map->pin_path); |
5076 | 0 | close(pin_fd); |
5077 | 0 | return -EINVAL; |
5078 | 0 | } |
5079 | | |
5080 | 0 | err = bpf_map__reuse_fd(map, pin_fd); |
5081 | 0 | close(pin_fd); |
5082 | 0 | if (err) |
5083 | 0 | return err; |
5084 | | |
5085 | 0 | map->pinned = true; |
5086 | 0 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
5087 | |
|
5088 | 0 | return 0; |
5089 | 0 | } |
5090 | | |
5091 | | static int |
5092 | | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
5093 | 0 | { |
5094 | 0 | enum libbpf_map_type map_type = map->libbpf_type; |
5095 | 0 | char *cp, errmsg[STRERR_BUFSIZE]; |
5096 | 0 | int err, zero = 0; |
5097 | |
|
5098 | 0 | if (obj->gen_loader) { |
5099 | 0 | bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps, |
5100 | 0 | map->mmaped, map->def.value_size); |
5101 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) |
5102 | 0 | bpf_gen__map_freeze(obj->gen_loader, map - obj->maps); |
5103 | 0 | return 0; |
5104 | 0 | } |
5105 | | |
5106 | 0 | err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); |
5107 | 0 | if (err) { |
5108 | 0 | err = -errno; |
5109 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5110 | 0 | pr_warn("Error setting initial map(%s) contents: %s\n", |
5111 | 0 | map->name, cp); |
5112 | 0 | return err; |
5113 | 0 | } |
5114 | | |
5115 | | /* Freeze .rodata and .kconfig map as read-only from syscall side. */ |
5116 | 0 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) { |
5117 | 0 | err = bpf_map_freeze(map->fd); |
5118 | 0 | if (err) { |
5119 | 0 | err = -errno; |
5120 | 0 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
5121 | 0 | pr_warn("Error freezing map(%s) as read-only: %s\n", |
5122 | 0 | map->name, cp); |
5123 | 0 | return err; |
5124 | 0 | } |
5125 | 0 | } |
5126 | 0 | return 0; |
5127 | 0 | } |
5128 | | |
5129 | | static void bpf_map__destroy(struct bpf_map *map); |
5130 | | |
5131 | | static bool map_is_created(const struct bpf_map *map) |
5132 | 0 | { |
5133 | 0 | return map->obj->loaded || map->reused; |
5134 | 0 | } |
5135 | | |
5136 | | static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner) |
5137 | 0 | { |
5138 | 0 | LIBBPF_OPTS(bpf_map_create_opts, create_attr); |
5139 | 0 | struct bpf_map_def *def = &map->def; |
5140 | 0 | const char *map_name = NULL; |
5141 | 0 | int err = 0, map_fd; |
5142 | |
|
5143 | 0 | if (kernel_supports(obj, FEAT_PROG_NAME)) |
5144 | 0 | map_name = map->name; |
5145 | 0 | create_attr.map_ifindex = map->map_ifindex; |
5146 | 0 | create_attr.map_flags = def->map_flags; |
5147 | 0 | create_attr.numa_node = map->numa_node; |
5148 | 0 | create_attr.map_extra = map->map_extra; |
5149 | 0 | create_attr.token_fd = obj->token_fd; |
5150 | 0 | if (obj->token_fd) |
5151 | 0 | create_attr.map_flags |= BPF_F_TOKEN_FD; |
5152 | |
|
5153 | 0 | if (bpf_map__is_struct_ops(map)) { |
5154 | 0 | create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
5155 | 0 | if (map->mod_btf_fd >= 0) { |
5156 | 0 | create_attr.value_type_btf_obj_fd = map->mod_btf_fd; |
5157 | 0 | create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD; |
5158 | 0 | } |
5159 | 0 | } |
5160 | |
|
5161 | 0 | if (obj->btf && btf__fd(obj->btf) >= 0) { |
5162 | 0 | create_attr.btf_fd = btf__fd(obj->btf); |
5163 | 0 | create_attr.btf_key_type_id = map->btf_key_type_id; |
5164 | |