Object: Add proper error handling.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133872 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael J. Spencer 2011-06-25 17:55:23 +00:00
parent 76fb9b0e5f
commit 25b15777df
10 changed files with 563 additions and 271 deletions

View File

@ -69,29 +69,33 @@ struct coff_section {
class COFFObjectFile : public ObjectFile { class COFFObjectFile : public ObjectFile {
private: private:
uint64_t HeaderOff;
const coff_file_header *Header; const coff_file_header *Header;
const coff_section *SectionTable; const coff_section *SectionTable;
const coff_symbol *SymbolTable; const coff_symbol *SymbolTable;
const char *StringTable; const char *StringTable;
uint32_t StringTableSize;
const coff_section *getSection(std::size_t index) const; error_code getSection(int32_t index,
const char *getString(std::size_t offset) const; const coff_section *&Res) const;
error_code getString(uint32_t offset, StringRef &Res) const;
const coff_symbol *toSymb(DataRefImpl Symb) const;
const coff_section *toSec(DataRefImpl Sec) const;
protected: protected:
virtual SymbolRef getSymbolNext(DataRefImpl Symb) const; virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual StringRef getSymbolName(DataRefImpl Symb) const; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual uint64_t getSymbolAddress(DataRefImpl Symb) const; virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual uint64_t getSymbolSize(DataRefImpl Symb) const; virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual char getSymbolNMTypeChar(DataRefImpl Symb) const; virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
virtual bool isSymbolInternal(DataRefImpl Symb) const; virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
virtual SectionRef getSectionNext(DataRefImpl Sec) const; virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
virtual StringRef getSectionName(DataRefImpl Sec) const; virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
virtual uint64_t getSectionAddress(DataRefImpl Sec) const; virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
virtual uint64_t getSectionSize(DataRefImpl Sec) const; virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
virtual StringRef getSectionContents(DataRefImpl Sec) const; virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
virtual bool isSectionText(DataRefImpl Sec) const; virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
public: public:
COFFObjectFile(MemoryBuffer *Object, error_code &ec); COFFObjectFile(MemoryBuffer *Object, error_code &ec);

View File

@ -25,7 +25,8 @@ struct object_error {
enum _ { enum _ {
success = 0, success = 0,
invalid_file_type, invalid_file_type,
parse_failed parse_failed,
unexpected_eof
}; };
_ v_; _ v_;

View File

@ -17,6 +17,7 @@
#include "llvm/Object/Binary.h" #include "llvm/Object/Binary.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include <cstring> #include <cstring>
@ -29,7 +30,7 @@ union DataRefImpl {
struct { struct {
uint32_t a, b; uint32_t a, b;
} d; } d;
intptr_t p; uintptr_t p;
}; };
static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) { static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
@ -38,6 +39,19 @@ static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0; return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
} }
class RelocationRef {
DataRefImpl RelocationPimpl;
const ObjectFile *OwningObject;
public:
RelocationRef() : OwningObject(NULL) { std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl)); }
RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
bool operator==(const RelocationRef &Other) const;
error_code getNext(RelocationRef &Result);
};
/// SymbolRef - This is a value type class that represents a single symbol in /// SymbolRef - This is a value type class that represents a single symbol in
/// the list of symbols in the object file. /// the list of symbols in the object file.
class SymbolRef { class SymbolRef {
@ -45,23 +59,24 @@ class SymbolRef {
const ObjectFile *OwningObject; const ObjectFile *OwningObject;
public: public:
SymbolRef() : OwningObject(NULL) { std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl)); }
SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
bool operator==(const SymbolRef &Other) const; bool operator==(const SymbolRef &Other) const;
SymbolRef getNext() const; error_code getNext(SymbolRef &Result) const;
StringRef getName() const; error_code getName(StringRef &Result) const;
uint64_t getAddress() const; error_code getAddress(uint64_t &Result) const;
uint64_t getSize() const; error_code getSize(uint64_t &Result) const;
/// Returns the ascii char that should be displayed in a symbol table dump via /// Returns the ascii char that should be displayed in a symbol table dump via
/// nm for this symbol. /// nm for this symbol.
char getNMTypeChar() const; error_code getNMTypeChar(char &Result) const;
/// Returns true for symbols that are internal to the object file format such /// Returns true for symbols that are internal to the object file format such
/// as section symbols. /// as section symbols.
bool isInternal() const; error_code isInternal(bool &Result) const;
}; };
/// SectionRef - This is a value type class that represents a single section in /// SectionRef - This is a value type class that represents a single section in
@ -71,19 +86,20 @@ class SectionRef {
const ObjectFile *OwningObject; const ObjectFile *OwningObject;
public: public:
SectionRef() : OwningObject(NULL) { std::memset(&SectionPimpl, 0, sizeof(SectionPimpl)); }
SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
bool operator==(const SectionRef &Other) const; bool operator==(const SectionRef &Other) const;
SectionRef getNext() const; error_code getNext(SectionRef &Result) const;
StringRef getName() const; error_code getName(StringRef &Result) const;
uint64_t getAddress() const; error_code getAddress(uint64_t &Result) const;
uint64_t getSize() const; error_code getSize(uint64_t &Result) const;
StringRef getContents() const; error_code getContents(StringRef &Result) const;
// FIXME: Move to the normalization layer when it's created. // FIXME: Move to the normalization layer when it's created.
bool isText() const; error_code isText(bool &Result) const;
}; };
const uint64_t UnknownAddressOrSize = ~0ULL; const uint64_t UnknownAddressOrSize = ~0ULL;
@ -108,22 +124,25 @@ protected:
// entry in the memory mapped object file. SymbolPimpl cannot contain any // entry in the memory mapped object file. SymbolPimpl cannot contain any
// virtual functions because then it could not point into the memory mapped // virtual functions because then it could not point into the memory mapped
// file. // file.
//
// Implementations assume that the DataRefImpl is valid and has not been
// modified externally. It's UB otherwise.
friend class SymbolRef; friend class SymbolRef;
virtual SymbolRef getSymbolNext(DataRefImpl Symb) const = 0; virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
virtual StringRef getSymbolName(DataRefImpl Symb) const = 0; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
virtual uint64_t getSymbolAddress(DataRefImpl Symb) const = 0; virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0; virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
virtual char getSymbolNMTypeChar(DataRefImpl Symb) const = 0; virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
virtual bool isSymbolInternal(DataRefImpl Symb) const = 0; virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
// Same as above for SectionRef. // Same as above for SectionRef.
friend class SectionRef; friend class SectionRef;
virtual SectionRef getSectionNext(DataRefImpl Sec) const = 0; virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
virtual StringRef getSectionName(DataRefImpl Sec) const = 0; virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0; virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
virtual StringRef getSectionContents(DataRefImpl Sec) const = 0; virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
virtual bool isSectionText(DataRefImpl Sec) const = 0; virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
public: public:
@ -146,8 +165,12 @@ public:
return !(*this == other); return !(*this == other);
} }
content_iterator& operator++() { // Preincrement content_iterator& increment(error_code &err) {
Current = Current.getNext(); content_type next;
if (error_code ec = Current.getNext(next))
err = ec;
else
Current = next;
return *this; return *this;
} }
}; };
@ -196,28 +219,28 @@ inline bool SymbolRef::operator==(const SymbolRef &Other) const {
return SymbolPimpl == Other.SymbolPimpl; return SymbolPimpl == Other.SymbolPimpl;
} }
inline SymbolRef SymbolRef::getNext() const { inline error_code SymbolRef::getNext(SymbolRef &Result) const {
return OwningObject->getSymbolNext(SymbolPimpl); return OwningObject->getSymbolNext(SymbolPimpl, Result);
} }
inline StringRef SymbolRef::getName() const { inline error_code SymbolRef::getName(StringRef &Result) const {
return OwningObject->getSymbolName(SymbolPimpl); return OwningObject->getSymbolName(SymbolPimpl, Result);
} }
inline uint64_t SymbolRef::getAddress() const { inline error_code SymbolRef::getAddress(uint64_t &Result) const {
return OwningObject->getSymbolAddress(SymbolPimpl); return OwningObject->getSymbolAddress(SymbolPimpl, Result);
} }
inline uint64_t SymbolRef::getSize() const { inline error_code SymbolRef::getSize(uint64_t &Result) const {
return OwningObject->getSymbolSize(SymbolPimpl); return OwningObject->getSymbolSize(SymbolPimpl, Result);
} }
inline char SymbolRef::getNMTypeChar() const { inline error_code SymbolRef::getNMTypeChar(char &Result) const {
return OwningObject->getSymbolNMTypeChar(SymbolPimpl); return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
} }
inline bool SymbolRef::isInternal() const { inline error_code SymbolRef::isInternal(bool &Result) const {
return OwningObject->isSymbolInternal(SymbolPimpl); return OwningObject->isSymbolInternal(SymbolPimpl, Result);
} }
@ -231,28 +254,28 @@ inline bool SectionRef::operator==(const SectionRef &Other) const {
return SectionPimpl == Other.SectionPimpl; return SectionPimpl == Other.SectionPimpl;
} }
inline SectionRef SectionRef::getNext() const { inline error_code SectionRef::getNext(SectionRef &Result) const {
return OwningObject->getSectionNext(SectionPimpl); return OwningObject->getSectionNext(SectionPimpl, Result);
} }
inline StringRef SectionRef::getName() const { inline error_code SectionRef::getName(StringRef &Result) const {
return OwningObject->getSectionName(SectionPimpl); return OwningObject->getSectionName(SectionPimpl, Result);
} }
inline uint64_t SectionRef::getAddress() const { inline error_code SectionRef::getAddress(uint64_t &Result) const {
return OwningObject->getSectionAddress(SectionPimpl); return OwningObject->getSectionAddress(SectionPimpl, Result);
} }
inline uint64_t SectionRef::getSize() const { inline error_code SectionRef::getSize(uint64_t &Result) const {
return OwningObject->getSectionSize(SectionPimpl); return OwningObject->getSectionSize(SectionPimpl, Result);
} }
inline StringRef SectionRef::getContents() const { inline error_code SectionRef::getContents(StringRef &Result) const {
return OwningObject->getSectionContents(SectionPimpl); return OwningObject->getSectionContents(SectionPimpl, Result);
} }
inline bool SectionRef::isText() const { inline error_code SectionRef::isText(bool &Result) const {
return OwningObject->isSectionText(SectionPimpl); return OwningObject->isSectionText(SectionPimpl, Result);
} }
} // end namespace object } // end namespace object

