Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wat2wasm --relocatable should record exports in linking symtab #1534

Closed
wingo opened this issue Sep 8, 2020 · 1 comment · Fixed by #1535
Closed

wat2wasm --relocatable should record exports in linking symtab #1534

wingo opened this issue Sep 8, 2020 · 1 comment · Fixed by #1535

Comments

@wingo
Copy link
Contributor

wingo commented Sep 8, 2020

Consider a.c which exports a function that calls something from b.c:

$ cat a.c
void b(void);
__attribute__((export_name("a"))) void a(void) { b(); }

$ cat b.c
void b(void) {}

With clang and wasm-ld, we can make a wasm file:

$ clang -Oz --target=wasm32 -nostdlib -c -o a.o a.c
$ clang -Oz --target=wasm32 -nostdlib -c -o b.o b.c
$ wasm-ld --no-entry -o clang.wasm a.clang.o b.o

Let's say we want to replace a.c with a wat file.

$ wasm2wat -o a.wat a.o
$ wat2wasm --relocatable -o a.wat.o a.wat
$ wasm-ld --no-entry -o wat.wasm a.wat.o b.o

OK great. However:

$ wasm2wat wat.wasm
(module
  (import "env" "memory" (memory (;0;) 2))
  (table (;0;) 1 1 funcref)
  (global (;0;) (mut i32) (i32.const 66560)))

That is to say: dude, where's my export (of a)?

The problem is that in the a.o as produced by clang, the linking and reloc sections look like this:

Custom:
 - name: "linking"
  - symbol table [count=2]
   - 0: F <a> func=1 exported no_strip binding=global vis=hidden
   - 1: F <env.b> func=0 undefined binding=global vis=default
Custom:
 - name: "reloc.CODE"
  - relocations for section: 4 (Code) [1]
   - R_WASM_FUNCTION_INDEX_LEB offset=0x000004(file=0x000077) symbol=1 <env.b>
Custom:
 - name: "producers"

Code Disassembly:

000075 func[1] <a>:
 000076: 10 80 80 80 80 00          | call 0 <env.b>
 00007c: 0b                         | end

Whereas as produced by wat2wasm, they look like this:

Custom:
 - name: "linking"
  - symbol table [count=1]
   - 0: F <env.b> func=0 undefined binding=global vis=default
Custom:
 - name: "reloc.Code"
  - relocations for section: 4 (Code) [1]
   - R_WASM_FUNCTION_INDEX_LEB offset=0x000004(file=0x000063) symbol=0 <env.b>

Code Disassembly:

000061 func[1] <a>:
 000062: 10 80 80 80 80 00          | call 0 <env.b>
 000068: 0b                         | end

I know that on #1137 there is a question about whether to keep up the functionality of --relocatable at all, but I think that with a couple of bug-fixes it can be usable enough for me, so I will take a look at fixing this unless there's an objection.

wingo added a commit to wingo/wabt that referenced this issue Sep 8, 2020
This commit refactors the representations of symbols in the binary
writer.  It will allow us to fix WebAssembly#1534 in a followup.
@wingo
Copy link
Contributor Author

wingo commented Sep 8, 2020

To fix this, I could either add an "exported" flag to the various IR nodes, and use it when recording symbol references in the binary writer, or I could precompute a side table in the binary writer if we are doing --relocatable. I guess it's easier to reason about a side table, compared to a mutable flag, so I'll take that approach.

wingo added a commit to wingo/wabt that referenced this issue Sep 9, 2020
This commit refactors the representations of symbols in the binary
writer.  It will allow us to fix WebAssembly#1534 in a followup.
wingo added a commit to wingo/wabt that referenced this issue Sep 9, 2020
This commit refactors the representations of symbols in the binary
writer.  It will allow us to fix WebAssembly#1534 in a followup.
binji pushed a commit that referenced this issue Sep 10, 2020
This commit refactors the representations of symbols in the binary
writer.  It will allow us to fix #1534 in a followup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant