Skip to content

Commit b3c164d

Browse files
sxaaduh95
authored andcommitted
build: fix AIX shared library exports generation
Signed-off-by: Stewart X Addison <sxa@ibm.com> Assisted-by: Cursor PR-URL: #67359 Reviewed-By: Richard Lau <richard.lau@ibm.com>
1 parent 9373f1f commit b3c164d

6 files changed

Lines changed: 159 additions & 57 deletions

File tree

‎node.gyp‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@
673673
},
674674
},
675675
}],
676+
['node_shared=="true" and OS in "aix os400"', {
677+
'dependencies': ['generate_node_exp'],
678+
'ldflags': [
679+
'-Wl,-bE:<(PRODUCT_DIR)/<(node_core_target_name).exp',
680+
],
681+
}],
676682
[ 'node_shared=="false"', {
677683
# Keep this whole-archive section in sync with the `node_lib` target below.
678684
'xcode_settings': {
@@ -1171,6 +1177,23 @@
11711177
'ldflags': [ '-Wl,-brtl' ],
11721178
},
11731179
}],
1180+
['OS in "aix os400"', {
1181+
'conditions': [
1182+
['node_use_node_snapshot=="true"', {
1183+
'variables': {
1184+
'expfile_extra_inputs': [
1185+
'<(obj_dir)/<(node_lib_target_name)/gen/node_snapshot.o',
1186+
],
1187+
},
1188+
}, {
1189+
'variables': {
1190+
'expfile_extra_inputs': [
1191+
'<(obj_dir)/<(node_lib_target_name)/src/node_snapshot_stub.o',
1192+
],
1193+
},
1194+
}],
1195+
],
1196+
}],
11741197
[ 'node_shared=="true" and OS=="win"', {
11751198
'sources': [
11761199
'src/res/node.rc',
@@ -1718,6 +1741,33 @@
17181741
], # end targets
17191742

17201743
'conditions': [
1744+
['OS in "aix os400" and node_shared=="true"', {
1745+
'targets': [
1746+
{
1747+
'target_name': 'generate_node_exp',
1748+
'type': 'none',
1749+
'dependencies': [
1750+
'<(node_lib_target_name)',
1751+
],
1752+
'actions': [
1753+
{
1754+
'action_name': 'generate_node_exp_action',
1755+
'inputs': [
1756+
'<(PRODUCT_DIR)/<(node_lib_target_name).exp',
1757+
],
1758+
'outputs': [
1759+
'<(PRODUCT_DIR)/<(node_core_target_name).exp',
1760+
],
1761+
'action': [
1762+
'<(python)', 'tools/copyfile.py',
1763+
'<@(_inputs)',
1764+
'<@(_outputs)',
1765+
],
1766+
},
1767+
],
1768+
},
1769+
],
1770+
}],
17211771
['OS=="win" and node_shared=="true"', {
17221772
'targets': [
17231773
{

‎node.gypi‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,24 @@
300300
[ 'force_load=="true"', {
301301
'variables': {
302302
'exp_filename': '<(PRODUCT_DIR)/<(_target_name).exp',
303+
'expfile_extra_inputs%': [],
303304
},
304305
'actions': [
305306
{
306307
'action_name': 'expfile',
307308
'inputs': [
308309
'<(obj_dir)',
310+
'<@(expfile_extra_inputs)',
309311
],
310312
'outputs': [
311313
'<(exp_filename)',
312314
],
313315
'action': [
314316
'sh', 'tools/create_expfile.sh',
315-
'<@(_inputs)',
317+
'<(obj_dir)',
316318
'<@(_outputs)',
319+
'<(_target_name)',
320+
'<@(expfile_extra_inputs)',
317321
],
318322
}
319323
],

‎test/addons/dlopen-ping-pong/binding.cc‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const char* dlopen_pong(void) {
1313

1414
namespace {
1515

16+
using v8::Exception;
1617
using v8::FunctionCallbackInfo;
1718
using v8::Isolate;
1819
using v8::Local;
@@ -24,20 +25,43 @@ typedef const char* (*ping)(void);
2425

2526
static ping ping_func;
2627

28+
void ThrowError(Isolate* isolate, const char* message) {
29+
isolate->ThrowException(
30+
Exception::Error(String::NewFromUtf8(isolate, message).ToLocalChecked()));
31+
}
32+
2733
void LoadLibrary(const FunctionCallbackInfo<Value>& args) {
28-
const String::Utf8Value filename(args.GetIsolate(), args[0]);
34+
Isolate* isolate = args.GetIsolate();
35+
const String::Utf8Value filename(isolate, args[0]);
36+
char* dlerrormsg;
2937
void* handle = dlopen(*filename, RTLD_LAZY);
30-
if (handle == nullptr) fprintf(stderr, "%s\n", dlerror());
31-
assert(handle != nullptr);
38+
if (handle == nullptr) {
39+
dlerrormsg = dlerror();
40+
ThrowError(isolate, dlerrormsg != nullptr ? dlerrormsg : "dlopen failed");
41+
return;
42+
}
3243
ping_func = reinterpret_cast<ping>(dlsym(handle, "dlopen_ping"));
33-
assert(ping_func != nullptr);
44+
if (ping_func == nullptr) {
45+
if (dlclose(handle) != 0) {
46+
ThrowError(isolate, "dlclose failed");
47+
return;
48+
}
49+
}
3450
}
3551

3652
void Ping(const FunctionCallbackInfo<Value>& args) {
3753
Isolate* isolate = args.GetIsolate();
38-
assert(ping_func != nullptr);
39-
args.GetReturnValue().Set(String::NewFromUtf8(
40-
isolate, ping_func()).ToLocalChecked());
54+
if (ping_func == nullptr) {
55+
ThrowError(isolate, "ping library was not loaded");
56+
return;
57+
}
58+
const char* answer = ping_func();
59+
if (answer == nullptr) {
60+
ThrowError(isolate, "dlopen_pong could not be resolved (RTLD_GLOBAL)");
61+
return;
62+
}
63+
args.GetReturnValue().Set(
64+
String::NewFromUtf8(isolate, answer).ToLocalChecked());
4165
}
4266

4367
void init(Local<Object> exports) {

‎test/addons/dlopen-ping-pong/binding.gyp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
'xcode_settings': {
1111
'OTHER_LDFLAGS': [ '-Wl,-undefined', '-Wl,dynamic_lookup' ]
1212
}}],
13-
# Enable the shared object to be linked by runtime linker
14-
['OS in "aix os400"', {
13+
# Deferred import of dlopen_pong (-Wl,-G) where ping.c links it directly.
14+
# AIX uses dlsym(RTLD_DEFAULT) in ping.c instead of a link-time import.
15+
['OS=="os400"', {
1516
'ldflags': [ '-Wl,-G' ]
16-
}]],
17+
}],
18+
],
1719
},
1820
{
1921
'target_name': 'binding',
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
#ifndef _WIN32
22

3+
#if defined(_AIX)
4+
#include <stddef.h>
5+
#include <dlfcn.h>
6+
#else
37
const char* dlopen_pong(void);
8+
#endif
49

510
__attribute__((visibility("default")))
611
const char* dlopen_ping(void) {
12+
#if defined(_AIX)
13+
/* ping.so is built with -Wl,-G: link-time import of dlopen_pong is resolved
14+
* when ping.so is loaded. On --shared libnode builds that resolution can fail
15+
* even when dlopen_pong appears in binding's loader table (dump). Resolve
16+
* at call time via the global symbol set after process.dlopen(RTLD_GLOBAL). */
17+
typedef const char* (*pong_fn)(void);
18+
pong_fn pong =
19+
(pong_fn)dlsym(RTLD_DEFAULT, "dlopen_pong");
20+
return pong != NULL ? pong() : NULL;
21+
#else
722
return dlopen_pong();
23+
#endif
824
}
925

1026
#endif // _WIN32

‎tools/create_expfile.sh‎

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# by native add-ons.
77
#
88
# The raw symbol data is obtained by using dump -tov on
9-
# the .a files which make up the node executable.
9+
# the .a and .o files which make up the node executable.
1010
#
1111
# dump -tov flags:
1212
# -t: Display symbol table entries
@@ -29,59 +29,65 @@
2929
# Symbols for the gtest libraries are excluded as
3030
# they are not linked into the node executable.
3131
#
32-
echo "Searching $1 to write out expfile to $2"
32+
SEARCH_DIR="$1"
33+
OUTPUT="$2"
34+
TARGET_NAME="$3"
35+
shift 3 || true
36+
# Remaining arguments are explicit extra inputs for the build to order on.
3337

34-
# This special sequence must be at the start of the exp file.
35-
echo "#!." > "$2.tmp"
38+
echo "Searching $SEARCH_DIR to write out expfile to $OUTPUT"
39+
40+
SYMFILE="${OUTPUT}.syms"
41+
: > "$SYMFILE"
3642

37-
# Pull the symbols from the .a files.
3843
# Use dump -tov to get visibility information and exclude HIDDEN symbols.
3944
# This prevents AIX linker error 0711-407 when addons try to import symbols
4045
# with visibility attributes.
4146
#
4247
# IMPORTANT: AIX dump -tov output has a visibility column that contains either
4348
# a visibility keyword (HIDDEN/EXPORTED/PROTECTED) or blank spaces when no
4449
# visibility attribute is set. Since AWK splits fields on whitespace, this
45-
# results in different field counts:
46-
#
47-
# With visibility keyword (8 fields after AWK splits):
48-
# [137] m 0x00003290 .text 1 weak HIDDEN ._ZNKSt3__1...
49-
# $1 $2 $3 $4 $5 $6 $7 $8
50-
# | | └─ Symbol name
51-
# | └─ Visibility (HIDDEN/EXPORTED/PROTECTED)
52-
# └─ Linkage (weak/extern)
53-
#
54-
# Without visibility keyword (7 fields after AWK splits, spaces collapsed):
55-
# [77] m 0x00000000 .text 1 weak ._ZN8simdjson...
56-
# $1 $2 $3 $4 $5 $6 $7
57-
# | └─ Symbol name
58-
# └─ Linkage (weak/extern)
59-
#
60-
# The awk script handles both cases by using an associative array V[]
61-
# to map field values to their export suffixes. For fields that don't match
62-
# any key in V[], the lookup returns an empty string, which is perfect for
63-
# handling the variable field positions caused by the blank visibility column.
64-
#
65-
find "$1" -name "*.a" | grep -v gtest | while read -r f; do
66-
dump -tov -X 32_64 "$f" 2>/dev/null | \
67-
awk '
68-
BEGIN {
69-
V["EXPORTED"]=" export"
70-
V["PROTECTED"]=" protected"
71-
V["HIDDEN"]=" hidden"
72-
V["weak"]=" weak"
73-
}
74-
/^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|tdata|bss) +[^ ]+ +(extern|weak)( +(EXPORTED|PROTECTED|HIDDEN))?/ {
75-
# Exclude symbols starting with dot, __sinit, __sterm, __[0-9]+__
76-
# Also exclude HIDDEN symbols to avoid visibility attribute issues
77-
if (!match($NF,/^(\.|__sinit|__sterm|__[0-9]+__)/)) {
78-
# Skip if HIDDEN visibility (can be at $(NF-1) or $(NF-2))
79-
if ($(NF-1) != "HIDDEN" && $(NF-2) != "HIDDEN") {
80-
print $NF V[$(NF-1)] V[$(NF-2)]
81-
}
82-
}
50+
# results in different field counts. The awk script handles both cases by using
51+
# an associative array V[] to map field values to their export suffixes.
52+
53+
# This is to bypass the lint-sh job from saying "Expressions don't expand in
54+
# single quotes, use double quotes for that." which is invalid for this case
55+
# shellcheck disable=SC2016
56+
DUMP_AWK='
57+
BEGIN {
58+
V["EXPORTED"]=" export"
59+
V["PROTECTED"]=" protected"
60+
V["HIDDEN"]=" hidden"
61+
V["weak"]=" weak"
62+
}
63+
/^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|tdata|bss) +[^ ]+ +(extern|weak)( +(EXPORTED|PROTECTED|HIDDEN))?/ {
64+
if (!match($NF,/^(\.|__sinit|__sterm|__[0-9]+__)/)) {
65+
if ($(NF-1) != "HIDDEN" && $(NF-2) != "HIDDEN") {
66+
print $NF V[$(NF-1)] V[$(NF-2)]
8367
}
84-
'
85-
done | sort -u >> "$2.tmp"
68+
}
69+
}
70+
'
71+
72+
find "$SEARCH_DIR" -name "*.a" | grep -v gtest | while read -r f; do
73+
dump -tov -X 32_64 "$f" 2>/dev/null | awk "$DUMP_AWK" >> "$SYMFILE"
74+
done
75+
76+
# Objects linked directly into the target (not archived), e.g. node_snapshot.o.
77+
if [ -n "$TARGET_NAME" ] && [ -d "$SEARCH_DIR/$TARGET_NAME" ]; then
78+
find "$SEARCH_DIR/$TARGET_NAME" -name "*.o" | while read -r f; do
79+
dump -tov -X 32_64 "$f" 2>/dev/null | awk "$DUMP_AWK" >> "$SYMFILE"
80+
done
81+
fi
82+
83+
for extra in "$@"; do
84+
if [ -f "$extra" ]; then
85+
dump -tov -X 32_64 "$extra" 2>/dev/null | awk "$DUMP_AWK" >> "$SYMFILE"
86+
fi
87+
done
8688

87-
mv -f "$2.tmp" "$2"
89+
{
90+
echo "#!."
91+
sort -u "$SYMFILE"
92+
} > "$OUTPUT"
93+
rm -f "$SYMFILE"

0 commit comments

Comments
 (0)