From 0a8d11c368d996bf4ecc226dda6a7155d2b55eeb Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Fri, 6 Nov 2020 21:00:27 +0100 Subject: [PATCH] genodeSources: make shared library references absolute Use patchelf in a postFixupHook to replace any relative library names with absolute file-sytsem paths. This ensures that the proper runtime closures are constructed and makes it easier to route the loader to the correct ROMs. This requires that packages built from the Genode depot must specify runtime dependencies as "buildInputs" in the depot-targets.nix file. --- packages/genodelabs/default.nix | 46 +++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/genodelabs/default.nix b/packages/genodelabs/default.nix index 06f33eb..a0acf0f 100644 --- a/packages/genodelabs/default.nix +++ b/packages/genodelabs/default.nix @@ -125,10 +125,46 @@ let stdenv' = pkgs.stdenvAdapters.overrideCC pkgs.stdenv toolchain; + patchRelativeLibraries = '' + if [ -z "''${noAbsoluteLibraries:-}" ]; then + find "''${!outputBin}" "''${!outputLib}" -type f | while read -r elf; do + if ! isELF "$elf"; then continue; fi + args= + for name in $(patchelf --print-needed "$elf"); do + if [ "ld.lib.so" == "$name" ]; then + continue + fi + if [ -f "$name" ]; then + continue + fi + argPair= + for input in ''${!outputLib} ''${libraryBuildInputs:-}; do + local libdir="$input/lib" + local libabs=$(realpath "$libdir/$name" 2>/dev/null || true) + if [ -f "$libabs" ]; then + echo "Replace $name reference with $libabs" + argPair="--replace-needed $name $libabs" + break + fi + done + if [ -z "$argPair" ]; then + echo "Could not find absolute path to $name in buildInputs" + exit 1 + fi + args="$args $argPair" + done + if [ -n "$args" ]; then + patchelf $args $elf + fi + done + fi + ''; + buildUpstream = # Build from the Genode sources - { name, targets, portInputs ? [ ], nativeBuildInputs ? [ ], patches ? [ ] - , enableParallelBuilding ? true, meta ? { }, ... }@extraAttrs: + { name, targets, portInputs ? [ ], nativeBuildInputs ? [ ] + , buildInputs ? [ ], patches ? [ ], enableParallelBuilding ? true + , meta ? { }, ... }@extraAttrs: let havePatches = patches != [ ]; in stdenv'.mkDerivation (extraAttrs // { @@ -172,6 +208,9 @@ let runHook postInstall ''; + libraryBuildInputs = map pkgs.stdenv.lib.getLib buildInputs; + postFixup = patchRelativeLibraries; + meta = { platforms = lib.platforms.genode; } // meta; }); @@ -230,6 +269,9 @@ let runHook postInstall ''; + libraryBuildInputs = map pkgs.stdenv.lib.getLib buildInputs; + postFixup = patchRelativeLibraries; + meta = { platforms = lib.platforms.genode; } // meta; });