1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # Handle various things related to ELF images
8 from collections import namedtuple, OrderedDict
16 # This is enabled from control.py
19 Symbol = namedtuple('Symbol', ['section', 'address', 'size', 'weak'])
22 def GetSymbols(fname, patterns):
23 """Get the symbols from an ELF file
26 fname: Filename of the ELF file to read
27 patterns: List of regex patterns to search for, each a string
30 None, if the file does not exist, or Dict:
32 value: Hex value of symbol
34 stdout = command.Output('objdump', '-t', fname, raise_on_error=False)
35 lines = stdout.splitlines()
37 re_syms = re.compile('|'.join(patterns))
43 if not line or not syms_started:
44 if 'SYMBOL TABLE' in line:
46 line = None # Otherwise code coverage complains about 'continue'
48 if re_syms and not re_syms.search(line):
51 space_pos = line.find(' ')
52 value, rest = line[:space_pos], line[space_pos + 1:]
54 parts = rest[7:].split()
55 section, size = parts[:2]
58 syms[name] = Symbol(section, int(value, 16), int(size,16),
61 # Sort dict by address
62 return OrderedDict(sorted(syms.iteritems(), key=lambda x: x[1].address))
64 def GetSymbolAddress(fname, sym_name):
65 """Get a value of a symbol from an ELF file
68 fname: Filename of the ELF file to read
69 patterns: List of regex patterns to search for, each a string
72 Symbol value (as an integer) or None if not found
74 syms = GetSymbols(fname, [sym_name])
75 sym = syms.get(sym_name)
80 def LookupAndWriteSymbols(elf_fname, entry, section):
81 """Replace all symbols in an entry with their correct values
83 The entry contents is updated so that values for referenced symbols will be
84 visible at run time. This is done by finding out the symbols offsets in the
85 entry (using the ELF file) and replacing them with values from binman's data
89 elf_fname: Filename of ELF image containing the symbol information for
91 entry: Entry to process
92 section: Section which can be used to lookup symbol values
94 fname = tools.GetInputFilename(elf_fname)
95 syms = GetSymbols(fname, ['image', 'binman'])
98 base = syms.get('__image_copy_start')
101 for name, sym in syms.iteritems():
102 if name.startswith('_binman'):
103 msg = ("Section '%s': Symbol '%s'\n in entry '%s'" %
104 (section.GetPath(), name, entry.GetPath()))
105 offset = sym.address - base.address
106 if offset < 0 or offset + sym.size > entry.contents_size:
107 raise ValueError('%s has offset %x (size %x) but the contents '
108 'size is %x' % (entry.GetPath(), offset,
109 sym.size, entry.contents_size))
115 raise ValueError('%s has size %d: only 4 and 8 are supported' %
118 # Look up the symbol in our entry tables.
119 value = section.LookupSymbol(name, sym.weak, msg)
120 if value is not None:
121 value += base.address
124 pack_string = pack_string.lower()
125 value_bytes = struct.pack(pack_string, value)
127 print('%s:\n insert %s, offset %x, value %x, length %d' %
128 (msg, name, offset, value, len(value_bytes)))
129 entry.data = (entry.data[:offset] + value_bytes +
130 entry.data[offset + sym.size:])