From a42f0724e89786972efcb35d363564a97409fc65 Mon Sep 17 00:00:00 2001 From: ishabi Date: Fri, 25 Jul 2025 12:02:25 +0200 Subject: [PATCH 1/5] use stripped libddwaf --- scripts/lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lib.js b/scripts/lib.js index 624edca1..1a051eac 100644 --- a/scripts/lib.js +++ b/scripts/lib.js @@ -11,7 +11,7 @@ const platform = process.env.PLATFORM || os.platform() const arch = process.env.ARCH || os.arch() const libNames = { - darwin: 'libddwaf.a', + darwin: 'libddwaf.a.stripped', win32: 'ddwaf_static.lib', linux: 'libddwaf.so' } From 18d453211f97bc292752a54a1447db8f5136e309 Mon Sep 17 00:00:00 2001 From: ishabi Date: Thu, 18 Sep 2025 10:47:28 +0200 Subject: [PATCH 2/5] reduce size via link time stripping --- binding.gyp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/binding.gyp b/binding.gyp index c64ff5f0..e6958131 100644 --- a/binding.gyp +++ b/binding.gyp @@ -17,10 +17,13 @@ "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ], "xcode_settings": { "MACOSX_DEPLOYMENT_TARGET": "10.10", + "OTHER_LDFLAGS": ["-Wl,-S"], + "DEPLOYMENT_POSTPROCESSING": "YES", + "STRIP_INSTALLED_PRODUCT": "YES" }, "conditions": [ ["OS == 'linux'", { - 'ldflags': ['-Wl,--rpath=\$$ORIGIN'] + 'ldflags': ['-Wl,--rpath=\$$ORIGIN', '-Wl,--strip-all'] }], ["OS == 'win'", { "libraries": ["Ws2_32.lib"], From d69c505e14e73aeddef8c84cca10a2bed2efcc6e Mon Sep 17 00:00:00 2001 From: ishabi Date: Tue, 23 Sep 2025 10:02:41 +0200 Subject: [PATCH 3/5] node gyp optimization --- binding.gyp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/binding.gyp b/binding.gyp index e6958131..6ee5f191 100644 --- a/binding.gyp +++ b/binding.gyp @@ -15,21 +15,66 @@ "src/main.cpp" ], "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ], + "cflags": [ + "-ffunction-sections", + "-fdata-sections", + "-fvisibility=hidden", + "-Os", + "-flto", + "-fno-rtti" + ], + "cflags_cc": [ + "-ffunction-sections", + "-fdata-sections", + "-fvisibility=hidden", + "-Os", + "-flto", + "-fno-rtti" + ], + "ldflags": [ + "-flto", + "-Wl,--gc-sections", + "-Wl,--exclude-libs,ALL" + ], "xcode_settings": { "MACOSX_DEPLOYMENT_TARGET": "10.10", - "OTHER_LDFLAGS": ["-Wl,-S"], + "OTHER_LDFLAGS": ["-Wl,-S", "-Wl,-dead_strip"], "DEPLOYMENT_POSTPROCESSING": "YES", - "STRIP_INSTALLED_PRODUCT": "YES" + "STRIP_INSTALLED_PRODUCT": "YES", + "GCC_SYMBOLS_PRIVATE_EXTERN": "YES", + "GCC_OPTIMIZATION_LEVEL": "s", + "LLVM_LTO": "YES" }, "conditions": [ ["OS == 'linux'", { - 'ldflags': ['-Wl,--rpath=\$$ORIGIN', '-Wl,--strip-all'] + 'ldflags': ['-Wl,--rpath=\$$ORIGIN', '-Wl,--strip-all', '-Wl,--gc-sections', '-Wl,--exclude-libs,ALL'] }], ["OS == 'win'", { "libraries": ["Ws2_32.lib"], "cflags": [ - "/WX" - ] + "/WX", + "/O1", + "/GL", + "/Gy", + "/GR-" + ], + "ldflags": [ + "/LTCG", + "/OPT:REF", + "/OPT:ICF" + ], + "msvs_settings": { + "VCCLCompilerTool": { + "Optimization": 1, + "FavorSizeOrSpeed": 2, + "WholeProgramOptimization": "true" + }, + "VCLinkerTool": { + "OptimizeReferences": 2, + "EnableCOMDATFolding": 2, + "LinkTimeCodeGeneration": 1 + } + } }] ] }] From b1c645847a17a1c526c80b78d281931161b33e53 Mon Sep 17 00:00:00 2001 From: ishabi Date: Wed, 24 Sep 2025 17:00:42 +0200 Subject: [PATCH 4/5] allow dynamic linking --- binding.gyp | 22 +++++++++++++++++----- scripts/lib.js | 8 +++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/binding.gyp b/binding.gyp index 6ee5f191..9e32eb2d 100644 --- a/binding.gyp +++ b/binding.gyp @@ -7,9 +7,6 @@ " Date: Wed, 24 Sep 2025 17:21:31 +0200 Subject: [PATCH 5/5] fix win32 issues --- scripts/lib.js | 19 +++++++++++++++---- scripts/postbuild.js | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/scripts/lib.js b/scripts/lib.js index 8f62d017..f8b5808e 100644 --- a/scripts/lib.js +++ b/scripts/lib.js @@ -10,12 +10,20 @@ const pkg = require('../package.json') const platform = process.env.PLATFORM || os.platform() const arch = process.env.ARCH || os.arch() -const libNames = { +// Runtime filenames (what the addon will dlopen at runtime) +const runtimeLibNames = { darwin: 'libddwaf.dylib', win32: 'ddwaf.dll', linux: 'libddwaf.so' } +// Link-time filenames (what the linker should link against) +const linkLibNames = { + darwin: runtimeLibNames.darwin, + win32: 'ddwaf.lib', + linux: runtimeLibNames.linux +} + const dirNames = { darwin: { arm64: 'darwin-arm64', @@ -40,13 +48,16 @@ function getDirName () { } const dirname = getDirName() -const libName = libNames[platform] +const runtimeLibName = runtimeLibNames[platform] +const linkLibName = linkLibNames[platform] const basename = path.join(__dirname, '..', 'libddwaf', dirname) module.exports = { includePath: path.join(basename, 'include').split('\\').join('\\\\'), - libPath: path.join(basename, 'lib', libName).split('\\').join('\\\\'), + // Used by the linker during build + libPath: path.join(basename, 'lib', linkLibName).split('\\').join('\\\\'), libDir: path.join(basename, 'lib').split('\\').join('\\\\'), libName: platform === 'darwin' ? 'ddwaf' : (platform === 'win32' ? 'ddwaf' : 'ddwaf'), - libFile: libName + // Runtime library filename (for packaging/copying alongside the addon) + libFile: runtimeLibName } diff --git a/scripts/postbuild.js b/scripts/postbuild.js index 1b51aeb0..eeed1e57 100644 --- a/scripts/postbuild.js +++ b/scripts/postbuild.js @@ -6,14 +6,26 @@ const fs = require('fs') const path = require('path') const os = require('os') -const { libPath, libName } = require('./lib') +const { libPath, libName, libDir, libFile } = require('./lib') const platform = os.platform() const arch = process.env.ARCH || os.arch() const libc = process.env.LIBC || '' const prebuildDir = path.join(__dirname, '..', 'prebuilds', `${platform}${libc}-${arch}`) +const runtimeLibPath = path.join(libDir, libFile) -if (platform === 'linux' && fs.existsSync(prebuildDir)) { - fs.copyFileSync(libPath, path.join(prebuildDir, libName)) +if (fs.existsSync(prebuildDir)) { + // Copy the runtime library next to the addon so rpath/@loader_path or loader can find it + // - linux: libddwaf.so + // - darwin: libddwaf.dylib + // - win32: ddwaf.dll + if (fs.existsSync(runtimeLibPath)) { + fs.copyFileSync(runtimeLibPath, path.join(prebuildDir, libFile)) + } + + // Ensure link-time library is also available on linux (same as runtime) for completeness + if (platform === 'linux' && fs.existsSync(libPath)) { + fs.copyFileSync(libPath, path.join(prebuildDir, libName)) + } }