Skip to content

MachoSymbol

Description

Decoded nlist_64 entry. name is borrowed from the file’s symbol string table (LC_SYMTAB.stroff).

Usage example (Cross-references)

Usage examples (Cross-references)
    
        // Address at the function start.
        const MachoSymbol *s  = MachoFileResolveAddress(&m, 0x100000010ull);
        bool               ok = s && s->name && ZstrCompare(s->name, "my_function") == 0;
        u64 vaddr        = runtime_addr - slide;
    
        const MachoSymbol *sym = MachoFileResolveAddress(&m, vaddr);
        bool               ok  = sym != NULL && sym->name != NULL &&
                  ZstrFindSubstring(sym->name, "test_macho_resolves_running_binary_symbol") != NULL;
    
        // (1) Main file's LC_SYMTAB.
        const MachoSymbol *s = MachoFileResolveAddress(&entry->main, file_relative);
        if (s && s->name && s->name[0]) {
            *out_name = s->name;
            if (n_strx >= ctx->strsize)
                continue; // bad index; skip
            MachoSymbol sym;
            sym.name          = (const char *)(str_base + n_strx);
            sym.value         = n_value;
    // matched stabs with all three bits set -- N_SO / N_FUN / N_OSO /
    // etc. were getting through and polluting lookups).
    const MachoSymbol *MachoFileResolveAddress(const MachoFile *self, u64 vaddr) {
        if (!self || self->symbols.length == 0)
            return NULL;
        };
    
        const MachoSymbol *best       = NULL;
        u64                best_value = 0;
        const MachoSymbol *next_above = NULL;
        const MachoSymbol *best       = NULL;
        u64                best_value = 0;
        const MachoSymbol *next_above = NULL;
        u64                next_value = (u64)-1;
    
        for (size i = 0; i < self->symbols.length; ++i) {
            const MachoSymbol *s = &self->symbols.data[i];
            if (s->type & N_STAB_MASK)
                continue; // any high bit set => STAB (debug) entry
        u8          type;          // n_type bitfield
        u8          section_index; // n_sect, 1-based (0 = NO_SECT)
    } MachoSymbol;
    
    typedef Vec(MachoSegment) MachoSegments;
    typedef Vec(MachoSegment) MachoSegments;
    typedef Vec(MachoSection) MachoSections;
    typedef Vec(MachoSymbol) MachoSymbols;
    
    ///
    /// FAILURE : Returns NULL.
    ///
    const MachoSymbol *MachoFileResolveAddress(const MachoFile *self, u64 vaddr);
    
    #endif // MISRA_PARSERS_MACHO_H
Last updated on