I am trying to get offsets/virtual addresses, strings in .rodata and .rodata1 sections.
For example:
#include <cstdio>
void myprintf(const char* ptr) {
printf("%p\n", ptr);
}
int main() {
myprintf("hello world");
myprintf("\0\0");
myprintf("ab\0cde");
}
Above program has .rodata per readelf -a
's output:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[16] .rodata PROGBITS 0000000000400600 00000600
And readelf -W -p .rodata
gives me the offsets and the associated non null strings:
String dump of section '.rodata':
[ 10] %p^J
[ 14] hello world
[ 23] ab
[ 26] cde
I would like to write a C or C++ code to retrieve:
The offsets of all the string literals (e.g. 10, 14, 23 above and the missing one for "\0\0")
The string literals (e.g. "%p\n", "hello wolrd", "\0\0" above)
The offset to the file for .rodata (e.g. 400600 above; is it guaranteed to be the virtual memory address? At least I see it is the case for all the string literal in my test code above.)
Because my end goal is to write a C/C++ code to read in an executable and accept user's input as the offset/virtual memory address, if the input matches the offset/virtual memory address of any string literal, then use printf()
to print it out. Otherwise, ignore. (Thanks @Armali for helping me clarify)
I have read this article. I am able to access the entire string table in .rodata
but not "string table indexes". The article mentions "string table indexes" but it doesn't specify how to retrieve indexes.
Hints?
Also, I wonder why there could be a section called .rodata1
. According to elf manpage:
.rodata1
This section holds read-only data that typically contributes to a nonwritable segment in the process image. This section is of type SHT_PROGBITS. The attribute used is SHF_ALLOC.
It has exactly the same description as .rodata
. Then, why do we have .rodata1
?
Thanks!