Statistics
| Branch: | Revision:

root / elf_ops.h @ cedd91d2

History | View | Annotate | Download (7.9 kB)

1 5fe141fd bellard
static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
2 5fe141fd bellard
{
3 5fe141fd bellard
    bswap16s(&ehdr->e_type);                        /* Object file type */
4 5fe141fd bellard
    bswap16s(&ehdr->e_machine);                /* Architecture */
5 5fe141fd bellard
    bswap32s(&ehdr->e_version);                /* Object file version */
6 5fe141fd bellard
    bswapSZs(&ehdr->e_entry);                /* Entry point virtual address */
7 5fe141fd bellard
    bswapSZs(&ehdr->e_phoff);                /* Program header table file offset */
8 5fe141fd bellard
    bswapSZs(&ehdr->e_shoff);                /* Section header table file offset */
9 5fe141fd bellard
    bswap32s(&ehdr->e_flags);                /* Processor-specific flags */
10 5fe141fd bellard
    bswap16s(&ehdr->e_ehsize);                /* ELF header size in bytes */
11 5fe141fd bellard
    bswap16s(&ehdr->e_phentsize);                /* Program header table entry size */
12 5fe141fd bellard
    bswap16s(&ehdr->e_phnum);                /* Program header table entry count */
13 5fe141fd bellard
    bswap16s(&ehdr->e_shentsize);                /* Section header table entry size */
14 5fe141fd bellard
    bswap16s(&ehdr->e_shnum);                /* Section header table entry count */
15 5fe141fd bellard
    bswap16s(&ehdr->e_shstrndx);                /* Section header string table index */
16 5fe141fd bellard
}
17 5fe141fd bellard
18 5fe141fd bellard
static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
19 5fe141fd bellard
{
20 5fe141fd bellard
    bswap32s(&phdr->p_type);                        /* Segment type */
21 5fe141fd bellard
    bswapSZs(&phdr->p_offset);                /* Segment file offset */
22 5fe141fd bellard
    bswapSZs(&phdr->p_vaddr);                /* Segment virtual address */
23 5fe141fd bellard
    bswapSZs(&phdr->p_paddr);                /* Segment physical address */
24 5fe141fd bellard
    bswapSZs(&phdr->p_filesz);                /* Segment size in file */
25 5fe141fd bellard
    bswapSZs(&phdr->p_memsz);                /* Segment size in memory */
26 5fe141fd bellard
    bswap32s(&phdr->p_flags);                /* Segment flags */
27 5fe141fd bellard
    bswapSZs(&phdr->p_align);                /* Segment alignment */
28 5fe141fd bellard
}
29 5fe141fd bellard
30 5fe141fd bellard
static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
31 5fe141fd bellard
{
32 5fe141fd bellard
    bswap32s(&shdr->sh_name);
33 5fe141fd bellard
    bswap32s(&shdr->sh_type);
34 5fe141fd bellard
    bswapSZs(&shdr->sh_flags);
35 5fe141fd bellard
    bswapSZs(&shdr->sh_addr);
36 5fe141fd bellard
    bswapSZs(&shdr->sh_offset);
37 5fe141fd bellard
    bswapSZs(&shdr->sh_size);
38 5fe141fd bellard
    bswap32s(&shdr->sh_link);
39 5fe141fd bellard
    bswap32s(&shdr->sh_info);
40 5fe141fd bellard
    bswapSZs(&shdr->sh_addralign);
41 5fe141fd bellard
    bswapSZs(&shdr->sh_entsize);
42 5fe141fd bellard
}
43 5fe141fd bellard
44 5fe141fd bellard
static void glue(bswap_sym, SZ)(struct elf_sym *sym)
45 5fe141fd bellard
{
46 5fe141fd bellard
    bswap32s(&sym->st_name);
47 5fe141fd bellard
    bswapSZs(&sym->st_value);
48 5fe141fd bellard
    bswapSZs(&sym->st_size);
49 5fe141fd bellard
    bswap16s(&sym->st_shndx);
50 5fe141fd bellard
}
51 5fe141fd bellard
52 5fafdf24 ths
static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
53 5fe141fd bellard
                                               int n, int type)
