Exemple #1
0
/* Load the PE file in the bin->mlist */
static void pe_load_mlist(r_binfmt_s *bin) {
  uint32_t flags;
  IMAGE_COFF_HEADER *coff;
  IMAGE_SECTION_HEADER *shdr;
  WORD sections_addr;
  int i;

  bin->mlist = r_binfmt_mlist_new();

  /* Get sections table */
  coff = (IMAGE_COFF_HEADER*)(pe_get_addr_coff(bin) + bin->mapped);
  sections_addr = pe_get_addr_sections(bin);
  shdr = (IMAGE_SECTION_HEADER*)(bin->mapped + sections_addr);


  /* Load each section */
  for(i = 0; i < coff->NumberOfSections; i++) {
    flags = 0;

    if(shdr[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)
      flags |= R_BINFMT_MEM_FLAG_PROT_X;
    if(shdr[i].Characteristics & IMAGE_SCN_MEM_WRITE)
      flags |= R_BINFMT_MEM_FLAG_PROT_W;
    if(shdr[i].Characteristics & IMAGE_SCN_MEM_READ)
      flags |= R_BINFMT_MEM_FLAG_PROT_R;

    if(flags)
      r_binfmt_mlist_add(bin->mlist, shdr[i].VirtualAddress,
			 bin->mapped + shdr[i].PointerToRawData,
			 shdr[i].SizeOfRawData,
			 flags);
  }

}
Exemple #2
0
/* Load the ELF binary in the bin->mlist */
static void elf64_load_mlist(r_binfmt_s *bin) {
  Elf64_Ehdr *ehdr = (Elf64_Ehdr*)bin->mapped;
  Elf64_Phdr *phdr;
  int i;
  uint64_t flags;
  uint64_t p_vaddr, p_offset, p_filesz;
  uint32_t p_type, p_flags;
  uint16_t e_phnum;

  bin->mlist = r_binfmt_mlist_new();

  phdr = (Elf64_Phdr*)(bin->mapped + r_binfmt_get_int64((byte_t*)&ehdr->e_phoff, bin->endian));

  e_phnum = r_binfmt_get_int16((byte_t*)&ehdr->e_phnum, bin->endian);

  for(i = 0; i < e_phnum; i++) {
    p_type = r_binfmt_get_int32((byte_t*)&phdr[i].p_type, bin->endian);
    p_flags = r_binfmt_get_int32((byte_t*)&phdr[i].p_flags, bin->endian);
    p_vaddr = r_binfmt_get_int64((byte_t*)&phdr[i].p_vaddr, bin->endian);
    p_offset = r_binfmt_get_int64((byte_t*)&phdr[i].p_offset, bin->endian);
    p_filesz = r_binfmt_get_int64((byte_t*)&phdr[i].p_filesz, bin->endian);

    if(p_type == PT_LOAD) {

      flags = 0;
      if(p_flags & PF_X)
	flags |= R_BINFMT_MEM_FLAG_PROT_X;
      if(p_flags & PF_R)
	flags |= R_BINFMT_MEM_FLAG_PROT_R;
      if(p_flags & PF_W)
	flags |= R_BINFMT_MEM_FLAG_PROT_W;

      r_binfmt_mlist_add(bin->mlist,
			p_vaddr,
			bin->mapped + p_offset,
			p_filesz,
			flags);
    }
  }
}
Exemple #3
0
static void r_binfmt_macho64_load_segment(r_binfmt_s *bin, r_binfmt_macho64_segment_s *seg) {
  u64 vaddr, filesz, fileoff;
  u32 flags, initprot;

  vaddr    = r_binfmt_get_int64((byte_t*)&seg->vm_addr, bin->endian);
  filesz   = r_binfmt_get_int64((byte_t*)&seg->file_size, bin->endian);
  fileoff  = r_binfmt_get_int64((byte_t*)&seg->file_off, bin->endian);
  initprot = r_binfmt_get_int32((byte_t*)&seg->init_prot, bin->endian);

  flags = 0;
  if(initprot & R_BINFMT_MACHO_PROT_R)
    flags |= R_BINFMT_MEM_FLAG_PROT_R;
  if(initprot & R_BINFMT_MACHO_PROT_W)
    flags |= R_BINFMT_MEM_FLAG_PROT_W;
  if(initprot & R_BINFMT_MACHO_PROT_X)
    flags |= R_BINFMT_MEM_FLAG_PROT_X;

  r_binfmt_mlist_add(bin->mlist,
		     vaddr,
		     bin->mapped + fileoff,
		     filesz,
		     flags);
}