-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Exported int sometimes corrupted with -sMAIN_MODULE #22980
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
Comments
I can't seem to reproduce this on tot:
|
I am still getting this on tip of tree:
|
I'm sorry, I got the order to link the object files wrong in the original report. #!/bin/bash
rm -rf build && mkdir build && cd build
echo "const int my_number = 123456;" > a.c
cat << EOF > b.c
extern const int my_number;
#include "stdio.h"
int main(void) {
printf("my_number: %d\n", my_number);
}
EOF
cat << EOF > pre.js
Module.preRun = () => {
console.log("HEAP32[_my_number/4]:", HEAP32[_my_number/4]);
}
EOF
emcc -fPIC -c a.c
emcc -fPIC -c b.c
LDFLAGS="\
-sEXPORTED_FUNCTIONS=_main,_my_number \
-sMAIN_MODULE=1 \
--pre-js=pre.js \
"
if test $# -eq 0; then
echo "This works fine:"
emcc $LDFLAGS a.o b.o
else
echo "This does not work:"
emcc $LDFLAGS b.o a.o
fi
node a.out.js If you run it with no arguments, you get the correct output. If you run it with one or more arguments, you get the wrong output. |
@sbc100 could you try again to see if you can reproduce this? |
Yes, I have been able to reproduce. I simplified a little removing the pre.js file:
|
An even simpler repro with just a single source file:
I found that issue and have a fix. |
In `-sMAIN_MODULE=1` mode we actually link twice, once to get the names the user exported symbols then again with everything exported. We when use the this `base_metadata` to limit the things that we export to on the JS module. However for data symbols we cannot use the addresses/values present in `base_metadata`. Fixes: emscripten-core#22980
In `-sMAIN_MODULE=1` mode we actually link twice, once to get the names the user exported symbols then again with everything exported. We when use the this `base_metadata` to limit the things that we export to on the JS module. However for data symbols we cannot use the addresses/values present in `base_metadata`. Fixes: emscripten-core#22980
Sometimes references a C variable via
HEAP32[_variable/4]
doesn't work.a.c
b.c
pre.js
Compile, link, execute
Edit: To reproduce,
b.o
comes beforea.o
.Output
I got:
It should print:
Changes that fix it
-sMAIN_MODULE
or-sMAIN_MODULE=2
.emcc $LDFLAGS a.o b.o
If I drop the
const
ina.c
, then it seems to return the same wrong number independent of the link order.Version of emscripten/emsdk
and
The text was updated successfully, but these errors were encountered: