environment: Apple clang version 14.0.3 (clang-1403.0.22.14.1)
I am trying to override c++ symbols at linking time for the purpose of inserting measurement code into an existing library.
Perhaps typical linkers would search for symbols in specifing order of library and adopt the first hit. Xcode's ld apparently does this in units of a object file. If you create one object file to override one function and link it first, but call another function in the same object file contained in the original library, that one will be adopted. As a result, you cannot override function. To override a function, the function to be overridden must be moved to a new cpp file and separated from the object file.
Here is a sample program to test this. https://onedrive.live.com/redir?resid=DD46698E2D493F32!395&authkey=!AJqfiva7CXIDI_Y&e=OaFlSr
As you can see in main.cpp, func() and bar() are called from main(). func() and bar() are implemented in func.cpp and bar.cpp, respectively. They are linked to libTest.lib. To override func(), link libTest2.lib implementing func2() in func2.cpp, before libTest.lib. If you run make and look at the code with objdump -d a.out, you can see the override. Because the content of func2() is return i+1, __Z4funci contains leal 1(%rdi), %eax.
Then, build with make clean and make CONCAT=1, func() and bar() are concatenated into one cpp file and compiled. The generated a.out is checked in the same way, you will see movl %edi, %eax at same position which means return i; which is the content of func().
This sample is small in scale, but in a larger project, it can be quite a hassle to isolate the functions you want to override.
Is there any easier way to override the function? Since the original function name cannot be changed, it is our policy not to use -alias symbol_name alternate_symbol_name.
Thanks.
Before we start, I want to point you at An Apple Library Primer, which explains a lot of backstory.
Xcode's
ld
apparently does this in units of a object file.
Yep. That’s pretty standard for Unix-y toolchains in my experience. One of our recent WWDC sessions (links in An Apple Library Primer) describes the process in great detail.
I’m not sure if there’s any way to achieve your goal. This limitation is pretty fundamental to how object files work. But perhaps someone else will chime in with something I’ve missed.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"