Remove bogus std::error_code returns form SectionRef.

There are two methods in SectionRef that can fail:

* getName: The index into the string table can be invalid.
* getContents: The section might point to invalid contents.

Every other method will always succeed and returning and std::error_code just
complicates the code. For example, a section can have an invalid alignment,
but if we are able to get to the section structure at all and create a
SectionRef, we will always be able to read that invalid alignment.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219314 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2014-10-08 15:28:58 +00:00
parent 1a98f792a5
commit 8175be535a
22 changed files with 252 additions and 470 deletions

View File

@ -546,24 +546,19 @@ protected:
void moveSectionNext(DataRefImpl &Sec) const override; void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec, std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAddress(DataRefImpl Sec, uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t &Res) const override; uint64_t getSectionSize(DataRefImpl Sec) const override;
std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
std::error_code getSectionContents(DataRefImpl Sec, std::error_code getSectionContents(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAlignment(DataRefImpl Sec, uint64_t getSectionAlignment(DataRefImpl Sec) const override;
uint64_t &Res) const override; bool isSectionText(DataRefImpl Sec) const override;
std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override; bool isSectionData(DataRefImpl Sec) const override;
std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override; bool isSectionBSS(DataRefImpl Sec) const override;
std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override; bool isSectionVirtual(DataRefImpl Sec) const override;
std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override; bool isSectionZeroInit(DataRefImpl Sec) const override;
std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override; bool isSectionReadOnlyData(DataRefImpl Sec) const override;
std::error_code isSectionReadOnlyData(DataRefImpl Sec, bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
bool &Res) const override; bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
bool &Res) const override;
std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
bool &Result) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override;

View File

@ -89,24 +89,19 @@ protected:
void moveSectionNext(DataRefImpl &Sec) const override; void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec, std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAddress(DataRefImpl Sec, uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t &Res) const override; uint64_t getSectionSize(DataRefImpl Sec) const override;
std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
std::error_code getSectionContents(DataRefImpl Sec, std::error_code getSectionContents(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAlignment(DataRefImpl Sec, uint64_t getSectionAlignment(DataRefImpl Sec) const override;
uint64_t &Res) const override; bool isSectionText(DataRefImpl Sec) const override;
std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override; bool isSectionData(DataRefImpl Sec) const override;
std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override; bool isSectionBSS(DataRefImpl Sec) const override;
std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override; bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
std::error_code isSectionRequiredForExecution(DataRefImpl Sec, bool isSectionVirtual(DataRefImpl Sec) const override;
bool &Res) const override; bool isSectionZeroInit(DataRefImpl Sec) const override;
std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override; bool isSectionReadOnlyData(DataRefImpl Sec) const override;
std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override; bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
std::error_code isSectionReadOnlyData(DataRefImpl Sec,
bool &Res) const override;
std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
bool &Result) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override;
section_iterator getRelocatedSection(DataRefImpl Sec) const override; section_iterator getRelocatedSection(DataRefImpl Sec) const override;
@ -413,17 +408,13 @@ std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec, uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
uint64_t &Result) const { return toELFShdrIter(Sec)->sh_addr;
Result = toELFShdrIter(Sec)->sh_addr;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec, uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
uint64_t &Result) const { return toELFShdrIter(Sec)->sh_size;
Result = toELFShdrIter(Sec)->sh_size;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
@ -436,79 +427,59 @@ ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
} }
template <class ELFT> template <class ELFT>
std::error_code uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec, return toELFShdrIter(Sec)->sh_addralign;
uint64_t &Result) const {
Result = toELFShdrIter(Sec)->sh_addralign;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
bool &Result) const { return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_PROGBITS; EShdr->sh_type == ELF::SHT_PROGBITS;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_NOBITS; EShdr->sh_type == ELF::SHT_NOBITS;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code bool ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec) const {
ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec, return toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
bool &Result) const {
Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
bool &Result) const { return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec) const {
bool &Result) const { return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec, bool ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec) const {
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); return !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
return object_error::success;
} }
template <class ELFT> template <class ELFT>
std::error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec, bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb, DataRefImpl Symb) const {
bool &Result) const {
Elf_Sym_Iter ESym = toELFSymIter(Symb); Elf_Sym_Iter ESym = toELFSymIter(Symb);
uintX_t Index = ESym->st_shndx; uintX_t Index = ESym->st_shndx;
bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE; bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx)); return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
return object_error::success;
} }
template <class ELFT> template <class ELFT>

View File

@ -215,24 +215,19 @@ public:
void moveSectionNext(DataRefImpl &Sec) const override; void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec, std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAddress(DataRefImpl Sec, uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t &Res) const override; uint64_t getSectionSize(DataRefImpl Sec) const override;
std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
std::error_code getSectionContents(DataRefImpl Sec, std::error_code getSectionContents(DataRefImpl Sec,
StringRef &Res) const override; StringRef &Res) const override;
std::error_code getSectionAlignment(DataRefImpl Sec, uint64_t getSectionAlignment(DataRefImpl Sec) const override;
uint64_t &Res) const override; bool isSectionText(DataRefImpl Sec) const override;
std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override; bool isSectionData(DataRefImpl Sec) const override;
std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override; bool isSectionBSS(DataRefImpl Sec) const override;
std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override; bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
std::error_code isSectionRequiredForExecution(DataRefImpl Sec, bool isSectionVirtual(DataRefImpl Sec) const override;
bool &Res) const override; bool isSectionZeroInit(DataRefImpl Sec) const override;
std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override; bool isSectionReadOnlyData(DataRefImpl Sec) const override;
std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override; bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
std::error_code isSectionReadOnlyData(DataRefImpl Sec,
bool &Res) const override;
std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
bool &Result) const override;
relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override;

View File

