diff options
author | Kevin Enderby <enderby@apple.com> | 2016-05-05 23:41:05 +0000 |
---|---|---|
committer | Kevin Enderby <enderby@apple.com> | 2016-05-05 23:41:05 +0000 |
commit | 8913496b7751d33eb57943f906925a23039375b5 (patch) | |
tree | 24699d6d98e85f15b34b7d7c2cce4c528a33b8ac /llvm/lib/Object/MachOObjectFile.cpp | |
parent | b012699741a39d473292758b6211963f8b2eea4b (diff) | |
download | bcm5719-llvm-8913496b7751d33eb57943f906925a23039375b5.tar.gz bcm5719-llvm-8913496b7751d33eb57943f906925a23039375b5.zip |
Cleanup and refactor of malformedError() in lib/Object/MachOObjectFile.cpp .
No functional change.
llvm-svn: 268694
Diffstat (limited to 'llvm/lib/Object/MachOObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/MachOObjectFile.cpp | 112 |
1 files changed, 41 insertions, 71 deletions
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 516607dea23..cca76e6f7d0 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -38,19 +38,12 @@ namespace { }; } -// FIXME: Remove ECOverride once Error has been plumbed down to obj tool code. static Error -malformedError(std::string FileName, std::string Msg, - object_error ECOverride = object_error::parse_failed) { - return make_error<GenericBinaryError>(std::move(FileName), std::move(Msg), - ECOverride); -} - -// FIXME: Remove ECOverride once Error has been plumbed down to obj tool code. -static Error -malformedError(const MachOObjectFile &Obj, Twine Msg, - object_error ECOverride = object_error::parse_failed) { - return malformedError(Obj.getFileName(), Msg.str(), ECOverride); +malformedError(const MachOObjectFile &Obj, Twine Msg) { + std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")"; + return make_error<GenericBinaryError>(std::move(Obj.getFileName()), + std::move(StringMsg), + object_error::parse_failed); } // FIXME: Replace all uses of this function with getStructOrErr. @@ -181,9 +174,8 @@ getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr, uint32_t LoadCommandIndex) { if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) { if (CmdOrErr->cmdsize < 8) - return malformedError(*Obj, Twine("truncated or malformed object (load " - "command ") + Twine(LoadCommandIndex) + - Twine(" with size less than 8 bytes)")); + return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + + " with size less than 8 bytes"); return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr}); } else return CmdOrErr.takeError(); @@ -194,9 +186,8 @@ getFirstLoadCommandInfo(const MachOObjectFile *Obj) { unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); if (sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getHeader().sizeofcmds) - return malformedError(*Obj, "truncated or malformed object (load command " - "0 extends past the end all load commands in the " - "file)"); + return malformedError(*Obj, "load command 0 extends past the end all load " + "commands in the file"); return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0); } @@ -207,10 +198,8 @@ getNextLoadCommandInfo(const MachOObjectFile *Obj, uint32_t LoadCommandIndex, : sizeof(MachO::mach_header); if (L.Ptr + L.C.cmdsize + sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getData().data() + HeaderSize + Obj->getHeader().sizeofcmds) - return malformedError(*Obj, Twine("truncated or malformed object " - "(load command ") + Twine(LoadCommandIndex + 1) + - Twine(" extends past the end all load commands in the " - "file)")); + return malformedError(*Obj, "load command " + Twine(LoadCommandIndex + 1) + + " extends past the end all load commands in the file"); return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1); } @@ -218,8 +207,8 @@ template <typename T> static void parseHeader(const MachOObjectFile *Obj, T &Header, Error &Err) { if (sizeof(T) > Obj->getData().size()) { - Err = malformedError(*Obj, "truncated or malformed object (the mach header " - "extends past the end of the file)"); + Err = malformedError(*Obj, "the mach header extends past the end of the " + "file"); return; } if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0))) @@ -238,19 +227,17 @@ static Error parseSegmentLoadCommand( uint32_t LoadCommandIndex, const char *CmdName) { const unsigned SegmentLoadSize = sizeof(SegmentCmd); if (Load.C.cmdsize < SegmentLoadSize) - return malformedError(*Obj, Twine("truncated or malformed object " - "(load command ") + Twine(LoadCommandIndex) + - Twine(" ") + CmdName + Twine(" cmdsize too small)")); + return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + + " " + CmdName + " cmdsize too small"); if (auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr)) { SegmentCmd S = SegOrErr.get(); const unsigned SectionSize = Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section); if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) - return malformedError(*Obj, Twine("truncated or malformed object " - "(load command ") + Twine(LoadCommandIndex) + - Twine(" inconsistent cmdsize in ") + CmdName + - Twine(" for the number of sections)")); + return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + + " inconsistent cmdsize in " + CmdName + + " for the number of sections"); for (unsigned J = 0; J < S.nsects; ++J) { const char *Sec = getSectionPtr(Obj, Load, J); Sections.push_back(Sec); @@ -294,8 +281,7 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, return; BigSize += getHeader().sizeofcmds; if (getData().data() + BigSize > getData().end()) { - Err = malformedError(getFileName(), "truncated or malformed object " - "(load commands extend past the end of the file)"); + Err = malformedError(*this, "load commands extend past the end of the file"); return; } @@ -383,10 +369,8 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, } if (!SymtabLoadCmd) { if (DysymtabLoadCmd) { - Err = malformedError(*this, - "truncated or malformed object (contains " - "LC_DYSYMTAB load command without a LC_SYMTAB load " - "command)"); + Err = malformedError(*this, "contains LC_DYSYMTAB load command without a " + "LC_SYMTAB load command"); return; } } else if (DysymtabLoadCmd) { @@ -395,51 +379,40 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, MachO::dysymtab_command Dysymtab = getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (iolocalsym in " - "LC_DYSYMTAB load command extends past the end of " - "the symbol table)"); + Err = malformedError(*this, "ilocalsym in LC_DYSYMTAB load command " + "extends past the end of the symbol table"); return; } uint64_t BigSize = Dysymtab.ilocalsym; BigSize += Dysymtab.nlocalsym; if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (ilocalsym plus " - "nlocalsym in LC_DYSYMTAB load command extends past " - "the end of the symbol table)"); + Err = malformedError(*this, "ilocalsym plus nlocalsym in LC_DYSYMTAB load " + "command extends past the end of the symbol table"); return; } if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (nextdefsym in " - "LC_DYSYMTAB load command extends past the end of " - "the symbol table)"); + Err = malformedError(*this, "nextdefsym in LC_DYSYMTAB load command " + "extends past the end of the symbol table"); return; } BigSize = Dysymtab.iextdefsym; BigSize += Dysymtab.nextdefsym; if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (iextdefsym plus " - "nextdefsym in LC_DYSYMTAB load command extends " - "past the end of the symbol table)"); + Err = malformedError(*this, "iextdefsym plus nextdefsym in LC_DYSYMTAB " + "load command extends past the end of the symbol " + "table"); return; } if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (nundefsym in " - "LC_DYSYMTAB load command extends past the end of " - "the symbol table)"); + Err = malformedError(*this, "nundefsym in LC_DYSYMTAB load command " + "extends past the end of the symbol table"); return; } BigSize = Dysymtab.iundefsym; BigSize += Dysymtab.nundefsym; if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { - Err = malformedError(*this, - "truncated or malformed object (iundefsym plus " - "nundefsym in LC_DYSYMTAB load command extends past " - "the end of the symbol table"); + Err = malformedError(*this, "iundefsym plus nundefsym in LC_DYSYMTAB load " + " command extends past the end of the symbol table"); return; } } @@ -460,10 +433,8 @@ Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const { MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); const char *Start = &StringTable.data()[Entry.n_strx]; if (Start < getData().begin() || Start >= getData().end()) { - return malformedError(*this, Twine("truncated or malformed object (bad " - "string index: ") + Twine(Entry.n_strx) + Twine(" for " - "symbol at index ") + Twine(getSymbolIndex(Symb)) + - Twine(")")); + return malformedError(*this, "bad string index: " + Twine(Entry.n_strx) + + " for symbol at index " + Twine(getSymbolIndex(Symb))); } return StringRef(Start); } @@ -593,10 +564,8 @@ MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { DataRefImpl DRI; DRI.d.a = index - 1; if (DRI.d.a >= Sections.size()){ - return malformedError(*this, Twine("truncated or malformed object (bad " - "section index: ") + Twine((int)index) + Twine(" for " - "symbol at index ") + Twine(getSymbolIndex(Symb)) + - Twine(")")); + return malformedError(*this, "bad section index: " + Twine((int)index) + + " for symbol at index " + Twine(getSymbolIndex(Symb))); } return section_iterator(SectionRef(DRI, this)); } @@ -2427,6 +2396,7 @@ ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) { return MachOObjectFile::create(Buffer, false, true); if (Magic == "\xCF\xFA\xED\xFE") return MachOObjectFile::create(Buffer, true, true); - return malformedError(Buffer.getBufferIdentifier(), - "Unrecognized MachO magic number"); + return make_error<GenericBinaryError>(std::move(Buffer.getBufferIdentifier()), + std::move("Unrecognized MachO magic number"), + object_error::invalid_file_type); } |