cbfstool: Make add-stage support multiple loadable segments

For x86 eXecute-In-Place (XIP) pre-memory `.data` section support, we
have to use an extra segment as the VMA/LMA of the data is different
than the VMA/LMA of the code.

To support this requirement, this patch makes cbfstool:
1. Allow the load of an ELF with an extra segment
2. Makes add-stage for XIP (cf. parse_elf_to_xip_stage()) write its
   content to the output binary.

To prevent the creation of unsuitable binaries, cbfstool verifies that
the LMA addresses of the segments are consecutives.

TEST=XIP pre-memory stages with a `.data` section have the `.data`
     section covered by a second segment properly included right after
     the code.

Change-Id: I480b4b047546c8aa4e12dfb688e0299f80283234
Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/77584
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Jeremy Compostella
2023-08-30 15:35:46 -07:00
committed by Julius Werner
parent c9cae530e5
commit 79f2e1fc8b
3 changed files with 87 additions and 21 deletions

View File

@ -247,13 +247,13 @@ static int find_program_segment(struct rmod_context *ctx)
for (i = 0; i < pelf->ehdr.e_phnum; i++) {
if (pelf->phdr[i].p_type != PT_LOAD)
continue;
phdr = &pelf->phdr[i];
if (!phdr)
phdr = &pelf->phdr[i];
nsegments++;
}
if (nsegments != 1) {
ERROR("Unexpected number of loadable segments: %d.\n",
nsegments);
if (nsegments == 0) {
ERROR("No loadable segment found.\n");
return -1;
}
@ -262,6 +262,7 @@ static int find_program_segment(struct rmod_context *ctx)
(long long)phdr->p_memsz);
ctx->phdr = phdr;
ctx->nsegments = nsegments;
return 0;
}
@ -269,18 +270,17 @@ static int find_program_segment(struct rmod_context *ctx)
static int
filter_relocation_sections(struct rmod_context *ctx)
{
int i;
int i, j;
const char *shstrtab;
struct parsed_elf *pelf;
const Elf64_Phdr *phdr;
pelf = &ctx->pelf;
phdr = ctx->phdr;
shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
/*
* Find all relocation sections that contain relocation entries
* for sections that fall within the bounds of the segment. For
* for sections that fall within the bounds of the segments. For
* easier processing the pointer to the relocation array for the
* sections that don't fall within the loadable program are NULL'd
* out.
@ -319,11 +319,18 @@ filter_relocation_sections(struct rmod_context *ctx)
continue;
}
if (shdr->sh_addr < phdr->p_vaddr ||
((shdr->sh_addr + shdr->sh_size) >
(phdr->p_vaddr + phdr->p_memsz))) {
for (j = 0; j < pelf->ehdr.e_phnum; j++) {
phdr = &pelf->phdr[j];
if (phdr->p_type == PT_LOAD &&
shdr->sh_addr >= phdr->p_vaddr &&
((shdr->sh_addr + shdr->sh_size) <=
(phdr->p_vaddr + phdr->p_memsz)))
break;
}
if (j == pelf->ehdr.e_phnum) {
ERROR("Relocations being applied to section %d not "
"within segment region.\n", sh_info);
"within segments region.\n", sh_info);
pelf->relocs[i] = NULL;
return -1;
}
}
@ -488,6 +495,11 @@ write_elf(const struct rmod_context *ctx, const struct buffer *in,
Elf64_Addr addr;
Elf64_Ehdr ehdr;
if (ctx->nsegments != 1) {
ERROR("Multiple loadable segments is not supported.\n");
return -1;
}
bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
/*