Skip to content

Commit 9932372

Browse files
committed
wasm-linker: support export flags
Adds support for both the `-rdynamic` and the `--export=<value>` flags. Support is added to both the incremental linker as well as the traditional linker (zld).
1 parent 4172c29 commit 9932372

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/link/Wasm.zig

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,9 +1800,36 @@ fn setupExports(wasm: *Wasm) !void {
18001800
if (wasm.base.options.output_mode == .Obj) return;
18011801
log.debug("Building exports from symbols", .{});
18021802

1803+
const force_exp_names = wasm.base.options.export_symbol_names;
1804+
if (force_exp_names.len > 0) {
1805+
var failed_exports = try std.ArrayList([]const u8).initCapacity(wasm.base.allocator, force_exp_names.len);
1806+
defer failed_exports.deinit();
1807+
1808+
for (force_exp_names) |exp_name| {
1809+
const name_index = wasm.string_table.getOffset(exp_name) orelse {
1810+
failed_exports.appendAssumeCapacity(exp_name);
1811+
continue;
1812+
};
1813+
const loc = wasm.globals.get(name_index) orelse {
1814+
failed_exports.appendAssumeCapacity(exp_name);
1815+
continue;
1816+
};
1817+
1818+
const symbol = loc.getSymbol(wasm);
1819+
symbol.setFlag(.WASM_SYM_EXPORTED);
1820+
}
1821+
1822+
if (failed_exports.items.len > 0) {
1823+
for (failed_exports.items) |exp_name| {
1824+
log.err("could not export '{s}', symbol not found", .{exp_name});
1825+
}
1826+
return error.MissingSymbol;
1827+
}
1828+
}
1829+
18031830
for (wasm.resolved_symbols.keys()) |sym_loc| {
18041831
const symbol = sym_loc.getSymbol(wasm);
1805-
if (!symbol.isExported()) continue;
1832+
if (!symbol.isExported(wasm.base.options.rdynamic)) continue;
18061833

18071834
const sym_name = sym_loc.getName(wasm);
18081835
const export_name = if (wasm.export_names.get(sym_loc)) |name| name else blk: {

src/link/Wasm/Symbol.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ pub fn isNoStrip(symbol: Symbol) bool {
139139
return symbol.flags & @enumToInt(Flag.WASM_SYM_NO_STRIP) != 0;
140140
}
141141

142-
pub fn isExported(symbol: Symbol) bool {
142+
pub fn isExported(symbol: Symbol, is_dynamic: bool) bool {
143143
if (symbol.isUndefined() or symbol.isLocal()) return false;
144-
if (symbol.isHidden()) return false;
145-
if (symbol.hasFlag(.WASM_SYM_EXPORTED)) return true;
146-
if (symbol.hasFlag(.WASM_SYM_BINDING_WEAK)) return false;
147-
return true;
144+
if (is_dynamic and symbol.isVisible()) return true;
145+
return symbol.hasFlag(.WASM_SYM_EXPORTED);
148146
}
149147

150148
pub fn isWeak(symbol: Symbol) bool {

0 commit comments

Comments
 (0)