Add a common helper to compute the total size of an ELF64 header
(Ehdr + program headers) from the number of program headers.
Replace open-coded calculations in powerpc, x86, vmcore,
and crash_core.

On ppc64, struct elfhdr maps to elf64_hdr, so the powerpc change
is a pure cleanup.

No functional change intended.

Cc: Madhavan Srinivasan <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Nicholas Piggin <[email protected]>
Cc: "Christophe Leroy (CS GROUP)" <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Pratyush Yadav <[email protected]>
Cc: Dave Young <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Sourabh Jain <[email protected]>
Signed-off-by: Jinjie Ruan <[email protected]>
---
 arch/powerpc/kexec/crash.c                 | 2 +-
 arch/powerpc/platforms/powernv/opal-core.c | 3 +--
 arch/x86/kernel/crash.c                    | 3 +--
 fs/proc/vmcore.c                           | 6 ++----
 include/linux/elf.h                        | 4 ++++
 kernel/crash_core.c                        | 2 +-
 6 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c
index 775895f31037..fc0105c7af4c 100644
--- a/arch/powerpc/kexec/crash.c
+++ b/arch/powerpc/kexec/crash.c
@@ -478,7 +478,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
        if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
                phdr_cnt += CONFIG_CRASH_MAX_MEMORY_RANGES;
 
-       return sizeof(struct elfhdr) + (phdr_cnt * sizeof(Elf64_Phdr));
+       return elf64_phdr_size(phdr_cnt);
 }
 
 /**
diff --git a/arch/powerpc/platforms/powernv/opal-core.c 
b/arch/powerpc/platforms/powernv/opal-core.c
index 32662d30d70f..fc0aad61504b 100644
--- a/arch/powerpc/platforms/powernv/opal-core.c
+++ b/arch/powerpc/platforms/powernv/opal-core.c
@@ -309,8 +309,7 @@ static int __init create_opalcore(void)
        char *bufp;
 
        /* Get size of header & CPU notes for OPAL core */
-       hdr_size = (sizeof(Elf64_Ehdr) +
-                   ((oc_conf->ptload_cnt + 1) * sizeof(Elf64_Phdr)));
+       hdr_size = elf64_phdr_size(oc_conf->ptload_cnt + 1);
        cpu_notes_size = ((oc_conf->num_cpus * (CRASH_CORE_NOTE_HEAD_BYTES +
                          CRASH_CORE_NOTE_NAME_BYTES +
                          CRASH_CORE_NOTE_DESC_BYTES)) +
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index 8f8c0e592849..a3bf786286d4 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -374,8 +374,7 @@ int crash_load_segments(struct kimage *image)
                pnum += 2 + CONFIG_NR_CPUS;
 
        if (pnum < (unsigned long)PN_XNUM) {
-               kbuf.memsz = pnum * sizeof(Elf64_Phdr);
-               kbuf.memsz += sizeof(Elf64_Ehdr);
+               kbuf.memsz = elf64_phdr_size(pnum);
 
                image->elfcorehdr_index = image->nr_segments;
 
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index 44d15436439f..ff324969d798 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -1238,8 +1238,7 @@ static int __init parse_crash_elf64_headers(void)
        }
 
        /* Read in all elf headers. */
-       elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
-                               ehdr.e_phnum * sizeof(Elf64_Phdr);
+       elfcorebuf_sz_orig = elf64_phdr_size(ehdr.e_phnum);
        elfcorebuf_sz = elfcorebuf_sz_orig;
        elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
                                              get_order(elfcorebuf_sz_orig));
@@ -1605,8 +1604,7 @@ static int vmcore_add_device_ram_elf64(struct list_head 
*list, size_t count)
        }
 
        /* elfcorebuf_sz must always cover full pages. */
-       new_size = sizeof(Elf64_Ehdr) +
-                  (ehdr->e_phnum + count) * sizeof(Elf64_Phdr);
+       new_size = elf64_phdr_size(ehdr->e_phnum + count);
        new_size = roundup(new_size, PAGE_SIZE);
 
        /*
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 5c402788da19..400f58a13d92 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -109,4 +109,8 @@ static inline int arch_elf_adjust_prot(int prot,
 }
 #endif
 
+static inline unsigned long elf64_phdr_size(unsigned long phdr_cnt)
+{
+       return phdr_cnt * sizeof(Elf64_Phdr) + sizeof(Elf64_Ehdr);
+}
 #endif /* _LINUX_ELF_H */
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 05a2a8be083d..bd3f82b62751 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -193,7 +193,7 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int 
need_kernel_map,
         */
 
        nr_phdr++;
-       elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
+       elf_sz = elf64_phdr_size(nr_phdr);
        elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
 
        buf = vzalloc(elf_sz);
-- 
2.34.1


Reply via email to