1
2
3
4
5
6
7
8#include "test_util.h"
9
10#include <bits/endian.h>
11#include <linux/elf.h>
12
13#include "kvm_util.h"
14#include "kvm_util_internal.h"
15
16static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp)
17{
18 off_t offset_rv;
19
20
21 int fd;
22 fd = open(filename, O_RDONLY);
23 TEST_ASSERT(fd >= 0, "Failed to open ELF file,\n"
24 " filename: %s\n"
25 " rv: %i errno: %i", filename, fd, errno);
26
27
28
29
30
31
32
33
34 unsigned char ident[EI_NIDENT];
35 test_read(fd, ident, sizeof(ident));
36 TEST_ASSERT((ident[EI_MAG0] == ELFMAG0) && (ident[EI_MAG1] == ELFMAG1)
37 && (ident[EI_MAG2] == ELFMAG2) && (ident[EI_MAG3] == ELFMAG3),
38 "ELF MAGIC Mismatch,\n"
39 " filename: %s\n"
40 " ident[EI_MAG0 - EI_MAG3]: %02x %02x %02x %02x\n"
41 " Expected: %02x %02x %02x %02x",
42 filename,
43 ident[EI_MAG0], ident[EI_MAG1], ident[EI_MAG2], ident[EI_MAG3],
44 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3);
45 TEST_ASSERT(ident[EI_CLASS] == ELFCLASS64,
46 "Current implementation only able to handle ELFCLASS64,\n"
47 " filename: %s\n"
48 " ident[EI_CLASS]: %02x\n"
49 " expected: %02x",
50 filename,
51 ident[EI_CLASS], ELFCLASS64);
52 TEST_ASSERT(((BYTE_ORDER == LITTLE_ENDIAN)
53 && (ident[EI_DATA] == ELFDATA2LSB))
54 || ((BYTE_ORDER == BIG_ENDIAN)
55 && (ident[EI_DATA] == ELFDATA2MSB)), "Current "
56 "implementation only able to handle\n"
57 "cases where the host and ELF file endianness\n"
58 "is the same:\n"
59 " host BYTE_ORDER: %u\n"
60 " host LITTLE_ENDIAN: %u\n"
61 " host BIG_ENDIAN: %u\n"
62 " ident[EI_DATA]: %u\n"
63 " ELFDATA2LSB: %u\n"
64 " ELFDATA2MSB: %u",
65 BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN,
66 ident[EI_DATA], ELFDATA2LSB, ELFDATA2MSB);
67 TEST_ASSERT(ident[EI_VERSION] == EV_CURRENT,
68 "Current implementation only able to handle current "
69 "ELF version,\n"
70 " filename: %s\n"
71 " ident[EI_VERSION]: %02x\n"
72 " expected: %02x",
73 filename, ident[EI_VERSION], EV_CURRENT);
74
75
76
77
78
79
80 offset_rv = lseek(fd, 0, SEEK_SET);
81 TEST_ASSERT(offset_rv == 0, "Seek to ELF header failed,\n"
82 " rv: %zi expected: %i", offset_rv, 0);
83 test_read(fd, hdrp, sizeof(*hdrp));
84 TEST_ASSERT(hdrp->e_phentsize == sizeof(Elf64_Phdr),
85 "Unexpected physical header size,\n"
86 " hdrp->e_phentsize: %x\n"
87 " expected: %zx",
88 hdrp->e_phentsize, sizeof(Elf64_Phdr));
89 TEST_ASSERT(hdrp->e_shentsize == sizeof(Elf64_Shdr),
90 "Unexpected section header size,\n"
91 " hdrp->e_shentsize: %x\n"
92 " expected: %zx",
93 hdrp->e_shentsize, sizeof(Elf64_Shdr));
94}
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
115{
116 off_t offset, offset_rv;
117 Elf64_Ehdr hdr;
118
119
120 int fd;
121 fd = open(filename, O_RDONLY);
122 TEST_ASSERT(fd >= 0, "Failed to open ELF file,\n"
123 " filename: %s\n"
124 " rv: %i errno: %i", filename, fd, errno);
125
126
127 elfhdr_get(filename, &hdr);
128
129
130
131
132
133
134
135
136
137 for (unsigned int n1 = 0; n1 < hdr.e_phnum; n1++) {
138
139 offset = hdr.e_phoff + (n1 * hdr.e_phentsize);
140 offset_rv = lseek(fd, offset, SEEK_SET);
141 TEST_ASSERT(offset_rv == offset,
142 "Failed to seek to begining of program header %u,\n"
143 " filename: %s\n"
144 " rv: %jd errno: %i",
145 n1, filename, (intmax_t) offset_rv, errno);
146
147
148 Elf64_Phdr phdr;
149 test_read(fd, &phdr, sizeof(phdr));
150
151
152 if (phdr.p_type != PT_LOAD)
153 continue;
154
155
156 TEST_ASSERT(phdr.p_memsz > 0, "Unexpected loadable segment "
157 "memsize of 0,\n"
158 " phdr index: %u p_memsz: 0x%" PRIx64,
159 n1, (uint64_t) phdr.p_memsz);
160 vm_vaddr_t seg_vstart = align_down(phdr.p_vaddr, vm->page_size);
161 vm_vaddr_t seg_vend = phdr.p_vaddr + phdr.p_memsz - 1;
162 seg_vend |= vm->page_size - 1;
163 size_t seg_size = seg_vend - seg_vstart + 1;
164
165 vm_vaddr_t vaddr = vm_vaddr_alloc(vm, seg_size, seg_vstart);
166 TEST_ASSERT(vaddr == seg_vstart, "Unable to allocate "
167 "virtual memory for segment at requested min addr,\n"
168 " segment idx: %u\n"
169 " seg_vstart: 0x%lx\n"
170 " vaddr: 0x%lx",
171 n1, seg_vstart, vaddr);
172 memset(addr_gva2hva(vm, vaddr), 0, seg_size);
173
174
175
176
177
178
179
180 if (phdr.p_filesz) {
181 offset_rv = lseek(fd, phdr.p_offset, SEEK_SET);
182 TEST_ASSERT(offset_rv == phdr.p_offset,
183 "Seek to program segment offset failed,\n"
184 " program header idx: %u errno: %i\n"
185 " offset_rv: 0x%jx\n"
186 " expected: 0x%jx\n",
187 n1, errno, (intmax_t) offset_rv,
188 (intmax_t) phdr.p_offset);
189 test_read(fd, addr_gva2hva(vm, phdr.p_vaddr),
190 phdr.p_filesz);
191 }
192 }
193}
194