@ -95,22 +95,22 @@ public:
void moveNext(); void moveNext();
std::error_code getName(StringRef &Result) const; std::error_code getName(StringRef &Result) const;
std::error_code getAddress(uint64_t &Result) const; uint64_t getAddress() const;
std::error_code getSize(uint64_t &Result) const; uint64_t getSize() const;
std::error_code getContents(StringRef &Result) const; std::error_code getContents(StringRef &Result) const;
/// @brief Get the alignment of this section as the actual value (not log 2). /// @brief Get the alignment of this section as the actual value (not log 2).
std::error_code getAlignment(uint64_t &Result) const; uint64_t getAlignment() const;
std::error_code isText(bool &Result) const; bool isText() const;
std::error_code isData(bool &Result) const; bool isData() const;
std::error_code isBSS(bool &Result) const; bool isBSS() const;
std::error_code isRequiredForExecution(bool &Result) const; bool isRequiredForExecution() const;
std::error_code isVirtual(bool &Result) const; bool isVirtual() const;
std::error_code isZeroInit(bool &Result) const; bool isZeroInit() const;
std::error_code isReadOnlyData(bool &Result) const; bool isReadOnlyData() const;
std::error_code containsSymbol(SymbolRef S, bool &Result) const; bool containsSymbol(SymbolRef S) const;
relocation_iterator relocation_begin() const; relocation_iterator relocation_begin() const;
relocation_iterator relocation_end() const; relocation_iterator relocation_end() const;
@ -225,29 +225,21 @@ protected:
virtual void moveSectionNext(DataRefImpl &Sec) const = 0; virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
virtual std::error_code getSectionName(DataRefImpl Sec, virtual std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const = 0; StringRef &Res) const = 0;
virtual std::error_code getSectionAddress(DataRefImpl Sec, virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
uint64_t &Res) const = 0; virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
virtual std::error_code getSectionSize(DataRefImpl Sec,
uint64_t &Res) const = 0;
virtual std::error_code getSectionContents(DataRefImpl Sec, virtual std::error_code getSectionContents(DataRefImpl Sec,
StringRef &Res) const = 0; StringRef &Res) const = 0;
virtual std::error_code getSectionAlignment(DataRefImpl Sec, virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
uint64_t &Res) const = 0; virtual bool isSectionText(DataRefImpl Sec) const = 0;
virtual std::error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0; virtual bool isSectionData(DataRefImpl Sec) const = 0;
virtual std::error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0; virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
virtual std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0; virtual bool isSectionRequiredForExecution(DataRefImpl Sec) const = 0;
virtual std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
bool &Res) const = 0;
// A section is 'virtual' if its contents aren't present in the object image. // A section is 'virtual' if its contents aren't present in the object image.
virtual std::error_code isSectionVirtual(DataRefImpl Sec, virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
bool &Res) const = 0; virtual bool isSectionZeroInit(DataRefImpl Sec) const = 0;
virtual std::error_code isSectionZeroInit(DataRefImpl Sec, virtual bool isSectionReadOnlyData(DataRefImpl Sec) const = 0;
bool &Res) const = 0; virtual bool sectionContainsSymbol(DataRefImpl Sec,
virtual std::error_code isSectionReadOnlyData(DataRefImpl Sec, DataRefImpl Symb) const = 0;
bool &Res) const = 0;
virtual std::error_code sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb,
bool &Result) const = 0;
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0; virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0; virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const; virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
@ -397,54 +389,53 @@ inline std::error_code SectionRef::getName(StringRef &Result) const {
return OwningObject->getSectionName(SectionPimpl, Result); return OwningObject->getSectionName(SectionPimpl, Result);
} }
inline std::error_code SectionRef::getAddress(uint64_t &Result) const { inline uint64_t SectionRef::getAddress() const {
return OwningObject->getSectionAddress(SectionPimpl, Result); return OwningObject->getSectionAddress(SectionPimpl);
} }
inline std::error_code SectionRef::getSize(uint64_t &Result) const { inline uint64_t SectionRef::getSize() const {
return OwningObject->getSectionSize(SectionPimpl, Result); return OwningObject->getSectionSize(SectionPimpl);
} }
inline std::error_code SectionRef::getContents(StringRef &Result) const { inline std::error_code SectionRef::getContents(StringRef &Result) const {
return OwningObject->getSectionContents(SectionPimpl, Result); return OwningObject->getSectionContents(SectionPimpl, Result);
} }
inline std::error_code SectionRef::getAlignment(uint64_t &Result) const { inline uint64_t SectionRef::getAlignment() const {
return OwningObject->getSectionAlignment(SectionPimpl, Result); return OwningObject->getSectionAlignment(SectionPimpl);
} }
inline std::error_code SectionRef::isText(bool &Result) const { inline bool SectionRef::isText() const {
return OwningObject->isSectionText(SectionPimpl, Result); return OwningObject->isSectionText(SectionPimpl);
} }
inline std::error_code SectionRef::isData(bool &Result) const { inline bool SectionRef::isData() const {
return OwningObject->isSectionData(SectionPimpl, Result); return OwningObject->isSectionData(SectionPimpl);
} }
inline std::error_code SectionRef::isBSS(bool &Result) const { inline bool SectionRef::isBSS() const {
return OwningObject->isSectionBSS(SectionPimpl, Result); return OwningObject->isSectionBSS(SectionPimpl);
} }
inline std::error_code SectionRef::isRequiredForExecution(bool &Result) const { inline bool SectionRef::isRequiredForExecution() const {
return OwningObject->isSectionRequiredForExecution(SectionPimpl, Result); return OwningObject->isSectionRequiredForExecution(SectionPimpl);
} }
inline std::error_code SectionRef::isVirtual(bool &Result) const { inline bool SectionRef::isVirtual() const {
return OwningObject->isSectionVirtual(SectionPimpl, Result); return OwningObject->isSectionVirtual(SectionPimpl);
} }
inline std::error_code SectionRef::isZeroInit(bool &Result) const { inline bool SectionRef::isZeroInit() const {
return OwningObject->isSectionZeroInit(SectionPimpl, Result); return OwningObject->isSectionZeroInit(SectionPimpl);
} }
inline std::error_code SectionRef::isReadOnlyData(bool &Result) const { inline bool SectionRef::isReadOnlyData() const {
return OwningObject->isSectionReadOnlyData(SectionPimpl, Result); return OwningObject->isSectionReadOnlyData(SectionPimpl);
} }
inline std::error_code SectionRef::containsSymbol(SymbolRef S, inline bool SectionRef::containsSymbol(SymbolRef S) const {
bool &Result) const {
return OwningObject->sectionContainsSymbol(SectionPimpl, return OwningObject->sectionContainsSymbol(SectionPimpl,
S.getRawDataRefImpl(), Result); S.getRawDataRefImpl());
} }
inline relocation_iterator SectionRef::relocation_begin() const { inline relocation_iterator SectionRef::relocation_begin() const {

View File

@ -516,12 +516,10 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile &Obj)
StringRef name; StringRef name;
Section.getName(name); Section.getName(name);
// Skip BSS and Virtual sections, they aren't interesting. // Skip BSS and Virtual sections, they aren't interesting.
bool IsBSS; bool IsBSS = Section.isBSS();
Section.isBSS(IsBSS);
if (IsBSS) if (IsBSS)
continue; continue;
bool IsVirtual; bool IsVirtual = Section.isVirtual();
Section.isVirtual(IsVirtual);
if (IsVirtual) if (IsVirtual)
continue; continue;
StringRef data; StringRef data;
@ -612,8 +610,7 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile &Obj)
} }
if (Section.relocation_begin() != Section.relocation_end()) { if (Section.relocation_begin() != Section.relocation_end()) {
uint64_t SectionSize; uint64_t SectionSize = RelocatedSection->getSize();
RelocatedSection->getSize(SectionSize);
for (const RelocationRef &Reloc : Section.relocations()) { for (const RelocationRef &Reloc : Section.relocations()) {
uint64_t Address; uint64_t Address;
Reloc.getOffset(Address); Reloc.getOffset(Address);

View File

@ -134,10 +134,7 @@ static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
return object_error::success; return object_error::success;
} }
uint64_t SectionAddress; uint64_t SectionAddress = SecI->getAddress();
if (std::error_code EC = SecI->getAddress(SectionAddress))
return EC;
Result = Address - SectionAddress; Result = Address - SectionAddress;
return object_error::success; return object_error::success;
} }
@ -199,14 +196,13 @@ RuntimeDyldImpl::loadObject(std::unique_ptr<ObjectImage> Obj) {
SymType == object::SymbolRef::ST_Unknown) { SymType == object::SymbolRef::ST_Unknown) {
uint64_t SectOffset; uint64_t SectOffset;
StringRef SectionData; StringRef SectionData;
bool IsCode;
section_iterator SI = Obj->end_sections(); section_iterator SI = Obj->end_sections();
Check(getOffset(*I, SectOffset)); Check(getOffset(*I, SectOffset));
Check(I->getSection(SI)); Check(I->getSection(SI));
if (SI == Obj->end_sections()) if (SI == Obj->end_sections())
continue; continue;
Check(SI->getContents(SectionData)); Check(SI->getContents(SectionData));
Check(SI->isText(IsCode)); bool IsCode = SI->isText();
unsigned SectionID = unsigned SectionID =
findOrEmitSection(*Obj, *SI, IsCode, LocalSections); findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
@ -236,8 +232,7 @@ RuntimeDyldImpl::loadObject(std::unique_ptr<ObjectImage> Obj) {
if (I == E && !ProcessAllSections) if (I == E && !ProcessAllSections)
continue; continue;
bool IsCode = false; bool IsCode = RelocatedSection->isText();
Check(RelocatedSection->isText(IsCode));
SectionID = SectionID =
findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections); findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
@ -291,20 +286,15 @@ void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
SI != SE; ++SI) { SI != SE; ++SI) {
const SectionRef &Section = *SI; const SectionRef &Section = *SI;
bool IsRequired; bool IsRequired = Section.isRequiredForExecution();
Check(Section.isRequiredForExecution(IsRequired));
// Consider only the sections that are required to be loaded for execution // Consider only the sections that are required to be loaded for execution
if (IsRequired) { if (IsRequired) {
uint64_t DataSize = 0;
uint64_t Alignment64 = 0;
bool IsCode = false;
bool IsReadOnly = false;
StringRef Name; StringRef Name;
Check(Section.getSize(DataSize)); uint64_t DataSize = Section.getSize();
Check(Section.getAlignment(Alignment64)); uint64_t Alignment64 = Section.getAlignment();
Check(Section.isText(IsCode)); bool IsCode = Section.isText();
Check(Section.isReadOnlyData(IsReadOnly)); bool IsReadOnly = Section.isReadOnlyData();
Check(Section.getName(Name)); Check(Section.getName(Name));
unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
@ -386,10 +376,8 @@ unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
} }
// Get section data size and alignment // Get section data size and alignment
uint64_t Alignment64; uint64_t DataSize = Section.getSize();
uint64_t DataSize; uint64_t Alignment64 = Section.getAlignment();
Check(Section.getSize(DataSize));
Check(Section.getAlignment(Alignment64));
// Add stubbuf size alignment // Add stubbuf size alignment
unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
@ -473,24 +461,18 @@ unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
const SectionRef &Section, bool IsCode) { const SectionRef &Section, bool IsCode) {
StringRef data; StringRef data;
uint64_t Alignment64;
Check(Section.getContents(data)); Check(Section.getContents(data));
Check(Section.getAlignment(Alignment64)); uint64_t Alignment64 = Section.getAlignment();
unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
bool IsRequired;
bool IsVirtual;
bool IsZeroInit;
bool IsReadOnly;
uint64_t DataSize;
unsigned PaddingSize = 0; unsigned PaddingSize = 0;
unsigned StubBufSize = 0; unsigned StubBufSize = 0;
StringRef Name; StringRef Name;
Check(Section.isRequiredForExecution(IsRequired)); bool IsRequired = Section.isRequiredForExecution();
Check(Section.isVirtual(IsVirtual)); bool IsVirtual = Section.isVirtual();
Check(Section.isZeroInit(IsZeroInit)); bool IsZeroInit = Section.isZeroInit();
Check(Section.isReadOnlyData(IsReadOnly)); bool IsReadOnly = Section.isReadOnlyData();
Check(Section.getSize(DataSize)); uint64_t DataSize = Section.getSize();
Check(Section.getName(Name)); Check(Section.getName(Name));
StubBufSize = computeSectionStubBufSize(Obj, Section); StubBufSize = computeSectionStubBufSize(Obj, Section);

View File

@ -702,8 +702,7 @@ void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
section_iterator tsi(Obj.end_sections()); section_iterator tsi(Obj.end_sections());
check(TargetSymbol->getSection(tsi)); check(TargetSymbol->getSection(tsi));
bool IsCode = false; bool IsCode = tsi->isText();
tsi->isText(IsCode);
Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections); Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rel.Addend = (intptr_t)Addend; Rel.Addend = (intptr_t)Addend;
return; return;
@ -983,9 +982,7 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
if (si == Obj.end_sections()) if (si == Obj.end_sections())
llvm_unreachable("Symbol section not found, bad object file format!"); llvm_unreachable("Symbol section not found, bad object file format!");
DEBUG(dbgs() << "\t\tThis is section symbol\n"); DEBUG(dbgs() << "\t\tThis is section symbol\n");
// Default to 'true' in case isText fails (though it never does). bool isCode = si->isText();
bool isCode = true;
si->isText(isCode);
Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID); Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
Value.Addend = Addend; Value.Addend = Addend;
break; break;

