Skip to content
ElfOpenFromMemoryCopy

ElfOpenFromMemoryCopy

Description

Parse an ELF object from an in-memory byte range – R-value / copy form (mirrors VecInsertR).

Parser allocates its own buffer through alloc and MemCopys the caller’s bytes in. The caller’s pointer is never retained: after this call returns, the caller’s buffer is theirs to do anything with (including free, mutate, or hand to another parser).

Parameters

Name Direction Description
out out Populated on success.
data in Raw ELF bytes. Read-only here; caller keeps them.
data_size in Length of data in bytes.
alloc in Allocator for the internal copy and the section / symbol vectors. Must outlive the Elf.

Success

Returns true; out owns an independent copy of data.

Failure

Returns false; logs the failing step. out is left zeroed and the caller’s data is untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
    bool elf_open_from_memory_copy(Elf *out, const u8 *data, size data_size, Allocator *alloc) {
        if (!out || !data || !alloc) {
            LOG_FATAL("ElfOpenFromMemoryCopy: NULL argument (contract violation)");
        }
        Buf copy = BufInit(alloc);
        Buf copy = BufInit(alloc);
        if (!VecReserve(&copy, (u64)data_size)) {
            LOG_ERROR("ElfOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);
            return false;
        }
    
        Elf  elf;
        bool ok = ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc));
        if (!ok) {
            DefaultAllocatorDeinit(&alloc);
    
        Elf  elf;
        bool ok = ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc));
        if (ok) {
            ok = VecLen(&elf.segments) == 2;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        DefaultAllocator alloc = DefaultAllocatorInit();
        Elf              elf;
        bool             opened = ElfOpenFromMemoryCopy(&elf, bytes, len, ALLOCATOR_OF(&alloc));
        if (opened)
            ElfDeinit(&elf);
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, dbg_blob, sizeof(dbg_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf  elf;
        bool ok = ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc));
        if (ok) {
            // Names resolved through the in-range shstrtab.
    
        Elf  elf;
        bool ok = ElfOpenFromMemoryCopy(&elf, bad, sizeof(bad), ALLOCATOR_OF(&alloc));
        if (ok) {
            // Empty shstrtab => every section name decodes to "".
    
        Elf  elf;
        bool ok = ElfOpenFromMemoryCopy(&elf, bad, sizeof(bad), ALLOCATOR_OF(&alloc));
        if (ok) {
            ok = VecLen(&elf.sections) == N_SECTIONS;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        DefaultAllocator alloc = DefaultAllocatorInit();
        Elf              elf;
        bool             opened = ElfOpenFromMemoryCopy(&elf, b, sizeof(b), ALLOCATOR_OF(&alloc));
        bool             ok     = opened && elf.header.shnum == 1 && VecLen(&elf.sections) == 1;
        if (opened)
        DefaultAllocator alloc = DefaultAllocatorInit();
        Elf              elf;
        bool             opened = ElfOpenFromMemoryCopy(&elf, elf_blob, sizeof(elf_blob), ALLOCATOR_OF(&alloc));
        if (opened)
            ElfDeinit(&elf);
        DefaultAllocator alloc = DefaultAllocatorInit();
        Elf              elf;
        bool             opened = ElfOpenFromMemoryCopy(&elf, bad, sizeof(bad), ALLOCATOR_OF(&alloc));
        if (opened) {
            // Should not happen on real code; clean up and fail the test.
    static bool open_sym(Elf *elf, DefaultAllocator *alloc) {
        build_sym_blob();
        return ElfOpenFromMemoryCopy(elf, sym_blob, sizeof(sym_blob), ALLOCATOR_OF(alloc));
    }
    
        Elf  elf;
        bool opened = ElfOpenFromMemoryCopy(&elf, sym_blob, sizeof(sym_blob), ALLOCATOR_OF(&alloc));
        if (opened)
            ElfDeinit(&elf);
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, bid_blob, sizeof(bid_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, bid_blob, sizeof(bid_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, bid_blob, sizeof(bid_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, bid_blob, sizeof(bid_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, bid_blob, sizeof(bid_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, dl_blob, sizeof(dl_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, dl_blob, sizeof(dl_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, dl_blob, sizeof(dl_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, dl_blob, sizeof(dl_blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf  elf;
        bool opened = ElfOpenFromMemoryCopy(&elf, blob, sizeof(blob), ALLOCATOR_OF(&alloc));
        if (opened)
            ElfDeinit(&elf);
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, blob, sizeof(blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, blob, sizeof(blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, blob, sizeof(blob), ALLOCATOR_OF(&alloc))) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf  elf;
        bool opened = ElfOpenFromMemoryCopy(&elf, buf, sizeof(buf), ALLOCATOR_OF(&alloc));
        if (opened)
            ElfDeinit(&elf);
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        bool ok = false;
        Elf  elf;
        if (ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DwarfCfi cfi;
            if (DwarfCfiBuildFromElf(&cfi, &elf, base)) {
        bool ok = false;
        Elf  elf;
        if (ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DwarfCfi cfi;
            if (DwarfCfiBuildFromElf(&cfi, &elf, base)) {
        bool ok = false;
        Elf  elf;
        if (ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DwarfCfi cfi;
            if (DwarfCfiBuildFromElf(&cfi, &elf, base)) {
        bool ok = false;
        Elf  elf;
        if (ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DwarfCfi cfi;
            if (DwarfCfiBuildFromElf(&cfi, &elf, base)) {
        bool ok = false;
        Elf  elf;
        if (ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DwarfCfi cfi;
            if (DwarfCfiBuildFromElf(&cfi, &elf, base)) {
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
        build_elf_with_debug_line_8192(elfbuf, &elf_len, dl, dl_len);
    
        if (!ElfOpenFromMemoryCopy(&fx->elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&fx->alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base))
            return false;
        u64       elf_len = 0;
        build_elf_with_debug_line_8192(elfbuf, &elf_len, dl, dl_len);
        if (!ElfOpenFromMemoryCopy(elf, elfbuf, (size)elf_len, base))
            return false;
        return DwarfLinesBuildFromElf(out, elf, base);
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base))
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DefaultAllocatorDeinit(&alloc);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DebugAllocatorDeinit(&dbg);
            return false;
    
        Elf elf;
        if (!ElfOpenFromMemoryCopy(&elf, elfbuf, (size)elf_len, base)) {
            DebugAllocatorDeinit(&dbg);
            return false;
        u64       elf_len = 0;
        build_elf_with_debug_line_8192(elfbuf, &elf_len, dl, dl_len);
        if (!ElfOpenFromMemoryCopy(elf, elfbuf, (size)elf_len, base))
            return false;
        bool built = DwarfLinesBuildFromElf(lines, elf, base);
Last updated on