diff --git a/doc/man/ade-sysroot.1 b/doc/man/ade-sysroot.1 index eff5cc146c4173d9da6eb9b478c06e3ce3de43b7..240a8888800db1d4fec2e8d1647c9f97bee0b06a 100644 --- a/doc/man/ade-sysroot.1 +++ b/doc/man/ade-sysroot.1 @@ -115,8 +115,8 @@ Checks a sysroot <archive> file for validity\&. .sp It makes sure the file is a valid gzip compressed tarball (\&.tar\&.gz) and that it contains a valid version file (/etc/image_version)\&. The tool also -assumes that the \fBls\fR utility will be part of the sysroot as it reads -its ELF header to determine the architecture\&. +assumes that the pkg-config\&.multiarch file will be part of the sysroot as +it uses it to determine the architecture\&. .sp .RE .PP diff --git a/tools/ade b/tools/ade index d3deecd3b0d9fa3506eb5793536c3afd602f23c3..09ee6d746c10da47685dbf54f0ff9d6393a20b78 100755 --- a/tools/ade +++ b/tools/ade @@ -85,46 +85,22 @@ class Colors: cls.ENDC = '\033[0m' -class ELFHeader: - - def __init__(self, data): - fields = struct.unpack('<4sBBBBB7sHHI', data[0:24]) - - # ELF Magic - if fields[0] != b'\x7fELF': - raise ValueError("Wrong ELF header magic") - - # Address size (32 or 64 bits) - if fields[1] == 1: - self.addr_size = 32 - elif fields[1] == 2: - self.addr_size = 64 - else: - raise ValueError("Not supported ELF address size {0}".format(fields[1])) - - # Instruction Set Architecture - if fields[8] == 0x28: - self.machine = 'arm' - elif fields[8] == 0xB7: - self.machine = 'aarch64' - else: - raise ValueError("Not supported machine type: {0}".format(fields[8])) - - if self.addr_size == 32: - self.flags, = struct.unpack('<I', data[36:40]) - elif self.addr_size == 64: - self.flags, = struct.unpack('<I', data[48:52]) +class TargetTriplet: + SUPPORTED = { + 'armhf': "arm-linux-gnueabihf", + 'arm64': "aarch64-linux-gnu" + } + + def __init__(self, string): + self.triplet = string + for arch, triplet in self.SUPPORTED.items(): + if string == triplet: + self.arch = arch + return + raise NotSupportedError() def get_architecture(self): - if self.machine == 'arm' and self.addr_size == 32: - if self.flags & HARD_FLOAT_FLAG: - return 'armhf' - else: - return 'armel' - elif self.machine == 'aarch64' and self.addr_size == 64: - return 'arm64' - else: - raise ValueError("Unsupported architecture") + return self.arch class SysrootVersion: @@ -159,8 +135,8 @@ class SysrootVersion: try: with open(os.path.join(path, 'etc', 'image_version')) as f: self.parse_string(f.read()) - with open(os.path.join(path, 'bin', 'ls'), 'rb') as f: - self.arch = ELFHeader(f.read(64)).get_architecture() + with open(os.path.join(path, 'usr', 'lib', 'pkg-config.multiarch')) as f: + self.arch = TargetTriplet(f.read().strip()).get_architecture() except FileNotFoundError as e: if not os.path.exists(path) or not os.listdir(path): raise NotInstalledError @@ -251,25 +227,25 @@ class SysrootArchive: except Exception as e: raise InvalidSysrootArchiveError('Invalid image_version file in archive') - # Extract /bin/ls and determine sysroot architecture - ls_file = None - for path in ['bin/ls', 'binary/bin/ls']: + # Extract /usr/lib/pkg-config.multiarch and determine sysroot architecture + arch_file = None + for path in ['usr/lib/pkg-config.multiarch', 'binary/usr/lib/pkg-config.multiarch']: try: - ls_file = f.getmember(path) + arch_file = f.getmember(path) break except KeyError: continue - if not ls_file: - raise InvalidSysrootArchiveError('Missing ls executable in archive') + + if not arch_file: + raise InvalidSysrootArchiveError('Missing pkg-config.multiarch file in archive') try: - with f.extractfile(ls_file) as reader: - header = ELFHeader(reader.read(64)) - self.version.arch = header.get_architecture() + with f.extractfile(arch_file) as reader: + self.version.arch = TargetTriplet(reader.read().decode().strip()).get_architecture() except NotSupportedError: raise InvalidSysrootArchiveError('Architecture is not supported') except: - raise InvalidSysrootArchiveError('Invalid ELF header for /bin/ls executable') + raise InvalidSysrootArchiveError('Invalid content in pkg-config.multiarch file') except tarfile.ReadError as e: raise InvalidSysrootArchiveError(e)