1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2018 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 (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)
34 invalid-segment-size-segment
38 elf-dynamic-info-soname
39 elf-dynamic-info-needed
40 elf-dynamic-info-rpath
41 elf-dynamic-info-runpath
44 validate-needed-in-runpath
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.
54 (define-condition-type &elf-error &error
58 (define-condition-type &invalid-segment-size &elf-error
60 (segment invalid-segment-size-segment))
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))
71 ;; This happens on separate debug output files created by
72 ;; 'strip --only-keep-debug' (Binutils 2.25.)
73 (raise (condition (&invalid-segment-size
77 (= (elf-segment-type segment) PT_DYNAMIC))
80 (define (word-reader size byte-order)
81 "Return a procedure to read a word of SIZE bytes according to BYTE-ORDER."
85 (bytevector-u64-ref bv index byte-order)))
88 (bytevector-u32-ref bv index byte-order)))))
95 ;; Elf64_Sxword d_tag; /* Dynamic entry type */
98 ;; Elf64_Xword d_val; /* Integer value */
99 ;; Elf64_Addr d_ptr; /* Address value */
103 (define-record-type <dynamic-entry>
104 (dynamic-entry type value offset)
106 (type dynamic-entry-type) ;DT_*
107 (value dynamic-entry-value) ;string | number | ...
108 (offset dynamic-entry-offset)) ;integer
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."
114 (elf-segment-offset segment))
120 (elf-byte-order elf))
122 (word-reader word-size byte-order))
124 (let loop ((offset 0)
126 (if (>= offset (elf-segment-memsz segment))
128 (let ((type (read-word bytes (+ start offset)))
129 (value (read-word bytes (+ start offset word-size))))
130 (if (= type DT_NULL) ;finished?
132 (loop (+ offset (* 2 word-size))
133 (cons (dynamic-entry type value
134 (+ start offset word-size))
137 (define (vma->offset elf vma)
138 "Convert VMA, a virtual memory address, to an offset within ELF.
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
143 ;; See 'offset_from_vma' in Binutils.
145 (filter (lambda (segment)
146 (= (elf-segment-type segment) PT_LOAD))
149 (let ((load (find (lambda (segment)
150 (let ((vaddr (elf-segment-vaddr segment)))
152 (< vma (+ (elf-segment-memsz segment)
155 (+ (- vma (elf-segment-vaddr load))
156 (elf-segment-offset load))))
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."
165 (raw-dynamic-entries elf segment))
167 (define string-table-offset
169 (and (= (dynamic-entry-type entry) DT_STRTAB)
170 (dynamic-entry-value entry)))
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))))
184 (dynamic-entry type value (dynamic-entry-offset entry))))))
186 (map interpret-dynamic-entry entries))
190 ;;; High-level interface.
193 (define-record-type <elf-dynamic-info>
194 (%elf-dynamic-info soname needed rpath runpath)
196 (soname elf-dynamic-info-soname)
197 (needed elf-dynamic-info-needed)
198 (rpath elf-dynamic-info-rpath)
199 (runpath elf-dynamic-info-runpath))
201 (define search-path->list
202 (let ((not-colon (char-set-complement (char-set #\:))))
204 "Split STR on ':' characters."
205 (string-tokenize str not-colon))))
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)
212 (= type (dynamic-entry-type entry))))
214 (match (dynamic-link-segment elf)
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)
222 (dynamic-entry-value entry)))
224 (or (and=> (find (matching-entry DT_RPATH)
226 (compose search-path->list
227 dynamic-entry-value))
229 (or (and=> (find (matching-entry DT_RUNPATH)
231 (compose search-path->list
232 dynamic-entry-value))
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.)
243 "libnsl.so" ;NEEDED by nscd
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))
255 (define (expand-variable str variable value)
256 "Replace occurrences of '$VARIABLE' or '${VARIABLE}' in STR with VALUE."
258 (list (string-append "$" variable)
259 (string-append "${" variable "}")))
261 (let loop ((thing variables)
267 (let ((index (string-contains str head))
268 (len (string-length head)))
269 (loop (if index variables tail)
271 (string-replace str value
275 (define (expand-origin str directory)
276 "Replace occurrences of '$ORIGIN' in STR with DIRECTORY."
277 (expand-variable str "ORIGIN" directory))
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
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~%"
291 (elf-segment-index segment)
292 (elf-segment-type segment))
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)))
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 <>)
309 (unless (null? bogus)
310 (format (current-error-port)
311 "~a: warning: RUNPATH contains bogus entries: ~s~%"
314 (for-each (lambda (lib)
315 (format (current-error-port)
316 "~a: error: depends on '~a', which cannot \
317 be found in RUNPATH ~s~%"
320 ;; (when (null? not-found)
321 ;; (format (current-error-port) "~a is OK~%" file))
322 (null? not-found))))))
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)
331 (file-exists? (string-append directory "/" lib)))
336 (open-file file "r+b"))
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)
345 (dynamic-entry-value entry)))
347 (runpath (find (lambda (entry)
348 (= DT_RUNPATH (dynamic-entry-type entry)))
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)~%"
357 (lset-difference string=? old new))
358 (seek port (dynamic-entry-offset runpath) SEEK_SET)
359 (put-bytevector port (string->utf8 (string-join new ":")))
364 (false-if-exception (close-port port))
365 (apply throw key args))))
367 ;;; gremlin.scm ends here