[PATCH v2 2/5] tools: relocate-rela: introduce elf16_to_cpu() and elf32_to_cpu()

Ovidiu Panait ovpanait at gmail.com
Sat Mar 11 18:38:35 CET 2023


Add elf16_to_cpu() and elf32_to_cpu() functions that allow to read data in
both big-endian and little-endian formats.

Reviewed-by: Michal Simek <michal.simek at amd.com>
Signed-off-by: Ovidiu Panait <ovpanait at gmail.com>
---

Changes in v2:
Replaced hardcoded 0x1 values with ELFDATA2LSB.

 tools/relocate-rela.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 689e2d4655..b27c41b96f 100644
--- a/tools/relocate-rela.c
+++ b/tools/relocate-rela.c
@@ -45,6 +45,7 @@
 #endif
 
 static int ei_class;
+static int ei_data;
 
 static uint64_t rela_start, rela_end, text_base, dyn_start;
 
@@ -61,6 +62,22 @@ static void debug(const char *fmt, ...)
 	}
 }
 
+static uint16_t elf16_to_cpu(uint16_t data)
+{
+	if (ei_data == ELFDATA2LSB)
+		return le16_to_cpu(data);
+
+	return be16_to_cpu(data);
+}
+
+static uint32_t elf32_to_cpu(uint32_t data)
+{
+	if (ei_data == ELFDATA2LSB)
+		return le32_to_cpu(data);
+
+	return be32_to_cpu(data);
+}
+
 static bool supported_rela(Elf64_Rela *rela)
 {
 	uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
@@ -384,6 +401,9 @@ static int decode_elf(char **argv)
 	ei_class = e_ident[4];
 	debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
 
+	ei_data = e_ident[5];
+	debug("EI_DATA(1=little endian, 2=big endian) %d\n", ei_data);
+
 	if (ei_class == 2)
 		return decode_elf64(felf, argv);
 
-- 
2.25.1



More information about the U-Boot mailing list