@@ -1187,7 +1187,8 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) {
11871187 uint64_t NumDynamicLocalLoadsFound = 0 ;
11881188
11891189 for (BinaryBasicBlock *BB : BF.getLayout ().blocks ()) {
1190- for (MCInst &Inst : *BB) {
1190+ for (auto It = BB->begin (); It != BB->end (); ++It) {
1191+ const MCInst &Inst = *It;
11911192 unsigned Opcode = Inst.getOpcode ();
11921193 const MCInstrDesc &Desc = BC.MII ->get (Opcode);
11931194
@@ -1200,7 +1201,7 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) {
12001201
12011202 if (MIB->hasPCRelOperand (Inst)) {
12021203 // Try to find the symbol that corresponds to the PC-relative operand.
1203- MCOperand *DispOpI = MIB->getMemOperandDisp (Inst);
1204+ MCOperand *DispOpI = MIB->getMemOperandDisp (const_cast <MCInst &>( Inst) );
12041205 assert (DispOpI != Inst.end () && " expected PC-relative displacement" );
12051206 assert (DispOpI->isExpr () &&
12061207 " found PC-relative with non-symbolic displacement" );
@@ -1226,28 +1227,49 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) {
12261227 }
12271228
12281229 // Get the contents of the section containing the target address of the
1229- // memory operand. We are only interested in read-only sections.
1230+ // memory operand. We are only interested in read-only sections for X86,
1231+ // for aarch64 the sections can be read-only or executable.
12301232 ErrorOr<BinarySection &> DataSection =
12311233 BC.getSectionForAddress (TargetAddress);
12321234 if (!DataSection || DataSection->isWritable ())
12331235 continue ;
12341236
1237+ if (DataSection->isText ()) {
1238+ // If data is not part of a function, check if it is part of a global CI
1239+ // Do not proceed if there aren't data markers for CIs
1240+ BinaryFunction *BFTgt =
1241+ BC.getBinaryFunctionContainingAddress (TargetAddress,
1242+ /* CheckPastEnd*/ false ,
1243+ /* UseMaxSize*/ true );
1244+ const bool IsInsideFunc =
1245+ BFTgt && BFTgt->isInConstantIsland (TargetAddress);
1246+
1247+ auto CIEndIter = BC.AddressToConstantIslandMap .end ();
1248+ auto CIIter = BC.AddressToConstantIslandMap .find (TargetAddress);
1249+ if (!IsInsideFunc && CIIter == CIEndIter)
1250+ continue ;
1251+ }
1252+
12351253 if (BC.getRelocationAt (TargetAddress) ||
12361254 BC.getDynamicRelocationAt (TargetAddress))
12371255 continue ;
12381256
1239- uint32_t Offset = TargetAddress - DataSection->getAddress ();
1240- StringRef ConstantData = DataSection->getContents ();
1241-
12421257 ++NumLocalLoadsFound;
12431258 if (BB->hasProfile ())
12441259 NumDynamicLocalLoadsFound += BB->getExecutionCount ();
12451260
1246- if (MIB->replaceMemOperandWithImm (Inst, ConstantData, Offset)) {
1247- ++NumLocalLoadsSimplified;
1248- if (BB->hasProfile ())
1249- NumDynamicLocalLoadsSimplified += BB->getExecutionCount ();
1250- }
1261+ uint32_t Offset = TargetAddress - DataSection->getAddress ();
1262+ StringRef ConstantData = DataSection->getContents ();
1263+ const InstructionListType Instrs =
1264+ MIB->materializeConstant (Inst, ConstantData, Offset);
1265+ if (Instrs.empty ())
1266+ continue ;
1267+
1268+ It = std::next (BB->replaceInstruction (It, Instrs), Instrs.size () - 1 );
1269+
1270+ ++NumLocalLoadsSimplified;
1271+ if (BB->hasProfile ())
1272+ NumDynamicLocalLoadsSimplified += BB->getExecutionCount ();
12511273 }
12521274 }
12531275
0 commit comments