ld-wrapper.in 12 KB

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