我无法理解为什么LD会给出rilocazione(重定位)错误

时间:2016-02-12 18:56:26

标签: assembly linker x86 ld gas

我写了这个小引导加载程序,但是当我链接它时,我收到了这个错误:

 boot.o: nella funzione "_start":
    (.text+0xa): rilocazione adattata per troncamento: R_X86_64_16 contro ".data"

在英语中错误是:

boot.o: In function `_start':
(.text+0xa): relocation truncated to fit: R_X86_64_16 against `.data'

我的链接器命令是:

ld -Ttext 0x7c00 --oformat=binary boot.o -o boot.bin

我用GNU汇编程序编写的引导程序代码是:

    code16

.data
        prova: .string "questa è una prova"

.text
.globl _start

_start:

//now i try to print on the screen a string
//for do that i'm gonna to use int 0x10

mov $0x13,%ah
mov $0x0,%bh
mov $0x01,%bl
mov $20,%cx

push $[prova]

pop %es

int $0x10

jmp boot
boot:
.=_start+510


.byte 0x55
.byte 0xaa

1 个答案:

答案 0 :(得分:0)

You need to get a better understanding of bootloader development. Although you are getting a linker error, even if you do manage to build the bootloader and put it in a floppy image, it won't run as expected. You can see my comments under your answer for some of the issues.

Since your question is just about the linker error, I am going to make an educated guess based on the error message you did get, that you also assembled with something like:

!important

The name of your assembly file may be different, but the assembler command will be similar.


I'm not sure this was just a typo introduced when you copied your code to Stackoverflow but this line at the top of your bootloader code:

as boot.s -o boot.o

Should be:

code16

This error suggests that you are developing in a 64-bit environment:

.code16

When using GNU Assembler and GNU Linker, you need to assemble your 16-bit bootloader code to 32-bit objects, and also link as 32-bit code. In a 64-bit development environment AS and LD generally default to generating 64-bit objects and executables, not 32-bit which is the cause of your problems.

Commands like these would probably solve your linker error:

boot.o: nella funzione "_start":
(.text+0xa): rilocazione adattata per troncamento: R_X86_64_16 contro ".data"

The first command assembles to a 32-bit ELF object using the as --32 boot.s -o boot.o ld -melf_i386 -Ttext 0x7c00 --oformat=binary boot.o -o boot.bin option. The second links as 32-bit using --32 option. This should eliminate your error.