*/*: Discontinue Gentoo SuperH port
[gentoo.git] / sys-libs / glibc / glibc-2.30-r6.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python3_{6,7,8} )
7
8 inherit python-any-r1 prefix eutils toolchain-funcs flag-o-matic gnuconfig \
9         multilib systemd multiprocessing
10
11 DESCRIPTION="GNU libc C library"
12 HOMEPAGE="https://www.gnu.org/software/libc/"
13 LICENSE="LGPL-2.1+ BSD HPND ISC inner-net rc PCRE"
14 SLOT="2.2"
15
16 EMULTILIB_PKG="true"
17
18 if [[ ${PV} == 9999* ]]; then
19         EGIT_REPO_URI="https://sourceware.org/git/glibc.git"
20         inherit git-r3
21 else
22         KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~riscv ~s390 ~sparc ~x86"
23         SRC_URI="mirror://gnu/glibc/${P}.tar.xz"
24 fi
25
26 RELEASE_VER=${PV}
27
28 GCC_BOOTSTRAP_VER=20180511
29
30 # Gentoo patchset
31 PATCH_VER=8
32 PATCH_DEV=dilfridge
33
34 SRC_URI+=" https://dev.gentoo.org/~${PATCH_DEV}/distfiles/${P}-patches-${PATCH_VER}.tar.xz"
35 SRC_URI+=" multilib? ( https://dev.gentoo.org/~dilfridge/distfiles/gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz )"
36
37 IUSE="audit caps cet compile-locales +crypt custom-cflags doc gd headers-only +multiarch multilib nscd profile selinux +ssp +static-libs suid systemtap test vanilla"
38
39 # Minimum kernel version that glibc requires
40 MIN_KERN_VER="3.2.0"
41
42 # Here's how the cross-compile logic breaks down ...
43 #  CTARGET - machine that will target the binaries
44 #  CHOST   - machine that will host the binaries
45 #  CBUILD  - machine that will build the binaries
46 # If CTARGET != CHOST, it means you want a libc for cross-compiling.
47 # If CHOST != CBUILD, it means you want to cross-compile the libc.
48 #  CBUILD = CHOST = CTARGET    - native build/install
49 #  CBUILD != (CHOST = CTARGET) - cross-compile a native build
50 #  (CBUILD = CHOST) != CTARGET - libc for cross-compiler
51 #  CBUILD != CHOST != CTARGET  - cross-compile a libc for a cross-compiler
52 # For install paths:
53 #  CHOST = CTARGET  - install into /
54 #  CHOST != CTARGET - install into /usr/CTARGET/
55 #
56 export CBUILD=${CBUILD:-${CHOST}}
57 export CTARGET=${CTARGET:-${CHOST}}
58 if [[ ${CTARGET} == ${CHOST} ]] ; then
59         if [[ ${CATEGORY} == cross-* ]] ; then
60                 export CTARGET=${CATEGORY#cross-}
61         fi
62 fi
63
64 # Note [Disable automatic stripping]
65 # Disabling automatic stripping for a few reasons:
66 # - portage's attempt to strip breaks non-native binaries at least on
67 #   arm: bug #697428
68 # - portage's attempt to strip libpthread.so.0 breaks gdb thread
69 #   enumeration: bug #697910. This is quite subtle:
70 #   * gdb uses glibc's libthread_db-1.0.so to enumerate threads.
71 #   * libthread_db-1.0.so needs access to libpthread.so.0 local symbols
72 #     via 'ps_pglobal_lookup' symbol defined in gdb.
73 #   * 'ps_pglobal_lookup' uses '.symtab' section table to resolve all
74 #     known symbols in 'libpthread.so.0'. Specifically 'nptl_version'
75 #     (unexported) is used to sanity check compatibility before enabling
76 #     debugging.
77 #     Also see https://sourceware.org/gdb/wiki/FAQ#GDB_does_not_see_any_threads_besides_the_one_in_which_crash_occurred.3B_or_SIGTRAP_kills_my_program_when_I_set_a_breakpoint
78 #   * normal 'strip' command trims '.symtab'
79 #   Thus our main goal here is to prevent 'libpthread.so.0' from
80 #   losing it's '.symtab' entries.
81 # As Gentoo's strip does not allow us to pass less aggressive stripping
82 # options and does not check the machine target we strip selectively.
83
84 # We need a new-enough binutils/gcc to match upstream baseline.
85 # Also we need to make sure our binutils/gcc supports TLS,
86 # and that gcc already contains the hardened patches.
87 BDEPEND="
88         ${PYTHON_DEPS}
89         >=app-misc/pax-utils-0.1.10
90         sys-devel/bison
91         !<sys-devel/bison-2.7
92         doc? ( sys-apps/texinfo )
93 "
94 COMMON_DEPEND="
95         gd? ( media-libs/gd:2= )
96         nscd? ( selinux? (
97                 audit? ( sys-process/audit )
98                 caps? ( sys-libs/libcap )
99         ) )
100         suid? ( caps? ( sys-libs/libcap ) )
101         selinux? ( sys-libs/libselinux )
102         systemtap? ( dev-util/systemtap )
103 "
104 DEPEND="${COMMON_DEPEND}
105         test? ( >=net-dns/libidn2-2.3.0 )
106 "
107 RDEPEND="${COMMON_DEPEND}
108         sys-apps/gentoo-functions
109 "
110
111 RESTRICT="!test? ( test )"
112
113 if [[ ${CATEGORY} == cross-* ]] ; then
114         BDEPEND+=" !headers-only? (
115                 >=${CATEGORY}/binutils-2.24
116                 >=${CATEGORY}/gcc-6
117         )"
118         [[ ${CATEGORY} == *-linux* ]] && DEPEND+=" ${CATEGORY}/linux-headers"
119 else
120         BDEPEND+="
121                 >=sys-devel/binutils-2.24
122                 >=sys-devel/gcc-6
123         "
124         DEPEND+=" virtual/os-headers "
125         RDEPEND+="
126                 >=net-dns/libidn2-2.3.0
127                 vanilla? ( !sys-libs/timezone-data )
128         "
129         PDEPEND+=" !vanilla? ( sys-libs/timezone-data )"
130 fi
131
132 # Ignore tests whitelisted below
133 GENTOO_GLIBC_XFAIL_TESTS="${GENTOO_GLIBC_XFAIL_TESTS:-yes}"
134
135 # The following tests fail due to the Gentoo build system and are thus
136 # executed but ignored:
137 XFAIL_TEST_LIST=(
138         # 1) Sandbox
139         tst-ldconfig-bad-aux-cache
140         tst-pldd
141         tst-mallocfork2
142         tst-nss-db-endgrent
143         tst-nss-db-endpwent
144         tst-nss-files-hosts-long
145         tst-nss-test3
146         # 2) Namespaces and cgroup
147         tst-locale-locpath
148         # 9) Failures of unknown origin
149         tst-latepthread
150 )
151
152 #
153 # Small helper functions
154 #
155
156 is_crosscompile() {
157         [[ ${CHOST} != ${CTARGET} ]]
158 }
159
160 just_headers() {
161         is_crosscompile && use headers-only
162 }
163
164 alt_prefix() {
165         is_crosscompile && echo /usr/${CTARGET}
166 }
167
168 # This prefix is applicable to CHOST when building against this
169 # glibc. It is baked into the library at configure time.
170 host_eprefix() {
171         is_crosscompile || echo "${EPREFIX}"
172 }
173
174 # This prefix is applicable to CBUILD when building against this
175 # glibc. It determines the destination path at install time.
176 build_eprefix() {
177         is_crosscompile && echo "${EPREFIX}"
178 }
179
180 # We need to be able to set alternative headers for compiling for non-native
181 # platform. Will also become useful for testing kernel-headers without screwing
182 # up the whole system.
183 alt_headers() {
184         echo ${ALT_HEADERS:=$(alt_prefix)/usr/include}
185 }
186
187 alt_build_headers() {
188         if [[ -z ${ALT_BUILD_HEADERS} ]] ; then
189                 ALT_BUILD_HEADERS="$(host_eprefix)$(alt_headers)"
190                 if tc-is-cross-compiler ; then
191                         ALT_BUILD_HEADERS=${SYSROOT}$(alt_headers)
192                         if [[ ! -e ${ALT_BUILD_HEADERS}/linux/version.h ]] ; then
193                                 local header_path=$(echo '#include <linux/version.h>' | $(tc-getCPP ${CTARGET}) ${CFLAGS} 2>&1 | grep -o '[^"]*linux/version.h')
194                                 ALT_BUILD_HEADERS=${header_path%/linux/version.h}
195                         fi
196                 fi
197         fi
198         echo "${ALT_BUILD_HEADERS}"
199 }
200
201 alt_libdir() {
202         echo $(alt_prefix)/$(get_libdir)
203 }
204 alt_usrlibdir() {
205         echo $(alt_prefix)/usr/$(get_libdir)
206 }
207
208 builddir() {
209         echo "${WORKDIR}/build-${ABI}-${CTARGET}-$1"
210 }
211
212 do_compile_test() {
213         local ret save_cflags=${CFLAGS}
214         CFLAGS+=" $1"
215         shift
216
217         pushd "${T}" >/dev/null
218
219         rm -f glibc-test*
220         printf '%b' "$*" > glibc-test.c
221
222         nonfatal emake glibc-test
223         ret=$?
224
225         popd >/dev/null
226
227         CFLAGS=${save_cflags}
228         return ${ret}
229 }
230
231 do_run_test() {
232         local ret
233
234         if [[ ${MERGE_TYPE} == "binary" ]] ; then
235                 # ignore build failures when installing a binary package #324685
236                 do_compile_test "" "$@" 2>/dev/null || return 0
237         else
238                 if ! do_compile_test "" "$@" ; then
239                         ewarn "Simple build failed ... assuming this is desired #324685"
240                         return 0
241                 fi
242         fi
243
244         pushd "${T}" >/dev/null
245
246         ./glibc-test
247         ret=$?
248         rm -f glibc-test*
249
250         popd >/dev/null
251
252         return ${ret}
253 }
254
255 setup_target_flags() {
256         # This largely mucks with compiler flags.  None of which should matter
257         # when building up just the headers.
258         just_headers && return 0
259
260         case $(tc-arch) in
261                 x86)
262                         # -march needed for #185404 #199334
263                         # TODO: When creating the first glibc cross-compile, this test will
264                         # always fail as it does a full link which in turn requires glibc.
265                         # Probably also applies when changing multilib profile settings (e.g.
266                         # enabling x86 when the profile was amd64-only previously).
267                         # We could change main to _start and pass -nostdlib here so that we
268                         # only test the gcc code compilation.  Or we could do a compile and
269                         # then look for the symbol via scanelf.
270                         if ! do_compile_test "" 'void f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n'; then
271                                 local t=${CTARGET_OPT:-${CTARGET}}
272                                 t=${t%%-*}
273                                 filter-flags '-march=*'
274                                 export CFLAGS="-march=${t} ${CFLAGS}"
275                                 einfo "Auto adding -march=${t} to CFLAGS #185404"
276                         fi
277                 ;;
278                 amd64)
279                         # -march needed for #185404 #199334
280                         # TODO: See cross-compile issues listed above for x86.
281                         [[ ${ABI} == x86 ]] &&
282                         if ! do_compile_test "${CFLAGS_x86}" 'void f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n'; then
283                                 local t=${CTARGET_OPT:-${CTARGET}}
284                                 t=${t%%-*}
285                                 # Normally the target is x86_64-xxx, so turn that into the -march that
286                                 # gcc actually accepts. #528708
287                                 [[ ${t} == "x86_64" ]] && t="x86-64"
288                                 filter-flags '-march=*'
289                                 # ugly, ugly, ugly.  ugly.
290                                 CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} filter-flags '-march=*'; echo "${CFLAGS}")
291                                 export CFLAGS_x86="${CFLAGS_x86} -march=${t}"
292                                 einfo "Auto adding -march=${t} to CFLAGS_x86 #185404 (ABI=${ABI})"
293                         fi
294                 ;;
295                 mips)
296                         # The mips abi cannot support the GNU style hashes. #233233
297                         filter-ldflags -Wl,--hash-style=gnu -Wl,--hash-style=both
298                 ;;
299                 sparc)
300                         # Both sparc and sparc64 can use -fcall-used-g6.  -g7 is bad, though.
301                         filter-flags "-fcall-used-g7"
302                         append-flags "-fcall-used-g6"
303
304                         local cpu
305                         case ${CTARGET} in
306                         sparc64-*)
307                                 cpu="sparc64"
308                                 case $(get-flag mcpu) in
309                                 v9)
310                                         # We need to force at least v9a because the base build doesn't
311                                         # work with just v9.
312                                         # https://sourceware.org/bugzilla/show_bug.cgi?id=19477
313                                         append-flags "-Wa,-xarch=v9a"
314                                         ;;
315                                 esac
316                                 ;;
317                         sparc-*)
318                                 case $(get-flag mcpu) in
319                                 v8|supersparc|hypersparc|leon|leon3)
320                                         cpu="sparcv8"
321                                         ;;
322                                 *)
323                                         cpu="sparcv9"
324                                         ;;
325                                 esac
326                         ;;
327                         esac
328                         [[ -n ${cpu} ]] && CTARGET_OPT="${cpu}-${CTARGET#*-}"
329                 ;;
330         esac
331 }
332
333 setup_flags() {
334         # Make sure host make.conf doesn't pollute us
335         if is_crosscompile || tc-is-cross-compiler ; then
336                 CHOST=${CTARGET} strip-unsupported-flags
337         fi
338
339         # Store our CFLAGS because it's changed depending on which CTARGET
340         # we are building when pulling glibc on a multilib profile
341         CFLAGS_BASE=${CFLAGS_BASE-${CFLAGS}}
342         CFLAGS=${CFLAGS_BASE}
343         CXXFLAGS_BASE=${CXXFLAGS_BASE-${CXXFLAGS}}
344         CXXFLAGS=${CXXFLAGS_BASE}
345         ASFLAGS_BASE=${ASFLAGS_BASE-${ASFLAGS}}
346         ASFLAGS=${ASFLAGS_BASE}
347
348         # Allow users to explicitly avoid flag sanitization via
349         # USE=custom-cflags.
350         if ! use custom-cflags; then
351                 # Over-zealous CFLAGS can often cause problems.  What may work for one
352                 # person may not work for another.  To avoid a large influx of bugs
353                 # relating to failed builds, we strip most CFLAGS out to ensure as few
354                 # problems as possible.
355                 strip-flags
356                 # Lock glibc at -O2; we want to be conservative here.
357                 filter-flags '-O?'
358                 append-flags -O2
359         fi
360         strip-unsupported-flags
361         filter-flags -m32 -m64 '-mabi=*'
362
363         # glibc aborts if rpath is set by LDFLAGS
364         filter-ldflags '-Wl,-rpath=*'
365
366         # #492892
367         filter-flags -frecord-gcc-switches
368
369         unset CBUILD_OPT CTARGET_OPT
370         if use multilib ; then
371                 CTARGET_OPT=$(get_abi_CTARGET)
372                 [[ -z ${CTARGET_OPT} ]] && CTARGET_OPT=$(get_abi_CHOST)
373         fi
374
375         setup_target_flags
376
377         if [[ -n ${CTARGET_OPT} && ${CBUILD} == ${CHOST} ]] && ! is_crosscompile; then
378                 CBUILD_OPT=${CTARGET_OPT}
379         fi
380
381         # glibc's headers disallow -O0 and fail at build time:
382         #  include/libc-symbols.h:75:3: #error "glibc cannot be compiled without optimization"
383         replace-flags -O0 -O1
384
385         filter-flags '-fstack-protector*'
386 }
387
388 want_tls() {
389         # Archs that can use TLS (Thread Local Storage)
390         case $(tc-arch) in
391                 x86)
392                         # requires i486 or better #106556
393                         [[ ${CTARGET} == i[4567]86* ]] && return 0
394                         return 1
395                 ;;
396         esac
397         return 0
398 }
399
400 want__thread() {
401         want_tls || return 1
402
403         # For some reason --with-tls --with__thread is causing segfaults on sparc32.
404         [[ ${PROFILE_ARCH} == "sparc" ]] && return 1
405
406         [[ -n ${WANT__THREAD} ]] && return ${WANT__THREAD}
407
408         # only test gcc -- can't test linking yet
409         tc-has-tls -c ${CTARGET}
410         WANT__THREAD=$?
411
412         return ${WANT__THREAD}
413 }
414
415 use_multiarch() {
416         # Allow user to disable runtime arch detection in multilib.
417         use multiarch || return 1
418         # Make sure binutils is new enough to support indirect functions,
419         # #336792. This funky sed supports gold and bfd linkers.
420         local bver nver
421         bver=$($(tc-getLD ${CTARGET}) -v | sed -n -r '1{s:[^0-9]*::;s:^([0-9.]*).*:\1:;p}')
422         case $(tc-arch ${CTARGET}) in
423         amd64|x86) nver="2.20" ;;
424         arm)       nver="2.22" ;;
425         hppa)      nver="2.23" ;;
426         ppc|ppc64) nver="2.20" ;;
427         # ifunc support was added in 2.23, but glibc also needs
428         # machinemode which is in 2.24.
429         s390)      nver="2.24" ;;
430         sparc)     nver="2.21" ;;
431         *)         return 1 ;;
432         esac
433         ver_test ${bver} -ge ${nver}
434 }
435
436 # Setup toolchain variables that had historically been defined in the
437 # profiles for these archs.
438 setup_env() {
439         # silly users
440         unset LD_RUN_PATH
441         unset LD_ASSUME_KERNEL
442
443         if is_crosscompile || tc-is-cross-compiler ; then
444                 multilib_env ${CTARGET_OPT:-${CTARGET}}
445
446                 if ! use multilib ; then
447                         MULTILIB_ABIS=${DEFAULT_ABI}
448                 else
449                         MULTILIB_ABIS=${MULTILIB_ABIS:-${DEFAULT_ABI}}
450                 fi
451
452                 # If the user has CFLAGS_<CTARGET> in their make.conf, use that,
453                 # and fall back on CFLAGS.
454                 local VAR=CFLAGS_${CTARGET//[-.]/_}
455                 CFLAGS=${!VAR-${CFLAGS}}
456                 einfo " $(printf '%15s' 'Manual CFLAGS:')   ${CFLAGS}"
457         fi
458
459         setup_flags
460
461         export ABI=${ABI:-${DEFAULT_ABI:-default}}
462
463         if just_headers ; then
464                 # Avoid mixing host's CC and target's CFLAGS_${ABI}:
465                 # At this bootstrap stage we have only binutils for
466                 # target but not compiler yet.
467                 einfo "Skip CC ABI injection. We can't use (cross-)compiler yet."
468                 return 0
469         fi
470         local VAR=CFLAGS_${ABI}
471         # We need to export CFLAGS with abi information in them because glibc's
472         # configure script checks CFLAGS for some targets (like mips).  Keep
473         # around the original clean value to avoid appending multiple ABIs on
474         # top of each other.
475         : ${__GLIBC_CC:=$(tc-getCC ${CTARGET_OPT:-${CTARGET}})}
476         export __GLIBC_CC CC="${__GLIBC_CC} ${!VAR}"
477         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
478 }
479
480 foreach_abi() {
481         setup_env
482
483         local ret=0
484         local abilist=""
485         if use multilib ; then
486                 abilist=$(get_install_abis)
487         else
488                 abilist=${DEFAULT_ABI}
489         fi
490         local -x ABI
491         for ABI in ${abilist:-default} ; do
492                 setup_env
493                 einfo "Running $1 for ABI ${ABI}"
494                 $1
495                 : $(( ret |= $? ))
496         done
497         return ${ret}
498 }
499
500 glibc_banner() {
501         local b="Gentoo ${PVR}"
502         [[ -n ${PATCH_VER} ]] && ! use vanilla && b+=" p${PATCH_VER}"
503         echo "${b}"
504 }
505
506 check_devpts() {
507         # Make sure devpts is mounted correctly for use w/out setuid pt_chown.
508
509         # If merely building the binary package, then there's nothing to verify.
510         [[ ${MERGE_TYPE} == "buildonly" ]] && return
511
512         # Only sanity check when installing the native glibc.
513         [[ -n ${ROOT} ]] && return
514
515         # If they're opting in to the old suid code, then no need to check.
516         use suid && return
517
518         if awk '$3 == "devpts" && $4 ~ /[, ]gid=5[, ]/ { exit 1 }' /proc/mounts ; then
519                 eerror "In order to use glibc with USE=-suid, you must make sure that"
520                 eerror "you have devpts mounted at /dev/pts with the gid=5 option."
521                 eerror "Openrc should do this for you, so you should check /etc/fstab"
522                 eerror "and make sure you do not have any invalid settings there."
523                 die "mount & fix your /dev/pts settings"
524         fi
525 }
526
527 # The following Kernel version handling functions are mostly copied from portage
528 # source. It's better not to use linux-info.eclass here since a) it adds too
529 # much magic, see bug 326693 for some of the arguments, and b) some of the
530 # functions are just not provided.
531
532 g_get_running_KV() {
533         uname -r
534         return $?
535 }
536
537 g_KV_major() {
538         [[ -z $1 ]] && return 1
539         local KV=$@
540         echo "${KV%%.*}"
541 }
542
543 g_KV_minor() {
544         [[ -z $1 ]] && return 1
545         local KV=$@
546         KV=${KV#*.}
547         echo "${KV%%.*}"
548 }
549
550 g_KV_micro() {
551         [[ -z $1 ]] && return 1
552         local KV=$@
553         KV=${KV#*.*.}
554         echo "${KV%%[^[:digit:]]*}"
555 }
556
557 g_KV_to_int() {
558         [[ -z $1 ]] && return 1
559         local KV_MAJOR=$(g_KV_major "$1")
560         local KV_MINOR=$(g_KV_minor "$1")
561         local KV_MICRO=$(g_KV_micro "$1")
562         local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
563
564         # We make version 2.2.0 the minimum version we will handle as
565         # a sanity check ... if its less, we fail ...
566         if [[ ${KV_int} -ge 131584 ]] ; then
567                 echo "${KV_int}"
568                 return 0
569         fi
570         return 1
571 }
572
573 g_int_to_KV() {
574         local version=$1 major minor micro
575         major=$((version / 65536))
576         minor=$(((version % 65536) / 256))
577         micro=$((version % 256))
578         echo ${major}.${minor}.${micro}
579 }
580
581 eend_KV() {
582         [[ $(g_KV_to_int $1) -ge $(g_KV_to_int $2) ]]
583         eend $?
584 }
585
586 get_kheader_version() {
587         printf '#include <linux/version.h>\nLINUX_VERSION_CODE\n' | \
588         $(tc-getCPP ${CTARGET}) -I "$(build_eprefix)$(alt_build_headers)" - | \
589         tail -n 1
590 }
591
592 # We collect all sanity checks here. Consistency is not guranteed between
593 # pkg_ and src_ phases, so we call this function both in pkg_pretend and in
594 # src_unpack.
595 sanity_prechecks() {
596         # Make sure devpts is mounted correctly for use w/out setuid pt_chown
597         check_devpts
598
599         # Prevent native builds from downgrading
600         if [[ ${MERGE_TYPE} != "buildonly" ]] && \
601            [[ -z ${ROOT} ]] && \
602            [[ ${CBUILD} == ${CHOST} ]] && \
603            [[ ${CHOST} == ${CTARGET} ]] ; then
604
605                 # The high rev # is to allow people to downgrade between -r#
606                 # versions. We want to block 2.20->2.19, but 2.20-r3->2.20-r2
607                 # should be fine. Hopefully we never actually use a r# this
608                 # high.
609                 if has_version ">${CATEGORY}/${P}-r10000" ; then
610                         eerror "Sanity check to keep you from breaking your system:"
611                         eerror " Downgrading glibc is not supported and a sure way to destruction."
612                         [[ ${I_ALLOW_TO_BREAK_MY_SYSTEM} = yes ]] || die "Aborting to save your system."
613                 fi
614
615                 if ! do_run_test '#include <unistd.h>\n#include <sys/syscall.h>\nint main(){return syscall(1000)!=-1;}\n' ; then
616                         eerror "Your old kernel is broken. You need to update it to a newer"
617                         eerror "version as syscall(<bignum>) will break. See bug 279260."
618                         die "Old and broken kernel."
619                 fi
620         fi
621
622         # Users have had a chance to phase themselves, time to give em the boot
623         if [[ -e ${EROOT}/etc/locale.gen ]] && [[ -e ${EROOT}/etc/locales.build ]] ; then
624                 eerror "You still haven't deleted ${EROOT}/etc/locales.build."
625                 eerror "Do so now after making sure ${EROOT}/etc/locale.gen is kosher."
626                 die "Lazy upgrader detected"
627         fi
628
629         if [[ ${CTARGET} == i386-* ]] ; then
630                 eerror "i386 CHOSTs are no longer supported."
631                 eerror "Chances are you don't actually want/need i386."
632                 eerror "Please read https://www.gentoo.org/doc/en/change-chost.xml"
633                 die "Please fix your CHOST"
634         fi
635
636         if [[ -e /proc/xen ]] && [[ $(tc-arch) == "x86" ]] && ! is-flag -mno-tls-direct-seg-refs ; then
637                 ewarn "You are using Xen but don't have -mno-tls-direct-seg-refs in your CFLAGS."
638                 ewarn "This will result in a 50% performance penalty when running with a 32bit"
639                 ewarn "hypervisor, which is probably not what you want."
640         fi
641
642         # Check for sanity of /etc/nsswitch.conf
643         if [[ -e ${EROOT}/etc/nsswitch.conf ]] ; then
644                 local entry
645                 for entry in passwd group shadow; do
646                         if ! egrep -q "^[ \t]*${entry}:.*files" "${EROOT}"/etc/nsswitch.conf; then
647                                 eerror "Your ${EROOT}/etc/nsswitch.conf is out of date."
648                                 eerror "Please make sure you have 'files' entries for"
649                                 eerror "'passwd:', 'group:' and 'shadow:' databases."
650                                 eerror "For more details see:"
651                                 eerror "  https://wiki.gentoo.org/wiki/Project:Toolchain/nsswitch.conf_in_glibc-2.26"
652                                 die "nsswitch.conf has no 'files' provider in '${entry}'."
653                         fi
654                 done
655         fi
656
657         # ABI-specific checks follow here. Hey, we have a lot more specific conditions that
658         # we test for...
659         if ! is_crosscompile ; then
660                 if use amd64 && use multilib && [[ ${MERGE_TYPE} != "binary" ]] ; then
661                         ebegin "Checking that IA32 emulation is enabled in the running kernel"
662                         echo 'int main(){return 0;}' > "${T}/check-ia32-emulation.c"
663                         local STAT
664                         if "${CC-${CHOST}-gcc}" ${CFLAGS_x86} "${T}/check-ia32-emulation.c" -o "${T}/check-ia32-emulation.elf32"; then
665                                 "${T}/check-ia32-emulation.elf32"
666                                 STAT=$?
667                         else
668                                 # Don't fail here to allow single->multi ABI switch
669                                 # or recover from breakage like bug #646424
670                                 ewarn "Failed to compile the ABI test. Broken host glibc?"
671                                 STAT=0
672                         fi
673                         rm -f "${T}/check-ia32-emulation.elf32"
674                         eend $STAT
675                         [[ $STAT -eq 0 ]] || die "CONFIG_IA32_EMULATION must be enabled in the kernel to compile a multilib glibc."
676                 fi
677
678         fi
679
680         # When we actually have to compile something...
681         if ! just_headers ; then
682                 ebegin "Checking gcc for __thread support"
683                 if ! eend $(want__thread ; echo $?) ; then
684                         echo
685                         eerror "Could not find a gcc that supports the __thread directive!"
686                         eerror "Please update your binutils/gcc and try again."
687                         die "No __thread support in gcc!"
688                 fi
689
690                 if [[ ${CTARGET} == *-linux* ]] ; then
691                         local run_kv build_kv want_kv
692
693                         run_kv=$(g_get_running_KV)
694                         build_kv=$(g_int_to_KV $(get_kheader_version))
695                         want_kv=${MIN_KERN_VER}
696
697                         if ! is_crosscompile && ! tc-is-cross-compiler ; then
698                                 # Building fails on an non-supporting kernel
699                                 ebegin "Checking running kernel version (${run_kv} >= ${want_kv})"
700                                 if ! eend_KV ${run_kv} ${want_kv} ; then
701                                         echo
702                                         eerror "You need a kernel of at least ${want_kv}!"
703                                         die "Kernel version too low!"
704                                 fi
705                         fi
706
707                         ebegin "Checking linux-headers version (${build_kv} >= ${want_kv})"
708                         if ! eend_KV ${build_kv} ${want_kv} ; then
709                                 echo
710                                 eerror "You need linux-headers of at least ${want_kv}!"
711                                 die "linux-headers version too low!"
712                         fi
713                 fi
714         fi
715 }
716
717 #
718 # the phases
719 #
720
721 # pkg_pretend
722
723 pkg_pretend() {
724         # All the checks...
725         einfo "Checking general environment sanity."
726         sanity_prechecks
727 }
728
729 pkg_setup() {
730         # see bug 682570
731         [[ -z ${BOOTSTRAP_RAP} ]] && python-any-r1_pkg_setup
732 }
733
734 # src_unpack
735
736 src_unpack() {
737         # Consistency is not guaranteed between pkg_ and src_ ...
738         sanity_prechecks
739
740         use multilib && unpack gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz
741
742         setup_env
743
744         if [[ -n ${EGIT_REPO_URI} ]] ; then
745                 git-r3_src_unpack
746         else
747                 unpack ${P}.tar.xz
748         fi
749
750         cd "${WORKDIR}" || die
751         unpack glibc-${RELEASE_VER}-patches-${PATCH_VER}.tar.xz
752 }
753
754 src_prepare() {
755         if ! use vanilla ; then
756                 elog "Applying Gentoo Glibc Patchset ${RELEASE_VER}-${PATCH_VER}"
757                 eapply "${WORKDIR}"/patches
758                 einfo "Done."
759         fi
760
761         default
762
763         gnuconfig_update
764
765         cd "${WORKDIR}"
766         find . -name configure -exec touch {} +
767
768         eprefixify extra/locale/locale-gen
769
770         # Fix permissions on some of the scripts.
771         chmod u+x "${S}"/scripts/*.sh
772
773         cd "${S}"
774 }
775
776 glibc_do_configure() {
777         # Glibc does not work with gold (for various reasons) #269274.
778         tc-ld-disable-gold
779
780         # CXX isnt handled by the multilib system, so if we dont unset here
781         # we accumulate crap across abis
782         unset CXX
783
784         einfo "Configuring glibc for nptl"
785
786         if use doc ; then
787                 export MAKEINFO=makeinfo
788         else
789                 export MAKEINFO=/dev/null
790         fi
791
792         local v
793         for v in ABI CBUILD CHOST CTARGET CBUILD_OPT CTARGET_OPT CC CXX LD {AS,C,CPP,CXX,LD}FLAGS MAKEINFO ; do
794                 einfo " $(printf '%15s' ${v}:)   ${!v}"
795         done
796
797         # CFLAGS can contain ABI-specific flags like -mfpu=neon, see bug #657760
798         # To build .S (assembly) files with the same ABI-specific flags
799         # upstream currently recommends adding CFLAGS to CC/CXX:
800         #    https://sourceware.org/PR23273
801         # Note: Passing CFLAGS via CPPFLAGS overrides glibc's arch-specific CFLAGS
802         # and breaks multiarch support. See 659030#c3 for an example.
803         # The glibc configure script doesn't properly use LDFLAGS all the time.
804         export CC="$(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS}"
805         einfo " $(printf '%15s' 'Manual CC:')   ${CC}"
806
807         # Some of the tests are written in C++, so we need to force our multlib abis in, bug 623548
808         export CXX="$(tc-getCXX ${CTARGET}) $(get_abi_CFLAGS) ${CFLAGS}"
809
810         if is_crosscompile; then
811                 # Assume worst-case bootstrap: glibc is buil first time
812                 # when ${CTARGET}-g++ is not available yet. We avoid
813                 # building auxiliary programs that require C++: bug #683074
814                 # It should not affect final result.
815                 export libc_cv_cxx_link_ok=no
816                 # The line above has the same effect. We set CXX explicitly
817                 # to make build logs less confusing.
818                 export CXX=
819         fi
820         einfo " $(printf '%15s' 'Manual CXX:')   ${CXX}"
821
822         echo
823
824         local myconf=()
825
826         case ${CTARGET} in
827                 m68k*)
828                         # setjmp() is not compatible with stack protection:
829                         # https://sourceware.org/PR24202
830                         myconf+=( --enable-stack-protector=no )
831                         ;;
832                 powerpc-*)
833                         # Currently gcc on powerpc32 generates invalid code for
834                         # __builtin_return_address(0) calls. Normally programs
835                         # don't do that but malloc hooks in glibc do:
836                         # https://gcc.gnu.org/PR81996
837                         # https://bugs.gentoo.org/629054
838                         myconf+=( --enable-stack-protector=no )
839                         ;;
840                 *)
841                         # Use '=strong' instead of '=all' to protect only functions
842                         # worth protecting from stack smashes.
843                         # '=all' is also known to have a problem in IFUNC resolution
844                         # tests: https://sourceware.org/PR25680, bug #712356.
845                         myconf+=( --enable-stack-protector=$(usex ssp strong no) )
846                         ;;
847         esac
848         myconf+=( --enable-stackguard-randomization )
849
850         # Keep a whitelist of targets supporing IFUNC. glibc's ./configure
851         # is not robust enough to detect proper support:
852         #    https://bugs.gentoo.org/641216
853         #    https://sourceware.org/PR22634#c0
854         case $(tc-arch ${CTARGET}) in
855                 # Keep whitelist of targets where autodetection mostly works.
856                 amd64|x86|sparc|ppc|ppc64|arm|arm64|s390) ;;
857                 # Blacklist everywhere else
858                 *) myconf+=( libc_cv_ld_gnu_indirect_function=no ) ;;
859         esac
860
861         # Enable Intel Control-flow Enforcement Technology on amd64 if requested
862         case ${CTARGET} in
863                 x86_64-*) myconf+=( $(use_enable cet) ) ;;
864                 *) ;;
865         esac
866
867         [[ $(tc-is-softfloat) == "yes" ]] && myconf+=( --without-fp )
868
869         myconf+=( --enable-kernel=${MIN_KERN_VER} )
870
871         # Since SELinux support is only required for nscd, only enable it if:
872         # 1. USE selinux
873         # 2. only for the primary ABI on multilib systems
874         # 3. Not a crosscompile
875         if ! is_crosscompile && use selinux ; then
876                 if use multilib ; then
877                         if is_final_abi ; then
878                                 myconf+=( --with-selinux )
879                         else
880                                 myconf+=( --without-selinux )
881                         fi
882                 else
883                         myconf+=( --with-selinux )
884                 fi
885         else
886                 myconf+=( --without-selinux )
887         fi
888
889         # Force a few tests where we always know the answer but
890         # configure is incapable of finding it.
891         if is_crosscompile ; then
892                 export \
893                         libc_cv_c_cleanup=yes \
894                         libc_cv_forced_unwind=yes
895         fi
896
897         myconf+=(
898                 --without-cvs
899                 --disable-werror
900                 --enable-bind-now
901                 --build=${CBUILD_OPT:-${CBUILD}}
902                 --host=${CTARGET_OPT:-${CTARGET}}
903                 $(use_enable profile)
904                 $(use_with gd)
905                 --with-headers=$(build_eprefix)$(alt_build_headers)
906                 --prefix="$(host_eprefix)/usr"
907                 --sysconfdir="$(host_eprefix)/etc"
908                 --localstatedir="$(host_eprefix)/var"
909                 --libdir='$(prefix)'/$(get_libdir)
910                 --mandir='$(prefix)'/share/man
911                 --infodir='$(prefix)'/share/info
912                 --libexecdir='$(libdir)'/misc/glibc
913                 --with-bugurl=https://bugs.gentoo.org/
914                 --with-pkgversion="$(glibc_banner)"
915                 $(use_enable crypt)
916                 $(use_multiarch || echo --disable-multi-arch)
917                 $(use_enable systemtap)
918                 $(use_enable nscd)
919                 ${EXTRA_ECONF}
920         )
921
922         # We rely on sys-libs/timezone-data for timezone tools normally.
923         myconf+=( $(use_enable vanilla timezone-tools) )
924
925         # These libs don't have configure flags.
926         ac_cv_lib_audit_audit_log_user_avc_message=$(usex audit || echo no)
927         ac_cv_lib_cap_cap_init=$(usex caps || echo no)
928
929         # There is no configure option for this and we need to export it
930         # since the glibc build will re-run configure on itself
931         export libc_cv_rootsbindir="$(host_eprefix)/sbin"
932         export libc_cv_slibdir="$(host_eprefix)/$(get_libdir)"
933
934         # We take care of patching our binutils to use both hash styles,
935         # and many people like to force gnu hash style only, so disable
936         # this overriding check.  #347761
937         export libc_cv_hashstyle=no
938
939         local builddir=$(builddir nptl)
940         mkdir -p "${builddir}"
941         cd "${builddir}"
942         set -- "${S}"/configure "${myconf[@]}"
943         echo "$@"
944         "$@" || die "failed to configure glibc"
945
946         # ia64 static cross-compilers are a pita in so much that they
947         # can't produce static ELFs (as the libgcc.a is broken).  so
948         # disable building of the programs for those targets if it
949         # doesn't work.
950         # XXX: We could turn this into a compiler test, but ia64 is
951         # the only one that matters, so this should be fine for now.
952         if is_crosscompile && [[ ${CTARGET} == ia64* ]] ; then
953                 sed -i '1i+link-static = touch $@' config.make
954         fi
955
956         # If we're trying to migrate between ABI sets, we need
957         # to lie and use a local copy of gcc.  Like if the system
958         # is built with MULTILIB_ABIS="amd64 x86" but we want to
959         # add x32 to it, gcc/glibc don't yet support x32.
960         #
961         if [[ -n ${GCC_BOOTSTRAP_VER} ]] && use multilib ; then
962                 echo 'main(){}' > "${T}"/test.c
963                 if ! $(tc-getCC ${CTARGET}) ${CFLAGS} ${LDFLAGS} "${T}"/test.c -Wl,-emain -lgcc 2>/dev/null ; then
964                         sed -i -e '/^CC = /s:$: -B$(objdir)/../'"gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}/${ABI}:" config.make || die
965                 fi
966         fi
967 }
968
969 glibc_headers_configure() {
970         export ABI=default
971
972         local builddir=$(builddir "headers")
973         mkdir -p "${builddir}"
974         cd "${builddir}"
975
976         # if we don't have a compiler yet, we can't really test it now ...
977         # hopefully they don't affect header generation, so let's hope for
978         # the best here ...
979         local v vars=(
980                 ac_cv_header_cpuid_h=yes
981                 libc_cv_{386,390,alpha,arm,hppa,ia64,mips,{powerpc,sparc}{,32,64},sh,x86_64}_tls=yes
982                 libc_cv_asm_cfi_directives=yes
983                 libc_cv_broken_visibility_attribute=no
984                 libc_cv_c_cleanup=yes
985                 libc_cv_compiler_powerpc64le_binary128_ok=yes
986                 libc_cv_forced_unwind=yes
987                 libc_cv_gcc___thread=yes
988                 libc_cv_mlong_double_128=yes
989                 libc_cv_mlong_double_128ibm=yes
990                 libc_cv_ppc_machine=yes
991                 libc_cv_ppc_rel16=yes
992                 libc_cv_predef_fortify_source=no
993                 libc_cv_target_power8_ok=yes
994                 libc_cv_visibility_attribute=yes
995                 libc_cv_z_combreloc=yes
996                 libc_cv_z_execstack=yes
997                 libc_cv_z_initfirst=yes
998                 libc_cv_z_nodelete=yes
999                 libc_cv_z_nodlopen=yes
1000                 libc_cv_z_relro=yes
1001                 libc_mips_abi=${ABI}
1002                 libc_mips_float=$([[ $(tc-is-softfloat) == "yes" ]] && echo soft || echo hard)
1003                 # These libs don't have configure flags.
1004                 ac_cv_lib_audit_audit_log_user_avc_message=no
1005                 ac_cv_lib_cap_cap_init=no
1006         )
1007
1008         einfo "Forcing cached settings:"
1009         for v in "${vars[@]}" ; do
1010                 einfo " ${v}"
1011                 export ${v}
1012         done
1013
1014         local headers_only_arch_CPPFLAGS=()
1015
1016         # Blow away some random CC settings that screw things up. #550192
1017         if [[ -d ${S}/sysdeps/mips ]]; then
1018                 pushd "${S}"/sysdeps/mips >/dev/null
1019                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=32:' mips32/Makefile mips64/n32/Makefile || die
1020                 sed -i -e '/^CC +=/s:=.*:= -D_MIPS_SZPTR=64:' mips64/n64/Makefile || die
1021
1022                 # Force the mips ABI to the default.  This is OK because the set of
1023                 # installed headers in this phase is the same between the 3 ABIs.
1024                 # If this ever changes, this hack will break, but that's unlikely
1025                 # as glibc discourages that behavior.
1026                 # https://crbug.com/647033
1027                 sed -i -e 's:abiflag=.*:abiflag=_ABIO32:' preconfigure || die
1028
1029                 popd >/dev/null
1030         fi
1031
1032         case ${CTARGET} in
1033         riscv*)
1034                 # RISC-V interrogates the compiler to determine which target to
1035                 # build.  If building the headers then we don't strictly need a
1036                 # RISC-V compiler, so the built-in definitions that are provided
1037                 # along with all RISC-V compiler might not exist.  This causes
1038                 # glibc's RISC-V preconfigure script to blow up.  Since we're just
1039                 # building the headers any value will actually work here, so just
1040                 # pick the standard one (rv64g/lp64d) to make the build scripts
1041                 # happy for now -- the headers are all the same anyway so it
1042                 # doesn't matter.
1043                 headers_only_arch_CPPFLAGS+=(
1044                         -D__riscv_xlen=64
1045                         -D__riscv_flen=64
1046                         -D__riscv_float_abi_double=1
1047                         -D__riscv_atomic=1
1048                 ) ;;
1049         esac
1050
1051         local myconf=()
1052         myconf+=(
1053                 --disable-sanity-checks
1054                 --enable-hacker-mode
1055                 --without-cvs
1056                 --disable-werror
1057                 --enable-bind-now
1058                 --build=${CBUILD_OPT:-${CBUILD}}
1059                 --host=${CTARGET_OPT:-${CTARGET}}
1060                 --with-headers=$(build_eprefix)$(alt_build_headers)
1061                 --prefix="$(host_eprefix)/usr"
1062                 ${EXTRA_ECONF}
1063         )
1064
1065         # Nothing is compiled here which would affect the headers for the target.
1066         # So forcing CC/CFLAGS is sane.
1067         local headers_only_CC=$(tc-getBUILD_CC)
1068         local headers_only_CFLAGS="-O1 -pipe"
1069         local headers_only_CPPFLAGS="-U_FORTIFY_SOURCE ${headers_only_arch_CPPFLAGS[*]}"
1070         local headers_only_LDFLAGS=""
1071         set -- "${S}"/configure "${myconf[@]}"
1072         echo \
1073                 "CC=${headers_only_CC}" \
1074                 "CFLAGS=${headers_only_CFLAGS}" \
1075                 "CPPFLAGS=${headers_only_CPPFLAGS}" \
1076                 "LDFLAGS=${headers_only_LDFLAGS}" \
1077                 "$@"
1078         CC=${headers_only_CC} \
1079         CFLAGS=${headers_only_CFLAGS} \
1080         CPPFLAGS=${headers_only_CPPFLAGS} \
1081         LDFLAGS="" \
1082         "$@" || die "failed to configure glibc"
1083 }
1084
1085 do_src_configure() {
1086         if just_headers ; then
1087                 glibc_headers_configure
1088         else
1089                 glibc_do_configure nptl
1090         fi
1091 }
1092
1093 src_configure() {
1094         foreach_abi do_src_configure
1095 }
1096
1097 do_src_compile() {
1098         emake -C "$(builddir nptl)"
1099 }
1100
1101 src_compile() {
1102         if just_headers ; then
1103                 return
1104         fi
1105
1106         foreach_abi do_src_compile
1107 }
1108
1109 glibc_src_test() {
1110         cd "$(builddir nptl)"
1111
1112         local myxfailparams=""
1113         if [[ "${GENTOO_GLIBC_XFAIL_TESTS}" == "yes" ]] ; then
1114                 for myt in ${XFAIL_TEST_LIST[@]} ; do
1115                         myxfailparams+="test-xfail-${myt}=yes "
1116                 done
1117         fi
1118
1119         emake ${myxfailparams} check
1120 }
1121
1122 do_src_test() {
1123         local ret=0
1124
1125         glibc_src_test
1126         : $(( ret |= $? ))
1127
1128         return ${ret}
1129 }
1130
1131 src_test() {
1132         if just_headers ; then
1133                 return
1134         fi
1135
1136         # Give tests more time to complete.
1137         export TIMEOUTFACTOR=5
1138
1139         foreach_abi do_src_test || die "tests failed"
1140 }
1141
1142 run_locale_gen() {
1143         # if the host locales.gen contains no entries, we'll install everything
1144         local root="$1"
1145         local inplace=""
1146
1147         if [[ "${root}" == "--inplace-glibc" ]] ; then
1148                 inplace="--inplace-glibc"
1149                 root="$2"
1150         fi
1151
1152         local locale_list="${root}/etc/locale.gen"
1153
1154         pushd "${ED}"/$(get_libdir) >/dev/null
1155
1156         if [[ -z $(locale-gen --list --config "${locale_list}") ]] ; then
1157                 [[ -z ${inplace} ]] && ewarn "Generating all locales; edit /etc/locale.gen to save time/space"
1158                 locale_list="${root}/usr/share/i18n/SUPPORTED"
1159         fi
1160
1161         locale-gen ${inplace} --jobs $(makeopts_jobs) --config "${locale_list}" \
1162                 --destdir "${root}"
1163
1164         popd >/dev/null
1165 }
1166
1167 glibc_do_src_install() {
1168         local builddir=$(builddir nptl)
1169         cd "${builddir}"
1170
1171         emake install_root="${D}/$(build_eprefix)$(alt_prefix)" install
1172
1173         # This version (2.26) provides some compatibility libraries for the NIS/NIS+ support
1174         # which come without headers etc. Only needed for binary packages since the
1175         # external net-libs/libnsl has increased soversion. Keep only versioned libraries.
1176         find "${D}" -name "libnsl.a" -delete
1177         find "${D}" -name "libnsl.so" -delete
1178
1179         # Normally upstream_pv is ${PV}. Live ebuilds are exception, there we need
1180         # to infer upstream version:
1181         # '#define VERSION "2.26.90"' -> '2.26.90'
1182         local upstream_pv=$(sed -n -r 's/#define VERSION "(.*)"/\1/p' "${S}"/version.h)
1183
1184         # Avoid stripping binaries not targeted by ${CHOST}. Or else
1185         # ${CHOST}-strip would break binaries build for ${CTARGET}.
1186         is_crosscompile && dostrip -x /
1187         # gdb thread introspection relies on local libpthreas symbols. stripping breaks it
1188         # See Note [Disable automatic stripping]
1189         dostrip -x $(alt_libdir)/libpthread-${upstream_pv}.so
1190
1191         if [[ -e ${ED}/$(alt_usrlibdir)/libm-${upstream_pv}.a ]] ; then
1192                 # Move versioned .a file out of libdir to evade portage QA checks
1193                 # instead of using gen_usr_ldscript(). We fix ldscript as:
1194                 # "GROUP ( /usr/lib64/libm-<pv>.a ..." -> "GROUP ( /usr/lib64/glibc-<pv>/libm-<pv>.a ..."
1195                 sed -i "s@\(libm-${upstream_pv}.a\)@${P}/\1@" "${ED}"/$(alt_usrlibdir)/libm.a || die
1196                 dodir $(alt_usrlibdir)/${P}
1197                 mv "${ED}"/$(alt_usrlibdir)/libm-${upstream_pv}.a "${ED}"/$(alt_usrlibdir)/${P}/libm-${upstream_pv}.a || die
1198         fi
1199
1200         # We'll take care of the cache ourselves
1201         rm -f "${ED}"/etc/ld.so.cache
1202
1203         # Everything past this point just needs to be done once ...
1204         is_final_abi || return 0
1205
1206         # Make sure the non-native interp can be found on multilib systems even
1207         # if the main library set isn't installed into the right place.  Maybe
1208         # we should query the active gcc for info instead of hardcoding it ?
1209         local i ldso_abi ldso_name
1210         local ldso_abi_list=(
1211                 # x86
1212                 amd64   /lib64/ld-linux-x86-64.so.2
1213                 x32     /libx32/ld-linux-x32.so.2
1214                 x86     /lib/ld-linux.so.2
1215                 # mips
1216                 o32     /lib/ld.so.1
1217                 n32     /lib32/ld.so.1
1218                 n64     /lib64/ld.so.1
1219                 # powerpc
1220                 ppc     /lib/ld.so.1
1221                 ppc64   /lib64/ld64.so.1
1222                 # riscv
1223                 lp64d   /lib/ld-linux-riscv64-lp64d.so.1
1224                 lp64    /lib/ld-linux-riscv64-lp64.so.1
1225                 # s390
1226                 s390    /lib/ld.so.1
1227                 s390x   /lib/ld64.so.1
1228                 # sparc
1229                 sparc32 /lib/ld-linux.so.2
1230                 sparc64 /lib64/ld-linux.so.2
1231         )
1232         case $(tc-endian) in
1233         little)
1234                 ldso_abi_list+=(
1235                         # arm
1236                         arm64   /lib/ld-linux-aarch64.so.1
1237                 )
1238                 ;;
1239         big)
1240                 ldso_abi_list+=(
1241                         # arm
1242                         arm64   /lib/ld-linux-aarch64_be.so.1
1243                 )
1244                 ;;
1245         esac
1246         if [[ ${SYMLINK_LIB} == "yes" ]] && [[ ! -e ${ED}/$(alt_prefix)/lib ]] ; then
1247                 dosym $(get_abi_LIBDIR ${DEFAULT_ABI}) $(alt_prefix)/lib
1248         fi
1249         for (( i = 0; i < ${#ldso_abi_list[@]}; i += 2 )) ; do
1250                 ldso_abi=${ldso_abi_list[i]}
1251                 has ${ldso_abi} $(get_install_abis) || continue
1252
1253                 ldso_name="$(alt_prefix)${ldso_abi_list[i+1]}"
1254                 if [[ ! -L ${ED}/${ldso_name} && ! -e ${ED}/${ldso_name} ]] ; then
1255                         dosym ../$(get_abi_LIBDIR ${ldso_abi})/${ldso_name##*/} ${ldso_name}
1256                 fi
1257         done
1258
1259         # With devpts under Linux mounted properly, we do not need the pt_chown
1260         # binary to be setuid.  This is because the default owners/perms will be
1261         # exactly what we want.
1262         if ! use suid ; then
1263                 find "${ED}" -name pt_chown -exec chmod -s {} +
1264         fi
1265
1266         #################################################################
1267         # EVERYTHING AFTER THIS POINT IS FOR NATIVE GLIBC INSTALLS ONLY #
1268         # Make sure we install some symlink hacks so that when we build
1269         # a 2nd stage cross-compiler, gcc finds the target system
1270         # headers correctly.  See gcc/doc/gccinstall.info
1271         if is_crosscompile ; then
1272                 # We need to make sure that /lib and /usr/lib always exists.
1273                 # gcc likes to use relative paths to get to its multilibs like
1274                 # /usr/lib/../lib64/.  So while we don't install any files into
1275                 # /usr/lib/, we do need it to exist.
1276                 keepdir $(alt_prefix)/lib
1277                 keepdir $(alt_prefix)/usr/lib
1278
1279                 dosym usr/include $(alt_prefix)/sys-include
1280                 return 0
1281         fi
1282
1283         # Files for Debian-style locale updating
1284         dodir /usr/share/i18n
1285         sed \
1286                 -e "/^#/d" \
1287                 -e "/SUPPORTED-LOCALES=/d" \
1288                 -e "s: \\\\::g" -e "s:/: :g" \
1289                 "${S}"/localedata/SUPPORTED > "${ED}"/usr/share/i18n/SUPPORTED \
1290                 || die "generating /usr/share/i18n/SUPPORTED failed"
1291         cd "${WORKDIR}"/extra/locale
1292         dosbin locale-gen
1293         doman *.[0-8]
1294         insinto /etc
1295         doins locale.gen
1296
1297         # Make sure all the ABI's can find the locales and so we only
1298         # have to generate one set
1299         local a
1300         keepdir /usr/$(get_libdir)/locale
1301         for a in $(get_install_abis) ; do
1302                 if [[ ! -e ${ED}/usr/$(get_abi_LIBDIR ${a})/locale ]] ; then
1303                         dosym ../$(get_libdir)/locale /usr/$(get_abi_LIBDIR ${a})/locale
1304                 fi
1305         done
1306
1307         # HACK: If we're building for riscv, we need to additionally make sure that
1308         # we can find the locale archive afterwards
1309         case ${CTARGET} in
1310                 riscv*)
1311                         if [[ ! -e ${ED}/usr/lib/locale ]] ; then
1312                                 dosym ../$(get_libdir)/locale /usr/lib/locale
1313                         fi
1314                         ;;
1315                 *) ;;
1316         esac
1317
1318         cd "${S}"
1319
1320         # Install misc network config files
1321         insinto /etc
1322         doins nscd/nscd.conf posix/gai.conf nss/nsswitch.conf
1323         doins "${WORKDIR}"/extra/etc/*.conf
1324
1325         if use nscd ; then
1326                 doinitd "$(prefixify_ro "${WORKDIR}"/extra/etc/nscd)"
1327
1328                 local nscd_args=(
1329                         -e "s:@PIDFILE@:$(strings "${ED}"/usr/sbin/nscd | grep nscd.pid):"
1330                 )
1331
1332                 sed -i "${nscd_args[@]}" "${ED}"/etc/init.d/nscd
1333
1334                 systemd_dounit nscd/nscd.service
1335                 systemd_newtmpfilesd nscd/nscd.tmpfiles nscd.conf
1336         else
1337                 # Do this since extra/etc/*.conf above might have nscd.conf.
1338                 rm -f "${ED}"/etc/nscd.conf
1339         fi
1340
1341         echo 'LDPATH="include ld.so.conf.d/*.conf"' > "${T}"/00glibc
1342         doenvd "${T}"/00glibc
1343
1344         for d in BUGS ChangeLog CONFORMANCE FAQ NEWS NOTES PROJECTS README* ; do
1345                 [[ -s ${d} ]] && dodoc ${d}
1346         done
1347         dodoc -r ChangeLog.old
1348
1349         # Prevent overwriting of the /etc/localtime symlink.  We'll handle the
1350         # creation of the "factory" symlink in pkg_postinst().
1351         rm -f "${ED}"/etc/localtime
1352
1353         # Generate all locales if this is a native build as locale generation
1354         if use compile-locales && ! is_crosscompile ; then
1355                 run_locale_gen --inplace-glibc "${ED}/"
1356                 sed -e 's:COMPILED_LOCALES="":COMPILED_LOCALES="1":' -i "${ED}"/usr/sbin/locale-gen || die
1357         fi
1358 }
1359
1360 glibc_headers_install() {
1361         local builddir=$(builddir "headers")
1362         cd "${builddir}"
1363         emake install_root="${D}/$(build_eprefix)$(alt_prefix)" install-headers
1364
1365         insinto $(alt_headers)/gnu
1366         doins "${S}"/include/gnu/stubs.h
1367
1368         # Make sure we install the sys-include symlink so that when
1369         # we build a 2nd stage cross-compiler, gcc finds the target
1370         # system headers correctly.  See gcc/doc/gccinstall.info
1371         dosym usr/include $(alt_prefix)/sys-include
1372 }
1373
1374 src_install() {
1375         if just_headers ; then
1376                 export ABI=default
1377                 glibc_headers_install
1378                 return
1379         fi
1380
1381         foreach_abi glibc_do_src_install
1382
1383         if ! use static-libs ; then
1384                 elog "Not installing static glibc libraries"
1385                 find "${ED}" -name "*.a" -and -not -name "*_nonshared.a" -delete
1386         fi
1387 }
1388
1389 # Simple test to make sure our new glibc isn't completely broken.
1390 # Make sure we don't test with statically built binaries since
1391 # they will fail.  Also, skip if this glibc is a cross compiler.
1392 #
1393 # If coreutils is built with USE=multicall, some of these files
1394 # will just be wrapper scripts, not actual ELFs we can test.
1395 glibc_sanity_check() {
1396         cd / #228809
1397
1398         # We enter ${ED} so to avoid trouble if the path contains
1399         # special characters; for instance if the path contains the
1400         # colon character (:), then the linker will try to split it
1401         # and look for the libraries in an unexpected place. This can
1402         # lead to unsafe code execution if the generated prefix is
1403         # within a world-writable directory.
1404         # (e.g. /var/tmp/portage:${HOSTNAME})
1405         pushd "${ED}"/$(get_libdir) >/dev/null
1406
1407         local x striptest
1408         for x in cal date env free ls true uname uptime ; do
1409                 x=$(type -p ${x})
1410                 [[ -z ${x} || ${x} != ${EPREFIX}/* ]] && continue
1411                 striptest=$(LC_ALL="C" file -L ${x} 2>/dev/null) || continue
1412                 case ${striptest} in
1413                 *"statically linked"*) continue;;
1414                 *"ASCII text"*) continue;;
1415                 esac
1416                 # We need to clear the locale settings as the upgrade might want
1417                 # incompatible locale data.  This test is not for verifying that.
1418                 LC_ALL=C \
1419                 ./ld-*.so --library-path . ${x} > /dev/null \
1420                         || die "simple run test (${x}) failed"
1421         done
1422
1423         popd >/dev/null
1424 }
1425
1426 pkg_preinst() {
1427         # nothing to do if just installing headers
1428         just_headers && return
1429
1430         # prepare /etc/ld.so.conf.d/ for files
1431         mkdir -p "${EROOT}"/etc/ld.so.conf.d
1432
1433         # Default /etc/hosts.conf:multi to on for systems with small dbs.
1434         if [[ $(wc -l < "${EROOT}"/etc/hosts) -lt 1000 ]] ; then
1435                 sed -i '/^multi off/s:off:on:' "${ED}"/etc/host.conf
1436                 einfo "Defaulting /etc/host.conf:multi to on"
1437         fi
1438
1439         [[ -n ${ROOT} ]] && return 0
1440         [[ -d ${ED}/$(get_libdir) ]] || return 0
1441         [[ -z ${BOOTSTRAP_RAP} ]] && glibc_sanity_check
1442 }
1443
1444 pkg_postinst() {
1445         # nothing to do if just installing headers
1446         just_headers && return
1447
1448         if ! tc-is-cross-compiler && [[ -x ${EROOT}/usr/sbin/iconvconfig ]] ; then
1449                 # Generate fastloading iconv module configuration file.
1450                 "${EROOT}"/usr/sbin/iconvconfig --prefix="${ROOT}/"
1451         fi
1452
1453         if ! is_crosscompile && [[ -z ${ROOT} ]] ; then
1454                 use compile-locales || run_locale_gen "${EROOT}/"
1455         fi
1456
1457         # Check for sanity of /etc/nsswitch.conf, take 2
1458         if [[ -e ${EROOT}/etc/nsswitch.conf ]] && ! has_version sys-auth/libnss-nis ; then
1459                 local entry
1460                 for entry in passwd group shadow; do
1461                         if egrep -q "^[ \t]*${entry}:.*nis" "${EROOT}"/etc/nsswitch.conf; then
1462                                 ewarn ""
1463                                 ewarn "Your ${EROOT}/etc/nsswitch.conf uses NIS. Support for that has been"
1464                                 ewarn "removed from glibc and is now provided by the package"
1465                                 ewarn "  sys-auth/libnss-nis"
1466                                 ewarn "Install it now to keep your NIS setup working."
1467                                 ewarn ""
1468                         fi
1469                 done
1470         fi
1471 }