]> Git Repo - qemu.git/blob - hw/elf_ops.h
Rename target_phys_addr_t to hwaddr
[qemu.git] / hw / elf_ops.h
1 static 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
18 static 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
30 static 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
44 static 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
52 static struct elf_shdr *glue(find_section, SZ)(struct elf_shdr *shdr_table,
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
63 static int glue(symfind, SZ)(const void *s0, const void *s1)
64 {
65     hwaddr addr = *(hwaddr *)s0;
66     struct elf_sym *sym = (struct elf_sym *)s1;
67     int result = 0;
68     if (addr < sym->st_value) {
69         result = -1;
70     } else if (addr >= sym->st_value + sym->st_size) {
71         result = 1;
72     }
73     return result;
74 }
75
76 static const char *glue(lookup_symbol, SZ)(struct syminfo *s,
77                                            hwaddr orig_addr)
78 {
79     struct elf_sym *syms = glue(s->disas_symtab.elf, SZ);
80     struct elf_sym *sym;
81
82     sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms),
83                   glue(symfind, SZ));
84     if (sym != NULL) {
85         return s->disas_strtab + sym->st_name;
86     }
87
88     return "";
89 }
90
91 static 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
100 static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab,
101                                   int clear_lsb)
102 {
103     struct elf_shdr *symtab, *strtab, *shdr_table = NULL;
104     struct elf_sym *syms = NULL;
105     struct syminfo *s;
106     int nsyms, i;
107     char *str = NULL;
108
109     shdr_table = load_at(fd, ehdr->e_shoff,
110                          sizeof(struct elf_shdr) * ehdr->e_shnum);
111     if (!shdr_table)
112         return -1;
113
114     if (must_swab) {
115         for (i = 0; i < ehdr->e_shnum; i++) {
116             glue(bswap_shdr, SZ)(shdr_table + i);
117         }
118     }
119
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);
128
129     i = 0;
130     while (i < nsyms) {
131         if (must_swab)
132             glue(bswap_sym, SZ)(&syms[i]);
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         }
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         }
148         i++;
149     }
150     if (nsyms) {
151         syms = g_realloc(syms, nsyms * sizeof(*syms));
152
153         qsort(syms, nsyms, sizeof(*syms), glue(symcmp, SZ));
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         }
159     } else {
160         g_free(syms);
161         syms = NULL;
162     }
163
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)
171         goto fail;
172
173     /* Commit */
174     s = g_malloc0(sizeof(*s));
175     s->lookup_symbol = glue(lookup_symbol, SZ);
176     glue(s->disas_symtab.elf, SZ) = syms;
177     s->disas_num_syms = nsyms;
178     s->disas_strtab = str;
179     s->next = syminfos;
180     syminfos = s;
181     g_free(shdr_table);
182     return 0;
183  fail:
184     g_free(syms);
185     g_free(str);
186     g_free(shdr_table);
187     return -1;
188 }
189
190 static int glue(load_elf, SZ)(const char *name, int fd,
191                               uint64_t (*translate_fn)(void *, uint64_t),
192                               void *translate_opaque,
193                               int must_swab, uint64_t *pentry,
194                               uint64_t *lowaddr, uint64_t *highaddr,
195                               int elf_machine, int clear_lsb)
196 {
197     struct elfhdr ehdr;
198     struct elf_phdr *phdr = NULL, *ph;
199     int size, i, total_size;
200     elf_word mem_size;
201     uint64_t addr, low = (uint64_t)-1, high = 0;
202     uint8_t *data = NULL;
203     char label[128];
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
211     switch (elf_machine) {
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;
222         case EM_MICROBLAZE:
223             if (EM_MICROBLAZE != ehdr.e_machine)
224                 if (EM_MICROBLAZE_OLD != ehdr.e_machine)
225                     goto fail;
226             break;
227         default:
228             if (elf_machine != ehdr.e_machine)
229                 goto fail;
230     }
231
232     if (pentry)
233         *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
234
235     glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb);
236
237     size = ehdr.e_phnum * sizeof(phdr[0]);
238     lseek(fd, ehdr.e_phoff, SEEK_SET);
239     phdr = g_malloc0(size);
240     if (!phdr)
241         goto fail;
242     if (read(fd, phdr, size) != size)
243         goto fail;
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     }
250
251     total_size = 0;
252     for(i = 0; i < ehdr.e_phnum; i++) {
253         ph = &phdr[i];
254         if (ph->p_type == PT_LOAD) {
255             mem_size = ph->p_memsz;
256             /* XXX: avoid allocating */
257             data = g_malloc0(mem_size);
258             if (ph->p_filesz > 0) {
259                 if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
260                     goto fail;
261                 if (read(fd, data, ph->p_filesz) != ph->p_filesz)
262                     goto fail;
263             }
264             /* address_offset is hack for kernel images that are
265                linked at the wrong physical address.  */
266             if (translate_fn) {
267                 addr = translate_fn(translate_opaque, ph->p_paddr);
268             } else {
269                 addr = ph->p_paddr;
270             }
271
272             /* the entry pointer in the ELF header is a virtual
273              * address, if the text segments paddr and vaddr differ
274              * we need to adjust the entry */
275             if (pentry && !translate_fn &&
276                     ph->p_vaddr != ph->p_paddr &&
277                     ehdr.e_entry >= ph->p_vaddr &&
278                     ehdr.e_entry < ph->p_vaddr + ph->p_filesz &&
279                     ph->p_flags & PF_X) {
280                 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
281             }
282
283             snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
284             rom_add_blob_fixed(label, data, mem_size, addr);
285
286             total_size += mem_size;
287             if (addr < low)
288                 low = addr;
289             if ((addr + mem_size) > high)
290                 high = addr + mem_size;
291
292             g_free(data);
293             data = NULL;
294         }
295     }
296     g_free(phdr);
297     if (lowaddr)
298         *lowaddr = (uint64_t)(elf_sword)low;
299     if (highaddr)
300         *highaddr = (uint64_t)(elf_sword)high;
301     return total_size;
302  fail:
303     g_free(data);
304     g_free(phdr);
305     return -1;
306 }
This page took 0.042554 seconds and 4 git commands to generate.