[PATCH v3 01/11] binman: elf: Check for ELF_TOOLS availability and remove extra semicolon

lukas.funke-oss at weidmueller.com lukas.funke-oss at weidmueller.com
Tue Jul 18 13:53:09 CEST 2023


From: Lukas Funke <lukas.funke at weidmueller.com>

Check if elf tools are available when running DecodeElf(). Also
remove superfuous semicolon at line ending.

Signed-off-by: Lukas Funke <lukas.funke at weidmueller.com>
Reviewed-by: Simon Glass <sjg at chromium.org>

---

Changes in v3:
- Improved test coverage regarding missing libelf
- Align error message

 tools/binman/elf.py      | 14 +++++++-------
 tools/binman/elf_test.py | 11 +++++++++++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/tools/binman/elf.py b/tools/binman/elf.py
index 5816284c32..e1a17cef96 100644
--- a/tools/binman/elf.py
+++ b/tools/binman/elf.py
@@ -255,9 +255,7 @@ def LookupAndWriteSymbols(elf_fname, entry, section, is_elf=False,
     syms = GetSymbols(fname, ['image', 'binman'])
     if is_elf:
         if not ELF_TOOLS:
-            msg = ("Section '%s': entry '%s'" %
-                   (section.GetPath(), entry.GetPath()))
-            raise ValueError(f'{msg}: Cannot write symbols to an ELF file without Python elftools')
+            raise ValueError("Python: No module named 'elftools'")
         new_syms = {}
         with open(fname, 'rb') as fd:
             elf = ELFFile(fd)
@@ -438,13 +436,15 @@ def DecodeElf(data, location):
     Returns:
         ElfInfo object containing information about the decoded ELF file
     """
+    if not ELF_TOOLS:
+        raise ValueError("Python: No module named 'elftools'")
     file_size = len(data)
     with io.BytesIO(data) as fd:
         elf = ELFFile(fd)
-        data_start = 0xffffffff;
-        data_end = 0;
-        mem_end = 0;
-        virt_to_phys = 0;
+        data_start = 0xffffffff
+        data_end = 0
+        mem_end = 0
+        virt_to_phys = 0
 
         for i in range(elf.num_segments()):
             segment = elf.get_segment(i)
diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py
index c98083961b..f78ad647d6 100644
--- a/tools/binman/elf_test.py
+++ b/tools/binman/elf_test.py
@@ -253,6 +253,17 @@ class TestElf(unittest.TestCase):
             fname = self.ElfTestFile('embed_data')
             with self.assertRaises(ValueError) as e:
                 elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
+            with self.assertRaises(ValueError) as e:
+                elf.DecodeElf(tools.read_file(fname), 0xdeadbeef)
+            with self.assertRaises(ValueError) as e:
+                elf.GetFileOffset(fname, 0xdeadbeef)
+            with self.assertRaises(ValueError) as e:
+                elf.GetSymbolFromAddress(fname, 0xdeadbeef)
+            with self.assertRaises(ValueError) as e:
+                entry = FakeEntry(10)
+                section = FakeSection()
+                elf.LookupAndWriteSymbols(fname, entry, section, True)
+
             self.assertIn("Python: No module named 'elftools'",
                       str(e.exception))
         finally:
-- 
2.30.2



More information about the U-Boot mailing list