List all ELF sections of all currently shared objects

I am looking for a good way to list all ELF sections and their addresses of the current binary and all common objects that the current process could load.

I am aware of the existence of dl_iterate_phdr (), which gives me an easy-to-use list of ELF segments, but I am looking for ELF sections, so this is not the right call for me.

I do not care about portability, if it works on Linux with ELF, I am happy!

Any hint appreciated!

+4
source share
3 answers

I assume that you are trying to programmatically access this information from a C / C ++ program. I suspect that you can do what you want with the GNU dendritic library library (BFD) library, which is used to implement the GNU linker, objdump, etc. (he also has a Wikipedia article ). See, In particular, section 2.6 of the manual for sections. BFD creates a linked struct bfd_section "list, accessed through a member of the struct bfd sections. I think you should be able to open the current binary using bfd_fopen in the argv [0] file. BFD comes with binutils .

+3
source

This sounds like a very bad idea, but if you have legal use, I just parse /proc/self/maps and open the link files. There is no reason to believe that the section headers are even mapped to memory; in all likelihood, they are located immediately after .data on the disk, and thus will be overwritten with zeros for .bss when the shared object is mapped to memory.

Once you open the files, you can use libbfd , but I would just use elf.h directly. It’s easy to keep track of the Ehdr table in the Shdr table.

+3
source

The readelf command can do this, for example, readelf -s

This is not trivial, so posting a link to the source code for readelf seems like the best choice. One of the reasons for this is the large number of macros needed to process the header and section structures.

http://rpm5.org/docs/api/readelf_8c-source.html

+1
source

Source: https://habr.com/ru/post/1394297/


All Articles