Clangs SanitizerCoverage with shared libraries - clang++

I am having trouble in using the sancovtool of clang with shared libraries.
While it works like documented for a simple binary I am not able to get it to work with shared libraries.
Whenever I use a shared library from my program I do get a *.sancov file for it but when I try to get a report from sancov I receive the following error message:
Ignoring libMyLib.so and its coverage because __sanitizer_cov* functions were not found.
It seems that the problem stems from the fact the the sanitizer functions are unresolved in my shared library. But I am not sure though.
nm libMyLib.so | grep saniti
U __sanitizer_cov
U __sanitizer_cov_module_init
U __sanitizer_cov_with_check
Can someone give me hint what I am doing wrong?

Related

Link to NAG library with -lnag

I'm trying to compile my first program which uses the NAG library, the following:
program naginfo
use nag_f77_a_chapter
implicit none
write(*,*) 'Calling NAG identification routine'
write(*,*)
call a00aaf
end program naginfo
This is copied from the tutorial and they suggest to compile it with the following statement:
f95 -o naginfo naginfo.f90 -lnag
and they suppose that this -lnag drives the linker to NAG library, but then I find this error:
Fatal Error: Can't open module file ‘nag_f77_a_chapter.mod’ for reading at (1): The directory does not exist
I've tried changing the directory of the NAG files to help the linker find it.
How do I get this to compile and link?
This is just a long explanation of francescalus's comment.
The flag -lnag only adds the library code to the already compiled program when linking all compiled pieces together. It has no effect during compilation and hence no effect on the error message you see.
The compiler must see the information about the NAG library modules. That is usually stored in module files with the .mod extension. Compilers normally only search for these in the current directory or in the system's include directories.
You can instruct the compiler to search in a different directory by using a special compiler flag. It may differ between different compilers, but is typically -I followed by the directory where the library stores its .mod files.
Be advised that the .mod files in the library are only compatible with the same compiler that was used to create them by the library vendor.

How to find which library exports a function?

For instance I have a program that reports undefined reference to 'XRenderFindVisualFormat'
Can I use something like a combination of find/grep/file ?
You can use nm (or nm -D) on a library do get the symbols defined or used there. See nm(1) and also objdump(1).
You can use ldd on some executable to understand which shared dynamic libraries it is linking. See ldd(1).
You can also search the web for XRenderFindVisualFormat
Remember that order of libraries and program options to gcc is significant.

Weak symbols, shared libraries and dlopen

I have a binary with a weak symbol that I want to be able to link at runtime with a run dependent shared library.
$nm testrun
...
w basic2.test
...
My first test was using a .o file at static linktime, that worked, but I need it to be shared.
So, my second test was getting a shared library with that symbol defined and link it at compile time with -lmy (libmy.so), and this, actually worked as well.
Third step tried not linking at compile time and use ld_preload trick and this did not work.
nm libmy.so
...
00000550 T basic2.test
...
I have really no idea why this particular one does not work, looks like dynamic loader should have enough information to set testruns weak symbol with the one in libmy.so.
My final objective, which I guess will require more work is to load at start a small function that does check for the appropiate symbol with dlsym and sets it there.
Any hint?
It seems that you may need to use LD_DYNAMIC_WEAK along with LD_PRELOAD from the man page:
LD_DYNAMIC_WEAK (glibc since 2.1.91) Allow weak symbols to be overridden (reverting to old glibc behavior). For security reasons, since glibc 2.3.4, LD_DYNAMIC_WEAK is ignored for set-user-ID/set-group-ID binaries.
Note: it could be a typo, but you should use -lmylib.so and not -Lmylib.so

Is there an automated way to figure out Shared Object Dependencies?

