1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:58:11 +00:00

Loader: Add dynamic loader program

The dynamic loader exists as /usr/lib/Loader.so and is loaded by the
kernel when ET_DYN programs are executed.

The dynamic loader is responsible for loading the dependencies of the
main program, allocating TLS storage, preparing all loaded objects for
execution and finally jumping to the entry of the main program.
This commit is contained in:
Itamar 2020-10-10 18:17:49 +03:00 committed by Andreas Kling
parent 781aa424a9
commit 07b4957361
18 changed files with 962 additions and 104 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -49,12 +50,12 @@ public:
// Load a full ELF image from file into the current process and create an DynamicObject
// from the SHT_DYNAMIC in the file.
bool load_from_image(unsigned flags);
RefPtr<DynamicObject> load_from_image(unsigned flags, size_t total_tls_size);
// Stage 2 of loading: relocations and init functions
// Assumes that the program headers have been loaded and that m_dynamic_object is initialized
// Splitting loading like this allows us to use the same code to relocate a main executable as an elf binary
bool load_stage_2(unsigned flags);
bool load_stage_2(unsigned flags, size_t total_tls_size);
// Intended for use by dlsym or other internal methods
void* symbol_for_name(const char*);
@ -67,6 +68,21 @@ public:
// Requested program interpreter from program headers. May be empty string
StringView program_interpreter() const { return m_program_interpreter; }
VirtualAddress text_segment_load_addresss() const { return m_text_segment_load_address; }
void set_tls_offset(size_t offset) { m_tls_offset = offset; };
size_t tls_size() const { return m_tls_size; }
size_t tls_offset() const { return m_tls_offset; }
const ELF::Image& image() const { return m_elf_image; }
template<typename F>
void for_each_needed_library(F) const;
using SymbolLookupFunction = DynamicObject::SymbolLookupResult (*)(const char*);
SymbolLookupFunction m_global_symbol_lookup_func { nullptr };
VirtualAddress text_segment_load_address() const { return m_text_segment_load_address; }
private:
class ProgramHeaderRegion {
public:
@ -94,31 +110,48 @@ 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);
RefPtr<DynamicObject> dynamic_object_from_image() const;
explicit DynamicLoader(const char* filename, int fd, size_t file_size);
explicit DynamicLoader(Elf32_Dyn* dynamic_location, Elf32_Addr load_address);
// Stage 1
void load_program_headers(const Image& elf_image);
void load_program_headers();
// Stage 2
void do_relocations();
void do_relocations(size_t total_tls_size);
void setup_plt_trampoline();
void call_object_init_functions();
bool validate();
size_t calculate_tls_size() const;
DynamicObject::SymbolLookupResult lookup_symbol(const ELF::DynamicObject::Symbol& symbol) const;
String m_filename;
String m_program_interpreter;
size_t m_file_size { 0 };
int m_image_fd { -1 };
void* m_file_mapping { MAP_FAILED };
ELF::Image m_elf_image;
bool m_valid { true };
OwnPtr<DynamicObject> m_dynamic_object;
RefPtr<DynamicObject> m_dynamic_object;
VirtualAddress m_text_segment_load_address;
size_t m_text_segment_size { 0 };
VirtualAddress m_tls_segment_address;
VirtualAddress m_dynamic_section_address;
size_t m_tls_offset { 0 };
size_t m_tls_size { 0 };
};
template<typename F>
void DynamicLoader::for_each_needed_library(F func) const
{
dynamic_object_from_image()->for_each_needed_library(move(func));
}
} // end namespace ELF