Skip to contentSkip to conversation

elf: add 64 bits elf support

PATCH Superseded Clément Leger 3 participants 12 messages Mar 18, 2019 – Sep 6, 2019 MIPS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 elf: add 64 bits elf support

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +23 -23
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +76,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +90,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +124,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +27 -0
index 92c8d9c12..189a34566 100644
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 6 lines
2.15.0.276.g89ea799

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hi Clément,

Quoted 36 lines · Clément Leger

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +4 -3
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */

common/elf.c static void elf_release_regions(struct elf_image *elf) · line 61

Quoted 10 lines
common/elf.c +3 -3
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);

%lu for p_filesz?

Quoted 19 lines
common/elf.c +8 -0
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}

common/elf.c struct elf_image { · line 427

Quoted 10 lines
common/elf.c +10 -0
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)

When it's always unsigned long why do we have to pass in the type as an argument?

I am undecided if this is the right approach. "unsigned long" is wrong when a ELF file for a foreign architecture is loaded. This can happen for example when code for the Cortex M4 cores is loaded from the 64bit Cortex A cores is loaded on an i.MX8 for example. Using the bigger types then is not a problem, but maybe it could happen the other way round, loading a 64bit ELF on a 32bit architecture?

I can't see a real problem here, I just wanted to note. Are there other opinions?

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Hi Sascha,

Quoted 43 lines

Hi Clément,

Quoted 40 lines · Clément Leger

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +5 -4
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
Quoted 8 lines

common/elf.c static void elf_release_regions(struct elf_image *elf) · line 61

Quoted 6 lines
common/elf.c +2 -2
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);

%lu for p_filesz?

Indeed, I missed this one.

Quoted 26 lines
Quoted 26 lines
common/elf.c +13 -0
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) {
 \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
Quoted 8 lines

common/elf.c struct elf_image { · line 428

Quoted 5 lines
common/elf.c +5 -0
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)

When it's always unsigned long why do we have to pass in the type as an argument?

Actually, some of them should not be defined as I did.
For instance, the e_type is an half in both elf32 and elf64 so it should be defined as u16.

Some other approaches to handle both 64bits/32bits elf were to copy the whole loading code and s/elf32/elf64. Since the code in barebox is not so big, maybe I could do that.

Quoted 7 lines

I am undecided if this is the right approach. "unsigned long" is wrong when a ELF file for a foreign architecture is loaded. This can happen for example when code for the Cortex M4 cores is loaded from the 64bit Cortex A cores is loaded on an i.MX8 for example. Using the bigger types then is not a problem, but maybe it could happen the other way round, loading a 64bit ELF on a 32bit architecture?

I was thinking about this one. I tried loading 32bit and 64bit elf from a 64bit core but indeed, not the other way. If so, then addresses will be truncated but since the processor will not be able to access a 64 bits memory space, I guess it's not possible (unless you have some DMA which can access the upper memory but this will probably not be handled by barebox elf loader).

Quoted 11 lines

I can't see a real problem here, I just wanted to note. Are there other opinions?

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#3 lore raw

Here is a V2 which uses correct type for elf header access macros (instead of simply unsigned long). Moreover types used are now of fixed size type (u64 instead of unsigned long). This could potentially allow a 32bit barebox to load a 64bit elf using some custom hardware which support 64bit addressing (DMA or such thing).

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 45 +++++++++++++++++++++++----------------------
 include/elf.h | 29 ++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
 	void *buf;
 };
 
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 1 line
2.15.0.276.g89ea799
Quoted 126 lines

Hi Sascha,

Quoted 43 lines

Hi Clément,

Quoted 40 lines · Clément Leger

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +5 -4
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
Quoted 8 lines

common/elf.c static void elf_release_regions(struct elf_image *elf) · line 61

Quoted 6 lines
common/elf.c +2 -2
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);

%lu for p_filesz?

Indeed, I missed this one.

