Skip to content

Commit 5c82901

Browse files
committed
win: allow node.exe/iojs.exe to be renamed
On Windows, when node or io.js attempts to dynamically load a compiled addon, the compiled addon tries to load node.exe or iojs.exe again - depending on which import library the module used when it was linked. This makes it impossible to rename node.exe or iojs.exe, because when that happens the module can't find its dependencies. With this patch, a delay-load hook is added to all modules that are compiled with node-gyp. The delay-load hook ensures that whenever a module tries to load imports from node.exe/iojs.exe, it'll just refer back to the process image, thus making it possible to rename the iojs/node binary. Bug: nodejs/node#751 Bug: nodejs/node#965 Downstream PR: nodejs/node#1251
1 parent abad2b5 commit 5c82901

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

addon.gypi

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
},
3030
}],
3131
[ 'OS=="win"', {
32+
'sources': [
33+
'src/win_delay_load_hook.c',
34+
],
3235
'libraries': [
3336
'-lkernel32.lib',
3437
'-luser32.lib',
@@ -47,6 +50,15 @@
4750
# warning C4251: 'node::ObjectWrap::handle_' : class 'v8::Persistent<T>'
4851
# needs to have dll-interface to be used by clients of class 'node::ObjectWrap'
4952
'msvs_disabled_warnings': [ 4251 ],
53+
# Set up delay-loading for node.exe/iojs.exe so the loadable module
54+
# will still be able to find it's imports if the binary is renamed.
55+
'msvs_settings': {
56+
'VCLinkerTool': {
57+
'DelayLoadDLLs': [ 'iojs.exe', 'node.exe' ],
58+
# Don't print a linker warning when no imports from either .exe are used.
59+
'AdditionalOptions': [ '/ignore:4199' ],
60+
}
61+
},
5062
}, {
5163
# OS!="win"
5264
'defines': [ '_LARGEFILE_SOURCE', '_FILE_OFFSET_BITS=64' ],

src/win_delay_load_hook.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* When this file is linked to a DLL, it sets up a delay-load hook that
3+
* intervenes when the DLL is trying to load 'node.exe' or 'iojs.exe'
4+
* dynamically. Instead of trying to locate the .exe file it'll just return
5+
* a handle to the process image.
6+
*
7+
* This allows compiled addons to work when node.exe or iojs.exe is renamed.
8+
*/
9+
10+
#ifdef _MSC_VER
11+
12+
#define WIN32_LEAN_AND_MEAN
13+
#include <windows.h>
14+
15+
#include <delayimp.h>
16+
#include <string.h>
17+
18+
static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
19+
if (event != dliNotePreLoadLibrary)
20+
return NULL;
21+
22+
if (_stricmp(info->szDll, "iojs.exe") != 0 &&
23+
_stricmp(info->szDll, "node.exe") != 0)
24+
return NULL;
25+
26+
HMODULE m = GetModuleHandle(NULL);
27+
return (FARPROC) m;
28+
}
29+
30+
PfnDliHook __pfnDliNotifyHook2 = load_exe_hook;
31+
32+
#endif

0 commit comments

Comments
 (0)