2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-05-30 07:28:59 -06:00
|
|
|
* Copyright (c) 2019-2020, Andrew Kaster <akaster@serenityos.org>
|
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>
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2020-01-03 23:31:51 -05:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
2020-04-11 12:24:07 -06:00
|
|
|
#include <LibELF/DynamicObject.h>
|
2023-04-30 19:07:21 +04:00
|
|
|
#include <LibELF/ELFABI.h>
|
2020-04-11 12:24:07 -06:00
|
|
|
#include <LibELF/Image.h>
|
2022-08-14 13:18:35 +02:00
|
|
|
#include <bits/dlfcn_integration.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
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-22 12:09:00 +02:00
|
|
|
enum class ShouldCallIfuncResolver {
|
|
|
|
|
Yes,
|
|
|
|
|
No
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-22 11:44:02 +02:00
|
|
|
extern "C" FlatPtr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset);
|
|
|
|
|
|
2020-04-11 12:24:07 -06:00
|
|
|
class DynamicLoader : public RefCounted<DynamicLoader> {
|
2020-01-03 23:31:51 -05:00
|
|
|
public:
|
2023-12-16 17:49:34 +03:30
|
|
|
static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> try_create(int fd, ByteString filepath);
|
2020-04-11 12:24:07 -06:00
|
|
|
~DynamicLoader();
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString const& filepath() const { return m_filepath; }
|
2021-02-26 14:40:48 +01:00
|
|
|
|
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();
|
|
|
|
|
|
2021-04-30 13:31:42 +03:00
|
|
|
bool link(unsigned flags);
|
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
|
2021-04-30 13:31:42 +03:00
|
|
|
bool load_stage_2(unsigned flags);
|
2020-01-03 23:31:51 -05:00
|
|
|
|
2021-02-26 14:40:48 +01:00
|
|
|
// Stage 3 of loading: lazy relocations
|
2021-04-30 13:31:42 +03:00
|
|
|
Result<NonnullRefPtr<DynamicObject>, DlErrorMessage> load_stage_3(unsigned flags);
|
2021-02-26 14:40:48 +01:00
|
|
|
|
|
|
|
|
// Stage 4 of loading: initializers
|
|
|
|
|
void load_stage_4();
|
|
|
|
|
|
2023-07-07 22:48:11 -04:00
|
|
|
void set_tls_offset(size_t offset) { m_tls_offset = offset; }
|
2021-04-24 21:15:28 +03:00
|
|
|
size_t tls_size_of_current_object() const { return m_tls_size_of_current_object; }
|
2022-07-05 01:18:40 +03:00
|
|
|
size_t tls_alignment_of_current_object() const { return m_tls_alignment_of_current_object; }
|
2020-10-10 18:17:49 +03:00
|
|
|
size_t tls_offset() const { return m_tls_offset; }
|
2022-06-04 23:29:09 +02:00
|
|
|
const ELF::Image& image() const { return *m_elf_image; }
|
2020-10-10 18:17:49 +03:00
|
|
|
|
|
|
|
|
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; }
|
2022-04-01 20:58:27 +03:00
|
|
|
Vector<LoadedSegment> const text_segments() const { return m_text_segments; }
|
2022-06-04 23:29:09 +02:00
|
|
|
bool is_dynamic() const { return 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&);
|
2021-04-30 13:31:42 +03:00
|
|
|
void copy_initial_tls_data_into(ByteBuffer& buffer) const;
|
2021-02-21 00:29:08 +01:00
|
|
|
|
2022-02-18 19:21:51 +01:00
|
|
|
DynamicObject const& dynamic_object() const;
|
|
|
|
|
|
2022-06-24 11:12:07 +02:00
|
|
|
bool is_fully_relocated() const { return m_fully_relocated; }
|
2022-06-24 10:50:34 +02:00
|
|
|
bool is_fully_initialized() const { return m_fully_initialized; }
|
|
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
private:
|
2023-12-16 17:49:34 +03:30
|
|
|
DynamicLoader(int fd, ByteString filepath, void* file_data, size_t file_size);
|
2021-01-31 10:13:23 +01:00
|
|
|
|
2020-01-03 23:31:51 -05:00
|
|
|
class ProgramHeaderRegion {
|
|
|
|
|
public:
|
2023-11-30 23:58:55 +01:00
|
|
|
void set_program_header(Elf_Phdr const& header) { m_program_header = header; }
|
2020-01-03 23:31:51 -05:00
|
|
|
|
|
|
|
|
// 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:
|
2023-11-30 23:58:55 +01:00
|
|
|
Elf_Phdr m_program_header; // Explicitly a copy of the PHDR in the image
|
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-04-30 13:31:42 +03:00
|
|
|
void do_main_relocations();
|
2021-01-02 00:48:19 +00:00
|
|
|
|
|
|
|
|
// Stage 3
|
2021-04-30 13:31:42 +03:00
|
|
|
void do_lazy_relocations();
|
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
|
|
|
|
2023-04-22 12:09:00 +02:00
|
|
|
friend FlatPtr _fixup_plt_entry(DynamicObject*, u32);
|
|
|
|
|
|
2021-01-02 00:48:19 +00:00
|
|
|
enum class RelocationResult : uint8_t {
|
|
|
|
|
Failed = 0,
|
|
|
|
|
Success = 1,
|
|
|
|
|
ResolveLater = 2,
|
2023-04-22 12:09:00 +02:00
|
|
|
CallIfuncResolver = 3,
|
2021-01-02 00:48:19 +00:00
|
|
|
};
|
2023-08-17 08:00:08 +02:00
|
|
|
struct CachedLookupResult {
|
|
|
|
|
DynamicObject::Symbol symbol;
|
|
|
|
|
Optional<DynamicObject::SymbolLookupResult> result;
|
|
|
|
|
};
|
|
|
|
|
RelocationResult do_direct_relocation(DynamicObject::Relocation const&, Optional<CachedLookupResult>&, ShouldInitializeWeak, ShouldCallIfuncResolver);
|
2023-04-22 12:09:00 +02:00
|
|
|
// Will be called from _fixup_plt_entry, as part of the PLT trampoline
|
|
|
|
|
static RelocationResult do_plt_relocation(DynamicObject::Relocation const&, ShouldCallIfuncResolver);
|
2021-10-28 09:31:51 +02:00
|
|
|
void do_relr_relocations();
|
2022-07-05 01:18:40 +03:00
|
|
|
void find_tls_size_and_alignment();
|
2020-10-10 18:17:49 +03:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_filepath;
|
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 };
|
2022-06-04 23:29:09 +02:00
|
|
|
OwnPtr<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
|
|
|
|
2021-07-04 00:01:06 +02:00
|
|
|
ssize_t m_tls_offset { 0 };
|
2021-04-24 21:15:28 +03:00
|
|
|
size_t m_tls_size_of_current_object { 0 };
|
2022-07-05 01:18:40 +03:00
|
|
|
size_t m_tls_alignment_of_current_object { 0 };
|
2021-01-02 00:48:19 +00:00
|
|
|
|
|
|
|
|
Vector<DynamicObject::Relocation> m_unresolved_relocations;
|
2023-04-22 12:09:00 +02:00
|
|
|
Vector<DynamicObject::Relocation> m_direct_ifunc_relocations;
|
|
|
|
|
Vector<DynamicObject::Relocation> m_plt_ifunc_relocations;
|
2021-01-25 13:16:39 +01:00
|
|
|
|
|
|
|
|
mutable RefPtr<DynamicObject> m_cached_dynamic_object;
|
2022-06-24 10:50:34 +02:00
|
|
|
|
2022-06-24 11:12:07 +02:00
|
|
|
bool m_fully_relocated { false };
|
2022-06-24 10:50:34 +02:00
|
|
|
bool m_fully_initialized { false };
|
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
|