You are not logged in.

#1 2005-09-20 08:26:05

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

ELF image size

Asking this here is probably a bit of a stretch, but it can't hurt to try.

Does anybody know how to determine the size of an arbitrary ELF image in memory? In particular, I want to find the size of a dynamic library in memory. I can just iterate through all of the ELF headers obviously, but is there some quicker way?

Offline

#2 2005-09-21 16:34:17

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: ELF image size

Well, I wasn't holding my breath, anyway...
In case anyone's interested, here's the function I've written to do it. Pass in the base address of the dynamic library, and it'll return the size.

#include <link.h> /* ElfW macro, <elf.h> include */

ElfW(Addr) dlsize(ElfW(Ehdr) *ehdr)
{
    int i;
    ElfW(Phdr) *phdr;
    ElfW(Addr) start, end;

    /* Find the first program header */
    phdr = (ElfW(Phdr)*)((ElfW(Addr))ehdr + ehdr->e_phoff);

    /* Find the final PT_LOAD segment's extent */
    for (i = 0; i < ehdr->e_phnum; ++i)
        if (phdr[i].p_type == PT_LOAD)
            end = phdr[i].p_vaddr + phdr[i].p_memsz;

    /* The start (virtual) address is always zero, so just return end.*/
    return end;
}

Offline

Board footer

Powered by FluxBB