llvm - How does `declare` in LLVM IR works
问题描述
For example
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
declare i32 @puts(i8* nocapture) nounwind
define i32 @main() {
%cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0
call i32 @puts(i8* %cast210)
ret i32 0
}
I don't understand where the function puts
comes from. It seems to be a C function in stdio.h
, but what does this have to do with LLVM? Where is its implementation?
解决方案
Speaking in C/C++ terms, each translation unit can reference external symbols. For compiler it doesn't matter where symbol is actually defined as far as you have a declaration for it.
After compiling your .c
files you get something that needs to be linked together (object files .o
or LLVM IR .ll
/.bc
). On the linking stage all symbol definitions are resolved (in different ways).
In your example, the puts
function is usually located in the system libc
, which gets linked automatically by default. You won't find LLVM IR code for this function unless you somehow compiled whole libc
into LLVM IR.
Read some general tutorial on the "compilation and linking" topic.
推荐阅读
- mysql - How to update column data based on the select and update query
- c# - "The scope https://graph.microsoft.com/Calendars.Read is not valid" error while getting access token for my app
- python - 如何选择列名并将其用作 Python 中变量名的输入?
- c# - 获取正在访问 ASP.NET Core 应用程序的当前 Windows 用户
- apache-camel - Camel - 检查 body 是实体列表还是简单实体
- java - Spring Security 每次返回相同的令牌
- typescript - I am getting the FileTransferError error code 3 while uploading images to a server using ionic
- swift - 自定义 UITableView 给出 NSInternalInconsistencyException
- html - 带有 PDF 嵌入的 IFrame 在 Chrome 中工作,但在 IE11 中不工作
- android - 如何使用 QR 码捕获 pdf 表单字段?