Skip to content
MachoFindSection

MachoFindSection

Description

Find a section by (segment, section) name pair.

Success

Returns a pointer to the matching MachoSection, borrowed from self (valid until MachoDeinit).

Failure

Returns NULL when no section matches.

Usage example (Cross-references)

Usage examples (Cross-references)
            return false;
    
        const MachoSection *info_sec   = MachoFindSection(&e->dsym, "__DWARF", "__debug_info");
        const MachoSection *abbrev_sec = MachoFindSection(&e->dsym, "__DWARF", "__debug_abbrev");
        const MachoSection *str_sec    = MachoFindSection(&e->dsym, "__DWARF", "__debug_str");
    
        const MachoSection *info_sec   = MachoFindSection(&e->dsym, "__DWARF", "__debug_info");
        const MachoSection *abbrev_sec = MachoFindSection(&e->dsym, "__DWARF", "__debug_abbrev");
        const MachoSection *str_sec    = MachoFindSection(&e->dsym, "__DWARF", "__debug_str");
        const MachoSection *info_sec   = MachoFindSection(&e->dsym, "__DWARF", "__debug_info");
        const MachoSection *abbrev_sec = MachoFindSection(&e->dsym, "__DWARF", "__debug_abbrev");
        const MachoSection *str_sec    = MachoFindSection(&e->dsym, "__DWARF", "__debug_str");
    
        const u8 *info_b   = info_sec ? BufData(MachoBuf(&e->dsym)) + info_sec->offset : NULL;
        }
    
        const MachoSection *hit = MachoFindSection(&m, "__TEXT", "__text");
        bool                ok  = hit != NULL && hit->addr == 0x100000000ull && hit->size == 0x100;
        // Right section name in the wrong segment -> no match.
        bool                ok  = hit != NULL && hit->addr == 0x100000000ull && hit->size == 0x100;
        // Right section name in the wrong segment -> no match.
        ok = ok && MachoFindSection(&m, "__DATA", "__text") == NULL;
        // Right segment, wrong section -> no match.
        ok = ok && MachoFindSection(&m, "__TEXT", "__nope") == NULL;
        ok = ok && MachoFindSection(&m, "__DATA", "__text") == NULL;
        // Right segment, wrong section -> no match.
        ok = ok && MachoFindSection(&m, "__TEXT", "__nope") == NULL;
    
        MachoDeinit(&m);
        ok      = ok && m.has_uuid;
        ok      = ok && VecLen(&m.segments) > 0;
        ok      = ok && MachoFindSection(&m, "__TEXT", "__text") != NULL;
        ok      = ok && VecLen(&m.symbols) > 0;
            return false;
        }
        const MachoSection *hit = MachoFindSection(&m, "__TEXT", "__text");
        ok = ok && hit != NULL && hit->addr == 0x100000000ull && hit->size == 0x100 && hit->offset == 0;
        ok = ok && MachoFindSection(&m, "__DATA", "__text") == NULL;
        const MachoSection *hit = MachoFindSection(&m, "__TEXT", "__text");
        ok = ok && hit != NULL && hit->addr == 0x100000000ull && hit->size == 0x100 && hit->offset == 0;
        ok = ok && MachoFindSection(&m, "__DATA", "__text") == NULL;
        ok = ok && MachoFindSection(&m, "__TEXT", "__nope") == NULL;
        MachoDeinit(&m);
        ok = ok && hit != NULL && hit->addr == 0x100000000ull && hit->size == 0x100 && hit->offset == 0;
        ok = ok && MachoFindSection(&m, "__DATA", "__text") == NULL;
        ok = ok && MachoFindSection(&m, "__TEXT", "__nope") == NULL;
        MachoDeinit(&m);
        DefaultAllocatorDeinit(&alloc);
        ok       = ok && VecLen(&m.sections) == 2;
        // The target at index 1 must be found (real ++i reaches it).
        const MachoSection *hit = MachoFindSection(&m, "__TEXT", "__cstring");
        ok = ok && hit != NULL && hit->addr == 0x100000100ull && hit->size == 0x80 && hit->offset == 0x100;
        if (ok)
Last updated on