View File

@ -66,11 +66,9 @@ RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
} }
} else { } else {
SectionRef Sec = Obj.getRelocationSection(RelInfo); SectionRef Sec = Obj.getRelocationSection(RelInfo);
bool IsCode = false; bool IsCode = Sec.isText();
Sec.isText(IsCode);
Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID); Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
uint64_t Addr; uint64_t Addr = Sec.getAddress();
Sec.getAddress(Addr);
Value.Offset = RE.Addend - Addr; Value.Offset = RE.Addend - Addr;
} }
@ -115,9 +113,8 @@ RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
section_iterator SE = Obj.section_end(); section_iterator SE = Obj.section_end();
for (; SI != SE; ++SI) { for (; SI != SE; ++SI) {
uint64_t SAddr, SSize; uint64_t SAddr = SI->getAddress();
SI->getAddress(SAddr); uint64_t SSize = SI->getSize();
SI->getSize(SSize);
if ((Addr >= SAddr) && (Addr < SAddr + SSize)) if ((Addr >= SAddr) && (Addr < SAddr + SSize))
return SI; return SI;
} }

View File

@ -232,20 +232,17 @@ private:
uint32_t AddrA = MachO->getScatteredRelocationValue(RE); uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
section_iterator SAI = getSectionByAddress(*MachO, AddrA); section_iterator SAI = getSectionByAddress(*MachO, AddrA);
assert(SAI != MachO->section_end() && "Can't find section for address A"); assert(SAI != MachO->section_end() && "Can't find section for address A");
uint64_t SectionABase; uint64_t SectionABase = SAI->getAddress();
SAI->getAddress(SectionABase);
uint64_t SectionAOffset = AddrA - SectionABase; uint64_t SectionAOffset = AddrA - SectionABase;
SectionRef SectionA = *SAI; SectionRef SectionA = *SAI;
bool IsCode; bool IsCode = SectionA.isText();
SectionA.isText(IsCode);
uint32_t SectionAID = uint32_t SectionAID =
findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID); findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
uint32_t AddrB = MachO->getScatteredRelocationValue(RE2); uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
section_iterator SBI = getSectionByAddress(*MachO, AddrB); section_iterator SBI = getSectionByAddress(*MachO, AddrB);
assert(SBI != MachO->section_end() && "Can't find section for address B"); assert(SBI != MachO->section_end() && "Can't find section for address B");
uint64_t SectionBBase; uint64_t SectionBBase = SBI->getAddress();
SBI->getAddress(SectionBBase);
uint64_t SectionBOffset = AddrB - SectionBBase; uint64_t SectionBOffset = AddrB - SectionBBase;
SectionRef SectionB = *SBI; SectionRef SectionB = *SBI;
uint32_t SectionBID = uint32_t SectionBID =

