coreboot-kgpe-d16/util/cbfstool
Jeremy Compostella 621ccf8a97 cbfstool: Skip relocation entries pointing to undefined symbol
The linker can make relocation entries of a symbol which has a value
of zero point to the undefined symbol entry.  It is permitted since
when the symbol value is zero as the documentation of the relocation
entry `r_info' field states:

"If the index is STN_UNDEF, the undefined symbol index, the relocation
 uses 0 as the symbol value."

The ELF binary does not really have any missing symbols.  It is an
optimization as the symbol points to the undefined symbol because its
value is zero.

A typical way to hit this cbfstool limitation is to define an empty
region using the REGION macro in the linker script.  Here is an
example if we assume `CONFIG_MY_REGION' is set to 0:

    .car.data {
            [...]
	    REGION(my_region, CONFIG_MY_REGION_SIZE)
	    [...]
    }

A region is defined as follow:

    #define REGION_SIZE(name) ((size_t)_##name##_size)

    #define DECLARE_REGION(name)	\
            extern u8 _##name[];	\
            extern u8 _e##name[];	\
            extern u8 _##name##_size[];

So the size of the region is actually the address of the
`_##name##_size' symbol.  Therefore, the `_my_region_size' symbol
address is zero and the linker can make the relocation entry of this
symbol point to the undefined symbol index.

In such a situation, cbfstool hits a segmentation fault when it
attempts to relocate the symbol in `parse_elf_to_xip_stage()'
function.  We resolves this issue by making cbfstool skips relocation
entries pointing to the undefined symbol similarly to the way it skips
relocation relative to absolute symbols.  A symbol which value is zero
can be considered an absolute symbol and therefore should not be
relocated.

Of course, we could argue that we could just prevent the declaration
of an empty region as illustrated in the following example:

    .car.data {
            [...]
	    #if CONFIG_MY_REGION_SIZE > 0
            REGION(my_region, CONFIG_MY_REGION_SIZE)
	    #endif
	    [...]
    }

However, this is not a satisfying solution because:

1. It requires to add unnecessary code in the linker script as an empty
   region is a valid declaration.  Such a workaround requires the code
   using it to mark the region symbols as weak symbols to handle the
   situation where the region is not defined.

2. There could be other situations which have yet to be uncovered which
   would lead the same cbfstool crash.

3. A binary with an empty region is a valid ELF file and cbfstool
   should not crash when it is asked to create an eXecute-In-Place stage
   out of it.

Change-Id: I2803fd3e96e7ff7a0b22d72d50bfbce7acaeb941
Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/77699
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
2023-10-20 14:32:20 +00:00
..
bpdt_formats
console
flashmap util: Use common ARRAY_SIZE define 2023-05-22 19:24:27 +00:00
fpt_formats
lz4
lzma
tests
.gitignore
EXAMPLE
Makefile
Makefile.inc util/cbfstool: Revise lex/yacc (bison) generation 2023-09-28 21:28:37 +00:00
ProcessorBind.h
amdcompress.c
cbfs-mkpayload.c
cbfs-mkstage.c cbfstool: Make add-stage support multiple loadable segments 2023-09-14 21:01:56 +00:00
cbfs-payload-linux.c linux_trampoline: Handle 64-bit framebuffer addresses 2023-08-03 17:55:01 +00:00
cbfs.h
cbfs_glue.h
cbfs_image.c cbfstool: Add comment to define stability rules for cbfstool print -k 2023-04-13 00:43:23 +00:00
cbfs_image.h
cbfs_sections.c
cbfs_sections.h
cbfscomptool.c
cbfstool.c cbfstool: Make add-stage support multiple ignore sections 2023-09-14 21:01:48 +00:00
coff.h
common.c
common.h
compress.c
cse_fpt.c
cse_fpt.h
cse_helpers.c
cse_helpers.h
cse_serger.c
cse_serger.h
default-x86.fmd soc/intel/alderlake/hsphy: Add possibility to cache HSPHY in flash 2023-06-23 08:59:50 +00:00
default.fmd
description.md
elf.h
elfheaders.c
elfparsing.h
elogtool.c
eventlog.c util/cbfstool: Add eventLog support for ELOG_TYPE_FW_SPLASH_SCREEN 2023-08-29 06:15:45 +00:00
eventlog.h
fdt.h
fit.c
fit.h
flashmap_tests.c
flashrom.c util: Drop flashrom -p host alias which equals to -p internal 2023-09-25 14:06:32 +00:00
flashrom.h
fmap_from_fmd.c
fmap_from_fmd.h
fmaptool.c
fmd.c
fmd.h
fmd_parser.c_shipped util/cbfstool: Revise lex/yacc (bison) generation 2023-09-28 21:28:37 +00:00
fmd_parser.h_shipped util/cbfstool: Revise lex/yacc (bison) generation 2023-09-28 21:28:37 +00:00
fmd_parser.y
fmd_scanner.c_shipped util/cbfstool: Revise lex/yacc (bison) generation 2023-09-28 21:28:37 +00:00
fmd_scanner.h_shipped util/cbfstool: Revise lex/yacc (bison) generation 2023-09-28 21:28:37 +00:00
fmd_scanner.l
fv.h
ifittool.c util/cbfstool: Add usage information about verbose output 2023-03-28 13:28:15 +00:00
ifwitool.c util: Use C99 flexible arrays 2023-08-01 12:51:25 +00:00
linux.h util/cbfstool: Qualify struct e820entry as packed 2023-04-16 14:04:32 +00:00
linux_trampoline.S linux_trampoline: Handle 64-bit framebuffer addresses 2023-08-03 17:55:01 +00:00
linux_trampoline.c linux_trampoline: Handle 64-bit framebuffer addresses 2023-08-03 17:55:01 +00:00
linux_trampoline.h
option.h
partitioned_file.c
partitioned_file.h
platform_fixups.c
rmodtool.c
rmodule.c cbfstool: Skip relocation entries pointing to undefined symbol 2023-10-20 14:32:20 +00:00
rmodule.h cbfstool: Make add-stage support multiple loadable segments 2023-09-14 21:01:56 +00:00
xdr.c