程序在写入代码段时收到信号 SIGSEGV

Program received signal SIGSEGV when writing to code section

我在尝试覆盖调用指令的地址时遇到 Program received signal SIGSEGV 错误。
我将调用参数的地址(地址 0x8048579)存储在 eax 中 和 edx 中的新值 (0xb7fb773a)。 根据我的理解,指令 mov %edx,(%eax) 应该这样做,但它失败了。
我做错了什么?
这是我大学任务的一部分,所以没有真正的系统受到伤害:-)

代码:

   0x08048566 <+35>:    mov    -0x8(%ebp),%edx
   0x08048569 <+38>:    mov    -0x4(%ebp),%eax
=> 0x0804856c <+41>:    mov    %edx,(%eax)    // Fails here.
   0x0804856e <+43>:    movl   [=10=]x0,(%esp)
   0x08048575 <+50>:    call   0x8048370 <_exit@plt>

登记:

eax            0x8048579    134514041   
edx            0xb7fb773a   -1208256710  

失败:

Program received signal SIGSEGV, Segmentation fault.
0x0804856c in foo (argv=0x58575655) at my_code.c:34

// 更新 1:CPU 信息:

Architecture:          i686
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 69
Stepping:              1
CPU MHz:               2306.609
BogoMIPS:              4613.21
L1d cache:             32K
L1d cache:             32K
L2d cache:             6144K

指令mov %edx,(%eax)确实会将EDX寄存器值保存到EAX指向的内存地址。

此代码失败,因为 Linux 上的 ELF 可执行文件的代码部分 (.text) 不可写。因此,写入此部分会导致来自 OS.

的 SIGSEGV 信号

您可能对这个问题的答案感兴趣:How can I make GCC compile the .text section as writable in an ELF binary?