Debian Codegraph @

Modules/Platform/AIX/ExportImportList

raw
1 #!/bin/sh
2 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
3 # file LICENSE.rst or https://cmake.org/licensing for details.
4
5 # This script is internal to CMake and meant only to be
6 # invoked by CMake-generated build systems on AIX.
7
8 usage='usage: ExportImportList -o <out-file> -c <compiler> [-l <lib>] [-n] [--] <objects>...'
9
10 die() {
11 echo "$@" 1>&2; exit 1
12 }
13
14 # Process command-line arguments.
15 out=''
16 lib=''
17 no_objects=''
18 compiler=''
19 while test "$#" != 0; do
20 case "$1" in
21 -l) shift; lib="$1" ;;
22 -o) shift; out="$1" ;;
23 -n) no_objects='1' ;;
24 -c) shift; compiler="$1" ;;
25 --) shift; break ;;
26 --target=*) ;;
27 -*) die "$usage" ;;
28 *) break ;;
29 esac
30 shift
31 done
32 test -n "$out" || die "$usage"
33 # We need the compiler executable to resolve where the ibm-llvm-nm executable is
34 test -n "$compiler" || die "$usage"
35
36 # Build a temporary file that atomically replaces the output later.
37 out_tmp="$out.tmp$$"
38 trap 'rm -f "$out_tmp"' EXIT INT TERM
39 > "$out_tmp"
40
41 # If IPA was enabled and a compiler from the IBMClang family is used, then
42 # the object files contain LLVM bitcode[0] rather than XCOFF objects and so
43 # need to be handled differently.
44 #
45 # [0]: https://www.ibm.com/docs/en/openxl-c-and-cpp-aix/17.1.0?topic=compatibility-link-time-optimization-lto
46 NM="$(dirname "$compiler")/../libexec/ibm-llvm-nm"
47
48 function IsBitcode {
49 # N4 = first 4 bytes, -tx = output in hexadecimal, -An = don't display offset
50 # cut: trim off the preceding whitespace where the offset would be
51 # 4243code is the hexadecimal magic number for LLVM bitcode
52 [ "$(od -N4 -tx -An $1 | cut -d ' ' -f 2)" == "4243c0de" ];
53 }
54
55 # Collect symbols exported from all object files.
56 if test -z "$no_objects"; then
57 for f in "$@"; do
58 if IsBitcode "$f"; then
59 "$NM" "$f" --defined-only --extern-only --just-symbol-name 2>/dev/null
60 else
61 dump -tov -X 32_64 "$f" |
62 awk '
63 BEGIN {
64 V["EXPORTED"]=" export"
65 V["PROTECTED"]=" protected"
66 }
67 /^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|tdata|bss|tbss) +[^ ]+ +(extern|weak) +(EXPORTED|PROTECTED| ) / {
68 if (!match($NF,/^(\.|__sinit|__sterm|__[0-9]+__)/)) {
69 print $NF V[$(NF-1)]
70 }
71 }
72 '
73 fi
74 done >> "$out_tmp"
75 fi
76
77 # Generate the export/import file.
78 {
79 if test -n "$lib"; then
80 echo "#! $lib"
81 fi
82 sort -u "$out_tmp"
83 } > "$out"