From 62ad1bd8cd86af7c5f37e905968ecc9852535fe5 Mon Sep 17 00:00:00 2001 From: Flier Lu Date: Mon, 17 Apr 2023 23:12:43 +0800 Subject: [PATCH] add reloc type and recipe for eBPF (#466) --- elftools/elf/enums.py | 10 ++++++++++ elftools/elf/relocation.py | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/elftools/elf/enums.py b/elftools/elf/enums.py index 6bcd4bb..c068a61 100644 --- a/elftools/elf/enums.py +++ b/elftools/elf/enums.py @@ -820,6 +820,16 @@ ENUM_RELOC_TYPE_x64 = dict( _default_=Pass, ) +ENUM_RELOC_TYPE_BPF = dict( + R_BPF_NONE=0, + R_BPF_64_64=1, + R_BPF_64_ABS64=2, + R_BPF_64_ABS32=3, + R_BPF_64_NODYLD32=4, + R_BPF_64_32=10, + _default_=Pass, +) + # Sunw Syminfo Bound To special values ENUM_SUNW_SYMINFO_BOUNDTO = dict( SYMINFO_BT_SELF=0xffff, diff --git a/elftools/elf/relocation.py b/elftools/elf/relocation.py index 4008e28..0c4e754 100644 --- a/elftools/elf/relocation.py +++ b/elftools/elf/relocation.py @@ -14,7 +14,7 @@ from .sections import Section from .enums import ( ENUM_RELOC_TYPE_i386, ENUM_RELOC_TYPE_x64, ENUM_RELOC_TYPE_MIPS, ENUM_RELOC_TYPE_ARM, ENUM_RELOC_TYPE_AARCH64, ENUM_RELOC_TYPE_PPC64, - ENUM_D_TAG) + ENUM_RELOC_TYPE_BPF, ENUM_D_TAG) from ..construct import Container @@ -253,6 +253,8 @@ class RelocationHandler(object): recipe = self._RELOCATION_RECIPES_AARCH64.get(reloc_type, None) elif self.elffile.get_machine_arch() == '64-bit PowerPC': recipe = self._RELOCATION_RECIPES_PPC64.get(reloc_type, None) + elif self.elffile.get_machine_arch() == 'Linux BPF - in-kernel virtual machine': + recipe = self._RELOCATION_RECIPES_EBPF.get(reloc_type, None) if recipe is None: raise ELFRelocationError( @@ -319,6 +321,9 @@ class RelocationHandler(object): def _arm_reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0): return sym_value // 4 + value - offset // 4 + def _bpf_64_32_reloc_calc_sym_plus_addend(value, sym_value, offset, addend=0): + return (sym_value + addend) // 8 - 1 + _RELOCATION_RECIPES_ARM = { ENUM_RELOC_TYPE_ARM['R_ARM_ABS32']: _RELOCATION_RECIPE_TYPE( bytesize=4, has_addend=False, @@ -381,4 +386,20 @@ class RelocationHandler(object): bytesize=4, has_addend=True, calc_func=_reloc_calc_sym_plus_addend), } + # https://www.kernel.org/doc/html/latest/bpf/llvm_reloc.html#different-relocation-types + _RELOCATION_RECIPES_EBPF = { + ENUM_RELOC_TYPE_BPF['R_BPF_NONE']: _RELOCATION_RECIPE_TYPE( + bytesize=8, has_addend=False, calc_func=_reloc_calc_identity), + ENUM_RELOC_TYPE_BPF['R_BPF_64_64']: _RELOCATION_RECIPE_TYPE( + bytesize=8, has_addend=False, calc_func=_reloc_calc_identity), + ENUM_RELOC_TYPE_BPF['R_BPF_64_32']: _RELOCATION_RECIPE_TYPE( + bytesize=8, has_addend=False, calc_func=_bpf_64_32_reloc_calc_sym_plus_addend), + ENUM_RELOC_TYPE_BPF['R_BPF_64_NODYLD32']: _RELOCATION_RECIPE_TYPE( + bytesize=4, has_addend=False, calc_func=_reloc_calc_identity), + ENUM_RELOC_TYPE_BPF['R_BPF_64_ABS64']: _RELOCATION_RECIPE_TYPE( + bytesize=8, has_addend=False, calc_func=_reloc_calc_identity), + ENUM_RELOC_TYPE_BPF['R_BPF_64_ABS32']: _RELOCATION_RECIPE_TYPE( + bytesize=4, has_addend=False, calc_func=_reloc_calc_identity), + } + -- 2.30.2