Skip to content

Commit

Permalink
Minor fixes while adding type annotaions (#600)
Browse files Browse the repository at this point in the history
* enums: Remove duplicate definition of ENUM_SUNW_SYMINFO_BOUNDTO

- 5db2966 introduced it first as `ENUM_SYMINFO_BOUNDTO`
- 594dc72 renamed it to `ENUM_SUNW_SYMINFO_BOUNDTO`
- 7b24670 added it a 2nd time

Signed-off-by: Philipp Hahn <phahn-oss@avm.de>

* relocation: Remove helper functions from class

A method expects `self` as the first argument; they are all functions.

Normally you would mark them as `@staticmethod`, but they are all
de-referenced already during class definition, at which point they are
actually still functions: they only become methods after the `class`
scope is finished.

```console
$ test/all_tests.py
E........EE...................E.....E.....E.....................................................................
======================================================================
ERROR: test_reloc (test_arm_call_reloc.TestARMRElocation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "…/pyelftools/test/test_arm_call_reloc.py", line 41, in test_reloc
    self.assertEqual(do_relocation(rel_elf),
  File "…/pyelftools/test/test_arm_call_reloc.py", line 28, in do_relocation
    rh.apply_section_relocations(stream, rel)
  File "…/pyelftools/elftools/elf/relocation.py", line 238, in apply_section_relocations
    self._do_apply_relocation(stream, reloc, symtab)
  File "…/pyelftools/elftools/elf/relocation.py", line 319, in _do_apply_relocation
    relocated_value = recipe.calc_func(
TypeError: 'staticmethod' object is not callable
```

For clarity move those functions before class `RelocationHandler`.

Signed-off-by: Philipp Hahn <phahn-oss@avm.de>

---------

Signed-off-by: Philipp Hahn <phahn-oss@avm.de>
  • Loading branch information
pmhahn authored Feb 25, 2025
1 parent f92bb71 commit d34b527
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
9 changes: 0 additions & 9 deletions elftools/elf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,6 @@
_default_=Pass,
)

# Sunw Syminfo Bound To special values
ENUM_SUNW_SYMINFO_BOUNDTO = dict(
SYMINFO_BT_SELF=0xffff,
SYMINFO_BT_PARENT=0xfffe,
SYMINFO_BT_NONE=0xfffd,
SYMINFO_BT_EXTERN=0xfffc,
_default_=Pass,
)

# PT_NOTE section types for all ELF types except ET_CORE
ENUM_NOTE_N_TYPE = dict(
NT_GNU_ABI_TAG=1,
Expand Down
56 changes: 32 additions & 24 deletions elftools/elf/relocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,38 @@ def __init__(self, header, name, elffile):
self['sh_offset'], self['sh_size'], self['sh_entsize'])


def _reloc_calc_identity(value, sym_value, offset, addend=0):
return value


def _reloc_calc_sym_plus_value(value, sym_value, offset, addend=0):
return sym_value + value + addend


def _reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0):
return sym_value + value - offset


def _reloc_calc_sym_plus_addend(value, sym_value, offset, addend=0):
return sym_value + addend


def _reloc_calc_sym_plus_addend_pcrel(value, sym_value, offset, addend=0):
return sym_value + addend - offset


def _reloc_calc_value_minus_sym_addend(value, sym_value, offset, addend=0):
return value - sym_value - addend


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


class RelocationHandler(object):
""" Handles the logic of relocations in ELF files.
"""
Expand Down Expand Up @@ -340,30 +372,6 @@ def _do_apply_relocation(self, stream, reloc, symtab):
_RELOCATION_RECIPE_TYPE = namedtuple('_RELOCATION_RECIPE_TYPE',
'bytesize has_addend calc_func')

def _reloc_calc_identity(value, sym_value, offset, addend=0):
return value

def _reloc_calc_sym_plus_value(value, sym_value, offset, addend=0):
return sym_value + value + addend

def _reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0):
return sym_value + value - offset

def _reloc_calc_sym_plus_addend(value, sym_value, offset, addend=0):
return sym_value + addend

def _reloc_calc_sym_plus_addend_pcrel(value, sym_value, offset, addend=0):
return sym_value + addend - offset

def _reloc_calc_value_minus_sym_addend(value, sym_value, offset, addend=0):
return value - sym_value - addend

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,
Expand Down

0 comments on commit d34b527

Please sign in to comment.