2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-02-01 17:57:12 -07:00
|
|
|
* Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
|
2020-10-10 18:17:49 +03:00
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Assertions.h>
|
2020-01-03 23:31:51 -05:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/String.h>
|
2021-04-16 21:53:43 +02:00
|
|
|
#include <LibC/elf.h>
|
2021-04-24 20:39:58 +02:00
|
|
|
#include <LibDl/dlfcn_integration.h>
|
2020-04-11 12:24:07 -06:00
|
|
|
#include <LibELF/DynamicObject.h>
|
|
|
|
|
#include <LibELF/Image.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <sys/mman.h>
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2020-04-11 12:24:07 -06:00
|
|
|
namespace ELF {
|
|
|
|
|
|
2021-04-13 19:31:34 +02:00
|
|
|
class LoadedSegment {
|
|
|
|
|
public:
|
|
|
|
|
LoadedSegment(VirtualAddress address, size_t size)
|
|
|
|
|
: m_address(address)
|
|
|
|
|
, m_size(size)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VirtualAddress address() const { return m_address; }
|
|
|
|
|
size_t size() const { return m_size; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
VirtualAddress m_address;
|
|
|
|
|
size_t m_size;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-19 11:39:31 +02:00
|
|
|
enum class ShouldInitializeWeak {
|
|
|
|
|
Yes,
|
|
|
|
|
No
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-11 12:24:07 -06:00
|
|
|
class DynamicLoader : public RefCounted<DynamicLoader> {
|
2020-01-03 23:31:51 -05:00
|
|
|
public:
|
2021-04-24 20:39:58 +02:00
|
|
|
static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> try_create(int fd, String filename);
|
2020-04-11 12:24:07 -06:00
|
|
|
~DynamicLoader();
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-02-26 14:40:48 +01:00
|
|
|
const String& filename() const { return m_filename; }
|
|
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
bool is_valid() const { return m_valid; }
|
|
|
|
|
|
2020-04-11 12:24:07 -06:00
|
|
|
// Load a full ELF image from file into the current process and create an DynamicObject
|
2020-01-03 23:31:51 -05:00
|
|
|
// from the SHT_DYNAMIC in the file.
|
2021-01-31 11:46:00 +01:00
|
|
|
// Note that the DynamicObject will not be linked yet. Callers are responsible for calling link() to finish it.
|
|
|
|
|
RefPtr<DynamicObject> map();
|
|
|
|
|
|
|
|
|
|
bool link(unsigned flags, size_t total_tls_size);
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-01-02 00:48:19 +00:00
|
|
|
// Stage 2 of loading: dynamic object loading and primary relocations
|
2020-10-10 18:17:49 +03:00
|
|
|
bool load_stage_2(unsigned flags, size_t total_tls_size);
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-02-26 14:40:48 +01:00
|
|
|
// Stage 3 of loading: lazy relocations
|
2021-04-24 20:39:58 +02:00
|
|
|
Result<NonnullRefPtr<DynamicObject>, DlErrorMessage> load_stage_3(unsigned flags, size_t total_tls_size);
|
2021-02-26 14:40:48 +01:00
|
|
|
|
|
|
|
|
// Stage 4 of loading: initializers
|
|
|
|
|
void load_stage_4();
|
|
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
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;
|
|
|
|
|
|
2021-04-13 19:31:34 +02:00
|
|
|
VirtualAddress base_address() const { return m_base_address; }
|
|
|
|
|
const Vector<LoadedSegment> text_segments() const { return m_text_segments; }
|
2020-12-18 15:59:22 +02:00
|
|
|
bool is_dynamic() const { return m_elf_image.is_dynamic(); }
|
2020-10-10 18:17:49 +03:00
|
|
|
|
2021-02-21 00:29:08 +01:00
|
|
|
static Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&);
|
|
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
private:
|
2021-01-31 10:13:23 +01:00
|
|
|
DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
|
|
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
class ProgramHeaderRegion {
|
|
|
|
|
public:
|
|
|
|
|
void set_program_header(const Elf32_Phdr& header) { m_program_header = header; }
|
|
|
|
|
|
|
|
|
|
// Information from ELF Program header
|
|
|
|
|
u32 type() const { return m_program_header.p_type; }
|
|
|
|
|
u32 flags() const { return m_program_header.p_flags; }
|
|
|
|
|
u32 offset() const { return m_program_header.p_offset; }
|
|
|
|
|
VirtualAddress desired_load_address() const { return VirtualAddress(m_program_header.p_vaddr); }
|
|
|
|
|
u32 size_in_memory() const { return m_program_header.p_memsz; }
|
|
|
|
|
u32 size_in_image() const { return m_program_header.p_filesz; }
|
|
|
|
|
u32 alignment() const { return m_program_header.p_align; }
|
|
|
|
|
bool is_readable() const { return flags() & PF_R; }
|
|
|
|
|
bool is_writable() const { return flags() & PF_W; }
|
|
|
|
|
bool is_executable() const { return flags() & PF_X; }
|
|
|
|
|
bool is_tls_template() const { return type() == PT_TLS; }
|
|
|
|
|
bool is_load() const { return type() == PT_LOAD; }
|
|
|
|
|
bool is_dynamic() const { return type() == PT_DYNAMIC; }
|
2021-02-18 18:43:20 +01:00
|
|
|
bool is_relro() const { return type() == PT_GNU_RELRO; }
|
2020-01-03 23:31:51 -05:00
|
|
|
|
|
|
|
|
private:
|
2020-08-16 15:17:57 -07:00
|
|
|
Elf32_Phdr m_program_header; // Explicitly a copy of the PHDR in the image
|
2020-01-03 23:31:51 -05:00
|
|
|
};
|
|
|
|
|
|
2021-01-25 13:16:39 +01:00
|
|
|
const DynamicObject& dynamic_object() const;
|
2020-10-10 18:17:49 +03:00
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
// Stage 1
|
2020-10-10 18:17:49 +03:00
|
|
|
void load_program_headers();
|
2020-01-03 23:31:51 -05:00
|
|
|
|
|
|
|
|
// Stage 2
|
2021-01-02 00:48:19 +00:00
|
|
|
void do_main_relocations(size_t total_tls_size);
|
|
|
|
|
|
|
|
|
|
// Stage 3
|
|
|
|
|
void do_lazy_relocations(size_t total_tls_size);
|
2020-01-03 23:31:51 -05:00
|
|
|
void setup_plt_trampoline();
|
2021-02-26 14:40:48 +01:00
|
|
|
|
|
|
|
|
// Stage 4
|
2020-01-03 23:31:51 -05:00
|
|
|
void call_object_init_functions();
|
|
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
bool validate();
|
2021-01-02 00:48:19 +00:00
|
|
|
|
|
|
|
|
enum class RelocationResult : uint8_t {
|
|
|
|
|
Failed = 0,
|
|
|
|
|
Success = 1,
|
|
|
|
|
ResolveLater = 2,
|
|
|
|
|
};
|
2021-04-19 11:39:31 +02:00
|
|
|
RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&, ShouldInitializeWeak should_initialize_weak);
|
2020-10-10 18:17:49 +03:00
|
|
|
size_t calculate_tls_size() const;
|
2021-04-24 11:24:02 +03:00
|
|
|
ssize_t negative_offset_from_tls_block_end(size_t value_of_symbol, size_t tls_offset, size_t total_tls_size) const;
|
2020-10-10 18:17:49 +03:00
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
String m_filename;
|
2020-04-11 12:34:00 -06:00
|
|
|
String m_program_interpreter;
|
2020-01-03 23:31:51 -05:00
|
|
|
size_t m_file_size { 0 };
|
|
|
|
|
int m_image_fd { -1 };
|
2021-01-31 10:13:23 +01:00
|
|
|
void* m_file_data { nullptr };
|
2020-10-10 18:17:49 +03:00
|
|
|
ELF::Image m_elf_image;
|
2020-01-03 23:31:51 -05:00
|
|
|
bool m_valid { true };
|
|
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
RefPtr<DynamicObject> m_dynamic_object;
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-04-13 19:31:34 +02:00
|
|
|
VirtualAddress m_base_address;
|
|
|
|
|
Vector<LoadedSegment> m_text_segments;
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-02-18 18:43:20 +01:00
|
|
|
VirtualAddress m_relro_segment_address;
|
|
|
|
|
size_t m_relro_segment_size { 0 };
|
|
|
|
|
|
2020-01-08 21:38:05 -07:00
|
|
|
VirtualAddress m_dynamic_section_address;
|
2020-10-10 18:17:49 +03:00
|
|
|
|
|
|
|
|
size_t m_tls_offset { 0 };
|
2021-04-24 11:24:02 +03:00
|
|
|
size_t m_tls_size { 0 }; // TLS size of the current object
|
2021-01-02 00:48:19 +00:00
|
|
|
|
|
|
|
|
Vector<DynamicObject::Relocation> m_unresolved_relocations;
|
2021-01-25 13:16:39 +01:00
|
|
|
|
|
|
|
|
mutable RefPtr<DynamicObject> m_cached_dynamic_object;
|
2020-01-03 23:31:51 -05:00
|
|
|
};
|
2020-04-11 12:24:07 -06:00
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
template<typename F>
|
|
|
|
|
void DynamicLoader::for_each_needed_library(F func) const
|
|
|
|
|
{
|
2021-01-25 13:16:39 +01:00
|
|
|
dynamic_object().for_each_needed_library(move(func));
|
2020-10-10 18:17:49 +03:00
|
|
|
}
|
|
|
|
|
|
2020-04-11 12:24:07 -06:00
|
|
|
} // end namespace ELF
|