From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id F3i5JY4lwl9eIwAA0tVLHw (envelope-from ) for ; Sat, 28 Nov 2020 10:25:18 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id sN8DIY4lwl83QwAAbx9fmQ (envelope-from ) for ; Sat, 28 Nov 2020 10:25:18 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 550239403CA for ; Sat, 28 Nov 2020 10:25:18 +0000 (UTC) Received: from localhost ([::1]:34708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kixPp-0002jd-8y for larch@yhetil.org; Sat, 28 Nov 2020 05:25:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33394) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kixPa-0002WI-Vd for guix-patches@gnu.org; Sat, 28 Nov 2020 05:25:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:35445) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kixPa-0002dN-OL for guix-patches@gnu.org; Sat, 28 Nov 2020 05:25:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kixPa-0006tU-K3 for guix-patches@gnu.org; Sat, 28 Nov 2020 05:25:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#44899] [PATCH v2 2/4] gremlin: Add 'file-needed/recursive'. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 28 Nov 2020 10:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44899 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 44899@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 44899-submit@debbugs.gnu.org id=B44899.160655908926439 (code B ref 44899); Sat, 28 Nov 2020 10:25:02 +0000 Received: (at 44899) by debbugs.gnu.org; 28 Nov 2020 10:24:49 +0000 Received: from localhost ([127.0.0.1]:46982 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kixPM-0006sL-H8 for submit@debbugs.gnu.org; Sat, 28 Nov 2020 05:24:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:43336) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kixPF-0006rQ-9G for 44899@debbugs.gnu.org; Sat, 28 Nov 2020 05:24:43 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]:42763) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kixPA-0002U5-49; Sat, 28 Nov 2020 05:24:36 -0500 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=49898 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1kixP9-0000YV-Av; Sat, 28 Nov 2020 05:24:35 -0500 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Sat, 28 Nov 2020 11:24:06 +0100 Message-Id: <20201128102409.3157-3-ludo@gnu.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201128102409.3157-1-ludo@gnu.org> References: <20201127083302.2578-1-ludo@gnu.org> <20201128102409.3157-1-ludo@gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -3.3 (---) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: 2.23 X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-TUID: 30jNa0gVrFko * guix/build/gremlin.scm (file-needed/recursive): New procedure. * tests/gremlin.scm ("file-needed/recursive"): New test. --- guix/build/gremlin.scm | 41 +++++++++++++++++++++++++++++++++++++++++ tests/gremlin.scm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/guix/build/gremlin.scm b/guix/build/gremlin.scm index 44604827a9..d56984b85e 100644 --- a/guix/build/gremlin.scm +++ b/guix/build/gremlin.scm @@ -44,6 +44,7 @@ file-dynamic-info file-runpath file-needed + file-needed/recursive missing-runpath-error? missing-runpath-error-file @@ -259,6 +260,46 @@ FILE lacks dynamic info." dynamic info." (and=> (file-dynamic-info file) elf-dynamic-info-needed)) +(define (file-needed/recursive file) + "Return two values: the list of absolute .so file names FILE depends on, +recursively, and the list of .so file names that could not be found. File +names are resolved by searching the RUNPATH of the file that NEEDs them. + +This is similar to the info returned by the 'ldd' command." + (let loop ((files (list file)) + (result '()) + (not-found '())) + (match files + (() + (values (reverse result) + (reverse (delete-duplicates not-found)))) + ((file . rest) + (match (file-dynamic-info (pk 'file file)) + (#f + (loop rest result not-found)) + (info + (let ((runpath (elf-dynamic-info-runpath info)) + (needed (elf-dynamic-info-needed info))) + (if (and runpath needed) + (let* ((runpath (map (cute expand-origin <> (dirname file)) + runpath)) + (resolved (map (cut search-path runpath <>) + needed)) + (failed (filter-map (lambda (needed resolved) + (and (not resolved) + (not (libc-library? needed)) + needed)) + needed resolved)) + (needed (remove (lambda (value) + (or (not value) + ;; XXX: quadratic + (member value result))) + resolved))) + (loop (append rest needed) + (append needed result) + (append failed not-found))) + (loop rest result not-found))))))))) + (define %libc-libraries ;; List of libraries as of glibc 2.21 (there are more but those are ;; typically mean to be LD_PRELOADed and thus do not appear as NEEDED.) diff --git a/tests/gremlin.scm b/tests/gremlin.scm index f191adb8b3..9ddac14265 100644 --- a/tests/gremlin.scm +++ b/tests/gremlin.scm @@ -27,6 +27,8 @@ #:use-module (srfi srfi-64) #:use-module (rnrs io ports) #:use-module (ice-9 popen) + #:use-module (ice-9 rdelim) + #:use-module (ice-9 regex) #:use-module (ice-9 match)) (define %guile-executable @@ -58,6 +60,40 @@ (string-take lib (string-contains lib ".so"))) (elf-dynamic-info-needed dyninfo)))))) +(unless (and %guile-executable (not (getenv "LD_LIBRARY_PATH")) + (file-needed %guile-executable)) ;statically linked? + (test-skip 1)) +(test-assert "file-needed/recursive" + (let* ((needed (file-needed/recursive %guile-executable)) + (pipe (dynamic-wind + (lambda () + ;; Tell ld.so to list loaded objects, like 'ldd' does. + (setenv "LD_TRACE_LOADED_OBJECTS" "yup")) + (lambda () + (open-pipe* OPEN_READ %guile-executable)) + (lambda () + (unsetenv "LD_TRACE_LOADED_OBJECTS"))))) + (define ldd-rx + (make-regexp "^[[:blank:]]+([[:graph:]]+ => )?([[:graph:]]+) .*$")) + + (define (read-ldd-output port) + ;; Read from PORT output in GNU ldd format. + (let loop ((result '())) + (match (read-line port) + ((? eof-object?) + (reverse result)) + ((= (cut regexp-exec ldd-rx <>) m) + (if m + (loop (cons (match:substring m 2) result)) + (loop result)))))) + + (define ground-truth + (remove (cut string-prefix? "linux-vdso.so" <>) + (read-ldd-output pipe))) + + (and (zero? (close-pipe pipe)) + (lset= string=? (pk 'truth ground-truth) (pk 'needed needed))))) + (test-equal "expand-origin" '("OOO/../lib" "OOO" -- 2.29.2