linker.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. ;;; Guile ELF linker
  2. ;; Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Commentary:
  17. ;;;
  18. ;;; A linker combines several linker objects into an executable or a
  19. ;;; loadable library.
  20. ;;;
  21. ;;; There are several common formats for libraries out there. Since
  22. ;;; Guile includes its own linker and loader, we are free to choose any
  23. ;;; format, or make up our own.
  24. ;;;
  25. ;;; There are essentially two requirements for a linker format:
  26. ;;; libraries should be able to be loaded with the minimal amount of
  27. ;;; work; and they should support introspection in some way, in order to
  28. ;;; enable good debugging.
  29. ;;;
  30. ;;; These requirements are somewhat at odds, as loading should not have
  31. ;;; to stumble over features related to introspection. It so happens
  32. ;;; that a lot of smart people have thought about this situation, and
  33. ;;; the ELF format embodies the outcome of their thinking. Guile uses
  34. ;;; ELF as its format, regardless of the platform's native library
  35. ;;; format. It's not inconceivable that Guile could interoperate with
  36. ;;; the native dynamic loader at some point, but it's not a near-term
  37. ;;; goal.
  38. ;;;
  39. ;;; Guile's linker takes a list of objects, sorts them according to
  40. ;;; similarity from the perspective of the loader, then writes them out
  41. ;;; into one big bytevector in ELF format.
  42. ;;;
  43. ;;; It is often the case that different parts of a library need to refer
  44. ;;; to each other. For example, program text may need to refer to a
  45. ;;; constant from writable memory. When the linker places sections
  46. ;;; (linker objects) into specific locations in the linked bytevector,
  47. ;;; it needs to fix up those references. This process is called
  48. ;;; /relocation/. References needing relocations are recorded in
  49. ;;; "linker-reloc" objects, and collected in a list in each
  50. ;;; "linker-object". The actual definitions of the references are
  51. ;;; stored in "linker-symbol" objects, also collected in a list in each
  52. ;;; "linker-object".
  53. ;;;
  54. ;;; By default, the ELF files created by the linker include some padding
  55. ;;; so that different parts of the file can be loaded in with different
  56. ;;; permissions. For example, some parts of the file are read-only and
  57. ;;; thus can be shared between processes. Some parts of the file don't
  58. ;;; need to be loaded at all. However this padding can be too much for
  59. ;;; interactive compilation, when the code is never written out to disk;
  60. ;;; in that case, pass #:page-aligned? #f to `link-elf'.
  61. ;;;
  62. ;;; Code:
  63. (define-module (system vm linker)
  64. #:use-module (rnrs bytevectors)
  65. #:use-module (system foreign)
  66. #:use-module (system base target)
  67. #:use-module ((srfi srfi-1) #:select (append-map))
  68. #:use-module (srfi srfi-9)
  69. #:use-module (ice-9 receive)
  70. #:use-module (ice-9 vlist)
  71. #:use-module (ice-9 match)
  72. #:use-module (system vm elf)
  73. #:export (make-linker-reloc
  74. make-linker-symbol
  75. make-linker-object
  76. linker-object?
  77. linker-object-section
  78. linker-object-bv
  79. linker-object-relocs
  80. (linker-object-symbols* . linker-object-symbols)
  81. make-string-table
  82. string-table-intern!
  83. link-string-table!
  84. link-elf))
  85. (define-syntax fold-values
  86. (lambda (x)
  87. (syntax-case x ()
  88. ((_ proc list seed ...)
  89. (with-syntax (((s ...) (generate-temporaries #'(seed ...))))
  90. #'(let ((p proc))
  91. (let lp ((l list) (s seed) ...)
  92. (match l
  93. (() (values s ...))
  94. ((elt . l)
  95. (call-with-values (lambda () (p elt s ...))
  96. (lambda (s ...) (lp l s ...))))))))))))
  97. ;; A relocation records a reference to a symbol. When the symbol is
  98. ;; resolved to an address, the reloc location will be updated to point
  99. ;; to the address.
  100. ;;
  101. ;; Two types. Abs32/1 and Abs64/1 are absolute offsets in bytes.
  102. ;; Rel32/4 is a relative signed offset in 32-bit units. Either can have
  103. ;; an arbitrary addend as well.
  104. ;;
  105. (define-record-type <linker-reloc>
  106. (make-linker-reloc type loc addend symbol)
  107. linker-reloc?
  108. (type linker-reloc-type) ;; rel32/4, abs32/1, abs64/1
  109. (loc linker-reloc-loc)
  110. (addend linker-reloc-addend)
  111. (symbol linker-reloc-symbol))
  112. ;; A symbol is an association between a name and an address. The
  113. ;; address is always in regard to some particular address space. When
  114. ;; objects come into the linker, their symbols live in the object
  115. ;; address space. When the objects are allocated into ELF segments, the
  116. ;; symbols will be relocated into memory address space, corresponding to
  117. ;; the position the ELF will be loaded at.
  118. ;;
  119. (define-record-type <linker-symbol>
  120. (make-linker-symbol name address)
  121. linker-symbol?
  122. (name linker-symbol-name)
  123. (address linker-symbol-address))
  124. (define-record-type <linker-object>
  125. (%make-linker-object section bv relocs symbols)
  126. linker-object?
  127. (section linker-object-section)
  128. (bv linker-object-bv)
  129. (relocs linker-object-relocs)
  130. (symbols linker-object-symbols))
  131. (define (make-linker-object section bv relocs symbols)
  132. "Create a linker object with the @code{<elf-section>} header
  133. @var{section}, bytevector contents @var{bv}, list of linker relocations
  134. @var{relocs}, and list of linker symbols @var{symbols}."
  135. (%make-linker-object section bv relocs
  136. ;; Hide a symbol to the beginning of the section
  137. ;; in the symbols.
  138. (cons (make-linker-symbol (gensym "*section*") 0)
  139. symbols)))
  140. (define (linker-object-section-symbol object)
  141. "Return the linker symbol corresponding to the start of this section."
  142. (car (linker-object-symbols object)))
  143. (define (linker-object-symbols* object)
  144. "Return the linker symbols defined by the user for this this section."
  145. (cdr (linker-object-symbols object)))
  146. (define-record-type <string-table>
  147. (%make-string-table strings linked?)
  148. string-table?
  149. (strings string-table-strings set-string-table-strings!)
  150. (linked? string-table-linked? set-string-table-linked?!))
  151. (define (make-string-table)
  152. "Return a string table with one entry: the empty string."
  153. (%make-string-table '(("" 0 #vu8())) #f))
  154. (define (string-table-length strings)
  155. "Return the number of bytes needed for the @var{strings}."
  156. (match strings
  157. (((str pos bytes) . _)
  158. ;; The + 1 is for the trailing NUL byte.
  159. (+ pos (bytevector-length bytes) 1))))
  160. (define (string-table-intern! table str)
  161. "Ensure that @var{str} is present in the string table @var{table}.
  162. Returns the byte index of the string in that table."
  163. (match table
  164. (($ <string-table> strings linked?)
  165. (match (assoc str strings)
  166. ((_ pos _) pos)
  167. (#f
  168. (let ((next (string-table-length strings)))
  169. (when linked?
  170. (error "string table already linked, can't intern" table str))
  171. (set-string-table-strings! table
  172. (cons (list str next (string->utf8 str))
  173. strings))
  174. next))))))
  175. (define (link-string-table! table)
  176. "Link the functional string table @var{table} into a sequence of
  177. bytes, suitable for use as the contents of an ELF string table section."
  178. (match table
  179. (($ <string-table> strings #f)
  180. (let ((out (make-bytevector (string-table-length strings) 0)))
  181. (for-each
  182. (match-lambda
  183. ((_ pos bytes)
  184. (bytevector-copy! bytes 0 out pos (bytevector-length bytes))))
  185. strings)
  186. (set-string-table-linked?! table #t)
  187. out))))
  188. (define (segment-kind section)
  189. "Return the type of segment needed to store @var{section}, as a pair.
  190. The car is the @code{PT_} segment type, or @code{#f} if the section
  191. doesn't need to be present in a loadable segment. The cdr is a bitfield
  192. of associated @code{PF_} permissions."
  193. (let ((flags (elf-section-flags section)))
  194. (cons (cond
  195. ((= (elf-section-type section) SHT_DYNAMIC) PT_DYNAMIC)
  196. ;; Sections without SHF_ALLOC don't go in segments.
  197. ((zero? flags) #f)
  198. (else PT_LOAD))
  199. (logior (if (zero? (logand SHF_ALLOC flags))
  200. 0
  201. PF_R)
  202. (if (zero? (logand SHF_EXECINSTR flags))
  203. 0
  204. PF_X)
  205. (if (zero? (logand SHF_WRITE flags))
  206. 0
  207. PF_W)))))
  208. (define (count-segments objects)
  209. "Return the total number of segments needed to represent the linker
  210. objects in @var{objects}, including the segment needed for the ELF
  211. header and segment table."
  212. (length
  213. (fold-values (lambda (object kinds)
  214. (let ((kind (segment-kind (linker-object-section object))))
  215. (if (and (car kind) (not (member kind kinds)))
  216. (cons kind kinds)
  217. kinds)))
  218. objects
  219. ;; We know there will be at least one segment,
  220. ;; containing at least the header and segment table.
  221. (list (cons PT_LOAD PF_R)))))
  222. (define (group-by-cars ls)
  223. (let lp ((ls ls) (k #f) (group #f) (out '()))
  224. (match ls
  225. (()
  226. (reverse!
  227. (if group
  228. (cons (cons k (reverse! group)) out)
  229. out)))
  230. (((k* . v) . ls)
  231. (if (and group (equal? k k*))
  232. (lp ls k (cons v group) out)
  233. (lp ls k* (list v)
  234. (if group
  235. (cons (cons k (reverse! group)) out)
  236. out)))))))
  237. (define (collate-objects-into-segments objects)
  238. "Given the list of linker objects @var{objects}, group them into
  239. contiguous ELF segments of the same type and flags. The result is an
  240. alist that maps segment types to lists of linker objects. See
  241. @code{segment-type} for a description of segment types. Within a
  242. segment, the order of the linker objects is preserved."
  243. (group-by-cars
  244. (stable-sort!
  245. (map (lambda (o)
  246. (cons (segment-kind (linker-object-section o)) o))
  247. objects)
  248. (lambda (x y)
  249. (let* ((x-kind (car x)) (y-kind (car y))
  250. (x-type (car x-kind)) (y-type (car y-kind))
  251. (x-flags (cdr x-kind)) (y-flags (cdr y-kind))
  252. (x-section (linker-object-section (cdr x)))
  253. (y-section (linker-object-section (cdr y))))
  254. (cond
  255. ((not (equal? x-kind y-kind))
  256. (cond
  257. ((and x-type y-type)
  258. (cond
  259. ((not (equal? x-flags y-flags))
  260. (< x-flags y-flags))
  261. (else
  262. (< x-type y-type))))
  263. (else
  264. (not y-type))))
  265. ((not (equal? (elf-section-type x-section)
  266. (elf-section-type y-section)))
  267. (cond
  268. ((equal? (elf-section-type x-section) SHT_NOBITS) #t)
  269. ((equal? (elf-section-type y-section) SHT_NOBITS) #f)
  270. (else (< (elf-section-type x-section)
  271. (elf-section-type y-section)))))
  272. (else
  273. ;; Leave them in the initial order. This allows us to ensure
  274. ;; that the ELF header is written first.
  275. #f)))))))
  276. (define (align address alignment)
  277. (if (zero? alignment)
  278. address
  279. (+ address
  280. (modulo (- alignment (modulo address alignment)) alignment))))
  281. (define (relocate-section-header sec addr)
  282. "Return a new section header, just like @var{sec} but with its
  283. @code{addr} and @code{offset} set to @var{addr}."
  284. (make-elf-section #:index (elf-section-index sec)
  285. #:name (elf-section-name sec)
  286. #:type (elf-section-type sec)
  287. #:flags (elf-section-flags sec)
  288. #:addr addr
  289. #:offset addr
  290. #:size (elf-section-size sec)
  291. #:link (elf-section-link sec)
  292. #:info (elf-section-info sec)
  293. #:addralign (elf-section-addralign sec)
  294. #:entsize (elf-section-entsize sec)))
  295. (define *page-size* 4096)
  296. (define (add-symbols symbols offset symtab)
  297. "Add @var{symbols} to the symbol table @var{symtab}, relocating them
  298. from object address space to memory address space. Returns a new symbol
  299. table."
  300. (fold-values
  301. (lambda (symbol symtab)
  302. (let ((name (linker-symbol-name symbol))
  303. (addr (linker-symbol-address symbol)))
  304. (when (vhash-assq name symtab)
  305. (error "duplicate symbol" name))
  306. (vhash-consq name (make-linker-symbol name (+ addr offset)) symtab)))
  307. symbols
  308. symtab))
  309. (define (allocate-segment write-segment-header!
  310. phidx type flags objects addr symtab alignment)
  311. "Given a list of linker objects that should go in a segment, the type
  312. and flags that the segment should have, and the address at which the
  313. segment should start, compute the positions that each object should have
  314. in the segment.
  315. Returns three values: the address of the next byte after the segment, a
  316. list of relocated objects, and the symbol table. The symbol table is
  317. the same as @var{symtab}, augmented with the symbols defined in
  318. @var{objects}, relocated to their positions in the image.
  319. In what is something of a quirky interface, this routine also patches up
  320. the segment table using @code{write-segment-header!}."
  321. (let* ((alignment (fold-values (lambda (o alignment)
  322. (lcm (elf-section-addralign
  323. (linker-object-section o))
  324. alignment))
  325. objects
  326. alignment))
  327. (addr (align addr alignment)))
  328. (receive (objects endaddr symtab)
  329. (fold-values
  330. (lambda (o out addr symtab)
  331. (let* ((section (linker-object-section o))
  332. (addr (align addr (elf-section-addralign section))))
  333. (values
  334. (cons (make-linker-object
  335. (relocate-section-header section addr)
  336. (linker-object-bv o)
  337. (linker-object-relocs o)
  338. (linker-object-symbols o))
  339. out)
  340. (+ addr (elf-section-size section))
  341. (add-symbols (linker-object-symbols o) addr symtab))))
  342. objects
  343. '() addr symtab)
  344. (when type
  345. (write-segment-header!
  346. (make-elf-segment #:index phidx #:type type
  347. #:offset addr #:vaddr addr
  348. #:filesz (- endaddr addr) #:memsz (- endaddr addr)
  349. #:flags flags #:align alignment)))
  350. (values endaddr
  351. (reverse objects)
  352. symtab))))
  353. (define (process-reloc reloc bv section-offset symtab endianness)
  354. "Process a relocation. Given that a section containing @var{reloc}
  355. was just written into the image @var{bv} at offset @var{section-offset},
  356. fix it up so that its reference points to the correct position of its
  357. symbol, as present in @var{symtab}."
  358. (match (vhash-assq (linker-reloc-symbol reloc) symtab)
  359. (#f
  360. (error "Undefined symbol" (linker-reloc-symbol reloc)))
  361. ((name . symbol)
  362. ;; The reloc was written at LOC bytes after SECTION-OFFSET.
  363. (let* ((offset (+ (linker-reloc-loc reloc) section-offset))
  364. (target (linker-symbol-address symbol)))
  365. (case (linker-reloc-type reloc)
  366. ((rel32/4)
  367. (let ((diff (- target offset)))
  368. (unless (zero? (modulo diff 4))
  369. (error "Bad offset" reloc symbol offset))
  370. (bytevector-s32-set! bv offset
  371. (+ (/ diff 4) (linker-reloc-addend reloc))
  372. endianness)))
  373. ((abs32/1)
  374. (bytevector-u32-set! bv offset target endianness))
  375. ((abs64/1)
  376. (bytevector-u64-set! bv offset target endianness))
  377. (else
  378. (error "bad reloc type" reloc)))))))
  379. (define (write-linker-object bv o symtab endianness)
  380. "Write the bytevector for the section wrapped by the linker object
  381. @var{o} into the image @var{bv}. The section header in @var{o} should
  382. already be relocated its final position in the image. Any relocations
  383. in the section will be processed to point to the correct symbol
  384. locations, as given in @var{symtab}."
  385. (let* ((section (linker-object-section o))
  386. (offset (elf-section-offset section))
  387. (len (elf-section-size section))
  388. (bytes (linker-object-bv o))
  389. (relocs (linker-object-relocs o)))
  390. (unless (= offset (elf-section-addr section))
  391. (error "offset != addr" section))
  392. (if (not (= (elf-section-type section) SHT_NOBITS))
  393. (begin
  394. (if (not (= len (bytevector-length bytes)))
  395. (error "unexpected length" section bytes))
  396. (bytevector-copy! bytes 0 bv offset len)
  397. (for-each (lambda (reloc)
  398. (process-reloc reloc bv offset symtab endianness))
  399. relocs)))))
  400. (define (find-shstrndx objects)
  401. "Find the section name string table in @var{objects}, and return its
  402. section index."
  403. (or-map (lambda (object)
  404. (let* ((section (linker-object-section object))
  405. (bv (linker-object-bv object))
  406. (name (elf-section-name section)))
  407. (and (= (elf-section-type section) SHT_STRTAB)
  408. (equal? (false-if-exception (string-table-ref bv name))
  409. ".shstrtab")
  410. (elf-section-index section))))
  411. objects))
  412. (define (add-elf-objects objects endianness word-size)
  413. "Given the list of linker objects supplied by the user, add linker
  414. objects corresponding to parts of the ELF file: the null object, the ELF
  415. header, and the section table.
  416. Both of these internal objects include relocs, allowing their
  417. inter-object references to be patched up when the final image allocation
  418. is known. There is special support for patching up the segment table,
  419. however. Because the segment table needs to know the segment sizes,
  420. which is the difference between two symbols in image space, and there is
  421. no reloc kind that is the difference between two symbols, we make a hack
  422. and return a closure that patches up segment table entries. It seems to
  423. work.
  424. Returns two values: the procedure to patch the segment table, and the
  425. list of objects, augmented with objects for the special ELF sections."
  426. (define phoff (elf-header-len word-size))
  427. (define phentsize (elf-program-header-len word-size))
  428. (define shentsize (elf-section-header-len word-size))
  429. (define shnum (+ (length objects) 3))
  430. (define reloc-kind
  431. (case word-size
  432. ((4) 'abs32/1)
  433. ((8) 'abs64/1)
  434. (else (error "bad word size" word-size))))
  435. ;; ELF requires that the first entry in the section table be of type
  436. ;; SHT_NULL.
  437. ;;
  438. (define (make-null-section)
  439. (make-linker-object (make-elf-section #:index 0 #:type SHT_NULL
  440. #:flags 0 #:addralign 0)
  441. #vu8() '() '()))
  442. ;; The ELF header and the segment table.
  443. ;;
  444. (define (make-header phnum index shoff-label)
  445. (let* ((header (make-elf #:byte-order endianness #:word-size word-size
  446. #:phoff phoff #:phnum phnum #:phentsize phentsize
  447. #:shoff 0 #:shnum shnum #:shentsize shentsize
  448. #:shstrndx (or (find-shstrndx objects) SHN_UNDEF)))
  449. (shoff-reloc (make-linker-reloc reloc-kind
  450. (elf-header-shoff-offset word-size)
  451. 0
  452. shoff-label))
  453. (size (+ phoff (* phnum phentsize)))
  454. (bv (make-bytevector size 0)))
  455. (write-elf-header bv header)
  456. ;; Leave the segment table uninitialized; it will be filled in
  457. ;; later by calls to the write-segment-header! closure.
  458. (make-linker-object (make-elf-section #:index index #:type SHT_PROGBITS
  459. #:flags SHF_ALLOC #:size size)
  460. bv
  461. (list shoff-reloc)
  462. '())))
  463. ;; The section table.
  464. ;;
  465. (define (make-footer objects shoff-label)
  466. (let* ((size (* shentsize shnum))
  467. (bv (make-bytevector size 0))
  468. (section-table (make-elf-section #:index (length objects)
  469. #:type SHT_PROGBITS
  470. #:flags 0
  471. #:size size)))
  472. (define (write-and-reloc section-label section relocs)
  473. (let ((offset (* shentsize (elf-section-index section))))
  474. (write-elf-section-header bv offset endianness word-size section)
  475. (if (= (elf-section-type section) SHT_NULL)
  476. relocs
  477. (cons* (make-linker-reloc
  478. reloc-kind
  479. (+ offset (elf-section-header-addr-offset word-size))
  480. 0
  481. section-label)
  482. (make-linker-reloc
  483. reloc-kind
  484. (+ offset (elf-section-header-offset-offset word-size))
  485. 0
  486. section-label)
  487. relocs))))
  488. (let ((relocs (fold-values
  489. (lambda (object relocs)
  490. (write-and-reloc
  491. (linker-symbol-name
  492. (linker-object-section-symbol object))
  493. (linker-object-section object)
  494. relocs))
  495. objects
  496. (write-and-reloc shoff-label section-table '()))))
  497. (%make-linker-object section-table bv relocs
  498. (list (make-linker-symbol shoff-label 0))))))
  499. (let* ((null-section (make-null-section))
  500. (objects (cons null-section objects))
  501. (shoff (gensym "*section-table*"))
  502. (header (make-header (count-segments objects) (length objects) shoff))
  503. (objects (cons header objects))
  504. (footer (make-footer objects shoff))
  505. (objects (cons footer objects)))
  506. ;; The header includes the segment table, which needs offsets and
  507. ;; sizes of the segments. Normally we would use relocs to rewrite
  508. ;; these values, but there is no reloc type that would allow us to
  509. ;; compute size. Such a reloc would need to take the difference
  510. ;; between two symbols, and it's probably a bad idea architecturally
  511. ;; to create one.
  512. ;;
  513. ;; So instead we return a closure to patch up the segment table.
  514. ;; Normally we'd shy away from such destructive interfaces, but it's
  515. ;; OK as we create the header section ourselves.
  516. ;;
  517. (define (write-segment-header! segment)
  518. (let ((bv (linker-object-bv header))
  519. (offset (+ phoff (* (elf-segment-index segment) phentsize))))
  520. (write-elf-program-header bv offset endianness word-size segment)))
  521. (values write-segment-header! objects)))
  522. (define (allocate-elf objects page-aligned? endianness word-size)
  523. "Lay out @var{objects} into an ELF image, computing the size of the
  524. file, the positions of the objects, and the global symbol table.
  525. If @var{page-aligned?} is true, read-only and writable data are
  526. separated so that only those writable parts of the image need be mapped
  527. with writable permissions. This makes the resulting image larger. It
  528. is more suitable to situations where you would write a file out to disk
  529. and read it in with mmap. Otherwise if @var{page-aligned?} is false,
  530. sections default to 8-byte alignment.
  531. Returns three values: the total image size, a list of objects with
  532. relocated headers, and the global symbol table."
  533. (receive (write-segment-header! objects)
  534. (add-elf-objects objects endianness word-size)
  535. (let lp ((seglists (collate-objects-into-segments objects))
  536. (objects '())
  537. (phidx 0)
  538. (addr 0)
  539. (symtab vlist-null)
  540. (prev-flags 0))
  541. (match seglists
  542. ((((type . flags) objs-in ...) seglists ...)
  543. (receive (addr objs-out symtab)
  544. (allocate-segment
  545. write-segment-header!
  546. phidx type flags objs-in addr symtab
  547. (if (and page-aligned?
  548. (not (= flags prev-flags))
  549. ;; Allow sections that are not in
  550. ;; loadable segments to share pages
  551. ;; with PF_R segments.
  552. (not (and (not type) (= PF_R prev-flags))))
  553. *page-size*
  554. 8))
  555. (lp seglists
  556. (fold-values cons objs-out objects)
  557. (if type (1+ phidx) phidx)
  558. addr
  559. symtab
  560. flags)))
  561. (()
  562. (values addr
  563. (reverse objects)
  564. symtab))))))
  565. (define (check-section-numbers objects)
  566. "Verify that taken as a whole, that all objects have distinct,
  567. contiguous section numbers, starting from 1. (Section 0 is the null
  568. section.)"
  569. (let* ((nsections (1+ (length objects))) ; 1+ for initial NULL section.
  570. (sections (make-vector nsections #f)))
  571. (for-each (lambda (object)
  572. (let ((n (elf-section-index (linker-object-section object))))
  573. (cond
  574. ((< n 1)
  575. (error "Invalid section number" object))
  576. ((>= n nsections)
  577. (error "Invalid section number" object))
  578. ((vector-ref sections n)
  579. (error "Duplicate section" (vector-ref sections n) object))
  580. (else
  581. (vector-set! sections n object)))))
  582. objects)))
  583. ;; Given a list of linker objects, collate the objects into segments,
  584. ;; allocate the segments, allocate the ELF bytevector, and write the
  585. ;; segments into the bytevector, relocating as we go.
  586. ;;
  587. (define* (link-elf objects #:key
  588. (page-aligned? #t)
  589. (endianness (target-endianness))
  590. (word-size (target-word-size)))
  591. "Create an ELF image from the linker objects, @var{objects}.
  592. If @var{page-aligned?} is true, read-only and writable data are
  593. separated so that only those writable parts of the image need be mapped
  594. with writable permissions. This is suitable for situations where you
  595. would write a file out to disk and read it in with @code{mmap}.
  596. Otherwise if @var{page-aligned?} is false, sections default to 8-byte
  597. alignment.
  598. Returns a bytevector."
  599. (check-section-numbers objects)
  600. (receive (size objects symtab)
  601. (allocate-elf objects page-aligned? endianness word-size)
  602. (let ((bv (make-bytevector size 0)))
  603. (for-each
  604. (lambda (object)
  605. (write-linker-object bv object symtab endianness))
  606. objects)
  607. bv)))