Skip to contentSkip to conversation

libbpf: Extend linker API to support in-memory ELF files

PATCH Applied Alastair Robertson 3 participants 6 messages Dec 11, 2024 – Dec 12, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover libbpf: Extend linker API to support in-memory ELF files

This gives API consumers the option of using anonymous files/memfds to avoid writing temporary ELFs to disk, which will be useful for performing linking as part of bpftrace's JIT compilation.

v3:
- Removed "filename" option. Now always generate our own filename for passed-in FDs and buffers.
- Use a common function (bpf_linker_add_file) for shared implementation of bpf_linker__add_file, bpf_linker__add_fd and bpf_linker__add_buf.

Alastair Robertson (2):
libbpf: Pull file-opening logic up to top-level functions
libbpf: Extend linker API to support in-memory ELF files

 tools/lib/bpf/libbpf.h   |   5 +
 tools/lib/bpf/libbpf.map |   4 +
 tools/lib/bpf/linker.c   | 228 ++++++++++++++++++++++++++++++---------
 3 files changed, 184 insertions(+), 53 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH v3 1/2 libbpf: Pull file-opening logic up to top-level functions

Move the filename arguments and file-descriptor handling from init_output_elf() and linker_load_obj_file() and instead handle them at the top-level in bpf_linker__new() and bpf_linker__add_file().

This will allow the inner functions to be shared with a new, non-filename-based, API in the next commit.

---
 tools/lib/bpf/linker.c | 83 +++++++++++++++++++++---------------------
 1 file changed, 41 insertions(+), 42 deletions(-)
