From 0fdee392b9eca3499b883b14c41c7aca53f376ce Mon Sep 17 00:00:00 2001 From: Giuliano Procida Date: Wed, 2 Dec 2020 12:35:37 +0000 Subject: [PATCH 1/2] btf: improve error-handling around objcopy * Report the correct filename when objcopy fails. * Unlink the temporary file on error. Signed-off-by: Giuliano Procida --- libbtf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libbtf.c b/libbtf.c index 246762c4..e24507c7 100644 --- a/libbtf.c +++ b/libbtf.c @@ -785,18 +785,19 @@ static int btf_elf__write(const char *filename, struct btf *btf) if (write(fd, raw_btf_data, raw_btf_size) != raw_btf_size) { fprintf(stderr, "%s: write of %d bytes to '%s' failed: %d!\n", __func__, raw_btf_size, tmp_fn, errno); - goto out; + goto unlink; } snprintf(cmd, sizeof(cmd), "%s --add-section .BTF=%s %s", llvm_objcopy, tmp_fn, filename); if (system(cmd)) { fprintf(stderr, "%s: failed to add .BTF section to '%s': %d!\n", - __func__, tmp_fn, errno); - goto out; + __func__, filename, errno); + goto unlink; } err = 0; + unlink: unlink(tmp_fn); } From a3d940a2b83d35137ddbdcddce1a9110d8b50ea3 Mon Sep 17 00:00:00 2001 From: Giuliano Procida Date: Wed, 2 Dec 2020 11:41:24 +0000 Subject: [PATCH 2/2] btf: set .BTF section alignment to 16 This is to avoid misaligned access when memory-mapping ELF sections. Signed-off-by: Giuliano Procida --- libbtf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libbtf.c b/libbtf.c index e24507c7..6bfc5677 100644 --- a/libbtf.c +++ b/libbtf.c @@ -796,6 +796,14 @@ static int btf_elf__write(const char *filename, struct btf *btf) goto unlink; } + snprintf(cmd, sizeof(cmd), "%s --set-section-alignment .BTF=16 %s", + llvm_objcopy, filename); + if (system(cmd)) { + /* non-fatal, this is a nice-to-have and it's only supported from LLVM 10 */ + fprintf(stderr, "%s: warning: failed to align .BTF section in '%s': %d!\n", + __func__, filename, errno); + } + err = 0; unlink: unlink(tmp_fn);