0

I am having the opposite problem that Can you compile a shared object to prefer local symbols even if it's being loaded by a program compiled with -rdynamic? is solving.

Using the naming from the linked question, I have a dynamic library where baz calls bar, and I have a test binary exercising the library, which substitutes its own fake implementation of bar for test purposes. This works fine on Linux, because -rdynamic is used to link.

The source of the test is https://github.com/apache/qpid-dispatch/blob/b172f501028b36d786b4c83bcee1e195cd17fcf2/tests/timer_test.c. The functions being mocked are, among others qd_server_timeout and qd_timer_now (that is the inlined one, see comments).

I am at a loss how to achieve the same on macOS. What are the correct linker options there?

user7610
  • 25,267
  • 15
  • 124
  • 150
  • Maybe I have it. I tried various likely options from `man ld`, and when I gave to CMake this, the test passed `-DCMAKE_EXE_LINKER_FLAGS="-Wl,-export_dynamic,-flat_namespace" -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-export_dynamic,-flat_namespace"` – user7610 Jun 21 '19 at 23:04
  • The above is not enough, I had to also add `-DCMAKE_C_FLAGS="-fno-inline"`. That passes the test, but is obviously a no-go patch for the project. We do want inlining for performance... Seems to me that the mocking strategy will need to be reevaluated. – user7610 Jun 22 '19 at 10:57

1 Answers1

0

For now, it is resolved by adding __attribute__((noinline)) to bar (commit), and linking with -Wl,-flat_namespace (commit). The -export_dynamic was actually not needed.

I am still looking for alternative solutions.

user7610
  • 25,267
  • 15
  • 124
  • 150