Skip to content

Commit 75011ad

Browse files
Carlos Llamasanakryiko
Carlos Llamas
authored andcommitted
libbpf: Fix implicit memfd_create() for bionic
Since memfd_create() is not consistently available across different bionic libc implementations, using memfd_create() directly can break some Android builds: tools/lib/bpf/linker.c:576:7: error: implicit declaration of function 'memfd_create' [-Werror,-Wimplicit-function-declaration] 576 | fd = memfd_create(filename, 0); | ^ To fix this, relocate and inline the sys_memfd_create() helper so that it can be used in "linker.c". Similar issues were previously fixed by commit 9fa5e1a ("libbpf: Call memfd_create() syscall directly"). Fixes: 6d5e5e5 ("libbpf: Extend linker API to support in-memory ELF files") Signed-off-by: Carlos Llamas <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 06a2236 commit 75011ad

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

tools/lib/bpf/libbpf.c

-9
Original file line numberDiff line numberDiff line change
@@ -1725,15 +1725,6 @@ static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *nam
17251725
return ERR_PTR(-ENOENT);
17261726
}
17271727

1728-
/* Some versions of Android don't provide memfd_create() in their libc
1729-
* implementation, so avoid complications and just go straight to Linux
1730-
* syscall.
1731-
*/
1732-
static int sys_memfd_create(const char *name, unsigned flags)
1733-
{
1734-
return syscall(__NR_memfd_create, name, flags);
1735-
}
1736-
17371728
#ifndef MFD_CLOEXEC
17381729
#define MFD_CLOEXEC 0x0001U
17391730
#endif

tools/lib/bpf/libbpf_internal.h

+9
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,15 @@ static inline int sys_dup3(int oldfd, int newfd, int flags)
667667
return syscall(__NR_dup3, oldfd, newfd, flags);
668668
}
669669

670+
/* Some versions of Android don't provide memfd_create() in their libc
671+
* implementation, so avoid complications and just go straight to Linux
672+
* syscall.
673+
*/
674+
static inline int sys_memfd_create(const char *name, unsigned flags)
675+
{
676+
return syscall(__NR_memfd_create, name, flags);
677+
}
678+
670679
/* Point *fixed_fd* to the same file that *tmp_fd* points to.
671680
* Regardless of success, *tmp_fd* is closed.
672681
* Whatever *fixed_fd* pointed to is closed silently.

tools/lib/bpf/linker.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
573573

574574
snprintf(filename, sizeof(filename), "mem:%p+%zu", buf, buf_sz);
575575

576-
fd = memfd_create(filename, 0);
576+
fd = sys_memfd_create(filename, 0);
577577
if (fd < 0) {
578578
ret = -errno;
579579
pr_warn("failed to create memfd '%s': %s\n", filename, errstr(ret));

0 commit comments

Comments
 (0)