1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
//===-- DNBDataRef.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Created by Greg Clayton on 1/11/06.
//
//===----------------------------------------------------------------------===//
#include "DNBDataRef.h"
#include "DNBLog.h"
#include <assert.h>
#include <ctype.h>
#include <libkern/OSByteOrder.h>
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
DNBDataRef::DNBDataRef()
: m_start(NULL), m_end(NULL), m_swap(false), m_ptrSize(0),
m_addrPCRelative(INVALID_NUB_ADDRESS), m_addrTEXT(INVALID_NUB_ADDRESS),
m_addrDATA(INVALID_NUB_ADDRESS) {}
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
DNBDataRef::DNBDataRef(const uint8_t *start, size_t size, bool swap)
: m_start(start), m_end(start + size), m_swap(swap), m_ptrSize(0),
m_addrPCRelative(INVALID_NUB_ADDRESS), m_addrTEXT(INVALID_NUB_ADDRESS),
m_addrDATA(INVALID_NUB_ADDRESS) {}
//----------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------
DNBDataRef::~DNBDataRef() {}
//----------------------------------------------------------------------
// Get8
//----------------------------------------------------------------------
uint8_t DNBDataRef::Get8(offset_t *offset_ptr) const {
uint8_t val = 0;
if (ValidOffsetForDataOfSize(*offset_ptr, sizeof(val))) {
val = *(m_start + *offset_ptr);
*offset_ptr += sizeof(val);
}
return val;
}
//----------------------------------------------------------------------
// Get16
//----------------------------------------------------------------------
uint16_t DNBDataRef::Get16(offset_t *offset_ptr) const {
uint16_t val = 0;
if (ValidOffsetForDataOfSize(*offset_ptr, sizeof(val))) {
const uint8_t *p = m_start + *offset_ptr;
memcpy(&val, p, sizeof(uint16_t));
if (m_swap)
val = OSSwapInt16(val);
// Advance the offset
*offset_ptr += sizeof(val);
}
return val;
}
//----------------------------------------------------------------------
// Get32
//----------------------------------------------------------------------
uint32_t DNBDataRef::Get32(offset_t *offset_ptr) const {
uint32_t val = 0;
if (ValidOffsetForDataOfSize(*offset_ptr, sizeof(val))) {
const uint8_t *p = m_start + *offset_ptr;
memcpy(&val, p, sizeof(uint32_t));
if (m_swap)
val = OSSwapInt32(val);
// Advance the offset
*offset_ptr += sizeof(val);
}
return val;
}
//----------------------------------------------------------------------
// Get64
//----------------------------------------------------------------------
uint64_t DNBDataRef::Get64(offset_t *offset_ptr) const {
uint64_t val = 0;
if (ValidOffsetForDataOfSize(*offset_ptr, sizeof(val))) {
const uint8_t *p = m_start + *offset_ptr;
memcpy(&val, p, sizeof(uint64_t));
if (m_swap)
val = OSSwapInt64(val);
// Advance the offset
*offset_ptr += sizeof(val);
}
return val;
}
//----------------------------------------------------------------------
// GetMax32
//
// Used for calls when the size can vary. Fill in extra cases if they
// are ever needed.
//----------------------------------------------------------------------
uint32_t DNBDataRef::GetMax32(offset_t *offset_ptr, uint32_t byte_size) const {
switch (byte_size) {
case 1:
return Get8(offset_ptr);
break;
case 2:
return Get16(offset_ptr);
break;
case 4:
return Get32(offset_ptr);
break;
default:
assert(false && "GetMax32 unhandled case!");
break;
}
return 0;
}
//----------------------------------------------------------------------
// GetMax64
//
// Used for calls when the size can vary. Fill in extra cases if they
// are ever needed.
//----------------------------------------------------------------------
uint64_t DNBDataRef::GetMax64(offset_t *offset_ptr, uint32_t size) const {
switch (size) {
case 1:
return Get8(offset_ptr);
break;
case 2:
return Get16(offset_ptr);
break;
case 4:
return Get32(offset_ptr);
break;
case 8:
return Get64(offset_ptr);
break;
default:
assert(false && "GetMax64 unhandled case!");
break;
}
return 0;
}
//----------------------------------------------------------------------
// GetPointer
//
// Extract a pointer value from the buffer. The pointer size must be
// set prior to using this using one of the SetPointerSize functions.
//----------------------------------------------------------------------
uint64_t DNBDataRef::GetPointer(offset_t *offset_ptr) const {
// Must set pointer size prior to using this call
assert(m_ptrSize != 0);
return GetMax64(offset_ptr, m_ptrSize);
}
//----------------------------------------------------------------------
// GetCStr
//----------------------------------------------------------------------
const char *DNBDataRef::GetCStr(offset_t *offset_ptr,
uint32_t fixed_length) const {
const char *s = NULL;
if (m_start < m_end) {
s = (const char *)m_start + *offset_ptr;
// Advance the offset
if (fixed_length)
*offset_ptr += fixed_length;
else
*offset_ptr += strlen(s) + 1;
}
return s;
}
//----------------------------------------------------------------------
// GetData
//----------------------------------------------------------------------
const uint8_t *DNBDataRef::GetData(offset_t *offset_ptr,
uint32_t length) const {
const uint8_t *data = NULL;
if (length > 0 && ValidOffsetForDataOfSize(*offset_ptr, length)) {
data = m_start + *offset_ptr;
*offset_ptr += length;
}
return data;
}
//----------------------------------------------------------------------
// Get_ULEB128
//----------------------------------------------------------------------
uint64_t DNBDataRef::Get_ULEB128(offset_t *offset_ptr) const {
uint64_t result = 0;
if (m_start < m_end) {
int shift = 0;
const uint8_t *src = m_start + *offset_ptr;
uint8_t byte;
int bytecount = 0;
while (src < m_end) {
bytecount++;
byte = *src++;
result |= (uint64_t)(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
*offset_ptr += bytecount;
}
return result;
}
//----------------------------------------------------------------------
// Get_SLEB128
//----------------------------------------------------------------------
int64_t DNBDataRef::Get_SLEB128(offset_t *offset_ptr) const {
int64_t result = 0;
if (m_start < m_end) {
int shift = 0;
int size = sizeof(uint32_t) * 8;
const uint8_t *src = m_start + *offset_ptr;
uint8_t byte = 0;
int bytecount = 0;
while (src < m_end) {
bytecount++;
byte = *src++;
result |= (int64_t)(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
// Sign bit of byte is 2nd high order bit (0x40)
if (shift < size && (byte & 0x40))
result |= -(1ll << shift);
*offset_ptr += bytecount;
}
return result;
}
//----------------------------------------------------------------------
// Skip_LEB128
//
// Skips past ULEB128 and SLEB128 numbers (just updates the offset)
//----------------------------------------------------------------------
void DNBDataRef::Skip_LEB128(offset_t *offset_ptr) const {
if (m_start < m_end) {
const uint8_t *start = m_start + *offset_ptr;
const uint8_t *src = start;
while ((src < m_end) && (*src++ & 0x80))
/* Do nothing */;
*offset_ptr += src - start;
}
}
uint32_t DNBDataRef::Dump(uint32_t startOffset, uint32_t endOffset,
uint64_t offsetBase, DNBDataRef::Type type,
uint32_t numPerLine, const char *format) {
uint32_t offset;
uint32_t count;
char str[1024];
str[0] = '\0';
size_t str_offset = 0;
for (offset = startOffset, count = 0;
ValidOffset(offset) && offset < endOffset; ++count) {
if ((count % numPerLine) == 0) {
// Print out any previous string
if (str[0] != '\0')
DNBLog("%s", str);
// Reset string offset and fill the current line string with address:
str_offset = 0;
str_offset += snprintf(str, sizeof(str), "0x%8.8llx:",
(uint64_t)(offsetBase + (offset - startOffset)));
}
// Make sure we don't pass the bounds of our current string buffer on each
// iteration through this loop
if (str_offset >= sizeof(str)) {
// The last snprintf consumed our string buffer, we will need to dump this
// out
// and reset the string with no address
DNBLog("%s", str);
str_offset = 0;
str[0] = '\0';
}
// We already checked that there is at least some room in the string str
// above, so it is safe to make
// the snprintf call each time through this loop
switch (type) {
case TypeUInt8:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %2.2x", Get8(&offset));
break;
case TypeChar: {
char ch = Get8(&offset);
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %c", isprint(ch) ? ch : ' ');
} break;
case TypeUInt16:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %4.4x", Get16(&offset));
break;
case TypeUInt32:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %8.8x", Get32(&offset));
break;
case TypeUInt64:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %16.16llx", Get64(&offset));
break;
case TypePointer:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " 0x%llx", GetPointer(&offset));
break;
case TypeULEB128:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " 0x%llx", Get_ULEB128(&offset));
break;
case TypeSLEB128:
str_offset += snprintf(str + str_offset, sizeof(str) - str_offset,
format ? format : " %lld", Get_SLEB128(&offset));
break;
}
}
if (str[0] != '\0')
DNBLog("%s", str);
return offset; // Return the offset at which we ended up
}
|