View File

@ -25,74 +25,165 @@ using support::ulittle32_t;
using support::little16_t; using support::little16_t;
} }
SymbolRef COFFObjectFile::getSymbolNext(DataRefImpl Symb) const { namespace {
const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p); // Returns false if size is greater than the buffer size. And sets ec.
symb += 1 + symb->NumberOfAuxSymbols; bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
Symb.p = reinterpret_cast<intptr_t>(symb); if (m->getBufferSize() < size) {
return SymbolRef(Symb, this); ec = object_error::unexpected_eof;
return false;
}
return true;
} }
StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const { // Returns false if any bytes in [addr, addr + size) fall outsize of m.
const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p); bool checkAddr(const MemoryBuffer *m,
error_code &ec,
uintptr_t addr,
uint64_t size) {
if (addr + size < addr ||
addr + size < size ||
addr + size > uintptr_t(m->getBufferEnd())) {
ec = object_error::unexpected_eof;
return false;
}
return true;
}
}
const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
# ifndef NDEBUG
// Verify that the symbol points to a valid entry in the symbol table.
uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
if (offset < Header->PointerToSymbolTable
|| offset >= Header->PointerToSymbolTable
+ (Header->NumberOfSymbols * sizeof(coff_symbol)))
report_fatal_error("Symbol was outside of symbol table.");
assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
== 0 && "Symbol did not point to the beginning of a symbol");
# endif
return addr;
}
const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
# ifndef NDEBUG
// Verify that the section points to a valid entry in the section table.
if (addr < SectionTable
|| addr >= (SectionTable + Header->NumberOfSections))
report_fatal_error("Section was outside of section table.");
uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
assert(offset % sizeof(coff_section) == 0 &&
"Section did not point to the beginning of a section");
# endif
return addr;
}
error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
SymbolRef &Result) const {
const coff_symbol *symb = toSymb(Symb);
symb += 1 + symb->NumberOfAuxSymbols;
Symb.p = reinterpret_cast<uintptr_t>(symb);
Result = SymbolRef(Symb, this);
return object_error::success;
}
error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
StringRef &Result) const {
const coff_symbol *symb = toSymb(Symb);
// Check for string table entry. First 4 bytes are 0. // Check for string table entry. First 4 bytes are 0.
if (symb->Name.Offset.Zeroes == 0) { if (symb->Name.Offset.Zeroes == 0) {
uint32_t Offset = symb->Name.Offset.Offset; uint32_t Offset = symb->Name.Offset.Offset;
return StringRef(getString(Offset)); if (error_code ec = getString(Offset, Result))
return ec;
return object_error::success;
} }
if (symb->Name.ShortName[7] == 0) if (symb->Name.ShortName[7] == 0)
// Null terminated, let ::strlen figure out the length. // Null terminated, let ::strlen figure out the length.
return StringRef(symb->Name.ShortName); Result = StringRef(symb->Name.ShortName);
// Not null terminated, use all 8 bytes. else
return StringRef(symb->Name.ShortName, 8); // Not null terminated, use all 8 bytes.
Result = StringRef(symb->Name.ShortName, 8);
return object_error::success;
} }
uint64_t COFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p); uint64_t &Result) const {
const coff_section *Section = getSection(symb->SectionNumber); const coff_symbol *symb = toSymb(Symb);
char Type = getSymbolNMTypeChar(Symb); const coff_section *Section;
if (error_code ec = getSection(symb->SectionNumber, Section))
return ec;
char Type;
if (error_code ec = getSymbolNMTypeChar(Symb, Type))
return ec;
if (Type == 'U' || Type == 'w') if (Type == 'U' || Type == 'w')
return UnknownAddressOrSize; Result = UnknownAddressOrSize;
if (Section) else if (Section)
return Section->VirtualAddress + symb->Value; Result = Section->VirtualAddress + symb->Value;
return symb->Value; else
Result = symb->Value;
return object_error::success;
} }
uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const { error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
uint64_t &Result) const {
// FIXME: Return the correct size. This requires looking at all the symbols // FIXME: Return the correct size. This requires looking at all the symbols
// in the same section as this symbol, and looking for either the next // in the same section as this symbol, and looking for either the next
// symbol, or the end of the section. // symbol, or the end of the section.
const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p); const coff_symbol *symb = toSymb(Symb);
const coff_section *Section = getSection(symb->SectionNumber); const coff_section *Section;
char Type = getSymbolNMTypeChar(Symb); if (error_code ec = getSection(symb->SectionNumber, Section))
return ec;
char Type;
if (error_code ec = getSymbolNMTypeChar(Symb, Type))
return ec;
if (Type == 'U' || Type == 'w') if (Type == 'U' || Type == 'w')
return UnknownAddressOrSize; Result = UnknownAddressOrSize;
if (Section) else if (Section)
return Section->SizeOfRawData - symb->Value; Result = Section->SizeOfRawData - symb->Value;
return 0; else
Result = 0;
return object_error::success;
} }
char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const { error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p); char &Result) const {
char ret = StringSwitch<char>(getSymbolName(Symb)) const coff_symbol *symb = toSymb(Symb);
StringRef name;
if (error_code ec = getSymbolName(Symb, name))
return ec;
char ret = StringSwitch<char>(name)
.StartsWith(".debug", 'N') .StartsWith(".debug", 'N')
.StartsWith(".sxdata", 'N') .StartsWith(".sxdata", 'N')
.Default('?'); .Default('?');
if (ret != '?') if (ret != '?') {
return ret; Result = ret;
return object_error::success;
}
uint32_t Characteristics = 0; uint32_t Characteristics = 0;
if (const coff_section *Section = getSection(symb->SectionNumber)) { if (symb->SectionNumber > 0) {
const coff_section *Section;
if (error_code ec = getSection(symb->SectionNumber, Section))
return ec;
Characteristics = Section->Characteristics; Characteristics = Section->Characteristics;
} }
switch (symb->SectionNumber) { switch (symb->SectionNumber) {
case COFF::IMAGE_SYM_UNDEFINED: case COFF::IMAGE_SYM_UNDEFINED:
// Check storage classes. // Check storage classes.
if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
return 'w'; // Don't do ::toupper. Result = 'w';
else return object_error::success; // Don't do ::toupper.
} else
ret = 'u'; ret = 'u';
break; break;
case COFF::IMAGE_SYM_ABSOLUTE: case COFF::IMAGE_SYM_ABSOLUTE:
@ -124,22 +215,28 @@ char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const {
if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
ret = ::toupper(ret); ret = ::toupper(ret);
return ret; Result = ret;
return object_error::success;
} }
bool COFFObjectFile::isSymbolInternal(DataRefImpl Symb) const { error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb,
return false; bool &Result) const {
Result = false;
return object_error::success;
} }
SectionRef COFFObjectFile::getSectionNext(DataRefImpl Sec) const { error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); SectionRef &Result) const {
const coff_section *sec = toSec(Sec);
sec += 1; sec += 1;
Sec.p = reinterpret_cast<intptr_t>(sec); Sec.p = reinterpret_cast<uintptr_t>(sec);
return SectionRef(Sec, this); Result = SectionRef(Sec, this);
return object_error::success;
} }
StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const { error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); StringRef &Result) const {
const coff_section *sec = toSec(Sec);
StringRef name; StringRef name;
if (sec->Name[7] == 0) if (sec->Name[7] == 0)
// Null terminated, let ::strlen figure out the length. // Null terminated, let ::strlen figure out the length.
@ -152,66 +249,117 @@ StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const {
if (name[0] == '/') { if (name[0] == '/') {
uint32_t Offset; uint32_t Offset;
name.substr(1).getAsInteger(10, Offset); name.substr(1).getAsInteger(10, Offset);
return StringRef(getString(Offset)); if (error_code ec = getString(Offset, name))
return ec;
} }
// It's just a normal name. Result = name;
return name; return object_error::success;
} }
uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Sec) const { error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); uint64_t &Result) const {
return sec->VirtualAddress; const coff_section *sec = toSec(Sec);
Result = sec->VirtualAddress;
return object_error::success;
} }
uint64_t COFFObjectFile::getSectionSize(DataRefImpl Sec) const { error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); uint64_t &Result) const {
return sec->SizeOfRawData; const coff_section *sec = toSec(Sec);
Result = sec->SizeOfRawData;
return object_error::success;
} }
StringRef COFFObjectFile::getSectionContents(DataRefImpl Sec) const { error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); StringRef &Result) const {
return StringRef(reinterpret_cast<const char *>(base() const coff_section *sec = toSec(Sec);
+ sec->PointerToRawData), // The only thing that we need to verify is that the contents is contained
sec->SizeOfRawData); // within the file bounds. We don't need to make sure it doesn't cover other
// data, as there's nothing that says that is not allowed.
uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
uintptr_t con_end = con_start + sec->SizeOfRawData;
if (con_end >= uintptr_t(Data->getBufferEnd()))
return object_error::parse_failed;
Result = StringRef(reinterpret_cast<const char*>(con_start),
sec->SizeOfRawData);
return object_error::success;
} }
bool COFFObjectFile::isSectionText(DataRefImpl Sec) const { error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p); bool &Result) const {
return sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; const coff_section *sec = toSec(Sec);
Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
return object_error::success;
} }
COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
: ObjectFile(Binary::isCOFF, Object, ec) { : ObjectFile(Binary::isCOFF, Object, ec) {
// Check that we at least have enough room for a header.
if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
HeaderOff = 0; // The actual starting location of the COFF header in the file. This can be
// non-zero in PE/COFF files.
uint64_t HeaderStart = 0;
// Check if this is a PE/COFF file.
if (base()[0] == 0x4d && base()[1] == 0x5a) { if (base()[0] == 0x4d && base()[1] == 0x5a) {
// PE/COFF, seek through MS-DOS compatibility stub and 4-byte // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
// PE signature to find 'normal' COFF header. // PE signature to find 'normal' COFF header.
HeaderOff += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c); if (!checkSize(Data, ec, 0x3c + 8)) return;
HeaderOff += 4; HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c);
// Check the PE header. ("PE\0\0")
if (StringRef(reinterpret_cast<const char *>(base() + HeaderStart), 4)
!= "PE\0\0") {
ec = object_error::parse_failed;
return;
}
HeaderStart += 4; // Skip the PE Header.
} }
Header = reinterpret_cast<const coff_file_header *>(base() + HeaderOff); Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
return;
SectionTable = SectionTable =
reinterpret_cast<const coff_section *>( base() reinterpret_cast<const coff_section *>( base()
+ HeaderOff + HeaderStart
+ sizeof(coff_file_header) + sizeof(coff_file_header)
+ Header->SizeOfOptionalHeader); + Header->SizeOfOptionalHeader);
if (!checkAddr(Data, ec, uintptr_t(SectionTable),
Header->NumberOfSections * sizeof(coff_section)))
return;
SymbolTable = SymbolTable =
reinterpret_cast<const coff_symbol *>(base() reinterpret_cast<const coff_symbol *>(base()
+ Header->PointerToSymbolTable); + Header->PointerToSymbolTable);
if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
Header->NumberOfSymbols * sizeof(coff_symbol)))
return;
// Find string table. // Find string table.
StringTable = reinterpret_cast<const char *>(base()) StringTable = reinterpret_cast<const char *>(base())
+ Header->PointerToSymbolTable + Header->PointerToSymbolTable
+ Header->NumberOfSymbols * 18; + Header->NumberOfSymbols * sizeof(coff_symbol);
if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
return;
StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
return;
// Check that the string table is null terminated if has any in it.
if (StringTableSize < 4
|| (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
ec = object_error::parse_failed;
return;
}
ec = object_error::success;
} }
ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const { ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
DataRefImpl ret; DataRefImpl ret;
memset(&ret, 0, sizeof(DataRefImpl)); std::memset(&ret, 0, sizeof(DataRefImpl));
ret.p = reinterpret_cast<intptr_t>(SymbolTable); ret.p = reinterpret_cast<intptr_t>(SymbolTable);
return symbol_iterator(SymbolRef(ret, this)); return symbol_iterator(SymbolRef(ret, this));
} }
@ -219,21 +367,21 @@ ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const { ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
// The symbol table ends where the string table begins. // The symbol table ends where the string table begins.
DataRefImpl ret; DataRefImpl ret;
memset(&ret, 0, sizeof(DataRefImpl)); std::memset(&ret, 0, sizeof(DataRefImpl));
ret.p = reinterpret_cast<intptr_t>(StringTable); ret.p = reinterpret_cast<intptr_t>(StringTable);
return symbol_iterator(SymbolRef(ret, this)); return symbol_iterator(SymbolRef(ret, this));
} }
ObjectFile::section_iterator COFFObjectFile::begin_sections() const { ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
DataRefImpl ret; DataRefImpl ret;
memset(&ret, 0, sizeof(DataRefImpl)); std::memset(&ret, 0, sizeof(DataRefImpl));
ret.p = reinterpret_cast<intptr_t>(SectionTable); ret.p = reinterpret_cast<intptr_t>(SectionTable);
return section_iterator(SectionRef(ret, this)); return section_iterator(SectionRef(ret, this));
} }
ObjectFile::section_iterator COFFObjectFile::end_sections() const { ObjectFile::section_iterator COFFObjectFile::end_sections() const {
DataRefImpl ret; DataRefImpl ret;
memset(&ret, 0, sizeof(DataRefImpl)); std::memset(&ret, 0, sizeof(DataRefImpl));
ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections); ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
return section_iterator(SectionRef(ret, this)); return section_iterator(SectionRef(ret, this));
} }
@ -264,18 +412,30 @@ unsigned COFFObjectFile::getArch() const {
} }
} }
const coff_section *COFFObjectFile::getSection(std::size_t index) const { error_code COFFObjectFile::getSection(int32_t index,
if (index > 0 && index <= Header->NumberOfSections) const coff_section *&Result) const {
return SectionTable + (index - 1); // Check for special index values.
return 0; if (index == COFF::IMAGE_SYM_UNDEFINED ||
index == COFF::IMAGE_SYM_ABSOLUTE ||
index == COFF::IMAGE_SYM_DEBUG)
Result = NULL;
else if (index > 0 && index <= Header->NumberOfSections)
// We already verified the section table data, so no need to check again.
Result = SectionTable + (index - 1);
else
return object_error::parse_failed;
return object_error::success;
} }
const char *COFFObjectFile::getString(std::size_t offset) const { error_code COFFObjectFile::getString(uint32_t offset,
const ulittle32_t *StringTableSize = StringRef &Result) const {
reinterpret_cast<const ulittle32_t *>(StringTable); if (StringTableSize <= 4)
if (offset < *StringTableSize) // Tried to get a string from an empty string table.
return StringTable + offset; return object_error::parse_failed;
return 0; if (offset >= StringTableSize)
return object_error::unexpected_eof;
Result = StringRef(StringTable + offset);
return object_error::success;
} }
namespace llvm { namespace llvm {

View File

@ -222,19 +222,19 @@ class ELFObjectFile : public ObjectFile {
const char *getString(const Elf_Shdr *section, uint32_t offset) const; const char *getString(const Elf_Shdr *section, uint32_t offset) const;
protected: protected:
virtual SymbolRef getSymbolNext(DataRefImpl Symb) const; virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual StringRef getSymbolName(DataRefImpl Symb) const; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual uint64_t getSymbolAddress(DataRefImpl Symb) const; virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual uint64_t getSymbolSize(DataRefImpl Symb) const; virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual char getSymbolNMTypeChar(DataRefImpl Symb) const; virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
virtual bool isSymbolInternal(DataRefImpl Symb) const; virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
virtual SectionRef getSectionNext(DataRefImpl Sec) const; virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
virtual StringRef getSectionName(DataRefImpl Sec) const; virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
virtual uint64_t getSectionAddress(DataRefImpl Sec) const; virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
virtual uint64_t getSectionSize(DataRefImpl Sec) const; virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
virtual StringRef getSectionContents(DataRefImpl Sec) const; virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
virtual bool isSectionText(DataRefImpl Sec) const; virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
public: public:
ELFObjectFile(MemoryBuffer *Object, error_code &ec); ELFObjectFile(MemoryBuffer *Object, error_code &ec);
@ -269,8 +269,9 @@ void ELFObjectFile<target_endianness, is64Bits>
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
SymbolRef ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolNext(DataRefImpl Symb) const { ::getSymbolNext(DataRefImpl Symb,
SymbolRef &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
@ -287,63 +288,80 @@ SymbolRef ELFObjectFile<target_endianness, is64Bits>
} }
} }
return SymbolRef(Symb, this); Result = SymbolRef(Symb, this);
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
StringRef ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolName(DataRefImpl Symb) const { ::getSymbolName(DataRefImpl Symb,
StringRef &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb); const Elf_Sym *symb = getSymbol(Symb);
if (symb->st_name == 0) { if (symb->st_name == 0) {
const Elf_Shdr *section = getSection(symb->st_shndx); const Elf_Shdr *section = getSection(symb->st_shndx);
if (!section) if (!section)
return ""; Result = "";
return getString(dot_shstrtab_sec, section->sh_name); else
Result = getString(dot_shstrtab_sec, section->sh_name);
return object_error::success;
} }
// Use the default symbol table name section. // Use the default symbol table name section.
return getString(dot_strtab_sec, symb->st_name); Result = getString(dot_strtab_sec, symb->st_name);
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
uint64_t ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolAddress(DataRefImpl Symb) const { ::getSymbolAddress(DataRefImpl Symb,
uint64_t &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb); const Elf_Sym *symb = getSymbol(Symb);
const Elf_Shdr *Section; const Elf_Shdr *Section;
switch (symb->st_shndx) { switch (symb->st_shndx) {
case ELF::SHN_COMMON: case ELF::SHN_COMMON:
// Undefined symbols have no address yet. // Undefined symbols have no address yet.
case ELF::SHN_UNDEF: return UnknownAddressOrSize; case ELF::SHN_UNDEF:
case ELF::SHN_ABS: return symb->st_value; Result = UnknownAddressOrSize;
return object_error::success;
case ELF::SHN_ABS:
Result = symb->st_value;
return object_error::success;
default: Section = getSection(symb->st_shndx); default: Section = getSection(symb->st_shndx);
} }
switch (symb->getType()) { switch (symb->getType()) {
case ELF::STT_SECTION: return Section ? Section->sh_addr case ELF::STT_SECTION:
: UnknownAddressOrSize; Result = Section ? Section->sh_addr : UnknownAddressOrSize;
return object_error::success;
case ELF::STT_FUNC: case ELF::STT_FUNC:
case ELF::STT_OBJECT: case ELF::STT_OBJECT:
case ELF::STT_NOTYPE: case ELF::STT_NOTYPE:
return symb->st_value; Result = symb->st_value;
default: return UnknownAddressOrSize; return object_error::success;
default:
Result = UnknownAddressOrSize;
return object_error::success;
} }
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
uint64_t ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolSize(DataRefImpl Symb) const { ::getSymbolSize(DataRefImpl Symb,
uint64_t &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb); const Elf_Sym *symb = getSymbol(Symb);
if (symb->st_size == 0) if (symb->st_size == 0)
return UnknownAddressOrSize; Result = UnknownAddressOrSize;
return symb->st_size; Result = symb->st_size;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
char ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolNMTypeChar(DataRefImpl Symb) const { ::getSymbolNMTypeChar(DataRefImpl Symb,
char &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb); const Elf_Sym *symb = getSymbol(Symb);
const Elf_Shdr *Section = getSection(symb->st_shndx); const Elf_Shdr *Section = getSection(symb->st_shndx);
@ -390,71 +408,91 @@ char ELFObjectFile<target_endianness, is64Bits>
ret = 'W'; ret = 'W';
} }
if (ret == '?' && symb->getType() == ELF::STT_SECTION) if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
return StringSwitch<char>(getSymbolName(Symb)) StringRef name;
if (error_code ec = getSymbolName(Symb, name))
return ec;
Result = StringSwitch<char>(name)
.StartsWith(".debug", 'N') .StartsWith(".debug", 'N')
.StartsWith(".note", 'n'); .StartsWith(".note", 'n');
return object_error::success;
}
return ret; Result = ret;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
bool ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::isSymbolInternal(DataRefImpl Symb) const { ::isSymbolInternal(DataRefImpl Symb,
bool &Result) const {
validateSymbol(Symb); validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb); const Elf_Sym *symb = getSymbol(Symb);
if ( symb->getType() == ELF::STT_FILE if ( symb->getType() == ELF::STT_FILE
|| symb->getType() == ELF::STT_SECTION) || symb->getType() == ELF::STT_SECTION)
return true; Result = true;
return false; Result = false;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
SectionRef ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSectionNext(DataRefImpl Sec) const { ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
sec += Header->e_shentsize; sec += Header->e_shentsize;
Sec.p = reinterpret_cast<intptr_t>(sec); Sec.p = reinterpret_cast<intptr_t>(sec);
return SectionRef(Sec, this); Result = SectionRef(Sec, this);
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
StringRef ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSectionName(DataRefImpl Sec) const { ::getSectionName(DataRefImpl Sec,
StringRef &Result) const {
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
return StringRef(getString(dot_shstrtab_sec, sec->sh_name)); Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
uint64_t ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSectionAddress(DataRefImpl Sec) const { ::getSectionAddress(DataRefImpl Sec,
uint64_t &Result) const {
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
return sec->sh_addr; Result = sec->sh_addr;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
uint64_t ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSectionSize(DataRefImpl Sec) const { ::getSectionSize(DataRefImpl Sec,
uint64_t &Result) const {
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
return sec->sh_size; Result = sec->sh_size;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
StringRef ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::getSectionContents(DataRefImpl Sec) const { ::getSectionContents(DataRefImpl Sec,
StringRef &Result) const {
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
const char *start = (char*)base() + sec->sh_offset; const char *start = (const char*)base() + sec->sh_offset;
return StringRef(start, sec->sh_size); Result = StringRef(start, sec->sh_size);
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>
bool ELFObjectFile<target_endianness, is64Bits> error_code ELFObjectFile<target_endianness, is64Bits>
::isSectionText(DataRefImpl Sec) const { ::isSectionText(DataRefImpl Sec,
bool &Result) const {
const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
if (sec->sh_flags & ELF::SHF_EXECINSTR) if (sec->sh_flags & ELF::SHF_EXECINSTR)
return true; Result = true;
return false; else
Result = false;
return object_error::success;
} }
template<support::endianness target_endianness, bool is64Bits> template<support::endianness target_endianness, bool is64Bits>

View File

@ -37,6 +37,8 @@ std::string _object_error_category::message(int ev) const {
return "The file was not recognized as a valid object file"; return "The file was not recognized as a valid object file";
case object_error::parse_failed: case object_error::parse_failed:
return "Invalid data was encountered while parsing the file"; return "Invalid data was encountered while parsing the file";
case object_error::unexpected_eof:
return "The end of the file was unexpectedly encountered";
default: default:
llvm_unreachable("An enumerator of object_error does not have a message " llvm_unreachable("An enumerator of object_error does not have a message "
"defined."); "defined.");

View File

@ -47,19 +47,19 @@ public:
virtual unsigned getArch() const; virtual unsigned getArch() const;
protected: protected:
virtual SymbolRef getSymbolNext(DataRefImpl Symb) const; virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual StringRef getSymbolName(DataRefImpl Symb) const; virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual uint64_t getSymbolAddress(DataRefImpl Symb) const; virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual uint64_t getSymbolSize(DataRefImpl Symb) const; virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual char getSymbolNMTypeChar(DataRefImpl Symb) const; virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
virtual bool isSymbolInternal(DataRefImpl Symb) const; virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
virtual SectionRef getSectionNext(DataRefImpl Sec) const; virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
virtual StringRef getSectionName(DataRefImpl Sec) const; virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
virtual uint64_t getSectionAddress(DataRefImpl Sec) const; virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
virtual uint64_t getSectionSize(DataRefImpl Sec) const; virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
virtual StringRef getSectionContents(DataRefImpl Sec) const; virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
virtual bool isSectionText(DataRefImpl Sec) const; virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
private: private:
MachOObject *MachOObj; MachOObject *MachOObj;
@ -115,29 +115,38 @@ void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
} }
SymbolRef MachOObjectFile::getSymbolNext(DataRefImpl DRI) const { error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
SymbolRef &Result) const {
DRI.d.b++; DRI.d.b++;
moveToNextSymbol(DRI); moveToNextSymbol(DRI);
return SymbolRef(DRI, this); Result = SymbolRef(DRI, this);
return object_error::success;
} }
StringRef MachOObjectFile::getSymbolName(DataRefImpl DRI) const { error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
StringRef &Result) const {
InMemoryStruct<macho::SymbolTableEntry> Entry; InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry); getSymbolTableEntry(DRI, Entry);
return MachOObj->getStringAtIndex(Entry->StringIndex); Result = MachOObj->getStringAtIndex(Entry->StringIndex);
return object_error::success;
} }
uint64_t MachOObjectFile::getSymbolAddress(DataRefImpl DRI) const { error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
uint64_t &Result) const {
InMemoryStruct<macho::SymbolTableEntry> Entry; InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry); getSymbolTableEntry(DRI, Entry);
return Entry->Value; Result = Entry->Value;
return object_error::success;
} }
uint64_t MachOObjectFile::getSymbolSize(DataRefImpl DRI) const { error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
return UnknownAddressOrSize; uint64_t &Result) const {
Result = UnknownAddressOrSize;
return object_error::success;
} }
char MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI) const { error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
char &Result) const {
InMemoryStruct<macho::SymbolTableEntry> Entry; InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry); getSymbolTableEntry(DRI, Entry);
@ -157,13 +166,16 @@ char MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI) const {
if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern)) if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern))
Char = toupper(Char); Char = toupper(Char);
return Char; Result = Char;
return object_error::success;
} }
bool MachOObjectFile::isSymbolInternal(DataRefImpl DRI) const { error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
bool &Result) const {
InMemoryStruct<macho::SymbolTableEntry> Entry; InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry); getSymbolTableEntry(DRI, Entry);
return Entry->Flags & macho::STF_StabsEntryMask; Result = Entry->Flags & macho::STF_StabsEntryMask;
return object_error::success;
} }
ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const { ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
@ -205,10 +217,12 @@ void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
} }
} }
SectionRef MachOObjectFile::getSectionNext(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
SectionRef &Result) const {
DRI.d.b++; DRI.d.b++;
moveToNextSection(DRI); moveToNextSection(DRI);
return SectionRef(DRI, this); Result = SectionRef(DRI, this);
return object_error::success;
} }
void void
@ -220,43 +234,53 @@ MachOObjectFile::getSection(DataRefImpl DRI,
MachOObj->ReadSection(LCI, DRI.d.b, Res); MachOObj->ReadSection(LCI, DRI.d.b, Res);
} }
StringRef MachOObjectFile::getSectionName(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
StringRef &Result) const {
InMemoryStruct<macho::SegmentLoadCommand> SLC; InMemoryStruct<macho::SegmentLoadCommand> SLC;
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
MachOObj->ReadSegmentLoadCommand(LCI, SLC); MachOObj->ReadSegmentLoadCommand(LCI, SLC);
InMemoryStruct<macho::Section> Sect; InMemoryStruct<macho::Section> Sect;
MachOObj->ReadSection(LCI, DRI.d.b, Sect); MachOObj->ReadSection(LCI, DRI.d.b, Sect);
static char Result[34]; static char result[34];
strcpy(Result, SLC->Name); strcpy(result, SLC->Name);
strcat(Result, ","); strcat(result, ",");
strcat(Result, Sect->Name); strcat(result, Sect->Name);
return StringRef(Result); Result = StringRef(result);
return object_error::success;
} }
uint64_t MachOObjectFile::getSectionAddress(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
uint64_t &Result) const {
InMemoryStruct<macho::Section> Sect; InMemoryStruct<macho::Section> Sect;
getSection(DRI, Sect); getSection(DRI, Sect);
return Sect->Address; Result = Sect->Address;
return object_error::success;
} }
uint64_t MachOObjectFile::getSectionSize(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
uint64_t &Result) const {
InMemoryStruct<macho::Section> Sect; InMemoryStruct<macho::Section> Sect;
getSection(DRI, Sect); getSection(DRI, Sect);
return Sect->Size; Result = Sect->Size;
return object_error::success;
} }
StringRef MachOObjectFile::getSectionContents(DataRefImpl DRI) const { error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
StringRef &Result) const {
InMemoryStruct<macho::Section> Sect; InMemoryStruct<macho::Section> Sect;
getSection(DRI, Sect); getSection(DRI, Sect);
return MachOObj->getData(Sect->Offset, Sect->Size); Result = MachOObj->getData(Sect->Offset, Sect->Size);
return object_error::success;
} }
bool MachOObjectFile::isSectionText(DataRefImpl DRI) const { error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
bool &Result) const {
InMemoryStruct<macho::SegmentLoadCommand> SLC; InMemoryStruct<macho::SegmentLoadCommand> SLC;
LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
MachOObj->ReadSegmentLoadCommand(LCI, SLC); MachOObj->ReadSegmentLoadCommand(LCI, SLC);
return !strcmp(SLC->Name, "__TEXT"); Result = !strcmp(SLC->Name, "__TEXT");
return object_error::success;
} }
ObjectFile::section_iterator MachOObjectFile::begin_sections() const { ObjectFile::section_iterator MachOObjectFile::begin_sections() const {

View File

@ -41,19 +41,28 @@ LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
} }
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) { void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
// We can't use unwrap() here because the argument to ++ must be an lvalue. error_code ec;
++*reinterpret_cast<ObjectFile::section_iterator*>(SI); unwrap(SI)->increment(ec);
if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
} }
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) { const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
return (*unwrap(SI))->getName().data(); StringRef ret;
if (error_code ec = (*unwrap(SI))->getName(ret))
report_fatal_error(ec.message());
return ret.data();
} }
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
return (*unwrap(SI))->getSize(); uint64_t ret;
if (error_code ec = (*unwrap(SI))->getSize(ret))
report_fatal_error(ec.message());
return ret;
} }
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) { const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
return (*unwrap(SI))->getContents().data(); StringRef ret;
if (error_code ec = (*unwrap(SI))->getContents(ret))
report_fatal_error(ec.message());
return ret.data();
} }

