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環境下的正文段是受作業系統寫保護的。
補充:函數的代碼存儲在代碼段中。