I compiled a C code using gcc and when I check the sections of the ELF using readelf I can see that the flags for .data section are set to WA (Writable and Allocatable).
Is it possible to modify these flags? Can I make this section executable?
I am using gdb to debug this binary and I would like to set the flags for .data section as Executable at a certain point. So, can this be done using either gdb or gcc?
Yes. If you want to do this as a one-off, the simplest approach may be to compile source to assembly, and modify section attributes there, then compile assembly into object file and link as usual.
You also could call
mprotect(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC)
from within GDB.Note: modifying the flags in the
.data
section after the binary has been linked will have no effect whatsoever: the kernel doesn't look at sections, only atPT_LOAD
segments.Yes, that looks correct. Did it not work?
Is your executable statically linked? If not,
mprotect
should be found (inlibc.so
), and you possibly have a GDB bug. It may help to nudge GDB into findingmprotect
if youprint &mprotect
first.Also note:
mprotect(0x0804a020, 80, PROT_READ, PROT_WRITE, PROT_EXEC)
is very different from what I suggested (mprotect
takes 3 parameters, not 5). You also need to readman mprotect
carefully -- it requires start address to be page-aligned.