2017-04-15 21:31:17 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <unordered_map>
|
2017-09-29 13:15:48 +00:00
|
|
|
#include <unordered_set>
|
2017-04-15 21:31:17 +00:00
|
|
|
#include <string>
|
|
|
|
#include "BinaryIO.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sstream>
|
2017-04-22 10:26:53 +00:00
|
|
|
#include <memory>
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::unordered_map;
|
|
|
|
|
2017-04-22 10:26:53 +00:00
|
|
|
unordered_map<string,string> sectionMap;
|
|
|
|
bool verbose = false;
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
enum RecordType {
|
|
|
|
kPad = 0,
|
|
|
|
kFirst = 1,
|
|
|
|
kLast = 2,
|
|
|
|
kDictionary = 4,
|
|
|
|
kModule = 5,
|
|
|
|
kEntryPoint = 6,
|
|
|
|
kSize = 7,
|
|
|
|
kContent = 8,
|
|
|
|
kReference = 9,
|
|
|
|
kComputedRef = 10
|
|
|
|
};
|
|
|
|
|
2017-10-15 10:57:06 +00:00
|
|
|
enum ReferenceFlags { // flags field of kReference
|
|
|
|
k16BitPatch = 0x10, // default 32Bit
|
|
|
|
kFromData = 0x01, // default fromCode
|
|
|
|
kA5Relative = 0x80, // default absolute
|
|
|
|
|
|
|
|
kUnknownReferenceFlags = 0x6E // rather a lot, isn't it?
|
|
|
|
// The following flags are known to exist from DumpOBJ,
|
|
|
|
// but their value is unkown as I haven't actually seen them yet:
|
|
|
|
// k32BitOffsets (default k16BitOffsets)
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ComputedReferenceFlags {
|
|
|
|
kDifference = 0x80
|
|
|
|
};
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
struct Reloc
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
string name1;
|
|
|
|
string name2;
|
|
|
|
|
|
|
|
int write(std::ostream& out, uint8_t *p);
|
|
|
|
};
|
|
|
|
|
|
|
|
int Reloc::write(std::ostream& out, uint8_t *p)
|
|
|
|
{
|
2017-10-15 10:57:06 +00:00
|
|
|
if(size == 2)
|
|
|
|
{
|
|
|
|
out << "\t.short ";
|
|
|
|
int val = (int)p[0] * 256 + p[1];
|
|
|
|
|
|
|
|
out << name1;
|
|
|
|
if(name2.size())
|
|
|
|
out << " - " << name2;
|
|
|
|
if(val > 0)
|
|
|
|
out << " + " << val;
|
|
|
|
else if(val < 0)
|
|
|
|
out << " - " << -val;
|
|
|
|
if(name2.size() == 0)
|
|
|
|
out << "-.";
|
|
|
|
out << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if(size == 4)
|
|
|
|
{
|
|
|
|
out << "\t.long ";
|
|
|
|
int val = ((int)p[0] << 24) | ((int)p[1] << 16) | ((int)p[2] << 8) | p[3];
|
|
|
|
|
|
|
|
out << name1;
|
|
|
|
if(name2.size())
|
|
|
|
out << " - " << name2;
|
|
|
|
if(val > 0)
|
|
|
|
out << " + " << val;
|
|
|
|
else if(val < 0)
|
|
|
|
out << " - " << -val;
|
|
|
|
if(name2.size() == 0)
|
|
|
|
out << "-.";
|
|
|
|
out << std::endl;
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
string segment;
|
|
|
|
vector<uint8_t> bytes;
|
2017-04-22 10:26:53 +00:00
|
|
|
unordered_map<uint32_t, vector<string>> labels;
|
2017-04-15 21:31:17 +00:00
|
|
|
unordered_map<uint32_t, Reloc> relocs;
|
|
|
|
|
2017-09-29 13:15:48 +00:00
|
|
|
std::vector<std::weak_ptr<Module>> nearrefs;
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
void write(std::ostream& out);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isValidIdentifier(const string& s)
|
|
|
|
{
|
|
|
|
if(s.empty())
|
|
|
|
return false;
|
|
|
|
if(s[0] != '_' && !isalpha(s[0]))
|
|
|
|
return false;
|
|
|
|
for(char c : s)
|
|
|
|
if(c != '_' && !isalnum(c))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string encodeIdentifier(const string& s)
|
|
|
|
{
|
|
|
|
if(isValidIdentifier(s))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
|
|
|
if(isdigit(s[0]))
|
|
|
|
ss << "__z";
|
|
|
|
|
|
|
|
for(char c : s)
|
|
|
|
{
|
|
|
|
if(c == '_' || isalnum(c))
|
|
|
|
ss << c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ss << "__z" << (int) c << "_";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::write(std::ostream& out)
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
uint32_t offset = 0;
|
2017-09-29 20:21:32 +00:00
|
|
|
string encodedName = encodeIdentifier(sectionMap[name]);
|
2017-04-15 21:31:17 +00:00
|
|
|
|
2017-09-29 20:21:32 +00:00
|
|
|
out << "\t.section .text." << encodedName << ",\"ax\",@progbits\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
while(offset < bytes.size())
|
|
|
|
{
|
|
|
|
auto labelP = labels.find(offset);
|
|
|
|
if(labelP != labels.end())
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
for(string rawLabel : labelP->second)
|
|
|
|
{
|
|
|
|
string label = encodeIdentifier(rawLabel);
|
|
|
|
out << "\t.globl " << label << "\n";
|
|
|
|
out << label << ":\n";
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto relocP = relocs.find(offset);
|
|
|
|
if(relocP != relocs.end())
|
|
|
|
{
|
|
|
|
offset += relocP->second.write(out, &bytes[offset]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << "\t.byte " << (int)bytes[offset] << "\n";
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 20:21:32 +00:00
|
|
|
out << "\t.section .text." << encodedName << ".macsbug,\"ax\",@progbits\n";
|
|
|
|
if(encodedName.size() < 32)
|
|
|
|
out << "\t.byte " << (encodedName.size() | 0x80) << "\n";
|
|
|
|
else
|
|
|
|
out << "\t.byte 0x80\n"
|
|
|
|
<< "\t.byte " << encodedName.size() << "\n";
|
|
|
|
out << "\t.ascii \"" << encodedName << "\"\n";
|
|
|
|
out << "\t.align 2,0\n\t.short 0\n";
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
out << "# ######\n\n";
|
|
|
|
}
|
|
|
|
|
2017-09-29 13:15:48 +00:00
|
|
|
void sortModules(std::vector<std::shared_ptr<Module>>& modules)
|
|
|
|
{
|
|
|
|
std::unordered_set<std::shared_ptr<Module>> unemitted;
|
|
|
|
for(auto& m : modules)
|
|
|
|
unemitted.insert(m);
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::shared_ptr<Module>> nameMap;
|
|
|
|
for(auto& m : modules)
|
|
|
|
for(auto& l : m->labels)
|
|
|
|
for(auto& str : l.second)
|
|
|
|
nameMap[str] = m;
|
|
|
|
|
|
|
|
|
|
|
|
for(auto& m : modules)
|
|
|
|
for(auto& r : m->relocs)
|
|
|
|
{
|
|
|
|
if(r.second.size != 2)
|
|
|
|
continue;
|
|
|
|
if(r.second.name2.empty())
|
|
|
|
{
|
|
|
|
std::shared_ptr<Module> m1;
|
|
|
|
m1 = nameMap.find(r.second.name1)->second;
|
|
|
|
m1->nearrefs.push_back(m);
|
|
|
|
m->nearrefs.push_back(m1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::shared_ptr<Module> m1;
|
|
|
|
m1 = nameMap.find(r.second.name1)->second;
|
|
|
|
std::shared_ptr<Module> m2;
|
|
|
|
m2 = nameMap.find(r.second.name2)->second;
|
|
|
|
m1->nearrefs.push_back(m2);
|
|
|
|
m2->nearrefs.push_back(m1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Module>> sorted;
|
|
|
|
sorted.reserve(modules.size());
|
|
|
|
|
|
|
|
auto p = sorted.begin();
|
|
|
|
|
|
|
|
while(!unemitted.empty())
|
|
|
|
{
|
|
|
|
while(p != sorted.end())
|
|
|
|
{
|
|
|
|
for(auto& m2weak : (*p)->nearrefs)
|
|
|
|
{
|
|
|
|
if(std::shared_ptr<Module> m2 = m2weak.lock())
|
|
|
|
{
|
|
|
|
auto unemittedP = unemitted.find(m2);
|
|
|
|
if(unemittedP != unemitted.end())
|
|
|
|
{
|
|
|
|
sorted.push_back(m2);
|
|
|
|
unemitted.erase(unemittedP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
sorted.push_back(*unemitted.begin());
|
|
|
|
unemitted.erase(unemitted.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
sorted.swap(modules);
|
|
|
|
}
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2017-10-15 10:48:44 +00:00
|
|
|
if(argc != 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: ConvertOBJ mpw.o > retro68.s\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
std::ifstream in(argv[1]);
|
2017-10-15 10:48:44 +00:00
|
|
|
if(!in)
|
|
|
|
{
|
|
|
|
std::cerr << "Could not read imput file \"" << argv[1] << "\"\n";
|
|
|
|
std::cerr << "Usage: ConvertOBJ mpw.o > retro68.s\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
unordered_map<int,string> stringDictionary;
|
2017-09-29 13:15:48 +00:00
|
|
|
|
|
|
|
bool shouldSortModules = true;
|
|
|
|
|
|
|
|
std::shared_ptr<Module> module;
|
|
|
|
std::vector<std::shared_ptr<Module>> modules;
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
std::cout << "\t.text\n\t.align 2\n";
|
|
|
|
|
|
|
|
for(bool endOfObject = false; !endOfObject;) {
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << std::hex << in.tellg() << ": ";
|
2017-04-15 21:31:17 +00:00
|
|
|
int recordType = byte(in);
|
|
|
|
if(!in)
|
|
|
|
{
|
|
|
|
std::cerr << "Unexpected End of File\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Record: " << recordType << std::endl;
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
switch(recordType)
|
|
|
|
{
|
|
|
|
case kPad:
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Pad\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
break;
|
|
|
|
case kFirst:
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
/*int flags =*/ byte(in);
|
|
|
|
int version = word(in);
|
|
|
|
if(verbose)
|
|
|
|
{
|
|
|
|
std::cerr << "First\n";
|
|
|
|
std::cerr << "Version: " << version << std::endl;
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kDictionary:
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
/*int flags =*/ byte(in);
|
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Dictionary\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
int sz = word(in);
|
|
|
|
int stringId = word(in);
|
|
|
|
int end = (int)(in.tellg()) - 6 + sz;
|
|
|
|
while(in.tellg() < end)
|
|
|
|
{
|
|
|
|
int n = byte(in);
|
|
|
|
string s;
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
s += (char) byte(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << s << std::endl;
|
2017-04-15 21:31:17 +00:00
|
|
|
stringDictionary[stringId++] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kModule:
|
|
|
|
{
|
|
|
|
int flags = byte(in);
|
|
|
|
string name = stringDictionary[word(in)];
|
2017-04-22 10:26:53 +00:00
|
|
|
sectionMap[name] = name;
|
2017-04-15 21:31:17 +00:00
|
|
|
string segment = stringDictionary[word(in)];
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Module " << name << "(" << segment << "), flags = " << flags << "\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
|
|
|
|
module.reset(new Module());
|
|
|
|
module->name = name;
|
2017-04-22 10:26:53 +00:00
|
|
|
module->labels[0].push_back(name);
|
2017-09-29 13:15:48 +00:00
|
|
|
modules.push_back(module);
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kContent:
|
|
|
|
{
|
|
|
|
int flags = byte(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
int sz = word(in) - 4;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
if(flags & 0x08)
|
|
|
|
{
|
|
|
|
offset = longword(in);
|
|
|
|
sz -= 4;
|
|
|
|
}
|
2017-04-15 21:31:17 +00:00
|
|
|
assert(module.get());
|
|
|
|
if(module->bytes.size() < offset + sz)
|
|
|
|
module->bytes.resize(offset + sz);
|
2017-04-22 10:26:53 +00:00
|
|
|
in.read((char*) &module->bytes[offset], sz);
|
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Content (offset = " << offset << ", size = " << sz << ")\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kSize:
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
/*int flags =*/ byte(in);
|
2017-04-15 21:31:17 +00:00
|
|
|
long size = longword(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Size " << size << "\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
assert(module.get());
|
|
|
|
module->bytes.resize(size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kReference:
|
|
|
|
{
|
|
|
|
int flags = byte(in);
|
|
|
|
int sz = word(in);
|
|
|
|
int end = (int)(in.tellg()) - 4 + sz;
|
|
|
|
string name = stringDictionary[word(in)];
|
|
|
|
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "Reference to " << name << " at\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
Reloc reloc;
|
|
|
|
reloc.name1 = name;
|
|
|
|
|
2017-10-15 10:57:06 +00:00
|
|
|
if(flags & kUnknownReferenceFlags)
|
|
|
|
{
|
|
|
|
std::cerr << "Unknown relocation flags: 0x" << std::hex << flags << std::endl;
|
|
|
|
std::cerr << "Cannot convert this file.\n";
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
if(flags & kA5Relative)
|
|
|
|
{
|
|
|
|
std::cerr << "Unsupported relocation flags: 0x" << std::hex << flags << std::endl;
|
|
|
|
std::cerr << "MPW .o files with near-model global variables or calls to imported functions will not work.\n";
|
|
|
|
std::cerr << "Cannot convert this file.\n";
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
flags &= ~kFromData; // FIXME: sticking data bits in the text section, not nice, but might sometimes work
|
|
|
|
|
|
|
|
reloc.size = (flags & k16BitPatch ? 2 : 4);
|
|
|
|
|
2017-04-15 21:31:17 +00:00
|
|
|
assert(module);
|
|
|
|
|
|
|
|
while(in.tellg() < end)
|
|
|
|
{
|
|
|
|
int offset = word(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << " " << offset << std::endl;
|
2017-04-15 21:31:17 +00:00
|
|
|
module->relocs[offset] = reloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kEntryPoint:
|
|
|
|
{
|
2017-04-22 10:26:53 +00:00
|
|
|
/*int flags =*/ byte(in);
|
2017-04-15 21:31:17 +00:00
|
|
|
string name = stringDictionary[word(in)];
|
|
|
|
long offset = longword(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << "EntryPoint " << name << " at offset " << offset << "\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
assert(module);
|
2017-04-22 10:26:53 +00:00
|
|
|
module->labels[offset].push_back(name);
|
2017-04-15 21:31:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kComputedRef:
|
|
|
|
{
|
|
|
|
int flags = byte(in);
|
|
|
|
int sz = word(in);
|
|
|
|
int end = (int)(in.tellg()) - 4 + sz;
|
|
|
|
string name1 = stringDictionary[word(in)];
|
|
|
|
string name2 = stringDictionary[word(in)];
|
|
|
|
|
|
|
|
Reloc reloc;
|
|
|
|
reloc.name1 = name1;
|
|
|
|
reloc.name2 = name2;
|
|
|
|
reloc.size = 2;
|
|
|
|
|
|
|
|
assert(flags == 0x90);
|
|
|
|
assert(module);
|
|
|
|
|
2017-04-22 10:26:53 +00:00
|
|
|
string secName = sectionMap[name1];
|
|
|
|
if(secName != "")
|
|
|
|
sectionMap[module->name] = secName;
|
|
|
|
if(verbose)
|
|
|
|
std::cerr << "ComputedReference to " << name1 << " - " << name2 << " at\n";
|
2017-04-15 21:31:17 +00:00
|
|
|
while(in.tellg() < end)
|
|
|
|
{
|
|
|
|
int offset = word(in);
|
2017-04-22 10:26:53 +00:00
|
|
|
if(verbose)
|
|
|
|
std::cerr << " " << offset << std::endl;
|
2017-04-15 21:31:17 +00:00
|
|
|
module->relocs[offset] = reloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kLast:
|
|
|
|
byte(in);
|
|
|
|
endOfObject = true;
|
|
|
|
break;
|
|
|
|
default:
|
2017-04-22 10:26:53 +00:00
|
|
|
std::cerr << "Unknown record (type " << recordType << ") at " << std::hex << in.tellg() << std::endl;
|
2017-04-15 21:31:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 13:15:48 +00:00
|
|
|
if(shouldSortModules)
|
|
|
|
sortModules(modules);
|
|
|
|
for(auto& m : modules)
|
|
|
|
m->write(std::cout);
|
2017-04-15 21:31:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|