1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

Change GenericBinaryError to no longer include a FileName, which is then not

part of the error message.

As the caller is the one that needs to add the name of where the "object file"
comes from to the error message as the object file could be in an archive, or
coming from a slice of a Mach-O universal file or a buffer created by a JIT.

In the cases of a Mach-O universal file the architecture name may or may not
also need to be printed which is up to the tool code.  For example if the tool
code is only selecting the host architecture slice then that architecture name
is never printed.

This patch is the change to the libObject code and there will be follow on
commits for changes to the code for each tool.

llvm-svn: 268789
This commit is contained in:
Kevin Enderby 2016-05-06 20:16:28 +00:00
parent b29cae8463
commit ee96ce6849
3 changed files with 32 additions and 36 deletions

View File

@ -64,13 +64,11 @@ public:
class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> { class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
public: public:
static char ID; static char ID;
GenericBinaryError(std::string FileName, Twine Msg); GenericBinaryError(Twine Msg);
GenericBinaryError(std::string FileName, Twine Msg, object_error ECOverride); GenericBinaryError(Twine Msg, object_error ECOverride);
const std::string &getFileName() const { return FileName; }
const std::string &getMessage() const { return Msg; } const std::string &getMessage() const { return Msg; }
void log(raw_ostream &OS) const override; void log(raw_ostream &OS) const override;
private: private:
std::string FileName;
std::string Msg; std::string Msg;
}; };

View File

@ -55,16 +55,15 @@ std::string _object_error_category::message(int EV) const {
char BinaryError::ID = 0; char BinaryError::ID = 0;
char GenericBinaryError::ID = 0; char GenericBinaryError::ID = 0;
GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg) GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
: FileName(std::move(FileName)), Msg(Msg.str()) {}
GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg, object_error ECOverride) GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
: FileName(std::move(FileName)), Msg(Msg.str()) { : Msg(Msg.str()) {
setErrorCode(make_error_code(ECOverride)); setErrorCode(make_error_code(ECOverride));
} }
void GenericBinaryError::log(raw_ostream &OS) const { void GenericBinaryError::log(raw_ostream &OS) const {
OS << "Error in " << FileName << ": " << Msg; OS << Msg;
} }
static ManagedStatic<_object_error_category> error_category; static ManagedStatic<_object_error_category> error_category;

View File

