2017-04-11 34 views
2

我在網上搜索了一段時間,還沒有看到任何類似的問題。Mac操作系統:ld:在__DATA,__數據reloc 0:長度<2和X86_64_RELOC_UNSIGNED不支持

因此,我正在編寫一個小型編譯器,將語言編譯爲x64代碼。

但是,當我嘗試做clang generated-code.s,我得到:

ld: in section __DATA,__data reloc 0: length < 2 and X86_64_RELOC_UNSIGNED not supported file '/var/folders/3g/ydlqd7bx3819pfrr9n52zjv80000gn/T/hello_world-795c7e.o' for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我真的不明白此錯誤消息的含義。這是否意味着我有未定義的符號?任何人都可以向我解釋嗎?

這裏是產生我的編譯器代碼:

.data 

    .globl Main_protoObj 
    .globl Int_protoObj 
    .globl String_protoObj 
    .globl Main_init 
    .globl Int_init 
    .globl String_init 
    .globl Bool_init 
    .globl Main_main 

class_objTab: 
    .word Object_protoObj 
    .word Object_init 
    .word IO_protoObj 
    .word IO_init 
    .word String_protoObj 
    .word String_init 
    .word Int_protoObj 
    .word Int_init 
    .word Bool_protoObj 
    .word Bool_init 
    .word Main_protoObj 
    .word Main_init 

Object_protoObj: 
    .word 0 
    .word 1 
    .word Object_dispatch_table 

IO_protoObj: 
    .word 1 
    .word 3 
    .word IO_dispatch_table 

String_protoObj: 
    .word 2 
    .word 3 
    .word String_dispatch_table 
    .word 0 
    .asciz "" 
    .align 8 

Int_protoObj: 
    .word 3 
    .word 2 
    .word Int_dispatch_table 
    .word 0 

Bool_protoObj: 
    .word 4 
    .word 2 
    .word Bool_dispatch_table 
    .word 0 

Main_protoObj: 
    .word 5 
    .word 1 
    .word Main_dispatch_table 

String_dispatch_table: 
    .word Object_abort 
    .word Object_copy 
    .word String_length 
    .word String_concat 
    .word String_substr 

Main_dispatch_table: 
    .word Object_abort 
    .word Object_copy 
    .word IO_out_string 
    .word IO_out_int 
    .word IO_in_string 
    .word IO_in_int 
    .word Main_main 

Bool_dispatch_table: 
    .word Object_abort 
    .word Object_copy 

Object_dispatch_table: 
    .word Object_abort 
    .word Object_copy 

IO_dispatch_table: 
    .word Object_abort 
    .word Object_copy 
    .word IO_out_string 
    .word IO_out_int 
    .word IO_in_string 
    .word IO_in_int 

Int_dispatch_table: 
    .word Object_abort 
    .word Object_copy 

int_const0: 
    .word 3 
    .word 4 
    .word Int_dispatch_table 
    .word 22 

string_const0: 
    .word 2 
    .word 6 
    .word String_dispatch_table 
    .word int_const0 
    .asciz "Hello, World.\n \n\n" 
    .align 8 


.text 


Object_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    leave 
    ret 

IO_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    movq %rdi, %rdi 
    callq Object_init 
    leave 
    ret 


String_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    movq %rdi, %rdi 
    callq Object_init 
    movq '', 32(%rbp) 
    movq $0, 24(%rbp) 
    leave 
    ret 


Int_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    movq %rdi, %rdi 
    callq Object_init 
    movq $0, 24(%rbp) 
    leave 
    ret 


Bool_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    movq %rdi, %rdi 
    callq Object_init 
    movq $0, 24(%rbp) 
    leave 
    ret 



Main_main: 
    pushq %rbp 
    movq %rsp, %rbp 
    subq $8, %rsp 
    movq %rdi, -8(%rbp) 
    movq -8(%rbp), %rdi 
    leaq string_const0(%rip), %rax 
    movq %rax, %rsi 
    movq 16(%rdi), %r10 
    movq 16(%r10), %r10 
    callq* %r10 
    leave 
    ret 

Main_init: 
    pushq %rbp 
    movq %rsp, %rbp 
    movq %rdi, %rdi 
    callq IO_init 
    leave 
    ret 
+1

如果我不得不冒險猜測 - 您正在使用'.word'來存儲標籤的偏移量。 '.word'我相信是16位的,你可能試圖將偏移量放在不能用16位表示的標籤上。也許你打算在這些情況下使用'.int'或'.quad'? –

+0

@MichaelPetch謝謝,這解決了這個問題 –

回答

1

錯誤:

ld: in section __DATA,__data reloc 0: length < 2 and X86_64_RELOC_UNSIGNED not supported

看起來好像是在說你的對象已分配的空間來存儲偏移到的數據項的一個相當模糊的方式這可能無法保持鏈接時生成的偏移量。

快速查看您的代碼,可以看出您使用的是.word以及一個值作爲標籤。示例.word Object_protoObj其中Object_protoObj偏移量是標籤。指令.word允許0x0000和0xffff(16位)之間的值。鏈接器錯誤可能表明它生成的標籤的偏移量不適合16位字。

也許你應該使用.quad(允許64位值)或.int(32位值)?我不知道如何在表格中訪問這些偏移量,因此您必須根據您的使用要求選擇.quad.int

相關問題