2013-01-13 07:25:41 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright 2013 Ben Vanik. All rights reserved. *
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2015-05-02 09:11:11 +00:00
|
|
|
#include "xenia/cpu/export_resolver.h"
|
2013-01-13 07:25:41 +00:00
|
|
|
|
2015-05-02 10:42:51 +00:00
|
|
|
#include "xenia/base/assert.h"
|
|
|
|
#include "xenia/base/math.h"
|
2013-01-13 07:25:41 +00:00
|
|
|
|
2014-08-17 18:48:29 +00:00
|
|
|
namespace xe {
|
2015-05-02 09:11:11 +00:00
|
|
|
namespace cpu {
|
2013-01-20 09:13:59 +00:00
|
|
|
|
2015-09-21 04:31:05 +00:00
|
|
|
ExportResolver::Table::Table(const char* module_name,
|
|
|
|
const std::vector<Export*>* exports_by_ordinal)
|
|
|
|
: exports_by_ordinal_(exports_by_ordinal) {
|
|
|
|
auto dot_pos = std::strrchr(module_name, '.');
|
|
|
|
if (dot_pos != nullptr) {
|
|
|
|
std::strncpy(module_name_, module_name,
|
|
|
|
static_cast<size_t>(dot_pos - module_name));
|
|
|
|
} else {
|
|
|
|
std::strncpy(module_name_, module_name, xe::countof(module_name_) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports_by_name_.reserve(exports_by_ordinal_->size());
|
|
|
|
for (size_t i = 0; i < exports_by_ordinal_->size(); ++i) {
|
2015-09-22 07:02:06 +00:00
|
|
|
auto export_entry = exports_by_ordinal_->at(i);
|
|
|
|
if (export_entry) {
|
|
|
|
exports_by_name_.push_back(export_entry);
|
2015-09-21 04:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(
|
|
|
|
exports_by_name_.begin(), exports_by_name_.end(),
|
|
|
|
[](Export* a, Export* b) { return std::strcmp(a->name, b->name) <= 0; });
|
|
|
|
}
|
|
|
|
|
2015-07-20 01:32:48 +00:00
|
|
|
ExportResolver::ExportResolver() = default;
|
2013-01-20 09:13:59 +00:00
|
|
|
|
2015-07-20 01:32:48 +00:00
|
|
|
ExportResolver::~ExportResolver() = default;
|
2013-01-13 07:25:41 +00:00
|
|
|
|
2015-06-01 00:54:44 +00:00
|
|
|
void ExportResolver::RegisterTable(
|
2015-09-21 04:31:05 +00:00
|
|
|
const char* module_name, const std::vector<xe::cpu::Export*>* exports) {
|
|
|
|
tables_.emplace_back(module_name, exports);
|
|
|
|
|
|
|
|
all_exports_by_name_.reserve(all_exports_by_name_.size() + exports->size());
|
|
|
|
for (size_t i = 0; i < exports->size(); ++i) {
|
2015-09-22 07:02:06 +00:00
|
|
|
auto export_entry = exports->at(i);
|
|
|
|
if (export_entry) {
|
|
|
|
all_exports_by_name_.push_back(export_entry);
|
2015-09-21 04:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(
|
|
|
|
all_exports_by_name_.begin(), all_exports_by_name_.end(),
|
|
|
|
[](Export* a, Export* b) { return std::strcmp(a->name, b->name) <= 0; });
|
2013-01-13 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 04:31:05 +00:00
|
|
|
Export* ExportResolver::GetExportByOrdinal(const char* module_name,
|
2015-05-31 23:58:12 +00:00
|
|
|
uint16_t ordinal) {
|
2014-08-17 18:48:29 +00:00
|
|
|
for (const auto& table : tables_) {
|
2015-09-21 04:31:05 +00:00
|
|
|
if (std::strncmp(module_name, table.module_name(),
|
|
|
|
std::strlen(table.module_name())) == 0) {
|
|
|
|
if (ordinal > table.exports_by_ordinal().size()) {
|
2015-06-01 00:54:44 +00:00
|
|
|
return nullptr;
|
2013-01-13 07:25:41 +00:00
|
|
|
}
|
2015-09-21 04:31:05 +00:00
|
|
|
return table.exports_by_ordinal().at(ordinal);
|
2013-01-13 07:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-17 18:48:29 +00:00
|
|
|
return nullptr;
|
2013-01-13 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 04:31:05 +00:00
|
|
|
void ExportResolver::SetVariableMapping(const char* module_name,
|
2015-05-31 23:58:12 +00:00
|
|
|
uint16_t ordinal, uint32_t value) {
|
2015-09-21 04:31:05 +00:00
|
|
|
auto export_entry = GetExportByOrdinal(module_name, ordinal);
|
2015-07-20 01:32:48 +00:00
|
|
|
assert_not_null(export_entry);
|
|
|
|
export_entry->tags |= ExportTag::kImplemented;
|
|
|
|
export_entry->variable_ptr = value;
|
2013-01-28 11:03:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 04:31:05 +00:00
|
|
|
void ExportResolver::SetFunctionMapping(const char* module_name,
|
2015-05-31 23:58:12 +00:00
|
|
|
uint16_t ordinal,
|
2014-08-17 18:48:29 +00:00
|
|
|
xe_kernel_export_shim_fn shim) {
|
2015-09-21 04:31:05 +00:00
|
|
|
auto export_entry = GetExportByOrdinal(module_name, ordinal);
|
2015-07-20 01:32:48 +00:00
|
|
|
assert_not_null(export_entry);
|
|
|
|
export_entry->tags |= ExportTag::kImplemented;
|
|
|
|
export_entry->function_data.shim = shim;
|
2013-01-28 11:03:37 +00:00
|
|
|
}
|
2014-08-17 18:48:29 +00:00
|
|
|
|
2015-09-21 04:31:05 +00:00
|
|
|
void ExportResolver::SetFunctionMapping(const char* module_name,
|
2015-05-31 23:58:12 +00:00
|
|
|
uint16_t ordinal,
|
|
|
|
ExportTrampoline trampoline) {
|
2015-09-21 04:31:05 +00:00
|
|
|
auto export_entry = GetExportByOrdinal(module_name, ordinal);
|
2015-07-20 01:32:48 +00:00
|
|
|
assert_not_null(export_entry);
|
|
|
|
export_entry->tags |= ExportTag::kImplemented;
|
|
|
|
export_entry->function_data.trampoline = trampoline;
|
2015-05-31 23:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 09:11:11 +00:00
|
|
|
} // namespace cpu
|
2014-08-17 18:48:29 +00:00
|
|
|
} // namespace xe
|