Statistics
| Branch: | Revision:

root / hw / elf_ops.h @ 46aaebff

History | View | Annotate | Download (8.7 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 ca20cf32 Blue Swirl
static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
77 c227f099 Anthony Liguori
                                           target_phys_addr_t orig_addr)
78 49918a75 pbrook
{
79 49918a75 pbrook
    struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
80 49918a75 pbrook
    struct elf_sym key;
81 49918a75 pbrook
    struct elf_sym *sym;
82 49918a75 pbrook
83 49918a75 pbrook
    key.st_value = orig_addr;
84 49918a75 pbrook
85 49918a75 pbrook
    sym = bsearch(&key, syms, s->disas_num_syms, sizeof(*syms), glue(symfind, SZ));
86 660f11be Blue Swirl
    if (sym != NULL) {
87 49918a75 pbrook
        return s->disas_strtab + sym->st_name;
88 49918a75 pbrook
    }
89 49918a75 pbrook
90 49918a75 pbrook
    return "";
91 49918a75 pbrook
}
92 49918a75 pbrook
93 49918a75 pbrook
static int glue(symcmp, SZ)(const void *s0, const void *s1)
94 49918a75 pbrook
{
95 49918a75 pbrook
    struct elf_sym *sym0 = (struct elf_sym *)s0;
96 49918a75 pbrook
    struct elf_sym *sym1 = (struct elf_sym *)s1;
97 49918a75 pbrook
    return (sym0->st_value < sym1->st_value)
98 49918a75 pbrook
        ? -1
99 49918a75 pbrook
        : ((sym0->st_value > sym1->st_value) ? 1 : 0);
100 49918a75 pbrook
}
101 49918a75 pbrook
102 ca20cf32 Blue Swirl
static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
103 ca20cf32 Blue Swirl
                                  int clear_lsb)
104 5fe141fd bellard
{
105 5fe141fd bellard
    struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
106 5fe141fd bellard
    struct elf_sym *syms = NULL;
107 5fe141fd bellard
    struct syminfo *s;
108 5fe141fd bellard
    int nsyms, i;
109 5fe141fd bellard
    char *str = NULL;
110 5fe141fd bellard
111 5fafdf24 ths
    shdr_table = load_at(fd, ehdr->e_shoff,
112 5fe141fd bellard
                         sizeof(struct elf_shdr) * ehdr->e_shnum);
113 5fe141fd bellard
    if (!shdr_table)
114 5fe141fd bellard
        return -1;
115 3b46e624 ths
116 5fe141fd bellard
    if (must_swab) {
117 5fe141fd bellard
        for (i = 0; i < ehdr->e_shnum; i++) {
118 5fe141fd bellard
            glue(bswap_shdr, SZ)(shdr_table + i);
119 5fe141fd bellard
        }
120 5fe141fd bellard
    }
121 3b46e624 ths
122 5fe141fd bellard
    symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB);
123 5fe141fd bellard
    if (!symtab)
124 5fe141fd bellard
        goto fail;
125 5fe141fd bellard
    syms = load_at(fd, symtab->sh_offset, symtab->sh_size);
126 5fe141fd bellard
    if (!syms)
127 5fe141fd bellard
        goto fail;
128 5fe141fd bellard
129 5fe141fd bellard
    nsyms = symtab->sh_size / sizeof(struct elf_sym);
130 49918a75 pbrook
131 49918a75 pbrook
    i = 0;
132 49918a75 pbrook
    while (i < nsyms) {
133 5fe141fd bellard
        if (must_swab)
134 5fe141fd bellard
            glue(bswap_sym, SZ)(&syms[i]);
135 49918a75 pbrook
        /* We are only interested in function symbols.
136 49918a75 pbrook
           Throw everything else away.  */
137 49918a75 pbrook
        if (syms[i].st_shndx == SHN_UNDEF ||
138 49918a75 pbrook
                syms[i].st_shndx >= SHN_LORESERVE ||
139 49918a75 pbrook
                ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
140 49918a75 pbrook
            nsyms--;
141 49918a75 pbrook
            if (i < nsyms) {
142 49918a75 pbrook
                syms[i] = syms[nsyms];
143 49918a75 pbrook
            }
144 49918a75 pbrook
            continue;
145 49918a75 pbrook
        }
146 ca20cf32 Blue Swirl
        if (clear_lsb) {
147 ca20cf32 Blue Swirl
            /* The bottom address bit marks a Thumb or MIPS16 symbol.  */
148 ca20cf32 Blue Swirl
            syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
149 ca20cf32 Blue Swirl
        }
150 49918a75 pbrook
        i++;
151 5fe141fd bellard
    }
