diff options
author | Rahman Lavaee <rahmanl@google.com> | 2021-01-22 09:13:30 -0800 |
---|---|---|
committer | Rahman Lavaee <rahmanl@google.com> | 2021-01-28 01:35:18 -0800 |
commit | 3ca502a7d60787abe14db1fa541950ff79c7585b (patch) | |
tree | 0f9ce714735e76d2c9b30eead8d069bb3a2f7252 /llvm/lib/Object/ELF.cpp | |
parent | 374862d71c1a2f63557a27f1555b37ff0f75117e (diff) | |
download | llvm-3ca502a7d60787abe14db1fa541950ff79c7585b.zip llvm-3ca502a7d60787abe14db1fa541950ff79c7585b.tar.gz llvm-3ca502a7d60787abe14db1fa541950ff79c7585b.tar.bz2 |
Use DataExtractor to decode SLEB128 in android_relas.
A simple refactoring patch which let us use `DataExtractor::getSLEB128` rather than using a lambda function.
Differential Revision: https://reviews.llvm.org/D95158
Diffstat (limited to 'llvm/lib/Object/ELF.cpp')
-rw-r--r-- | llvm/lib/Object/ELF.cpp | 58 |
1 files changed, 23 insertions, 35 deletions
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp index 264f115..27a35bb 100644 --- a/llvm/lib/Object/ELF.cpp +++ b/llvm/lib/Object/ELF.cpp @@ -8,7 +8,7 @@ #include "llvm/Object/ELF.h" #include "llvm/BinaryFormat/ELF.h" -#include "llvm/Support/LEB128.h" +#include "llvm/Support/DataExtractor.h" using namespace llvm; using namespace object; @@ -373,39 +373,31 @@ ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const { Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); if (!ContentsOrErr) return ContentsOrErr.takeError(); - const uint8_t *Cur = ContentsOrErr->begin(); - const uint8_t *End = ContentsOrErr->end(); - if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' || - Cur[2] != 'S' || Cur[3] != '2') + ArrayRef<uint8_t> Content = *ContentsOrErr; + if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' || + Content[2] != 'S' || Content[3] != '2') return createError("invalid packed relocation header"); - Cur += 4; - - const char *ErrStr = nullptr; - auto ReadSLEB = [&]() -> int64_t { - if (ErrStr) - return 0; - unsigned Len; - int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr); - Cur += Len; - return Result; - }; + DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4); + DataExtractor::Cursor Cur(/*Offset=*/4); - uint64_t NumRelocs = ReadSLEB(); - uint64_t Offset = ReadSLEB(); + uint64_t NumRelocs = Data.getSLEB128(Cur); + uint64_t Offset = Data.getSLEB128(Cur); uint64_t Addend = 0; - if (ErrStr) - return createError(ErrStr); + if (!Cur) + return std::move(Cur.takeError()); std::vector<Elf_Rela> Relocs; Relocs.reserve(NumRelocs); while (NumRelocs) { - uint64_t NumRelocsInGroup = ReadSLEB(); + uint64_t NumRelocsInGroup = Data.getSLEB128(Cur); + if (!Cur) + return std::move(Cur.takeError()); if (NumRelocsInGroup > NumRelocs) return createError("relocation group unexpectedly large"); NumRelocs -= NumRelocsInGroup; - uint64_t GroupFlags = ReadSLEB(); + uint64_t GroupFlags = Data.getSLEB128(Cur); bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG; bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG; bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG; @@ -413,34 +405,30 @@ ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const { uint64_t GroupOffsetDelta; if (GroupedByOffsetDelta) - GroupOffsetDelta = ReadSLEB(); + GroupOffsetDelta = Data.getSLEB128(Cur); uint64_t GroupRInfo; if (GroupedByInfo) - GroupRInfo = ReadSLEB(); + GroupRInfo = Data.getSLEB128(Cur); if (GroupedByAddend && GroupHasAddend) - Addend += ReadSLEB(); + Addend += Data.getSLEB128(Cur); if (!GroupHasAddend) Addend = 0; - for (uint64_t I = 0; I != NumRelocsInGroup; ++I) { + for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) { Elf_Rela R; - Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB(); + Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur); R.r_offset = Offset; - R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB(); + R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur); if (GroupHasAddend && !GroupedByAddend) - Addend += ReadSLEB(); + Addend += Data.getSLEB128(Cur); R.r_addend = Addend; Relocs.push_back(R); - - if (ErrStr) - return createError(ErrStr); } - - if (ErrStr) - return createError(ErrStr); + if (!Cur) + return std::move(Cur.takeError()); } return Relocs; |