View File

@ -152,20 +152,17 @@ private:
uint32_t AddrA = MachO->getScatteredRelocationValue(RE); uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
section_iterator SAI = getSectionByAddress(*MachO, AddrA); section_iterator SAI = getSectionByAddress(*MachO, AddrA);
assert(SAI != MachO->section_end() && "Can't find section for address A"); assert(SAI != MachO->section_end() && "Can't find section for address A");
uint64_t SectionABase; uint64_t SectionABase = SAI->getAddress();
SAI->getAddress(SectionABase);
uint64_t SectionAOffset = AddrA - SectionABase; uint64_t SectionAOffset = AddrA - SectionABase;
SectionRef SectionA = *SAI; SectionRef SectionA = *SAI;
bool IsCode; bool IsCode = SectionA.isText();
SectionA.isText(IsCode);
uint32_t SectionAID = uint32_t SectionAID =
findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID); findOrEmitSection(Obj, SectionA, IsCode, ObjSectionToID);
uint32_t AddrB = MachO->getScatteredRelocationValue(RE2); uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
section_iterator SBI = getSectionByAddress(*MachO, AddrB); section_iterator SBI = getSectionByAddress(*MachO, AddrB);
assert(SBI != MachO->section_end() && "Can't find section for address B"); assert(SBI != MachO->section_end() && "Can't find section for address B");
uint64_t SectionBBase; uint64_t SectionBBase = SBI->getAddress();
SBI->getAddress(SectionBBase);
uint64_t SectionBOffset = AddrB - SectionBBase; uint64_t SectionBOffset = AddrB - SectionBBase;
SectionRef SectionB = *SBI; SectionRef SectionB = *SBI;
uint32_t SectionBID = uint32_t SectionBID =
@ -211,11 +208,9 @@ private:
unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE); unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr); section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
assert(TargetSI != MachO->section_end() && "Can't find section for symbol"); assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
uint64_t SectionBaseAddr; uint64_t SectionBaseAddr = TargetSI->getAddress();
TargetSI->getAddress(SectionBaseAddr);
SectionRef TargetSection = *TargetSI; SectionRef TargetSection = *TargetSI;
bool IsCode; bool IsCode = TargetSection.isText();
TargetSection.isText(IsCode);
uint32_t TargetSectionID = uint32_t TargetSectionID =
findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID); findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);

View File

