http://llvm.org/bugs/show_bug.cgi?id=21001
Bug ID: 21001
Summary: clang++ doesn't export operator delete with -flto
Product: clang
Version: unspecified
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: C++
Assignee: [email protected]
Reporter: [email protected]
CC: [email protected], [email protected]
Classification: Unclassified
When using -flto, clang doesn't export a replacement operator delete, causing
shared libraries to call a different implementation than the main program.
Example:
$ cat main.cpp
#include <cstdlib>
#include <new>
#include <iostream>
void* operator new(std::size_t size) {
void* result = std::malloc(size);
if (result == nullptr) {
operator delete(result);
throw std::bad_alloc();
}
return result;
}
void operator delete(void* ptr) noexcept {
std::cout << "Deleting!" << std::endl;
std::free(ptr);
}
void deleteIt(int* ptr);
int main() {
deleteIt(new int);
return 0;
}
$ cat shared.cpp
void deleteIt(int* ptr) {
delete ptr;
}
$ clang++ -std=c++11 -g -O3 -flto -fuse-ld=gold -fPIC -shared shared.cpp -o
libshared.so
$ clang++ -std=c++11 -g -O3 -flto -fuse-ld=gold main.cpp -L. -lshared -o main
$ LD_LIBRARY_PATH=. ./main
$
(Note the missing "Deleting!" output.) objdump shows that the symbol isn't
exported:
$ objdump -T main | c++filt | grep operator
0000000000400e40 g DF .text 000000000000003a Base operator
new(unsigned long)
0000000000000000 DF *UND* 0000000000000000 Base operator
delete(void*)
The bug happens with C++98 mode too (with appropriate code changes), but the
C++14 operator delete(void*, std::size_t) works.
A workaround is to add __attribute__((used)) to operator delete. See
http://stackoverflow.com/q/25922895/502399.
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs