From 40def8ad752d68e68e1c66701850369402148119 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 18 Mar 2022 19:19:49 -0600 Subject: [PATCH] binman: Complete elf test coverage Add coverage for the new elf functions needed for the event_dump.py script. Signed-off-by: Simon Glass --- tools/binman/elf.py | 4 ++-- tools/binman/elf_test.py | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/tools/binman/elf.py b/tools/binman/elf.py index 35971731d0..f24ccd7fb9 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -112,7 +112,7 @@ def GetFileOffset(fname, addr): int: Offset of that address in the ELF file, or None if not valid """ if not ELF_TOOLS: - raise ValueError('Python elftools package is not available') + raise ValueError("Python: No module named 'elftools'") with open(fname, 'rb') as fd: elf = ELFFile(fd) return _GetFileOffset(elf, addr) @@ -128,7 +128,7 @@ def GetSymbolFromAddress(fname, addr): str: Symbol name, or None if no symbol at that address """ if not ELF_TOOLS: - raise ValueError('Python elftools package is not available') + raise ValueError("Python: No module named 'elftools'") with open(fname, 'rb') as fd: elf = ELFFile(fd) syms = GetSymbols(fname, None) diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py index 5084838b91..d401b5b52d 100644 --- a/tools/binman/elf_test.py +++ b/tools/binman/elf_test.py @@ -284,6 +284,54 @@ class TestElf(unittest.TestCase): elf.read_segments(tools.get_bytes(100, 100)) self.assertIn('Magic number does not match', str(e.exception)) + def test_get_file_offset(self): + """Test GetFileOffset() gives the correct file offset for a symbol""" + fname = self.ElfTestFile('embed_data') + syms = elf.GetSymbols(fname, ['embed']) + addr = syms['embed'].address + offset = elf.GetFileOffset(fname, addr) + data = tools.read_file(fname) + + # Just use the first 4 bytes and assume it is little endian + embed_data = data[offset:offset + 4] + embed_value = struct.unpack('