linker.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;;; linker.test -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2013, 2019 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. (define-module (test-suite test-linker)
  19. #:use-module (test-suite lib)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (system base target)
  22. #:use-module (system vm elf)
  23. #:use-module (system vm linker))
  24. (define (link-elf-with-one-main-section name bytes)
  25. (let ((strtab (make-string-table)))
  26. (define (make-object index name bv relocs . kwargs)
  27. (let ((name-idx (string-table-intern! strtab (symbol->string name))))
  28. (make-linker-object (symbol->string name)
  29. (apply make-elf-section
  30. #:index index
  31. #:name name-idx
  32. #:size (bytevector-length bv)
  33. kwargs)
  34. bv relocs
  35. (list (make-linker-symbol name 0)))))
  36. (define (make-shstrtab)
  37. (string-table-intern! strtab ".shstrtab")
  38. (make-object 2 '.shstrtab (link-string-table! strtab) '()
  39. #:type SHT_STRTAB #:flags 0))
  40. (let* ((word-size (target-word-size))
  41. (endianness (target-endianness))
  42. (sec (make-object 1 name bytes '()))
  43. ;; This needs to be linked last, because linking other
  44. ;; sections adds entries to the string table.
  45. (shstrtab (make-shstrtab)))
  46. (link-elf (list sec shstrtab)
  47. #:endianness endianness #:word-size word-size))))
  48. (with-test-prefix "simple"
  49. (define foo-bytes #vu8(0 1 2 3 4 5))
  50. (define bytes #f)
  51. (define elf #f)
  52. (define (bytevectors-equal? bv-a bv-b start-a start-b size)
  53. (or (zero? size)
  54. (and (equal? (bytevector-u8-ref bv-a start-a)
  55. (bytevector-u8-ref bv-b start-b))
  56. (bytevectors-equal? bv-a bv-b (1+ start-a) (1+ start-b)
  57. (1- size)))))
  58. (pass-if "linking succeeds"
  59. (begin
  60. (set! bytes (link-elf-with-one-main-section '.foo foo-bytes))
  61. #t))
  62. (pass-if "parsing succeeds"
  63. (begin
  64. (set! elf (parse-elf bytes))
  65. (elf? elf)))
  66. ;; 5 sections: the initial NULL section, .foo, .shstrtab, the initial
  67. ;; header with segment table, and the section table.
  68. (pass-if-equal 5 (elf-shnum elf))
  69. (pass-if ".foo section checks out"
  70. (let ((sec (assoc-ref (elf-sections-by-name elf) ".foo")))
  71. (and sec
  72. (= (elf-section-size sec) (bytevector-length foo-bytes))
  73. (bytevectors-equal? bytes foo-bytes
  74. (elf-section-offset sec) 0
  75. (bytevector-length foo-bytes))))))