diff options
author | Sam Clegg <sbc@chromium.org> | 2019-04-02 14:53:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-02 14:53:48 -0700 |
commit | b192c53c6ef549bb5264bc58a71424ce311a4890 (patch) | |
tree | 895f6535ba16bed2c5ec75d365dfe37eda776315 /scripts | |
parent | 8e8cb825cd28bc1c188f9cf796bd97626bc1bb40 (diff) | |
download | binaryen-b192c53c6ef549bb5264bc58a71424ce311a4890.tar.gz binaryen-b192c53c6ef549bb5264bc58a71424ce311a4890.tar.bz2 binaryen-b192c53c6ef549bb5264bc58a71424ce311a4890.zip |
wasm-emscripten-finalize: Improve shared library support (#1961)
Convert PIC code generated by llvm to work with the current emscripten
ABI for dynamic linking:
- Convert mutable global imports from GOT.mem and GOT.func into
internal globals.
- Initialize these globals on started up in g$foo and f$foo imported functions
to calculate addresses at runtime.
Also:
- Add a test case for linking and finalizing a shared library
- Allow __stack_pointer global to be non-existent as can be case for
a shared library.
- Allow __stack_pointer global to be an import, as can be the case for
a shared library.
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/test/generate_lld_tests.py | 68 | ||||
-rwxr-xr-x | scripts/test/lld.py | 15 |
2 files changed, 50 insertions, 33 deletions
diff --git a/scripts/test/generate_lld_tests.py b/scripts/test/generate_lld_tests.py index f3905b077..31634e7b0 100755 --- a/scripts/test/generate_lld_tests.py +++ b/scripts/test/generate_lld_tests.py @@ -22,7 +22,7 @@ import shared def files_with_extensions(path, extensions): for file in sorted(os.listdir(path)): - _, ext = os.path.splitext(file) + ext = os.path.splitext(file)[1] if ext in extensions: yield file, ext @@ -33,37 +33,47 @@ def generate_wast_files(llvm_bin, emscripten_root): lld_path = os.path.join(shared.options.binaryen_test, 'lld') for src_file, ext in files_with_extensions(lld_path, ['.c', '.cpp']): print '..', src_file - try: - obj_file = src_file.replace(ext, '.o') + obj_file = src_file.replace(ext, '.o') + + src_path = os.path.join(lld_path, src_file) + obj_path = os.path.join(lld_path, obj_file) + + wasm_file = src_file.replace(ext, '.wasm') + wast_file = src_file.replace(ext, '.wast') - src_path = os.path.join(lld_path, src_file) - obj_path = os.path.join(lld_path, obj_file) - run_command([ - os.path.join(llvm_bin, 'clang'), src_path, '-o', obj_path, - '--target=wasm32-unknown-unknown-wasm', - '-c', - '-nostdinc', - '-Xclang', '-nobuiltininc', - '-Xclang', '-nostdsysteminc', - '-Xclang', '-I%s/system/include' % emscripten_root, - '-O1', - ]) + obj_path = os.path.join(lld_path, obj_file) + wasm_path = os.path.join(lld_path, wasm_file) + wast_path = os.path.join(lld_path, wast_file) + is_shared = 'shared' in src_file - wasm_file = src_file.replace(ext, '.wasm') - wast_file = src_file.replace(ext, '.wast') + compile_cmd = [ + os.path.join(llvm_bin, 'clang'), src_path, '-o', obj_path, + '--target=wasm32-unknown-unknown-wasm', + '-c', + '-nostdinc', + '-Xclang', '-nobuiltininc', + '-Xclang', '-nostdsysteminc', + '-Xclang', '-I%s/system/include' % emscripten_root, + '-O1', + ] - obj_path = os.path.join(lld_path, obj_file) - wasm_path = os.path.join(lld_path, wasm_file) - wast_path = os.path.join(lld_path, wast_file) - run_command([ - os.path.join(llvm_bin, 'wasm-ld'), - '-z', '-stack-size=1048576', - obj_path, '-o', wasm_path, - '--entry=main', - '--allow-undefined', - '--export', '__wasm_call_ctors', - '--global-base=568', - ]) + link_cmd = [ + os.path.join(llvm_bin, 'wasm-ld'), '-flavor', 'wasm', + '-z', '-stack-size=1048576', + obj_path, '-o', wasm_path, + '--entry=main', + '--allow-undefined', + '--export', '__wasm_call_ctors', + '--global-base=568', + ] + if is_shared: + compile_cmd.append('-fPIC') + compile_cmd.append('-fvisibility=default') + link_cmd.append('-shared') + + try: + run_command(compile_cmd) + run_command(link_cmd) run_command(shared.WASM_DIS + [wasm_path, '-o', wast_path]) finally: # Don't need the .o or .wasm files, don't leave them around diff --git a/scripts/test/lld.py b/scripts/test/lld.py index a96062331..fae551f0d 100755 --- a/scripts/test/lld.py +++ b/scripts/test/lld.py @@ -21,6 +21,13 @@ from shared import ( ) +def args_for_finalize(filename): + if 'shared' in filename: + return ['--side-module'] + else: + return ['--global-base=568', '--initial-stack-pointer=16384'] + + def test_wasm_emscripten_finalize(): print '\n[ checking wasm-emscripten-finalize testcases... ]\n' @@ -36,8 +43,8 @@ def test_wasm_emscripten_finalize(): if ext != '.out' and not os.path.exists(expected_file): continue - cmd = (WASM_EMSCRIPTEN_FINALIZE + - [wast_path, '-S', '--global-base=568', '--initial-stack-pointer=16384'] + ext_args) + cmd = WASM_EMSCRIPTEN_FINALIZE + [wast_path, '-S'] + ext_args + cmd += args_for_finalize(os.path.basename(wast_path)) actual = run_command(cmd) if not os.path.exists(expected_file): @@ -65,8 +72,8 @@ def update_lld_tests(): out_path = wast_path + ext if ext != '.out' and not os.path.exists(out_path): continue - cmd = (WASM_EMSCRIPTEN_FINALIZE + - [wast_path, '-S', '--global-base=568', '--initial-stack-pointer=16384'] + ext_args) + cmd = WASM_EMSCRIPTEN_FINALIZE + [wast_path, '-S'] + ext_args + cmd += args_for_finalize(os.path.basename(wast_path)) actual = run_command(cmd) with open(out_path, 'w') as o: o.write(actual) |