Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. void *extract_elf(int prog_file, char *buffer_base_ptr, unsigned int size){
  2.     Elf64_Ehdr *header = NULL;
  3.     Elf64_Phdr *pheader = NULL;
  4.     Elf64_Shdr *shdr = NULL;
  5.     Elf64_Sym  *syms = NULL;
  6.     char       *strings = NULL;
  7.     char       *start = NULL;
  8.     char       *taddr = NULL;
  9.     void       *entry = NULL;
  10.     int        seg_size;
  11.     int        mem_size;
  12.     int        seg_offset;    
  13.    
  14.     int i = 0;
  15.     char *seg_mem_location = NULL;
  16.     char *bss_mem_location = NULL;
  17.     header = (Elf64_Ehdr *)buffer_base_ptr;
  18.  
  19.     pheader = (Elf64_Phdr *)(buffer_base_ptr + header->e_phoff);
  20.    
  21.     for(i=0; i < header->e_phnum; ++i){
  22.        
  23.         if(pheader[i].p_type != PT_LOAD){
  24.             /*What is an example of a non-loadable program segment?
  25.             Also, what is a program segment?*/
  26.             continue;
  27.         }
  28.         if(pheader[i].p_filesz == 0){
  29.             continue;
  30.         }
  31.  
  32.         start = buffer_base_ptr + pheader[i].p_offset;
  33.         taddr = pheader[i].p_vaddr;
  34.         seg_size = pheader[i].p_filesz;
  35.         mem_size = pheader[i].p_memsz;
  36.         seg_offset = pheader[i].p_offset;
  37.        
  38.         char* a_taddr = ELF_PAGESTART((unsigned long long)taddr);
  39.         int addr_diff = (unsigned long long)taddr - (unsigned long long)a_taddr;
  40.         int a_seg_size = ELF_PAGEALIGN(seg_size + addr_diff);
  41.         int a_seg_offset = ELF_PAGEOFFSET(seg_offset);
  42.        
  43.         int offset_diff = seg_offset - a_seg_offset;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement