[Support] Add LEB128 support to BinaryStreamReader/Writer.

Summary:
This patch adds support for ULEB128 and SLEB128 encoding and decoding to
BinaryStreamWriter and BinaryStreamReader respectively.

Support for ULEB128/SLEB128 will be used for eh-frame parsing in the JITLink
library currently under development (see https://reviews.llvm.org/D58704).

Reviewers: zturner, dblaikie

Subscribers: kristina, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D60810

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358584 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Lang Hames
2019-04-17 15:38:27 +00:00
parent 94ebdffbc8
commit 3d0200eec2
5 changed files with 142 additions and 1 deletions
+32 -1
View File
@@ -10,6 +10,7 @@
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/LEB128.h"
using namespace llvm;
using endianness = llvm::support::endianness;
@@ -40,6 +41,36 @@ Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
return Error::success();
}
Error BinaryStreamReader::readULEB128(uint64_t &Dest) {
SmallVector<uint8_t, 10> EncodedBytes;
ArrayRef<uint8_t> NextByte;
// Copy the encoded ULEB into the buffer.
do {
if (auto Err = readBytes(NextByte, 1))
return Err;
EncodedBytes.push_back(NextByte[0]);
} while (NextByte[0] & 0x80);
Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
return Error::success();
}
Error BinaryStreamReader::readSLEB128(int64_t &Dest) {
SmallVector<uint8_t, 10> EncodedBytes;
ArrayRef<uint8_t> NextByte;
// Copy the encoded ULEB into the buffer.
do {
if (auto Err = readBytes(NextByte, 1))
return Err;
EncodedBytes.push_back(NextByte[0]);
} while (NextByte[0] & 0x80);
Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
return Error::success();
}
Error BinaryStreamReader::readCString(StringRef &Dest) {
uint32_t OriginalOffset = getOffset();
uint32_t FoundOffset = 0;
@@ -145,4 +176,4 @@ BinaryStreamReader::split(uint32_t Off) const {
BinaryStreamReader W1{First};
BinaryStreamReader W2{Second};
return std::make_pair(W1, W2);
}
}