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
22 changes: 18 additions & 4 deletions core/relinker/relinker/src/guest/GuestModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,19 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path
if (end == dynamic.DynStrData.end()) throw Domain::RelinkerException("Unterminated executable symbol name");
const std::string name(start, end);
if (declaresHost(executableModules[offset / 24])) continue;
rejectSharedImport(name.substr(0, name.find('#')), Io::Utf8Path(inputPath));
const std::string base = name.substr(0, name.find('#'));
const auto shared = sharedExports.find(base);
std::size_t needed = 0;
if (shared != sharedExports.end()) {
for (const auto provider : shared->second) {
const auto& file = images[provider].SourcePath.filename().string();
const auto& soname = images[provider].Soname;
if (std::find(neededNames.begin(), neededNames.end(), file) == neededNames.end() &&
(soname.empty() || std::find(neededNames.begin(), neededNames.end(), soname) == neededNames.end())) continue;
if (++needed > 1) break;
}
}
if (needed != 1) rejectSharedImport(base, Io::Utf8Path(inputPath));
if (!windows && exports.contains(name)) rename(dynamic.DynSymData, dynamic.DynStrData, offset / 24, name);
}
std::vector<std::set<std::size_t>> dependencies(images.size());
Expand All @@ -255,13 +267,15 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path
for (std::size_t symbolIndex = 0; symbolIndex < images[index].Symbols.size(); ++symbolIndex) {
const auto& symbol = images[index].Symbols[symbolIndex];
if (symbol.Section != 0 || symbol.Name.empty() || declaresHost(symbol.Library)) continue;
rejectSharedImport(symbol.Name, Io::Utf8Path(images[index].SourcePath));
const auto declared = symbol.Library.empty() ? guestNames.end() : findGuest(symbol.Library);
const auto shared = sharedExports.find(symbol.Name);
if (shared == sharedExports.end() || declared == guestNames.end() || !shared->second.contains(declared->second))
rejectSharedImport(symbol.Name, Io::Utf8Path(images[index].SourcePath));
const auto found = exports.find(symbol.Name);
std::vector<std::size_t> providers;
if (found != exports.end()) {
for (const auto provider : found->second) {
const auto declared = findGuest(symbol.Library);
if (!windows || symbol.Library.empty() || (declared != guestNames.end() && declared->second == provider)) providers.push_back(provider);
if (symbol.Library.empty() || (declared != guestNames.end() && declared->second == provider)) providers.push_back(provider);
}
}
if (providers.size() > 1) throw Domain::RelinkerException("Ambiguous guest import after stripping #: " + symbol.Name);
Expand Down
13 changes: 13 additions & 0 deletions core/relinker/relinker/tests/test_guest_symbol_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ def main():
imports = imported_symbols(declared.read_bytes())
assert imports == {"AAAAAAAAAAA#guest", "EEEEEEEEEEE"}, imports

twins = {"libc.prx": guest_module([("AAAAAAAAAAA", True)]), "other.prx": guest_module([("AAAAAAAAAAA", True)])}
bound = relink(relinker, work / "needed-one", importing_executable(["AAAAAAAAAAA#B#B"], ["libc"]), twins)
imports = imported_symbols(bound.read_bytes())
assert imports == {"AAAAAAAAAAA#guest"}, imports

(work / "needed-both" / "sce_module").mkdir(parents=True)
(work / "needed-both" / "input.elf").write_bytes(importing_executable(["AAAAAAAAAAA#B#B"], ["libc", "other"]))
for name, image in twins.items():
(work / "needed-both" / "sce_module" / name).write_bytes(image)
result = subprocess.run([str(relinker), str(work / "needed-both" / "input.elf"), str(work / "needed-both" / "output.elf")],
capture_output=True, text=True, timeout=30)
assert result.returncode != 0 and "Ambiguous guest import AAAAAAAAAAA" in result.stderr, (result.stdout, result.stderr)

guests = relink(relinker, work / "guest-declared", importing_executable([]), {
"b.prx": provider(7),
"c.prx": consumer("libSceLibcInternal.prx", module_name="libSceLibcInternal"),
Expand Down
Loading