Statistics
| Branch: | Revision:

root / include / hw / elf_ops.h @ 0445259b

History | View | Annotate | Download (9.4 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 a8170e5e Avi Kivity
    hwaddr addr = *(hwaddr *)s0;
66 49918a75 pbrook
    struct elf_sym *sym = (struct elf_sym *)s1;
67 49918a75 pbrook
    int result = 0;
68 c7c530cd Stefan Weil
    if (addr < sym->st_value) {
69 49918a75 pbrook
        result = -1;
70 c7c530cd Stefan Weil
    } else if (addr >= 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 a8170e5e Avi Kivity
                                           hwaddr orig_addr)
78 49918a75 pbrook
{
79 49918a75 pbrook
    struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
80 49918a75 pbrook
    struct elf_sym *sym;
81 49918a75 pbrook
82 c7c530cd Stefan Weil
    sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms),
83 c7c530cd Stefan Weil
                  glue(symfind, SZ));
84 660f11be Blue Swirl
    if (sym != NULL) {
85 49918a75 pbrook
        return s->disas_strtab + sym->st_name;
86 49918a75 pbrook
    }
87 49918a75 pbrook
88 49918a75 pbrook
    return "";
89 49918a75 pbrook
}
90 49918a75 pbrook
91 49918a75 pbrook
static int glue(symcmp, SZ)(const void *s0, const void *s1)
92 49918a75 pbrook
{
93 49918a75 pbrook
    struct elf_sym *sym0 = (struct elf_sym *)s0;
94 49918a75 pbrook
    struct elf_sym *sym1 = (struct elf_sym *)s1;
95 49918a75 pbrook
    return (sym0->st_value < sym1->st_value)
96 49918a75 pbrook
        ? -1
97 49918a75 pbrook
        : ((sym0->st_value > sym1->st_value) ? 1 : 0);
98 49918a75 pbrook
}
99 49918a75 pbrook
100 ca20cf32 Blue Swirl
static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
101 ca20cf32 Blue Swirl
                                  int clear_lsb)
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 ca20cf32 Blue Swirl
        if (clear_lsb) {
145 ca20cf32 Blue Swirl
            /* The bottom address bit marks a Thumb or MIPS16 symbol.  */
146 ca20cf32 Blue Swirl
            syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
147 ca20cf32 Blue Swirl
        }
148 49918a75 pbrook
        i++;
149 5fe141fd bellard
    }
150 3e372cf8 Aurelien Jarno
    if (nsyms) {
151 7267c094 Anthony Liguori
        syms = g_realloc(syms, nsyms * sizeof(*syms));
152 49918a75 pbrook
153 3e372cf8 Aurelien Jarno
        qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
154 e403e433 Stefan Weil
        for (i = 0; i < nsyms - 1; i++) {
155 e403e433 Stefan Weil
            if (syms[i].st_size == 0) {
156 e403e433 Stefan Weil
                syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
157 e403e433 Stefan Weil
            }
158 e403e433 Stefan Weil
        }
159 3e372cf8 Aurelien Jarno
    } else {
160 7267c094 Anthony Liguori
        g_free(syms);
161 3e372cf8 Aurelien Jarno
        syms = NULL;
162 3e372cf8 Aurelien Jarno
    }
163 49918a75 pbrook
164 5fe141fd bellard
    /* String table */
165 5fe141fd bellard
    if (symtab->sh_link >= ehdr->e_shnum)
166 5fe141fd bellard
        goto fail;
167 5fe141fd bellard
    strtab = &shdr_table[symtab->sh_link];
168 5fe141fd bellard
169 5fe141fd bellard
    str = load_at(fd, strtab->sh_offset, strtab->sh_size);
170 5fe141fd bellard
    if (!str)
171 49918a75 pbrook
        goto fail;
172 5fe141fd bellard
173 5fe141fd bellard
    /* Commit */
174 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(*s));
175 49918a75 pbrook
    s->lookup_symbol = glue(lookup_symbol, SZ);
176 49918a75 pbrook
    glue(s->disas_symtab.elf, SZ) = syms;
177 5fe141fd bellard
    s->disas_num_syms = nsyms;
178 5fe141fd bellard
    s->disas_strtab = str;
179 5fe141fd bellard
    s->next = syminfos;