View File

@ -143,6 +143,14 @@ namespace {
StringRef CurrentFilename; StringRef CurrentFilename;
typedef std::vector<NMSymbol> SymbolListT; typedef std::vector<NMSymbol> SymbolListT;
SymbolListT SymbolList; SymbolListT SymbolList;
bool error(error_code ec) {
if (!ec) return false;
outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
outs().flush();
return true;
}
} }
static void SortAndPrintSymbolList() { static void SortAndPrintSymbolList() {
@ -261,19 +269,25 @@ static void DumpSymbolNamesFromModule(Module *M) {
} }
static void DumpSymbolNamesFromObject(ObjectFile *obj) { static void DumpSymbolNamesFromObject(ObjectFile *obj) {
error_code ec;
for (ObjectFile::symbol_iterator i = obj->begin_symbols(), for (ObjectFile::symbol_iterator i = obj->begin_symbols(),
e = obj->end_symbols(); i != e; ++i) { e = obj->end_symbols();
if (!DebugSyms && i->isInternal()) i != e; i.increment(ec)) {
if (error(ec)) break;
bool internal;
if (error(i->isInternal(internal))) break;
if (!DebugSyms && internal)
continue; continue;
NMSymbol s; NMSymbol s;
s.Size = object::UnknownAddressOrSize; s.Size = object::UnknownAddressOrSize;
s.Address = object::UnknownAddressOrSize; s.Address = object::UnknownAddressOrSize;
if (PrintSize || SizeSort) if (PrintSize || SizeSort) {
s.Size = i->getSize(); if (error(i->getSize(s.Size))) break;
}
if (PrintAddress) if (PrintAddress)
s.Address = i->getAddress(); if (error(i->getAddress(s.Address))) break;
s.TypeChar = i->getNMTypeChar(); if (error(i->getNMTypeChar(s.TypeChar))) break;
s.Name = i->getName(); if (error(i->getName(s.Name))) break;
SymbolList.push_back(s); SymbolList.push_back(s);
} }

View File

@ -69,6 +69,14 @@ namespace {
"see -version for available targets")); "see -version for available targets"));
StringRef ToolName; StringRef ToolName;
bool error(error_code ec) {
if (!ec) return false;
outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
outs().flush();
return true;
}
} }
static const Target *GetTarget(const ObjectFile *Obj = NULL) { static const Target *GetTarget(const ObjectFile *Obj = NULL) {
@ -161,12 +169,18 @@ static void DisassembleInput(const StringRef &Filename) {
outs() << Filename outs() << Filename
<< ":\tfile format " << Obj->getFileFormatName() << "\n\n\n"; << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n";
error_code ec;
for (ObjectFile::section_iterator i = Obj->begin_sections(), for (ObjectFile::section_iterator i = Obj->begin_sections(),
e = Obj->end_sections(); e = Obj->end_sections();
i != e; ++i) { i != e; i.increment(ec)) {
if (!i->isText()) if (error(ec)) break;
continue; bool text;
outs() << "Disassembly of section " << i->getName() << ":\n\n"; if (error(i->isText(text))) break;
if (!text) continue;
StringRef name;
if (error(i->getName(name))) break;
outs() << "Disassembly of section " << name << ":\n\n";
// Set up disassembler. // Set up disassembler.
OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName)); OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName));
@ -202,7 +216,8 @@ static void DisassembleInput(const StringRef &Filename) {
return; return;
} }
StringRef Bytes = i->getContents(); StringRef Bytes;
if (error(i->getContents(Bytes))) break;
StringRefMemoryObject memoryObject(Bytes); StringRefMemoryObject memoryObject(Bytes);
uint64_t Size; uint64_t Size;
uint64_t Index; uint64_t Index;
@ -217,7 +232,9 @@ static void DisassembleInput(const StringRef &Filename) {
# endif # endif
if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) { if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) {
outs() << format("%8x:\t", i->getAddress() + Index); uint64_t addr;
if (error(i->getAddress(addr))) break;
outs() << format("%8x:\t", addr + Index);
DumpBytes(StringRef(Bytes.data() + Index, Size)); DumpBytes(StringRef(Bytes.data() + Index, Size));
IP->printInst(&Inst, outs()); IP->printInst(&Inst, outs());
outs() << "\n"; outs() << "\n";