Skip to contentSkip to conversation

common: elf: use proper field to get segment memory size

PATCH Applied Denis Orlov 2 participants 2 messages Mar 15, 2023 – Mar 16, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common: elf: use proper field to get segment memory size

For a program header, the value of 'p_filesz' may be less than the one of 'p_memsz', in which case the leftover amount of space should be filled with zeroes. This is done correctly when loading elf to memory. However, when we are requesting memory beforehand we do pass the value of 'p_filesz', instead of 'p_memsz', to 'request_sdram_region', resulting in potentially smaller amount of memory to be reserved.

As 'p_memsz' is always greater or equal to 'p_filesz', use only the former for checking memory requirements in 'request_elf_segment'.

---
 common/elf.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
common/elf.c +3 -4
index eec62cad61..c9b3d7bb37 100644
@@ -59,14 +59,13 @@ static int request_elf_segment(struct elf_image *elf, void *phdr)
 {
 	void *dst = (void *) (phys_addr_t) elf_phdr_p_paddr(elf, phdr);
 	int ret;
-	u64 p_filesz = elf_phdr_p_filesz(elf, phdr);
 	u64 p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
 	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!p_filesz)
+	if (!p_memsz)
 		return 0;
 
 	if (dst < elf->low_addr)
@@ -74,9 +73,9 @@ static int request_elf_segment(struct elf_image *elf, void *phdr)
 	if (dst + p_memsz > elf->high_addr)
 		elf->high_addr = dst + p_memsz;
 
-	pr_debug("Requesting segment 0x%p (%llu bytes)\n", dst, p_filesz);
+	pr_debug("Requesting segment 0x%p (%llu bytes)\n", dst, p_memsz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz, phdr);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_memsz, phdr);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.30.2
Applied
#1 lore raw
Quoted 7 lines · Denis Orlov

For a program header, the value of 'p_filesz' may be less than the one of 'p_memsz', in which case the leftover amount of space should be filled with zeroes. This is done correctly when loading elf to memory. However, when we are requesting memory beforehand we do pass the value of 'p_filesz', instead of 'p_memsz', to 'request_sdram_region', resulting in potentially smaller amount of memory to be reserved.

Quoted 7 lines

As 'p_memsz' is always greater or equal to 'p_filesz', use only the former for checking memory requirements in 'request_elf_segment'.

---
 common/elf.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Applied, thanks

Sascha

Quoted 37 lines
common/elf.c +3 -4
index eec62cad61..c9b3d7bb37 100644
@@ -59,14 +59,13 @@ static int request_elf_segment(struct elf_image *elf, void *phdr)
 {
 	void *dst = (void *) (phys_addr_t) elf_phdr_p_paddr(elf, phdr);
 	int ret;
-	u64 p_filesz = elf_phdr_p_filesz(elf, phdr);
 	u64 p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
 	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!p_filesz)
+	if (!p_memsz)
 		return 0;
 
 	if (dst < elf->low_addr)
@@ -74,9 +73,9 @@ static int request_elf_segment(struct elf_image *elf, void *phdr)
 	if (dst + p_memsz > elf->high_addr)
 		elf->high_addr = dst + p_memsz;
 
-	pr_debug("Requesting segment 0x%p (%llu bytes)\n", dst, p_filesz);
+	pr_debug("Requesting segment 0x%p (%llu bytes)\n", dst, p_memsz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz, phdr);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_memsz, phdr);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.30.2
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |