public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] backends,bpf: add proper relocation support
@ 2018-06-15 22:40 Yonghong Song
  2018-06-16 14:04 ` Mark Wielaard
  0 siblings, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2018-06-15 22:40 UTC (permalink / raw)
  To: elfutils-devel, ast, kafai

Due to libdw does not have proper BPF relocation support,
the pahole cannot display filenames correctly for objects
with default llvm options. So we have to invent
a special option "llc -march=bpf -mattr=dwarfris" to
prevent llvm from generating cross-section dwarf relocation
records (https://reviews.llvm.org/rL326505).
The pahole related discussion is in linux netdev
mailing list (http://lists.openwall.net/netdev/2018/06/15/38, etc.)

We would like to add proper BPF relocation support
to libdw so eventually we could retire the special llc bpf
flag "-mattr=dwarfris".

The bpf relocations are defined in
llvm_repo:include/llvm/BinaryFormat/ELFRelocs/BPF.def:
  ELF_RELOC(R_BPF_NONE,        0)
  ELF_RELOC(R_BPF_64_64,       1)
  ELF_RELOC(R_BPF_64_32,      10)

Removed the relocation type R_BPF_MAP_FD whoes name does not
confirm to llvm definition and replaced it with R_BPF_64_64.
The BPF object is just a relocatible object, not an executable or
a shared library, so assign ELF type to REL only in bpf_reloc.def.

Tested locally with building pahole with this patch and
pahole is able to display structures in bpf object file properly.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 backends/Makefile.am   |  2 +-
 backends/bpf_init.c    |  1 +
 backends/bpf_reloc.def |  3 ++-
 backends/bpf_symbol.c  | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
 libelf/elf.h           |  3 ++-
 5 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 backends/bpf_symbol.c

diff --git a/backends/Makefile.am b/backends/Makefile.am
index 80aa00e7..b5e721d8 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -126,7 +126,7 @@ am_libebl_m68k_pic_a_OBJECTS = $(m68k_SRCS:.c=.os)
 # an issue.
 m68k_corenote_no_Wpacked_not_aligned = yes
 
-bpf_SRCS = bpf_init.c bpf_regs.c
+bpf_SRCS = bpf_init.c bpf_regs.c bpf_symbol.c
 cpu_bpf = ../libcpu/libcpu_bpf.a
 libebl_bpf_pic_a_SOURCES = $(bpf_SRCS)
 am_libebl_bpf_pic_a_OBJECTS = $(bpf_SRCS:.c=.os)
diff --git a/backends/bpf_init.c b/backends/bpf_init.c
index 8ea1bc1a..a046e069 100644
--- a/backends/bpf_init.c
+++ b/backends/bpf_init.c
@@ -53,6 +53,7 @@ bpf_init (Elf *elf __attribute__ ((unused)),
   bpf_init_reloc (eh);
   HOOK (eh, register_info);
   HOOK (eh, disasm);
+  HOOK (eh, reloc_simple_type);
 
   return MODVERSION;
 }
diff --git a/backends/bpf_reloc.def b/backends/bpf_reloc.def
index a410da97..59f519b5 100644
--- a/backends/bpf_reloc.def
+++ b/backends/bpf_reloc.def
@@ -28,4 +28,5 @@
 /*	    NAME,		REL|EXEC|DYN	*/
 
 RELOC_TYPE (NONE,		EXEC|DYN)
-RELOC_TYPE (MAP_FD,		REL|EXEC|DYN)
+RELOC_TYPE (64_64,		REL)
+RELOC_TYPE (64_32,		REL)
diff --git a/backends/bpf_symbol.c b/backends/bpf_symbol.c
new file mode 100644
index 00000000..c9856f26
--- /dev/null
+++ b/backends/bpf_symbol.c
@@ -0,0 +1,54 @@
+/* BPF specific symbolic name handling.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at
+       your option) any later version
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at
+       your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <elf.h>
+#include <stddef.h>
+#include <string.h>
+
+#define BACKEND bpf_
+#include "libebl_CPU.h"
+
+
+/* Check for the simple reloc types.  */
+Elf_Type
+bpf_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), int type)
+{
+  switch (type)
+    {
+    case R_BPF_64_64:
+      return ELF_T_XWORD;
+    case R_BPF_64_32:
+      return ELF_T_WORD;
+    default:
+      return ELF_T_NUM;
+    }
+}
diff --git a/libelf/elf.h b/libelf/elf.h
index f7748983..940e88dd 100644
--- a/libelf/elf.h
+++ b/libelf/elf.h
@@ -3848,7 +3848,8 @@ enum
 /* BPF specific declarations.  */
 
 #define R_BPF_NONE		0	/* No reloc */
-#define R_BPF_MAP_FD		1	/* Map fd to pointer */
+#define R_BPF_64_64		1
+#define R_BPF_64_32		10
 
 /* Imagination Meta specific relocations. */
 
-- 
2.14.3

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] backends,bpf: add proper relocation support
  2018-06-15 22:40 [PATCH] backends,bpf: add proper relocation support Yonghong Song
@ 2018-06-16 14:04 ` Mark Wielaard
  2018-06-16 17:58   ` Yonghong Song
  2018-06-16 18:33   ` Richard Henderson
  0 siblings, 2 replies; 4+ messages in thread
