【发布时间】:2011-12-28 08:01:53
【问题描述】:
跟进Why is the ELF execution entry point virtual address of the form 0x80xxxxx and not zero 0x0? 和Why do virtual memory addresses for linux binaries start at 0x8048000?,为什么我不能让ld 使用与默认ld -e 不同的入口点?
如果我这样做,我会得到一个返回代码为 139 的 segmentation fault,即使是默认入口点附近的地址也是如此。为什么?
编辑:
我会让问题更具体:
.text
.globl _start
_start:
movl $0x4,%eax # eax = code for 'write' system call
movl $1,%ebx # ebx = file descriptor to standard output
movl $message,%ecx # ecx = pointer to the message
movl $13,%edx # edx = length of the message
int $0x80 # make the system call
movl $0x0,%ebx # the status returned by 'exit'
movl $0x1,%eax # eax = code for 'exit' system call
int $0x80 # make the system call
.data
.globl message
message:
.string "Hello world\n" # The message as data
如果我用as program.s -o program.o 编译它,然后用ld -N program.o -o program 静态链接它,readelf -l program 显示0x0000000000400078 作为文本段的VirtAddr,0x400078 作为入口点。运行时会打印“Hello world”。
但是,当我尝试与ld -N -e0x400082 -Ttext=0x400082 program.o -o program 链接时(将文本段和入口点移动4 个字节),程序将是killed。使用readelf -l 检查它现在会显示两个不同的LOAD 类型的标题,一个在0x0000000000400082,一个在0x00000000004000b0。
当我尝试0x400086 时,一切正常,而且只有一个LOAD 部分。
- 这是怎么回事?
- 哪些内存地址可以选择,哪些不能选择,为什么?
谢谢。
【问题讨论】:
-
我还可以使用链接描述文件修改入口点:stackoverflow.com/a/30536800/895245
标签: linker elf memory-layout