|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (system vm elf) |
|
#:use-module (rnrs bytevectors) |
|
#:use-module (system foreign) |
|
#:use-module (system base target) |
|
#:use-module (srfi srfi-9) |
|
#:export (has-elf-header? |
|
|
|
(make-elf* . make-elf) |
|
elf? |
|
elf-bytes elf-word-size elf-byte-order |
|
elf-abi elf-type elf-machine-type |
|
elf-entry elf-phoff elf-shoff elf-flags elf-ehsize |
|
elf-phentsize elf-phnum elf-shentsize elf-shnum elf-shstrndx |
|
|
|
ELFOSABI_NONE ELFOSABI_HPUX ELFOSABI_NETBSD ELFOSABI_GNU |
|
ELFOSABI_SOLARIS ELFOSABI_AIX ELFOSABI_IRIX ELFOSABI_FREEBSD |
|
ELFOSABI_TRU64 ELFOSABI_MODESTO ELFOSABI_OPENBSD |
|
ELFOSABI_ARM_AEABI ELFOSABI_ARM ELFOSABI_STANDALONE |
|
|
|
ET_NONE ET_REL ET_EXEC ET_DYN ET_CORE |
|
|
|
EM_NONE EM_SPARC EM_386 EM_MIPS EM_PPC EM_PPC64 EM_ARM EM_SH |
|
EM_SPARCV9 EM_IA_64 EM_X86_64 |
|
|
|
elf-header-len elf-header-shoff-offset |
|
write-elf-header |
|
|
|
(make-elf-segment* . make-elf-segment) |
|
elf-segment? |
|
elf-segment-index |
|
elf-segment-type elf-segment-offset elf-segment-vaddr |
|
elf-segment-paddr elf-segment-filesz elf-segment-memsz |
|
elf-segment-flags elf-segment-align |
|
|
|
elf-program-header-len write-elf-program-header |
|
|
|
PT_NULL PT_LOAD PT_DYNAMIC PT_INTERP PT_NOTE PT_SHLIB |
|
PT_PHDR PT_TLS PT_NUM PT_LOOS PT_GNU_EH_FRAME PT_GNU_STACK |
|
PT_GNU_RELRO |
|
|
|
PF_R PF_W PF_X |
|
|
|
(make-elf-section* . make-elf-section) |
|
elf-section? |
|
elf-section-index |
|
elf-section-name elf-section-type elf-section-flags |
|
elf-section-addr elf-section-offset elf-section-size |
|
elf-section-link elf-section-info elf-section-addralign |
|
elf-section-entsize |
|
|
|
elf-section-header-len elf-section-header-addr-offset |
|
elf-section-header-offset-offset |
|
write-elf-section-header |
|
|
|
(make-elf-symbol* . make-elf-symbol) |
|
elf-symbol? |
|
elf-symbol-name elf-symbol-value elf-symbol-size |
|
elf-symbol-info elf-symbol-other elf-symbol-shndx |
|
elf-symbol-binding elf-symbol-type elf-symbol-visibility |
|
|
|
elf-symbol-len elf-symbol-value-offset write-elf-symbol |
|
|
|
SHN_UNDEF |
|
|
|
SHT_NULL SHT_PROGBITS SHT_SYMTAB SHT_STRTAB SHT_RELA |
|
SHT_HASH SHT_DYNAMIC SHT_NOTE SHT_NOBITS SHT_REL SHT_SHLIB |
|
SHT_DYNSYM SHT_INIT_ARRAY SHT_FINI_ARRAY SHT_PREINIT_ARRAY |
|
SHT_GROUP SHT_SYMTAB_SHNDX SHT_NUM SHT_LOOS SHT_HIOS |
|
SHT_LOPROC SHT_HIPROC SHT_LOUSER SHT_HIUSER |
|
|
|
SHF_WRITE SHF_ALLOC SHF_EXECINSTR SHF_MERGE SHF_STRINGS |
|
SHF_INFO_LINK SHF_LINK_ORDER SHF_OS_NONCONFORMING SHF_GROUP |
|
SHF_TLS |
|
|
|
DT_NULL DT_NEEDED DT_PLTRELSZ DT_PLTGOT DT_HASH DT_STRTAB |
|
DT_SYMTAB DT_RELA DT_RELASZ DT_RELAENT DT_STRSZ DT_SYMENT |
|
DT_INIT DT_FINI DT_SONAME DT_RPATH DT_SYMBOLIC DT_REL |
|
DT_RELSZ DT_RELENT DT_PLTREL DT_DEBUG DT_TEXTREL DT_JMPREL |
|
DT_BIND_NOW DT_INIT_ARRAY DT_FINI_ARRAY DT_INIT_ARRAYSZ |
|
DT_FINI_ARRAYSZ DT_RUNPATH DT_FLAGS DT_ENCODING |
|
DT_PREINIT_ARRAY DT_PREINIT_ARRAYSZ DT_NUM DT_LOGUILE |
|
DT_GUILE_GC_ROOT DT_GUILE_GC_ROOT_SZ DT_GUILE_ENTRY |
|
DT_GUILE_VM_VERSION DT_GUILE_FRAME_MAPS DT_HIGUILE |
|
DT_LOOS DT_HIOS DT_LOPROC DT_HIPROC |
|
|
|
string-table-ref |
|
|
|
STB_LOCAL STB_GLOBAL STB_WEAK STB_NUM STB_LOOS STB_GNU |
|
STB_HIOS STB_LOPROC STB_HIPROC |
|
|
|
STT_NOTYPE STT_OBJECT STT_FUNC STT_SECTION STT_FILE |
|
STT_COMMON STT_TLS STT_NUM STT_LOOS STT_GNU STT_HIOS |
|
STT_LOPROC STT_HIPROC |
|
|
|
STV_DEFAULT STV_INTERNAL STV_HIDDEN STV_PROTECTED |
|
|
|
NT_GNU_ABI_TAG NT_GNU_HWCAP NT_GNU_BUILD_ID NT_GNU_GOLD_VERSION |
|
|
|
parse-elf |
|
elf-segment elf-segments |
|
elf-section elf-sections elf-section-by-name elf-sections-by-name |
|
elf-symbol-table-len elf-symbol-table-ref |
|
|
|
parse-elf-note |
|
elf-note-name elf-note-desc elf-note-type)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define elf32-header-len 52) |
|
(define elf64-header-len 64) |
|
(define (elf-header-len word-size) |
|
(case word-size |
|
((4) elf32-header-len) |
|
((8) elf64-header-len) |
|
(else (error "invalid word size" word-size)))) |
|
(define (elf-header-shoff-offset word-size) |
|
(case word-size |
|
((4) 32) |
|
((8) 40) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define ELFCLASS32 1) |
|
(define ELFCLASS64 2) |
|
|
|
(define ELFDATA2LSB 1) |
|
(define ELFDATA2MSB 2) |
|
|
|
(define EV_CURRENT 1) |
|
|
|
(define ELFOSABI_NONE 0) |
|
(define ELFOSABI_HPUX 1) |
|
(define ELFOSABI_NETBSD 2) |
|
(define ELFOSABI_GNU 3) |
|
(define ELFOSABI_SOLARIS 6) |
|
(define ELFOSABI_AIX 7) |
|
(define ELFOSABI_IRIX 8) |
|
(define ELFOSABI_FREEBSD 9) |
|
(define ELFOSABI_TRU64 10) |
|
(define ELFOSABI_MODESTO 11) |
|
(define ELFOSABI_OPENBSD 12) |
|
(define ELFOSABI_ARM_AEABI 64) |
|
(define ELFOSABI_ARM 97) |
|
(define ELFOSABI_STANDALONE 255) |
|
|
|
(define ET_NONE 0) |
|
(define ET_REL 1) |
|
(define ET_EXEC 2) |
|
(define ET_DYN 3) |
|
(define ET_CORE 4) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define EM_NONE 0) |
|
(define EM_SPARC 2) |
|
(define EM_386 3) |
|
(define EM_MIPS 8) |
|
(define EM_PPC 20) |
|
(define EM_PPC64 21) |
|
(define EM_ARM 40) |
|
(define EM_SH 42) |
|
(define EM_SPARCV9 43) |
|
(define EM_IA_64 50) |
|
(define EM_X86_64 62) |
|
|
|
(define cpu-mapping (make-hash-table)) |
|
(for-each (lambda (pair) |
|
(hashq-set! cpu-mapping (car pair) (cdr pair))) |
|
`((none . ,EM_NONE) |
|
(sparc . ,EM_SPARC) ; FIXME: map 64-bit to SPARCV9 ? |
|
(i386 . ,EM_386) |
|
(mips . ,EM_MIPS) |
|
(ppc . ,EM_PPC) |
|
(ppc64 . ,EM_PPC64) |
|
(arm . ,EM_ARM) ; FIXME: there are more arm cpu variants |
|
(sh . ,EM_SH) ; FIXME: there are more sh cpu variants |
|
(ia64 . ,EM_IA_64) |
|
(x86_64 . ,EM_X86_64))) |
|
|
|
(define SHN_UNDEF 0) |
|
|
|
(define host-machine-type |
|
(hashq-ref cpu-mapping |
|
(string->symbol (car (string-split %host-type #\-))) |
|
EM_NONE)) |
|
|
|
(define host-word-size |
|
(sizeof '*)) |
|
|
|
(define host-byte-order |
|
(native-endianness)) |
|
|
|
(define (has-elf-header? bv) |
|
(and |
|
|
|
(>= (bytevector-length bv) 16) |
|
(= (bytevector-u8-ref bv 0) #x7f) |
|
(= (bytevector-u8-ref bv 1) (char->integer #\E)) |
|
(= (bytevector-u8-ref bv 2) (char->integer #\L)) |
|
(= (bytevector-u8-ref bv 3) (char->integer #\F)) |
|
(cond |
|
((= (bytevector-u8-ref bv 4) ELFCLASS32) |
|
(>= (bytevector-length bv) elf32-header-len)) |
|
((= (bytevector-u8-ref bv 4) ELFCLASS64) |
|
(>= (bytevector-length bv) elf64-header-len)) |
|
(else #f)) |
|
(or (= (bytevector-u8-ref bv 5) ELFDATA2LSB) |
|
(= (bytevector-u8-ref bv 5) ELFDATA2MSB)) |
|
(= (bytevector-u8-ref bv 6) EV_CURRENT) |
|
|
|
(= (bytevector-u8-ref bv 8) 0) |
|
|
|
|
|
|
|
(let ((byte-order (if (= (bytevector-u8-ref bv 5) ELFDATA2LSB) |
|
(endianness little) |
|
(endianness big)))) |
|
(= (bytevector-u32-ref bv 20 byte-order) EV_CURRENT)))) |
|
|
|
(define-record-type <elf> |
|
(make-elf bytes word-size byte-order abi type machine-type |
|
entry phoff shoff flags ehsize |
|
phentsize phnum shentsize shnum shstrndx) |
|
elf? |
|
(bytes elf-bytes) |
|
(word-size elf-word-size) |
|
(byte-order elf-byte-order) |
|
(abi elf-abi) |
|
(type elf-type) |
|
(machine-type elf-machine-type) |
|
(entry elf-entry) |
|
(phoff elf-phoff) |
|
(shoff elf-shoff) |
|
(flags elf-flags) |
|
(ehsize elf-ehsize) |
|
(phentsize elf-phentsize) |
|
(phnum elf-phnum) |
|
(shentsize elf-shentsize) |
|
(shnum elf-shnum) |
|
(shstrndx elf-shstrndx)) |
|
|
|
(define* (make-elf* #:key (bytes #f) |
|
(byte-order (target-endianness)) |
|
(word-size (target-word-size)) |
|
(abi ELFOSABI_STANDALONE) |
|
(type ET_DYN) |
|
(machine-type EM_NONE) |
|
(entry 0) |
|
(phoff (elf-header-len word-size)) |
|
(shoff -1) |
|
(flags 0) |
|
(ehsize (elf-header-len word-size)) |
|
(phentsize (elf-program-header-len word-size)) |
|
(phnum 0) |
|
(shentsize (elf-section-header-len word-size)) |
|
(shnum 0) |
|
(shstrndx SHN_UNDEF)) |
|
(make-elf bytes word-size byte-order abi type machine-type |
|
entry phoff shoff flags ehsize |
|
phentsize phnum shentsize shnum shstrndx)) |
|
|
|
(define (parse-elf32 bv byte-order) |
|
(make-elf bv 4 byte-order |
|
(bytevector-u8-ref bv 7) |
|
(bytevector-u16-ref bv 16 byte-order) |
|
(bytevector-u16-ref bv 18 byte-order) |
|
(bytevector-u32-ref bv 24 byte-order) |
|
(bytevector-u32-ref bv 28 byte-order) |
|
(bytevector-u32-ref bv 32 byte-order) |
|
(bytevector-u32-ref bv 36 byte-order) |
|
(bytevector-u16-ref bv 40 byte-order) |
|
(bytevector-u16-ref bv 42 byte-order) |
|
(bytevector-u16-ref bv 44 byte-order) |
|
(bytevector-u16-ref bv 46 byte-order) |
|
(bytevector-u16-ref bv 48 byte-order) |
|
(bytevector-u16-ref bv 50 byte-order))) |
|
|
|
(define (write-elf-ident bv class data abi) |
|
(bytevector-u8-set! bv 0 #x7f) |
|
(bytevector-u8-set! bv 1 (char->integer #\E)) |
|
(bytevector-u8-set! bv 2 (char->integer #\L)) |
|
(bytevector-u8-set! bv 3 (char->integer #\F)) |
|
(bytevector-u8-set! bv 4 class) |
|
(bytevector-u8-set! bv 5 data) |
|
(bytevector-u8-set! bv 6 EV_CURRENT) |
|
(bytevector-u8-set! bv 7 abi) |
|
(bytevector-u8-set! bv 8 0) |
|
(bytevector-u8-set! bv 9 0) |
|
(bytevector-u8-set! bv 10 0) |
|
(bytevector-u8-set! bv 11 0) |
|
(bytevector-u8-set! bv 12 0) |
|
(bytevector-u8-set! bv 13 0) |
|
(bytevector-u8-set! bv 14 0) |
|
(bytevector-u8-set! bv 15 0)) |
|
|
|
(define (write-elf32-header bv elf) |
|
(let ((byte-order (elf-byte-order elf))) |
|
(write-elf-ident bv ELFCLASS32 |
|
(case byte-order |
|
((little) ELFDATA2LSB) |
|
((big) ELFDATA2MSB) |
|
(else (error "unknown endianness" byte-order))) |
|
(elf-abi elf)) |
|
(bytevector-u16-set! bv 16 (elf-type elf) byte-order) |
|
(bytevector-u16-set! bv 18 (elf-machine-type elf) byte-order) |
|
(bytevector-u32-set! bv 20 EV_CURRENT byte-order) |
|
(bytevector-u32-set! bv 24 (elf-entry elf) byte-order) |
|
(bytevector-u32-set! bv 28 (elf-phoff elf) byte-order) |
|
(bytevector-u32-set! bv 32 (elf-shoff elf) byte-order) |
|
(bytevector-u32-set! bv 36 (elf-flags elf) byte-order) |
|
(bytevector-u16-set! bv 40 (elf-ehsize elf) byte-order) |
|
(bytevector-u16-set! bv 42 (elf-phentsize elf) byte-order) |
|
(bytevector-u16-set! bv 44 (elf-phnum elf) byte-order) |
|
(bytevector-u16-set! bv 46 (elf-shentsize elf) byte-order) |
|
(bytevector-u16-set! bv 48 (elf-shnum elf) byte-order) |
|
(bytevector-u16-set! bv 50 (elf-shstrndx elf) byte-order))) |
|
|
|
(define (parse-elf64 bv byte-order) |
|
(make-elf bv 8 byte-order |
|
(bytevector-u8-ref bv 7) |
|
(bytevector-u16-ref bv 16 byte-order) |
|
(bytevector-u16-ref bv 18 byte-order) |
|
(bytevector-u64-ref bv 24 byte-order) |
|
(bytevector-u64-ref bv 32 byte-order) |
|
(bytevector-u64-ref bv 40 byte-order) |
|
(bytevector-u32-ref bv 48 byte-order) |
|
(bytevector-u16-ref bv 52 byte-order) |
|
(bytevector-u16-ref bv 54 byte-order) |
|
(bytevector-u16-ref bv 56 byte-order) |
|
(bytevector-u16-ref bv 58 byte-order) |
|
(bytevector-u16-ref bv 60 byte-order) |
|
(bytevector-u16-ref bv 62 byte-order))) |
|
|
|
(define (write-elf64-header bv elf) |
|
(let ((byte-order (elf-byte-order elf))) |
|
(write-elf-ident bv ELFCLASS64 |
|
(case byte-order |
|
((little) ELFDATA2LSB) |
|
((big) ELFDATA2MSB) |
|
(else (error "unknown endianness" byte-order))) |
|
(elf-abi elf)) |
|
(bytevector-u16-set! bv 16 (elf-type elf) byte-order) |
|
(bytevector-u16-set! bv 18 (elf-machine-type elf) byte-order) |
|
(bytevector-u32-set! bv 20 EV_CURRENT byte-order) |
|
(bytevector-u64-set! bv 24 (elf-entry elf) byte-order) |
|
(bytevector-u64-set! bv 32 (elf-phoff elf) byte-order) |
|
(bytevector-u64-set! bv 40 (elf-shoff elf) byte-order) |
|
(bytevector-u32-set! bv 48 (elf-flags elf) byte-order) |
|
(bytevector-u16-set! bv 52 (elf-ehsize elf) byte-order) |
|
(bytevector-u16-set! bv 54 (elf-phentsize elf) byte-order) |
|
(bytevector-u16-set! bv 56 (elf-phnum elf) byte-order) |
|
(bytevector-u16-set! bv 58 (elf-shentsize elf) byte-order) |
|
(bytevector-u16-set! bv 60 (elf-shnum elf) byte-order) |
|
(bytevector-u16-set! bv 62 (elf-shstrndx elf) byte-order))) |
|
|
|
(define (parse-elf bv) |
|
(cond |
|
((has-elf-header? bv) |
|
(let ((class (bytevector-u8-ref bv 4)) |
|
(byte-order (let ((data (bytevector-u8-ref bv 5))) |
|
(cond |
|
((= data ELFDATA2LSB) (endianness little)) |
|
((= data ELFDATA2MSB) (endianness big)) |
|
(else (error "unhandled byte order" data)))))) |
|
(cond |
|
((= class ELFCLASS32) (parse-elf32 bv byte-order)) |
|
((= class ELFCLASS64) (parse-elf64 bv byte-order)) |
|
(else (error "unhandled class" class))))) |
|
(else |
|
(error "Invalid ELF" bv)))) |
|
|
|
(define* (write-elf-header bv elf) |
|
((case (elf-word-size elf) |
|
((4) write-elf32-header) |
|
((8) write-elf64-header) |
|
(else (error "unknown word size" (elf-word-size elf)))) |
|
bv elf)) |
|
|
|
|
|
|
|
|
|
(define PT_NULL 0) |
|
(define PT_LOAD 1) |
|
(define PT_DYNAMIC 2) |
|
(define PT_INTERP 3) |
|
(define PT_NOTE 4) |
|
(define PT_SHLIB 5) |
|
(define PT_PHDR 6) |
|
(define PT_TLS 7) |
|
(define PT_NUM 8) |
|
(define PT_LOOS #x60000000) |
|
(define PT_GNU_EH_FRAME #x6474e550) |
|
(define PT_GNU_STACK #x6474e551) |
|
(define PT_GNU_RELRO #x6474e552) |
|
|
|
|
|
|
|
|
|
(define PF_X (ash 1 0)) |
|
(define PF_W (ash 1 1)) |
|
(define PF_R (ash 1 2)) |
|
|
|
(define-record-type <elf-segment> |
|
(make-elf-segment index type offset vaddr paddr filesz memsz flags align) |
|
elf-segment? |
|
(index elf-segment-index) |
|
(type elf-segment-type) |
|
(offset elf-segment-offset) |
|
(vaddr elf-segment-vaddr) |
|
(paddr elf-segment-paddr) |
|
(filesz elf-segment-filesz) |
|
(memsz elf-segment-memsz) |
|
(flags elf-segment-flags) |
|
(align elf-segment-align)) |
|
|
|
(define* (make-elf-segment* #:key (index -1) (type PT_LOAD) (offset 0) (vaddr 0) |
|
(paddr 0) (filesz 0) (memsz filesz) |
|
(flags (logior PF_W PF_R)) |
|
(align 8)) |
|
(make-elf-segment index type offset vaddr paddr filesz memsz flags align)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (parse-elf32-program-header index bv offset byte-order) |
|
(if (<= (+ offset 32) (bytevector-length bv)) |
|
(make-elf-segment index |
|
(bytevector-u32-ref bv offset byte-order) |
|
(bytevector-u32-ref bv (+ offset 4) byte-order) |
|
(bytevector-u32-ref bv (+ offset 8) byte-order) |
|
(bytevector-u32-ref bv (+ offset 12) byte-order) |
|
(bytevector-u32-ref bv (+ offset 16) byte-order) |
|
(bytevector-u32-ref bv (+ offset 20) byte-order) |
|
(bytevector-u32-ref bv (+ offset 24) byte-order) |
|
(bytevector-u32-ref bv (+ offset 28) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf32-program-header bv offset byte-order seg) |
|
(bytevector-u32-set! bv offset (elf-segment-type seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 4) (elf-segment-offset seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 8) (elf-segment-vaddr seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 12) (elf-segment-paddr seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 16) (elf-segment-filesz seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 20) (elf-segment-memsz seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 24) (elf-segment-flags seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 28) (elf-segment-align seg) byte-order)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (parse-elf64-program-header index bv offset byte-order) |
|
(if (<= (+ offset 56) (bytevector-length bv)) |
|
(make-elf-segment index |
|
(bytevector-u32-ref bv offset byte-order) |
|
(bytevector-u64-ref bv (+ offset 8) byte-order) |
|
(bytevector-u64-ref bv (+ offset 16) byte-order) |
|
(bytevector-u64-ref bv (+ offset 24) byte-order) |
|
(bytevector-u64-ref bv (+ offset 32) byte-order) |
|
(bytevector-u64-ref bv (+ offset 40) byte-order) |
|
(bytevector-u32-ref bv (+ offset 4) byte-order) |
|
(bytevector-u64-ref bv (+ offset 48) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf64-program-header bv offset byte-order seg) |
|
(bytevector-u32-set! bv offset (elf-segment-type seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 8) (elf-segment-offset seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 16) (elf-segment-vaddr seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 24) (elf-segment-paddr seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 32) (elf-segment-filesz seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 40) (elf-segment-memsz seg) byte-order) |
|
(bytevector-u32-set! bv (+ offset 4) (elf-segment-flags seg) byte-order) |
|
(bytevector-u64-set! bv (+ offset 48) (elf-segment-align seg) byte-order)) |
|
|
|
(define (write-elf-program-header bv offset byte-order word-size seg) |
|
((case word-size |
|
((4) write-elf32-program-header) |
|
((8) write-elf64-program-header) |
|
(else (error "invalid word size" word-size))) |
|
bv offset byte-order seg)) |
|
|
|
(define (elf-program-header-len word-size) |
|
(case word-size |
|
((4) 32) |
|
((8) 56) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (elf-segment elf n) |
|
(if (not (< -1 n (elf-phnum elf))) |
|
(error "bad segment number" n)) |
|
((case (elf-word-size elf) |
|
((4) parse-elf32-program-header) |
|
((8) parse-elf64-program-header) |
|
(else (error "unhandled pointer size"))) |
|
n |
|
(elf-bytes elf) |
|
(+ (elf-phoff elf) (* n (elf-phentsize elf))) |
|
(elf-byte-order elf))) |
|
|
|
(define (elf-segments elf) |
|
(let lp ((n (elf-phnum elf)) (out '())) |
|
(if (zero? n) |
|
out |
|
(lp (1- n) (cons (elf-segment elf (1- n)) out))))) |
|
|
|
(define-record-type <elf-section> |
|
(make-elf-section index name type flags |
|
addr offset size link info addralign entsize) |
|
elf-section? |
|
(index elf-section-index) |
|
(name elf-section-name) |
|
(type elf-section-type) |
|
(flags elf-section-flags) |
|
(addr elf-section-addr) |
|
(offset elf-section-offset) |
|
(size elf-section-size) |
|
(link elf-section-link) |
|
(info elf-section-info) |
|
(addralign elf-section-addralign) |
|
(entsize elf-section-entsize)) |
|
|
|
(define* (make-elf-section* #:key (index SHN_UNDEF) (name 0) (type SHT_PROGBITS) |
|
(flags SHF_ALLOC) (addr 0) (offset 0) (size 0) |
|
(link 0) (info 0) (addralign 8) (entsize 0)) |
|
(make-elf-section index name type flags addr offset size link info addralign |
|
entsize)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (parse-elf32-section-header index bv offset byte-order) |
|
(if (<= (+ offset 40) (bytevector-length bv)) |
|
(make-elf-section index |
|
(bytevector-u32-ref bv offset byte-order) |
|
(bytevector-u32-ref bv (+ offset 4) byte-order) |
|
(bytevector-u32-ref bv (+ offset 8) byte-order) |
|
(bytevector-u32-ref bv (+ offset 12) byte-order) |
|
(bytevector-u32-ref bv (+ offset 16) byte-order) |
|
(bytevector-u32-ref bv (+ offset 20) byte-order) |
|
(bytevector-u32-ref bv (+ offset 24) byte-order) |
|
(bytevector-u32-ref bv (+ offset 28) byte-order) |
|
(bytevector-u32-ref bv (+ offset 32) byte-order) |
|
(bytevector-u32-ref bv (+ offset 36) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf32-section-header bv offset byte-order sec) |
|
(bytevector-u32-set! bv offset (elf-section-name sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 4) (elf-section-type sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 8) (elf-section-flags sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 12) (elf-section-addr sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 16) (elf-section-offset sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 20) (elf-section-size sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 24) (elf-section-link sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 28) (elf-section-info sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 32) (elf-section-addralign sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 36) (elf-section-entsize sec) byte-order)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (elf-section-header-len word-size) |
|
(case word-size |
|
((4) 40) |
|
((8) 64) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (elf-section-header-addr-offset word-size) |
|
(case word-size |
|
((4) 12) |
|
((8) 16) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (elf-section-header-offset-offset word-size) |
|
(case word-size |
|
((4) 16) |
|
((8) 24) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (parse-elf64-section-header index bv offset byte-order) |
|
(if (<= (+ offset 64) (bytevector-length bv)) |
|
(make-elf-section index |
|
(bytevector-u32-ref bv offset byte-order) |
|
(bytevector-u32-ref bv (+ offset 4) byte-order) |
|
(bytevector-u64-ref bv (+ offset 8) byte-order) |
|
(bytevector-u64-ref bv (+ offset 16) byte-order) |
|
(bytevector-u64-ref bv (+ offset 24) byte-order) |
|
(bytevector-u64-ref bv (+ offset 32) byte-order) |
|
(bytevector-u32-ref bv (+ offset 40) byte-order) |
|
(bytevector-u32-ref bv (+ offset 44) byte-order) |
|
(bytevector-u64-ref bv (+ offset 48) byte-order) |
|
(bytevector-u64-ref bv (+ offset 56) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf64-section-header bv offset byte-order sec) |
|
(bytevector-u32-set! bv offset (elf-section-name sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 4) (elf-section-type sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 8) (elf-section-flags sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 16) (elf-section-addr sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 24) (elf-section-offset sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 32) (elf-section-size sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 40) (elf-section-link sec) byte-order) |
|
(bytevector-u32-set! bv (+ offset 44) (elf-section-info sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 48) (elf-section-addralign sec) byte-order) |
|
(bytevector-u64-set! bv (+ offset 56) (elf-section-entsize sec) byte-order)) |
|
|
|
(define (elf-section elf n) |
|
(if (not (< -1 n (elf-shnum elf))) |
|
(error "bad section number" n)) |
|
((case (elf-word-size elf) |
|
((4) parse-elf32-section-header) |
|
((8) parse-elf64-section-header) |
|
(else (error "unhandled pointer size"))) |
|
n |
|
(elf-bytes elf) |
|
(+ (elf-shoff elf) (* n (elf-shentsize elf))) |
|
(elf-byte-order elf))) |
|
|
|
(define (write-elf-section-header bv offset byte-order word-size sec) |
|
((case word-size |
|
((4) write-elf32-section-header) |
|
((8) write-elf64-section-header) |
|
(else (error "invalid word size" word-size))) |
|
bv offset byte-order sec)) |
|
|
|
(define (elf-sections elf) |
|
(let lp ((n (elf-shnum elf)) (out '())) |
|
(if (zero? n) |
|
out |
|
(lp (1- n) (cons (elf-section elf (1- n)) out))))) |
|
|
|
|
|
|
|
|
|
(define SHT_NULL 0) |
|
(define SHT_PROGBITS 1) |
|
(define SHT_SYMTAB 2) |
|
(define SHT_STRTAB 3) |
|
(define SHT_RELA 4) |
|
(define SHT_HASH 5) |
|
(define SHT_DYNAMIC 6) |
|
(define SHT_NOTE 7) |
|
(define SHT_NOBITS 8) |
|
(define SHT_REL 9) |
|
(define SHT_SHLIB 10) |
|
(define SHT_DYNSYM 11) |
|
(define SHT_INIT_ARRAY 14) |
|
(define SHT_FINI_ARRAY 15) |
|
(define SHT_PREINIT_ARRAY 16) |
|
(define SHT_GROUP 17) |
|
(define SHT_SYMTAB_SHNDX 18) |
|
(define SHT_NUM 19) |
|
(define SHT_LOOS #x60000000) |
|
(define SHT_HIOS #x6fffffff) |
|
(define SHT_LOPROC #x70000000) |
|
(define SHT_HIPROC #x7fffffff) |
|
(define SHT_LOUSER #x80000000) |
|
(define SHT_HIUSER #x8fffffff) |
|
|
|
|
|
|
|
|
|
(define SHF_WRITE (ash 1 0)) |
|
(define SHF_ALLOC (ash 1 1)) |
|
(define SHF_EXECINSTR (ash 1 2)) |
|
(define SHF_MERGE (ash 1 4)) |
|
(define SHF_STRINGS (ash 1 5)) |
|
(define SHF_INFO_LINK (ash 1 6)) |
|
(define SHF_LINK_ORDER (ash 1 7)) |
|
(define SHF_OS_NONCONFORMING (ash 1 8)) |
|
(define SHF_GROUP (ash 1 9)) |
|
(define SHF_TLS (ash 1 10)) |
|
|
|
|
|
|
|
|
|
(define DT_NULL 0) |
|
(define DT_NEEDED 1) |
|
(define DT_PLTRELSZ 2) |
|
(define DT_PLTGOT 3) |
|
(define DT_HASH 4) |
|
(define DT_STRTAB 5) |
|
(define DT_SYMTAB 6) |
|
(define DT_RELA 7) |
|
(define DT_RELASZ 8) |
|
(define DT_RELAENT 9) |
|
(define DT_STRSZ 10) |
|
(define DT_SYMENT 11) |
|
(define DT_INIT 12) |
|
(define DT_FINI 13) |
|
(define DT_SONAME 14) |
|
(define DT_RPATH 15) |
|
(define DT_SYMBOLIC 16) |
|
(define DT_REL 17) |
|
(define DT_RELSZ 18) |
|
(define DT_RELENT 19) |
|
(define DT_PLTREL 20) |
|
(define DT_DEBUG 21) |
|
(define DT_TEXTREL 22) |
|
(define DT_JMPREL 23) |
|
(define DT_BIND_NOW 24) |
|
(define DT_INIT_ARRAY 25) |
|
(define DT_FINI_ARRAY 26) |
|
(define DT_INIT_ARRAYSZ 27) |
|
(define DT_FINI_ARRAYSZ 28) |
|
(define DT_RUNPATH 29) |
|
(define DT_FLAGS 30) |
|
(define DT_ENCODING 32) |
|
(define DT_PREINIT_ARRAY 32) |
|
(define DT_PREINIT_ARRAYSZ 33) |
|
(define DT_NUM 34) |
|
(define DT_LOGUILE #x37146000) |
|
(define DT_GUILE_GC_ROOT #x37146000) |
|
(define DT_GUILE_GC_ROOT_SZ #x37146001) |
|
(define DT_GUILE_ENTRY #x37146002) |
|
(define DT_GUILE_VM_VERSION #x37146003) |
|
(define DT_GUILE_FRAME_MAPS #x37146004) |
|
(define DT_HIGUILE #x37146fff) |
|
(define DT_LOOS #x6000000d) |
|
(define DT_HIOS #x6ffff000) |
|
(define DT_LOPROC #x70000000) |
|
(define DT_HIPROC #x7fffffff) |
|
|
|
|
|
(define (string-table-ref bv offset) |
|
(let lp ((end offset)) |
|
(if (zero? (bytevector-u8-ref bv end)) |
|
(let ((out (make-bytevector (- end offset)))) |
|
(bytevector-copy! bv offset out 0 (- end offset)) |
|
(utf8->string out)) |
|
(lp (1+ end))))) |
|
|
|
(define (elf-section-by-name elf name) |
|
(let ((off (elf-section-offset (elf-section elf (elf-shstrndx elf))))) |
|
(let lp ((n (elf-shnum elf))) |
|
(and (> n 0) |
|
(let ((section (elf-section elf (1- n)))) |
|
(if (equal? (string-table-ref (elf-bytes elf) |
|
(+ off (elf-section-name section))) |
|
name) |
|
section |
|
(lp (1- n)))))))) |
|
|
|
(define (elf-sections-by-name elf) |
|
(let* ((sections (elf-sections elf)) |
|
(off (elf-section-offset (list-ref sections (elf-shstrndx elf))))) |
|
(map (lambda (section) |
|
(cons (string-table-ref (elf-bytes elf) |
|
(+ off (elf-section-name section))) |
|
section)) |
|
sections))) |
|
|
|
(define-record-type <elf-symbol> |
|
(make-elf-symbol name value size info other shndx) |
|
elf-symbol? |
|
(name elf-symbol-name) |
|
(value elf-symbol-value) |
|
(size elf-symbol-size) |
|
(info elf-symbol-info) |
|
(other elf-symbol-other) |
|
(shndx elf-symbol-shndx)) |
|
|
|
(define* (make-elf-symbol* #:key (name 0) (value 0) (size 0) |
|
(binding STB_LOCAL) (type STT_NOTYPE) |
|
(info (logior (ash binding 4) type)) |
|
(visibility STV_DEFAULT) (other visibility) |
|
(shndx SHN_UNDEF)) |
|
(make-elf-symbol name value size info other shndx)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (elf-symbol-len word-size) |
|
(case word-size |
|
((4) 16) |
|
((8) 24) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (elf-symbol-value-offset word-size) |
|
(case word-size |
|
((4) 4) |
|
((8) 8) |
|
(else (error "bad word size" word-size)))) |
|
|
|
(define (parse-elf32-symbol bv offset stroff byte-order) |
|
(if (<= (+ offset 16) (bytevector-length bv)) |
|
(make-elf-symbol (let ((name (bytevector-u32-ref bv offset byte-order))) |
|
(if stroff |
|
(string-table-ref bv (+ stroff name)) |
|
name)) |
|
(bytevector-u32-ref bv (+ offset 4) byte-order) |
|
(bytevector-u32-ref bv (+ offset 8) byte-order) |
|
(bytevector-u8-ref bv (+ offset 12)) |
|
(bytevector-u8-ref bv (+ offset 13)) |
|
(bytevector-u16-ref bv (+ offset 14) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf32-symbol bv offset byte-order sym) |
|
(bytevector-u32-set! bv offset (elf-symbol-name sym) byte-order) |
|
(bytevector-u32-set! bv (+ offset 4) (elf-symbol-value sym) byte-order) |
|
(bytevector-u32-set! bv (+ offset 8) (elf-symbol-size sym) byte-order) |
|
(bytevector-u8-set! bv (+ offset 12) (elf-symbol-info sym)) |
|
(bytevector-u8-set! bv (+ offset 13) (elf-symbol-other sym)) |
|
(bytevector-u16-set! bv (+ offset 14) (elf-symbol-shndx sym) byte-order)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (parse-elf64-symbol bv offset stroff byte-order) |
|
(if (<= (+ offset 24) (bytevector-length bv)) |
|
(make-elf-symbol (let ((name (bytevector-u32-ref bv offset byte-order))) |
|
(if stroff |
|
(string-table-ref bv (+ stroff name)) |
|
name)) |
|
(bytevector-u64-ref bv (+ offset 8) byte-order) |
|
(bytevector-u64-ref bv (+ offset 16) byte-order) |
|
(bytevector-u8-ref bv (+ offset 4)) |
|
(bytevector-u8-ref bv (+ offset 5)) |
|
(bytevector-u16-ref bv (+ offset 6) byte-order)) |
|
(error "corrupt ELF (offset out of range)" offset))) |
|
|
|
(define (write-elf64-symbol bv offset byte-order sym) |
|
(bytevector-u32-set! bv offset (elf-symbol-name sym) byte-order) |
|
(bytevector-u8-set! bv (+ offset 4) (elf-symbol-info sym)) |
|
(bytevector-u8-set! bv (+ offset 5) (elf-symbol-other sym)) |
|
(bytevector-u16-set! bv (+ offset 6) (elf-symbol-shndx sym) byte-order) |
|
(bytevector-u64-set! bv (+ offset 8) (elf-symbol-value sym) byte-order) |
|
(bytevector-u64-set! bv (+ offset 16) (elf-symbol-size sym) byte-order)) |
|
|
|
(define (write-elf-symbol bv offset byte-order word-size sym) |
|
((case word-size |
|
((4) write-elf32-symbol) |
|
((8) write-elf64-symbol) |
|
(else (error "invalid word size" word-size))) |
|
bv offset byte-order sym)) |
|
|
|
(define (elf-symbol-table-len section) |
|
(let ((len (elf-section-size section)) |
|
(entsize (elf-section-entsize section))) |
|
(unless (and (not (zero? entsize)) (zero? (modulo len entsize))) |
|
(error "bad symbol table" section)) |
|
(/ len entsize))) |
|
|
|
(define* (elf-symbol-table-ref elf section n #:optional strtab) |
|
(let ((bv (elf-bytes elf)) |
|
(byte-order (elf-byte-order elf)) |
|
(stroff (and strtab (elf-section-offset strtab))) |
|
(base (elf-section-offset section)) |
|
(len (elf-section-size section)) |
|
(entsize (elf-section-entsize section))) |
|
(unless (<= (* (1+ n) entsize) len) |
|
(error "out of range symbol table access" section n)) |
|
(case (elf-word-size elf) |
|
((4) |
|
(unless (<= 16 entsize) |
|
(error "bad entsize for symbol table" section)) |
|
(parse-elf32-symbol bv (+ base (* n entsize)) stroff byte-order)) |
|
((8) |
|
(unless (<= 24 entsize) |
|
(error "bad entsize for symbol table" section)) |
|
(parse-elf64-symbol bv (+ base (* n entsize)) stroff byte-order)) |
|
(else (error "bad word size" elf))))) |
|
|
|
|
|
|
|
(define STB_LOCAL 0) |
|
(define STB_GLOBAL 1) |
|
(define STB_WEAK 2) |
|
(define STB_NUM 3) |
|
(define STB_LOOS 10) |
|
(define STB_GNU_UNIQUE 10) |
|
(define STB_HIOS 12) |
|
(define STB_LOPROC 13) |
|
(define STB_HIPROC 15) |
|
|
|
|
|
|
|
(define STT_NOTYPE 0) |
|
(define STT_OBJECT 1) |
|
(define STT_FUNC 2) |
|
(define STT_SECTION 3) |
|
(define STT_FILE 4) |
|
(define STT_COMMON 5) |
|
(define STT_TLS 6) |
|
(define STT_NUM 7) |
|
(define STT_LOOS 10) |
|
(define STT_GNU_IFUNC 10) |
|
(define STT_HIOS 12) |
|
(define STT_LOPROC 13) |
|
(define STT_HIPROC 15) |
|
|
|
|
|
|
|
(define STV_DEFAULT 0) |
|
(define STV_INTERNAL 1) |
|
(define STV_HIDDEN 2) |
|
(define STV_PROTECTED 3) |
|
|
|
(define (elf-symbol-binding sym) |
|
(ash (elf-symbol-info sym) -4)) |
|
|
|
(define (elf-symbol-type sym) |
|
(logand (elf-symbol-info sym) #xf)) |
|
|
|
(define (elf-symbol-visibility sym) |
|
(logand (elf-symbol-other sym) #x3)) |
|
|
|
(define NT_GNU_ABI_TAG 1) |
|
(define NT_GNU_HWCAP 2) |
|
(define NT_GNU_BUILD_ID 3) |
|
(define NT_GNU_GOLD_VERSION 4) |
|
|
|
(define-record-type <elf-note> |
|
(make-elf-note name desc type) |
|
elf-note? |
|
(name elf-note-name) |
|
(desc elf-note-desc) |
|
(type elf-note-type)) |
|
|
|
(define (parse-elf-note elf section) |
|
(let ((bv (elf-bytes elf)) |
|
(byte-order (elf-byte-order elf)) |
|
(offset (elf-section-offset section))) |
|
(unless (<= (+ offset 12) (bytevector-length bv)) |
|
(error "corrupt ELF (offset out of range)" offset)) |
|
(let ((namesz (bytevector-u32-ref bv offset byte-order)) |
|
(descsz (bytevector-u32-ref bv (+ offset 4) byte-order)) |
|
(type (bytevector-u32-ref bv (+ offset 8) byte-order))) |
|
(unless (<= (+ offset 12 namesz descsz) (bytevector-length bv)) |
|
(error "corrupt ELF (offset out of range)" offset)) |
|
(let ((name (make-bytevector (1- namesz))) |
|
(desc (make-bytevector descsz))) |
|
(bytevector-copy! bv (+ offset 12) name 0 (1- namesz)) |
|
(bytevector-copy! bv (+ offset 12 namesz) desc 0 descsz) |
|
(make-elf-note (utf8->string name) desc type))))) |
|
|