Re: src/lib/libgcc_s needs mv /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a

From: Ian Lepore <ian_at_freebsd.org>
Date: Tue, 14 Sep 2021 18:09:46 UTC
On Sun, 2021-09-12 at 14:53 +0200, Julian H. Stacey wrote:
> Hi all,
> Anyone else seen this ? After
>         cd /usr/src ; make install
> this fails
>         cd /usr/src/lib/libgcc_s ; make
> until a manual
>         mv /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a    \
>            /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a.MV
> when 
>         cd /usr/src ; make all ; make install
> can run OK, but then again needs another 'mv'.
> 
> Identical: md5 /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a*
> /usr/lib/libc.a
> 
> Maybe its changing where linking from ? **
> 
> I've had this on & off for months I think, on multiple hardware, but
> possibly
> just 12.2-STABLE, 
> it's still so with a stable src/ fetched a day or ago with git pull --
> ff-only
> 
> To help simplify debug, I have no /etc/src.conf /etc/make.conf
> I also stripped my env with
> http://berklix.com/~jhs/bin/.csh/unsetenv.csh
>         source `which unsetenv.csh`
>         Remaining: DESTDIR DISPLAY HOSTDISPLAY PATH TERM TERMCAP
> TERMPATH
> None of that helps.
> 
> I am running a 12.2 self built system, uncustomised src/, no kernel
> modules,
> uname -a
>         FreeBSD fire.js.berklix.net 12.2-RELEASE FreeBSD 12.2-RELEASE
>         #0: Sat May 22 20:41:18 CEST 2021
>         jhs@fire.js.berklix.net:/1s4/ftp/pri/FreeBSD/releases/12.2-
> RELEASE/generic/src/sys/amd64/compile/GENERIC
>         amd64
> 
> with the exception of /boot/kernel which is 12.2-RELEASE as for some
> unknown
> reason I cant boot a local compiled or cross compiled 12.2-STABLE
> kernel
> (I'm still investigating that presumed un-associated issue)
> 
> ** Here's a script log :
> {-------
> 
> ===> etc (install)
> ===> etc/sendmail (install)
> cd /usr1/src/share/man; make makedb
> makewhatis /usr/share/man
> makewhatis /usr/share/openssl/man
> 
> ....
> 
> cd /usr/src/lib/libgcc_s ; make
> cat /usr1/src/lib/libgcc_s/Symbol.map
> /usr1/src/lib/libgcc_s/SymbolDefault.map | cpp - -  | awk -v
> vfile=/usr1/src/lib/libgcc_s/Versions.def -f
> /usr/share/mk/version_gen.awk > Version.map
> building shared library libgcc_s.so.1
> cc  -nodefaultlibs -Wl,--version-script=Version.map   -shared -Wl,-x -
> Wl,--fatal-warnings -Wl,--warn-shared-textrel  -o libgcc_s.so.1.full -
> Wl,-soname,libgcc_s.so.1  `NM='nm' NMFLAGS='' lorder i386/fp_mode.pico
> absvdi2.pico absvsi2.pico absvti2.pico addvdi3.pico addvsi3.pico
> addvti3.pico apple_versioning.pico ashldi3.pico ashlti3.pico
> ashrdi3.pico ashrti3.pico bswapdi2.pico bswapsi2.pico clear_cache.pico
> clzdi2.pico clzsi2.pico clzti2.pico cmpdi2.pico cmpti2.pico ctzdi2.pico
> ctzsi2.pico ctzti2.pico divdc3.pico divdi3.pico divmoddi4.pico
> divmodsi4.pico divsc3.pico divsi3.pico divtc3.pico divti3.pico
> divxc3.pico enable_execute_stack.pico eprintf.pico extendhfsf2.pico
> ffsdi2.pico ffssi2.pico ffsti2.pico fixdfdi.pico fixdfti.pico
> fixsfdi.pico fixsfti.pico fixunsdfdi.pico fixunsdfsi.pico
> fixunsdfti.pico fixunssfdi.pico fixunssfsi.pico fixunssfti.pico
> fixunsxfdi.pico fixunsxfsi.pico fixunsxfti.pico fixxfdi.pico
> fixxfti.pico floatditf.pico floattidf.pico floattisf.pico
> floattixf.pico floatunditf.pico floatunsidf.pico floatunsisf.pico
> floatuntidf.pico floatuntisf.pico floatuntixf.pico
> gcc_personality_v0.pico int_util.pico lshrdi3.pico lshrti3.pico
> moddi3.pico modsi3.pico modti3.pico muldc3.pico muldi3.pico
> mulodi4.pico mulosi4.pico muloti4.pico mulsc3.pico multc3.pico
> multi3.pico mulvdi3.pico mulvsi3.pico mulvti3.pico mulxc3.pico
> negdf2.pico negdi2.pico negsf2.pico negti2.pico negvdi2.pico
> negvsi2.pico negvti2.pico paritydi2.pico paritysi2.pico parityti2.pico
> popcountdi2.pico popcountsi2.pico popcountti2.pico powidf2.pico
> powisf2.pico powitf2.pico powixf2.pico subvdi3.pico subvsi3.pico
> subvti3.pico trampoline_setup.pico truncdfhf2.pico truncsfhf2.pico
> ucmpdi2.pico ucmpti2.pico udivdi3.pico udivmoddi4.pico udivmodsi4.pico
> udivmodti4.pico udivsi3.pico udivti3.pico umoddi3.pico umodsi3.pico
> umodti3.pico floatdidf.pico floatdisf.pico floatdixf.pico
> floatundidf.pico floatundisf.pico floatundixf.pico cpu_model.pico
> adddf3.pico addsf3.pico divdf3.pico divsf3.pico extendsfdf2.pico
> fixdfsi.pico fixsfsi.pico floatsidf.pico floatsisf.pico muldf3.pico
> mulsf3.pico subdf3.pico subsf3.pico truncdfsf2.pico comparedf2.pico
> comparesf2.pico gcc_personality_v0.pico int_util.pico Unwind-EHABI.pico
> Unwind-sjlj.pico UnwindLevel1-gcc-ext.pico UnwindLevel1.pico
> UnwindRegistersRestore.pico UnwindRegistersSave.pico libunwind.pico
> s_fabs.pico s_fabsf.pico s_fabsl.pico s_fmax.pico s_fmaxf.pico
> s_logb.pico s_logbf.pico s_scalbn.pico s_scalbnf.pico s_fmaxl.pico
> s_logbl.pico s_scalbnl.pico |  tsort -q` -L/1s4/release/12.2-
> STABLE/usr/obj/usr/src/amd64.amd64/lib/libc -lc
> ld: error: can't create dynamic relocation R_X86_64_32S
> against symbol: __je_sz_size2index_tab in readonly segment; recompile
> object files with -fPIC or pass '-Wl,-z,notext' to allow text
> relocations in the output
> > > > defined in /1s4/release/12.2-
> > > > STABLE/usr/obj/usr/src/amd64.amd64/lib/libc/libc.a(jemalloc_sz.o)
> > > > referenced by sz.h:158
> > > > (/usr1/src/contrib/jemalloc/include/jemalloc/internal/sz.h:158)
> > > >               jemalloc_jemalloc.o:(a0ialloc) in archive
> > > > /1s4/release/12.2-
> > > > STABLE/usr/obj/usr/src/amd64.amd64/lib/libc/libc.a
> 
> ld: error: can't create dynamic relocation R_X86_64_32S
> against symbol: __je_arenas in readonly segment; recompile object files
> with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the
> output
> > > > defined in /1s4/release/12.2-
> > > > STABLE/usr/obj/usr/src/amd64.amd64/lib/libc/libc.a(jemalloc_jemal
> > > > loc.o)
> > > > referenced by atomic.h:55
> > > > (/usr1/src/contrib/jemalloc/include/jemalloc/internal/atomic.h:55
> > > > )
> > > >               jemalloc_jemalloc.o:(a0ialloc) in archive
> > > > /1s4/release/12.2-
> > > > STABLE/usr/obj/usr/src/amd64.amd64/lib/libc/libc.a

> [many similar errors snipped]

> d: error: too many errors emitted, stopping now (use -error-
> limit=0 to see all errors)
> cc: error: linker command failed with exit code 1 (use -v
> to see invocation)
> *** Error code 1
> 
> Stop.
> make: stopped in /usr1/src/lib/libgcc_s
> 
> 
> 12.2-RELEASE /dev/pts/2 jhs 1 fire/usr1/src/lib/libgcc_s ls -l 
> /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a
> -rw-r--r--  1 jhs  staff  17050712 Sep 12 13:28
> /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a
> 
> 12.2-RELEASE /dev/pts/2 jhs 2 fire/usr1/src/lib/libgcc_s mv
> /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a
> /usr/obj/usr/src/amd64.amd64/lib/libc/libc.a.MV
> 
> 12.2-RELEASE /dev/pts/2 jhs 3 fire/usr1/src/lib/libgcc_s make
> building shared library libgcc_s.so.1
> cc  -nodefaultlibs -Wl,--version-script=Version.map   -shared -Wl,-x -
> Wl,--fatal-warnings -Wl,--warn-shared-textrel  -o libgcc_s.so.1.full -
> Wl,-soname,libgcc_s.so.1  `NM='nm' NMFLAGS='' lorder i386/fp_mode.pico
> absvdi2.pico absvsi2.pico absvti2.pico addvdi3.pico addvsi3.pico
> addvti3.pico apple_versioning.pico ashldi3.pico ashlti3.pico
> ashrdi3.pico ashrti3.pico bswapdi2.pico bswapsi2.pico clear_cache.pico
> clzdi2.pico clzsi2.pico clzti2.pico cmpdi2.pico cmpti2.pico ctzdi2.pico
> ctzsi2.pico ctzti2.pico divdc3.pico divdi3.pico divmoddi4.pico
> divmodsi4.pico divsc3.pico divsi3.pico divtc3.pico divti3.pico
> divxc3.pico enable_execute_stack.pico eprintf.pico extendhfsf2.pico
> ffsdi2.pico ffssi2.pico ffsti2.pico fixdfdi.pico fixdfti.pico
> fixsfdi.pico fixsfti.pico fixunsdfdi.pico fixunsdfsi.pico
> fixunsdfti.pico fixunssfdi.pico fixunssfsi.pico fixunssfti.pico
> fixunsxfdi.pico fixunsxfsi.pico fixunsxfti.pico fixxfdi.pico
> fixxfti.pico floatditf.pico floattidf.pico floattisf.pico
> floattixf.pico floatunditf.pico floatunsidf.pico floatunsisf.pico
> floatuntidf.pico floatuntisf.pico floatuntixf.pico
> gcc_personality_v0.pico int_util.pico lshrdi3.pico lshrti3.pico
> moddi3.pico modsi3.pico modti3.pico muldc3.pico muldi3.pico
> mulodi4.pico mulosi4.pico muloti4.pico mulsc3.pico multc3.pico
> multi3.pico mulvdi3.pico mulvsi3.pico mulvti3.pico mulxc3.pico
> negdf2.pico negdi2.pico negsf2.pico negti2.pico negvdi2.pico
> negvsi2.pico negvti2.pico paritydi2.pico paritysi2.pico parityti2.pico
> popcountdi2.pico popcountsi2.pico popcountti2.pico powidf2.pico
> powisf2.pico powitf2.pico powixf2.pico subvdi3.pico subvsi3.pico
> subvti3.pico trampoline_setup.pico truncdfhf2.pico truncsfhf2.pico
> ucmpdi2.pico ucmpti2.pico udivdi3.pico udivmoddi4.pico udivmodsi4.pico
> udivmodti4.pico udivsi3.pico udivti3.pico umoddi3.pico umodsi3.pico
> umodti3.pico floatdidf.pico floatdisf.pico floatdixf.pico
> floatundidf.pico floatundisf.pico floatundixf.pico cpu_model.pico
> adddf3.pico addsf3.pico divdf3.pico divsf3.pico extendsfdf2.pico
> fixdfsi.pico fixsfsi.pico floatsidf.pico floatsisf.pico muldf3.pico
> mulsf3.pico subdf3.pico subsf3.pico truncdfsf2.pico comparedf2.pico
> comparesf2.pico gcc_personality_v0.pico int_util.pico Unwind-EHABI.pico
> Unwind-sjlj.pico UnwindLevel1-gcc-ext.pico UnwindLevel1.pico
> UnwindRegistersRestore.pico UnwindRegistersSave.pico libunwind.pico
> s_fabs.pico s_fabsf.pico s_fabsl.pico s_fmax.pico s_fmaxf.pico
> s_logb.pico s_logbf.pico s_scalbn.pico s_scalbnf.pico s_fmaxl.pico
> s_logbl.pico s_scalbnl.pico |  tsort -q` -L/1s4/release/12.2-
> STABLE/usr/obj/usr/src/amd64.amd64/lib/libc -lc
> objcopy --only-keep-debug libgcc_s.so.1.full libgcc_s.so.1.debug
> objcopy --strip-debug --add-gnu-debuglink=libgcc_s.so.1.debug 
> libgcc_s.so.1.full libgcc_s.so.1
> 
> 12.2-RELEASE /dev/pts/2 jhs 4 fire/usr1/src/lib/libgcc_s 
> -------}
> 
> Cheers,

I ran into this problem when trying to update our build system to use
freebsd 13 for our armv7-based products (we were previously using 11.2-
stable; we skipped 12.x completely).

Our build process creates a chroot directory, checks out the freebsd
source into it, bootstraps the compiler, builds the world with that
compiler and installs the world into the chroot dir.  It then chroots
into that area and builds clang and world-libs again as an armv7 cross-
build environment.  It was while doing the latter (specifically, doing
"make xdev") that I ran into the errors about being unable to create a
relocation in a read-only text segment while linking libgcc_s.

I tracked the cause of the error message down to trying to static-link
libc (which had been built as non-PIC code) into libgcc_s which was
being built as a shared (PIC) library.  Here's the fix I committed to
get around the problem (with some more commentary after it):

    files:       lib/libgcc_s/Makefile
    description:
    When building libgcc_s for armv7, link against libc_pic.a, which is built as
    position-independent code.  That means it will not include movw/movt
    instructions which require relocation fixup in the text segment, which is
    readonly in a shared lib such as libgcc_s.
    
    I'm not certain this is the best or only fix available for this problem,
    but it is a fix for what has prevented us from building freebsd 12 or later
    for almost 2 years now.  It seems to me that other shared libs link with
    libc.a and they don't have any problems with text relocs.  But maybe there
    are only a few problematic functions in libc, and not all shared libs run
    into those few functions.
    
    
    diff -r 38de27edd9d7 -r 118e28fad4ff lib/libgcc_s/Makefile
    --- a/lib/libgcc_s/Makefile     Mon Jul 05 11:45:38 2021 -0600
    +++ b/lib/libgcc_s/Makefile     Tue Jul 06 13:22:25 2021 -0600
    @@ -10,7 +10,11 @@ MK_SSP=              no
     WARNS?=        2
     
     LDFLAGS+=      -nodefaultlibs
    +.if ${MACHINE_ARCH} == "armv7"
    +LIBADD+=       c_pic
    +.else
     LIBADD+=       c
    +.endif
     
     VERSION_DEF=   ${.CURDIR}/Versions.def
     SYMBOL_MAPS=   ${.CURDIR}/Symbol.map

As you can see from my commit message, I don't feel like I really
properly understood the problem and fixed it the right way.  It was
just something I could do at the time to make progress (we still aren't
actually using this 13-stable yet).

I had to wrap the fix in .if to limit it to armv7, because with the
change in effect all the time, I ended up with different errors during
the initial amd64 build of the chroot.

So all in all, I'm not saying this is the fix for the problem you're
seeing, but I'm pretty sure your problem and mine are related somehow.
Maybe with this as an additional clue, the people who understand the
toolchains better will be able to offer some advice.

-- Ian