1{
2 lib,
3 stdenv,
4 callPackage,
5 withLinuxHeaders ? true,
6 profilingLibraries ? false,
7 withGd ? false,
8 enableCET ? if stdenv.hostPlatform.isx86_64 then "permissive" else false,
9 enableCETRuntimeDefault ? false,
10 pkgsBuildBuild,
11 libgcc,
12}:
13
14let
15 gdCflags = [
16 "-Wno-error=stringop-truncation"
17 "-Wno-error=missing-attributes"
18 "-Wno-error=array-bounds"
19 ];
20in
21
22(callPackage ./common.nix { inherit stdenv; } {
23 inherit
24 withLinuxHeaders
25 withGd
26 profilingLibraries
27 enableCET
28 enableCETRuntimeDefault
29 ;
30 pname =
31 "glibc"
32 + lib.optionalString withGd "-gd"
33 + lib.optionalString (stdenv.cc.isGNU && libgcc == null) "-nolibgcc";
34}).overrideAttrs
35 (previousAttrs: {
36
37 # Note:
38 # Things you write here override, and do not add to,
39 # the values in `common.nix`.
40 # (For example, if you define `patches = [...]` here, it will
41 # override the patches in `common.nix` -- so instead you should
42 # write `patches = (previousAttrs.patches or []) ++ [ ... ]`.
43
44 NIX_NO_SELF_RPATH = true;
45
46 postConfigure = ''
47 # Hack: get rid of the `-static' flag set by the bootstrap stdenv.
48 # This has to be done *after* `configure' because it builds some
49 # test binaries.
50 export NIX_CFLAGS_LINK=
51 export NIX_LDFLAGS_BEFORE=
52
53 export NIX_DONT_SET_RPATH=1
54 unset CFLAGS
55
56 # Apparently --bindir is not respected.
57 makeFlagsArray+=("bindir=$bin/bin" "sbindir=$bin/sbin" "rootsbindir=$bin/sbin")
58 '';
59
60 # The pie, stackprotector and fortify hardening flags are autodetected by
61 # glibc and enabled by default if supported. Setting it for every gcc
62 # invocation does not work.
63 hardeningDisable = [
64 "fortify"
65 "pie"
66 "stackprotector"
67 ];
68
69 env = (previousAttrs.env or { }) // {
70 NIX_CFLAGS_COMPILE =
71 (previousAttrs.env.NIX_CFLAGS_COMPILE or "")
72 + lib.concatStringsSep " " (
73 builtins.concatLists [
74 (lib.optionals withGd gdCflags)
75 # Fix -Werror build failure when building glibc with musl with GCC >= 8, see:
76 # https://github.com/NixOS/nixpkgs/pull/68244#issuecomment-544307798
77 (lib.optional stdenv.hostPlatform.isMusl "-Wno-error=attribute-alias")
78 (lib.optionals ((stdenv.hostPlatform != stdenv.buildPlatform) || stdenv.hostPlatform.isMusl) [
79 # Ignore "error: '__EI___errno_location' specifies less restrictive attributes than its target '__errno_location'"
80 # New warning as of GCC 9
81 # Same for musl: https://github.com/NixOS/nixpkgs/issues/78805
82 "-Wno-error=missing-attributes"
83 ])
84 (lib.optionals (stdenv.hostPlatform.isPower64) [
85 # Do not complain about the Processor Specific ABI (i.e. the
86 # choice to use IEEE-standard `long double`). We pass this
87 # flag in order to mute a `-Werror=psabi` passed by glibc;
88 # hopefully future glibc releases will not pass that flag.
89 "-Wno-error=psabi"
90 ])
91 ]
92 );
93 };
94
95 # glibc needs to `dlopen()` `libgcc_s.so` but does not link
96 # against it. Furthermore, glibc doesn't use the ordinary
97 # `dlopen()` call to do this; instead it uses one which ignores
98 # most paths:
99 #
100 # https://sourceware.org/legacy-ml/libc-help/2013-11/msg00026.html
101 #
102 # In order to get it to not ignore `libgcc_s.so`, we have to add its path to
103 # `user-defined-trusted-dirs`:
104 #
105 # https://sourceware.org/git/?p=glibc.git;a=blob;f=elf/Makefile;h=b509b3eada1fb77bf81e2a0ca5740b94ad185764#l1355
106 #
107 # Conveniently, this will also inform Nix of the fact that glibc depends on
108 # gcc.libgcc, since the path will be embedded in the resulting binary.
109 #
110 makeFlags =
111 (previousAttrs.makeFlags or [ ])
112 ++ lib.optionals (libgcc != null) [
113 "user-defined-trusted-dirs=${libgcc}/lib"
114 ];
115
116 postInstall =
117 previousAttrs.postInstall
118 + (
119 if stdenv.buildPlatform.canExecute stdenv.hostPlatform then
120 ''
121 echo SUPPORTED-LOCALES=C.UTF-8/UTF-8 > ../glibc-2*/localedata/SUPPORTED
122 # Don't install C.utf-8 into the archive, but into $out/lib/locale: on non-NixOS
123 # systems with an empty /usr/lib/locale/locale-archive, glibc would fall back to
124 # $libdir/locale/C.utf-8 instead of the locale archive of pkgs.glibc. See also #347965.
125 make -j''${NIX_BUILD_CORES:-1} localedata/install-locale-files
126 ''
127 else
128 lib.optionalString stdenv.buildPlatform.isLinux
129 # This is based on http://www.linuxfromscratch.org/lfs/view/development/chapter06/glibc.html
130 # Instead of using their patch to build a build-native localedef,
131 # we simply use the one from pkgsBuildBuild.
132 #
133 # Note that we can't use pkgsBuildHost (aka buildPackages) here, because
134 # that will cause an eval-time infinite recursion: "buildPackages.glibc
135 # depended on buildPackages.libgcc, which, since it's GCC, depends on the
136 # target's bintools, which depend on the target's glibc, which, again,
137 # depends on buildPackages.glibc, causing an infinute recursion when
138 # evaluating buildPackages.glibc when glibc hasn't come from stdenv
139 # (e.g. on musl)." https://github.com/NixOS/nixpkgs/pull/259964
140 ''
141 pushd ../glibc-2*/localedata
142 export I18NPATH=$PWD GCONV_PATH=$PWD/../iconvdata
143 mkdir -p $NIX_BUILD_TOP/${pkgsBuildBuild.glibc}/lib/locale
144 ${lib.getBin pkgsBuildBuild.glibc}/bin/localedef \
145 --alias-file=../intl/locale.alias \
146 -i locales/C \
147 -f charmaps/UTF-8 \
148 --prefix $NIX_BUILD_TOP \
149 ${
150 if stdenv.hostPlatform.parsed.cpu.significantByte.name == "littleEndian" then
151 "--little-endian"
152 else
153 "--big-endian"
154 } \
155 C.UTF-8
156 cp -r $NIX_BUILD_TOP/${pkgsBuildBuild.glibc}/lib/locale $out/lib
157 popd
158 ''
159 )
160 + ''
161
162 test -f $out/etc/ld.so.cache && rm $out/etc/ld.so.cache
163
164 if test -n "$linuxHeaders"; then
165 # Include the Linux kernel headers in Glibc, except the `scsi'
166 # subdirectory, which Glibc provides itself.
167 (cd $dev/include && \
168 ln -sv $(ls -d $linuxHeaders/include/* | grep -v scsi\$) .)
169 fi
170
171 # Fix for NIXOS-54 (ldd not working on x86_64). Make a symlink
172 # "lib64" to "lib".
173 if test -n "$is64bit"; then
174 ln -s lib $out/lib64
175 fi
176
177 # Get rid of more unnecessary stuff.
178 rm -rf $out/var $bin/bin/sln
179
180 # Backwards-compatibility to fix e.g.
181 # "configure: error: Pthreads are required to build libgomp" during `gcc`-build
182 # because it's not actually needed anymore to link against `pthreads` since
183 # it's now part of `libc.so.6` itself, but the gcc build breaks if
184 # this doesn't work.
185 ln -sf $out/lib/libpthread.so.0 $out/lib/libpthread.so
186 ln -sf $out/lib/librt.so.1 $out/lib/librt.so
187 ln -sf $out/lib/libdl.so.2 $out/lib/libdl.so
188 test -f $out/lib/libutil.so.1 && ln -sf $out/lib/libutil.so.1 $out/lib/libutil.so
189 touch $out/lib/libpthread.a
190
191 # Put libraries for static linking in a separate output. Note
192 # that libc_nonshared.a and libpthread_nonshared.a are required
193 # for dynamically-linked applications.
194 mkdir -p $static/lib
195 mv $out/lib/*.a $static/lib
196 mv $static/lib/lib*_nonshared.a $out/lib
197 # If libutil.so.1 is missing, libutil.a is required.
198 test -f $out/lib/libutil.so.1 || mv $static/lib/libutil.a $out/lib
199 # Some of *.a files are linker scripts where moving broke the paths.
200 sed "/^GROUP/s|$out/lib/lib|$static/lib/lib|g" \
201 -i "$static"/lib/*.a
202
203 # Work around a Nix bug: hard links across outputs cause a build failure.
204 cp $bin/bin/getconf $bin/bin/getconf_
205 mv $bin/bin/getconf_ $bin/bin/getconf
206 '';
207
208 separateDebugInfo = true;
209
210 passthru =
211 (previousAttrs.passthru or { })
212 // lib.optionalAttrs (libgcc != null) {
213 inherit libgcc;
214 };
215
216 meta = (previousAttrs.meta or { }) // {
217 description = "GNU C Library";
218 };
219 })