Implement elf64_to_cpu and use it when necessary.

Signed-off-by: Jiaxun Yang <jiaxun.y...@flygoat.com>
---
 tools/relocate-rela.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 613abd25ef46..cbdd2552ec6e 100644
--- a/tools/relocate-rela.c
+++ b/tools/relocate-rela.c
@@ -111,6 +111,14 @@ static uint32_t cpu_to_elf32(uint32_t data)
        return cpu_to_be32(data);
 }
 
+static uint64_t elf64_to_cpu(uint64_t data)
+{
+       if (ei_data == ELFDATA2LSB)
+               return le64_to_cpu(data);
+
+       return be64_to_cpu(data);
+}
+
 static bool supported_rela(Elf64_Rela *rela)
 {
        uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
@@ -150,7 +158,7 @@ static int decode_elf64(FILE *felf, char **argv)
                return 25;
        }
 
-       machine = le16_to_cpu(header.e_machine);
+       machine = elf16_to_cpu(header.e_machine);
        debug("Machine\t%d\n", machine);
 
        if (machine != EM_AARCH64) {
@@ -158,10 +166,10 @@ static int decode_elf64(FILE *felf, char **argv)
                return 30;
        }
 
-       text_base = le64_to_cpu(header.e_entry);
-       section_header_base = le64_to_cpu(header.e_shoff);
-       section_header_size = le16_to_cpu(header.e_shentsize) *
-                             le16_to_cpu(header.e_shnum);
+       text_base = elf64_to_cpu(header.e_entry);
+       section_header_base = elf64_to_cpu(header.e_shoff);
+       section_header_size = elf16_to_cpu(header.e_shentsize) *
+                             elf16_to_cpu(header.e_shnum);
 
        sh_table = malloc(section_header_size);
        if (!sh_table) {
@@ -189,8 +197,8 @@ static int decode_elf64(FILE *felf, char **argv)
                return 27;
        }
 
-       sh_index = le16_to_cpu(header.e_shstrndx);
-       sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
+       sh_index = elf16_to_cpu(header.e_shstrndx);
+       sh_size = elf64_to_cpu(sh_table[sh_index].sh_size);
        debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
 
        sh_str = malloc(sh_size);
@@ -205,8 +213,8 @@ static int decode_elf64(FILE *felf, char **argv)
         * Specifies the byte offset from the beginning of the file
         * to the first byte in the section.
         */
-       sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
-       sh_num = le16_to_cpu(header.e_shnum);
+       sh_offset = elf64_to_cpu(sh_table[sh_index].sh_offset);
+       sh_num = elf16_to_cpu(header.e_shnum);
 
        ret = fseek(felf, sh_offset, SEEK_SET);
        if (ret) {
@@ -228,13 +236,13 @@ static int decode_elf64(FILE *felf, char **argv)
        }
 
        for (i = 0; i < sh_num; i++) {
-               char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
+               char *sh_name = sh_str + elf32_to_cpu(sh_table[i].sh_name);
 
                debug("%s\n", sh_name);
 
-               sh_addr = le64_to_cpu(sh_table[i].sh_addr);
-               sh_offset = le64_to_cpu(sh_table[i].sh_offset);
-               sh_size = le64_to_cpu(sh_table[i].sh_size);
+               sh_addr = elf64_to_cpu(sh_table[i].sh_addr);
+               sh_offset = elf64_to_cpu(sh_table[i].sh_offset);
+               sh_size = elf64_to_cpu(sh_table[i].sh_size);
 
                if (!strcmp(".rela.dyn", sh_name)) {
                        debug("Found section\t\".rela_dyn\"\n");
@@ -480,9 +488,9 @@ static int rela_elf64(char **argv, FILE *f)
                        return 4;
                }
 
-               swrela.r_offset = le64_to_cpu(rela.r_offset);
-               swrela.r_info = le64_to_cpu(rela.r_info);
-               swrela.r_addend = le64_to_cpu(rela.r_addend);
+               swrela.r_offset = elf64_to_cpu(rela.r_offset);
+               swrela.r_info = elf64_to_cpu(rela.r_info);
+               swrela.r_addend = elf64_to_cpu(rela.r_addend);
 
                if (!supported_rela(&swrela))
                        continue;

-- 
2.43.0

Reply via email to