Fix a crash in llvm-nm for a bad Mach-O file that has an N_SECT type symbol and a zero n_sect value.

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
This commit is contained in:
Kevin Enderby 2017-09-13 21:01:49 +00:00
parent 2029d1efeb
commit 0833191bf8
3 changed files with 14 additions and 0 deletions

View File

@ -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

View 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);