@@ -1097,8 +1097,8 @@ where
10971097 pub fn if_expr < ' a : ' b , ' b , C > (
10981098 & ' a self ,
10991099 cond : C ,
1100- true_label : & ' b mut Label ,
1101- false_label : & ' b mut Label ,
1100+ true_label : & ' b mut LowLevelILLabel ,
1101+ false_label : & ' b mut LowLevelILLabel ,
11021102 ) -> LowLevelILExpression < ' a , A , Mutable , NonSSA < LiftedNonSSA > , VoidExpr >
11031103 where
11041104 C : LiftableLowLevelIL < ' b , A , Result = ValueExpr > ,
@@ -1119,24 +1119,27 @@ where
11191119 } ;
11201120
11211121 // Update the labels after they have been resolved.
1122- * true_label = Label :: from ( raw_true_label) ;
1123- * false_label = Label :: from ( raw_false_label) ;
1122+ * true_label = LowLevelILLabel :: from ( raw_true_label) ;
1123+ * false_label = LowLevelILLabel :: from ( raw_false_label) ;
1124+ self . update_label_map_for_label ( true_label) ;
1125+ self . update_label_map_for_label ( false_label) ;
11241126
11251127 LowLevelILExpression :: new ( self , LowLevelExpressionIndex ( expr_idx) )
11261128 }
11271129
11281130 // TODO: Wtf are these lifetimes??
11291131 pub fn goto < ' a : ' b , ' b > (
11301132 & ' a self ,
1131- label : & ' b mut Label ,
1133+ label : & ' b mut LowLevelILLabel ,
11321134 ) -> LowLevelILExpression < ' a , A , Mutable , NonSSA < LiftedNonSSA > , VoidExpr > {
11331135 use binaryninjacore_sys:: BNLowLevelILGoto ;
11341136
11351137 let mut raw_label = BNLowLevelILLabel :: from ( * label) ;
11361138 let expr_idx = unsafe { BNLowLevelILGoto ( self . handle , & mut raw_label) } ;
11371139
11381140 // Update the labels after they have been resolved.
1139- * label = Label :: from ( raw_label) ;
1141+ * label = LowLevelILLabel :: from ( raw_label) ;
1142+ self . update_label_map_for_label ( label) ;
11401143
11411144 LowLevelILExpression :: new ( self , LowLevelExpressionIndex ( expr_idx) )
11421145 }
@@ -1549,51 +1552,58 @@ where
15491552 }
15501553 }
15511554
1552- pub fn label_for_address < L : Into < Location > > ( & self , loc : L ) -> Option < Label > {
1555+ pub fn label_for_address < L : Into < Location > > ( & self , loc : L ) -> Option < LowLevelILLabel > {
15531556 use binaryninjacore_sys:: BNGetLowLevelILLabelForAddress ;
15541557
15551558 let loc: Location = loc. into ( ) ;
15561559 let arch = loc. arch . unwrap_or_else ( || * self . arch ( ) . as_ref ( ) ) ;
15571560 let raw_label =
15581561 unsafe { BNGetLowLevelILLabelForAddress ( self . handle , arch. handle , loc. addr ) } ;
15591562 match raw_label. is_null ( ) {
1560- false => Some ( unsafe { Label :: from ( * raw_label) } ) ,
1563+ false => {
1564+ let mut label = unsafe { LowLevelILLabel :: from ( * raw_label) } ;
1565+ // Set the location so that calls to [Self::update_label_map_for_label] will update the label map.
1566+ label. location = Some ( loc) ;
1567+ Some ( label)
1568+ }
15611569 true => None ,
15621570 }
15631571 }
15641572
1565- // TODO: Make this private and then force the updates in the expressions.
15661573 /// Call this after updating the label through an il operation or via [`Self::mark_label`].
1567- ///
1568- /// If you retrieved a label via [`Self::label_for_address`] than you very likely want to use this.
1569- pub fn update_label_for_address < L : Into < Location > > ( & self , loc : L , label : Label ) {
1574+ fn update_label_map_for_label ( & self , label : & LowLevelILLabel ) {
15701575 use binaryninjacore_sys:: BNGetLowLevelILLabelForAddress ;
15711576
1572- let loc: Location = loc. into ( ) ;
1573- let arch = loc. arch . unwrap_or_else ( || * self . arch ( ) . as_ref ( ) ) ;
1574- // Add the label into the label map
1575- unsafe { BNAddLowLevelILLabelForAddress ( self . handle , arch. handle , loc. addr ) } ;
1576- // Retrieve a pointer to the label in the map
1577- let raw_label =
1578- unsafe { BNGetLowLevelILLabelForAddress ( self . handle , arch. handle , loc. addr ) } ;
1579- // We should always have a valid label here
1580- assert ! ( !raw_label. is_null( ) , "Failed to add label for address!" ) ;
1581- // Update the label in the map with `label`
1582- unsafe { * raw_label = label. into ( ) } ;
1577+ // Only need to update the label if there is an associated address.
1578+ if let Some ( loc) = label. location {
1579+ let loc: Location = loc. into ( ) ;
1580+ let arch = loc. arch . unwrap_or_else ( || * self . arch ( ) . as_ref ( ) ) ;
1581+ // Add the label into the label map
1582+ unsafe { BNAddLowLevelILLabelForAddress ( self . handle , arch. handle , loc. addr ) } ;
1583+ // Retrieve a pointer to the label in the map
1584+ let raw_label =
1585+ unsafe { BNGetLowLevelILLabelForAddress ( self . handle , arch. handle , loc. addr ) } ;
1586+ // We should always have a valid label here
1587+ assert ! ( !raw_label. is_null( ) , "Failed to add label for address!" ) ;
1588+ // Update the label in the map with `label`
1589+ unsafe { * raw_label = label. into ( ) } ;
1590+ }
15831591 }
15841592
1585- pub fn mark_label ( & self , label : & mut Label ) {
1593+ pub fn mark_label ( & self , label : & mut LowLevelILLabel ) {
15861594 use binaryninjacore_sys:: BNLowLevelILMarkLabel ;
15871595
15881596 let mut raw_label = BNLowLevelILLabel :: from ( * label) ;
15891597 unsafe { BNLowLevelILMarkLabel ( self . handle , & mut raw_label) } ;
1590- * label = Label :: from ( raw_label) ;
1598+ * label = LowLevelILLabel :: from ( raw_label) ;
1599+ self . update_label_map_for_label ( label) ;
15911600 }
15921601}
15931602
1594- // TODO: Rename to LowLevelILLabel
1595- #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
1596- pub struct Label {
1603+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
1604+ pub struct LowLevelILLabel {
1605+ /// Used to update the label map if the label is associated with a location.
1606+ pub location : Option < Location > ,
15971607 pub resolved : bool ,
15981608 // TODO: This expr_ref is not actually a valid one sometimes...
15991609 // TODO: We should make these non public and only accessible if resolved is true.
@@ -1602,7 +1612,7 @@ pub struct Label {
16021612 pub operand : usize ,
16031613}
16041614
1605- impl Label {
1615+ impl LowLevelILLabel {
16061616 pub fn new ( ) -> Self {
16071617 use binaryninjacore_sys:: BNLowLevelILInitLabel ;
16081618
@@ -1612,18 +1622,19 @@ impl Label {
16121622 }
16131623}
16141624
1615- impl From < BNLowLevelILLabel > for Label {
1625+ impl From < BNLowLevelILLabel > for LowLevelILLabel {
16161626 fn from ( value : BNLowLevelILLabel ) -> Self {
16171627 Self {
1628+ location : None ,
16181629 resolved : value. resolved ,
16191630 expr_ref : LowLevelExpressionIndex ( value. ref_ ) ,
16201631 operand : value. operand ,
16211632 }
16221633 }
16231634}
16241635
1625- impl From < Label > for BNLowLevelILLabel {
1626- fn from ( value : Label ) -> Self {
1636+ impl From < LowLevelILLabel > for BNLowLevelILLabel {
1637+ fn from ( value : LowLevelILLabel ) -> Self {
16271638 Self {
16281639 resolved : value. resolved ,
16291640 ref_ : value. expr_ref . 0 ,
@@ -1632,13 +1643,13 @@ impl From<Label> for BNLowLevelILLabel {
16321643 }
16331644}
16341645
1635- impl From < & Label > for BNLowLevelILLabel {
1636- fn from ( value : & Label ) -> Self {
1646+ impl From < & LowLevelILLabel > for BNLowLevelILLabel {
1647+ fn from ( value : & LowLevelILLabel ) -> Self {
16371648 Self :: from ( * value)
16381649 }
16391650}
16401651
1641- impl Default for Label {
1652+ impl Default for LowLevelILLabel {
16421653 fn default ( ) -> Self {
16431654 Self :: new ( )
16441655 }
0 commit comments