From 9f12da56703fa7e805fc31a7af80b314b30a9c2e Mon Sep 17 00:00:00 2001 From: Patrick Gordon Date: Sat, 23 May 2020 15:49:28 +0100 Subject: [PATCH] fix issue in aranges cu_offset_at_addr (#310) * fix issue in aranges cu_offset_at_addr if there are aranges for some parts of the binary but not others, incorrect aranges may be returned * add test suite for absent/partial/complete aranges --- elftools/dwarf/aranges.py | 5 ++- test/test_dwarf_aranges.py | 36 ++++++++++++++++++ .../aranges_absent.elf | Bin 0 -> 17408 bytes .../aranges_complete.elf | Bin 0 -> 17600 bytes .../aranges_partial.elf | Bin 0 -> 17552 bytes .../aranges_partial_a.c | 20 ++++++++++ .../aranges_partial_b.c | 4 ++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/test_dwarf_aranges.py create mode 100755 test/testfiles_for_unittests/aranges_absent.elf create mode 100755 test/testfiles_for_unittests/aranges_complete.elf create mode 100755 test/testfiles_for_unittests/aranges_partial.elf create mode 100644 test/testfiles_for_unittests/aranges_partial_a.c create mode 100644 test/testfiles_for_unittests/aranges_partial_b.c diff --git a/elftools/dwarf/aranges.py b/elftools/dwarf/aranges.py index 633edec..3f140f4 100644 --- a/elftools/dwarf/aranges.py +++ b/elftools/dwarf/aranges.py @@ -49,7 +49,10 @@ class ARanges(object): 'offset' refers to the offset in the .debug_info section. """ tup = self.entries[bisect_right(self.keys, addr) - 1] - return tup.info_offset + if tup.begin_addr <= addr < tup.begin_addr + tup.length: + return tup.info_offset + else: + return None #------ PRIVATE ------# diff --git a/test/test_dwarf_aranges.py b/test/test_dwarf_aranges.py new file mode 100644 index 0000000..7b7d0cc --- /dev/null +++ b/test/test_dwarf_aranges.py @@ -0,0 +1,36 @@ +import os +import unittest + +from elftools.elf.elffile import ELFFile + +address_a = 0x112f; +address_b = 0x1154; + +class TestRangeLists(unittest.TestCase): + def test_arange_absent(self): + with open(os.path.join('test', 'testfiles_for_unittests', 'aranges_absent.elf'), 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + aranges = elffile.get_dwarf_info().get_aranges() + self.assertIsNone(aranges) + + def test_arange_partial(self): + with open(os.path.join('test', 'testfiles_for_unittests', 'aranges_partial.elf'), 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + aranges = elffile.get_dwarf_info().get_aranges() + self.assertIsNotNone(aranges) + self.assertIsNone(aranges.cu_offset_at_addr(address_a)) + self.assertIsNotNone(aranges.cu_offset_at_addr(address_b)) + + def test_arange_complete(self): + with open(os.path.join('test', 'testfiles_for_unittests', 'aranges_complete.elf'), 'rb') as f: + elffile = ELFFile(f) + self.assertTrue(elffile.has_dwarf_info()) + aranges = elffile.get_dwarf_info().get_aranges() + self.assertIsNotNone(aranges) + self.assertIsNotNone(aranges.cu_offset_at_addr(address_a)) + self.assertIsNotNone(aranges.cu_offset_at_addr(address_b)) + +if __name__ == '__main__': + unittest.main() diff --git a/test/testfiles_for_unittests/aranges_absent.elf b/test/testfiles_for_unittests/aranges_absent.elf new file mode 100755 index 0000000000000000000000000000000000000000..5672d2d1a099134068faf55f6bc0ba9d98470526 GIT binary patch literal 17408 zcmeHOU2Ggz6~41Sjvd?UwG%fcDfJLQa-bPIsS`O(o$T6PXG}Ifq)sSm-C?{twpXlo z-QC$@M@2+31WYi5K2!qr1(gR74@3x_=!1i!QV>E(k&qE8jevkbrKE+Tl%`qEnK@^7 zW_FDtfsjz=u6EA7-}$-sp1E^>X72q=A~_n6B*7sl4hc-jQ5%U7BlZwQfW*XrXohRI zxKA`dI%HusKV}o)+F>T(rj3LLfui2Gi0`HMk{nYmAyU-aC=7&c4pTw(Ks}2^X~vn} zpbVHYUdd0U96hv=^q4yJ&^KL*t=DzPO`}xrE~2c5N!05hy&lqIdWrI5%Kk*3;QtEQ z^HLdj7-Z{O_2J1IX4=vvM#w|^NuDYD{-dCW<-FU#Za+o&ZRD4AsvlEIir#Eya({ns zHoZHW$(81J&kyY1y}w^6=9Rrxz0p3(9yxy6DiXFYb}a&!#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kSy(qW6w2;TM`y}1w Ya?a(lZaZY^**}8=6QuXM3|y@E7usWLWdHyG literal 0 HcmV?d00001 diff --git a/test/testfiles_for_unittests/aranges_partial.elf b/test/testfiles_for_unittests/aranges_partial.elf new file mode 100755 index 0000000000000000000000000000000000000000..6ed3ea6b31ed3e4e24124a0844bc1de6be7c816f GIT binary patch literal 17552 zcmeHOU2Ggz6~42x>o~DJUORDXlByocj~r;$PH-ZpsncEC>x{`JF{u*@tvem>j_rl@ zuDd&1?5K!Hh7>Cp0uM-_zM%2|;(-Xk3-lquQ7H(aq)5mJl}13opi)vGD5YtZb7s!j zof)rDgb)(y+||yx=Q}@h=FFYBcV_PWR5CRZk|e=J76%1#YScmEBsljVRe;1rzi5K3 zD!N1i@L?NsK;0J!VU@B7pE2h!UCJ zqwG#gKPakolrg^cI`}wXY8^bi0gf@CCeyU`mB-7 z3*<3h`^2Ht=-`mPC$=ZHSLmbTNA!$YG^evA%Pfu`8Ojy%=D0DLLm$(#g*^Gt9o4#u zc05k@IENBs598$XUf!PFj2XV4V(06D+i$eZ5vbJO8NWad)VL#OocYy<930Or=7!5v z7tS%EY|VvZTi|ltg>xH6*-aPjZZm8b&>#ar27(L(83-~EWFW{ukbxir|4$kCTkF05 z)D~ZD)XqkJ*&~GZnG05^a#>rv)VN|F2FiE;d35Q@pjqko5}5z}oVGl01^p~Xeu8BGO{)W3=gIZv%GK7c)984GDCmF9*5CV8Bs*_w zOV_oFuRWw)yeVta3)+?2);4gEBnOR^t5dCA!!>)1@ASX~h(-AWC$z?7lU9(!W zvjYc#sdPkPyOyq0DlbI(pejwQOvPHeK8<0Bm8n|2wJR>r-yf$zm*P7Ct{^|rA0=<0 z`gyUo9mrDh=Go*;$Wqkp3++*#bk8PlO{}25#D`PM1K)xtpTx=0rC%gYj4s_sj3<_E zozRwdJqz%|shw}(Ian*)u2i&(Z^>5I?qAXVQ%iqMExk6p^oK-c+bi1Qilps-x%?+Q zKaWl%9!pFl9#81*W5E64nL}!y*X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Qf4w>!JK2^ zG#ST&>)_6q&8A^r1@Hx%*jx@Ojx$J?h(XJpg$4N?b(ePmgU^C&&*MI(s#_WAd_QA&FI4c} z&G!8K%JhCxJ&2^-Q;g8S0yni0QGUfLM96#$Z#pg48mag)Y7#{DcAjjg5 z-$8|#h6>hl z>({-pf&FPR{I1(+9bJ5d;`fOreRyMra>gOmtzWlw1N-lj{Wq(YPQ`D(gY5msncm~* zFK=MaevtCD=T^N=_D@j5y%&TS5}!|QSNNWj&%N7zY`?>8_c`bNvToZ3_8-HJ3CeqY I20m8&3ze*8F8}}l literal 0 HcmV?d00001 diff --git a/test/testfiles_for_unittests/aranges_partial_a.c b/test/testfiles_for_unittests/aranges_partial_a.c new file mode 100644 index 0000000..47ae2bc --- /dev/null +++ b/test/testfiles_for_unittests/aranges_partial_a.c @@ -0,0 +1,20 @@ +/* +clang -g -c aranges_partial_a.c +clang -g -gdwarf-aranges -c aranges_partial_b.c +clang -g aranges_partial_{a,b}.o -o aranges_partial.elf + +clang -g -gdwarf-aranges -c aranges_partial_a.c +clang -g -gdwarf-aranges -c aranges_partial_b.c +clang -g aranges_partial_{a,b}.o -o aranges_complete.elf + +clang -g -c aranges_partial_a.c +clang -g -c aranges_partial_b.c +clang -g aranges_partial_{a,b}.o -o aranges_absent.elf +*/ + +extern int test(); + +int main() { + int a = test(); + return a; +} diff --git a/test/testfiles_for_unittests/aranges_partial_b.c b/test/testfiles_for_unittests/aranges_partial_b.c new file mode 100644 index 0000000..496a99d --- /dev/null +++ b/test/testfiles_for_unittests/aranges_partial_b.c @@ -0,0 +1,4 @@ +int test() { + int a = 0; + return a; +} -- 2.30.2