Skip to content

Commit

Permalink
Add --no-as-needed, --export-dynamic
Browse files Browse the repository at this point in the history
This makes dynamic libraries work on Linux arm64! Thanks to Lukáš
Zaoral at Red Hat for finding the key to the fix, which is passing
--no-as-needed to the linker on Linux to avoid glibc interfering
with libast in identically named functions like AST regcomp(3).
Linux systems vary as to whether --as-needed is on by default.

src/cmd/INIT/make.probe:
- Add probe for the --as-needed and --no-as-needed linker options.
  Thanks to Johnothan King.

**/Mamfile:
- Use the --no-as-needed probe result (either -Wl,-no-as-needed or
  empty) from ${mam_cc_LD_NOASNEEDED} whenever linking anything;
  add it to LDFLAGS before invoking dylink.
- ksh and shcomp also need the results of the existing
  --export-dynamic probe; this option is needed on Linux as without
  it ksh fails to export symbols to loadable builtins. See:
  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=821806
  Thanks to Johnothan King, again.

src/cmd/INIT/mamprobe.sh:
- Update version number. This change causes mamake to redo the
  probe (it checks for a change in mamprobe but not in make.probe;
  I should probably fix that at some point). We need mamake to redo
  the probe for the --no-as-needed addition to take effect.

src/lib/libast/comp/errno.c:
- Make one trivial change (copyright year) to force libast to
  relink -- which then triggers a relink of everything else.

src/cmd/INIT/dylink.sh:
- Remove crash warning for Linux arm. :-)

Co-authored-by: Johnothan King <johnothanking@protonmail.com>
Co-authored-by: Lukáš Zaoral <lzaoral@redhat.com>
  • Loading branch information
3 people committed Mar 6, 2024
1 parent af10d5a commit 0fdbcdc
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/cmd/INIT/dylink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ ms) case $suffix in
esac

case $HOSTTYPE in
linux.aarch* | linux.arm*)
case ${AST_CRASH_ME:+s} in
'') note "ksh with dynamic libraries is known to crash on $HOSTTYPE." \
"The cause is still a mystery to us. Please help us debug it at:" \
"https://github.com/ksh93/ksh/issues/TODO" \
"Skipping. Export AST_CRASH_ME to build dynamic libraries."
exit 0 ;; # continue build
esac ;;
cygwin.*)
note "Dynamic libraries are not supported on $HOSTTYPE."
exit 0 ;; # continue build
Expand Down
22 changes: 21 additions & 1 deletion src/cmd/INIT/make.probe
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Glenn Fowler
# AT&T Research
#
# @(#)make.probe (ksh 93u+m) 2024-01-09
# @(#)make.probe (ksh 93u+m) 2024-03-05
#
# C probe for make
#
Expand All @@ -21,6 +21,7 @@ probe_export_dynamic="-rdynamic -export-dynamic -Wl,-export-dynamic -Wl,-E -bexp
probe_gcc_optimize="-Os"
probe_gcc_version="*[Gg][Cc][Cc]*"
probe_include_local="'-ignore-source-dir -iquote' -iquote -I-"
probe_ldasneeded="-Wl,-as-needed -Wl,-no-as-needed"
probe_ldlazy='-zlazyload -znolazyload -Wl,-zlazyload -Wl,-znolazyload'
probe_ldlib="LD_LIBRARY_PATH LIBPATH LPATH"
probe_ldmap="'-Wl,-M' '-Qoption ld -M' '-Wl,-m' '-m'"
Expand Down Expand Up @@ -129,6 +130,7 @@ export_dynamic=
gnu=
implicitc=
include_local=
ldasneeded=
lddynamic=
ldlazy=
ldnolazy=
Expand Down Expand Up @@ -557,6 +559,20 @@ then eval set x x $probe_ldlazy
ldnorecord=$2
break
done
eval set x x $probe_ldasneeded
while :
do shift
shift
case $# in
0) break ;;
esac
rm -f strip.$exe
$cc -o strip.$exe $1 strip.$obj $2 2>f && test -f strip.$exe || continue
cmp -s e f || continue
ldasneeded=$1
ldnoasneeded=$2
break
done
fi

