gnu: r-qtl2: Move to (gnu packages cran).
[jackhill/guix/guix.git] / guix / build / gremlin.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19 (define-module (guix build gremlin)
20 #:use-module (guix elf)
21 #:use-module ((guix build utils) #:select (store-file-name?))
22 #:use-module (ice-9 match)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-9)
25 #:use-module (srfi srfi-26)
26 #:use-module (srfi srfi-34)
27 #:use-module (srfi srfi-35)
28 #:use-module (system foreign)
29 #:use-module (rnrs bytevectors)
30 #:use-module (rnrs io ports)
31 #:export (elf-error?
32 elf-error-elf
33 invalid-segment-size?
34 invalid-segment-size-segment
35
36 elf-dynamic-info
37 elf-dynamic-info?
38 elf-dynamic-info-soname
39 elf-dynamic-info-needed
40 elf-dynamic-info-rpath
41 elf-dynamic-info-runpath
42 expand-origin
43
44 validate-needed-in-runpath
45 strip-runpath))
46
47 ;;; Commentary:
48 ;;;
49 ;;; A gremlin is sort-of like an elf, you know, and this module provides tools
50 ;;; to deal with dynamic-link information from ELF files.
51 ;;;
52 ;;; Code:
53
54 (define-condition-type &elf-error &error
55 elf-error?
56 (elf elf-error-elf))
57
58 (define-condition-type &invalid-segment-size &elf-error
59 invalid-segment-size?
60 (segment invalid-segment-size-segment))
61
62
63 (define (dynamic-link-segment elf)
64 "Return the 'PT_DYNAMIC' segment of ELF--i.e., the segment that contains
65 dynamic linking information."
66 (let ((size (bytevector-length (elf-bytes elf))))
67 (find (lambda (segment)
68 (unless (<= (+ (elf-segment-offset segment)
69 (elf-segment-filesz segment))
70 size)
71 ;; This happens on separate debug output files created by
72 ;; 'strip --only-keep-debug' (Binutils 2.25.)
73 (raise (condition (&invalid-segment-size
74 (elf elf)
75 (segment segment)))))
76
77 (= (elf-segment-type segment) PT_DYNAMIC))
78 (elf-segments elf))))
79
80 (define (word-reader size byte-order)
81 "Return a procedure to read a word of SIZE bytes according to BYTE-ORDER."
82 (case size
83 ((8)
84 (lambda (bv index)
85 (bytevector-u64-ref bv index byte-order)))
86 ((4)
87 (lambda (bv index)
88 (bytevector-u32-ref bv index byte-order)))))
89
90
91 ;; Dynamic entry:
92 ;;
93 ;; typedef struct
94 ;; {
95 ;; Elf64_Sxword d_tag; /* Dynamic entry type */
96 ;; union
97 ;; {
98 ;; Elf64_Xword d_val; /* Integer value */
99 ;; Elf64_Addr d_ptr; /* Address value */
100 ;; } d_un;
101 ;; } Elf64_Dyn;
102
103 (define-record-type <dynamic-entry>
104 (dynamic-entry type value offset)
105 dynamic-entry?
106 (type dynamic-entry-type) ;DT_*
107 (value dynamic-entry-value) ;string | number | ...
108 (offset dynamic-entry-offset)) ;integer
109
110 (define (raw-dynamic-entries elf segment)
111 "Return as a list of <dynamic-entry> for the dynamic entries found in
112 SEGMENT, the 'PT_DYNAMIC' segment of ELF."
113 (define start
114 (elf-segment-offset segment))
115 (define bytes
116 (elf-bytes elf))
117 (define word-size
118 (elf-word-size elf))
119 (define byte-order
120 (elf-byte-order elf))
121 (define read-word
122 (word-reader word-size byte-order))
123
124 (let loop ((offset 0)
125 (result '()))
126 (if (>= offset (elf-segment-memsz segment))
127 (reverse result)
128 (let ((type (read-word bytes (+ start offset)))
129 (value (read-word bytes (+ start offset word-size))))
130 (if (= type DT_NULL) ;finished?
131 (reverse result)
132 (loop (+ offset (* 2 word-size))
133 (cons (dynamic-entry type value
134 (+ start offset word-size))
135 result)))))))
136
137 (define (vma->offset elf vma)
138 "Convert VMA, a virtual memory address, to an offset within ELF.
139
140 Do that by looking at the loadable program segment (PT_LOAD) of ELF that
141 contains VMA and by taking into account that segment's virtual address and
142 offset."
143 ;; See 'offset_from_vma' in Binutils.
144 (define loads
145 (filter (lambda (segment)
146 (= (elf-segment-type segment) PT_LOAD))
147 (elf-segments elf)))
148
149 (let ((load (find (lambda (segment)
150 (let ((vaddr (elf-segment-vaddr segment)))
151 (and (>= vma vaddr)
152 (< vma (+ (elf-segment-memsz segment)
153 vaddr)))))
154 loads)))
155 (+ (- vma (elf-segment-vaddr load))
156 (elf-segment-offset load))))
157
158 (define (dynamic-entries elf segment)
159 "Return all the dynamic entries found in SEGMENT, the 'PT_DYNAMIC' segment
160 of ELF, as a list of <dynamic-entry>. The value of each entry may be a string
161 or an integer depending on the entry type (for instance, the value of
162 DT_NEEDED entries is a string.) Likewise the offset is the offset within the
163 string table if the type is a string."
164 (define entries
165 (raw-dynamic-entries elf segment))
166
167 (define string-table-offset
168 (any (lambda (entry)
169 (and (= (dynamic-entry-type entry) DT_STRTAB)
170 (dynamic-entry-value entry)))
171 entries))
172
173 (define (interpret-dynamic-entry entry)
174 (let ((type (dynamic-entry-type entry))
175 (value (dynamic-entry-value entry)))
176 (cond ((memv type (list DT_NEEDED DT_SONAME DT_RPATH DT_RUNPATH))
177 (if string-table-offset
178 (let* ((offset (vma->offset elf (+ string-table-offset value)))
179 (value (pointer->string
180 (bytevector->pointer (elf-bytes elf) offset))))
181 (dynamic-entry type value offset))
182 (dynamic-entry type value (dynamic-entry-offset entry))))
183 (else
184 (dynamic-entry type value (dynamic-entry-offset entry))))))
185
186 (map interpret-dynamic-entry entries))
187
188 \f
189 ;;;
190 ;;; High-level interface.
191 ;;;
192
193 (define-record-type <elf-dynamic-info>
194 (%elf-dynamic-info soname needed rpath runpath)
195 elf-dynamic-info?
196 (soname elf-dynamic-info-soname)
197 (needed elf-dynamic-info-needed)
198 (rpath elf-dynamic-info-rpath)
199 (runpath elf-dynamic-info-runpath))
200
201 (define search-path->list
202 (let ((not-colon (char-set-complement (char-set #\:))))
203 (lambda (str)
204 "Split STR on ':' characters."
205 (string-tokenize str not-colon))))
206
207 (define (elf-dynamic-info elf)
208 "Return dynamic-link information for ELF as an <elf-dynamic-info> object, or
209 #f if ELF lacks dynamic-link information."
210 (define (matching-entry type)
211 (lambda (entry)
212 (= type (dynamic-entry-type entry))))
213
214 (match (dynamic-link-segment elf)
215 (#f #f)
216 ((? elf-segment? dynamic)
217 (let ((entries (dynamic-entries elf dynamic)))
218 (%elf-dynamic-info (find (matching-entry DT_SONAME) entries)
219 (filter-map (lambda (entry)
220 (and (= (dynamic-entry-type entry)
221 DT_NEEDED)
222 (dynamic-entry-value entry)))
223 entries)
224 (or (and=> (find (matching-entry DT_RPATH)
225 entries)
226 (compose search-path->list
227 dynamic-entry-value))
228 '())
229 (or (and=> (find (matching-entry DT_RUNPATH)
230 entries)
231 (compose search-path->list
232 dynamic-entry-value))
233 '()))))))
234
235 (define %libc-libraries
236 ;; List of libraries as of glibc 2.21 (there are more but those are
237 ;; typically mean to be LD_PRELOADed and thus do not appear as NEEDED.)
238 '("libanl.so"
239 "libcrypt.so"
240 "libc.so"
241 "libdl.so"
242 "libm.so"
243 "libnsl.so" ;NEEDED by nscd
244 "libpthread.so"
245 "libresolv.so"
246 "librt.so"
247 "libutil.so"))
248
249 (define (libc-library? lib)
250 "Return #t if LIB is one of the libraries shipped with the GNU C Library."
251 (find (lambda (libc-lib)
252 (string-prefix? libc-lib lib))
253 %libc-libraries))
254
255 (define (expand-variable str variable value)
256 "Replace occurrences of '$VARIABLE' or '${VARIABLE}' in STR with VALUE."
257 (define variables
258 (list (string-append "$" variable)
259 (string-append "${" variable "}")))
260
261 (let loop ((thing variables)
262 (str str))
263 (match thing
264 (()
265 str)
266 ((head tail ...)
267 (let ((index (string-contains str head))
268 (len (string-length head)))
269 (loop (if index variables tail)
270 (if index
271 (string-replace str value
272 index (+ index len))
273 str)))))))
274
275 (define (expand-origin str directory)
276 "Replace occurrences of '$ORIGIN' in STR with DIRECTORY."
277 (expand-variable str "ORIGIN" directory))
278
279 (define* (validate-needed-in-runpath file
280 #:key (always-found? libc-library?))
281 "Return #t if all the libraries listed as FILE's 'DT_NEEDED' entries are
282 present in its RUNPATH, or if FILE lacks dynamic-link information. Return #f
283 otherwise. Libraries whose name matches ALWAYS-FOUND? are considered to be
284 always available."
285 (guard (c ((invalid-segment-size? c)
286 (let ((segment (invalid-segment-size-segment c)))
287 (format (current-error-port)
288 "~a: error: offset + size of segment ~a (type ~a) \
289 exceeds total size~%"
290 file
291 (elf-segment-index segment)
292 (elf-segment-type segment))
293 #f)))
294
295 (let* ((elf (call-with-input-file file
296 (compose parse-elf get-bytevector-all)))
297 (expand (cute expand-origin <> (dirname file)))
298 (dyninfo (elf-dynamic-info elf)))
299 (when dyninfo
300 ;; XXX: In theory we should also expand $PLATFORM and $LIB, but these
301 ;; appear to be really unused.
302 (let* ((expanded (map expand (elf-dynamic-info-runpath dyninfo)))
303 (runpath (filter store-file-name? expanded))
304 (bogus (remove store-file-name? expanded))
305 (needed (remove always-found?
306 (elf-dynamic-info-needed dyninfo)))
307 (not-found (remove (cut search-path runpath <>)
308 needed)))
309 (unless (null? bogus)
310 (format (current-error-port)
311 "~a: warning: RUNPATH contains bogus entries: ~s~%"
312 file bogus))
313
314 (for-each (lambda (lib)
315 (format (current-error-port)
316 "~a: error: depends on '~a', which cannot \
317 be found in RUNPATH ~s~%"
318 file lib runpath))
319 not-found)
320 ;; (when (null? not-found)
321 ;; (format (current-error-port) "~a is OK~%" file))
322 (null? not-found))))))
323
324 (define (strip-runpath file)
325 "Remove from the DT_RUNPATH of FILE any entries that are not necessary
326 according to DT_NEEDED."
327 (define (minimal-runpath needed runpath)
328 (filter (lambda (directory)
329 (and (string-prefix? "/" directory)
330 (any (lambda (lib)
331 (file-exists? (string-append directory "/" lib)))
332 needed)))
333 runpath))
334
335 (define port
336 (open-file file "r+b"))
337
338 (catch #t
339 (lambda ()
340 (let* ((elf (parse-elf (get-bytevector-all port)))
341 (entries (dynamic-entries elf (dynamic-link-segment elf)))
342 (needed (filter-map (lambda (entry)
343 (and (= (dynamic-entry-type entry)
344 DT_NEEDED)
345 (dynamic-entry-value entry)))
346 entries))
347 (runpath (find (lambda (entry)
348 (= DT_RUNPATH (dynamic-entry-type entry)))
349 entries))
350 (old (search-path->list
351 (dynamic-entry-value runpath)))
352 (new (minimal-runpath needed old)))
353 (unless (equal? old new)
354 (format (current-error-port)
355 "~a: stripping RUNPATH to ~s (removed ~s)~%"
356 file new
357 (lset-difference string=? old new))
358 (seek port (dynamic-entry-offset runpath) SEEK_SET)
359 (put-bytevector port (string->utf8 (string-join new ":")))
360 (put-u8 port 0))
361 (close-port port)
362 new))
363 (lambda (key . args)
364 (false-if-exception (close-port port))
365 (apply throw key args))))
366
367 ;;; gremlin.scm ends here