views:

42

answers:

3

I need to find the size of an elf image for some computation. I have tried with the readelf utility on linux which gives the informations about the headers and section. I need to have the exact file size of the elf(on the whole).

How do I find the size of the ELF from the header information or Is there any other means to find the size of an elf without reading the full image.

+1  A: 

You can use the stat functions family (stat(), lstat(), fstat()) to get the size of any file (using the st_size member of the stat member). Do you need something more specific?


If you really want to use the ELF structure, use the elf.h header which contains that structure:

typedef struct {
         unsigned char  e_ident[EI_NIDENT];
         uint16_t       e_type;
         uint16_t       e_machine;
         uint32_t       e_version;
         ElfN_Addr      e_entry;
         ElfN_Off       e_phoff;
         ElfN_Off       e_shoff;
         uint32_t       e_flags;
         uint16_t       e_ehsize;
         uint16_t       e_phentsize;
         uint16_t       e_phnum;
         uint16_t       e_shentsize;
         uint16_t       e_shnum;
         uint16_t       e_shstrndx;
 } Elf32_Ehdr;

It's the header of an ELF32 file (replace 32 with 64 for a 64-bit file). e_ehsize is the size of the file in bytes.

Bastien Léonard
How do we use this structure to find the size of any elf file.. I mean API kind of stuff..
fasil
any usage example ?
fasil
What is your language? In C for example it should be pretty easy to read the header with `fread()`, then print the `e_ehsize` member.
Bastien Léonard
I m using C and I have to find the elf image size which might be in RAM...
fasil
A: 

Perhaps gelf could be useful.

GElf is a generic, ELF class-independent API for manipulat- ing ELF object files. GElf provides a single, common inter- face for handling 32-bit and 64-bit ELF format object files.

specifically these functions:

elf32_fsize, elf64_fsize - return the size of an object file type

Dmitry Yudakov
Any usage example for using GELF library...
fasil
I cannot go with this as I need to install the library stuffs in the Root file system that would take more space..
fasil
A: 

Have you tried using the gnu "readelf" utility?

http://sourceware.org/binutils/docs/binutils/readelf.html

areo