gnu: sssd: Update to 2.7.4.
[jackhill/guix/guix.git] / gnu / packages / ld-wrapper.in
CommitLineData
dfb53ee2 1#!@BASH@
82dc2b9a
LC
2# -*- mode: scheme; coding: utf-8; -*-
3
4# XXX: We have to go through Bash because there's no command-line switch to
5# augment %load-compiled-path, and because of the silly 127-byte limit for
6# the shebang line in Linux.
7# Use `load-compiled' because `load' (and `-l') doesn't otherwise load our
8# .go file (see <http://bugs.gnu.org/12519>).
2a5739b4
LC
9# Unset 'GUILE_LOAD_COMPILED_PATH' to make sure we do not stumble upon
10# incompatible .go files. See
11# <https://lists.gnu.org/archive/html/guile-devel/2016-03/msg00000.html>.
82dc2b9a 12
2a5739b4 13unset GUILE_LOAD_COMPILED_PATH
a83860cd 14unset GUILE_SYSTEM_COMPILED_PATH
dfb53ee2 15main="(@ (gnu build-support ld-wrapper) ld-wrapper)"
9bab6bea 16exec @GUILE@ -c "(load-compiled \"@SELF@.go\") (apply $main (cdr (command-line)))" "$@"
82dc2b9a 17!#
4155e2a9 18;;; GNU Guix --- Functional package management for GNU
a83860cd 19;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
feb8c5da 20;;; Copyright © 2020 Marius Bakke <mbakke@fastmail.com>
82dc2b9a 21;;;
4155e2a9 22;;; This file is part of GNU Guix.
82dc2b9a 23;;;
4155e2a9 24;;; GNU Guix is free software; you can redistribute it and/or modify it
82dc2b9a
LC
25;;; under the terms of the GNU General Public License as published by
26;;; the Free Software Foundation; either version 3 of the License, or (at
27;;; your option) any later version.
28;;;
4155e2a9 29;;; GNU Guix is distributed in the hope that it will be useful, but
82dc2b9a
LC
30;;; WITHOUT ANY WARRANTY; without even the implied warranty of
31;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32;;; GNU General Public License for more details.
33;;;
34;;; You should have received a copy of the GNU General Public License
4155e2a9 35;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
82dc2b9a 36
dfb53ee2 37(define-module (gnu build-support ld-wrapper)
82dc2b9a 38 #:use-module (srfi srfi-1)
d8491ba5 39 #:use-module (ice-9 match)
feb8c5da 40 #:autoload (ice-9 rdelim) (read-delimited)
82dc2b9a
LC
41 #:export (ld-wrapper))
42
43;;; Commentary:
44;;;
45;;; This is a wrapper for the linker. Its purpose is to inspect the -L and
46;;; -l switches passed to the linker, add corresponding -rpath arguments, and
47;;; invoke the actual linker with this new set of arguments.
48;;;
49;;; The alternatives to this hack would be:
50;;;
51;;; 1. Using $LD_RUN_PATH. However, that would tend to include more than
52;;; needed in the RPATH; for instance, given a package with `libfoo' as
53;;; an input, all its binaries would have libfoo in their RPATH,
54;;; regardless of whether they actually NEED it.
55;;;
56;;; 2. Use a GCC "lib" spec string such as `%{L*:-rpath %*}', which adds a
57;;; `-rpath LIBDIR' argument for each occurrence of `-L LIBDIR'.
58;;; However, this doesn't work when $LIBRARY_PATH is used, because the
59;;; additional `-L' switches are not matched by the above rule, because
60;;; the rule only matches explicit user-provided switches. See
61;;; <http://gcc.gnu.org/ml/gcc-help/2012-09/msg00110.html> for details.
62;;;
63;;; As a bonus, this wrapper checks for "impurities"--i.e., references to
64;;; libraries outside the store.
65;;;
66;;; Code:
67
68(define %real-ld
69 ;; Name of the linker that we wrap.
70 "@LD@")
71
72(define %store-directory
73 ;; File name of the store.
8be3b8a3 74 (or (getenv "NIX_STORE") "/gnu/store"))
82dc2b9a
LC
75
76(define %temporary-directory
77 ;; Temporary directory.
78 (or (getenv "TMPDIR") "/tmp"))
79
80(define %build-directory
81 ;; Top build directory when run from a builder.
82 (getenv "NIX_BUILD_TOP"))
83
84(define %allow-impurities?
85 ;; Whether to allow references to libraries outside the store.
d0a2db47
LC
86 ;; Allow them by default for convenience.
87 (let ((value (getenv "GUIX_LD_WRAPPER_ALLOW_IMPURITIES")))
88 (or (not value)
89 (let ((value (string-downcase value)))
90 (cond ((member value '("yes" "y" "t" "true" "1"))
91 #t)
92 ((member value '("no" "n" "f" "false" "0"))
93 #f)
94 (else
95 (format (current-error-port)
96 "ld-wrapper: ~s: invalid value for \
97'GUIX_LD_WRAPPER_ALLOW_IMPURITIES'~%"
98 value)))))))
82dc2b9a
LC
99
100(define %debug?
101 ;; Whether to emit debugging output.
102 (getenv "GUIX_LD_WRAPPER_DEBUG"))
103
71b67168
LC
104(define %disable-rpath?
105 ;; Whether to disable automatic '-rpath' addition.
106 (getenv "GUIX_LD_WRAPPER_DISABLE_RPATH"))
107
41fc0eb9
LC
108(define (readlink* file)
109 ;; Call 'readlink' until the result is not a symlink.
cfbf7877
LC
110 (define %max-symlink-depth 50)
111
112 (let loop ((file file)
113 (depth 0))
cbbb11c8
LC
114 (define (absolute target)
115 (if (absolute-file-name? target)
116 target
117 (string-append (dirname file) "/" target)))
118
119 (if (>= depth %max-symlink-depth)
120 file
121 (call-with-values
122 (lambda ()
123 (catch 'system-error
124 (lambda ()
125 (values #t (readlink file)))
126 (lambda args
127 (let ((errno (system-error-errno args)))
128 (if (or (= errno EINVAL) (= errno ENOENT))
129 (values #f file)
130 (apply throw args))))))
131 (lambda (success? target)
132 (if success?
133 (loop (absolute target) (+ depth 1))
134 file))))))
41fc0eb9
LC
135
136(define (pure-file-name? file)
137 ;; Return #t when FILE is the name of a file either within the store
138 ;; (possibly via a symlink) or within the build directory.
cbbb11c8 139 (let ((file (readlink* file)))
cfbf7877
LC
140 (or (not (string-prefix? "/" file))
141 (string-prefix? %store-directory file)
142 (string-prefix? %temporary-directory file)
41fc0eb9
LC
143 (and %build-directory
144 (string-prefix? %build-directory file)))))
82dc2b9a 145
51d0cd9b
LC
146(define (store-file-name? file)
147 ;; Return #t when FILE is a store file, possibly indirectly.
cbbb11c8 148 (string-prefix? %store-directory (readlink* file)))
51d0cd9b 149
f307947e
LC
150(define (shared-library? file)
151 ;; Return #t when FILE denotes a shared library.
152 (or (string-suffix? ".so" file)
153 (let ((index (string-contains file ".so.")))
154 ;; Since we cannot use regexps during bootstrap, roll our own.
155 (and index
156 (string-every (char-set-union (char-set #\.) char-set:digit)
157 (string-drop file (+ index 3)))))))
158
e946b609
LC
159(define (library-search-path args)
160 ;; Return the library search path as a list of directory names. The GNU ld
161 ;; manual notes that "[a]ll `-L' options apply to all `-l' options,
162 ;; regardless of the order in which the options appear", so we must compute
163 ;; the search path independently of the -l options.
164 (let loop ((args args)
165 (path '()))
166 (match args
167 (()
168 (reverse path))
169 (("-L" directory . rest)
170 (loop rest (cons directory path)))
171 ((argument . rest)
172 (if (string-prefix? "-L" argument) ;augment the search path
173 (loop rest
174 (cons (string-drop argument 2) path))
175 (loop rest path))))))
176
177(define (library-files-linked args library-path)
178 ;; Return the absolute file names of shared libraries explicitly linked
179 ;; against via `-l' or with an absolute file name in ARGS, looking them up
180 ;; in LIBRARY-PATH.
181 (define files+args
d8491ba5
LC
182 (fold (lambda (argument result)
183 (match result
e946b609
LC
184 ((library-files ((and flag
185 (or "-dynamic-linker" "-plugin"))
186 . rest))
b5616bc3
LC
187 ;; When passed '-dynamic-linker ld.so', ignore 'ld.so'; when
188 ;; passed '-plugin liblto_plugin.so', ignore
189 ;; 'liblto_plugin.so'. See <http://bugs.gnu.org/20102>.
e946b609 190 (list library-files
b5616bc3 191 (cons* argument flag rest)))
e946b609
LC
192 ((library-files previous-args)
193 (cond ((string-prefix? "-l" argument) ;add library
d8491ba5
LC
194 (let* ((lib (string-append "lib"
195 (string-drop argument 2)
196 ".so"))
197 (full (search-path library-path lib)))
e946b609 198 (list (if full
4a2b74bf
LC
199 (cons full library-files)
200 library-files)
201 (cons argument previous-args))))
d8491ba5 202 ((and (string-prefix? %store-directory argument)
f307947e 203 (shared-library? argument)) ;add library
e946b609 204 (list (cons argument library-files)
4a2b74bf 205 (cons argument previous-args)))
d8491ba5 206 (else
e946b609 207 (list library-files
4a2b74bf 208 (cons argument previous-args)))))))
e946b609 209 (list '() '())
d8491ba5
LC
210 args))
211
e946b609
LC
212 (match files+args
213 ((files arguments)
d8491ba5
LC
214 (reverse files))))
215
216(define (rpath-arguments library-files)
217 ;; Return the `-rpath' argument list for each of LIBRARY-FILES, a list of
218 ;; absolute file names.
82dc2b9a 219 (fold-right (lambda (file args)
51d0cd9b
LC
220 ;; Add '-rpath' if and only if FILE is in the store; we don't
221 ;; want to add '-rpath' for files under %BUILD-DIRECTORY or
222 ;; %TEMPORARY-DIRECTORY because that could leak to installed
223 ;; files.
71b67168
LC
224 (cond ((and (not %disable-rpath?)
225 (store-file-name? file))
51d0cd9b
LC
226 (cons* "-rpath" (dirname file) args))
227 ((or %allow-impurities?
228 (pure-file-name? file))
229 args)
230 (else
231 (begin
232 (format (current-error-port)
233 "ld-wrapper: error: attempt to use \
1a5e07c3
LC
234library outside of ~a: ~s~%"
235 %store-directory file)
51d0cd9b 236 (exit 1)))))
82dc2b9a
LC
237 '()
238 library-files))
239
696487d6
LC
240(define (expand-arguments args)
241 ;; Expand ARGS such that "response file" arguments, such as "@args.txt", are
242 ;; expanded (info "(gcc) Overall Options").
243 (define (response-file-arguments file)
feb8c5da
MB
244 (define (tokenize port)
245 ;; Return a list of all strings found in PORT. Quote characters are
246 ;; removed, but whitespaces within quoted strings are preserved.
247 (let loop ((tokens '()))
248 (let* ((token+delimiter (read-delimited " '\"\n" port 'split))
249 (token (car token+delimiter))
250 (delim (cdr token+delimiter)))
251 (if (eof-object? token)
252 (reverse tokens)
253 (case delim
254 ((#\") (loop (cons (read-delimited "\"" port) tokens)))
255 ((#\') (loop (cons (read-delimited "'" port) tokens)))
256 (else (if (> (string-length token) 0)
257 (loop (cons token tokens))
258 (loop tokens))))))))
259
696487d6
LC
260 (when %debug?
261 (format (current-error-port)
262 "ld-wrapper: attempting to read arguments from '~a'~%" file))
263
feb8c5da 264 (call-with-input-file file tokenize))
696487d6
LC
265
266 (define result
267 (fold-right (lambda (arg result)
268 (if (string-prefix? "@" arg)
269 (let ((file (string-drop arg 1)))
270 (append (catch 'system-error
271 (lambda ()
272 (response-file-arguments file))
273 (lambda args
274 ;; FILE doesn't exist or cannot be read so
275 ;; leave ARG as is.
276 (list arg)))
277 result))
278 (cons arg result)))
279 '()
280 args))
281
282 ;; If there are "@" arguments in RESULT *and* we can expand them (they don't
283 ;; refer to nonexistent files), then recurse.
284 (if (equal? result args)
285 result
286 (expand-arguments result)))
287
82dc2b9a
LC
288(define (ld-wrapper . args)
289 ;; Invoke the real `ld' with ARGS, augmented with `-rpath' switches.
696487d6
LC
290 (let* ((args (expand-arguments args))
291 (path (library-search-path args))
e946b609 292 (libs (library-files-linked args path))
d8491ba5
LC
293 (args (append args (rpath-arguments libs))))
294 (when %debug?
e946b609
LC
295 (format (current-error-port)
296 "ld-wrapper: library search path: ~s~%" path)
4267c637
LC
297 (format (current-error-port)
298 "ld-wrapper: libraries linked: ~s~%" libs)
d8491ba5
LC
299 (format (current-error-port)
300 "ld-wrapper: invoking `~a' with ~s~%"
67645383
LC
301 %real-ld args)
302 (force-output (current-error-port)))
82dc2b9a
LC
303 (apply execl %real-ld (basename %real-ld) args)))
304
305;;; ld-wrapper.scm ends here