From: Mark Wielaard @ 2018-06-16 14:04 UTC (permalink / raw)
  To: Yonghong Song, elfutils-devel, ast, kafai; +Cc: Richard Henderson

Hi,

I added Richard to the CC, who added the original BPF support.
Who might remember where the R_BPF_MAP_FD comes from (see at the end).

On Fri, 2018-06-15 at 15:40 -0700, Yonghong Song wrote:
> Due to libdw does not have proper BPF relocation support,
> the pahole cannot display filenames correctly for objects
> with default llvm options. So we have to invent
> a special option "llc -march=bpf -mattr=dwarfris" to
> prevent llvm from generating cross-section dwarf relocation
> records (https://reviews.llvm.org/rL326505).
> The pahole related discussion is in linux netdev
> mailing list (http://lists.openwall.net/netdev/2018/06/15/38, etc.)
> 
> We would like to add proper BPF relocation support
> to libdw so eventually we could retire the special llc bpf
> flag "-mattr=dwarfris".

Yes. elfutils/libdwfl only does "simple relocations", but that is all
you need anyway.

Do you have a test file (binary for something simple/trivial generated
by llc -march=bpf that contains at least one reloc). I looked at your
implementation and I am sure it works correctly. But having a small
testfile is always a plus.

> The bpf relocations are defined in
> llvm_repo:include/llvm/BinaryFormat/ELFRelocs/BPF.def:
>   ELF_RELOC(R_BPF_NONE,        0)
>   ELF_RELOC(R_BPF_64_64,       1)
>   ELF_RELOC(R_BPF_64_32,      10)
> 
> Removed the relocation type R_BPF_MAP_FD whoes name does not
> confirm to llvm definition and replaced it with R_BPF_64_64.
> The BPF object is just a relocatible object, not an executable or
> a shared library, so assign ELF type to REL only in bpf_reloc.def.
>
> Tested locally with building pahole with this patch and
> pahole is able to display structures in bpf object file properly.

Patch looks good. Thanks. I'll add a ChangeLog entry because that is
what we still do.

> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  backends/Makefile.am   |  2 +-
>  backends/bpf_init.c    |  1 +
>  backends/bpf_reloc.def |  3 ++-
>  backends/bpf_symbol.c  | 54
> ++++++++++++++++++++++++++++++++++++++++++++++++++
>  libelf/elf.h           |  3 ++-
>  5 files changed, 60 insertions(+), 3 deletions(-)
>  create mode 100644 backends/bpf_symbol.c
> [...]
> diff --git a/libelf/elf.h b/libelf/elf.h
> index f7748983..940e88dd 100644
> --- a/libelf/elf.h
> +++ b/libelf/elf.h
> @@ -3848,7 +3848,8 @@ enum
>  /* BPF specific declarations.  */
>  
>  #define R_BPF_NONE		0	/* No reloc */
> -#define R_BPF_MAP_FD		1	/* Map fd to pointer */
> +#define R_BPF_64_64		1
> +#define R_BPF_64_32		10

We should sync this with glibc. This file really is a copy of elf/elf.h
in glibc, which we periodically sync. It would be good if all projects
agree on the constants.

I would like to understand where the R_BPF_MAP_FD comes from. But I
assume it was a typo for BPF_PSEUDO_MAP_FD from bpf.h (which has the
same constant number 1).

I'll sent a patch to libc-alpha@sourceware.org unless you beat me to
it.

Thanks,

Mark

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] backends,bpf: add proper relocation support
  2018-06-16 14:04 ` Mark Wielaard
@ 2018-06-16 17:58   ` Yonghong Song
  2018-06-16 18:33   ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2018-06-16 17:58 UTC (permalink / raw)
  To: Mark Wielaard, elfutils-devel, ast, kafai; +Cc: Richard Henderson



On 6/16/18 7:04 AM, Mark Wielaard wrote:
> Hi,
> 
> I added Richard to the CC, who added the original BPF support.
> Who might remember where the R_BPF_MAP_FD comes from (see at the end).
> 
> On Fri, 2018-06-15 at 15:40 -0700, Yonghong Song wrote:
>> Due to libdw does not have proper BPF relocation support,
>> the pahole cannot display filenames correctly for objects
>> with default llvm options. So we have to invent
>> a special option "llc -march=bpf -mattr=dwarfris" to
>> prevent llvm from generating cross-section dwarf relocation
>> records (https://urldefense.proofpoint.com/v2/url?u=https-3A__reviews.llvm.org_rL326505&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=DA8e1B5r073vIqRrFz7MRA&m=reaovSjSH40LhrR6vohT7wIrLEVKTNdKYsXxw2wN0hM&s=_1Zapq0e2D1JVvkHQ6roxhWYP307V4tveppbAo2yTpc&e=).
>> The pahole related discussion is in linux netdev
>> mailing list (https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.openwall.net_netdev_2018_06_15_38&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=DA8e1B5r073vIqRrFz7MRA&m=reaovSjSH40LhrR6vohT7wIrLEVKTNdKYsXxw2wN0hM&s=OJqgsG_v099f9jVBOq7h0SpxOSySbSy3Pcf7_XgIRG0&e=, etc.)
>>
>> We would like to add proper BPF relocation support
>> to libdw so eventually we could retire the special llc bpf
>> flag "-mattr=dwarfris".
> 
> Yes. elfutils/libdwfl only does "simple relocations", but that is all
> you need anyway.

That is correct.

> 
> Do you have a test file (binary for something simple/trivial generated
> by llc -march=bpf that contains at least one reloc). I looked at your
> implementation and I am sure it works correctly. But having a small
> testfile is always a plus.

Okay let me create a test case and submit the patch again.

>> The bpf relocations are defined in
>> llvm_repo:include/llvm/BinaryFormat/ELFRelocs/BPF.def:
>>    ELF_RELOC(R_BPF_NONE,        0)
>>    ELF_RELOC(R_BPF_64_64,       1)
>>    ELF_RELOC(R_BPF_64_32,      10)
>>
>> Removed the relocation type R_BPF_MAP_FD whoes name does not
>> confirm to llvm definition and replaced it with R_BPF_64_64.
>> The BPF object is just a relocatible object, not an executable or
>> a shared library, so assign ELF type to REL only in bpf_reloc.def.
>>
>> Tested locally with building pahole with this patch and
>> pahole is able to display structures in bpf object file properly.
> 
> Patch looks good. Thanks. I'll add a ChangeLog entry because that is
> what we still do.

Sure.

> 
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   backends/Makefile.am   |  2 +-
>>   backends/bpf_init.c    |  1 +
>>   backends/bpf_reloc.def |  3 ++-
>>   backends/bpf_symbol.c  | 54
>> ++++++++++++++++++++++++++++++++++++++++++++++++++
>>   libelf/elf.h           |  3 ++-
>>   5 files changed, 60 insertions(+), 3 deletions(-)
>>   create mode 100644 backends/bpf_symbol.c
>> [...]
>> diff --git a/libelf/elf.h b/libelf/elf.h
>> index f7748983..940e88dd 100644
>> --- a/libelf/elf.h
>> +++ b/libelf/elf.h
>> @@ -3848,7 +3848,8 @@ enum
>>   /* BPF specific declarations.  */
>>   
>>   #define R_BPF_NONE		0	/* No reloc */
>> -#define R_BPF_MAP_FD		1	/* Map fd to pointer */
>> +#define R_BPF_64_64		1
>> +#define R_BPF_64_32		10
> 
> We should sync this with glibc. This file really is a copy of elf/elf.h
> in glibc, which we periodically sync. It would be good if all projects
> agree on the constants.
> 
> I would like to understand where the R_BPF_MAP_FD comes from. But I
> assume it was a typo for BPF_PSEUDO_MAP_FD from bpf.h (which has the
> same constant number 1).
> 
> I'll sent a patch to libc-alpha@sourceware.org unless you beat me to
> it.

Please go ahead to submit the necessary elf.h changes to libc. Thanks!

> 
> Thanks,
> 
> Mark
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] backends,bpf: add proper relocation support
  2018-06-16 14:04 ` Mark Wielaard
  2018-06-16 17:58   ` Yonghong Song
@ 2018-06-16 18:33   ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2018-06-16 18:33 UTC (permalink / raw)
  To: Mark Wielaard, Yonghong Song, elfutils-devel, ast, kafai

On 06/16/2018 04:04 AM, Mark Wielaard wrote:
>>  #define R_BPF_NONE		0	/* No reloc */
>> -#define R_BPF_MAP_FD		1	/* Map fd to pointer */
>> +#define R_BPF_64_64		1
>> +#define R_BPF_64_32		10
> 
> We should sync this with glibc. This file really is a copy of elf/elf.h
> in glibc, which we periodically sync. It would be good if all projects
> agree on the constants.
> 
> I would like to understand where the R_BPF_MAP_FD comes from. But I
> assume it was a typo for BPF_PSEUDO_MAP_FD from bpf.h (which has the
> same constant number 1).

If I remember correctly, I was intending this as an extension to allow an
actual map number to be patched in to the byte code stream at load time, where
the relocation would be against a symbol referencing the map descriptor.  The
loader would register the descriptor with the kernel and then patch the byte
code with the runtime map number.

The similarity in ordinal between R_BPF_MAP_FD and BPF_PSEUDO_MAP_FD is
coincidental.

Anyway, I don't think my work progressed quite that far before I left, so the
value is in the end unused.


r~

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-06-16 18:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-15 22:40 [PATCH] backends,bpf: add proper relocation support Yonghong Song
2018-06-16 14:04 ` Mark Wielaard
2018-06-16 17:58   ` Yonghong Song
2018-06-16 18:33   ` Richard Henderson

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 read-only IMAP folder(s) and NNTP newsgroup(s).