Quoted 26 lines
Quoted 26 lines
common/elf.c +13 -0
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) {
 \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
Quoted 8 lines

common/elf.c struct elf_image { · line 428

Quoted 5 lines
common/elf.c +5 -0
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)

When it's always unsigned long why do we have to pass in the type as an argument?

Actually, some of them should not be defined as I did.
For instance, the e_type is an half in both elf32 and elf64 so it should be defined as u16.

Some other approaches to handle both 64bits/32bits elf were to copy the whole loading code and s/elf32/elf64. Since the code in barebox is not so big, maybe I could do that.

Quoted 7 lines

I am undecided if this is the right approach. "unsigned long" is wrong when a ELF file for a foreign architecture is loaded. This can happen for example when code for the Cortex M4 cores is loaded from the 64bit Cortex A cores is loaded on an i.MX8 for example. Using the bigger types then is not a problem, but maybe it could happen the other way round, loading a 64bit ELF on a 32bit architecture?

I was thinking about this one. I tried loading 32bit and 64bit elf from a 64bit core but indeed, not the other way. If so, then addresses will be truncated but since the processor will not be able to access a 64 bits memory space, I guess it's not possible (unless you have some DMA which can access the upper memory but this will probably not be handled by barebox elf loader).

Quoted 11 lines

I can't see a real problem here, I just wanted to note. Are there other opinions?

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Quoted 4 lines

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#4 lore raw

Hi Clément,

I guess this version looks good. Could you resend with a Signed-off-by tag?

Sascha

Quoted 305 lines · Clément Leger

Here is a V2 which uses correct type for elf header access macros (instead of simply unsigned long). Moreover types used are now of fixed size type (u64 instead of unsigned long). This could potentially allow a 32bit barebox to load a 64bit elf using some custom hardware which support 64bit addressing (DMA or such thing).

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 45 +++++++++++++++++++++++----------------------
 include/elf.h | 29 ++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
 	void *buf;
 };
 
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 1 line
2.15.0.276.g89ea799
Quoted 130 lines

Hi Sascha,

Quoted 43 lines

Hi Clément,

Quoted 40 lines · Clément Leger

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +5 -4
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
Quoted 8 lines

common/elf.c static void elf_release_regions(struct elf_image *elf)

Quoted 6 lines
common/elf.c +2 -2
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);

%lu for p_filesz?

Indeed, I missed this one.

Quoted 26 lines
Quoted 26 lines
common/elf.c +13 -0
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) {
 \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
Quoted 8 lines

common/elf.c struct elf_image { · line 428

Quoted 5 lines
common/elf.c +5 -0
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)

When it's always unsigned long why do we have to pass in the type as an argument?

Actually, some of them should not be defined as I did.
For instance, the e_type is an half in both elf32 and elf64 so it should be defined as u16.

Some other approaches to handle both 64bits/32bits elf were to copy the whole loading code and s/elf32/elf64. Since the code in barebox is not so big, maybe I could do that.

Quoted 7 lines

I am undecided if this is the right approach. "unsigned long" is wrong when a ELF file for a foreign architecture is loaded. This can happen for example when code for the Cortex M4 cores is loaded from the 64bit Cortex A cores is loaded on an i.MX8 for example. Using the bigger types then is not a problem, but maybe it could happen the other way round, loading a 64bit ELF on a 32bit architecture?

I was thinking about this one. I tried loading 32bit and 64bit elf from a 64bit core but indeed, not the other way. If so, then addresses will be truncated but since the processor will not be able to access a 64 bits memory space, I guess it's not possible (unless you have some DMA which can access the upper memory but this will probably not be handled by barebox elf loader).

Quoted 11 lines

I can't see a real problem here, I just wanted to note. Are there other opinions?

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

Hi Sascha,

I will try to compile it in 32bits also (only tried 64bits). I'm guessing it will spit out some warning about type sizes when casting so I will resend a V3.

Thanks,

Clément

----- Mail original -----
De: "Sascha Hauer" <[email protected]>
À: "Clément Leger" <[email protected]>
Cc: "Barebox List" <[email protected]>
Envoyé: Mercredi 27 Mars 2019 08:48:51
Objet: Re: [PATCH] elf: add 64 bits elf support

Hi Clément,

I guess this version looks good. Could you resend with a Signed-off-by tag?

Sascha

Quoted 305 lines · Clément Leger

Here is a V2 which uses correct type for elf header access macros (instead of simply unsigned long). Moreover types used are now of fixed size type (u64 instead of unsigned long). This could potentially allow a 32bit barebox to load a 64bit elf using some custom hardware which support 64bit addressing (DMA or such thing).

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 45 +++++++++++++++++++++++----------------------
 include/elf.h | 29 ++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
 	void *buf;
 };
 
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 1 line
2.15.0.276.g89ea799
Quoted 130 lines

