且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用llvm_sys的LLVMCreateTargetMachine在Rust中生成目标文件时出现段错误

更新时间:2022-10-15 16:37:30

您误解了如何调用LLVMGetTargetFromTriple:

pub unsafe extern "C" fn LLVMGetTargetFromTriple(Triple: *const c_char,
                                                 T: *mut LLVMTargetRef,
                                                 ErrorMessage: *mut *mut c_char)
                                                 -> LLVMBool

此函数接受一个指向C样式的字符串的指针,该字符串在发生错误的情况下将被填充.结果报告了该方法的实际成功.

This function accepts a pointer to a C-style string that will be filled in in case of error. The actual success of the method is reported by the result.

根据 LLVM文档:

查找与给定三元组对应的目标并将其存储在T中.

Finds the target corresponding to the given triple and stores it in T.

成功返回0 . (可选)在ErrorMessage中返回任何错误.使用LLVMDisposeMessage来处理消息.

Returns 0 on success. Optionally returns any error in ErrorMessage. Use LLVMDisposeMessage to dispose the message.

(重点是我的)

现在,该调用失败,因此永远不会初始化目标ref,因此您试图在未定义的代码上调用方法.

Right now, that call is failing, thus the target ref is never initialized, thus you are trying to call methods on undefined code.