Skip to content

Commit 1af7054

Browse files
lux01mhdawson
authored andcommittedMay 6, 2022
build: fix various shared library build issues
Node.js unofficially supports a shared library variant where the main node executable is a thin wrapper around node.dll/libnode.so. The key benefit of this is to support embedding Node.js in other applications. Since Node.js 12 there have been a number of issues preventing the shared library build from working correctly, primarily on Windows: * A number of functions used executables such as `mksnapshot` are not exported from `libnode.dll` using a `NODE_EXTERN` attribute * A dependency on the `Winmm` system library is missing * Incorrect defines on executable targets leads to `node.exe` claiming to export a number of functions that are actually in `libnode.dll` * Because `node.exe` attempts to export symbols, `node.lib` gets generated causing native extensions to try to link against `node.exe` not `libnode.dll`. * Similarly, because `node.dll` was renamed to `libnode.dll`, native extensions don't know to look for `libnode.lib` rather than `node.lib`. * On macOS an RPATH is added to find `libnode.dylib` relative to `node` in the same folder. This works fine from the `out/Release` folder but not from an installed prefix, where `node` will be in `bin/` and `libnode.dylib` will be in `lib/`. * Similarly on Linux, no RPATH is added so LD_LIBRARY_PATH needs setting correctly for `bin/node` to find `lib/libnode.so`. For the `libnode.lib` vs `node.lib` issue there are two possible options: 1. Ensure `node.lib` from `node.exe` does not get generated, and instead copy `libnode.lib` to `node.lib`. This means addons compiled when referencing the correct `node.lib` file will correctly depend on `libnode.dll`. The down side is that native addons compiled with stock Node.js will still try to resolve symbols against node.exe rather than libnode.dll. 2. After building `libnode.dll`, dump the exports using `dumpbin`, and process this to generate a `node.def` file to be linked into `node.exe` with the `/DEF:node.def` flag. The export entries in `node.def` will all read ``` my_symbol=libnode.my_symbol ``` so that `node.exe` will redirect all exported symbols back to `libnode.dll`. This has the benefit that addons compiled with stock Node.js will load correctly into `node.exe` from a shared library build, but means that every embedding executable also needs to perform this same trick. I went with the first option as it is the cleaner of the two solutions in my opinion. Projects wishing to generate a shared library variant of Node.js can now, for example, ``` .\vcbuild dll package vs ``` to generate a full node installation including `libnode.dll`, `Release\node.lib`, and all the necessary headers. Native addons can then be built against the shared library build easily by specifying the correct `nodedir` option. For example ``` >npx node-gyp configure --nodedir C:\Users\User\node\Release\node-v18.0.0-win-x64 ... >npx node-gyp build ... >dumpbin /dependents build\Release\binding.node Microsoft (R) COFF/PE Dumper Version 14.29.30136.0 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file build\Release\binding.node File Type: DLL Image has the following dependencies: KERNEL32.dll libnode.dll VCRUNTIME140.dll api-ms-win-crt-string-l1-1-0.dll api-ms-win-crt-stdio-l1-1-0.dll api-ms-win-crt-runtime-l1-1-0.dll ... ``` PR-URL: nodejs#41850 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 1834478 commit 1af7054

13 files changed

+316
-30
lines changed
 

‎node.gyp

