<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi all!
<div><br>
</div>
<div>I played around with the LLVM LTO example(https://llvm.org/docs/LinkTimeOptimization.html#example-of-link-time-optimization), but got some questions.</div>
<div><br>
</div>
<div>As the document says, the function foo4 should be removed. However, under my test, function foo4 is not removed.</div>
<div>I have tried both gnu ld with LLVMgold.so and lld, neither of them removes function foo4.
</div>
<div><br>
</div>
<div>I compile and run the example as instructed:</div>
<div>% clang -flto -c a.c -o a.o # <-- a.o is LLVM bitcode file</div>
<div>% clang -c main.c -o main.o # <-- main.o is native object file</div>
<div>% clang -flto a.o main.o -o main # <-- standard link command with -flto</div>
<div>% readelf -sW ./main | awk '$4 == "FUNC"'</div>
<div> 1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2)</div>
<div> 2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2)</div>
<div> 27: 0000000000400440 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones</div>
<div> 28: 0000000000400470 0 FUNC LOCAL DEFAULT 13 register_tm_clones</div>
<div> 29: 00000000004004b0 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux</div>
<div> 32: 00000000004004e0 0 FUNC LOCAL DEFAULT 13 frame_dummy</div>
<div> 43: 00000000004005c0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini</div>
<div> 46: 00000000004005c4 0 FUNC GLOBAL DEFAULT 14 _fini</div>
<div> 47: 0000000000000000 0 FUNC GLOBAL DEFAULT UND printf@@GLIBC_2.2.5</div>
<div> 48: 0000000000400510 23 FUNC GLOBAL DEFAULT 13 foo4</div>
<div> 49: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_2.2.5</div>
<div> 54: 0000000000400550 101 FUNC GLOBAL DEFAULT 13 __libc_csu_init</div>
<div> 56: 0000000000400430 2 FUNC GLOBAL HIDDEN 13 _dl_relocate_static_pie</div>
<div> 57: 0000000000400400 43 FUNC GLOBAL DEFAULT 13 _start</div>
<div> 59: 0000000000400530 26 FUNC GLOBAL DEFAULT 13 main</div>
<div> 60: 00000000004004f0 25 FUNC GLOBAL DEFAULT 13 foo1</div>
<div> 62: 00000000004003c8 0 FUNC GLOBAL DEFAULT 11 _init</div>
<div><br>
</div>
<div><br>
</div>
<div>Is there something that I'm doing wrong?</div>
<div><br>
</div>
<div>Here is the source code of a.c and main.c:</div>
<div>--- a.h ---</div>
<div>extern int foo1(void);</div>
<div>extern void foo2(void);</div>
<div>extern void foo4(void);</div>
<div><br>
</div>
<div>--- a.c ---</div>
<div>#include "a.h"</div>
<div><br>
</div>
<div>static signed int i = 0;</div>
<div><br>
</div>
<div>void foo2(void) {</div>
<div> i = -1;</div>
<div>}</div>
<div><br>
</div>
<div>static int foo3() {</div>
<div> foo4();</div>
<div> return 10;</div>
<div>}</div>
<div><br>
</div>
<div>int foo1(void) {</div>
<div> int data = 0;</div>
<div><br>
</div>
<div> if (i < 0)</div>
<div> data = foo3();</div>
<div><br>
</div>
<div> data = data + 42;</div>
<div> return data;</div>
<div>}</div>
<div><br>
</div>
<div>--- main.c ---</div>
<div>#include <stdio.h></div>
<div>#include "a.h"</div>
<div><br>
</div>
<div>void foo4(void) {</div>
<div> printf("Hi\n");</div>
<div>}</div>
<div><br>
</div>
<div>int main() {</div>
<div> return foo1();</div>
<div>}</div>
<div><br>
</div>
<div><br>
</div>
Thanks!<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Xu Mingjie</div>
</body>
</html>