Skip to content
Snippets Groups Projects
Commit 5dd785cb authored by Louis-Francis Ratté-Boulianne's avatar Louis-Francis Ratté-Boulianne Committed by Guillaume Desmottes
Browse files

Use pkg-config.multiarch file to determine architecture


The file contains a GNU Target Triplet string that describes the
sysroot. It is needed for the cross-compilation step so can safely
assume to be available.

Signed-off-by: default avatarLouis-Francis Ratté-Boulianne <lfrb@collabora.com>
Reviewed-by: default avatarGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Differential Revision: https://phabricator.apertis.org/D4916
parent 63486c1e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment