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

2023-08-02 Thread Simon Glass
From: Lukas Funke 

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

Signed-off-by: Lukas Funke 
Reviewed-by: Simon Glass 

---

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(-)

Applied to u-boot-dm, thanks!


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

2023-07-18 Thread lukas . funke-oss
From: Lukas Funke 

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

Signed-off-by: Lukas Funke 
Reviewed-by: Simon Glass 

---

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 = 0x;
-data_end = 0;
-mem_end = 0;
-virt_to_phys = 0;
+data_start = 0x
+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