Hi Sascha,

Quoted 43 lines

Hi Clément,

Quoted 40 lines · Clément Leger

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
 common/elf.c  | 46 +++++++++++++++++++++++-----------------------
 include/elf.h | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 23 deletions(-)
common/elf.c +5 -4
index 8edf38856..bfb878954 100644
@@ -43,31 +43,32 @@ static void elf_release_regions(struct elf_image *elf)
 	}
 }
 
-
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) elf_phdr_p_paddr(elf, phdr);
 	int ret;
+	unsigned long p_filesz = elf_phdr_p_filesz(elf, phdr);
+	unsigned long p_memsz = elf_phdr_p_memsz(elf, phdr);
 
 	/* we care only about PT_LOAD segments */
-	if (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
Quoted 8 lines

common/elf.c static void elf_release_regions(struct elf_image *elf)

Quoted 6 lines
common/elf.c +2 -2
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%ld bytes)\n", dst, p_filesz);

%lu for p_filesz?

Indeed, I missed this one.

Quoted 26 lines
Quoted 26 lines
common/elf.c +13 -0
@@ -400,6 +400,7 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
+	unsigned long class;
 	unsigned long entry;
 	void *buf;
 };
@@ -407,4 +408,30 @@ struct elf_image {
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) {
 \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, unsigned long)
+ELF_GET_FIELD(hdr, e_phnum, unsigned long)
+ELF_GET_FIELD(hdr, e_phoff, unsigned long)
+ELF_GET_FIELD(hdr, e_type, unsigned long)
Quoted 8 lines

common/elf.c struct elf_image { · line 428

Quoted 5 lines
common/elf.c +5 -0
+ELF_GET_FIELD(phdr, p_paddr, unsigned long)
+ELF_GET_FIELD(phdr, p_filesz, unsigned long)
+ELF_GET_FIELD(phdr, p_memsz, unsigned long)
+ELF_GET_FIELD(phdr, p_type, unsigned long)
+ELF_GET_FIELD(phdr, p_offset, unsigned long)

When it's always unsigned long why do we have to pass in the type as an argument?

Actually, some of them should not be defined as I did.
For instance, the e_type is an half in both elf32 and elf64 so it should be defined as u16.

Some other approaches to handle both 64bits/32bits elf were to copy the whole loading code and s/elf32/elf64. Since the code in barebox is not so big, maybe I could do that.

Quoted 7 lines

I am undecided if this is the right approach. "unsigned long" is wrong when a ELF file for a foreign architecture is loaded. This can happen for example when code for the Cortex M4 cores is loaded from the 64bit Cortex A cores is loaded on an i.MX8 for example. Using the bigger types then is not a problem, but maybe it could happen the other way round, loading a 64bit ELF on a 32bit architecture?

I was thinking about this one. I tried loading 32bit and 64bit elf from a 64bit core but indeed, not the other way. If so, then addresses will be truncated but since the processor will not be able to access a 64 bits memory space, I guess it's not possible (unless you have some DMA which can access the upper memory but this will probably not be handled by barebox elf loader).

Quoted 11 lines

I can't see a real problem here, I just wanted to note. Are there other opinions?

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH v3 1/1 elf: add 64 bits elf loading support

In reply to Sascha Hauer

From: Clement Leger <[email protected]>

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. THis allows to keep a common loader code even if types are different.

---
V3:
- Compiled on ARM with 32bit toolchain (no warning)
- Add Signed-off-by.

 common/elf.c  | 45 +++++++++++++++++++++++----------------------
 include/elf.h | 29 ++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
 	void *buf;
 };
 
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 7 lines
2.15.0.276.g89ea799


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Tested-by on v3 1/1
#7 lore raw