+54-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@
196196
'dependencies': [ 'node_aix_shared' ],
197197
}, {
198198
'dependencies': [ '<(node_lib_target_name)' ],
199+
'conditions': [
200+
['OS=="win" and node_shared=="true"', {
201+
'dependencies': ['generate_node_def'],
202+
'msvs_settings': {
203+
'VCLinkerTool': {
204+
'ModuleDefinitionFile': '<(PRODUCT_DIR)/<(node_core_target_name).def',
205+
},
206+
},
207+
}],
208+
],
199209
}],
200210
[ 'node_intermediate_lib_type=="static_library" and node_shared=="false"', {
201211
'xcode_settings': {
@@ -235,8 +245,15 @@
235245
}],
236246
[ 'node_shared=="true"', {
237247
'xcode_settings': {
238-
'OTHER_LDFLAGS': [ '-Wl,-rpath,@loader_path', ],
248+
'OTHER_LDFLAGS': [ '-Wl,-rpath,@loader_path', '-Wl,-rpath,@loader_path/../lib'],
239249
},
250+
'conditions': [
251+
['OS=="linux"', {
252+
'ldflags': [
253+
'-Wl,-rpath,\\$$ORIGIN/../lib'
254+
],
255+
}],
256+
],
240257
}],
241258
[ 'enable_lto=="true"', {
242259
'xcode_settings': {
@@ -751,6 +768,7 @@
751768
'libraries': [
752769
'Dbghelp',
753770
'Psapi',
771+
'Winmm',
754772
'Ws2_32',
755773
],
756774
}],
@@ -1504,5 +1522,40 @@
15041522
},
15051523
]
15061524
}], # end aix section
1525+
['OS=="win" and node_shared=="true"', {
1526+
'targets': [
1527+
{
1528+
'target_name': 'gen_node_def',
1529+
'type': 'executable',
1530+
'sources': [
1531+
'tools/gen_node_def.cc'
1532+
],
1533+
},
1534+
{
1535+
'target_name': 'generate_node_def',
1536+
'dependencies': [
1537+
'gen_node_def',
1538+
'<(node_lib_target_name)',
1539+
],
1540+
'type': 'none',
1541+
'actions': [
1542+
{
1543+
'action_name': 'generate_node_def_action',
1544+
'inputs': [
1545+
'<(PRODUCT_DIR)/<(node_lib_target_name).dll'
1546+
],
1547+
'outputs': [
1548+
'<(PRODUCT_DIR)/<(node_core_target_name).def',
1549+
],
1550+
'action': [
1551+
'<(PRODUCT_DIR)/gen_node_def.exe',
1552+
'<@(_inputs)',
1553+
'<@(_outputs)',
1554+
],
1555+
},
1556+
],
1557+
},
1558+
],
1559+
}], # end win section
15071560
], # end conditions block
15081561
}

