<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Note, I'm using a linux system.<br></div><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 27 Jan 2019 at 11:33, Peng Yu <<a href="mailto:pengyu.ut@gmail.com">pengyu.ut@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> 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.<br>
<br>
How does it know whether it is a system function or a user-defined function?<br>
<br>
It seems that user functions have higher priorities over system<br>
functions as demonstrated by the following example.<br>
<br>
Is there any way to always use system functions for certain function<br>
calls (but not always default to system functions) when there are<br>
local functions with the same name?<br>
<br>
==> main.c <==<br>
/* vim: set noexpandtab tabstop=2: */<br>
#include <stdio.h><br>
<br>
int main() {<br>
    puts("Hello World!");<br>
    return 0;<br>
}<br>
<br>
==> myputs.c <==<br>
/* vim: set noexpandtab tabstop=2: */<br>
#include <stdio.h><br>
<br>
int myputs(char *s) {<br>
    return printf("myputs:%s\n", s);<br>
}<br>
<br>
$ ./main1.sh<br>
clang -Wall -pedantic -S -emit-llvm -c -o main.ll main.c<br>
clang -Wall -pedantic -S -emit-llvm -c -o myputs.ll myputs.c<br>
clang -Wall -pedantic -c -o main.o main.ll<br></blockquote><div><br></div><div>objdump -t main.o</div><div>...</div><div>0000000000000000         *UND*    0000000000000000 puts<br></div><div>...</div><div>At this point the .o doesn't specify where puts should be found.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
clang -Wall -pedantic -c -o myputs.o myputs.ll <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
clang main.o myputs.o -o ./main.exe<br></blockquote><div><br></div><div><div>objdump -t main.exe</div><div>...</div>0000000000000000       F *UND*    0000000000000000              puts@@GLIBC_2.2.5</div><div>...</div><div>The linker found puts in glibc. However using LD_PRELOAD you could still link against a different implementation at runtime.<br> </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
./main.exe<br>
Hello World!<br>
sed -i 's/@myputs\>/@puts/g' myputs.ll<br>
clang -Wall -pedantic -c -o myputs.o myputs.ll<br>
clang main.o myputs.o -o ./main.exe<br></blockquote><div><br></div><div>objdump -t main.exe</div><div>...<br></div><div>0000000000000000       F *UND*    0000000000000000              printf@@GLIBC_2.2.5<br>...</div><div>No external reference to puts, because it was found during the link step.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
./main.exe<br>
myputs:Hello World!<br>
<br>
$ cat ./main1.sh<br>
#!/usr/bin/env bash<br>
# vim: set noexpandtab tabstop=2:<br>
<br>
set -v<br>
clang -Wall -pedantic -S -emit-llvm -c -o main.ll main.c<br>
clang -Wall -pedantic -S -emit-llvm -c -o myputs.ll myputs.c<br>
clang -Wall -pedantic -c -o main.o main.ll<br>
clang -Wall -pedantic -c -o myputs.o myputs.ll<br>
clang main.o myputs.o -o ./main.exe<br>
./main.exe<br>
sed -i 's/@myputs\>/@puts/g' myputs.ll<br>
clang -Wall -pedantic -c -o myputs.o myputs.ll<br>
clang main.o myputs.o -o ./main.exe<br>
./main.exe<br>
<br>
<br>
-- <br>
Regards,<br>
Peng<br>
</blockquote></div></div></div></div></div></div>