Skip to content

Commit e8eaa01

Browse files
authored
Fix linking SIDE_MODULE with ports (#26514)
With the removal of the RELOCATABLE flag (cfb4642), building side module with ports are not working correctly because of 1) `-fPIC` flag missing when building ports, and 2) `pic` directory not correctly passed when calculating the libdir. Without this, building a side module that links to the port library fails with the following error: ```c // dummy.c #include <emscripten/emscripten.h> #include <jpeglib.h> EMSCRIPTEN_KEEPALIVE int dummy_libjpeg_smoke_test(void) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); return 0; } ``` ``` $ emcc dummy.c -o dummy.wasm -s USE_LIBJPEG=1 -s SIDE_MODULE=1 -ljpeg wasm-ld: error: /home/siha/dev/temp/emscripten-regression/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libjpeg.a(jdinput.c.o): relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `finish_input_pass`; recompile with -fPIC wasm-ld: error: /home/siha/dev/temp/emscripten-regression/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libjpeg.a(jdinput.c.o): relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `start_input_pass`; recompile with -fPIC wasm-ld: error: /home/siha/dev/temp/emscripten-regression/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libjpeg.a(jdinput.c.o): relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `reset_input_controller`; recompile with -fPIC wasm-ld: error: /home/siha/dev/temp/emscripten-regression/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/libjpeg.a(jdinput.c.o): relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `consume_markers`; recompile with -fPIC ```
1 parent 1aa7fb5 commit e8eaa01

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

test/test_other.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,42 @@ def test_libjpeg(self):
25592559
cflags=['--embed-file', 'screenshot.jpg', '--use-port=libjpeg'],
25602560
args=['screenshot.jpg'])
25612561

2562+
@requires_network
2563+
def test_side_module_with_ports(self):
2564+
# Verify that ports can be used in side modules, and that the resulting
2565+
# side module can be used from a main module.
2566+
create_file('side.c', r'''
2567+
#include <stdio.h>
2568+
#include <gif_lib.h>
2569+
2570+
void gif_side_test(void) {
2571+
ColorMapObject* map = GifMakeMapObject(2, NULL);
2572+
if (map) {
2573+
GifFreeMapObject(map);
2574+
}
2575+
puts("gif side ok");
2576+
}
2577+
''')
2578+
2579+
create_file('main.c', r'''
2580+
#include <stdio.h>
2581+
2582+
void gif_side_test(void);
2583+
2584+
int main() {
2585+
gif_side_test();
2586+
puts("main ok");
2587+
return 0;
2588+
}
2589+
''')
2590+
2591+
self.emcc('side.c', args=['-sSIDE_MODULE', '-sUSE_GIFLIB', '-lgif', '-o', 'libgif_side.so'])
2592+
self.assertExists('libgif_side.so')
2593+
2594+
# Linking the side module into the main module should cause it to be loaded
2595+
# automatically at runtime.
2596+
self.do_runf('main.c', 'gif side ok\n', cflags=['-sMAIN_MODULE=2', 'libgif_side.so'])
2597+
25622598
@requires_network
25632599
@also_with_wasm64
25642600
def test_bullet(self):

tools/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def get_lib_dir(absolute):
124124
subdir.append('thinlto')
125125
else:
126126
subdir.append('lto')
127-
if settings.MAIN_MODULE:
127+
if settings.MAIN_MODULE or settings.SIDE_MODULE:
128128
subdir.append('pic')
129129
if subdir:
130130
path = Path(path, '-'.join(subdir))

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_base_cflags(build_dir, force_object_files=False, preprocess=True):
6161
flags = ['-g', '-sSTRICT', '-Werror']
6262
if settings.LTO and not force_object_files:
6363
flags += ['-flto=' + settings.LTO]
64-
if settings.MAIN_MODULE:
64+
if settings.MAIN_MODULE or settings.SIDE_MODULE:
6565
# Explicitly include `-sMAIN_MODULE` when building system libraries.
6666
# `-fPIC` alone is not enough to configure trigger the building and
6767
# caching of `pic` libraries (see `get_lib_dir` in `cache.py`)

0 commit comments

Comments
 (0)