assembly - 重定位被截断以适应 r_386_8 针对 .bss'

标签 assembly x86 nasm ld relocation

当我尝试将我的源代码构建为适用于 Linux 的 32 位静态可执行文件时

 nasm -f elf -F dwarf -g loop.asm
 ld -m elf_i386 -o loop loop.o

我收到此 R_386_8 错误,知道是什么原因造成的吗?

foo.o: in function `loop1':
foo.asm:(.text+0x12): relocation truncated to fit: R_386_8 against `.bss'
foo.o: in function `endend':
foo.asm:(.text+0x2f): relocation truncated to fit: R_386_8 against `.bss'

循环.asm

cr equ 13 
lf equ 10 

section .bss
numA resb 1

section .text

global _start:

mov [numA],byte 0
call loop1
jmp endend
loop1:
xor cx,cx
mov al, $numA
cmp cx, 0x0A
jle else 
inc al
jmp end
else:
dec al
jmp end
end:
mov [$numA], al
inc cx
cmp cx,20
jle loop1

endend:
mov dl,$numA
mov ah,2
int 21h           ; note: DOS system calls won't work in Linux

(编者注:此代码有多个错误;此问答主要是关于阻止它链接的错误。但修复它不会使 Linux 程序正常工作。)

最佳答案

在 NASM 中,$numAnumA 相同。 A leading $ stops the assembler from considering it as a register name .因此,您可以编写 mov eax, [$eax] 从名为 eax 的符号加载 eax 寄存器。 (因此您可以链接使用 int eax = 123; 的 C)

所以 mov [$numA], al 看起来很奇怪,但它实际上只是 mov [numA], al 而不是错误的来源。


您从 mov dl,$numA 获取地址低字节的 mov dl, imm8 错误。

大多数时候,这是 Basic use of immediates vs. square brackets in YASM/NASM x86 assembly 的情况。 - 您打算从该地址的内存加载的位置,例如 movzx edx, byte [numA]mov dl, [numA]


链接器警告您,因为 numA 的地址不适合 1 个字节,因此 r_386_8 重定位必须截断地址。

_8 告诉您这是一个重定位,要求链接器填写 8 位(1 字节)作为绝对地址。 (8 位相对分支位移具有不同的重定位类型,尽管通常您会使用 32 位位移来跳转到另一个文件中的符号。)

r_386 告诉您这是一个 i386 重定位而不是某种类型的 r_x86_64重定位(可以是绝对的或相对于 RIP 的),或 MIPS 跳转目标重定位(需要将偏移量右移 2)。可能相关:Relocations in the System V gABI (通用 ABI,i386 SysV psABI 是其“处理器补充”)。

关于assembly - 重定位被截断以适应 r_386_8 针对 .bss',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928566/

相关文章:

assembly - Intel 和 AMD x86-64 实现的兼容子集是什么?

assembly - 通过堆栈进行 32 位扩展乘法

linux - Hello World 程序 Nasm 汇编和 C 的执行指令数不同

assembly - ARM assembly 中的 movw 和 movt

c++ - 高度优化的矩阵乘法代码的 MSVC 和 GCC 之间的性能差异

c - 在以下 NASM 任务中需要帮助

c - 为什么 scanf() 加载的地址似乎低于我正在写入的缓冲区的地址?

multithreading - LOCK 前缀与 MESI 协议(protocol)?

程序集:用于自定义操作系统键盘支持的引导加载程序

assembly - 引导加载程序不断将 eax 添加到 al