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
Notes: sideshowbarker 2024-07-19 15:37:13 +09:00
2 changed files with 5 additions and 41 deletions

View file

@ -47,7 +47,8 @@ public:
class ProgramHeader {
public:
ProgramHeader(const ELFImage& image, unsigned program_header_index)
: m_program_header(image.program_header_internal(program_header_index))
: m_image(image)
, m_program_header(image.program_header_internal(program_header_index))
, m_program_header_index(program_header_index)
{
}
@ -64,7 +65,9 @@ public:
bool is_readable() const { return flags() & PF_R; }
bool is_writable() const { return flags() & PF_W; }
bool is_executable() const { return flags() & PF_X; }
const char* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
private:
const ELFImage& m_image;
const Elf32_Phdr& m_program_header;
unsigned m_program_header_index { 0 };
};

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;
}