2023-05-23 06:48:25 +02:00
|
|
|
#pragma once
|
2023-06-19 06:51:56 +02:00
|
|
|
|
2023-05-23 09:47:56 +02:00
|
|
|
#include <vector>
|
2023-06-19 06:51:56 +02:00
|
|
|
#include "Loader/Elf.h"
|
2023-06-26 17:12:19 +02:00
|
|
|
#include "Loader/SymbolsResolver.h"
|
2023-08-09 10:42:50 +02:00
|
|
|
#include "Lib/Threads.h"
|
2023-05-23 06:48:25 +02:00
|
|
|
|
2023-06-08 11:51:11 +02:00
|
|
|
struct DynamicModuleInfo;
|
2023-07-11 17:50:29 +02:00
|
|
|
class Linker;
|
2023-06-08 11:51:11 +02:00
|
|
|
|
2023-07-20 11:18:43 +02:00
|
|
|
struct EntryParams {
|
|
|
|
int argc;
|
|
|
|
u32 padding;
|
|
|
|
const char* argv[3];
|
|
|
|
};
|
|
|
|
|
2023-05-23 06:48:25 +02:00
|
|
|
/*this struct keeps neccesary info about loaded modules.Main executeable is included too as well*/
|
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
Elf* elf = nullptr;
|
2023-05-23 18:39:24 +02:00
|
|
|
u64 aligned_base_size = 0;
|
|
|
|
u64 base_virtual_addr = 0; //base virtual address
|
2023-05-26 18:22:43 +02:00
|
|
|
|
2023-07-11 17:50:29 +02:00
|
|
|
Linker* linker = nullptr;
|
|
|
|
|
2023-05-26 18:22:43 +02:00
|
|
|
void* m_dynamic = nullptr;
|
|
|
|
void* m_dynamic_data = nullptr;
|
2023-06-08 11:51:11 +02:00
|
|
|
DynamicModuleInfo* dynamic_info = nullptr;
|
2023-06-26 17:12:19 +02:00
|
|
|
|
|
|
|
SymbolsResolver* export_sym = nullptr;
|
|
|
|
SymbolsResolver* import_sym = nullptr;
|
2023-06-08 11:51:11 +02:00
|
|
|
};
|
|
|
|
|
2023-06-11 14:35:04 +02:00
|
|
|
struct ModuleInfo
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u64 value;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 name_offset;
|
|
|
|
u08 version_minor;
|
|
|
|
u08 version_major;
|
|
|
|
u16 id;
|
|
|
|
};
|
|
|
|
};
|
2023-06-14 06:47:44 +02:00
|
|
|
std::string enc_id;
|
2023-06-11 14:35:04 +02:00
|
|
|
};
|
|
|
|
|
2023-06-12 07:16:20 +02:00
|
|
|
struct LibraryInfo
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u64 value;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 name_offset;
|
|
|
|
u16 version;
|
|
|
|
u16 id;
|
|
|
|
};
|
|
|
|
};
|
2023-06-14 06:47:44 +02:00
|
|
|
std::string enc_id;
|
2023-06-12 07:16:20 +02:00
|
|
|
};
|
|
|
|
|
2023-06-08 11:51:11 +02:00
|
|
|
struct DynamicModuleInfo
|
|
|
|
{
|
|
|
|
void* hash_table = nullptr;
|
2023-06-08 12:25:24 +02:00
|
|
|
u64 hash_table_size = 0;
|
|
|
|
|
|
|
|
char* str_table = nullptr;
|
|
|
|
u64 str_table_size = 0;
|
2023-06-08 18:05:03 +02:00
|
|
|
|
|
|
|
elf_symbol* symbol_table = nullptr;
|
|
|
|
u64 symbol_table_total_size = 0;
|
2023-06-09 12:37:46 +02:00
|
|
|
u64 symbol_table_entries_size = 0;
|
2023-06-08 18:05:03 +02:00
|
|
|
|
|
|
|
u64 init_virtual_addr = 0;
|
|
|
|
u64 fini_virtual_addr = 0;
|
|
|
|
u64 pltgot_virtual_addr = 0;
|
2023-06-09 12:37:46 +02:00
|
|
|
u64 init_array_virtual_addr = 0;
|
|
|
|
u64 fini_array_virtual_addr = 0;
|
|
|
|
u64 preinit_array_virtual_addr = 0;
|
|
|
|
u64 init_array_size = 0;
|
|
|
|
u64 fini_array_size = 0;
|
|
|
|
u64 preinit_array_size = 0;
|
2023-06-08 18:05:03 +02:00
|
|
|
|
2023-06-09 10:37:18 +02:00
|
|
|
elf_relocation* jmp_relocation_table = nullptr;
|
|
|
|
u64 jmp_relocation_table_size = 0;
|
|
|
|
s64 jmp_relocation_type = 0;
|
|
|
|
|
|
|
|
elf_relocation* relocation_table = nullptr;
|
|
|
|
u64 relocation_table_size = 0;
|
|
|
|
u64 relocation_table_entries_size = 0;
|
2023-06-09 17:10:40 +02:00
|
|
|
|
|
|
|
u64 debug = 0;
|
|
|
|
u64 textrel = 0;
|
|
|
|
u64 flags = 0;
|
|
|
|
|
|
|
|
std::vector<const char*> needed;
|
2023-06-12 07:16:20 +02:00
|
|
|
std::vector<ModuleInfo> import_modules;
|
|
|
|
std::vector<ModuleInfo> export_modules;
|
|
|
|
std::vector<LibraryInfo> import_libs;
|
2023-06-13 06:43:58 +02:00
|
|
|
std::vector<LibraryInfo> export_libs;
|
2023-06-12 07:16:20 +02:00
|
|
|
|
|
|
|
std::string filename;//filename with absolute path
|
2023-06-09 17:10:40 +02:00
|
|
|
|
2023-05-23 06:48:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Linker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Linker();
|
|
|
|
virtual ~Linker();
|
|
|
|
|
|
|
|
Module* LoadModule(const std::string& elf_name);
|
2023-05-23 09:47:56 +02:00
|
|
|
Module* FindModule(/*u32 id*/);
|
2023-05-23 18:39:24 +02:00
|
|
|
void LoadModuleToMemory(Module* m);
|
2023-06-13 06:43:58 +02:00
|
|
|
void LoadDynamicInfo(Module* m);
|
|
|
|
void LoadSymbols(Module* m);
|
2023-06-28 19:15:19 +02:00
|
|
|
SymbolsResolver* getHLESymbols() { return m_HLEsymbols; }
|
2023-07-04 17:34:23 +02:00
|
|
|
void Relocate(Module* m);
|
2023-07-11 17:50:29 +02:00
|
|
|
void Resolve(const std::string& name, int Symtype, Module* m, SymbolRecord* return_info);
|
2023-07-20 11:18:43 +02:00
|
|
|
void Execute();
|
2023-05-23 09:47:56 +02:00
|
|
|
|
2023-07-20 11:18:43 +02:00
|
|
|
private:
|
2023-06-18 16:54:22 +02:00
|
|
|
const ModuleInfo* FindModule(const Module& m, const std::string& id);
|
|
|
|
const LibraryInfo* FindLibrary(const Module& program, const std::string& id);
|
2023-06-28 19:15:19 +02:00
|
|
|
|
2023-05-23 09:47:56 +02:00
|
|
|
std::vector<Module*> m_modules;
|
2023-06-28 19:15:19 +02:00
|
|
|
SymbolsResolver* m_HLEsymbols = nullptr;
|
2023-08-09 10:42:50 +02:00
|
|
|
Lib::Mutex m_mutex;
|
2023-05-23 06:48:25 +02:00
|
|
|
};
|