tools/lib/bpf/linker.c +41 -42
index e56ba6e67451..eb2ac7afce01 100644
@@ -157,10 +157,9 @@ struct bpf_linker {
 #define pr_warn_elf(fmt, ...)									\
 	libbpf_print(LIBBPF_WARN, "libbpf: " fmt ": %s\n", ##__VA_ARGS__, elf_errmsg(-1))
 
-static int init_output_elf(struct bpf_linker *linker, const char *file);
+static int init_output_elf(struct bpf_linker *linker);
 
-static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
-				const struct bpf_linker_file_opts *opts,
+static int linker_load_obj_file(struct bpf_linker *linker,
 				struct src_obj *obj);
 static int linker_sanity_check_elf(struct src_obj *obj);
 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
@@ -233,9 +232,20 @@ struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts
 	if (!linker)
 		return errno = ENOMEM, NULL;
 
-	linker->fd = -1;
+	linker->filename = strdup(filename);
+	if (!linker->filename) {
+		err = -ENOMEM;
+		goto err_out;
+	}
+
+	linker->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
+	if (linker->fd < 0) {
+		err = -errno;
+		pr_warn("failed to create '%s': %d\n", filename, err);
+		goto err_out;
+	}
 
-	err = init_output_elf(linker, filename);
+	err = init_output_elf(linker);
 	if (err)
 		goto err_out;
 
@@ -294,23 +304,12 @@ static Elf64_Sym *add_new_sym(struct bpf_linker *linker, size_t *sym_idx)
 	return sym;
 }
 
-static int init_output_elf(struct bpf_linker *linker, const char *file)
+static int init_output_elf(struct bpf_linker *linker)
 {
 	int err, str_off;
 	Elf64_Sym *init_sym;
 	struct dst_sec *sec;
 
-	linker->filename = strdup(file);
-	if (!linker->filename)
-		return -ENOMEM;
-
-	linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
-	if (linker->fd < 0) {
-		err = -errno;
-		pr_warn("failed to create '%s': %s\n", file, errstr(err));
-		return err;
-	}
-
 	linker->elf = elf_begin(linker->fd, ELF_C_WRITE, NULL);
 	if (!linker->elf) {
 		pr_warn_elf("failed to create ELF object");
@@ -440,7 +439,7 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
 			 const struct bpf_linker_file_opts *opts)
 {
 	struct src_obj obj = {};
-	int err = 0;
+	int err = 0, fd;
 
 	if (!OPTS_VALID(opts, bpf_linker_file_opts))
 		return libbpf_err(-EINVAL);
@@ -448,7 +447,16 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
 	if (!linker->elf)
 		return libbpf_err(-EINVAL);
 
-	err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
+	fd = open(filename, O_RDONLY | O_CLOEXEC);
+	if (fd < 0) {
+		pr_warn("failed to open file '%s': %s\n", filename, errstr(errno));
+		return -errno;
+	}
+
+	obj.filename = filename;
+	obj.fd = fd;
+
+	err = err ?: linker_load_obj_file(linker, &obj);
 	err = err ?: linker_append_sec_data(linker, &obj);
 	err = err ?: linker_append_elf_syms(linker, &obj);
 	err = err ?: linker_append_elf_relos(linker, &obj);
@@ -534,8 +542,7 @@ static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
 	return sec;
 }
 
-static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
-				const struct bpf_linker_file_opts *opts,
+static int linker_load_obj_file(struct bpf_linker *linker,
 				struct src_obj *obj)
 {
 	int err = 0;
@@ -554,26 +561,18 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 #error "Unknown __BYTE_ORDER__"
 #endif
 
-	pr_debug("linker: adding object file '%s'...\n", filename);
-
-	obj->filename = filename;
+	pr_debug("linker: adding object file '%s'...\n", obj->filename);
 
-	obj->fd = open(filename, O_RDONLY | O_CLOEXEC);
-	if (obj->fd < 0) {
-		err = -errno;
-		pr_warn("failed to open file '%s': %s\n", filename, errstr(err));
-		return err;
-	}
 	obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL);
 	if (!obj->elf) {
-		pr_warn_elf("failed to parse ELF file '%s'", filename);
+		pr_warn_elf("failed to parse ELF file '%s'", obj->filename);
 		return -EINVAL;
 	}
 
 	/* Sanity check ELF file high-level properties */
 	ehdr = elf64_getehdr(obj->elf);
 	if (!ehdr) {
-		pr_warn_elf("failed to get ELF header for %s", filename);
+		pr_warn_elf("failed to get ELF header for %s", obj->filename);
 		return -EINVAL;
 	}
 
@@ -581,7 +580,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 	obj_byteorder = ehdr->e_ident[EI_DATA];
 	if (obj_byteorder != ELFDATA2LSB && obj_byteorder != ELFDATA2MSB) {
 		err = -EOPNOTSUPP;
-		pr_warn("unknown byte order of ELF file %s\n", filename);
+		pr_warn("unknown byte order of ELF file %s\n", obj->filename);
 		return err;
 	}
 	if (link_byteorder == ELFDATANONE) {
@@ -591,7 +590,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 			 obj_byteorder == ELFDATA2MSB ? "big" : "little");
 	} else if (link_byteorder != obj_byteorder) {
 		err = -EOPNOTSUPP;
-		pr_warn("byte order mismatch with ELF file %s\n", filename);
+		pr_warn("byte order mismatch with ELF file %s\n", obj->filename);
 		return err;
 	}
 
@@ -599,12 +598,12 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 	    || ehdr->e_machine != EM_BPF
 	    || ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
 		err = -EOPNOTSUPP;
-		pr_warn_elf("unsupported kind of ELF file %s", filename);
+		pr_warn_elf("unsupported kind of ELF file %s", obj->filename);
 		return err;
 	}
 
 	if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) {
-		pr_warn_elf("failed to get SHSTRTAB section index for %s", filename);
+		pr_warn_elf("failed to get SHSTRTAB section index for %s", obj->filename);
 		return -EINVAL;
 	}
 
