Thursday, December 03, 2020

Re: gcc --static produces programs that crash

Hi George,


On 2020-12-02 19:57, George Koehler wrote:
> This diff in lang/gcc/8 works for me on amd64 and powerpc. I would
> like tests on more arches. Is it ok to commit?

I tested this diff on a Raspberry Pi4 (aarch64) and it succeeds with
testing -Bdynamic or -static for Fortran and C, and
checking the -R option for c++. These tests fail for -static
on a stock 6.8 release.


for linking in "-Bdynamic" "-static" ; do

echo linking is ${linking}
egfortran -o fworks fworks.f90 ${linking}
./fworks || echo " " Fortran ${linking} fails

egcc -o cworks cworks.c ${linking}
./cworks || echo " " CC ${linking} fails

done

eg++ -fPIC -shared cppshare.cpp -o libcppshare.so
eg++ -L . -lcppshare -o cppworks cppworks.cpp
./cppworks && echo " ./cppworks somehow finds local libcppshare.so
without LD_LIBRARY_PATH!!!"
LD_LIBRARY_PATH=. ./cppworks

eg++ -L . -lcppshare -o cppworks cppworks.cpp -R `pwd`
./cppworks || echo " cant find local libcppshare.so with -R option"


So I believe I have covered most of your diff with these tests, except
the -nostdlib parsing.



John


