Object, COFF: Refactor code to get relocation iterators

No functional change intended.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221880 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer
2014-11-13 09:50:18 +00:00
parent 6fef94e4d4
commit a98fcec40c

View File

@ -40,7 +40,7 @@ static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
} }
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
const size_t Size) { const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size || if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) || Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) { Addr < uintptr_t(M.getBufferStart())) {
@ -408,40 +408,38 @@ static uint32_t getNumberOfRelocations(const coff_section *Sec,
return Sec->NumberOfRelocations; return Sec->NumberOfRelocations;
} }
relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { static const coff_relocation *
const coff_section *Sec = toSec(Ref); getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
DataRefImpl Ret; uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
if (getNumberOfRelocations(Sec, Data, base()) == 0) { if (!NumRelocs)
Ret.p = 0; return nullptr;
} else { auto begin = reinterpret_cast<const coff_relocation *>(
auto begin = reinterpret_cast<const coff_relocation*>( Base + Sec->PointerToRelocations);
base() + Sec->PointerToRelocations);
if (Sec->hasExtendedRelocations()) { if (Sec->hasExtendedRelocations()) {
// Skip the first relocation entry repurposed to store the number of // Skip the first relocation entry repurposed to store the number of
// relocations. // relocations.
begin++; begin++;
} }
if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
return nullptr;
return begin;
}
relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref);
const coff_relocation *begin = getFirstReloc(Sec, Data, base());
DataRefImpl Ret;
Ret.p = reinterpret_cast<uintptr_t>(begin); Ret.p = reinterpret_cast<uintptr_t>(begin);
}
return relocation_iterator(RelocationRef(Ret, this)); return relocation_iterator(RelocationRef(Ret, this));
} }
relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref); const coff_section *Sec = toSec(Ref);
const coff_relocation *I = getFirstReloc(Sec, Data, base());
if (I)
I += getNumberOfRelocations(Sec, Data, base());
DataRefImpl Ret; DataRefImpl Ret;
if (getNumberOfRelocations(Sec, Data, base()) == 0) { Ret.p = reinterpret_cast<uintptr_t>(I);
Ret.p = 0;
} else {
auto begin = reinterpret_cast<const coff_relocation*>(
base() + Sec->PointerToRelocations);
if (Sec->hasExtendedRelocations()) {
// Skip the first relocation entry repurposed to store the number of
// relocations.
begin++;
}
uint32_t NumReloc = getNumberOfRelocations(Sec, Data, base());
Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
}
return relocation_iterator(RelocationRef(Ret, this)); return relocation_iterator(RelocationRef(Ret, this));
} }