@ -260,18 +260,14 @@ std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
return getSectionName(Sec, Result); return getSectionName(Sec, Result);
} }
std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
uint64_t &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->VirtualAddress; return Sec->VirtualAddress;
return object_error::success;
} }
std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
uint64_t &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->SizeOfRawData; return Sec->SizeOfRawData;
return object_error::success;
} }
std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
@ -283,71 +279,52 @@ std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
return EC; return EC;
} }
std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
uint64_t &Res) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref, bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
bool &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref, bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
bool &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
bool &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
return object_error::success;
} }
std::error_code bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
bool &Result) const {
// FIXME: Unimplemented // FIXME: Unimplemented
Result = true; return true;
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
bool &Result) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
bool &Result) const {
// FIXME: Unimplemented. // FIXME: Unimplemented.
Result = false; return false;
return object_error::success;
} }
std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
bool &Result) const {
// FIXME: Unimplemented. // FIXME: Unimplemented.
Result = false; return false;
return object_error::success;
} }
std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
DataRefImpl SymbRef, DataRefImpl SymbRef) const {
bool &Result) const {
const coff_section *Sec = toSec(SecRef); const coff_section *Sec = toSec(SecRef);
COFFSymbolRef Symb = getCOFFSymbol(SymbRef); COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
int32_t SecNumber = (Sec - SectionTable) + 1; int32_t SecNumber = (Sec - SectionTable) + 1;
Result = SecNumber == Symb.getSectionNumber(); return SecNumber == Symb.getSectionNumber();
return object_error::success;
} }
relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {

View File

@ -144,11 +144,9 @@ static void printRelocationTargetName(const MachOObjectFile *O,
// to find a section beginning instead. // to find a section beginning instead.
for (const SectionRef &Section : O->sections()) { for (const SectionRef &Section : O->sections()) {
std::error_code ec; std::error_code ec;
uint64_t Addr;
StringRef Name;
if ((ec = Section.getAddress(Addr))) StringRef Name;
report_fatal_error(ec.message()); uint64_t Addr = Section.getAddress();
if (Addr != Val) if (Addr != Val)
continue; continue;
if ((ec = Section.getName(Name))) if ((ec = Section.getName(Name)))
@ -394,11 +392,10 @@ std::error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
EndOffset = Value; EndOffset = Value;
} }
if (!EndOffset) { if (!EndOffset) {
uint64_t Size;
DataRefImpl Sec; DataRefImpl Sec;
Sec.d.a = SectionIndex-1; Sec.d.a = SectionIndex-1;
getSectionSize(Sec, Size); uint64_t Size = getSectionSize(Sec);
getSectionAddress(Sec, EndOffset); EndOffset = getSectionAddress(Sec);
EndOffset += Size; EndOffset += Size;
} }
Result = EndOffset - BeginOffset; Result = EndOffset - BeginOffset;
@ -495,29 +492,16 @@ std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
return object_error::success; return object_error::success;
} }
std::error_code MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
uint64_t &Res) const { if (is64Bit())
if (is64Bit()) { return getSection64(Sec).addr;
MachO::section_64 Sect = getSection64(Sec); return getSection(Sec).addr;
Res = Sect.addr;
} else {
MachO::section Sect = getSection(Sec);
Res = Sect.addr;
}
return object_error::success;
} }
std::error_code MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
uint64_t &Res) const { if (is64Bit())
if (is64Bit()) { return getSection64(Sec).size;
MachO::section_64 Sect = getSection64(Sec); return getSection(Sec).size;
Res = Sect.size;
} else {
MachO::section Sect = getSection(Sec);
Res = Sect.size;
}
return object_error::success;
} }
std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec, std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
@ -539,8 +523,7 @@ std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
return object_error::success; return object_error::success;
} }
std::error_code MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
uint64_t &Res) const {
uint32_t Align; uint32_t Align;
if (is64Bit()) { if (is64Bit()) {
MachO::section_64 Sect = getSection64(Sec); MachO::section_64 Sect = getSection64(Sec);
@ -550,92 +533,70 @@ std::error_code MachOObjectFile::getSectionAlignment(DataRefImpl Sec,
Align = Sect.align; Align = Sect.align;
} }
Res = uint64_t(1) << Align; return uint64_t(1) << Align;
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionText(DataRefImpl Sec, bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
bool &Res) const {
uint32_t Flags = getSectionFlags(this, Sec); uint32_t Flags = getSectionFlags(this, Sec);
Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionData(DataRefImpl Sec, bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
bool &Result) const {
uint32_t Flags = getSectionFlags(this, Sec); uint32_t Flags = getSectionFlags(this, Sec);
unsigned SectionType = Flags & MachO::SECTION_TYPE; unsigned SectionType = Flags & MachO::SECTION_TYPE;
Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
!(SectionType == MachO::S_ZEROFILL || !(SectionType == MachO::S_ZEROFILL ||
SectionType == MachO::S_GB_ZEROFILL); SectionType == MachO::S_GB_ZEROFILL);
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec, bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
bool &Result) const {
uint32_t Flags = getSectionFlags(this, Sec); uint32_t Flags = getSectionFlags(this, Sec);
unsigned SectionType = Flags & MachO::SECTION_TYPE; unsigned SectionType = Flags & MachO::SECTION_TYPE;
Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
(SectionType == MachO::S_ZEROFILL || (SectionType == MachO::S_ZEROFILL ||
SectionType == MachO::S_GB_ZEROFILL); SectionType == MachO::S_GB_ZEROFILL);
return object_error::success;
} }
std::error_code bool MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sect) const {
MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
bool &Result) const {
// FIXME: Unimplemented. // FIXME: Unimplemented.
Result = true; return true;
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
bool &Result) const {
// FIXME: Unimplemented. // FIXME: Unimplemented.
Result = false; return false;
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool MachOObjectFile::isSectionZeroInit(DataRefImpl Sec) const {
bool &Res) const {
uint32_t Flags = getSectionFlags(this, Sec); uint32_t Flags = getSectionFlags(this, Sec);
unsigned SectionType = Flags & MachO::SECTION_TYPE; unsigned SectionType = Flags & MachO::SECTION_TYPE;
Res = SectionType == MachO::S_ZEROFILL || return SectionType == MachO::S_ZEROFILL ||
SectionType == MachO::S_GB_ZEROFILL; SectionType == MachO::S_GB_ZEROFILL;
return object_error::success;
} }
std::error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, bool MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec) const {
bool &Result) const {
// Consider using the code from isSectionText to look for __const sections. // Consider using the code from isSectionText to look for __const sections.
// Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
// to use section attributes to distinguish code from data. // to use section attributes to distinguish code from data.
// FIXME: Unimplemented. // FIXME: Unimplemented.
Result = false; return false;
return object_error::success;
} }
std::error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, bool MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb, DataRefImpl Symb) const {
bool &Result) const {
SymbolRef::Type ST; SymbolRef::Type ST;
this->getSymbolType(Symb, ST); this->getSymbolType(Symb, ST);
if (ST == SymbolRef::ST_Unknown) { if (ST == SymbolRef::ST_Unknown)
Result = false; return false;
return object_error::success;
}
uint64_t SectBegin, SectEnd; uint64_t SectBegin = getSectionAddress(Sec);
getSectionAddress(Sec, SectBegin); uint64_t SectEnd = getSectionSize(Sec);
getSectionSize(Sec, SectEnd);
SectEnd += SectBegin; SectEnd += SectBegin;
uint64_t SymAddr; uint64_t SymAddr;
getSymbolAddress(Symb, SymAddr); getSymbolAddress(Symb, SymAddr);
Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); return (SymAddr >= SectBegin) && (SymAddr < SectEnd);
return object_error::success;
} }
relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
@ -673,8 +634,7 @@ std::error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
DataRefImpl Sec; DataRefImpl Sec;
Sec.d.a = Rel.d.a; Sec.d.a = Rel.d.a;
uint64_t SecAddress; uint64_t SecAddress = getSectionAddress(Sec);
getSectionAddress(Sec, SecAddress);
Res = SecAddress + Offset; Res = SecAddress + Offset;
return object_error::success; return object_error::success;
} }

