For use by the ELF loader in PBL to relocate barebox proper, export a
new relocate_image capable of relocating barebox and implement
relocate_to_current_adr() in terms of it.

When doing ELF loading, the PT_DYNAMIC segment will contain the info
needed to derive the arguments for relocate_image():

  - dstart: DT_REL  /DT_RELA
  - dend:   DT_RELSZ/DT_RELASZ
  - dynsym: DT_SYMTAB

dynend is optional and could in theory be generated by looking at
DT_HASH or DT_GNU_HASH, but it's not required. Best guess is that the
zeroing is there just to signify that we don't need this data anymore.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 arch/arm/cpu/common.c        | 34 +++++++++++++++-------------------
 arch/arm/include/asm/reloc.h |  3 +++
 2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/arch/arm/cpu/common.c b/arch/arm/cpu/common.c
index 5b65b0745b98..f4f3f8f6325b 100644
--- a/arch/arm/cpu/common.c
+++ b/arch/arm/cpu/common.c
@@ -60,22 +60,10 @@ void pbl_barebox_break(void)
 /*
  * relocate binary to the currently running address
  */
-void __prereloc relocate_to_current_adr(void)
+void __prereloc relocate_image(unsigned long offset,
+                              void *dstart, void *dend,
+                              long *dynsym, long *dynend)
 {
-       unsigned long offset;
-       unsigned long __maybe_unused *dynsym, *dynend;
-       void *dstart, *dend;
-
-       /* Get offset between linked address and runtime address */
-       offset = get_runtime_offset();
-
-       /*
-        * We have yet to relocate, so using runtime_address
-        * to compute the relocated address
-        */
-       dstart = runtime_address(__rel_dyn_start);
-       dend = runtime_address(__rel_dyn_end);
-
 #if defined(CONFIG_CPU_64)
        while (dstart < dend) {
                struct elf64_rela *rel = dstart;
@@ -105,8 +93,6 @@ void __prereloc relocate_to_current_adr(void)
                dstart += sizeof(*rel);
        }
 #elif defined(CONFIG_CPU_32)
-       dynsym = runtime_address(__dynsym_start);
-       dynend = runtime_address(__dynsym_end);
 
        while (dstart < dend) {
                struct elf32_rel *rel = dstart;
@@ -141,10 +127,21 @@ void __prereloc relocate_to_current_adr(void)
                dstart += sizeof(*rel);
        }
 
-       __memset(dynsym, 0, (unsigned long)dynend - (unsigned long)dynsym);
+       /* Optional: not required for correctness */
+       if (dynend)
+               __memset(dynsym, 0, (unsigned long)dynend - (unsigned 
long)dynsym);
 #else
 #error "Architecture not specified"
 #endif
+}
+
+void __prereloc relocate_to_current_adr(void)
+{
+       relocate_image(get_runtime_offset(),
+                      runtime_address(__rel_dyn_start),
+                      runtime_address(__rel_dyn_end),
+                      runtime_address(__dynsym_start),
+                      runtime_address(__dynsym_end));
 
        sync_caches_for_execution();
 }
@@ -196,4 +193,3 @@ void print_pbl_mem_layout(ulong membase, ulong endmem, 
ulong barebox_base)
        printf("membase               = 0x%08lx+0x%08lx\n",
               membase, endmem - membase);
 }
-
diff --git a/arch/arm/include/asm/reloc.h b/arch/arm/include/asm/reloc.h
index 2d7411ab5284..62c07932fad4 100644
--- a/arch/arm/include/asm/reloc.h
+++ b/arch/arm/include/asm/reloc.h
@@ -30,6 +30,9 @@ static inline __prereloc unsigned long 
global_variable_offset(void)
 }
 #define global_variable_offset() global_variable_offset()
 
+void relocate_image(unsigned long offset,
+                   void *dstart, void *dend,
+                   long *dynsym, long *dynend);
 void relocate_to_current_adr(void);
 void relocate_to_adr(unsigned long target);
 void relocate_to_adr_full(unsigned long target);
-- 
2.47.3


Reply via email to