This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]