summaryrefslogtreecommitdiffhomepage
path: root/Translations/objcopy.py
blob: be0cc4099c5cbf1fdcf3c4e63c9e4d0751e9406d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os
import subprocess
import tempfile


if "OBJCOPY" in os.environ:
    OBJCOPY = os.environ["OBJCOPY"]
else:
    OBJCOPY = "objcopy"


def get_binary_from_obj(objfile_path: str, section_name: str) -> bytes:
    tmpfd, tmpfile = tempfile.mkstemp()
    result = subprocess.run(
        [OBJCOPY, "-O", "binary", "-j", section_name, objfile_path, tmpfile]
    )
    result.check_returncode()
    with open(tmpfd, "rb") as f:
        bin: bytes = f.read()
    os.remove(tmpfile)
    return bin


def cpp_var_to_section_name(var_name: str) -> str:
    return f".rodata._ZL{len(var_name)}{var_name}"