Skip to content

Commit

Permalink
autoPatchelfHook: add keep_libc flag
Browse files Browse the repository at this point in the history
- Add keep_libc flag to disable the default libc handling. Intended
  to be used by systemd.
- Add autoPatchelfFlags to autoPatchelfHook for passing arguments to
  the autoPatchelf script

This reverts part of the change made in NixOS#307068 / 80be926.

Fixes NixOS#332533
  • Loading branch information
squalus committed Aug 6, 2024
1 parent b772ff9 commit 747706d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkgs/build-support/setup-hooks/auto-patchelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,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] = [], extra_args: list[str] = []) -> list[Dependency]:
def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: list[Path] = [], keep_libc: bool = False, extra_args: list[str] = []) -> list[Dependency]:
try:
with open_elf(path) as elf:

Expand Down Expand Up @@ -267,16 +267,21 @@ def auto_patchelf_file(path: Path, runtime_deps: list[Path], append_rpaths: list
# add the dependency to rpath, as the original binary
# presumably had it and this should be preserved.

is_libc = (libc_lib / candidate).is_file()

if candidate.is_absolute() and candidate.is_file():
was_found = True
break
elif is_libc and not keep_libc:
was_found = True
break
elif found_dependency := find_dependency(candidate.name, file_arch, file_osabi):
rpath.append(found_dependency)
dependencies.append(Dependency(path, candidate, found=True))
print(f" {candidate} -> found: {found_dependency}")
was_found = True
break
elif (libc_lib / candidate).is_file():
elif is_libc and keep_libc:
was_found = True
break

Expand Down Expand Up @@ -306,6 +311,7 @@ def auto_patchelf(
recursive: bool = True,
ignore_missing: list[str] = [],
append_rpaths: list[Path] = [],
keep_libc: bool = False,
extra_args: list[str] = []) -> None:

if not paths_to_patch:
Expand All @@ -319,7 +325,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, extra_args)
dependencies += auto_patchelf_file(path, runtime_deps, append_rpaths, keep_libc, extra_args)

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

Expand Down Expand Up @@ -377,6 +383,12 @@ def main() -> None:
type=Path,
help="Paths to append to all runtime paths unconditionally",
)
parser.add_argument(
"--keep-libc",
dest="keep_libc",
action="store_true",
help="Leave libc in the rpath if it's found",
)
parser.add_argument(
"--extra-args",
# Undocumented Python argparse feature: consume all remaining arguments
Expand All @@ -398,6 +410,7 @@ def main() -> None:
args.recursive,
args.ignore_missing,
append_rpaths=args.append_rpaths,
keep_libc=args.keep_libc,
extra_args=args.extra_args)


Expand Down
3 changes: 3 additions & 0 deletions pkgs/build-support/setup-hooks/auto-patchelf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ autoPatchelf() {
local appendRunpathsArray=( "${appendRunpaths[@]}" )
local runtimeDependenciesArray=( "${runtimeDependencies[@]}" )
local patchelfFlagsArray=( "${patchelfFlags[@]}" )
local autoPatchelfFlagsArray=( "${autoPatchelfFlags[@]}" )
else
readarray -td' ' ignoreMissingDepsArray < <(echo -n "$autoPatchelfIgnoreMissingDeps")
local appendRunpathsArray=($appendRunpaths)
local runtimeDependenciesArray=($runtimeDependencies)
local patchelfFlagsArray=($patchelfFlags)
local autoPatchelfFlagsArray=($autoPatchelfFlags)
fi

# Check if ignoreMissingDepsArray contains "1" and if so, replace it with
Expand All @@ -85,6 +87,7 @@ autoPatchelf() {
"${extraAutoPatchelfLibs[@]}" \
--runtime-dependencies "${runtimeDependenciesArray[@]/%//lib}" \
--append-rpaths "${appendRunpathsArray[@]}" \
"${autoPatchelfFlagsArray[@]}" \
--extra-args "${patchelfFlagsArray[@]}"
}

Expand Down
2 changes: 2 additions & 0 deletions pkgs/os-specific/linux/systemd/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ stdenv.mkDerivation (finalAttrs: {
]
;

autoPatchelfFlags = [ "--keep-libc" ];

buildInputs =
[
libxcrypt
Expand Down

0 comments on commit 747706d

Please sign in to comment.