4 #define ELF_CLASS ELFCLASS32
5 #define ELF_DATA ELFDATA2MSB
6 #define ELF_ARCH EM_SPARC
11 static void bswap_ehdr(Elf32_Ehdr *ehdr)
13 bswap16s(&ehdr->e_type); /* Object file type */
14 bswap16s(&ehdr->e_machine); /* Architecture */
15 bswap32s(&ehdr->e_version); /* Object file version */
16 bswap32s(&ehdr->e_entry); /* Entry point virtual address */
17 bswap32s(&ehdr->e_phoff); /* Program header table file offset */
18 bswap32s(&ehdr->e_shoff); /* Section header table file offset */
19 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
20 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
21 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
22 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
23 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
24 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
25 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
28 static void bswap_phdr(Elf32_Phdr *phdr)
30 bswap32s(&phdr->p_type); /* Segment type */
31 bswap32s(&phdr->p_offset); /* Segment file offset */
32 bswap32s(&phdr->p_vaddr); /* Segment virtual address */
33 bswap32s(&phdr->p_paddr); /* Segment physical address */
34 bswap32s(&phdr->p_filesz); /* Segment size in file */
35 bswap32s(&phdr->p_memsz); /* Segment size in memory */
36 bswap32s(&phdr->p_flags); /* Segment flags */
37 bswap32s(&phdr->p_align); /* Segment alignment */
40 static void bswap_shdr(Elf32_Shdr *shdr)
42 bswap32s(&shdr->sh_name);
43 bswap32s(&shdr->sh_type);
44 bswap32s(&shdr->sh_flags);
45 bswap32s(&shdr->sh_addr);
46 bswap32s(&shdr->sh_offset);
47 bswap32s(&shdr->sh_size);
48 bswap32s(&shdr->sh_link);
49 bswap32s(&shdr->sh_info);
50 bswap32s(&shdr->sh_addralign);
51 bswap32s(&shdr->sh_entsize);
54 static void bswap_sym(Elf32_Sym *sym)
56 bswap32s(&sym->st_name);
57 bswap32s(&sym->st_value);
58 bswap32s(&sym->st_size);
59 bswap16s(&sym->st_shndx);
62 #define bswap_ehdr(e) do { } while (0)
63 #define bswap_phdr(e) do { } while (0)
64 #define bswap_shdr(e) do { } while (0)
65 #define bswap_sym(e) do { } while (0)
68 static int find_phdr(struct elfhdr *ehdr, int fd, struct elf_phdr *phdr, uint32_t type)
72 retval = lseek(fd, ehdr->e_phoff, SEEK_SET);
76 for (i = 0; i < ehdr->e_phnum; i++) {
77 retval = read(fd, phdr, sizeof(*phdr));
81 if (phdr->p_type == type)
87 static void *find_shdr(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, uint32_t type)
91 retval = lseek(fd, ehdr->e_shoff, SEEK_SET);
95 for (i = 0; i < ehdr->e_shnum; i++) {
96 retval = read(fd, shdr, sizeof(*shdr));
100 if (shdr->sh_type == type)
101 return qemu_malloc(shdr->sh_size);
106 static int find_strtab(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, struct elf_shdr *symtab)
110 retval = lseek(fd, ehdr->e_shoff + sizeof(struct elf_shdr) * symtab->sh_link, SEEK_SET);
114 retval = read(fd, shdr, sizeof(*shdr));
118 if (shdr->sh_type == SHT_STRTAB)
119 return qemu_malloc(shdr->sh_size);;
123 static int read_program(int fd, struct elf_phdr *phdr, void *dst)
126 retval = lseek(fd, 0x4000, SEEK_SET);
129 return read(fd, dst, phdr->p_filesz);
132 static int read_section(int fd, struct elf_shdr *s, void *dst)
136 retval = lseek(fd, s->sh_offset, SEEK_SET);
139 retval = read(fd, dst, s->sh_size);
145 static void *process_section(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, uint32_t type)
149 dst = find_shdr(ehdr, fd, shdr, type);
153 if (read_section(fd, shdr, dst))
161 static void *process_strtab(struct elfhdr *ehdr, int fd, struct elf_shdr *shdr, struct elf_shdr *symtab)
165 dst = find_strtab(ehdr, fd, shdr, symtab);
169 if (read_section(fd, shdr, dst))
177 static void load_symbols(struct elfhdr *ehdr, int fd)
179 struct elf_shdr symtab, strtab;
180 struct elf_sym *syms;
185 syms = process_section(ehdr, fd, &symtab, SHT_SYMTAB);
189 nsyms = symtab.sh_size / sizeof(struct elf_sym);
190 for (i = 0; i < nsyms; i++)
194 str = process_strtab(ehdr, fd, &strtab, &symtab);
200 qemu_free(disas_symtab); /* XXX Merge with old symbols? */
202 qemu_free(disas_strtab);
204 disas_num_syms = nsyms;
212 int load_elf(const char * filename, uint8_t *addr)
215 struct elf_phdr phdr;
218 fd = open(filename, O_RDONLY | O_BINARY);
222 retval = read(fd, &ehdr, sizeof(ehdr));
228 if (ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E'
229 || ehdr.e_ident[2] != 'L' || ehdr.e_ident[3] != 'F'
230 || ehdr.e_machine != EM_SPARC)
233 if (find_phdr(&ehdr, fd, &phdr, PT_LOAD))
235 retval = read_program(fd, &phdr, addr);
239 load_symbols(&ehdr, fd);
248 int load_kernel(const char *filename, uint8_t *addr)
252 fd = open(filename, O_RDONLY | O_BINARY);
255 /* load 32 bit code */
256 size = read(fd, addr, 16 * 1024 * 1024);
266 typedef struct MAGICState {
270 char saved_kfn[1024];
273 static uint32_t magic_mem_readl(void *opaque, target_phys_addr_t addr)
276 MAGICState *s = opaque;
278 if (s->magic_state == 0) {
279 ret = load_elf(s->saved_kfn, (uint8_t *)s->saved_addr);
281 ret = load_kernel(s->saved_kfn, (uint8_t *)s->saved_addr);
283 fprintf(stderr, "qemu: could not load kernel '%s'\n",
286 s->magic_state = 1; /* No more magic */
293 static void magic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
298 static CPUReadMemoryFunc *magic_mem_read[3] = {
304 static CPUWriteMemoryFunc *magic_mem_write[3] = {
310 void magic_init(const char *kfn, int kloadaddr, uint32_t addr)
315 s = qemu_mallocz(sizeof(MAGICState));
319 strcpy(s->saved_kfn, kfn);
320 s->saved_addr = kloadaddr;
323 magic_io_memory = cpu_register_io_memory(0, magic_mem_read, magic_mem_write, s);
324 cpu_register_physical_memory(addr, 4, magic_io_memory);