1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 03:05:10 +00:00

Kernel: Simplify ELF loading a bit.

Instead of iterating over the sections and memcpy()ing per-section,
do all the copying based on program headers instead.
This commit is contained in:
Andreas Kling 2019-02-26 15:52:06 +01:00
parent c80182f81f
commit 2e5b9d318f
2 changed files with 5 additions and 41 deletions

View file

@ -46,50 +46,11 @@ bool ELFLoader::layout()
#endif
if (program_header.is_writable()) {
allocate_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.is_readable(), program_header.is_writable());
memcpy(program_header.laddr().as_ptr(), program_header.raw_data(), program_header.size_in_image());
} else {
map_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.offset(), program_header.is_readable(), program_header.is_writable());
}
});
m_image.for_each_section_of_type(SHT_PROGBITS, [] (const ELFImage::Section& section) {
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Copying progbits section: %s\n", section.name());
#endif
if (!section.size())
return true;
char* ptr = (char*)section.address();
if (!ptr) {
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: ignoring section '%s' with null address\n", section.name());
#endif
return true;
}
// If this section isn't writable, it's already mmapped.
if (section.is_writable())
memcpy(ptr, section.raw_data(), section.size());
#ifdef SUPPORT_RELOCATIONS
m_sections.set(section.name(), move(ptr));
#endif
return true;
});
m_image.for_each_section_of_type(SHT_NOBITS, [&failed] (const ELFImage::Section& section) {
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Copying nobits section: %s\n", section.name());
#endif
if (!section.size())
return true;
char* ptr = (char*)section.address();
if (!ptr) {
kprintf("ELFLoader: failed to allocate section '%s'\n", section.name());
failed = true;
return false;
}
memset(ptr, 0, section.size());
#ifdef SUPPORT_RELOCATIONS
m_sections.set(section.name(), move(ptr));
#endif
return true;
});
return !failed;
}