gremlin.scm 13 KB

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