]> git.proxmox.com Git - qemu.git/blame - include/hw/elf_ops.h
vfio-pci: Fix multifunction=on
[qemu.git] / include / hw / elf_ops.h
CommitLineData
5fe141fd
FB
1static void glue(bswap_ehdr, SZ)(struct elfhdr *ehdr)
2{
3 bswap16s(&ehdr->e_type); /* Object file type */
4 bswap16s(&ehdr->e_machine); /* Architecture */
5 bswap32s(&ehdr->e_version); /* Object file version */
6 bswapSZs(&ehdr->e_entry); /* Entry point virtual address */
7 bswapSZs(&ehdr->e_phoff); /* Program header table file offset */
8 bswapSZs(&ehdr->e_shoff); /* Section header table file offset */
9 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
10 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
11 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
12 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
13 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
14 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
15 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
16}
17
18static void glue(bswap_phdr, SZ)(struct elf_phdr *phdr)
19{
20 bswap32s(&phdr->p_type); /* Segment type */
21 bswapSZs(&phdr->p_offset); /* Segment file offset */
22 bswapSZs(&phdr->p_vaddr); /* Segment virtual address */
23 bswapSZs(&phdr->p_paddr); /* Segment physical address */
24 bswapSZs(&phdr->p_filesz); /* Segment size in file */
25 bswapSZs(&phdr->p_memsz); /* Segment size in memory */
26 bswap32s(&phdr->p_flags); /* Segment flags */
27 bswapSZs(&phdr->p_align); /* Segment alignment */
28}
29
30static void glue(bswap_shdr, SZ)(struct elf_shdr *shdr)
31{
32 bswap32s(&shdr->sh_name);
33 bswap32s(&shdr->sh_type);
34 bswapSZs(&shdr->sh_flags);
35 bswapSZs(&shdr->sh_addr);
36 bswapSZs(&shdr->sh_offset);
37 bswapSZs(&shdr->sh_size);
38 bswap32s(&shdr->sh_link);
39 bswap32s(&shdr->sh_info);
40 bswapSZs(&shdr->sh_addralign);
41 bswapSZs(&shdr->sh_entsize);
42}
43
44static void glue(bswap_sym, SZ)(struct elf_sym *sym)
45{
46 bswap32s(&sym->st_name);
47 bswapSZs(&sym->st_value);
48 bswapSZs(&sym->st_size);
49 bswap16s(&sym->st_shndx);
50}
51
5fafdf24 52static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
5fe141fd
FB
53 int n, int type)
54{
55 int i;
56 for(i=0;i<n;i++) {
57 if (shdr_table[i].sh_type == type)
58 return shdr_table + i;
59 }
60 return NULL;
61}
62
49918a75
PB
63static int glue(symfind, SZ)(const void *s0, const void *s1)
64{
a8170e5e 65 hwaddr addr = *(hwaddr *)s0;
49918a75
PB
66 struct elf_sym *sym = (struct elf_sym *)s1;
67 int result = 0;
c7c530cd 68 if (addr < sym->st_value) {
49918a75 69 result = -1;
c7c530cd 70 } else if (addr >= sym->st_value + sym->st_size) {
49918a75
PB
71 result = 1;
72 }
73 return result;
74}
75
ca20cf32 76static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
a8170e5e 77 hwaddr orig_addr)
49918a75
PB
78{
79 struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
49918a75
PB
80 struct elf_sym *sym;
81
c7c530cd
SW
82 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms),
83 glue(symfind, SZ));
660f11be 84 if (sym != NULL) {
49918a75
PB
85 return s->disas_strtab + sym->st_name;
86 }
87
88 return "";
89}
90
91static int glue(symcmp, SZ)(const void *s0, const void *s1)
92{
93 struct elf_sym *sym0 = (struct elf_sym *)s0;
94 struct elf_sym *sym1 = (struct elf_sym *)s1;
95 return (sym0->st_value < sym1->st_value)
96 ? -1
97 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
98}
99
ca20cf32
BS
100static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
101 int clear_lsb)
5fe141fd
FB
102{
103 struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
104 struct elf_sym *syms = NULL;
5fe141fd
FB
105 struct syminfo *s;
106 int nsyms, i;
107 char *str = NULL;
108
5fafdf24 109 shdr_table = load_at(fd, ehdr->e_shoff,
5fe141fd
FB
110 sizeof(struct elf_shdr) * ehdr->e_shnum);
111 if (!shdr_table)
112 return -1;
3b46e624 113
5fe141fd
FB
114 if (must_swab) {
115 for (i = 0; i < ehdr->e_shnum; i++) {
116 glue(bswap_shdr, SZ)(shdr_table + i);
117 }
118 }
3b46e624 119
5fe141fd
FB
120 symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB);
121 if (!symtab)
122 goto fail;
123 syms = load_at(fd, symtab->sh_offset, symtab->sh_size);
124 if (!syms)
125 goto fail;
126
127 nsyms = symtab->sh_size / sizeof(struct elf_sym);
49918a75
PB
128
129 i = 0;
130 while (i < nsyms) {
5fe141fd
FB
131 if (must_swab)
132 glue(bswap_sym, SZ)(&syms[i]);
49918a75
PB
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) {
138 nsyms--;
139 if (i < nsyms) {
140 syms[i] = syms[nsyms];
141 }
142 continue;
143 }
ca20cf32
BS
144 if (clear_lsb) {
145 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
146 syms[i].st_value &= ~(glue(glue(Elf, SZ), _Addr))1;
147 }
49918a75 148 i++;
5fe141fd 149 }
3e372cf8 150 if (nsyms) {
7267c094 151 syms = g_realloc(syms, nsyms * sizeof(*syms));
49918a75 152
3e372cf8 153 qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
e403e433
SW
154 for (i = 0; i < nsyms - 1; i++) {
155 if (syms[i].st_size == 0) {
156 syms[i].st_size = syms[i + 1].st_value - syms[i].st_value;
157 }
158 }
3e372cf8 159 } else {
7267c094 160 g_free(syms);
3e372cf8
AJ
161 syms = NULL;
162 }
49918a75 163
5fe141fd
FB
164 /* String table */
165 if (symtab->sh_link >= ehdr->e_shnum)
166 goto fail;
167 strtab = &shdr_table[symtab->sh_link];
168
169 str = load_at(fd, strtab->sh_offset, strtab->sh_size);
170 if (!str)
49918a75 171 goto fail;
5fe141fd
FB
172
173 /* Commit */
7267c094 174 s = g_malloc0(sizeof(*s));
49918a75
PB
175 s->lookup_symbol = glue(lookup_symbol, SZ);
176 glue(s->disas_symtab.elf, SZ) = syms;
5fe141fd
FB
177 s->disas_num_syms = nsyms;
178 s->disas_strtab = str;
179 s->next = syminfos;
180 syminfos = s;
7267c094 181 g_free(shdr_table);
5fe141fd
FB
182 return 0;
183 fail:
7267c094
AL
184 g_free(syms);
185 g_free(str);
186 g_free(shdr_table);
5fe141fd
FB
187 return -1;
188}
189
409dbce5
AJ
190static int glue(load_elf, SZ)(const char *name, int fd,
191 uint64_t (*translate_fn)(void *, uint64_t),
192 void *translate_opaque,
9596ebb7 193 int must_swab, uint64_t *pentry,
ca20cf32
BS
194 uint64_t *lowaddr, uint64_t *highaddr,
195 int elf_machine, int clear_lsb)
5fe141fd
FB
196{
197 struct elfhdr ehdr;
198 struct elf_phdr *phdr = NULL, *ph;
199 int size, i, total_size;
d60fa42e 200 elf_word mem_size, file_size;
fd93a799 201 uint64_t addr, low = (uint64_t)-1, high = 0;
9ee3c029 202 uint8_t *data = NULL;
45a50b16 203 char label[128];
5fe141fd
FB
204
205 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
206 goto fail;
207 if (must_swab) {
208 glue(bswap_ehdr, SZ)(&ehdr);
209 }
210
ca20cf32 211 switch (elf_machine) {
7f70c937
BS
212 case EM_PPC64:
213 if (EM_PPC64 != ehdr.e_machine)
214 if (EM_PPC != ehdr.e_machine)
215 goto fail;
216 break;
217 case EM_X86_64:
218 if (EM_X86_64 != ehdr.e_machine)
219 if (EM_386 != ehdr.e_machine)
220 goto fail;
221 break;
16f04416
EI
222 case EM_MICROBLAZE:
223 if (EM_MICROBLAZE != ehdr.e_machine)
224 if (EM_MICROBLAZE_OLD != ehdr.e_machine)
225 goto fail;
226 break;
7f70c937 227 default:
ca20cf32 228 if (elf_machine != ehdr.e_machine)
7f70c937
BS
229 goto fail;
230 }
9042c0e2 231
9ee3c029 232 if (pentry)
82790064 233 *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
9ee3c029 234
ca20cf32 235 glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
5fe141fd
FB
236
237 size = ehdr.e_phnum * sizeof(phdr[0]);
238 lseek(fd, ehdr.e_phoff, SEEK_SET);
7267c094 239 phdr = g_malloc0(size);
5fe141fd
FB
240 if (!phdr)
241 goto fail;
242 if (read(fd, phdr, size) != size)
04d4b0c3 243 goto fail;
5fe141fd
FB
244 if (must_swab) {
245 for(i = 0; i < ehdr.e_phnum; i++) {
246 ph = &phdr[i];
247 glue(bswap_phdr, SZ)(ph);
248 }
249 }
3b46e624 250
5fe141fd
FB
251 total_size = 0;
252 for(i = 0; i < ehdr.e_phnum; i++) {
253 ph = &phdr[i];
254 if (ph->p_type == PT_LOAD) {
d60fa42e
FC
255 mem_size = ph->p_memsz; /* Size of the ROM */
256 file_size = ph->p_filesz; /* Size of the allocated data */
257 data = g_malloc0(file_size);
5fe141fd 258 if (ph->p_filesz > 0) {
d60fa42e 259 if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
04d4b0c3 260 goto fail;
d60fa42e
FC
261 }
262 if (read(fd, data, file_size) != file_size) {
04d4b0c3 263 goto fail;
d60fa42e 264 }
5fe141fd 265 }
83c1f87c
PB
266 /* address_offset is hack for kernel images that are
267 linked at the wrong physical address. */
409dbce5
AJ
268 if (translate_fn) {
269 addr = translate_fn(translate_opaque, ph->p_paddr);
270 } else {
271 addr = ph->p_paddr;
272 }
5fe141fd 273
7e9c7ffe
HS
274 /* the entry pointer in the ELF header is a virtual
275 * address, if the text segments paddr and vaddr differ
276 * we need to adjust the entry */
277 if (pentry && !translate_fn &&
278 ph->p_vaddr != ph->p_paddr &&
279 ehdr.e_entry >= ph->p_vaddr &&
280 ehdr.e_entry < ph->p_vaddr + ph->p_filesz &&
281 ph->p_flags & PF_X) {
282 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
283 }
284
45a50b16 285 snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
d60fa42e
FC
286
287 /* rom_add_elf_program() seize the ownership of 'data' */
288 rom_add_elf_program(label, data, file_size, mem_size, addr);
5fe141fd
FB
289
290 total_size += mem_size;
fd93a799 291 if (addr < low)
74287114 292 low = addr;
fd93a799 293 if ((addr + mem_size) > high)
74287114 294 high = addr + mem_size;
5fe141fd 295
9ee3c029 296 data = NULL;
5fe141fd
FB
297 }
298 }
7267c094 299 g_free(phdr);
74287114 300 if (lowaddr)
82790064 301 *lowaddr = (uint64_t)(elf_sword)low;
74287114 302 if (highaddr)
82790064 303 *highaddr = (uint64_t)(elf_sword)high;
5fe141fd 304 return total_size;
04d4b0c3 305 fail:
7267c094
AL
306 g_free(data);
307 g_free(phdr);
5fe141fd
FB
308 return -1;
309}