Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/relinker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_library(relinker_core STATIC
${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/guest/GuestImageReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/guest/GuestModuleBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsPePatcher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsExportBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsIconResourceBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsPeFormat.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsLoadImage.cpp
Expand Down Expand Up @@ -587,6 +588,9 @@ if(BUILD_TESTING)
if(Python3_Interpreter_FOUND)
add_test(NAME cli_help COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/cli/tests/test_help.py $<TARGET_FILE:relinker>)
add_test(NAME windows_executable_exports COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_executable_exports.py $<TARGET_FILE:relinker>)
set_tests_properties(windows_executable_exports PROPERTIES TIMEOUT 20)
add_test(NAME windows_fixed_base COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_fixed_base.py $<TARGET_FILE:relinker>)
set_tests_properties(windows_fixed_base PROPERTIES TIMEOUT 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Elfpatcher::Windows {

class WindowsPePatcher : public IElfPatcher {
public:
explicit WindowsPePatcher(bool windowsGui = false, std::optional<std::vector<std::uint8_t>> icon = std::nullopt, std::filesystem::path iconName = "icon0.png", std::function<void(std::uint64_t, std::uint64_t)> progress = {}, std::optional<std::uint64_t> fixedBase = std::nullopt, bool traceImports = false, std::function<void(std::string_view)> message = {});
explicit WindowsPePatcher(bool windowsGui = false, std::optional<std::vector<std::uint8_t>> icon = std::nullopt, std::filesystem::path iconName = "icon0.png", std::function<void(std::uint64_t, std::uint64_t)> progress = {}, std::optional<std::uint64_t> fixedBase = std::nullopt, bool traceImports = false, std::function<void(std::string_view)> message = {}, std::string outputName = "app.exe");
const std::string& GetDiagnostics() const;

std::vector<std::uint8_t> Patch(std::vector<std::uint8_t> sourceElf, const std::vector<Domain::ProgramHeader>& originalHeaders, const Domain::SysVDynamicSection& dynamicSection, std::uint64_t originalPltGotVaddr, const std::string& runPath, bool lazyBinding, bool dependencyDiagnostics, const std::vector<Codegen::TrampolineSite>& trampolines) override;
Expand All @@ -26,6 +26,7 @@ class WindowsPePatcher : public IElfPatcher {
std::optional<std::uint64_t> fixedBase;
bool traceImports;
std::function<void(std::string_view)> message;
std::string outputName;
};

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef ELFPATCHER_WINDOWS_EXPORTBUILDER_HPP
#define ELFPATCHER_WINDOWS_EXPORTBUILDER_HPP

#include <elfpatcher/windows/WindowsLoadImage.hpp>
#include <map>

namespace Elfpatcher::Windows {

class WindowsExportBuilder {
public:
PeDirectory Build(const std::map<std::string, std::uint32_t>& exports, const std::string& name, std::vector<PeSection>& sections, std::uint32_t& nextRva) const;
PeDirectory BuildExecutable(const Domain::SysVDynamicSection& dynamic, const WindowsLoadImage& image, const std::vector<Domain::ProgramHeader>& headers, std::uint32_t tlsIndex, std::vector<std::uint32_t>& relocations, const std::string& name, std::vector<PeSection>& sections, std::uint32_t& nextRva) const;
};

}

#endif
85 changes: 85 additions & 0 deletions core/relinker/elfpatcher/src/windows/WindowsExportBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <elfpatcher/windows/WindowsExportBuilder.hpp>
#include <relinker/guest/GuestImage.hpp>
#include <io/BufferUtils.hpp>
#include <algorithm>

namespace Elfpatcher::Windows {

PeDirectory WindowsExportBuilder::Build(const std::map<std::string, std::uint32_t>& exports, const std::string& name, std::vector<PeSection>& sections, std::uint32_t& nextRva) const {
if (exports.size() > 65535) throw Domain::RelinkerException("Too many PE exports");
PeSection section{".edata", nextRva, SectionRead | 0x40u, std::vector<std::uint8_t>(40)};
auto& data = section.Data;
const auto count = CheckedRva(exports.size());
const auto functions = data.size();
data.resize(data.size() + count * 4);
const auto names = data.size();
data.resize(data.size() + count * 4);
const auto ordinals = data.size();
data.resize(data.size() + count * 2);
Io::WriteU32(data, 12, CheckedRva(nextRva + data.size()));
Io::AppendString(data, name);
Io::WriteU32(data, 16, 1);
Io::WriteU32(data, 20, count);
Io::WriteU32(data, 24, count);
Io::WriteU32(data, 28, CheckedRva(nextRva + functions));
Io::WriteU32(data, 32, CheckedRva(nextRva + names));
Io::WriteU32(data, 36, CheckedRva(nextRva + ordinals));
std::size_t index = 0;
for (const auto& [symbol, rva] : exports) {
Io::WriteU32(data, functions + index * 4, rva);
Io::WriteU32(data, names + index * 4, CheckedRva(nextRva + data.size()));
Io::WriteU16(data, ordinals + index * 2, static_cast<std::uint16_t>(index));
Io::AppendString(data, symbol);
++index;
}
const PeDirectory directory{nextRva, CheckedRva(data.size())};
nextRva = AlignRva(nextRva + data.size());
sections.push_back(std::move(section));
return directory;
}

PeDirectory WindowsExportBuilder::BuildExecutable(const Domain::SysVDynamicSection& dynamic, const WindowsLoadImage& image, const std::vector<Domain::ProgramHeader>& headers, const std::uint32_t tlsIndex, std::vector<std::uint32_t>& relocations, const std::string& name, std::vector<PeSection>& sections, std::uint32_t& nextRva) const {
if (dynamic.DynSymData.size() % 24 != 0) throw Domain::RelinkerException("Invalid executable symbol table");
std::map<std::string, Relinker::GuestSymbol> symbols;
std::map<std::string, std::uint32_t> exports;
PeSection tlsExports{".tlsrefs", nextRva, SectionRead | 0x40u, {}};
for (std::size_t offset = 24; offset < dynamic.DynSymData.size(); offset += 24) {
const auto info = dynamic.DynSymData[offset + 4];
const auto visibility = dynamic.DynSymData[offset + 5] & 3;
const auto section = Io::ReadU16(dynamic.DynSymData, offset + 6);
if (section == 0 || (info >> 4) == 0 || visibility == 1 || visibility == 2) continue;
const auto symbolName = ReadString(dynamic.DynStrData, Io::ReadU32(dynamic.DynSymData, offset));
if (symbolName.empty()) throw Domain::RelinkerException("Empty executable export");
Relinker::GuestSymbol symbol{symbolName, info, static_cast<std::uint8_t>(visibility), section, Io::ReadU64(dynamic.DynSymData, offset + 8), Io::ReadU64(dynamic.DynSymData, offset + 16), {}, {}};
const auto [previous, inserted] = symbols.emplace(symbolName, symbol);
if (!inserted) {
const auto& first = previous->second;
if (first.Info != symbol.Info || first.Visibility != symbol.Visibility || first.Section != symbol.Section || first.Value != symbol.Value || first.Size != symbol.Size) throw Domain::RelinkerException("Duplicate executable export: " + symbolName);
continue;
}
if (section == Relinker::AbsoluteSection) continue;
if (section >= 0xff00) throw Domain::RelinkerException("Unsupported executable export section: " + symbolName);
const auto type = info & 15;
std::uint32_t rva = 0;
if (type == 6) {
const auto tls = std::find_if(headers.begin(), headers.end(), [](const auto& header) { return header.Type == 7; });
if (tlsIndex == 0 || tls == headers.end()) throw Domain::RelinkerException("Executable TLS export has no TLS block: " + symbolName);
if (symbol.Value >= tls->MemorySize || std::max<std::uint64_t>(symbol.Size, 1) > tls->MemorySize - symbol.Value) throw Domain::RelinkerException("Executable TLS export is outside its TLS block: " + symbolName);
rva = CheckedRva(tlsExports.Rva + tlsExports.Data.size());
Io::AppendU64(tlsExports.Data, image.GetImageBase() + tlsIndex);
Io::AppendU64(tlsExports.Data, symbol.Value);
relocations.push_back(rva);
} else {
if (type != 0 && type != 1 && type != 2) throw Domain::RelinkerException("Unsupported executable export type: " + symbolName);
rva = image.GetRva(symbol.Value, std::max<std::uint64_t>(symbol.Size, 1));
}
exports.emplace(symbolName, rva);
}
if (!tlsExports.Data.empty()) {
nextRva = AlignRva(nextRva + tlsExports.Data.size());
sections.push_back(std::move(tlsExports));
}
return exports.empty() ? PeDirectory{} : Build(exports, name, sections, nextRva);
}

}
30 changes: 2 additions & 28 deletions core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <elfpatcher/general/GuestModuleWriter.hpp>
#include <elfpatcher/windows/WindowsImportBuilder.hpp>
#include <elfpatcher/windows/WindowsExportBuilder.hpp>
#include <elfpatcher/windows/WindowsLoadImage.hpp>
#include <elfpatcher/windows/WindowsTlsBuilder.hpp>
#include <elfpatcher/windows/WindowsPeWriter.hpp>
Expand Down Expand Up @@ -158,34 +159,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
sections.push_back(std::move(lifecycle));
}
if (exports.size() > 65535) throw Domain::RelinkerException("Too many guest PE exports");
PeSection exportSection{".edata", nextRva, SectionRead | 0x40u, std::vector<std::uint8_t>(40)};
auto& data = exportSection.Data;
const auto count = CheckedRva(exports.size());
const auto functions = data.size();
data.resize(data.size() + count * 4);
const auto names = data.size();
data.resize(data.size() + count * 4);
const auto ordinals = data.size();
data.resize(data.size() + count * 2);
Io::WriteU32(data, 12, CheckedRva(nextRva + data.size()));
Io::AppendString(data, guest.OutputName);
Io::WriteU32(data, 16, 1);
Io::WriteU32(data, 20, count);
Io::WriteU32(data, 24, count);
Io::WriteU32(data, 28, CheckedRva(nextRva + functions));
Io::WriteU32(data, 32, CheckedRva(nextRva + names));
Io::WriteU32(data, 36, CheckedRva(nextRva + ordinals));
std::size_t index = 0;
for (const auto& [name, rva] : exports) {
Io::WriteU32(data, functions + index * 4, rva);
Io::WriteU32(data, names + index * 4, CheckedRva(nextRva + data.size()));
Io::WriteU16(data, ordinals + index * 2, static_cast<std::uint16_t>(index));
Io::AppendString(data, name);
++index;
}
directories[0] = {nextRva, CheckedRva(data.size())};
nextRva = AlignRva(nextRva + data.size());
sections.push_back(std::move(exportSection));
directories[0] = WindowsExportBuilder().Build(exports, guest.OutputName, sections, nextRva);
if (tlsIndex != 0) {
auto imports = WindowsImportBuilder().Build(nextRva);
directories[1] = imports.Directory;
Expand Down
7 changes: 5 additions & 2 deletions core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <elfpatcher/windows/WindowsElfPatcher.hpp>
#include <elfpatcher/windows/WindowsEntryStubBuilder.hpp>
#include <elfpatcher/windows/WindowsExportBuilder.hpp>
#include <elfpatcher/windows/WindowsIconResourceBuilder.hpp>
#include <elfpatcher/windows/WindowsLoadImage.hpp>
#include <elfpatcher/windows/WindowsPeWriter.hpp>
Expand Down Expand Up @@ -28,7 +29,7 @@ void writeGotStub(std::vector<PeSection>& sections, const std::uint32_t targetRv

}

WindowsPePatcher::WindowsPePatcher(const bool windowsGui, std::optional<std::vector<std::uint8_t>> icon, std::filesystem::path iconName, std::function<void(std::uint64_t, std::uint64_t)> progress, const std::optional<std::uint64_t> fixedBase, const bool traceImports, std::function<void(std::string_view)> message) : progress(std::move(progress)), windowsGui(windowsGui), icon(std::move(icon)), iconName(std::move(iconName)), fixedBase(fixedBase), traceImports(traceImports), message(std::move(message)) {
WindowsPePatcher::WindowsPePatcher(const bool windowsGui, std::optional<std::vector<std::uint8_t>> icon, std::filesystem::path iconName, std::function<void(std::uint64_t, std::uint64_t)> progress, const std::optional<std::uint64_t> fixedBase, const bool traceImports, std::function<void(std::string_view)> message, std::string outputName) : progress(std::move(progress)), windowsGui(windowsGui), icon(std::move(icon)), iconName(std::move(iconName)), fixedBase(fixedBase), traceImports(traceImports), message(std::move(message)), outputName(std::move(outputName)) {
if (fixedBase && (*fixedBase < 0x20000 || *fixedBase > 0x7fffffff0000 || (*fixedBase & 0xffff) != 0))
throw Domain::RelinkerException("Invalid fixed Windows guest image base");
}
Expand Down Expand Up @@ -79,7 +80,9 @@ std::vector<std::uint8_t> WindowsPePatcher::Patch(std::vector<std::uint8_t> sour
sections.push_back({".ehmeta", nextRva, SectionRead | 0x40u, std::move(metadata)});
nextRva = AlignRva(nextRva + sections.back().Data.size());
}
directories[9] = WindowsTlsBuilder().Build(sourceElf, originalHeaders, image, sections, relocations.BaseRelocations, nextRva, nullptr, [&](auto completed, auto total) { report(1500 + 6500 * completed / total); }, message);
std::uint32_t tlsIndex = 0;
directories[9] = WindowsTlsBuilder().Build(sourceElf, originalHeaders, image, sections, relocations.BaseRelocations, nextRva, &tlsIndex, [&](auto completed, auto total) { report(1500 + 6500 * completed / total); }, message);
directories[0] = WindowsExportBuilder().BuildExecutable(dynamicSection, image, originalHeaders, tlsIndex, relocations.BaseRelocations, outputName, sections, nextRva);
report(8000);
WindowsTrampolineBuilder().Build(trampolines, image, sections, nextRva);
const WindowsImportBuilder importBuilder;
Expand Down
2 changes: 1 addition & 1 deletion core/relinker/relinker/src/pipeline/Conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ ConversionOutput Conversion::Convert(ConversionInput input, const ConversionOpti
if (macos) {
patcher = std::make_shared<Elfpatcher::MacOs::MacOsMachOPatcher>(options.MacosFixedBase, [&](std::string_view text) { inputProgress.Message(text); }, options.TraceImports);
} else if (windows) {
patcher = std::make_shared<Elfpatcher::Windows::WindowsPePatcher>(options.WindowsGui, std::move(input.Icon), input.InputPath.parent_path() / "sce_sys" / "icon0.png", [&](auto completed, auto total) { inputProgress.Report({Domain::ProgressStage::Image, {}, completed, total}); }, options.WindowsFixedBase, options.TraceImports, [&](std::string_view text) { inputProgress.Message(text); });
patcher = std::make_shared<Elfpatcher::Windows::WindowsPePatcher>(options.WindowsGui, std::move(input.Icon), input.InputPath.parent_path() / "sce_sys" / "icon0.png", [&](auto completed, auto total) { inputProgress.Report({Domain::ProgressStage::Image, {}, completed, total}); }, options.WindowsFixedBase, options.TraceImports, [&](std::string_view text) { inputProgress.Message(text); }, Io::Utf8Path(input.OutputPath.filename()));
} else {
patcher = std::make_shared<Elfpatcher::Linux::LinuxElfPatcher>(
std::make_shared<Elfpatcher::EntryStubBuilder>(),
Expand Down
Loading
Loading