Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 9980e0d

Browse files
committed
move table manifest relocations into the compiler
The table manifest was already being defined in the compiler, but the relocation for the pointer to the guest tables was not, requiring a fixup after the fact. There's no need to do the fixup; we can define the appropriate relocation while we're building all the tables.
1 parent 6306e4f commit 9980e0d

3 files changed

Lines changed: 22 additions & 33 deletions

File tree

lucetc/src/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'a> Compiler<'a> {
177177

178178
write_module_data(&mut self.clif_module, module_data_bytes)?;
179179
write_startfunc_data(&mut self.clif_module, &self.decls)?;
180-
let table_names = write_table_data(&mut self.clif_module, &self.decls)?;
180+
let table_len = write_table_data(&mut self.clif_module, &self.decls)?;
181181

182182
let function_manifest: Vec<(String, FunctionSpec)> = self
183183
.clif_module
@@ -199,7 +199,7 @@ impl<'a> Compiler<'a> {
199199
self.clif_module.finish(),
200200
module_data_len,
201201
function_manifest,
202-
table_names,
202+
table_len,
203203
)?;
204204

205205
Ok(obj)

lucetc/src/output.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error::Error;
22
use crate::name::Name;
33
use crate::stack_probe;
4-
use crate::table::{TABLE_REF_SIZE, TABLE_SYM};
4+
use crate::table::TABLE_SYM;
55
use crate::traps::{translate_trapcode, trap_sym_for_func};
66
use byteorder::{LittleEndian, WriteBytesExt};
77
use cranelift_codegen::{binemit::TrapSink, ir, isa};
@@ -54,7 +54,7 @@ impl ObjectFile {
5454
product: FaerieProduct,
5555
module_data_len: usize,
5656
mut function_manifest: Vec<(String, FunctionSpec)>,
57-
table_manifest: Vec<Name>,
57+
table_manifest_len: usize,
5858
) -> Result<Self, Error> {
5959
let mut obj = Self {
6060
artifact: product.artifact,
@@ -90,12 +90,11 @@ impl ObjectFile {
9090

9191
obj.write_trap_tables(&trap_manifest)?;
9292
obj.write_function_manifest(function_manifest.as_slice())?;
93-
obj.link_tables(table_manifest.as_slice())?;
9493

9594
// And now write out the actual structure tying together all the data in this module.
9695
write_module(
9796
module_data_len,
98-
table_manifest.len(),
97+
table_manifest_len,
9998
function_manifest.len(),
10099
&mut obj.artifact,
101100
)?;
@@ -258,19 +257,6 @@ impl ObjectFile {
258257
Ok(())
259258
}
260259

261-
fn link_tables(&mut self, tables: &[Name]) -> Result<(), Error> {
262-
for (idx, table) in tables.iter().enumerate() {
263-
self.artifact
264-
.link(Link {
265-
from: TABLE_SYM,
266-
to: table.symbol(),
267-
at: (TABLE_REF_SIZE * idx) as u64,
268-
})
269-
.map_err(|source| Error::Failure(source, "Table error".to_owned()))?;
270-
}
271-
Ok(())
272-
}
273-
274260
pub fn write<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
275261
let _ = path.as_ref().file_name().ok_or(|| {
276262
let message = format!("Path must be filename {:?}", path.as_ref());

lucetc/src/table.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::decls::{ModuleDecls, TableDecl};
22
use crate::error::Error;
33
use crate::module::UniqueFuncIndex;
4-
use crate::name::Name;
54
use crate::pointer::NATIVE_POINTER_SIZE;
65
use byteorder::{LittleEndian, WriteBytesExt};
76
use cranelift_codegen::entity::EntityRef;
@@ -53,9 +52,10 @@ fn table_elements(decl: &TableDecl<'_>) -> Result<Vec<Elem>, Error> {
5352
pub 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

Comments
 (0)