From ab76d5ca08c676fd861aa4f49fa63d89024b72ff Mon Sep 17 00:00:00 2001 From: Frederik Sdun Date: Mon, 16 Jul 2018 15:22:55 +0200 Subject: [PATCH] Handle ARM relocations (#121) * relocation: handle ARM binaries * relocation: handle R_ARM_ABS32 for ARM machines * testfiles: add reloc_arm_gcc.o.elf Generated on Ubuntu 14.04 using: arm-linux-gnueabi-gcc-4.7 -c -g -o reloc_armhf_gcc.o.elf hello.c * testfiles: add reloc_armhf_gcc.o.elf Generated on Ubuntu 14.04 using: arm-linux-gnueabihf-gcc-4.7 -c -g -o reloc_armhf_gcc.o.elf hello.c * readelf: print soft-float abi for ARM if EF_ARM_ABI_FLOAT_SOFT in flags * readelf: print hard-float abi for ARM if EF_ARM_ABI_FLOAT_HARD in flags * readelf: print BE8 info for armeb binaries * testfiles: add simple_armhf_gcc.o.elf Generated on Ubuntu 14.04 using: arm-linux-gnueabihf-gcc-4.7 -g -o simple_armhf_gcc.o.elf hello.c * elf: remove unwind from dicts and set ARM_EXIDX description * testfiles: add reloc_armsf_gcc.o.elf as soft float testcase taken from binutils 2.30 * testfiles: add reloc_armeb_gcc.o.elf as arm big endian testcase taken from binutils 2.30 testcase arm-be8 * readelf: print endian info LE8 if flag was set in header flags --- elftools/elf/descriptions.py | 2 +- elftools/elf/relocation.py | 15 ++++++++++++++- scripts/readelf.py | 13 ++++++++++++- test/testfiles_for_readelf/reloc_arm_gcc.o.elf | Bin 0 -> 3096 bytes .../testfiles_for_readelf/reloc_armhf_gcc.o.elf | Bin 0 -> 3112 bytes .../testfiles_for_readelf/reloc_armsf_gcc.o.elf | Bin 0 -> 396 bytes .../simple_armeb_gcc.o.elf | Bin 0 -> 33352 bytes .../simple_armhf_gcc.o.elf | Bin 0 -> 9942 bytes 8 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/testfiles_for_readelf/reloc_arm_gcc.o.elf create mode 100644 test/testfiles_for_readelf/reloc_armhf_gcc.o.elf create mode 100755 test/testfiles_for_readelf/reloc_armsf_gcc.o.elf create mode 100755 test/testfiles_for_readelf/simple_armeb_gcc.o.elf create mode 100644 test/testfiles_for_readelf/simple_armhf_gcc.o.elf diff --git a/elftools/elf/descriptions.py b/elftools/elf/descriptions.py index b76238a..022b074 100644 --- a/elftools/elf/descriptions.py +++ b/elftools/elf/descriptions.py @@ -319,7 +319,7 @@ _DESCR_SH_TYPE = dict( SHT_GNU_verneed='VERNEED', SHT_GNU_versym='VERSYM', SHT_GNU_LIBLIST='GNU_LIBLIST', - SHT_ARM_EXIDX='EXIDX', # binutils calls this EXIDX, not ARM_EXIDX + SHT_ARM_EXIDX='ARM_EXIDX', SHT_ARM_PREEMPTMAP='ARM_PREEMPTMAP', SHT_ARM_ATTRIBUTES='ARM_ATTRIBUTES', SHT_ARM_DEBUGOVERLAY='ARM_DEBUGOVERLAY', diff --git a/elftools/elf/relocation.py b/elftools/elf/relocation.py index 633bbf5..6f2c4b4 100644 --- a/elftools/elf/relocation.py +++ b/elftools/elf/relocation.py @@ -12,7 +12,7 @@ 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_i386, ENUM_RELOC_TYPE_x64, ENUM_RELOC_TYPE_MIPS, ENUM_RELOC_TYPE_ARM) class Relocation(object): @@ -151,6 +151,11 @@ class RelocationHandler(object): raise ELFRelocationError( 'Unexpected RELA relocation for MIPS: %s' % reloc) recipe = self._RELOCATION_RECIPES_MIPS.get(reloc_type, None) + elif self.elffile.get_machine_arch() == 'ARM': + if reloc.is_RELA(): + raise ELFRelocationError( + 'Unexpected RELA relocation for ARM: %s' % reloc) + recipe = self._RELOCATION_RECIPES_ARM.get(reloc_type, None) if recipe is None: raise ELFRelocationError( @@ -214,6 +219,12 @@ class RelocationHandler(object): def _reloc_calc_sym_plus_addend_pcrel(value, sym_value, offset, addend=0): return sym_value + addend - offset + _RELOCATION_RECIPES_ARM = { + ENUM_RELOC_TYPE_ARM['R_ARM_ABS32']: _RELOCATION_RECIPE_TYPE( + bytesize=4, has_addend=False, + calc_func=_reloc_calc_sym_plus_value), + } + # https://dmz-portal.mips.com/wiki/MIPS_relocation_types _RELOCATION_RECIPES_MIPS = { ENUM_RELOC_TYPE_MIPS['R_MIPS_NONE']: _RELOCATION_RECIPE_TYPE( @@ -247,3 +258,5 @@ class RelocationHandler(object): ENUM_RELOC_TYPE_x64['R_X86_64_32S']: _RELOCATION_RECIPE_TYPE( bytesize=4, has_addend=True, calc_func=_reloc_calc_sym_plus_addend), } + + diff --git a/scripts/readelf.py b/scripts/readelf.py index d65c385..8535119 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -131,8 +131,19 @@ class ReadElf(object): flags &= ~E_FLAGS.EF_ARM_RELEXEC if eabi == E_FLAGS.EF_ARM_EABI_VER5: + EF_ARM_KNOWN_FLAGS = E_FLAGS.EF_ARM_ABI_FLOAT_SOFT|E_FLAGS.EF_ARM_ABI_FLOAT_HARD|E_FLAGS.EF_ARM_LE8|E_FLAGS.EF_ARM_BE8 description += ', Version5 EABI' - if flags: + if flags & E_FLAGS.EF_ARM_ABI_FLOAT_SOFT: + description += ", soft-float ABI" + elif flags & E_FLAGS.EF_ARM_ABI_FLOAT_HARD: + description += ", hard-float ABI" + + if flags & E_FLAGS.EF_ARM_BE8: + description += ", BE8" + elif flags & E_FLAGS.EF_ARM_LE8: + description += ", LE8" + + if flags & ~EF_ARM_KNOWN_FLAGS: description += ', ' else: description += ', ' diff --git a/test/testfiles_for_readelf/reloc_arm_gcc.o.elf b/test/testfiles_for_readelf/reloc_arm_gcc.o.elf new file mode 100644 index 0000000000000000000000000000000000000000..4d8e4e9f0166ef2928ea5a2d24bd5b101de810db GIT binary patch literal 3096 zcma)8OK%%h6ux)HcASi3@@kVlnKo(F0v#u93RQuYCO}d(v=X!+V3CgPvE$b9MB_0u zK!t=@AXPk8sGEvPut2bYB@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6Qca{MT%Y=39)^Z<=lJL;h1dbnh`sci8YF7)5$r zAeUl9Sq}aYD{*(s@IXAA57vyv`;-ss-_C>G#z?ONWsH8`j2g{R_JP(MTLJ6othGHp zmOI~)@{GfnahZ20M^{HYeGBM#s8mSimguJJ$fzA16_QE#*9ytxN}*1uS3o$CkWAVt zKNK37GkN#A=E!`C?r-j61g~Y>E8y`X06Y2=S>n8=nTz(8+=u>(FEq%it}N1mPh(%l z@Vh0ylk&^DKJ*7159^fqyMZb#G9vqQF}O$YLju|05}?CNBmbD z*vkr!D11iY8HLX&d{yBUg)N1Bg+qm(E5tj5^?y?Mi^5+Onp`N@ClnTdyw6#MbHEJI qlCqxw@`0RF@+BZA`I?ejKrTGG%HV@Xy%7&*1jJbyTpTW$l>Y%x0YYm4 literal 0 HcmV?d00001 diff --git a/test/testfiles_for_readelf/reloc_armsf_gcc.o.elf b/test/testfiles_for_readelf/reloc_armsf_gcc.o.elf new file mode 100755 index 0000000000000000000000000000000000000000..04c3849f5fae36659048bc5844570babbe6360db GIT binary patch literal 396 zcmb<-^>JflWMqH=CI$@#Fs}h3as)&%F|e9|xf%?t3@i+e8bD5BYGP6*1EU;}$?BY6 zRFYbu>uAZq#>MW)!N|$P#jM5Xf-n`V8G~R3+JtPE5QrCFT#{H+!oZ+cT$x*vm;|It ziXe0bjO7^QtCv_(Qk0ogT9R6fVJuDoX`l=<1498!0mx69Fb)Dy4LoOP0`teZ^NA<9K*0>j4NiC3CB(67#La90F1Wq?PayN8A>_i&FhsXt? zb{9{3&#zo}?4;4UzG?){#r<&Pxyv6Q7L`i)mHltRv uvOBD-n|oHtu`XWpv3nM?ZWLxZf8V?2rar#>eJi>#H}_Q2eBy_+r*q#TOh9}9 literal 0 HcmV?d00001 diff --git a/test/testfiles_for_readelf/simple_armhf_gcc.o.elf b/test/testfiles_for_readelf/simple_armhf_gcc.o.elf new file mode 100644 index 0000000000000000000000000000000000000000..a5a02335fee5b5ff93ff151b9bb214965d8d4d98 GIT binary pat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