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