@@ -6,15 +6,15 @@ use self::inkwell::targets::{
6
6
ByteOrdering , CodeModel , FileType , InitializationConfig , RelocMode , Target , TargetData ,
7
7
TargetMachine , TargetTriple ,
8
8
} ;
9
+ use self :: inkwell:: values:: BasicValue ;
9
10
use self :: inkwell:: OptimizationLevel ;
10
11
11
- #[ test]
12
- fn test_section_iterator ( ) {
12
+ fn get_native_target_machine ( ) -> TargetMachine {
13
13
Target :: initialize_native ( & InitializationConfig :: default ( ) )
14
14
. expect ( "Failed to initialize native target" ) ;
15
15
let target_triple = TargetMachine :: get_default_triple ( ) ;
16
16
let target = Target :: from_triple ( & target_triple) . unwrap ( ) ;
17
- let target_machine = target
17
+ target
18
18
. create_target_machine (
19
19
& target_triple,
20
20
& TargetMachine :: get_host_cpu_name ( ) . to_string ( ) ,
@@ -23,19 +23,29 @@ fn test_section_iterator() {
23
23
RelocMode :: Static ,
24
24
CodeModel :: Small ,
25
25
)
26
- . unwrap ( ) ;
26
+ . unwrap ( )
27
+ }
28
+
29
+ #[ test]
30
+ fn test_section_iterator ( ) {
31
+ let target_machine = get_native_target_machine ( ) ;
27
32
28
33
let context = Context :: create ( ) ;
29
34
let mut module = context. create_module ( "my_module" ) ;
30
- module. set_inline_assembly (
31
- ".section A\n \
32
- .byte 1\n \
33
- .section B\n \
34
- .byte 2, 2\n \
35
- .section C\n \
36
- .byte 3, 3, 3",
37
- ) ;
38
- module. set_triple ( & target_triple) ;
35
+
36
+ let gv_a = module. add_global ( context. i8_type ( ) , None , "a" ) ;
37
+ gv_a. set_initializer ( & context. i8_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
38
+ gv_a. set_section ( "A" ) ;
39
+
40
+ let gv_b = module. add_global ( context. i16_type ( ) , None , "b" ) ;
41
+ gv_b. set_initializer ( & context. i16_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
42
+ gv_b. set_section ( "B" ) ;
43
+
44
+ let gv_c = module. add_global ( context. i32_type ( ) , None , "c" ) ;
45
+ gv_c. set_initializer ( & context. i32_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
46
+ gv_c. set_section ( "C" ) ;
47
+
48
+ module. set_triple ( & target_machine. get_triple ( ) ) ;
39
49
module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
40
50
41
51
let memory_buffer = target_machine
@@ -65,7 +75,7 @@ fn test_section_iterator() {
65
75
"C" => {
66
76
assert ! ( !has_section_c) ;
67
77
has_section_c = true ;
68
- assert_eq ! ( section. size( ) , 3 ) ;
78
+ assert_eq ! ( section. size( ) , 4 ) ;
69
79
}
70
80
_ => { }
71
81
}
@@ -74,3 +84,88 @@ fn test_section_iterator() {
74
84
assert ! ( has_section_b) ;
75
85
assert ! ( has_section_c) ;
76
86
}
87
+
88
+ #[ test]
89
+ fn test_symbol_iterator ( ) {
90
+ let target_machine = get_native_target_machine ( ) ;
91
+
92
+ let context = Context :: create ( ) ;
93
+ let mut module = context. create_module ( "my_module" ) ;
94
+ module
95
+ . add_global ( context. i8_type ( ) , None , "a" )
96
+ . set_initializer ( & context. i8_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
97
+ module
98
+ . add_global ( context. i16_type ( ) , None , "b" )
99
+ . set_initializer ( & context. i16_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
100
+ module
101
+ . add_global ( context. i32_type ( ) , None , "c" )
102
+ . set_initializer ( & context. i32_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
103
+ module. set_triple ( & target_machine. get_triple ( ) ) ;
104
+ module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
105
+
106
+ let memory_buffer = target_machine
107
+ . write_to_memory_buffer ( & mut module, FileType :: Object )
108
+ . unwrap ( ) ;
109
+ let object_file = memory_buffer. create_object_file ( ) . unwrap ( ) ;
110
+
111
+ let mut has_symbol_a = false ;
112
+ let mut has_symbol_b = false ;
113
+ let mut has_symbol_c = false ;
114
+ for symbol in object_file. get_symbols ( ) {
115
+ match symbol. get_name ( ) . to_str ( ) . unwrap ( ) {
116
+ "a" => {
117
+ assert ! ( !has_symbol_a) ;
118
+ has_symbol_a = true ;
119
+ assert_eq ! ( symbol. size( ) , 1 ) ;
120
+ }
121
+ "b" => {
122
+ assert ! ( !has_symbol_b) ;
123
+ has_symbol_b = true ;
124
+ assert_eq ! ( symbol. size( ) , 2 ) ;
125
+ }
126
+ "c" => {
127
+ assert ! ( !has_symbol_c) ;
128
+ has_symbol_c = true ;
129
+ assert_eq ! ( symbol. size( ) , 4 ) ;
130
+ }
131
+ _ => { }
132
+ }
133
+ }
134
+ assert ! ( has_symbol_a) ;
135
+ assert ! ( has_symbol_b) ;
136
+ assert ! ( has_symbol_c) ;
137
+ }
138
+
139
+ #[ test]
140
+ fn test_reloc_iterator ( ) {
141
+ let target_machine = get_native_target_machine ( ) ;
142
+
143
+ let context = Context :: create ( ) ;
144
+ let mut module = context. create_module ( "my_module" ) ;
145
+ let x_ptr = module
146
+ . add_global ( context. i8_type ( ) , None , "x" )
147
+ . as_pointer_value ( ) ;
148
+ let x_plus_4 = x_ptr
149
+ . const_to_int ( context. i64_type ( ) )
150
+ . const_add ( context. i64_type ( ) . const_int ( 4 , false ) ) ;
151
+ module
152
+ . add_global ( context. i64_type ( ) , None , "a" )
153
+ . set_initializer ( & x_plus_4) ;
154
+
155
+ module. set_triple ( & target_machine. get_triple ( ) ) ;
156
+ module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
157
+
158
+ let memory_buffer = target_machine
159
+ . write_to_memory_buffer ( & mut module, FileType :: Object )
160
+ . unwrap ( ) ;
161
+ let object_file = memory_buffer. create_object_file ( ) . unwrap ( ) ;
162
+
163
+ let mut found_relocation = false ;
164
+ for section in object_file. get_sections ( ) {
165
+ for reloc in section. get_relocations ( ) {
166
+ found_relocation = true ;
167
+ // We don't stop the traversal here, so as to exercise the iterators.
168
+ }
169
+ }
170
+ assert ! ( found_relocation) ;
171
+ }
0 commit comments