netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, ast@kernel.org, joe@wand.net.nz,
	yhs@fb.com, andrii.nakryiko@gmail.com, kafai@fb.com,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH bpf-next v4 10/16] bpf, libbpf: refactor relocation handling
Date: Fri,  5 Apr 2019 22:59:36 +0200	[thread overview]
Message-ID: <2475c1d25de8b26f56673cd653bb36a273d41f5a.1554497408.git.daniel@iogearbox.net> (raw)
In-Reply-To: <cover.1554497408.git.daniel@iogearbox.net>
In-Reply-To: <cover.1554497408.git.daniel@iogearbox.net>

From: Joe Stringer <joe@wand.net.nz>

Adjust the code for relocations slightly with no functional changes,
so that upcoming patches that will introduce support for relocations
into the .data, .rodata and .bss sections can be added independent
of these changes.

Signed-off-by: Joe Stringer <joe@wand.net.nz>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e1e4d35..476da3d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -866,20 +866,20 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 				obj->efile.symbols = data;
 				obj->efile.strtabidx = sh.sh_link;
 			}
-		} else if ((sh.sh_type == SHT_PROGBITS) &&
-			   (sh.sh_flags & SHF_EXECINSTR) &&
-			   (data->d_size > 0)) {
-			if (strcmp(name, ".text") == 0)
-				obj->efile.text_shndx = idx;
-			err = bpf_object__add_program(obj, data->d_buf,
-						      data->d_size, name, idx);
-			if (err) {
-				char errmsg[STRERR_BUFSIZE];
-				char *cp = libbpf_strerror_r(-err, errmsg,
-							     sizeof(errmsg));
-
-				pr_warning("failed to alloc program %s (%s): %s",
-					   name, obj->path, cp);
+		} else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
+			if (sh.sh_flags & SHF_EXECINSTR) {
+				if (strcmp(name, ".text") == 0)
+					obj->efile.text_shndx = idx;
+				err = bpf_object__add_program(obj, data->d_buf,
+							      data->d_size, name, idx);
+				if (err) {
+					char errmsg[STRERR_BUFSIZE];
+					char *cp = libbpf_strerror_r(-err, errmsg,
+								     sizeof(errmsg));
+
+					pr_warning("failed to alloc program %s (%s): %s",
+						   name, obj->path, cp);
+				}
 			}
 		} else if (sh.sh_type == SHT_REL) {
 			void *reloc = obj->efile.reloc;
@@ -1041,24 +1041,26 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
 			return -LIBBPF_ERRNO__RELOC;
 		}
 
-		/* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
-		for (map_idx = 0; map_idx < nr_maps; map_idx++) {
-			if (maps[map_idx].offset == sym.st_value) {
-				pr_debug("relocation: find map %zd (%s) for insn %u\n",
-					 map_idx, maps[map_idx].name, insn_idx);
-				break;
+		if (sym.st_shndx == maps_shndx) {
+			/* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
+			for (map_idx = 0; map_idx < nr_maps; map_idx++) {
+				if (maps[map_idx].offset == sym.st_value) {
+					pr_debug("relocation: find map %zd (%s) for insn %u\n",
+						 map_idx, maps[map_idx].name, insn_idx);
+					break;
+				}
 			}
-		}
 
-		if (map_idx >= nr_maps) {
-			pr_warning("bpf relocation: map_idx %d large than %d\n",
-				   (int)map_idx, (int)nr_maps - 1);
-			return -LIBBPF_ERRNO__RELOC;
-		}
+			if (map_idx >= nr_maps) {
+				pr_warning("bpf relocation: map_idx %d large than %d\n",
+					   (int)map_idx, (int)nr_maps - 1);
+				return -LIBBPF_ERRNO__RELOC;
+			}
 
-		prog->reloc_desc[i].type = RELO_LD64;
-		prog->reloc_desc[i].insn_idx = insn_idx;
-		prog->reloc_desc[i].map_idx = map_idx;
+			prog->reloc_desc[i].type = RELO_LD64;
+			prog->reloc_desc[i].insn_idx = insn_idx;
+			prog->reloc_desc[i].map_idx = map_idx;
+		}
 	}
 	return 0;
 }
@@ -1420,7 +1422,7 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
 			}
 			insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
 			insns[insn_idx].imm = obj->maps[map_idx].fd;
-		} else {
+		} else if (prog->reloc_desc[i].type == RELO_CALL) {
 			err = bpf_program__reloc_text(prog, obj,
 						      &prog->reloc_desc[i]);
 			if (err)
-- 
2.9.5


  parent reply	other threads:[~2019-04-05 21:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-05 20:59 [PATCH bpf-next v4 00/16] BPF support for global data Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 01/16] bpf: implement lookup-free direct value access for maps Daniel Borkmann
2019-04-06  1:56   ` Alexei Starovoitov
2019-04-06 10:58     ` Daniel Borkmann
2019-04-06 16:54       ` Alexei Starovoitov
2019-04-06 23:55         ` Daniel Borkmann
2019-04-07  2:57           ` Alexei Starovoitov
2019-04-07 18:35             ` Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 02/16] bpf: do not retain flags that are not tied to map lifetime Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 03/16] bpf: add program side {rd,wr}only support for maps Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 04/16] bpf: add syscall side map freeze support Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 05/16] bpf: allow . char as part of the object name Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 06/16] bpf: add specification for BTF Var and DataSec kinds Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 07/16] bpf: kernel side support for BTF Var and DataSec Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 08/16] bpf: allow for key-less BTF in array map Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 09/16] bpf: sync {btf,bpf}.h uapi header from tools infrastructure Daniel Borkmann
2019-04-05 20:59 ` Daniel Borkmann [this message]
2019-04-05 20:59 ` [PATCH bpf-next v4 11/16] bpf, libbpf: support global data/bss/rodata sections Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 12/16] bpf, libbpf: add support for BTF Var and DataSec Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 13/16] bpf: bpftool support for dumping data/bss/rodata sections Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 14/16] bpf, selftest: test {rd,wr}only flags and direct value access Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 15/16] bpf, selftest: test global data/bss/rodata sections Daniel Borkmann
2019-04-05 20:59 ` [PATCH bpf-next v4 16/16] bpf, selftest: add test cases for BTF Var and DataSec Daniel Borkmann
2019-04-06  5:57 ` [PATCH bpf-next v4 00/16] BPF support for global data Martin Lau

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=2475c1d25de8b26f56673cd653bb36a273d41f5a.1554497408.git.daniel@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=joe@wand.net.nz \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.com \
    /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).