Skip to content

Commit 0722dd8

Browse files
committed
Merge branch 'cranelift-native' of github.com:wasmerio/wasmer into cranelift-native
2 parents 2e28932 + 9585c51 commit 0722dd8

File tree

1 file changed

+101
-70
lines changed

1 file changed

+101
-70
lines changed

lib/object/src/module.rs

+101-70
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use crate::error::ObjectError;
2-
use object::write::{Object, Relocation, StandardSection, Symbol as ObjSymbol, SymbolSection};
3-
use object::{elf, RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, SymbolScope};
2+
use object::write::{
3+
Object, Relocation, StandardSection, StandardSegment, Symbol as ObjSymbol, SymbolSection,
4+
};
5+
use object::{
6+
elf, RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
7+
};
48
use wasmer_compiler::{
59
Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness,
6-
RelocationKind as Reloc, RelocationTarget, Symbol, SymbolRegistry, Triple,
10+
RelocationKind as Reloc, RelocationTarget, SectionIndex, Symbol, SymbolRegistry, Triple,
711
};
12+
use wasmer_types::entity::PrimaryMap;
13+
use wasmer_types::LocalFunctionIndex;
14+
15+
const DWARF_SECTION_NAME: &[u8] = b".eh_frame";
816

917
/// Create an object for a given target `Triple`.
1018
///
@@ -128,103 +136,130 @@ pub fn emit_compilation(
128136
let function_call_trampolines = compilation.get_function_call_trampolines();
129137
let dynamic_function_trampolines = compilation.get_dynamic_function_trampolines();
130138

139+
let debug_index = compilation.get_debug().map(|d| d.eh_frame);
140+
131141
// Add sections
132-
for (section_index, custom_section) in custom_sections.iter() {
133-
// TODO: We need to rename the sections corresponding to the DWARF information
134-
// to the proper names (like `.eh_frame`)
135-
let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
136-
let (section_kind, standard_section) = match custom_section.protection {
137-
CustomSectionProtection::ReadExecute => (SymbolKind::Text, StandardSection::Text),
138-
// TODO: Fix this to be StandardSection::Data
139-
CustomSectionProtection::Read => (SymbolKind::Data, StandardSection::Text),
140-
};
141-
let symbol_id = obj.add_symbol(ObjSymbol {
142-
name: section_name.into_bytes(),
143-
value: 0,
144-
size: 0,
145-
kind: section_kind,
146-
scope: SymbolScope::Dynamic,
147-
weak: false,
148-
section: SymbolSection::Undefined,
149-
flags: SymbolFlags::None,
150-
});
151-
let section_id = obj.section_id(standard_section);
152-
obj.add_symbol_data(symbol_id, section_id, custom_section.bytes.as_slice(), 1);
153-
}
142+
let custom_section_ids = custom_sections
143+
.into_iter()
144+
.map(|(section_index, custom_section)| {
145+
if debug_index.map(|d| d == section_index).unwrap_or(false) {
146+
// If this is the debug section
147+
let segment = obj.segment_name(StandardSegment::Debug).to_vec();
148+
let section_id =
149+
obj.add_section(segment, DWARF_SECTION_NAME.to_vec(), SectionKind::Debug);
150+
obj.append_section_data(section_id, custom_section.bytes.as_slice(), 1);
151+
let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
152+
let symbol_id = obj.add_symbol(ObjSymbol {
153+
name: section_name.into_bytes(),
154+
value: 0,
155+
size: custom_section.bytes.len() as _,
156+
kind: SymbolKind::Data,
157+
scope: SymbolScope::Compilation,
158+
weak: false,
159+
section: SymbolSection::Section(section_id),
160+
flags: SymbolFlags::None,
161+
});
162+
(section_id, symbol_id)
163+
} else {
164+
let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
165+
let (section_kind, standard_section) = match custom_section.protection {
166+
CustomSectionProtection::ReadExecute => {
167+
(SymbolKind::Text, StandardSection::Text)
168+
}
169+
CustomSectionProtection::Read => (SymbolKind::Data, StandardSection::Data),
170+
};
171+
let section_id = obj.section_id(standard_section);
172+
let symbol_id = obj.add_symbol(ObjSymbol {
173+
name: section_name.into_bytes(),
174+
value: 0,
175+
size: custom_section.bytes.len() as _,
176+
kind: section_kind,
177+
scope: SymbolScope::Dynamic,
178+
weak: false,
179+
section: SymbolSection::Section(section_id),
180+
flags: SymbolFlags::None,
181+
});
182+
obj.add_symbol_data(symbol_id, section_id, custom_section.bytes.as_slice(), 1);
183+
(section_id, symbol_id)
184+
}
185+
})
186+
.collect::<PrimaryMap<SectionIndex, _>>();
154187

155188
// Add functions
156-
for (function_local_index, function) in function_bodies.into_iter() {
157-
let function_name =
158-
symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index));
159-
let symbol_id = obj.add_symbol(ObjSymbol {
160-
name: function_name.into_bytes(),
161-
value: 0,
162-
size: 0,
163-
kind: SymbolKind::Text,
164-
scope: SymbolScope::Dynamic,
165-
weak: false,
166-
section: SymbolSection::Undefined,
167-
flags: SymbolFlags::None,
168-
});
169-
170-
let section_id = obj.section_id(StandardSection::Text);
171-
obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
172-
}
189+
let function_symbol_ids = function_bodies
190+
.into_iter()
191+
.map(|(function_local_index, function)| {
192+
let function_name =
193+
symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index));
194+
let section_id = obj.section_id(StandardSection::Text);
195+
let symbol_id = obj.add_symbol(ObjSymbol {
196+
name: function_name.into_bytes(),
197+
value: 0,
198+
size: function.body.len() as _,
199+
kind: SymbolKind::Text,
200+
scope: SymbolScope::Dynamic,
201+
weak: false,
202+
section: SymbolSection::Section(section_id),
203+
flags: SymbolFlags::None,
204+
});
205+
obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
206+
(section_id, symbol_id)
207+
})
208+
.collect::<PrimaryMap<LocalFunctionIndex, _>>();
173209

