1
1
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
+ } ;
4
8
use wasmer_compiler:: {
5
9
Architecture , BinaryFormat , Compilation , CustomSectionProtection , Endianness ,
6
- RelocationKind as Reloc , RelocationTarget , Symbol , SymbolRegistry , Triple ,
10
+ RelocationKind as Reloc , RelocationTarget , SectionIndex , Symbol , SymbolRegistry , Triple ,
7
11
} ;
12
+ use wasmer_types:: entity:: PrimaryMap ;
13
+ use wasmer_types:: LocalFunctionIndex ;
14
+
15
+ const DWARF_SECTION_NAME : & [ u8 ] = b".eh_frame" ;
8
16
9
17
/// Create an object for a given target `Triple`.
10
18
///
@@ -128,103 +136,130 @@ pub fn emit_compilation(
128
136
let function_call_trampolines = compilation. get_function_call_trampolines ( ) ;
129
137
let dynamic_function_trampolines = compilation. get_dynamic_function_trampolines ( ) ;
130
138
139
+ let debug_index = compilation. get_debug ( ) . map ( |d| d. eh_frame ) ;
140
+
131
141
// 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 , _ > > ( ) ;
154
187
155
188
// 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 , _ > > ( ) ;
173
209
174
210
// Add function call trampolines
175
211
for ( signature_index, function) in function_call_trampolines. into_iter ( ) {
176
212
let function_name =
177
213
symbol_registry. symbol_to_name ( Symbol :: FunctionCallTrampoline ( signature_index) ) ;
214
+ let section_id = obj. section_id ( StandardSection :: Text ) ;
178
215
let symbol_id = obj. add_symbol ( ObjSymbol {
179
216
name : function_name. into_bytes ( ) ,
180
217
value : 0 ,
181
- size : 0 ,
218
+ size : function . body . len ( ) as _ ,
182
219
kind : SymbolKind :: Text ,
183
220
scope : SymbolScope :: Dynamic ,
184
221
weak : false ,
185
- section : SymbolSection :: Undefined ,
222
+ section : SymbolSection :: Section ( section_id ) ,
186
223
flags : SymbolFlags :: None ,
187
224
} ) ;
188
- let section_id = obj. section_id ( StandardSection :: Text ) ;
189
225
obj. add_symbol_data ( symbol_id, section_id, & function. body , 1 ) ;
190
226
}
191
227
192
228
// Add dynamic function trampolines
193
229
for ( func_index, function) in dynamic_function_trampolines. into_iter ( ) {
194
230
let function_name =
195
231
symbol_registry. symbol_to_name ( Symbol :: DynamicFunctionTrampoline ( func_index) ) ;
232
+ let section_id = obj. section_id ( StandardSection :: Text ) ;
196
233
let symbol_id = obj. add_symbol ( ObjSymbol {
197
234
name : function_name. into_bytes ( ) ,
198
235
value : 0 ,
199
- size : 0 ,
236
+ size : function . body . len ( ) as _ ,
200
237
kind : SymbolKind :: Text ,
201
238
scope : SymbolScope :: Dynamic ,
202
239
weak : false ,
203
- section : SymbolSection :: Undefined ,
240
+ section : SymbolSection :: Section ( section_id ) ,
204
241
flags : SymbolFlags :: None ,
205
242
} ) ;
206
- let section_id = obj. section_id ( StandardSection :: Text ) ;
207
243
obj. add_symbol_data ( symbol_id, section_id, & function. body , 1 ) ;
208
244
}
209
245
210
246
let mut all_relocations = Vec :: new ( ) ;
211
247
212
248
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) )
217
251
}
218
252
219
253
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
+ }
223
259
}
224
260
225
- for ( symbol_id, relocations) in all_relocations. into_iter ( ) {
261
+ for ( section_id , symbol_id, relocations) in all_relocations. into_iter ( ) {
226
262
let ( _symbol_id, section_offset) = obj. symbol_section_and_offset ( symbol_id) . unwrap ( ) ;
227
- let section_id = obj. section_id ( StandardSection :: Text ) ;
228
263
229
264
for r in relocations {
230
265
let ( relocation_kind, relocation_encoding, relocation_size) = match r. kind {
@@ -242,19 +277,18 @@ pub fn emit_compilation(
242
277
Reloc :: X86GOTPCRel4 => {
243
278
( RelocationKind :: GotRelative , RelocationEncoding :: Generic , 32 )
244
279
}
245
- Reloc :: ElfX86_64TlsGd => (
246
- RelocationKind :: Elf ( elf:: R_X86_64_TLSGD ) ,
247
- RelocationEncoding :: Generic ,
248
- 32 ,
249
- ) ,
250
280
// Reloc::X86PCRelRodata4 => {
251
- // return None;
252
281
// }
253
282
Reloc :: Arm64Call => (
254
283
RelocationKind :: Elf ( elf:: R_AARCH64_CALL26 ) ,
255
284
RelocationEncoding :: Generic ,
256
285
32 ,
257
286
) ,
287
+ Reloc :: ElfX86_64TlsGd => (
288
+ RelocationKind :: Elf ( elf:: R_X86_64_TLSGD ) ,
289
+ RelocationEncoding :: Generic ,
290
+ 32 ,
291
+ ) ,
258
292
other => {
259
293
return Err ( ObjectError :: UnsupportedArchitecture ( format ! (
260
294
"{} (relocation: {}" ,
@@ -267,16 +301,15 @@ pub fn emit_compilation(
267
301
268
302
match r. reloc_target {
269
303
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 ( ) ;
272
305
obj. add_relocation (
273
306
section_id,
274
307
Relocation {
275
308
offset : relocation_address,
276
309
size : relocation_size,
277
310
kind : relocation_kind,
278
311
encoding : relocation_encoding,
279
- symbol : target_symbol,
312
+ symbol : * target_symbol,
280
313
addend : r. addend ,
281
314
} ,
282
315
)
@@ -311,17 +344,15 @@ pub fn emit_compilation(
311
344
. map_err ( ObjectError :: Write ) ?;
312
345
}
313
346
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 ( ) ;
317
348
obj. add_relocation (
318
349
section_id,
319
350
Relocation {
320
351
offset : relocation_address,
321
352
size : relocation_size,
322
353
kind : relocation_kind,
323
354
encoding : relocation_encoding,
324
- symbol : target_symbol,
355
+ symbol : * target_symbol,
325
356
addend : r. addend ,
326
357
} ,
327
358
)
0 commit comments