1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (test-gremlin)
20 #:use-module (guix elf)
21 #:use-module ((guix utils) #:select (call-with-temporary-directory))
22 #:use-module (guix build utils)
23 #:use-module (guix build gremlin)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-26)
26 #:use-module (srfi srfi-34)
27 #:use-module (srfi srfi-64)
28 #:use-module (rnrs io ports)
29 #:use-module (ice-9 popen)
30 #:use-module (ice-9 rdelim)
31 #:use-module (ice-9 regex)
32 #:use-module (ice-9 match))
34 (define %guile-executable
35 (match (false-if-exception (readlink "/proc/self/exe"))
37 (and (file-exists? program) (elf-file? program)
43 (compose parse-elf get-bytevector-all))
46 (or (which "gcc") (which "cc") (which "g++")))
49 (test-begin "gremlin")
51 (unless %guile-executable (test-skip 1))
52 (test-assert "elf-dynamic-info-needed, executable"
53 (let* ((elf (call-with-input-file %guile-executable read-elf))
54 (dyninfo (elf-dynamic-info elf)))
55 (or (not dyninfo) ;static executable
57 (list (string-append "libguile-" (effective-version))
60 (string-take lib (string-contains lib ".so")))
61 (elf-dynamic-info-needed dyninfo))))))
63 (unless (and %guile-executable (not (getenv "LD_LIBRARY_PATH"))
64 (file-needed %guile-executable) ;statically linked?
65 ;; When Guix has been built on a foreign distro, using a
66 ;; toolchain and libraries from that foreign distro, it is not
67 ;; unusual for the runpath to be empty.
68 (pair? (file-runpath %guile-executable)))
70 (test-assert "file-needed/recursive"
71 (let* ((needed (file-needed/recursive %guile-executable))
74 ;; Tell ld.so to list loaded objects, like 'ldd' does.
75 (setenv "LD_TRACE_LOADED_OBJECTS" "yup"))
77 (open-pipe* OPEN_READ %guile-executable))
79 (unsetenv "LD_TRACE_LOADED_OBJECTS")))))
81 (make-regexp "^[[:blank:]]+([[:graph:]]+ => )?([[:graph:]]+) .*$"))
83 (define (read-ldd-output port)
84 ;; Read from PORT output in GNU ldd format.
85 (let loop ((result '()))
86 (match (read-line port)
89 ((= (cut regexp-exec ldd-rx <>) m)
91 (loop (cons (match:substring m 2) result))
95 (remove (cut string-prefix? "linux-vdso.so" <>)
96 (read-ldd-output pipe)))
98 (and (zero? (close-pipe pipe))
99 (lset= string=? (pk 'truth ground-truth) (pk 'needed needed)))))
101 (test-equal "expand-origin"
106 (map (cut expand-origin <> "OOO")
109 "../${ORIGIN}/bar/$ORIGIN/baz"
114 (test-equal "strip-runpath"
116 (call-with-temporary-directory
118 (with-directory-excursion directory
119 (call-with-output-file "t.c"
121 (display "int main () { puts(\"hello\"); }" port)))
122 (invoke c-compiler "t.c"
123 "-Wl,--enable-new-dtags" "-Wl,-rpath=/foo" "-Wl,-rpath=/bar")
124 (let* ((dyninfo (elf-dynamic-info
125 (parse-elf (call-with-input-file "a.out"
126 get-bytevector-all))))
127 (old (elf-dynamic-info-runpath dyninfo))
128 (new (strip-runpath "a.out"))
129 (new* (strip-runpath "a.out")))
130 (validate-needed-in-runpath "a.out")
131 (and (member "/foo" old) (member "/bar" old)
132 (not (member "/foo" new))
133 (not (member "/bar" new))
135 (let* ((pipe (open-input-pipe "./a.out"))
136 (str (get-string-all pipe)))
142 (test-equal "set-file-runpath + file-runpath"
144 (call-with-temporary-directory
146 (with-directory-excursion directory
147 (call-with-output-file "t.c"
149 (display "int main () { puts(\"hello\"); }" port)))
151 (invoke c-compiler "t.c"
152 "-Wl,--enable-new-dtags" "-Wl,-rpath=/xxxxxxxxx")
154 (let ((original-runpath (file-runpath "a.out")))
155 (and (member "/xxxxxxxxx" original-runpath)
156 (guard (c ((runpath-too-long-error? c)
157 (string=? "a.out" (runpath-too-long-error-file c))))
158 (set-file-runpath "a.out" (list (make-string 777 #\y))))
159 (let ((runpath (delete "/xxxxxxxxx" original-runpath)))
160 (set-file-runpath "a.out" runpath)
161 (equal? runpath (file-runpath "a.out")))
162 (let* ((pipe (open-input-pipe "./a.out"))
163 (str (get-string-all pipe)))
169 (test-equal "elf-dynamic-info-soname"
171 (call-with-temporary-directory
173 (with-directory-excursion directory
174 (call-with-output-file "t.c"
176 (display "// empty file" port)))
177 (invoke c-compiler "t.c"
178 "-shared" "-Wl,-soname,libfoo.so.2")
179 (let* ((dyninfo (elf-dynamic-info
180 (parse-elf (call-with-input-file "a.out"
181 get-bytevector-all))))
182 (soname (elf-dynamic-info-soname dyninfo)))