linux - Nasm - 符号 `printf' 导致 R_X86_64_PC32 重定位溢出

标签 linux gcc segmentation-fault nasm x86-64

<分区>

我正在尝试在 nasm 中创建一个应该显示字母 a 的简单程序。然而,它给了我一个段错误并说:

./a.out: Symbol `printf' causes overflow in R_X86_64_PC32 relocation
Segmentation fault (core dumped)

基本上,我试图将值 0x61(字母 a 的十六进制)移动到内存地址 1234,然后将其作为参数传递给 printf。这是我的确切代码:

extern printf
section .text
global main
main:
push rbp
mov rax,0
mov qword [1234], 0x61 ; move 0x61 into address 1234
mov rdi, qword [1234] ; mov address 1234 into rdi
call printf ; should print the letter a
pop rbp
mov rax,0
ret

我正在运行 Linux x86_64

最佳答案

尝试使用 -no-pie 进行编译,查看这些帖子以获得解释: Assembling with GCC causes weird relocation error with regards to .data

简而言之:

Debian switched to PIC/PIE binaries in 64-bits mode & GCC in your case is trying to link your object as PIC, but it will encounter absolute address in mov $str, %rdi.

关于linux - Nasm - 符号 `printf' 导致 R_X86_64_PC32 重定位溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071280/

相关文章:

c - 为什么以下代码给出运行时错误 SIGSEGV

c - C 中的 Flex Bison 段错误

c - 如何跟踪 C 中 malloc 中的内存分配错误

linux - 将多个文件写入(倒带)磁带存档

c - 在 Linux C 控制台应用程序中模拟按键

c - gcc 警告 "unnamed struct/union that defines no instances",但该结构确实有一个名称

c - 重置 gcc 的 asm 内联输入

linux - 来自给定输入的空文件夹列表

c++ - 如何指示编译器为虚函数生成别名?

c - 为什么 GCC pad 与 NOP 一起工作?