h1-mod/src/client/loader/tls.cpp

35 lines
804 B
C++
Raw Normal View History

2022-02-03 14:05:24 -05:00
#include <std_include.hpp>
#include "tls.hpp"
#include <utils/nt.hpp>
#include <utils/binary_resource.hpp>
namespace tls
{
namespace
{
utils::binary_resource tls_dll_file(TLS_DLL, "s1x-tlsdll.dll");
}
PIMAGE_TLS_DIRECTORY allocate_tls_index()
{
static auto already_allocated = false;
if (already_allocated)
{
throw std::runtime_error("Currently only a single allocation is supported!");
}
already_allocated = true;
const auto dll_path = tls_dll_file.get_extracted_file();
const auto tls_dll = utils::nt::library::load(dll_path);
if (!tls_dll)
{
throw std::runtime_error("Failed to load TLS DLL");
}
return reinterpret_cast<PIMAGE_TLS_DIRECTORY>(tls_dll.get_ptr() + tls_dll.get_optional_header()
->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress);
}
}