sdcard: Don't always set the high capacity bit
[qemu/ar7.git] / include / hw / elf_ops.h
blobd192e7e2a383f007eb5c2434788e654193adb296
1 static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
3 bswap16s(&ehdr->e_type); /* Object file type */
4 bswap16s(&ehdr->e_machine); /* Architecture */
5 bswap32s(&ehdr->e_version); /* Object file version */
6 bswapSZs(&ehdr->e_entry); /* Entry point virtual address */
7 bswapSZs(&ehdr->e_phoff); /* Program header table file offset */
8 bswapSZs(&ehdr->e_shoff); /* Section header table file offset */
9 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
10 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
11 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
12 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
13 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
14 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
15 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
18 static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
20 bswap32s(&phdr->p_type); /* Segment type */
21 bswapSZs(&phdr->p_offset); /* Segment file offset */
22 bswapSZs(&phdr->p_vaddr); /* Segment virtual address */
23 bswapSZs(&phdr->p_paddr); /* Segment physical address */
24 bswapSZs(&phdr->p_filesz); /* Segment size in file */
25 bswapSZs(&phdr->p_memsz); /* Segment size in memory */
26 bswap32s(&phdr->p_flags); /* Segment flags */
27 bswapSZs(&phdr->p_align); /* Segment alignment */
30 static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
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);
44 static void glue(bswap_sym, SZ)(struct elf_sym *sym)
46 bswap32s(&sym->st_name);
47 bswapSZs(&sym->st_value);
48 bswapSZs(&sym->st_size);
49 bswap16s(&sym->st_shndx);
52 static void glue(bswap_rela, SZ)(struct elf_rela *rela)
54 bswapSZs(&rela->r_offset);
55 bswapSZs(&rela->r_info);
56 bswapSZs((elf_word *)&rela->r_addend);
59 static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
60 int n, int type)
62 int i;
63 for(i=0;i<n;i++) {
64 if (shdr_table[i].sh_type == type)
65 return shdr_table + i;
67 return NULL;
70 static int glue(symfind, SZ)(const void *s0, const void *s1)
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;
80 return result;
83 static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
84 hwaddr orig_addr)
86 struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
87 struct elf_sym *sym;
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;
95 return "";
98 static int glue(symcmp, SZ)(const void *s0, const void *s1)
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);
107 static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
108 int clear_lsb)
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;
116 shdr_table = load_at(fd, ehdr->e_shoff,
117 sizeof(struct elf_shdr) * ehdr->e_shnum);
118 if (!shdr_table)
119 return -1;
121 if (must_swab) {
122 for (i = 0; i < ehdr->e_shnum; i++) {
123 glue(bswap_shdr, SZ)(shdr_table + i);
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;
134 nsyms = symtab->sh_size / sizeof(struct elf_sym);
136 i = 0;
137 while (i < nsyms) {
138 if (must_swab)
139 glue(bswap_sym, SZ)(&syms[i]);
140 /* We are only interested in function symbols.
141 Throw everything else away. */
142 if (syms[i].st_shndx == SHN_UNDEF ||
143 syms[i].st_shndx >= SHN_LORESERVE ||
144 ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
145 nsyms--;
146 if (i < nsyms) {
147 syms[i] = syms[nsyms];
149 continue;
151 if (clear_lsb) {
152 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
153 syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
155 i++;
157 syms = g_realloc(syms, nsyms * sizeof(*syms));
159 qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
160 for (i = 0; i < nsyms - 1; i++) {
161 if (syms[i].st_size == 0) {
162 syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
166 /* String table */
167 if (symtab->sh_link >= ehdr->e_shnum)
168 goto fail;
169 strtab = &shdr_table[symtab->sh_link];
171 str = load_at(fd, strtab->sh_offset, strtab->sh_size);
172 if (!str)
173 goto fail;
175 /* Commit */
176 s = g_malloc0(sizeof(*s));
177 s->lookup_symbol = glue(lookup_symbol, SZ);
178 glue(s->disas_symtab.elf, SZ) = syms;
179 s->disas_num_syms = nsyms;
180 s->disas_strtab = str;
181 s->next = syminfos;
182 syminfos = s;
183 g_free(shdr_table);
184 return 0;
185 fail:
186 g_free(syms);
187 g_free(str);
188 g_free(shdr_table);
189 return -1;
192 static int glue(elf_reloc, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
193 uint64_t (*translate_fn)(void *, uint64_t),
194 void *translate_opaque, uint8_t *data,
195 struct elf_phdr *ph, int elf_machine)
197 struct elf_shdr *reltab, *shdr_table = NULL;
198 struct elf_rela *rels = NULL;
199 int nrels, i, ret = -1;
200 elf_word wordval;
201 void *addr;
203 shdr_table = load_at(fd, ehdr->e_shoff,
204 sizeof(struct elf_shdr) * ehdr->e_shnum);
205 if (!shdr_table) {
206 return -1;
208 if (must_swab) {
209 for (i = 0; i < ehdr->e_shnum; i++) {
210 glue(bswap_shdr, SZ)(&shdr_table[i]);
214 reltab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_RELA);
215 if (!reltab) {
216 goto fail;
218 rels = load_at(fd, reltab->sh_offset, reltab->sh_size);
219 if (!rels) {
220 goto fail;
222 nrels = reltab->sh_size / sizeof(struct elf_rela);
224 for (i = 0; i < nrels; i++) {
225 if (must_swab) {
226 glue(bswap_rela, SZ)(&rels[i]);
228 if (rels[i].r_offset < ph->p_vaddr ||
229 rels[i].r_offset >= ph->p_vaddr + ph->p_filesz) {
230 continue;
232 addr = &data[rels[i].r_offset - ph->p_vaddr];
233 switch (elf_machine) {
234 case EM_S390:
235 switch (rels[i].r_info) {
236 case R_390_RELATIVE:
237 wordval = *(elf_word *)addr;
238 if (must_swab) {
239 bswapSZs(&wordval);
241 wordval = translate_fn(translate_opaque, wordval);
242 if (must_swab) {
243 bswapSZs(&wordval);
245 *(elf_word *)addr = wordval;
246 break;
247 default:
248 fprintf(stderr, "Unsupported relocation type %i!\n",
249 (int)rels[i].r_info);
254 ret = 0;
255 fail:
256 g_free(rels);
257 g_free(shdr_table);
258 return ret;
261 static int glue(load_elf, SZ)(const char *name, int fd,
262 uint64_t (*translate_fn)(void *, uint64_t),
263 void *translate_opaque,
264 int must_swab, uint64_t *pentry,
265 uint64_t *lowaddr, uint64_t *highaddr,
266 int elf_machine, int clear_lsb, int data_swab,
267 AddressSpace *as, bool load_rom)
269 struct elfhdr ehdr;
270 struct elf_phdr *phdr = NULL, *ph;
271 int size, i, total_size;
272 elf_word mem_size, file_size;
273 uint64_t addr, low = (uint64_t)-1, high = 0;
274 uint8_t *data = NULL;
275 char label[128];
276 int ret = ELF_LOAD_FAILED;
278 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
279 goto fail;
280 if (must_swab) {
281 glue(bswap_ehdr, SZ)(&ehdr);
284 if (elf_machine <= EM_NONE) {
285 /* The caller didn't specify an ARCH, we can figure it out */
286 elf_machine = ehdr.e_machine;
289 switch (elf_machine) {
290 case EM_PPC64:
291 if (ehdr.e_machine != EM_PPC64) {
292 if (ehdr.e_machine != EM_PPC) {
293 ret = ELF_LOAD_WRONG_ARCH;
294 goto fail;
297 break;
298 case EM_X86_64:
299 if (ehdr.e_machine != EM_X86_64) {
300 if (ehdr.e_machine != EM_386) {
301 ret = ELF_LOAD_WRONG_ARCH;
302 goto fail;
305 break;
306 case EM_MICROBLAZE:
307 if (ehdr.e_machine != EM_MICROBLAZE) {
308 if (ehdr.e_machine != EM_MICROBLAZE_OLD) {
309 ret = ELF_LOAD_WRONG_ARCH;
310 goto fail;
313 break;
314 case EM_MOXIE:
315 if (ehdr.e_machine != EM_MOXIE) {
316 if (ehdr.e_machine != EM_MOXIE_OLD) {
317 ret = ELF_LOAD_WRONG_ARCH;
318 goto fail;
321 break;
322 default:
323 if (elf_machine != ehdr.e_machine) {
324 ret = ELF_LOAD_WRONG_ARCH;
325 goto fail;
329 if (pentry)
330 *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
332 glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
334 size = ehdr.e_phnum * sizeof(phdr[0]);
335 if (lseek(fd, ehdr.e_phoff, SEEK_SET) != ehdr.e_phoff) {
336 goto fail;
338 phdr = g_malloc0(size);
339 if (!phdr)
340 goto fail;
341 if (read(fd, phdr, size) != size)
342 goto fail;
343 if (must_swab) {
344 for(i = 0; i < ehdr.e_phnum; i++) {
345 ph = &phdr[i];
346 glue(bswap_phdr, SZ)(ph);
350 total_size = 0;
351 for(i = 0; i < ehdr.e_phnum; i++) {
352 ph = &phdr[i];
353 if (ph->p_type == PT_LOAD) {
354 mem_size = ph->p_memsz; /* Size of the ROM */
355 file_size = ph->p_filesz; /* Size of the allocated data */
356 data = g_malloc0(file_size);
357 if (ph->p_filesz > 0) {
358 if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
359 goto fail;
361 if (read(fd, data, file_size) != file_size) {
362 goto fail;
366 /* The ELF spec is somewhat vague about the purpose of the
367 * physical address field. One common use in the embedded world
368 * is that physical address field specifies the load address
369 * and the virtual address field specifies the execution address.
370 * Segments are packed into ROM or flash, and the relocation
371 * and zero-initialization of data is done at runtime. This
372 * means that the memsz header represents the runtime size of the
373 * segment, but the filesz represents the loadtime size. If
374 * we try to honour the memsz value for an ELF file like this
375 * we will end up with overlapping segments (which the
376 * loader.c code will later reject).
377 * We support ELF files using this scheme by by checking whether
378 * paddr + memsz for this segment would overlap with any other
379 * segment. If so, then we assume it's using this scheme and
380 * truncate the loaded segment to the filesz size.
381 * If the segment considered as being memsz size doesn't overlap
382 * then we use memsz for the segment length, to handle ELF files
383 * which assume that the loader will do the zero-initialization.
385 if (mem_size > file_size) {
386 /* If this segment's zero-init portion overlaps another
387 * segment's data or zero-init portion, then truncate this one.
388 * Invalid ELF files where the segments overlap even when
389 * only file_size bytes are loaded will be rejected by
390 * the ROM overlap check in loader.c, so we don't try to
391 * explicitly detect those here.
393 int j;
394 elf_word zero_start = ph->p_paddr + file_size;
395 elf_word zero_end = ph->p_paddr + mem_size;
397 for (j = 0; j < ehdr.e_phnum; j++) {
398 struct elf_phdr *jph = &phdr[j];
400 if (i != j && jph->p_type == PT_LOAD) {
401 elf_word other_start = jph->p_paddr;
402 elf_word other_end = jph->p_paddr + jph->p_memsz;
404 if (!(other_start >= zero_end ||
405 zero_start >= other_end)) {
406 mem_size = file_size;
407 break;
413 /* address_offset is hack for kernel images that are
414 linked at the wrong physical address. */
415 if (translate_fn) {
416 addr = translate_fn(translate_opaque, ph->p_paddr);
417 glue(elf_reloc, SZ)(&ehdr, fd, must_swab, translate_fn,
418 translate_opaque, data, ph, elf_machine);
419 } else {
420 addr = ph->p_paddr;
423 if (data_swab) {
424 int j;
425 for (j = 0; j < file_size; j += (1 << data_swab)) {
426 uint8_t *dp = data + j;
427 switch (data_swab) {
428 case (1):
429 *(uint16_t *)dp = bswap16(*(uint16_t *)dp);
430 break;
431 case (2):
432 *(uint32_t *)dp = bswap32(*(uint32_t *)dp);
433 break;
434 case (3):
435 *(uint64_t *)dp = bswap64(*(uint64_t *)dp);
436 break;
437 default:
438 g_assert_not_reached();
443 /* the entry pointer in the ELF header is a virtual
444 * address, if the text segments paddr and vaddr differ
445 * we need to adjust the entry */
446 if (pentry && !translate_fn &&
447 ph->p_vaddr != ph->p_paddr &&
448 ehdr.e_entry >= ph->p_vaddr &&
449 ehdr.e_entry < ph->p_vaddr + ph->p_filesz &&
450 ph->p_flags & PF_X) {
451 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
454 if (mem_size == 0) {
455 /* Some ELF files really do have segments of zero size;
456 * just ignore them rather than trying to create empty
457 * ROM blobs, because the zero-length blob can falsely
458 * trigger the overlapping-ROM-blobs check.
460 g_free(data);
461 } else {
462 if (load_rom) {
463 snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
465 /* rom_add_elf_program() seize the ownership of 'data' */
466 rom_add_elf_program(label, data, file_size, mem_size,
467 addr, as);
468 } else {
469 cpu_physical_memory_write(addr, data, file_size);
470 g_free(data);
474 total_size += mem_size;
475 if (addr < low)
476 low = addr;
477 if ((addr + mem_size) > high)
478 high = addr + mem_size;
480 data = NULL;
483 g_free(phdr);
484 if (lowaddr)
485 *lowaddr = (uint64_t)(elf_sword)low;
486 if (highaddr)
487 *highaddr = (uint64_t)(elf_sword)high;
488 return total_size;
489 fail:
490 g_free(data);
491 g_free(phdr);
492 return ret;