1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibELF: Call mmap() before constructing the DynamicLoader object

Refactor DynamicLoader construction with a try_create() helper so that
we can call mmap() before making a loader. This way the loader doesn't
need to have an "mmap failed" state.

This patch also takes care of determining the ELF file size in
try_create() instead of expecting callers to provide it.
This commit is contained in:
Andreas Kling 2021-01-31 10:13:23 +01:00
parent d71bfb9614
commit 68576bcf1b
4 changed files with 54 additions and 71 deletions

View file

@ -42,8 +42,7 @@ namespace ELF {
class DynamicLoader : public RefCounted<DynamicLoader> {
public:
static NonnullRefPtr<DynamicLoader> construct(const char* filename, int fd, size_t file_size);
static RefPtr<DynamicLoader> try_create(int fd, String filename);
~DynamicLoader();
bool is_valid() const { return m_valid; }
@ -79,6 +78,8 @@ public:
bool is_dynamic() const { return m_elf_image.is_dynamic(); }
private:
DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
class ProgramHeaderRegion {
public:
void set_program_header(const Elf32_Phdr& header) { m_program_header = header; }
@ -105,11 +106,8 @@ private:
Elf32_Phdr m_program_header; // Explicitly a copy of the PHDR in the image
};
static void* do_mmap(int fd, size_t size, const String& name);
const DynamicObject& dynamic_object() const;
explicit DynamicLoader(const char* filename, int fd, size_t file_size);
// Stage 1
void load_program_headers();
@ -137,7 +135,7 @@ private:
String m_program_interpreter;
size_t m_file_size { 0 };
int m_image_fd { -1 };
void* m_file_mapping { MAP_FAILED };
void* m_file_data { nullptr };
ELF::Image m_elf_image;
bool m_valid { true };