Skip to content

Commit af1846f

Browse files
authored
Merge pull request rust-lang#183 from wasmerio/section_contents_as_slice
Section contents are arbitrary byte sequences, return a &[u8] for them instead of a CStr.
2 parents 9dd5214 + fc4fa63 commit af1846f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/object_file.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ impl Section {
130130
}
131131
}
132132

133-
pub fn get_contents(&self) -> &CStr {
133+
pub fn get_contents(&self) -> &[u8] {
134134
unsafe {
135-
CStr::from_ptr(LLVMGetSectionContents(self.section))
135+
std::slice::from_raw_parts(
136+
LLVMGetSectionContents(self.section) as *const u8,
137+
self.size() as usize)
136138
}
137139
}
138140

tests/all/test_object_file.rs

+34
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,37 @@ fn test_reloc_iterator() {
210210
}
211211
assert!(found_relocation);
212212
}
213+
214+
#[test]
215+
fn test_section_contains_nul() {
216+
let target_machine = get_native_target_machine();
217+
218+
let context = Context::create();
219+
let mut module = context.create_module("test_section_iterator");
220+
221+
let gv = module.add_global(context.i32_type(), None, "gv");
222+
gv.set_initializer(
223+
&context
224+
.i32_type()
225+
.const_int(0xff0000ff, false)
226+
.as_basic_value_enum(),
227+
);
228+
gv.set_section("test");
229+
230+
apply_target_to_module(&target_machine, &module);
231+
232+
let memory_buffer = target_machine
233+
.write_to_memory_buffer(&mut module, FileType::Object)
234+
.unwrap();
235+
let object_file = memory_buffer.create_object_file().unwrap();
236+
237+
let mut has_section_test = false;
238+
for section in object_file.get_sections() {
239+
if section.get_name().and_then(|name| name.to_str().ok()) == Some("test") {
240+
assert_eq!(section.get_contents(), 0xff0000ffu32.to_ne_bytes());
241+
has_section_test = true;
242+
break;
243+
}
244+
}
245+
assert!(has_section_test);
246+
}

0 commit comments

Comments
 (0)