‎node.gypi

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
[ 'clang==1', {
3030
'cflags': [ '-Werror=undefined-inline', ]
3131
}],
32-
[ 'node_shared=="false" and "<(_type)"=="executable"', {
32+
[ '"<(_type)"=="executable"', {
3333
'msvs_settings': {
3434
'VCManifestTool': {
3535
'EmbedManifest': 'true',
@@ -41,6 +41,19 @@
4141
'defines': [
4242
'NODE_SHARED_MODE',
4343
],
44+
'conditions': [
45+
['"<(_type)"=="executable"', {
46+
'defines': [
47+
'USING_UV_SHARED',
48+
'USING_V8_SHARED',
49+
'BUILDING_NODE_EXTENSION'
50+
],
51+
'defines!': [
52+
'BUILDING_V8_SHARED=1',
53+
'BUILDING_UV_SHARED=1'
54+
]
55+
}],
56+
],
4457
}],
4558
[ 'OS=="win"', {
4659
'defines!': [

‎src/debug_utils.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ template <typename... Args>
3535
inline std::string SPrintF(const char* format, Args&&... args);
3636
template <typename... Args>
3737
inline void FPrintF(FILE* file, const char* format, Args&&... args);
38-
void FWrite(FILE* file, const std::string& str);
38+
void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
3939

4040
// Listing the AsyncWrap provider types first enables us to cast directly
4141
// from a provider type to a debug category.
@@ -57,7 +57,7 @@ enum class DebugCategory {
5757
CATEGORY_COUNT
5858
};
5959

60-
class EnabledDebugList {
60+
class NODE_EXTERN_PRIVATE EnabledDebugList {
6161
public:
6262
bool enabled(DebugCategory category) const {
6363
DCHECK_GE(static_cast<int>(category), 0);
@@ -168,7 +168,7 @@ void CheckedUvLoopClose(uv_loop_t* loop);
168168
void PrintLibuvHandleInformation(uv_loop_t* loop, FILE* stream);
169169

170170
namespace per_process {
171-
extern EnabledDebugList enabled_debug_list;
171+
extern NODE_EXTERN_PRIVATE EnabledDebugList enabled_debug_list;
172172

173173
template <typename... Args>
174174
inline void FORCE_INLINE Debug(DebugCategory cat,

‎src/env.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ class Environment;
558558
struct AllocatedBuffer;
559559

560560
typedef size_t SnapshotIndex;
561-
class IsolateData : public MemoryRetainer {
561+
class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
562562
public:
563563
IsolateData(v8::Isolate* isolate,
564564
uv_loop_t* event_loop,

‎src/node.h

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
# define NODE_EXTERN __attribute__((visibility("default")))
3333
#endif
3434

35+
// Declarations annotated with NODE_EXTERN_PRIVATE do not form part of
36+
// the public API. They are implementation details that can and will
37+
// change between releases, even in semver patch releases. Do not use
38+
// any such symbol in external code.
39+
#ifdef NODE_SHARED_MODE
40+
#define NODE_EXTERN_PRIVATE NODE_EXTERN
41+
#else
42+
#define NODE_EXTERN_PRIVATE
43+
#endif
44+
3545
#ifdef BUILDING_NODE_EXTENSION
3646
# undef BUILDING_V8_SHARED
3747
# undef BUILDING_UV_SHARED

‎src/node_internals.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ enum InitializationSettingsFlags : uint64_t {
320320
};
321321

322322
// TODO(codebytere): eventually document and expose to embedders.
323-
InitializationResult InitializeOncePerProcess(int argc, char** argv);
324-
InitializationResult InitializeOncePerProcess(
325-
int argc,
326-
char** argv,
327-
InitializationSettingsFlags flags,
328-
ProcessFlags::Flags process_flags = ProcessFlags::kNoFlags);
329-
void TearDownOncePerProcess();
323+
InitializationResult NODE_EXTERN_PRIVATE InitializeOncePerProcess(int argc,
324+
char** argv);
325+
InitializationResult NODE_EXTERN_PRIVATE InitializeOncePerProcess(
326+
int argc,
327+
char** argv,
328+
InitializationSettingsFlags flags,
329+
ProcessFlags::Flags process_flags = ProcessFlags::kNoFlags);
330+
void NODE_EXTERN_PRIVATE TearDownOncePerProcess();
330331
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
331332
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
332333
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);

‎src/node_native_module.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using NativeModuleCacheMap =
2929
// This class should not depend on any Environment, or depend on access to
3030
// the its own singleton - that should be encapsulated in NativeModuleEnv
3131
// instead.
32-
class NativeModuleLoader {
32+
class NODE_EXTERN_PRIVATE NativeModuleLoader {
3333
public:
3434
NativeModuleLoader(const NativeModuleLoader&) = delete;
3535
NativeModuleLoader& operator=(const NativeModuleLoader&) = delete;

‎src/node_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ void Parse(
492492
namespace per_process {
493493

494494
extern Mutex cli_options_mutex;
495-
extern std::shared_ptr<PerProcessOptions> cli_options;
495+
extern NODE_EXTERN_PRIVATE std::shared_ptr<PerProcessOptions> cli_options;
496496

497497
} // namespace per_process
498498

‎src/node_snapshot_builder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace node {
1313
class ExternalReferenceRegistry;
1414
struct SnapshotData;
1515

16-
class SnapshotBuilder {
16+
class NODE_EXTERN_PRIVATE SnapshotBuilder {
1717
public:
1818
static std::string Generate(const std::vector<std::string> args,
1919
const std::vector<std::string> exec_args);

‎src/util.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "v8.h"
2828

29+
#include "node.h"
30+
2931
#include <climits>
3032
#include <cstddef>
3133
#include <cstdio>
@@ -110,8 +112,8 @@ struct AssertionInfo {
110112
const char* message;
111113
const char* function;
112114
};
113-
[[noreturn]] void Assert(const AssertionInfo& info);
114-
[[noreturn]] void Abort();
115+
[[noreturn]] void NODE_EXTERN_PRIVATE Assert(const AssertionInfo& info);
116+
[[noreturn]] void NODE_EXTERN_PRIVATE Abort();
115117
void DumpBacktrace(FILE* fp);
116118

117119
// Windows 8+ does not like abort() in Release mode

‎tools/gen_node_def.cc

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#include <Windows.h>
2+
#include <algorithm>
3+
#include <cstdint>
4+
#include <fstream>
5+
#include <iostream>
6+
#include <memory>
7+
#include <vector>
8+
9+
// This executable takes a Windows DLL and uses it to generate
10+
// a module-definition file [1] which forwards all the exported
11+
// symbols from the DLL and redirects them back to the DLL.
12+
// This allows node.exe to export the same symbols as libnode.dll
13+
// when building Node.js as a shared library. This is conceptually
14+
// similary to the create_expfile.sh script used on AIX.
15+
//
16+
// Generating this .def file requires parsing data out of the
17+
// PE32/PE32+ file format. Helper structs are defined in <Windows.h>
18+
// hence why this is an executable and not a script. See [2] for
19+
// details on the PE format.
20+
//
21+
// [1]: https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files
22+
// [2]: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
23+
24+
// The PE32 format encodes pointers as Relative Virtual Addresses
25+
// which are 32 bit offsets from the start of the image. This helper
26+
// class hides the mess of the pointer arithmetic
27+
struct RelativeAddress {
28+
uintptr_t root;
29+
uintptr_t offset = 0;
30+
31+
RelativeAddress(HMODULE handle) noexcept
32+
: root(reinterpret_cast<uintptr_t>(handle)) {}
33+
34+
RelativeAddress(HMODULE handle, uintptr_t offset) noexcept
35+
: root(reinterpret_cast<uintptr_t>(handle)), offset(offset) {}
36+
37+
RelativeAddress(uintptr_t root, uintptr_t offset) noexcept
38+
: root(root), offset(offset) {}
39+
40+
template <typename T>
41+
const T* AsPtrTo() const noexcept {
42+
return reinterpret_cast<const T*>(root + offset);
43+
}
44+
45+
template <typename T>
46+
T Read() const noexcept {
47+
return *AsPtrTo<T>();
48+
}
49+
50+
RelativeAddress AtOffset(uintptr_t amount) const noexcept {
51+
return {root, offset + amount};
52+
}
53+
54+
RelativeAddress operator+(uintptr_t amount) const noexcept {
55+
return {root, offset + amount};
56+
}
57+
58+
RelativeAddress ReadRelativeAddress() const noexcept {
59+
return {root, Read<uint32_t>()};
60+
}
61+
};
62+
63+
// A wrapper around a dynamically loaded Windows DLL. This steps through the
64+
// PE file structure to find the export directory and pulls out a list of
65+
// all the exported symbol names.
66+
struct Library {
67+
HMODULE library;
68+
std::string libraryName;
69+
std::vector<std::string> exportedSymbols;
70+
71+
Library(HMODULE library) : library(library) {
72+
auto libnode = RelativeAddress(library);
73+
74+
// At relative offset 0x3C is a 32 bit offset to the COFF signature, 4 bytes
75+
// after that is the start of the COFF header.
76+
auto coffHeaderPtr =
77+
libnode.AtOffset(0x3C).ReadRelativeAddress().AtOffset(4);
78+
auto coffHeader = coffHeaderPtr.AsPtrTo<IMAGE_FILE_HEADER>();
79+
80+
// After the coff header is the Optional Header (which is not optional). We
81+
// don't know what type of optional header we have without examining the
82+
// magic number
83+
auto optionalHeaderPtr = coffHeaderPtr.AtOffset(sizeof(IMAGE_FILE_HEADER));
84+
auto optionalHeader = optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER>();
85+
86+
auto exportDirectory =
87+
(optionalHeader->Magic == 0x20b) ? optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER64>()
88+
->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]
89+
: optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER32>()
90+
->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
91+
92+
auto exportTable = libnode.AtOffset(exportDirectory.VirtualAddress)
93+
.AsPtrTo<IMAGE_EXPORT_DIRECTORY>();
94+
95+
// This is the name of the library without the suffix, this is more robust
96+
// than parsing the filename as this is what the linker uses.
97+
libraryName = libnode.AtOffset(exportTable->Name).AsPtrTo<char>();
98+
libraryName = libraryName.substr(0, libraryName.size() - 4);
99+
100+
const uint32_t* functionNameTable =
101+
libnode.AtOffset(exportTable->AddressOfNames).AsPtrTo<uint32_t>();
102+
103+
// Given an RVA, parse it as a std::string. The resulting string is empty
104+
// if the symbol does not have a name (i.e. it is ordinal only).
105+
auto nameRvaToName = [&](uint32_t rva) -> std::string {
106+
auto namePtr = libnode.AtOffset(rva).AsPtrTo<char>();
107+
if (namePtr == nullptr) return {};
108+
return {namePtr};
109+
};
110+
std::transform(functionNameTable,
111+
functionNameTable + exportTable->NumberOfNames,
112+
std::back_inserter(exportedSymbols),
113+
nameRvaToName);
114+
}
115+
116+
~Library() { FreeLibrary(library); }
117+
};
118+
119+
bool IsPageExecutable(void* address) {
120+
MEMORY_BASIC_INFORMATION memoryInformation;
121+
size_t rc = VirtualQuery(
122+
address, &memoryInformation, sizeof(MEMORY_BASIC_INFORMATION));
123+
124+
if (rc != 0 && memoryInformation.Protect != 0) {
125+
return memoryInformation.Protect == PAGE_EXECUTE ||
126+
memoryInformation.Protect == PAGE_EXECUTE_READ ||
127+
memoryInformation.Protect == PAGE_EXECUTE_READWRITE ||
128+
memoryInformation.Protect == PAGE_EXECUTE_WRITECOPY;
129+
}
130+
return false;
131+
}
132+
133+
Library LoadLibraryOrExit(const char* dllPath) {
134+
auto library = LoadLibrary(dllPath);
135+
if (library != nullptr) return library;
136+
137+
auto error = GetLastError();
138+
std::cerr << "ERROR: Failed to load " << dllPath << std::endl;
139+
LPCSTR buffer = nullptr;
140+
auto rc = FormatMessageA(
141+
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
142+
nullptr,
143+
error,
144+
LANG_USER_DEFAULT,
145+
(LPSTR)&buffer,
146+
0,
147+
nullptr);
148+
if (rc != 0) {
149+
std::cerr << buffer << std::endl;
150+
LocalFree((HLOCAL)buffer);
151+
}
152+
exit(1);
153+
}
154+
155+
int main(int argc, char** argv) {
156+
if (argc != 3) {
157+
std::cerr << "Usage: " << argv[0]
158+
<< " path\\to\\libnode.dll path\\to\\node.def" << std::endl;
159+
return 1;
160+
}
161+
162+
auto libnode = LoadLibraryOrExit(argv[1]);
163+
auto defFile = std::ofstream(argv[2]);
164+
defFile << "EXPORTS" << std::endl;
165+
166+
for (const std::string& functionName : libnode.exportedSymbols) {
167+
// If a symbol doesn't have a name then it has been exported as an
168+
// ordinal only. We assume that only named symbols are exported.
169+
if (functionName.empty()) continue;
170+
171+
// Every name in the exported symbols table should be resolvable
172+
// to an address because we have actually loaded the library into
173+
// our address space.
174+
auto address = GetProcAddress(libnode.library, functionName.c_str());
175+
if (address == nullptr) {
176+
std::cerr << "WARNING: " << functionName
177+
<< " appears in export table but is not a valid symbol"
178+
<< std::endl;
179+
continue;
180+
}
181+
182+
defFile << " " << functionName << " = " << libnode.libraryName << "."
183+
<< functionName;
184+
185+
// Nothing distinguishes exported global data from exported functions
186+
// with C linkage. If we do not specify the DATA keyword for such symbols
187+
// then consumers of the .def file will get a linker error. This manifests
188+
// as nodedbg_ symbols not being found. We assert that if the symbol is in
189+
// an executable page in this process then it is a function, not data.
190+
if (!IsPageExecutable(address)) {
191+
defFile << " DATA";
192+
}
193+
defFile << std::endl;
194+
}
195+
196+
return 0;
197+
}

‎tools/install.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,17 @@ def files(action):
133133
output_file = 'node'
134134
output_prefix = 'out/Release/'
135135

136-
if 'false' == variables.get('node_shared'):
137-
if is_windows:
138-
output_file += '.exe'
139-
else:
136+
if is_windows:
137+
output_file += '.exe'
138+
action([output_prefix + output_file], 'bin/' + output_file)
139+
140+
if 'true' == variables.get('node_shared'):
140141
if is_windows:
141-
output_file += '.dll'
142+
action([output_prefix + 'libnode.dll'], 'bin/libnode.dll')
143+
action([output_prefix + 'libnode.lib'], 'lib/libnode.lib')
142144
else:
143-
output_file = 'lib' + output_file + '.' + variables.get('shlib_suffix')
144-
145-
if 'false' == variables.get('node_shared'):
146-
action([output_prefix + output_file], 'bin/' + output_file)
147-
else:
148-
action([output_prefix + output_file], 'lib/' + output_file)
149-
145+
output_lib = 'libnode.' + variables.get('shlib_suffix')
146+
action([output_prefix + output_lib], 'lib/' + output_lib)
150147
if 'true' == variables.get('node_use_dtrace'):
151148
action(['out/Release/node.d'], 'lib/dtrace/node.d')
152149

‎vcbuild.bat

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ if not defined noetw (
468468
copy /Y ..\src\res\node_etw_provider.man %TARGET_NAME%\ > nul
469469
if errorlevel 1 echo Cannot copy node_etw_provider.man && goto package_error
470470
)
471+
if defined dll (
472+
copy /Y libnode.dll %TARGET_NAME%\ > nul
473+
if errorlevel 1 echo Cannot copy libnode.dll && goto package_error
474+
475+
mkdir %TARGET_NAME%\Release > nul
476+
copy /Y node.def %TARGET_NAME%\Release\ > nul
477+
if errorlevel 1 echo Cannot copy node.def && goto package_error
478+
479+
set HEADERS_ONLY=1
480+
python ..\tools\install.py install %CD%\%TARGET_NAME% \ > nul
481+
if errorlevel 1 echo Cannot install headers && goto package_error
482+
set HEADERS_ONLY=
483+
)
471484
cd ..
472485

473486
:package

0 commit comments

Comments
 (0)
Please sign in to comment.