rem: size_t totalSize; status_t status;
add: preloaded_image *image;
context: elf_load_image(int fd, preloaded_image *image){	size_t totalSize;	status_t status;	TRACE(("elf_load_image(fd = %d, image = %p)\n", fd, image));	struct Elf32_Ehdr &elfHeader = image->elf_header;	ssize_t length = read_pos(fd, 0, &elfHeader, sizeof(Elf32_Ehdr));	if (length < (ssize_t)sizeof(Elf32_Ehdr))		return B_BAD_TYPE;	status = verify_elf_header(elfHeader);	if (status < B_OK)		return status;	ssize_t size = elfHeader.e_phnum * elfHeader.e_phentsize;	Elf32_Phdr *programHeaders = (struct Elf32_Phdr *)malloc(size);	if (programHeaders == NULL) {		dprintf("error allocating space for program headers\n");		return B_NO_MEMORY;	}	length = read_pos(fd, elfHeader.e_phoff, programHeaders, size);	if (length < size) {		TRACE(("error reading in program headers\n"));		status = B_ERROR;		goto error1;	}	// create an area large enough to hold the image	image->data_region.size = 0;	image->text_region.size = 0;	for (int32 i = 0; i < elfHeader.e_phnum; i++) {		Elf32_Phdr &header = programHeaders[i];		switch (header.p_type) {			case PT_LOAD:				break;			case PT_DYNAMIC:				image->dynamic_section.start = header.p_vaddr;				image->dynamic_section.size = header.p_memsz;				continue;			default:				dprintf("unhandled pheader type 0x%lx\n", header.p_type);				continue;		}		elf_region *region;		if (header.IsReadWrite()) {			if (image->data_region.size != 0) {				dprintf("elf: rw already handled!\n");				continue;			}			region = &image->data_region;		} else if (header.IsExecutable()) {			if (image->text_region.size != 0) {				dprintf("elf: ro already handled!\n");				continue;			}			region = &image->text_region;		} else			continue;		region->start = ROUNDOWN(header.p_vaddr, B_PAGE_SIZE);		region->size = ROUNDUP(header.p_memsz + (header.p_vaddr % B_PAGE_SIZE), B_PAGE_SIZE);		region->delta = -region->start;		TRACE(("segment %d: start = %p, size = %lu, delta = %lx\n", i,			region->start, region->size, region->delta));	}	// if image->text_region.start == NULL (image is relocatable), 	// platform_allocate_region() automatically allocates an address	totalSize = image->text_region.size + image->data_region.size;	if (platform_allocate_region((void **)&image->text_region.start, totalSize,			B_READ_AREA | B_WRITE_AREA) < B_OK) {		status = B_NO_MEMORY;		goto error1;	}	// initialize the region pointers to the allocated region	// (text region comes first)	image->data_region.start = image->text_region.start + image->text_region.size;	image->data_region.delta += image->data_region.start;	image->text_region.delta += image->text_region.start;	// load program data	for (int i = 0; i < elfHeader.e_phnum; i++) {		Elf32_Phdr &header = programHeaders[i];		if (header.p_type != PT_LOAD)			continue;		elf_region *region;		if (header.IsReadWrite())			region = &image->data_region;		else if (header.IsExecutable())			region = &image->text_region;		else			continue;		TRACE(("load segment %d (%ld bytes)...\n", i, header.p_filesz));		length = read_pos(fd, header.p_offset,			(void *)(region->start + (header.p_vaddr % B_PAGE_SIZE)), header.p_filesz);		if (length < (ssize_t)header.p_filesz) {			status = B_BAD_DATA;			dprintf("error reading in seg %d\n", i);			goto error2;		}		// clear anything above the file size (that may also contain the BSS area)		uint32 offset = (header.p_vaddr % B_PAGE_SIZE) + header.p_filesz;		if (offset < region->size)			memset((void *)(region->start + offset), 0, region->size - offset);	}	// modify the dynamic section by the delta of the regions	image->dynamic_section.start += image->text_region.delta;	image->num_debug_symbols = 0;	image->debug_symbols = NULL;	image->debug_string_table = NULL;	// ToDo: this should be enabled by kernel settings!	if (1)		load_elf_symbol_table(fd, image);	free(programHeaders);	return B_OK;error2:	if (image->text_region.start != NULL)		platform_free_region((void *)image->text_region.start, totalSize);error1:	free(programHeaders);	return status;}