Skip to content

Commit 1820ac5

Browse files
authored
Add TABLE_BASE setting for controlling the offset of static table slots. (#20149)
See https://reviews.llvm.org/D158892 Fixes: #20097
1 parent dfb3585 commit 1820ac5

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ See docs/process.md for more on how version tagging works.
4141
the existing behaviour for `MINIMAL_RUNTIME` and `STRICT` mode.
4242
If you use GLFW you now need to explictly opt into it using `-sUSE_GLFW` or
4343
`-lglfw`. (#19939)
44+
- A new settings `TABLE_BASE` was introduced that can be used to place static
45+
function addresses (table slots) at a certain offset. This defaults to 1
46+
which is the previously fixed value. (#20149)
4447

4548
3.1.45 - 08/23/23
4649
-----------------

src/library.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,12 +3520,12 @@ addToLibrary({
35203520
// tell the memory segments where to place themselves
35213521
__memory_base: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': false}, {{{ to64(GLOBAL_BASE) }}})",
35223522
// the wasm backend reserves slot 0 for the NULL function pointer
3523-
__table_base: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': false}, {{{ to64(1) }}})",
3523+
__table_base: "new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': false}, {{{ to64(TABLE_BASE) }}})",
35243524
#if MEMORY64 == 2
35253525
__memory_base32: "new WebAssembly.Global({'value': 'i32', 'mutable': false}, {{{ GLOBAL_BASE }}})",
35263526
#endif
35273527
#if MEMORY64
3528-
__table_base32: 1,
3528+
__table_base32: {{{ TABLE_BASE }}},
35293529
#endif
35303530
// To support such allocations during startup, track them on __heap_base and
35313531
// then when the main module is loaded it reads that value and uses it to

src/settings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ var ALLOW_TABLE_GROWTH = false;
248248
// [link]
249249
var GLOBAL_BASE = 1024;
250250

251+
// Where where table slots (function addresses) are allocated.
252+
// This must be at least 1 to reserve the zero slot for the null pointer.
253+
// [link]
254+
var TABLE_BASE = 1;
255+
251256
// Whether closure compiling is being run on this output
252257
// [link]
253258
var USE_CLOSURE_COMPILER = false;

test/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13725,3 +13725,12 @@ def test_no_minify(self):
1372513725
self.run_process([EMCC, test_file('hello_world.c'), '--pre-js=pre.js', '-Oz', '--minify=0'])
1372613726
content = read_file('a.out.js')
1372713727
self.assertContained(comment, content)
13728+
13729+
def test_table_base(self):
13730+
create_file('test.c', r'''
13731+
#include <stdio.h>
13732+
int main() {
13733+
printf("addr = %p\n", &printf);
13734+
}''')
13735+
self.do_runf('test.c', 'addr = 0x1\n')
13736+
self.do_runf('test.c', 'addr = 0x400\n', emcc_args=['-sTABLE_BASE=1024'])

tools/building.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ def lld_flags_for_executable(external_symbols):
232232

233233
if settings.STACK_FIRST:
234234
cmd.append('--stack-first')
235-
elif not settings.RELOCATABLE:
236-
cmd.append('--global-base=%s' % settings.GLOBAL_BASE)
235+
236+
if not settings.RELOCATABLE:
237+
cmd.append('--table-base=%s' % settings.TABLE_BASE)
238+
if not settings.STACK_FIRST:
239+
cmd.append('--global-base=%s' % settings.GLOBAL_BASE)
237240

238241
return cmd
239242

0 commit comments

Comments
 (0)