Load map files and set function names.

This commit is contained in:
Ben Vanik
2013-12-22 14:04:41 -08:00
parent 47f0397245
commit 9208132ad9
12 changed files with 142 additions and 30 deletions

View File

@@ -9,12 +9,17 @@
#include <alloy/runtime/module.h>
#include <fstream>
#include <sstream>
#include <alloy/runtime/runtime.h>
using namespace alloy;
using namespace alloy::runtime;
Module::Module(Memory* memory) :
memory_(memory) {
Module::Module(Runtime* runtime) :
runtime_(runtime), memory_(runtime->memory()) {
lock_ = AllocMutex(10000);
}
@@ -164,3 +169,74 @@ void Module::ForEachFunction(std::function<void (FunctionInfo*)> callback) {
}
UnlockMutex(lock_);
}
int Module::ReadMap(const char* file_name) {
std::ifstream infile(file_name);
// Skip until ' Address'. Skip the next blank line.
std::string line;
while (std::getline(infile, line)) {
if (line.find(" Address") == 0) {
// Skip the next line.
std::getline(infile, line);
break;
}
}
std::stringstream sstream;
std::string ignore;
std::string name;
std::string addr_str;
std::string type_str;
while (std::getline(infile, line)) {
// Remove newline.
while (line.size() &&
(line[line.size() - 1] == '\r' ||
line[line.size() - 1] == '\n')) {
line.erase(line.end() - 1);
}
// End when we hit the first whitespace.
if (line.size() == 0) {
break;
}
// Line is [ws][ignore][ws][name][ws][hex addr][ws][(f)][ws][library]
sstream.clear();
sstream.str(line);
sstream >> std::ws;
sstream >> ignore;
sstream >> std::ws;
sstream >> name;
sstream >> std::ws;
sstream >> addr_str;
sstream >> std::ws;
sstream >> type_str;
uint32_t address = (uint32_t)strtoul(addr_str.c_str(), NULL, 16);
if (!address) {
continue;
}
if (type_str == "f") {
// Function.
FunctionInfo* fn_info;
if (runtime_->LookupFunctionInfo(this, address, &fn_info)) {
continue;
}
// Don't overwrite names we've set elsewhere.
if (!fn_info->name()) {
// If it's an mangled C++ name (?name@...) just use the name.
if (name[0] == '?') {
size_t at = name.find('@');
name = name.substr(1, at - 1);
}
fn_info->set_name(name.c_str());
}
} else {
// Variable.
}
}
return 0;
}

View File

@@ -21,11 +21,12 @@ namespace alloy {
namespace runtime {
class Function;
class Runtime;
class Module {
public:
Module(Memory* memory);
Module(Runtime* runtime);
virtual ~Module();
Memory* memory() const { return memory_; }
@@ -45,12 +46,15 @@ public:
void ForEachFunction(std::function<void (FunctionInfo*)> callback);
int ReadMap(const char* file_name);
private:
SymbolInfo::Status DeclareSymbol(
SymbolInfo::Type type, uint64_t address, SymbolInfo** out_symbol_info);
SymbolInfo::Status DefineSymbol(SymbolInfo* symbol_info);
protected:
Runtime* runtime_;
Memory* memory_;
private:

View File

@@ -13,10 +13,10 @@ using namespace alloy;
using namespace alloy::runtime;
RawModule::RawModule(Memory* memory) :
RawModule::RawModule(Runtime* runtime) :
name_(0),
base_address_(0), low_address_(0), high_address_(0),
Module(memory) {
Module(runtime) {
}
RawModule::~RawModule() {

View File

@@ -19,7 +19,7 @@ namespace runtime {
class RawModule : public Module {
public:
RawModule(Memory* memory);
RawModule(Runtime* runtime);
virtual ~RawModule();
int LoadFile(uint64_t base_address, const char* path);

View File

@@ -195,11 +195,16 @@ int Runtime::LookupFunctionInfo(
return 1;
}
return LookupFunctionInfo(code_module, address, out_symbol_info);
}
int Runtime::LookupFunctionInfo(Module* module, uint64_t address,
FunctionInfo** out_symbol_info) {
// Atomic create/lookup symbol in module.
// If we get back the NEW flag we must declare it now.
FunctionInfo* symbol_info = NULL;
SymbolInfo::Status symbol_status =
code_module->DeclareFunction(address, &symbol_info);
module->DeclareFunction(address, &symbol_info);
if (symbol_status == SymbolInfo::STATUS_NEW) {
// Symbol is undeclared, so declare now.
int result = frontend_->DeclareFunction(symbol_info);

View File

@@ -47,6 +47,8 @@ public:
ModuleList GetModules();
int LookupFunctionInfo(uint64_t address, FunctionInfo** out_symbol_info);
int LookupFunctionInfo(Module* module, uint64_t address,
FunctionInfo** out_symbol_info);
int ResolveFunction(uint64_t address, Function** out_function);
void AddRegisterAccessCallbacks(

View File

@@ -15,10 +15,20 @@ using namespace alloy::runtime;
SymbolInfo::SymbolInfo(Type type, Module* module, uint64_t address) :
type_(type), status_(STATUS_DEFINING),
module_(module), address_(address) {
module_(module), address_(address), name_(0) {
}
SymbolInfo::~SymbolInfo() {
if (name_) {
xe_free(name_);
}
}
void SymbolInfo::set_name(const char* name) {
if (name_) {
xe_free(name_);
}
name_ = xestrdupa(name);
}
FunctionInfo::FunctionInfo(Module* module, uint64_t address) :

View File

@@ -44,11 +44,16 @@ public:
void set_status(Status value) { status_ = value; }
uint64_t address() const { return address_; }
const char* name() const { return name_; }
void set_name(const char* name);
protected:
Type type_;
Module* module_;
Status status_;
uint64_t address_;
char* name_;
};
class FunctionInfo : public SymbolInfo {