cbfstool: Fix segmentation fault for data segment relocation
commit892257ec2187e84c1610d97367ca59270bcbadda
authorJeremy Compostella <jeremy.compostella@intel.com>
Thu, 24 Oct 2024 21:38:52 +0000 (24 14:38 -0700)
committerJérémy Compostella <jeremy.compostella@intel.com>
Fri, 25 Oct 2024 23:01:45 +0000 (25 23:01 +0000)
tree0c792ad30c9d0d8720f2c91b4e13be68fd8389cd
parent87f0224c0a9eb266b44c622713511047404c05c2
cbfstool: Fix segmentation fault for data segment relocation

`cbfstool add-stage' crashes with a segmentation fault when generating
the program binary out of a romstage ELF containing relocation within
the data segment.

This commit makes `parse_elf_to_xip_stage()' look for the segment to
which the current relocation applies and compute the appropriate
location within the program binary.

This issue can be reproduced by defining a global variable with a
pointer to constant data. This variable is defined within the .data
section and contains a pointer to a constant which resides in the
.text section. As a result, a relocation entry is generated in the ELF
file.

    struct my_struct {
            const char *name;
    };

    struct my_struct my_global = { .name = "EXAMPLE" };

    void fun(void)
    {
           printk(BIOS_DEBUG, "my_global.name=%s\n", my_global.name);
    }

TEST=global data structure with a pointer to a constant does not make
     cbfstool crash

Change-Id: I480b4b047546c8aa4e12dfb688e0299f80283235
Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/84864
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Shuo Liu <shuo.liu@intel.com>
Reviewed-by: Wonkyu Kim <wonkyu.kim@intel.com>
util/cbfstool/cbfs-mkstage.c