Skip to content

Commit b4d4fd9

Browse files
committed
build: export openssl symbols on windows
Export symbols from the bundled openssl for add-ons to link against. Fixes: nodejs/node-v0.x-archive#4051 PR-URL: #6274 Reviewed-By: James M Snell <[email protected]>
1 parent d80432d commit b4d4fd9

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

node.gyp

+56
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@
109109
}, {
110110
'node_target_type%': 'executable',
111111
}],
112+
[ 'OS=="win" and '
113+
'node_use_openssl=="true" and '
114+
'node_shared_openssl=="false"', {
115+
'use_openssl_def': 1,
116+
}, {
117+
'use_openssl_def': 0,
118+
}],
112119
],
113120
},
114121

@@ -354,6 +361,9 @@
354361
'-Wl,--no-whole-archive',
355362
],
356363
}],
364+
['use_openssl_def==1', {
365+
'sources': ['<(SHARED_INTERMEDIATE_DIR)/openssl.def'],
366+
}],
357367
],
358368
}],
359369
],
@@ -530,6 +540,52 @@
530540
}],
531541
],
532542
},
543+
{
544+
'target_name': 'mkssldef',
545+
'type': 'none',
546+
# TODO(bnoordhuis) Make all platforms export the same list of symbols.
547+
# Teach mkssldef.py to generate linker maps that UNIX linkers understand.
548+
'conditions': [
549+
[ 'use_openssl_def==1', {
550+
'variables': {
551+
'mkssldef_flags': [
552+
# Categories to export.
553+
'-CAES,BF,BIO,DES,DH,DSA,EC,ECDH,ECDSA,ENGINE,EVP,HMAC,MD4,MD5,'
554+
'NEXTPROTONEG,PSK,RC2,RC4,RSA,SHA,SHA0,SHA1,SHA256,SHA512,TLSEXT',
555+
# Defines.
556+
'-DWIN32',
557+
# Symbols to filter from the export list.
558+
'-X^DSO',
559+
'-X^_',
560+
'-X^private_',
561+
],
562+
},
563+
'conditions': [
564+
['openssl_fips!=""', {
565+
'variables': { 'mkssldef_flags': ['-DOPENSSL_FIPS'] },
566+
}],
567+
],
568+
'actions': [
569+
{
570+
'action_name': 'mkssldef',
571+
'inputs': [
572+
'deps/openssl/openssl/util/libeay.num',
573+
'deps/openssl/openssl/util/ssleay.num',
574+
],
575+
'outputs': ['<(SHARED_INTERMEDIATE_DIR)/openssl.def'],
576+
'action': [
577+
'python',
578+
'tools/mkssldef.py',
579+
'<@(mkssldef_flags)',
580+
'-o',
581+
'<@(_outputs)',
582+
'<@(_inputs)',
583+
],
584+
},
585+
],
586+
}],
587+
],
588+
},
533589
# generate ETW header and resource files
534590
{
535591
'target_name': 'node_etw',
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "node.h"
2+
#include "../../../src/util.h"
3+
#include "../../../src/util-inl.h"
4+
5+
#include <assert.h>
6+
#include <openssl/rand.h>
7+
8+
namespace {
9+
10+
inline void RandomBytes(const v8::FunctionCallbackInfo<v8::Value>& info) {
11+
assert(info[0]->IsArrayBufferView());
12+
auto view = info[0].As<v8::ArrayBufferView>();
13+
auto byte_offset = view->ByteOffset();
14+
auto byte_length = view->ByteLength();
15+
assert(view->HasBuffer());
16+
auto buffer = view->Buffer();
17+
auto contents = buffer->GetContents();
18+
auto data = static_cast<unsigned char*>(contents.Data()) + byte_offset;
19+
assert(RAND_poll());
20+
auto rval = RAND_bytes(data, static_cast<int>(byte_length));
21+
info.GetReturnValue().Set(rval > 0);
22+
}
23+
24+
inline void Initialize(v8::Local<v8::Object> exports,
25+
v8::Local<v8::Value> module,
26+
v8::Local<v8::Context> context) {
27+
auto isolate = context->GetIsolate();
28+
auto key = v8::String::NewFromUtf8(isolate, "randomBytes");
29+
auto value = v8::FunctionTemplate::New(isolate, RandomBytes)->GetFunction();
30+
assert(exports->Set(context, key, value).IsJust());
31+
}
32+
33+
} // anonymous namespace
34+
35+
NODE_MODULE_CONTEXT_AWARE(binding, Initialize)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': ['binding.cc'],
6+
'include_dirs': ['../../../deps/openssl/openssl/include'],
7+
},
8+
]
9+
}

test/addons/openssl-binding/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
require('../../common');
4+
const assert = require('assert');
5+
const binding = require('./build/Release/binding');
6+
const bytes = new Uint8Array(1024);
7+
assert(binding.randomBytes(bytes));
8+
assert(bytes.reduce((v, a) => v + a) > 0);

tools/mkssldef.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import print_function
4+
import re
5+
import sys
6+
7+
categories = []
8+
defines = []
9+
excludes = []
10+
11+
if __name__ == '__main__':
12+
out = sys.stdout
13+
filenames = sys.argv[1:]
14+
15+
while filenames and filenames[0].startswith('-'):
16+
option = filenames.pop(0)
17+
if option == '-o': out = open(filenames.pop(0), 'w')
18+
elif option.startswith('-C'): categories += option[2:].split(',')
19+
elif option.startswith('-D'): defines += option[2:].split(',')
20+
elif option.startswith('-X'): excludes += option[2:].split(',')
21+
22+
excludes = map(re.compile, excludes)
23+
exported = []
24+
25+
for filename in filenames:
26+
for line in open(filename).readlines():
27+
name, _, meta, _ = re.split('\s+', line)
28+
if any(map(lambda p: p.match(name), excludes)): continue
29+
meta = meta.split(':')
30+
assert meta[0] in ('EXIST', 'NOEXIST')
31+
assert meta[2] in ('FUNCTION', 'VARIABLE')
32+
if meta[0] != 'EXIST': continue
33+
if meta[2] != 'FUNCTION': continue
34+
def satisfy(expr, rules):
35+
def test(expr):
36+
if expr.startswith('!'): return not expr[1:] in rules
37+
return expr == '' or expr in rules
38+
return all(map(test, expr.split(',')))
39+
if not satisfy(meta[1], defines): continue
40+
if not satisfy(meta[3], categories): continue
41+
exported.append(name)
42+
43+
print('EXPORTS', file=out)
44+
for name in sorted(exported): print(' ', name, file=out)

0 commit comments

Comments
 (0)