1 static void glue(bswap_ehdr
, SZ
)(struct elfhdr
*ehdr
)
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 */
18 static void glue(bswap_phdr
, SZ
)(struct elf_phdr
*phdr
)
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 */
30 static void glue(bswap_shdr
, SZ
)(struct elf_shdr
*shdr
)
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
);
44 static void glue(bswap_sym
, SZ
)(struct elf_sym
*sym
)
46 bswap32s(&sym
->st_name
);
47 bswapSZs(&sym
->st_value
);
48 bswapSZs(&sym
->st_size
);
49 bswap16s(&sym
->st_shndx
);
52 static struct elf_shdr
*glue(find_section
, SZ
)(struct elf_shdr
*shdr_table
,
57 if (shdr_table
[i
].sh_type
== type
)
58 return shdr_table
+ i
;
63 static int glue(symfind
, SZ
)(const void *s0
, const void *s1
)
65 struct elf_sym
*key
= (struct elf_sym
*)s0
;
66 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
68 if (key
->st_value
< sym
->st_value
) {
70 } else if (key
->st_value
>= sym
->st_value
+ sym
->st_size
) {
76 static const char *glue(lookup_symbol
, SZ
)(struct syminfo
*s
,
77 target_phys_addr_t orig_addr
)
79 struct elf_sym
*syms
= glue(s
->disas_symtab
.elf
, SZ
);
83 key
.st_value
= orig_addr
;
85 sym
= bsearch(&key
, syms
, s
->disas_num_syms
, sizeof(*syms
), glue(symfind
, SZ
));
87 return s
->disas_strtab
+ sym
->st_name
;
93 static int glue(symcmp
, SZ
)(const void *s0
, const void *s1
)
95 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
96 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
97 return (sym0
->st_value
< sym1
->st_value
)
99 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
102 static int glue(load_symbols
, SZ
)(struct elfhdr
*ehdr
, int fd
, int must_swab
,
105 struct elf_shdr
*symtab
, *strtab
, *shdr_table
= NULL
;
106 struct elf_sym
*syms
= NULL
;
111 shdr_table
= load_at(fd
, ehdr
->e_shoff
,
112 sizeof(struct elf_shdr
) * ehdr
->e_shnum
);
117 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
118 glue(bswap_shdr
, SZ
)(shdr_table
+ i
);
122 symtab
= glue(find_section
, SZ
)(shdr_table
, ehdr
->e_shnum
, SHT_SYMTAB
);
125 syms
= load_at(fd
, symtab
->sh_offset
, symtab
->sh_size
);
129 nsyms
= symtab
->sh_size
/ sizeof(struct elf_sym
);
134 glue(bswap_sym
, SZ
)(&syms
[i
]);
135 /* We are only interested in function symbols.
136 Throw everything else away. */
137 if (syms
[i
].st_shndx
== SHN_UNDEF
||
138 syms
[i
].st_shndx
>= SHN_LORESERVE
||
139 ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
142 syms
[i
] = syms
[nsyms
];
147 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
148 syms
[i
].st_value
&= ~(glue(glue(Elf
, SZ
), _Addr
))1;
153 syms
= qemu_realloc(syms
, nsyms
* sizeof(*syms
));
155 qsort(syms
, nsyms
, sizeof(*syms
), glue(symcmp
, SZ
));
156 for (i
= 0; i
< nsyms
- 1; i
++) {
157 if (syms
[i
].st_size
== 0) {
158 syms
[i
].st_size
= syms
[i
+ 1].st_value
- syms
[i
].st_value
;
167 if (symtab
->sh_link
>= ehdr
->e_shnum
)
169 strtab
= &shdr_table
[symtab
->sh_link
];
171 str
= load_at(fd
, strtab
->sh_offset
, strtab
->sh_size
);
176 s
= qemu_mallocz(sizeof(*s
));
177 s
->lookup_symbol
= glue(lookup_symbol
, SZ
);
178 glue(s
->disas_symtab
.elf
, SZ
) = syms
;
179 s
->disas_num_syms
= nsyms
;
180 s
->disas_strtab
= str
;
183 qemu_free(shdr_table
);
188 qemu_free(shdr_table
);
192 static int glue(load_elf
, SZ
)(const char *name
, int fd
,
193 uint64_t (*translate_fn
)(void *, uint64_t),
194 void *translate_opaque
,
195 int must_swab
, uint64_t *pentry
,
196 uint64_t *lowaddr
, uint64_t *highaddr
,
197 int elf_machine
, int clear_lsb
)
200 struct elf_phdr
*phdr
= NULL
, *ph
;
201 int size
, i
, total_size
;
203 uint64_t addr
, low
= (uint64_t)-1, high
= 0;
204 uint8_t *data
= NULL
;
207 if (read(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
))
210 glue(bswap_ehdr
, SZ
)(&ehdr
);
213 switch (elf_machine
) {
215 if (EM_PPC64
!= ehdr
.e_machine
)
216 if (EM_PPC
!= ehdr
.e_machine
)
220 if (EM_X86_64
!= ehdr
.e_machine
)
221 if (EM_386
!= ehdr
.e_machine
)
225 if (EM_MICROBLAZE
!= ehdr
.e_machine
)
226 if (EM_MICROBLAZE_OLD
!= ehdr
.e_machine
)
230 if (elf_machine
!= ehdr
.e_machine
)
235 *pentry
= (uint64_t)(elf_sword
)ehdr
.e_entry
;
237 glue(load_symbols
, SZ
)(&ehdr
, fd
, must_swab
, clear_lsb
);
239 size
= ehdr
.e_phnum
* sizeof(phdr
[0]);
240 lseek(fd
, ehdr
.e_phoff
, SEEK_SET
);
241 phdr
= qemu_mallocz(size
);
244 if (read(fd
, phdr
, size
) != size
)
247 for(i
= 0; i
< ehdr
.e_phnum
; i
++) {
249 glue(bswap_phdr
, SZ
)(ph
);
254 for(i
= 0; i
< ehdr
.e_phnum
; i
++) {
256 if (ph
->p_type
== PT_LOAD
) {
257 mem_size
= ph
->p_memsz
;
258 /* XXX: avoid allocating */
259 data
= qemu_mallocz(mem_size
);
260 if (ph
->p_filesz
> 0) {
261 if (lseek(fd
, ph
->p_offset
, SEEK_SET
) < 0)
263 if (read(fd
, data
, ph
->p_filesz
) != ph
->p_filesz
)
266 /* address_offset is hack for kernel images that are
267 linked at the wrong physical address. */
269 addr
= translate_fn(translate_opaque
, ph
->p_paddr
);
274 snprintf(label
, sizeof(label
), "phdr #%d: %s", i
, name
);
275 rom_add_blob_fixed(label
, data
, mem_size
, addr
);
277 total_size
+= mem_size
;
280 if ((addr
+ mem_size
) > high
)
281 high
= addr
+ mem_size
;
289 *lowaddr
= (uint64_t)(elf_sword
)low
;
291 *highaddr
= (uint64_t)(elf_sword
)high
;