From 061635e520c81961d962fde1e9863280b128fc23 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 15 Nov 2013 20:23:25 +0000 Subject: [PATCH] Readobj: If NumbersOfSections is 0xffff, it's an COFF import library. 0xffff does not mean that there are 65535 sections in a COFF file but indicates that it's a COFF import library. This patch fixes SEGV error when an import library file is passed to llvm-readobj. llvm-svn: 194844 --- include/llvm/Object/COFF.h | 2 ++ lib/Object/COFFObjectFile.cpp | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index ec8998c7465..e05ae6c654c 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -57,6 +57,8 @@ struct coff_file_header { support::ulittle32_t NumberOfSymbols; support::ulittle16_t SizeOfOptionalHeader; support::ulittle16_t Characteristics; + + bool isImportLibrary() const { return NumberOfSections == 0xffff; } }; /// The 32-bit PE header that follows the COFF header. diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index f33caee3349..42066c372b4 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -507,9 +507,10 @@ COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) CurPtr += COFFHeader->SizeOfOptionalHeader; } - if ((ec = getObject(SectionTable, Data, base() + CurPtr, - COFFHeader->NumberOfSections * sizeof(coff_section)))) - return; + if (!COFFHeader->isImportLibrary()) + if ((ec = getObject(SectionTable, Data, base() + CurPtr, + COFFHeader->NumberOfSections * sizeof(coff_section)))) + return; // Initialize the pointer to the symbol table. if (COFFHeader->PointerToSymbolTable != 0) @@ -586,7 +587,9 @@ section_iterator COFFObjectFile::begin_sections() const { section_iterator COFFObjectFile::end_sections() const { DataRefImpl ret; - ret.p = reinterpret_cast(SectionTable + COFFHeader->NumberOfSections); + int numSections = COFFHeader->isImportLibrary() + ? 0 : COFFHeader->NumberOfSections; + ret.p = reinterpret_cast(SectionTable + numSections); return section_iterator(SectionRef(ret, this)); }