1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#![allow(clippy::cast_ptr_alignment)]
use anyhow::Error;
use more_asserts::assert_gt;
use object::write::{Object, Relocation, StandardSegment};
use object::{RelocationEncoding, RelocationKind, SectionKind};
use std::collections::HashMap;
use target_lexicon::BinaryFormat;
use wasmtime_environ::isa::TargetIsa;
pub use crate::read_debuginfo::{read_debuginfo, DebugInfoData, WasmFileInfo};
pub use crate::write_debuginfo::{emit_dwarf, DwarfSection};
mod gc;
mod read_debuginfo;
mod transform;
mod write_debuginfo;
pub fn write_debugsections(obj: &mut Object, sections: Vec<DwarfSection>) -> Result<(), Error> {
let (bodies, relocs) = sections
.into_iter()
.map(|s| ((s.name.clone(), s.body), (s.name, s.relocs)))
.unzip::<_, _, Vec<_>, Vec<_>>();
let mut ids = HashMap::new();
for (name, body) in bodies {
let segment = obj.segment_name(StandardSegment::Debug).to_vec();
let section_id = obj.add_section(segment, name.as_bytes().to_vec(), SectionKind::Debug);
ids.insert(name, section_id);
obj.append_section_data(section_id, &body, 1);
}
for (name, relocs) in relocs {
let section_id = *ids.get(&name).unwrap();
for reloc in relocs {
let target_symbol = if reloc.target.starts_with("_wasm_function") {
obj.symbol_id(reloc.target.as_bytes()).unwrap()
} else {
obj.section_symbol(*ids.get(&reloc.target).unwrap())
};
obj.add_relocation(
section_id,
Relocation {
offset: u64::from(reloc.offset),
size: reloc.size << 3,
kind: RelocationKind::Absolute,
encoding: RelocationEncoding::Generic,
symbol: target_symbol,
addend: i64::from(reloc.addend),
},
)?;
}
}
Ok(())
}
fn patch_dwarf_sections(sections: &mut [DwarfSection], funcs: &[*const u8]) {
for section in sections {
const FUNC_SYMBOL_PREFIX: &str = "_wasm_function_";
for reloc in section.relocs.iter() {
if !reloc.target.starts_with(FUNC_SYMBOL_PREFIX) {
continue;
}
let func_index = reloc.target[FUNC_SYMBOL_PREFIX.len()..]
.parse::<usize>()
.expect("func index");
let target = (funcs[func_index] as u64).wrapping_add(reloc.addend as i64 as u64);
let entry_ptr = section.body
[reloc.offset as usize..reloc.offset as usize + reloc.size as usize]
.as_mut_ptr();
unsafe {
match reloc.size {
4 => std::ptr::write(entry_ptr as *mut u32, target as u32),
8 => std::ptr::write(entry_ptr as *mut u64, target),
_ => panic!("unexpected reloc entry size"),
}
}
}
section
.relocs
.retain(|r| !r.target.starts_with(FUNC_SYMBOL_PREFIX));
}
}
pub fn write_debugsections_image(
isa: &dyn TargetIsa,
mut sections: Vec<DwarfSection>,
code_region: (*const u8, usize),
funcs: &[*const u8],
) -> Result<Vec<u8>, Error> {
let mut obj = Object::new(BinaryFormat::Elf, isa.triple().architecture);
assert!(!code_region.0.is_null() && code_region.1 > 0);
assert_gt!(funcs.len(), 0);
let body = unsafe { std::slice::from_raw_parts(code_region.0, code_region.1) };
let section_id = obj.add_section(vec![], ".text.all".as_bytes().to_vec(), SectionKind::Text);
obj.append_section_data(section_id, body, 1);
patch_dwarf_sections(&mut sections, funcs);
write_debugsections(&mut obj, sections)?;
let mut bytes = obj.write()?;
convert_object_elf_to_loadable_file(&mut bytes, code_region.0);
Ok(bytes)
}
fn convert_object_elf_to_loadable_file(bytes: &mut Vec<u8>, code_ptr: *const u8) {
use object::elf::*;
use object::endian::LittleEndian;
use std::ffi::CStr;
use std::mem::size_of;
use std::os::raw::c_char;
let e = LittleEndian;
let header: &FileHeader64<LittleEndian> =
unsafe { &*(bytes.as_mut_ptr() as *const FileHeader64<_>) };
assert!(
header.e_ident.class == ELFCLASS64 && header.e_ident.data == ELFDATA2LSB,
"bits and endianess in .ELF",
);
assert!(
header.e_phoff.get(e) == 0 && header.e_phnum.get(e) == 0,
"program header table is empty"
);
let e_shentsize = header.e_shentsize.get(e);
assert_eq!(
e_shentsize as usize,
size_of::<SectionHeader64<LittleEndian>>(),
"size of sh"
);
let e_shoff = header.e_shoff.get(e);
let e_shnum = header.e_shnum.get(e);
let mut shstrtab_off = 0;
for i in 0..e_shnum {
let off = e_shoff as isize + i as isize * e_shentsize as isize;
let section: &SectionHeader64<LittleEndian> =
unsafe { &*(bytes.as_ptr().offset(off) as *const SectionHeader64<_>) };
if section.sh_type.get(e) != SHT_STRTAB {
continue;
}
shstrtab_off = section.sh_offset.get(e);
}
let mut segment = None;
for i in 0..e_shnum {
let off = e_shoff as isize + i as isize * e_shentsize as isize;
let section: &mut SectionHeader64<LittleEndian> =
unsafe { &mut *(bytes.as_mut_ptr().offset(off) as *mut SectionHeader64<_>) };
if section.sh_type.get(e) != SHT_PROGBITS {
continue;
}
let sh_name_off = section.sh_name.get(e);
let sh_name = unsafe {
CStr::from_ptr(
bytes
.as_ptr()
.offset((shstrtab_off + sh_name_off as u64) as isize)
as *const c_char,
)
.to_str()
.expect("name")
};
if sh_name != ".text.all" {
continue;
}
assert!(segment.is_none());
section.sh_addr.set(e, code_ptr as u64);
let sh_offset = section.sh_offset.get(e);
let sh_size = section.sh_size.get(e);
segment = Some((sh_offset, code_ptr, sh_size));
bytes[(shstrtab_off + sh_name_off as u64) as usize + ".text".len()] = 0;
}
let ph_off = bytes.len();
let e_phentsize = size_of::<ProgramHeader64<LittleEndian>>();
if let Some((sh_offset, v_offset, sh_size)) = segment {
bytes.resize(ph_off + e_phentsize, 0);
let program: &mut ProgramHeader64<LittleEndian> =
unsafe { &mut *(bytes.as_ptr().add(ph_off) as *mut ProgramHeader64<_>) };
program.p_type.set(e, PT_LOAD);
program.p_offset.set(e, sh_offset);
program.p_vaddr.set(e, v_offset as u64);
program.p_paddr.set(e, v_offset as u64);
program.p_filesz.set(e, sh_size as u64);
program.p_memsz.set(e, sh_size as u64);
} else {
unreachable!();
}
let header: &mut FileHeader64<LittleEndian> =
unsafe { &mut *(bytes.as_mut_ptr() as *mut FileHeader64<_>) };
header.e_type.set(e, ET_DYN);
header.e_phoff.set(e, ph_off as u64);
header.e_phentsize.set(e, e_phentsize as u16);
header.e_phnum.set(e, 1u16);
}