Skip to content

Bug: undefined exported symbol when symbols file contain unused symbols #21516

Open

Description

@Labnann

Version: 3.1.52 onwards

Steps to reproduce:

//hello.c

#include <stdio.h>

void hello_world() {
    printf("Hello, World!\n");
}

Where the symfile is:

_hello_world
_unused_symbol

Now in terminal, run:

$ emcc -fPIC -c src/hello.c  -fPIC -DPIC -o src/hello.o
$ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -o hello.so src/hello.o

This should give the error:
emcc: error: undefined exported symbol: "_unused_symbol" [-Wundefined] [-Werror]
Expected:
hello.so should be compiled without any errors.

More information:

This bug is introduced in 3.1.52. With the commit 7e808f1

GCC does not generate error in such situation.
To demonstrate, if the symfile file were:

hello_world
unused_symbol

Running this in terminal would generate hello.so without any issues:

gcc -fPIC -c src/hello.c  -fPIC -DPIC -o src/hello.o
gcc -shared -Wl,--retain-symbols-file=symfile -o hello.so src/hello.o

Is this error: "undefined exported symbol" introduced from 7e808f1
expected behavior or is it a bug? Many projects use only one symbol file
in whole project, and gcc seems to allow it.

Activity

  1. sbc100 commented on Mar 14, 2024

    @sbc100
    Collaborator

    I've not heard of --retain-symbols-file before. Is this a commonly used flag?

    I would find it a little strange to say -sEXPORTED_SYMBOLS=foo when linking then allow foo to be undefined.. so I guess from my POV this is working as intended. Is the problem that you don't know the list of symbols that you expect to be defined in your side module? (i.e. you only have one big list that contains symbols from many different side modules).

    It sounds like maybe you want something more like -sMAYBE_EXPORT_SYMBOLS. BTW, we do have -sERROR_ON_UNDEFINED_SYMOLS=0. I guess we could modify that setting that it applies to explicitly exported functions too. Would that work for you? i.e. -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMOLS=0?

  2. Labnann commented on Mar 20, 2024

    @Labnann
    Author

    I've not heard of --retain-symbols-file before. Is this a commonly used flag?

    We can do it this way:

    symfile_gcc.ver:

    {
    	global:
    		hello_world;
    		hello2_world;
    	local: *;
    };
    

    Then in terminal:

    $ gcc  -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ gcc -shared -Wl,--version-script=symfile_gcc.ver -o hello.so src/hello.o
    

    This executes without any issue.

    I would find it a little strange to say -sEXPORTED_SYMBOLS=foo when linking then allow foo to be undefined.. so I guess from my POV this is working as intended. Is the problem that you don't know the list of symbols that you expect to be defined in your side module? (i.e. you only have one big list that contains symbols from many different side modules).

    It sounds like maybe you want something more like -sMAYBE_EXPORT_SYMBOLS. BTW, we do have -sERROR_ON_UNDEFINED_SYMOLS=0. I guess we could modify that setting that it applies to explicitly exported functions too. Would that work for you? i.e. -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMOLS=0?

    Thanks for the suggestion. I tried this. Here is the result:

    $ emcc -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMBOLS=1 -o hello.so src/hello.o
    emcc: error: undefined exported symbol: "_hello2_world" [-Wundefined] [-Werror]
    

    The error still exists :(

  3. sbc100 commented on Mar 20, 2024

    @sbc100
    Collaborator

    Thanks for the suggestion. I tried this. Here is the result:

    $ emcc -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMBOLS=1 -o hello.so src/hello.o
    emcc: error: undefined exported symbol: "_hello2_world" [-Wundefined] [-Werror]
    

    The error still exists :(

    Indeed, I was suggesting that we could extent -sERROR_ON_UNDEFINED_SYMBOLS=0 to apply to explicitly exported symbols too.

    However, I'm not really sure its a good idea.

    Is there some reason that in your case you cannot be precise about that exact symbols you want to export from each shared library?

  4. Labnann commented on Mar 20, 2024

    @Labnann
    Author

    I am sorry, I tried both, got the same error and accidentally copy pasted result from the wrong output.

    Here is the correct one:

    $ emcc -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMBOLS=0 -o hello.so src/hello.o
    emcc: error: undefined exported symbol: "_hello2_world" [-Wundefined] [-Werror]
    

    I was experimenting a use case when user has the freedom to disable certain libraries
    without changing the sym file. (I will elaborate more after I run some more tests).

  5. Labnann commented on Mar 31, 2024

    @Labnann
    Author

    By the way, it generates the .so file anyway. And it is usable.

  6. Labnann commented on Mar 31, 2024

    @Labnann
    Author

    Thanks for the suggestion. I tried this. Here is the result:

    $ emcc -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMBOLS=1 -o hello.so src/hello.o
    emcc: error: undefined exported symbol: "_hello2_world" [-Wundefined] [-Werror]
    

    The error still exists :(

    Indeed, I was suggesting that we could extent -sERROR_ON_UNDEFINED_SYMBOLS=0 to apply to explicitly exported symbols too.

    However, I'm not really sure its a good idea.

    Is there some reason that in your case you cannot be precise about that exact symbols you want to export from each shared library?

    I am sorry, I tried both, got the same error and accidentally copy pasted result from the wrong output.

    Here is the correct one:

    $ emcc -c src/hello.c  -fPIC -DPIC -o src/hello.o
    $ emcc -sSIDE_MODULE=2 -sEXPORTED_FUNCTIONS=@symfile -sERROR_ON_UNDEFINED_SYMBOLS=0 -o hello.so src/hello.o
    emcc: error: undefined exported symbol: "_hello2_world" [-Wundefined] [-Werror]
    

    I was experimenting a use case when user has the freedom to disable certain libraries without changing the sym file. (I will elaborate more after I run some more tests).

    So, my exact reason to report this was experimenting with another bug. Where emcc seems to be not "seeing" an already included library. I had a reproducer ready for that bug.

    But the reproducer was not working in the newer versions, which lead me to discover this one. So I filed this one instead.

    Anyway, I was able to create a new reproducer for the other bug, bypassing this issue. So I will create a separate issue reporting that bug and mention it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions