it's quite simple to get a pointer to an specific section of a program from within itself
Not necessarily. The section table is not actually needed at runtime, and can be completely stripped (only segments matter, not sections).
Since the addresses of its sections are defined in runtime, how is it possible to get pointers to sections of a dynamic library?
The library is not at all different from the main executable. The main difference is that the library is usually linked at address 0
(the main executable is not), and relocated by the runtime loader to some other constant offset.
Once you know that offset, just add it to the section start (which you can find from readelf -S foo.so
or from libelf), and voila: you've got the runtime address of the section.
So how can you find the relocation for a given shared library?
Inelegant solution (already suggested by Nick) is to parse /proc/self/maps
.
A better solution is to use (glibc-specific) dl_iterate_phdr
. Documentation here. You'll want to use dlpi_addr
.