From 3fc9188fa8cb939539e568a0ef602892aa3fc59a Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Fri, 3 Nov 2017 21:32:44 +0000 Subject: [PATCH] Fix a crash in llvm-objdump when printing a bad x86_64 relocation in a Mach-O file with a bad section number. rdar://35207539 llvm-svn: 317373 --- .../X86/Inputs/macho-invalid-reloc-section-index | Bin 0 -> 2768 bytes llvm/test/tools/llvm-objdump/X86/malformed-machos.test | 3 +++ llvm/tools/llvm-objdump/llvm-objdump.cpp | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 llvm/test/tools/llvm-objdump/X86/Inputs/macho-invalid-reloc-section-index diff --git a/llvm/test/tools/llvm-objdump/X86/Inputs/macho-invalid-reloc-section-index b/llvm/test/tools/llvm-objdump/X86/Inputs/macho-invalid-reloc-section-index new file mode 100644 index 0000000000000000000000000000000000000000..a9d0b48449b733af6a717bf6e71425bc17e2be0a GIT binary patch literal 2768 zcmd5-O-vI(6dwKrY7Y<&Dq`G5RE!C>*cMbuFo2ebLTpPg6t@j6P?46DlJww##2AB3 z4DrMfJelahqi`@yh%p*bFWxxlp@|-dTs){r@q4o~bt!GcCI(+J`~GI$?!5Wt+t=yk z=Zvx9BF4(0OEAogl`TLQV~AMUd&JIy}~9IAS@~WLAEQ#cH;+*lc!lv!ZBm z?P)Qa?N+OzSel!}WS>D*8(r5fcXnM+T)kJ_U7b|8fgX?zj5?uhLc4|bLMQQsW6)}^ z>ryZtyBLghOh==^aI8CkM`$DzjBd~MMEn7_e{_4IKNy_~MZ&IFG!#Z!or?LRF|H$% zcnKu$KJB^pX}8O+_zc5Zy~c=Eia+1|SWly=Mbv2YoOkszWi~hI$qr!5b1V0wM}!+_ zF+}=wP&7msdx)VuG(!_rb$1O=BeO)Vm-XT(P)c)3*^GWFfnh(!0`hw(fxryZK@5to zFP(ik{jkmZxco*aofsTEhFBHGJmyzGpAbH+--&pfvngkSW6qgi&O6Ci$J9egFhQ5$%bU1njt-$~tE*vYdq0eCtmPhK3N-nx!apPY zlfoYmzHpfj2Bv3ZJj z*YF&f)Zm;K#|rK6&p4L-mAI3Bp*WW7Wj!YjfuBPj91kE#m?pd{&=SXMmBY&)zPBtb ztro4{JOAyI#Bm?y>2XZ!cOrVUZpAU7Qo|)mIb;F>kS+wnay^x9 zMIFL{o`WU-`j8|3eFPncmbgtl{rc*K%@M!5_ND(xuzpqI)+=gB{-yPr^m{xWX$zgn zG3J>~EmBlZytAoiMub4Hu;|CZtwp*PoMh>nF^v15(Kn+>XyQupYYYayFXGc0#1ss& zk3&mdJp^0NsXq9a!X=wacY{a&mOoG1K&(2vN&YMpXVf>s8RrjXHR>zSN zXAMY@-40FvcZsue*N>X61Z?*o%y@rRc8_jIoYi91~{z}Xzt!E(Y{1}|i1 Jb73wX`~|bv!9V~2 literal 0 HcmV?d00001 diff --git a/llvm/test/tools/llvm-objdump/X86/malformed-machos.test b/llvm/test/tools/llvm-objdump/X86/malformed-machos.test index 292666a..e29df46 100644 --- a/llvm/test/tools/llvm-objdump/X86/malformed-machos.test +++ b/llvm/test/tools/llvm-objdump/X86/malformed-machos.test @@ -66,3 +66,6 @@ INVALID-SYMBOL-LIB_ORDINAL: macho-invalid-symbol-lib_ordinal': truncated or malf RUN: not llvm-objdump -macho -objc-meta-data %p/Inputs/macho-invalid-bind-entry 2>&1 | FileCheck -check-prefix INVALID-BIND-ENTRY %s INVALID-BIND-ENTRY: macho-invalid-bind-entry': truncated or malformed object (for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad library ordinal: 83 (max 0) for opcode at: 0x0) + +RUN: llvm-objdump -macho -r %p/Inputs/macho-invalid-reloc-section-index | FileCheck -check-prefix INVALID-RELOC-SECTION-INDEX %s +INVALID-RELOC-SECTION-INDEX: 0000000000000021 X86_64_RELOC_UNSIGNED 8388613 (?,?) diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index d80f1cb..02eaa89 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -865,8 +865,19 @@ static void printRelocationTargetName(const MachOObjectFile *O, } else { section_iterator SI = O->section_begin(); // Adjust for the fact that sections are 1-indexed. - advance(SI, Val - 1); - SI->getName(S); + if (Val == 0) { + fmt << "0 (?,?)"; + return; + } + uint32_t i = Val - 1; + while (i != 0 && SI != O->section_end()) { + i--; + advance(SI, 1); + } + if (SI == O->section_end()) + fmt << Val << " (?,?)"; + else + SI->getName(S); } fmt << S; -- 2.7.4