Statistics
| Branch: | Revision:

root / hw / elf_ops.h @ 28366c3a

History | View | Annotate | Download (8.2 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 49918a75 pbrook
    syms = qemu_realloc(syms, nsyms * sizeof(*syms));
153 49918a75 pbrook
154 49918a75 pbrook
    qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
155 49918a75 pbrook
156 5fe141fd bellard
    /* String table */
157 5fe141fd bellard
    if (symtab->sh_link >= ehdr->e_shnum)
158 5fe141fd bellard
        goto fail;
159 5fe141fd bellard
    strtab = &shdr_table[symtab->sh_link];
160 5fe141fd bellard
161 5fe141fd bellard
    str = load_at(fd, strtab->sh_offset, strtab->sh_size);
162 5fe141fd bellard
    if (!str)
163 49918a75 pbrook
        goto fail;
164 5fe141fd bellard
165 5fe141fd bellard
    /* Commit */
166 5fe141fd bellard
    s = qemu_mallocz(sizeof(*s));
167 49918a75 pbrook
    s->lookup_symbol = glue(lookup_symbol, SZ);
168 49918a75 pbrook
    glue(s->disas_symtab.elf, SZ) = syms;
169 5fe141fd bellard
    s->disas_num_syms = nsyms;
170 5fe141fd bellard
    s->disas_strtab = str;
171 5fe141fd bellard
    s->next = syminfos;
172 5fe141fd bellard
    syminfos = s;
173 5fe141fd bellard
    qemu_free(shdr_table);
174 5fe141fd bellard
    return 0;
175 5fe141fd bellard
 fail:
176 5fe141fd bellard
    qemu_free(syms);
177 5fe141fd bellard
    qemu_free(str);
178 5fe141fd bellard
    qemu_free(shdr_table);
179 5fe141fd bellard
    return -1;
180 5fe141fd bellard
}
181 5fe141fd bellard
182 45a50b16 Gerd Hoffmann
static int glue(load_elf, SZ)(const char *name, int fd, int64_t address_offset,
183 9596ebb7 pbrook
                              int must_swab, uint64_t *pentry,
184 ca20cf32 Blue Swirl
                              uint64_t *lowaddr, uint64_t *highaddr,
185 ca20cf32 Blue Swirl
                              int elf_machine, int clear_lsb)
