2023-05-23 04:48:25 +00:00
|
|
|
#pragma once
|
2023-06-19 04:51:56 +00:00
|
|
|
|
2023-10-22 14:10:25 +00:00
|
|
|
#include <mutex>
|
2024-02-23 20:57:57 +00:00
|
|
|
#include <vector>
|
2023-11-05 23:11:54 +00:00
|
|
|
#include "core/loader/elf.h"
|
|
|
|
#include "core/loader/symbols_resolver.h"
|
|
|
|
|
|
|
|
namespace Core {
|
2023-05-23 04:48:25 +00:00
|
|
|
|
2023-06-08 09:51:11 +00:00
|
|
|
struct DynamicModuleInfo;
|
2023-07-11 15:50:29 +00:00
|
|
|
class Linker;
|
2023-06-08 09:51:11 +00:00
|
|
|
|
2023-07-20 09:18:43 +00:00
|
|
|
struct EntryParams {
|
|
|
|
int argc;
|
|
|
|
u32 padding;
|
|
|
|
const char* argv[3];
|
|
|
|
};
|
|
|
|
|
2023-11-05 23:11:54 +00:00
|
|
|
struct ModuleInfo {
|
2024-02-23 20:57:57 +00:00
|
|
|
std::string name;
|
2023-11-05 23:11:54 +00:00
|
|
|
union {
|
2024-02-23 20:57:57 +00:00
|
|
|
u64 value;
|
2023-11-05 23:11:54 +00:00
|
|
|
struct {
|
2024-02-23 20:57:57 +00:00
|
|
|
u32 name_offset;
|
|
|
|
u08 version_minor;
|
|
|
|
u08 version_major;
|
|
|
|
u16 id;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
std::string enc_id;
|
2023-06-11 12:35:04 +00:00
|
|
|
};
|
|
|
|
|
2023-11-05 23:11:54 +00:00
|
|
|
struct LibraryInfo {
|
2024-02-23 20:57:57 +00:00
|
|
|
std::string name;
|
2023-11-05 23:11:54 +00:00
|
|
|
union {
|
2024-02-23 20:57:57 +00:00
|
|
|
u64 value;
|
2023-11-05 23:11:54 +00:00
|
|
|
struct {
|
2024-02-23 20:57:57 +00:00
|
|
|
u32 name_offset;
|
|
|
|
u16 version;
|
|
|
|
u16 id;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
std::string enc_id;
|
2023-06-12 05:16:20 +00:00
|
|
|
};
|
|
|
|
|
2023-11-21 15:27:11 +00:00
|
|
|
struct PS4ThreadLocal {
|
|
|
|
u64 image_virtual_addr = 0;
|
|
|
|
u64 image_size = 0;
|
|
|
|
u64 handler_virtual_addr = 0;
|
|
|
|
};
|
2023-11-05 23:11:54 +00:00
|
|
|
struct DynamicModuleInfo {
|
2023-10-26 20:07:15 +00:00
|
|
|
void* hash_table = nullptr;
|
|
|
|
u64 hash_table_size = 0;
|
|
|
|
|
|
|
|
char* str_table = nullptr;
|
|
|
|
u64 str_table_size = 0;
|
|
|
|
|
|
|
|
elf_symbol* symbol_table = nullptr;
|
|
|
|
u64 symbol_table_total_size = 0;
|
|
|
|
u64 symbol_table_entries_size = 0;
|
|
|
|
|
|
|
|
u64 init_virtual_addr = 0;
|
|
|
|
u64 fini_virtual_addr = 0;
|
|
|
|
u64 pltgot_virtual_addr = 0;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
u64 debug = 0;
|
|
|
|
u64 textrel = 0;
|
|
|
|
u64 flags = 0;
|
|
|
|
|
|
|
|
std::vector<const char*> needed;
|
|
|
|
std::vector<ModuleInfo> import_modules;
|
|
|
|
std::vector<ModuleInfo> export_modules;
|
|
|
|
std::vector<LibraryInfo> import_libs;
|
|
|
|
std::vector<LibraryInfo> export_libs;
|
|
|
|
|
|
|
|
std::string filename; // Filename with absolute path
|
2023-05-23 04:48:25 +00:00
|
|
|
};
|
|
|
|
|
2023-10-26 20:07:15 +00:00
|
|
|
// This struct keeps neccesary info about loaded modules. Main executeable is included too as well
|
2023-11-05 23:11:54 +00:00
|
|
|
struct Module {
|
|
|
|
Loader::Elf elf;
|
2023-10-26 20:07:15 +00:00
|
|
|
u64 aligned_base_size = 0;
|
|
|
|
u64 base_virtual_addr = 0; // Base virtual address
|
|
|
|
|
|
|
|
Linker* linker = nullptr;
|
|
|
|
|
|
|
|
std::vector<u08> m_dynamic;
|
|
|
|
std::vector<u08> m_dynamic_data;
|
|
|
|
DynamicModuleInfo dynamic_info{};
|
|
|
|
|
2023-11-05 23:11:54 +00:00
|
|
|
Loader::SymbolsResolver export_sym;
|
|
|
|
Loader::SymbolsResolver import_sym;
|
2023-11-21 15:27:11 +00:00
|
|
|
|
|
|
|
PS4ThreadLocal tls;
|
2023-10-26 20:07:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Linker {
|
2023-05-23 04:48:25 +00:00
|
|
|
public:
|
2024-02-23 20:57:57 +00:00
|
|
|
Linker();
|
|
|
|
virtual ~Linker();
|
|
|
|
|
|
|
|
Module* LoadModule(const std::string& elf_name);
|
|
|
|
Module* FindModule(/*u32 id*/);
|
|
|
|
void LoadModuleToMemory(Module* m);
|
|
|
|
void LoadDynamicInfo(Module* m);
|
|
|
|
void LoadSymbols(Module* m);
|
|
|
|
Loader::SymbolsResolver& getHLESymbols() {
|
|
|
|
return m_hle_symbols;
|
|
|
|
}
|
|
|
|
void Relocate(Module* m);
|
|
|
|
void Resolve(const std::string& name, int Symtype, Module* m,
|
|
|
|
Loader::SymbolRecord* return_info);
|
2023-07-20 09:18:43 +00:00
|
|
|
void Execute();
|
2023-05-23 07:47:56 +00:00
|
|
|
|
2023-10-26 20:07:15 +00:00
|
|
|
private:
|
2024-02-23 20:57:57 +00:00
|
|
|
const ModuleInfo* FindModule(const Module& m, const std::string& id);
|
|
|
|
const LibraryInfo* FindLibrary(const Module& program, const std::string& id);
|
2023-06-28 17:15:19 +00:00
|
|
|
|
2023-10-26 20:07:15 +00:00
|
|
|
std::vector<Module> m_modules;
|
2023-11-05 23:11:54 +00:00
|
|
|
Loader::SymbolsResolver m_hle_symbols{};
|
2023-10-22 14:10:25 +00:00
|
|
|
std::mutex m_mutex;
|
2023-10-26 19:55:13 +00:00
|
|
|
};
|
2023-11-05 23:11:54 +00:00
|
|
|
|
|
|
|
} // namespace Core
|