如何在llvm IR表示中链接extern图书馆

时间:2016-10-06 16:56:23

标签: c++ clang llvm

嘿,我使用clang ++用IR命令转换程序:

clang ++ -S -emit-llvm program.cpp

然后我用这个.ll文件搜索llvm传递给某些属性。是否有可能在此表示中获得外部图书馆? 通常我只能看到来自调用函数的名字。

示例:

#include "stdio.h"

 int main(int argc, char const *argv[]) {
    printf("hi");
    return 0;
 }

IR:

; ModuleID = 'main.cpp'
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"

 @.str = private unnamed_addr constant [3 x i8] c"hi\00", align 1

; Function Attrs: norecurse uwtable
  define i32 @main(i32 %argc, i8** %argv) #0 {
  entry:
   %retval = alloca i32, align 4
   %argc.addr = alloca i32, align 4
   %argv.addr = alloca i8**, align 8
   store i32 0, i32* %retval, align 4
   store i32 %argc, i32* %argc.addr, align 4
   store i8** %argv, i8*** %argv.addr, align 8
   %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0))
   ret i32 0
     }

declare i32 @printf(i8*, ...) #1

attributes #0 = { norecurse uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.8.1 (tags/RELEASE_381/final)"}

在这种情况下,我只能看到printf函数。有没有办法在表示中获得“stdio.h”?

1 个答案:

答案 0 :(得分:0)

stdio.h只包含printf的声明,而不是定义。编译器在发出LLVM IR时看不到它。

从技术上讲,您可以使用LTO(包括静态归档库)在IR中获取完整程序。但是通常libc不是为LTO构建的,无论如何你都不会看到printf的主体。

对于嵌入式开发(固件......),LTO阶段通常会看到完整的程序。