diff mbox

[1/2] lib/oe/qa: add ELF machine to string function

Message ID 1476188384-16064-1-git-send-email-ross.burton@intel.com
State Accepted
Commit 72336003741fb16a7ecdd6b753eae56310413ff7
Headers show

Commit Message

Ross Burton Oct. 11, 2016, 12:19 p.m. UTC
Add a function (and test suite) to turn the ELF machine field (e_machine) into a
string, so we can tell the user "x86-64" instead of 0x3E.

Signed-off-by: Ross Burton <ross.burton@intel.com>

---
 meta/lib/oe/qa.py             | 20 ++++++++++++++++++++
 meta/lib/oe/tests/test_elf.py | 21 +++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 meta/lib/oe/tests/test_elf.py

-- 
2.8.1

-- 
_______________________________________________
Openembedded-core mailing list
Openembedded-core@lists.openembedded.org
http://lists.openembedded.org/mailman/listinfo/openembedded-core
diff mbox

Patch

diff --git a/meta/lib/oe/qa.py b/meta/lib/oe/qa.py
index 75e7df8..fbe719d 100644
--- a/meta/lib/oe/qa.py
+++ b/meta/lib/oe/qa.py
@@ -144,6 +144,26 @@  class ELFFile:
             bb.note("%s %s %s failed: %s" % (objdump, cmd, self.name, e))
             return ""
 
+def elf_machine_to_string(machine):
+    """
+    Return the name of a given ELF e_machine field or the hex value as a string
+    if it isn't recognised.
+    """
+    try:
+        return {
+            0x02: "SPARC",
+            0x03: "x86",
+            0x08: "MIPS",
+            0x14: "PowerPC",
+            0x28: "ARM",
+            0x2A: "SuperH",
+            0x32: "IA-64",
+            0x3E: "x86-64",
+            0xB7: "AArch64"
+        }[machine]
+    except:
+        return "Unknown (%s)" % repr(machine)
+
 if __name__ == "__main__":
     import sys
     elf = ELFFile(sys.argv[1])
diff --git a/meta/lib/oe/tests/test_elf.py b/meta/lib/oe/tests/test_elf.py
new file mode 100644
index 0000000..1f59037
--- /dev/null
+++ b/meta/lib/oe/tests/test_elf.py
@@ -0,0 +1,21 @@ 
+import unittest
+import oe.qa
+
+class TestElf(unittest.TestCase):
+    def test_machine_name(self):
+        """
+        Test elf_machine_to_string()
+        """
+        self.assertEqual(oe.qa.elf_machine_to_string(0x02), "SPARC")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x03), "x86")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x08), "MIPS")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x14), "PowerPC")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x28), "ARM")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x2A), "SuperH")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x32), "IA-64")
+        self.assertEqual(oe.qa.elf_machine_to_string(0x3E), "x86-64")
+        self.assertEqual(oe.qa.elf_machine_to_string(0xB7), "AArch64")
+
+        self.assertEqual(oe.qa.elf_machine_to_string(0x00), "Unknown (0)")
+        self.assertEqual(oe.qa.elf_machine_to_string(0xDEADBEEF), "Unknown (3735928559)")
+        self.assertEqual(oe.qa.elf_machine_to_string("foobar"), "Unknown ('foobar')")