@@ -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,53 @@ 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);
1232- if (!DataSection || DataSection-> isWritable () )
1234+ if (!DataSection)
12331235 continue ;
12341236
1237+ if (BC.isX86 () && DataSection->isWritable ())
1238+ continue ;
1239+
1240+ if (DataSection->isText ()) {
1241+ // If data is not part of a function, check if it is part of a global CI
1242+ // Do not proceed if there aren't data markers for CIs
1243+ BinaryFunction *BFTgt =
1244+ BC.getBinaryFunctionContainingAddress (TargetAddress,
1245+ /* CheckPastEnd*/ false ,
1246+ /* UseMaxSize*/ true );
1247+ const bool IsInsideFunc =
1248+ BFTgt && BFTgt->isInConstantIsland (TargetAddress);
1249+
1250+ auto CIEndIter = BC.AddressToConstantIslandMap .end ();
1251+ auto CIIter = BC.AddressToConstantIslandMap .find (TargetAddress);
1252+ if (!IsInsideFunc && CIIter == CIEndIter)
1253+ continue ;
1254+ }
1255+
12351256 if (BC.getRelocationAt (TargetAddress) ||
12361257 BC.getDynamicRelocationAt (TargetAddress))
12371258 continue ;
12381259
1239- uint32_t Offset = TargetAddress - DataSection->getAddress ();
1240- StringRef ConstantData = DataSection->getContents ();
1241-
12421260 ++NumLocalLoadsFound;
12431261 if (BB->hasProfile ())
12441262 NumDynamicLocalLoadsFound += BB->getExecutionCount ();
12451263
1246- if (MIB->replaceMemOperandWithImm (Inst, ConstantData, Offset)) {
1247- ++NumLocalLoadsSimplified;
1248- if (BB->hasProfile ())
1249- NumDynamicLocalLoadsSimplified += BB->getExecutionCount ();
1250- }
1264+ uint32_t Offset = TargetAddress - DataSection->getAddress ();
1265+ StringRef ConstantData = DataSection->getContents ();
1266+ const InstructionListType Instrs =
1267+ MIB->materializeConstant (Inst, ConstantData, Offset);
1268+ if (Instrs.empty ())
1269+ continue ;
1270+
1271+ auto IIter = BB->findInstruction (&Inst);
1272+ It = std::next (BB->replaceInstruction (IIter, Instrs), Instrs.size ());
1273+
1274+ ++NumLocalLoadsSimplified;
1275+ if (BB->hasProfile ())
1276+ NumDynamicLocalLoadsSimplified += BB->getExecutionCount ();
12511277 }
12521278 }
12531279
@@ -1260,6 +1286,9 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) {
12601286}
12611287
12621288Error SimplifyRODataLoads::runOnFunctions (BinaryContext &BC) {
1289+ if (BC.isRISCV ())
1290+ return Error::success ();
1291+
12631292 for (auto &It : BC.getBinaryFunctions ()) {
12641293 BinaryFunction &Function = It.second ;
12651294 if (shouldOptimize (Function) && simplifyRODataLoads (Function))
0 commit comments