From 3a89ad0d55c0297ed5478dd049e476534d1316a3 Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Wed, 7 Feb 2024 19:43:04 +0500 Subject: [PATCH] elf loader: The rproc elf loading problem The elf loader assumes that the last ELF program segment will always be a LOAD type segment. I deduce this from the fact that the elf_load() function, when loading the remote ELF sections during the RPROC_LOADER_READY_TO_LOAD stage, compares the last load segment num to the total ELF sections to determine if the loading is complete and it can move to the next stage RPROC_LOADER_POST_DATA_LOAD. If the last program segment in the ELF is not of type LOAD, the last loaded LOAD segment never equals total ELF sections. This creates an error condition and the firmware loading fails. This patch fixes this issue by comparing the last loaded LOAD segment number with the max LOAD segment number in the ELF. Signed-off-by: Umair Khan --- lib/include/openamp/elf_loader.h | 2 ++ lib/remoteproc/elf_loader.c | 50 ++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/include/openamp/elf_loader.h b/lib/include/openamp/elf_loader.h index a2950f8d7..5842adde9 100644 --- a/lib/include/openamp/elf_loader.h +++ b/lib/include/openamp/elf_loader.h @@ -290,6 +290,7 @@ struct elf32_info { Elf32_Phdr *phdrs; Elf32_Shdr *shdrs; void *shstrtab; + int max_load_phnum; }; struct elf64_info { @@ -298,6 +299,7 @@ struct elf64_info { Elf64_Phdr *phdrs; Elf64_Shdr *shdrs; void *shstrtab; + int max_load_phnum; }; #define ELF_STATE_INIT 0x0L diff --git a/lib/remoteproc/elf_loader.c b/lib/remoteproc/elf_loader.c index c90b8d46b..f21069ff2 100644 --- a/lib/remoteproc/elf_loader.c +++ b/lib/remoteproc/elf_loader.c @@ -161,6 +161,23 @@ static void **elf_shstrtab_ptr(void *elf_info) } } +static int *elf_max_load_phnum(void *elf_info) +{ + int *max_load_phnum; + + if (elf_is_64(elf_info) == 0) { + struct elf32_info *einfo = elf_info; + + max_load_phnum = &einfo->max_load_phnum; + } else { + struct elf64_info *einfo = elf_info; + + max_load_phnum = &einfo->max_load_phnum; + } + + return max_load_phnum; +} + static int *elf_load_state(void *elf_info) { if (elf_is_64(elf_info) == 0) { @@ -374,6 +391,28 @@ static const void *elf_next_load_segment(void *elf_info, int *nseg, return phdr; } +static void elf_find_max_load_phnum(void *elf_info) +{ + const void *phdr = PT_NULL; + unsigned int p_type = PT_NULL; + int nseg = 0; + int *max_load_seg; + + max_load_seg = elf_max_load_phnum(elf_info); + phdr = elf_get_segment_from_index(elf_info, nseg); + while (phdr != PT_NULL) { + elf_parse_segment(elf_info, phdr, &p_type, NULL, + NULL, NULL, NULL, NULL); + /* Return an incremented segment number for compatibility with + * elf_next_load_segment() + */ + nseg++; + if (p_type == PT_LOAD) + *max_load_seg = nseg; + phdr = elf_get_segment_from_index(elf_info, nseg); + } +} + static size_t elf_info_size(const void *img_data) { if (elf_is_64(img_data) == 0) @@ -453,6 +492,7 @@ int elf_load_header(const void *img_data, size_t offset, size_t len, if (!*phdrs) return -RPROC_ENOMEM; memcpy(*phdrs, img_phdrs, phdrs_size); + elf_find_max_load_phnum(*img_info); *load_state = ELF_STATE_WAIT_FOR_SHDRS | RPROC_LOADER_READY_TO_LOAD; } @@ -566,7 +606,7 @@ int elf_load(struct remoteproc *rproc, int nsegment; size_t nsegmsize = 0; size_t nsize = 0; - int phnums = 0; + int *max_load_phnum = 0; nsegment = *load_state & ELF_NEXT_SEGMENT_MASK; phdr = elf_next_load_segment(*img_info, &nsegment, da, @@ -579,10 +619,10 @@ int elf_load(struct remoteproc *rproc, } *nlen = nsize; *nmemsize = nsegmsize; - phnums = elf_phnum(*img_info); - metal_log(METAL_LOG_DEBUG, "segment: %d, total segs %d\r\n", - nsegment, phnums); - if (nsegment == phnums) { + max_load_phnum = elf_max_load_phnum(*img_info); + metal_log(METAL_LOG_DEBUG, "segment: %d, last load seg %d\r\n", + nsegment, *max_load_phnum); + if (nsegment == *max_load_phnum) { *load_state = (*load_state & (~RPROC_LOADER_MASK)) | RPROC_LOADER_POST_DATA_LOAD; }