Blame SOURCES/0060-bpf-check-map-symbol-type-properly-with-newer-llvm-c.patch

8def76
From 9783e8b3de077c2e6399a9aa83f93237690bd744 Mon Sep 17 00:00:00 2001
8def76
From: Andrea Claudi <aclaudi@redhat.com>
8def76
Date: Thu, 13 Jun 2019 14:37:57 +0200
8def76
Subject: [PATCH] bpf: check map symbol type properly with newer llvm compiler
8def76
8def76
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1716361
8def76
Upstream Status: iproute2.git commit 7a04dd84a7f93
8def76
8def76
commit 7a04dd84a7f938f72fcef9efe8383314b0a66274
8def76
Author: Yonghong Song <yhs@fb.com>
8def76
Date:   Mon Oct 29 15:32:03 2018 -0700
8def76
8def76
    bpf: check map symbol type properly with newer llvm compiler
8def76
8def76
    With llvm 7.0 or earlier, the map symbol type is STT_NOTYPE.
8def76
      -bash-4.4$ cat t.c
8def76
      __attribute__((section("maps"))) int g;
8def76
      -bash-4.4$ clang -target bpf -O2 -c t.c
8def76
      -bash-4.4$ readelf -s t.o
8def76
8def76
      Symbol table '.symtab' contains 2 entries:
8def76
         Num:    Value          Size Type    Bind   Vis      Ndx Name
8def76
           0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
8def76
           1: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    3 g
8def76
8def76
    The following llvm commit enables BPF target to generate
8def76
    proper symbol type and size.
8def76
      commit bf6ec206615b9718869d48b4e5400d0c6e3638dd
8def76
      Author: Yonghong Song <yhs@fb.com>
8def76
      Date:   Wed Sep 19 16:04:13 2018 +0000
8def76
8def76
          [bpf] Symbol sizes and types in object file
8def76
8def76
          Clang-compiled object files currently don't include the symbol sizes and
8def76
          types.  Some tools however need that information.  For example, ctfconvert
8def76
          uses that information to generate FreeBSD's CTF representation from ELF
8def76
          files.
8def76
          With this patch, symbol sizes and types are included in object files.
8def76
8def76
          Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
8def76
          Reported-by: Yutaro Hayakawa <yhayakawa3720@gmail.com>
8def76
8def76
    Hence, for llvm 8.0.0 (currently trunk), symbol type will be not NOTYPE, but OBJECT.
8def76
      -bash-4.4$ clang -target bpf -O2 -c t.c
8def76
      -bash-4.4$ readelf -s t.o
8def76
8def76
      Symbol table '.symtab' contains 3 entries:
8def76
         Num:    Value          Size Type    Bind   Vis      Ndx Name
8def76
           0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
8def76
           1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS t.c
8def76
           2: 0000000000000000     4 OBJECT  GLOBAL DEFAULT    3 g
8def76
8def76
    This patch makes sure bpf library accepts both NOTYPE and OBJECT types
8def76
    of global map symbols.
8def76
8def76
    Signed-off-by: Yonghong Song <yhs@fb.com>
8def76
    Acked-by: Daniel Borkmann <daniel@iogearbox.net>
8def76
    Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
8def76
---
8def76
 lib/bpf.c | 12 +++++++++---
8def76
 1 file changed, 9 insertions(+), 3 deletions(-)
8def76
8def76
diff --git a/lib/bpf.c b/lib/bpf.c
8def76
index d093d0bd86eae..45f279fa4a416 100644
8def76
--- a/lib/bpf.c
8def76
+++ b/lib/bpf.c
8def76
@@ -1758,11 +1758,13 @@ static const char *bpf_map_fetch_name(struct bpf_elf_ctx *ctx, int which)
8def76
 	int i;
8def76
 
8def76
 	for (i = 0; i < ctx->sym_num; i++) {
8def76
+		int type = GELF_ST_TYPE(sym.st_info);
8def76
+
8def76
 		if (gelf_getsym(ctx->sym_tab, i, &sym) != &sym)
8def76
 			continue;
8def76
 
8def76
 		if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
8def76
-		    GELF_ST_TYPE(sym.st_info) != STT_NOTYPE ||
8def76
+		    (type != STT_NOTYPE && type != STT_OBJECT) ||
8def76
 		    sym.st_shndx != ctx->sec_maps ||
8def76
 		    sym.st_value / ctx->map_len != which)
8def76
 			continue;
8def76
@@ -1849,11 +1851,13 @@ static int bpf_map_num_sym(struct bpf_elf_ctx *ctx)
8def76
 	GElf_Sym sym;
8def76
 
8def76
 	for (i = 0; i < ctx->sym_num; i++) {
8def76
+		int type = GELF_ST_TYPE(sym.st_info);
8def76
+
8def76
 		if (gelf_getsym(ctx->sym_tab, i, &sym) != &sym)
8def76
 			continue;
8def76
 
8def76
 		if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
8def76
-		    GELF_ST_TYPE(sym.st_info) != STT_NOTYPE ||
8def76
+		    (type != STT_NOTYPE && type != STT_OBJECT) ||
8def76
 		    sym.st_shndx != ctx->sec_maps)
8def76
 			continue;
8def76
 		num++;
8def76
@@ -1927,10 +1931,12 @@ static int bpf_map_verify_all_offs(struct bpf_elf_ctx *ctx, int end)
8def76
 		 * the table again.
8def76
 		 */
8def76
 		for (i = 0; i < ctx->sym_num; i++) {
8def76
+			int type = GELF_ST_TYPE(sym.st_info);
8def76
+
8def76
 			if (gelf_getsym(ctx->sym_tab, i, &sym) != &sym)
8def76
 				continue;
8def76
 			if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
8def76
-			    GELF_ST_TYPE(sym.st_info) != STT_NOTYPE ||
8def76
+			    (type != STT_NOTYPE && type != STT_OBJECT) ||
8def76
 			    sym.st_shndx != ctx->sec_maps)
8def76
 				continue;
8def76
 			if (sym.st_value == off)
8def76
-- 
8def76
2.20.1
8def76