@@ -77,3 +77,76 @@ impl SectionRelocations {
7777 self . maps . get ( & target_idx) . unwrap_or ( & self . empty )
7878 }
7979}
80+
81+
82+ #[ cfg( test) ]
83+ mod tests {
84+ use super :: * ;
85+
86+
87+ /// Exercise the `Debug` representation of relocation types.
88+ #[ test]
89+ fn debug_repr ( ) {
90+ let map = RelocationMap :: default ( ) ;
91+ assert_ne ! ( format!( "{map:?}" ) , "" ) ;
92+
93+ let entry = RelocationEntry {
94+ addend : 42 ,
95+ implicit : false ,
96+ } ;
97+ assert_ne ! ( format!( "{entry:?}" ) , "" ) ;
98+ }
99+
100+ /// Test explicit (RELA) relocation.
101+ #[ test]
102+ fn relocate_explicit ( ) {
103+ let mut map = RelocationMap :: default ( ) ;
104+ let ( ) = map. insert ( 0x100 , 0xABCD , false ) ;
105+
106+ assert_eq ! ( map. relocate( 0x100 , 999 ) , 0xABCD ) ;
107+ assert_eq ! ( map. relocate( 0x200 , 999 ) , 999 ) ;
108+ }
109+
110+ /// Test implicit (REL) relocation.
111+ #[ test]
112+ fn relocate_implicit ( ) {
113+ let mut map = RelocationMap :: default ( ) ;
114+ let ( ) = map. insert ( 0x100 , 0x10 , true ) ;
115+
116+ assert_eq ! ( map. relocate( 0x100 , 0x20 ) , 0x30 ) ;
117+ assert_eq ! ( map. relocate( 0x200 , 0x20 ) , 0x20 ) ;
118+ }
119+
120+ /// Test wrapping behavior of implicit relocation.
121+ #[ test]
122+ fn relocate_implicit_wrapping ( ) {
123+ let mut map = RelocationMap :: default ( ) ;
124+ let ( ) = map. insert ( 0x0 , 1 , true ) ;
125+
126+ assert_eq ! ( map. relocate( 0x0 , u64 :: MAX ) , 0 ) ;
127+ }
128+
129+ /// Check that `SectionRelocations::empty()` returns identity maps
130+ /// for any index.
131+ #[ test]
132+ fn section_relocations_empty ( ) {
133+ let relocs = SectionRelocations :: empty ( ) ;
134+ assert_eq ! ( relocs. get( 0 ) . relocate( 0x0 , 42 ) , 42 ) ;
135+ assert_eq ! ( relocs. get( 99 ) . relocate( 0x0 , 42 ) , 42 ) ;
136+ }
137+
138+ /// Make sure that `SectionRelocations::new(maps)` returns populated
139+ /// map for known index and empty map for unknown.
140+ #[ test]
141+ fn section_relocations_with_maps ( ) {
142+ let mut map = RelocationMap :: default ( ) ;
143+ let ( ) = map. insert ( 0x0 , 0xFF , false ) ;
144+
145+ let mut maps = HashMap :: new ( ) ;
146+ let _prev = maps. insert ( 3 , map) ;
147+
148+ let relocs = SectionRelocations :: new ( maps) ;
149+ assert_eq ! ( relocs. get( 3 ) . relocate( 0x0 , 0 ) , 0xFF ) ;
150+ assert_eq ! ( relocs. get( 7 ) . relocate( 0x0 , 42 ) , 42 ) ;
151+ }
152+ }
0 commit comments