@ -39,9 +39,9 @@ namespace {
} }
static Error static Error
malformedError(const MachOObjectFile &Obj, Twine Msg) { malformedError(Twine Msg) {
std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")"; std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")";
return make_error<GenericBinaryError>(Obj.getFileName(), std::move(StringMsg), return make_error<GenericBinaryError>(std::move(StringMsg),
object_error::parse_failed); object_error::parse_failed);
} }
@ -63,7 +63,7 @@ template <typename T>
static Expected<T> getStructOrErr(const MachOObjectFile *O, const char *P) { static Expected<T> getStructOrErr(const MachOObjectFile *O, const char *P) {
// Don't read before the beginning or past the end of the file // Don't read before the beginning or past the end of the file
if (P < O->getData().begin() || P + sizeof(T) > O->getData().end()) if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
return malformedError(*O, "Structure read out-of-range"); return malformedError("Structure read out-of-range");
T Cmd; T Cmd;
memcpy(&Cmd, P, sizeof(T)); memcpy(&Cmd, P, sizeof(T));
@ -173,7 +173,7 @@ getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr,
uint32_t LoadCommandIndex) { uint32_t LoadCommandIndex) {
if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) { if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) {
if (CmdOrErr->cmdsize < 8) if (CmdOrErr->cmdsize < 8)
return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + return malformedError("load command " + Twine(LoadCommandIndex) +
" with size less than 8 bytes"); " with size less than 8 bytes");
return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr}); return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr});
} else } else
@ -185,7 +185,7 @@ getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64) unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
: sizeof(MachO::mach_header); : sizeof(MachO::mach_header);
if (sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getHeader().sizeofcmds) if (sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getHeader().sizeofcmds)
return malformedError(*Obj, "load command 0 extends past the end all load " return malformedError("load command 0 extends past the end all load "
"commands in the file"); "commands in the file");
return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0); return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0);
} }
@ -197,7 +197,7 @@ getNextLoadCommandInfo(const MachOObjectFile *Obj, uint32_t LoadCommandIndex,
: sizeof(MachO::mach_header); : sizeof(MachO::mach_header);
if (L.Ptr + L.C.cmdsize + sizeof(MachOObjectFile::LoadCommandInfo) > if (L.Ptr + L.C.cmdsize + sizeof(MachOObjectFile::LoadCommandInfo) >
Obj->getData().data() + HeaderSize + Obj->getHeader().sizeofcmds) Obj->getData().data() + HeaderSize + Obj->getHeader().sizeofcmds)
return malformedError(*Obj, "load command " + Twine(LoadCommandIndex + 1) + return malformedError("load command " + Twine(LoadCommandIndex + 1) +
" extends past the end all load commands in the file"); " extends past the end all load commands in the file");
return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1); return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1);
} }
@ -206,7 +206,7 @@ template <typename T>
static void parseHeader(const MachOObjectFile *Obj, T &Header, static void parseHeader(const MachOObjectFile *Obj, T &Header,
Error &Err) { Error &Err) {
if (sizeof(T) > Obj->getData().size()) { if (sizeof(T) > Obj->getData().size()) {
Err = malformedError(*Obj, "the mach header extends past the end of the " Err = malformedError("the mach header extends past the end of the "
"file"); "file");
return; return;
} }
@ -226,7 +226,7 @@ static Error parseSegmentLoadCommand(
uint32_t LoadCommandIndex, const char *CmdName) { uint32_t LoadCommandIndex, const char *CmdName) {
const unsigned SegmentLoadSize = sizeof(SegmentCmd); const unsigned SegmentLoadSize = sizeof(SegmentCmd);
if (Load.C.cmdsize < SegmentLoadSize) if (Load.C.cmdsize < SegmentLoadSize)
return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + return malformedError("load command " + Twine(LoadCommandIndex) +
" " + CmdName + " cmdsize too small"); " " + CmdName + " cmdsize too small");
if (auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr)) { if (auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr)) {
SegmentCmd S = SegOrErr.get(); SegmentCmd S = SegOrErr.get();
@ -234,7 +234,7 @@ static Error parseSegmentLoadCommand(
Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section); Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section);
if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
return malformedError(*Obj, "load command " + Twine(LoadCommandIndex) + return malformedError("load command " + Twine(LoadCommandIndex) +
" inconsistent cmdsize in " + CmdName + " inconsistent cmdsize in " + CmdName +
" for the number of sections"); " for the number of sections");
for (unsigned J = 0; J < S.nsects; ++J) { for (unsigned J = 0; J < S.nsects; ++J) {
@ -280,7 +280,7 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
return; return;
BigSize += getHeader().sizeofcmds; BigSize += getHeader().sizeofcmds;
if (getData().data() + BigSize > getData().end()) { if (getData().data() + BigSize > getData().end()) {
Err = malformedError(*this, "load commands extend past the end of the file"); Err = malformedError("load commands extend past the end of the file");
return; return;
} }
@ -301,28 +301,28 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
if (Load.C.cmd == MachO::LC_SYMTAB) { if (Load.C.cmd == MachO::LC_SYMTAB) {
// Multiple symbol tables // Multiple symbol tables
if (SymtabLoadCmd) { if (SymtabLoadCmd) {
Err = malformedError(*this, "Multiple symbol tables"); Err = malformedError("Multiple symbol tables");
return; return;
} }
SymtabLoadCmd = Load.Ptr; SymtabLoadCmd = Load.Ptr;
} else if (Load.C.cmd == MachO::LC_DYSYMTAB) { } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
// Multiple dynamic symbol tables // Multiple dynamic symbol tables
if (DysymtabLoadCmd) { if (DysymtabLoadCmd) {
Err = malformedError(*this, "Multiple dynamic symbol tables"); Err = malformedError("Multiple dynamic symbol tables");
return; return;
} }
DysymtabLoadCmd = Load.Ptr; DysymtabLoadCmd = Load.Ptr;
} else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
// Multiple data in code tables // Multiple data in code tables
if (DataInCodeLoadCmd) { if (DataInCodeLoadCmd) {
Err = malformedError(*this, "Multiple data-in-code tables"); Err = malformedError("Multiple data-in-code tables");
return; return;
} }
DataInCodeLoadCmd = Load.Ptr; DataInCodeLoadCmd = Load.Ptr;
} else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
// Multiple linker optimization hint tables // Multiple linker optimization hint tables
if (LinkOptHintsLoadCmd) { if (LinkOptHintsLoadCmd) {
Err = malformedError(*this, "Multiple linker optimization hint tables"); Err = malformedError("Multiple linker optimization hint tables");
return; return;
} }
LinkOptHintsLoadCmd = Load.Ptr; LinkOptHintsLoadCmd = Load.Ptr;
@ -330,14 +330,14 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) { Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
// Multiple dyldinfo load commands // Multiple dyldinfo load commands
if (DyldInfoLoadCmd) { if (DyldInfoLoadCmd) {
Err = malformedError(*this, "Multiple dyldinfo load commands"); Err = malformedError("Multiple dyldinfo load commands");
return; return;
} }
DyldInfoLoadCmd = Load.Ptr; DyldInfoLoadCmd = Load.Ptr;
} else if (Load.C.cmd == MachO::LC_UUID) { } else if (Load.C.cmd == MachO::LC_UUID) {
// Multiple UUID load commands // Multiple UUID load commands
if (UuidLoadCmd) { if (UuidLoadCmd) {
Err = malformedError(*this, "Multiple UUID load commands"); Err = malformedError("Multiple UUID load commands");
return; return;
} }
UuidLoadCmd = Load.Ptr; UuidLoadCmd = Load.Ptr;
@ -368,7 +368,7 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
} }
if (!SymtabLoadCmd) { if (!SymtabLoadCmd) {
if (DysymtabLoadCmd) { if (DysymtabLoadCmd) {
Err = malformedError(*this, "contains LC_DYSYMTAB load command without a " Err = malformedError("contains LC_DYSYMTAB load command without a "
"LC_SYMTAB load command"); "LC_SYMTAB load command");
return; return;
} }
@ -378,39 +378,39 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
MachO::dysymtab_command Dysymtab = MachO::dysymtab_command Dysymtab =
getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
Err = malformedError(*this, "ilocalsym in LC_DYSYMTAB load command " Err = malformedError("ilocalsym in LC_DYSYMTAB load command "
"extends past the end of the symbol table"); "extends past the end of the symbol table");
return; return;
} }
uint64_t BigSize = Dysymtab.ilocalsym; uint64_t BigSize = Dysymtab.ilocalsym;
BigSize += Dysymtab.nlocalsym; BigSize += Dysymtab.nlocalsym;
if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) {
Err = malformedError(*this, "ilocalsym plus nlocalsym in LC_DYSYMTAB load " Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load "
"command extends past the end of the symbol table"); "command extends past the end of the symbol table");
return; return;
} }
if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
Err = malformedError(*this, "nextdefsym in LC_DYSYMTAB load command " Err = malformedError("nextdefsym in LC_DYSYMTAB load command "
"extends past the end of the symbol table"); "extends past the end of the symbol table");
return; return;
} }
BigSize = Dysymtab.iextdefsym; BigSize = Dysymtab.iextdefsym;
BigSize += Dysymtab.nextdefsym; BigSize += Dysymtab.nextdefsym;
if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) {
Err = malformedError(*this, "iextdefsym plus nextdefsym in LC_DYSYMTAB " Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB "
"load command extends past the end of the symbol " "load command extends past the end of the symbol "
"table"); "table");
return; return;
} }
if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) {
Err = malformedError(*this, "nundefsym in LC_DYSYMTAB load command " Err = malformedError("nundefsym in LC_DYSYMTAB load command "
"extends past the end of the symbol table"); "extends past the end of the symbol table");
return; return;
} }
BigSize = Dysymtab.iundefsym; BigSize = Dysymtab.iundefsym;
BigSize += Dysymtab.nundefsym; BigSize += Dysymtab.nundefsym;
if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) {
Err = malformedError(*this, "iundefsym plus nundefsym in LC_DYSYMTAB load " Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load "
" command extends past the end of the symbol table"); " command extends past the end of the symbol table");
return; return;
} }
@ -432,7 +432,7 @@ Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
const char *Start = &StringTable.data()[Entry.n_strx]; const char *Start = &StringTable.data()[Entry.n_strx];
if (Start < getData().begin() || Start >= getData().end()) { if (Start < getData().begin() || Start >= getData().end()) {
return malformedError(*this, "bad string index: " + Twine(Entry.n_strx) + return malformedError("bad string index: " + Twine(Entry.n_strx) +
" for symbol at index " + Twine(getSymbolIndex(Symb))); " for symbol at index " + Twine(getSymbolIndex(Symb)));
} }
return StringRef(Start); return StringRef(Start);
@ -563,7 +563,7 @@ MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
DataRefImpl DRI; DataRefImpl DRI;
DRI.d.a = index - 1; DRI.d.a = index - 1;
if (DRI.d.a >= Sections.size()){ if (DRI.d.a >= Sections.size()){
return malformedError(*this, "bad section index: " + Twine((int)index) + return malformedError("bad section index: " + Twine((int)index) +
" for symbol at index " + Twine(getSymbolIndex(Symb))); " for symbol at index " + Twine(getSymbolIndex(Symb)));
} }
return section_iterator(SectionRef(DRI, this)); return section_iterator(SectionRef(DRI, this));
@ -2395,7 +2395,6 @@ ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
return MachOObjectFile::create(Buffer, false, true); return MachOObjectFile::create(Buffer, false, true);
if (Magic == "\xCF\xFA\xED\xFE") if (Magic == "\xCF\xFA\xED\xFE")
return MachOObjectFile::create(Buffer, true, true); return MachOObjectFile::create(Buffer, true, true);
return make_error<GenericBinaryError>(Buffer.getBufferIdentifier(), return make_error<GenericBinaryError>("Unrecognized MachO magic number",
"Unrecognized MachO magic number",
object_error::invalid_file_type); object_error::invalid_file_type);
} }