+void Compiler::RelocateScript(char *text, unsigned int textlen) {
+ for (char *i = text, *end = text + textlen; i < end;) {
+ common::Script::Code *code =
+ reinterpret_cast<common::Script::Code *>(i);
+ if (code->type == common::Script::TYPE_ADDRESS && code->numParams > 0) {
+ if (code->reg1 == 7) {
+ char *addr = i + sizeof(common::Script::Code);
+ std::map<const void *, unsigned int>::const_iterator
+ found(addressMap.find(*reinterpret_cast<void **>(addr)));
+ if (found == addressMap.end()) {
+ throw std::runtime_error("unable to relocate script code");
+ }
+ *reinterpret_cast<unsigned int *>(addr) = found->second;
+ }
+
+ if (code->numParams > 1 && code->reg2 == 7) {
+ char *addr = i + sizeof(common::Script::Code);
+ if (code->reg1 == 7) {
+ addr += sizeof(void *);
+ }
+ std::map<const void *, unsigned int>::const_iterator
+ found(addressMap.find(*reinterpret_cast<void **>(addr)));
+ if (found == addressMap.end()) {
+ throw std::runtime_error("unable to relocate script code");
+ }
+ *reinterpret_cast<unsigned int *>(addr) = found->second;
+ }
+ }
+ i += code->Size();
+ }
+}
+