@@ -2,14 +2,14 @@ use crate::error::Error;
22use crate :: name:: Name ;
33use crate :: stack_probe;
44use crate :: table:: { link_tables, TABLE_SYM } ;
5- use crate :: traps:: { trap_sym_for_func , write_trap_tables } ;
5+ use crate :: traps:: { translate_trapcode , trap_sym_for_func } ;
66use byteorder:: { LittleEndian , WriteBytesExt } ;
77use cranelift_codegen:: { ir, isa, binemit:: TrapSink } ;
88use cranelift_faerie:: traps:: { FaerieTrapManifest , FaerieTrapSink } ;
99use cranelift_faerie:: FaerieProduct ;
1010use faerie:: { Artifact , Decl , Link } ;
1111use lucet_module:: {
12- FunctionSpec , SerializedModule , VersionInfo , LUCET_MODULE_SYM , MODULE_DATA_SYM ,
12+ FunctionSpec , SerializedModule , TrapSite , VersionInfo , LUCET_MODULE_SYM , MODULE_DATA_SYM ,
1313} ;
1414use std:: collections:: HashMap ;
1515use std:: fs:: File ;
@@ -88,7 +88,7 @@ impl ObjectFile {
8888 }
8989 }
9090
91- write_trap_tables ( & trap_manifest, & mut obj . artifact ) ?;
91+ obj . write_trap_tables ( & trap_manifest) ?;
9292 obj. write_function_manifest ( function_manifest. as_slice ( ) ) ?;
9393 link_tables ( table_manifest. as_slice ( ) , & mut obj. artifact ) ?;
9494
@@ -213,6 +213,44 @@ impl ObjectFile {
213213 Ok ( ( ) )
214214 }
215215
216+ fn write_trap_tables ( & mut self , manifest : & FaerieTrapManifest ) -> Result < ( ) , Error > {
217+ for sink in manifest. sinks . iter ( ) {
218+ let func_sym = & sink. name ;
219+ let trap_sym = trap_sym_for_func ( func_sym) ;
220+
221+ self . artifact . declare ( & trap_sym, Decl :: data ( ) ) . map_err ( |source| {
222+ let message = format ! ( "Trap table error declaring {}" , trap_sym) ;
223+ Error :: ArtifactError ( source, message)
224+ } ) ?;
225+
226+ // write the actual function-level trap table
227+ let traps: Vec < TrapSite > = sink
228+ . sites
229+ . iter ( )
230+ . map ( |site| TrapSite {
231+ offset : site. offset ,
232+ code : translate_trapcode ( site. code ) ,
233+ } )
234+ . collect ( ) ;
235+
236+ let trap_site_bytes = unsafe {
237+ std:: slice:: from_raw_parts (
238+ traps. as_ptr ( ) as * const u8 ,
239+ traps. len ( ) * std:: mem:: size_of :: < TrapSite > ( ) ,
240+ )
241+ } ;
242+
243+ // and write the function trap table into the object
244+ self . artifact . define ( & trap_sym, trap_site_bytes. to_vec ( ) )
245+ . map_err ( |source| {
246+ let message = format ! ( "Trap table error defining {}" , trap_sym) ;
247+ Error :: ArtifactError ( source, message)
248+ } ) ?;
249+ }
250+
251+ Ok ( ( ) )
252+ }
253+
216254 pub fn write < P : AsRef < Path > > ( & self , path : P ) -> Result < ( ) , Error > {
217255 let _ = path. as_ref ( ) . file_name ( ) . ok_or ( || {
218256 let message = format ! ( "Path must be filename {:?}" , path. as_ref( ) ) ;
0 commit comments