Bug 214258 - devel/openmp: spurious libm dependency
Summary: devel/openmp: spurious libm dependency
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: Jan Beich
URL:
Keywords: needs-patch, regression
Depends on:
Blocks: 210337
  Show dependency treegraph
 
Reported: 2016-11-06 02:07 UTC by Jan Beich
Modified: 2017-03-29 18:37 UTC (History)
5 users (show)

See Also:


Attachments
ports/devel/openmp/files/patch-link-libm.patch (1.36 KB, patch)
2017-01-25 10:43 UTC, Yuta Satoh
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Beich freebsd_committer 2016-11-06 02:07:23 UTC
LLVM openmp picks up math symbols despite not using them in any of its object files.

$ cd /usr/ports/devel/openmp
$ make install
$ cd $(make -V WRKSRC)

$ fgrep -lr scalbnl .
./runtime/src/libgomp.so
./runtime/src/libiomp5.so
./runtime/src/libomp.so
./runtime/exports/lin_32e/lib/libomp.so

$ echo 'int main() { }' | cc ./runtime/src/libomp.so -xc -
./runtime/src/libomp.so: undefined reference to `scalbnl'
./runtime/src/libomp.so: undefined reference to `fmaxl'
./runtime/src/libomp.so: undefined reference to `logbl'
./runtime/src/libomp.so: undefined reference to `scalbnf'
./runtime/src/libomp.so: undefined reference to `logb'
./runtime/src/libomp.so: undefined reference to `logbf'
./runtime/src/libomp.so: undefined reference to `scalbn'
cc: error: linker command failed with exit code 1 (use -v to see invocation)

$ echo 'int main() { }' | cc -fopenmp -L/usr/local/lib -xc -
/usr/local/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/lib/libomp.so: undefined reference to `logbl'
/usr/local/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/lib/libomp.so: undefined reference to `logb'
/usr/local/lib/libomp.so: undefined reference to `logbf'
/usr/local/lib/libomp.so: undefined reference to `scalbn'
cc: error: linker command failed with exit code 1 (use -v to see invocation)
Comment 1 Jan Beich freebsd_committer 2016-11-06 04:01:27 UTC
Assigning to toolchain@ in order to help bisecting base change. It affects 11.0+ but not 10.3 or 9.3.
Comment 2 Yuta Satoh 2017-01-25 10:43:58 UTC
Created attachment 179304 [details]
ports/devel/openmp/files/patch-link-libm.patch

Sample patch for devel/openmp-3.9.0.

The attached patch links libm.
-Wl,--no-as-needed is necessary for forced linking.


Before applying the patch)
# ldd /usr/local/lib/libomp.so
/usr/local/lib/libomp.so:
        libthr.so.3 => /lib/libthr.so.3 (0x801288000)
        libc.so.7 => /lib/libc.so.7 (0x800823000)

After applying the patch)
# ldd /usr/local/lib/libomp.so
/usr/local/lib/libomp.so:
        libm.so.5 => /lib/libm.so.5 (0x801288000)
        libthr.so.3 => /lib/libthr.so.3 (0x8014b3000)
        libc.so.7 => /lib/libc.so.7 (0x800823000)


$ echo 'int main() { }' | cc -fopenmp -L/usr/local/lib -xc - -v
FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0)
Target: x86_64-unknown-freebsd11.0
Thread model: posix
InstalledDir: /usr/bin
 "/usr/bin/cc" -cc1 -triple x86_64-unknown-freebsd11.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name - -mrelocation-model static -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/bin/../lib/clang/3.8.0 -fdebug-compilation-dir /usr/home/nigoro -ferror-limit 19 -fmessage-length 172 -fopenmp -fobjc-runtime=gnustep -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/--26d632.o -x c -
clang -cc1 version 3.8.0 based upon LLVM 3.8.0 default target x86_64-unknown-freebsd11.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../lib/clang/3.8.0/include
 /usr/include
End of search list.
 "/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o a.out /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/local/lib -L/usr/lib /tmp/--26d632.o -lomp -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
Comment 3 Jan Beich freebsd_committer 2017-03-15 09:32:27 UTC
To avoid maintenance burden it'd be nice if we fix base system regression before FreeBSD 11.1-RELEASE. devel/openmp isn't the only -lomp provider.

$ fetch https://computing.llnl.gov/tutorials/openMP/samples/C/omp_hello.c

$ pkg install llvm37 llvm38 llvm39 llvm40 llvm-devel

$ find -s /usr/local -name libomp.so
/usr/local/lib/libomp.so
/usr/local/llvm-devel/lib/libomp.so
/usr/local/llvm37/lib/libomp.so
/usr/local/llvm38/lib/libomp.so
/usr/local/llvm39/lib/libomp.so
/usr/local/llvm40/lib/libomp.so

$ clang37 -fopenmp omp_hello.c
/usr/bin/ld: cannot find -lomp
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang37 -fopenmp omp_hello.c $(llvm-config37 --ldflags)
/usr/local/llvm37/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/llvm37/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/llvm37/lib/libomp.so: undefined reference to `logbl'
/usr/local/llvm37/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/llvm37/lib/libomp.so: undefined reference to `logb'
/usr/local/llvm37/lib/libomp.so: undefined reference to `logbf'
/usr/local/llvm37/lib/libomp.so: undefined reference to `scalbn'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang38 -fopenmp omp_hello.c
/usr/local/llvm38/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/llvm38/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/llvm38/lib/libomp.so: undefined reference to `logbl'
/usr/local/llvm38/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/llvm38/lib/libomp.so: undefined reference to `logb'
/usr/local/llvm38/lib/libomp.so: undefined reference to `logbf'
/usr/local/llvm38/lib/libomp.so: undefined reference to `scalbn'
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang39 -fopenmp omp_hello.c
/usr/local/llvm39/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/llvm39/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/llvm39/lib/libomp.so: undefined reference to `logbl'
/usr/local/llvm39/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/llvm39/lib/libomp.so: undefined reference to `logb'
/usr/local/llvm39/lib/libomp.so: undefined reference to `logbf'
/usr/local/llvm39/lib/libomp.so: undefined reference to `scalbn'
clang-3.9: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang40 -fopenmp omp_hello.c
/usr/local/llvm40/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/llvm40/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/llvm40/lib/libomp.so: undefined reference to `logbl'
/usr/local/llvm40/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/llvm40/lib/libomp.so: undefined reference to `logb'
/usr/local/llvm40/lib/libomp.so: undefined reference to `logbf'
/usr/local/llvm40/lib/libomp.so: undefined reference to `scalbn'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang-devel -fopenmp omp_hello.c
/usr/bin/ld: cannot find -lomp
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang-devel -fopenmp omp_hello.c $(llvm-config-devel --ldflags)
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `scalbnl'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `fmaxl'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `logbl'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `scalbnf'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `logb'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `logbf'
/usr/local/llvm-devel/lib/libomp.so: undefined reference to `scalbn'
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
Comment 4 Jan Beich freebsd_committer 2017-03-15 09:36:36 UTC
Comment on attachment 179304 [details]
ports/devel/openmp/files/patch-link-libm.patch

