]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
libbpf: Fix implicit memfd_create() for bionic
authorCarlos Llamas <cmllamas@google.com>
Sun, 30 Mar 2025 21:13:23 +0000 (21:13 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 4 Apr 2025 15:52:37 +0000 (08:52 -0700)
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 9fa5e1a180aa ("libbpf: Call memfd_create() syscall directly").

Fixes: 6d5e5e5d7ce1 ("libbpf: Extend linker API to support in-memory ELF files")
Signed-off-by: Carlos Llamas <cmllamas@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250330211325.530677-1-cmllamas@google.com
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf_internal.h
tools/lib/bpf/linker.c

index 6b85060f07b3b45ebf0543eee0cb08670ab6dc19..37d563e140515633bbcb80ba7c514050e757661c 100644 (file)
@@ -1725,15 +1725,6 @@ static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *nam
        return ERR_PTR(-ENOENT);
 }
 
-/* Some versions of Android don't provide memfd_create() in their libc
- * implementation, so avoid complications and just go straight to Linux
- * syscall.
- */
-static int sys_memfd_create(const char *name, unsigned flags)
-{
-       return syscall(__NR_memfd_create, name, flags);
-}
-
 #ifndef MFD_CLOEXEC
 #define MFD_CLOEXEC 0x0001U
 #endif
index 76669c73dcd162a0953aea3980f27958b69aed1d..477a3b3389a0914c671cf642c90e00572ad53740 100644 (file)
@@ -667,6 +667,15 @@ static inline int sys_dup3(int oldfd, int newfd, int flags)
        return syscall(__NR_dup3, oldfd, newfd, flags);
 }
 
+/* Some versions of Android don't provide memfd_create() in their libc
+ * implementation, so avoid complications and just go straight to Linux
+ * syscall.
+ */
+static inline int sys_memfd_create(const char *name, unsigned flags)
+{
+       return syscall(__NR_memfd_create, name, flags);
+}
+
 /* Point *fixed_fd* to the same file that *tmp_fd* points to.
  * Regardless of success, *tmp_fd* is closed.
  * Whatever *fixed_fd* pointed to is closed silently.
index 800e0ef09c37875cb720fb82272e7ab68932b0dc..56f5068e2ebab0a9232f265cf4d7519436d88262 100644 (file)
@@ -573,7 +573,7 @@ int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
 
        snprintf(filename, sizeof(filename), "mem:%p+%zu", buf, buf_sz);
 
-       fd = memfd_create(filename, 0);
+       fd = sys_memfd_create(filename, 0);
        if (fd < 0) {
                ret = -errno;
                pr_warn("failed to create memfd '%s': %s\n", filename, errstr(ret));