Hi,

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

According to ld(1), `-z defs' is:

>       -z keyword
>           The recognized keywords are:
>
>           defs
>               Disallows undefined symbols in object files.  Undefined symbols
>               in shared libraries are still allowed.
>
>          ...

So, as in my case I am building a shared libraries (`-shared'),
according to the documentation, undefined symbols shouldn't be an error
when using `-z defs'.

Looking at ld code, it seems there are no check for "Undefined symbols
in shared libraries are still allowed". So I added this check: if it was
request to ignore unresolved symbols in a shared library, and we are in
a shared library, skip the error.

The check mainly copies the one just before for unresolved symbols in
objects.

If the patch corrects my problem, I don't known enough ld(1) to be sure
it doesn't break something else.

Comments ? OK ?
-- 
Sebastien Marie


Index: elf-bfd.h
===================================================================
RCS file: /cvs/src/gnu/usr.bin/binutils-2.17/bfd/elf-bfd.h,v
retrieving revision 1.4
diff -u -p -r1.4 elf-bfd.h
--- elf-bfd.h   25 Aug 2015 02:24:49 -0000      1.4
+++ elf-bfd.h   19 Sep 2015 16:41:17 -0000
@@ -1939,6 +1939,10 @@ extern bfd_boolean _sh_elf_set_mach_from
       else if (info->unresolved_syms_in_objects == RM_IGNORE           \
               && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)          \
        ;                                                               \
+      else if (info->unresolved_syms_in_shared_libs == RM_IGNORE       \
+              && info->shared                                          \
+              && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)          \
+        ;                                                              \
       else                                                             \
        {                                                               \
          bfd_boolean err;                                              \

Reply via email to