Skip to content

Commit

Permalink
[CONTRIB] Add nm symbol dump (#16763)
Browse files Browse the repository at this point in the history
This PR adds nm symbol dump utils so we can use it
to validate static compiled files.
  • Loading branch information
tqchen authored Mar 21, 2024
1 parent 6c701fe commit 89cd74c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
46 changes: 46 additions & 0 deletions python/tvm/contrib/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# pylint: disable=invalid-name
import sys
from typing import Dict

from .._ffi.base import py_str
from . import tar as _tar
Expand Down Expand Up @@ -178,6 +179,51 @@ def create_executable(output, objects, options=None, cc=None, cwd=None, ccache_e
raise ValueError("Unsupported platform")


def get_global_symbol_section_map(path, *, nm=None) -> Dict[str, str]:
"""Get global symbols from a library via nm -g
Parameters
----------
path : str
The library path
nm: str
The path to nm command
Returns
-------
symbol_section_map: Dict[str, str]
A map from defined global symbol to their sections
"""
if nm is None:
if not _is_linux_like():
raise ValueError("Unsupported platform")
nm = "nm"

symbol_section_map = {}

if not os.path.isfile(path):
raise FileNotFoundError(f"{path} does not exist")

cmd = [nm, "-gU", path]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(out, _) = proc.communicate()

if proc.returncode != 0:
msg = "Runtime error:\n"
msg += py_str(out)
raise RuntimeError(msg)

for line in py_str(out).split("\n"):
data = line.strip().split()
if len(data) != 3:
continue
symbol = data[-1]
section = data[-2]
symbol_section_map[symbol] = section
return symbol_section_map


def get_target_by_dump_machine(compiler):
"""Functor of get_target_triple that can get the target triple using compiler.
Expand Down
31 changes: 30 additions & 1 deletion python/tvm/contrib/ndk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import subprocess
import os
import shutil
from typing import Dict

from .._ffi.base import py_str
from . import utils as _utils, tar as _tar
from . import utils as _utils, tar as _tar, cc as _cc
from .cc import get_target_by_dump_machine


Expand Down Expand Up @@ -123,3 +125,30 @@ def create_staticlib(output, inputs):


create_staticlib.output_format = "a"


def get_global_symbol_section_map(path, *, nm=None) -> Dict[str, str]:
"""Get global symbols from a library via nm -gU in NDK
Parameters
----------
path : str
The library path
nm: str
The path to nm command
Returns
-------
symbol_section_map: Dict[str, str]
A map from defined global symbol to their sections
"""
if "TVM_NDK_CC" not in os.environ:
raise RuntimeError(
"Require environment variable TVM_NDK_CC" " to be the NDK standalone compiler"
)
if nm is None:
compiler = os.environ["TVM_NDK_CC"]
base_path = os.path.dirname(compiler)
nm = os.path.join(base_path, "llvm-nm")
return _cc.get_global_symbol_section_map(path, nm=nm)

0 comments on commit 89cd74c

Please sign in to comment.