Skip to content

Commit

Permalink
upg patches and linker
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Nov 25, 2024
1 parent 410094d commit b2400eb
Show file tree
Hide file tree
Showing 28 changed files with 853 additions and 582 deletions.
103 changes: 58 additions & 45 deletions cibuild/getsyms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import sys
import os


def dbg(*argv, **kw):
kw.setdefault('file',sys.stderr)
return print(*argv,**kw)
kw.setdefault("file", sys.stderr)
return print(*argv, **kw)


SNIFF="clock_gettime"
SNIFF=""
SNIFF = ""


# we use output from wasi `wasm-objdump -x` run
Expand All @@ -20,8 +20,8 @@ def dbg(*argv, **kw):
SECTION = sys.argv[-1].lower()

if SECTION.startswith("export"):
BEGIN= "Export["
END="Start:"
BEGIN = "Export["
END = "Start:"
SECTION = "exports"
WAY = "-> "

Expand All @@ -32,126 +32,139 @@ def dbg(*argv, **kw):
print(f"_{line}")

else:
BEGIN= "Import["
END="Function["
BEGIN = "Import["
END = "Function["
SECTION = "imports"
WAY = "<- "

with open(os.environ.get("OBJDUMP", "dump.wasm-objdump"),'r') as wasmdump:
with open(os.environ.get("OBJDUMP", "dump.wasm-objdump"), "r") as wasmdump:
for line in wasmdump.readlines():
line = line.strip()

if not line:
continue
if SNIFF:
if line.find(SNIFF)>=0:
dbg(line, recording)
if line.find(SNIFF) >= 0:
dbg(line, recording)

if recording:
if line.startswith(END):
dbg("-"*40)
dbg("-" * 40)
recording = False
break

exports.append(line)
if line[0]!='-':
if line[0] != "-":
dbg(line)

else:
if line[0]=='-':
if line[0] == "-":
continue

if line.startswith(BEGIN):
dbg("\n\n#",line)
dbg("\n\n#", line)
recording = True
continue
dbg("skip",line)
dbg("skip", line)

dbg(f"found {len(exports)} {SECTION}")

if 1:
badchars = '"<> '
VERBOSE = '-v' in sys.argv
VERBOSE = "-v" in sys.argv
REPORT = []
for line in exports:
typ, header = line.split('] ',1)
if typ.startswith('- func['):
typ, header = line.split("] ", 1)
if typ.startswith("- func["):
typ = "def"
elif typ.startswith('- global['):
elif typ.startswith("- global["):
pass
typ = "var"
elif typ.startswith('- memory['):
elif typ.startswith("- memory["):
pass
typ = "mem"
elif typ.startswith('- table['):
elif typ.startswith("- table["):
pass
typ = "tbl"

if SNIFF:
if line.find(SNIFF)>=0:
dbg(line)
if line.find(SNIFF) >= 0:
dbg(
f"""
-------------------------------------------------------------------------------------
{line=}
{typ=}
-------------------------------------------------------------------------------------
"""
)

try:
if typ in ('def','var'):
if typ in ("def", "var"):
left, right = header.rsplit(WAY, 1)
left = left.strip(badchars)
right = right.strip(badchars)
# GOT.mem. GOT.func. env.
clean = False
for clean in ('.', ' <'):
if left.find(clean)>=0:
for clean in (".", " <"):
if left.find(clean) >= 0:
_, left = left.rsplit(clean, 1)
_, right = right.rsplit(".", 1)
clean = True
break
if clean:
left = left.strip(badchars)
if right.find('.')>0:
if right.find(".") > 0:
_, right = right.rsplit(".", 1)
right = right.strip(badchars)

if left.find('=')> 0:
if left.find("=") > 0:
left = right

if left.find('=')> 0:
if left.find("=") > 0:
left = ""

if not left:
left = right

if SNIFF:
if line.find(SNIFF)>=0:
if line.find(SNIFF) >= 0:
dbg(f"{left=} {right=} '{line}'")


if left.find('=')> 0:
if left.find("=") > 0:
left = ""
elif left.find('::')> 0:
elif left.find("::") > 0:
if VERBOSE:
raise Exception("bad export (c++)")
continue
elif left.find(' ')> 0:
# continue
elif left.find(" ") > 0:
if VERBOSE:
raise Exception("bad export (space)")
continue


if VERBOSE:
demangle = os.popen(f'c++filt {right}').read().strip()
if not left or demangle==left:
demangle = os.popen(f"c++filt {right}").read().strip()
if not left or demangle == left:
dbg(typ, right, "# right")
elif demangle!=right:
dbg(typ,left,"#", demangle)
elif demangle != right:
dbg(typ, left, "#", demangle)
else:
dbg(typ,"LEFT=", left, "RIGHT=", demangle)
dbg(typ, "LEFT=", left, "RIGHT=", demangle)

if (right or left) not in REPORT:
REPORT.append(right or left)
except Exception as e:
dbg("ERROR", typ, header, e)

