From c2409db926de4b7435e14e8366e8d08b0f69fd12 Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 16 Nov 2021 18:40:02 +0800 Subject: [PATCH 1/4] firefox: remove unnecessary make flags and LD_FLAGS Since we are using LLVM stdenv when enabling LTO, there are no need to manually specify them. --- .../networking/browsers/firefox/common.nix | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index 1c0832e32ca2..99a71ab86a88 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -174,10 +174,6 @@ buildStdenv.mkDerivation ({ AVFoundation MediaToolbox CoreLocation Foundation libobjc AddressBook cups ]; - NIX_LDFLAGS = lib.optionalString ltoSupport '' - -rpath ${llvmPackages.libunwind.out}/lib - ''; - MACH_USE_SYSTEM_PYTHON = "1"; postPatch = '' @@ -297,14 +293,7 @@ buildStdenv.mkDerivation ({ cd obj-* ''; - makeFlags = lib.optionals ltoSupport [ - "AR=${buildStdenv.cc.bintools.bintools}/bin/llvm-ar" - "LLVM_OBJDUMP=${buildStdenv.cc.bintools.bintools}/bin/llvm-objdump" - "NM=${buildStdenv.cc.bintools.bintools}/bin/llvm-nm" - "RANLIB=${buildStdenv.cc.bintools.bintools}/bin/llvm-ranlib" - "STRIP=${buildStdenv.cc.bintools.bintools}/bin/llvm-strip" - ] - ++ extraMakeFlags; + makeFlags = extraMakeFlags; enableParallelBuilding = true; doCheck = false; # "--disable-tests" above From 8d4bef7124cfab618d25113ef22d0852b0e33387 Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 16 Nov 2021 18:38:56 +0800 Subject: [PATCH 2/4] firefox: enable separated debug symbols --- .../networking/browsers/firefox/common.nix | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index 99a71ab86a88..41ed625779d4 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -71,6 +71,9 @@ # > the experience of Firefox users, you won't have any issues using the # > official branding. , enableOfficialBranding ? true + +# On 32bit platforms, we disable adding "-g" for easier linking. +, enableDebugSymbols ? !stdenv.is32bit }: assert stdenv.cc.libc or null != null; @@ -116,12 +119,6 @@ let }) else stdenv; - # --enable-release adds -ffunction-sections & LTO that require a big amount of - # RAM and the 32-bit memory space cannot handle that linking - # We also disable adding "-g" for easier linking - releaseFlags = if stdenv.is32bit - then [ "--disable-release" "--disable-debug-symbols" ] - else [ "--enable-release" ]; in buildStdenv.mkDerivation ({ @@ -202,6 +199,9 @@ buildStdenv.mkDerivation ({ ++ lib.optionals buildStdenv.isDarwin [ xcbuild rsync ] ++ extraNativeBuildInputs; + separateDebugInfo = enableDebugSymbols; + setOutputFlags = false; # `./mach configure` doesn't understand `--*dir=` flags. + preConfigure = '' # remove distributed configuration files rm -f configure @@ -225,7 +225,11 @@ buildStdenv.mkDerivation ({ ${lib.optionalString buildStdenv.cc.isClang "-idirafter ${buildStdenv.cc.cc.lib}/lib/clang/${lib.getVersion buildStdenv.cc.cc}/include"} \ ${lib.optionalString buildStdenv.cc.isGNU "-isystem ${lib.getDev buildStdenv.cc.cc}/include/c++/${lib.getVersion buildStdenv.cc.cc} -isystem ${buildStdenv.cc.cc}/include/c++/${lib.getVersion buildStdenv.cc.cc}/${buildStdenv.hostPlatform.config}"} \ $NIX_CFLAGS_COMPILE" - + ${ + # Bindgen doesn't like the flag added by `separateDebugInfo`. + lib.optionalString enableDebugSymbols '' + BINDGEN_CFLAGS="''${BINDGEN_CFLAGS/ -Wa,--compress-debug-sections/}" + ''} echo "ac_add_options BINDGEN_CFLAGS='$BINDGEN_CFLAGS'" >> $MOZCONFIG '' + (lib.optionalString googleAPISupport '' # Google API key used by Chromium and Firefox. @@ -283,9 +287,13 @@ buildStdenv.mkDerivation ({ ++ lib.optional drmSupport "--enable-eme=widevine" ++ (if debugBuild then [ "--enable-debug" "--enable-profiling" ] - else ([ "--disable-debug" - "--enable-optimize" - "--enable-strip" ] ++ releaseFlags)) + else [ "--disable-debug" "--enable-optimize" ]) + # --enable-release adds -ffunction-sections & LTO that require a big amount of + # RAM and the 32-bit memory space cannot handle that linking + ++ flag (!debugBuild && !stdenv.is32bit) "release" + ++ flag enableDebugSymbols "debug-symbols" + ++ lib.optionals enableDebugSymbols [ "--disable-strip" "--disable-install-strip" ] + ++ lib.optional enableOfficialBranding "--enable-official-branding" ++ extraConfigureFlags; @@ -311,6 +319,42 @@ buildStdenv.mkDerivation ({ gappsWrapperArgs+=(--argv0 "$out/bin/.${binaryName}-wrapped") ''; + # Workaround: The separateDebugInfo hook skips artifacts whose build ID's length is not 40. + # But we got 16-length build ID here. The function body is mainly copied from pkgs/build-support/setup-hooks/separate-debug-info.sh + # Remove it when PR #146275 is merged. + preFixup = lib.optionalString enableDebugSymbols '' + _separateDebugInfo() { + [ -e "$prefix" ] || return 0 + + local dst="''${debug:-$out}" + if [ "$prefix" = "$dst" ]; then return 0; fi + + dst="$dst/lib/debug/.build-id" + + # Find executables and dynamic libraries. + local i + while IFS= read -r -d $'\0' i; do + if ! isELF "$i"; then continue; fi + + # Extract the Build ID. FIXME: there's probably a cleaner way. + local id="$($READELF -n "$i" | sed 's/.*Build ID: \([0-9a-f]*\).*/\1/; t; d')" + if [[ -z "$id" ]]; then + echo "could not find build ID of $i, skipping" >&2 + continue + fi + + # Extract the debug info. + header "separating debug info from $i (build ID $id)" + mkdir -p "$dst/''${id:0:2}" + $OBJCOPY --only-keep-debug "$i" "$dst/''${id:0:2}/''${id:2}.debug" + $STRIP --strip-debug "$i" + + # Also a create a symlink .debug. + ln -sfn ".build-id/''${id:0:2}/''${id:2}.debug" "$dst/../$(basename "$i")" + done < <(find "$prefix" -type f -print0) + } + ''; + doInstallCheck = true; installCheckPhase = '' # Some basic testing From a343380d9dca9305951fe4eca8b58745b4bb3f97 Mon Sep 17 00:00:00 2001 From: oxalica Date: Fri, 19 Nov 2021 23:44:10 +0800 Subject: [PATCH 3/4] firefox: enable cross-language LTO --- pkgs/applications/networking/browsers/firefox/common.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index 41ed625779d4..0ac5977653ea 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -273,7 +273,7 @@ buildStdenv.mkDerivation ({ # https://bugzilla.mozilla.org/show_bug.cgi?id=1538724 # elf-hack is broken when using clang+lld: # https://bugzilla.mozilla.org/show_bug.cgi?id=1482204 - ++ lib.optional ltoSupport "--enable-lto" + ++ lib.optional ltoSupport "--enable-lto=cross" # Cross-language LTO. ++ lib.optional (ltoSupport && (buildStdenv.isAarch32 || buildStdenv.isi686 || buildStdenv.isx86_64)) "--disable-elf-hack" ++ lib.optional (ltoSupport && !buildStdenv.isDarwin) "--enable-linker=lld" From 8a2be27251d9a36c7cbdb684d97c07e481d2c5f4 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 21 Nov 2021 02:29:37 +0800 Subject: [PATCH 4/4] firefox: disable jemalloc by default to fix crash --- pkgs/applications/networking/browsers/firefox/common.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index 0ac5977653ea..b0d11bd835b6 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -27,7 +27,9 @@ , ltoSupport ? (stdenv.isLinux && stdenv.is64bit), overrideCC, buildPackages , gssSupport ? true, libkrb5 , pipewireSupport ? waylandSupport && webrtcSupport, pipewire -, jemallocSupport ? true, jemalloc +# Workaround: disabled since currently jemalloc causes crashes with LLVM 13. +# https://bugzilla.mozilla.org/show_bug.cgi?id=1741454 +, jemallocSupport ? false, jemalloc ## privacy-related options