
Provide a function to obtain the integer value of an ELF symbol. This will be used
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v1)
tools/binman/elf.py | 53 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-)
diff --git a/tools/binman/elf.py b/tools/binman/elf.py index 6d440ddf21d..fec2116880b 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -188,6 +188,28 @@ def GetSymbolAddress(fname, sym_name): return None return sym.address
+def GetPackString(sym, msg): + """Get the struct.pack/unpack string to use with a given symbol + + Args: + sym (Symbol): Symbol to check. Only the size member is checked + @msg (str): String which indicates the entry being processed, used for + errors + + Returns: + str: struct string to use, .e.g. '<I' + + Raises: + ValueError: Symbol has an unexpected size + """ + if sym.size == 4: + return '<I' + elif sym.size == 8: + return '<Q' + else: + raise ValueError('%s has size %d: only 4 and 8 are supported' % + (msg, sym.size)) + def LookupAndWriteSymbols(elf_fname, entry, section): """Replace all symbols in an entry with their correct values
@@ -218,14 +240,7 @@ def LookupAndWriteSymbols(elf_fname, entry, section): raise ValueError('%s has offset %x (size %x) but the contents ' 'size is %x' % (entry.GetPath(), offset, sym.size, entry.contents_size)) - if sym.size == 4: - pack_string = '<I' - elif sym.size == 8: - pack_string = '<Q' - else: - raise ValueError('%s has size %d: only 4 and 8 are supported' % - (msg, sym.size)) - + pack_string = GetPackString(sym, msg) if name == '_binman_sym_magic': value = BINMAN_SYM_MAGIC_VALUE else: @@ -241,6 +256,28 @@ def LookupAndWriteSymbols(elf_fname, entry, section): entry.data = (entry.data[:offset] + value_bytes + entry.data[offset + sym.size:])
+def GetSymbolValue(sym, data, msg): + """Get the value of a symbol + + This can only be used on symbols with an integer value. + + Args: + sym (Symbol): Symbol to check + data (butes): Data for the ELF file - the symbol data appears at offset + sym.offset + @msg (str): String which indicates the entry being processed, used for + errors + + Returns: + int: Value of the symbol + + Raises: + ValueError: Symbol has an unexpected size + """ + pack_string = GetPackString(sym, msg) + value = struct.unpack(pack_string, data[sym.offset:sym.offset + sym.size]) + return value[0] + def MakeElf(elf_fname, text, data): """Make an elf file with the given data in a single section