diff options
author | Simon Glass | 2022-10-20 18:22:44 -0600 |
---|---|---|
committer | Tom Rini | 2022-10-31 11:01:31 -0400 |
commit | 28565796df769e0a8d5ab1f8424cb262e40edc2e (patch) | |
tree | 479190d4d1f48f38a49fcc7be3899df1db47df1f /tools | |
parent | 6e55b114aa0b8feff4e7a15a98842f9eaa87407d (diff) |
binman: Allow obtaining a symbol value
Provide a function to obtain the integer value of an ELF symbol. This will
be used
Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/binman/elf.py | 53 |
1 files 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 |