@@ -582,65 +582,47 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
582
582
}
583
583
584
584
void LowerAnnotations::runOnFunctions (BinaryContext &BC) {
585
- std::vector<std::pair<MCInst *, uint32_t >> PreservedOffsetAnnotations;
586
- std::vector<std::pair<MCInst *, MCSymbol *>> PreservedLabelAnnotations;
587
-
588
- for (auto &It : BC.getBinaryFunctions ()) {
589
- BinaryFunction &BF = It.second ;
590
-
591
- for (FunctionFragment &FF : BF.getLayout ().fragments ()) {
585
+ for (BinaryFunction *BF : BC.getAllBinaryFunctions ()) {
586
+ for (FunctionFragment &FF : BF->getLayout ().fragments ()) {
587
+ // Reset at the start of the new fragment.
592
588
int64_t CurrentGnuArgsSize = 0 ;
593
589
594
590
for (BinaryBasicBlock *const BB : FF) {
595
- // First convert GnuArgsSize annotations into CFIs. This may change
596
- // instr pointers, so do it before recording ptrs for preserved
597
- // annotations
598
- if (BF.usesGnuArgsSize ()) {
599
- for (auto II = BB->begin (); II != BB->end (); ++II) {
600
- if (!BC.MIB ->isInvoke (*II))
601
- continue ;
591
+ for (auto II = BB->begin (); II != BB->end (); ++II) {
592
+
593
+ // Convert GnuArgsSize annotations into CFIs.
594
+ if (BF->usesGnuArgsSize () && BC.MIB ->isInvoke (*II)) {
602
595
const int64_t NewGnuArgsSize = BC.MIB ->getGnuArgsSize (*II);
603
596
assert (NewGnuArgsSize >= 0 &&
604
- " expected non-negative GNU_args_size" );
597
+ " Expected non-negative GNU_args_size. " );
605
598
if (NewGnuArgsSize != CurrentGnuArgsSize) {
606
- auto InsertII = BF. addCFIInstruction (
599
+ auto InsertII = BF-> addCFIInstruction (
607
600
BB, II,
608
601
MCCFIInstruction::createGnuArgsSize (nullptr , NewGnuArgsSize));
609
602
CurrentGnuArgsSize = NewGnuArgsSize;
610
603
II = std::next (InsertII);
611
604
}
612
605
}
613
- }
614
606
615
- // Now record preserved annotations separately and then strip
616
- // annotations.
617
- for (auto II = BB->begin (); II != BB->end (); ++II) {
618
- if (BF.requiresAddressTranslation () && BC.MIB ->getOffset (*II))
619
- PreservedOffsetAnnotations.emplace_back (&(*II),
620
- *BC.MIB ->getOffset (*II));
621
- if (MCSymbol *Label = BC.MIB ->getLabel (*II))
622
- PreservedLabelAnnotations.emplace_back (&*II, Label);
607
+ // Preserve selected annotations and strip the rest.
608
+ std::optional<uint32_t > Offset = BF->requiresAddressTranslation ()
609
+ ? BC.MIB ->getOffset (*II)
610
+ : std::nullopt;
611
+ MCSymbol *Label = BC.MIB ->getLabel (*II);
612
+
623
613
BC.MIB ->stripAnnotations (*II);
614
+
615
+ if (Offset)
616
+ BC.MIB ->setOffset (*II, *Offset);
617
+ if (Label)
618
+ BC.MIB ->setLabel (*II, Label);
624
619
}
625
620
}
626
621
}
627
622
}
628
- for (BinaryFunction *BF : BC.getInjectedBinaryFunctions ())
629
- for (BinaryBasicBlock &BB : *BF)
630
- for (MCInst &Instruction : BB) {
631
- if (MCSymbol *Label = BC.MIB ->getLabel (Instruction))
632
- PreservedLabelAnnotations.emplace_back (&Instruction, Label);
633
- BC.MIB ->stripAnnotations (Instruction);
634
- }
635
623
636
624
// Release all memory taken by annotations
637
625
BC.MIB ->freeAnnotations ();
638
-
639
- // Reinsert preserved annotations we need during code emission.
640
- for (const std::pair<MCInst *, uint32_t > &Item : PreservedOffsetAnnotations)
641
- BC.MIB ->setOffset (*Item.first , Item.second );
642
- for (auto [Instr, Label] : PreservedLabelAnnotations)
643
- BC.MIB ->setLabel (*Instr, Label);
644
626
}
645
627
646
628
// Check for dirty state in MCSymbol objects that might be a consequence
0 commit comments