2016-02-12 101 views
1

我寫了這個小引導程序,但是當我鏈接它,我得到這個錯誤:我不明白爲什麼LD給出rilocazione(搬遷)錯誤

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' 

我的鏈接器命令是:寫在GNU彙編

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

我的Bootloader代碼是:

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 
+2

'ld'想鏈接64位代碼。嘗試添加'-melf_i386'選項。 – Jester

+1

也驚訝'code16'工程。通常你需要像'.code16'這樣的一段時間。即使在與Jester的更改成功鏈接後,您的代碼也不會像您期望的那樣運行。 –

+1

我強烈建議如果您在GNU彙編程序中執行bootloader,爲了簡單起見,您可以在最後一段代碼之後和.byte 0x55 .byte之前刪除'data'部分並將數據放入'.text'部分0xaa'。 '。= _ start + 510'沒有做你認爲的事。既然你在鏈接器'-Ttext 0x7c00'上有這個,'_start'的值將是0x7c00而不是0x0000。 '。= _ start + 510'會將位置計數器設置爲0x7c00 + 510,這不是你想要的。試試這個,而不是'.space 510 - (。-_start)' –

回答

0

您需要更好地瞭解bootloader的開發。儘管你得到了一個鏈接器錯誤,即使你設法創建引導程序並將其放入軟盤映像中,它也不會按預期運行。你可以在你的答案中看到我的評論中的一些問題。

因爲你的問題就是關於連接錯誤,我會做出基於該錯誤消息,並得到一個受過教育的猜測,你還裝配有類似:

as boot.s -o boot.o 

的名字你程序集文件可能不同,但彙編程序命令將會類似。


我不知道這只是當你複製你的代碼的StackOverflow推出一個錯字,但此行以你的引導代碼的頂部:

code16 

應該是:

.code16 

此錯誤表明您正在開發64位環境:

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

當使用GNU彙編器和GNU鏈接器時,您需要將16位引導加載程序代碼組裝到32位對象中,並且還需要鏈接爲32位代碼。在64位開發環境中ASLD通常默認爲生成64位對象和可執行文件,而不是32位,這是您問題的原因。

命令像這樣可能會解決您的鏈接錯誤:

as --32 boot.s -o boot.o 
ld -melf_i386 -Ttext 0x7c00 --oformat=binary boot.o -o boot.bin 

第一個命令彙編爲使用--32選擇32位ELF對象。第二個鏈接爲32位,使用-melf_i386選項。這應該可以消除你的錯誤。

相關問題