emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.36k stars 3.25k forks source link

Fix asan.test_dlfcn_preload. NFC #22092

Closed sbc100 closed 2 weeks ago

sbc100 commented 2 weeks ago

This was a new test added in #21985.

This changes means that any symbols that libb.so needs are exported/preserved when building the main module.

kripken commented 2 weeks ago

From the comment on AUTOLOAD_DYLIBS I only understand that it affects the runtime dynamic linking process. Is there more to it then? The docs say

// For MAIN_MODULE builds, automatically load any dynamic library dependencies
// on startup, before loading the main module.
sbc100 commented 2 weeks ago

From the comment on AUTOLOAD_DYLIBS I only understand that it affects the runtime dynamic linking process. Is there more to it then? The docs say

// For MAIN_MODULE builds, automatically load any dynamic library dependencies
// on startup, before loading the main module.

When building with -sMAIN_MODULE=2 then the linker will keep alive any symbols needed by side modules. So the documention for this is really under -sMAIN_MODULE=2 :

https://github.com/emscripten-core/emscripten/blob/0c504193efb3d0b51d30c07895544b29cbad1950/src/settings.js#L1135-L1137

kripken commented 2 weeks ago

Fix lgtm but I still don't quite understand the docs. Are you saying that AUTOLOAD_DYLIBS=0 tells it to not load dynamic libraries automatically, and from that it infers that it does not need to keep alive the symbols those libraries need? That seems a little odd if so (if the dynamic libraries were provided on the commandline then I'd expect autoload=0 to just affect how they are loaded, not whether they are taken into account otherwise).

sbc100 commented 2 weeks ago

Fix lgtm but I still don't quite understand the docs. Are you saying that AUTOLOAD_DYLIBS=0 tells it to not load dynamic libraries automatically, and from that it infers that it does not need to keep alive the symbols those libraries need? That seems a little odd if so (if the dynamic libraries were provided on the commandline then I'd expect autoload=0 to just affect how they are loaded, not whether they are taken into account otherwise).

When you add shared libraries to the command line that normally does two things:

  1. It tells the dynamic loader to load those libraries on startup.
  2. It tells the linker to keep any needed symbols from the main module alive.

The AUTOLOAD_DYLIBS=0 setting is a way to explicitly opt out of (1) while still allowing (2).

kripken commented 2 weeks ago

I see, thanks. That all makes perfect sense.

I was still confused so I re-read the code and I think I misread

['--preload-file', 'libb.so', '--use-preload-plugins', '-L.', '-sAUTOLOAD_DYLIBS=0', 'libb.so']

libb.so appears twice, once as a preload and once so the compiler knows its symbols, and the second one is added - I think that is what I've been missing.