From db9da5d184417efabdb464b37adb41ea58faf942 Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:50:39 -0500 Subject: [PATCH] build: add weak symbol detection to export script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AIX export files support the `weak` keyword to mark weak symbols. This helps preserve C++ weak symbol semantics and preventing potential linker conflicts. PR-URL: https://github.com/nodejs/node/pull/62656 Refs: https://www.ibm.com/docs/en/aix/7.2.0?topic=l-ld-command#ld__a3119106d Reviewed-By: Richard Lau Reviewed-By: Michaël Zasso --- tools/create_expfile.sh | 69 ++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/tools/create_expfile.sh b/tools/create_expfile.sh index d76a1d496a0..977f509e2ec 100755 --- a/tools/create_expfile.sh +++ b/tools/create_expfile.sh @@ -5,45 +5,63 @@ # specifically for export so that they can be used # by native add-ons. # -# The raw symbol data is obtained by using nm on +# The raw symbol data is obtained by using dump -tov on # the .a files which make up the node executable. # -# -Xany processes symbols for both 32-bit and -# 64-bit (the default is for 32-bit only). +# dump -tov flags: +# -t: Display symbol table entries +# -o: Display object file headers +# -v: Display visibility information (HIDDEN/EXPORTED/PROTECTED) +# -X 32_64: Process both 32-bit and 64-bit symbols # -# -g selects only exported symbols. +# This approach is similar to CMake's ExportImportList module for AIX: +# https://github.com/Kitware/CMake/blob/45f4742cbfe6c25c7c801e3806c8af04a2c4b09b/Modules/Platform/AIX/ExportImportList#L67-L80 # -# -C, -B and -p ensure that the output is in a -# format that can be easily parsed and converted -# into the required symbol. -# -# -C suppresses the demangling of C++ names. -# -B writes the output in BSD format. -# -p displays the info in a standard portable -# output format. -# -# Only include symbols if they are of the following -# types and don't start with a dot. -# -# T - Global text symbol. -# D - Global data symbol. -# B - Global bss symbol. +# We filter for symbols in .text, .data, .tdata, and .bss sections +# with extern or weak linkage, excluding: +# - Symbols starting with dot (internal/local symbols) +# - __sinit/__sterm (static initialization/termination) +# - __[0-9]+__ (compiler-generated symbols) +# - HIDDEN visibility symbols (to avoid linker error 0711-407) # # The final sort allows removal of any duplicates. # # Symbols for the gtest libraries are excluded as # they are not linked into the node executable. # -set -x echo "Searching $1 to write out expfile to $2" # This special sequence must be at the start of the exp file. echo "#!." > "$2.tmp" # Pull the symbols from the .a files. -# Use dump -tov to get visibility information and exclude HIDDEN symbols +# Use dump -tov to get visibility information and exclude HIDDEN symbols. # This prevents AIX linker error 0711-407 when addons try to import symbols # with visibility attributes. +# +# IMPORTANT: AIX dump -tov output has a visibility column that contains either +# a visibility keyword (HIDDEN/EXPORTED/PROTECTED) or blank spaces when no +# visibility attribute is set. Since AWK splits fields on whitespace, this +# results in different field counts: +# +# With visibility keyword (8 fields after AWK splits): +# [137] m 0x00003290 .text 1 weak HIDDEN ._ZNKSt3__1... +# $1 $2 $3 $4 $5 $6 $7 $8 +# | | └─ Symbol name +# | └─ Visibility (HIDDEN/EXPORTED/PROTECTED) +# └─ Linkage (weak/extern) +# +# Without visibility keyword (7 fields after AWK splits, spaces collapsed): +# [77] m 0x00000000 .text 1 weak ._ZN8simdjson... +# $1 $2 $3 $4 $5 $6 $7 +# | └─ Symbol name +# └─ Linkage (weak/extern) +# +# The awk script handles both cases by using an associative array V[] +# to map field values to their export suffixes. For fields that don't match +# any key in V[], the lookup returns an empty string, which is perfect for +# handling the variable field positions caused by the blank visibility column. +# find "$1" -name "*.a" | grep -v gtest | while read -r f; do dump -tov -X 32_64 "$f" 2>/dev/null | \ awk ' @@ -51,14 +69,15 @@ find "$1" -name "*.a" | grep -v gtest | while read -r f; do V["EXPORTED"]=" export" V["PROTECTED"]=" protected" V["HIDDEN"]=" hidden" + V["weak"]=" weak" } - /^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|tdata|bss) +[^ ]+ +(extern|weak) +(EXPORTED|PROTECTED|HIDDEN| ) / { + /^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|tdata|bss) +[^ ]+ +(extern|weak)( +(EXPORTED|PROTECTED|HIDDEN))?/ { # Exclude symbols starting with dot, __sinit, __sterm, __[0-9]+__ # Also exclude HIDDEN symbols to avoid visibility attribute issues if (!match($NF,/^(\.|__sinit|__sterm|__[0-9]+__)/)) { - visibility = $(NF-1) - if (visibility != "HIDDEN") { - print $NF + # Skip if HIDDEN visibility (can be at $(NF-1) or $(NF-2)) + if ($(NF-1) != "HIDDEN" && $(NF-2) != "HIDDEN") { + print $NF V[$(NF-1)] V[$(NF-2)] } } }