LibELF: Cache the DynamicObject in DynamicLoader

This avoids reparsing the same dynamic library file multiple times.
This commit is contained in:
Andreas Kling 2021-01-25 13:16:39 +01:00
parent 41d8734288
commit adcc1c1eff
Notes: sideshowbarker 2024-07-18 22:52:01 +09:00
2 changed files with 17 additions and 12 deletions

View file

@ -80,19 +80,22 @@ DynamicLoader::DynamicLoader(const char* filename, int fd, size_t size)
m_valid = validate();
}
RefPtr<DynamicObject> DynamicLoader::dynamic_object_from_image() const
const DynamicObject& DynamicLoader::dynamic_object() const
{
VirtualAddress dynamic_section_address;
if (!m_cached_dynamic_object) {
VirtualAddress dynamic_section_address;
m_elf_image.for_each_program_header([&dynamic_section_address](auto program_header) {
if (program_header.type() == PT_DYNAMIC) {
dynamic_section_address = VirtualAddress(program_header.raw_data());
}
return IterationDecision::Continue;
});
ASSERT(!dynamic_section_address.is_null());
m_elf_image.for_each_program_header([&dynamic_section_address](auto program_header) {
if (program_header.type() == PT_DYNAMIC) {
dynamic_section_address = VirtualAddress(program_header.raw_data());
}
return IterationDecision::Continue;
});
ASSERT(!dynamic_section_address.is_null());
return ELF::DynamicObject::construct(VirtualAddress(m_elf_image.base_address()), dynamic_section_address);
m_cached_dynamic_object = ELF::DynamicObject::construct(VirtualAddress(m_elf_image.base_address()), dynamic_section_address);
}
return *m_cached_dynamic_object;
}
size_t DynamicLoader::calculate_tls_size() const

View file

@ -106,7 +106,7 @@ private:
};
static void* do_mmap(int fd, size_t size, const String& name);
RefPtr<DynamicObject> dynamic_object_from_image() const;
const DynamicObject& dynamic_object() const;
explicit DynamicLoader(const char* filename, int fd, size_t file_size);
@ -153,12 +153,14 @@ private:
size_t m_tls_size { 0 };
Vector<DynamicObject::Relocation> m_unresolved_relocations;
mutable RefPtr<DynamicObject> m_cached_dynamic_object;
};
template<typename F>
void DynamicLoader::for_each_needed_library(F func) const
{
dynamic_object_from_image()->for_each_needed_library(move(func));
dynamic_object().for_each_needed_library(move(func));
}
} // end namespace ELF