54 5fe141fd bellard
{
55 5fe141fd bellard
    int i;
56 5fe141fd bellard
    for(i=0;i<n;i++) {
57 5fe141fd bellard
        if (shdr_table[i].sh_type == type)
58 5fe141fd bellard
            return shdr_table + i;
59 5fe141fd bellard
    }
60 5fe141fd bellard
    return NULL;
61 5fe141fd bellard
}
62 5fe141fd bellard
63 49918a75 pbrook
static int glue(symfind, SZ)(const void *s0, const void *s1)
64 49918a75 pbrook
{
65 49918a75 pbrook
    struct elf_sym *key = (struct elf_sym *)s0;
66 49918a75 pbrook
    struct elf_sym *sym = (struct elf_sym *)s1;
67 49918a75 pbrook
    int result = 0;
68 49918a75 pbrook
    if (key->st_value < sym->st_value) {
69 49918a75 pbrook
        result = -1;
70 ec822001 Laurent Desnogues
    } else if (key->st_value >= sym->st_value + sym->st_size) {
71 49918a75 pbrook
        result = 1;
72 49918a75 pbrook
    }
73 49918a75 pbrook
    return result;
74 49918a75 pbrook
}
75 49918a75 pbrook
76 49918a75 pbrook
static const char *glue(lookup_symbol, SZ)(struct syminfo *s, target_ulong orig_addr)
77 49918a75 pbrook
{
78 49918a75 pbrook
    struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
79 49918a75 pbrook
    struct elf_sym key;
80 49918a75 pbrook
    struct elf_sym *sym;
81 49918a75 pbrook
82 49918a75 pbrook
    key.st_value = orig_addr;
83 49918a75 pbrook
84 49918a75 pbrook
    sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), glue(symfind, SZ));
85 660f11be Blue Swirl
    if (sym != NULL) {
86 49918a75 pbrook
        return s->disas_strtab + sym->st_name;
87 49918a75 pbrook
    }
88 49918a75 pbrook
89 49918a75 pbrook
    return "";
90 49918a75 pbrook
}
91 49918a75 pbrook
92 49918a75 pbrook
static int glue(symcmp, SZ)(const void *s0, const void *s1)
93 49918a75 pbrook
{
94 49918a75 pbrook
    struct elf_sym *sym0 = (struct elf_sym *)s0;
95 49918a75 pbrook
    struct elf_sym *sym1 = (struct elf_sym *)s1;
96 49918a75 pbrook
    return (sym0->st_value < sym1->st_value)
97 49918a75 pbrook
        ? -1
98 49918a75 pbrook
        : ((sym0->st_value > sym1->st_value) ? 1 : 0);
99 49918a75 pbrook
}
100 49918a75 pbrook
101 5fe141fd bellard
static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab)
102 5fe141fd bellard
{
103 5fe141fd bellard
    struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
104 5fe141fd bellard
    struct elf_sym *syms = NULL;
105 5fe141fd bellard
    struct syminfo *s;
106 5fe141fd bellard
    int nsyms, i;
107 5fe141fd bellard
    char *str = NULL;
108 5fe141fd bellard
109 5fafdf24 ths
    shdr_table = load_at(fd, ehdr->e_shoff,
110 5fe141fd bellard
                         sizeof(struct elf_shdr) * ehdr->e_shnum);
111 5fe141fd bellard
    if (!shdr_table)
112 5fe141fd bellard
        return -1;
113 3b46e624 ths
114 5fe141fd bellard
    if (must_swab) {
115 5fe141fd bellard
        for (i = 0; i < ehdr->e_shnum; i++) {
116 5fe141fd bellard
            glue(bswap_shdr, SZ)(shdr_table + i);
117 5fe141fd bellard
        }
118 5fe141fd bellard
    }
119 3b46e624 ths
120 5fe141fd bellard
    symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB);
121 5fe141fd bellard
    if (!symtab)
122 5fe141fd bellard
        goto fail;
123 5fe141fd bellard
    syms = load_at(fd, symtab->sh_offset, symtab->sh_size);
124 5fe141fd bellard
    if (!syms)
125 5fe141fd bellard
        goto fail;
126 5fe141fd bellard
127 5fe141fd bellard
    nsyms = symtab->sh_size / sizeof(struct elf_sym);
128 49918a75 pbrook
129 49918a75 pbrook
    i = 0;
