[LLVMbugs] [Bug 21001] New: clang++ doesn't export operator delete with -flto

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Sep 19 07:48:58 PDT 2014


            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: unassignedclangbugs at nondot.org
          Reporter: tavianator at tavianator.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    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. 

$ 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;

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
$ 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

$ objdump -T main | c++filt | grep operator
0000000000400e40 g    DF .text  000000000000003a  Base        operator
new(unsigned long)
0000000000000000      DF *UND*  0000000000000000  Base        operator

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

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140919/5c65d4d2/attachment.html>

More information about the llvm-bugs mailing list