dbg(len(REPORT),"unique")
dbg(len(REPORT), "unique")
NODUNDER = []

for rep in REPORT:
print(f"_{rep}")

if rep in NODUNDER:
print(f"{rep}")
else:
print(f"_{rep}")
11 changes: 6 additions & 5 deletions cibuild/linkexport.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# this only runs when wasm-objdump is working and OBJDUMP not set to false

echo "============= link export : begin ==============="
emcc $EMCC_WEB -fPIC -sMAIN_MODULE=1 -O0 \
-D__PYDK__=1 -DPREFIX=${PGROOT} \
-sTOTAL_MEMORY=${TOTAL_MEMORY} -sEXPORT_ALL -sSTACK_SIZE=4MB -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH -sGLOBAL_BASE=${CMA_MB}MB \
-sERROR_ON_UNDEFINED_SYMBOLS -sASSERTIONS=0 \
-lnodefs.js -lidbfs.js \

# -O0 -sEXPORT_ALL should make all symbols visible without any mangling.
COPT="-O0 -g3" \
emcc \
$EMCC_WEB -fPIC -sMAIN_MODULE=1 -sEXPORT_ALL -sERROR_ON_UNDEFINED_SYMBOLS -sASSERTIONS=0 \
-DPREFIX=${PGROOT} -lnodefs.js -lidbfs.js \
-sEXPORTED_RUNTIME_METHODS=FS,setValue,getValue,UTF8ToString,stringToNewUTF8,stringToUTF8OnStack,ccall,cwrap,callMain \
$PGPRELOAD \
-o postgres.html $PG_O $PG_L || exit 14
Expand Down
17 changes: 13 additions & 4 deletions cibuild/linkimports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ echo "============= link imports : begin ==============="

pushd ${WORKSPACE}
> patches/imports/pgcore

echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
cp -v /tmp/arrays.so /tmp/pglite/lib/postgresql/arrays.so
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

for extra_pg_so in $(find $PGROOT/lib/postgresql/|grep \.so$)
do
SOBASE=patches/imports.pgcore/$(basename $extra_pg_so .so)
Expand Down Expand Up @@ -50,6 +55,7 @@ matches = list( imports.intersection(exports) )
# ?
for sym in """
_ErrorContext
_check_function_bodies
_clock_gettime
_CurrentMemoryContext
Expand All @@ -61,9 +67,8 @@ _interactive_read
_interactive_write
_lowerstr
_main
_main_repl
_pg_getport
_pg_initdb
_pg_repl_raf
_pg_shutdown
_readstoplist
_searchstoplist
Expand All @@ -72,12 +77,16 @@ _shmem_request_hook
_shmem_startup_hook
_stderr
_TopMemoryContext
__ZNSt12length_errorD1Ev
__ZNSt20bad_array_new_lengthD1Ev
__ZNSt16invalid_argumentD1Ev
""".splitlines():
if sym and not sym in matches:
matches.append(sym)
#__ZNSt13runtime_errorD1Ev
# __ZNSt13runtime_errorD1Ev : i32 std::length_error::~length_error(i32)
# __ZNSt20bad_array_new_lengthD1Ev : std::bad_array_new_length::~bad_array_new_length()
# __ZNSt16invalid_argumentD1Ev : std::invalid_argument::~invalid_argument()
matches.sort()
for sym in matches:
Expand Down
9 changes: 6 additions & 3 deletions cibuild/linkwasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WASI_CFLAGS="-DPATCH_PG_DEBUG=/tmp/pglite/include/pg_debug.h -DPREFIX=/tmp/pglit
-Ipostgresql/src/backend \
-c -o build/postgres/wasi_dlfcn.o \
-Ibuild/postgres/src/include patches/wasi_dlfcn.c || exit 8

# -L./build/postgres/src/backend/snowball -ldict_snowball
# ./build/postgres/src/backend/snowball/dict_snowball.o
# ./build/postgres/src/backend/snowball/libdict_snowball.a
Expand Down Expand Up @@ -770,7 +771,7 @@ llvm-ar cr ../../libpglite.a $PGOBJ

# just linking


# -Wl,--no-entry -mexec-model=reactor

$CC -o postgres \
-fno-strict-aliasing \
Expand All @@ -784,11 +785,13 @@ $CC -o postgres \
../../src/pl/plpgsql/src/libplpgsql.a \
-lz -lm -lwasi-emulated-mman -lwasi-emulated-signal -lc \
-Wl,--export=pg_initdb \
-Wl,--export=setup \
-Wl,--export=loop \
-Wl,--export=interactive_one \
-Wl,--export=use_socketfile \
-Wl,--export=interactive_write \
-Wl,--export=interactive_read
-Wl,--global-base=33554432
-Wl,--export=interactive_read \
-Wl,--global-base=33333333

cp -vf postgres postgres.wasi || exit 192
#cp -vf postgres.wasi /tmp/pglite/bin/postgres.wasi
Expand Down
Loading

0 comments on commit b2400eb

Please sign in to comment.