llvm-link:我可以链接具有不同功能的.ll文件和通用名称

问题描述 投票:0回答:1

我有3个文件,第一个文件是库代码mylib.c,第二个是myProgram1.c,第三个是myProgram2.c。我需要一个组合的llvm bitcode文件。所以我做了以下事情。

clang mylib.c -S -emit-llvm -o mylib.ll

clang myProgram1.c -S -emit-llvm -o myProgram1.ll

clang myProgram2.c -S -emit-llvm -o myProgram2.ll

llvm-link mylib.ll myProgram1.ll myProgram2.ll -o final.ll -S

但问题是我在myProgram1.c和myProgram2.c中都有主要功能。所以它无法链接说有重复的符号。 那么有什么方法可以通过llvm-link链接所有3个文件?

llvm
1个回答
2
投票

我有一个想法,使用可能有效的llvm-extract

假设我们有以下两个源代码。

$ cat prog1.c

#include <stdlib.h>
#include <stdio.h>

void f1() {
  printf("f1\n");
}

int main() {
  f1();
  return 0;
}

$ cat prog2.c

#include <stdio.h>

extern void f1();

void f2() {
  printf("f2\n");
}

int main() {
  f1();
  f2();
  return 0;
}

首先我们用clang编译它。

$ clang -c -emit-llvm prog2.c
$ clang -c -emit-llvm prog1.c

然后列出用llvm-nm在字节码中定义的符号。

$ llvm-nm prog1.bc |grep -v main
---------------- T f1
                 U printf

然后使用llvm-extract提取排除main的所有内容。

$ llvm-extract -func=f1 prog1.bc -o prog1_f1.bc  

最后,链接llvm-link。

$ llvm-link prog2.bc prog1_f1.bc -o link.bc  
$ llvm-dis link.bc -o -

看来字节码是有效的。

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

@.str.4 = private unnamed_addr constant [4 x i8] c"f2\0A\00", align 1
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: noinline nounwind uwtable
define void @f2() #0 {
entry:
  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0))
  ret void
}

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

; Function Attrs: noinline nounwind uwtable
define i32 @main() #0 {
entry:
  %retval = alloca i32, align 4
  store i32 0, i32* %retval, align 4
  call void (...) bitcast (void ()* @f1 to void (...)*)()
  call void @f2()
  ret i32 0
}

; Function Attrs: noinline nounwind uwtable
define void @f1() #0 {
entry:
  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0))
  ret void
}

attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "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-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "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" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0, !0}

!0 = !{!"clang version 4.0.0 (tags/RELEASE_400/final)"}
© www.soinside.com 2019 - 2024. All rights reserved.