11use crate :: decls:: { ModuleDecls , TableDecl } ;
22use crate :: error:: Error ;
33use crate :: module:: UniqueFuncIndex ;
4- use crate :: name:: Name ;
54use crate :: pointer:: NATIVE_POINTER_SIZE ;
65use byteorder:: { LittleEndian , WriteBytesExt } ;
76use cranelift_codegen:: entity:: EntityRef ;
@@ -53,9 +52,10 @@ fn table_elements(decl: &TableDecl<'_>) -> Result<Vec<Elem>, Error> {
5352pub fn write_table_data < B : ClifBackend > (
5453 clif_module : & mut ClifModule < B > ,
5554 decls : & ModuleDecls < ' _ > ,
56- ) -> Result < Vec < Name > , Error > {
55+ ) -> Result < usize , Error > {
5756 let mut tables_vec = Cursor :: new ( Vec :: new ( ) ) ;
58- let mut table_names: Vec < Name > = Vec :: new ( ) ;
57+ let mut table_ctx = DataContext :: new ( ) ;
58+ let mut table_len = 0 ;
5959
6060 if let Ok ( table_decl) = decls. get_table ( TableIndex :: new ( 0 ) ) {
6161 // Indirect calls are performed by looking up the callee function and type in a table that
@@ -71,7 +71,7 @@ pub fn write_table_data<B: ClifBackend>(
7171 table. write_u64 :: < LittleEndian > ( elem) . unwrap ( )
7272 }
7373
74- let mut table_ctx = DataContext :: new ( ) ;
74+ let mut table_data_ctx = DataContext :: new ( ) ;
7575
7676 // table.elems is a vector that gives every entry of the table, either specifying the
7777 // wasm function index or that no element was given for that table entry.
@@ -86,10 +86,10 @@ pub fn write_table_data<B: ClifBackend>(
8686
8787 // Second element in row is the pointer to the function. The Reloc is doing the work
8888 // here. We put a 0 in the table data itself to be overwritten at link time.
89- let funcref = table_ctx . import_function ( func. name . into ( ) ) ;
89+ let funcref = table_data_ctx . import_function ( func. name . into ( ) ) ;
9090 let position = table_data. position ( ) ;
9191 assert ! ( position < <u32 >:: max_value( ) as u64 ) ;
92- table_ctx . write_function_addr ( position as u32 , funcref) ;
92+ table_data_ctx . write_function_addr ( position as u32 , funcref) ;
9393 putelem ( & mut table_data, 0 ) ;
9494 } else {
9595 let message = format ! ( "{:?}" , func_index) ;
@@ -107,35 +107,38 @@ pub fn write_table_data<B: ClifBackend>(
107107 }
108108 }
109109 }
110- table_ctx . define ( table_data. into_inner ( ) . into_boxed_slice ( ) ) ;
110+ table_data_ctx . define ( table_data. into_inner ( ) . into_boxed_slice ( ) ) ;
111111 let table_id = table_decl
112112 . contents_name
113113 . as_dataid ( )
114114 . expect ( "tables are data" ) ;
115- clif_module. define_data ( table_id, & table_ctx ) ?;
115+ clif_module. define_data ( table_id, & table_data_ctx ) ?;
116116
117117 // have to link TABLE_SYM, table_id,
118118 // add space for the TABLE_SYM pointer
119+ let dataref = clif_module. declare_data_in_data ( table_id, & mut table_ctx) ;
120+ let position = tables_vec. position ( ) ;
121+ assert ! ( position < <u32 >:: max_value( ) as u64 ) ;
122+ table_ctx. write_data_addr ( position as u32 , dataref, 0 as i64 ) ;
119123 tables_vec. write_u64 :: < LittleEndian > ( 0 ) . unwrap ( ) ;
120124
121125 // Define the length of the table as a u64:
126+ table_len = elements. len ( ) ;
122127 tables_vec
123- . write_u64 :: < LittleEndian > ( elements . len ( ) as u64 )
128+ . write_u64 :: < LittleEndian > ( table_len as u64 )
124129 . unwrap ( ) ;
125- table_names. push ( table_decl. contents_name . clone ( ) )
126130 }
127131
128132 let inner = tables_vec. into_inner ( ) ;
129133
130- let mut table_data_ctx = DataContext :: new ( ) ;
131- table_data_ctx. define ( inner. into_boxed_slice ( ) ) ;
134+ table_ctx. define ( inner. into_boxed_slice ( ) ) ;
132135
133136 clif_module. define_data (
134137 decls
135138 . get_tables_list_name ( )
136139 . as_dataid ( )
137140 . expect ( "lucet_tables is declared as data" ) ,
138- & table_data_ctx ,
141+ & table_ctx ,
139142 ) ?;
140- Ok ( table_names )
143+ Ok ( table_len )
141144}
0 commit comments