aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Object/COFFObjectFile.cpp
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2014-11-13 09:50:18 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2014-11-13 09:50:18 +0000
commit94751be7fa2c2bf657d397c4f85d42e0a3397fb2 (patch)
tree4910d75c8a0b73d27a61efc4d424c5d315364c41 /llvm/lib/Object/COFFObjectFile.cpp
parente353eba33b5ce1d09f62b951ee1fe562b331372d (diff)
downloadllvm-94751be7fa2c2bf657d397c4f85d42e0a3397fb2.zip
llvm-94751be7fa2c2bf657d397c4f85d42e0a3397fb2.tar.gz
llvm-94751be7fa2c2bf657d397c4f85d42e0a3397fb2.tar.bz2
Object, COFF: Refactor code to get relocation iterators
No functional change intended. llvm-svn: 221880
Diffstat (limited to 'llvm/lib/Object/COFFObjectFile.cpp')
-rw-r--r--llvm/lib/Object/COFFObjectFile.cpp50
1 files changed, 24 insertions, 26 deletions
diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp
index b8e7d5e..dccc467 100644
--- a/llvm/lib/Object/COFFObjectFile.cpp
+++ b/llvm/lib/Object/COFFObjectFile.cpp
@@ -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,
- const size_t Size) {
+ const uint64_t Size) {
if (Addr + Size < Addr || Addr + Size < Size ||
Addr + Size > uintptr_t(M.getBufferEnd()) ||
Addr < uintptr_t(M.getBufferStart())) {
@@ -408,40 +408,38 @@ static uint32_t getNumberOfRelocations(const coff_section *Sec,
return Sec->NumberOfRelocations;
}
+static const coff_relocation *
+getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
+ uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
+ if (!NumRelocs)
+ return nullptr;
+ 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++;
+ }
+ 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;
- if (getNumberOfRelocations(Sec, Data, base()) == 0) {
- 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++;
- }
- Ret.p = reinterpret_cast<uintptr_t>(begin);
- }
+ Ret.p = reinterpret_cast<uintptr_t>(begin);
return relocation_iterator(RelocationRef(Ret, this));
}
relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
const coff_section *Sec = toSec(Ref);
+ const coff_relocation *I = getFirstReloc(Sec, Data, base());
+ if (I)
+ I += getNumberOfRelocations(Sec, Data, base());
DataRefImpl Ret;
- if (getNumberOfRelocations(Sec, Data, base()) == 0) {
- 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);
- }
+ Ret.p = reinterpret_cast<uintptr_t>(I);
return relocation_iterator(RelocationRef(Ret, this));
}