152 3e372cf8 Aurelien Jarno
    if (nsyms) {
153 3e372cf8 Aurelien Jarno
        syms = qemu_realloc(syms, nsyms * sizeof(*syms));
154 49918a75 pbrook
155 3e372cf8 Aurelien Jarno
        qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
156 3e372cf8 Aurelien Jarno
    } else {
157 3e372cf8 Aurelien Jarno
        qemu_free(syms);
158 3e372cf8 Aurelien Jarno
        syms = NULL;
159 3e372cf8 Aurelien Jarno
    }
160 49918a75 pbrook
161 5fe141fd bellard
    /* String table */
162 5fe141fd bellard
    if (symtab->sh_link >= ehdr->e_shnum)
163 5fe141fd bellard
        goto fail;
164 5fe141fd bellard
    strtab = &shdr_table[symtab->sh_link];
165 5fe141fd bellard
166 5fe141fd bellard
    str = load_at(fd, strtab->sh_offset, strtab->sh_size);
167 5fe141fd bellard
    if (!str)
168 49918a75 pbrook
        goto fail;
169 5fe141fd bellard
170 5fe141fd bellard
    /* Commit */
171 5fe141fd bellard
    s = qemu_mallocz(sizeof(*s));
172 49918a75 pbrook
    s->lookup_symbol = glue(lookup_symbol, SZ);
173 49918a75 pbrook
    glue(s->disas_symtab.elf, SZ) = syms;
174 5fe141fd bellard
    s->disas_num_syms = nsyms;
175 5fe141fd bellard
    s->disas_strtab = str;
176 5fe141fd bellard
    s->next = syminfos;
177 5fe141fd bellard
    syminfos = s;
178 5fe141fd bellard
    qemu_free(shdr_table);
179 5fe141fd bellard
    return 0;
180 5fe141fd bellard
 fail:
181 5fe141fd bellard
    qemu_free(syms);
182 5fe141fd bellard
    qemu_free(str);
183 5fe141fd bellard
    qemu_free(shdr_table);
184 5fe141fd bellard
    return -1;
185 5fe141fd bellard
}
186 5fe141fd bellard
187 409dbce5 Aurelien Jarno
static int glue(load_elf, SZ)(const char *name, int fd,
188 409dbce5 Aurelien Jarno
                              uint64_t (*translate_fn)(void *, uint64_t),
189 409dbce5 Aurelien Jarno
                              void *translate_opaque,
190 9596ebb7 pbrook
                              int must_swab, uint64_t *pentry,
191 ca20cf32 Blue Swirl
                              uint64_t *lowaddr, uint64_t *highaddr,
192 ca20cf32 Blue Swirl
                              int elf_machine, int clear_lsb)