130 49918a75 pbrook
    while (i < nsyms) {
131 5fe141fd bellard
        if (must_swab)
132 5fe141fd bellard
            glue(bswap_sym, SZ)(&syms[i]);
133 49918a75 pbrook
        /* We are only interested in function symbols.
134 49918a75 pbrook
           Throw everything else away.  */
135 49918a75 pbrook
        if (syms[i].st_shndx == SHN_UNDEF ||
136 49918a75 pbrook
                syms[i].st_shndx >= SHN_LORESERVE ||
137 49918a75 pbrook
                ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
138 49918a75 pbrook
            nsyms--;
139 49918a75 pbrook
            if (i < nsyms) {
140 49918a75 pbrook
                syms[i] = syms[nsyms];
141 49918a75 pbrook
            }
142 49918a75 pbrook
            continue;
143 49918a75 pbrook
        }
144 49918a75 pbrook
#if defined(TARGET_ARM) || defined (TARGET_MIPS)
145 49918a75 pbrook
        /* The bottom address bit marks a Thumb or MIPS16 symbol.  */
146 49918a75 pbrook
        syms[i].st_value &= ~(target_ulong)1;
147 5fe141fd bellard
#endif
148 49918a75 pbrook
        i++;
149 5fe141fd bellard
    }
150 49918a75 pbrook
    syms = qemu_realloc(syms, nsyms * sizeof(*syms));
151 49918a75 pbrook
152 49918a75 pbrook
    qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
153 49918a75 pbrook
154 5fe141fd bellard
    /* String table */
155 5fe141fd bellard
    if (symtab->sh_link >= ehdr->e_shnum)
156 5fe141fd bellard
        goto fail;
157 5fe141fd bellard
    strtab = &shdr_table[symtab->sh_link];
158 5fe141fd bellard
159 5fe141fd bellard
    str = load_at(fd, strtab->sh_offset, strtab->sh_size);
160 5fe141fd bellard
    if (!str)
161 49918a75 pbrook
        goto fail;
162 5fe141fd bellard
163 5fe141fd bellard
    /* Commit */
164 5fe141fd bellard
    s = qemu_mallocz(sizeof(*s));
165 49918a75 pbrook
    s->lookup_symbol = glue(lookup_symbol, SZ);
166 49918a75 pbrook
    glue(s->disas_symtab.elf, SZ) = syms;
167 5fe141fd bellard
    s->disas_num_syms = nsyms;
168 5fe141fd bellard
    s->disas_strtab = str;
169 5fe141fd bellard
    s->next = syminfos;
170 5fe141fd bellard
    syminfos = s;
171 5fe141fd bellard
    qemu_free(shdr_table);
172 5fe141fd bellard
    return 0;
173 5fe141fd bellard
 fail:
174 5fe141fd bellard
    qemu_free(syms);
175 5fe141fd bellard
    qemu_free(str);
176 5fe141fd bellard
    qemu_free(shdr_table);
177 5fe141fd bellard
    return -1;
178 5fe141fd bellard
}
179 5fe141fd bellard
180 83c1f87c pbrook
static int glue(load_elf, SZ)(int fd, int64_t address_offset,
181 9596ebb7 pbrook
                              int must_swab, uint64_t *pentry,
182 9596ebb7 pbrook
                              uint64_t *lowaddr, uint64_t *highaddr)
