[llvm-dev] How does LLVM know where to resolve declared only functions?

Peng Yu via llvm-dev llvm-dev at lists.llvm.org
Sat Jan 26 17:03:23 PST 2019


> It's the linkers job to hook together functions and definitions that end up in the same binary. Your OS will then hook in functions from other binaries when your executable is loaded into memory.

How does it know whether it is a system function or a user-defined function?

It seems that user functions have higher priorities over system
functions as demonstrated by the following example.

Is there any way to always use system functions for certain function
calls (but not always default to system functions) when there are
local functions with the same name?

==> main.c <==
/* vim: set noexpandtab tabstop=2: */
#include <stdio.h>

int main() {
    puts("Hello World!");
    return 0;
}

==> myputs.c <==
/* vim: set noexpandtab tabstop=2: */
#include <stdio.h>

int myputs(char *s) {
    return printf("myputs:%s\n", s);
}

$ ./main1.sh
clang -Wall -pedantic -S -emit-llvm -c -o main.ll main.c
clang -Wall -pedantic -S -emit-llvm -c -o myputs.ll myputs.c
clang -Wall -pedantic -c -o main.o main.ll
clang -Wall -pedantic -c -o myputs.o myputs.ll
clang main.o myputs.o -o ./main.exe
./main.exe
Hello World!
sed -i 's/@myputs\>/@puts/g' myputs.ll
clang -Wall -pedantic -c -o myputs.o myputs.ll
clang main.o myputs.o -o ./main.exe
./main.exe
myputs:Hello World!

$ cat ./main1.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
clang -Wall -pedantic -S -emit-llvm -c -o main.ll main.c
clang -Wall -pedantic -S -emit-llvm -c -o myputs.ll myputs.c
clang -Wall -pedantic -c -o main.o main.ll
clang -Wall -pedantic -c -o myputs.o myputs.ll
clang main.o myputs.o -o ./main.exe
./main.exe
sed -i 's/@myputs\>/@puts/g' myputs.ll
clang -Wall -pedantic -c -o myputs.o myputs.ll
clang main.o myputs.o -o ./main.exe
./main.exe


-- 
Regards,
Peng


More information about the llvm-dev mailing list