summaryrefslogtreecommitdiff
path: root/test-suite/tests/linker.test
blob: e7ecc291e04299ffb8ab3b7fe3d180b53a1fdf8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;;; linker.test                               -*- scheme -*-
;;;;
;;;; Copyright 2013, 2019 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(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 bv 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 (bytevector-length bv)
                                   kwargs)
                            bv relocs
                            (list (make-linker-symbol name 0)))))
    (define (make-shstrtab)
      (string-table-intern! strtab ".shstrtab")
      (make-object 2 '.shstrtab (link-string-table! strtab) '()
                   #:type SHT_STRTAB #:flags 0))
    (let* ((word-size (target-word-size))
           (endianness (target-endianness))
           (sec (make-object 1 name bytes '()))
           ;; This needs to be linked last, because linking other
           ;; sections adds entries to the string table.
           (shstrtab (make-shstrtab)))
      (link-elf (list sec shstrtab)
                #: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)))

  ;; 5 sections: the initial NULL section, .foo, .shstrtab, the initial
  ;; header with segment table, and the section table.
  (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))))))