Hi, my friends
I finally insert the callInst into the hello.bc file.Then I compile the hello.bc to hello.o file and the check.c to check.o file. And I think by link those to .o file togetherI can get the executable ELF file(clang hello.o check.o -o finalfile).
But when I link the two objective file, it said
“hello.o: In function main': hello.bc:(.text+0x69): undefined reference to
check’
clang: error: linker command failed with exit code 1 (use -v to see invocation)” What’s wrong??
To make it readable I convert the .bc file into .ll file using llvm-dis. The IR code is as follow, any help?
Before running the pass
; ModuleID = ‘hello.bc’
target datalayout = “e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128”
target triple = “i386-pc-linux-gnu”
@.str = private unnamed_addr constant [18 x i8] c"add result is %d\0A\00", align 1
@.str1 = private unnamed_addr constant [20 x i8] c"minus result is %d\0A\00", align 1
@str = private unnamed_addr constant [13 x i8] c"hello world!\00"
define i32 @add(i32 %a, i32 %b) nounwind readnone {
entry:
%add = add nsw i32 %b, %a
ret i32 %add
}
define i32 @minus(i32 %a, i32 %b) nounwind readnone {
entry:
%sub = sub nsw i32 %a, %b
ret i32 %sub
}
define i32 @main() nounwind {
entry:
%call1 = tail call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([18 x i8]* @.str, i32 0, i32 0), i32 3) nounwind
%call3 = tail call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([20 x i8]* @.str1, i32 0, i32 0), i32 1) nounwind
%puts = tail call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @str, i32 0, i32 0))
ret i32 0
}
declare i32 @printf(i8* nocapture, …) nounwind
declare i32 @puts(i8* nocapture) nounwind
After running the pass
; ModuleID = ‘newhello.bc’
target datalayout = “e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128”
target triple = “i386-pc-linux-gnu”
@.str = private unnamed_addr constant [18 x i8] c"add result is %d\0A\00", align 1
@.str1 = private unnamed_addr constant [20 x i8] c"minus result is %d\0A\00", align 1
@str = private unnamed_addr constant [13 x i8] c"hello world!\00"
define i32 @add(i32 %a, i32 %b) nounwind readnone {
entry:
%add = add nsw i32 %b, %a
ret i32 %add
}
define i32 @minus(i32 %a, i32 %b) nounwind readnone {
entry:
%sub = sub nsw i32 %a, %b
ret i32 %sub
}
define i32 @main() nounwind {
entry:
%call1 = tail call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([18 x i8]* @.str, i32 0, i32 0), i32 3) nounwind
%call3 = tail call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([20 x i8]* @.str1, i32 0, i32 0), i32 1) nounwind
%CallCheck = call i32 @check()
%puts = tail call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @str, i32 0, i32 0))
ret i32 0
}
declare i32 @printf(i8* nocapture, …) nounwind
declare i32 @puts(i8* nocapture) nounwind
declare i32 @check()
The original hello.c file
#include <stdio.h>
extern int check();
int add(int a,int b)
{
return a+b;
}
int minus(int a,int b)
{
return a-b;
}
int main()
{
int a=2,b=1;
printf(“add result is %d\n”,add(a,b));
printf(“minus result is %d\n”,minus(a,b));
printf(“hello world!\n”);
return 0;
}
The original check.c file
#include <stdio.h>
void print()
{
printf(“Check function works!\n”);
}