|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite test-linker) |
|
#:use-module (test-suite lib) |
|
#:use-module (rnrs bytevectors) |
|
#:use-module (system base target) |
|
#:use-module (system vm elf) |
|
#:use-module (system vm linker)) |
|
|
|
(define (link-elf-with-one-main-section name bytes) |
|
(let ((strtab (make-string-table))) |
|
(define (make-object index name size writer relocs . kwargs) |
|
(let ((name-idx (string-table-intern! strtab (symbol->string name)))) |
|
(make-linker-object (symbol->string name) |
|
(apply make-elf-section |
|
#:index index |
|
#:name name-idx |
|
#:size size |
|
kwargs) |
|
size writer relocs |
|
(list (make-linker-symbol name 0))))) |
|
(define (make-shstrtab) |
|
(string-table-intern! strtab ".shstrtab") |
|
(make-object 2 '.shstrtab |
|
(string-table-size strtab) |
|
(string-table-writer strtab) |
|
'() |
|
#:type SHT_STRTAB #:flags 0)) |
|
(let* ((word-size (target-word-size)) |
|
(endianness (target-endianness)) |
|
(sec (make-object 1 name |
|
(bytevector-length bytes) |
|
(lambda (bv) |
|
(bytevector-copy! bytes 0 bv 0 |
|
(bytevector-length |
|
bytes))) |
|
'())) |
|
|
|
|
|
(shstrtab (make-shstrtab))) |
|
(link-elf (list sec shstrtab) |
|
#:page-aligned? #f |
|
#:endianness endianness #:word-size word-size)))) |
|
|
|
(with-test-prefix "simple" |
|
(define foo-bytes #vu8(0 1 2 3 4 5)) |
|
(define bytes #f) |
|
(define elf #f) |
|
|
|
(define (bytevectors-equal? bv-a bv-b start-a start-b size) |
|
(or (zero? size) |
|
(and (equal? (bytevector-u8-ref bv-a start-a) |
|
(bytevector-u8-ref bv-b start-b)) |
|
(bytevectors-equal? bv-a bv-b (1+ start-a) (1+ start-b) |
|
(1- size))))) |
|
|
|
(pass-if "linking succeeds" |
|
(begin |
|
(set! bytes (link-elf-with-one-main-section '.foo foo-bytes)) |
|
#t)) |
|
|
|
(pass-if "parsing succeeds" |
|
(begin |
|
(set! elf (parse-elf bytes)) |
|
(elf? elf))) |
|
|
|
|
|
|
|
(pass-if-equal 5 (elf-shnum elf)) |
|
|
|
(pass-if ".foo section checks out" |
|
(let ((sec (assoc-ref (elf-sections-by-name elf) ".foo"))) |
|
(and sec |
|
(= (elf-section-size sec) (bytevector-length foo-bytes)) |
|
(bytevectors-equal? bytes foo-bytes |
|
(elf-section-offset sec) 0 |
|
(bytevector-length foo-bytes)))))) |
|
|