Skip to content

Commit

Permalink
initial support for aarch64 little endian
Browse files Browse the repository at this point in the history
See also:
https://static.docs.arm.com/ihi0056/b/IHI0056B_aaelf64.pdf
for relocation types and
https://developer.arm.com/docs/ihi0057/c/dwarf-for-the-arm-64-bit-architecture-aarch64-abi-2018q4
for DWARF register names.

Issue eliben#317
Link: ClangBuiltLinux/frame-larger-than#4
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
  • Loading branch information
nickdesaulniers committed Jun 6, 2020
1 parent 1ed78f5 commit 4a842dc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions elftools/dwarf/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def describe_reg_name(regnum, machine_arch=None, default=True):
return _REG_NAMES_x86[regnum]
elif machine_arch == 'x64':
return _REG_NAMES_x64[regnum]
elif machine_arch == 'AArch64':
return _REG_NAMES_AArch64[regnum]
elif default:
return 'r%s' % regnum
else:
Expand Down Expand Up @@ -528,6 +530,14 @@ def _import_extra(attr, die, section_offset):
'mxcsr', 'fcw', 'fsw'
]

# https://developer.arm.com/docs/ihi0057/c/dwarf-for-the-arm-64-bit-architecture-aarch64-abi-2018q4#id24
_REG_NAMES_AArch64 = [
'x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9',
'x10', 'x11', 'x12', 'x13', 'x14', 'x15', 'x16', 'x17', 'x18', 'x19',
'x20', 'x21', 'x22', 'x23', 'x24', 'x25', 'x26', 'x27', 'x28', 'x29',
'x30', 'sp'
]


class ExprDumper(object):
""" A dumper for DWARF expressions that dumps a textual
Expand Down
15 changes: 14 additions & 1 deletion elftools/elf/relocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from ..common.utils import elf_assert, struct_parse
from .sections import Section
from .enums import (
ENUM_RELOC_TYPE_i386, ENUM_RELOC_TYPE_x64, ENUM_RELOC_TYPE_MIPS, ENUM_RELOC_TYPE_ARM, ENUM_D_TAG)
ENUM_RELOC_TYPE_i386, ENUM_RELOC_TYPE_x64, ENUM_RELOC_TYPE_MIPS,
ENUM_RELOC_TYPE_ARM, ENUM_RELOC_TYPE_AARCH64, ENUM_D_TAG)


class Relocation(object):
Expand Down Expand Up @@ -172,6 +173,8 @@ def _do_apply_relocation(self, stream, reloc, symtab):
raise ELFRelocationError(
'Unexpected RELA relocation for ARM: %s' % reloc)
recipe = self._RELOCATION_RECIPES_ARM.get(reloc_type, None)
elif self.elffile.get_machine_arch() == 'AArch64':
recipe = self._RELOCATION_RECIPES_AARCH64.get(reloc_type, None)

if recipe is None:
raise ELFRelocationError(
Expand Down Expand Up @@ -247,6 +250,16 @@ def _arm_reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0):
calc_func=_arm_reloc_calc_sym_plus_value_pcrel),
}

_RELOCATION_RECIPES_AARCH64 = {
ENUM_RELOC_TYPE_AARCH64['R_AARCH64_ABS64']: _RELOCATION_RECIPE_TYPE(
bytesize=8, has_addend=True, calc_func=_reloc_calc_sym_plus_addend),
ENUM_RELOC_TYPE_AARCH64['R_AARCH64_ABS32']: _RELOCATION_RECIPE_TYPE(
bytesize=4, has_addend=True, calc_func=_reloc_calc_sym_plus_addend),
ENUM_RELOC_TYPE_AARCH64['R_AARCH64_PREL32']: _RELOCATION_RECIPE_TYPE(
bytesize=4, has_addend=True,
calc_func=_reloc_calc_sym_plus_addend_pcrel),
}

# https://dmz-portal.mips.com/wiki/MIPS_relocation_types
_RELOCATION_RECIPES_MIPS = {
ENUM_RELOC_TYPE_MIPS['R_MIPS_NONE']: _RELOCATION_RECIPE_TYPE(
Expand Down
Binary file not shown.
16 changes: 16 additions & 0 deletions test/testfiles_for_readelf/aarch64-relocs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This source was compiled for aarch64 (little endian).
aarch64-linux-gnu-gcc -c -o aarch64-relocs-le.o.elf aarch64-relocs.c -g
*/

extern struct {
int i, j;
} data;

extern int bar (void);

int
foo (int a)
{
data.i += a;
data.j -= bar();
}

0 comments on commit 4a842dc

Please sign in to comment.