'GDB source code - Access elf header from within source code to get properties

I have a question regarding GDB source code which I want to modify in order to change the read/write access to memory. I have an elf file where the data is coded which means it needs to be transformed before reading and writing. Therefore I have found out that the reading and write operation are performed in read_value_memory (gdb/valops.c) and target_write_memory (gdb/target.c).

Now I need to differentiate to only perform this in case the access it done to an shared object file.

The question is: How can I access the elf from GDB source code to access the elf header properties.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source