Skip to content

Commit 673be14

Browse files
committed
Cleaned up the Mach-O symbol table parser
1 parent 7641049 commit 673be14

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/parser/file/macho/macho_parser.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
* CallstackLibrary, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
#include <string.h>
22+
#include "macho_parser.h"
2323

24+
#include <string.h>
2425
#include <mach-o/nlist.h>
2526
#include <mach-o/stab.h>
2627

27-
#include "macho_parser.h"
28-
2928
#include "cache.h"
3029
#include "macho_parser_nlist.h"
3130

@@ -44,13 +43,13 @@
4443
ENSYM: <function address>
4544
*/
4645

47-
bool macho_parseSymtab(struct symtab_command* command,
48-
const void* baseAddress,
49-
uint64_t offset,
50-
bool bytesSwapped,
51-
bool bit64,
52-
macho_addObjectFile objCb,
53-
macho_addFunction funCb,
46+
bool macho_parseSymtab(struct symtab_command* command,
47+
const void* baseAddress,
48+
const uint64_t offset,
49+
const bool bytesSwapped,
50+
const bool bit64,
51+
const macho_addObjectFile objCb,
52+
const macho_addFunction funCb,
5453
...) {
5554
if (objCb == NULL && funCb == NULL) return false;
5655

@@ -67,7 +66,7 @@ bool macho_parseSymtab(struct symtab_command* command,
6766
struct objectFile* currObj = NULL;
6867

6968
for (uint32_t i = 0; i < nsyms; ++i) {
70-
struct macho_parser_nlist entry = macho_parser_nlist_from(baseAddress + symoff + offset + i * macho_parser_nlist_sizeof(bit64), bit64, bytesSwapped);
69+
const struct macho_parser_nlist entry = macho_parser_nlist_from(baseAddress + symoff + offset + i * macho_parser_nlist_sizeof(bit64), bit64, bytesSwapped);
7170
switch (entry.n_type) {
7271
case N_BNSYM:
7372
if (currFun.has_value) {

src/parser/file/macho/macho_parser.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* CallstackLibrary - Library creating human-readable call stacks.
33
*
4-
* Copyright (C) 2024 mhahnFr
4+
* Copyright (C) 2024 - 2025 mhahnFr
55
*
66
* This file is part of the CallstackLibrary.
77
*
@@ -22,15 +22,13 @@
2222
#ifndef macho_parser_h
2323
#define macho_parser_h
2424

25+
#include <stdarg.h>
2526
#include <stdbool.h>
26-
2727
#include <mach-o/loader.h>
2828

2929
#include "objectFile.h"
3030
#include "optional_pair_funcFile.h"
3131

32-
#include "../function.h"
33-
3432
/**
3533
* @brief The function prototype for the callback called with a new object file object.
3634
*
@@ -47,23 +45,27 @@ typedef void (*macho_addFunction)(pair_funcFile_t, va_list);
4745
/**
4846
* @brief Parses the given Mach-O symbol table.
4947
*
50-
* At least one callback function needs to be provided (the other one
51-
* may be `NULL`) or both. If none ise passed, the given symbol table
52-
* is not parsed and `false` is returned.
53-
*
54-
* The object file callback is called when all information associated
55-
* with an object file is read.
56-
* The function / object file callback is called for every symbol that
57-
* is not external.
58-
* Both functions are passed the additional arguments passed to this function.
48+
* At least one callback function needs to be provided (the other one may be
49+
* @c NULL) or both. If none is given, the given symbol table is not parsed and
50+
* @c false is returned.
51+
* <br><br>
52+
* The object file callback will be called when all information associated with
53+
* an object file is read.<br>
54+
* The function / object file callback is called for every symbol that is not
55+
* external.<br>
56+
* Both functions will be passed the additional arguments passed to this
57+
* function.
5958
*
6059
* @param command the Mach-O symbol table load command
61-
* @param baseAddress the start address of the Mach-O file the given symbol table is in
60+
* @param baseAddress the start address of the Mach-O file the given symbol
61+
* table is in
6262
* @param offset the additional parsing offset
63-
* @param bytesSwapped whether the bytes need to be swapped to be in host byte order
63+
* @param bytesSwapped whether the bytes need to be swapped to be in host byte
64+
* order
6465
* @param bit64 whether a 64 bit Mach-O file is parsed
6566
* @param objCb the object file callback function
6667
* @param funCb the function / object file pair callback function
68+
* @param ... the additional parameters to pass to the given functions
6769
* @return whether the symbol table was parsed successfully
6870
*/
6971
bool macho_parseSymtab(struct symtab_command* command,

0 commit comments

Comments
 (0)