I'm trying to wrap one symbol by another during link. As I understand this is easily done with ld --wrap option, but on OS X it's not available. There is '-idefinition:indirection', here how I'm trying to cheat main so it will print 42:
a.cpp:
int foo() {
return 1;
}
b.cpp:
int wrap_foo() {
return 42;
}
main.cpp:
#include <cstdio>
int foo();
int wrap_foo();
int main() {
printf("%d\n", foo());
}
How I build and link them:
$ gcc -c *.cpp
$ gcc -Wl,-i__Z3foov:__Z8wrap_foov *.o
duplicate symbol __Z3foov in:
a.o
b.o
ld: 1 duplicate symbol for architecture x86_64
Is it even possible to achieve what I want?
EDIT: In my task I don't have control over a.cpp and main.cpp (have only objects for them), but I can write any code and do whatever I need with the objects before link.
I've found following quistion before which related to similar task (and describes it better) GNU gcc/ld - wrapping a call to symbol with caller and callee defined in the same object file Rereading the answers I see that the case is same, symbol already present and I will have a collision.
How to remove foo from a.o without touching source code?