Home > Blockchain >  Can a library detect/prevent linking in case of detected missmatched ABI?
Can a library detect/prevent linking in case of detected missmatched ABI?

Time:12-09

Motivation: Some libraries, e.g. Eigen have ABI that depends on compiler switches.

Question: could users of this library somehow encode this behavior so that if you try to link components compiled with different compiler switches you get a linker error.

So if it is not clear why I am asking about:

Alice the library developer builds her (not header only library) with no special compiler flag and exposes Eigen in her API.

Bob the library consumer compiles with AVX2 enabled, he also uses Eigen in his code. He uses Alice library.

Bob gets crashes at runtime.

Can Alice prevent her library linking in Bob's application? Assume that Alice knows to detect ABI mismatch using preprocessor macros that affect Eigen ABI.

note: I am fine with using C 20 or C 23 solutions if implemented in compilers.

CodePudding user response:

For avx2 at least it looks like you can know if the compiler is compiling for it or not via built in defines. This probably won't help though since it sounds like you just get a binary of Alice's library.

Ideally I think Bob would just compile Alices library himself and then link to it guaranteeing compatible abi. Barring this option I can think of one less than ideal way of checking, objdump the library and grep for avx2 instructions in the disassembly to see if they are being used.

CodePudding user response:

C knows nothing about compiler switches/options. They are compiler specific and not formally specified (usually). So there's no way to encode it portable and generic way. You just won't know what you need to encode. There are some options if you limit the scope to a particular platform/compiler/toolset/option. But, again, it is out of C scope and more about tools. For example gcc/clang allow recording of command line options (-grecord-gcc-switches). By forcing library suppliers to use that and checking the records in pre-link step you might achieve what you want. Theoretically atleast....

  • Related