186 5fe141fd bellard
{
187 5fe141fd bellard
    struct elfhdr ehdr;
188 5fe141fd bellard
    struct elf_phdr *phdr = NULL, *ph;
189 5fe141fd bellard
    int size, i, total_size;
190 9437454a blueswir1
    elf_word mem_size;
191 fd93a799 Paul Brook
    uint64_t addr, low = (uint64_t)-1, high = 0;
192 9ee3c029 bellard
    uint8_t *data = NULL;
193 45a50b16 Gerd Hoffmann
    char label[128];
194 5fe141fd bellard
195 5fe141fd bellard
    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
196 5fe141fd bellard
        goto fail;
197 5fe141fd bellard
    if (must_swab) {
198 5fe141fd bellard
        glue(bswap_ehdr, SZ)(&ehdr);
199 5fe141fd bellard
    }
200 5fe141fd bellard
201 ca20cf32 Blue Swirl
    switch (elf_machine) {
202 7f70c937 blueswir1
        case EM_PPC64:
203 7f70c937 blueswir1
            if (EM_PPC64 != ehdr.e_machine)
204 7f70c937 blueswir1
                if (EM_PPC != ehdr.e_machine)
205 7f70c937 blueswir1
                    goto fail;
206 7f70c937 blueswir1
            break;
207 7f70c937 blueswir1
        case EM_X86_64:
208 7f70c937 blueswir1
            if (EM_X86_64 != ehdr.e_machine)
209 7f70c937 blueswir1
                if (EM_386 != ehdr.e_machine)
210 7f70c937 blueswir1
                    goto fail;
211 7f70c937 blueswir1
            break;
212 7f70c937 blueswir1
        default:
213 ca20cf32 Blue Swirl
            if (elf_machine != ehdr.e_machine)
214 7f70c937 blueswir1
                goto fail;
215 7f70c937 blueswir1
    }
216 9042c0e2 ths
217 9ee3c029 bellard
    if (pentry)
218 82790064 ths
           *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
219 9ee3c029 bellard
220 ca20cf32 Blue Swirl
    glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
221 5fe141fd bellard
222 5fe141fd bellard
    size = ehdr.e_phnum * sizeof(phdr[0]);
223 5fe141fd bellard
    lseek(fd, ehdr.e_phoff, SEEK_SET);
224 5fe141fd bellard
    phdr = qemu_mallocz(size);
225 5fe141fd bellard
    if (!phdr)
226 5fe141fd bellard
        goto fail;
227 5fe141fd bellard
    if (read(fd, phdr, size) != size)
228 04d4b0c3 ths
        goto fail;
229 5fe141fd bellard
    if (must_swab) {
230 5fe141fd bellard
        for(i = 0; i < ehdr.e_phnum; i++) {
231 5fe141fd bellard
            ph = &phdr[i];
232 5fe141fd bellard
            glue(bswap_phdr, SZ)(ph);
233 5fe141fd bellard
        }
234 5fe141fd bellard
    }
235 3b46e624 ths
236 5fe141fd bellard
    total_size = 0;
237 5fe141fd bellard
    for(i = 0; i < ehdr.e_phnum; i++) {
238 5fe141fd bellard
        ph = &phdr[i];
239 5fe141fd bellard
        if (ph->p_type == PT_LOAD) {
240 5fe141fd bellard
            mem_size = ph->p_memsz;
241 5fe141fd bellard
            /* XXX: avoid allocating */
242 5fe141fd bellard
            data = qemu_mallocz(mem_size);
243 5fe141fd bellard
            if (ph->p_filesz > 0) {
244 9ee3c029 bellard
                if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
245 04d4b0c3 ths
                    goto fail;
246 5fe141fd bellard
                if (read(fd, data, ph->p_filesz) != ph->p_filesz)
247 04d4b0c3 ths
                    goto fail;
248 5fe141fd bellard
            }
249 83c1f87c pbrook
            /* address_offset is hack for kernel images that are
250 83c1f87c pbrook
               linked at the wrong physical address.  */
251 83c1f87c pbrook
            addr = ph->p_paddr + address_offset;
252 5fe141fd bellard
253 45a50b16 Gerd Hoffmann
            snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
254 45a50b16 Gerd Hoffmann
            rom_add_blob_fixed(label, data, mem_size, addr);
255 5fe141fd bellard
256 5fe141fd bellard
            total_size += mem_size;
257 fd93a799 Paul Brook
            if (addr < low)
258 74287114 ths
                low = addr;
259 fd93a799 Paul Brook
            if ((addr + mem_size) > high)
260 74287114 ths
                high = addr + mem_size;
261 5fe141fd bellard
262 5fe141fd bellard
            qemu_free(data);
263 9ee3c029 bellard
            data = NULL;
264 5fe141fd bellard
        }
265 5fe141fd bellard
    }
266 3aee288b bellard
    qemu_free(phdr);
267 74287114 ths
    if (lowaddr)
268 82790064 ths
        *lowaddr = (uint64_t)(elf_sword)low;
269 74287114 ths
    if (highaddr)
270 82790064 ths
        *highaddr = (uint64_t)(elf_sword)high;
271 5fe141fd bellard
    return total_size;
272 04d4b0c3 ths
 fail:
273 9ee3c029 bellard
    qemu_free(data);
274 5fe141fd bellard
    qemu_free(phdr);
275 5fe141fd bellard
    return -1;
276 5fe141fd bellard
}