From 3d91dbeef0731d14fc777f6a0bea6d653c83b28d Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Tue, 9 Jun 2020 05:53:08 -0700 Subject: [PATCH] initial support for aarch64 little endian (#318) 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 #317 Link: https://github.com/ClangBuiltLinux/frame-larger-than/issues/4 Signed-off-by: Nick Desaulniers --- elftools/dwarf/descriptions.py | 10 ++++++++++ elftools/elf/relocation.py | 15 ++++++++++++++- .../aarch64-relocs-le.o.elf | Bin 0 -> 3232 bytes test/testfiles_for_readelf/aarch64-relocs.c | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/testfiles_for_readelf/aarch64-relocs-le.o.elf create mode 100644 test/testfiles_for_readelf/aarch64-relocs.c diff --git a/elftools/dwarf/descriptions.py b/elftools/dwarf/descriptions.py index c3fa078..e5c8c26 100644 --- a/elftools/dwarf/descriptions.py +++ b/elftools/dwarf/descriptions.py @@ -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: @@ -528,6 +530,14 @@ _REG_NAMES_x64 = [ '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 diff --git a/elftools/elf/relocation.py b/elftools/elf/relocation.py index be03165..427a56f 100644 --- a/elftools/elf/relocation.py +++ b/elftools/elf/relocation.py @@ -12,7 +12,8 @@ from ..common.exceptions import ELFRelocationError 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): @@ -172,6 +173,8 @@ class RelocationHandler(object): 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( @@ -247,6 +250,16 @@ class RelocationHandler(object): 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( diff --git a/test/testfiles_for_readelf/aarch64-relocs-le.o.elf b/test/testfiles_for_readelf/aarch64-relocs-le.o.elf new file mode 100644 index 0000000000000000000000000000000000000000..3f74d2292a4ba36d4e2339e49a7d58f6ff54bca4 GIT binary patch literal 3232 zcmbtW&u<$=6n^7b$8KD^O$?$@5ZOv8Z7RHp(_@l=9~Au_vY7I z-+S%N>p8|i34`z9aHLRxZwiNcL#PdS2CA@s_lIxyIo#iyf*piE2Wb4ops}+z)z~=+ zP&&lK{1?A+IIA$Q8aoWw8*0of*qO5V)BU?owjcA=pFYJ23x5Dbk4YJ$G849hqbQf`VZp^YLC)R(2svZy zRHa(tmC4E&8!sGx4j@-$rP9UH%cTn?R>f##;Kg)!v$}*Ux2B;~dbPxh0Qm*r6*YHx z;*|-E?y$=fig0@3W|iDFPJDS4=L&hvRqh1l^%DMalpvrtO;o-bB-C5jayX5x#s038 zfuZ04e+*nVYOOCW)FQ7NwqmCx2zdra(g4IPv;63#eEF07sFPq^7A=sJnFnSdj%iqni^a0J@+nj34Y ze$WYVPUvp48F}ybO}mjBtYKx+DP2GC6b0je(+Yb%4?jpw5GJ03Ww90~ZtFcs^43>7 zk=ygq`I*O1;tTcCsj{hl(<>( z9!j3skq=$y{gBW9%BOo_`Xv7<{%17M#I&vBT-N$`<@z5>F0G#+bNyElGkp?eMuPWf zpowWq@5aQ)d|QHZ8kbbl_HD$n@_!&NgyK{Fl)IU~pWzcB*p-29(?An5ejDeq;{Pb) zt9wH6sV7sb5r~jZz*X@3jd>td* zw0$Ho)BJY{EEx{e`W5^YDp~utm$85Jl~sJTuau7&n|z;<59&LaF#XTS;!_Pb-yZRI bDJhgwX?M_?>!)+g;{Pf0-#%cbipl>M5~=6R literal 0 HcmV?d00001 diff --git a/test/testfiles_for_readelf/aarch64-relocs.c b/test/testfiles_for_readelf/aarch64-relocs.c new file mode 100644 index 0000000..c78a562 --- /dev/null +++ b/test/testfiles_for_readelf/aarch64-relocs.c @@ -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(); +} -- 2.30.2