Let the entry point go through the dispatcher

We used to jump directly to the program entry point (typically
`_start`), without initializing the program counter. We now initialize
the program counter and the let the program start with the dispatcher,
since it seems a safer solution.
This commit is contained in:
Alessandro Di Federico
2017-02-20 12:24:49 +01:00
parent 1a3950a3ea
commit 451a321bfc
+10 -8
View File
@@ -608,17 +608,19 @@ void CodeGenerator::translate(uint64_t VirtualAddress) {
JumpTargets.harvestGlobalData();
VirtualAddress = Binary.entryPoint();
}
JumpTargets.registerJT(VirtualAddress, JumpTargetManager::GlobalData);
BasicBlock *Head = JumpTargets.registerJT(VirtualAddress,
JumpTargetManager::GlobalData);
// Initialize the program counter
auto *StartPC = ConstantInt::get(PCReg->getType(), VirtualAddress);
// Use this instruction as the delimiter for local variables
auto *Delimiter = Builder.CreateStore(StartPC, PCReg);
// Fake jumps to the dispatcher-related basic blocks. This way all the blocks
// are always reachable. Also, use this switch as the delimiter to create
// local variables.
SwitchInst *Delimiter = Builder.CreateSwitch(Builder.getInt8(0), Head);
Delimiter->addCase(Builder.getInt8(1), JumpTargets.dispatcher());
Delimiter->addCase(Builder.getInt8(2), JumpTargets.anyPC());
Delimiter->addCase(Builder.getInt8(3), JumpTargets.unexpectedPC());
// are always reachable.
SwitchInst *ReachSwitch = Builder.CreateSwitch(Builder.getInt8(0),
JumpTargets.dispatcher());
ReachSwitch->addCase(Builder.getInt8(1), JumpTargets.anyPC());
ReachSwitch->addCase(Builder.getInt8(2), JumpTargets.unexpectedPC());
std::tie(VirtualAddress, Entry) = JumpTargets.peek();