11# -*- coding: utf-8 -*-
22
33import os
4+ import pathlib
45import re
56import shutil
67import subprocess
78from os import PathLike
89
10+ from elftools .elf .elffile import ELFFile
11+
912from ..logger import LoggerSetup
1013
1114# Parses dependencies from ld output
@@ -41,32 +44,27 @@ def _getInterpreter(path: str, logger) -> str:
4144 """
4245
4346 with open (path , "rb" ) as f :
44- head = f .read (19 )
47+ elf = ELFFile (f )
48+ interp = elf .get_section_by_name (".interp" )
4549
46- if head [5 ] == 1 :
47- arch = head [17 :]
48- elif head [5 ] == 2 :
49- arch = head [17 :][::- 1 ]
50- else :
51- logger .error (
52- f"Error: Unknown endianess value for { path } : expected 1 or 2, but was { head [5 ]} "
53- )
54- exit (1 )
55-
56- if arch == b"\x00 \xb7 " : # 00b7: aarch64
57- return "/lib/ld-linux-aarch64.so.1"
58- elif arch == b"\x00 \x3e " : # 003e: x86_64
59- return "/lib64/ld-linux-x86-64.so.2"
60- elif arch == b"\x00 \x03 " : # 0003: i686
61- return "/lib/ld-linux.so.2"
50+ if interp :
51+ return interp .data ().split (b"\x00 " )[0 ].decode ()
6252 else :
63- logger .error (
64- f"Error: Unsupported architecture for { path } : only support x86_64 (003e), aarch64 (00b7) and i686 (0003), but was { arch } "
65- )
66- exit (1 )
67-
68-
69- def _get_default_package_dir () -> str :
53+ match elf .header ["e_machine" ]:
54+ case "EM_AARCH64" :
55+ return "/lib/ld-linux-aarch64.so.1"
56+ case "EM_386" :
57+ return "/lib/ld-linux.so.2"
58+ case "EM_X86_64" :
59+ return "/lib64/ld-linux-x86-64.so.2"
60+ case arch :
61+ logger .error (
62+ f"Error: Unsupported architecture for { path } : only support x86_64 (003e), aarch64 (00b7) and i686 (0003), but was { arch } "
63+ )
64+ exit (1 )
65+
66+
67+ def _get_default_package_dir () -> PathLike [str ]:
7068 """
7169 Finds the default site-packages or dist-packages directory of the default python3 environment
7270
@@ -79,12 +77,12 @@ def _get_default_package_dir() -> str:
7977 ["/bin/sh" , "-c" , 'python3 -c "import site; print(site.getsitepackages()[0])"' ],
8078 stdout = subprocess .PIPE ,
8179 )
82- return out .stdout .decode ().strip ()
80+ return pathlib . Path ( out .stdout .decode ().strip () )
8381
8482
8583def export (
8684 output_dir : str | PathLike [str ] = "/required_libs" ,
87- package_dir : str | PathLike [str ] = None ,
85+ package_dir : str | PathLike [str ] | None = None ,
8886 logger = None ,
8987) -> None :
9088 """
0 commit comments