Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 5 | # Handle various things related to ELF images |
| 6 | # |
| 7 | |
| 8 | from collections import namedtuple, OrderedDict |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 9 | import io |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 10 | import os |
| 11 | import re |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 12 | import shutil |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 13 | import struct |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 14 | import tempfile |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 15 | |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 16 | from patman import command |
| 17 | from patman import tools |
| 18 | from patman import tout |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 19 | |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 20 | ELF_TOOLS = True |
| 21 | try: |
| 22 | from elftools.elf.elffile import ELFFile |
Simon Glass | 4d38dd7 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 23 | from elftools.elf.elffile import ELFError |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 24 | from elftools.elf.sections import SymbolTableSection |
| 25 | except: # pragma: no cover |
| 26 | ELF_TOOLS = False |
| 27 | |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 28 | # BSYM in little endian, keep in sync with include/binman_sym.h |
| 29 | BINMAN_SYM_MAGIC_VALUE = 0x4d595342 |
| 30 | |
Simon Glass | 056f0ef | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 31 | # Information about an EFL symbol: |
| 32 | # section (str): Name of the section containing this symbol |
| 33 | # address (int): Address of the symbol (its value) |
| 34 | # size (int): Size of the symbol in bytes |
| 35 | # weak (bool): True if the symbol is weak |
| 36 | # offset (int or None): Offset of the symbol's data in the ELF file, or None if |
| 37 | # not known |
| 38 | Symbol = namedtuple('Symbol', ['section', 'address', 'size', 'weak', 'offset']) |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 39 | |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 40 | # Information about an ELF file: |
| 41 | # data: Extracted program contents of ELF file (this would be loaded by an |
| 42 | # ELF loader when reading this file |
| 43 | # load: Load address of code |
| 44 | # entry: Entry address of code |
| 45 | # memsize: Number of bytes in memory occupied by loading this ELF file |
| 46 | ElfInfo = namedtuple('ElfInfo', ['data', 'load', 'entry', 'memsize']) |
| 47 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 48 | |
| 49 | def GetSymbols(fname, patterns): |
| 50 | """Get the symbols from an ELF file |
| 51 | |
| 52 | Args: |
| 53 | fname: Filename of the ELF file to read |
| 54 | patterns: List of regex patterns to search for, each a string |
| 55 | |
| 56 | Returns: |
| 57 | None, if the file does not exist, or Dict: |
| 58 | key: Name of symbol |
| 59 | value: Hex value of symbol |
| 60 | """ |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 61 | stdout = tools.run('objdump', '-t', fname) |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 62 | lines = stdout.splitlines() |
| 63 | if patterns: |
| 64 | re_syms = re.compile('|'.join(patterns)) |
| 65 | else: |
| 66 | re_syms = None |
| 67 | syms = {} |
| 68 | syms_started = False |
| 69 | for line in lines: |
| 70 | if not line or not syms_started: |
| 71 | if 'SYMBOL TABLE' in line: |
| 72 | syms_started = True |
| 73 | line = None # Otherwise code coverage complains about 'continue' |
| 74 | continue |
| 75 | if re_syms and not re_syms.search(line): |
| 76 | continue |
| 77 | |
| 78 | space_pos = line.find(' ') |
| 79 | value, rest = line[:space_pos], line[space_pos + 1:] |
| 80 | flags = rest[:7] |
| 81 | parts = rest[7:].split() |
| 82 | section, size = parts[:2] |
| 83 | if len(parts) > 2: |
Simon Glass | 39c8e47 | 2019-08-24 07:22:46 -0600 | [diff] [blame] | 84 | name = parts[2] if parts[2] != '.hidden' else parts[3] |
Simon Glass | 056f0ef | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 85 | syms[name] = Symbol(section, int(value, 16), int(size, 16), |
| 86 | flags[1] == 'w', None) |
| 87 | |
| 88 | # Sort dict by address |
| 89 | return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address)) |
| 90 | |
Simon Glass | 64defba | 2022-03-04 08:42:59 -0700 | [diff] [blame] | 91 | def _GetFileOffset(elf, addr): |
| 92 | """Get the file offset for an address |
| 93 | |
| 94 | Args: |
| 95 | elf (ELFFile): ELF file to check |
| 96 | addr (int): Address to search for |
| 97 | |
| 98 | Returns |
| 99 | int: Offset of that address in the ELF file, or None if not valid |
| 100 | """ |
| 101 | for seg in elf.iter_segments(): |
| 102 | seg_end = seg['p_vaddr'] + seg['p_filesz'] |
| 103 | if seg.header['p_type'] == 'PT_LOAD': |
| 104 | if addr >= seg['p_vaddr'] and addr < seg_end: |
| 105 | return addr - seg['p_vaddr'] + seg['p_offset'] |
| 106 | |
| 107 | def GetFileOffset(fname, addr): |
| 108 | """Get the file offset for an address |
| 109 | |
| 110 | Args: |
| 111 | fname (str): Filename of ELF file to check |
| 112 | addr (int): Address to search for |
| 113 | |
| 114 | Returns |
| 115 | int: Offset of that address in the ELF file, or None if not valid |
| 116 | """ |
| 117 | if not ELF_TOOLS: |
Simon Glass | 40def8a | 2022-03-18 19:19:49 -0600 | [diff] [blame] | 118 | raise ValueError("Python: No module named 'elftools'") |
Simon Glass | 64defba | 2022-03-04 08:42:59 -0700 | [diff] [blame] | 119 | with open(fname, 'rb') as fd: |
| 120 | elf = ELFFile(fd) |
| 121 | return _GetFileOffset(elf, addr) |
| 122 | |
| 123 | def GetSymbolFromAddress(fname, addr): |
| 124 | """Get the symbol at a particular address |
| 125 | |
| 126 | Args: |
| 127 | fname (str): Filename of ELF file to check |
| 128 | addr (int): Address to search for |
| 129 | |
| 130 | Returns: |
| 131 | str: Symbol name, or None if no symbol at that address |
| 132 | """ |
| 133 | if not ELF_TOOLS: |
Simon Glass | 40def8a | 2022-03-18 19:19:49 -0600 | [diff] [blame] | 134 | raise ValueError("Python: No module named 'elftools'") |
Simon Glass | 64defba | 2022-03-04 08:42:59 -0700 | [diff] [blame] | 135 | with open(fname, 'rb') as fd: |
| 136 | elf = ELFFile(fd) |
| 137 | syms = GetSymbols(fname, None) |
| 138 | for name, sym in syms.items(): |
| 139 | if sym.address == addr: |
| 140 | return name |
| 141 | |
Simon Glass | 056f0ef | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 142 | def GetSymbolFileOffset(fname, patterns): |
| 143 | """Get the symbols from an ELF file |
| 144 | |
| 145 | Args: |
| 146 | fname: Filename of the ELF file to read |
| 147 | patterns: List of regex patterns to search for, each a string |
| 148 | |
| 149 | Returns: |
| 150 | None, if the file does not exist, or Dict: |
| 151 | key: Name of symbol |
| 152 | value: Hex value of symbol |
| 153 | """ |
Simon Glass | 056f0ef | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 154 | if not ELF_TOOLS: |
Simon Glass | 17b4ffc | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 155 | raise ValueError("Python: No module named 'elftools'") |
Simon Glass | 056f0ef | 2021-11-03 21:09:16 -0600 | [diff] [blame] | 156 | |
| 157 | syms = {} |
| 158 | with open(fname, 'rb') as fd: |
| 159 | elf = ELFFile(fd) |
| 160 | |
| 161 | re_syms = re.compile('|'.join(patterns)) |
| 162 | for section in elf.iter_sections(): |
| 163 | if isinstance(section, SymbolTableSection): |
| 164 | for symbol in section.iter_symbols(): |
| 165 | if not re_syms or re_syms.search(symbol.name): |
| 166 | addr = symbol.entry['st_value'] |
| 167 | syms[symbol.name] = Symbol( |
| 168 | section.name, addr, symbol.entry['st_size'], |
| 169 | symbol.entry['st_info']['bind'] == 'STB_WEAK', |
| 170 | _GetFileOffset(elf, addr)) |
Simon Glass | 46d61a2 | 2018-07-17 13:25:24 -0600 | [diff] [blame] | 171 | |
| 172 | # Sort dict by address |
Simon Glass | 5097915 | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 173 | return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address)) |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 174 | |
| 175 | def GetSymbolAddress(fname, sym_name): |
| 176 | """Get a value of a symbol from an ELF file |
| 177 | |
| 178 | Args: |
| 179 | fname: Filename of the ELF file to read |
| 180 | patterns: List of regex patterns to search for, each a string |
| 181 | |
| 182 | Returns: |
| 183 | Symbol value (as an integer) or None if not found |
| 184 | """ |
| 185 | syms = GetSymbols(fname, [sym_name]) |
| 186 | sym = syms.get(sym_name) |
| 187 | if not sym: |
| 188 | return None |
| 189 | return sym.address |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 190 | |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 191 | def LookupAndWriteSymbols(elf_fname, entry, section): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 192 | """Replace all symbols in an entry with their correct values |
| 193 | |
| 194 | The entry contents is updated so that values for referenced symbols will be |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 195 | visible at run time. This is done by finding out the symbols offsets in the |
| 196 | entry (using the ELF file) and replacing them with values from binman's data |
| 197 | structures. |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 198 | |
| 199 | Args: |
| 200 | elf_fname: Filename of ELF image containing the symbol information for |
| 201 | entry |
| 202 | entry: Entry to process |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 203 | section: Section which can be used to lookup symbol values |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 204 | """ |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 205 | fname = tools.get_input_filename(elf_fname) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 206 | syms = GetSymbols(fname, ['image', 'binman']) |
| 207 | if not syms: |
| 208 | return |
| 209 | base = syms.get('__image_copy_start') |
| 210 | if not base: |
| 211 | return |
Simon Glass | 5097915 | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 212 | for name, sym in syms.items(): |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 213 | if name.startswith('_binman'): |
Simon Glass | f55382b | 2018-06-01 09:38:13 -0600 | [diff] [blame] | 214 | msg = ("Section '%s': Symbol '%s'\n in entry '%s'" % |
| 215 | (section.GetPath(), name, entry.GetPath())) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 216 | offset = sym.address - base.address |
| 217 | if offset < 0 or offset + sym.size > entry.contents_size: |
| 218 | raise ValueError('%s has offset %x (size %x) but the contents ' |
| 219 | 'size is %x' % (entry.GetPath(), offset, |
| 220 | sym.size, entry.contents_size)) |
| 221 | if sym.size == 4: |
| 222 | pack_string = '<I' |
| 223 | elif sym.size == 8: |
| 224 | pack_string = '<Q' |
| 225 | else: |
| 226 | raise ValueError('%s has size %d: only 4 and 8 are supported' % |
| 227 | (msg, sym.size)) |
| 228 | |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 229 | if name == '_binman_sym_magic': |
| 230 | value = BINMAN_SYM_MAGIC_VALUE |
| 231 | else: |
| 232 | # Look up the symbol in our entry tables. |
| 233 | value = section.GetImage().LookupImageSymbol(name, sym.weak, |
| 234 | msg, base.address) |
Simon Glass | 15c981c | 2019-10-20 21:31:34 -0600 | [diff] [blame] | 235 | if value is None: |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 236 | value = -1 |
| 237 | pack_string = pack_string.lower() |
| 238 | value_bytes = struct.pack(pack_string, value) |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 239 | tout.debug('%s:\n insert %s, offset %x, value %x, length %d' % |
Simon Glass | 9f297b0 | 2019-07-20 12:23:36 -0600 | [diff] [blame] | 240 | (msg, name, offset, value, len(value_bytes))) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 241 | entry.data = (entry.data[:offset] + value_bytes + |
| 242 | entry.data[offset + sym.size:]) |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 243 | |
| 244 | def MakeElf(elf_fname, text, data): |
| 245 | """Make an elf file with the given data in a single section |
| 246 | |
| 247 | The output file has a several section including '.text' and '.data', |
| 248 | containing the info provided in arguments. |
| 249 | |
| 250 | Args: |
| 251 | elf_fname: Output filename |
| 252 | text: Text (code) to put in the file's .text section |
| 253 | data: Data to put in the file's .data section |
| 254 | """ |
| 255 | outdir = tempfile.mkdtemp(prefix='binman.elf.') |
| 256 | s_file = os.path.join(outdir, 'elf.S') |
| 257 | |
| 258 | # Spilt the text into two parts so that we can make the entry point two |
| 259 | # bytes after the start of the text section |
Simon Glass | 6a4ccad | 2020-11-08 20:36:19 -0700 | [diff] [blame] | 260 | text_bytes1 = ['\t.byte\t%#x' % byte for byte in text[:2]] |
| 261 | text_bytes2 = ['\t.byte\t%#x' % byte for byte in text[2:]] |
| 262 | data_bytes = ['\t.byte\t%#x' % byte for byte in data] |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 263 | with open(s_file, 'w') as fd: |
| 264 | print('''/* Auto-generated C program to produce an ELF file for testing */ |
| 265 | |
| 266 | .section .text |
| 267 | .code32 |
| 268 | .globl _start |
| 269 | .type _start, @function |
| 270 | %s |
| 271 | _start: |
| 272 | %s |
| 273 | .ident "comment" |
| 274 | |
| 275 | .comm fred,8,4 |
| 276 | |
| 277 | .section .empty |
| 278 | .globl _empty |
| 279 | _empty: |
| 280 | .byte 1 |
| 281 | |
| 282 | .globl ernie |
| 283 | .data |
| 284 | .type ernie, @object |
| 285 | .size ernie, 4 |
| 286 | ernie: |
| 287 | %s |
| 288 | ''' % ('\n'.join(text_bytes1), '\n'.join(text_bytes2), '\n'.join(data_bytes)), |
| 289 | file=fd) |
| 290 | lds_file = os.path.join(outdir, 'elf.lds') |
| 291 | |
| 292 | # Use a linker script to set the alignment and text address. |
| 293 | with open(lds_file, 'w') as fd: |
| 294 | print('''/* Auto-generated linker script to produce an ELF file for testing */ |
| 295 | |
| 296 | PHDRS |
| 297 | { |
| 298 | text PT_LOAD ; |
| 299 | data PT_LOAD ; |
| 300 | empty PT_LOAD FLAGS ( 6 ) ; |
| 301 | note PT_NOTE ; |
| 302 | } |
| 303 | |
| 304 | SECTIONS |
| 305 | { |
| 306 | . = 0xfef20000; |
| 307 | ENTRY(_start) |
| 308 | .text . : SUBALIGN(0) |
| 309 | { |
| 310 | *(.text) |
| 311 | } :text |
| 312 | .data : { |
| 313 | *(.data) |
| 314 | } :data |
| 315 | _bss_start = .; |
| 316 | .empty : { |
| 317 | *(.empty) |
| 318 | } :empty |
Simon Glass | 9d44a7e | 2019-08-24 07:22:45 -0600 | [diff] [blame] | 319 | /DISCARD/ : { |
| 320 | *(.note.gnu.property) |
| 321 | } |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 322 | .note : { |
| 323 | *(.comment) |
| 324 | } :note |
| 325 | .bss _bss_start (OVERLAY) : { |
| 326 | *(.bss) |
| 327 | } |
| 328 | } |
| 329 | ''', file=fd) |
| 330 | # -static: Avoid requiring any shared libraries |
| 331 | # -nostdlib: Don't link with C library |
| 332 | # -Wl,--build-id=none: Don't generate a build ID, so that we just get the |
| 333 | # text section at the start |
| 334 | # -m32: Build for 32-bit x86 |
| 335 | # -T...: Specifies the link script, which sets the start address |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 336 | cc, args = tools.get_target_compile_tool('cc') |
Alper Nebi Yasak | 1e4687a | 2020-09-06 14:46:05 +0300 | [diff] [blame] | 337 | args += ['-static', '-nostdlib', '-Wl,--build-id=none', '-m32', '-T', |
| 338 | lds_file, '-o', elf_fname, s_file] |
Simon Glass | d980069 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 339 | stdout = command.output(cc, *args) |
Simon Glass | f58558a | 2019-07-08 13:18:34 -0600 | [diff] [blame] | 340 | shutil.rmtree(outdir) |
Simon Glass | d8d4074 | 2019-07-08 13:18:35 -0600 | [diff] [blame] | 341 | |
| 342 | def DecodeElf(data, location): |
| 343 | """Decode an ELF file and return information about it |
| 344 | |
| 345 | Args: |
| 346 | data: Data from ELF file |
| 347 | location: Start address of data to return |
| 348 | |
| 349 | Returns: |
| 350 | ElfInfo object containing information about the decoded ELF file |
| 351 | """ |
| 352 | file_size = len(data) |
| 353 | with io.BytesIO(data) as fd: |
| 354 | elf = ELFFile(fd) |
| 355 | data_start = 0xffffffff; |
| 356 | data_end = 0; |
| 357 | mem_end = 0; |
| 358 | virt_to_phys = 0; |
| 359 | |
| 360 | for i in range(elf.num_segments()): |
| 361 | segment = elf.get_segment(i) |
| 362 | if segment['p_type'] != 'PT_LOAD' or not segment['p_memsz']: |
| 363 | skipped = 1 # To make code-coverage see this line |
| 364 | continue |
| 365 | start = segment['p_paddr'] |
| 366 | mend = start + segment['p_memsz'] |
| 367 | rend = start + segment['p_filesz'] |
| 368 | data_start = min(data_start, start) |
| 369 | data_end = max(data_end, rend) |
| 370 | mem_end = max(mem_end, mend) |
| 371 | if not virt_to_phys: |
| 372 | virt_to_phys = segment['p_paddr'] - segment['p_vaddr'] |
| 373 | |
| 374 | output = bytearray(data_end - data_start) |
| 375 | for i in range(elf.num_segments()): |
| 376 | segment = elf.get_segment(i) |
| 377 | if segment['p_type'] != 'PT_LOAD' or not segment['p_memsz']: |
| 378 | skipped = 1 # To make code-coverage see this line |
| 379 | continue |
| 380 | start = segment['p_paddr'] |
| 381 | offset = 0 |
| 382 | if start < location: |
| 383 | offset = location - start |
| 384 | start = location |
| 385 | # A legal ELF file can have a program header with non-zero length |
| 386 | # but zero-length file size and a non-zero offset which, added |
| 387 | # together, are greater than input->size (i.e. the total file size). |
| 388 | # So we need to not even test in the case that p_filesz is zero. |
| 389 | # Note: All of this code is commented out since we don't have a test |
| 390 | # case for it. |
| 391 | size = segment['p_filesz'] |
| 392 | #if not size: |
| 393 | #continue |
| 394 | #end = segment['p_offset'] + segment['p_filesz'] |
| 395 | #if end > file_size: |
| 396 | #raise ValueError('Underflow copying out the segment. File has %#x bytes left, segment end is %#x\n', |
| 397 | #file_size, end) |
| 398 | output[start - data_start:start - data_start + size] = ( |
| 399 | segment.data()[offset:]) |
| 400 | return ElfInfo(output, data_start, elf.header['e_entry'] + virt_to_phys, |
| 401 | mem_end - data_start) |
Simon Glass | 0427bed | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 402 | |
| 403 | def UpdateFile(infile, outfile, start_sym, end_sym, insert): |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 404 | tout.notice("Creating file '%s' with data length %#x (%d) between symbols '%s' and '%s'" % |
Simon Glass | 0427bed | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 405 | (outfile, len(insert), len(insert), start_sym, end_sym)) |
| 406 | syms = GetSymbolFileOffset(infile, [start_sym, end_sym]) |
| 407 | if len(syms) != 2: |
| 408 | raise ValueError("Expected two symbols '%s' and '%s': got %d: %s" % |
| 409 | (start_sym, end_sym, len(syms), |
| 410 | ','.join(syms.keys()))) |
| 411 | |
| 412 | size = syms[end_sym].offset - syms[start_sym].offset |
| 413 | if len(insert) > size: |
| 414 | raise ValueError("Not enough space in '%s' for data length %#x (%d); size is %#x (%d)" % |
| 415 | (infile, len(insert), len(insert), size, size)) |
| 416 | |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 417 | data = tools.read_file(infile) |
Simon Glass | 0427bed | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 418 | newdata = data[:syms[start_sym].offset] |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 419 | newdata += insert + tools.get_bytes(0, size - len(insert)) |
Simon Glass | 0427bed | 2021-11-03 21:09:18 -0600 | [diff] [blame] | 420 | newdata += data[syms[end_sym].offset:] |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 421 | tools.write_file(outfile, newdata) |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 422 | tout.info('Written to offset %#x' % syms[start_sym].offset) |
Simon Glass | 4d38dd7 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 423 | |
Simon Glass | 17b4ffc | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 424 | def read_loadable_segments(data): |
Simon Glass | 4d38dd7 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 425 | """Read segments from an ELF file |
| 426 | |
| 427 | Args: |
| 428 | data (bytes): Contents of file |
| 429 | |
| 430 | Returns: |
| 431 | tuple: |
| 432 | list of segments, each: |
| 433 | int: Segment number (0 = first) |
| 434 | int: Start address of segment in memory |
| 435 | bytes: Contents of segment |
| 436 | int: entry address for image |
| 437 | |
| 438 | Raises: |
| 439 | ValueError: elftools is not available |
| 440 | """ |
| 441 | if not ELF_TOOLS: |
Simon Glass | 17b4ffc | 2022-03-05 20:18:57 -0700 | [diff] [blame] | 442 | raise ValueError("Python: No module named 'elftools'") |
Simon Glass | 4d38dd7 | 2022-02-08 11:49:55 -0700 | [diff] [blame] | 443 | with io.BytesIO(data) as inf: |
| 444 | try: |
| 445 | elf = ELFFile(inf) |
| 446 | except ELFError as err: |
| 447 | raise ValueError(err) |
| 448 | entry = elf.header['e_entry'] |
| 449 | segments = [] |
| 450 | for i in range(elf.num_segments()): |
| 451 | segment = elf.get_segment(i) |
| 452 | if segment['p_type'] != 'PT_LOAD' or not segment['p_memsz']: |
| 453 | skipped = 1 # To make code-coverage see this line |
| 454 | continue |
| 455 | start = segment['p_offset'] |
| 456 | rend = start + segment['p_filesz'] |
| 457 | segments.append((i, segment['p_paddr'], data[start:rend])) |
| 458 | return segments, entry |