1 # Copyright (c) 2016 Google, Inc
2 # Written by Simon Glass <sjg@chromium.org>
4 # SPDX-License-Identifier: GPL-2.0+
6 # Handle various things related to ELF images
9 from collections import namedtuple, OrderedDict
17 Symbol = namedtuple('Symbol', ['section', 'address', 'size', 'weak'])
19 # Used for tests which don't have an ELF file to read
20 ignore_missing_files = False
23 def GetSymbols(fname, patterns):
24 """Get the symbols from an ELF file
27 fname: Filename of the ELF file to read
28 patterns: List of regex patterns to search for, each a string
31 None, if the file does not exist, or Dict:
33 value: Hex value of symbol
35 stdout = command.Output('objdump', '-t', fname, raise_on_error=False)
36 lines = stdout.splitlines()
38 re_syms = re.compile('|'.join(patterns))
44 if not line or not syms_started:
45 if 'SYMBOL TABLE' in line:
47 line = None # Otherwise code coverage complains about 'continue'
49 if re_syms and not re_syms.search(line):
52 space_pos = line.find(' ')
53 value, rest = line[:space_pos], line[space_pos + 1:]
55 parts = rest[7:].split()
56 section, size = parts[:2]
59 syms[name] = Symbol(section, int(value, 16), int(size,16),
63 def GetSymbolAddress(fname, sym_name):
64 """Get a value of a symbol from an ELF file
67 fname: Filename of the ELF file to read
68 patterns: List of regex patterns to search for, each a string
71 Symbol value (as an integer) or None if not found
73 syms = GetSymbols(fname, [sym_name])
74 sym = syms.get(sym_name)