Accessing String Table Indexes for .rodata

Henry C
Mon Aug 20 16:16:00 GMT 2018


I have a sample code like this:
#include <cstdio>

void myprintf(const char* ptr) {
        printf("%p\n", ptr);

int main() {
        myprintf("hello world");

I would like to access the .rodata by using elf.h.  Someone told me
this is the right place to ask how.  If not, please do let me know.

I am able to access the string table for .rodata section by calling my function:
void print_rodata_table64(int32_t fd,
Elf64_Ehdr eh,
Elf64_Shdr sh_table[],
uint32_t indexToRodata)
char* sh_str; /* section-header string-table is also a section. */

/* Read section-header string-table */
        Elf64_Shdr& sh = sh_table[indexToRodata];
sh_str = malloc(sh.sh_size);
        lseek(fd, (off_t)sh.sh_offset, SEEK_SET);
        read(fd, (void *)sh_str, sh.sh_size);
for (uint64_t i = 0; i < sh.sh_size; i++) {
printf("data[%lu]=%u|%c\n", i, (uint32_t)sh_str[i], sh_str[i]);

Tho, I have no clue how to get the index to each of the string in the
string table above.

Any sample code or pointer is highly appreciated!

And one more related question is that I noticed the virtual memory
addresses of the string literals are same as the offsets to the
(executable) file.  Is it intended?  Guaranteed?


More information about the Elfutils-devel mailing list