Skip to content

Commit 8ac3774

Browse files
committed
Expand the tests to cover symbol and relocation iterators.
Use llvm IR instead of assembly. Module level assembly isn't the same as a raw assembly file being assembled, hence extra section/symbols/etc. in the object file which may be confusing.
1 parent 8d7ea88 commit 8ac3774

File tree

1 file changed

+109
-14
lines changed

1 file changed

+109
-14
lines changed

tests/all/test_object_file.rs

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use self::inkwell::targets::{
66
ByteOrdering, CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetData,
77
TargetMachine, TargetTriple,
88
};
9+
use self::inkwell::values::BasicValue;
910
use self::inkwell::OptimizationLevel;
1011

11-
#[test]
12-
fn test_section_iterator() {
12+
fn get_native_target_machine() -> TargetMachine {
1313
Target::initialize_native(&InitializationConfig::default())
1414
.expect("Failed to initialize native target");
1515
let target_triple = TargetMachine::get_default_triple();
1616
let target = Target::from_triple(&target_triple).unwrap();
17-
let target_machine = target
17+
target
1818
.create_target_machine(
1919
&target_triple,
2020
&TargetMachine::get_host_cpu_name().to_string(),
@@ -23,19 +23,29 @@ fn test_section_iterator() {
2323
RelocMode::Static,
2424
CodeModel::Small,
2525
)
26-
.unwrap();
26+
.unwrap()
27+
}
28+
29+
#[test]
30+
fn test_section_iterator() {
31+
let target_machine = get_native_target_machine();
2732

2833
let context = Context::create();
2934
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());
3949
module.set_data_layout(&target_machine.get_target_data().get_data_layout());
4050

4151
let memory_buffer = target_machine
@@ -65,7 +75,7 @@ fn test_section_iterator() {
6575
"C" => {
6676
assert!(!has_section_c);
6777
has_section_c = true;
68-
assert_eq!(section.size(), 3);
78+
assert_eq!(section.size(), 4);
6979
}
7080
_ => {}
7181
}
@@ -74,3 +84,88 @@ fn test_section_iterator() {
7484
assert!(has_section_b);
7585
assert!(has_section_c);
7686
}
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

Comments
 (0)