netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Stringer <joe@wand.net.nz>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, daniel@iogearbox.net, ast@kernel.org
Subject: [PATCH bpf-next 3/4] libbpf: Support relocations for bss.
Date: Mon, 11 Feb 2019 16:47:28 -0800	[thread overview]
Message-ID: <20190212004729.535-4-joe@wand.net.nz> (raw)
In-Reply-To: <20190212004729.535-1-joe@wand.net.nz>

The BSS section in an ELF generated by LLVM represents constants for
uninitialized variables or variables that are configured with a zero
value. Support initializing zeroed static data by parsing the
relocations with references to the .bss section and zeroing them.

Signed-off-by: Joe Stringer <joe@wand.net.nz>
---
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index da35d5559b22..ff66d7e970c9 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -141,6 +141,7 @@ struct bpf_program {
 			RELO_LD64,
 			RELO_CALL,
 			RELO_DATA,
+			RELO_ZERO,
 		} type;
 		int insn_idx;
 		union {
@@ -222,6 +223,7 @@ struct bpf_object {
 		int maps_shndx;
 		int text_shndx;
 		int data_shndx;
+		int bss_shndx;
 	} efile;
 	/*
 	 * All loaded bpf_object is linked in a list, which is
@@ -901,6 +903,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 				obj->efile.reloc[n].shdr = sh;
 				obj->efile.reloc[n].data = data;
 			}
+		} else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
+			obj->efile.bss_shndx = idx;
 		} else {
 			pr_debug("skip section(%d) %s\n", idx, name);
 		}
@@ -971,6 +975,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
 	int text_shndx = obj->efile.text_shndx;
 	int maps_shndx = obj->efile.maps_shndx;
 	int data_shndx = obj->efile.data_shndx;
+	int bss_shndx = obj->efile.bss_shndx;
 	struct bpf_map *maps = obj->maps;
 	size_t nr_maps = obj->nr_maps;
 	int i, nrels;
@@ -1010,7 +1015,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
 			 (long long) sym.st_value, sym.st_name);
 
 		if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx &&
-		    sym.st_shndx != data_shndx) {
+		    sym.st_shndx != data_shndx && sym.st_shndx != bss_shndx) {
 			pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n",
 				   prog->section_name, sym.st_shndx);
 			return -LIBBPF_ERRNO__RELOC;
@@ -1070,6 +1075,9 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
 			prog->reloc_desc[i].type = RELO_DATA;
 			prog->reloc_desc[i].insn_idx = insn_idx;
 			prog->reloc_desc[i].data = *static_data;
+		} else if (sym.st_shndx == bss_shndx) {
+			prog->reloc_desc[i].type = RELO_ZERO;
+			prog->reloc_desc[i].insn_idx = insn_idx;
 		}
 	}
 	return 0;
@@ -1429,6 +1437,10 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
 
 			insn_idx = prog->reloc_desc[i].insn_idx;
 			insns[insn_idx].imm = prog->reloc_desc[i].data;
+		} else if (prog->reloc_desc[i].type == RELO_ZERO) {
+			int insn_idx = prog->reloc_desc[i].insn_idx;
+
+			prog->insns[insn_idx].imm = 0;
 		}
 	}
 
-- 
2.19.1


  parent reply	other threads:[~2019-02-12  0:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-12  0:47 [PATCH bpf-next 0/4] libbpf: Add support for 32-bit static data Joe Stringer
2019-02-12  0:47 ` [PATCH bpf-next 1/4] libbpf: Refactor relocations Joe Stringer
2019-02-12  0:47 ` [PATCH bpf-next 2/4] libbpf: Support 32-bit static data loads Joe Stringer
2019-02-15  5:38   ` Y Song
2019-02-15  7:16     ` Joe Stringer
2019-02-15 20:18       ` Y Song
2019-02-27 22:42         ` Y Song
2019-02-15 16:17     ` Alexei Starovoitov
2019-02-12  0:47 ` Joe Stringer [this message]
2019-02-12  0:47 ` [PATCH bpf-next 4/4] selftests/bpf: Test static data relocation Joe Stringer
2019-02-12  5:01   ` Alexei Starovoitov
2019-02-12 20:43     ` Joe Stringer
2019-02-14  0:35       ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190212004729.535-4-joe@wand.net.nz \
    --to=joe@wand.net.nz \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).