ExportImportList 2.5 KB

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