Skip to content

Link cephes-gtsam against libm#2590

Open
hz2 wants to merge 1 commit into
borglab:developfrom
hz2:fix-cephes-libm-link
Open

Link cephes-gtsam against libm#2590
hz2 wants to merge 1 commit into
borglab:developfrom
hz2:fix-cephes-libm-link

Conversation

@hz2

@hz2 hz2 commented Jul 3, 2026

Copy link
Copy Markdown

Summary

cephes-gtsam calls libm functions directly (e.g. sin in cephes/gamma.c)
but was never linked against libm itself. It happened to keep working
because the symbol got resolved transitively through whatever else ends up
in the final process, but that's fragile: depending on link order in a
downstream consumer, glibc's dynamic linker can crash while relinking the
IFUNC-resolved sin symbol (prints Relink libcephes-gtsam.so' with
libm.so' for IFUNC symbol sin'right before segfaulting inside_dl_relocate_object`). Same failure mode as
RHBZ#1883501 in an
unrelated project (cp2k/libxc).

I hit this building a downstream Rust FFI binding against GTSAM: a build
that pulled in more of libgtsam's symbol surface shifted link order enough
to trigger the crash reliably. Confirmed the missing dependency directly
(readelf -d libcephes-gtsam.so had no NEEDED entry for libm.so), and
confirmed this fix resolves it.

Fix

find_library(MATH_LIBRARY m) + conditional target_link_libraries,
rather than a bare -lm, so it naturally no-ops on platforms without a
separate libm (Windows, and effectively macOS where it's already part of
libSystem).

Not a duplicate of #2273 (fixed a different issue: duplicate declarations of
libm functions in cephes.h conflicting under static linking) or #2371
(trimmed which cephes source files are built); neither touched linking
cephes-gtsam against libm itself.

Test plan

  • cmake configure + make cephes-gtsam succeeds
  • readelf -d on the resulting libcephes-gtsam.so shows a NEEDED
    entry for libm.so.6 (previously absent)
  • Downstream build that previously crashed at dynamic-link time now
    runs cleanly, repeated across multiple rebuilds

cephes/gamma.c, igam.c, and the other remaining cephes sources call libm
functions (e.g. sin) directly, but cephes-gtsam was never linked against
libm itself, leaving the symbol to be resolved transitively through
whatever else the final binary happens to link against.

That can crash the dynamic linker while relinking an IFUNC-resolved
symbol (glibc printing "Relink `libcephes-gtsam.so' with `libm.so' for
IFUNC symbol `sin'" before segfaulting in _dl_relocate_object) depending
on link order in the consuming binary. Same failure mode as
https://bugzilla.redhat.com/show_bug.cgi?id=1883501 in an unrelated
project. Reproduced consistently when linking a downstream consumer
that pulls in enough of libgtsam's symbol surface to shift link order,
and confirmed fixed by this change (readelf -d shows a proper NEEDED
entry for libm.so.6 afterward).

Uses find_library(MATH_LIBRARY m) rather than an explicit -lm so this
naturally no-ops on platforms without a separate libm (Windows, and
effectively macOS where it's already part of libSystem).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant