Skip to contentSkip to conversation

lib: move phdr increment to for loop heading

PATCH In review Maxim Moskalets 3 participants 3 messages Aug 17, 2024 – Aug 28, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: move phdr increment to for loop heading

Shifting this pointer in the loop will be more logical when working with the code later, because you can see at a glance what exactly changes at each iteration. Moreover, the code remains equivalent because this variable is not used after the loop.

---
 lib/elf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
lib/elf.c +2 -4
index dc13935e103..205724b71dc 100644
@@ -86,7 +86,7 @@ unsigned long load_elf64_image_phdr(unsigned long addr)
 	phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
 
 	/* Load each program header */
-	for (i = 0; i < ehdr->e_phnum; ++i) {
+	for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
 		void *dst = (void *)(ulong)phdr->p_paddr;
 		void *src = (void *)addr + phdr->p_offset;
 
@@ -99,7 +99,6 @@ unsigned long load_elf64_image_phdr(unsigned long addr)
 			       phdr->p_memsz - phdr->p_filesz);
 		flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
 			    roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
-		++phdr;
 	}
 
 	if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
@@ -201,7 +200,7 @@ unsigned long load_elf_image_phdr(unsigned long addr)
 	phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
 
 	/* Load each program header */
-	for (i = 0; i < ehdr->e_phnum; ++i) {
+	for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
 		void *dst = (void *)(uintptr_t)phdr->p_paddr;
 		void *src = (void *)addr + phdr->p_offset;
 
@@ -214,7 +213,6 @@ unsigned long load_elf_image_phdr(unsigned long addr)
 			       phdr->p_memsz - phdr->p_filesz);
 		flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
 			    roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
-		++phdr;
 	}
 
 	return ehdr->e_entry;
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hi Maxim,

Quoted 11 lines · Maxim Moskalets

Shifting this pointer in the loop will be more logical when working with the code later, because you can see at a glance what exactly changes at each iteration. Moreover, the code remains equivalent because this variable is not used after the loop.

---
 lib/elf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

BTW we normally use post-increment in loops.

Quoted 39 lines
lib/elf.c +2 -5
index dc13935e103..205724b71dc 100644
@@ -86,7 +86,7 @@ unsigned long load_elf64_image_phdr(unsigned long addr)
        phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
 
        /* Load each program header */
-       for (i = 0; i < ehdr->e_phnum; ++i) {
+       for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
                void *dst = (void *)(ulong)phdr->p_paddr;
                void *src = (void *)addr + phdr->p_offset;
 
@@ -99,7 +99,6 @@ unsigned long load_elf64_image_phdr(unsigned long addr)
                               phdr->p_memsz - phdr->p_filesz);
                flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
                            roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
-               ++phdr;
        }
 
        if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
@@ -201,7 +200,7 @@ unsigned long load_elf_image_phdr(unsigned long addr)
        phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
 
        /* Load each program header */
-       for (i = 0; i < ehdr->e_phnum; ++i) {
+       for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
                void *dst = (void *)(uintptr_t)phdr->p_paddr;
                void *src = (void *)addr + phdr->p_offset;
 
@@ -214,7 +213,6 @@ unsigned long load_elf_image_phdr(unsigned long addr)
                               phdr->p_memsz - phdr->p_filesz);
                flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
                            roundup(phdr->p_memsz, ARCH_DMA_MINALIGN));
-               ++phdr;
        }
 
        return ehdr->e_entry;
--
Quoted 2 lines

2.39.2

Regards,
Simon

#2 lore raw
In reply to Maxim Moskalets
Quoted 6 lines · Maxim Moskalets

Shifting this pointer in the loop will be more logical when working with the code later, because you can see at a glance what exactly changes at each iteration. Moreover, the code remains equivalent because this variable is not used after the loop.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom