| 1 |
"""Calculate symbol_versions for a policy in policy.json by collection |
| 2 |
defined version (.gnu.version_d) from libraries in lib_whitelist. |
| 3 |
This should be run inside a manylinux Docker container. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import argparse |
| 9 |
import contextlib |
| 10 |
import json |
| 11 |
import os |
| 12 |
import platform |
| 13 |
|
| 14 |
from elftools.elf.elffile import ELFFile |
| 15 |
|
| 16 |
if platform.architecture()[0] == "64bit": |
| 17 |
LIBRARY_PATHS = ["/lib64", "/usr/lib64"] |
| 18 |
else: |
| 19 |
LIBRARY_PATHS = ["/lib", "/usr/lib"] |
| 20 |
|
| 21 |
parser = argparse.ArgumentParser(description=__doc__) |
| 22 |
parser.add_argument("policy", help="The policy name") |
| 23 |
parser.add_argument("policyjson", help="The policy.json file.") |
| 24 |
|
| 25 |
|
| 26 |
def load_policies(path): |
| 27 |
with open(path) as f: |
| 28 |
return json.load(f) |
| 29 |
|
| 30 |
|
| 31 |
def choose_policy(name, policies): |
| 32 |
try: |
| 33 |
return next(policy for policy in policies if policy["name"] == name) |
| 34 |
except StopIteration: |
| 35 |
msg = f"Unknown policy {name}" |
| 36 |
raise RuntimeError(msg) from None |
| 37 |
|
| 38 |
|
| 39 |
def find_library(library): |
| 40 |
for p in LIBRARY_PATHS: |
| 41 |
path = os.path.join(p, library) |
| 42 |
if os.path.exists(path): |
| 43 |
return path |
| 44 |
msg = f"Unknown library {library}" |
| 45 |
raise RuntimeError(msg) |
| 46 |
|
| 47 |
|
| 48 |
def versionify(version_string): |
| 49 |
try: |
| 50 |
result = [int(n) for n in version_string.split(".")] |
| 51 |
assert len(result) <= 3 # noqa: S101 |
| 52 |
except ValueError: |
| 53 |
result = [999999, 999999, 999999, version_string] |
| 54 |
return result |
| 55 |
|
| 56 |
|
| 57 |
def calculate_symbol_versions(libraries, symbol_versions, arch): |
| 58 |
calculated_symbol_versions = {k: set() for k in symbol_versions} |
| 59 |
prefixes = ["/lib", "/usr/lib"] |
| 60 |
if arch == "64bit": |
| 61 |
prefixes = [p + "64" for p in prefixes] |
| 62 |
|
| 63 |
for library in libraries: |
| 64 |
library_path = find_library(library) |
| 65 |
with open(library_path, "rb") as f: |
| 66 |
e = ELFFile(f) |
| 67 |
section = e.get_section_by_name(".gnu.version_d") |
| 68 |
if section: |
| 69 |
for _, verdef_iter in section.iter_versions(): |
| 70 |
for vernaux in verdef_iter: |
| 71 |
with contextlib.suppress(ValueError): |
| 72 |
name, version = vernaux.name.split("_", 1) |
| 73 |
if name in calculated_symbol_versions and version != "PRIVATE": |
| 74 |
calculated_symbol_versions[name].add(version) |
| 75 |
return {k: sorted(v, key=versionify) for k, v in calculated_symbol_versions.items()} |
| 76 |
|
| 77 |
|
| 78 |
def main(): |
| 79 |
args = parser.parse_args() |
| 80 |
policies = load_policies(args.policyjson) |
| 81 |
policy = choose_policy(args.policy, policies) |
| 82 |
arch, _ = platform.architecture() |
| 83 |
print( |
| 84 |
json.dumps( |
| 85 |
calculate_symbol_versions( |
| 86 |
policy["lib_whitelist"], |
| 87 |
policy["symbol_versions"], |
| 88 |
arch, |
| 89 |
), |
| 90 |
), |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
main() |