emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

clarification request: generating a bitcode file with 2.0.5

Open ericmandel opened this issue 6 years ago • 4 comments

I generate wasm and non-wasm versions of my project because some environments (e.g. Jupyter) do not yet support wasm. To date, I have done this by generating a bitcode file and then linking it with wasm and non-wasm flags:

astroem.bc:	$(OBJS) $(PKGOBJS) $(LIBS)
		$(EMCC) -o $@ $+

$(ASTROEMJS):	astroem.bc pre.js post.js shell-pre.js shell-post.js Makefile
		$(EMCC) $(NOWASMOPTS) astroem.bc -o $(ASTROEMJS);
                 ...

$(ASTROEMWJS):	astroem.bc pre.js post.js shell-pre.js shell-post.js Makefile
		$(EMCC) $(WASMOPTS) astroem.bc -o $(ASTROEMWJS);
                 ....

As of v2.0.2, the astroem.bc rule now produces an executable instead of bitcode. Trial and error indicates that I need to add the -r switch:

astroem.bc:	$(OBJS) $(PKGOBJS) $(LIBS)
		$(EMCC) -r -o $@ $+

but I can find no emscripten or clang documentation to describe what -r does.

Can you please verify that -r is the switch needed to generate a bitcode file from multiple input sources, or else tell me what is required?

Thanks,

Eric

ericmandel avatar Sep 30 '20 16:09 ericmandel

Indeed this behaviour was recently changed in order to be more consistent with existing compilers. Sorry for the breakage. The changelog entry was this one:

- The default output format is now executable JavaScript.  Previously we would   
  default to output objecting files unless, for example, the output name ended   
  in `.js`.  This is contrary to behaviour of clang and gcc.  Now emscripten        
  will always produce and executable unless the `-c`, `-r` or `-shared` flags       
  are given.  This is true even when the name of the output file ends in `.o`.   
  e.g, `emcc foo.c -o foo.o` will produce a JavaScript file called `foo.o`.         
  This might surprise some users (although it matches the behavior of existing   
  toolchains) so we now produce a warning in this case.                          

The -r flag corresponds to the -r/--relocatable linker flag which is documented in the ld man page. It basically take a bunch of input objects and combines them into a single object file.

Both gcc and clang happen to also support passing -r through to ld although this is not well documented.

Having said all that your best bet is to switch to an ar archive which means you should replace astroem.bc with libastroem.a and ceate is using emar rcs libastroem.a <objs>.

sbc100 avatar Sep 30 '20 16:09 sbc100

Thanks for the quick response! If I use emar, I get hundreds of undefined symbols like this:

error: undefined symbol: wcsceq (referenced by top-level compiled C/C++ code)
warning: _wcsceq may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library

The wcsceq routine is used inside the C code that is compiled by emcc, but not outside. Do I really have to declare each of them in EXPORTED_FUNCTIONS? Or do I just want to suppress these messages?

ericmandel avatar Sep 30 '20 17:09 ericmandel

No that should not be needed. Can you post the full link command you are using? Is wcsceq supposed to be defined in your library?

You can use emar t libastroem.a to see what files ended up in the archive and llvm-nm libastroem.a to see all the symbols.

sbc100 avatar Sep 30 '20 17:09 sbc100

Ah, never mind, I had a mistake in the generation of the new library file. Looks like it all works now using emar. Thanks for your help!

ericmandel avatar Sep 30 '20 17:09 ericmandel