linker.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. ;;; Guile ELF linker
  2. ;; Copyright (C) 2011, 2012, 2013, 2014, 2018, 2023 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 (rnrs bytevectors gnu)
  66. #:use-module (system base target)
  67. #:use-module (srfi srfi-9)
  68. #:use-module (ice-9 binary-ports)
  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-name
  78. linker-object-section
  79. linker-object-size
  80. linker-object-writer
  81. linker-object-relocs
  82. (linker-object-symbols* . linker-object-symbols)
  83. make-string-table
  84. string-table-intern!
  85. string-table-size
  86. string-table-writer
  87. link-elf))
  88. (define-syntax fold-values
  89. (lambda (x)
  90. (syntax-case x ()
  91. ((_ proc list seed ...)
  92. (with-syntax (((s ...) (generate-temporaries #'(seed ...))))
  93. #'(let ((p proc))
  94. (let lp ((l list) (s seed) ...)
  95. (match l
  96. (() (values s ...))
  97. ((elt . l)
  98. (call-with-values (lambda () (p elt s ...))
  99. (lambda (s ...) (lp l s ...))))))))))))
  100. ;; A relocation records a reference to a symbol. When the symbol is
  101. ;; resolved to an address, the reloc location will be updated to point
  102. ;; to the address.
  103. ;;
  104. ;; Two types. Abs32/1 and Abs64/1 are absolute offsets in bytes.
  105. ;; Rel32/1 and Rel32/1 are relative signed offsets, in 8-bit or 32-bit
  106. ;; units, respectively. Either can have an arbitrary addend as well.
  107. ;;
  108. (define-record-type <linker-reloc>
  109. (make-linker-reloc type loc addend symbol)
  110. linker-reloc?
  111. (type linker-reloc-type) ;; rel32/1, rel32/4, abs32/1, abs64/1
  112. (loc linker-reloc-loc)
  113. (addend linker-reloc-addend)
  114. (symbol linker-reloc-symbol))
  115. ;; A symbol is an association between a name and an address. The
  116. ;; address is always in regard to some particular address space. When
  117. ;; objects come into the linker, their symbols live in the object
  118. ;; address space. When the objects are allocated into ELF segments, the
  119. ;; symbols will be relocated into memory address space, corresponding to
  120. ;; the position the ELF will be loaded at.
  121. ;;
  122. (define-record-type <linker-symbol>
  123. (make-linker-symbol name address)
  124. linker-symbol?
  125. (name linker-symbol-name)
  126. (address linker-symbol-address))
  127. (define-record-type <linker-object>
  128. (%make-linker-object name section size writer relocs symbols)
  129. linker-object?
  130. (name linker-object-name)
  131. (section linker-object-section)
  132. (size linker-object-size)
  133. (writer linker-object-writer set-linker-object-writer!)
  134. (relocs linker-object-relocs)
  135. (symbols linker-object-symbols))
  136. (define (make-linker-object name section size writer relocs symbols)
  137. "Create a linker object named @var{name} (a string, or #f for no name),
  138. @code{<elf-section>} header @var{section}, its @var{size} in bytes,
  139. a procedure @code{writer} to write its contents to a bytevector, a
  140. list of linker relocations @var{relocs}, and list of linker symbols
  141. @var{symbols}."
  142. (%make-linker-object name section size writer relocs
  143. ;; Hide a symbol to the beginning of the section
  144. ;; in the symbols.
  145. (cons (make-linker-symbol (gensym "*section*") 0)
  146. symbols)))
  147. (define (linker-object-section-symbol object)
  148. "Return the linker symbol corresponding to the start of this section."
  149. (car (linker-object-symbols object)))
  150. (define (linker-object-symbols* object)
  151. "Return the linker symbols defined by the user for this this section."
  152. (cdr (linker-object-symbols object)))
  153. (define-record-type <string-table>
  154. (%make-string-table strings linked?)
  155. string-table?
  156. (strings string-table-strings set-string-table-strings!)
  157. (linked? string-table-linked? set-string-table-linked?!))
  158. (define (make-string-table)
  159. "Return a string table with one entry: the empty string."
  160. (%make-string-table '(("" 0 #vu8())) #f))
  161. (define (string-table-size strtab)
  162. "Return the size in bytes of the wire representation of @var{strtab}."
  163. (string-table-length (string-table-strings strtab)))
  164. (define (string-table-length strings)
  165. "Return the number of bytes needed for the @var{strings}."
  166. (match strings
  167. (((str pos bytes) . _)
  168. ;; The + 1 is for the trailing NUL byte.
  169. (+ pos (bytevector-length bytes) 1))))
  170. (define (string-table-intern! table str)
  171. "Ensure that @var{str} is present in the string table @var{table}.
  172. Returns the byte index of the string in that table."
  173. (match table
  174. (($ <string-table> strings linked?)
  175. (match (assoc str strings)
  176. ((_ pos _) pos)
  177. (#f
  178. (let ((next (string-table-length strings)))
  179. (when linked?
  180. (error "string table already linked, can't intern" table str))
  181. (set-string-table-strings! table
  182. (cons (list str next (string->utf8 str))
  183. strings))
  184. next))))))
  185. (define (string-table-writer table)
  186. "Return a <linker-object> \"writer\" procedure that links the string
  187. table @var{table} into a sequence of bytes, suitable for use as the
  188. contents of an ELF string table section."
  189. (lambda (bv)
  190. (match table
  191. (($ <string-table> strings #f)
  192. (for-each (match-lambda
  193. ((_ pos bytes)
  194. (bytevector-copy! bytes 0 bv pos
  195. (bytevector-length bytes))))
  196. strings)
  197. (set-string-table-linked?! table #t)))))
  198. (define (segment-kind section)
  199. "Return the type of segment needed to store @var{section}, as a pair.
  200. The car is the @code{PT_} segment type, or @code{#f} if the section
  201. doesn't need to be present in a loadable segment. The cdr is a bitfield
  202. of associated @code{PF_} permissions."
  203. (let ((flags (elf-section-flags section)))
  204. ;; Sections without SHF_ALLOC don't go in segments.
  205. (cons (if (zero? flags) #f PT_LOAD)
  206. (logior (if (logtest SHF_ALLOC flags) PF_R 0)
  207. (if (logtest SHF_EXECINSTR flags) PF_X 0)
  208. (if (logtest SHF_WRITE flags) PF_W 0)))))
  209. (define (count-segments objects)
  210. "Return the total number of segments needed to represent the linker
  211. objects in @var{objects}, including the segment needed for the ELF
  212. header and segment table."
  213. (define (adjoin x xs)
  214. (if (member x xs) xs (cons x xs)))
  215. (length
  216. (fold-values (lambda (object kinds)
  217. (let ((kind (segment-kind (linker-object-section object))))
  218. (if (= (elf-section-type (linker-object-section object))
  219. SHT_DYNAMIC)
  220. ;; The dynamic section is part of a loadable
  221. ;; segment, and also gets the additional
  222. ;; PT_DYNAMIC segment header.
  223. (cons (cons PT_DYNAMIC (cdr kind))
  224. (adjoin kind kinds))
  225. (if (car kind) (adjoin kind kinds) kinds))))
  226. objects
  227. ;; We know there will be at least one segment,
  228. ;; containing at least the header and segment table.
  229. (list (cons PT_LOAD PF_R)))))
  230. (define (group-by-cars ls)
  231. (let lp ((ls ls) (k #f) (group #f) (out '()))
  232. (match ls
  233. (()
  234. (reverse!
  235. (if group
  236. (cons (cons k (reverse! group)) out)
  237. out)))
  238. (((k* . v) . ls)
  239. (if (and group (equal? k k*))
  240. (lp ls k (cons v group) out)
  241. (lp ls k* (list v)
  242. (if group
  243. (cons (cons k (reverse! group)) out)
  244. out)))))))
  245. (define (collate-objects-into-segments objects)
  246. "Given the list of linker objects @var{objects}, group them into
  247. contiguous ELF segments of the same type and flags. The result is an
  248. alist that maps segment types to lists of linker objects. See
  249. @code{segment-type} for a description of segment types. Within a
  250. segment, the order of the linker objects is preserved."
  251. (group-by-cars
  252. (stable-sort!
  253. (map (lambda (o)
  254. (cons (segment-kind (linker-object-section o)) o))
  255. objects)
  256. (lambda (x y)
  257. (let* ((x-kind (car x)) (y-kind (car y))
  258. (x-type (car x-kind)) (y-type (car y-kind))
  259. (x-flags (cdr x-kind)) (y-flags (cdr y-kind))
  260. (x-section (linker-object-section (cdr x)))
  261. (y-section (linker-object-section (cdr y))))
  262. (cond
  263. ((not (equal? x-kind y-kind))
  264. (cond
  265. ((and x-type y-type)
  266. (cond
  267. ((not (equal? x-flags y-flags))
  268. (< x-flags y-flags))
  269. (else
  270. (< x-type y-type))))
  271. (else
  272. (not y-type))))
  273. ((not (equal? (elf-section-type x-section)
  274. (elf-section-type y-section)))
  275. (cond
  276. ((equal? (elf-section-type x-section) SHT_NOBITS) #t)
  277. ((equal? (elf-section-type y-section) SHT_NOBITS) #f)
  278. (else (< (elf-section-type x-section)
  279. (elf-section-type y-section)))))
  280. (else
  281. ;; Leave them in the initial order. This allows us to ensure
  282. ;; that the ELF header is written first.
  283. #f)))))))
  284. (define (align address alignment)
  285. (if (zero? alignment)
  286. address
  287. (+ address
  288. (modulo (- alignment (modulo address alignment)) alignment))))
  289. (define (relocate-section-header sec offset)
  290. "Return a new section header, just like @var{sec} but with its
  291. @code{offset} (and @code{addr} if it is loadable) set to @var{offset}."
  292. (make-elf-section #:index (elf-section-index sec)
  293. #:name (elf-section-name sec)
  294. #:type (elf-section-type sec)
  295. #:flags (elf-section-flags sec)
  296. #:addr (if (zero? (logand SHF_ALLOC
  297. (elf-section-flags sec)))
  298. 0
  299. offset)
  300. #:offset offset
  301. #:size (elf-section-size sec)
  302. #:link (elf-section-link sec)
  303. #:info (elf-section-info sec)
  304. #:addralign (elf-section-addralign sec)
  305. #:entsize (elf-section-entsize sec)))
  306. ;; We assume that 64K is a multiple of the page size. A
  307. ;; least-common-multiple, if you will.
  308. ;;
  309. ;; It would be possible to choose smaller, target-specific page sizes.
  310. ;; This is still a little tricky; on amd64 for example, systems commonly
  311. ;; have 4KB pages, but they are allowed by the ABI to have any
  312. ;; multiple-of-2 page size up to 64 KB. On Cygwin, pages are 4kB but
  313. ;; they can only be allocated 16 at a time. MIPS and ARM64 can use 64K
  314. ;; pages too and that's not uncommon.
  315. ;;
  316. ;; At the current time, in Guile we would like to reduce the number of
  317. ;; binaries we ship to the existing 32-or-64-bit and
  318. ;; big-or-little-endian variants, if possible. It would seem that with
  319. ;; the least-common-multiple of 64 KB pages, we can do that.
  320. ;;
  321. ;; See https://github.com/golang/go/issues/10180 for a discussion of
  322. ;; this issue in the Go context.
  323. ;;
  324. ;; Using 64KB instead of the more usual 4KB will increase the size of
  325. ;; our .go files, but not the prebuilt/ part of the tarball as that part
  326. ;; of the file will be zeroes and compress well. Additionally on a
  327. ;; system with 4KB pages, the extra padding will never be paged in, nor
  328. ;; read from disk (though it causes more seeking etc so on spinning
  329. ;; metal it's a bit of a lose).
  330. ;;
  331. ;; By way of comparison, on many 64-bit platforms, binutils currently
  332. ;; defaults to aligning segments on 2MB boundaries. It does so by
  333. ;; making the file and the memory images not the same: the pages are all
  334. ;; together on disk, but then when loading, the loader will mmap a
  335. ;; region "memsz" large which might be greater than the file size, then
  336. ;; map segments into that region. We can avoid this complication for
  337. ;; now. We can consider adding it in the future in a compatible way in
  338. ;; 2.2 if it is important.
  339. ;;
  340. (define *lcm-page-size* (ash 1 16))
  341. (define (add-symbols symbols offset symtab)
  342. "Add @var{symbols} to the symbol table @var{symtab}, relocating them
  343. from object address space to memory address space. Returns a new symbol
  344. table."
  345. (fold-values
  346. (lambda (symbol symtab)
  347. (let ((name (linker-symbol-name symbol))
  348. (addr (linker-symbol-address symbol)))
  349. (when (vhash-assq name symtab)
  350. (error "duplicate symbol" name))
  351. (vhash-consq name (make-linker-symbol name (+ addr offset)) symtab)))
  352. symbols
  353. symtab))
  354. (define (allocate-segment write-segment-header!
  355. phidx type flags objects addr symtab alignment)
  356. "Given a list of linker objects that should go in a segment, the type
  357. and flags that the segment should have, and the address at which the
  358. segment should start, compute the positions that each object should have
  359. in the segment.
  360. Returns three values: the address of the next byte after the segment, a
  361. list of relocated objects, and the symbol table. The symbol table is
  362. the same as @var{symtab}, augmented with the symbols defined in
  363. @var{objects}, relocated to their positions in the image.
  364. In what is something of a quirky interface, this routine also patches up
  365. the segment table using @code{write-segment-header!}."
  366. (let* ((alignment (fold-values (lambda (o alignment)
  367. (lcm (elf-section-addralign
  368. (linker-object-section o))
  369. alignment))
  370. objects
  371. alignment))
  372. (addr (align addr alignment)))
  373. (receive (objects endaddr symtab)
  374. (fold-values
  375. (lambda (o out addr symtab)
  376. (let* ((section (linker-object-section o))
  377. (addr (align addr (elf-section-addralign section))))
  378. (values
  379. (cons (make-linker-object
  380. (linker-object-name o)
  381. (relocate-section-header section addr)
  382. (linker-object-size o)
  383. (linker-object-writer o)
  384. (linker-object-relocs o)
  385. (linker-object-symbols o))
  386. out)
  387. (+ addr (elf-section-size section))
  388. (add-symbols (linker-object-symbols o) addr symtab))))
  389. objects
  390. '() addr symtab)
  391. (when type
  392. (write-segment-header!
  393. (make-elf-segment #:index phidx #:type type
  394. #:offset addr #:vaddr addr #:paddr addr
  395. #:filesz (- endaddr addr) #:memsz (- endaddr addr)
  396. #:flags flags #:align alignment)))
  397. (values endaddr
  398. (reverse objects)
  399. symtab))))
  400. (define (process-reloc reloc bv section-offset symtab endianness)
  401. "Process a relocation. Given that a section containing @var{reloc}
  402. was just written into the image @var{bv} at offset @var{section-offset},
  403. fix it up so that its reference points to the correct position of its
  404. symbol, as present in @var{symtab}."
  405. (match (vhash-assq (linker-reloc-symbol reloc) symtab)
  406. (#f
  407. (error "Undefined symbol" (linker-reloc-symbol reloc)))
  408. ((name . symbol)
  409. ;; The reloc was written at LOC bytes after SECTION-OFFSET.
  410. (let* ((offset (+ (linker-reloc-loc reloc) section-offset))
  411. (target (linker-symbol-address symbol)))
  412. (case (linker-reloc-type reloc)
  413. ((rel32/4)
  414. (let ((diff (+ (- target offset) (linker-reloc-addend reloc))))
  415. (unless (zero? (modulo diff 4))
  416. (error "Bad offset" reloc symbol offset))
  417. (bytevector-s32-set! bv (- offset section-offset) (/ diff 4) endianness)))
  418. ((rel32/1)
  419. (let ((diff (- target offset)))
  420. (bytevector-s32-set! bv (- offset section-offset)
  421. (+ diff (linker-reloc-addend reloc))
  422. endianness)))
  423. ((abs32/1)
  424. (bytevector-u32-set! bv (- offset section-offset) target endianness))
  425. ((abs64/1)
  426. (bytevector-u64-set! bv (- offset section-offset) target endianness))
  427. (else
  428. (error "bad reloc type" reloc)))))))
  429. (define (write-linker-object bv o symtab endianness)
  430. "Write the bytevector for the section wrapped by the linker object
  431. @var{o} into the image @var{bv}. The section header in @var{o} should
  432. already be relocated its final position in the image. Any relocations
  433. in the section will be processed to point to the correct symbol
  434. locations, as given in @var{symtab}."
  435. (let* ((section (linker-object-section o))
  436. (offset (elf-section-offset section))
  437. (len (elf-section-size section))
  438. (relocs (linker-object-relocs o)))
  439. (if (zero? (logand SHF_ALLOC (elf-section-flags section)))
  440. (unless (zero? (elf-section-addr section))
  441. (error "non-loadable section has non-zero addr" section))
  442. (unless (= offset (elf-section-addr section))
  443. (error "loadable section has offset != addr" section)))
  444. (if (not (= (elf-section-type section) SHT_NOBITS))
  445. (begin
  446. (unless (= len (linker-object-size o))
  447. (error "unexpected length" section o))
  448. ((linker-object-writer o) bv)
  449. (for-each (lambda (reloc)
  450. (process-reloc reloc bv offset symtab endianness))
  451. relocs)))))
  452. (define (find-shstrndx objects)
  453. "Find the section name string table in @var{objects}, and return its
  454. section index."
  455. (or-map (lambda (object)
  456. (and (equal? (linker-object-name object) ".shstrtab")
  457. (elf-section-index (linker-object-section object))))
  458. objects))
  459. (define (add-elf-objects objects endianness word-size abi type machine-type)
  460. "Given the list of linker objects supplied by the user, add linker
  461. objects corresponding to parts of the ELF file: the null object, the ELF
  462. header, and the section table.
  463. Both of these internal objects include relocs, allowing their
  464. inter-object references to be patched up when the final image allocation
  465. is known. There is special support for patching up the segment table,
  466. however. Because the segment table needs to know the segment sizes,
  467. which is the difference between two symbols in image space, and there is
  468. no reloc kind that is the difference between two symbols, we make a hack
  469. and return a closure that patches up segment table entries. It seems to
  470. work.
  471. Returns two values: the procedure to patch the segment table, and the
  472. list of objects, augmented with objects for the special ELF sections."
  473. (define phoff (elf-header-len word-size))
  474. (define phentsize (elf-program-header-len word-size))
  475. (define shentsize (elf-section-header-len word-size))
  476. (define shnum (+ (length objects) 3))
  477. (define reloc-kind
  478. (case word-size
  479. ((4) 'abs32/1)
  480. ((8) 'abs64/1)
  481. (else (error "bad word size" word-size))))
  482. ;; ELF requires that the first entry in the section table be of type
  483. ;; SHT_NULL.
  484. ;;
  485. (define (make-null-section)
  486. (make-linker-object ""
  487. (make-elf-section #:index 0 #:type SHT_NULL
  488. #:flags 0 #:addralign 0)
  489. 0 (lambda (bv) #t) '() '()))
  490. ;; The ELF header and the segment table.
  491. ;;
  492. (define (make-header phnum index shoff-label)
  493. (let* ((header (make-elf #:byte-order endianness #:word-size word-size
  494. #:abi abi #:type type #:machine-type machine-type
  495. #:phoff phoff #:phnum phnum #:phentsize phentsize
  496. #:shoff 0 #:shnum shnum #:shentsize shentsize
  497. #:shstrndx (or (find-shstrndx objects) SHN_UNDEF)))
  498. (shoff-reloc (make-linker-reloc reloc-kind
  499. (elf-header-shoff-offset word-size)
  500. 0
  501. shoff-label))
  502. (size (+ phoff (* phnum phentsize))))
  503. ;; Leave the segment table uninitialized; it will be filled in
  504. ;; later by calls to the write-segment-header! closure.
  505. (make-linker-object #f
  506. (make-elf-section #:index index #:type SHT_PROGBITS
  507. #:flags SHF_ALLOC #:size size)
  508. size
  509. (lambda (bv)
  510. (write-elf-header bv header))
  511. (list shoff-reloc)
  512. '())))
  513. ;; The section table.
  514. ;;
  515. (define (make-footer objects shoff-label)
  516. (let* ((size (* shentsize shnum))
  517. (section-table (make-elf-section #:index (length objects)
  518. #:type SHT_PROGBITS
  519. #:flags 0
  520. #:size size)))
  521. (define (compute-reloc section-label section relocs)
  522. (let ((offset (* shentsize (elf-section-index section))))
  523. (if (= (elf-section-type section) SHT_NULL)
  524. relocs
  525. (let ((relocs
  526. (cons (make-linker-reloc
  527. reloc-kind
  528. (+ offset
  529. (elf-section-header-offset-offset word-size))
  530. 0
  531. section-label)
  532. relocs)))
  533. (if (zero? (logand SHF_ALLOC (elf-section-flags section)))
  534. relocs
  535. (cons (make-linker-reloc
  536. reloc-kind
  537. (+ offset
  538. (elf-section-header-addr-offset word-size))
  539. 0
  540. section-label)
  541. relocs))))))
  542. (define (write-object-elf-header! bv object)
  543. (let ((section (linker-object-section object)))
  544. (let ((offset (* shentsize (elf-section-index section))))
  545. (write-elf-section-header bv offset endianness word-size section))))
  546. (let ((relocs (fold-values
  547. (lambda (object relocs)
  548. (compute-reloc
  549. (linker-symbol-name
  550. (linker-object-section-symbol object))
  551. (linker-object-section object)
  552. relocs))
  553. objects
  554. (compute-reloc shoff-label section-table '()))))
  555. (%make-linker-object #f section-table size
  556. (lambda (bv)
  557. (for-each (lambda (object)
  558. (write-object-elf-header! bv
  559. object))
  560. objects))
  561. relocs
  562. (list (make-linker-symbol shoff-label 0))))))
  563. (let* ((null-section (make-null-section))
  564. (objects (cons null-section objects))
  565. (shoff (gensym "*section-table*"))
  566. (header (make-header (count-segments objects) (length objects) shoff))
  567. (objects (cons header objects))
  568. (footer (make-footer objects shoff))
  569. (objects (cons footer objects))
  570. (segments '()))
  571. ;; The header includes the segment table, which needs offsets and
  572. ;; sizes of the segments. Normally we would use relocs to rewrite
  573. ;; these values, but there is no reloc type that would allow us to
  574. ;; compute size. Such a reloc would need to take the difference
  575. ;; between two symbols, and it's probably a bad idea architecturally
  576. ;; to create one.
  577. ;;
  578. ;; So instead change HEADER's writer to patch up the segment table.
  579. (define (add-header-segment! segment)
  580. (set! segments (cons segment segments)))
  581. (define write-header!
  582. (linker-object-writer header))
  583. (define (write-header+segments! bv)
  584. (for-each (lambda (segment)
  585. (let ((offset (+ phoff
  586. (* (elf-segment-index segment) phentsize))))
  587. (write-elf-program-header bv offset
  588. endianness
  589. word-size
  590. segment)))
  591. segments)
  592. (write-header! bv))
  593. (set-linker-object-writer! header write-header+segments!)
  594. (values add-header-segment! objects)))
  595. (define (record-special-segments write-segment-header! phidx all-objects)
  596. (let lp ((phidx phidx) (objects all-objects))
  597. (match objects
  598. (() #t)
  599. ((object . objects)
  600. (let ((section (linker-object-section object)))
  601. (cond
  602. ((eqv? (elf-section-type section) SHT_DYNAMIC)
  603. (let ((addr (elf-section-offset section))
  604. (size (elf-section-size section))
  605. (align (elf-section-addralign section))
  606. (flags (cdr (segment-kind section))))
  607. (write-segment-header!
  608. (make-elf-segment #:index phidx #:type PT_DYNAMIC
  609. #:offset addr #:vaddr addr #:paddr addr
  610. #:filesz size #:memsz size
  611. #:flags flags #:align align))
  612. (lp (1+ phidx) objects)))
  613. (else
  614. (lp phidx objects))))))))
  615. (define (allocate-elf objects page-aligned? endianness word-size
  616. abi type machine-type)
  617. "Lay out @var{objects} into an ELF image, computing the size of the
  618. file, the positions of the objects, and the global symbol table.
  619. If @var{page-aligned?} is true, read-only and writable data are
  620. separated so that only those writable parts of the image need be mapped
  621. with writable permissions. This makes the resulting image larger. It
  622. is more suitable to situations where you would write a file out to disk
  623. and read it in with mmap. Otherwise if @var{page-aligned?} is false,
  624. sections default to 8-byte alignment.
  625. Returns three values: the total image size, a list of objects with
  626. relocated headers, and the global symbol table."
  627. (receive (write-segment-header! objects)
  628. (add-elf-objects objects endianness word-size abi type machine-type)
  629. (let lp ((seglists (collate-objects-into-segments objects))
  630. (objects '())
  631. (phidx 0)
  632. (addr 0)
  633. (symtab vlist-null)
  634. (prev-flags 0))
  635. (match seglists
  636. ((((type . flags) objs-in ...) seglists ...)
  637. (receive (addr objs-out symtab)
  638. (allocate-segment
  639. write-segment-header!
  640. phidx type flags objs-in addr symtab
  641. (if (and page-aligned?
  642. (not (= flags prev-flags))
  643. ;; Allow sections that are not in
  644. ;; loadable segments to share pages
  645. ;; with PF_R segments.
  646. (not (and (not type) (= PF_R prev-flags))))
  647. *lcm-page-size*
  648. 8))
  649. (lp seglists
  650. (fold-values cons objs-out objects)
  651. (if type (1+ phidx) phidx)
  652. addr
  653. symtab
  654. flags)))
  655. (()
  656. (record-special-segments write-segment-header! phidx objects)
  657. (values addr
  658. (reverse objects)
  659. symtab))))))
  660. (define (check-section-numbers objects)
  661. "Verify that taken as a whole, that all objects have distinct,
  662. contiguous section numbers, starting from 1. (Section 0 is the null
  663. section.)"
  664. (let* ((nsections (1+ (length objects))) ; 1+ for initial NULL section.
  665. (sections (make-vector nsections #f)))
  666. (for-each (lambda (object)
  667. (let ((n (elf-section-index (linker-object-section object))))
  668. (cond
  669. ((< n 1)
  670. (error "Invalid section number" object))
  671. ((>= n nsections)
  672. (error "Invalid section number" object))
  673. ((vector-ref sections n)
  674. (error "Duplicate section" (vector-ref sections n) object))
  675. (else
  676. (vector-set! sections n object)))))
  677. objects)))
  678. ;; Given a list of linker objects, collate the objects into segments,
  679. ;; allocate the segments, allocate the ELF bytevector, and write the
  680. ;; segments into the bytevector, relocating as we go.
  681. ;;
  682. (define* (link-elf objects #:key
  683. (page-aligned? #t)
  684. (endianness (target-endianness))
  685. (word-size (target-word-size))
  686. (abi ELFOSABI_STANDALONE)
  687. (type ET_DYN)
  688. (machine-type EM_NONE))
  689. "Create an ELF image from the linker objects, @var{objects}.
  690. If @var{page-aligned?} is true, read-only and writable data are
  691. separated so that only those writable parts of the image need be mapped
  692. with writable permissions. This is suitable for situations where you
  693. would write a file out to disk and read it in with @code{mmap}.
  694. Otherwise if @var{page-aligned?} is false, sections default to 8-byte
  695. alignment.
  696. Returns a bytevector."
  697. (check-section-numbers objects)
  698. (receive (size objects symtab)
  699. (allocate-elf objects page-aligned? endianness word-size
  700. abi type machine-type)
  701. ;; XXX: When PAGE-ALIGNED? is false, assume the caller expects to
  702. ;; see a bytevector. Otherwise return a procedure that will write
  703. ;; the ELF stream to the given port.
  704. (if (not page-aligned?)
  705. (let ((bv (make-bytevector size 0)))
  706. (for-each
  707. (lambda (object)
  708. (let* ((section (linker-object-section object))
  709. (offset (elf-section-offset section))
  710. (len (elf-section-size section)))
  711. (write-linker-object (bytevector-slice bv offset len)
  712. object symtab endianness)))
  713. objects)
  714. bv)
  715. (lambda (port)
  716. (define write-padding
  717. (let ((blank (make-bytevector 4096 0)))
  718. (lambda (port size)
  719. ;; Write SIZE bytes of padding to PORT.
  720. (let loop ((size size))
  721. (unless (zero? size)
  722. (let ((count (min size
  723. (bytevector-length blank))))
  724. (put-bytevector port blank 0 count)
  725. (loop (- size count))))))))
  726. (define (compute-padding objects)
  727. ;; Return the list of padding in between OBJECTS--the list
  728. ;; of sizes of padding to be inserted before each object.
  729. (define object-offset
  730. (compose elf-section-offset linker-object-section))
  731. (let loop ((objects objects)
  732. (offset 0)
  733. (result '()))
  734. (match objects
  735. (()
  736. (reverse result))
  737. ((object . tail)
  738. (loop tail
  739. (+ (linker-object-size object)
  740. (object-offset object))
  741. (cons (- (object-offset object) offset)
  742. result))))))
  743. (for-each
  744. (lambda (object padding)
  745. (let ((bv (make-bytevector (linker-object-size object) 0)))
  746. (write-padding port padding)
  747. (write-linker-object bv object symtab endianness)
  748. (put-bytevector port bv)))
  749. objects
  750. (compute-padding objects))))))