| 1 |
;;;; linker.test -*- scheme -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright 2013, 2019, 2023 Free Software Foundation, Inc. |
| 4 |
;;;; |
| 5 |
;;;; This library is free software; you can redistribute it and/or |
| 6 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 7 |
;;;; License as published by the Free Software Foundation; either |
| 8 |
;;;; version 3 of the License, or (at your option) any later version. |
| 9 |
;;;; |
| 10 |
;;;; This library is distributed in the hope that it will be useful, |
| 11 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
;;;; Lesser General Public License for more details. |
| 14 |
;;;; |
| 15 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 16 |
;;;; License along with this library; if not, write to the Free Software |
| 17 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
(define-module (test-suite test-linker) |
| 20 |
#:use-module (test-suite lib) |
| 21 |
#:use-module (rnrs bytevectors) |
| 22 |
#:use-module (system base target) |
| 23 |
#:use-module (system vm elf) |
| 24 |
#:use-module (system vm linker)) |
| 25 |
|
| 26 |
(define (link-elf-with-one-main-section name bytes) |
| 27 |
(let ((strtab (make-string-table))) |
| 28 |
(define (make-object index name size writer relocs . kwargs) |
| 29 |
(let ((name-idx (string-table-intern! strtab (symbol->string name)))) |
| 30 |
(make-linker-object (symbol->string name) |
| 31 |
(apply make-elf-section |
| 32 |
#:index index |
| 33 |
#:name name-idx |
| 34 |
#:size size |
| 35 |
kwargs) |
| 36 |
size writer relocs |
| 37 |
(list (make-linker-symbol name 0))))) |
| 38 |
(define (make-shstrtab) |
| 39 |
(string-table-intern! strtab ".shstrtab") |
| 40 |
(make-object 2 '.shstrtab |
| 41 |
(string-table-size strtab) |
| 42 |
(string-table-writer strtab) |
| 43 |
'() |
| 44 |
#:type SHT_STRTAB #:flags 0)) |
| 45 |
(let* ((word-size (target-word-size)) |
| 46 |
(endianness (target-endianness)) |
| 47 |
(sec (make-object 1 name |
| 48 |
(bytevector-length bytes) |
| 49 |
(lambda (bv) |
| 50 |
(bytevector-copy! bytes 0 bv 0 |
| 51 |
(bytevector-length |
| 52 |
bytes))) |
| 53 |
'())) |
| 54 |
;; This needs to be linked last, because linking other |
| 55 |
;; sections adds entries to the string table. |
| 56 |
(shstrtab (make-shstrtab))) |
| 57 |
(link-elf (list sec shstrtab) |
| 58 |
#:page-aligned? #f ;return a bytevector |
| 59 |
#:endianness endianness #:word-size word-size)))) |
| 60 |
|
| 61 |
(with-test-prefix "simple" |
| 62 |
(define foo-bytes #vu8(0 1 2 3 4 5)) |
| 63 |
(define bytes #f) |
| 64 |
(define elf #f) |
| 65 |
|
| 66 |
(define (bytevectors-equal? bv-a bv-b start-a start-b size) |
| 67 |
(or (zero? size) |
| 68 |
(and (equal? (bytevector-u8-ref bv-a start-a) |
| 69 |
(bytevector-u8-ref bv-b start-b)) |
| 70 |
(bytevectors-equal? bv-a bv-b (1+ start-a) (1+ start-b) |
| 71 |
(1- size))))) |
| 72 |
|
| 73 |
(pass-if "linking succeeds" |
| 74 |
(begin |
| 75 |
(set! bytes (link-elf-with-one-main-section '.foo foo-bytes)) |
| 76 |
#t)) |
| 77 |
|
| 78 |
(pass-if "parsing succeeds" |
| 79 |
(begin |
| 80 |
(set! elf (parse-elf bytes)) |
| 81 |
(elf? elf))) |
| 82 |
|
| 83 |
;; 5 sections: the initial NULL section, .foo, .shstrtab, the initial |
| 84 |
;; header with segment table, and the section table. |
| 85 |
(pass-if-equal 5 (elf-shnum elf)) |
| 86 |
|
| 87 |
(pass-if ".foo section checks out" |
| 88 |
(let ((sec (assoc-ref (elf-sections-by-name elf) ".foo"))) |
| 89 |
(and sec |
| 90 |
(= (elf-section-size sec) (bytevector-length foo-bytes)) |
| 91 |
(bytevectors-equal? bytes foo-bytes |
| 92 |
(elf-section-offset sec) 0 |
| 93 |
(bytevector-length foo-bytes)))))) |