elf.scm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. ;;; Guile ELF reader and writer
  2. ;; Copyright (C) 2011, 2012, 2013, 2014, 2015 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. ;;; This file was taken from the Guile 2.1 branch, where it is known as
  19. ;;; (system vm elf), and renamed to (guix elf). It will be unneeded when Guix
  20. ;;; switches to Guile 2.1/2.2.
  21. ;;;
  22. ;;; A module to read and write Executable and Linking Format (ELF)
  23. ;;; files.
  24. ;;;
  25. ;;; This module exports a number of record types that represent the
  26. ;;; various parts that make up ELF files. Fundamentally this is the
  27. ;;; main header, the segment headers (program headers), and the section
  28. ;;; headers. It also exports bindings for symbolic constants and
  29. ;;; utilities to parse and write special kinds of ELF sections.
  30. ;;;
  31. ;;; See elf(5) for more information on ELF.
  32. ;;;
  33. ;;; Code:
  34. (define-module (guix elf)
  35. #:use-module (rnrs bytevectors)
  36. #:use-module (system foreign)
  37. #:use-module (system base target)
  38. #:use-module (srfi srfi-9)
  39. #:use-module (ice-9 receive)
  40. #:use-module (ice-9 vlist)
  41. #:export (has-elf-header?
  42. (make-elf* . make-elf)
  43. elf?
  44. elf-bytes elf-word-size elf-byte-order
  45. elf-abi elf-type elf-machine-type
  46. elf-entry elf-phoff elf-shoff elf-flags elf-ehsize
  47. elf-phentsize elf-phnum elf-shentsize elf-shnum elf-shstrndx
  48. ELFOSABI_NONE ELFOSABI_HPUX ELFOSABI_NETBSD ELFOSABI_GNU
  49. ELFOSABI_SOLARIS ELFOSABI_AIX ELFOSABI_IRIX ELFOSABI_FREEBSD
  50. ELFOSABI_TRU64 ELFOSABI_MODESTO ELFOSABI_OPENBSD
  51. ELFOSABI_ARM_AEABI ELFOSABI_ARM ELFOSABI_STANDALONE
  52. ET_NONE ET_REL ET_EXEC ET_DYN ET_CORE
  53. EM_NONE EM_SPARC EM_386 EM_MIPS EM_PPC EM_PPC64 EM_ARM EM_SH
  54. EM_SPARCV9 EM_IA_64 EM_X86_64
  55. elf-header-len elf-header-shoff-offset
  56. write-elf-header
  57. (make-elf-segment* . make-elf-segment)
  58. elf-segment?
  59. elf-segment-index
  60. elf-segment-type elf-segment-offset elf-segment-vaddr
  61. elf-segment-paddr elf-segment-filesz elf-segment-memsz
  62. elf-segment-flags elf-segment-align
  63. elf-program-header-len write-elf-program-header
  64. PT_NULL PT_LOAD PT_DYNAMIC PT_INTERP PT_NOTE PT_SHLIB
  65. PT_PHDR PT_TLS PT_NUM PT_LOOS PT_GNU_EH_FRAME PT_GNU_STACK
  66. PT_GNU_RELRO
  67. PF_R PF_W PF_X
  68. (make-elf-section* . make-elf-section)
  69. elf-section?
  70. elf-section-index
  71. elf-section-name elf-section-type elf-section-flags
  72. elf-section-addr elf-section-offset elf-section-size
  73. elf-section-link elf-section-info elf-section-addralign
  74. elf-section-entsize
  75. elf-section-header-len elf-section-header-addr-offset
  76. elf-section-header-offset-offset
  77. write-elf-section-header
  78. (make-elf-symbol* . make-elf-symbol)
  79. elf-symbol?
  80. elf-symbol-name elf-symbol-value elf-symbol-size
  81. elf-symbol-info elf-symbol-other elf-symbol-shndx
  82. elf-symbol-binding elf-symbol-type elf-symbol-visibility
  83. elf-symbol-len elf-symbol-value-offset write-elf-symbol
  84. SHN_UNDEF
  85. SHT_NULL SHT_PROGBITS SHT_SYMTAB SHT_STRTAB SHT_RELA
  86. SHT_HASH SHT_DYNAMIC SHT_NOTE SHT_NOBITS SHT_REL SHT_SHLIB
  87. SHT_DYNSYM SHT_INIT_ARRAY SHT_FINI_ARRAY SHT_PREINIT_ARRAY
  88. SHT_GROUP SHT_SYMTAB_SHNDX SHT_NUM SHT_LOOS SHT_HIOS
  89. SHT_LOPROC SHT_HIPROC SHT_LOUSER SHT_HIUSER
  90. SHF_WRITE SHF_ALLOC SHF_EXECINSTR SHF_MERGE SHF_STRINGS
  91. SHF_INFO_LINK SHF_LINK_ORDER SHF_OS_NONCONFORMING SHF_GROUP
  92. SHF_TLS
  93. DT_NULL DT_NEEDED DT_PLTRELSZ DT_PLTGOT DT_HASH DT_STRTAB
  94. DT_SYMTAB DT_RELA DT_RELASZ DT_RELAENT DT_STRSZ DT_SYMENT
  95. DT_INIT DT_FINI DT_SONAME DT_RPATH DT_SYMBOLIC DT_REL
  96. DT_RELSZ DT_RELENT DT_PLTREL DT_DEBUG DT_TEXTREL DT_JMPREL
  97. DT_BIND_NOW DT_INIT_ARRAY DT_FINI_ARRAY DT_INIT_ARRAYSZ
  98. DT_FINI_ARRAYSZ DT_RUNPATH DT_FLAGS DT_ENCODING
  99. DT_PREINIT_ARRAY DT_PREINIT_ARRAYSZ DT_NUM DT_LOGUILE
  100. DT_GUILE_GC_ROOT DT_GUILE_GC_ROOT_SZ DT_GUILE_ENTRY
  101. DT_GUILE_VM_VERSION DT_GUILE_FRAME_MAPS DT_HIGUILE
  102. DT_LOOS DT_HIOS DT_LOPROC DT_HIPROC
  103. string-table-ref
  104. STB_LOCAL STB_GLOBAL STB_WEAK STB_NUM STB_LOOS STB_GNU
  105. STB_HIOS STB_LOPROC STB_HIPROC
  106. STT_NOTYPE STT_OBJECT STT_FUNC STT_SECTION STT_FILE
  107. STT_COMMON STT_TLS STT_NUM STT_LOOS STT_GNU STT_HIOS
  108. STT_LOPROC STT_HIPROC
  109. STV_DEFAULT STV_INTERNAL STV_HIDDEN STV_PROTECTED
  110. NT_GNU_ABI_TAG NT_GNU_HWCAP NT_GNU_BUILD_ID NT_GNU_GOLD_VERSION
  111. parse-elf
  112. elf-segment elf-segments
  113. elf-section elf-sections elf-section-by-name elf-sections-by-name
  114. elf-symbol-table-len elf-symbol-table-ref
  115. parse-elf-note
  116. elf-note-name elf-note-desc elf-note-type))
  117. ;; #define EI_NIDENT 16
  118. ;; typedef struct {
  119. ;; unsigned char e_ident[EI_NIDENT];
  120. ;; uint16_t e_type;
  121. ;; uint16_t e_machine;
  122. ;; uint32_t e_version;
  123. ;; ElfN_Addr e_entry;
  124. ;; ElfN_Off e_phoff;
  125. ;; ElfN_Off e_shoff;
  126. ;; uint32_t e_flags;
  127. ;; uint16_t e_ehsize;
  128. ;; uint16_t e_phentsize;
  129. ;; uint16_t e_phnum;
  130. ;; uint16_t e_shentsize;
  131. ;; uint16_t e_shnum;
  132. ;; uint16_t e_shstrndx;
  133. ;; } ElfN_Ehdr;
  134. (define elf32-header-len 52)
  135. (define elf64-header-len 64)
  136. (define (elf-header-len word-size)
  137. (case word-size
  138. ((4) elf32-header-len)
  139. ((8) elf64-header-len)
  140. (else (error "invalid word size" word-size))))
  141. (define (elf-header-shoff-offset word-size)
  142. (case word-size
  143. ((4) 32)
  144. ((8) 40)
  145. (else (error "bad word size" word-size))))
  146. (define ELFCLASS32 1) ; 32-bit objects
  147. (define ELFCLASS64 2) ; 64-bit objects
  148. (define ELFDATA2LSB 1) ; 2's complement, little endian
  149. (define ELFDATA2MSB 2) ; 2's complement, big endian
  150. (define EV_CURRENT 1) ; Current version
  151. (define ELFOSABI_NONE 0) ; UNIX System V ABI */
  152. (define ELFOSABI_HPUX 1) ; HP-UX
  153. (define ELFOSABI_NETBSD 2) ; NetBSD.
  154. (define ELFOSABI_GNU 3) ; Object uses GNU ELF extensions.
  155. (define ELFOSABI_SOLARIS 6) ; Sun Solaris.
  156. (define ELFOSABI_AIX 7) ; IBM AIX.
  157. (define ELFOSABI_IRIX 8) ; SGI Irix.
  158. (define ELFOSABI_FREEBSD 9) ; FreeBSD.
  159. (define ELFOSABI_TRU64 10) ; Compaq TRU64 UNIX.
  160. (define ELFOSABI_MODESTO 11) ; Novell Modesto.
  161. (define ELFOSABI_OPENBSD 12) ; OpenBSD.
  162. (define ELFOSABI_ARM_AEABI 64) ; ARM EABI
  163. (define ELFOSABI_ARM 97) ; ARM
  164. (define ELFOSABI_STANDALONE 255) ; Standalone (embedded) application
  165. (define ET_NONE 0) ; No file type
  166. (define ET_REL 1) ; Relocatable file
  167. (define ET_EXEC 2) ; Executable file
  168. (define ET_DYN 3) ; Shared object file
  169. (define ET_CORE 4) ; Core file
  170. ;;
  171. ;; Machine types
  172. ;;
  173. ;; Just a sampling of these values. We could include more, but the
  174. ;; important thing is to recognize architectures for which we have a
  175. ;; native compiler. Recognizing more common machine types is icing on
  176. ;; the cake.
  177. ;;
  178. (define EM_NONE 0) ; No machine
  179. (define EM_SPARC 2) ; SUN SPARC
  180. (define EM_386 3) ; Intel 80386
  181. (define EM_MIPS 8) ; MIPS R3000 big-endian
  182. (define EM_PPC 20) ; PowerPC
  183. (define EM_PPC64 21) ; PowerPC 64-bit
  184. (define EM_ARM 40) ; ARM
  185. (define EM_SH 42) ; Hitachi SH
  186. (define EM_SPARCV9 43) ; SPARC v9 64-bit
  187. (define EM_IA_64 50) ; Intel Merced
  188. (define EM_X86_64 62) ; AMD x86-64 architecture
  189. (define cpu-mapping (make-hash-table))
  190. (for-each (lambda (pair)
  191. (hashq-set! cpu-mapping (car pair) (cdr pair)))
  192. `((none . ,EM_NONE)
  193. (sparc . ,EM_SPARC) ; FIXME: map 64-bit to SPARCV9 ?
  194. (i386 . ,EM_386)
  195. (mips . ,EM_MIPS)
  196. (ppc . ,EM_PPC)
  197. (ppc64 . ,EM_PPC64)
  198. (arm . ,EM_ARM) ; FIXME: there are more arm cpu variants
  199. (sh . ,EM_SH) ; FIXME: there are more sh cpu variants
  200. (ia64 . ,EM_IA_64)
  201. (x86_64 . ,EM_X86_64)))
  202. (define SHN_UNDEF 0)
  203. (define host-machine-type
  204. (hashq-ref cpu-mapping
  205. (string->symbol (car (string-split %host-type #\-)))
  206. EM_NONE))
  207. (define host-word-size
  208. (sizeof '*))
  209. (define host-byte-order
  210. (native-endianness))
  211. (define (has-elf-header? bv)
  212. (and
  213. ;; e_ident
  214. (>= (bytevector-length bv) 16)
  215. (= (bytevector-u8-ref bv 0) #x7f)
  216. (= (bytevector-u8-ref bv 1) (char->integer #\E))
  217. (= (bytevector-u8-ref bv 2) (char->integer #\L))
  218. (= (bytevector-u8-ref bv 3) (char->integer #\F))
  219. (cond
  220. ((= (bytevector-u8-ref bv 4) ELFCLASS32)
  221. (>= (bytevector-length bv) elf32-header-len))
  222. ((= (bytevector-u8-ref bv 4) ELFCLASS64)
  223. (>= (bytevector-length bv) elf64-header-len))
  224. (else #f))
  225. (or (= (bytevector-u8-ref bv 5) ELFDATA2LSB)
  226. (= (bytevector-u8-ref bv 5) ELFDATA2MSB))
  227. (= (bytevector-u8-ref bv 6) EV_CURRENT)
  228. ;; Look at ABI later.
  229. (= (bytevector-u8-ref bv 8) 0) ; ABI version
  230. ;; The rest of the e_ident is padding.
  231. ;; e_version
  232. (let ((byte-order (if (= (bytevector-u8-ref bv 5) ELFDATA2LSB)
  233. (endianness little)
  234. (endianness big))))
  235. (= (bytevector-u32-ref bv 20 byte-order) EV_CURRENT))))
  236. (define-record-type <elf>
  237. (make-elf bytes word-size byte-order abi type machine-type
  238. entry phoff shoff flags ehsize
  239. phentsize phnum shentsize shnum shstrndx)
  240. elf?
  241. (bytes elf-bytes)
  242. (word-size elf-word-size)
  243. (byte-order elf-byte-order)
  244. (abi elf-abi)
  245. (type elf-type)
  246. (machine-type elf-machine-type)
  247. (entry elf-entry)
  248. (phoff elf-phoff)
  249. (shoff elf-shoff)
  250. (flags elf-flags)
  251. (ehsize elf-ehsize)
  252. (phentsize elf-phentsize)
  253. (phnum elf-phnum)
  254. (shentsize elf-shentsize)
  255. (shnum elf-shnum)
  256. (shstrndx elf-shstrndx))
  257. (define* (make-elf* #:key (bytes #f)
  258. (byte-order (target-endianness))
  259. (word-size (target-word-size))
  260. (abi ELFOSABI_STANDALONE)
  261. (type ET_DYN)
  262. (machine-type EM_NONE)
  263. (entry 0)
  264. (phoff (elf-header-len word-size))
  265. (shoff -1)
  266. (flags 0)
  267. (ehsize (elf-header-len word-size))
  268. (phentsize (elf-program-header-len word-size))
  269. (phnum 0)
  270. (shentsize (elf-section-header-len word-size))
  271. (shnum 0)
  272. (shstrndx SHN_UNDEF))
  273. (make-elf bytes word-size byte-order abi type machine-type
  274. entry phoff shoff flags ehsize
  275. phentsize phnum shentsize shnum shstrndx))
  276. (define (parse-elf32 bv byte-order)
  277. (make-elf bv 4 byte-order
  278. (bytevector-u8-ref bv 7)
  279. (bytevector-u16-ref bv 16 byte-order)
  280. (bytevector-u16-ref bv 18 byte-order)
  281. (bytevector-u32-ref bv 24 byte-order)
  282. (bytevector-u32-ref bv 28 byte-order)
  283. (bytevector-u32-ref bv 32 byte-order)
  284. (bytevector-u32-ref bv 36 byte-order)
  285. (bytevector-u16-ref bv 40 byte-order)
  286. (bytevector-u16-ref bv 42 byte-order)
  287. (bytevector-u16-ref bv 44 byte-order)
  288. (bytevector-u16-ref bv 46 byte-order)
  289. (bytevector-u16-ref bv 48 byte-order)
  290. (bytevector-u16-ref bv 50 byte-order)))
  291. (define (write-elf-ident bv class data abi)
  292. (bytevector-u8-set! bv 0 #x7f)
  293. (bytevector-u8-set! bv 1 (char->integer #\E))
  294. (bytevector-u8-set! bv 2 (char->integer #\L))
  295. (bytevector-u8-set! bv 3 (char->integer #\F))
  296. (bytevector-u8-set! bv 4 class)
  297. (bytevector-u8-set! bv 5 data)
  298. (bytevector-u8-set! bv 6 EV_CURRENT)
  299. (bytevector-u8-set! bv 7 abi)
  300. (bytevector-u8-set! bv 8 0) ; ABI version
  301. (bytevector-u8-set! bv 9 0) ; Pad to 16 bytes.
  302. (bytevector-u8-set! bv 10 0)
  303. (bytevector-u8-set! bv 11 0)
  304. (bytevector-u8-set! bv 12 0)
  305. (bytevector-u8-set! bv 13 0)
  306. (bytevector-u8-set! bv 14 0)
  307. (bytevector-u8-set! bv 15 0))
  308. (define (write-elf32-header bv elf)
  309. (let ((byte-order (elf-byte-order elf)))
  310. (write-elf-ident bv ELFCLASS32
  311. (case byte-order
  312. ((little) ELFDATA2LSB)
  313. ((big) ELFDATA2MSB)
  314. (else (error "unknown endianness" byte-order)))
  315. (elf-abi elf))
  316. (bytevector-u16-set! bv 16 (elf-type elf) byte-order)
  317. (bytevector-u16-set! bv 18 (elf-machine-type elf) byte-order)
  318. (bytevector-u32-set! bv 20 EV_CURRENT byte-order)
  319. (bytevector-u32-set! bv 24 (elf-entry elf) byte-order)
  320. (bytevector-u32-set! bv 28 (elf-phoff elf) byte-order)
  321. (bytevector-u32-set! bv 32 (elf-shoff elf) byte-order)
  322. (bytevector-u32-set! bv 36 (elf-flags elf) byte-order)
  323. (bytevector-u16-set! bv 40 (elf-ehsize elf) byte-order)
  324. (bytevector-u16-set! bv 42 (elf-phentsize elf) byte-order)
  325. (bytevector-u16-set! bv 44 (elf-phnum elf) byte-order)
  326. (bytevector-u16-set! bv 46 (elf-shentsize elf) byte-order)
  327. (bytevector-u16-set! bv 48 (elf-shnum elf) byte-order)
  328. (bytevector-u16-set! bv 50 (elf-shstrndx elf) byte-order)))
  329. (define (parse-elf64 bv byte-order)
  330. (make-elf bv 8 byte-order
  331. (bytevector-u8-ref bv 7)
  332. (bytevector-u16-ref bv 16 byte-order)
  333. (bytevector-u16-ref bv 18 byte-order)
  334. (bytevector-u64-ref bv 24 byte-order)
  335. (bytevector-u64-ref bv 32 byte-order)
  336. (bytevector-u64-ref bv 40 byte-order)
  337. (bytevector-u32-ref bv 48 byte-order)
  338. (bytevector-u16-ref bv 52 byte-order)
  339. (bytevector-u16-ref bv 54 byte-order)
  340. (bytevector-u16-ref bv 56 byte-order)
  341. (bytevector-u16-ref bv 58 byte-order)
  342. (bytevector-u16-ref bv 60 byte-order)
  343. (bytevector-u16-ref bv 62 byte-order)))
  344. (define (write-elf64-header bv elf)
  345. (let ((byte-order (elf-byte-order elf)))
  346. (write-elf-ident bv ELFCLASS64
  347. (case byte-order
  348. ((little) ELFDATA2LSB)
  349. ((big) ELFDATA2MSB)
  350. (else (error "unknown endianness" byte-order)))
  351. (elf-abi elf))
  352. (bytevector-u16-set! bv 16 (elf-type elf) byte-order)
  353. (bytevector-u16-set! bv 18 (elf-machine-type elf) byte-order)
  354. (bytevector-u32-set! bv 20 EV_CURRENT byte-order)
  355. (bytevector-u64-set! bv 24 (elf-entry elf) byte-order)
  356. (bytevector-u64-set! bv 32 (elf-phoff elf) byte-order)
  357. (bytevector-u64-set! bv 40 (elf-shoff elf) byte-order)
  358. (bytevector-u32-set! bv 48 (elf-flags elf) byte-order)
  359. (bytevector-u16-set! bv 52 (elf-ehsize elf) byte-order)
  360. (bytevector-u16-set! bv 54 (elf-phentsize elf) byte-order)
  361. (bytevector-u16-set! bv 56 (elf-phnum elf) byte-order)
  362. (bytevector-u16-set! bv 58 (elf-shentsize elf) byte-order)
  363. (bytevector-u16-set! bv 60 (elf-shnum elf) byte-order)
  364. (bytevector-u16-set! bv 62 (elf-shstrndx elf) byte-order)))
  365. (define (parse-elf bv)
  366. (cond
  367. ((has-elf-header? bv)
  368. (let ((class (bytevector-u8-ref bv 4))
  369. (byte-order (let ((data (bytevector-u8-ref bv 5)))
  370. (cond
  371. ((= data ELFDATA2LSB) (endianness little))
  372. ((= data ELFDATA2MSB) (endianness big))
  373. (else (error "unhandled byte order" data))))))
  374. (cond
  375. ((= class ELFCLASS32) (parse-elf32 bv byte-order))
  376. ((= class ELFCLASS64) (parse-elf64 bv byte-order))
  377. (else (error "unhandled class" class)))))
  378. (else
  379. (error "Invalid ELF" bv))))
  380. (define* (write-elf-header bv elf)
  381. ((case (elf-word-size elf)
  382. ((4) write-elf32-header)
  383. ((8) write-elf64-header)
  384. (else (error "unknown word size" (elf-word-size elf))))
  385. bv elf))
  386. ;;
  387. ;; Segment types
  388. ;;
  389. (define PT_NULL 0) ; Program header table entry unused
  390. (define PT_LOAD 1) ; Loadable program segment
  391. (define PT_DYNAMIC 2) ; Dynamic linking information
  392. (define PT_INTERP 3) ; Program interpreter
  393. (define PT_NOTE 4) ; Auxiliary information
  394. (define PT_SHLIB 5) ; Reserved
  395. (define PT_PHDR 6) ; Entry for header table itself
  396. (define PT_TLS 7) ; Thread-local storage segment
  397. (define PT_NUM 8) ; Number of defined types
  398. (define PT_LOOS #x60000000) ; Start of OS-specific
  399. (define PT_GNU_EH_FRAME #x6474e550) ; GCC .eh_frame_hdr segment
  400. (define PT_GNU_STACK #x6474e551) ; Indicates stack executability
  401. (define PT_GNU_RELRO #x6474e552) ; Read-only after relocation
  402. ;;
  403. ;; Segment flags
  404. ;;
  405. (define PF_X (ash 1 0)) ; Segment is executable
  406. (define PF_W (ash 1 1)) ; Segment is writable
  407. (define PF_R (ash 1 2)) ; Segment is readable
  408. (define-record-type <elf-segment>
  409. (make-elf-segment index type offset vaddr paddr filesz memsz flags align)
  410. elf-segment?
  411. (index elf-segment-index)
  412. (type elf-segment-type)
  413. (offset elf-segment-offset)
  414. (vaddr elf-segment-vaddr)
  415. (paddr elf-segment-paddr)
  416. (filesz elf-segment-filesz)
  417. (memsz elf-segment-memsz)
  418. (flags elf-segment-flags)
  419. (align elf-segment-align))
  420. (define* (make-elf-segment* #:key (index -1) (type PT_LOAD) (offset 0) (vaddr 0)
  421. (paddr 0) (filesz 0) (memsz filesz)
  422. (flags (logior PF_W PF_R))
  423. (align 8))
  424. (make-elf-segment index type offset vaddr paddr filesz memsz flags align))
  425. ;; typedef struct {
  426. ;; uint32_t p_type;
  427. ;; Elf32_Off p_offset;
  428. ;; Elf32_Addr p_vaddr;
  429. ;; Elf32_Addr p_paddr;
  430. ;; uint32_t p_filesz;
  431. ;; uint32_t p_memsz;
  432. ;; uint32_t p_flags;
  433. ;; uint32_t p_align;
  434. ;; } Elf32_Phdr;
  435. (define (parse-elf32-program-header index bv offset byte-order)
  436. (if (<= (+ offset 32) (bytevector-length bv))
  437. (make-elf-segment index
  438. (bytevector-u32-ref bv offset byte-order)
  439. (bytevector-u32-ref bv (+ offset 4) byte-order)
  440. (bytevector-u32-ref bv (+ offset 8) byte-order)
  441. (bytevector-u32-ref bv (+ offset 12) byte-order)
  442. (bytevector-u32-ref bv (+ offset 16) byte-order)
  443. (bytevector-u32-ref bv (+ offset 20) byte-order)
  444. (bytevector-u32-ref bv (+ offset 24) byte-order)
  445. (bytevector-u32-ref bv (+ offset 28) byte-order))
  446. (error "corrupt ELF (offset out of range)" offset)))
  447. (define (write-elf32-program-header bv offset byte-order seg)
  448. (bytevector-u32-set! bv offset (elf-segment-type seg) byte-order)
  449. (bytevector-u32-set! bv (+ offset 4) (elf-segment-offset seg) byte-order)
  450. (bytevector-u32-set! bv (+ offset 8) (elf-segment-vaddr seg) byte-order)
  451. (bytevector-u32-set! bv (+ offset 12) (elf-segment-paddr seg) byte-order)
  452. (bytevector-u32-set! bv (+ offset 16) (elf-segment-filesz seg) byte-order)
  453. (bytevector-u32-set! bv (+ offset 20) (elf-segment-memsz seg) byte-order)
  454. (bytevector-u32-set! bv (+ offset 24) (elf-segment-flags seg) byte-order)
  455. (bytevector-u32-set! bv (+ offset 28) (elf-segment-align seg) byte-order))
  456. ;; typedef struct {
  457. ;; uint32_t p_type;
  458. ;; uint32_t p_flags;
  459. ;; Elf64_Off p_offset;
  460. ;; Elf64_Addr p_vaddr;
  461. ;; Elf64_Addr p_paddr;
  462. ;; uint64_t p_filesz;
  463. ;; uint64_t p_memsz;
  464. ;; uint64_t p_align;
  465. ;; } Elf64_Phdr;
  466. ;; NB: position of `flags' is different!
  467. (define (parse-elf64-program-header index bv offset byte-order)
  468. (if (<= (+ offset 56) (bytevector-length bv))
  469. (make-elf-segment index
  470. (bytevector-u32-ref bv offset byte-order)
  471. (bytevector-u64-ref bv (+ offset 8) byte-order)
  472. (bytevector-u64-ref bv (+ offset 16) byte-order)
  473. (bytevector-u64-ref bv (+ offset 24) byte-order)
  474. (bytevector-u64-ref bv (+ offset 32) byte-order)
  475. (bytevector-u64-ref bv (+ offset 40) byte-order)
  476. (bytevector-u32-ref bv (+ offset 4) byte-order)
  477. (bytevector-u64-ref bv (+ offset 48) byte-order))
  478. (error "corrupt ELF (offset out of range)" offset)))
  479. (define (write-elf64-program-header bv offset byte-order seg)
  480. (bytevector-u32-set! bv offset (elf-segment-type seg) byte-order)
  481. (bytevector-u64-set! bv (+ offset 8) (elf-segment-offset seg) byte-order)
  482. (bytevector-u64-set! bv (+ offset 16) (elf-segment-vaddr seg) byte-order)
  483. (bytevector-u64-set! bv (+ offset 24) (elf-segment-paddr seg) byte-order)
  484. (bytevector-u64-set! bv (+ offset 32) (elf-segment-filesz seg) byte-order)
  485. (bytevector-u64-set! bv (+ offset 40) (elf-segment-memsz seg) byte-order)
  486. (bytevector-u32-set! bv (+ offset 4) (elf-segment-flags seg) byte-order)
  487. (bytevector-u64-set! bv (+ offset 48) (elf-segment-align seg) byte-order))
  488. (define (write-elf-program-header bv offset byte-order word-size seg)
  489. ((case word-size
  490. ((4) write-elf32-program-header)
  491. ((8) write-elf64-program-header)
  492. (else (error "invalid word size" word-size)))
  493. bv offset byte-order seg))
  494. (define (elf-program-header-len word-size)
  495. (case word-size
  496. ((4) 32)
  497. ((8) 56)
  498. (else (error "bad word size" word-size))))
  499. (define (elf-segment elf n)
  500. (if (not (< -1 n (elf-phnum elf)))
  501. (error "bad segment number" n))
  502. ((case (elf-word-size elf)
  503. ((4) parse-elf32-program-header)
  504. ((8) parse-elf64-program-header)
  505. (else (error "unhandled pointer size")))
  506. n
  507. (elf-bytes elf)
  508. (+ (elf-phoff elf) (* n (elf-phentsize elf)))
  509. (elf-byte-order elf)))
  510. (define (elf-segments elf)
  511. (let lp ((n (elf-phnum elf)) (out '()))
  512. (if (zero? n)
  513. out
  514. (lp (1- n) (cons (elf-segment elf (1- n)) out)))))
  515. (define-record-type <elf-section>
  516. (make-elf-section index name type flags
  517. addr offset size link info addralign entsize)
  518. elf-section?
  519. (index elf-section-index)
  520. (name elf-section-name)
  521. (type elf-section-type)
  522. (flags elf-section-flags)
  523. (addr elf-section-addr)
  524. (offset elf-section-offset)
  525. (size elf-section-size)
  526. (link elf-section-link)
  527. (info elf-section-info)
  528. (addralign elf-section-addralign)
  529. (entsize elf-section-entsize))
  530. (define* (make-elf-section* #:key (index SHN_UNDEF) (name 0) (type SHT_PROGBITS)
  531. (flags SHF_ALLOC) (addr 0) (offset 0) (size 0)
  532. (link 0) (info 0) (addralign 8) (entsize 0))
  533. (make-elf-section index name type flags addr offset size link info addralign
  534. entsize))
  535. ;; typedef struct {
  536. ;; uint32_t sh_name;
  537. ;; uint32_t sh_type;
  538. ;; uint32_t sh_flags;
  539. ;; Elf32_Addr sh_addr;
  540. ;; Elf32_Off sh_offset;
  541. ;; uint32_t sh_size;
  542. ;; uint32_t sh_link;
  543. ;; uint32_t sh_info;
  544. ;; uint32_t sh_addralign;
  545. ;; uint32_t sh_entsize;
  546. ;; } Elf32_Shdr;
  547. (define (parse-elf32-section-header index bv offset byte-order)
  548. (if (<= (+ offset 40) (bytevector-length bv))
  549. (make-elf-section index
  550. (bytevector-u32-ref bv offset byte-order)
  551. (bytevector-u32-ref bv (+ offset 4) byte-order)
  552. (bytevector-u32-ref bv (+ offset 8) byte-order)
  553. (bytevector-u32-ref bv (+ offset 12) byte-order)
  554. (bytevector-u32-ref bv (+ offset 16) byte-order)
  555. (bytevector-u32-ref bv (+ offset 20) byte-order)
  556. (bytevector-u32-ref bv (+ offset 24) byte-order)
  557. (bytevector-u32-ref bv (+ offset 28) byte-order)
  558. (bytevector-u32-ref bv (+ offset 32) byte-order)
  559. (bytevector-u32-ref bv (+ offset 36) byte-order))
  560. (error "corrupt ELF (offset out of range)" offset)))
  561. (define (write-elf32-section-header bv offset byte-order sec)
  562. (bytevector-u32-set! bv offset (elf-section-name sec) byte-order)
  563. (bytevector-u32-set! bv (+ offset 4) (elf-section-type sec) byte-order)
  564. (bytevector-u32-set! bv (+ offset 8) (elf-section-flags sec) byte-order)
  565. (bytevector-u32-set! bv (+ offset 12) (elf-section-addr sec) byte-order)
  566. (bytevector-u32-set! bv (+ offset 16) (elf-section-offset sec) byte-order)
  567. (bytevector-u32-set! bv (+ offset 20) (elf-section-size sec) byte-order)
  568. (bytevector-u32-set! bv (+ offset 24) (elf-section-link sec) byte-order)
  569. (bytevector-u32-set! bv (+ offset 28) (elf-section-info sec) byte-order)
  570. (bytevector-u32-set! bv (+ offset 32) (elf-section-addralign sec) byte-order)
  571. (bytevector-u32-set! bv (+ offset 36) (elf-section-entsize sec) byte-order))
  572. ;; typedef struct {
  573. ;; uint32_t sh_name;
  574. ;; uint32_t sh_type;
  575. ;; uint64_t sh_flags;
  576. ;; Elf64_Addr sh_addr;
  577. ;; Elf64_Off sh_offset;
  578. ;; uint64_t sh_size;
  579. ;; uint32_t sh_link;
  580. ;; uint32_t sh_info;
  581. ;; uint64_t sh_addralign;
  582. ;; uint64_t sh_entsize;
  583. ;; } Elf64_Shdr;
  584. (define (elf-section-header-len word-size)
  585. (case word-size
  586. ((4) 40)
  587. ((8) 64)
  588. (else (error "bad word size" word-size))))
  589. (define (elf-section-header-addr-offset word-size)
  590. (case word-size
  591. ((4) 12)
  592. ((8) 16)
  593. (else (error "bad word size" word-size))))
  594. (define (elf-section-header-offset-offset word-size)
  595. (case word-size
  596. ((4) 16)
  597. ((8) 24)
  598. (else (error "bad word size" word-size))))
  599. (define (parse-elf64-section-header index bv offset byte-order)
  600. (if (<= (+ offset 64) (bytevector-length bv))
  601. (make-elf-section index
  602. (bytevector-u32-ref bv offset byte-order)
  603. (bytevector-u32-ref bv (+ offset 4) byte-order)
  604. (bytevector-u64-ref bv (+ offset 8) byte-order)
  605. (bytevector-u64-ref bv (+ offset 16) byte-order)
  606. (bytevector-u64-ref bv (+ offset 24) byte-order)
  607. (bytevector-u64-ref bv (+ offset 32) byte-order)
  608. (bytevector-u32-ref bv (+ offset 40) byte-order)
  609. (bytevector-u32-ref bv (+ offset 44) byte-order)
  610. (bytevector-u64-ref bv (+ offset 48) byte-order)
  611. (bytevector-u64-ref bv (+ offset 56) byte-order))
  612. (error "corrupt ELF (offset out of range)" offset)))
  613. (define (write-elf64-section-header bv offset byte-order sec)
  614. (bytevector-u32-set! bv offset (elf-section-name sec) byte-order)
  615. (bytevector-u32-set! bv (+ offset 4) (elf-section-type sec) byte-order)
  616. (bytevector-u64-set! bv (+ offset 8) (elf-section-flags sec) byte-order)
  617. (bytevector-u64-set! bv (+ offset 16) (elf-section-addr sec) byte-order)
  618. (bytevector-u64-set! bv (+ offset 24) (elf-section-offset sec) byte-order)
  619. (bytevector-u64-set! bv (+ offset 32) (elf-section-size sec) byte-order)
  620. (bytevector-u32-set! bv (+ offset 40) (elf-section-link sec) byte-order)
  621. (bytevector-u32-set! bv (+ offset 44) (elf-section-info sec) byte-order)
  622. (bytevector-u64-set! bv (+ offset 48) (elf-section-addralign sec) byte-order)
  623. (bytevector-u64-set! bv (+ offset 56) (elf-section-entsize sec) byte-order))
  624. (define (elf-section elf n)
  625. (if (not (< -1 n (elf-shnum elf)))
  626. (error "bad section number" n))
  627. ((case (elf-word-size elf)
  628. ((4) parse-elf32-section-header)
  629. ((8) parse-elf64-section-header)
  630. (else (error "unhandled pointer size")))
  631. n
  632. (elf-bytes elf)
  633. (+ (elf-shoff elf) (* n (elf-shentsize elf)))
  634. (elf-byte-order elf)))
  635. (define (write-elf-section-header bv offset byte-order word-size sec)
  636. ((case word-size
  637. ((4) write-elf32-section-header)
  638. ((8) write-elf64-section-header)
  639. (else (error "invalid word size" word-size)))
  640. bv offset byte-order sec))
  641. (define (elf-sections elf)
  642. (let lp ((n (elf-shnum elf)) (out '()))
  643. (if (zero? n)
  644. out
  645. (lp (1- n) (cons (elf-section elf (1- n)) out)))))
  646. ;;
  647. ;; Section Types
  648. ;;
  649. (define SHT_NULL 0) ; Section header table entry unused
  650. (define SHT_PROGBITS 1) ; Program data
  651. (define SHT_SYMTAB 2) ; Symbol table
  652. (define SHT_STRTAB 3) ; String table
  653. (define SHT_RELA 4) ; Relocation entries with addends
  654. (define SHT_HASH 5) ; Symbol hash table
  655. (define SHT_DYNAMIC 6) ; Dynamic linking information
  656. (define SHT_NOTE 7) ; Notes
  657. (define SHT_NOBITS 8) ; Program space with no data (bss)
  658. (define SHT_REL 9) ; Relocation entries, no addends
  659. (define SHT_SHLIB 10) ; Reserved
  660. (define SHT_DYNSYM 11) ; Dynamic linker symbol table
  661. (define SHT_INIT_ARRAY 14) ; Array of constructors
  662. (define SHT_FINI_ARRAY 15) ; Array of destructors
  663. (define SHT_PREINIT_ARRAY 16) ; Array of pre-constructors
  664. (define SHT_GROUP 17) ; Section group
  665. (define SHT_SYMTAB_SHNDX 18) ; Extended section indeces
  666. (define SHT_NUM 19) ; Number of defined types.
  667. (define SHT_LOOS #x60000000) ; Start OS-specific.
  668. (define SHT_HIOS #x6fffffff) ; End OS-specific type
  669. (define SHT_LOPROC #x70000000) ; Start of processor-specific
  670. (define SHT_HIPROC #x7fffffff) ; End of processor-specific
  671. (define SHT_LOUSER #x80000000) ; Start of application-specific
  672. (define SHT_HIUSER #x8fffffff) ; End of application-specific
  673. ;;
  674. ;; Section Flags
  675. ;;
  676. (define SHF_WRITE (ash 1 0)) ; Writable
  677. (define SHF_ALLOC (ash 1 1)) ; Occupies memory during execution
  678. (define SHF_EXECINSTR (ash 1 2)) ; Executable
  679. (define SHF_MERGE (ash 1 4)) ; Might be merged
  680. (define SHF_STRINGS (ash 1 5)) ; Contains nul-terminated strings
  681. (define SHF_INFO_LINK (ash 1 6)) ; `sh_info' contains SHT index
  682. (define SHF_LINK_ORDER (ash 1 7)) ; Preserve order after combining
  683. (define SHF_OS_NONCONFORMING (ash 1 8)) ; Non-standard OS specific handling required
  684. (define SHF_GROUP (ash 1 9)) ; Section is member of a group.
  685. (define SHF_TLS (ash 1 10)) ; Section hold thread-local data.
  686. ;;
  687. ;; Dynamic entry types. The DT_GUILE types are non-standard.
  688. ;;
  689. (define DT_NULL 0) ; Marks end of dynamic section
  690. (define DT_NEEDED 1) ; Name of needed library
  691. (define DT_PLTRELSZ 2) ; Size in bytes of PLT relocs
  692. (define DT_PLTGOT 3) ; Processor defined value
  693. (define DT_HASH 4) ; Address of symbol hash table
  694. (define DT_STRTAB 5) ; Address of string table
  695. (define DT_SYMTAB 6) ; Address of symbol table
  696. (define DT_RELA 7) ; Address of Rela relocs
  697. (define DT_RELASZ 8) ; Total size of Rela relocs
  698. (define DT_RELAENT 9) ; Size of one Rela reloc
  699. (define DT_STRSZ 10) ; Size of string table
  700. (define DT_SYMENT 11) ; Size of one symbol table entry
  701. (define DT_INIT 12) ; Address of init function
  702. (define DT_FINI 13) ; Address of termination function
  703. (define DT_SONAME 14) ; Name of shared object
  704. (define DT_RPATH 15) ; Library search path (deprecated)
  705. (define DT_SYMBOLIC 16) ; Start symbol search here
  706. (define DT_REL 17) ; Address of Rel relocs
  707. (define DT_RELSZ 18) ; Total size of Rel relocs
  708. (define DT_RELENT 19) ; Size of one Rel reloc
  709. (define DT_PLTREL 20) ; Type of reloc in PLT
  710. (define DT_DEBUG 21) ; For debugging ; unspecified
  711. (define DT_TEXTREL 22) ; Reloc might modify .text
  712. (define DT_JMPREL 23) ; Address of PLT relocs
  713. (define DT_BIND_NOW 24) ; Process relocations of object
  714. (define DT_INIT_ARRAY 25) ; Array with addresses of init fct
  715. (define DT_FINI_ARRAY 26) ; Array with addresses of fini fct
  716. (define DT_INIT_ARRAYSZ 27) ; Size in bytes of DT_INIT_ARRAY
  717. (define DT_FINI_ARRAYSZ 28) ; Size in bytes of DT_FINI_ARRAY
  718. (define DT_RUNPATH 29) ; Library search path
  719. (define DT_FLAGS 30) ; Flags for the object being loaded
  720. (define DT_ENCODING 32) ; Start of encoded range
  721. (define DT_PREINIT_ARRAY 32) ; Array with addresses of preinit fc
  722. (define DT_PREINIT_ARRAYSZ 33) ; size in bytes of DT_PREINIT_ARRAY
  723. (define DT_NUM 34) ; Number used
  724. (define DT_LOGUILE #x37146000) ; Start of Guile-specific
  725. (define DT_GUILE_GC_ROOT #x37146000) ; Offset of GC roots
  726. (define DT_GUILE_GC_ROOT_SZ #x37146001) ; Size in machine words of GC roots
  727. (define DT_GUILE_ENTRY #x37146002) ; Address of entry thunk
  728. (define DT_GUILE_VM_VERSION #x37146003) ; Bytecode version
  729. (define DT_GUILE_FRAME_MAPS #x37146004) ; Offset of .guile.frame-maps
  730. (define DT_HIGUILE #x37146fff) ; End of Guile-specific
  731. (define DT_LOOS #x6000000d) ; Start of OS-specific
  732. (define DT_HIOS #x6ffff000) ; End of OS-specific
  733. (define DT_LOPROC #x70000000) ; Start of processor-specific
  734. (define DT_HIPROC #x7fffffff) ; End of processor-specific
  735. (define (string-table-ref bv offset)
  736. (let lp ((end offset))
  737. (if (zero? (bytevector-u8-ref bv end))
  738. (let ((out (make-bytevector (- end offset))))
  739. (bytevector-copy! bv offset out 0 (- end offset))
  740. (utf8->string out))
  741. (lp (1+ end)))))
  742. (define (elf-section-by-name elf name)
  743. (let ((off (elf-section-offset (elf-section elf (elf-shstrndx elf)))))
  744. (let lp ((n (elf-shnum elf)))
  745. (and (> n 0)
  746. (let ((section (elf-section elf (1- n))))
  747. (if (equal? (string-table-ref (elf-bytes elf)
  748. (+ off (elf-section-name section)))
  749. name)
  750. section
  751. (lp (1- n))))))))
  752. (define (elf-sections-by-name elf)
  753. (let* ((sections (elf-sections elf))
  754. (off (elf-section-offset (list-ref sections (elf-shstrndx elf)))))
  755. (map (lambda (section)
  756. (cons (string-table-ref (elf-bytes elf)
  757. (+ off (elf-section-name section)))
  758. section))
  759. sections)))
  760. (define-record-type <elf-symbol>
  761. (make-elf-symbol name value size info other shndx)
  762. elf-symbol?
  763. (name elf-symbol-name)
  764. (value elf-symbol-value)
  765. (size elf-symbol-size)
  766. (info elf-symbol-info)
  767. (other elf-symbol-other)
  768. (shndx elf-symbol-shndx))
  769. (define* (make-elf-symbol* #:key (name 0) (value 0) (size 0)
  770. (binding STB_LOCAL) (type STT_NOTYPE)
  771. (info (logior (ash binding 4) type))
  772. (visibility STV_DEFAULT) (other visibility)
  773. (shndx SHN_UNDEF))
  774. (make-elf-symbol name value size info other shndx))
  775. ;; typedef struct {
  776. ;; uint32_t st_name;
  777. ;; Elf32_Addr st_value;
  778. ;; uint32_t st_size;
  779. ;; unsigned char st_info;
  780. ;; unsigned char st_other;
  781. ;; uint16_t st_shndx;
  782. ;; } Elf32_Sym;
  783. (define (elf-symbol-len word-size)
  784. (case word-size
  785. ((4) 16)
  786. ((8) 24)
  787. (else (error "bad word size" word-size))))
  788. (define (elf-symbol-value-offset word-size)
  789. (case word-size
  790. ((4) 4)
  791. ((8) 8)
  792. (else (error "bad word size" word-size))))
  793. (define (parse-elf32-symbol bv offset stroff byte-order)
  794. (if (<= (+ offset 16) (bytevector-length bv))
  795. (make-elf-symbol (let ((name (bytevector-u32-ref bv offset byte-order)))
  796. (if stroff
  797. (string-table-ref bv (+ stroff name))
  798. name))
  799. (bytevector-u32-ref bv (+ offset 4) byte-order)
  800. (bytevector-u32-ref bv (+ offset 8) byte-order)
  801. (bytevector-u8-ref bv (+ offset 12))
  802. (bytevector-u8-ref bv (+ offset 13))
  803. (bytevector-u16-ref bv (+ offset 14) byte-order))
  804. (error "corrupt ELF (offset out of range)" offset)))
  805. (define (write-elf32-symbol bv offset byte-order sym)
  806. (bytevector-u32-set! bv offset (elf-symbol-name sym) byte-order)
  807. (bytevector-u32-set! bv (+ offset 4) (elf-symbol-value sym) byte-order)
  808. (bytevector-u32-set! bv (+ offset 8) (elf-symbol-size sym) byte-order)
  809. (bytevector-u8-set! bv (+ offset 12) (elf-symbol-info sym))
  810. (bytevector-u8-set! bv (+ offset 13) (elf-symbol-other sym))
  811. (bytevector-u16-set! bv (+ offset 14) (elf-symbol-shndx sym) byte-order))
  812. ;; typedef struct {
  813. ;; uint32_t st_name;
  814. ;; unsigned char st_info;
  815. ;; unsigned char st_other;
  816. ;; uint16_t st_shndx;
  817. ;; Elf64_Addr st_value;
  818. ;; uint64_t st_size;
  819. ;; } Elf64_Sym;
  820. (define (parse-elf64-symbol bv offset stroff byte-order)
  821. (if (<= (+ offset 24) (bytevector-length bv))
  822. (make-elf-symbol (let ((name (bytevector-u32-ref bv offset byte-order)))
  823. (if stroff
  824. (string-table-ref bv (+ stroff name))
  825. name))
  826. (bytevector-u64-ref bv (+ offset 8) byte-order)
  827. (bytevector-u64-ref bv (+ offset 16) byte-order)
  828. (bytevector-u8-ref bv (+ offset 4))
  829. (bytevector-u8-ref bv (+ offset 5))
  830. (bytevector-u16-ref bv (+ offset 6) byte-order))
  831. (error "corrupt ELF (offset out of range)" offset)))
  832. (define (write-elf64-symbol bv offset byte-order sym)
  833. (bytevector-u32-set! bv offset (elf-symbol-name sym) byte-order)
  834. (bytevector-u8-set! bv (+ offset 4) (elf-symbol-info sym))
  835. (bytevector-u8-set! bv (+ offset 5) (elf-symbol-other sym))
  836. (bytevector-u16-set! bv (+ offset 6) (elf-symbol-shndx sym) byte-order)
  837. (bytevector-u64-set! bv (+ offset 8) (elf-symbol-value sym) byte-order)
  838. (bytevector-u64-set! bv (+ offset 16) (elf-symbol-size sym) byte-order))
  839. (define (write-elf-symbol bv offset byte-order word-size sym)
  840. ((case word-size
  841. ((4) write-elf32-symbol)
  842. ((8) write-elf64-symbol)
  843. (else (error "invalid word size" word-size)))
  844. bv offset byte-order sym))
  845. (define (elf-symbol-table-len section)
  846. (let ((len (elf-section-size section))
  847. (entsize (elf-section-entsize section)))
  848. (unless (and (not (zero? entsize)) (zero? (modulo len entsize)))
  849. (error "bad symbol table" section))
  850. (/ len entsize)))
  851. (define* (elf-symbol-table-ref elf section n #:optional strtab)
  852. (let ((bv (elf-bytes elf))
  853. (byte-order (elf-byte-order elf))
  854. (stroff (and strtab (elf-section-offset strtab)))
  855. (base (elf-section-offset section))
  856. (len (elf-section-size section))
  857. (entsize (elf-section-entsize section)))
  858. (unless (<= (* (1+ n) entsize) len)
  859. (error "out of range symbol table access" section n))
  860. (case (elf-word-size elf)
  861. ((4)
  862. (unless (<= 16 entsize)
  863. (error "bad entsize for symbol table" section))
  864. (parse-elf32-symbol bv (+ base (* n entsize)) stroff byte-order))
  865. ((8)
  866. (unless (<= 24 entsize)
  867. (error "bad entsize for symbol table" section))
  868. (parse-elf64-symbol bv (+ base (* n entsize)) stroff byte-order))
  869. (else (error "bad word size" elf)))))
  870. ;; Legal values for ST_BIND subfield of st_info (symbol binding).
  871. (define STB_LOCAL 0) ; Local symbol
  872. (define STB_GLOBAL 1) ; Global symbol
  873. (define STB_WEAK 2) ; Weak symbol
  874. (define STB_NUM 3) ; Number of defined types.
  875. (define STB_LOOS 10) ; Start of OS-specific
  876. (define STB_GNU_UNIQUE 10) ; Unique symbol.
  877. (define STB_HIOS 12) ; End of OS-specific
  878. (define STB_LOPROC 13) ; Start of processor-specific
  879. (define STB_HIPROC 15) ; End of processor-specific
  880. ;; Legal values for ST_TYPE subfield of st_info (symbol type).
  881. (define STT_NOTYPE 0) ; Symbol type is unspecified
  882. (define STT_OBJECT 1) ; Symbol is a data object
  883. (define STT_FUNC 2) ; Symbol is a code object
  884. (define STT_SECTION 3) ; Symbol associated with a section
  885. (define STT_FILE 4) ; Symbol's name is file name
  886. (define STT_COMMON 5) ; Symbol is a common data object
  887. (define STT_TLS 6) ; Symbol is thread-local data objec
  888. (define STT_NUM 7) ; Number of defined types.
  889. (define STT_LOOS 10) ; Start of OS-specific
  890. (define STT_GNU_IFUNC 10) ; Symbol is indirect code object
  891. (define STT_HIOS 12) ; End of OS-specific
  892. (define STT_LOPROC 13) ; Start of processor-specific
  893. (define STT_HIPROC 15) ; End of processor-specific
  894. ;; Symbol visibility specification encoded in the st_other field.
  895. (define STV_DEFAULT 0) ; Default symbol visibility rules
  896. (define STV_INTERNAL 1) ; Processor specific hidden class
  897. (define STV_HIDDEN 2) ; Sym unavailable in other modules
  898. (define STV_PROTECTED 3) ; Not preemptible, not exported
  899. (define (elf-symbol-binding sym)
  900. (ash (elf-symbol-info sym) -4))
  901. (define (elf-symbol-type sym)
  902. (logand (elf-symbol-info sym) #xf))
  903. (define (elf-symbol-visibility sym)
  904. (logand (elf-symbol-other sym) #x3))
  905. (define NT_GNU_ABI_TAG 1)
  906. (define NT_GNU_HWCAP 2)
  907. (define NT_GNU_BUILD_ID 3)
  908. (define NT_GNU_GOLD_VERSION 4)
  909. (define-record-type <elf-note>
  910. (make-elf-note name desc type)
  911. elf-note?
  912. (name elf-note-name)
  913. (desc elf-note-desc)
  914. (type elf-note-type))
  915. (define (parse-elf-note elf section)
  916. (let ((bv (elf-bytes elf))
  917. (byte-order (elf-byte-order elf))
  918. (offset (elf-section-offset section)))
  919. (unless (<= (+ offset 12) (bytevector-length bv))
  920. (error "corrupt ELF (offset out of range)" offset))
  921. (let ((namesz (bytevector-u32-ref bv offset byte-order))
  922. (descsz (bytevector-u32-ref bv (+ offset 4) byte-order))
  923. (type (bytevector-u32-ref bv (+ offset 8) byte-order)))
  924. (unless (<= (+ offset 12 namesz descsz) (bytevector-length bv))
  925. (error "corrupt ELF (offset out of range)" offset))
  926. (let ((name (make-bytevector (1- namesz)))
  927. (desc (make-bytevector descsz)))
  928. (bytevector-copy! bv (+ offset 12) name 0 (1- namesz))
  929. (bytevector-copy! bv (+ offset 12 namesz) desc 0 descsz)
  930. (make-elf-note (utf8->string name) desc type)))))