void PE_(r_bin_pe_check_sections)(struct PE_(r_bin_pe_obj_t)* bin, struct r_bin_pe_section_t* * sects) {
	int i = 0;
	struct r_bin_pe_section_t* sections = *sects;
	ut64 addr_beg, addr_end, new_section_size, new_perm, base_addr;
	struct r_bin_pe_addr_t* entry = PE_(r_bin_pe_get_entrypoint) (bin);

	if (!entry) {
		return;
	}
	new_section_size = bin->size;
	new_section_size -= entry->paddr > bin->size? 0: entry->paddr;
	new_perm = (PE_IMAGE_SCN_MEM_READ | PE_IMAGE_SCN_MEM_WRITE | PE_IMAGE_SCN_MEM_EXECUTE);
	base_addr = PE_(r_bin_pe_get_image_base) (bin);

	for (i = 0; !sections[i].last; i++) {
		//strcmp against .text doesn't work in somes cases
		if (strstr ((const char*) sections[i].name, "text")) {
			bool fix = false;
			int j;
			//check paddr boundaries
			addr_beg = sections[i].paddr;
			addr_end = addr_beg + sections[i].size;
			if (entry->paddr < addr_beg || entry->paddr > addr_end) {
				fix = true;
			}
			//check vaddr boundaries
			addr_beg = sections[i].vaddr + base_addr;
			addr_end = addr_beg + sections[i].vsize;
			if (entry->vaddr < addr_beg || entry->vaddr > addr_end) {
				fix = true;
			}
			//look for other segment with x that is already mapped and hold entrypoint
			for (j = 0; !sections[j].last; j++) {
				if (sections[j].flags & PE_IMAGE_SCN_MEM_EXECUTE) {
					addr_beg = sections[j].paddr;
					addr_end = addr_beg + sections[j].size;
					if (addr_beg <= entry->paddr && entry->paddr < addr_end) {
						if (!sections[j].vsize) {
							sections[j].vsize = sections[j].size;
						}
						addr_beg = sections[j].vaddr + base_addr;
						addr_end = addr_beg + sections[j].vsize;
						if (addr_beg <= entry->vaddr || entry->vaddr < addr_end) {
							fix = false;
							break;
						}
					}
				}

			}
			//if either vaddr or paddr fail we should update this section
			if (fix) {
				strcpy ((char*) sections[i].name, "blob");
				sections[i].paddr = entry->paddr;
				sections[i].vaddr = entry->vaddr - base_addr;
				sections[i].size = sections[i].vsize = new_section_size;
				sections[i].flags = new_perm;
			}
			goto out_function;
		}
	}
	//if we arrive til here means there is no text section find one that is holding the code
	for (i = 0; !sections[i].last; i++) {
		if (sections[i].size > bin->size) {
			continue;
		}
		addr_beg = sections[i].paddr;
		addr_end = addr_beg + sections[i].size;
		if (addr_beg <= entry->paddr && entry->paddr < addr_end) {
			if (!sections[i].vsize) {
				sections[i].vsize = sections[i].size;
			}
			addr_beg = sections[i].vaddr + base_addr;
			addr_end = addr_beg + sections[i].vsize;
			if (entry->vaddr < addr_beg || entry->vaddr > addr_end) {
				sections[i].vaddr = entry->vaddr - base_addr;
			}
			goto out_function;
		}
	}
	//we need to create another section in order to load the entrypoint
	sections = realloc (sections, (bin->num_sections + 2) * sizeof(struct r_bin_pe_section_t));
	i = bin->num_sections;
	sections[i].last = 0;
	strcpy ((char*) sections[i].name, "blob");
	sections[i].paddr = entry->paddr;
	sections[i].vaddr = entry->vaddr - base_addr;
	sections[i].size = sections[i].vsize = new_section_size;
	sections[i].flags = new_perm;
	sections[i + 1].last = 1;
	*sects = sections;
out_function:
	free (entry);
	return;

}
void PE_(r_bin_pe_check_sections)(struct PE_(r_bin_pe_obj_t)* bin, struct r_bin_pe_section_t* * sects) {
	int i = 0;
	struct r_bin_pe_section_t* sections = *sects;
	ut64 addr_beg, addr_end, new_section_size, new_perm, base_addr;
	struct r_bin_pe_addr_t* entry = PE_(r_bin_pe_get_entrypoint) (bin);

	if (!entry) {
		return;
	}
	new_section_size = bin->size;
	new_section_size -= entry->paddr > bin->size? 0: entry->paddr;
	new_perm = (PE_IMAGE_SCN_MEM_READ | PE_IMAGE_SCN_MEM_WRITE | PE_IMAGE_SCN_MEM_EXECUTE);
	base_addr = PE_(r_bin_pe_get_image_base) (bin);

	for (i = 0; !sections[i].last; i++) {
		 
		if (strstr ((const char*) sections[i].name, "text")) {
			bool fix = false;
			int j;
			 
			addr_beg = sections[i].paddr;
			addr_end = addr_beg + sections[i].size;
			if (entry->paddr < addr_beg || entry->paddr > addr_end) {
				fix = true;
			}
			 
			addr_beg = sections[i].vaddr + base_addr;
			addr_end = addr_beg + sections[i].vsize;
			if (entry->vaddr < addr_beg || entry->vaddr > addr_end) {
				fix = true;
			}
			 
			for (j = 0; !sections[j].last; j++) {
				if (sections[j].flags & PE_IMAGE_SCN_MEM_EXECUTE) {
					addr_beg = sections[j].paddr;
					addr_end = addr_beg + sections[j].size;
					if (addr_beg <= entry->paddr && entry->paddr < addr_end) {
						if (!sections[j].vsize) {
							sections[j].vsize = sections[j].size;
						}
						addr_beg = sections[j].vaddr + base_addr;
						addr_end = addr_beg + sections[j].vsize;
						if (addr_beg <= entry->vaddr || entry->vaddr < addr_end) {
							fix = false;
							break;
						}
					}
				}

			}
			 
			if (fix) {
				strcpy ((char*) sections[i].name, "blob");
				sections[i].paddr = entry->paddr;
				sections[i].vaddr = entry->vaddr - base_addr;
				sections[i].size = sections[i].vsize = new_section_size;
				sections[i].flags = new_perm;
			}
			goto out_function;
		}
	}
	 
	for (i = 0; !sections[i].last; i++) {
		if (sections[i].size > bin->size) {
			continue;
		}
		addr_beg = sections[i].paddr;
		addr_end = addr_beg + sections[i].size;
		if (addr_beg <= entry->paddr && entry->paddr < addr_end) {
			if (!sections[i].vsize) {
				sections[i].vsize = sections[i].size;
			}
			addr_beg = sections[i].vaddr + base_addr;
			addr_end = addr_beg + sections[i].vsize;
			if (entry->vaddr < addr_beg || entry->vaddr > addr_end) {
				sections[i].vaddr = entry->vaddr - base_addr;
			}
			goto out_function;
		}
	}
	 
	sections = realloc (sections, (bin->num_sections + 2) * sizeof(struct r_bin_pe_section_t));
	i = bin->num_sections;
	sections[i].last = 0;
	strcpy ((char*) sections[i].name, "blob");
	sections[i].paddr = entry->paddr;
	sections[i].vaddr = entry->vaddr - base_addr;
	sections[i].size = sections[i].vsize = new_section_size;
	sections[i].flags = new_perm;
	sections[i + 1].last = 1;
	*sects = sections;
out_function:
	free (entry);
	return;

}