@@ -616,21 +615,21 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 		shdr = elf64_getshdr(scn);
 		if (!shdr) {
 			pr_warn_elf("failed to get section #%zu header for %s",
-				    sec_idx, filename);
+				    sec_idx, obj->filename);
 			return -EINVAL;
 		}
 
 		sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name);
 		if (!sec_name) {
 			pr_warn_elf("failed to get section #%zu name for %s",
-				    sec_idx, filename);
+				    sec_idx, obj->filename);
 			return -EINVAL;
 		}
 
 		data = elf_getdata(scn, 0);
 		if (!data) {
 			pr_warn_elf("failed to get section #%zu (%s) data from %s",
-				    sec_idx, sec_name, filename);
+				    sec_idx, sec_name, obj->filename);
 			return -EINVAL;
 		}
 
@@ -666,7 +665,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 				err = libbpf_get_error(obj->btf);
 				if (err) {
 					pr_warn("failed to parse .BTF from %s: %s\n",
-						filename, errstr(err));
+						obj->filename, errstr(err));
 					return err;
 				}
 				sec->skipped = true;
@@ -677,7 +676,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 				err = libbpf_get_error(obj->btf_ext);
 				if (err) {
 					pr_warn("failed to parse .BTF.ext from '%s': %s\n",
-						filename, errstr(err));
+						obj->filename, errstr(err));
 					return err;
 				}
 				sec->skipped = true;
@@ -694,7 +693,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
 			break;
 		default:
 			pr_warn("unrecognized section #%zu (%s) in %s\n",
-				sec_idx, sec_name, filename);
+				sec_idx, sec_name, obj->filename);
 			err = -EINVAL;
 			return err;
 		}
Signature · 1 line
2.43.5
#2 lore raw

Re: [PATCH bpf-next v3 1/2] libbpf: Pull file-opening logic up to top-level functions

Quoted 13 lines · Alastair Robertson

Move the filename arguments and file-descriptor handling from init_output_elf() and linker_load_obj_file() and instead handle them at the top-level in bpf_linker__new() and bpf_linker__add_file().

This will allow the inner functions to be shared with a new, non-filename-based, API in the next commit.

---
 tools/lib/bpf/linker.c | 83 +++++++++++++++++++++---------------------
 1 file changed, 41 insertions(+), 42 deletions(-)

[...]

Quoted 10 lines
tools/lib/bpf/linker.c +1 -1
@@ -440,7 +439,7 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
                         const struct bpf_linker_file_opts *opts)
 {
        struct src_obj obj = {};
-       int err = 0;
+       int err = 0, fd;
 
        if (!OPTS_VALID(opts, bpf_linker_file_opts))
                return libbpf_err(-EINVAL);

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 453

Quoted 8 lines
tools/lib/bpf/linker.c +4 -1
@@ -448,7 +447,16 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
        if (!linker->elf)
                return libbpf_err(-EINVAL);
 
-       err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
+       fd = open(filename, O_RDONLY | O_CLOEXEC);
+       if (fd < 0) {
+               pr_warn("failed to open file '%s': %s\n", filename, errstr(errno));
+               return -errno;

errno can be clobbered by pr_warn(), so need to save it like in another pr_warn case above, fixed while applying

Quoted 19 lines
tools/lib/bpf/linker.c +7 -2
+       }
+
+       obj.filename = filename;
+       obj.fd = fd;
+
+       err = err ?: linker_load_obj_file(linker, &obj);
        err = err ?: linker_append_sec_data(linker, &obj);
        err = err ?: linker_append_elf_syms(linker, &obj);
        err = err ?: linker_append_elf_relos(linker, &obj);
@@ -534,8 +542,7 @@ static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
        return sec;
 }
 
