Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoPatchelfHook: add patchelfFlags option #256525

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions pkgs/applications/networking/browsers/firefox-bin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
, runtimeShell
, systemLocale ? config.i18n.defaultLocale or "en_US"
, patchelfUnstable # have to use patchelfUnstable to support --no-clobber-old-sections
, makeWrapper
}:

let
Expand Down Expand Up @@ -58,28 +57,14 @@ let
source = lib.findFirst (sourceMatches mozLocale) defaultSource sources;

pname = "firefox-${channel}-bin-unwrapped";

# FIXME: workaround for not being able to pass flags to patchelf
# Remove after https://github.com/NixOS/nixpkgs/pull/256525
wrappedPatchelf = stdenv.mkDerivation {
pname = "patchelf-wrapped";
inherit (patchelfUnstable) version;

nativeBuildInputs = [ makeWrapper ];

buildCommand = ''
mkdir -p $out/bin
makeWrapper ${patchelfUnstable}/bin/patchelf $out/bin/patchelf --append-flags "--no-clobber-old-sections"
'';
};
in

stdenv.mkDerivation {
inherit pname version;

src = fetchurl { inherit (source) url sha256; };

nativeBuildInputs = [ wrapGAppsHook autoPatchelfHook wrappedPatchelf ];
nativeBuildInputs = [ wrapGAppsHook autoPatchelfHook patchelfUnstable ];
buildInputs = [
gtk3
adwaita-icon-theme
Expand All @@ -95,6 +80,8 @@ stdenv.mkDerivation {
appendRunpaths = [
"${pipewire.lib}/lib"
];
# Firefox uses "relrhack" to manually process relocations from a fixed offset
patchelfFlags = [ "--no-clobber-old-sections" ];

installPhase =
''
Expand Down
20 changes: 14 additions & 6 deletions pkgs/build-support/setup-hooks/auto-patchelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Dependency:
found: bool = False # Whether it was found somewhere


def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: List[Path] = []) -> list[Dependency]:
def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: List[Path] = [], extra_args: List[str] = []) -> list[Dependency]:
try:
with open_elf(path) as elf:

Expand Down Expand Up @@ -213,7 +213,7 @@ def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: List
if file_is_dynamic_executable:
print("setting interpreter of", path)
subprocess.run(
["patchelf", "--set-interpreter", interpreter_path.as_posix(), path.as_posix()],
["patchelf", "--set-interpreter", interpreter_path.as_posix(), path.as_posix()] + extra_args,
check=True)
rpath += runtime_deps

Expand Down Expand Up @@ -250,7 +250,7 @@ def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: List
if rpath:
print("setting RPATH to:", rpath_str)
subprocess.run(
["patchelf", "--set-rpath", rpath_str, path.as_posix()],
["patchelf", "--set-rpath", rpath_str, path.as_posix()] + extra_args,
check=True)

return dependencies
Expand All @@ -262,7 +262,8 @@ def auto_patchelf(
runtime_deps: List[Path],
recursive: bool = True,
ignore_missing: List[str] = [],
append_rpaths: List[Path] = []) -> None:
append_rpaths: List[Path] = [],
extra_args: List[str] = []) -> None:

if not paths_to_patch:
sys.exit("No paths to patch, stopping.")
Expand All @@ -275,7 +276,7 @@ def auto_patchelf(
dependencies = []
for path in chain.from_iterable(glob(p, '*', recursive) for p in paths_to_patch):
if not path.is_symlink() and path.is_file():
dependencies += auto_patchelf_file(path, runtime_deps, append_rpaths)
dependencies += auto_patchelf_file(path, runtime_deps, append_rpaths, extra_args)

missing = [dep for dep in dependencies if not dep.found]

Expand Down Expand Up @@ -333,6 +334,12 @@ def main() -> None:
type=Path,
help="Paths to append to all runtime paths unconditionally",
)
parser.add_argument(
"--extra-args",
nargs="*",
type=str,
help="Extra arguments to pass to patchelf"
)

print("automatically fixing dependencies for ELF files")
args = parser.parse_args()
Expand All @@ -344,7 +351,8 @@ def main() -> None:
args.runtime_dependencies,
args.recursive,
args.ignore_missing,
append_rpaths=args.append_rpaths)
append_rpaths=args.append_rpaths,
extra_args=args.extra_args)


interpreter_path: Path = None # type: ignore
Expand Down
4 changes: 3 additions & 1 deletion pkgs/build-support/setup-hooks/auto-patchelf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ autoPatchelf() {

local appendRunpathsArray=($appendRunpaths)
local runtimeDependenciesArray=($runtimeDependencies)
local patchelfFlagsArray=($patchelfFlags)
@pythonInterpreter@ @autoPatchelfScript@ \
${norecurse:+--no-recurse} \
--ignore-missing "${ignoreMissingDepsArray[@]}" \
--paths "$@" \
--libs "${autoPatchelfLibs[@]}" \
"${extraAutoPatchelfLibs[@]}" \
--runtime-dependencies "${runtimeDependenciesArray[@]/%//lib}" \
--append-rpaths "${appendRunpathsArray[@]}"
--append-rpaths "${appendRunpathsArray[@]}" \
--extra-args "${patchelfFlagsArray[@]}"
}

# XXX: This should ultimately use fixupOutputHooks but we currently don't have
Expand Down