<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi All, <div class="">I am using LLVM to analysis some C++ codes. I need to find the related function implementation for a given indirect CallInst. </div><div class="">To make my question a little bit clear, I construct a simple example here. For “<b class=""> call void %3(%class.Base* %1)</b>”, I need to figure out </div><div class="">Which implementation is called ( it can be manually figured out that _ZN1A5helloEv (A::hello() is called,how to figure it out in a pass ?)</div><div class=""><br class=""></div><div class="">My initial thinking is trace back from "<b class="">call void %3(%class.Base* %1)” </b>until find <b class="">%obj1 = alloca %class.A, align 8, </b>but how can I associate <b class="">%class.A </b>with <b class="">@_ZTV1A, </b>so I can get the function from <b class="">@_ZTV1A ?</b></div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">;virtual tables</div><div class="">@_ZTV1A = linkonce_odr unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1A to i8*), i8* bitcast (void (%class.A*)* @_ZN1A5helloEv to i8*)] }, align 8</div><div class="">@_ZTV1B = linkonce_odr unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1B to i8*), i8* bitcast (void (%class.B*)* @_ZN1B5helloEv to i8*)] }, align 8</div><div class=""><br class=""></div><div class="">;A::hello()</div><div class=""><div class="">define linkonce_odr void @_ZN1A5helloEv(%class.A* %this) unnamed_addr #2 align 2 {</div><div class="">entry:</div><div class=""> %this.addr = alloca %class.A*, align 8</div><div class=""> store %class.A* %this, %class.A** %this.addr, align 8</div><div class=""> %this1 = load %class.A*, %class.A** %this.addr, align 8</div><div class=""> %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0))</div><div class=""> ret void</div><div class="">}</div></div><div class=""><br class=""></div><div class="">;B::hello()</div><div class=""><div class="">define linkonce_odr void @_ZN1B5helloEv(%class.B* %this) unnamed_addr #2 align 2 {</div><div class="">entry:</div><div class=""> %this.addr = alloca %class.B*, align 8</div><div class=""> store %class.B* %this, %class.B** %this.addr, align 8</div><div class=""> %this1 = load %class.B*, %class.B** %this.addr, align 8</div><div class=""> %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0))</div><div class=""> ret void</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><div class="">define i32 @main() #0 {</div><div class="">entry:</div><div class=""> %obj1 = alloca %class.A, align 8</div><div class=""> %obj2 = alloca %class.B, align 8</div><div class=""> %C = alloca %class.Base*, align 8</div><div class=""> call void @_ZN1AC1Ev(%class.A* %obj1) #4</div><div class=""> call void @_ZN1BC1Ev(%class.B* %obj2) #4</div><div class=""> %0 = bitcast %class.A* %obj1 to %class.Base*</div><div class=""> store %class.Base* %0, %class.Base** %C, align 8</div><div class=""> %1 = load %class.Base*, %class.Base** %C, align 8</div><div class=""> %2 = bitcast %class.Base* %1 to void (%class.Base*)***</div><div class=""> %vtable = load void (%class.Base*)**, void (%class.Base*)*** %2, align 8</div><div class=""> %vfn = getelementptr inbounds void (%class.Base*)*, void (%class.Base*)** %vtable, i64 0</div><div class=""> %3 = load void (%class.Base*)*, void (%class.Base*)** %vfn, align 8</div><div class=""><b class=""> call void %3(%class.Base* %1)</b></div><div class=""> ret i32 0</div><div class="">}</div></div><div class=""> </div><div class=""><br class=""></div><div class="">Related C++ code</div><div class=""><br class=""></div><div class=""><div class="">class Base {</div><div class=""> public:</div><div class=""> virtual void hello();</div><div class="">};</div><div class=""><br class=""></div><div class="">class A: public Base {</div><div class="">public:</div><div class=""> void hello() {</div><div class=""> printf("hello\n");</div><div class=""> };</div><div class="">};</div><div class=""><br class=""></div><div class="">class B: public Base {</div><div class="">public:</div><div class=""> void hello() {</div><div class=""> printf("world\n");</div><div class=""> };</div><div class="">};</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">int main() {</div><div class=""> A obj1;</div><div class=""> B obj2;</div><div class=""> Base * C = &obj1;</div><div class=""> C->hello();</div><div class="">}</div></div><div class=""> </div><div class=""><div class="">
Best Regards,<br class=""></div><div class="">Chao</div></div></body></html>