diff options
| author | ruki <[email protected]> | 2026-02-12 10:09:12 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-12 10:09:12 +0800 |
| commit | 9d084db557d256923fc7f8a5daf66b21709f4bc4 (patch) | |
| tree | 80be799cf286994b09bc0bc00bb6cdf766029ec6 /tests/modules | |
| parent | 2f944ae3380afc0129baff3f944ce31ed1faf72e (diff) | |
| parent | 37b506c86c3390d6a175ce37828e8defd44a2407 (diff) | |
Merge pull request #7312 from xmake-io/binutils
Improve binutils to support wasm
Diffstat (limited to 'tests/modules')
| -rw-r--r-- | tests/modules/binutils/test.lua | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/modules/binutils/test.lua b/tests/modules/binutils/test.lua index bbb466144..ce68bcda8 100644 --- a/tests/modules/binutils/test.lua +++ b/tests/modules/binutils/test.lua @@ -44,6 +44,10 @@ function test_format(t) io.writefile(ar, "!<arch>\n") t:are_equal(binutils.format(ar), "ar") + local wasmso = path.join(tempdir, "libfoo.so") + io.writefile(wasmso, string.char(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)) + t:are_equal(binutils.format(wasmso), "wasm") + local elf = path.join(tempdir, "a.elf") io.writefile(elf, string.char(0x7f, string.byte("E"), string.byte("L"), string.byte("F"), 0, 0, 0, 0)) t:are_equal(binutils.format(elf), "elf") @@ -76,3 +80,104 @@ function test_format(t) os.tryrm(tempdir) end + +function test_deplibs(t) + local tempdir = "temp/binutils_deplibs" + os.tryrm(tempdir) + os.mkdir(tempdir) + + local wasmso = path.join(tempdir, "libfoo.so") + io.writefile(wasmso, string.char(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)) + local libs = binutils.deplibs(wasmso) + t:are_equal(#libs, 0) + + os.tryrm(tempdir) +end + +function test_readsyms(t) + local tempdir = "temp/binutils_readsyms" + os.tryrm(tempdir) + os.mkdir(tempdir) + + local function _writebin(filepath, data) + io.writefile(filepath, data, {encoding = "binary"}) + end + + local wasmso = path.join(tempdir, "libfoo.so") + _writebin(wasmso, string.char( + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x0b, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x62, 0x61, 0x72, 0x00, 0x00, + 0x07, 0x07, 0x01, 0x03, 0x66, 0x6f, 0x6f, 0x00, 0x00)) + local results = binutils.readsyms(wasmso) + t:are_equal(#results, 1) + t:are_equal(results[1].objectfile, "libfoo.so") + t:are_equal(#results[1].symbols, 2) + t:are_equal(results[1].symbols[1].name, "bar") + t:are_equal(results[1].symbols[1].type, "U") + t:are_equal(results[1].symbols[2].name, "foo") + t:are_equal(results[1].symbols[2].type, "T") + + local wasmso64 = path.join(tempdir, "libfoo64.so") + _writebin(wasmso64, string.char( + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x0c, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x6d, 0x65, 0x6d, 0x02, 0x04, 0x01, + 0x07, 0x07, 0x01, 0x03, 0x66, 0x6f, 0x6f, 0x00, 0x00)) + local results64 = binutils.readsyms(wasmso64) + t:are_equal(#results64, 1) + t:are_equal(results64[1].objectfile, "libfoo64.so") + t:are_equal(#results64[1].symbols, 2) + t:are_equal(results64[1].symbols[1].name, "mem") + t:are_equal(results64[1].symbols[1].type, "U") + t:are_equal(results64[1].symbols[2].name, "foo") + t:are_equal(results64[1].symbols[2].type, "T") + + local wasmlink = path.join(tempdir, "liblink.so") + _writebin(wasmlink, string.char( + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, + 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, + 0x03, 0x02, 0x01, 0x00, + 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, + 0x00, 0x13, 0x07, 0x6c, 0x69, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x02, 0x08, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x61, 0x64, 0x64)) + local resultslink = binutils.readsyms(wasmlink) + t:are_equal(#resultslink, 1) + t:are_equal(resultslink[1].objectfile, "liblink.so") + t:are_equal(#resultslink[1].symbols, 1) + t:are_equal(resultslink[1].symbols[1].name, "add") + t:are_equal(resultslink[1].symbols[1].type, "T") + + local wasmar = path.join(tempdir, "libbar.a") + local function _pad(str, n) + if #str < n then + return str .. string.rep(" ", n - #str) + end + return str:sub(1, n) + end + local function _ar_header(name, size) + return _pad(name, 16) .. + _pad("0", 12) .. + _pad("0", 6) .. + _pad("0", 6) .. + _pad("644", 8) .. + _pad(tostring(size), 10) .. + "`\n" + end + local wasmobj = string.char( + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, + 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, + 0x03, 0x02, 0x01, 0x00, + 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b, + 0x00, 0x13, 0x07, 0x6c, 0x69, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x02, 0x08, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x61, 0x64, 0x64) + local symtab = string.char(0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50) .. "add\0" + local ardata = "!<arch>\n" .. + _ar_header("/", #symtab) .. symtab .. ((#symtab % 2 == 1) and "\n" or "") .. + _ar_header("foo.cpp.o/", #wasmobj) .. wasmobj .. ((#wasmobj % 2 == 1) and "\n" or "") + _writebin(wasmar, ardata) + local resultsar = binutils.readsyms(wasmar) + t:are_equal(#resultsar, 1) + t:are_equal(resultsar[1].objectfile, "foo.cpp.o") + t:are_equal(#resultsar[1].symbols, 1) + t:are_equal(resultsar[1].symbols[1].name, "add") + t:are_equal(resultsar[1].symbols[1].type, "T") + + os.tryrm(tempdir) +end |
