__memory __INTERNAL_FUNC__ GenerateCipherExpCode(PCIPHEREXP pCipherExp, __integer *piOutBinSize, \ PPROTOTYPE_CIPHEREXP_ASM_CONFIGURE pCipherExpAsmConfigure, __integer iImageBase, \ __bool bReverse) { __memory pCipherExpCode = NULL; __integer iBinSize = 0; ASM_SOURCE AsmSource; // 产生默认的汇编代码结构 GenerateDefAsmSource(&AsmSource); InitAssembler(&AsmSource, NULL); // 首先选择寄存器 InitCipherExpReg(pCipherExpAsmConfigure); // 进行汇编 AsmCipherExpCode(pCipherExp, pCipherExpAsmConfigure, bReverse); if (bReverse) { if (pCipherExp->bNot) { WriteInst(LINESTART);WriteInst(NOT);WriteInst(pCipherExpAsmConfigure->ResultReg);WriteInst(LINEND); } }/* end if */ WriteInst(END);//写入源代码结束标记 pCipherExpCode = Assembler(&AsmSource, &iBinSize, iImageBase); *piOutBinSize = iBinSize; return pCipherExpCode; }
int main() { ASM_SOURCE Source; ASM_TOKEN TokenList[100] = { ASMER_TK_KW_ADDR_LAB, ASMER_TK_KW_LINEND, ASMER_TK_INST_PUSH, ASMER_TK_REG_EBP, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_REG_EBP, ASMER_TK_REG_ESP, ASMER_TK_KW_LINEND, ASMER_TK_INST_SUB, ASMER_TK_REG_ESP, ASMER_TK_KW_IMMEDIATE32, ASMER_TK_KW_LINEND, ASMER_TK_KW_ADDR_LAB, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_KW_DWORD_PTR, ASMER_TK_REG_EBP, ASMER_TK_KW_SUB, ASMER_TK_KW_DISPLACEMENT8, ASMER_TK_KW_MEMEND, ASMER_TK_KW_IMMEDIATE32, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_KW_DWORD_PTR, ASMER_TK_REG_EBP, ASMER_TK_KW_SUB, ASMER_TK_KW_DISPLACEMENT32, ASMER_TK_KW_MEMEND, ASMER_TK_KW_IMMEDIATE32, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_REG_ESP, ASMER_TK_REG_EBP, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_KW_DWORD_PTR, ASMER_TK_KW_ADDR_LAB, ASMER_TK_KW_MEMEND, ASMER_TK_KW_IMMEDIATE32, ASMER_TK_KW_LINEND, ASMER_TK_INST_POP, ASMER_TK_REG_EBP, ASMER_TK_KW_LINEND, ASMER_TK_INST_RET, ASMER_TK_KW_LINEND, ASMER_TK_INST_JMP, ASMER_TK_KW_ADDR_LAB, ASMER_TK_KW_LINEND, ASMER_TK_KW_ADDR_VALUE_LAB, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_KW_DWORD_PTR, ASMER_TK_REG_EBP, ASMER_TK_KW_SUB, ASMER_TK_KW_DISPLACEMENT32, ASMER_TK_KW_MEMEND, ASMER_TK_KW_IMMEDIATE32, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_REG_ESP, ASMER_TK_REG_EBP, ASMER_TK_KW_LINEND, ASMER_TK_INST_MOV, ASMER_TK_REG_EBP, ASMER_TK_KW_ADDR_VALUE_LAB, ASMER_TK_KW_LINEND, ASMER_TK_END }; DWORD ConstList[100] = { 0x1000, 0x04, 0x1000, 0x1000, 0x2000, 0x1234, 0x1111111, 0xFFFFFFEE }; CHAR *SymbolList[100] = { "label1","label2", "label2","label1","label3","label3" }; Source.pAsmTokenList = TokenList; Source.pConstList = ConstList; Source.pSymbolList = SymbolList; DWORD dwSize = 0; LPBYTE Code = Assembler(&dwSize, &Source, 0x0400000);//映射的地址是随便给出的 return 1; }
uint8_t* initializePatchpoint2(uint8_t* start_addr, uint8_t* slowpath_start, uint8_t* end_addr, StackInfo stack_info, const std::unordered_set<int>& live_outs) { assert(start_addr < slowpath_start); static const int INITIAL_CALL_SIZE = 13; assert(end_addr > slowpath_start + INITIAL_CALL_SIZE); #ifndef NDEBUG // if (VERBOSITY()) printf("initializing patchpoint at %p - %p\n", addr, addr + size); // for (int i = 0; i < size; i++) { // printf("%02x ", *(addr + i)); //} // printf("\n"); // Check the exact form of the patchpoint call. // It's important to make sure that the only live registers // are the ones that are used as arguments; ie it wouldn't // matter if the call happened on %r10 instead of %r11, // but it would matter if there wasn't a mov immediately before // the call, since then %r11 would be live and we couldn't // use it as a temporary. // mov $imm, %r11: ASSERT(start_addr[0] == 0x49, "%x", start_addr[0]); assert(start_addr[1] == 0xbb); // 8 bytes of the addr // callq *%r11: assert(start_addr[10] == 0x41); assert(start_addr[11] == 0xff); assert(start_addr[12] == 0xd3); int i = INITIAL_CALL_SIZE; while (*(start_addr + i) == 0x66 || *(start_addr + i) == 0x0f || *(start_addr + i) == 0x2e) i++; assert(*(start_addr + i) == 0x90 || *(start_addr + i) == 0x1f); #endif void* call_addr = *(void**)&start_addr[2]; Assembler(start_addr, slowpath_start - start_addr).fillWithNops(); std::vector<GenericRegister> regs_to_spill; for (int dwarf_regnum : live_outs) { GenericRegister ru = GenericRegister::fromDwarf(dwarf_regnum); if (ru.type == GenericRegister::GP) { if (ru.gp == RSP || ru.gp.isCalleeSave()) continue; } regs_to_spill.push_back(ru); } Assembler assem(slowpath_start, end_addr - slowpath_start); // if (regs_to_spill.size()) // assem.trap(); assem.emitBatchPush(stack_info, regs_to_spill); uint8_t* rtn = assem.emitCall(call_addr, R11); assem.emitBatchPop(stack_info, regs_to_spill); assem.fillWithNops(); return rtn; }