Size of string constant symbol may be larger than its length measured up to NUL terminator. In this case pmdinfogen included padding bytes after NUL terminator in generated source, yielding incorrect code.
Always trim string data to NUL terminator while reading ELF. It was already done for COFF because there's no symbol size. Bugzilla ID: 720 Fixes: f0f93a7adfee ("buildtools: use Python pmdinfogen") Signed-off-by: Dmitry Kozlyuk <dmitry.kozl...@gmail.com> --- buildtools/coff.py | 6 ------ buildtools/pmdinfogen.py | 10 ++++++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/buildtools/coff.py b/buildtools/coff.py index 86fb0602b7..a7b6c37e32 100644 --- a/buildtools/coff.py +++ b/buildtools/coff.py @@ -146,9 +146,3 @@ def get_section_data(self, number): def get_string(self, offset): return decode_asciiz(self._strings[offset:]) - - -def decode_asciiz(data): - index = data.find(b'\x00') - end = index if index >= 0 else len(data) - return data[:end].decode() diff --git a/buildtools/pmdinfogen.py b/buildtools/pmdinfogen.py index 7a739ec7d4..8bb59699b8 100755 --- a/buildtools/pmdinfogen.py +++ b/buildtools/pmdinfogen.py @@ -19,6 +19,12 @@ import coff +def decode_asciiz(data): + index = data.find(b'\x00') + end = index if index >= 0 else len(data) + return data[:end].decode() + + class ELFSymbol: def __init__(self, image, symbol): self._image = image @@ -28,7 +34,7 @@ def __init__(self, image, symbol): def string_value(self): size = self._symbol["st_size"] value = self.get_value(0, size) - return value[:-1].decode() if value else "" + return decode_asciiz(value) if value else "" def get_value(self, offset, size): section = self._symbol["st_shndx"] @@ -86,7 +92,7 @@ def get_value(self, offset, size): @property def string_value(self): value = self._symbol.get_value(0) - return coff.decode_asciiz(value) if value else '' + return decode_asciiz(value) if value else "" class COFFImage: -- 2.29.3