使用C程序编译ASM文件的命令[重复]

问题描述 投票:4回答:1

这个问题在这里已有答案:

使用à64Linux系统并使用NASM。

我正在尝试将我的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 <stdio.h>

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了。问题来自我的命令还是我的代码?

c linux assembly x86-64
1个回答
2
投票

我使用您的@Jester解决方案解决了这个问题:

gcc -no-pie -o executable main.o hello.o

并感谢Ped7g的解释。

© www.soinside.com 2019 - 2024. All rights reserved.