Re: [PATCH v3] elf: add 64 bits elf loading support

Hi Clement,

thank you for your patch. I tested it on MIPS32 system.

I get this compile warning:
arch/mips/lib/bootm.c: In function 'do_bootm_elf':
arch/mips/lib/bootm.c:75:10: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

  entry = (void *)elf->entry;
          ^

Kernel boot is working.

Quoted 160 lines · Clement Leger

From: Clement Leger <[email protected]>

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. THis allows to keep a common loader code even if types are different.

---
V3:
- Compiled on ARM with 32bit toolchain (no warning)
- Add Signed-off-by.

  common/elf.c  | 45 +++++++++++++++++++++++----------------------
  include/elf.h | 29 ++++++++++++++++++++++++++++-
  2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
  static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
  {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
  		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
  		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
  	if (ret)
  		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
  	return 0;
  }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
  static int load_elf_image_phdr(struct elf_image *elf)
  {
  	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
  	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
  		ret = load_elf_phdr_segment(elf, src, phdr);
  		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
  		if (ret)
  			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
  	}
 
  	return 0;
  }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
  {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
  		pr_err("ELF magic not found.\n");
  		return -EINVAL;
  	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
  		pr_err("Non EXEC ELF image.\n");
  		return -ENOEXEC;
  	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
  	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
  	if (ret)
  		return ERR_PTR(ret);
 
include/elf.h +19 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
  struct elf_image {
  	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
  	void *buf;
  };
 
  struct elf_image *elf_load_image(void *buf);
  void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)

include/elf.h static inline void arch_write_notes(struct file *file) { } · line 437

Quoted 10 lines
include/elf.h +9 -0
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
  #endif /* _LINUX_ELF_H */

Kind regards,
Oleksij Rempel

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

Re: [PATCH v3] elf: add 64 bits elf loading support

Hi Oleksij,

Thanks for testing, since I only tested on arm, it explain this problem :) I will try to search for all elf->entry usage and fix that according to architectures. Thanks,

Clément

Quoted 192 lines · ----- On 2 Sep, 2019, at 08:07, Oleksij Rempel [email protected] wrote:

Hi Clement,

thank you for your patch. I tested it on MIPS32 system.

I get this compile warning:
arch/mips/lib/bootm.c: In function 'do_bootm_elf':
arch/mips/lib/bootm.c:75:10: warning: cast to pointer from integer of different size
[-Wint-to-pointer-cast]

  entry = (void *)elf->entry;
          ^

Kernel boot is working.

Quoted 170 lines · Clement Leger

From: Clement Leger <[email protected]>

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. THis allows to keep a common loader code even if types are different.

