如何在AMD64架构的Linux汇编程序中使用RIP相对寻址?我正在寻找一个使用AMD64 RIP相对地址模式的简单示例(Hello world程序).
例如,以下64位汇编程序将与普通(绝对寻址)一起使用:
.text
.global _start
_start:
mov $0xd, %rdx
mov $msg, %rsi
pushq $0x1
pop %rax
mov %rax, %rdi
syscall
xor %rdi, %rdi
pushq $0x3c
pop %rax
syscall
.data
msg:
.ascii "Hello world!\n"
Run Code Online (Sandbox Code Playgroud)
我猜测使用RIP相对寻址的相同程序将是这样的:
.text
.global _start
_start:
mov $0xd, %rdx
mov msg(%rip), %rsi
pushq $0x1
pop %rax
mov %rax, %rdi
syscall
xor %rdi, %rdi
pushq $0x3c
pop %rax
syscall
msg:
.ascii "Hello world!\n"
Run Code Online (Sandbox Code Playgroud)
编译时,正常版本运行正常:
as -o hello.o hello.s && ld -s -o hello hello.o && ./hello
Run Code Online (Sandbox Code Playgroud)
但我无法使RIP版本正常工作. …
我有一个正在建设的图书馆.当我运行以下任何一个时,我的所有对象都会连续编译和链接:
ar rcs lib/libryftts.a $^
gcc -shared $^ -o lib/libryftts.so
在我的Makefile中.我也能够成功安装它们/usr/local/lib
当我用nm测试文件时,所有的功能都在那里.我的问题是,当我跑步gcc testing/test.c -lryftts -o test && file ./test或gcc testing/test.c lib/libryftts.a -o test && file ./test
它说:
test: ELF 64-bit LSB shared object而不是test: ELF 64-bit LSB executable像我期望的那样.我究竟做错了什么?
考虑这个 AMD64 汇编程序:
.globl _start
_start:
xorl %edi, %edi
movl $60, %eax
syscall
Run Code Online (Sandbox Code Playgroud)
如果我编译gcc -nostdlib并运行ldd a.out,我得到这个:
statically linked
Run Code Online (Sandbox Code Playgroud)
如果我改为使用gcc -static -nostdlib并运行编译它ldd a.out,我会得到这个:
not a dynamic executable
Run Code Online (Sandbox Code Playgroud)
什么之间的区别statically linked和not a dynamic executable?如果我的二进制文件已经静态链接,为什么添加-static会影响任何事情?
以下程序:
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int j = 0; j < argc; j++)
printf("%d: %s\n", j, argv[j]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
内置于静态链接的 PIE 中:
gcc -g -fpie main.c -static-pie -o ld.so
Run Code Online (Sandbox Code Playgroud)
工作正常:
$ ./ld.so foo bar
0: ./ld.so
1: foo
2: bar
Run Code Online (Sandbox Code Playgroud)
但是当我将该程序用作另一个程序的 ELF 解释器时:
$ gcc -g main.c -Wl,-I./ld.so -o a.out
Run Code Online (Sandbox Code Playgroud)
它像这样崩溃:
gdb -q ./a.out
(gdb) run
Starting program: /tmp/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7da84e2 in __ctype_init () at ctype-info.c:31
31 …Run Code Online (Sandbox Code Playgroud)