Is there a C function to get the size of a file's data segment?

375 Views Asked by At

I'm wondering if there's a C function that can be used to get another executable file's data segment size? For example, something that works like the size utility in Linux? The output of size includes the information I'm after, the data segment size; for example, it might look like:

text    data    bss     dec     hex     filename
294880  81920   11592   388392  5ed28   ranlib
2

There are 2 best solutions below

0
On BEST ANSWER

Using elf.h you have all the data structures you need. Follow the main ELF header to the program headers, then iterate through them. The PT_LOAD header(s) with write permission is/are the data segment(s).

0
On

It doesn't look like there's single function to get it, but the size source code is pretty small (~500 lines). Is what you're looking for in here? https://github.com/pathscale/binutils/blob/master/binutils/size.c