193 5fe141fd bellard
{
194 5fe141fd bellard
    struct elfhdr ehdr;
195 5fe141fd bellard
    struct elf_phdr *phdr = NULL, *ph;
196 5fe141fd bellard
    int size, i, total_size;
197 9437454a blueswir1
    elf_word mem_size;
198 fd93a799 Paul Brook
    uint64_t addr, low = (uint64_t)-1, high = 0;
199 9ee3c029 bellard
    uint8_t *data = NULL;
200 45a50b16 Gerd Hoffmann
    char label[128];
201 5fe141fd bellard
202 5fe141fd bellard
    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
203 5fe141fd bellard
        goto fail;
204 5fe141fd bellard
    if (must_swab) {
205 5fe141fd bellard
        glue(bswap_ehdr, SZ)(&ehdr);
206 5fe141fd bellard
    }
207 5fe141fd bellard
208 ca20cf32 Blue Swirl
    switch (elf_machine) {
209 7f70c937 blueswir1
        case EM_PPC64:
210 7f70c937 blueswir1
            if (EM_PPC64 != ehdr.e_machine)
211 7f70c937 blueswir1
                if (EM_PPC != ehdr.e_machine)
212 7f70c937 blueswir1
                    goto fail;
213 7f70c937 blueswir1
            break;
214 7f70c937 blueswir1
        case EM_X86_64:
215 7f70c937 blueswir1
            if (EM_X86_64 != ehdr.e_machine)
216 7f70c937 blueswir1
                if (EM_386 != ehdr.e_machine)
217 7f70c937 blueswir1
                    goto fail;
218 7f70c937 blueswir1
            break;
219 16f04416 Edgar E. Iglesias
        case EM_MICROBLAZE:
220 16f04416 Edgar E. Iglesias
            if (EM_MICROBLAZE != ehdr.e_machine)
221 16f04416 Edgar E. Iglesias
                if (EM_MICROBLAZE_OLD != ehdr.e_machine)
222 16f04416 Edgar E. Iglesias
                    goto fail;
223 16f04416 Edgar E. Iglesias
            break;
224 7f70c937 blueswir1
        default:
225 ca20cf32 Blue Swirl
            if (elf_machine != ehdr.e_machine)
226 7f70c937 blueswir1
                goto fail;
227 7f70c937 blueswir1
    }
228 9042c0e2 ths
229 9ee3c029 bellard
    if (pentry)
230 82790064 ths
           *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
231 9ee3c029 bellard
232 ca20cf32 Blue Swirl
    glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
233 5fe141fd bellard
234 5fe141fd bellard
    size = ehdr.e_phnum * sizeof(phdr[0]);
235 5fe141fd bellard
    lseek(fd, ehdr.e_phoff, SEEK_SET);
236 5fe141fd bellard
    phdr = qemu_mallocz(size);
237 5fe141fd bellard
    if (!phdr)
238 5fe141fd bellard
        goto fail;
239 5fe141fd bellard
    if (read(fd, phdr, size) != size)
240 04d4b0c3 ths
        goto fail;
241 5fe141fd bellard
    if (must_swab) {
242 5fe141fd bellard
        for(i = 0; i < ehdr.e_phnum; i++) {
243 5fe141fd bellard
            ph = &phdr[i];
244 5fe141fd bellard
            glue(bswap_phdr, SZ)(ph);
245 5fe141fd bellard
        }
246 5fe141fd bellard
    }
247 3b46e624 ths
248 5fe141fd bellard
    total_size = 0;
249 5fe141fd bellard
    for(i = 0; i < ehdr.e_phnum; i++) {
250 5fe141fd bellard
        ph = &phdr[i];
251 5fe141fd bellard
        if (ph->p_type == PT_LOAD) {
252 5fe141fd bellard
            mem_size = ph->p_memsz;
253 5fe141fd bellard
            /* XXX: avoid allocating */
254 5fe141fd bellard
            data = qemu_mallocz(mem_size);
255 5fe141fd bellard
            if (ph->p_filesz > 0) {
256 9ee3c029 bellard
                if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
257 04d4b0c3 ths
                    goto fail;
258 5fe141fd bellard
                if (read(fd, data, ph->p_filesz) != ph->p_filesz)
259 04d4b0c3 ths
                    goto fail;
260 5fe141fd bellard
            }
261 83c1f87c pbrook
            /* address_offset is hack for kernel images that are
262 83c1f87c pbrook
               linked at the wrong physical address.  */
263 409dbce5 Aurelien Jarno
            if (translate_fn) {
264 409dbce5 Aurelien Jarno
                addr = translate_fn(translate_opaque, ph->p_paddr);
265 409dbce5 Aurelien Jarno
            } else {
266 409dbce5 Aurelien Jarno
                addr = ph->p_paddr;
267 409dbce5 Aurelien Jarno
            }
268 5fe141fd bellard
269 45a50b16 Gerd Hoffmann
            snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
270 45a50b16 Gerd Hoffmann
            rom_add_blob_fixed(label, data, mem_size, addr);
271 5fe141fd bellard
272 5fe141fd bellard
            total_size += mem_size;
273 fd93a799 Paul Brook
            if (addr < low)
274 74287114 ths
                low = addr;
275 fd93a799 Paul Brook
            if ((addr + mem_size) > high)
276 74287114 ths
                high = addr + mem_size;
277 5fe141fd bellard
278 5fe141fd bellard
            qemu_free(data);
279 9ee3c029 bellard
            data = NULL;
280 5fe141fd bellard
        }
281 5fe141fd bellard
    }
282 3aee288b bellard
    qemu_free(phdr);
283 74287114 ths
    if (lowaddr)
284 82790064 ths
        *lowaddr = (uint64_t)(elf_sword)low;
285 74287114 ths
    if (highaddr)
286 82790064 ths
        *highaddr = (uint64_t)(elf_sword)high;
287 5fe141fd bellard
    return total_size;
288 04d4b0c3 ths
 fail:
289 9ee3c029 bellard
    qemu_free(data);
290 5fe141fd bellard
    qemu_free(phdr);
291 5fe141fd bellard
    return -1;
292 5fe141fd bellard
}