linker.test 3.3 KB

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