174210
// Add function call trampolines
175211
for (signature_index, function) in function_call_trampolines.into_iter() {
176212
let function_name =
177213
symbol_registry.symbol_to_name(Symbol::FunctionCallTrampoline(signature_index));
214+
let section_id = obj.section_id(StandardSection::Text);
178215
let symbol_id = obj.add_symbol(ObjSymbol {
179216
name: function_name.into_bytes(),
180217
value: 0,
181-
size: 0,
218+
size: function.body.len() as _,
182219
kind: SymbolKind::Text,
183220
scope: SymbolScope::Dynamic,
184221
weak: false,
185-
section: SymbolSection::Undefined,
222+
section: SymbolSection::Section(section_id),
186223
flags: SymbolFlags::None,
187224
});
188-
let section_id = obj.section_id(StandardSection::Text);
189225
obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
190226
}
191227

192228
// Add dynamic function trampolines
193229
for (func_index, function) in dynamic_function_trampolines.into_iter() {
194230
let function_name =
195231
symbol_registry.symbol_to_name(Symbol::DynamicFunctionTrampoline(func_index));
232+
let section_id = obj.section_id(StandardSection::Text);
196233
let symbol_id = obj.add_symbol(ObjSymbol {
197234
name: function_name.into_bytes(),
198235
value: 0,
199-
size: 0,
236+
size: function.body.len() as _,
200237
kind: SymbolKind::Text,
201238
scope: SymbolScope::Dynamic,
202239
weak: false,
203-
section: SymbolSection::Undefined,
240+
section: SymbolSection::Section(section_id),
204241
flags: SymbolFlags::None,
205242
});
206-
let section_id = obj.section_id(StandardSection::Text);
207243
obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
208244
}
209245

210246
let mut all_relocations = Vec::new();
211247

