clinuxdata-segment

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


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

Solution

  • 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).