Missing /usr/lib/cxa_finalize.o on Solaris 11.1

Hi, all!

I'm trying to compile a C++ program on Solaris 11.1 with the lastest
LLVM and Clang
and get a failure:
../build/Debug+Asserts/bin/clang++ 1.cpp
ld: fatal: file /usr/lib/cxa_finalize.o: open failed: No such file or directory
ld: fatal: file processing errors. No output written to a.out
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the program:
int main()
{
   return 0;
}

LLVM and Clang are takes from SVN as described here -
http://clang.llvm.org/get_started.html
LLVM revision is r183143, Clang - r183144.

Solaris info:
$ cat /etc/release
                             Oracle Solaris 11.1 X86
  Copyright (c) 1983, 2012, Oracle and/or its affiliates. All rights reserved.
                           Assembled 19 September 2012

Linker command:
$ LD_ALTEXEC=/bin/echo ../build/Debug+Asserts/bin/clang++ 1.cpp
-C -e _start -Bdynamic --dynamic-linker /usr/lib/ld.so.1 -o a.out
/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/values-Xa.o
/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtbegin.o
/usr/lib/cxa_finalize.o
-L/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/ /tmp/1-P9aWqd.o
-lstdc++ -lgcc_s -lgcc -lc -lm
/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtend.o
/usr/lib/crtn.o

clang (not clang++) - OK
g++ - OK

"pkg search -r cxa_finalize.o" shows nothing

There is no in source and build trees:
find ../llvm/ ../build/ -name cxa_finalize\*

cxa_finalize.o is mentioned only once in LLVM+Clang sources:
$ ggrep -rnH cxa_finalize.o ../llvm/
../llvm/tools/clang/lib/Driver/Tools.cpp:4958:
CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));

When googling for cxa_finalize.o I always find it in context of glibc :slight_smile:

Removing cxa_finalize.o from linker args results in a valid executable:
$ ../build/Debug+Asserts/bin/clang++ -c 1.cpp
$ ld -C -e _start -Bdynamic --dynamic-linker /usr/lib/ld.so.1 -o a.out
/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/values-Xa.o
/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtbegin.o
-L/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/ 1.o -lstdc++ -lgcc_s
-lgcc -lc -lm /usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtend.o
/usr/lib/crtn.o
$ ./a.out ; echo $?
0

Hi Igor,

I'm trying to compile a C++ program on Solaris 11.1 with the lastest
LLVM and Clang
and get a failure:
../build/Debug+Asserts/bin/clang++ 1.cpp
ld: fatal: file /usr/lib/cxa_finalize.o: open failed: No such file or directory
ld: fatal: file processing errors. No output written to a.out
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You can get it from Pathscale's libcxxrt: https://github.com/pathscale/libcxxrt
Just compile this single object with clang and put to /usr/lib

Removing cxa_finalize.o from linker args results in a valid executable:
$ ../build/Debug+Asserts/bin/clang++ -c 1.cpp
$ ld -C -e _start -Bdynamic --dynamic-linker /usr/lib/ld.so.1 -o a.out
/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/values-Xa.o
/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtbegin.o
-L/usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/ 1.o -lstdc++ -lgcc_s
-lgcc -lc -lm /usr/gcc/4.5/lib/gcc/i386-pc-solaris2.11/4.5.2/crtend.o
/usr/lib/crtn.o
$ ./a.out ; echo $?
0

AFAIR this file is needed to call destructors of C++ global/static objects.
You can also try to use atexit() for this by giving '-fno-use-cxa-atexit' to clang.

Bye!

Ok, I've managed to build cxa_finalize.o in these steps:
gcc -std=c99 -c cxa_atexit.c
gcc -std=c99 -c cxa_finalize.c
ld -r cxa_finalize.o cxa_atexit.o -o 1.o
sudo cp 1.o /usr/lib/cxa_finalize.o

And /usr/lib/cxa_finalize.o looks like a random name :slight_smile: [1]
cxa_finalize.c and cxa_atexit.c are not compiled into libcxxrt.

[1] http://llvm.org/viewvc/llvm-project?view=revision&revision=151648

Send a patch or a pull request and I'll get it merged after review - Thanks for catching this

It looks quite trivial, but I want to test it on Solaris:

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0b44852..ba3b906 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -8,6 +8,8 @@ set(CXXRT_SOURCES
     memory.cc
     auxhelper.cc
     libelftc_dem_gnu3.c
+ cxa_atexit.c
+ cxa_finalize.c
    )

On linux this is provided by glibc - At the very least we'd need to wrap it in an OS check. Please test and report back

That's correct. They need to be built differently on Solaris to other operating systems. See the libcxxrt install guide: the compilation options depend entirely on how you want to install it. On Solaris, you need to link cxa_finalize with every library, because it refers to the DSO handle for each library to run cleanups.

David

FYI, https://www.illumos.org/issues/3849

Feature #3849: implement __cxa_atexit/__cxa_finalize