This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Segmentation fault when trying to call a function locating in .data section
- From: "PRC" <panruochen at gmail dot com>
- To: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 15 Aug 2007 15:23:05 +0800
- Subject: Segmentation fault when trying to call a function locating in .data section
I'm trying the following code on Linux
------code start------
#include <stdio.h>
#include <stdlib.h>
static unsigned char _func1[0x200] = {
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0xc3, 0x90, 0x90, 0x90, 0x90,
};
int main()
{
((void (*)(void)) _func1)();
printf("okay\n");
return 0;
}
------code end------
gcc a.c
./a.out
Segmentation fault
I am confused. In x86 architecture, the physical memory pages have either Read or Write attributes. But they don't have eXecutable attributes. Since the code stored in _func1 is valid instructions, how segmentation fault arise when CPU run
to _func1?
Thanks,
PRC