2 * Common eBPF ELF object loading operations.
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
16 #include <asm/unistd.h>
17 #include <linux/kernel.h>
18 #include <linux/bpf.h>
24 #define __printf(a, b) __attribute__((format(printf, a, b)))
27 static int __base_pr(const char *format
, ...)
32 va_start(args
, format
);
33 err
= vfprintf(stderr
, format
, args
);
38 static __printf(1, 2) libbpf_print_fn_t __pr_warning
= __base_pr
;
39 static __printf(1, 2) libbpf_print_fn_t __pr_info
= __base_pr
;
40 static __printf(1, 2) libbpf_print_fn_t __pr_debug
;
42 #define __pr(func, fmt, ...) \
45 (func)("libbpf: " fmt, ##__VA_ARGS__); \
48 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
49 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
50 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
52 void libbpf_set_print(libbpf_print_fn_t warn
,
53 libbpf_print_fn_t info
,
54 libbpf_print_fn_t debug
)
61 /* Copied from tools/perf/util/util.h */
63 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
67 # define zclose(fd) ({ \
70 ___err = close((fd)); \
75 #ifdef HAVE_LIBELF_MMAP_SUPPORT
76 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
78 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
85 * Information when doing elf related work. Only valid if fd
97 #define obj_elf_valid(o) ((o)->efile.elf)
99 static struct bpf_object
*bpf_object__new(const char *path
,
103 struct bpf_object
*obj
;
105 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
107 pr_warning("alloc memory failed for %s\n", path
);
111 strcpy(obj
->path
, path
);
115 * Caller of this function should also calls
116 * bpf_object__elf_finish() after data collection to return
117 * obj_buf to user. If not, we should duplicate the buffer to
118 * avoid user freeing them before elf finish.
120 obj
->efile
.obj_buf
= obj_buf
;
121 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
126 static void bpf_object__elf_finish(struct bpf_object
*obj
)
128 if (!obj_elf_valid(obj
))
131 if (obj
->efile
.elf
) {
132 elf_end(obj
->efile
.elf
);
133 obj
->efile
.elf
= NULL
;
135 zclose(obj
->efile
.fd
);
136 obj
->efile
.obj_buf
= NULL
;
137 obj
->efile
.obj_buf_sz
= 0;
140 static int bpf_object__elf_init(struct bpf_object
*obj
)
145 if (obj_elf_valid(obj
)) {
146 pr_warning("elf init: internal error\n");
150 if (obj
->efile
.obj_buf_sz
> 0) {
152 * obj_buf should have been validated by
153 * bpf_object__open_buffer().
155 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
156 obj
->efile
.obj_buf_sz
);
158 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
159 if (obj
->efile
.fd
< 0) {
160 pr_warning("failed to open %s: %s\n", obj
->path
,
165 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
166 LIBBPF_ELF_C_READ_MMAP
,
170 if (!obj
->efile
.elf
) {
171 pr_warning("failed to open %s as ELF file\n",
177 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
178 pr_warning("failed to get EHDR from %s\n",
183 ep
= &obj
->efile
.ehdr
;
185 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
!= 0)) {
186 pr_warning("%s is not an eBPF object file\n",
194 bpf_object__elf_finish(obj
);
199 bpf_object__check_endianness(struct bpf_object
*obj
)
201 static unsigned int const endian
= 1;
203 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
205 /* We are big endian, BPF obj is little endian. */
206 if (*(unsigned char const *)&endian
!= 1)
211 /* We are little endian, BPF obj is big endian. */
212 if (*(unsigned char const *)&endian
!= 0)
222 pr_warning("Error: endianness mismatch.\n");
227 bpf_object__init_license(struct bpf_object
*obj
,
228 void *data
, size_t size
)
230 memcpy(obj
->license
, data
,
231 min(size
, sizeof(obj
->license
) - 1));
232 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
237 bpf_object__init_kversion(struct bpf_object
*obj
,
238 void *data
, size_t size
)
242 if (size
!= sizeof(kver
)) {
243 pr_warning("invalid kver section in %s\n", obj
->path
);
246 memcpy(&kver
, data
, sizeof(kver
));
247 obj
->kern_version
= kver
;
248 pr_debug("kernel version of %s is %x\n", obj
->path
,
253 static int bpf_object__elf_collect(struct bpf_object
*obj
)
255 Elf
*elf
= obj
->efile
.elf
;
256 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
258 int idx
= 0, err
= 0;
260 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
261 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
262 pr_warning("failed to get e_shstrndx from %s\n",
267 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
273 if (gelf_getshdr(scn
, &sh
) != &sh
) {
274 pr_warning("failed to get section header from %s\n",
280 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
282 pr_warning("failed to get section name from %s\n",
288 data
= elf_getdata(scn
, 0);
290 pr_warning("failed to get section data from %s(%s)\n",
295 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
296 name
, (unsigned long)data
->d_size
,
297 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
300 if (strcmp(name
, "license") == 0)
301 err
= bpf_object__init_license(obj
,
304 else if (strcmp(name
, "version") == 0)
305 err
= bpf_object__init_kversion(obj
,
315 static int bpf_object__validate(struct bpf_object
*obj
)
317 if (obj
->kern_version
== 0) {
318 pr_warning("%s doesn't provide kernel version\n",
325 static struct bpf_object
*
326 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
328 struct bpf_object
*obj
;
330 if (elf_version(EV_CURRENT
) == EV_NONE
) {
331 pr_warning("failed to init libelf for %s\n", path
);
335 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
339 if (bpf_object__elf_init(obj
))
341 if (bpf_object__check_endianness(obj
))
343 if (bpf_object__elf_collect(obj
))
345 if (bpf_object__validate(obj
))
348 bpf_object__elf_finish(obj
);
351 bpf_object__close(obj
);
355 struct bpf_object
*bpf_object__open(const char *path
)
357 /* param validation */
361 pr_debug("loading %s\n", path
);
363 return __bpf_object__open(path
, NULL
, 0);
366 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
369 /* param validation */
370 if (!obj_buf
|| obj_buf_sz
<= 0)
373 pr_debug("loading object from buffer\n");
375 return __bpf_object__open("[buffer]", obj_buf
, obj_buf_sz
);
378 void bpf_object__close(struct bpf_object
*obj
)
383 bpf_object__elf_finish(obj
);