>
> This diff makes 4 changes to LINK_SPEC:
> 1. use "%{!static:-dynamic-linker /usr/libexec/ld.so}" to fix
> static linking; you can try the Fortran example below.
> 2. change %{r*:} to %{r:}; base-gcc uses %{r*:}, but upstream
> changed %{r*:} to %{r:} in commit 7aed7df [1].
> 3. change -L/usr/lib to %{!nostdlib:-L/usr/lib} to be like base-gcc.
> 4. rewrite powerpc's *_rs6000_openbsd_h to be like other arches,
> and add %{!nostdlib:-L/usr/lib} so I can try lld on powerpc.
>
> $ cat works.f90
> print *, 'Fortran works.'
> end
> $ egfortran -o works works.f90 -static
> $ ./works
> Fortran works.
>
> %{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}}
> means if not "gcc -shared" and not "gcc -nostdlib" and not "gcc -r"
> and not "gcc -e something_else", then run "ld -e __start". We don't
> need this spec on arches like powerpc, where ld's default entry is
> __start, but I added this spec to powerpc to be like other arches.
> %{shared:-shared}
> means if "gcc -shared" then "ld -shared".
> %{R*}
> means if "gcc -R/some/dir" then "ld -R /some/dir". This had been
> missing on powerpc, where "gcc -R/some/dir" got ignored.
> %{static:-Bstatic}
> means if "gcc -static" then "ld -Bstatic". We need this to tell ld
> to avoid shared libs when linking static bins. powerpc was using
> "ld -static", but I changed it to -Bstatic to be like other arches.
> %{!static:-Bdynamic}
> might not be needed, but I kept it and added it to powerpc.
> %{rdynamic:-export-dynamic}
> was added by our patches, and I kept it.
> %{assert*}
> is useless. The intent is "gcc -assert X" => "ld -assert X", but
> gcc rejects -assert and uses --assert for Fortran. I didn't delete
> the %{assert*} because upstream hadn't deleted it.
> %{!static:-dynamic-linker /usr/libexec/ld.so}
> fixes static linking. base-gcc uses the longer spec
> %{!static:%{!dynamic-linker:-dynamic-linker /usr/libexec/ld.so}}
> but there is no such option "gcc -dynamic-linker X"; upstream
> deleted %{!dynamic-linker:} in commit e59dabd [2].
> %{!nostdlib:-L/usr/lib}
> is for lld to search /usr/lib. There is a bug, for which I don't
> know the fix: this spec passes -L/usr/lib too early, so the search
> order is /usr/lib before anything else, and you can't tell gcc to
> use your own libcurses.a instead of the one in /usr/lib [3].
>
> [1]
> https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=7aed7dfc45e81230bd4fa01c16b55c0904b49535
> [2]
> https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=e59dabd237f24b87814a3664e2f092c2b107facf
> [3] https://marc.info/?l=openbsd-bugs&m=156547894114050&w=2
>
> Index: Makefile
> ===================================================================
> RCS file: /cvs/ports/lang/gcc/8/Makefile,v
> retrieving revision 1.36
> diff -u -p -r1.36 Makefile
> --- Makefile 14 Nov 2020 00:00:39 -0000 1.36
> +++ Makefile 2 Dec 2020 04:27:15 -0000
> @@ -18,7 +18,7 @@ DPB_PROPERTIES = parallel
> V = 8.4.0
> FULL_VERSION = $V
> FULL_PKGVERSION = $V
> -REVISION = 1
> +REVISION = 2
>
> ADASTRAP-amd64 = adastrap-amd64-8.3.0-2.tar.xz
> ADASTRAP-arm = adastrap-arm-4.9.4-0.tar.xz
> Index: patches/patch-gcc_config_aarch64_openbsd_h
> ===================================================================
> RCS file:
> /cvs/ports/lang/gcc/8/patches/patch-gcc_config_aarch64_openbsd_h,v
> retrieving revision 1.2
> diff -u -p -r1.2 patch-gcc_config_aarch64_openbsd_h
> --- patches/patch-gcc_config_aarch64_openbsd_h 20 May 2019 14:59:05
> -0000 1.2
> +++ patches/patch-gcc_config_aarch64_openbsd_h 2 Dec 2020 04:27:15
> -0000
> @@ -51,14 +51,14 @@ Index: gcc/config/aarch64/openbsd.h
> +#define SUBTARGET_CPP_SPEC OBSD_CPP_SPEC
> +
> +#define LINK_SPEC \
> -+ "%{!shared:%{!nostdlib:%{!r*:%{!e*:-e __start}}}} \
> ++ "%{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}} \
> + %{shared:-shared} %{R*} \
> + %{static:-Bstatic} \
> + %{!static:-Bdynamic} \
> + %{rdynamic:-export-dynamic} \
> + %{assert*} \
> -+ %{!shared:%{!dynamic-linker:-dynamic-linker /usr/libexec/ld.so}} \
> -+ -L/usr/lib"
> ++ %{!static:-dynamic-linker /usr/libexec/ld.so} \
> ++ %{!nostdlib:-L/usr/lib}"
> +
> +#define OPENBSD_ENTRY_POINT "__start"
> +
> Index: patches/patch-gcc_config_alpha_openbsd_h
> ===================================================================
> RCS file:
> /cvs/ports/lang/gcc/8/patches/patch-gcc_config_alpha_openbsd_h,v
> retrieving revision 1.2
> diff -u -p -r1.2 patch-gcc_config_alpha_openbsd_h
> --- patches/patch-gcc_config_alpha_openbsd_h 20 May 2019 14:59:05
> -0000 1.2
> +++ patches/patch-gcc_config_alpha_openbsd_h 2 Dec 2020 04:27:15 -0000
> @@ -10,14 +10,14 @@ Index: gcc/config/alpha/openbsd.h
> +#define TARGET_DEFAULT \
> + (MASK_FPREGS | MASK_IEEE | MASK_IEEE_CONFORMANT)
> +
> -+ #define LINK_SPEC \
> -+ "%{!shared:%{!nostdlib:%{!r*:%{!e*:-e __start}}}} \
> ++#define LINK_SPEC \
> ++ "%{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}} \
> + %{shared:-shared} %{R*} \
> + %{static:-Bstatic} \
> + %{!static:-Bdynamic} \
> + %{rdynamic:-export-dynamic} \
> + %{assert*} \
> -+ %{!shared:%{!dynamic-linker:-dynamic-linker /usr/libexec/ld.so}}"
> ++ %{!static:-dynamic-linker /usr/libexec/ld.so}"
> +
> +/* As an elf system, we need crtbegin/crtend stuff. */
> +#undef STARTFILE_SPEC
> Index: patches/patch-gcc_config_arm_openbsd_h
> ===================================================================
> RCS file:
> /cvs/ports/lang/gcc/8/patches/patch-gcc_config_arm_openbsd_h,v
> retrieving revision 1.2
> diff -u -p -r1.2 patch-gcc_config_arm_openbsd_h
> --- patches/patch-gcc_config_arm_openbsd_h 20 May 2019 14:59:05
> -0000 1.2
> +++ patches/patch-gcc_config_arm_openbsd_h 2 Dec 2020 04:27:15 -0000
> @@ -73,16 +73,16 @@ Index: gcc/config/arm/openbsd.h
> +#undef OBSD_LINK_SPEC
> +#ifdef OBSD_NO_DYNAMIC_LIBRARIES
> +#define OBSD_LINK_SPEC \
> -+ "%{!nostdlib:%{!r*:%{!e*:-e __start}}} %{assert*}"
> ++ "%{!nostdlib:%{!r:%{!e*:-e __start}}} %{assert*}"
> +#else
> +#define OBSD_LINK_SPEC \
> -+ "%{!shared:%{!nostdlib:%{!r*:%{!e*:-e __start}}}} \
> ++ "%{!shared:%{!nostdlib:%{!r:%{!e*:-e __start}}}} \
> + %{shared:-shared} %{R*} \
> + %{static:-Bstatic} \
> + %{!static:-Bdynamic} \
> + %{rdynamic:-export-dynamic} \
> + %{assert*} \
> -+ %{!shared:%{!dynamic-linker:-dynamic-linker /usr/libexec/ld.so}} \
> ++ %{!static:-dynamic-linker /usr/libexec/ld.so} \
> + %{!nostdlib:-L/usr/lib}"
> +

No comments:

Post a Comment