-static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
-                               const struct bpf_linker_file_opts *opts,
+static int linker_load_obj_file(struct bpf_linker *linker,
                                struct src_obj *obj)
 {
        int err = 0;

[...]

#3 lore raw

PATCH v3 2/2 libbpf: Extend linker API to support in-memory ELF files

In reply to Alastair Robertson

The new_fd and add_fd functions correspond to the original new and add_file functions, but accept an FD instead of a file name. This gives API consumers the option of using anonymous files/memfds to avoid writing ELFs to disk.

This new API will be useful for performing linking as part of bpftrace's JIT compilation.

The add_buf function is a convenience wrapper that does the work of creating a memfd for the caller.

---
 tools/lib/bpf/libbpf.h   |   5 ++
 tools/lib/bpf/libbpf.map |   4 +
 tools/lib/bpf/linker.c   | 163 ++++++++++++++++++++++++++++++++++-----
 3 files changed, 152 insertions(+), 20 deletions(-)
tools/lib/bpf/libbpf.h +5 -0
index b2ce3a72b11d..d45807103565 100644
@@ -1796,9 +1796,14 @@ struct bpf_linker_file_opts {
 struct bpf_linker;
 
 LIBBPF_API struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts);
+LIBBPF_API struct bpf_linker *bpf_linker__new_fd(int fd, struct bpf_linker_opts *opts);
 LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker,
 				    const char *filename,
 				    const struct bpf_linker_file_opts *opts);
+LIBBPF_API int bpf_linker__add_fd(struct bpf_linker *linker, int fd,
+				  const struct bpf_linker_file_opts *opts);
+LIBBPF_API int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
+				   const struct bpf_linker_file_opts *opts);
 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
tools/lib/bpf/libbpf.map +4 -0
index 54b6f312cfa8..a8b2936a1646 100644
@@ -432,4 +432,8 @@ LIBBPF_1.5.0 {
 } LIBBPF_1.4.0;
 
 LIBBPF_1.6.0 {
+	global:
+		bpf_linker__add_buf;
+		bpf_linker__add_fd;
+		bpf_linker__new_fd;
 } LIBBPF_1.5.0;
tools/lib/bpf/linker.c +143 -20

224 lines not on this page · raw message

Signature · 1 line
2.43.5
#4 lore raw
Quoted 12 lines · Alastair Robertson

The new_fd and add_fd functions correspond to the original new and add_file functions, but accept an FD instead of a file name. This gives API consumers the option of using anonymous files/memfds to avoid writing ELFs to disk.

This new API will be useful for performing linking as part of bpftrace's JIT compilation.

The add_buf function is a convenience wrapper that does the work of creating a memfd for the caller.

Quoted 7 lines
---
 tools/lib/bpf/libbpf.h   |   5 ++
 tools/lib/bpf/libbpf.map |   4 +
 tools/lib/bpf/linker.c   | 163 ++++++++++++++++++++++++++++++++++-----
 3 files changed, 152 insertions(+), 20 deletions(-)

There were a bunch of errno mishandling issues and a small memory leak. I fixed all that up and landed in bpf-next.

