ELFLoader: Make check_elf_image return how much memory the executable will use on success

This commit is contained in:
apio 2022-10-12 19:20:14 +02:00
parent 4091799701
commit 9cddf9485d
2 changed files with 7 additions and 5 deletions

View File

@ -8,6 +8,6 @@ namespace ELFLoader
ELFImage* load_elf_from_vfs(VFS::Node* node); // This function assumes check_elf_image has been called first. ELFImage* load_elf_from_vfs(VFS::Node* node); // This function assumes check_elf_image has been called first.
ELFImage* load_elf_from_filesystem(const char* filename); ELFImage* load_elf_from_filesystem(const char* filename);
void release_elf_image(ELFImage* image); void release_elf_image(ELFImage* image);
int check_elf_image(VFS::Node* node); long check_elf_image(VFS::Node* node);
int check_elf_image_from_filesystem(const char* filename); long check_elf_image_from_filesystem(const char* filename);
} }

View File

@ -43,7 +43,7 @@ ELFImage* ELFLoader::load_elf_from_filesystem(const char* filename)
return result; return result;
} }
int ELFLoader::check_elf_image_from_filesystem(const char* filename) long ELFLoader::check_elf_image_from_filesystem(const char* filename)
{ {
VFS::Node* node = VFS::resolve_path(filename); VFS::Node* node = VFS::resolve_path(filename);
@ -105,7 +105,7 @@ ELFImage* ELFLoader::load_elf_from_vfs(VFS::Node* node)
return image; return image;
} }
int ELFLoader::check_elf_image(VFS::Node* node) long ELFLoader::check_elf_image(VFS::Node* node)
{ {
Elf64_Ehdr elf_ehdr; Elf64_Ehdr elf_ehdr;
if (VFS::read(node, 0, sizeof(elf_ehdr), (char*)&elf_ehdr) < 0) if (VFS::read(node, 0, sizeof(elf_ehdr), (char*)&elf_ehdr) < 0)
@ -145,6 +145,7 @@ int ELFLoader::check_elf_image(VFS::Node* node)
} }
int i; int i;
int loadable_sections = 0; int loadable_sections = 0;
long memusage = 0;
Elf64_Phdr phdr; Elf64_Phdr phdr;
for (VFS::read(node, elf_ehdr.e_phoff, sizeof(Elf64_Phdr), (char*)&phdr), i = 0; i < elf_ehdr.e_phnum; for (VFS::read(node, elf_ehdr.e_phoff, sizeof(Elf64_Phdr), (char*)&phdr), i = 0; i < elf_ehdr.e_phnum;
i++, VFS::read(node, elf_ehdr.e_phoff + (i * elf_ehdr.e_phentsize), sizeof(Elf64_Phdr), (char*)&phdr)) i++, VFS::read(node, elf_ehdr.e_phoff + (i * elf_ehdr.e_phentsize), sizeof(Elf64_Phdr), (char*)&phdr))
@ -162,6 +163,7 @@ int ELFLoader::check_elf_image(VFS::Node* node)
return -1; return -1;
} }
loadable_sections++; loadable_sections++;
memusage += Utilities::get_blocks_from_size(PAGE_SIZE, phdr.p_memsz) * PAGE_SIZE;
} }
} }
if (!loadable_sections) if (!loadable_sections)
@ -169,7 +171,7 @@ int ELFLoader::check_elf_image(VFS::Node* node)
kwarnln("No loadable sections"); kwarnln("No loadable sections");
return -1; return -1;
} }
return 0; return memusage;
} }
void ELFLoader::release_elf_image(ELFImage* image) void ELFLoader::release_elf_image(ELFImage* image)