tools: add elf tool
This commit is contained in:
+3934
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "elf.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FILE* fp;
|
||||
long file_size;
|
||||
|
||||
fp = fopen("testfile", "rb");
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
file_size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
printf("size: %d!!\n", file_size);
|
||||
|
||||
uint8_t* file = (uint8_t*)malloc(file_size);
|
||||
|
||||
fread(file, 1, file_size, fp);
|
||||
|
||||
Elf32_Ehdr *elf_ehdr = (Elf32_Ehdr*)file;
|
||||
|
||||
int program_header_table_entry_count = elf_ehdr->e_phnum;
|
||||
int program_header_table_entry_size = elf_ehdr->e_phentsize;
|
||||
|
||||
printf("magic: %s, type: %d, entry: 0x%X\n", elf_ehdr->e_ident, elf_ehdr->e_type, elf_ehdr->e_entry);
|
||||
printf("PHNUM: %d\n", program_header_table_entry_count);
|
||||
printf("PHENTSIZE: %d\n\n", program_header_table_entry_size);
|
||||
|
||||
for(int i = 0; i < program_header_table_entry_count; i++)
|
||||
{
|
||||
Elf32_Phdr *elf_phdr = (Elf32_Phdr *)((uint32_t)file + elf_ehdr->e_phoff +
|
||||
i * elf_ehdr->e_phentsize);
|
||||
|
||||
printf("HEADER %d, type: 0x%X, vaddr: 0x%X, paddr: 0x%X, memsz: 0x%X\n", i, elf_phdr->p_type, elf_phdr->p_vaddr, elf_phdr->p_paddr, elf_phdr->p_memsz);
|
||||
if(elf_phdr->p_type != PT_LOAD)
|
||||
{
|
||||
printf("not PT_LOAD, skipping...\n");
|
||||
continue;
|
||||
}
|
||||
int pages_needed = elf_phdr->p_memsz / 0x1000;
|
||||
printf("pages needed: %d\n", pages_needed);
|
||||
///////////////////////////
|
||||
//MAP HERE
|
||||
///////////////////////////
|
||||
|
||||
//memcpy(elf_phdr->p_vaddr, file+elf_phdr->p_offset, elf_phdr->p_filesz);
|
||||
|
||||
uint32_t file_start = elf_phdr->p_vaddr + elf_phdr->p_filesz;
|
||||
uint32_t file_end = elf_phdr->p_vaddr + pages_needed * 0x1000;
|
||||
//memset(file_start, 0, file_end - file_start);
|
||||
|
||||
printf("p_filesz: 0x%X (%d)\n", elf_phdr->p_filesz, elf_phdr->p_filesz);
|
||||
|
||||
|
||||
|
||||
printf("================================\n\n\n\n");
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
free(file);
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user