180 5fe141fd bellard
    syminfos = s;
181 7267c094 Anthony Liguori
    g_free(shdr_table);
182 5fe141fd bellard
    return 0;
183 5fe141fd bellard
 fail:
184 7267c094 Anthony Liguori
    g_free(syms);
185 7267c094 Anthony Liguori
    g_free(str);
186 7267c094 Anthony Liguori
    g_free(shdr_table);
187 5fe141fd bellard
    return -1;
188 5fe141fd bellard
}
189 5fe141fd bellard
190 409dbce5 Aurelien Jarno
static int glue(load_elf, SZ)(const char *name, int fd,
191 409dbce5 Aurelien Jarno
                              uint64_t (*translate_fn)(void *, uint64_t),
192 409dbce5 Aurelien Jarno
                              void *translate_opaque,
193 9596ebb7 pbrook
                              int must_swab, uint64_t *pentry,
194 ca20cf32 Blue Swirl
                              uint64_t *lowaddr, uint64_t *highaddr,
195 ca20cf32 Blue Swirl
                              int elf_machine, int clear_lsb)
196 5fe141fd bellard
{
197 5fe141fd bellard
    struct elfhdr ehdr;
198 5fe141fd bellard
    struct elf_phdr *phdr = NULL, *ph;
199 5fe141fd bellard
    int size, i, total_size;
200 d60fa42e Fabien Chouteau
    elf_word mem_size, file_size;
201 fd93a799 Paul Brook
    uint64_t addr, low = (uint64_t)-1, high = 0;
202 9ee3c029 bellard
    uint8_t *data = NULL;
203 45a50b16 Gerd Hoffmann
    char label[128];
204 5fe141fd bellard
205 5fe141fd bellard
    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
206 5fe141fd bellard
        goto fail;
207 5fe141fd bellard
    if (must_swab) {
208 5fe141fd bellard
        glue(bswap_ehdr, SZ)(&ehdr);
209 5fe141fd bellard
    }
210 5fe141fd bellard
211 ca20cf32 Blue Swirl
    switch (elf_machine) {
212 7f70c937 blueswir1
        case EM_PPC64:
213 7f70c937 blueswir1
            if (EM_PPC64 != ehdr.e_machine)
214 7f70c937 blueswir1
                if (EM_PPC != ehdr.e_machine)
215 7f70c937 blueswir1
                    goto fail;
216 7f70c937 blueswir1
            break;
217 7f70c937 blueswir1
        case EM_X86_64:
218 7f70c937 blueswir1
            if (EM_X86_64 != ehdr.e_machine)
219 7f70c937 blueswir1
                if (EM_386 != ehdr.e_machine)
220 7f70c937 blueswir1
                    goto fail;
221 7f70c937 blueswir1
            break;
222 16f04416 Edgar E. Iglesias
        case EM_MICROBLAZE:
223 16f04416 Edgar E. Iglesias
            if (EM_MICROBLAZE != ehdr.e_machine)
224 16f04416 Edgar E. Iglesias
                if (EM_MICROBLAZE_OLD != ehdr.e_machine)
225 16f04416 Edgar E. Iglesias
                    goto fail;
226 16f04416 Edgar E. Iglesias
            break;
227 7f70c937 blueswir1
        default:
228 ca20cf32 Blue Swirl
            if (elf_machine != ehdr.e_machine)
229 7f70c937 blueswir1
                goto fail;
230 7f70c937 blueswir1
    }
231 9042c0e2 ths
232 9ee3c029 bellard
    if (pentry)
233 82790064 ths
           *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
234 9ee3c029 bellard
235 ca20cf32 Blue Swirl
    glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
236 5fe141fd bellard
237 5fe141fd bellard
    size = ehdr.e_phnum * sizeof(phdr[0]);
238 5fe141fd bellard
    lseek(fd, ehdr.e_phoff, SEEK_SET);
239 7267c094 Anthony Liguori
    phdr = g_malloc0(size);
240 5fe141fd bellard
    if (!phdr)
241 5fe141fd bellard
        goto fail;
242 5fe141fd bellard
    if (read(fd, phdr, size) != size)
243 04d4b0c3 ths
        goto fail;
244 5fe141fd bellard
    if (must_swab) {
245 5fe141fd bellard
        for(i = 0; i < ehdr.e_phnum; i++) {
246 5fe141fd bellard
            ph = &phdr[i];
247 5fe141fd bellard
            glue(bswap_phdr, SZ)(ph);
248 5fe141fd bellard
        }
249 5fe141fd bellard
    }
250 3b46e624 ths
251 5fe141fd bellard
    total_size = 0;
252 5fe141fd bellard
    for(i = 0; i < ehdr.e_phnum; i++) {
253 5fe141fd bellard
        ph = &phdr[i];
254 5fe141fd bellard
        if (ph->p_type == PT_LOAD) {
255 d60fa42e Fabien Chouteau
            mem_size = ph->p_memsz; /* Size of the ROM */
256 d60fa42e Fabien Chouteau
            file_size = ph->p_filesz; /* Size of the allocated data */
257 d60fa42e Fabien Chouteau
            data = g_malloc0(file_size);
258 5fe141fd bellard
            if (ph->p_filesz > 0) {
259 d60fa42e Fabien Chouteau
                if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
260 04d4b0c3 ths
                    goto fail;
261 d60fa42e Fabien Chouteau
                }
262 d60fa42e Fabien Chouteau
                if (read(fd, data, file_size) != file_size) {
263 04d4b0c3 ths
                    goto fail;
264 d60fa42e Fabien Chouteau
                }
265 5fe141fd bellard
            }
266 83c1f87c pbrook
            /* address_offset is hack for kernel images that are
267 83c1f87c pbrook
               linked at the wrong physical address.  */
268 409dbce5 Aurelien Jarno
            if (translate_fn) {
269 409dbce5 Aurelien Jarno
                addr = translate_fn(translate_opaque, ph->p_paddr);
270 409dbce5 Aurelien Jarno
            } else {
271 409dbce5 Aurelien Jarno
                addr = ph->p_paddr;
272 409dbce5 Aurelien Jarno
            }
273 5fe141fd bellard
274 7e9c7ffe Henning Schild
            /* the entry pointer in the ELF header is a virtual
275 7e9c7ffe Henning Schild
             * address, if the text segments paddr and vaddr differ
276 7e9c7ffe Henning Schild
             * we need to adjust the entry */
277 7e9c7ffe Henning Schild
            if (pentry && !translate_fn &&
278 7e9c7ffe Henning Schild
                    ph->p_vaddr != ph->p_paddr &&
279 7e9c7ffe Henning Schild
                    ehdr.e_entry >= ph->p_vaddr &&
280 7e9c7ffe Henning Schild
                    ehdr.e_entry < ph->p_vaddr + ph->p_filesz &&
281 7e9c7ffe Henning Schild
                    ph->p_flags & PF_X) {
282 7e9c7ffe Henning Schild
                *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
283 7e9c7ffe Henning Schild
            }
284 7e9c7ffe Henning Schild
285 45a50b16 Gerd Hoffmann
            snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
286 d60fa42e Fabien Chouteau
287 d60fa42e Fabien Chouteau
            /* rom_add_elf_program() seize the ownership of 'data' */
288 d60fa42e Fabien Chouteau
            rom_add_elf_program(label, data, file_size, mem_size, addr);
289 5fe141fd bellard
290 5fe141fd bellard
            total_size += mem_size;
291 fd93a799 Paul Brook
            if (addr < low)
292 74287114 ths
                low = addr;
293 fd93a799 Paul Brook
            if ((addr + mem_size) > high)
294 74287114 ths
                high = addr + mem_size;
295 5fe141fd bellard
296 9ee3c029 bellard
            data = NULL;
297 5fe141fd bellard
        }
298 5fe141fd bellard
    }
299 7267c094 Anthony Liguori
    g_free(phdr);
300 74287114 ths
    if (lowaddr)
301 82790064 ths
        *lowaddr = (uint64_t)(elf_sword)low;
302 74287114 ths
    if (highaddr)
303 82790064 ths
        *highaddr = (uint64_t)(elf_sword)high;
304 5fe141fd bellard
    return total_size;
305 04d4b0c3 ths
 fail:
306 7267c094 Anthony Liguori
    g_free(data);
307 7267c094 Anthony Liguori
    g_free(phdr);
308 5fe141fd bellard
    return -1;
309 5fe141fd bellard
}