---
V3:
- Compiled on ARM with 32bit toolchain (no warning)
- Add Signed-off-by.

  common/elf.c  | 45 +++++++++++++++++++++++----------------------
  include/elf.h | 29 ++++++++++++++++++++++++++++-
  2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +16 -15
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
  static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
  {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
  		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
  		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
  	if (ret)
  		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
  	return 0;
  }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void
 *src,
  static int load_elf_image_phdr(struct elf_image *elf)
  {
  	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
  	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
  		ret = load_elf_phdr_segment(elf, src, phdr);

/* in case of error elf_load_image() caller should clean up and

common/elf.c +7 -7
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
  		if (ret)
  			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
  	}
 
  	return 0;
  }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
  {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
  		pr_err("ELF magic not found.\n");
  		return -EINVAL;
  	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
  		pr_err("Non EXEC ELF image.\n");
  		return -ENOEXEC;
  	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
  	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
  	if (ret)
  		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
  struct elf_image {
  	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
  	void *buf;
  };
 
  struct elf_image *elf_load_image(void *buf);
  void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) {
 \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
Quoted 2 lines
  #endif /* _LINUX_ELF_H */

Kind regards,
Oleksij Rempel

Quoted 5 lines

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#9 lore raw

PATCH v4 1/2 elf: add 64 bits elf loading support

In reply to Oleksij Rempel

From: Clement Leger <[email protected]>

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

---
Changes in V4:
- Fix mips elf entry cast to pointer
- Add Tested-by Oleksij Rempel

 common/elf.c  | 45 +++++++++++++++++++++++----------------------
 include/elf.h | 29 ++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 23 deletions(-)
common/elf.c +23 -22
index 8edf38856..4733accb0 100644
@@ -45,29 +45,31 @@ static void elf_release_regions(struct elf_image *elf)
 
 
 static int load_elf_phdr_segment(struct elf_image *elf, void *src,
-				 Elf32_Phdr *phdr)
+				 void *phdr)
 {
-	void *dst = (void *)phdr->p_paddr;
+	void *dst = (void *) 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 (phdr->p_type != PT_LOAD)
+	if (elf_phdr_p_type(elf, phdr) != PT_LOAD)
 		return 0;
 
-	if (!phdr->p_filesz)
+	if (!p_filesz)
 		return 0;
 
-	pr_debug("Loading phdr to 0x%p (%i bytes)\n", dst, phdr->p_filesz);
+	pr_debug("Loading phdr to 0x%p (%llu bytes)\n", dst, p_filesz);
 
-	ret = elf_request_region(elf, (resource_size_t)dst, phdr->p_filesz);
+	ret = elf_request_region(elf, (resource_size_t)dst, p_filesz);
 	if (ret)
 		return ret;
 
-	memcpy(dst, src, phdr->p_filesz);
+	memcpy(dst, src, p_filesz);
 
-	if (phdr->p_filesz < phdr->p_memsz)
-		memset(dst + phdr->p_filesz, 0x00,
-		       phdr->p_memsz - phdr->p_filesz);
+	if (p_filesz < p_memsz)
+		memset(dst + p_filesz, 0x00,
+		       p_memsz - p_filesz);
 
 	return 0;
 }
