steleman / address-sanitizer

Automatically exported from code.google.com/p/address-sanitizer
0 stars 0 forks source link

link error when LDFLAGS has -Wl,--no-undefined #380

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
The following is libtest.cpp
    #include <stdio.h>
    void print(int *pBuf, int size) {
        for (int i = 0; i < size; ++i)
        {
            printf("%d ", pBuf[i]);
        }
        printf("\n");
    }
2.Run the following build.sh
#!/bin/sh
set -x
#PASS
clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so
#PASS
clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address
#FAIL
clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address 
-Wl,--no-undefined

#PASS
g++-4.9 -shared -fPIC libtest.cpp -o libtest.so
#PASS
g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address
#FAIL
g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address 
-Wl,--no-undefined

What is the expected output? What do you see instead?
If the two link flags are set, "-fsanitize=address -Wl,--no-undefined", the 
linker will fail always. I hope it should work with both.

+ clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so
+ clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address
+ clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address 
-Wl,--no-undefined
/tmp/libtest-ecd182.o: In function `print(int*, int)':
libtest.cpp:(.text+0x16): undefined reference to 
`__asan_option_detect_stack_use_after_return'
libtest.cpp:(.text+0x55): undefined reference to `__asan_stack_malloc_1'
libtest.cpp:(.text+0x133): undefined reference to `__asan_report_store8'
libtest.cpp:(.text+0x18b): undefined reference to `__asan_report_store4'
libtest.cpp:(.text+0x1e1): undefined reference to `__asan_report_store4'
libtest.cpp:(.text+0x231): undefined reference to `__asan_report_load4'
libtest.cpp:(.text+0x284): undefined reference to `__asan_report_load4'
libtest.cpp:(.text+0x2da): undefined reference to `__asan_report_load4'
libtest.cpp:(.text+0x307): undefined reference to `__asan_report_load8'
libtest.cpp:(.text+0x363): undefined reference to `__asan_report_load4'
libtest.cpp:(.text+0x3c1): undefined reference to `__asan_report_load4'
/tmp/libtest-ecd182.o: In function `asan.module_ctor':
libtest.cpp:(.text+0x455): undefined reference to `__asan_init_v5'
libtest.cpp:(.text+0x468): undefined reference to `__asan_register_globals'
/tmp/libtest-ecd182.o: In function `asan.module_dtor':
libtest.cpp:(.text+0x483): undefined reference to `__asan_unregister_globals'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
+ g++-4.9 -shared -fPIC libtest.cpp -o libtest.so
+ g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address
+ g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address 
-Wl,--no-undefined
/tmp/ccRlkbkY.o: In function `print(int*, int)':
libtest.cpp:(.text+0x60): undefined reference to `__asan_report_load4'
/tmp/ccRlkbkY.o: In function `_GLOBAL__sub_D_00099_0_libtest.cpp':
libtest.cpp:(.text+0xa2): undefined reference to `__asan_unregister_globals'
/tmp/ccRlkbkY.o: In function `_GLOBAL__sub_I_00099_1_libtest.cpp':
libtest.cpp:(.text+0xad): undefined reference to `__asan_init_v3'
libtest.cpp:(.text+0xbe): undefined reference to `__asan_register_globals'
collect2: error: ld returned 1 exit status

What version of the product are you using? On what operating system?
Ubuntu 12.04 X86-64 LTS, clang 3.6(http://llvm.org/apt/) / gcc 
4.9.2(https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test?field.serie
s_filter=precise)

Please provide any additional information below.

Original issue reported on code.google.com by xiaoyur...@gmail.com on 9 Mar 2015 at 7:44

Attachments:

GoogleCodeExporter commented 9 years ago
Update the tests with
#PASS
clang++-3.6 -shared -fPIC libtest.cpp -o libtest.so -Wl,--no-undefined
#PASS
g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -Wl,--no-undefined

And I find for my built toolchain mipsel-linux-g++, everything works fine with 
these two flags.

Original comment by xiaoyur...@gmail.com on 9 Mar 2015 at 9:32

Attachments:

GoogleCodeExporter commented 9 years ago
There are a few other linker flags that will not work with -fsanitize=* (e.g. 
-z,defs).
--no-undefined is not any kind of special here and I think that the right 
solution is 
just not to use it. 

Other ideas? 

Original comment by konstant...@gmail.com on 9 Mar 2015 at 5:23

GoogleCodeExporter commented 9 years ago
For ubuntu GCC, it works when 
"g++-4.9 -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address -lasan". 
And for my build toolchain(mips gcc 4.9), it works when 
"mipsel-linux-g++ -shared -fPIC libtest.cpp -o libtest.so -fsanitize=address 
-fstack-protector".
"-lasan" flag isn't needed at all.
I guess maybe the asan build for ubuntu-toolchain-r doesn't add "-lasan" for 
"-fsanitize=address" by default.

For clang, maybe the problem is that the clang only compile asan static 
libraries, like
/usr/lib/llvm-3.6/lib/clang/3.6.0/lib/linux/libclang_rt.asan_cxx-x86_64.a
/usr/lib/llvm-3.6/lib/clang/3.6.0/lib/linux/libclang_rt.asan-x86_64.a
As https://code.google.com/p/address-sanitizer/wiki/AsanAsDso says, "Until 
recently, ASAN runtime in Clang on Linux was offered only in the form of static 
library (e.g. libclang_rt.asan-x86_64.a). This has recently been changed and 
you can now ask for shared runtime (aka ASAN-DSO) by cmaking with 
-DCOMPILER_RT_BUILD_SHARED_ASAN=ON (and then compiling your code with 
-fsanitize=address -shared-libasan)."

I find the link flag "-Wl,--no-undefined" which used by Android is nice, which 
can avoid libraries developers forgeting to write the dependent libraries of 
the library in the Makefile or CMakeLists.txt. So, I hope there is a way to use 
both flags.

My solution is the following:
(1)For GCC, I use "-Wl,--no-undefined" and "-fsanitize=address" both, if the 
linker fails, add "-lasan" for linker flag.
(2)For Clang Ubuntu, I use "-Wl,--no-undefined" by default. If 
"-fsanitize=address" is set, turn off the flag "-Wl,--no-undefined".
(3)For Android NDK, since both static and shared libraries are built for asan, 
both flags work fine, which I have approved for NDK r10d clang3.5.

Original comment by xiaoyur...@gmail.com on 10 Mar 2015 at 2:01

GoogleCodeExporter commented 9 years ago
> There are a few other linker flags that will not work
> with -fsanitize=* (e.g. -z,defs).

Kostya, do you have a complete list? We should probably add this to FAQ and 
ideally verify incompatible flags in the frontend(s).

Original comment by tetra2...@gmail.com on 10 Mar 2015 at 4:40

GoogleCodeExporter commented 9 years ago
>> do you have a complete list?
No, z,defs is the only one we hit periodically. 
http://clang.llvm.org/docs/AddressSanitizer.html mentions it. 
(we have too much documentation, sadly)

Original comment by konstant...@gmail.com on 13 Mar 2015 at 10:07