Skip to contentSkip to conversation

lib: elf: Move the generic elf loading/validating functions to lib

PATCH Superseded Keerthy 1 participant 1 message Jan 29, 2020

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Keerthy

author
#0 lore raw

PATCH v6 2/10 lib: elf: Move the generic elf loading/validating functions to lib

Move the generic elf loading/validating functions to lib/ so that they can be re-used and accessed by code existing outside cmd.

While at it remove the duplicate static version of load_elf_image_phdr under arch/arm/mach-imx/imx_bootaux.c.

---

Changes in v6:

* Fixed a build issue due to duplicate static version of load_elf_image_phdr under arch/arm/mach-imx/imx_bootaux.c

 arch/arm/mach-imx/imx_bootaux.c |  46 ------
 cmd/Kconfig                     |   1 +
 cmd/elf.c                       | 229 ----------------------------
 include/elf.h                   |   4 +
 lib/Kconfig                     |   6 +
 lib/Makefile                    |   1 +
 lib/elf.c                       | 256 ++++++++++++++++++++++++++++++++
 7 files changed, 268 insertions(+), 275 deletions(-)
 create mode 100644 lib/elf.c
arch/arm/mach-imx/imx_bootaux.c +0 -46
index 21e96f8c88..8092268a3b 100644
@@ -28,52 +28,6 @@ static const struct rproc_att *get_host_mapping(unsigned long auxcore)
 
 	return NULL;
 }
-
-/*
- * A very simple elf loader, assumes the image is valid, returns the
- * entry point address.
- */
-static unsigned long load_elf_image_phdr(unsigned long addr)
-{
-	Elf32_Ehdr *ehdr; /* ELF header structure pointer */
-	Elf32_Phdr *phdr; /* Program header structure pointer */
-	int i;
-
-	ehdr = (Elf32_Ehdr *)addr;
-	phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
-
-	/* Load each program header */
-	for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
-		const struct rproc_att *mmap = get_host_mapping(phdr->p_paddr);
-		void *dst, *src;
-
-		if (phdr->p_type != PT_LOAD)
-			continue;
-
-		if (!mmap) {
-			printf("Invalid aux core address: %08x",
-			       phdr->p_paddr);
-			return 0;
-		}
-
-		dst = (void *)(phdr->p_paddr - mmap->da) + mmap->sa;
-		src = (void *)addr + phdr->p_offset;
-
-		debug("Loading phdr %i to 0x%p (%i bytes)\n",
-		      i, dst, phdr->p_filesz);
-
-		if (phdr->p_filesz)
-			memcpy(dst, src, phdr->p_filesz);
-		if (phdr->p_filesz != phdr->p_memsz)
-			memset(dst + phdr->p_filesz, 0x00,
-			       phdr->p_memsz - phdr->p_filesz);
-		flush_cache((unsigned long)dst &
-			    ~(CONFIG_SYS_CACHELINE_SIZE - 1),
-			    ALIGN(phdr->p_filesz, CONFIG_SYS_CACHELINE_SIZE));
-	}
-
-	return ehdr->e_entry;
-}
 #endif
 
 int arch_auxiliary_core_up(u32 core_id, ulong addr)
cmd/Kconfig +1 -0
index e6ba57035e..9819dd1aaf 100644
@@ -399,6 +399,7 @@ config CMD_ABOOTIMG
 config CMD_ELF
 	bool "bootelf, bootvx"
 	default y
+	select LIB_ELF
 	help
 	  Boot an ELF/vxWorks image from the memory.
 
cmd/elf.c +0 -229

241 lines not on this page · raw message

include/elf.h +4 -0
index 81f40191d7..e7c51986df 100644
@@ -692,6 +692,10 @@ unsigned long elf_hash(const unsigned char *name);
 
 #ifndef __ASSEMBLER__
 int valid_elf_image(unsigned long addr);
+unsigned long load_elf64_image_phdr(unsigned long addr);
+unsigned long load_elf64_image_shdr(unsigned long addr);
+unsigned long load_elf_image_phdr(unsigned long addr);
+unsigned long load_elf_image_shdr(unsigned long addr);
 #endif
 
 #endif /* _ELF_H */
lib/Kconfig +6 -0
index ab6aff710d..452f390c80 100644
@@ -602,4 +602,10 @@ config TEST_FDTDEC
 config LIB_DATE
 	bool
 
+config LIB_ELF
+	bool
+	help
+	  Supoort basic elf loading/validating functions.
+	  This supports fir 32 bit and 64 bit versions.
+
 endmenu
lib/Makefile +1 -0
index 15259d0473..32bf3f3693 100644
@@ -122,6 +122,7 @@ obj-y += vsprintf.o strto.o
 endif
 
 obj-y += date.o
+obj-$(CONFIG_LIB_ELF) += elf.o
 
 #
 # Build a fast OID lookup registry from include/linux/oid_registry.h
lib/elf.c new file +256 -0

256 lines not on this page · raw message

Signature · 1 line
2.17.1