Short:
I'm looking for something that will list all unresolved dependencies in an SO, taking into account the SOs that are in it's dependencies.
Long:
I'm converting a lot of static-compiled code to Shared Objects in Linux- is there an easy way to determine what other SOs my recently compiled SO is dependent on besides trial & error while trying to load it?
I'm sure there is a better way, but I haven't been able to find it yet.
I've found "ldd", but that only lists what the SO says it's dependent on.
I've also used "nm" to figure out once an SO fails to load to verify what other SO contains it.
I don't have code for you, but I can give pointers:
It's just a graph problem. You should use objdump -T to dump the dynamic symbol table for a given binary or shared object. You'll see many lines of output, and the flags can be a little confusing, but the important part if that symbols will either be *UND* or they'll have a segment name (.text etc).
Any time you see *UND*, that means that it's an undefined symbol which has to be resolved. Defined symbols are the targets of resolution.
With that, and a little Python, you should be able to find what you need.
"ldd -r foo.so" should print the set of symbols which foo.so needs but which aren't provided by its direct dependencies.
Alternatively, link foo.so like this:
gcc -shared -o foo.so foo.o bar.o -ldep1 -ldep2 -Wl,--no-undefined
This should fail (to link) if foo.o or bar.o uses something not provided by libdep1 or libdep2 or libc.

Restricting symbols to local scope for linux executable

Can anyone please suggest some way we can restrict exporting of our symbols to global symbol table?
Thanks in advance
Hi,
Thanks for replying...
Actually I have an executable which is statically linked to a third party library say "ver1.a" and also uses a third party ".so" file which is again linked with same library but different version say "ver2.a". Problem is implementation of both these versions is different. At the beginning, when executable is loaded, symbols from "ver1.a" will get exported to global symbol table. Now whenever ".so" is loaded it will try to refer to symbols from ver2.a, it will end up referring to symbols from "ver1.a" which were previously loaded.Thus crashing our binary.
we thought of a solution that we wont be exporting the symbols for executable to Global symbol table, thus when ".so" gets loaded and will try to use symbols from ver2.a it wont find it in global symbol table and it will use its own symbols i.e symbols from ver2.a
I cant find any way by which i can restrict exporting of symbols to global symbol table. I tried with --version-script and retain-symbol-file, but it didn't work. For -fvisibility=hidden option, its giving an error that " -f option may only be used with -shared". So I guess, this too like "--version-script" works only for shared libraries not for executable binaries.
code is in c++, OS-Linux, gcc version-3.2. It may not be possible to recompile any of the third party libraries and ".so"s. So option of recompiling "so' file with bsymbolic flag is ruled out.
Any help would be appreciated.
Pull in the 3rd party library with dlopen.
You might be able to avoid that by creating your own shared lib that hides all the third party symbols and only exposes your own API to them, but if all else fails dlopen gives you complete control.
I had, what sounds like, a similar issue/question: Segfault on C++ Plugin Library with Duplicate Symbols
If you can rebuild the 3rd party library, you could try adding the linker flag -Bsymbolic (the flag to gcc/g++ would be -Wl,-Bsymbolic). That might solve your issue. It all depends on the organization of your code and stuff, as there are caveats to using it:
http://www.technovelty.org/code/c/bsymbolic.html
http://software.intel.com/en-us/articles/performance-tools-for-software-developers-bsymbolic-can-cause-dangerous-side-effects/
If you can't rebuild it, according to the first caveat link:
In fact, the only thing the -Bsymbolic
flag does when building a shared
library is add a flag in the dynamic
section of the binary called
DT_SYMBOLIC.
So maybe there's a way to add the DT_SYMBOLIC flag to the dynamic section post-linking?
The simplest solution is to rename the symbols (by changing source code) in your executable so they don't conflict with the shared library in the first place.
The next simplest thing is to localize the "problem" symbols with 'objcopy -L problem_symbol'.
Finally, if you don't link directly with the third party library (but dlopen it instead, as bmargulies suggests), and none of your other shared libraries use of define the "problem" symbol, and you don't link with -rdynamic or one of its equivalents, then the symbol should not be exported to the dynamic symbol table of the executable, and thus you shouldn't have a conflict.
Note: 'nm a.out' will still, show the symbol as globally defined, but that doesn't matter for dynamic linking. You want to look at the dynamic symbol table of a.out with 'nm -D a.out'.

Resources