4
* Created on: Jan 8, 2020
16
int32_t decode_sleb128(uint8_t *sleb128)
18
int32_t num = 0, shift = 0, size = 0;
20
num |= ((*sleb128 & 0x7f) << shift);
23
} while(*sleb128++ & 0x80);
24
if((shift < size) && (*(--sleb128) & 0x40)) {
25
num |= - (1 << shift);
30
uint32_t decode_uleb128(uint8_t *uleb128)
32
uint32_t num = 0, shift = 0;
34
num |= ((*uleb128 & 0x7f) << shift);
36
} while(*uleb128++ & 0x80);
40
// Utility function to encode a ULEB128 value to a buffer. Returns
41
// the length in bytes of the encoded value. by default PadTo = 0
42
inline unsigned encode_uleb128(uint64_t value, uint8_t *p, unsigned PadTo)
47
uint8_t Byte = value & 0x7f;
50
if (value != 0 || count < PadTo)
51
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
55
// Pad with 0x80 and emit a null byte at the end.
57
for (; count < PadTo - 1; ++count)
62
return (unsigned)(p - orig_p);
65
// Utility function to encode a SLEB128 value to a buffer. Returns
66
// the length in bytes of the encoded value. by default PadTo = 0
67
inline unsigned encode_sleb128(int64_t value, uint8_t *p, unsigned PadTo)
73
uint8_t Byte = value & 0x7f;
74
// NOTE: this assumes that this signed shift is an arithmetic right shift.
76
More = !((((value == 0 ) && ((Byte & 0x40) == 0)) ||
77
((value == -1) && ((Byte & 0x40) != 0))));
79
if (More || count < PadTo)
80
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
84
// Pad with 0x80 and emit a terminating byte at the end.
86
uint8_t PadValue = value < 0 ? 0x7f : 0x00;
87
for (; count < PadTo - 1; ++count)
88
*p++ = (PadValue | 0x80);
91
return (unsigned)(p - orig_p);
94
// Utility function to get the size of the ULEB128-encoded value.
95
unsigned getULEB128Size(uint64_t Value)
100
Size += sizeof(int8_t);
106
// Utility function to get the size of the SLEB128-encoded value.
107
unsigned getSLEB128Size(int64_t Value)
110
int Sign = Value >> (8 * sizeof(Value) - 1);
114
unsigned Byte = Value & 0x7f;
116
IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
117
Size += sizeof(int8_t);
123
int pst_pointer_valid(void *p, uint32_t size)
125
long page_size = sysconf(_SC_PAGESIZE);
127
pst_log(SEVERITY_ERROR, "%s: Failed to determine page size");
131
void *aligned = (void *)((((long)p) / page_size) * page_size);
132
//void *aligned = (void *)((uintptr_t)p & ~(page_size - 1));
133
if(msync(aligned, size, MS_ASYNC) == 0) {
137
return (errno == ENOMEM ? EINVAL : EFAULT);