2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2017-11-14 01:54:54 +00:00
|
|
|
# Copyright (c) 2017 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Test for the elf module
|
|
|
|
|
|
|
|
import os
|
2019-07-08 19:18:34 +00:00
|
|
|
import shutil
|
2021-11-04 03:09:16 +00:00
|
|
|
import struct
|
2017-11-14 01:54:54 +00:00
|
|
|
import sys
|
2019-07-08 19:18:34 +00:00
|
|
|
import tempfile
|
2017-11-14 01:54:54 +00:00
|
|
|
import unittest
|
|
|
|
|
2020-04-18 00:09:03 +00:00
|
|
|
from binman import elf
|
2023-02-24 01:18:04 +00:00
|
|
|
from u_boot_pylib import command
|
|
|
|
from u_boot_pylib import test_util
|
|
|
|
from u_boot_pylib import tools
|
|
|
|
from u_boot_pylib import tout
|
2017-11-14 01:54:54 +00:00
|
|
|
|
|
|
|
binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FakeEntry:
|
2018-07-17 19:25:26 +00:00
|
|
|
"""A fake Entry object, usedfor testing
|
|
|
|
|
|
|
|
This supports an entry with a given size.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def __init__(self, contents_size):
|
|
|
|
self.contents_size = contents_size
|
2022-01-29 21:14:04 +00:00
|
|
|
self.data = tools.get_bytes(ord('a'), contents_size)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
def GetPath(self):
|
|
|
|
return 'entry_path'
|
|
|
|
|
2018-07-17 19:25:26 +00:00
|
|
|
|
2018-06-01 15:38:13 +00:00
|
|
|
class FakeSection:
|
2018-07-17 19:25:26 +00:00
|
|
|
"""A fake Section object, used for testing
|
|
|
|
|
|
|
|
This has the minimum feature set needed to support testing elf functions.
|
|
|
|
A LookupSymbol() function is provided which returns a fake value for amu
|
|
|
|
symbol requested.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def __init__(self, sym_value=1):
|
|
|
|
self.sym_value = sym_value
|
|
|
|
|
|
|
|
def GetPath(self):
|
2018-06-01 15:38:13 +00:00
|
|
|
return 'section_path'
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2021-01-07 04:35:15 +00:00
|
|
|
def LookupImageSymbol(self, name, weak, msg, base_addr):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Fake implementation which returns the same value for all symbols"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
return self.sym_value
|
2017-11-14 01:54:54 +00:00
|
|
|
|
2021-01-07 04:35:15 +00:00
|
|
|
def GetImage(self):
|
|
|
|
return self
|
2018-07-17 19:25:26 +00:00
|
|
|
|
2019-08-24 13:22:53 +00:00
|
|
|
def BuildElfTestFiles(target_dir):
|
|
|
|
"""Build ELF files used for testing in binman
|
|
|
|
|
2022-02-08 18:49:55 +00:00
|
|
|
This compiles and links the test files into the specified directory. It uses
|
|
|
|
the Makefile and source files in the binman test/ directory.
|
2019-08-24 13:22:53 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
target_dir: Directory to put the files into
|
|
|
|
"""
|
|
|
|
if not os.path.exists(target_dir):
|
|
|
|
os.mkdir(target_dir)
|
|
|
|
testdir = os.path.join(binman_dir, 'test')
|
|
|
|
|
|
|
|
# If binman is involved from the main U-Boot Makefile the -r and -R
|
|
|
|
# flags are set in MAKEFLAGS. This prevents this Makefile from working
|
|
|
|
# correctly. So drop any make flags here.
|
|
|
|
if 'MAKEFLAGS' in os.environ:
|
|
|
|
del os.environ['MAKEFLAGS']
|
2021-11-04 03:09:15 +00:00
|
|
|
try:
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.run('make', '-C', target_dir, '-f',
|
2021-11-04 03:09:15 +00:00
|
|
|
os.path.join(testdir, 'Makefile'), 'SRC=%s/' % testdir)
|
|
|
|
except ValueError as e:
|
|
|
|
# The test system seems to suppress this in a strange way
|
|
|
|
print(e)
|
2019-08-24 13:22:53 +00:00
|
|
|
|
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
class TestElf(unittest.TestCase):
|
2018-10-02 03:12:41 +00:00
|
|
|
@classmethod
|
2019-08-24 13:22:54 +00:00
|
|
|
def setUpClass(cls):
|
|
|
|
cls._indir = tempfile.mkdtemp(prefix='elf.')
|
2022-01-29 21:14:04 +00:00
|
|
|
tools.set_input_dirs(['.'])
|
2019-08-24 13:22:54 +00:00
|
|
|
BuildElfTestFiles(cls._indir)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
if cls._indir:
|
|
|
|
shutil.rmtree(cls._indir)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ElfTestFile(cls, fname):
|
|
|
|
return os.path.join(cls._indir, fname)
|
2018-10-02 03:12:41 +00:00
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
def testAllSymbols(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we can obtain a symbol from the ELF file"""
|
2019-08-24 13:22:54 +00:00
|
|
|
fname = self.ElfTestFile('u_boot_ucode_ptr')
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, [])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
|
|
|
|
def testRegexSymbols(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we can obtain from the ELF file by regular expression"""
|
2019-08-24 13:22:54 +00:00
|
|
|
fname = self.ElfTestFile('u_boot_ucode_ptr')
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['ucode'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['missing'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertNotIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
syms = elf.GetSymbols(fname, ['missing', 'ucode'])
|
2022-01-10 03:13:37 +00:00
|
|
|
self.assertIn('_dt_ucode_base_size', syms)
|
2017-11-14 01:54:54 +00:00
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
def testMissingFile(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that a missing file is detected"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols('missing-file', entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
self.assertIn("Filename 'missing-file' not found in input path",
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testOutsideFile(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test a symbol which extends outside the entry area is detected"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
spl: binman: Check at runtime if binman symbols were filled in
Binman lets us declare symbols in SPL/TPL that refer to other entries in
the same binman image as them. These symbols are filled in with the
correct values while binman assembles the images, but this is done
in-memory only. Symbols marked as optional can be filled with
BINMAN_SYM_MISSING as an error value if their referred entry is missing.
However, the unmodified SPL/TPL binaries are still available on disk,
and can be used by people. For these files, nothing ensures that the
symbols are set to this error value, and they will be considered valid
when they are not.
Empirically, all symbols show up as zero in a sandbox_vpl build when we
run e.g. tpl/u-boot-tpl directly. On the other hand, zero is a perfectly
fine value for a binman-written symbol, so we cannot say the symbols
have wrong values based on that.
Declare a magic symbol that binman always fills in with a fixed value.
Check this value as an indicator that symbols were filled in correctly.
Return the error value for all symbols when this magic symbol has the
wrong value.
For binman tests, we need to make room for the new symbol in the mocked
SPL/TPL data by extending them by four bytes. This messes up some test
image layouts. Fix the affected values, and check the magic symbol
wherever it makes sense.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2022-06-18 12:13:11 +00:00
|
|
|
self.assertIn('entry_path has offset 8 (size 8) but the contents size '
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
'is a', str(e.exception))
|
|
|
|
|
|
|
|
def testMissingImageStart(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that we detect a missing __image_copy_start symbol
|
|
|
|
|
|
|
|
This is needed to mark the start of the image. Without it we cannot
|
|
|
|
locate the offset of a binman symbol within the image.
|
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:58 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms_bad')
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
def testBadSymbolSize(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test that an attempt to use an 8-bit symbol are detected
|
|
|
|
|
|
|
|
Only 32 and 64 bits are supported, since we need to store an offset
|
|
|
|
into the image.
|
|
|
|
"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
entry = FakeEntry(10)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:57 +00:00
|
|
|
elf_fname =self.ElfTestFile('u_boot_binman_syms_size')
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
with self.assertRaises(ValueError) as e:
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
self.assertIn('has size 1: only 4 and 8 are supported',
|
|
|
|
str(e.exception))
|
|
|
|
|
|
|
|
def testNoValue(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Test the case where we have no value for the symbol
|
|
|
|
|
|
|
|
This should produce -1 values for all thress symbols, taking up the
|
|
|
|
first 16 bytes of the image.
|
|
|
|
"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
spl: binman: Check at runtime if binman symbols were filled in
Binman lets us declare symbols in SPL/TPL that refer to other entries in
the same binman image as them. These symbols are filled in with the
correct values while binman assembles the images, but this is done
in-memory only. Symbols marked as optional can be filled with
BINMAN_SYM_MISSING as an error value if their referred entry is missing.
However, the unmodified SPL/TPL binaries are still available on disk,
and can be used by people. For these files, nothing ensures that the
symbols are set to this error value, and they will be considered valid
when they are not.
Empirically, all symbols show up as zero in a sandbox_vpl build when we
run e.g. tpl/u-boot-tpl directly. On the other hand, zero is a perfectly
fine value for a binman-written symbol, so we cannot say the symbols
have wrong values based on that.
Declare a magic symbol that binman always fills in with a fixed value.
Check this value as an indicator that symbols were filled in correctly.
Return the error value for all symbols when this magic symbol has the
wrong value.
For binman tests, we need to make room for the new symbol in the mocked
SPL/TPL data by extending them by four bytes. This messes up some test
image layouts. Fix the affected values, and check the magic symbol
wherever it makes sense.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2022-06-18 12:13:11 +00:00
|
|
|
entry = FakeEntry(28)
|
2018-06-01 15:38:13 +00:00
|
|
|
section = FakeSection(sym_value=None)
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
spl: binman: Check at runtime if binman symbols were filled in
Binman lets us declare symbols in SPL/TPL that refer to other entries in
the same binman image as them. These symbols are filled in with the
correct values while binman assembles the images, but this is done
in-memory only. Symbols marked as optional can be filled with
BINMAN_SYM_MISSING as an error value if their referred entry is missing.
However, the unmodified SPL/TPL binaries are still available on disk,
and can be used by people. For these files, nothing ensures that the
symbols are set to this error value, and they will be considered valid
when they are not.
Empirically, all symbols show up as zero in a sandbox_vpl build when we
run e.g. tpl/u-boot-tpl directly. On the other hand, zero is a perfectly
fine value for a binman-written symbol, so we cannot say the symbols
have wrong values based on that.
Declare a magic symbol that binman always fills in with a fixed value.
Check this value as an indicator that symbols were filled in correctly.
Return the error value for all symbols when this magic symbol has the
wrong value.
For binman tests, we need to make room for the new symbol in the mocked
SPL/TPL data by extending them by four bytes. This messes up some test
image layouts. Fix the affected values, and check the magic symbol
wherever it makes sense.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2022-06-18 12:13:11 +00:00
|
|
|
expected = (struct.pack('<L', elf.BINMAN_SYM_MAGIC_VALUE) +
|
|
|
|
tools.get_bytes(255, 20) +
|
|
|
|
tools.get_bytes(ord('a'), 4))
|
|
|
|
self.assertEqual(expected, entry.data)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
|
|
|
def testDebug(self):
|
2018-07-17 19:25:26 +00:00
|
|
|
"""Check that enabling debug in the elf module produced debug output"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
2019-07-20 18:23:36 +00:00
|
|
|
try:
|
2022-01-29 21:14:15 +00:00
|
|
|
tout.init(tout.DEBUG)
|
spl: binman: Check at runtime if binman symbols were filled in
Binman lets us declare symbols in SPL/TPL that refer to other entries in
the same binman image as them. These symbols are filled in with the
correct values while binman assembles the images, but this is done
in-memory only. Symbols marked as optional can be filled with
BINMAN_SYM_MISSING as an error value if their referred entry is missing.
However, the unmodified SPL/TPL binaries are still available on disk,
and can be used by people. For these files, nothing ensures that the
symbols are set to this error value, and they will be considered valid
when they are not.
Empirically, all symbols show up as zero in a sandbox_vpl build when we
run e.g. tpl/u-boot-tpl directly. On the other hand, zero is a perfectly
fine value for a binman-written symbol, so we cannot say the symbols
have wrong values based on that.
Declare a magic symbol that binman always fills in with a fixed value.
Check this value as an indicator that symbols were filled in correctly.
Return the error value for all symbols when this magic symbol has the
wrong value.
For binman tests, we need to make room for the new symbol in the mocked
SPL/TPL data by extending them by four bytes. This messes up some test
image layouts. Fix the affected values, and check the magic symbol
wherever it makes sense.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2022-06-18 12:13:11 +00:00
|
|
|
entry = FakeEntry(24)
|
2019-07-20 18:23:36 +00:00
|
|
|
section = FakeSection()
|
2019-08-24 13:22:56 +00:00
|
|
|
elf_fname = self.ElfTestFile('u_boot_binman_syms')
|
2019-07-20 18:23:36 +00:00
|
|
|
with test_util.capture_sys_output() as (stdout, stderr):
|
2022-02-11 20:23:21 +00:00
|
|
|
elf.LookupAndWriteSymbols(elf_fname, entry, section)
|
2019-07-20 18:23:36 +00:00
|
|
|
self.assertTrue(len(stdout.getvalue()) > 0)
|
|
|
|
finally:
|
2022-01-29 21:14:15 +00:00
|
|
|
tout.init(tout.WARNING)
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2019-07-08 19:18:34 +00:00
|
|
|
def testMakeElf(self):
|
|
|
|
"""Test for the MakeElf function"""
|
|
|
|
outdir = tempfile.mkdtemp(prefix='elf.')
|
|
|
|
expected_text = b'1234'
|
|
|
|
expected_data = b'wxyz'
|
|
|
|
elf_fname = os.path.join(outdir, 'elf')
|
2019-08-24 13:22:45 +00:00
|
|
|
bin_fname = os.path.join(outdir, 'bin')
|
2019-07-08 19:18:34 +00:00
|
|
|
|
|
|
|
# Make an Elf file and then convert it to a fkat binary file. This
|
|
|
|
# should produce the original data.
|
|
|
|
elf.MakeElf(elf_fname, expected_text, expected_data)
|
2022-01-29 21:14:04 +00:00
|
|
|
objcopy, args = tools.get_target_compile_tool('objcopy')
|
binman: Use target-specific tools when cross-compiling
Currently, binman always runs the compile tools like cc, objcopy, strip,
etc. using their literal name. Instead, this patch makes it use the
target-specific versions by default, derived from the tool-specific
environment variables (CC, OBJCOPY, STRIP, etc.) or from the
CROSS_COMPILE environment variable.
For example, the u-boot-elf etype directly uses 'strip'. Trying to run
the tests with 'CROSS_COMPILE=i686-linux-gnu- binman test' on an arm64
host results in the '097_elf_strip.dts' test to fail as the arm64
version of 'strip' can't understand the format of the x86 ELF file.
This also adjusts some command.Output() calls that caused test errors or
failures to use the target versions of the tools they call. After this,
patch, an arm64 host can run all tests with no errors or failures using
a correct CROSS_COMPILE value.
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
2020-09-06 11:46:05 +00:00
|
|
|
args += ['-O', 'binary', elf_fname, bin_fname]
|
2022-01-29 21:14:05 +00:00
|
|
|
stdout = command.output(objcopy, *args)
|
2019-07-08 19:18:34 +00:00
|
|
|
with open(bin_fname, 'rb') as fd:
|
|
|
|
data = fd.read()
|
|
|
|
self.assertEqual(expected_text + expected_data, data)
|
|
|
|
shutil.rmtree(outdir)
|
|
|
|
|
2019-07-08 19:18:35 +00:00
|
|
|
def testDecodeElf(self):
|
|
|
|
"""Test for the MakeElf function"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
outdir = tempfile.mkdtemp(prefix='elf.')
|
|
|
|
expected_text = b'1234'
|
|
|
|
expected_data = b'wxyz'
|
|
|
|
elf_fname = os.path.join(outdir, 'elf')
|
|
|
|
elf.MakeElf(elf_fname, expected_text, expected_data)
|
2022-01-29 21:14:04 +00:00
|
|
|
data = tools.read_file(elf_fname)
|
2019-07-08 19:18:35 +00:00
|
|
|
|
|
|
|
load = 0xfef20000
|
|
|
|
entry = load + 2
|
|
|
|
expected = expected_text + expected_data
|
|
|
|
self.assertEqual(elf.ElfInfo(expected, load, entry, len(expected)),
|
|
|
|
elf.DecodeElf(data, 0))
|
|
|
|
self.assertEqual(elf.ElfInfo(b'\0\0' + expected[2:],
|
|
|
|
load, entry, len(expected)),
|
|
|
|
elf.DecodeElf(data, load + 2))
|
2019-08-24 13:22:54 +00:00
|
|
|
shutil.rmtree(outdir)
|
2019-07-08 19:18:35 +00:00
|
|
|
|
2021-11-04 03:09:16 +00:00
|
|
|
def testEmbedData(self):
|
|
|
|
"""Test for the GetSymbolFileOffset() function"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
|
|
|
|
start = offset['embed_start'].offset
|
|
|
|
end = offset['embed_end'].offset
|
2022-01-29 21:14:04 +00:00
|
|
|
data = tools.read_file(fname)
|
2021-11-04 03:09:16 +00:00
|
|
|
embed_data = data[start:end]
|
2023-01-23 18:29:41 +00:00
|
|
|
expect = struct.pack('<IIIII', 2, 3, 0x1234, 0x5678, 0)
|
2021-11-04 03:09:16 +00:00
|
|
|
self.assertEqual(expect, embed_data)
|
|
|
|
|
|
|
|
def testEmbedFail(self):
|
|
|
|
"""Test calling GetSymbolFileOffset() without elftools"""
|
|
|
|
try:
|
|
|
|
old_val = elf.ELF_TOOLS
|
|
|
|
elf.ELF_TOOLS = False
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end'])
|
2022-03-06 03:18:57 +00:00
|
|
|
self.assertIn("Python: No module named 'elftools'",
|
2021-11-04 03:09:16 +00:00
|
|
|
str(e.exception))
|
|
|
|
finally:
|
|
|
|
elf.ELF_TOOLS = old_val
|
|
|
|
|
|
|
|
def testEmbedDataNoSym(self):
|
|
|
|
"""Test for GetSymbolFileOffset() getting no symbols"""
|
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
offset = elf.GetSymbolFileOffset(fname, ['missing_sym'])
|
|
|
|
self.assertEqual({}, offset)
|
|
|
|
|
2022-03-06 03:18:57 +00:00
|
|
|
def test_read_loadable_segments(self):
|
|
|
|
"""Test for read_loadable_segments()"""
|
2022-02-08 18:49:55 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
2022-03-06 03:18:57 +00:00
|
|
|
segments, entry = elf.read_loadable_segments(tools.read_file(fname))
|
2022-02-08 18:49:55 +00:00
|
|
|
|
|
|
|
def test_read_segments_fail(self):
|
2022-03-06 03:18:57 +00:00
|
|
|
"""Test for read_loadable_segments() without elftools"""
|
2022-02-08 18:49:55 +00:00
|
|
|
try:
|
|
|
|
old_val = elf.ELF_TOOLS
|
|
|
|
elf.ELF_TOOLS = False
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
2022-03-06 03:18:57 +00:00
|
|
|
elf.read_loadable_segments(tools.read_file(fname))
|
|
|
|
self.assertIn("Python: No module named 'elftools'",
|
2022-02-08 18:49:55 +00:00
|
|
|
str(e.exception))
|
|
|
|
finally:
|
|
|
|
elf.ELF_TOOLS = old_val
|
|
|
|
|
|
|
|
def test_read_segments_bad_data(self):
|
2022-03-06 03:18:57 +00:00
|
|
|
"""Test for read_loadable_segments() with an invalid ELF file"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
2022-02-08 18:49:55 +00:00
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
2022-03-06 03:18:57 +00:00
|
|
|
elf.read_loadable_segments(tools.get_bytes(100, 100))
|
2022-02-08 18:49:55 +00:00
|
|
|
self.assertIn('Magic number does not match', str(e.exception))
|
|
|
|
|
2022-03-19 01:19:49 +00:00
|
|
|
def test_get_file_offset(self):
|
|
|
|
"""Test GetFileOffset() gives the correct file offset for a symbol"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
2022-03-19 01:19:49 +00:00
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
syms = elf.GetSymbols(fname, ['embed'])
|
|
|
|
addr = syms['embed'].address
|
|
|
|
offset = elf.GetFileOffset(fname, addr)
|
|
|
|
data = tools.read_file(fname)
|
|
|
|
|
|
|
|
# Just use the first 4 bytes and assume it is little endian
|
|
|
|
embed_data = data[offset:offset + 4]
|
|
|
|
embed_value = struct.unpack('<I', embed_data)[0]
|
|
|
|
self.assertEqual(0x1234, embed_value)
|
|
|
|
|
|
|
|
def test_get_file_offset_fail(self):
|
|
|
|
"""Test calling GetFileOffset() without elftools"""
|
|
|
|
try:
|
|
|
|
old_val = elf.ELF_TOOLS
|
|
|
|
elf.ELF_TOOLS = False
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
elf.GetFileOffset(fname, 0)
|
|
|
|
self.assertIn("Python: No module named 'elftools'",
|
|
|
|
str(e.exception))
|
|
|
|
finally:
|
|
|
|
elf.ELF_TOOLS = old_val
|
|
|
|
|
|
|
|
def test_get_symbol_from_address(self):
|
|
|
|
"""Test GetSymbolFromAddress()"""
|
2022-08-19 14:25:18 +00:00
|
|
|
if not elf.ELF_TOOLS:
|
|
|
|
self.skipTest('Python elftools not available')
|
2022-03-19 01:19:49 +00:00
|
|
|
fname = self.ElfTestFile('elf_sections')
|
|
|
|
sym_name = 'calculate'
|
|
|
|
syms = elf.GetSymbols(fname, [sym_name])
|
|
|
|
addr = syms[sym_name].address
|
|
|
|
sym = elf.GetSymbolFromAddress(fname, addr)
|
|
|
|
self.assertEqual(sym_name, sym)
|
|
|
|
|
|
|
|
def test_get_symbol_from_address_fail(self):
|
|
|
|
"""Test calling GetSymbolFromAddress() without elftools"""
|
|
|
|
try:
|
|
|
|
old_val = elf.ELF_TOOLS
|
|
|
|
elf.ELF_TOOLS = False
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
with self.assertRaises(ValueError) as e:
|
|
|
|
elf.GetSymbolFromAddress(fname, 0x1000)
|
|
|
|
self.assertIn("Python: No module named 'elftools'",
|
|
|
|
str(e.exception))
|
|
|
|
finally:
|
|
|
|
elf.ELF_TOOLS = old_val
|
|
|
|
|
2023-01-07 21:07:13 +00:00
|
|
|
def test_is_valid(self):
|
|
|
|
"""Test is_valid()"""
|
|
|
|
self.assertEqual(False, elf.is_valid(b''))
|
|
|
|
self.assertEqual(False, elf.is_valid(b'1234'))
|
|
|
|
|
|
|
|
fname = self.ElfTestFile('elf_sections')
|
|
|
|
data = tools.read_file(fname)
|
|
|
|
self.assertEqual(True, elf.is_valid(data))
|
|
|
|
self.assertEqual(False, elf.is_valid(data[4:]))
|
|
|
|
|
2023-01-23 18:29:41 +00:00
|
|
|
def test_get_symbol_offset(self):
|
|
|
|
fname = self.ElfTestFile('embed_data')
|
|
|
|
syms = elf.GetSymbols(fname, ['embed_start', 'embed'])
|
|
|
|
expected = syms['embed'].address - syms['embed_start'].address
|
|
|
|
val = elf.GetSymbolOffset(fname, 'embed', 'embed_start')
|
|
|
|
self.assertEqual(expected, val)
|
|
|
|
|
|
|
|
with self.assertRaises(KeyError) as e:
|
|
|
|
elf.GetSymbolOffset(fname, 'embed')
|
|
|
|
self.assertIn('__image_copy_start', str(e.exception))
|
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-14 01:55:01 +00:00
|
|
|
|
2017-11-14 01:54:54 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|