linking clang address sanitizer on FreeBSD 10.1 Release - c

I'm trying to get address sanitizer working on FreeBSD 10.1 Release, but whenever I try to compile a program with -fsanitize=address I get undefined references to _asan_stack_malloc_1 etc, etc. I found
https://forums.freebsd.org/threads/gcc-clang-address-sanitizer.47985/ on google but the suggestion of adding -L/usr/local/lib -I/usr/local/include didn't resolve the linking issue. I tried the llvm binaries for FreeBSD but when I go to compile with that clang I get /usr/bin/../lib/clang/3.6.0/lib/freebsd/libclang_rt.asan-x86_64.a , no such file or directory. . Either way I'm not sure what library I need to link or where it is.
Below is the program I tried compiling and here is the command I used,
clang -fsanitize=address san.c
#include <stdio.h>
int main(void)
{
return 0;
}

As an alternative to building LLVM, as suggested in this answer on Unix SE, you can install llvm37 from ports, which supports AddressSanitizer, and build with that:
# pkg install llvm37
$ clang37 -fsanitize=address san.c

To use asan on FreeBSD you can build llvm with asan support as shown below or you can install from packages/ports like in Kevinoid's answer.
Step one, grab the latest stable llvm source.
fetch http://llvm.org/releases/3.9.0/llvm-3.9.0.src.tar.xz
Now uncompress the llvm source directory.
tar -xvf llvm-3.9.0.src.tar.xz
Next change directory to llvm and grab the clang source files.
cd llvm-3.9.0.src/tools && fetch http://llvm.org/releases/3.9.0/cfe-3.9.0.src.tar.xz
Uncompress clang.
tar -xvf cfe-3.9.0.src.tar.xz
Enter the projects directory and grab compiler-rt.
cd ../projects && fetch http://llvm.org/releases/3.9.0/compiler-rt-3.9.0.src.tar.xz
Uncompress compiler-rt.
tar -xvf compiler-rt-3.9.0.src.tar.xz
Goto the root llvm directory and make a build directory for cmake.
cd ../ && mkdir build && cd build
Use cmake to setup the llvm build.
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON [-DLLVM_ENABLE_WERROR=ON] [-DLLVM_TARGETS_TO_BUILD=X86] -DBUILD_SHARED_LIBS=ON ../
Build llvm and go grab some tea, it will take a bit.
make -j12
If the build worked you should be left with clang with asan support. Make sure to remove the old compiler, /usr/bin/clang, /usr/bin/clang++ and /usr/bin/cc.
Then install the new clang by doing sudo make install. Finally you will probably want to link /usr/bin/cc to /usr/local/bin/clang by running sudo ln /usr/local/bin/clang /usr/bin/cc.
After doing all these steps you should be able to compile code with the -fsanitize=address compile option.

Related

How to modify CMakeList.txt: library found, but there are linking errors

I am creating a C library which is to be built with cmake, using Mac OS for development. In the CMakeList.txt, I have the following
#htslib
find_package(htslib REQUIRED)
include_directories(${HTSLIB_INCLUDE_DIR})
target_link_libraries(projectname ${HTSlib_LIBRARIES})
which outputs upon cmake ..
Found hstlib
However, upon make, I'm getting linker errors:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So...it can find the library, and the library is definitely installed with sudo make install, but there are linking errors only with this library.
(1) I'm guessing that find_package(htslib REQUIRED) is finding something else. How do I find out what?
(2) How do I explicitly write in CMakeList.txt to find the library which I know has been installed correctly?
Use VERBOSE=1 make to see the linker output. Search for -lhtslib
Read the documentation for the specific Find<LIB>.cmake.
Your specific questions:
"How do I find what CMake found": Use cmake-gui or ccmake. They both show the same info, but one is a GUI and the other is a Curses interface. In the advanced mode ("t" on ccmake) you will find all the variables for the searched packages. Additionally, you may use MESSAGE(STATUS "Found htslib at: ${htslib_LIBRARIES}").
"How to explicitly write in CMakeLists.txt where the library is?" Please, do not do that! CMake is meant for abstracting exactly this kind of information away. You have two options, first the good one: configure cmake on the command line (or in the GUIs mentioned above) to get a CMAKE_MODULES_PATH or a more specific hint to the library -D htslib_PATH=/usr/local/.../ (pointing to the dir where libhts.dylib resides). The worse solution would be to provide a HINT to find_package. find_package(htslib REQUIRED PATH /usr/local/lib) or find_package(htslib REQUIRED HINT /usr/local/lib /some/second/path/where/it/may/be).
Solution
Your linked project has a custom FindHTSlib.cmake link. This one uses pkg_config to configure the library. To replicate your problem, I used brew to install htslib. The pkg-config file can be found (for me, but brew info htslib tells you) under /usr/local/Cellar/htslib/1.8/lib/htslib.pc. So, let's give CMake the required hint.
I couldn't test this, because for me it found the htslib package directly with no further hints.
git clone https://github.com/D-Lo/bamdb # I am using version f5f03d0
mkdir -p bamdb/build; cd bamdb/build
brew install ck htslib lmdb
cmake .. # -G Ninja recommended, but needs brew install ninja
make # lot's of missing symbols
I would recommend to change in CMakeLists.txt the minimum required version of CMake from 2.8 to 3.10 (or at least 3.6).
This is the error I get:
[ 62%] Linking C shared library libbamdb.dylib
/usr/local/Cellar/cmake/3.11.1/bin/cmake -E cmake_link_script CMakeFiles/libbamdb.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/cc -Wall -g -std=gnu99 -fPIC -dynamiclib -Wl,-headerpad_max_install_names -o libbamdb.dylib -install_name #rpath/libbamdb.dylib CMakeFiles/libbamdb.dir/src/bam_api.c.o CMakeFiles/libbamdb.dir/src/bam_lmdb.c.o CMakeFiles/libbamdb.dir/src/bamdb.c.o
Undefined symbols for architecture x86_64:
"_bam_destroy1", referenced from:
_get_bam_row in bam_api.c.o
_deserialize_func in bam_lmdb.c.o
This can be fixed by adding the following line in the CMakeLists.txt, after the line add_library(libbamdb ${SOURCES}):
target_link_libraries(libbamdb ${LIBS})
Some further notes: you now have a library with a main function. This is because ${SOURCES} is used to build the executable and the library. That can have unexpected side effects. Unless it's needed, don't do it.

