Skip to content

Commit 427d599

Browse files
committed
Readded Mach-O function starts handling
1 parent b70dc91 commit 427d599

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/parser/file/macho/machoFile.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "../bounds.h"
3838
#include "../loader.h"
39+
#include "../dwarf/leb128.h"
3940

4041
#include "../../callstack_parser.h"
4142

@@ -245,6 +246,26 @@ static inline void machoFile_addFunction(struct pair_funcFile function, va_list
245246
vector_pairFuncFile_push_back(&self->functions, function);
246247
}
247248

249+
static inline int machoFile_uint64Compare(uint64_t* lhs, uint64_t* rhs) {
250+
if (*lhs == *rhs) return 0;
251+
252+
return *lhs < *rhs ? -1 : +1;
253+
}
254+
255+
static inline bool machoFile_handleFunctionStarts(struct machoFile* self, struct linkedit_data_command* command, const void* baseAddress, bool bitsReversed) {
256+
uint32_t offset = macho_maybeSwap(32, bitsReversed, command->dataoff);
257+
uint32_t size = macho_maybeSwap(32, bitsReversed, command->datasize);
258+
259+
const void* bytes = baseAddress + offset;
260+
uint64_t funcAddr = self->addressOffset;
261+
for (size_t i = 0; i < size;) {
262+
funcAddr += getULEB128(bytes, &i);
263+
vector_uint64_push_back(&self->functionStarts, funcAddr);
264+
}
265+
vector_uint64_sort(&self->functionStarts, &machoFile_uint64Compare);
266+
return true;
267+
}
268+
248269
/**
249270
* Parses a Mach-O file into the given Mach-O file abstraction object.
250271
*
@@ -277,6 +298,10 @@ static inline bool machoFile_parseFileImpl(struct machoFile * self,
277298
memcpy(&self->uuid, &((struct uuid_command*) ((void*) lc))->uuid, 16);
278299
result = true;
279300
break;
301+
302+
case LC_FUNCTION_STARTS:
303+
result = machoFile_handleFunctionStarts(self, (void*) lc, baseAddress, bitsReversed);
304+
break;
280305
}
281306
if (!result) {
282307
return false;
@@ -318,6 +343,10 @@ static inline bool machoFile_parseFileImpl64(struct machoFile * self,
318343
memcpy(&self->uuid, &((struct uuid_command*) ((void*) lc))->uuid, 16);
319344
result = true;
320345
break;
346+
347+
case LC_FUNCTION_STARTS:
348+
result = machoFile_handleFunctionStarts(self, (void*) lc, baseAddress, bitsReversed);
349+
break;
321350
}
322351
if (!result) {
323352
return false;

src/parser/file/macho/machoFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "objectFile.h"
2929
#include "vector_pair_funcFile.h"
30+
#include "vector_uint64.h"
3031

3132
#include "../binaryFile.h"
3233
#include "../debugInfo.h"
@@ -59,6 +60,7 @@ struct machoFile {
5960

6061
/** The functions mapped to their object file. */
6162
vector_pairFuncFile_t functions;
63+
vector_uint64_t functionStarts;
6264
};
6365

6466
/**

0 commit comments

Comments
 (0)