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
17 changes: 12 additions & 5 deletions core/relinker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,13 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_rela_size.py $<TARGET_FILE:relinker>)
add_test(NAME none_relocations COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_none_relocations.py $<TARGET_FILE:relinker>)
add_test(NAME guest_none_relocations COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_none_relocations.py $<TARGET_FILE:relinker> plain)
add_test(NAME guest_none_relocations_sce COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_none_relocations.py $<TARGET_FILE:relinker> sce)
set_tests_properties(guest_none_relocations guest_none_relocations_sce PROPERTIES TIMEOUT 20)
foreach(mode IN ITEMS plain sce)
foreach(system IN ITEMS linux windows macos rejected)
add_test(NAME guest_none_relocations_${mode}_${system} COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_none_relocations.py $<TARGET_FILE:relinker> ${mode} ${system})
set_tests_properties(guest_none_relocations_${mode}_${system} PROPERTIES TIMEOUT 20)
endforeach()
endforeach()
add_test(NAME interpreter_in_code COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_interpreter_in_code.py $<TARGET_FILE:relinker>)
set_tests_properties(interpreter_in_code PROPERTIES TIMEOUT 20)
Expand Down Expand Up @@ -689,6 +691,11 @@ if(BUILD_TESTING)
add_test(NAME gs_trap_entry_roots COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_gs_trap_entry_roots.py $<TARGET_FILE:relinker>)
set_tests_properties(gs_trap_entry_roots PROPERTIES TIMEOUT 30)
foreach(group IN ITEMS executable module rejected)
add_test(NAME function_symbol_roots_${group} COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_function_symbol_roots.py $<TARGET_FILE:relinker> ${group})
set_tests_properties(function_symbol_roots_${group} PROPERTIES TIMEOUT 30)
endforeach()
set(tlsCoverageGroups metadata padding register_loads displacement_bounds alu_execution register_address_loads moved_branch rejected)
foreach(register IN ITEMS 0 1 2 3 5 6 7 8 9 10 11 12 13 14 15)
list(APPEND tlsCoverageGroups displacement_r${register})
Expand Down
1 change: 1 addition & 0 deletions core/relinker/cli/include/Cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Args {
bool showVersion = false;
bool skipSyscallCheck = false;
bool trapSdkSyscallStubs = false;
bool functionSymbolRoots = false;
bool skipSceModule = false;
bool toIntel = false;
bool toRosetta = false;
Expand Down
5 changes: 4 additions & 1 deletion core/relinker/cli/src/CliArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const char* Usage() {
" --lazy-binding Deprecated. Enable lazy symbol binding instead of eager binding.\n"
"\n"
"Other:\n"
" --function-symbol-roots Analyze function symbols as entry points instead of sized ranges.\n"
" --help Print this help and exit with code 0.\n"
" --version Print the build revision and exit with code 0.\n"
" -- Treat all remaining arguments as paths.\n"
Expand All @@ -71,7 +72,7 @@ namespace {
constexpr std::string_view kVerifyRejectedOptions[] = {
"--dry-run",
"--windows", "--macos", "--windows-gui", "--windows-diagnostics", "--windows-fixed-base", "--trace-imports", "--linux-fixed-base", "--macos-fixed-base",
"--to-intel", "--to-rosetta", "--frame-red-zone", "--skip-syscall-check", "--trap-sdk-syscall-stubs", "unused-filter=",
"--to-intel", "--to-rosetta", "--frame-red-zone", "--skip-syscall-check", "--trap-sdk-syscall-stubs", "--function-symbol-roots", "unused-filter=",
"--rpath", "--registry", "--omit-unavailable-dependencies", "--lazy-binding", "--autorun", "--patches", "--no-patches",
};

Expand Down Expand Up @@ -145,6 +146,8 @@ Args ParseArgs(int argc, char* argv[]) {
args.skipSyscallCheck = true;
} else if (arg == "--trap-sdk-syscall-stubs") {
args.trapSdkSyscallStubs = true;
} else if (arg == "--function-symbol-roots") {
args.functionSymbolRoots = true;
} else if (arg == "--skip-sce-module") {
args.skipSceModule = true;
} else if (arg == "--sce-module-path") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct MacOsTraceImport {

struct MacOsImageInput {
bool Executable = true;
bool FunctionSymbolRoots = false;
std::string InstallName;
const std::vector<std::uint8_t>* Source = nullptr;
const std::vector<Domain::ProgramHeader>* Headers = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Elfpatcher::MacOs {

class MacOsMachOPatcher final : public IElfPatcher {
public:
explicit MacOsMachOPatcher(std::optional<std::uint64_t> fixedBase = std::nullopt, std::function<void(std::string_view)> message = {}, bool traceImports = false);
explicit MacOsMachOPatcher(std::optional<std::uint64_t> fixedBase = std::nullopt, std::function<void(std::string_view)> message = {}, bool traceImports = false, bool functionSymbolRoots = false);

std::vector<std::uint8_t> Patch(
std::vector<std::uint8_t> sourceElf,
Expand All @@ -28,6 +28,7 @@ class MacOsMachOPatcher final : public IElfPatcher {
std::optional<std::uint64_t> fixedBase;
std::function<void(std::string_view)> message;
bool traceImports;
bool functionSymbolRoots;
};

}
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 = {}, std::string outputName = "app.exe");
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", bool functionSymbolRoots = false);
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 @@ -25,6 +25,7 @@ class WindowsPePatcher : public IElfPatcher {
std::filesystem::path iconName;
std::optional<std::uint64_t> fixedBase;
bool traceImports;
bool functionSymbolRoots;
std::function<void(std::string_view)> message;
std::string outputName;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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;
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, bool functionSymbolRoots = false) const;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elfpatcher::Windows {

class WindowsTlsBuilder {
public:
PeDirectory Build(const std::vector<std::uint8_t>& source, const std::vector<Domain::ProgramHeader>& headers, const WindowsLoadImage& image, std::vector<PeSection>& sections, std::vector<std::uint32_t>& relocations, std::uint32_t& nextRva, std::uint32_t* tlsIndexRva = nullptr, const std::function<void(std::uint64_t, std::uint64_t)>& progress = {}, const std::function<void(std::string_view)>& message = {}) const;
PeDirectory Build(const std::vector<std::uint8_t>& source, const std::vector<Domain::ProgramHeader>& headers, const WindowsLoadImage& image, std::vector<PeSection>& sections, std::vector<std::uint32_t>& relocations, std::uint32_t& nextRva, std::uint32_t* tlsIndexRva = nullptr, const std::function<void(std::uint64_t, std::uint64_t)>& progress = {}, const std::function<void(std::string_view)>& message = {}, bool functionSymbolRoots = false) const;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteMacOs(const Relinker::GuestIma
Windows::WindowsLoadImage image(guest.Bytes, guest.Headers, false);
MacOsImageInput input;
input.Executable = false;
input.FunctionSymbolRoots = guest.FunctionSymbolRoots;
input.InstallName = "@rpath/" + guest.OutputName;
input.Source = &guest.Bytes;
input.Headers = &guest.Headers;
Expand Down Expand Up @@ -89,7 +90,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteMacOs(const Relinker::GuestIma
input.TlsExports.push_back({name, symbol.Value, flags});
continue;
}
input.Exports.push_back({name, image.GetRva(symbol.Value, std::max<std::uint64_t>(symbol.Size, 1)), flags});
input.Exports.push_back({name, image.GetRva(symbol.Value, guest.FunctionSymbolRoots && (symbol.Info & 15) == 2 ? 1 : std::max<std::uint64_t>(symbol.Size, 1)), flags});
}

for (const auto& dependency : dependencies) input.Dylibs.push_back(MachODependency(dependency, "@loader_path"));
Expand Down
6 changes: 3 additions & 3 deletions core/relinker/elfpatcher/src/macos/MacOsImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ struct TlsScan {
std::vector<GsTrap> GsTraps;
};

TlsScan ScanTls(const std::vector<std::uint8_t>& source, const std::vector<Domain::ProgramHeader>& headers, const Windows::WindowsLoadImage& image, const std::function<void(std::string_view)>& message) {
TlsScan ScanTls(const std::vector<std::uint8_t>& source, const std::vector<Domain::ProgramHeader>& headers, const Windows::WindowsLoadImage& image, const std::function<void(std::string_view)>& message, const bool functionSymbolRoots) {
TlsScan scan;
const Codegen::X64InstructionDecoder decoder;
std::set<Domain::VirtualAddress> entryRoots;
const auto instructions = Relinker::CodeInstructionCollector().Collect(source, headers, {}, &entryRoots, message);
const auto instructions = Relinker::CodeInstructionCollector().Collect(source, headers, {}, &entryRoots, message, functionSymbolRoots);
for (const auto& header : headers) {
if (header.Type == 7) {
if (scan.Tls != nullptr) throw Domain::RelinkerException("Multiple ELF TLS segments");
Expand Down Expand Up @@ -457,7 +457,7 @@ std::vector<std::uint8_t> WriteMacOsImage(MacOsImageInput& input) {
auto& image = *input.Image;
const auto& headers = *input.Headers;
const auto imageBase = image.GetImageBase();
const auto tls = ScanTls(*input.Source, headers, image, input.Message);
const auto tls = ScanTls(*input.Source, headers, image, input.Message, input.FunctionSymbolRoots);
const bool hasTls = tls.Tls != nullptr && (tls.Tls->MemorySize != 0 || !tls.Accesses.empty());
if (!hasTls && !tls.Accesses.empty()) throw Domain::RelinkerException("Guest TLS access without a non-empty PT_TLS");
if (!hasTls && !input.TlsModuleSlots.empty()) throw Domain::RelinkerException("Guest TLS module relocation without a non-empty PT_TLS");
Expand Down
5 changes: 3 additions & 2 deletions core/relinker/elfpatcher/src/macos/MacOsMachOPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::string MachODependency(const std::string& name, const std::string& origin)
return "@rpath/" + name;
}

MacOsMachOPatcher::MacOsMachOPatcher(const std::optional<std::uint64_t> fixedBase, std::function<void(std::string_view)> message, const bool traceImports) : fixedBase(fixedBase), message(std::move(message)), traceImports(traceImports) {
MacOsMachOPatcher::MacOsMachOPatcher(const std::optional<std::uint64_t> fixedBase, std::function<void(std::string_view)> message, const bool traceImports, const bool functionSymbolRoots) : fixedBase(fixedBase), message(std::move(message)), traceImports(traceImports), functionSymbolRoots(functionSymbolRoots) {
if (fixedBase && (*fixedBase < 0x20000 || *fixedBase > 0x7fffffff0000 || (*fixedBase & 0xffff) != 0))
throw Domain::RelinkerException("Invalid fixed macOS guest image base");
}
Expand All @@ -72,6 +72,7 @@ std::vector<std::uint8_t> MacOsMachOPatcher::Patch(std::vector<std::uint8_t> sou

MacOsImageInput input;
input.Executable = true;
input.FunctionSymbolRoots = functionSymbolRoots;
input.Source = &sourceElf;
input.Headers = &originalHeaders;
input.Image = &image;
Expand All @@ -94,7 +95,7 @@ std::vector<std::uint8_t> MacOsMachOPatcher::Patch(std::vector<std::uint8_t> sou
} else if (section == SHN_ABS) input.Exports.push_back({name, value, flags | ExportKindAbsolute});
else {
if (section >= SHN_LORESERVE) throw Domain::RelinkerException("Unsupported macOS executable export section: " + name);
input.Exports.push_back({name, image.GetRva(value, std::max<std::uint64_t>(Io::ReadU64(symbols, offset + 16), 1)), flags});
input.Exports.push_back({name, image.GetRva(value, functionSymbolRoots && (info & 15) == STT_FUNC ? 1 : std::max<std::uint64_t>(Io::ReadU64(symbols, offset + 16), 1)), flags});
}
}
input.Rebases = relocations.BaseRelocations;
Expand Down
4 changes: 2 additions & 2 deletions core/relinker/elfpatcher/src/windows/WindowsExportBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PeDirectory WindowsExportBuilder::Build(const std::map<std::string, std::uint32_
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 {
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 bool functionSymbolRoots) 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;
Expand Down Expand Up @@ -71,7 +71,7 @@ PeDirectory WindowsExportBuilder::BuildExecutable(const Domain::SysVDynamicSecti
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));
rva = image.GetRva(symbol.Value, functionSymbolRoots && type == 2 ? 1 : std::max<std::uint64_t>(symbol.Size, 1));
}
exports.emplace(symbolName, rva);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
std::array<PeDirectory, 16> directories{};
std::uint32_t tlsIndex = 0;
try {
directories[9] = WindowsTlsBuilder().Build(guest.Bytes, guest.Headers, image, sections, relocations, nextRva, &tlsIndex, {}, message);
directories[9] = WindowsTlsBuilder().Build(guest.Bytes, guest.Headers, image, sections, relocations, nextRva, &tlsIndex, {}, message, guest.FunctionSymbolRoots);
} catch (Domain::RelinkerException& error) {
if (error.InputPath.empty()) error.InputPath = Io::Utf8Path(guest.SourcePath);
throw;
Expand Down Expand Up @@ -140,7 +140,7 @@ std::vector<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
Io::AppendU64(tlsExports.Data, ImageBase + tlsIndex);
Io::AppendU64(tlsExports.Data, symbol.Value);
relocations.push_back(rva);
} else rva = image.GetRva(symbol.Value, std::max<std::uint64_t>(symbol.Size, 1));
} else rva = image.GetRva(symbol.Value, guest.FunctionSymbolRoots && (symbol.Info & 15) == 2 ? 1 : std::max<std::uint64_t>(symbol.Size, 1));
if (!exports.emplace(symbol.Name, rva).second) throw Domain::RelinkerException("Duplicate guest export: " + symbol.Name);
if ((symbol.Info & 15) == 2 && returnsZero(symbol.Value)) runtime.ZeroReturnExports.insert(symbol.Name);
}
Expand Down
6 changes: 3 additions & 3 deletions core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,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, 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)) {
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, const bool functionSymbolRoots) : progress(std::move(progress)), windowsGui(windowsGui), icon(std::move(icon)), iconName(std::move(iconName)), fixedBase(fixedBase), traceImports(traceImports), functionSymbolRoots(functionSymbolRoots), 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 @@ -81,8 +81,8 @@ std::vector<std::uint8_t> WindowsPePatcher::Patch(std::vector<std::uint8_t> sour
nextRva = AlignRva(nextRva + sections.back().Data.size());
}
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);
directories[9] = WindowsTlsBuilder().Build(sourceElf, originalHeaders, image, sections, relocations.BaseRelocations, nextRva, &tlsIndex, [&](auto completed, auto total) { report(1500 + 6500 * completed / total); }, message, functionSymbolRoots);
directories[0] = WindowsExportBuilder().BuildExecutable(dynamicSection, image, originalHeaders, tlsIndex, relocations.BaseRelocations, outputName, sections, nextRva, functionSymbolRoots);
report(8000);
WindowsTrampolineBuilder().Build(trampolines, image, sections, nextRva);
const WindowsImportBuilder importBuilder;
Expand Down
Loading
Loading