error when compiling testfiles from installed c-algorithms library

I'm trying to install and test c library c-algorithms from Github.
https://github.com/fragglet/c-algorithms/blob/master/test/test-queue.c
When I try to test the installation from the generated test folder with:
gcc -o test-arraylist `pkg-config --cflags --libs libcalg-1.0` test-arraylist.c
I get the following error massage:
test-arraylist.c:30:23: fatal error: arraylist.h: No such file or directory
compilation terminated.
I use a Vagrant box: ubuntu/xenial32 with Ubuntu 14.04.5 LTS
Prior to installation of c-algorithms:
sudo apt-get install autoconf
sudo apt-get install libtool
sudo apt-get install pkg-config
To install the library I have done following:
sudo ./autogen.sh
sudo ./configure
sudo make
sudo make install
Any help would be highly apriciated
The test-arraylist.c has line #include "arraylist.h" but it is under the libcalg subdirectory not directly in the include path.
libcalg subdir should be added to the include path or you have to modify the include like #include "libcalg/arraylist.h"
If you want only run the tests, then run the
sudo make check from the build root (in your case it is the source root)
This is probably going to be stomped on by process-fetishizers.
But.
When you build in a Unix/Linux operating system (and derivatives like RTEMS), you are building off other people's libraries - so you need those libraries and their header files ( just like c-alg... ) installed in locations that your compiler can find.
To find a file that is associated with a package, use dpkg as explained here:
https://askubuntu.com/questions/481/how-do-i-find-the-package-that-provides-a-file
But you have another problem you might not be aware of. You are trying to compile a test program using a gcc command when the software uses GNU autoconf automake and probably libtool to function PROPERLY.
Perhaps you don't understand you need to make sure autoconf, automake, and then libtool find the right configuration from one directory system to another. Fedora puts files in differing spots from Ubuntu distros.
Instead run:
autoreconf -fvi
first in the top level directory and see if this finds your header file.
THEN you run
./configure
and then
make test/check
(whichever it uses, some use recipe "all-tests", etc.)
make all
This would make all if your system is ready to handle them.

C: GTK+ 3.0 (3.20) - Cross-Compile from GNU/Linux (Arch Linux) to Windows

