Code:
- #include<stdio.h>
- int f(int a,int b)
- {
- return a+b;
- }
- int main()
- {
- int(*p)(int ,int );
- void *q;
- p=f;
- q=(void *)p;
- printf("the code is : 0x%x", *((int *)q));
- *((int *)q)=0x12345678;
- return 0;
- }
打印的是函数f中的语句“return a+b"
Code:
- $ ./motify_code
- the code is : 0x8be58955
- Segmentation fault
修改正文段时报错可知:linux环境下的正文段是受操作系统写保护的。
补充:函数的代码存储在代码段中。