The workaround isn't really correct. FreeBSD versions before 11.0 don't really need -lm.

$ clang40 -fopenmp omp_hello.c
$ ldd a.out
a.out:
        libomp.so => /usr/local/llvm40/lib/libomp.so (0x80081f000)
        libc.so.7 => /lib/libc.so.7 (0x800aa3000)
        libthr.so.3 => /lib/libthr.so.3 (0x800e50000)
Comment 5 Konstantin Belousov freebsd_committer 2017-03-15 10:08:29 UTC
(In reply to Jan Beich (mail not working) from comment #4)
A library cannot 'pick up symbols without referencing them'.  The presence of the the undefined references means that there are real references in the code.

Note that existence of libm.so as a separate shared object from libc is a minor optimization.  The libm services are mandated by the C standard, so the separate library is only a way to slighly reduce working set of the programs that do not need them.  Linking it in is fine.

If you are so intolerate to the presence of -lm in the dependency list even when symbols are not referenced, you can use '-Wl,--as-needed -lm -Wl,--no-as-needed' construct to only record DT_NEEDED fro libm.so when references actually exist.
Comment 6 Jan Beich freebsd_committer 2017-03-15 10:13:25 UTC
(In reply to Konstantin Belousov from comment #5)
> If you are so intolerate to the presence of -lm in the dependency
> list even when symbols are not referenced, you can use
> '-Wl,--as-needed -lm -Wl,--no-as-needed' construct to only record
> DT_NEEDED fro libm.so when references actually exist.

devel/openmp already adds -Wl,--as-needed and it doesn't work because the references come either from libgcc, crt or something similar.
Comment 7 Jan Beich freebsd_committer 2017-03-15 10:31:39 UTC
LDFLAGS+=-Wl,--verbose diff shows:

+(/usr/lib/libgcc.a)mulxc3.o
+(/usr/lib/libgcc.a)mulsc3.o
+(/usr/lib/libgcc.a)muldc3.o
+(/usr/lib/libgcc.a)divxc3.o
+(/usr/lib/libgcc.a)divsc3.o
+(/usr/lib/libgcc.a)divdc3.o

while nm kmp_atomic.cpp.o diff shows:

-                 U _GLOBAL_OFFSET_TABLE_
+                 U __divdc3
+                 U __divsc3
+                 U __divxc3
+                 U __muldc3
+                 U __mulsc3
+                 U __mulxc3

but using older libgcc.a won't help:

$ nm -A /poudriere/jails/103amd64/usr/lib/libgcc.a | fgrep scalbnl
/poudriere/jails/103amd64/usr/lib/libgcc.a:divxc3.o:                 U scalbnl

$ nm -A /poudriere/jails/110amd64/usr/lib/libgcc.a | fgrep scalbnl
/poudriere/jails/110amd64/usr/lib/libgcc.a:divxc3.o:                 U scalbnl
/poudriere/jails/110amd64/usr/lib/libgcc.a:divtc3.o:                 U scalbnl
Comment 8 Konstantin Belousov freebsd_committer 2017-03-15 10:38:54 UTC
(In reply to Jan Beich (mail not working) from comment #7)
Which means that libm is really needed.
Comment 9 Jan Beich freebsd_committer 2017-03-15 10:41:58 UTC
Maybe but cc -E doesn't show any calls to __divdc3 et al.
Comment 10 Konstantin Belousov freebsd_committer 2017-03-15 10:58:59 UTC
(In reply to Jan Beich (mail not working) from comment #9)
Why it should ?  The symbols like __divdc3 are referenced by a compiler-generated code,  for instance the __divdc3 definition is
   complex double __divdc3 (double a, double b, double c, double d)
with the semantic of return ((a + i * b) / (c + i * d)), where i is imaginary one.  The source code should contain complex division operation, not __divdc3 call, to get the reference.
Comment 11 Jan Beich freebsd_committer 2017-03-15 11:07:30 UTC
Because if compiler emits undefined references the linker cannot be expected to know when -lm is required. Looking at contrib/llvm/tools/clang/lib/Driver/Tools.cpp there are already cases when -lm is passed together with --no-as-needed. Maybe something like  https://reviews.llvm.org/D5698 added one more case.
Comment 12 Jan Beich freebsd_committer 2017-03-29 14:54:55 UTC
2017Q2 is around the corner, so I've landed before upstream review. ;\

https://svnweb.freebsd.org/changeset/ports/437204
Comment 13 commit-hook freebsd_committer 2017-03-29 18:37:47 UTC
A commit references this bug:

Author: jbeich
Date: Wed Mar 29 14:43:30 UTC 2017
New revision: 437204
URL: https://svnweb.freebsd.org/changeset/ports/437204

Log:
  devel/openmp: link libomp.so against -lm for clang 3.6+

  PR:		214258
  Submitted by:	Yuta Satoh <nigoro.dev@gmail.com>
  Approved by:	portmgr blanket

Changes:
  head/devel/llvm-devel/Makefile
  head/devel/llvm-devel/files/openmp-patch-bug32279
  head/devel/llvm37/Makefile
  head/devel/llvm37/files/openmp-patch-bug32279
  head/devel/llvm38/Makefile
  head/devel/llvm38/files/openmp-patch-bug32279
  head/devel/llvm39/Makefile
  head/devel/llvm39/files/openmp-patch-bug32279
  head/devel/llvm40/Makefile
  head/devel/llvm40/files/openmp-patch-bug32279
  head/devel/openmp/Makefile
  head/devel/openmp/files/patch-bug32279