case $cc_dll:$cc_pic:$so:$dynamic:$static in
Expand Down Expand Up @@ -1941,6 +1957,8 @@ CC_DYNAMIC=$dynamic
CC_EXPORT_DYNAMIC=$export_dynamic
CC_INCLUDE_LOCAL=$include_local
CC_LD=$ld
CC_LD_ASNEEDED=$ldasneeded
CC_LD_NOASNEEDED=$ldnoasneeded
CC_LD_DYNAMIC=$lddynamic
CC_LD_LAZY=$ldlazy
CC_LD_NOLAZY=$ldnolazy
Expand Down Expand Up @@ -2045,6 +2063,8 @@ $CC_CC) echo if LDSHARED
*) echo CC.LD = $CC_LD
;;
esac
echo CC.LD.ASNEEDED = $CC_LD_ASNEEDED
echo CC.LD.NOASNEEDED = $CC_LD_NOASNEEDED
echo CC.LD.DYNAMIC = $CC_LD_DYNAMIC
echo CC.LD.LAZY = $CC_LD_LAZY
echo CC.LD.NOLAZY = $CC_LD_NOLAZY
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/INIT/mamprobe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ opt=
case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in
0123) USAGE=$'
[-?
@(#)$Id: mamprobe (ksh 93u+m) 2024-02-10 $
@(#)$Id: mamprobe (ksh 93u+m) 2024-03-05 $
]
[+NAME?mamprobe - generate MAM cc probe info]
[+DESCRIPTION?\bmamprobe\b generates MAM (make abstract machine) \bcc\b(1)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/builtin/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ make install virtual
make ${INSTALLROOT}/dyn/bin/pty dontcare
prev pty
prev ${INSTALLROOT}/lib/lib/cmd
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED}"
exec - dylink -e pty ${mam_libutil} $(cat ${INSTALLROOT}/lib/lib/cmd) pty.o
done

Expand Down
3 changes: 3 additions & 0 deletions src/cmd/ksh93/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -1242,18 +1242,21 @@ make install virtual
make ${INSTALLROOT}/dyn/lib/libshell${DYLIB_SUFFIX} dontcare
prev libshell.a
prev shell.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED}"
exec - dylink -m shell -s ${DYLIB_SUFFIX} $(sed 1d shell.req) ${_libshell_object_files_}
done

make ${INSTALLROOT}/dyn/bin/ksh dontcare
prev ksh
prev shell.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED} ${mam_cc_EXPORT_DYNAMIC}"
exec - dylink -e ksh $(cat shell.req) ${mam_libnsl} ${mam_libm} pmain.o
done

make ${INSTALLROOT}/dyn/bin/shcomp dontcare
prev shcomp
prev shell.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED} ${mam_cc_EXPORT_DYNAMIC}"
exec - dylink -e shcomp $(cat shell.req) ${mam_libnsl} ${mam_libm} shcomp.o
done

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/tests/exit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# #
# This software is part of the ast package #
# Copyright (c) 1982-2011 AT&T Intellectual Property #
# Copyright (c) 2020-2023 Contributors to ksh 93u+m #
# Copyright (c) 2020-2024 Contributors to ksh 93u+m #
# and is licensed under the #
# Eclipse Public License, Version 2.0 #
# #
Expand Down
1 change: 1 addition & 0 deletions src/lib/libast/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -4956,6 +4956,7 @@ make install virtual
make ${INSTALLROOT}/dyn/lib/libast${DYLIB_SUFFIX} dontcare
prev libast.a
prev ast.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED}"
exec - dylink -m ast -s ${DYLIB_SUFFIX} $(sed 1d ast.req) ${_libast_object_files_}
done

Expand Down
2 changes: 1 addition & 1 deletion src/lib/libast/comp/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2022 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down
1 change: 1 addition & 0 deletions src/lib/libcmd/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ make install virtual
make ${INSTALLROOT}/dyn/lib/libcmd${DYLIB_SUFFIX} dontcare
prev libcmd.a
prev cmd.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED}"
exec - dylink -m cmd -s ${DYLIB_SUFFIX} $(sed 1d cmd.req) ${_libcmd_object_files_}
done

Expand Down
1 change: 1 addition & 0 deletions src/lib/libdll/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ make install virtual
make ${INSTALLROOT}/dyn/lib/libdll${DYLIB_SUFFIX} dontcare
prev libdll.a
prev dll.req
exec - export LDFLAGS="$LDFLAGS ${mam_cc_LD_NOASNEEDED}"
exec - dylink -m dll -s ${DYLIB_SUFFIX} $(sed 1d dll.req) ${_libdll_object_files_}
done

Expand Down

0 comments on commit 0fdbcdc

Please sign in to comment.