]> granicus.if.org Git - llvm/commitdiff
Fix a crash in llvm-nm for a bad Mach-O file that has an N_SECT type symbol and a...
authorKevin Enderby <enderby@apple.com>
Wed, 13 Sep 2017 21:01:49 +0000 (21:01 +0000)
committerKevin Enderby <enderby@apple.com>
Wed, 13 Sep 2017 21:01:49 +0000 (21:01 +0000)
The code in llvm-nm for Mach-O files to determine the section type for an
N_SECT type symbol it will call getSymbolSection() and check for the error,
but in the case the n_sect value is zero it will return section_end() (aka nullptr).
And the code was using that and crashing instead of just returning a ā€™sā€™ for a
section or printing (?,?) as it would if getSymbolSection() returned an error.

rdar://33136604

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313193 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-nm/X86/Inputs/macho-bad-zero-nsect-for-N_SECT [new file with mode: 0755]
test/tools/llvm-nm/X86/macho-bad-zero-nsect-for-N_SECT.test [new file with mode: 0644]
tools/llvm-nm/llvm-nm.cpp

diff --git a/test/tools/llvm-nm/X86/Inputs/macho-bad-zero-nsect-for-N_SECT b/test/tools/llvm-nm/X86/Inputs/macho-bad-zero-nsect-for-N_SECT
new file mode 100755 (executable)
index 0000000..dff3188
Binary files /dev/null and b/test/tools/llvm-nm/X86/Inputs/macho-bad-zero-nsect-for-N_SECT differ
diff --git a/test/tools/llvm-nm/X86/macho-bad-zero-nsect-for-N_SECT.test b/test/tools/llvm-nm/X86/macho-bad-zero-nsect-for-N_SECT.test
new file mode 100644 (file)
index 0000000..86c1cb8
--- /dev/null
@@ -0,0 +1,8 @@
+RUN: llvm-nm %p/Inputs/macho-bad-zero-nsect-for-N_SECT | FileCheck -check-prefix DEFAULT %s
+DEFAULT: 0000000000000000 S dyld_stub_binder
+
+RUN: llvm-nm -m %p/Inputs/macho-bad-zero-nsect-for-N_SECT | FileCheck -check-prefix MACHO %s
+MACHO: 0000000000000000 (?,?) private external dyld_stub_binder
+
+RUN: llvm-nm -x %p/Inputs/macho-bad-zero-nsect-for-N_SECT | FileCheck -check-prefix HEX %s
+HEX: 0000000000000000 1f 00 0000 00000024 dyld_stub_binder
index 1934051c65d7bb0db1b7781ba4d1396240c735b7..4ad0d95d67f6625b86b88d4ab2254f1b6d9754c0 100644 (file)
@@ -486,6 +486,10 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
         break;
       }
       Sec = *SecOrErr;
+      if (Sec == MachO->section_end()) {
+        outs() << "(?,?) ";
+        break;
+      }
     } else {
       Sec = I->Section;
     }
@@ -997,6 +1001,8 @@ static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
       return 's';
     }
     section_iterator Sec = *SecOrErr;
+    if (Sec == Obj.section_end())
+      return 's';
     DataRefImpl Ref = Sec->getRawDataRefImpl();
     StringRef SectionName;
     Obj.getSectionName(Ref, SectionName);