2012-12-19 23:49:31 +01:00
|
|
|
#!@BASH@
|
2012-09-26 18:27:04 +02:00
|
|
|
# -*- mode: scheme; coding: utf-8; -*-
|
|
|
|
|
|
|
|
# XXX: We have to go through Bash because there's no command-line switch to
|
|
|
|
# augment %load-compiled-path, and because of the silly 127-byte limit for
|
|
|
|
# the shebang line in Linux.
|
|
|
|
# Use `load-compiled' because `load' (and `-l') doesn't otherwise load our
|
|
|
|
# .go file (see <http://bugs.gnu.org/12519>).
|
2016-04-06 14:20:50 +02:00
|
|
|
# Unset 'GUILE_LOAD_COMPILED_PATH' to make sure we do not stumble upon
|
|
|
|
# incompatible .go files. See
|
|
|
|
# <https://lists.gnu.org/archive/html/guile-devel/2016-03/msg00000.html>.
|
2012-09-26 18:27:04 +02:00
|
|
|
|
2016-04-06 14:20:50 +02:00
|
|
|
unset GUILE_LOAD_COMPILED_PATH
|
2012-12-19 23:49:31 +01:00
|
|
|
main="(@ (gnu build-support ld-wrapper) ld-wrapper)"
|
2015-04-03 22:27:45 +02:00
|
|
|
exec @GUILE@ -c "(load-compiled \"@SELF@.go\") (apply $main (cdr (command-line)))" "$@"
|
2012-09-26 18:27:04 +02:00
|
|
|
!#
|
2013-01-05 16:08:07 +01:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2017-05-25 14:34:18 +02:00
|
|
|
;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
|
2012-09-26 18:27:04 +02:00
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
;;; This file is part of GNU Guix.
|
2012-09-26 18:27:04 +02:00
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-09-26 18:27:04 +02:00
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-09-26 18:27:04 +02:00
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2013-01-05 16:08:07 +01:00
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-09-26 18:27:04 +02:00
|
|
|
|
2012-12-19 23:49:31 +01:00
|
|
|
(define-module (gnu build-support ld-wrapper)
|
2012-09-26 18:27:04 +02:00
|
|
|
#:use-module (srfi srfi-1)
|
2015-01-04 22:21:11 +01:00
|
|
|
#:use-module (ice-9 match)
|
2017-05-25 14:34:18 +02:00
|
|
|
#:autoload (ice-9 rdelim) (read-string)
|
2012-09-26 18:27:04 +02:00
|
|
|
#:export (ld-wrapper))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This is a wrapper for the linker. Its purpose is to inspect the -L and
|
|
|
|
;;; -l switches passed to the linker, add corresponding -rpath arguments, and
|
|
|
|
;;; invoke the actual linker with this new set of arguments.
|
|
|
|
;;;
|
|
|
|
;;; The alternatives to this hack would be:
|
|
|
|
;;;
|
|
|
|
;;; 1. Using $LD_RUN_PATH. However, that would tend to include more than
|
|
|
|
;;; needed in the RPATH; for instance, given a package with `libfoo' as
|
|
|
|
;;; an input, all its binaries would have libfoo in their RPATH,
|
|
|
|
;;; regardless of whether they actually NEED it.
|
|
|
|
;;;
|
|
|
|
;;; 2. Use a GCC "lib" spec string such as `%{L*:-rpath %*}', which adds a
|
|
|
|
;;; `-rpath LIBDIR' argument for each occurrence of `-L LIBDIR'.
|
|
|
|
;;; However, this doesn't work when $LIBRARY_PATH is used, because the
|
|
|
|
;;; additional `-L' switches are not matched by the above rule, because
|
|
|
|
;;; the rule only matches explicit user-provided switches. See
|
|
|
|
;;; <http://gcc.gnu.org/ml/gcc-help/2012-09/msg00110.html> for details.
|
|
|
|
;;;
|
|
|
|
;;; As a bonus, this wrapper checks for "impurities"--i.e., references to
|
|
|
|
;;; libraries outside the store.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define %real-ld
|
|
|
|
;; Name of the linker that we wrap.
|
|
|
|
"@LD@")
|
|
|
|
|
|
|
|
(define %store-directory
|
|
|
|
;; File name of the store.
|
2014-03-10 23:51:31 +01:00
|
|
|
(or (getenv "NIX_STORE") "/gnu/store"))
|
2012-09-26 18:27:04 +02:00
|
|
|
|
|
|
|
(define %temporary-directory
|
|
|
|
;; Temporary directory.
|
|
|
|
(or (getenv "TMPDIR") "/tmp"))
|
|
|
|
|
|
|
|
(define %build-directory
|
|
|
|
;; Top build directory when run from a builder.
|
|
|
|
(getenv "NIX_BUILD_TOP"))
|
|
|
|
|
|
|
|
(define %allow-impurities?
|
|
|
|
;; Whether to allow references to libraries outside the store.
|
|
|
|
(getenv "GUIX_LD_WRAPPER_ALLOW_IMPURITIES"))
|
|
|
|
|
|
|
|
(define %debug?
|
|
|
|
;; Whether to emit debugging output.
|
|
|
|
(getenv "GUIX_LD_WRAPPER_DEBUG"))
|
|
|
|
|
2015-04-07 10:29:51 +02:00
|
|
|
(define %disable-rpath?
|
|
|
|
;; Whether to disable automatic '-rpath' addition.
|
|
|
|
(getenv "GUIX_LD_WRAPPER_DISABLE_RPATH"))
|
|
|
|
|
2015-04-07 09:47:43 +02:00
|
|
|
(define (readlink* file)
|
|
|
|
;; Call 'readlink' until the result is not a symlink.
|
2013-06-12 09:39:31 +02:00
|
|
|
(define %max-symlink-depth 50)
|
|
|
|
|
|
|
|
(let loop ((file file)
|
|
|
|
(depth 0))
|
2015-04-21 21:07:11 +02:00
|
|
|
(define (absolute target)
|
|
|
|
(if (absolute-file-name? target)
|
|
|
|
target
|
|
|
|
(string-append (dirname file) "/" target)))
|
|
|
|
|
|
|
|
(if (>= depth %max-symlink-depth)
|
|
|
|
file
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(catch 'system-error
|
|
|
|
(lambda ()
|
|
|
|
(values #t (readlink file)))
|
|
|
|
(lambda args
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
(if (or (= errno EINVAL) (= errno ENOENT))
|
|
|
|
(values #f file)
|
|
|
|
(apply throw args))))))
|
|
|
|
(lambda (success? target)
|
|
|
|
(if success?
|
|
|
|
(loop (absolute target) (+ depth 1))
|
|
|
|
file))))))
|
2015-04-07 09:47:43 +02:00
|
|
|
|
|
|
|
(define (pure-file-name? file)
|
|
|
|
;; Return #t when FILE is the name of a file either within the store
|
|
|
|
;; (possibly via a symlink) or within the build directory.
|
2015-04-21 21:07:11 +02:00
|
|
|
(let ((file (readlink* file)))
|
2013-06-12 09:39:31 +02:00
|
|
|
(or (not (string-prefix? "/" file))
|
|
|
|
(string-prefix? %store-directory file)
|
|
|
|
(string-prefix? %temporary-directory file)
|
2015-04-07 09:47:43 +02:00
|
|
|
(and %build-directory
|
|
|
|
(string-prefix? %build-directory file)))))
|
2012-09-26 18:27:04 +02:00
|
|
|
|
2015-04-07 10:21:36 +02:00
|
|
|
(define (store-file-name? file)
|
|
|
|
;; Return #t when FILE is a store file, possibly indirectly.
|
2015-04-21 21:07:11 +02:00
|
|
|
(string-prefix? %store-directory (readlink* file)))
|
2015-04-07 10:21:36 +02:00
|
|
|
|
2015-02-27 00:08:04 +01:00
|
|
|
(define (shared-library? file)
|
|
|
|
;; Return #t when FILE denotes a shared library.
|
|
|
|
(or (string-suffix? ".so" file)
|
|
|
|
(let ((index (string-contains file ".so.")))
|
|
|
|
;; Since we cannot use regexps during bootstrap, roll our own.
|
|
|
|
(and index
|
|
|
|
(string-every (char-set-union (char-set #\.) char-set:digit)
|
|
|
|
(string-drop file (+ index 3)))))))
|
|
|
|
|
2015-11-28 22:06:31 +01:00
|
|
|
(define (library-search-path args)
|
|
|
|
;; Return the library search path as a list of directory names. The GNU ld
|
|
|
|
;; manual notes that "[a]ll `-L' options apply to all `-l' options,
|
|
|
|
;; regardless of the order in which the options appear", so we must compute
|
|
|
|
;; the search path independently of the -l options.
|
|
|
|
(let loop ((args args)
|
|
|
|
(path '()))
|
|
|
|
(match args
|
|
|
|
(()
|
|
|
|
(reverse path))
|
|
|
|
(("-L" directory . rest)
|
|
|
|
(loop rest (cons directory path)))
|
|
|
|
((argument . rest)
|
|
|
|
(if (string-prefix? "-L" argument) ;augment the search path
|
|
|
|
(loop rest
|
|
|
|
(cons (string-drop argument 2) path))
|
|
|
|
(loop rest path))))))
|
|
|
|
|
|
|
|
(define (library-files-linked args library-path)
|
|
|
|
;; Return the absolute file names of shared libraries explicitly linked
|
|
|
|
;; against via `-l' or with an absolute file name in ARGS, looking them up
|
|
|
|
;; in LIBRARY-PATH.
|
|
|
|
(define files+args
|
2015-01-04 22:21:11 +01:00
|
|
|
(fold (lambda (argument result)
|
|
|
|
(match result
|
2015-11-28 22:06:31 +01:00
|
|
|
((library-files ((and flag
|
|
|
|
(or "-dynamic-linker" "-plugin"))
|
|
|
|
. rest))
|
2015-06-14 01:00:27 +02:00
|
|
|
;; When passed '-dynamic-linker ld.so', ignore 'ld.so'; when
|
|
|
|
;; passed '-plugin liblto_plugin.so', ignore
|
|
|
|
;; 'liblto_plugin.so'. See <http://bugs.gnu.org/20102>.
|
2015-11-28 22:06:31 +01:00
|
|
|
(list library-files
|
2015-06-14 01:00:27 +02:00
|
|
|
(cons* argument flag rest)))
|
2015-11-28 22:06:31 +01:00
|
|
|
((library-files previous-args)
|
|
|
|
(cond ((string-prefix? "-l" argument) ;add library
|
2015-01-04 22:21:11 +01:00
|
|
|
(let* ((lib (string-append "lib"
|
|
|
|
(string-drop argument 2)
|
|
|
|
".so"))
|
|
|
|
(full (search-path library-path lib)))
|
2015-11-28 22:06:31 +01:00
|
|
|
(list (if full
|
2015-04-21 21:14:28 +02:00
|
|
|
(cons full library-files)
|
|
|
|
library-files)
|
|
|
|
(cons argument previous-args))))
|
2015-01-04 22:21:11 +01:00
|
|
|
((and (string-prefix? %store-directory argument)
|
2015-02-27 00:08:04 +01:00
|
|
|
(shared-library? argument)) ;add library
|
2015-11-28 22:06:31 +01:00
|
|
|
(list (cons argument library-files)
|
2015-04-21 21:14:28 +02:00
|
|
|
(cons argument previous-args)))
|
2015-01-04 22:21:11 +01:00
|
|
|
(else
|
2015-11-28 22:06:31 +01:00
|
|
|
(list library-files
|
2015-04-21 21:14:28 +02:00
|
|
|
(cons argument previous-args)))))))
|
2015-11-28 22:06:31 +01:00
|
|
|
(list '() '())
|
2015-01-04 22:21:11 +01:00
|
|
|
args))
|
|
|
|
|
2015-11-28 22:06:31 +01:00
|
|
|
(match files+args
|
|
|
|
((files arguments)
|
2015-01-04 22:21:11 +01:00
|
|
|
(reverse files))))
|
|
|
|
|
|
|
|
(define (rpath-arguments library-files)
|
|
|
|
;; Return the `-rpath' argument list for each of LIBRARY-FILES, a list of
|
|
|
|
;; absolute file names.
|
2012-09-26 18:27:04 +02:00
|
|
|
(fold-right (lambda (file args)
|
2015-04-07 10:21:36 +02:00
|
|
|
;; Add '-rpath' if and only if FILE is in the store; we don't
|
|
|
|
;; want to add '-rpath' for files under %BUILD-DIRECTORY or
|
|
|
|
;; %TEMPORARY-DIRECTORY because that could leak to installed
|
|
|
|
;; files.
|
2015-04-07 10:29:51 +02:00
|
|
|
(cond ((and (not %disable-rpath?)
|
|
|
|
(store-file-name? file))
|
2015-04-07 10:21:36 +02:00
|
|
|
(cons* "-rpath" (dirname file) args))
|
|
|
|
((or %allow-impurities?
|
|
|
|
(pure-file-name? file))
|
|
|
|
args)
|
|
|
|
(else
|
|
|
|
(begin
|
|
|
|
(format (current-error-port)
|
|
|
|
"ld-wrapper: error: attempt to use \
|
|
|
|
impure library ~s~%"
|
|
|
|
file)
|
|
|
|
(exit 1)))))
|
2012-09-26 18:27:04 +02:00
|
|
|
'()
|
|
|
|
library-files))
|
|
|
|
|
2017-05-25 14:34:18 +02:00
|
|
|
(define (expand-arguments args)
|
|
|
|
;; Expand ARGS such that "response file" arguments, such as "@args.txt", are
|
|
|
|
;; expanded (info "(gcc) Overall Options").
|
|
|
|
(define (response-file-arguments file)
|
|
|
|
(when %debug?
|
|
|
|
(format (current-error-port)
|
|
|
|
"ld-wrapper: attempting to read arguments from '~a'~%" file))
|
|
|
|
|
|
|
|
;; FIXME: Options can contain whitespace if they are protected by single
|
|
|
|
;; or double quotes; this is not implemented here.
|
|
|
|
(string-tokenize (call-with-input-file file read-string)))
|
|
|
|
|
|
|
|
(define result
|
|
|
|
(fold-right (lambda (arg result)
|
|
|
|
(if (string-prefix? "@" arg)
|
|
|
|
(let ((file (string-drop arg 1)))
|
|
|
|
(append (catch 'system-error
|
|
|
|
(lambda ()
|
|
|
|
(response-file-arguments file))
|
|
|
|
(lambda args
|
|
|
|
;; FILE doesn't exist or cannot be read so
|
|
|
|
;; leave ARG as is.
|
|
|
|
(list arg)))
|
|
|
|
result))
|
|
|
|
(cons arg result)))
|
|
|
|
'()
|
|
|
|
args))
|
|
|
|
|
|
|
|
;; If there are "@" arguments in RESULT *and* we can expand them (they don't
|
|
|
|
;; refer to nonexistent files), then recurse.
|
|
|
|
(if (equal? result args)
|
|
|
|
result
|
|
|
|
(expand-arguments result)))
|
|
|
|
|
2012-09-26 18:27:04 +02:00
|
|
|
(define (ld-wrapper . args)
|
|
|
|
;; Invoke the real `ld' with ARGS, augmented with `-rpath' switches.
|
2017-05-25 14:34:18 +02:00
|
|
|
(let* ((args (expand-arguments args))
|
|
|
|
(path (library-search-path args))
|
2015-11-28 22:06:31 +01:00
|
|
|
(libs (library-files-linked args path))
|
2015-01-04 22:21:11 +01:00
|
|
|
(args (append args (rpath-arguments libs))))
|
|
|
|
(when %debug?
|
2015-11-28 22:06:31 +01:00
|
|
|
(format (current-error-port)
|
|
|
|
"ld-wrapper: library search path: ~s~%" path)
|
2015-04-21 21:08:44 +02:00
|
|
|
(format (current-error-port)
|
|
|
|
"ld-wrapper: libraries linked: ~s~%" libs)
|
2015-01-04 22:21:11 +01:00
|
|
|
(format (current-error-port)
|
|
|
|
"ld-wrapper: invoking `~a' with ~s~%"
|
|
|
|
%real-ld args))
|
2012-09-26 18:27:04 +02:00
|
|
|
(apply execl %real-ld (basename %real-ld) args)))
|
|
|
|
|
|
|
|
;;; ld-wrapper.scm ends here
|