Quoted 5 lines
tools/lib/bpf/linker.c +5 -0
+int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
+                        const struct bpf_linker_file_opts *opts)
+{
+       int fd, ret;
+

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 533

Quoted 10 lines
tools/lib/bpf/linker.c +10 -0
+       if (!OPTS_VALID(opts, bpf_linker_file_opts))
+               return libbpf_err(-EINVAL);
+
+       if (!linker->elf)
+               return libbpf_err(-EINVAL);
+
+       fd = open(filename, O_RDONLY | O_CLOEXEC);
+       if (fd < 0) {
+               pr_warn("failed to open file '%s': %s\n", filename, errstr(errno));
+               return -errno;

same issue with errno clobbering, fixed, but please be careful with errno, pretty much anything can clobber errno in libc, except free() and maybe a few more APIs

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 540

Quoted 7 lines
tools/lib/bpf/linker.c +7 -0
+       }
+
+       ret = bpf_linker_add_file(linker, fd, filename);
+
+       close(fd);
+
+       return ret;

this needed to be libbpf_err(ret), because close() can clobber errno, added while applying

Quoted 12 lines
tools/lib/bpf/linker.c +12 -0
+}
+
+int bpf_linker__add_fd(struct bpf_linker *linker, int fd,
+                      const struct bpf_linker_file_opts *opts)
+{
+       char filename[32];
+       int ret;
+
+       if (!OPTS_VALID(opts, bpf_linker_file_opts))
+               return libbpf_err(-EINVAL);
+
+       if (!linker->elf)

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 562

Quoted 10 lines
tools/lib/bpf/linker.c +10 -0
+               return libbpf_err(-EINVAL);
+
+       if (fd < 0)
+               return libbpf_err(-EINVAL);
+
+       snprintf(filename, sizeof(filename), "fd:%d", fd);
+
+       ret = bpf_linker_add_file(linker, fd, filename);
+
+       return ret;

here as well

Quoted 8 lines
tools/lib/bpf/linker.c +8 -0
+}
+
+int bpf_linker__add_buf(struct bpf_linker *linker, void *buf, size_t buf_sz,
+                       const struct bpf_linker_file_opts *opts)
+{
+       char filename[32];
+       int fd, written, ret;
+

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 580

Quoted 10 lines
tools/lib/bpf/linker.c +10 -0
+       if (!OPTS_VALID(opts, bpf_linker_file_opts))
+               return libbpf_err(-EINVAL);
+
+       if (!linker->elf)
+               return libbpf_err(-EINVAL);
+
+       snprintf(filename, sizeof(filename), "mem:%p+%zu", buf, buf_sz);
+
+       fd = memfd_create(filename, 0);
+       if (fd < 0) {

and here

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 588

Quoted 8 lines
tools/lib/bpf/linker.c +8 -0
+               pr_warn("failed to create memfd '%s': %s\n", filename, errstr(errno));
+               return -errno;
+       }
+
+       written = 0;
+       while (written < buf_sz) {
+               ret = write(fd, buf, buf_sz);
+               if (ret < 0) {

and here

and also you were leaking memfd here, I added jump to close(fd)

tools/lib/bpf/linker.c int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, · line 599

Quoted 11 lines
tools/lib/bpf/linker.c +11 -0
+                       pr_warn("failed to write '%s': %s\n", filename, errstr(errno));
+                       return -errno;
+               }
+               written += ret;
+       }
+
+       ret = bpf_linker_add_file(linker, fd, filename);
+
+       close(fd);
+
+       return ret;

and here

Quoted 21 lines
tools/lib/bpf/linker.c +5 -3
+}
+
 static bool is_dwarf_sec_name(const char *name)
 {
        /* approximation, but the actual list is too long */
@@ -2686,9 +2808,10 @@ int bpf_linker__finalize(struct bpf_linker *linker)
        }
 
        elf_end(linker->elf);
-       close(linker->fd);
-
        linker->elf = NULL;
+
+       if (linker->fd_is_owned)
+               close(linker->fd);
        linker->fd = -1;
 
        return 0;
--

2.43.5

#5 lore raw
In reply to Alastair Robertson
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 4 lines · On Wed, 11 Dec 2024 08:40:28 -0800 you wrote:

This gives API consumers the option of using anonymous files/memfds to avoid writing temporary ELFs to disk, which will be useful for performing linking as part of bpftrace's JIT compilation.

Quoted 8 lines

v3:
- Removed "filename" option. Now always generate our own filename for passed-in FDs and buffers.
- Use a common function (bpf_linker_add_file) for shared implementation of bpf_linker__add_file, bpf_linker__add_fd and bpf_linker__add_buf.

[...]

Here is the summary with links:
- [bpf-next,v3,1/2] libbpf: Pull file-opening logic up to top-level functions

    https://git.kernel.org/bpf/bpf-next/c/b641712925bf
  - [bpf-next,v3,2/2] libbpf: Extend linker API to support in-memory ELF files
    https://git.kernel.org/bpf/bpf-next/c/6d5e5e5d7ce1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html