使用全局变量从 llvm 字节码编译目标文件时,Clang-3.8 未定义对“main”的引用
Clang-3.8 undefined reference to `main' when compiling object file from llvm bytecode with global variables
我正在尝试通过 C++ API 在 llvm 中实现嵌套函数。在我执行语义检查(以确保将要编译的程序具有正确的嵌套引用等)之后,我在 llvm 中定义了相同范围内的所有函数并将所有变量设置为全局变量。我原以为这会起作用,但我发现自己在使用以下命令从 .o 文件获取可执行文件时遇到了很大的问题:
"llvm-as-3.8 output.ll| llc-3.8 -filetype=obj | clang-3.8 ../library/library.a -v -o out"
通过对全局变量使用 llvm::GlobalValue::WeakAnyLinkage
或 llvm::GlobalValue::ExternalLinkage
链接,我得到:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
...
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
这似乎与main的定义有关,在我的ir代码定义中,谁是:
; Function Attrs: nounwind uwtable
define void @main() #0 {
问题是,当我不使用全局变量时,相同的命令没有任何问题,当然除了在子函数中使用变量的情况,这就是我试图更改 alloca 的原因到全球。
帮助找到解决此问题的方法。
提前致谢。
注 1:我使用 llvm-3.8
更新:
当我从 clang 命令中删除 -v 时,结果是:
refined_output.o: In function bsort':
refined_output.ll:(.text+0x4): undefined reference to
x'
refined_output.ll:(.text+0xa): undefined reference to n'
refined_output.ll:(.text+0x10): undefined reference to
changed'
refined_output.ll:(.text+0x22): undefined reference to i'
refined_output.ll:(.text+0x2c): undefined reference to
changed'
refined_output.ll:(.text+0x42): undefined reference to i'
refined_output.ll:(.text+0x48): undefined reference to
n'
refined_output.ll:(.text+0x50): undefined reference to i'
refined_output.ll:(.text+0x59): undefined reference to
x'
refined_output.ll:(.text+0x60): undefined reference to i'
refined_output.ll:(.text+0x75): undefined reference to
x'
refined_output.ll:(.text+0x7c): undefined reference to i'
refined_output.ll:(.text+0x95): undefined reference to
changed'
refined_output.ll:(.text+0xa3): undefined reference to changed'
refined_output.o: In function
swap':
refined_output.ll:(.text+0xc3): undefined reference to x.1'
refined_output.ll:(.text+0xca): undefined reference to
y'
refined_output.ll:(.text+0xd1): undefined reference to x.1'
refined_output.ll:(.text+0xd9): undefined reference to
t'
refined_output.ll:(.text+0xe0): undefined reference to x.1'
refined_output.ll:(.text+0xe7): undefined reference to
y'
refined_output.ll:(.text+0xf2): undefined reference to y'
refined_output.ll:(.text+0xf8): undefined reference to
t'
refined_output.o: In function main':
refined_output.ll:(.text+0x102): undefined reference to
i.4'
refined_output.ll:(.text+0x10c): undefined reference to seed'
refined_output.ll:(.text+0x123): undefined reference to
x.2'
refined_output.ll:(.text+0x12a): undefined reference to i.4'
refined_output.ll:(.text+0x130): undefined reference to
seed'
refined_output.ll:(.text+0x15d): undefined reference to seed'
refined_output.ll:(.text+0x166): undefined reference to
i.4'
refined_output.ll:(.text+0x16c): undefined reference to i.4'
refined_output.ll:(.text+0x177): undefined reference to
x.2'
refined_output.ll:(.text+0x18d): undefined reference to x.2'
refined_output.ll:(.text+0x19e): undefined reference to
x.2'
refined_output.o: In function printArray':
refined_output.ll:(.text+0x1c4): undefined reference to
msg'
refined_output.ll:(.text+0x1cb): undefined reference to x.2'
refined_output.ll:(.text+0x1d1): undefined reference to
n.3'
refined_output.ll:(.text+0x1d8): undefined reference to msg'
refined_output.ll:(.text+0x1e3): undefined reference to
i.4'
refined_output.ll:(.text+0x1f3): undefined reference to x.2'
refined_output.ll:(.text+0x1fa): undefined reference to
i.4'
refined_output.ll:(.text+0x208): undefined reference to i.4'
refined_output.ll:(.text+0x20e): undefined reference to
i.4'
refined_output.ll:(.text+0x214): undefined reference to n.3'
refined_output.ll:(.text+0x21c): undefined reference to
i.4'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
所有这些变量都在我的程序开始时在 llvm IR 中定义为:
@x = external global i32*
@n = external global i32
@i = external global i32
@changed = external global i1
@x.1 = external global i32*
@y = external global i32*
@t = external global i32
@msg = external global i8*
@x.2 = external global i32*
@n.3 = external global i32
@i.4 = external global i32
@const_string_temp = private constant [3 x i8] c", [=11=]", align 1
@const_string_temp.5 = private constant [2 x i8] c"[=11=]A[=11=]", align 1
@i.6 = external global i32
@x.7 = external global i32
@seed = external global i32
@const_string_temp.8 = private constant [16 x i8] c"Initial array: [=11=]", align 1
@const_string_temp.9 = private constant [15 x i8] c"Sorted array: [=11=]", align 1
我希望这能帮助你帮助我 --- 或者在我的 IR 代码生成中找到问题。
很期待..
@x = external global i32*
等等都是全局变量声明,不是定义。因此,"undefined reference" 错误是有效的,您确实需要定义它们。
有关详细信息,请参阅 http://llvm.org/docs/LangRef.html#global-variables
另请注意,llvm-as / llc 部分是多余的 - clang 可以很好地编译 .ll / .bc 文件。
我正在尝试通过 C++ API 在 llvm 中实现嵌套函数。在我执行语义检查(以确保将要编译的程序具有正确的嵌套引用等)之后,我在 llvm 中定义了相同范围内的所有函数并将所有变量设置为全局变量。我原以为这会起作用,但我发现自己在使用以下命令从 .o 文件获取可执行文件时遇到了很大的问题:
"llvm-as-3.8 output.ll| llc-3.8 -filetype=obj | clang-3.8 ../library/library.a -v -o out"
通过对全局变量使用 llvm::GlobalValue::WeakAnyLinkage
或 llvm::GlobalValue::ExternalLinkage
链接,我得到:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
...
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
这似乎与main的定义有关,在我的ir代码定义中,谁是:
; Function Attrs: nounwind uwtable
define void @main() #0 {
问题是,当我不使用全局变量时,相同的命令没有任何问题,当然除了在子函数中使用变量的情况,这就是我试图更改 alloca 的原因到全球。
帮助找到解决此问题的方法。 提前致谢。 注 1:我使用 llvm-3.8
更新: 当我从 clang 命令中删除 -v 时,结果是:
refined_output.o: In function
bsort': refined_output.ll:(.text+0x4): undefined reference to
x' refined_output.ll:(.text+0xa): undefined reference ton' refined_output.ll:(.text+0x10): undefined reference to
changed' refined_output.ll:(.text+0x22): undefined reference toi' refined_output.ll:(.text+0x2c): undefined reference to
changed' refined_output.ll:(.text+0x42): undefined reference toi' refined_output.ll:(.text+0x48): undefined reference to
n' refined_output.ll:(.text+0x50): undefined reference toi' refined_output.ll:(.text+0x59): undefined reference to
x' refined_output.ll:(.text+0x60): undefined reference toi' refined_output.ll:(.text+0x75): undefined reference to
x' refined_output.ll:(.text+0x7c): undefined reference toi' refined_output.ll:(.text+0x95): undefined reference to
changed' refined_output.ll:(.text+0xa3): undefined reference tochanged' refined_output.o: In function
swap': refined_output.ll:(.text+0xc3): undefined reference tox.1' refined_output.ll:(.text+0xca): undefined reference to
y' refined_output.ll:(.text+0xd1): undefined reference tox.1' refined_output.ll:(.text+0xd9): undefined reference to
t' refined_output.ll:(.text+0xe0): undefined reference tox.1' refined_output.ll:(.text+0xe7): undefined reference to
y' refined_output.ll:(.text+0xf2): undefined reference toy' refined_output.ll:(.text+0xf8): undefined reference to
t' refined_output.o: In functionmain': refined_output.ll:(.text+0x102): undefined reference to
i.4' refined_output.ll:(.text+0x10c): undefined reference toseed' refined_output.ll:(.text+0x123): undefined reference to
x.2' refined_output.ll:(.text+0x12a): undefined reference toi.4' refined_output.ll:(.text+0x130): undefined reference to
seed' refined_output.ll:(.text+0x15d): undefined reference toseed' refined_output.ll:(.text+0x166): undefined reference to
i.4' refined_output.ll:(.text+0x16c): undefined reference toi.4' refined_output.ll:(.text+0x177): undefined reference to
x.2' refined_output.ll:(.text+0x18d): undefined reference tox.2' refined_output.ll:(.text+0x19e): undefined reference to
x.2' refined_output.o: In functionprintArray': refined_output.ll:(.text+0x1c4): undefined reference to
msg' refined_output.ll:(.text+0x1cb): undefined reference tox.2' refined_output.ll:(.text+0x1d1): undefined reference to
n.3' refined_output.ll:(.text+0x1d8): undefined reference tomsg' refined_output.ll:(.text+0x1e3): undefined reference to
i.4' refined_output.ll:(.text+0x1f3): undefined reference tox.2' refined_output.ll:(.text+0x1fa): undefined reference to
i.4' refined_output.ll:(.text+0x208): undefined reference toi.4' refined_output.ll:(.text+0x20e): undefined reference to
i.4' refined_output.ll:(.text+0x214): undefined reference ton.3' refined_output.ll:(.text+0x21c): undefined reference to
i.4'clang: error: linker command failed with exit code 1 (use -v to see invocation)
所有这些变量都在我的程序开始时在 llvm IR 中定义为:
@x = external global i32*
@n = external global i32
@i = external global i32
@changed = external global i1
@x.1 = external global i32*
@y = external global i32*
@t = external global i32
@msg = external global i8*
@x.2 = external global i32*
@n.3 = external global i32
@i.4 = external global i32
@const_string_temp = private constant [3 x i8] c", [=11=]", align 1
@const_string_temp.5 = private constant [2 x i8] c"[=11=]A[=11=]", align 1
@i.6 = external global i32
@x.7 = external global i32
@seed = external global i32
@const_string_temp.8 = private constant [16 x i8] c"Initial array: [=11=]", align 1
@const_string_temp.9 = private constant [15 x i8] c"Sorted array: [=11=]", align 1
我希望这能帮助你帮助我 --- 或者在我的 IR 代码生成中找到问题。
很期待..
@x = external global i32*
等等都是全局变量声明,不是定义。因此,"undefined reference" 错误是有效的,您确实需要定义它们。
有关详细信息,请参阅 http://llvm.org/docs/LangRef.html#global-variables
另请注意,llvm-as / llc 部分是多余的 - clang 可以很好地编译 .ll / .bc 文件。