From f203ab5be38163cd2c561f64c0453acf22978a0b Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Thu, 14 Jul 2016 22:13:32 +0000 Subject: [PATCH] llvm-objdump: handle stubbed and malformed dylibs better We were quite happy to read past the end of the valid section data when disassembling. Instead we entirely skip stub dylibs, and tell the user what's happened if their section only has partial data. llvm-svn: 275487 --- .../llvm-objdump/X86/Inputs/stubbed.dylib.macho-x86_64 | Bin 0 -> 784 bytes .../X86/Inputs/truncated-section.dylib.macho-x86_64 | Bin 0 -> 4208 bytes llvm/test/tools/llvm-objdump/X86/stubbed-dylib.test | 5 +++++ llvm/test/tools/llvm-objdump/X86/truncated-section.test | 7 +++++++ llvm/tools/llvm-objdump/MachODump.cpp | 14 ++++++++++++-- 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 llvm/test/tools/llvm-objdump/X86/Inputs/stubbed.dylib.macho-x86_64 create mode 100755 llvm/test/tools/llvm-objdump/X86/Inputs/truncated-section.dylib.macho-x86_64 create mode 100644 llvm/test/tools/llvm-objdump/X86/stubbed-dylib.test create mode 100644 llvm/test/tools/llvm-objdump/X86/truncated-section.test diff --git a/llvm/test/tools/llvm-objdump/X86/Inputs/stubbed.dylib.macho-x86_64 b/llvm/test/tools/llvm-objdump/X86/Inputs/stubbed.dylib.macho-x86_64 new file mode 100755 index 0000000000000000000000000000000000000000..144738bc97d6e76f6343ae7c0be2fade64e6013f GIT binary patch literal 784 zcma)4Jxjw-6g@FYsaD!<6^DX|s6#86oW+7w5y1{_4`Nyf*jSnb?bfBMf55>^ZbC=@ zg&^o}s6+pNgLrP5&}YGq3n%xUcW=&p_r3T0G5*W|h6x}`QlWS4dX93v))f=~(M$KD>!h*F zc@XOy*H4aW`}K6zWWG*P7$qVpmpP3jCvKV8Bua$7e(BmlyHjy`44A?ik9kLeKTP_G zmb4ZB#6a$9L@#}oh*Ls0sTJt`_55$~DwERZ@V+;G&sgSN>Gs8Ydwf{uT0wryEymbR zxvbIT-Ru(SUV&F$pMh%VcdMLPaMtsKX1lUCJq2Kc|HQiVVq1jHVW?KY5S=rA8;Ld&QMe!?Y7r$^Sc?I_5J3eSS%^s>U`|OcXgUimY$Uyfsq6$h zEd_soAm~33wD1pDi0|F)aYqXM0q(%Nnc1D2-FbJ-d;0qP?h>(LBGL)Ofs;0oGZ{c6 zG7P)}N^NC#wz$$_ep$GvbM%xB0IfVz$}JqbI)Q8L*G%|(L%M`{dsxooB$_H!bB;<* zUX`5U0XACu-Rpi!`X%PUU7~%0m~*mLskPS|WOWbpN+_1e-i(<*NSq z(Upl^CNG|y*PH8ctl>!fAiW-@gPdzH{x|bLa95ERd_GHNYSqdNW+pc4Rku)1FN7?J zOyC{%P2$lFKc#cdY0NNS7%&VN1`Gp+0mFb{z%XDKFbw=B2DX>Z-*z91>4bgijqT@+ z2DRO&CL-#o`>37g;E6k}gBepd#-YguLS<-)rHVCYPkLg~w5^N`#^lyUUDNgj82cq~ lSfJ0T(U?EzzH~@=9nVtY_w$|^f!@uls$0vc9K?E|B0od2RBiwO literal 0 HcmV?d00001 diff --git a/llvm/test/tools/llvm-objdump/X86/stubbed-dylib.test b/llvm/test/tools/llvm-objdump/X86/stubbed-dylib.test new file mode 100644 index 0000000..6f7f722 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/stubbed-dylib.test @@ -0,0 +1,5 @@ +RUN: llvm-objdump -macho -d %p/Inputs/stubbed.dylib.macho-x86_64 | FileCheck %s + +CHECK: (__TEXT,__text) section +CHECK-NOT: func +CHECK-NOT: func2 diff --git a/llvm/test/tools/llvm-objdump/X86/truncated-section.test b/llvm/test/tools/llvm-objdump/X86/truncated-section.test new file mode 100644 index 0000000..e9fe75d --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/truncated-section.test @@ -0,0 +1,7 @@ +RUN: llvm-objdump -macho -d %p/Inputs/truncated-section.dylib.macho-x86_64 | FileCheck %s + +CHECK: _func: +CHECK: retq +CHECK: retq + +CHECK: section data ends, _func2 lies outside valid range diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index 08bc1f4..991ccee 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -6662,6 +6662,10 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, unsigned int Arch = MachOOF->getArch(); + // Skip all symbols if this is a stubs file. + if (Bytes.size() == 0) + return; + // Disassemble symbol by symbol. for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { Expected SymNameOrErr = Symbols[SymIdx].getName(); @@ -6716,10 +6720,17 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, continue; // Start at the address of the symbol relative to the section's address. + uint64_t SectSize = Sections[SectIdx].getSize(); uint64_t Start = Symbols[SymIdx].getValue(); uint64_t SectionAddress = Sections[SectIdx].getAddress(); Start -= SectionAddress; + if (Start > SectSize) { + outs() << "section data ends, " << SymName + << " lies outside valid range\n"; + return; + } + // Stop disassembling either at the beginning of the next symbol or at // the end of the section. bool containsNextSym = false; @@ -6745,8 +6756,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, ++NextSymIdx; } - uint64_t SectSize = Sections[SectIdx].getSize(); - uint64_t End = containsNextSym ? NextSym : SectSize; + uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize; uint64_t Size; symbolTableWorked = true; -- 2.7.4