63
static int glue(symfind, SZ)(const void *s0, const void *s1)
65
struct elf_sym *key = (struct elf_sym *)s0;
66
struct elf_sym *sym = (struct elf_sym *)s1;
68
if (key->st_value < sym->st_value) {
70
} else if (key->st_value >= sym->st_value + sym->st_size) {
76
static const char *glue(lookup_symbol, SZ)(struct syminfo *s, target_ulong orig_addr)
78
struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
82
key.st_value = orig_addr;
84
sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), glue(symfind, SZ));
86
return s->disas_strtab + sym->st_name;
92
static int glue(symcmp, SZ)(const void *s0, const void *s1)
94
struct elf_sym *sym0 = (struct elf_sym *)s0;
95
struct elf_sym *sym1 = (struct elf_sym *)s1;
96
return (sym0->st_value < sym1->st_value)
98
: ((sym0->st_value > sym1->st_value) ? 1 : 0);
63
101
static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab)
65
103
struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
66
104
struct elf_sym *syms = NULL;
68
struct elf32_sym *syms32 = NULL;
70
105
struct syminfo *s;
92
127
nsyms = symtab->sh_size / sizeof(struct elf_sym);
94
syms32 = qemu_mallocz(nsyms * sizeof(struct elf32_sym));
96
for (i = 0; i < nsyms; i++) {
98
132
glue(bswap_sym, SZ)(&syms[i]);
100
syms32[i].st_name = syms[i].st_name;
101
syms32[i].st_info = syms[i].st_info;
102
syms32[i].st_other = syms[i].st_other;
103
syms32[i].st_shndx = syms[i].st_shndx;
104
syms32[i].st_value = syms[i].st_value & 0xffffffff;
105
syms32[i].st_size = syms[i].st_size & 0xffffffff;
133
/* We are only interested in function symbols.
134
Throw everything else away. */
135
if (syms[i].st_shndx == SHN_UNDEF ||
136
syms[i].st_shndx >= SHN_LORESERVE ||
137
ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
140
syms[i] = syms[nsyms];
144
#if defined(TARGET_ARM) || defined (TARGET_MIPS)
145
/* The bottom address bit marks a Thumb or MIPS16 symbol. */
146
syms[i].st_value &= ~(target_ulong)1;
150
syms = qemu_realloc(syms, nsyms * sizeof(*syms));
152
qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
108
154
/* String table */
109
155
if (symtab->sh_link >= ehdr->e_shnum)
113
159
str = load_at(fd, strtab->sh_offset, strtab->sh_size);
118
164
s = qemu_mallocz(sizeof(*s));
120
s->disas_symtab = syms32;
123
s->disas_symtab = syms;
165
s->lookup_symbol = glue(lookup_symbol, SZ);
166
glue(s->disas_symtab.elf, SZ) = syms;
125
167
s->disas_num_syms = nsyms;
126
168
s->disas_strtab = str;
127
169
s->next = syminfos;
129
171
qemu_free(shdr_table);
137
176
qemu_free(shdr_table);
141
static int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend,
180
static int glue(load_elf, SZ)(int fd, int64_t address_offset,
142
181
int must_swab, uint64_t *pentry,
143
182
uint64_t *lowaddr, uint64_t *highaddr)
146
185
struct elf_phdr *phdr = NULL, *ph;
147
186
int size, i, total_size;
148
187
elf_word mem_size;
149
uint64_t addr, low = 0, high = 0;
188
uint64_t addr, low = (uint64_t)-1, high = 0;
150
189
uint8_t *data = NULL;
152
191
if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
155
194
glue(bswap_ehdr, SZ)(&ehdr);
158
if (ELF_MACHINE != ehdr.e_machine)
197
switch (ELF_MACHINE) {
199
if (EM_PPC64 != ehdr.e_machine)
200
if (EM_PPC != ehdr.e_machine)
204
if (EM_X86_64 != ehdr.e_machine)
205
if (EM_386 != ehdr.e_machine)
209
if (ELF_MACHINE != ehdr.e_machine)
162
214
*pentry = (uint64_t)(elf_sword)ehdr.e_entry;
190
242
if (read(fd, data, ph->p_filesz) != ph->p_filesz)
193
addr = ph->p_vaddr + virt_to_phys_addend;
245
/* address_offset is hack for kernel images that are
246
linked at the wrong physical address. */
247
addr = ph->p_paddr + address_offset;
195
249
cpu_physical_memory_write_rom(addr, data, mem_size);
197
251
total_size += mem_size;
198
if (!low || addr < low)
200
if (!high || (addr + mem_size) > high)
254
if ((addr + mem_size) > high)
201
255
high = addr + mem_size;