183 5fe141fd bellard
{
184 5fe141fd bellard
    struct elfhdr ehdr;
185 5fe141fd bellard
    struct elf_phdr *phdr = NULL, *ph;
186 5fe141fd bellard
    int size, i, total_size;
187 9437454a blueswir1
    elf_word mem_size;
188 fd93a799 Paul Brook
    uint64_t addr, low = (uint64_t)-1, high = 0;
189 9ee3c029 bellard
    uint8_t *data = NULL;
190 5fe141fd bellard
191 5fe141fd bellard
    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
192 5fe141fd bellard
        goto fail;
193 5fe141fd bellard
    if (must_swab) {
194 5fe141fd bellard
        glue(bswap_ehdr, SZ)(&ehdr);
195 5fe141fd bellard
    }
196 5fe141fd bellard
197 7f70c937 blueswir1
    switch (ELF_MACHINE) {
198 7f70c937 blueswir1
        case EM_PPC64:
199 7f70c937 blueswir1
            if (EM_PPC64 != ehdr.e_machine)
200 7f70c937 blueswir1
                if (EM_PPC != ehdr.e_machine)
201 7f70c937 blueswir1
                    goto fail;
202 7f70c937 blueswir1
            break;
203 7f70c937 blueswir1
        case EM_X86_64:
204 7f70c937 blueswir1
            if (EM_X86_64 != ehdr.e_machine)
205 7f70c937 blueswir1
                if (EM_386 != ehdr.e_machine)
206 7f70c937 blueswir1
                    goto fail;
207 7f70c937 blueswir1
            break;
208 7f70c937 blueswir1
        default:
209 7f70c937 blueswir1
            if (ELF_MACHINE != ehdr.e_machine)
210 7f70c937 blueswir1
                goto fail;
211 7f70c937 blueswir1
    }
212 9042c0e2 ths
213 9ee3c029 bellard
    if (pentry)
214 82790064 ths
           *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
215 9ee3c029 bellard
216 5fe141fd bellard
    glue(load_symbols, SZ)(&ehdr, fd, must_swab);
217 5fe141fd bellard
218 5fe141fd bellard
    size = ehdr.e_phnum * sizeof(phdr[0]);
219 5fe141fd bellard
    lseek(fd, ehdr.e_phoff, SEEK_SET);
220 5fe141fd bellard
    phdr = qemu_mallocz(size);
221 5fe141fd bellard
    if (!phdr)
222 5fe141fd bellard
        goto fail;
223 5fe141fd bellard
    if (read(fd, phdr, size) != size)
224 04d4b0c3 ths
        goto fail;
225 5fe141fd bellard
    if (must_swab) {
226 5fe141fd bellard
        for(i = 0; i < ehdr.e_phnum; i++) {
227 5fe141fd bellard
            ph = &phdr[i];
228 5fe141fd bellard
            glue(bswap_phdr, SZ)(ph);
229 5fe141fd bellard
        }
230 5fe141fd bellard
    }
231 3b46e624 ths
232 5fe141fd bellard
    total_size = 0;
233 5fe141fd bellard
    for(i = 0; i < ehdr.e_phnum; i++) {
234 5fe141fd bellard
        ph = &phdr[i];
235 5fe141fd bellard
        if (ph->p_type == PT_LOAD) {
236 5fe141fd bellard
            mem_size = ph->p_memsz;
237 5fe141fd bellard
            /* XXX: avoid allocating */
238 5fe141fd bellard
            data = qemu_mallocz(mem_size);
239 5fe141fd bellard
            if (ph->p_filesz > 0) {
240 9ee3c029 bellard
                if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
241 04d4b0c3 ths
                    goto fail;
242 5fe141fd bellard
                if (read(fd, data, ph->p_filesz) != ph->p_filesz)
243 04d4b0c3 ths
                    goto fail;
244 5fe141fd bellard
            }
245 83c1f87c pbrook
            /* address_offset is hack for kernel images that are
246 83c1f87c pbrook
               linked at the wrong physical address.  */
247 83c1f87c pbrook
            addr = ph->p_paddr + address_offset;
248 5fe141fd bellard
249 5fe141fd bellard
            cpu_physical_memory_write_rom(addr, data, mem_size);
250 5fe141fd bellard
251 5fe141fd bellard
            total_size += mem_size;
252 fd93a799 Paul Brook
            if (addr < low)
253 74287114 ths
                low = addr;
254 fd93a799 Paul Brook
            if ((addr + mem_size) > high)
255 74287114 ths
                high = addr + mem_size;
256 5fe141fd bellard
257 5fe141fd bellard
            qemu_free(data);
258 9ee3c029 bellard
            data = NULL;
259 5fe141fd bellard
        }
260 5fe141fd bellard
    }
261 3aee288b bellard
    qemu_free(phdr);
262 74287114 ths
    if (lowaddr)
263 82790064 ths
        *lowaddr = (uint64_t)(elf_sword)low;
264 74287114 ths
    if (highaddr)
265 82790064 ths
        *highaddr = (uint64_t)(elf_sword)high;
266 5fe141fd bellard
    return total_size;
267 04d4b0c3 ths
 fail:
268 9ee3c029 bellard
    qemu_free(data);
269 5fe141fd bellard
    qemu_free(phdr);
270 5fe141fd bellard
    return -1;
271 5fe141fd bellard
}