1static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
2{
3 bswap16s(&ehdr->e_type);
4 bswap16s(&ehdr->e_machine);
5 bswap32s(&ehdr->e_version);
6 bswapSZs(&ehdr->e_entry);
7 bswapSZs(&ehdr->e_phoff);
8 bswapSZs(&ehdr->e_shoff);
9 bswap32s(&ehdr->e_flags);
10 bswap16s(&ehdr->e_ehsize);
11 bswap16s(&ehdr->e_phentsize);
12 bswap16s(&ehdr->e_phnum);
13 bswap16s(&ehdr->e_shentsize);
14 bswap16s(&ehdr->e_shnum);
15 bswap16s(&ehdr->e_shstrndx);
16}
17
18static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
19{
20 bswap32s(&phdr->p_type);
21 bswapSZs(&phdr->p_offset);
22 bswapSZs(&phdr->p_vaddr);
23 bswapSZs(&phdr->p_paddr);
24 bswapSZs(&phdr->p_filesz);
25 bswapSZs(&phdr->p_memsz);
26 bswap32s(&phdr->p_flags);
27 bswapSZs(&phdr->p_align);
28}
29
30static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
31{
32 bswap32s(&shdr->sh_name);
33 bswap32s(&shdr->sh_type);
34 bswapSZs(&shdr->sh_flags);
35 bswapSZs(&shdr->sh_addr);
36 bswapSZs(&shdr->sh_offset);
37 bswapSZs(&shdr->sh_size);
38 bswap32s(&shdr->sh_link);
39 bswap32s(&shdr->sh_info);
40 bswapSZs(&shdr->sh_addralign);
41 bswapSZs(&shdr->sh_entsize);
42}
43
44static void glue(bswap_sym, SZ)(struct elf_sym *sym)
45{
46 bswap32s(&sym->st_name);
47 bswapSZs(&sym->st_value);
48 bswapSZs(&sym->st_size);
49 bswap16s(&sym->st_shndx);
50}
51
52static void glue(bswap_rela, SZ)(struct elf_rela *rela)
53{
54 bswapSZs(&rela->r_offset);
55 bswapSZs(&rela->r_info);
56 bswapSZs((elf_word *)&rela->r_addend);
57}
58
59static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
60 int n, int type)
61{
62 int i;
63 for(i=0;i<n;i++) {
64 if (shdr_table[i].sh_type == type)
65 return shdr_table + i;
66 }
67 return NULL;
68}
69
70static int glue(symfind, SZ)(const void *s0, const void *s1)
71{
72 hwaddr addr = *(hwaddr *)s0;
73 struct elf_sym *sym = (struct elf_sym *)s1;
74 int result = 0;
75 if (addr < sym->st_value) {
76 result = -1;
77 } else if (addr >= sym->st_value + sym->st_size) {
78 result = 1;
79 }
80 return result;
81}
82
83static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
84 hwaddr orig_addr)
85{
86 struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
87 struct elf_sym *sym;
88
89 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms),
90 glue(symfind, SZ));
91 if (sym != NULL) {
92 return s->disas_strtab + sym->st_name;
93 }
94
95 return "";
96}
97
98static int glue(symcmp, SZ)(const void *s0, const void *s1)
99{
100 struct elf_sym *sym0 = (struct elf_sym *)s0;
101 struct elf_sym *sym1 = (struct elf_sym *)s1;
102 return (sym0->st_value < sym1->st_value)
103 ? -1
104 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
105}
106
107static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
108 int clear_lsb, symbol_fn_t sym_cb)
109{
110 struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
111 struct elf_sym *syms = NULL;
112 struct syminfo *s;
113 int nsyms, i;
114 char *str = NULL;
115
116 shdr_table = load_at(fd, ehdr->e_shoff,
117 sizeof(struct elf_shdr) * ehdr->e_shnum);
118 if (!shdr_table)
119 return -1;
120
121 if (must_swab) {
122 for (i = 0; i < ehdr->e_shnum; i++) {
123 glue(bswap_shdr, SZ)(shdr_table + i);
124 }
125 }
126
127 symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB);
128 if (!symtab)
129 goto fail;
130 syms = load_at(fd, symtab->sh_offset, symtab->sh_size);
131 if (!syms)
132 goto fail;
133
134 nsyms = symtab->sh_size / sizeof(struct elf_sym);
135
136
137 if (symtab->sh_link >= ehdr->e_shnum) {
138 goto fail;
139 }
140 strtab = &shdr_table[symtab->sh_link];
141
142 str = load_at(fd, strtab->sh_offset, strtab->sh_size);
143 if (!str) {
144 goto fail;
145 }
146
147 i = 0;
148 while (i < nsyms) {
149 if (must_swab) {
150 glue(bswap_sym, SZ)(&syms[i]);
151 }
152 if (sym_cb) {
153 sym_cb(str + syms[i].st_name, syms[i].st_info,
154 syms[i].st_value, syms[i].st_size);
155 }
156
157
158 if (syms[i].st_shndx == SHN_UNDEF ||
159 syms[i].st_shndx >= SHN_LORESERVE ||
160 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
161 nsyms--;
162 if (i < nsyms) {
163 syms[i] = syms[nsyms];
164 }
165 continue;
166 }
167 if (clear_lsb) {
168
169 syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
170 }
171 i++;
172 }
173 syms = g_realloc(syms, nsyms * sizeof(*syms));
174
175 qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
176 for (i = 0; i < nsyms - 1; i++) {
177 if (syms[i].st_size == 0) {
178 syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
179 }
180 }
181
182
183 s = g_malloc0(sizeof(*s));
184 s->lookup_symbol = glue(lookup_symbol, SZ);
185 glue(s->disas_symtab.elf, SZ) = syms;
186 s->disas_num_syms = nsyms;
187 s->disas_strtab = str;
188 s->next = syminfos;
189 syminfos = s;
190 g_free(shdr_table);
191 return 0;
192 fail:
193 g_free(syms);
194 g_free(str);
195 g_free(shdr_table);
196 return -1;
197}
198
199static int glue(elf_reloc, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
200 uint64_t (*translate_fn)(void *, uint64_t),
201 void *translate_opaque, uint8_t *data,
202 struct elf_phdr *ph, int elf_machine)
203{
204 struct elf_shdr *reltab, *shdr_table = NULL;
205 struct elf_rela *rels = NULL;
206 int nrels, i, ret = -1;
207 elf_word wordval;
208 void *addr;
209
210 shdr_table = load_at(fd, ehdr->e_shoff,
211 sizeof(struct elf_shdr) * ehdr->e_shnum);
212 if (!shdr_table) {
213 return -1;
214 }
215 if (must_swab) {
216 for (i = 0; i < ehdr->e_shnum; i++) {
217 glue(bswap_shdr, SZ)(&shdr_table[i]);
218 }
219 }
220
221 reltab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_RELA);
222 if (!reltab) {
223 goto fail;
224 }
225 rels = load_at(fd, reltab->sh_offset, reltab->sh_size);
226 if (!rels) {
227 goto fail;
228 }
229 nrels = reltab->sh_size / sizeof(struct elf_rela);
230
231 for (i = 0; i < nrels; i++) {
232 if (must_swab) {
233 glue(bswap_rela, SZ)(&rels[i]);
234 }
235 if (rels[i].r_offset < ph->p_vaddr ||
236 rels[i].r_offset >= ph->p_vaddr + ph->p_filesz) {
237 continue;
238 }
239 addr = &data[rels[i].r_offset - ph->p_vaddr];
240 switch (elf_machine) {
241 case EM_S390:
242 switch (rels[i].r_info) {
243 case R_390_RELATIVE:
244 wordval = *(elf_word *)addr;
245 if (must_swab) {
246 bswapSZs(&wordval);
247 }
248 wordval = translate_fn(translate_opaque, wordval);
249 if (must_swab) {
250 bswapSZs(&wordval);
251 }
252 *(elf_word *)addr = wordval;
253 break;
254 default:
255 fprintf(stderr, "Unsupported relocation type %i!\n",
256 (int)rels[i].r_info);
257 }
258 }
259 }
260
261 ret = 0;
262fail:
263 g_free(rels);
264 g_free(shdr_table);
265 return ret;
266}
267
268static int glue(load_elf, SZ)(const char *name, int fd,
269 uint64_t (*translate_fn)(void *, uint64_t),
270 void *translate_opaque,
271 int must_swab, uint64_t *pentry,
272 uint64_t *lowaddr, uint64_t *highaddr,
273 int elf_machine, int clear_lsb, int data_swab,
274 AddressSpace *as, bool load_rom,
275 symbol_fn_t sym_cb)
276{
277 struct elfhdr ehdr;
278 struct elf_phdr *phdr = NULL, *ph;
279 int size, i, total_size;
280 elf_word mem_size, file_size;
281 uint64_t addr, low = (uint64_t)-1, high = 0;
282 uint8_t *data = NULL;
283 char label[128];
284 int ret = ELF_LOAD_FAILED;
285
286 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
287 goto fail;
288 if (must_swab) {
289 glue(bswap_ehdr, SZ)(&ehdr);
290 }
291
292 if (elf_machine <= EM_NONE) {
293
294 elf_machine = ehdr.e_machine;
295 }
296
297 switch (elf_machine) {
298 case EM_PPC64:
299 if (ehdr.e_machine != EM_PPC64) {
300 if (ehdr.e_machine != EM_PPC) {
301 ret = ELF_LOAD_WRONG_ARCH;
302 goto fail;
303 }
304 }
305 break;
306 case EM_X86_64:
307 if (ehdr.e_machine != EM_X86_64) {
308 if (ehdr.e_machine != EM_386) {
309 ret = ELF_LOAD_WRONG_ARCH;
310 goto fail;
311 }
312 }
313 break;
314 case EM_MICROBLAZE:
315 if (ehdr.e_machine != EM_MICROBLAZE) {
316 if (ehdr.e_machine != EM_MICROBLAZE_OLD) {
317 ret = ELF_LOAD_WRONG_ARCH;
318 goto fail;
319 }
320 }
321 break;
322 case EM_MOXIE:
323 if (ehdr.e_machine != EM_MOXIE) {
324 if (ehdr.e_machine != EM_MOXIE_OLD) {
325 ret = ELF_LOAD_WRONG_ARCH;
326 goto fail;
327 }
328 }
329 break;
330 default:
331 if (elf_machine != ehdr.e_machine) {
332 ret = ELF_LOAD_WRONG_ARCH;
333 goto fail;
334 }
335 }
336
337 if (pentry)
338 *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
339
340 glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb, sym_cb);
341
342 size = ehdr.e_phnum * sizeof(phdr[0]);
343 if (lseek(fd, ehdr.e_phoff, SEEK_SET) != ehdr.e_phoff) {
344 goto fail;
345 }
346 phdr = g_malloc0(size);
347 if (!phdr)
348 goto fail;
349 if (read(fd, phdr, size) != size)
350 goto fail;
351 if (must_swab) {
352 for(i = 0; i < ehdr.e_phnum; i++) {
353 ph = &phdr[i];
354 glue(bswap_phdr, SZ)(ph);
355 }
356 }
357
358 total_size = 0;
359 for(i = 0; i < ehdr.e_phnum; i++) {
360 ph = &phdr[i];
361 if (ph->p_type == PT_LOAD) {
362 mem_size = ph->p_memsz;
363 file_size = ph->p_filesz;
364 data = g_malloc0(file_size);
365 if (ph->p_filesz > 0) {
366 if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
367 goto fail;
368 }
369 if (read(fd, data, file_size) != file_size) {
370 goto fail;
371 }
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 if (mem_size > file_size) {
394
395
396
397
398
399
400
401 int j;
402 elf_word zero_start = ph->p_paddr + file_size;
403 elf_word zero_end = ph->p_paddr + mem_size;
404
405 for (j = 0; j < ehdr.e_phnum; j++) {
406 struct elf_phdr *jph = &phdr[j];
407
408 if (i != j && jph->p_type == PT_LOAD) {
409 elf_word other_start = jph->p_paddr;
410 elf_word other_end = jph->p_paddr + jph->p_memsz;
411
412 if (!(other_start >= zero_end ||
413 zero_start >= other_end)) {
414 mem_size = file_size;
415 break;
416 }
417 }
418 }
419 }
420
421
422
423 if (translate_fn) {
424 addr = translate_fn(translate_opaque, ph->p_paddr);
425 glue(elf_reloc, SZ)(&ehdr, fd, must_swab, translate_fn,
426 translate_opaque, data, ph, elf_machine);
427 } else {
428 addr = ph->p_paddr;
429 }
430
431 if (data_swab) {
432 int j;
433 for (j = 0; j < file_size; j += (1 << data_swab)) {
434 uint8_t *dp = data + j;
435 switch (data_swab) {
436 case (1):
437 *(uint16_t *)dp = bswap16(*(uint16_t *)dp);
438 break;
439 case (2):
440 *(uint32_t *)dp = bswap32(*(uint32_t *)dp);
441 break;
442 case (3):
443 *(uint64_t *)dp = bswap64(*(uint64_t *)dp);
444 break;
445 default:
446 g_assert_not_reached();
447 }
448 }
449 }
450
451
452
453
454 if (pentry && !translate_fn &&
455 ph->p_vaddr != ph->p_paddr &&
456 ehdr.e_entry >= ph->p_vaddr &&
457 ehdr.e_entry < ph->p_vaddr + ph->p_filesz &&
458 ph->p_flags & PF_X) {
459 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
460 }
461
462 if (mem_size == 0) {
463
464
465
466
467
468 g_free(data);
469 } else {
470 if (load_rom) {
471 snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
472
473
474 rom_add_elf_program(label, data, file_size, mem_size,
475 addr, as);
476 } else {
477 cpu_physical_memory_write(addr, data, file_size);
478 g_free(data);
479 }
480 }
481
482 total_size += mem_size;
483 if (addr < low)
484 low = addr;
485 if ((addr + mem_size) > high)
486 high = addr + mem_size;
487
488 data = NULL;
489 }
490 }
491 g_free(phdr);
492 if (lowaddr)
493 *lowaddr = (uint64_t)(elf_sword)low;
494 if (highaddr)
495 *highaddr = (uint64_t)(elf_sword)high;
496 return total_size;
497 fail:
498 g_free(data);
499 g_free(phdr);
500 return ret;
501}
502