我正在尝试将我的ASM(hello.asm)文件与C文件(main.c)链接并编译为执行文件.
我创建了一个ASM文件,通过printHello函数使用printf打印“Hello”.
extern printf, exit
section .data
format db "Hello", 10, 0
section .text
global printHello
printHello:
sub rsp, 8
mov rsi, 0x12345677
mov rdi, format
xor rax, rax
call printf
mov rdi, 0
call exit
我创建一个简单的main.c并调用我的函数“printHello”来打印“Hello”
#include
void printHello();
int main()
{
printHello();
}
我的编译命令:
$nasm -f elf64 hello.asm
$gcc -c main.c
$gcc -o executable main.o hello.o
$./executable
它打印:
./executable: Symbol `printf' causes overflow in R_X86_64_PC32 relocation
./executable: Symbol `exit' causes overflow in R_X86_64_PC32 relocation
[1] 6011 segmentation fault ./executable
我已经在学习ASM了.问题来自我的命令还是我的代码?
解决方法:
我使用您的@Jester解决方案解决了这个问题:
gcc -no-pie -o executable main.o hello.o
并感谢Ped7g的解释.
标签:c-3,linux,assembly,x86-64
来源: https://codeday.me/bug/20190701/1347580.html