2024-02-23 22:32:32 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2023-05-23 06:48:25 +02:00
|
|
|
#pragma once
|
2023-06-19 06:51:56 +02:00
|
|
|
|
2024-06-15 13:36:07 +02:00
|
|
|
#include <algorithm>
|
2023-10-22 16:10:25 +02:00
|
|
|
#include <mutex>
|
2024-02-23 21:57:57 +01:00
|
|
|
#include <vector>
|
2024-06-05 21:08:18 +02:00
|
|
|
#include "core/module.h"
|
2023-11-06 00:11:54 +01:00
|
|
|
|
|
|
|
namespace Core {
|
2024-06-05 21:08:18 +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];
|
|
|
|
};
|
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
using HeapApiFunc = PS4_SYSV_ABI void* (*)(size_t);
|
2023-06-12 07:16:20 +02:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
class Linker {
|
|
|
|
public:
|
|
|
|
explicit Linker();
|
|
|
|
~Linker();
|
2024-02-27 23:10:34 +01:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
Loader::SymbolsResolver& GetHLESymbols() {
|
|
|
|
return m_hle_symbols;
|
|
|
|
}
|
2023-05-23 06:48:25 +02:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
VAddr GetProcParam() const {
|
|
|
|
return m_modules[0]->GetProcParam();
|
|
|
|
}
|
2023-10-26 22:07:15 +02:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
Module* GetModule(s32 index) const {
|
|
|
|
return m_modules.at(index).get();
|
|
|
|
}
|
2024-03-11 12:26:33 +01:00
|
|
|
|
2024-06-15 13:36:07 +02:00
|
|
|
void RelocateAnyImports(Module* m) {
|
|
|
|
Relocate(m);
|
|
|
|
for (auto& module : m_modules) {
|
|
|
|
const auto imports = module->GetImportModules();
|
|
|
|
if (std::ranges::contains(imports, m->name, &ModuleInfo::name)) {
|
|
|
|
Relocate(module.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
void SetHeapApiFunc(void* func) {
|
|
|
|
heap_api_func = *reinterpret_cast<HeapApiFunc*>(func);
|
|
|
|
}
|
2023-10-26 22:07:15 +02:00
|
|
|
|
2024-06-15 13:36:07 +02:00
|
|
|
void AdvanceGenerationCounter() noexcept {
|
|
|
|
dtv_generation_counter++;
|
|
|
|
}
|
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
void* TlsGetAddr(u64 module_index, u64 offset);
|
|
|
|
void InitTlsForThread(bool is_primary = false);
|
2023-11-21 16:27:11 +01:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
s32 LoadModule(const std::filesystem::path& elf_name);
|
2024-06-15 13:36:07 +02:00
|
|
|
Module* FindByAddress(VAddr address);
|
2023-10-26 22:07:15 +02:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
void Relocate(Module* module);
|
2024-06-15 13:36:07 +02:00
|
|
|
bool Resolve(const std::string& name, Loader::SymbolType type, Module* module,
|
2024-02-23 21:57:57 +01:00
|
|
|
Loader::SymbolRecord* return_info);
|
2023-07-20 11:18:43 +02:00
|
|
|
void Execute();
|
2024-03-11 12:26:33 +01:00
|
|
|
void DebugDump();
|
2023-05-23 09:47:56 +02:00
|
|
|
|
2023-10-26 22:07:15 +02:00
|
|
|
private:
|
2024-06-05 21:08:18 +02:00
|
|
|
const Module* FindExportedModule(const ModuleInfo& m, const LibraryInfo& l);
|
2023-06-28 19:15:19 +02:00
|
|
|
|
2024-06-05 21:08:18 +02:00
|
|
|
std::mutex mutex;
|
|
|
|
u32 dtv_generation_counter{1};
|
|
|
|
size_t static_tls_size{};
|
2024-06-15 13:36:07 +02:00
|
|
|
u32 max_tls_index{};
|
2024-06-05 21:08:18 +02:00
|
|
|
HeapApiFunc heap_api_func{};
|
2024-02-27 23:10:34 +01:00
|
|
|
std::vector<std::unique_ptr<Module>> m_modules;
|
2023-11-06 00:11:54 +01:00
|
|
|
Loader::SymbolsResolver m_hle_symbols{};
|
2023-10-26 21:55:13 +02:00
|
|
|
};
|
2023-11-06 00:11:54 +01:00
|
|
|
|
|
|
|
} // namespace Core
|