I need to cross-compile GTK+ application from GNU/Linux (Arch Linux) to Windows. I have already tried to use mingw32, but it does not see all libraries (including c's standard library!). So, I need to: find the standard libary; find all other (glib, gio, gtk, etc) libraries. But there's a problemm - I cannot find them. I also cannot compile from Windows. What must I do?
You can find the binaries for gtk 3 for windows on source forge or on http://win32builder.gnome.org/.
Once you have extracted them, you can follow this tutorial steps:
1) Install the compilation toolchain
Install the GCC compiler for Windows (namely MinGW, Arch Linux doc of package):
#pacman -S migw-w64
Download the latest all-in-one bundle ZIP archive directly from the official website (here's a direct link)
Adapt GTK+ to its location : In a terminal, move to the "gtk3-win32" folder you just created. For example: $cd /opt/gtk3-win32
then do :
find -name '*.pc' | while read pc; do sed -e "s#^prefix=.*#prefix=$PWD#" -i "$pc"; done
2) Compile
We will tell pkg-config to locate GTK+3 libraries in our custom path. If you extracted to /opt/gtk3-win32:
export PKG_CONFIG_PATH=/opt/gtk3-win32/lib/pkgconfig
We are ready to compile a sample C source ! Let's use a command in this style :
i586-mingw32msvc-gcc source.c -o executable.exe `pkg-config --cflags --libs gtk+-3.0
A new executable should have been created if everything went well.
It won't run on our Linux system, because it's targeting Windows !
3) Release
Create a folder containing the binary and the Windows .dll files. If you extracted to /opt/gtk3-win32:
mkdir ~/distri
cp executable.exe ~/distri
cp /opt/gtk3-win32/bin/*.dll ~/distri
Here we go ! Transfer this folder to a Windows box. Double-click on the executable and...
The following instructions are for gcc 5.3.0 (thread model: posix) and gtk+ 3.20.4
Install msys2 on windows
Install gtk3 files and copy the files
After installation, in msys2 shell
pacman -S mingw-w64-i686-gtk3
cd /mingw32
tar cfz c:/temp/mingw32.tar.gz
Ensure matching gcc version on Arch Linux
For gcc 5.3.0, edit /etc/pacman.conf
[core]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
[extra]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
[community]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
In case of upgrade
pacman -Syu
In case of downgrade
pacman -Syyuu
Later Arch Linux version seem to have gcc 6.1.1 that might be incompatible with msys2 gcc. A virtual machine might be a good idea for an Arch Linux installation that will not be upgraded (i.e. no security updates).
Copy files to linux
Unpack mingw32.tar.gz on linux, for example /opt/mingw32
Modify pkg-config files
perl -pi.bak -e 's,^prefix=.*,prefix=/opt/mingw32,' /opt/mingw32/lib/pkgconfig/*.pc
Set PKG_CONFIG_PATH
For example, before executing a configure script
export PKG_CONFIG_PATH=/opt/mingw32/lib/pkgconfig

compiling Vim 7.4 under AIX 6.1

I have a problem while compiling Vim 7.4 under AIX 6.1.
My options for the configure script are: "--prefix /opt/freeware/bin" and "--enable-pythoninterp".
There where no Errors while running the configure Script but when I try to run "make" I get the error message:
cd src && make first
cc -qlanglvl=extc89 -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_ATHENA -DFUNCPROTO=15 -g -o objects/regexp.o regexp.c "regexp_nfa.c"
line 4410.1: 1506-046 (S) Syntax error.
make: 1254-004 > The error code from the last command is
1.
Stop. make: 1254-004 The error code from the last command is 2.
Stop.
Does anyone know what to do?
I had compiled Vim 7.4 in my home directory so I know that there is a workaround but I can't find it anymore.
AIX's built in make (based on standard AT&T make) is not compatible with the Makefiles built by autoconf tools. Use GNU make (gmake) instead. You may already have it installed (check /opt/freeware/bin), install from the Linux Toolbox for AIX set (from IBM), or from one of the websites providing prebuilt GNU tools for AIX systems (perzl, bullfreeware, etc). Just provide an alias from make to gmake, or override the use of make in the Makefile itself.

Building/Linking libgcrypt for Mingw

I'd like to implement some features of libgcrypt in my program, but it is currently running on Windows, OSX, and Linux (Arch/Xubuntu), so I can only really do so if I can build it for all three platforms. On OSX and Linux I had no problem.
I got the sources from the github page for libgcrypt and libgpg-error, and I've successfully built and run the libraries on both Linux and OSX, so I know that my test code is valid (which I am now having trouble with on Windows w/MinGW).
I did the following on Xubuntu (and similar on Arch but using pacman instead of apt-get):
sudo apt-get install mingw32 mingw32-runtime mingw32-binutils
to get the cross compiling tool chain and
git clone https://github.com/Chronic-Dev/libgcrypt.git
git clone https://github.com/Chronic-Dev/libgpg-error.git
cd libgpg-error
autoreconf -vfi
./autogen.sh --build-w32
make
sudo make install
cd ../libgcrypt
autoreconf -vfi
./autogen.sh --build-w32
make
sudo make install
to build, and it successfully builds these files in home/myuser/w32root/:
libgcrypt.a
libgcrypt.def
libgcrypt.dll.a
libgcrypt.la
libgpg-error.dll.a
libgpg-error.la
include/
gcrypt.h
gcrypt-module.h
gpg-error.h
I took these files over to windows, and tried compiling the test code (named main.c locally) with
gcc main.c -o main.exe -lgcrypt
but I get undefined reference errors leading me to the conclusion that the library wasn't linked correctly (initially only using libgcrypt.a), so I looked some stuff up, and found that some libraries require a set of files like .a, .def, et al. to work, so I dropped them all in C:\Mingw\lib to see if it made a difference; it didn't. The following was also silent in finding the library file to link, but didn't resolve the undefined references:
gcc main.c -o main.exe -lgcrypt -lgpg-error
So I'm not really sure where to go from here. The readme doesn't get into cross compiling too much, like what files to copy and link once you're on the Windows side. Any pointers (to docs for it I missed maybe?) are appreciated! Thanks a bunch for reading my wall of text.

Resources