Character designation during layout on OS X

I am trying to wrap one character with another during a link. As far as I understand, this is easy to do with the ld-wrap option, but on OS X it is not available. There is a β€œsingle definition: indirect,” here, as I try to trick the main one into typing 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 do I create and link them:

 $ gcc -c *.o $ gcc -Wl,-i__Z3foov:__Z8wrap_foov *.o duplicate symbol __Z3foov in: ao bo ld: 1 duplicate symbol for architecture x86_64 

Is it possible to achieve what I want?


EDIT: In my task, I have no control over a.cpp and main.cpp (there are only objects for them), but I can write any code and do everything I need with objects before the link.

I found the following quistion, which is associated with a similar task (and describes it better) GNU gcc / ld - transferring a call to a character with the caller and the called user defined in the same object file When I read the answers, I see that the case is the same the symbol is already present and I will have a collision.

How to remove foo from ao without touching the source code?

0
c ++ gcc shared-libraries ld macos
source share
1 answer

Does this need to be done in link mode compared to runtime? Notice that you are completely sure what you are trying to do, so just ask.

The problem with using this linker command is that it creates a new character, so you get a duplicate of the character.

 -alias symbol_name alternate_symbol_name Create an alias named alternate_symbol_name for the symbol symbol_name. By default the alias symbol has global visibil- ity. This option was previous the -idef:indir option. 

If you don't have a.cpp that defines foo (i.e. you omit foo), you can do something like this:

 tmp$ gcc -Wl,-alias,__Z8wrap_foov,__Z3foov bo main.o tmp$ ./a.out 42 

What you can always do is have a.cpp another foo like original_foo . Thus, if you want this implementation, you can use an alias to match this with foo . Not perfect, but he will achieve what you want. You are already trying to configure it by reference, so this may seem potentially acceptable.

+2
source share

All Articles