[Lldb-commits] [lldb] [lldb] Add windows support for LLDB_EXPORT_ALL_SYMBOLS (PR #67628)
Walter Erquinigo via lldb-commits
lldb-commits at lists.llvm.org
Wed Sep 27 19:51:27 PDT 2023
================
@@ -0,0 +1,100 @@
+"""A tool for extracting a list of private lldb symbols to export for MSVC.
+
+When exporting symbols from a dll or exe we either need to mark the symbols in
+the source code as __declspec(dllexport) or supply a list of symbols to the
+linker. Private symbols in LLDB don't explicitly specific dllexport, so we
+automate that by examining the symbol table.
+"""
+
+import argparse
+import os
+import re
+import subprocess
+import sys
+
+
+def extract_symbols(nm_path: str, lib: str):
+ """Extract all of the private lldb symbols from the given path to llvm-nm and
+ library to extract from."""
+
+ # Matches mangled symbols containing 'lldb_private'.
+ lldb_sym_re = r"0* [BT] (?P<symbol>[?]+[^?].*lldb_private.*)"
+
+ # '-g' means we only get global symbols.
+ # '-p' do not waste time sorting the symbols.
+ process = subprocess.Popen(
+ [nm_path, "-g", "-p", lib],
+ bufsize=1,
+ stdout=subprocess.PIPE,
+ stdin=subprocess.PIPE,
+ universal_newlines=True,
+ )
+ process.stdin.close()
+
+ lldb_symbols = set()
+ for line in process.stdout:
+ match = re.match(lldb_sym_re, line)
+ if match:
+ symbol = match.group("symbol")
+ assert symbol.count(" ") == 0, (
+ "Regex matched too much, probably got undecorated name as well"
+ )
+ # Deleting destructors start with ?_G or ?_E and can be discarded
+ # because link.exe gives you a warning telling you they can't be
+ # exported if you don't.
+ if symbol.startswith("??_G") or symbol.startswith("??_E"):
+ continue
+ lldb_symbols.add(symbol)
+
+ return lldb_symbols
+
+
+def main():
+ parser = argparse.ArgumentParser(description="Generate LLDB dll exports")
+ parser.add_argument(
+ "-o", metavar="file", type=str, help="The name of the resultant export file."
+ )
+ parser.add_argument("--nm", help="Path to the llvm-nm executable.")
+ parser.add_argument(
+ "libs",
+ metavar="lib",
+ type=str,
+ nargs="+",
+ help="The libraries to extract symbols from.",
+ )
+ args = parser.parse_args()
+
+ # Get the list of libraries to extract symbols from
+ libs = list()
+ for lib in args.libs:
+ # When invoked by cmake the arguments are the cmake target names of the
+ # libraries, so we need to add .lib/.a to the end and maybe lib to the
+ # start to get the filename. Also allow objects.
+ suffixes = [".lib", ".a", ".obj", ".o"]
+ if not any([lib.endswith(s) for s in suffixes]):
+ for s in suffixes:
+ if os.path.exists(lib + s):
+ lib = lib + s
+ break
+ if os.path.exists("lib" + lib + s):
+ lib = "lib" + lib + s
+ break
----------------
walter-erquinigo wrote:
I'd rewrite this like
```suggestion
for suffix in suffixes:
for path in [lib + suffix, "lib" + lib + suffix]:
if os.path.exists(path):
lib = path
break
```
https://github.com/llvm/llvm-project/pull/67628
More information about the lldb-commits
mailing list