diff --git a/0001-Call-relocation-for-ARM-V3-194.patch b/0001-Call-relocation-for-ARM-V3-194.patch new file mode 100644 index 0000000000000000000000000000000000000000..ed162bbff66c7fcb557c592f7ef854f953dc66fe --- /dev/null +++ b/0001-Call-relocation-for-ARM-V3-194.patch @@ -0,0 +1,214 @@ +From f52f66634d1f9419239ce80ce455fea43ecc6aa7 Mon Sep 17 00:00:00 2001 +From: Dmitry Koltunov +Date: Wed, 5 Sep 2018 15:25:28 +0300 +Subject: [PATCH] Call relocation for ARM V3 (#194) + +* Add support for 'R_ARM_CALL' relocation type + +* Add test script and test files to verify support for 'R_ARM_CALL' + +Signed-off-by: Koltunov Dmitry +--- + elftools/elf/relocation.py | 6 ++ + test/run_arm_reloc_call_test.py | 62 ++++++++++++++++++ + .../reloc_simple_arm_llvm.o | Bin 0 -> 1488 bytes + test/testfiles_for_unittests/simple.c | 41 ++++++++++++ + .../simple_arm_llvm.elf | Bin 0 -> 34272 bytes + 5 files changed, 109 insertions(+) + create mode 100755 test/run_arm_reloc_call_test.py + create mode 100644 test/testfiles_for_unittests/reloc_simple_arm_llvm.o + create mode 100644 test/testfiles_for_unittests/simple.c + create mode 100755 test/testfiles_for_unittests/simple_arm_llvm.elf + +diff --git a/elftools/elf/relocation.py b/elftools/elf/relocation.py +index 6f2c4b4..59b6750 100644 +--- a/elftools/elf/relocation.py ++++ b/elftools/elf/relocation.py +@@ -219,10 +219,16 @@ class RelocationHandler(object): + def _reloc_calc_sym_plus_addend_pcrel(value, sym_value, offset, addend=0): + return sym_value + addend - offset + ++ def _arm_reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0): ++ return sym_value / 4 + value - offset / 4 ++ + _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), ++ ENUM_RELOC_TYPE_ARM['R_ARM_CALL']: _RELOCATION_RECIPE_TYPE( ++ bytesize=4, has_addend=False, ++ calc_func=_arm_reloc_calc_sym_plus_value_pcrel), + } + + # https://dmz-portal.mips.com/wiki/MIPS_relocation_types +diff --git a/test/run_arm_reloc_call_test.py b/test/run_arm_reloc_call_test.py +new file mode 100755 +index 0000000..261949d +--- /dev/null ++++ b/test/run_arm_reloc_call_test.py +@@ -0,0 +1,62 @@ ++#!/usr/bin/env python ++#------------------------------------------------------------------------------- ++# test/run_arm_reloc_call_test.py ++# ++# Test 'R_ARM_CALL' relocation type support. ++# Compare the '.text' section data of ELF file that relocated by elftools ++# and ELF file that relocated by linker. ++# ++# Dmitry Koltunov (koltunov@ispras.ru) ++#------------------------------------------------------------------------------- ++from os.path import ( ++ join, ++ dirname ++) ++from sys import ( ++ exit ++) ++ ++from elftools.common.py3compat import ( ++ BytesIO ++) ++from elftools.elf.elffile import ( ++ ELFFile ++) ++from elftools.elf.relocation import ( ++ RelocationHandler ++) ++ ++ ++def do_relocation(rel_elf): ++ data = rel_elf.get_section_by_name('.text').data() ++ rh = RelocationHandler(rel_elf) ++ ++ stream = BytesIO() ++ stream.write(data) ++ ++ rel = rel_elf.get_section_by_name('.rel.text') ++ rh.apply_section_relocations(stream, rel) ++ ++ stream.seek(0) ++ data = stream.readlines() ++ ++ return data ++ ++ ++def main(): ++ test_dir = join(dirname(__file__) or '.', 'testfiles_for_unittests') ++ with open(join(test_dir, 'reloc_simple_arm_llvm.o'), 'rb') as rel_f, \ ++ open(join(test_dir, 'simple_arm_llvm.elf'), 'rb') as f: ++ rel_elf = ELFFile(rel_f) ++ elf = ELFFile(f) ++ ++ # Comparison of '.text' section data ++ if do_relocation(rel_elf).pop() != elf.get_section_by_name('.text').data(): ++ print 'FAIL' ++ return 1 ++ print 'OK' ++ return 0 ++ ++ ++if __name__ == '__main__': ++ exit(main()) +diff --git a/test/testfiles_for_unittests/reloc_simple_arm_llvm.o b/test/testfiles_for_unittests/reloc_simple_arm_llvm.o +new file mode 100644 +index 0000000000000000000000000000000000000000..a1bfbaa893443246d3da536c7f6685252c51827b +GIT binary patch +literal 1488 +zcmb7Ezi-n}5Wd&CC6ItjRYYm4VgZ5_sgCo*Ns1U+@vB42075Jbb!?|Z3tN17p+35{LzMMEEYTOBx{rCp~}neRsLL_nw`nw-#>;At*3G +zQTok@=A=$!DAj=|oj_FHT{;ko+a%3xMyTP11hY!ZbrQ`nPe0yyDCl&~jbuAlApR|!0%K*Rh3 +z^#)|O*{g^8b@1>m_Pp@!Gw-RlACcSQULR$*^=5Lj#XTG6-If^NY(?OAdrx?BU(G=8 +zpCQouX9#quua1nfKQbHcitkYKaoNY0K1v97?$|6E+1X^)Or~|y)HN%W&u0w7GP7nTJ5BR(jBVIPu0Y}n+#3m1 +zo26k95~t|27?DQB8F6-SOq>(r15?8n#O2=C7x_dFJJ0SzqVH{|hZ7APVT0iJeTC0& +z1Uw1kGu!rg7PySX-vr0m=iDHF5k8-F6^rBVfs^=-J~196`o=g+bQ3jMf5ZsgVTAsI +zF@z-8)j=Y58D$^Gd{lfSwPqL9`Fl&Myt-j;-Fg`(UQ-80Hf~Mzv^@VOdl!l;El+)mL+*)}5MTFm99>FBvLt_n*x| +zCbn>BA3!z^ng2e5k78l_gk$I!pRvn=Z{5G6?pYiUM4*Lwd@ro=jdC8J+YlChUVRQ^ +z+XIOLgIf>kaf{x1+o;ERtZ`0IZyY}72Alr@@)|raj`6zgW1znZ8u|j~uwBGLt7?bf +Y!Fh>*0E4_D{N56KAX|Psm`?`(1@iAQlK=n! + +literal 0 +HcmV?d00001 + +diff --git a/test/testfiles_for_unittests/simple.c b/test/testfiles_for_unittests/simple.c +new file mode 100644 +index 0000000..4ea1c67 +--- /dev/null ++++ b/test/testfiles_for_unittests/simple.c +@@ -0,0 +1,41 @@ ++/* Generated by compiling with any LLVM version and ++** with any GNU Arm Embedded Toolchain version. ++** LLVM 3.8.0/5.0.0 and GNU Arm Embedded Toolchain 2.26 is fine. ++** ++** clang -O0 --target=arm-none-eabi -emit-llvm -c simple.c -o simple.bc ++** llc -O0 -march=arm -filetype=obj simple.bc -o reloc_simple_arm_llvm.o ++** arm-none-eabi-ld -e main reloc_simple_arm_llvm.o -o simple_arm_llvm.elf ++** ++** reloc_simple_arm_llvm.o is ELF file that needs call relocation. ++** ++** simple_arm_llvm.elf is a relocated ELF file. ++*/ ++ ++int add(int a, int b) { ++ return a + b; ++} ++ ++int sub(int a, int b) { ++ return a - b; ++} ++ ++int mul(int a, int b) { ++ return a * b; ++} ++ ++void triple(int a, int b) { ++ add(a, b); ++ sub(a, b); ++ mul(a, b); ++} ++ ++int main(void) { ++ int a = 0xABCD, b = 0x1234; ++ ++ add(a, b); ++ sub(a, b); ++ mul(a, b); ++ triple(a, b); ++ ++ return 0; ++} +diff --git a/test/testfiles_for_unittests/simple_arm_llvm.elf b/test/testfiles_for_unittests/simple_arm_llvm.elf +new file mode 100755 +index 0000000000000000000000000000000000000000..f1cfbb8d6593d38281a49dc2cb8e2ccacb42ed8e +GIT binary patch +literal 34272 +zcmeIw&u`mg7zgm@P27^vAiN5+A4S8U0h<8FY2qe>3lkdbupU4hkT|4{?Q{`I8YS*j +zPD{33BXQukNxn>~gpiOpaX{i90QJC$Q?tkth!g+7!t>a%aH9&rf!p_$UcY{RP6#+G2A3#5m+$nJaNt +z(MM6>ooHADtGw!`6_A4f1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa +z0SG_<0uX=z1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SG_<0uX=z +z1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SG_<0uX=z1Rwwb2tWV= +z5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SNrh0{YSR6J>-`vchSRCQ(73L`8bU +z>!V1~=vXr*5m}Q+KbkzbG%rp6x&OWY)c>U_9v3N`E&z|ACOg?*t?=yKhOM2!XIj^&Q +z_G~*(=XVu<18&FZ?HYId!LYs8Gi=i`E#u1VAn3nSDs?({y5`*{ik6)lIKfwyJ); +zQg?qjS8@@mC|FKCkQCe_^Tq2FJ3W#f#M6vsL#BIg8r!sdS95962omzNFhlYenl`bWtOlfP9m +zER~E`c;R&a9S8>BMIq| +yah7NHYyI>A(T%v0T+%*SvtRj$_uJv*Gn|)m%K3DjXLx4!JHAhJz`1m!`~3yEO<^Md + +literal 0 +HcmV?d00001 + +-- +2.42.0.windows.2 + diff --git a/0001-Fix-division-in-calculation-function-for-R_ARM_CALL-.patch b/0001-Fix-division-in-calculation-function-for-R_ARM_CALL-.patch new file mode 100644 index 0000000000000000000000000000000000000000..609a40f84d68971389636df37d52b8581b43e376 --- /dev/null +++ b/0001-Fix-division-in-calculation-function-for-R_ARM_CALL-.patch @@ -0,0 +1,25 @@ +From f61368970b11002d0a17e57094e90abd66055e27 Mon Sep 17 00:00:00 2001 +From: Dmitry Koltunov +Date: Thu, 6 Sep 2018 01:00:34 +0300 +Subject: [PATCH] Fix division in calculation function for 'R_ARM_CALL' (#196) + +--- + elftools/elf/relocation.py | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/elftools/elf/relocation.py b/elftools/elf/relocation.py +index 59b6750..e245df4 100644 +--- a/elftools/elf/relocation.py ++++ b/elftools/elf/relocation.py +@@ -220,7 +220,7 @@ class RelocationHandler(object): + return sym_value + addend - offset + + def _arm_reloc_calc_sym_plus_value_pcrel(value, sym_value, offset, addend=0): +- return sym_value / 4 + value - offset / 4 ++ return sym_value // 4 + value - offset // 4 + + _RELOCATION_RECIPES_ARM = { + ENUM_RELOC_TYPE_ARM['R_ARM_ABS32']: _RELOCATION_RECIPE_TYPE( +-- +2.42.0.windows.2 + diff --git a/0001-Redo-the-new-arm-reloc-test-as-a-proper-unit-test-al.patch b/0001-Redo-the-new-arm-reloc-test-as-a-proper-unit-test-al.patch new file mode 100644 index 0000000000000000000000000000000000000000..b0d84bd739f4bf2d5348b7759b96faf035c40ca6 --- /dev/null +++ b/0001-Redo-the-new-arm-reloc-test-as-a-proper-unit-test-al.patch @@ -0,0 +1,135 @@ +From 117bb6878379e4e97582748b3de911461d0e8017 Mon Sep 17 00:00:00 2001 +From: Eli Bendersky +Date: Wed, 5 Sep 2018 05:29:44 -0700 +Subject: [PATCH] Redo the new arm reloc test as a proper unit test, aligned + with other tests + +--- + test/run_arm_reloc_call_test.py | 62 --------------------------------- + test_arm_call_reloc.py | 45 ++++++++++++++++++++++++ + 2 files changed, 45 insertions(+), 62 deletions(-) + delete mode 100755 test/run_arm_reloc_call_test.py + create mode 100755 test_arm_call_reloc.py + +diff --git a/test/run_arm_reloc_call_test.py b/test/run_arm_reloc_call_test.py +deleted file mode 100755 +index 261949d..0000000 +--- a/test/run_arm_reloc_call_test.py ++++ /dev/null +@@ -1,62 +0,0 @@ +-#!/usr/bin/env python +-#------------------------------------------------------------------------------- +-# test/run_arm_reloc_call_test.py +-# +-# Test 'R_ARM_CALL' relocation type support. +-# Compare the '.text' section data of ELF file that relocated by elftools +-# and ELF file that relocated by linker. +-# +-# Dmitry Koltunov (koltunov@ispras.ru) +-#------------------------------------------------------------------------------- +-from os.path import ( +- join, +- dirname +-) +-from sys import ( +- exit +-) +- +-from elftools.common.py3compat import ( +- BytesIO +-) +-from elftools.elf.elffile import ( +- ELFFile +-) +-from elftools.elf.relocation import ( +- RelocationHandler +-) +- +- +-def do_relocation(rel_elf): +- data = rel_elf.get_section_by_name('.text').data() +- rh = RelocationHandler(rel_elf) +- +- stream = BytesIO() +- stream.write(data) +- +- rel = rel_elf.get_section_by_name('.rel.text') +- rh.apply_section_relocations(stream, rel) +- +- stream.seek(0) +- data = stream.readlines() +- +- return data +- +- +-def main(): +- test_dir = join(dirname(__file__) or '.', 'testfiles_for_unittests') +- with open(join(test_dir, 'reloc_simple_arm_llvm.o'), 'rb') as rel_f, \ +- open(join(test_dir, 'simple_arm_llvm.elf'), 'rb') as f: +- rel_elf = ELFFile(rel_f) +- elf = ELFFile(f) +- +- # Comparison of '.text' section data +- if do_relocation(rel_elf).pop() != elf.get_section_by_name('.text').data(): +- print 'FAIL' +- return 1 +- print 'OK' +- return 0 +- +- +-if __name__ == '__main__': +- exit(main()) +diff --git a/test_arm_call_reloc.py b/test_arm_call_reloc.py +new file mode 100755 +index 0000000..685cd71 +--- /dev/null ++++ b/test_arm_call_reloc.py +@@ -0,0 +1,45 @@ ++#------------------------------------------------------------------------------- ++# elftools tests ++# ++# Test 'R_ARM_CALL' relocation type support. ++# Compare the '.text' section data of ELF file that was relocated by elftools ++# with an ELF file that was relocated by linker. ++# ++# Dmitry Koltunov (koltunov@ispras.ru) ++# This code is in the public domain ++#------------------------------------------------------------------------------- ++import os ++import sys ++ ++from elftools.common.py3compat import BytesIO ++from elftools.elf.elffile import ELFFile ++from elftools.elf.relocation import RelocationHandler ++ ++ ++def do_relocation(rel_elf): ++ data = rel_elf.get_section_by_name('.text').data() ++ rh = RelocationHandler(rel_elf) ++ ++ stream = BytesIO() ++ stream.write(data) ++ ++ rel = rel_elf.get_section_by_name('.rel.text') ++ rh.apply_section_relocations(stream, rel) ++ ++ stream.seek(0) ++ data = stream.readlines() ++ ++ return data ++ ++ ++class TestARMRElocation(unittest.TestCase): ++ def test_reloc(self): ++ test_dir = os.path.joinjoin('test', 'testfiles_for_unittests') ++ with open(join(test_dir, 'reloc_simple_arm_llvm.o'), 'rb') as rel_f, \ ++ open(join(test_dir, 'simple_arm_llvm.elf'), 'rb') as f: ++ rel_elf = ELFFile(rel_f) ++ elf = ELFFile(f) ++ ++ # Comparison of '.text' section data ++ self.assertEquals(do_relocation(rel_elf).pop(), ++ elf.get_section_by_name('.text').data()) +-- +2.42.0.windows.2 + diff --git a/0001-Rename-input-files-for-arm-relocation-test-to-be-mor.patch b/0001-Rename-input-files-for-arm-relocation-test-to-be-mor.patch new file mode 100644 index 0000000000000000000000000000000000000000..cd857e86051d4420c20411dc8b7eadd5b6530dc7 --- /dev/null +++ b/0001-Rename-input-files-for-arm-relocation-test-to-be-mor.patch @@ -0,0 +1,46 @@ +From 8e8e8b86fe1dd438072313508dc1e0ec20f41caf Mon Sep 17 00:00:00 2001 +From: Eli Bendersky +Date: Wed, 5 Sep 2018 05:32:04 -0700 +Subject: [PATCH] Rename input files for arm relocation test to be more + consistent + +--- + ...{simple_arm_llvm.elf => arm_reloc_relocated.elf} | Bin + .../{simple.c => arm_reloc_source.c} | 0 + ...oc_simple_arm_llvm.o => arm_reloc_unrelocated.o} | Bin + test_arm_call_reloc.py | 4 ++-- + 4 files changed, 2 insertions(+), 2 deletions(-) + rename test/testfiles_for_unittests/{simple_arm_llvm.elf => arm_reloc_relocated.elf} (100%) + rename test/testfiles_for_unittests/{simple.c => arm_reloc_source.c} (100%) + rename test/testfiles_for_unittests/{reloc_simple_arm_llvm.o => arm_reloc_unrelocated.o} (100%) + +diff --git a/test/testfiles_for_unittests/simple_arm_llvm.elf b/test/testfiles_for_unittests/arm_reloc_relocated.elf +similarity index 100% +rename from test/testfiles_for_unittests/simple_arm_llvm.elf +rename to test/testfiles_for_unittests/arm_reloc_relocated.elf +diff --git a/test/testfiles_for_unittests/simple.c b/test/testfiles_for_unittests/arm_reloc_source.c +similarity index 100% +rename from test/testfiles_for_unittests/simple.c +rename to test/testfiles_for_unittests/arm_reloc_source.c +diff --git a/test/testfiles_for_unittests/reloc_simple_arm_llvm.o b/test/testfiles_for_unittests/arm_reloc_unrelocated.o +similarity index 100% +rename from test/testfiles_for_unittests/reloc_simple_arm_llvm.o +rename to test/testfiles_for_unittests/arm_reloc_unrelocated.o +diff --git a/test_arm_call_reloc.py b/test_arm_call_reloc.py +index 685cd71..cb52419 100755 +--- a/test_arm_call_reloc.py ++++ b/test_arm_call_reloc.py +@@ -35,8 +35,8 @@ def do_relocation(rel_elf): + class TestARMRElocation(unittest.TestCase): + def test_reloc(self): + test_dir = os.path.joinjoin('test', 'testfiles_for_unittests') +- with open(join(test_dir, 'reloc_simple_arm_llvm.o'), 'rb') as rel_f, \ +- open(join(test_dir, 'simple_arm_llvm.elf'), 'rb') as f: ++ with open(join(test_dir, 'arm_reloc_unrelocated.o'), 'rb') as rel_f, \ ++ open(join(test_dir, 'arm_reloc_relocated.elf'), 'rb') as f: + rel_elf = ELFFile(rel_f) + elf = ELFFile(f) + +-- +2.42.0.windows.2 + diff --git a/0001-Simplify-arm-reloc-test-file.patch b/0001-Simplify-arm-reloc-test-file.patch new file mode 100644 index 0000000000000000000000000000000000000000..6b8b6258b0b350435d2a90006c0471217e635933 --- /dev/null +++ b/0001-Simplify-arm-reloc-test-file.patch @@ -0,0 +1,47 @@ +From e762568c490f26ed7f75afc6a48c339bd43b5689 Mon Sep 17 00:00:00 2001 +From: Eli Bendersky +Date: Wed, 5 Sep 2018 05:35:33 -0700 +Subject: [PATCH] Simplify arm reloc test file + +--- + test_arm_call_reloc.py | 10 ++++++---- + 1 file changed, 6 insertions(+), 4 deletions(-) + +diff --git a/test_arm_call_reloc.py b/test_arm_call_reloc.py +index cb52419..8a2eac5 100755 +--- a/test_arm_call_reloc.py ++++ b/test_arm_call_reloc.py +@@ -10,6 +10,7 @@ + #------------------------------------------------------------------------------- + import os + import sys ++import unittest + + from elftools.common.py3compat import BytesIO + from elftools.elf.elffile import ELFFile +@@ -25,11 +26,12 @@ def do_relocation(rel_elf): + + rel = rel_elf.get_section_by_name('.rel.text') + rh.apply_section_relocations(stream, rel) ++ return data.getvalue() + +- stream.seek(0) +- data = stream.readlines() ++ #stream.seek(0) ++ #data = stream.readlines() + +- return data ++ #return data + + + class TestARMRElocation(unittest.TestCase): +@@ -41,5 +43,5 @@ class TestARMRElocation(unittest.TestCase): + elf = ELFFile(f) + + # Comparison of '.text' section data +- self.assertEquals(do_relocation(rel_elf).pop(), ++ self.assertEquals(do_relocation(rel_elf), + elf.get_section_by_name('.text').data()) +-- +2.42.0.windows.2 + diff --git a/pyelftools.spec b/pyelftools.spec index a760ca89dee4c3edd4d7edb5ccf78f8ee4585324..efce4ada65002489709b626044a7a4a215765f27 100644 --- a/pyelftools.spec +++ b/pyelftools.spec @@ -1,6 +1,6 @@ Name: pyelftools Version: 0.25 -Release: 3 +Release: 4 License: Public Domain Summary: Pure-Python library for parsing and analyzing ELF files URL: https://github.com/eliben/pyelftools @@ -9,6 +9,12 @@ Source0: https://files.pythonhosted.org/packages/source/p/pyelftools/pyel BuildRequires: python3-devel python3-setuptools BuildArch: noarch +#Patch0001: 0001-Call-relocation-for-ARM-V3-194.patch +#Patch0002: 0001-Fix-division-in-calculation-function-for-R_ARM_CALL-.patch +#Patch0003: 0001-Simplify-arm-reloc-test-file.patch +#Patch0004: 0001-Redo-the-new-arm-reloc-test-as-a-proper-unit-test-al.patch +Patch0005: 0001-Rename-input-files-for-arm-relocation-test-to-be-mor.patch + %description This package provides a python library for parsing and analyzing ELF files and DWARF debugging information. @@ -44,6 +50,9 @@ mv %{buildroot}/usr/bin/readelf.py %{buildroot}/usr/bin/pyreadelf %changelog +* Wed Dec 20 2023 liubo - 0.25-4 +- Fix division in calculation function for 'R_ARM_CALL' + * Mon Nov 16 2020 baizhonggui - 0.25-3 - Drop Python 2 subpackage