]>
git.proxmox.com Git - qemu.git/blob - hw/elf_ops.h
2 static void glue(bswap_ehdr
, SZ
)(struct elfhdr
*ehdr
)
4 bswap16s(&ehdr
->e_type
); /* Object file type */
5 bswap16s(&ehdr
->e_machine
); /* Architecture */
6 bswap32s(&ehdr
->e_version
); /* Object file version */
7 bswapSZs(&ehdr
->e_entry
); /* Entry point virtual address */
8 bswapSZs(&ehdr
->e_phoff
); /* Program header table file offset */
9 bswapSZs(&ehdr
->e_shoff
); /* Section header table file offset */
10 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
11 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
12 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
13 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
14 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
15 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
16 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
19 static void glue(bswap_phdr
, SZ
)(struct elf_phdr
*phdr
)
21 bswap32s(&phdr
->p_type
); /* Segment type */
22 bswapSZs(&phdr
->p_offset
); /* Segment file offset */
23 bswapSZs(&phdr
->p_vaddr
); /* Segment virtual address */
24 bswapSZs(&phdr
->p_paddr
); /* Segment physical address */
25 bswapSZs(&phdr
->p_filesz
); /* Segment size in file */
26 bswapSZs(&phdr
->p_memsz
); /* Segment size in memory */
27 bswap32s(&phdr
->p_flags
); /* Segment flags */
28 bswapSZs(&phdr
->p_align
); /* Segment alignment */
31 static void glue(bswap_shdr
, SZ
)(struct elf_shdr
*shdr
)
33 bswap32s(&shdr
->sh_name
);
34 bswap32s(&shdr
->sh_type
);
35 bswapSZs(&shdr
->sh_flags
);
36 bswapSZs(&shdr
->sh_addr
);
37 bswapSZs(&shdr
->sh_offset
);
38 bswapSZs(&shdr
->sh_size
);
39 bswap32s(&shdr
->sh_link
);
40 bswap32s(&shdr
->sh_info
);
41 bswapSZs(&shdr
->sh_addralign
);
42 bswapSZs(&shdr
->sh_entsize
);
45 static void glue(bswap_sym
, SZ
)(struct elf_sym
*sym
)
47 bswap32s(&sym
->st_name
);
48 bswapSZs(&sym
->st_value
);
49 bswapSZs(&sym
->st_size
);
50 bswap16s(&sym
->st_shndx
);
54 static int glue(find_phdr
, SZ
)(struct elfhdr
*ehdr
, int fd
, struct elf_phdr
*phdr
, elf_word type
)
58 retval
= lseek(fd
, ehdr
->e_phoff
, SEEK_SET
);
62 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
63 retval
= read(fd
, phdr
, sizeof(*phdr
));
66 glue(bswap_phdr
, SZ
)(phdr
);
67 if (phdr
->p_type
== type
)
73 static void * glue(find_shdr
, SZ
)(struct elfhdr
*ehdr
, int fd
, struct elf_shdr
*shdr
, elf_word type
)
77 retval
= lseek(fd
, ehdr
->e_shoff
, SEEK_SET
);
81 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
82 retval
= read(fd
, shdr
, sizeof(*shdr
));
85 glue(bswap_shdr
, SZ
)(shdr
);
86 if (shdr
->sh_type
== type
)
87 return qemu_malloc(shdr
->sh_size
);
92 static void * glue(find_strtab
, SZ
)(struct elfhdr
*ehdr
, int fd
, struct elf_shdr
*shdr
, struct elf_shdr
*symtab
)
96 retval
= lseek(fd
, ehdr
->e_shoff
+ sizeof(struct elf_shdr
) * symtab
->sh_link
, SEEK_SET
);
100 retval
= read(fd
, shdr
, sizeof(*shdr
));
103 glue(bswap_shdr
, SZ
)(shdr
);
104 if (shdr
->sh_type
== SHT_STRTAB
)
105 return qemu_malloc(shdr
->sh_size
);;
109 static int glue(read_program
, SZ
)(int fd
, struct elf_phdr
*phdr
, void *dst
, elf_word entry
)
112 retval
= lseek(fd
, phdr
->p_offset
+ entry
- phdr
->p_vaddr
, SEEK_SET
);
115 return read(fd
, dst
, phdr
->p_filesz
);
118 static int glue(read_section
, SZ
)(int fd
, struct elf_shdr
*s
, void *dst
)
122 retval
= lseek(fd
, s
->sh_offset
, SEEK_SET
);
125 retval
= read(fd
, dst
, s
->sh_size
);
131 static void * glue(process_section
, SZ
)(struct elfhdr
*ehdr
, int fd
, struct elf_shdr
*shdr
, elf_word type
)
135 dst
= glue(find_shdr
, SZ
)(ehdr
, fd
, shdr
, type
);
139 if (glue(read_section
, SZ
)(fd
, shdr
, dst
))
147 static void * glue(process_strtab
, SZ
)(struct elfhdr
*ehdr
, int fd
, struct elf_shdr
*shdr
, struct elf_shdr
*symtab
)
151 dst
= glue(find_strtab
, SZ
)(ehdr
, fd
, shdr
, symtab
);
155 if (glue(read_section
, SZ
)(fd
, shdr
, dst
))
163 static void glue(load_symbols
, SZ
)(struct elfhdr
*ehdr
, int fd
)
165 struct elf_shdr symtab
, strtab
;
166 struct elf_sym
*syms
;
168 struct elf32_sym
*syms32
;
175 syms
= glue(process_section
, SZ
)(ehdr
, fd
, &symtab
, SHT_SYMTAB
);
179 nsyms
= symtab
.sh_size
/ sizeof(struct elf_sym
);
181 syms32
= qemu_mallocz(nsyms
* sizeof(struct elf32_sym
));
183 for (i
= 0; i
< nsyms
; i
++) {
184 glue(bswap_sym
, SZ
)(&syms
[i
]);
186 syms32
[i
].st_name
= syms
[i
].st_name
;
187 syms32
[i
].st_info
= syms
[i
].st_info
;
188 syms32
[i
].st_other
= syms
[i
].st_other
;
189 syms32
[i
].st_shndx
= syms
[i
].st_shndx
;
190 syms32
[i
].st_value
= syms
[i
].st_value
& 0xffffffff;
191 syms32
[i
].st_size
= syms
[i
].st_size
& 0xffffffff;
195 str
= glue(process_strtab
, SZ
)(ehdr
, fd
, &strtab
, &symtab
);
200 s
= qemu_mallocz(sizeof(*s
));
202 s
->disas_symtab
= syms32
;
205 s
->disas_symtab
= syms
;
207 s
->disas_num_syms
= nsyms
;
208 s
->disas_strtab
= str
;