]>
Commit | Line | Data |
---|---|---|
5fe141fd FB |
1 | static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr) |
2 | { | |
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 */ | |
16 | } | |
17 | ||
18 | static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr) | |
19 | { | |
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 */ | |
28 | } | |
29 | ||
30 | static 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 | ||
44 | static 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 | ||
5fafdf24 | 52 | static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table, |
5fe141fd FB |
53 | int n, int type) |
54 | { | |
55 | int i; | |
56 | for(i=0;i<n;i++) { | |
57 | if (shdr_table[i].sh_type == type) | |
58 | return shdr_table + i; | |
59 | } | |
60 | return NULL; | |
61 | } | |
62 | ||
49918a75 PB |
63 | static int glue(symfind, SZ)(const void *s0, const void *s1) |
64 | { | |
65 | struct elf_sym *key = (struct elf_sym *)s0; | |
66 | struct elf_sym *sym = (struct elf_sym *)s1; | |
67 | int result = 0; | |
68 | if (key->st_value < sym->st_value) { | |
69 | result = -1; | |
ec822001 | 70 | } else if (key->st_value >= sym->st_value + sym->st_size) { |
49918a75 PB |
71 | result = 1; |
72 | } | |
73 | return result; | |
74 | } | |
75 | ||
ca20cf32 | 76 | static const char *glue(lookup_symbol, SZ)(struct syminfo *s, |
c227f099 | 77 | target_phys_addr_t orig_addr) |
49918a75 PB |
78 | { |
79 | struct elf_sym *syms = glue(s->disas_symtab.elf, SZ); | |
80 | struct elf_sym key; | |
81 | struct elf_sym *sym; | |
82 | ||
83 | key.st_value = orig_addr; | |
84 | ||
85 | sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), glue(symfind, SZ)); | |
660f11be | 86 | if (sym != NULL) { |
49918a75 PB |
87 | return s->disas_strtab + sym->st_name; |
88 | } | |
89 | ||
90 | return ""; | |
91 | } | |
92 | ||
93 | static int glue(symcmp, SZ)(const void *s0, const void *s1) | |
94 | { | |
95 | struct elf_sym *sym0 = (struct elf_sym *)s0; | |
96 | struct elf_sym *sym1 = (struct elf_sym *)s1; | |
97 | return (sym0->st_value < sym1->st_value) | |
98 | ? -1 | |
99 | : ((sym0->st_value > sym1->st_value) ? 1 : 0); | |
100 | } | |
101 | ||
ca20cf32 BS |
102 | static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab, |
103 | int clear_lsb) | |
5fe141fd FB |
104 | { |
105 | struct elf_shdr *symtab, *strtab, *shdr_table = NULL; | |
106 | struct elf_sym *syms = NULL; | |
5fe141fd FB |
107 | struct syminfo *s; |
108 | int nsyms, i; | |
109 | char *str = NULL; | |
110 | ||
5fafdf24 | 111 | shdr_table = load_at(fd, ehdr->e_shoff, |
5fe141fd FB |
112 | sizeof(struct elf_shdr) * ehdr->e_shnum); |
113 | if (!shdr_table) | |
114 | return -1; | |
3b46e624 | 115 | |
5fe141fd FB |
116 | if (must_swab) { |
117 | for (i = 0; i < ehdr->e_shnum; i++) { | |
118 | glue(bswap_shdr, SZ)(shdr_table + i); | |
119 | } | |
120 | } | |
3b46e624 | 121 | |
5fe141fd FB |
122 | symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB); |
123 | if (!symtab) | |
124 | goto fail; | |
125 | syms = load_at(fd, symtab->sh_offset, symtab->sh_size); | |
126 | if (!syms) | |
127 | goto fail; | |
128 | ||
129 | nsyms = symtab->sh_size / sizeof(struct elf_sym); | |
49918a75 PB |
130 | |
131 | i = 0; | |
132 | while (i < nsyms) { | |
5fe141fd FB |
133 | if (must_swab) |
134 | glue(bswap_sym, SZ)(&syms[i]); | |
49918a75 PB |
135 | /* We are only interested in function symbols. |
136 | Throw everything else away. */ | |
137 | if (syms[i].st_shndx == SHN_UNDEF || | |
138 | syms[i].st_shndx >= SHN_LORESERVE || | |
139 | ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { | |
140 | nsyms--; | |
141 | if (i < nsyms) { | |
142 | syms[i] = syms[nsyms]; | |
143 | } | |
144 | continue; | |
145 | } | |
ca20cf32 BS |
146 | if (clear_lsb) { |
147 | /* The bottom address bit marks a Thumb or MIPS16 symbol. */ | |
148 | syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1; | |
149 | } | |
49918a75 | 150 | i++; |
5fe141fd | 151 | } |
49918a75 PB |
152 | syms = qemu_realloc(syms, nsyms * sizeof(*syms)); |
153 | ||
154 | qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ)); | |
155 | ||
5fe141fd FB |
156 | /* String table */ |
157 | if (symtab->sh_link >= ehdr->e_shnum) | |
158 | goto fail; | |
159 | strtab = &shdr_table[symtab->sh_link]; | |
160 | ||
161 | str = load_at(fd, strtab->sh_offset, strtab->sh_size); | |
162 | if (!str) | |
49918a75 | 163 | goto fail; |
5fe141fd FB |
164 | |
165 | /* Commit */ | |
166 | s = qemu_mallocz(sizeof(*s)); | |
49918a75 PB |
167 | s->lookup_symbol = glue(lookup_symbol, SZ); |
168 | glue(s->disas_symtab.elf, SZ) = syms; | |
5fe141fd FB |
169 | s->disas_num_syms = nsyms; |
170 | s->disas_strtab = str; | |
171 | s->next = syminfos; | |
172 | syminfos = s; | |
173 | qemu_free(shdr_table); | |
174 | return 0; | |
175 | fail: | |
5fe141fd FB |
176 | qemu_free(syms); |
177 | qemu_free(str); | |
178 | qemu_free(shdr_table); | |
179 | return -1; | |
180 | } | |
181 | ||
83c1f87c | 182 | static int glue(load_elf, SZ)(int fd, int64_t address_offset, |
9596ebb7 | 183 | int must_swab, uint64_t *pentry, |
ca20cf32 BS |
184 | uint64_t *lowaddr, uint64_t *highaddr, |
185 | int elf_machine, int clear_lsb) | |
5fe141fd FB |
186 | { |
187 | struct elfhdr ehdr; | |
188 | struct elf_phdr *phdr = NULL, *ph; | |
189 | int size, i, total_size; | |
9437454a | 190 | elf_word mem_size; |
fd93a799 | 191 | uint64_t addr, low = (uint64_t)-1, high = 0; |
9ee3c029 | 192 | uint8_t *data = NULL; |
5fe141fd FB |
193 | |
194 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) | |
195 | goto fail; | |
196 | if (must_swab) { | |
197 | glue(bswap_ehdr, SZ)(&ehdr); | |
198 | } | |
199 | ||
ca20cf32 | 200 | switch (elf_machine) { |
7f70c937 BS |
201 | case EM_PPC64: |
202 | if (EM_PPC64 != ehdr.e_machine) | |
203 | if (EM_PPC != ehdr.e_machine) | |
204 | goto fail; | |
205 | break; | |
206 | case EM_X86_64: | |
207 | if (EM_X86_64 != ehdr.e_machine) | |
208 | if (EM_386 != ehdr.e_machine) | |
209 | goto fail; | |
210 | break; | |
211 | default: | |
ca20cf32 | 212 | if (elf_machine != ehdr.e_machine) |
7f70c937 BS |
213 | goto fail; |
214 | } | |
9042c0e2 | 215 | |
9ee3c029 | 216 | if (pentry) |
82790064 | 217 | *pentry = (uint64_t)(elf_sword)ehdr.e_entry; |
9ee3c029 | 218 | |
ca20cf32 | 219 | glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb); |
5fe141fd FB |
220 | |
221 | size = ehdr.e_phnum * sizeof(phdr[0]); | |
222 | lseek(fd, ehdr.e_phoff, SEEK_SET); | |
223 | phdr = qemu_mallocz(size); | |
224 | if (!phdr) | |
225 | goto fail; | |
226 | if (read(fd, phdr, size) != size) | |
04d4b0c3 | 227 | goto fail; |
5fe141fd FB |
228 | if (must_swab) { |
229 | for(i = 0; i < ehdr.e_phnum; i++) { | |
230 | ph = &phdr[i]; | |
231 | glue(bswap_phdr, SZ)(ph); | |
232 | } | |
233 | } | |
3b46e624 | 234 | |
5fe141fd FB |
235 | total_size = 0; |
236 | for(i = 0; i < ehdr.e_phnum; i++) { | |
237 | ph = &phdr[i]; | |
238 | if (ph->p_type == PT_LOAD) { | |
239 | mem_size = ph->p_memsz; | |
240 | /* XXX: avoid allocating */ | |
241 | data = qemu_mallocz(mem_size); | |
242 | if (ph->p_filesz > 0) { | |
9ee3c029 | 243 | if (lseek(fd, ph->p_offset, SEEK_SET) < 0) |
04d4b0c3 | 244 | goto fail; |
5fe141fd | 245 | if (read(fd, data, ph->p_filesz) != ph->p_filesz) |
04d4b0c3 | 246 | goto fail; |
5fe141fd | 247 | } |
83c1f87c PB |
248 | /* address_offset is hack for kernel images that are |
249 | linked at the wrong physical address. */ | |
250 | addr = ph->p_paddr + address_offset; | |
5fe141fd FB |
251 | |
252 | cpu_physical_memory_write_rom(addr, data, mem_size); | |
253 | ||
254 | total_size += mem_size; | |
fd93a799 | 255 | if (addr < low) |
74287114 | 256 | low = addr; |
fd93a799 | 257 | if ((addr + mem_size) > high) |
74287114 | 258 | high = addr + mem_size; |
5fe141fd FB |
259 | |
260 | qemu_free(data); | |
9ee3c029 | 261 | data = NULL; |
5fe141fd FB |
262 | } |
263 | } | |
3aee288b | 264 | qemu_free(phdr); |
74287114 | 265 | if (lowaddr) |
82790064 | 266 | *lowaddr = (uint64_t)(elf_sword)low; |
74287114 | 267 | if (highaddr) |
82790064 | 268 | *highaddr = (uint64_t)(elf_sword)high; |
5fe141fd | 269 | return total_size; |
04d4b0c3 | 270 | fail: |
9ee3c029 | 271 | qemu_free(data); |
5fe141fd FB |
272 | qemu_free(phdr); |
273 | return -1; | |
274 | } |