root / loader.c @ d796321b
History | View | Annotate | Download (5.9 kB)
1 | 5fe141fd | bellard | /*
|
---|---|---|---|
2 | 5fe141fd | bellard | * QEMU Executable loader
|
3 | 5fe141fd | bellard | *
|
4 | 5fe141fd | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 5fe141fd | bellard | *
|
6 | 5fe141fd | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 5fe141fd | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 5fe141fd | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 5fe141fd | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 5fe141fd | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 5fe141fd | bellard | * furnished to do so, subject to the following conditions:
|
12 | 5fe141fd | bellard | *
|
13 | 5fe141fd | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 5fe141fd | bellard | * all copies or substantial portions of the Software.
|
15 | 5fe141fd | bellard | *
|
16 | 5fe141fd | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 5fe141fd | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 5fe141fd | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 5fe141fd | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 5fe141fd | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 5fe141fd | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 5fe141fd | bellard | * THE SOFTWARE.
|
23 | 5fe141fd | bellard | */
|
24 | 5fe141fd | bellard | #include "vl.h" |
25 | 5fe141fd | bellard | #include "disas.h" |
26 | 5fe141fd | bellard | |
27 | 5fe141fd | bellard | /* return the size or -1 if error */
|
28 | 5fe141fd | bellard | int get_image_size(const char *filename) |
29 | 5fe141fd | bellard | { |
30 | 5fe141fd | bellard | int fd, size;
|
31 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
32 | 5fe141fd | bellard | if (fd < 0) |
33 | 5fe141fd | bellard | return -1; |
34 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
35 | 5fe141fd | bellard | close(fd); |
36 | 5fe141fd | bellard | return size;
|
37 | 5fe141fd | bellard | } |
38 | 5fe141fd | bellard | |
39 | 5fe141fd | bellard | /* return the size or -1 if error */
|
40 | 5fe141fd | bellard | int load_image(const char *filename, uint8_t *addr) |
41 | 5fe141fd | bellard | { |
42 | 5fe141fd | bellard | int fd, size;
|
43 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
44 | 5fe141fd | bellard | if (fd < 0) |
45 | 5fe141fd | bellard | return -1; |
46 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
47 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
48 | 5fe141fd | bellard | if (read(fd, addr, size) != size) {
|
49 | 5fe141fd | bellard | close(fd); |
50 | 5fe141fd | bellard | return -1; |
51 | 5fe141fd | bellard | } |
52 | 5fe141fd | bellard | close(fd); |
53 | 5fe141fd | bellard | return size;
|
54 | 5fe141fd | bellard | } |
55 | 5fe141fd | bellard | |
56 | 5fe141fd | bellard | /* A.OUT loader */
|
57 | 5fe141fd | bellard | |
58 | 5fe141fd | bellard | struct exec
|
59 | 5fe141fd | bellard | { |
60 | 5fe141fd | bellard | uint32_t a_info; /* Use macros N_MAGIC, etc for access */
|
61 | 5fe141fd | bellard | uint32_t a_text; /* length of text, in bytes */
|
62 | 5fe141fd | bellard | uint32_t a_data; /* length of data, in bytes */
|
63 | 5fe141fd | bellard | uint32_t a_bss; /* length of uninitialized data area, in bytes */
|
64 | 5fe141fd | bellard | uint32_t a_syms; /* length of symbol table data in file, in bytes */
|
65 | 5fe141fd | bellard | uint32_t a_entry; /* start address */
|
66 | 5fe141fd | bellard | uint32_t a_trsize; /* length of relocation info for text, in bytes */
|
67 | 5fe141fd | bellard | uint32_t a_drsize; /* length of relocation info for data, in bytes */
|
68 | 5fe141fd | bellard | }; |
69 | 5fe141fd | bellard | |
70 | 5fe141fd | bellard | #ifdef BSWAP_NEEDED
|
71 | 5fe141fd | bellard | static void bswap_ahdr(struct exec *e) |
72 | 5fe141fd | bellard | { |
73 | 5fe141fd | bellard | bswap32s(&e->a_info); |
74 | 5fe141fd | bellard | bswap32s(&e->a_text); |
75 | 5fe141fd | bellard | bswap32s(&e->a_data); |
76 | 5fe141fd | bellard | bswap32s(&e->a_bss); |
77 | 5fe141fd | bellard | bswap32s(&e->a_syms); |
78 | 5fe141fd | bellard | bswap32s(&e->a_entry); |
79 | 5fe141fd | bellard | bswap32s(&e->a_trsize); |
80 | 5fe141fd | bellard | bswap32s(&e->a_drsize); |
81 | 5fe141fd | bellard | } |
82 | 5fe141fd | bellard | #else
|
83 | 5fe141fd | bellard | #define bswap_ahdr(x) do { } while (0) |
84 | 5fe141fd | bellard | #endif
|
85 | 5fe141fd | bellard | |
86 | 5fe141fd | bellard | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
87 | 5fe141fd | bellard | #define OMAGIC 0407 |
88 | 5fe141fd | bellard | #define NMAGIC 0410 |
89 | 5fe141fd | bellard | #define ZMAGIC 0413 |
90 | 5fe141fd | bellard | #define QMAGIC 0314 |
91 | 5fe141fd | bellard | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
92 | 5fe141fd | bellard | #define N_TXTOFF(x) \
|
93 | 5fe141fd | bellard | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
94 | 5fe141fd | bellard | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
95 | 5fe141fd | bellard | #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0) |
96 | 5fe141fd | bellard | #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
|
97 | 5fe141fd | bellard | #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1)) |
98 | 5fe141fd | bellard | |
99 | 5fe141fd | bellard | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
|
100 | 5fe141fd | bellard | |
101 | 5fe141fd | bellard | #define N_DATADDR(x) \
|
102 | 5fe141fd | bellard | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \ |
103 | 5fe141fd | bellard | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x)))) |
104 | 5fe141fd | bellard | |
105 | 5fe141fd | bellard | |
106 | 5fe141fd | bellard | int load_aout(const char *filename, uint8_t *addr) |
107 | 5fe141fd | bellard | { |
108 | 5fe141fd | bellard | int fd, size, ret;
|
109 | 5fe141fd | bellard | struct exec e;
|
110 | 5fe141fd | bellard | uint32_t magic; |
111 | 5fe141fd | bellard | |
112 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
113 | 5fe141fd | bellard | if (fd < 0) |
114 | 5fe141fd | bellard | return -1; |
115 | 5fe141fd | bellard | |
116 | 5fe141fd | bellard | size = read(fd, &e, sizeof(e));
|
117 | 5fe141fd | bellard | if (size < 0) |
118 | 5fe141fd | bellard | goto fail;
|
119 | 5fe141fd | bellard | |
120 | 5fe141fd | bellard | bswap_ahdr(&e); |
121 | 5fe141fd | bellard | |
122 | 5fe141fd | bellard | magic = N_MAGIC(e); |
123 | 5fe141fd | bellard | switch (magic) {
|
124 | 5fe141fd | bellard | case ZMAGIC:
|
125 | 5fe141fd | bellard | case QMAGIC:
|
126 | 5fe141fd | bellard | case OMAGIC:
|
127 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
128 | 5fe141fd | bellard | size = read(fd, addr, e.a_text + e.a_data); |
129 | 5fe141fd | bellard | if (size < 0) |
130 | 5fe141fd | bellard | goto fail;
|
131 | 5fe141fd | bellard | break;
|
132 | 5fe141fd | bellard | case NMAGIC:
|
133 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
134 | 5fe141fd | bellard | size = read(fd, addr, e.a_text); |
135 | 5fe141fd | bellard | if (size < 0) |
136 | 5fe141fd | bellard | goto fail;
|
137 | 5fe141fd | bellard | ret = read(fd, addr + N_DATADDR(e), e.a_data); |
138 | 5fe141fd | bellard | if (ret < 0) |
139 | 5fe141fd | bellard | goto fail;
|
140 | 5fe141fd | bellard | size += ret; |
141 | 5fe141fd | bellard | break;
|
142 | 5fe141fd | bellard | default:
|
143 | 5fe141fd | bellard | goto fail;
|
144 | 5fe141fd | bellard | } |
145 | 5fe141fd | bellard | close(fd); |
146 | 5fe141fd | bellard | return size;
|
147 | 5fe141fd | bellard | fail:
|
148 | 5fe141fd | bellard | close(fd); |
149 | 5fe141fd | bellard | return -1; |
150 | 5fe141fd | bellard | } |
151 | 5fe141fd | bellard | |
152 | 5fe141fd | bellard | /* ELF loader */
|
153 | 5fe141fd | bellard | |
154 | 5fe141fd | bellard | static void *load_at(int fd, int offset, int size) |
155 | 5fe141fd | bellard | { |
156 | 5fe141fd | bellard | void *ptr;
|
157 | 5fe141fd | bellard | if (lseek(fd, offset, SEEK_SET) < 0) |
158 | 5fe141fd | bellard | return NULL; |
159 | 5fe141fd | bellard | ptr = qemu_malloc(size); |
160 | 5fe141fd | bellard | if (!ptr)
|
161 | 5fe141fd | bellard | return NULL; |
162 | 5fe141fd | bellard | if (read(fd, ptr, size) != size) {
|
163 | 5fe141fd | bellard | qemu_free(ptr); |
164 | 5fe141fd | bellard | return NULL; |
165 | 5fe141fd | bellard | } |
166 | 5fe141fd | bellard | return ptr;
|
167 | 5fe141fd | bellard | } |
168 | 5fe141fd | bellard | |
169 | 5fe141fd | bellard | |
170 | 5fe141fd | bellard | #define ELF_CLASS ELFCLASS32
|
171 | 5fe141fd | bellard | #include "elf.h" |
172 | 5fe141fd | bellard | |
173 | 5fe141fd | bellard | #define SZ 32 |
174 | 5fe141fd | bellard | #define elf_word uint32_t
|
175 | 5fe141fd | bellard | #define bswapSZs bswap32s
|
176 | 5fe141fd | bellard | #include "elf_ops.h" |
177 | 5fe141fd | bellard | |
178 | 5fe141fd | bellard | #undef elfhdr
|
179 | 5fe141fd | bellard | #undef elf_phdr
|
180 | 5fe141fd | bellard | #undef elf_shdr
|
181 | 5fe141fd | bellard | #undef elf_sym
|
182 | 5fe141fd | bellard | #undef elf_note
|
183 | 5fe141fd | bellard | #undef elf_word
|
184 | 5fe141fd | bellard | #undef bswapSZs
|
185 | 5fe141fd | bellard | #undef SZ
|
186 | 5fe141fd | bellard | #define elfhdr elf64_hdr
|
187 | 5fe141fd | bellard | #define elf_phdr elf64_phdr
|
188 | 5fe141fd | bellard | #define elf_note elf64_note
|
189 | 5fe141fd | bellard | #define elf_shdr elf64_shdr
|
190 | 5fe141fd | bellard | #define elf_sym elf64_sym
|
191 | 5fe141fd | bellard | #define elf_word uint64_t
|
192 | 5fe141fd | bellard | #define bswapSZs bswap64s
|
193 | 5fe141fd | bellard | #define SZ 64 |
194 | 5fe141fd | bellard | #include "elf_ops.h" |
195 | 5fe141fd | bellard | |
196 | 5fe141fd | bellard | /* return < 0 if error, otherwise the number of bytes loaded in memory */
|
197 | 9ee3c029 | bellard | int load_elf(const char *filename, int64_t virt_to_phys_addend, |
198 | 9ee3c029 | bellard | uint64_t *pentry) |
199 | 5fe141fd | bellard | { |
200 | 5fe141fd | bellard | int fd, data_order, must_swab, ret;
|
201 | 5fe141fd | bellard | uint8_t e_ident[EI_NIDENT]; |
202 | 5fe141fd | bellard | |
203 | 699e4642 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
204 | 5fe141fd | bellard | if (fd < 0) { |
205 | 5fe141fd | bellard | perror(filename); |
206 | 5fe141fd | bellard | return -1; |
207 | 5fe141fd | bellard | } |
208 | 5fe141fd | bellard | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
209 | 5fe141fd | bellard | goto fail;
|
210 | 5fe141fd | bellard | if (e_ident[0] != ELFMAG0 || |
211 | 5fe141fd | bellard | e_ident[1] != ELFMAG1 ||
|
212 | 5fe141fd | bellard | e_ident[2] != ELFMAG2 ||
|
213 | 5fe141fd | bellard | e_ident[3] != ELFMAG3)
|
214 | 5fe141fd | bellard | goto fail;
|
215 | 5fe141fd | bellard | #ifdef WORDS_BIGENDIAN
|
216 | 5fe141fd | bellard | data_order = ELFDATA2MSB; |
217 | 5fe141fd | bellard | #else
|
218 | 5fe141fd | bellard | data_order = ELFDATA2LSB; |
219 | 5fe141fd | bellard | #endif
|
220 | 5fe141fd | bellard | must_swab = data_order != e_ident[EI_DATA]; |
221 | 5fe141fd | bellard | |
222 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
223 | 5fe141fd | bellard | if (e_ident[EI_CLASS] == ELFCLASS64) {
|
224 | 9ee3c029 | bellard | ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry); |
225 | 5fe141fd | bellard | } else {
|
226 | 9ee3c029 | bellard | ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry); |
227 | 5fe141fd | bellard | } |
228 | 5fe141fd | bellard | |
229 | 5fe141fd | bellard | close(fd); |
230 | 5fe141fd | bellard | return ret;
|
231 | 5fe141fd | bellard | |
232 | 5fe141fd | bellard | fail:
|
233 | 5fe141fd | bellard | close(fd); |
234 | 5fe141fd | bellard | return -1; |
235 | 5fe141fd | bellard | } |