elf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tools/testing/selftests/kvm/lib/elf.c
  4. *
  5. * Copyright (C) 2018, Google LLC.
  6. */
  7. #include "test_util.h"
  8. #include <bits/endian.h>
  9. #include <linux/elf.h>
  10. #include "kvm_util.h"
  11. static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp)
  12. {
  13. off_t offset_rv;
  14. /* Open the ELF file. */
  15. int fd;
  16. fd = open(filename, O_RDONLY);
  17. TEST_ASSERT(fd >= 0, "Failed to open ELF file,\n"
  18. " filename: %s\n"
  19. " rv: %i errno: %i", filename, fd, errno);
  20. /* Read in and validate ELF Identification Record.
  21. * The ELF Identification record is the first 16 (EI_NIDENT) bytes
  22. * of the ELF header, which is at the beginning of the ELF file.
  23. * For now it is only safe to read the first EI_NIDENT bytes. Once
  24. * read and validated, the value of e_ehsize can be used to determine
  25. * the real size of the ELF header.
  26. */
  27. unsigned char ident[EI_NIDENT];
  28. test_read(fd, ident, sizeof(ident));
  29. TEST_ASSERT((ident[EI_MAG0] == ELFMAG0) && (ident[EI_MAG1] == ELFMAG1)
  30. && (ident[EI_MAG2] == ELFMAG2) && (ident[EI_MAG3] == ELFMAG3),
  31. "ELF MAGIC Mismatch,\n"
  32. " filename: %s\n"
  33. " ident[EI_MAG0 - EI_MAG3]: %02x %02x %02x %02x\n"
  34. " Expected: %02x %02x %02x %02x",
  35. filename,
  36. ident[EI_MAG0], ident[EI_MAG1], ident[EI_MAG2], ident[EI_MAG3],
  37. ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3);
  38. TEST_ASSERT(ident[EI_CLASS] == ELFCLASS64,
  39. "Current implementation only able to handle ELFCLASS64,\n"
  40. " filename: %s\n"
  41. " ident[EI_CLASS]: %02x\n"
  42. " expected: %02x",
  43. filename,
  44. ident[EI_CLASS], ELFCLASS64);
  45. TEST_ASSERT(((BYTE_ORDER == LITTLE_ENDIAN)
  46. && (ident[EI_DATA] == ELFDATA2LSB))
  47. || ((BYTE_ORDER == BIG_ENDIAN)
  48. && (ident[EI_DATA] == ELFDATA2MSB)), "Current "
  49. "implementation only able to handle\n"
  50. "cases where the host and ELF file endianness\n"
  51. "is the same:\n"
  52. " host BYTE_ORDER: %u\n"
  53. " host LITTLE_ENDIAN: %u\n"
  54. " host BIG_ENDIAN: %u\n"
  55. " ident[EI_DATA]: %u\n"
  56. " ELFDATA2LSB: %u\n"
  57. " ELFDATA2MSB: %u",
  58. BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN,
  59. ident[EI_DATA], ELFDATA2LSB, ELFDATA2MSB);
  60. TEST_ASSERT(ident[EI_VERSION] == EV_CURRENT,
  61. "Current implementation only able to handle current "
  62. "ELF version,\n"
  63. " filename: %s\n"
  64. " ident[EI_VERSION]: %02x\n"
  65. " expected: %02x",
  66. filename, ident[EI_VERSION], EV_CURRENT);
  67. /* Read in the ELF header.
  68. * With the ELF Identification portion of the ELF header
  69. * validated, especially that the value at EI_VERSION is
  70. * as expected, it is now safe to read the entire ELF header.
  71. */
  72. offset_rv = lseek(fd, 0, SEEK_SET);
  73. TEST_ASSERT(offset_rv == 0, "Seek to ELF header failed,\n"
  74. " rv: %zi expected: %i", offset_rv, 0);
  75. test_read(fd, hdrp, sizeof(*hdrp));
  76. TEST_ASSERT(hdrp->e_phentsize == sizeof(Elf64_Phdr),
  77. "Unexpected physical header size,\n"
  78. " hdrp->e_phentsize: %x\n"
  79. " expected: %zx",
  80. hdrp->e_phentsize, sizeof(Elf64_Phdr));
  81. TEST_ASSERT(hdrp->e_shentsize == sizeof(Elf64_Shdr),
  82. "Unexpected section header size,\n"
  83. " hdrp->e_shentsize: %x\n"
  84. " expected: %zx",
  85. hdrp->e_shentsize, sizeof(Elf64_Shdr));
  86. }
  87. /* VM ELF Load
  88. *
  89. * Input Args:
  90. * filename - Path to ELF file
  91. *
  92. * Output Args: None
  93. *
  94. * Input/Output Args:
  95. * vm - Pointer to opaque type that describes the VM.
  96. *
  97. * Return: None, TEST_ASSERT failures for all error conditions
  98. *
  99. * Loads the program image of the ELF file specified by filename,
  100. * into the virtual address space of the VM pointed to by vm. On entry
  101. * the VM needs to not be using any of the virtual address space used
  102. * by the image and it needs to have sufficient available physical pages, to
  103. * back the virtual pages used to load the image.
  104. */
  105. void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
  106. {
  107. off_t offset, offset_rv;
  108. Elf64_Ehdr hdr;
  109. /* Open the ELF file. */
  110. int fd;
  111. fd = open(filename, O_RDONLY);
  112. TEST_ASSERT(fd >= 0, "Failed to open ELF file,\n"
  113. " filename: %s\n"
  114. " rv: %i errno: %i", filename, fd, errno);
  115. /* Read in the ELF header. */
  116. elfhdr_get(filename, &hdr);
  117. /* For each program header.
  118. * The following ELF header members specify the location
  119. * and size of the program headers:
  120. *
  121. * e_phoff - File offset to start of program headers
  122. * e_phentsize - Size of each program header
  123. * e_phnum - Number of program header entries
  124. */
  125. for (unsigned int n1 = 0; n1 < hdr.e_phnum; n1++) {
  126. /* Seek to the beginning of the program header. */
  127. offset = hdr.e_phoff + (n1 * hdr.e_phentsize);
  128. offset_rv = lseek(fd, offset, SEEK_SET);
  129. TEST_ASSERT(offset_rv == offset,
  130. "Failed to seek to begining of program header %u,\n"
  131. " filename: %s\n"
  132. " rv: %jd errno: %i",
  133. n1, filename, (intmax_t) offset_rv, errno);
  134. /* Read in the program header. */
  135. Elf64_Phdr phdr;
  136. test_read(fd, &phdr, sizeof(phdr));
  137. /* Skip if this header doesn't describe a loadable segment. */
  138. if (phdr.p_type != PT_LOAD)
  139. continue;
  140. /* Allocate memory for this segment within the VM. */
  141. TEST_ASSERT(phdr.p_memsz > 0, "Unexpected loadable segment "
  142. "memsize of 0,\n"
  143. " phdr index: %u p_memsz: 0x%" PRIx64,
  144. n1, (uint64_t) phdr.p_memsz);
  145. vm_vaddr_t seg_vstart = align_down(phdr.p_vaddr, vm->page_size);
  146. vm_vaddr_t seg_vend = phdr.p_vaddr + phdr.p_memsz - 1;
  147. seg_vend |= vm->page_size - 1;
  148. size_t seg_size = seg_vend - seg_vstart + 1;
  149. vm_vaddr_t vaddr = vm_vaddr_alloc(vm, seg_size, seg_vstart);
  150. TEST_ASSERT(vaddr == seg_vstart, "Unable to allocate "
  151. "virtual memory for segment at requested min addr,\n"
  152. " segment idx: %u\n"
  153. " seg_vstart: 0x%lx\n"
  154. " vaddr: 0x%lx",
  155. n1, seg_vstart, vaddr);
  156. memset(addr_gva2hva(vm, vaddr), 0, seg_size);
  157. /* TODO(lhuemill): Set permissions of each memory segment
  158. * based on the least-significant 3 bits of phdr.p_flags.
  159. */
  160. /* Load portion of initial state that is contained within
  161. * the ELF file.
  162. */
  163. if (phdr.p_filesz) {
  164. offset_rv = lseek(fd, phdr.p_offset, SEEK_SET);
  165. TEST_ASSERT(offset_rv == phdr.p_offset,
  166. "Seek to program segment offset failed,\n"
  167. " program header idx: %u errno: %i\n"
  168. " offset_rv: 0x%jx\n"
  169. " expected: 0x%jx\n",
  170. n1, errno, (intmax_t) offset_rv,
  171. (intmax_t) phdr.p_offset);
  172. test_read(fd, addr_gva2hva(vm, phdr.p_vaddr),
  173. phdr.p_filesz);
  174. }
  175. }
  176. }