mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:27:35 +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:
parent
c80182f81f
commit
2e5b9d318f
2 changed files with 5 additions and 41 deletions
|
@ -47,7 +47,8 @@ public:
|
||||||
class ProgramHeader {
|
class ProgramHeader {
|
||||||
public:
|
public:
|
||||||
ProgramHeader(const ELFImage& image, unsigned program_header_index)
|
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)
|
, m_program_header_index(program_header_index)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -64,7 +65,9 @@ public:
|
||||||
bool is_readable() const { return flags() & PF_R; }
|
bool is_readable() const { return flags() & PF_R; }
|
||||||
bool is_writable() const { return flags() & PF_W; }
|
bool is_writable() const { return flags() & PF_W; }
|
||||||
bool is_executable() const { return flags() & PF_X; }
|
bool is_executable() const { return flags() & PF_X; }
|
||||||
|
const char* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
|
||||||
private:
|
private:
|
||||||
|
const ELFImage& m_image;
|
||||||
const Elf32_Phdr& m_program_header;
|
const Elf32_Phdr& m_program_header;
|
||||||
unsigned m_program_header_index { 0 };
|
unsigned m_program_header_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
|
@ -46,50 +46,11 @@ bool ELFLoader::layout()
|
||||||
#endif
|
#endif
|
||||||
if (program_header.is_writable()) {
|
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());
|
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 {
|
} 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());
|
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;
|
return !failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue