Skip to contentSkip to conversation

lib/oe/qa: add ELF machine to string function

PATCH In review Ross Burton 2 participants 5 messages Oct 11, 2016

lore
Share & tools
lore
#0 lore raw

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

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.

---
 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
meta/lib/oe/qa.py +20 -0
index 75e7df8..fbe719d 100644
@@ -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])
meta/lib/oe/tests/test_elf.py new file +21 -0
new file mode 100644
index 0000000..1f59037
@@ -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')")
Signature · 1 line
2.8.1
#1 lore raw

PATCH 2/2 insane: display names instead of ELF machine numbers

The 'arch' QA test currently simply outputs the ELF machine field as a number which isn't helpful. Display this as a human-readable name to make it clearer to the user what the problem is.

---
 meta/classes/insane.bbclass | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
meta/classes/insane.bbclass +2 -2
index b347638..17c9284 100644
@@ -543,8 +543,8 @@ def package_qa_check_arch(path,name,d, elf, messages):
     # Check the architecture and endiannes of the binary
     if not ((machine == elf.machine()) or \
         ((("virtual/kernel" in provides) or bb.data.inherits_class("module", d) ) and (target_os == "linux-gnux32" or target_os == "linux-gnun32"))):
-        package_qa_add_message(messages, "arch", "Architecture did not match (%d to %d) on %s" % \
-                 (machine, elf.machine(), package_qa_clean_path(path,d)))
+        package_qa_add_message(messages, "arch", "Architecture did not match (%s, expected %s) on %s" % \
+                 (oe.qa.elf_machine_to_string(elf.machine()), oe.qa.elf_machine_to_string(machine), package_qa_clean_path(path,d)))
     elif not ((bits == elf.abiSize()) or  \
         ((("virtual/kernel" in provides) or bb.data.inherits_class("module", d) ) and (target_os == "linux-gnux32" or target_os == "linux-gnun32"))):
         package_qa_add_message(messages, "arch", "Bit size did not match (%d to %d) %s on %s" % \
Signature · 1 line
2.8.1
#2 lore raw

Re: [PATCH 2/2] insane: display names instead of ELF machine numbers

Quoted 15 lines · Ross Burton

The 'arch' QA test currently simply outputs the ELF machine field as a number which isn't helpful. Display this as a human-readable name to make it clearer to the user what the problem is.

---
 meta/classes/insane.bbclass | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
meta/classes/insane.bbclass +0 -0
index b347638..17c9284 100644

meta/classes/insane.bbclass def package_qa_check_arch(path,name,d, elf, messages): · line 550

Quoted 10 lines
meta/classes/insane.bbclass +2 -2
@@ -543,8 +543,8 @@ def package_qa_check_arch(path,name,d, elf, messages):
     # Check the architecture and endiannes of the binary
     if not ((machine == elf.machine()) or \
         ((("virtual/kernel" in provides) or bb.data.inherits_class("module", d) ) and (target_os == "linux-gnux32" or target_os == "linux-gnun32"))):
-        package_qa_add_message(messages, "arch", "Architecture did not match (%d to %d) on %s" % \
-                 (machine, elf.machine(), package_qa_clean_path(path,d)))
+        package_qa_add_message(messages, "arch", "Architecture did not match (%s, expected %s) on %s" % \
+                 (oe.qa.elf_machine_to_string(elf.machine()), oe.qa.elf_machine_to_string(machine), package_qa_clean_path(path,d)))
     elif not ((bits == elf.abiSize()) or  \
         ((("virtual/kernel" in provides) or bb.data.inherits_class("module", d) ) and (target_os == "linux-gnux32" or target_os == "linux-gnun32"))):
         package_qa_add_message(messages, "arch", "Bit size did not match (%d to %d) %s on %s" % \

Did you mean to swap the items? The old version used
machine, elf.machine()
and this version uses
elf.machine(), machine

Signature · 4 lines
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------
#3 lore raw

Re: [PATCH 2/2] insane: display names instead of ELF machine numbers

Quoted 5 lines · Gary Thomas

Did you mean to swap the items? The old version used
machine, elf.machine()
and this version uses
elf.machine(), machine

Yes, because the string format changed to be clearer too:

- package_qa_add_message(messages, "arch", "Architecture did not
match (%d to %d) on %s" % \
- (machine, elf.machine(), package_qa_clean_path(path,d)))
+ package_qa_add_message(messages, "arch", "Architecture did not
match (%s, expected %s) on %s" % \
+ (oe.qa.elf_machine_to_string(elf.machine()),
oe.qa.elf_machine_to_string(machine),

The old message would say "20 to 62", the new message is "x86-64, expected PowerPC".

Ross

#4 lore raw

Re: [PATCH 2/2] insane: display names instead of ELF machine numbers

Quoted 16 lines · Ross

On 11 October 2016 at 13:26, Gary Thomas <[email protected] <mailto:[email protected]>> wrote:

Did you mean to swap the items? The old version used
machine, elf.machine()
and this version uses
elf.machine(), machine

Yes, because the string format changed to be clearer too:

meta/classes/insane.bbclass +2 -2
-        package_qa_add_message(messages, "arch", "Architecture did not match (%d to %d) on %s" % \
-                 (machine, elf.machine(), package_qa_clean_path(path,d)))
+        package_qa_add_message(messages, "arch", "Architecture did not match (%s, expected %s) on %s" % \
+                 (oe.qa.elf_machine_to_string(elf.machine()), oe.qa.elf_machine_to_string(machine),
Quoted 1 line

The old message would say "20 to 62", the new message is "x86-64, expected PowerPC".

Much nicer indeed.

Signature · 4 lines
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------