I’m on a Linux environment and I need to make a program that retrieves some data that is placed in one of the sections of its executable file. So, how to get a pointer to a section of a program (by its name) from within itself?
I know it’s possible to use elf_getdata() passing the index of the section as argument to get and Elf_Data struct and one of the fields of this struct is d_buf, which is a pointer to the actual data. However, it seems the elf_getdata() function makes a copy of the section data from the file to the memory and that’s not what I want. I want a pointer to the data the has been loaded to the memory in loading time.
So, guys, any idea?
Actually, using
libelf, it’s possible to use theElf64_Shdrstruct (for 64-bit systems) to get a pointer to a section, because thesh_addrfield do points to the actual adress where the section will be loaded in runtime. So, it can be used as a pointer. This way, it’s not even necessary to use theelf_getdata()function to retrieve aElf_Datastruct.Since what I want to do is a library which other object files can be linked against, my code may have a function which opens the executable file itself to make use of some
libelffeatures, so that it can read data from the main file sections, as follows: