scripts/symbolize.py: get binary architecture from ELF file

Instead of making a fragile assumption, that we have a 64-bit TEE core
if we encounter 64-bit registers in the dump, read the architecture
from the ELF file itself. This allows to correctly parse 32- and 64-bit
TEE core call stacks without any context but the string "Call stack:"
at the beginning. Therefore, the helper script can now be used on core
panics.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/scripts/symbolize.py b/scripts/symbolize.py
index b39f101..9599b6f 100755
--- a/scripts/symbolize.py
+++ b/scripts/symbolize.py
@@ -39,7 +39,6 @@
                         'load address: (?P<load_addr>0x[0-9a-f]+)')
 CALL_STACK_RE = re.compile('Call stack:')
 STACK_ADDR_RE = re.compile(r':  (?P<addr>0x[0-9a-f]+)')
-X64_REGS_RE = re.compile(':  x0  [0-9a-f]{16} x1  [0-9a-f]{16}')
 ABORT_ADDR_RE = re.compile('-abort at address (?P<addr>0x[0-9a-f]+)')
 
 epilog = '''
@@ -103,13 +102,22 @@
             if elf:
                 return elf[0]
 
+    def set_arch(self):
+        if self._arch:
+            return
+        if self._bin:
+            p = subprocess.Popen([ 'file', self.get_elf(self._bin) ],
+                                 stdout=subprocess.PIPE)
+            output = p.stdout.readlines()
+            p.terminate()
+            if 'ARM aarch64,' in output[0]:
+                self._arch = 'aarch64-linux-gnu-'
+            elif 'ARM,' in output[0]:
+                self._arch = 'arm-linux-gnueabihf-'
+
     def arch_prefix(self, cmd):
-        if self._arch == 'arm':
-            return 'arm-linux-gnueabihf-' + cmd
-        elif self._arch == 'aarch64':
-            return 'aarch64-linux-gnu-' + cmd
-        else:
-            return ''
+        self.set_arch()
+        return self._arch + cmd
 
     def spawn_addr2line(self):
         if not self._addr2line:
@@ -230,7 +238,7 @@
         if self._addr2line:
             self._addr2line.terminate()
             self._addr2line = None
-        self._arch = 'arm'
+        self._arch = None
         self._saved_abort_line = ''
 
     def write(self, line):
@@ -263,13 +271,7 @@
                 self._bin = match.group('uuid')
             match = re.search(TA_INFO_RE, line)
             if match:
-                self._arch = match.group('arch')
                 self._load_addr = match.group('load_addr')
-            match = re.search(X64_REGS_RE, line)
-            if match:
-                # Assume _arch represents the TEE core. If we have a TA dump,
-                # it will be overwritten later
-                self._arch = 'aarch64'
             match = re.search(ABORT_ADDR_RE, line)
             if match:
                 # At this point the arch and TA load address are unknown.