6

The question essentially is how to compile a program that uses AddressSanitizer in FreeBSD.

I use FreeBSD 10.0 (64-bit). I am trying to compile the following code with -fsanitize=address flag:

int main() {
  return 0;
}

When I compile with clang as clang -fsanitize=address file.c, I get the following error message:

/tmp/t-tEh7oh.o: In function `main`:
file.c:(.text+0xc4): undefined reference to `__asan_report_store4`
/tmp/t-tEh7oh.o: In function `asan.module_ctor`:
file.c:(.text+0x119): undefined reference to `__asan_init_v3`
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I compile with gcc48 as gcc48 -fsanitize=address file.c, I get the following error message:

/usr/local/bin/ld: cannot find -lasan
collect2: error: ld returned 1 exit status

In my GNU/Linux system I have libasan.* files somewhere in a file system. But I am unable to find ones in FreeBSD - neither in /usr/ports, nor via pkg.

How should I better make that working?

Upd. Responding to a comment, I added output of gcc48 -v (more precisely, output of gcc48 -fsanitize=address file.c -v):

Using built-in specs.
COLLECT_GCC=gcc48
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/lto-wrapper
Target: x86_64-portbld-freebsd10.0
Configured with: ./../gcc-4.8.3/configure --disable-bootstrap --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc48 --libexecdir=/usr/local/libexec/gcc48 --program-suffix=48 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gcc48/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc48 --build=x86_64-portbld-freebsd10.0
Thread model: posix
gcc version 4.8.3 (FreeBSD Ports Collection) 
COLLECT_GCC_OPTIONS='-fsanitize=address' '-v' '-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/cc1 -quiet -v file.c -quiet -dumpbase file.c -mtune=generic -march=x86-64 -auxbase t -version -fsanitize=address -o /tmp//ccjvAeJG.s
GNU C (FreeBSD Ports Collection) version 4.8.3 (x86_64-portbld-freebsd10.0)
    compiled by GNU C version 4.2.1 Compatible FreeBSD Clang 3.3 (tags/RELEASE_33/final 183502), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128184
ignoring nonexistent directory "/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../../../../x86_64-portbld-freebsd10.0/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/include
 /usr/local/include
 /usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/include-fixed
 /usr/include
End of search list.
GNU C (FreeBSD Ports Collection) version 4.8.3 (x86_64-portbld-freebsd10.0)
    compiled by GNU C version 4.2.1 Compatible FreeBSD Clang 3.3 (tags/RELEASE_33/final 183502), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128184
Compiler executable checksum: 6bc4873e12afab16db5dd7bf12b3232f
COLLECT_GCC_OPTIONS='-fsanitize=address' '-v' '-mtune=generic' '-march=x86-64'
 /usr/local/bin/as -v -o /tmp//ccBydWt7.o /tmp//ccjvAeJG.s
GNU assembler version 2.24 (x86_64-portbld-freebsd10.0) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=/usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/:/usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/:/usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/:/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/:/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/:/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../../../../x86_64-portbld-freebsd10.0/bin/
LIBRARY_PATH=/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/:/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../../../../x86_64-portbld-freebsd10.0/lib/:/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-fsanitize=address' '-v' '-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/collect2 --eh-frame-hdr -V -dynamic-linker /libexec/ld-elf.so.1 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/crtbegin.o -L/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3 -L/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../../../../x86_64-portbld-freebsd10.0/lib -L/usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/../../.. /tmp//ccBydWt7.o -lasan -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/local/lib/gcc48/gcc/x86_64-portbld-freebsd10.0/4.8.3/crtend.o /usr/lib/crtn.o
GNU ld (GNU Binutils) 2.24
  Supported emulations:
   elf_x86_64_fbsd
   elf_i386_fbsd
   elf_x86_64
   elf_i386
   elf_l1om
   elf_l1om_fbsd
   elf_k1om
   elf_k1om_fbsd/usr/local/bin/ld: cannot find -lasan
collect2: error: ld returned 1 exit status
  • What is the output of gcc -v? – eyoung100 Sep 24 '14 at 16:10
  • @eyoung100, please find that in the updated version of the question. – Alexander Pozdneev Sep 24 '14 at 17:33
  • 1
    Don't hate the messenger, but I needed to make sure you were running 4.8. I'm not experienced with building a custom port, but here is what you need to do: Download the Patch File Here, then manually rebuild the Port to include the custom patch. On gentoo, we do this by using a local portage tree. Surely there is a BSD equivalent. – eyoung100 Sep 24 '14 at 17:44
  • @eyoung100 Indeed there is. Place the patch in the port's ./files subdirectory, and ensure its name begins with patch-. The ports system will apply the patch before it goes on to configure and build the port. – D_Bye Sep 25 '14 at 8:48
  • @D_Bye When you manually add a patch, it might be deleted when you update the ports tree. – Roland Smith Mar 30 '16 at 9:21
1

Try using llvm37 from ports. It builds the sanitizers by default on AMD64.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.