212248
for (function_local_index, relocations) in function_relocations.into_iter() {
213-
let function_name =
214-
symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index));
215-
let symbol_id = obj.symbol_id(function_name.as_bytes()).unwrap();
216-
all_relocations.push((symbol_id, relocations))
249+
let (section_id, symbol_id) = function_symbol_ids.get(function_local_index).unwrap();
250+
all_relocations.push((*section_id, *symbol_id, relocations))
217251
}
218252

219253
for (section_index, relocations) in custom_section_relocations.into_iter() {
220-
let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
221-
let symbol_id = obj.symbol_id(section_name.as_bytes()).unwrap();
222-
all_relocations.push((symbol_id, relocations))
254+
if !debug_index.map(|d| d == section_index).unwrap_or(false) {
255+
// Skip DWARF relocations just yet
256+
let (section_id, symbol_id) = custom_section_ids.get(section_index).unwrap();
257+
all_relocations.push((*section_id, *symbol_id, relocations));
258+
}
223259
}
224260

225-
for (symbol_id, relocations) in all_relocations.into_iter() {
261+
for (section_id, symbol_id, relocations) in all_relocations.into_iter() {
226262
let (_symbol_id, section_offset) = obj.symbol_section_and_offset(symbol_id).unwrap();
227-
let section_id = obj.section_id(StandardSection::Text);
228263

229264
for r in relocations {
230265
let (relocation_kind, relocation_encoding, relocation_size) = match r.kind {
@@ -242,19 +277,18 @@ pub fn emit_compilation(
242277
Reloc::X86GOTPCRel4 => {
243278
(RelocationKind::GotRelative, RelocationEncoding::Generic, 32)
244279
}
245-
Reloc::ElfX86_64TlsGd => (
246-
RelocationKind::Elf(elf::R_X86_64_TLSGD),
247-
RelocationEncoding::Generic,
248-
32,
249-
),
250280
// Reloc::X86PCRelRodata4 => {
251-
// return None;
252281
// }
253282
Reloc::Arm64Call => (
254283
RelocationKind::Elf(elf::R_AARCH64_CALL26),
255284
RelocationEncoding::Generic,
256285
32,
257286
),
287+
Reloc::ElfX86_64TlsGd => (
288+
RelocationKind::Elf(elf::R_X86_64_TLSGD),
289+
RelocationEncoding::Generic,
290+
32,
291+
),
258292
other => {
259293
return Err(ObjectError::UnsupportedArchitecture(format!(
260294
"{} (relocation: {}",
@@ -267,16 +301,15 @@ pub fn emit_compilation(
267301

268302
match r.reloc_target {
269303
RelocationTarget::LocalFunc(index) => {
270-
let target_name = symbol_registry.symbol_to_name(Symbol::LocalFunction(index));
271-
let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap();
304+
let (_, target_symbol) = function_symbol_ids.get(index).unwrap();
272305
obj.add_relocation(
273306
section_id,
274307
Relocation {
275308
offset: relocation_address,
276309
size: relocation_size,
277310
kind: relocation_kind,
278311
encoding: relocation_encoding,
279-
symbol: target_symbol,
312+
symbol: *target_symbol,
280313
addend: r.addend,
281314
},
282315
)
@@ -311,17 +344,15 @@ pub fn emit_compilation(
311344
.map_err(ObjectError::Write)?;
312345
}
313346
RelocationTarget::CustomSection(section_index) => {
314-
let target_name =
315-
symbol_registry.symbol_to_name(Symbol::Section(section_index));
316-
let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap();
347+
let (_, target_symbol) = custom_section_ids.get(section_index).unwrap();
317348
obj.add_relocation(
318349
section_id,
319350
Relocation {
320351
offset: relocation_address,
321352
size: relocation_size,
322353
kind: relocation_kind,
323354
encoding: relocation_encoding,
324-
symbol: target_symbol,
355+
symbol: *target_symbol,
325356
addend: r.addend,
326357
},
327358
)

0 commit comments

Comments
 (0)