@@ -75,14 +77,13 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,
 static int load_elf_image_phdr(struct elf_image *elf)
 {
 	void *buf = elf->buf;
-	Elf32_Ehdr *ehdr = buf;
-	Elf32_Phdr *phdr = (Elf32_Phdr *)(buf + ehdr->e_phoff);
+	void *phdr = (void *) (buf + elf_hdr_e_phoff(elf, buf));
 	int i, ret;
 
-	elf->entry = ehdr->e_entry;
+	elf->entry = elf_hdr_e_entry(elf, buf);
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
-		void *src = buf + phdr->p_offset;
+	for (i = 0; i < elf_hdr_e_phnum(elf, buf) ; ++i) {
+		void *src = buf + elf_phdr_p_offset(elf, phdr);
 
 		ret = load_elf_phdr_segment(elf, src, phdr);
 		/* in case of error elf_load_image() caller should clean up and
@@ -90,22 +91,22 @@ static int load_elf_image_phdr(struct elf_image *elf)
 		if (ret)
 			return ret;
 
-		++phdr;
+		phdr += elf_size_of_phdr(elf);
 	}
 
 	return 0;
 }
 
-static int elf_check_image(void *buf)
+static int elf_check_image(struct elf_image *elf)
 {
-	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf;
-
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (strncmp(elf->buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
 
-	if (ehdr->e_type != ET_EXEC) {
+	elf->class = ((char *) elf->buf)[EI_CLASS];
+
+	if (elf_hdr_e_type(elf, elf->buf) != ET_EXEC) {
 		pr_err("Non EXEC ELF image.\n");
 		return -ENOEXEC;
 	}
@@ -124,7 +125,7 @@ struct elf_image *elf_load_image(void *buf)
 
 	elf->buf = buf;
 
-	ret = elf_check_image(buf);
+	ret = elf_check_image(elf);
 	if (ret)
 		return ERR_PTR(ret);
 
include/elf.h +28 -1
index 92c8d9c12..633f4992d 100644
@@ -400,11 +400,38 @@ static inline void arch_write_notes(struct file *file) { }
 
 struct elf_image {
 	struct list_head list;
-	unsigned long entry;
+	u8 class;
+	u64 entry;
 	void *buf;
 };
 
 struct elf_image *elf_load_image(void *buf);
 void elf_release_image(struct elf_image *elf);
 
+#define ELF_GET_FIELD(__s, __field, __type) \
+static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
+	if (elf->class == ELFCLASS32) \
+		return (__type) ((struct elf32_##__s *) arg)->__field; \
+	else \
+		return (__type) ((struct elf64_##__s *) arg)->__field; \
+}
+
+ELF_GET_FIELD(hdr, e_entry, u64)
+ELF_GET_FIELD(hdr, e_phnum, u16)
+ELF_GET_FIELD(hdr, e_phoff, u64)
+ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(phdr, p_paddr, u64)
+ELF_GET_FIELD(phdr, p_filesz, u64)
+ELF_GET_FIELD(phdr, p_memsz, u64)
+ELF_GET_FIELD(phdr, p_type, u32)
+ELF_GET_FIELD(phdr, p_offset, u64)
+
+static inline unsigned long elf_size_of_phdr(struct elf_image *elf)
+{
+	if (elf->class == ELFCLASS32)
+		return sizeof(Elf32_Phdr);
+	else
+		return sizeof(Elf64_Phdr);
+}
+
 #endif /* _LINUX_ELF_H */
Signature · 7 lines
2.15.0.276.g89ea799


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#10 lore raw

PATCH v4 2/2 mips: bootm: Cast elf entry into unsigned long

Since elf entry type is now a u64 to accomodate both type of elf files (64 and 32 bits), we need to cast it to the pointer length before casting it to the pointer type.

---
 arch/mips/lib/bootm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/lib/bootm.c +1 -1
index f14540a4c..5bb09cc2d 100644
@@ -72,7 +72,7 @@ static int do_bootm_elf(struct image_data *data)
 
 	shutdown_barebox();
 
-	entry = (void *)elf->entry;
+	entry = (void *) (unsigned long) elf->entry;
 
 	entry(-2, phys_to_virt((unsigned long)fdt));
 
Signature · 7 lines
2.15.0.276.g89ea799


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#11 lore raw

Re: [PATCH v4 1/2] elf: add 64 bits elf loading support

In reply to Clement Leger
Quoted 12 lines · Clement Leger

From: Clement Leger <[email protected]>

This patch add elf64 loading support to the elf loader. Since elf32 and elf64 uses completely different types, to avoid copying all the code and simply replace elf32 with elf64, use a macro which will return the appropriate field for each type of header. This macro generates getter for elf structures according to the class of the loaded elf.
All direct elf struct dereference are then replaced by call to generated functions. This allows to keep a common loader code even if types are different.

Quoted 6 lines

---
Changes in V4:
- Fix mips elf entry cast to pointer
- Add Tested-by Oleksij Rempel

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox