root / elf_ops.h @ beb811bd
History | View | Annotate | Download (6.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 | 5fe141fd | bellard | 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 | 5fe141fd | bellard | static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab) |
64 | 5fe141fd | bellard | { |
65 | 5fe141fd | bellard | struct elf_shdr *symtab, *strtab, *shdr_table = NULL; |
66 | 5fe141fd | bellard | struct elf_sym *syms = NULL; |
67 | 5fe141fd | bellard | #if (SZ == 64) |
68 | 5fe141fd | bellard | struct elf32_sym *syms32 = NULL; |
69 | 5fe141fd | bellard | #endif
|
70 | 5fe141fd | bellard | struct syminfo *s;
|
71 | 5fe141fd | bellard | int nsyms, i;
|
72 | 5fe141fd | bellard | char *str = NULL; |
73 | 5fe141fd | bellard | |
74 | 5fe141fd | bellard | shdr_table = load_at(fd, ehdr->e_shoff, |
75 | 5fe141fd | bellard | sizeof(struct elf_shdr) * ehdr->e_shnum); |
76 | 5fe141fd | bellard | if (!shdr_table)
|
77 | 5fe141fd | bellard | return -1; |
78 | 5fe141fd | bellard | |
79 | 5fe141fd | bellard | if (must_swab) {
|
80 | 5fe141fd | bellard | for (i = 0; i < ehdr->e_shnum; i++) { |
81 | 5fe141fd | bellard | glue(bswap_shdr, SZ)(shdr_table + i); |
82 | 5fe141fd | bellard | } |
83 | 5fe141fd | bellard | } |
84 | 5fe141fd | bellard | |
85 | 5fe141fd | bellard | symtab = glue(find_section, SZ)(shdr_table, ehdr->e_shnum, SHT_SYMTAB); |
86 | 5fe141fd | bellard | if (!symtab)
|
87 | 5fe141fd | bellard | goto fail;
|
88 | 5fe141fd | bellard | syms = load_at(fd, symtab->sh_offset, symtab->sh_size); |
89 | 5fe141fd | bellard | if (!syms)
|
90 | 5fe141fd | bellard | goto fail;
|
91 | 5fe141fd | bellard | |
92 | 5fe141fd | bellard | nsyms = symtab->sh_size / sizeof(struct elf_sym); |
93 | 5fe141fd | bellard | #if (SZ == 64) |
94 | 5fe141fd | bellard | syms32 = qemu_mallocz(nsyms * sizeof(struct elf32_sym)); |
95 | 5fe141fd | bellard | #endif
|
96 | 5fe141fd | bellard | for (i = 0; i < nsyms; i++) { |
97 | 5fe141fd | bellard | if (must_swab)
|
98 | 5fe141fd | bellard | glue(bswap_sym, SZ)(&syms[i]); |
99 | 5fe141fd | bellard | #if (SZ == 64) |
100 | 5fe141fd | bellard | syms32[i].st_name = syms[i].st_name; |
101 | 5fe141fd | bellard | syms32[i].st_info = syms[i].st_info; |
102 | 5fe141fd | bellard | syms32[i].st_other = syms[i].st_other; |
103 | 5fe141fd | bellard | syms32[i].st_shndx = syms[i].st_shndx; |
104 | 5fe141fd | bellard | syms32[i].st_value = syms[i].st_value & 0xffffffff;
|
105 | 5fe141fd | bellard | syms32[i].st_size = syms[i].st_size & 0xffffffff;
|
106 | 5fe141fd | bellard | #endif
|
107 | 5fe141fd | bellard | } |
108 | 5fe141fd | bellard | /* String table */
|
109 | 5fe141fd | bellard | if (symtab->sh_link >= ehdr->e_shnum)
|
110 | 5fe141fd | bellard | goto fail;
|
111 | 5fe141fd | bellard | strtab = &shdr_table[symtab->sh_link]; |
112 | 5fe141fd | bellard | |
113 | 5fe141fd | bellard | str = load_at(fd, strtab->sh_offset, strtab->sh_size); |
114 | 5fe141fd | bellard | if (!str)
|
115 | 5fe141fd | bellard | goto fail;
|
116 | 5fe141fd | bellard | |
117 | 5fe141fd | bellard | /* Commit */
|
118 | 5fe141fd | bellard | s = qemu_mallocz(sizeof(*s));
|
119 | 5fe141fd | bellard | #if (SZ == 64) |
120 | 5fe141fd | bellard | s->disas_symtab = syms32; |
121 | 5fe141fd | bellard | qemu_free(syms); |
122 | 5fe141fd | bellard | #else
|
123 | 5fe141fd | bellard | s->disas_symtab = syms; |
124 | 5fe141fd | bellard | #endif
|
125 | 5fe141fd | bellard | s->disas_num_syms = nsyms; |
126 | 5fe141fd | bellard | s->disas_strtab = str; |
127 | 5fe141fd | bellard | s->next = syminfos; |
128 | 5fe141fd | bellard | syminfos = s; |
129 | 5fe141fd | bellard | qemu_free(shdr_table); |
130 | 5fe141fd | bellard | return 0; |
131 | 5fe141fd | bellard | fail:
|
132 | 5fe141fd | bellard | #if (SZ == 64) |
133 | 5fe141fd | bellard | qemu_free(syms32); |
134 | 5fe141fd | bellard | #endif
|
135 | 5fe141fd | bellard | qemu_free(syms); |
136 | 5fe141fd | bellard | qemu_free(str); |
137 | 5fe141fd | bellard | qemu_free(shdr_table); |
138 | 5fe141fd | bellard | return -1; |
139 | 5fe141fd | bellard | } |
140 | 5fe141fd | bellard | |
141 | 9ee3c029 | bellard | int glue(load_elf, SZ)(int fd, int64_t virt_to_phys_addend, |
142 | 74287114 | ths | int must_swab, uint64_t *pentry,
|
143 | 74287114 | ths | uint64_t *lowaddr, uint64_t *highaddr) |
144 | 5fe141fd | bellard | { |
145 | 5fe141fd | bellard | struct elfhdr ehdr;
|
146 | 5fe141fd | bellard | struct elf_phdr *phdr = NULL, *ph; |
147 | 5fe141fd | bellard | int size, i, total_size;
|
148 | 74287114 | ths | elf_word low = 0, high = 0; |
149 | 5fe141fd | bellard | elf_word mem_size, addr; |
150 | 9ee3c029 | bellard | uint8_t *data = NULL;
|
151 | 5fe141fd | bellard | |
152 | 5fe141fd | bellard | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) |
153 | 5fe141fd | bellard | goto fail;
|
154 | 5fe141fd | bellard | if (must_swab) {
|
155 | 5fe141fd | bellard | glue(bswap_ehdr, SZ)(&ehdr); |
156 | 5fe141fd | bellard | } |
157 | 5fe141fd | bellard | |
158 | 9042c0e2 | ths | if (ELF_MACHINE != ehdr.e_machine)
|
159 | 9042c0e2 | ths | goto fail;
|
160 | 9042c0e2 | ths | |
161 | 9ee3c029 | bellard | if (pentry)
|
162 | 9ee3c029 | bellard | *pentry = (uint64_t)ehdr.e_entry; |
163 | 9ee3c029 | bellard | |
164 | 5fe141fd | bellard | glue(load_symbols, SZ)(&ehdr, fd, must_swab); |
165 | 5fe141fd | bellard | |
166 | 5fe141fd | bellard | size = ehdr.e_phnum * sizeof(phdr[0]); |
167 | 5fe141fd | bellard | lseek(fd, ehdr.e_phoff, SEEK_SET); |
168 | 5fe141fd | bellard | phdr = qemu_mallocz(size); |
169 | 5fe141fd | bellard | if (!phdr)
|
170 | 5fe141fd | bellard | goto fail;
|
171 | 5fe141fd | bellard | if (read(fd, phdr, size) != size)
|
172 | 04d4b0c3 | ths | goto fail;
|
173 | 5fe141fd | bellard | if (must_swab) {
|
174 | 5fe141fd | bellard | for(i = 0; i < ehdr.e_phnum; i++) { |
175 | 5fe141fd | bellard | ph = &phdr[i]; |
176 | 5fe141fd | bellard | glue(bswap_phdr, SZ)(ph); |
177 | 5fe141fd | bellard | } |
178 | 5fe141fd | bellard | } |
179 | 5fe141fd | bellard | |
180 | 5fe141fd | bellard | total_size = 0;
|
181 | 5fe141fd | bellard | for(i = 0; i < ehdr.e_phnum; i++) { |
182 | 5fe141fd | bellard | ph = &phdr[i]; |
183 | 5fe141fd | bellard | if (ph->p_type == PT_LOAD) {
|
184 | 5fe141fd | bellard | mem_size = ph->p_memsz; |
185 | 5fe141fd | bellard | /* XXX: avoid allocating */
|
186 | 5fe141fd | bellard | data = qemu_mallocz(mem_size); |
187 | 5fe141fd | bellard | if (ph->p_filesz > 0) { |
188 | 9ee3c029 | bellard | if (lseek(fd, ph->p_offset, SEEK_SET) < 0) |
189 | 04d4b0c3 | ths | goto fail;
|
190 | 5fe141fd | bellard | if (read(fd, data, ph->p_filesz) != ph->p_filesz)
|
191 | 04d4b0c3 | ths | goto fail;
|
192 | 5fe141fd | bellard | } |
193 | 5fe141fd | bellard | addr = ph->p_vaddr + virt_to_phys_addend; |
194 | 5fe141fd | bellard | |
195 | 5fe141fd | bellard | cpu_physical_memory_write_rom(addr, data, mem_size); |
196 | 5fe141fd | bellard | |
197 | 5fe141fd | bellard | total_size += mem_size; |
198 | 74287114 | ths | if (!low || addr < low)
|
199 | 74287114 | ths | low = addr; |
200 | 74287114 | ths | if (!high || (addr + mem_size) > high)
|
201 | 74287114 | ths | high = addr + mem_size; |
202 | 5fe141fd | bellard | |
203 | 5fe141fd | bellard | qemu_free(data); |
204 | 9ee3c029 | bellard | data = NULL;
|
205 | 5fe141fd | bellard | } |
206 | 5fe141fd | bellard | } |
207 | 3aee288b | bellard | qemu_free(phdr); |
208 | 74287114 | ths | if (lowaddr)
|
209 | 74287114 | ths | *lowaddr = (uint64_t)low; |
210 | 74287114 | ths | if (highaddr)
|
211 | 74287114 | ths | *highaddr = (uint64_t)high; |
212 | 5fe141fd | bellard | return total_size;
|
213 | 04d4b0c3 | ths | fail:
|
214 | 9ee3c029 | bellard | qemu_free(data); |
215 | 5fe141fd | bellard | qemu_free(phdr); |
216 | 5fe141fd | bellard | return -1; |
217 | 5fe141fd | bellard | } |