On Sat, 19 Sep 2015 19:29:56 +0200, Sebastien Marie wrote:
> On Sat, Sep 19, 2015 at 10:07:04AM -0700, Philip Guenther wrote:
> > On Sat, Sep 19, 2015 at 9:50 AM, Sebastien Marie <[email protected]>
> > wrote:
> > > While working on building llvm 3.7.0 on openbsd (-current amd64 and
> > > i386), I encounter a problem when linking a shared library, while
> > > -Wl,-z,defs was passed on command-line.
> > >
> > > I build a minimal reproductible testcase:
> > >
> > > $ touch test.c
> > > $ c++ -Wl,-z,defs -shared -o libtest.so test.c
> > > /usr/lib/crtbeginS.o: In function `atexit':
> > > (.text+0x18f): undefined reference to `__cxa_atexit'
> > > collect2: ld returned 1 exit status
> >
> > If you want to use -zdefs when building a shared library, you MUST
> > include in the link the libraries that it depends on, including libc.
> >
>
> First, I encountered this problem while building clang. The linking of
> libclang.so.3.7 use -Wl,-z,defs.
FWIW, the comment in the cmake fragment says (HandleLLVMOptions.cmake):
[...]
# Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise
a DSO
# build might work on ELF but fail on MachO/COFF.
[...]
and it then goes on to disable the option on FreeBSD, Darwin etc.
I think it's safe to say the option should also be disabled on OpenBSD,
so my current patch looks like this:
$OpenBSD$
--- cmake/modules/HandleLLVMOptions.cmake.orig Wed Sep 9 14:34:05 2015
+++ cmake/modules/HandleLLVMOptions.cmake Wed Sep 9 14:34:55 2015
@@ -132,7 +132,8 @@ endif()
# Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
# build might work on ELF but fail on MachO/COFF.
if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32 OR CYGWIN OR
- ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") AND
+ ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
+ ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") AND
NOT LLVM_USE_SANITIZER)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
endif()
> For building with it, it would require -lc for several symbols, and the
> last symbols will be `environ'. If I add `crt0.o' for `environ', it will
> require `__init' and `main'.
>
> I am not sure that I can provide them for linking libclang.so ?
>
> > I believe the current behavior is correct and matches other systems.
>
> I tested under Linux (debian wheezy) the testcase written before, and I
> don't have any error like under OpenBSD.
That might depend on the specifics of gcc specs on Linux; I have no idea
why it works.
> $ uname -a
> Linux srvlinux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3
> (2015-08-04) x86_64 GNU/Linux
> $ touch test.c
> $ c++ -Wl,-z,defs -shared -o libtest.so test.c
> $ ls libtest.so
> libtest.so
> $
>
> Thanks.
> --
> Sebastien Marie
>
>