linux - nasm,86_64,linux ,"hello world"程序。当链接时,它说 "relocation truncated to fit"

标签 linux assembly nasm

[section .data]
strHello db "Hello World"
STRLEN equ $-strHello
MessageLength equ 9
Message db "hi!!!!   "


[section .text]
global main
main:
mov edx,STRLEN;
mov ecx,strHello;
mov ebx,1
mov eax,4
int 0x80


call DispStr


mov ebx,0   
mov eax,1   
int 0x80    


DispStr:      
  mov ax,MessageLength
  mov dh,0
  mul dh
  add ax,Message
  mov bp,ax 
  mov ax,ds
  mov es,ax 
  mov cx,MessageLength
  mov ax,01301h 
  mov bx,0007h
  mov dl,0
  int 10h
  ret 

编译运行:

$ nasm -f elf64 helloworld.asm -o helloworld.o
$ gcc -s -o helloworld helloworld.o
helloworld.o: In function `DispStr':
helloworld.asm:(.text+0x31): relocation truncated to fit: R_X86_64_16 against `.data'
collect2: ld return 1

最佳答案

这个确切的错误发生是因为:

add ax,Message

ax 只有 16 位宽,但是 Message 是一个 64 位宽的地址,因此在重定位期间它不适合。

我已经在以下位置详细解释了这个错误:https://stackoverflow.com/a/32639540/895245

这种情况下的解决方案是使用链接描述文件,如:Using .org directive with data in .data section: In connection with ld

此存储库包含引导扇区和 BIOS 的工作示例:https://github.com/cirosantilli/x86-bare-metal-examples/tree/d217b180be4220a0b4a453f31275d38e697a99e0

关于linux - nasm,86_64,linux ,"hello world"程序。当链接时,它说 "relocation truncated to fit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17318924/

相关文章:

linux - 在 X11 中检测笔记本电脑盖子关闭(至少对于 Xorg/Linux)

linux - 当你在 Bash 中执行 "ls"时会发生什么

regex - 用于处理目录的 Bash 脚本 (REGEX)

linux - 在汇编中使用 printf 会导致管道传输时输出为空,但可以在终端上使用

assembly - .W 后缀在 ARM 汇编程序中的用法

linux - 链接错误 : DSO missing from command line

c++ - 为什么函数序言中没有 "mov rsp, rbp"?

linux - 使用断点和符号调试时的gdb问题

assembly - 汇编语言将变量存储在特定地址

assembly - 将十进制转换为十六进制