Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32"""Check that a libsecp256k1 shared library exports only expected symbols.4Usage examples:5- When building with Autotools:6./tools/symbol-check.py .libs/libsecp256k1.so7./tools/symbol-check.py .libs/libsecp256k1-<V>.dll8./tools/symbol-check.py .libs/libsecp256k1.dylib10- When building with CMake:11./tools/symbol-check.py build/lib/libsecp256k1.so12./tools/symbol-check.py build/bin/libsecp256k1-<V>.dll13./tools/symbol-check.py build/lib/libsecp256k1.dylib"""15import re16import sys17import subprocess19import lief22class UnexpectedExport(RuntimeError):23pass26def get_exported_exports(library) -> list[str]:27"""Adapter function to get exported symbols based on the library format."""28if library.format == lief.Binary.FORMATS.ELF:29return [symbol.name for symbol in library.exported_symbols]30elif library.format == lief.Binary.FORMATS.PE:31return [entry.name for entry in library.get_export().entries]32elif library.format == lief.Binary.FORMATS.MACHO:33return [symbol.name[1:] for symbol in library.exported_symbols]34raise NotImplementedError(f"Unsupported format: {library.format}")37def grep_expected_symbols() -> list[str]:38"""Guess the list of expected exported symbols from the source code."""39grep_output = subprocess.check_output(40["git", "grep", r"^\s*SECP256K1_API", "--", "include"],41universal_newlines=True,42encoding="utf-8"43)44lines = grep_output.split("\n")45pattern = re.compile(r'\bsecp256k1_\w+')46exported: list[str] = [pattern.findall(line)[-1] for line in lines if line.strip()]47return exported50def check_symbols(library, expected_exports) -> None:51"""Check that the library exports only the expected symbols."""52actual_exports = get_exported_exports(library)53unexpected_exports = set(actual_exports) - set(expected_exports)54if unexpected_exports != set():55raise UnexpectedExport(f"Unexpected exported symbols: {unexpected_exports}")57def main():58if len(sys.argv) != 2:59print(__doc__)60return 161library = lief.parse(sys.argv[1])62expected_exports = grep_expected_symbols()63try:64check_symbols(library, expected_exports)65except UnexpectedExport as e:66print(f"{sys.argv[0]}: In {sys.argv[1]}: {e}")67return 168return 071if __name__ == "__main__":72sys.exit(main())
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.