Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions libwild/src/args/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct WasmArgs {
pub(crate) common: super::CommonArgs,
pub(crate) lib_search_path: Vec<Box<Path>>,
pub(crate) export_symbols: Vec<String>,
pub(crate) required_export_symbols: Vec<String>,
pub(crate) export_memory: Option<String>,
pub(crate) z_stack_size: u32,
// Since LLVM 22, the default option is true.
Expand Down Expand Up @@ -70,6 +71,7 @@ impl Default for WasmArgs {
common: CommonArgs::default(),
lib_search_path: Vec::new(),
export_symbols: Vec::new(),
required_export_symbols: Vec::new(),
z_stack_size: DEFAULT_STACK_SIZE,
stack_first: true,
entry: Some(DEFAULT_ENTRY.to_owned()),
Expand Down Expand Up @@ -241,6 +243,17 @@ fn setup_argument_parser() -> ArgumentParser<WasmArgs> {
.declare_with_param()
.long("export")
.help("Force a symbol to be exported")
.execute(|args, _modifier_stack, value| {
let value = value.to_owned();
args.export_symbols.push(value.clone());
args.required_export_symbols.push(value);
Ok(())
});

parser
.declare_with_param()
.long("export-if-defined")
.help("Export a symbol if it is defined")
.execute(|args, _modifier_stack, value| {
args.export_symbols.push(value.to_owned());
Ok(())
Expand Down Expand Up @@ -445,4 +458,23 @@ mod tests {
assert_eq!(args.export_symbols, ["__main_void"]);
assert_eq!(args.common.inputs, []);
}

#[test]
fn export_if_defined() {
let args = parse_args([
"--export-if-defined",
"foo",
"--export=bar",
"--export-if-defined=baz",
"-o",
"out.wasm",
]);

assert_eq!(args.export_symbols, ["foo", "bar", "baz"]);
assert_eq!(args.required_export_symbols, ["bar"]);
assert_eq!(
Args::force_export_symbol_names(&args),
["foo", "bar", "baz"]
);
}
}
18 changes: 14 additions & 4 deletions libwild/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5499,7 +5499,7 @@ fn ensure_entry_export<'data>(
});
}

/// Export symbols requested via `--export`.
/// Export symbols requested via `--export` and `--export-if-defined`.
fn ensure_force_exports<'data>(
exports: &mut Vec<OutputExport<'data>>,
layout_inputs: &[WasmObjectLayoutInput<'data>],
Expand All @@ -5509,10 +5509,14 @@ fn ensure_force_exports<'data>(
entry_wrapper_func: Option<u32>,
) -> Result<()> {
for name in symbol_db.args.force_export_symbol_names() {
let required = symbol_db.args.required_export_symbols.contains(name);
let Some(symbol_id) =
symbol_db.get_unversioned(&UnversionedSymbolName::prehashed(name.as_bytes()))
else {
bail!("symbol exported via --export not found: {name}");
if required {
bail!("symbol exported via --export not found: {name}");
}
continue;
};
let def_id = symbol_db.definition(symbol_id);
let def_file_id = symbol_db.file_id_for_symbol(def_id);
Expand All @@ -5521,12 +5525,18 @@ fn ensure_force_exports<'data>(
.iter()
.position(|input| input.file_id == def_file_id)
else {
bail!("symbol exported via --export not found: {name}");
if required {
bail!("symbol exported via --export not found: {name}");
}
continue;
};
let def_input = &layout_inputs[def_obj_idx];
let def_sym = &def_input.symbols[def_input.symbol_id_range.id_to_offset(def_id)];
if def_sym.is_undefined() {
bail!("symbol exported via --export not found: {name}");
if required {
bail!("symbol exported via --export not found: {name}");
}
continue;
}

let index_map = object_index_maps
Expand Down
5 changes: 5 additions & 0 deletions wild/tests/sources/wasm/export/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
//#LinkArgs: --export does_not_exist
//#ExpectError: symbol exported via --export not found: does_not_exist

//#Config:export-if-defined
//#LinkArgs: --export-if-defined=not_exported --export-if-defined=does_not_exist
//#ExpectSym: not_exported
//#NoSym: does_not_exist

int foo(void) { return 42; }

void not_exported(void) {}
Expand Down
Loading