Skip to content

Commit

Permalink
Allow insert call with more than 8 parameters on aarch64
Browse files Browse the repository at this point in the history
Fix bug on insert call to local function on rewrite mode
  • Loading branch information
Sasha Nicolas (zeroah) committed Oct 28, 2020
1 parent 4311e94 commit 56c0989
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
53 changes: 47 additions & 6 deletions dyninstAPI/src/inst-aarch64.C
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ Register EmitterAARCH64::emitCall(opCode op,
assert(0);
}

inst_printf("emitCall to: %s\n", callee->name().c_str());

vector<int> savedRegs;

// save r0-r7
Expand All @@ -641,7 +643,8 @@ Register EmitterAARCH64::emitCall(opCode op,
}

// Passing operands to registers
for(size_t id = 0; id < operands.size(); id++)
// first 8 put into registers r0-r7
for(size_t id = 0; id < operands.size() && id < 8; id++)
{
Register reg = REG_NULL;
if (gen.rs()->allocateSpecificRegister(gen, registerSpace::r0 + id, true))
Expand All @@ -653,19 +656,47 @@ Register EmitterAARCH64::emitCall(opCode op,
assert(reg!=REG_NULL);
}

// remaining parameters put into the stack
auto num_operands = operands.size();
if( num_operands > 8 ){
// adjust stack
insnCodeGen::generateAddSubImmediate(gen, insnCodeGen::Sub, 0,
ALIGN_QUADWORD( (num_operands-8)*8 ), REG_SP, REG_SP, true);

// generate parameter code and load into the stack
for(size_t id = 8; id < num_operands; id++)
{
Register scratch = gen.rs()->getScratchRegister(gen);
Address unnecessary = ADDR_NULL;
if (!operands[id]->generateCode_phase2(gen, false, unnecessary, scratch))
assert(0);

// move scratch to stack
int offset_from_sp = 8 * (id-8);
insnCodeGen::saveRegister(gen, scratch, offset_from_sp);

}
}

assert(gen.rs());

//Address of function to call in scratch register
Register scratch = gen.rs()->getScratchRegister(gen);
assert(scratch != REG_NULL && "cannot get a scratch register");
gen.markRegDefined(scratch);

if (gen.addrSpace()->edit() != NULL) {
// gen.as.edit() checks if we are in rewriter mode
Address dest = getInterModuleFuncAddr(callee, gen);
// prepare register with address to call
// if ( rewrite mode )
// - if ( callee in different module ) call getInterModuleFunc
// else (attach or create mode)
// - use the scratch register to load calle address
//
if (gen.addrSpace()->edit() != NULL) { // rewriter mode
Address dest = callee->addr();
if( gen.func()->obj() != callee->obj() )
dest = getInterModuleFuncAddr(callee, gen);

// emit ADR instruction

long disp = dest - gen.currAddr();
instruction insn;
insn.clear();
Expand All @@ -676,11 +707,13 @@ Register EmitterAARCH64::emitCall(opCode op,
INSN_SET(insn, 0, 4, scratch);
insnCodeGen::generate(gen, insn);

insnCodeGen::generateMemAccess(gen, insnCodeGen::Load, scratch, scratch, 0, 8, insnCodeGen::Offset);
if( gen.func()->obj() != callee->obj() )
insnCodeGen::generateMemAccess(gen, insnCodeGen::Load, scratch, scratch, 0, 8, insnCodeGen::Offset);
} else {
insnCodeGen::loadImmIntoReg<Address>(gen, scratch, callee->addr());
}

// emit BL instruction
instruction branchInsn;
branchInsn.clear();

Expand All @@ -697,6 +730,13 @@ Register EmitterAARCH64::emitCall(opCode op,
INSN_SET(branchInsn, 21, 21, 1);
insnCodeGen::generate(gen, branchInsn);

// reset stack pointer if used for parameters
if( num_operands > 8 ){
// re-adjust stack
insnCodeGen::generateAddSubImmediate(gen, insnCodeGen::Add, 0,
ALIGN_QUADWORD( (num_operands-8)*8 ), REG_SP, REG_SP, true);
}

/*
* Restoring registers
*/
Expand Down Expand Up @@ -1739,6 +1779,7 @@ Address Emitter::getInterModuleFuncAddr(func_instance *func, codeGen &gen) {
if (!binEdit || !func) {
assert(!"Invalid function call (function info is missing)");
}
inst_printf("getInterModuleFuncAddr to %s\n", func->get_name().c_str());

// find the Symbol corresponding to the func_instance
std::vector<SymtabAPI::Symbol *> syms;
Expand Down
2 changes: 2 additions & 0 deletions symtabAPI/src/emitElf.C
Original file line number Diff line number Diff line change
Expand Up @@ -1744,13 +1744,15 @@ bool emitElf<ElfTypes>::createSymbolTables(set<Symbol *> &allSymbols) {
}
}

rewrite_printf("dynamic symbols: \n");
for (auto sym_iter = allSymbols.begin(); sym_iter != allSymbols.end(); ++sym_iter) {
if ((*sym_iter)->isInSymtab()) {
allSymSymbols.push_back(*sym_iter);
}
if (!obj->isStaticBinary()) {
if ((*sym_iter)->isInDynSymtab()) {
allDynSymbols.push_back(*sym_iter);
rewrite_printf("\t%s\n", (*sym_iter)->getMangledName().c_str());
}
}
}
Expand Down

0 comments on commit 56c0989

Please sign in to comment.