View File

@ -132,10 +132,7 @@ const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
} }
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
uint64_t ret; return (*unwrap(SI))->getSize();
if (std::error_code ec = (*unwrap(SI))->getSize(ret))
report_fatal_error(ec.message());
return ret;
} }
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) { const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
@ -146,18 +143,12 @@ const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
} }
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) { uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
uint64_t ret; return (*unwrap(SI))->getAddress();
if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
report_fatal_error(ec.message());
return ret;
} }
LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI, LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
LLVMSymbolIteratorRef Sym) { LLVMSymbolIteratorRef Sym) {
bool ret; return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
report_fatal_error(ec.message());
return ret;
} }
// Section Relocation iterators // Section Relocation iterators

View File

@ -333,7 +333,8 @@ struct SectionData {
std::error_code load(SectionRef &Section) { std::error_code load(SectionRef &Section) {
if (auto Err = Section.getContents(Data)) if (auto Err = Section.getContents(Data))
return Err; return Err;
return Section.getAddress(Address); Address = Section.getAddress();
return instrprof_error::success;
} }
std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) { std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {

View File

@ -67,11 +67,10 @@ int convert_for_testing_main(int argc, const char **argv) {
return 1; return 1;
// Get the contents of the given sections. // Get the contents of the given sections.
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
StringRef CoverageMappingData; StringRef CoverageMappingData;
uint64_t ProfileNamesAddress;
StringRef ProfileNamesData; StringRef ProfileNamesData;
if (CoverageMapping.getContents(CoverageMappingData) || if (CoverageMapping.getContents(CoverageMappingData) ||
ProfileNames.getAddress(ProfileNamesAddress) ||
ProfileNames.getContents(ProfileNamesData)) ProfileNames.getContents(ProfileNamesData))
return 1; return 1;

View File

@ -283,8 +283,7 @@ int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
return 0; return 0;
// First search the section's relocation entries (if any) for an entry // First search the section's relocation entries (if any) for an entry
// for this section offset. // for this section offset.
uint64_t sect_addr; uint64_t sect_addr = info->S.getAddress();
info->S.getAddress(sect_addr);
uint64_t sect_offset = (Pc + Offset) - sect_addr; uint64_t sect_offset = (Pc + Offset) - sect_addr;
bool reloc_found = false; bool reloc_found = false;
DataRefImpl Rel; DataRefImpl Rel;
@ -522,8 +521,7 @@ const char *GuessLiteralPointer(uint64_t ReferenceValue, uint64_t ReferencePC,
return nullptr; return nullptr;
// First see if there is an external relocation entry at the ReferencePC. // First see if there is an external relocation entry at the ReferencePC.
uint64_t sect_addr; uint64_t sect_addr = info->S.getAddress();
info->S.getAddress(sect_addr);
uint64_t sect_offset = ReferencePC - sect_addr; uint64_t sect_offset = ReferencePC - sect_addr;
bool reloc_found = false; bool reloc_found = false;
DataRefImpl Rel; DataRefImpl Rel;
@ -820,7 +818,7 @@ static void DisassembleInputMachO2(StringRef Filename,
// Build a data in code table that is sorted on by the address of each entry. // Build a data in code table that is sorted on by the address of each entry.
uint64_t BaseAddress = 0; uint64_t BaseAddress = 0;
if (Header.filetype == MachO::MH_OBJECT) if (Header.filetype == MachO::MH_OBJECT)
Sections[0].getAddress(BaseAddress); BaseAddress = Sections[0].getAddress();
else else
BaseAddress = BaseSegmentAddress; BaseAddress = BaseSegmentAddress;
DiceTable Dices; DiceTable Dices;
@ -863,8 +861,7 @@ static void DisassembleInputMachO2(StringRef Filename,
for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
bool SectIsText = false; bool SectIsText = Sections[SectIdx].isText();
Sections[SectIdx].isText(SectIsText);
if (SectIsText == false) if (SectIsText == false)
continue; continue;
@ -881,8 +878,7 @@ static void DisassembleInputMachO2(StringRef Filename,
StringRef Bytes; StringRef Bytes;
Sections[SectIdx].getContents(Bytes); Sections[SectIdx].getContents(Bytes);
uint64_t SectAddress = 0; uint64_t SectAddress = Sections[SectIdx].getAddress();
Sections[SectIdx].getAddress(SectAddress);
DisasmMemoryObject MemoryObject((const uint8_t *)Bytes.data(), Bytes.size(), DisasmMemoryObject MemoryObject((const uint8_t *)Bytes.data(), Bytes.size(),
SectAddress); SectAddress);
bool symbolTableWorked = false; bool symbolTableWorked = false;
@ -890,9 +886,9 @@ static void DisassembleInputMachO2(StringRef Filename,
// Parse relocations. // Parse relocations.
std::vector<std::pair<uint64_t, SymbolRef>> Relocs; std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
uint64_t RelocOffset, SectionAddress; uint64_t RelocOffset;
Reloc.getOffset(RelocOffset); Reloc.getOffset(RelocOffset);
Sections[SectIdx].getAddress(SectionAddress); uint64_t SectionAddress = Sections[SectIdx].getAddress();
RelocOffset -= SectionAddress; RelocOffset -= SectionAddress;
symbol_iterator RelocSym = Reloc.getSymbol(); symbol_iterator RelocSym = Reloc.getSymbol();
@ -933,15 +929,13 @@ static void DisassembleInputMachO2(StringRef Filename,
continue; continue;
// Make sure the symbol is defined in this section. // Make sure the symbol is defined in this section.
bool containsSym = false; bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
if (!containsSym) if (!containsSym)
continue; continue;
// Start at the address of the symbol relative to the section's address. // Start at the address of the symbol relative to the section's address.
uint64_t SectionAddress = 0;
uint64_t Start = 0; uint64_t Start = 0;
Sections[SectIdx].getAddress(SectionAddress); uint64_t SectionAddress = Sections[SectIdx].getAddress();
Symbols[SymIdx].getAddress(Start); Symbols[SymIdx].getAddress(Start);
Start -= SectionAddress; Start -= SectionAddress;
@ -954,8 +948,8 @@ static void DisassembleInputMachO2(StringRef Filename,
SymbolRef::Type NextSymType; SymbolRef::Type NextSymType;
Symbols[NextSymIdx].getType(NextSymType); Symbols[NextSymIdx].getType(NextSymType);
if (NextSymType == SymbolRef::ST_Function) { if (NextSymType == SymbolRef::ST_Function) {
Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], containsNextSym =
containsNextSym); Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Symbols[NextSymIdx].getAddress(NextSym); Symbols[NextSymIdx].getAddress(NextSym);
NextSym -= SectionAddress; NextSym -= SectionAddress;
break; break;
@ -963,8 +957,7 @@ static void DisassembleInputMachO2(StringRef Filename,
++NextSymIdx; ++NextSymIdx;
} }
uint64_t SectSize; uint64_t SectSize = Sections[SectIdx].getSize();
Sections[SectIdx].getSize(SectSize);
uint64_t End = containsNextSym ? NextSym : SectSize; uint64_t End = containsNextSym ? NextSym : SectSize;
uint64_t Size; uint64_t Size;
@ -1051,10 +1044,8 @@ static void DisassembleInputMachO2(StringRef Filename,
} }
if (!symbolTableWorked) { if (!symbolTableWorked) {
// Reading the symbol table didn't work, disassemble the whole section. // Reading the symbol table didn't work, disassemble the whole section.
uint64_t SectAddress; uint64_t SectAddress = Sections[SectIdx].getAddress();
Sections[SectIdx].getAddress(SectAddress); uint64_t SectSize = Sections[SectIdx].getSize();
uint64_t SectSize;
Sections[SectIdx].getSize(SectSize);
uint64_t InstSize; uint64_t InstSize;
for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
MCInst Inst; MCInst Inst;
@ -1159,8 +1150,7 @@ static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
SectionRef RelocSection = Obj->getRelocationSection(RE); SectionRef RelocSection = Obj->getRelocationSection(RE);
uint64_t SectionAddr; uint64_t SectionAddr = RelocSection.getAddress();
RelocSection.getAddress(SectionAddr);
auto Sym = Symbols.upper_bound(Addr); auto Sym = Symbols.upper_bound(Addr);
if (Sym == Symbols.begin()) { if (Sym == Symbols.begin()) {
@ -2731,10 +2721,8 @@ SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
SectionInfo Info; SectionInfo Info;
if (error(Section.getName(Info.SectionName))) if (error(Section.getName(Info.SectionName)))
return; return;
if (error(Section.getAddress(Info.Address))) Info.Address = Section.getAddress();
return; Info.Size = Section.getSize();
if (error(Section.getSize(Info.Size)))
return;
Info.SegmentName = Info.SegmentName =
Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl()); Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
if (!Info.SegmentName.equals(CurSegName)) { if (!Info.SegmentName.equals(CurSegName)) {

View File

@ -307,25 +307,17 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
} }
for (const SectionRef &Section : Obj->sections()) { for (const SectionRef &Section : Obj->sections()) {
bool Text; bool Text = Section.isText();
if (error(Section.isText(Text)))
break;
if (!Text) if (!Text)
continue; continue;
uint64_t SectionAddr; uint64_t SectionAddr = Section.getAddress();
if (error(Section.getAddress(SectionAddr))) uint64_t SectSize = Section.getSize();
break;
uint64_t SectSize;
if (error(Section.getSize(SectSize)))
break;
// Make a list of all the symbols in this section. // Make a list of all the symbols in this section.
std::vector<std::pair<uint64_t, StringRef>> Symbols; std::vector<std::pair<uint64_t, StringRef>> Symbols;
for (const SymbolRef &Symbol : Obj->symbols()) { for (const SymbolRef &Symbol : Obj->symbols()) {
bool contains; if (Section.containsSymbol(Symbol)) {
if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
uint64_t Address; uint64_t Address;
if (error(Symbol.getAddress(Address))) if (error(Symbol.getAddress(Address)))
break; break;
@ -501,19 +493,11 @@ static void PrintSectionHeaders(const ObjectFile *Obj) {
StringRef Name; StringRef Name;
if (error(Section.getName(Name))) if (error(Section.getName(Name)))
return; return;
uint64_t Address; uint64_t Address = Section.getAddress();
if (error(Section.getAddress(Address))) uint64_t Size = Section.getSize();
return; bool Text = Section.isText();
uint64_t Size; bool Data = Section.isData();
if (error(Section.getSize(Size))) bool BSS = Section.isBSS();
return;
bool Text, Data, BSS;
if (error(Section.isText(Text)))
return;
if (error(Section.isData(Data)))
return;
if (error(Section.isBSS(BSS)))
return;
std::string Type = (std::string(Text ? "TEXT " : "") + std::string Type = (std::string(Text ? "TEXT " : "") +
(Data ? "DATA " : "") + (BSS ? "BSS" : "")); (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i, outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
@ -527,20 +511,14 @@ static void PrintSectionContents(const ObjectFile *Obj) {
for (const SectionRef &Section : Obj->sections()) { for (const SectionRef &Section : Obj->sections()) {
StringRef Name; StringRef Name;
StringRef Contents; StringRef Contents;
uint64_t BaseAddr;
bool BSS;
if (error(Section.getName(Name))) if (error(Section.getName(Name)))
continue; continue;
if (error(Section.getAddress(BaseAddr))) uint64_t BaseAddr = Section.getAddress();
continue; bool BSS = Section.isBSS();
if (error(Section.isBSS(BSS)))
continue;
outs() << "Contents of section " << Name << ":\n"; outs() << "Contents of section " << Name << ":\n";
if (BSS) { if (BSS) {
uint64_t Size; uint64_t Size = Section.getSize();
if (error(Section.getSize(Size)))
continue;
outs() << format("<skipping contents of bss section at [%04" PRIx64 outs() << format("<skipping contents of bss section at [%04" PRIx64
", %04" PRIx64 ")>\n", ", %04" PRIx64 ")>\n",
BaseAddr, BaseAddr + Size); BaseAddr, BaseAddr + Size);

View File

@ -186,13 +186,8 @@ void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask)
ErrorOr<object::SectionRef> ErrorOr<object::SectionRef>
Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) { Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
for (const auto &Section : COFF.sections()) { for (const auto &Section : COFF.sections()) {
uint64_t Address; uint64_t Address = Section.getAddress();
uint64_t Size; uint64_t Size = Section.getSize();
if (std::error_code EC = Section.getAddress(Address))
return EC;
if (std::error_code EC = Section.getSize(Size))
return EC;
if (VA >= Address && (VA - Address) <= Size) if (VA >= Address && (VA - Address) <= Size)
return Section; return Section;
@ -525,10 +520,7 @@ bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents)) if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
return false; return false;
uint64_t SectionVA; uint64_t SectionVA = Section.getAddress();
if (Section.getAddress(SectionVA))
return false;
uint64_t Offset = VA - SectionVA; uint64_t Offset = VA - SectionVA;
const ulittle32_t *Data = const ulittle32_t *Data =
reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset); reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);

View File

@ -629,8 +629,7 @@ void COFFDumper::printSections() {
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
ListScope D(W, "Symbols"); ListScope D(W, "Symbols");
for (const SymbolRef &Symbol : Obj->symbols()) { for (const SymbolRef &Symbol : Obj->symbols()) {
bool Contained = false; if (!Sec.containsSymbol(Symbol))
if (Sec.containsSymbol(Symbol, Contained) || !Contained)
continue; continue;
printSymbol(Symbol); printSymbol(Symbol);

View File

@ -257,8 +257,7 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
if (opts::SectionSymbols) { if (opts::SectionSymbols) {
ListScope D(W, "Symbols"); ListScope D(W, "Symbols");
for (const SymbolRef &Symbol : Obj->symbols()) { for (const SymbolRef &Symbol : Obj->symbols()) {
bool Contained = false; if (!Section.containsSymbol(Symbol))
if (Section.containsSymbol(Symbol, Contained) || !Contained)
continue; continue;
printSymbol(Symbol); printSymbol(Symbol);
@ -266,9 +265,7 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
} }
if (opts::SectionData) { if (opts::SectionData) {
bool IsBSS; bool IsBSS = Section.isBSS();
if (error(Section.isBSS(IsBSS)))
break;
if (!IsBSS) { if (!IsBSS) {
StringRef Data; StringRef Data;
if (error(Section.getContents(Data))) if (error(Section.getContents(Data)))

View File

@ -297,17 +297,13 @@ static void PrintObjectSectionSizes(ObjectFile *Obj) {
std::size_t max_size_len = strlen("size"); std::size_t max_size_len = strlen("size");
std::size_t max_addr_len = strlen("addr"); std::size_t max_addr_len = strlen("addr");
for (const SectionRef &Section : Obj->sections()) { for (const SectionRef &Section : Obj->sections()) {
uint64_t size = 0; uint64_t size = Section.getSize();
if (error(Section.getSize(size)))
return;
total += size; total += size;
StringRef name; StringRef name;
uint64_t addr = 0;
if (error(Section.getName(name))) if (error(Section.getName(name)))
return; return;
if (error(Section.getAddress(addr))) uint64_t addr = Section.getAddress();
return;
max_name_len = std::max(max_name_len, name.size()); max_name_len = std::max(max_name_len, name.size());
max_size_len = std::max(max_size_len, getNumLengthAsString(size)); max_size_len = std::max(max_size_len, getNumLengthAsString(size));
max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr)); max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
@ -337,14 +333,10 @@ static void PrintObjectSectionSizes(ObjectFile *Obj) {
// Print each section. // Print each section.
for (const SectionRef &Section : Obj->sections()) { for (const SectionRef &Section : Obj->sections()) {
StringRef name; StringRef name;
uint64_t size = 0;
uint64_t addr = 0;
if (error(Section.getName(name))) if (error(Section.getName(name)))
return; return;
if (error(Section.getSize(size))) uint64_t size = Section.getSize();
return; uint64_t addr = Section.getAddress();
if (error(Section.getAddress(addr)))
return;
std::string namestr = name; std::string namestr = name;
outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr); outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
@ -365,18 +357,10 @@ static void PrintObjectSectionSizes(ObjectFile *Obj) {
// Make one pass over the section table to calculate sizes. // Make one pass over the section table to calculate sizes.
for (const SectionRef &Section : Obj->sections()) { for (const SectionRef &Section : Obj->sections()) {
uint64_t size = 0; uint64_t size = Section.getSize();
bool isText = false; bool isText = Section.isText();
bool isData = false; bool isData = Section.isData();
bool isBSS = false; bool isBSS = Section.isBSS();
if (error(Section.getSize(size)))
return;
if (error(Section.isText(isText)))
return;
if (error(Section.isData(isData)))
return;
if (error(Section.isBSS(isBSS)))
return;
if (isText) if (isText)
total_text += size; total_text += size;
else if (isData) else if (isData)

View File

@ -139,9 +139,8 @@ static void dumpVTables(const ObjectFile *Obj) {
// Skip external symbols. // Skip external symbols.
if (SecI == Obj->section_end()) if (SecI == Obj->section_end())
continue; continue;
bool IsBSS, IsVirtual; bool IsBSS = SecI->isBSS();
if (error(SecI->isBSS(IsBSS)) || error(SecI->isVirtual(IsVirtual))) bool IsVirtual = SecI->isVirtual();
break;
// Skip virtual or BSS sections. // Skip virtual or BSS sections.
if (IsBSS || IsVirtual) if (IsBSS || IsVirtual)
continue; continue;