183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
222723828SAlexey Brodkin /*
322723828SAlexey Brodkin * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
422723828SAlexey Brodkin */
522723828SAlexey Brodkin
622723828SAlexey Brodkin #include <common.h>
722723828SAlexey Brodkin #include <elf.h>
89bef24d0SAlexey Brodkin #include <asm-generic/sections.h>
99bef24d0SAlexey Brodkin
109bef24d0SAlexey Brodkin extern ulong __image_copy_start;
11*ce307128SAlexey Brodkin extern ulong __ivt_start;
129bef24d0SAlexey Brodkin extern ulong __ivt_end;
13*ce307128SAlexey Brodkin extern ulong __text_end;
1422723828SAlexey Brodkin
1522723828SAlexey Brodkin DECLARE_GLOBAL_DATA_PTR;
1622723828SAlexey Brodkin
copy_uboot_to_ram(void)173fb80163SAlexey Brodkin int copy_uboot_to_ram(void)
183fb80163SAlexey Brodkin {
193fb80163SAlexey Brodkin size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
203fb80163SAlexey Brodkin
21264d298fSAlexey Brodkin if (gd->flags & GD_FLG_SKIP_RELOC)
22264d298fSAlexey Brodkin return 0;
23264d298fSAlexey Brodkin
243fb80163SAlexey Brodkin memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
253fb80163SAlexey Brodkin
263fb80163SAlexey Brodkin return 0;
273fb80163SAlexey Brodkin }
283fb80163SAlexey Brodkin
clear_bss(void)293fb80163SAlexey Brodkin int clear_bss(void)
303fb80163SAlexey Brodkin {
313fb80163SAlexey Brodkin ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
323fb80163SAlexey Brodkin size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
333fb80163SAlexey Brodkin
343fb80163SAlexey Brodkin memset((void *)dst_addr, 0x00, len);
353fb80163SAlexey Brodkin
363fb80163SAlexey Brodkin return 0;
373fb80163SAlexey Brodkin }
383fb80163SAlexey Brodkin
3922723828SAlexey Brodkin /*
4022723828SAlexey Brodkin * Base functionality is taken from x86 version with added ARC-specifics
4122723828SAlexey Brodkin */
do_elf_reloc_fixups(void)4222723828SAlexey Brodkin int do_elf_reloc_fixups(void)
4322723828SAlexey Brodkin {
4422723828SAlexey Brodkin Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
4522723828SAlexey Brodkin Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
4622723828SAlexey Brodkin
47264d298fSAlexey Brodkin if (gd->flags & GD_FLG_SKIP_RELOC)
48264d298fSAlexey Brodkin return 0;
49264d298fSAlexey Brodkin
50ffffcd15SAlexey Brodkin debug("Section .rela.dyn is located at %08x-%08x\n",
51ffffcd15SAlexey Brodkin (unsigned int)re_src, (unsigned int)re_end);
52ffffcd15SAlexey Brodkin
53*ce307128SAlexey Brodkin Elf32_Addr *offset_ptr_rom;
5422723828SAlexey Brodkin Elf32_Addr *offset_ptr_ram;
5522723828SAlexey Brodkin
5622723828SAlexey Brodkin do {
5722723828SAlexey Brodkin /* Get the location from the relocation entry */
5822723828SAlexey Brodkin offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
5922723828SAlexey Brodkin
6022723828SAlexey Brodkin /* Check that the location of the relocation is in .text */
611c91a3d9SAlexey Brodkin if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
62*ce307128SAlexey Brodkin offset_ptr_rom < (Elf32_Addr *)&__image_copy_end) {
63*ce307128SAlexey Brodkin unsigned int val, do_swap = 0;
6422723828SAlexey Brodkin /* Switch to the in-RAM version */
6522723828SAlexey Brodkin offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
6622723828SAlexey Brodkin gd->reloc_off);
6722723828SAlexey Brodkin
68*ce307128SAlexey Brodkin #ifdef __LITTLE_ENDIAN__
69*ce307128SAlexey Brodkin /* If location in ".text" section swap value */
70*ce307128SAlexey Brodkin if (((u32)offset_ptr_rom >= (u32)&__text_start &&
71*ce307128SAlexey Brodkin (u32)offset_ptr_rom <= (u32)&__text_end)
72*ce307128SAlexey Brodkin #if defined(__ARC700__) || defined(__ARC600__)
73*ce307128SAlexey Brodkin || ((u32)offset_ptr_rom >= (u32)&__ivt_start &&
74*ce307128SAlexey Brodkin (u32)offset_ptr_rom <= (u32)&__ivt_end)
75*ce307128SAlexey Brodkin #endif
76*ce307128SAlexey Brodkin )
77*ce307128SAlexey Brodkin do_swap = 1;
78*ce307128SAlexey Brodkin #endif
79*ce307128SAlexey Brodkin
80*ce307128SAlexey Brodkin debug("Patching value @ %08x (relocated to %08x)%s\n",
81ffffcd15SAlexey Brodkin (unsigned int)offset_ptr_rom,
82*ce307128SAlexey Brodkin (unsigned int)offset_ptr_ram,
83*ce307128SAlexey Brodkin do_swap ? ", middle-endian encoded" : "");
84ffffcd15SAlexey Brodkin
8522723828SAlexey Brodkin /*
8622723828SAlexey Brodkin * Use "memcpy" because target location might be
8722723828SAlexey Brodkin * 16-bit aligned on ARC so we may need to read
8822723828SAlexey Brodkin * byte-by-byte. On attempt to read entire word by
8922723828SAlexey Brodkin * CPU throws an exception
9022723828SAlexey Brodkin */
9122723828SAlexey Brodkin memcpy(&val, offset_ptr_ram, sizeof(int));
9222723828SAlexey Brodkin
93*ce307128SAlexey Brodkin if (do_swap)
9422723828SAlexey Brodkin val = (val << 16) | (val >> 16);
9522723828SAlexey Brodkin
961c91a3d9SAlexey Brodkin /* Check that the target points into executable */
97*ce307128SAlexey Brodkin if (val < (unsigned int)&__image_copy_start ||
98*ce307128SAlexey Brodkin val > (unsigned int)&__image_copy_end) {
99*ce307128SAlexey Brodkin /* TODO: Use panic() instead of debug()
100*ce307128SAlexey Brodkin *
101*ce307128SAlexey Brodkin * For some reason GCC might generate
102*ce307128SAlexey Brodkin * fake relocation even for LD/SC of constant
103*ce307128SAlexey Brodkin * inderectly. See an example below:
104*ce307128SAlexey Brodkin * ----------------------->8--------------------
105*ce307128SAlexey Brodkin * static int setup_mon_len(void)
106*ce307128SAlexey Brodkin * {
107*ce307128SAlexey Brodkin * gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
108*ce307128SAlexey Brodkin * return 0;
109*ce307128SAlexey Brodkin * }
110*ce307128SAlexey Brodkin * ----------------------->8--------------------
111*ce307128SAlexey Brodkin *
112*ce307128SAlexey Brodkin * And that's what we get in the binary:
113*ce307128SAlexey Brodkin * ----------------------->8--------------------
114*ce307128SAlexey Brodkin * 10005cb4 <setup_mon_len>:
115*ce307128SAlexey Brodkin * 10005cb4: 193c 3f80 0003 2f80 st 0x32f80,[r25,60]
116*ce307128SAlexey Brodkin * 10005cb8: R_ARC_RELATIVE *ABS*-0x10000000
117*ce307128SAlexey Brodkin * 10005cbc: 7fe0 j_s.d [blink]
118*ce307128SAlexey Brodkin * 10005cbe: 700c mov_s r0,0
119*ce307128SAlexey Brodkin * ----------------------->8--------------------
120*ce307128SAlexey Brodkin */
121*ce307128SAlexey Brodkin debug("Relocation target %08x points outside of image\n",
122*ce307128SAlexey Brodkin val);
123*ce307128SAlexey Brodkin }
124*ce307128SAlexey Brodkin
12522723828SAlexey Brodkin val += gd->reloc_off;
126*ce307128SAlexey Brodkin
127*ce307128SAlexey Brodkin if (do_swap)
12822723828SAlexey Brodkin val = (val << 16) | (val >> 16);
129*ce307128SAlexey Brodkin
13022723828SAlexey Brodkin memcpy(offset_ptr_ram, &val, sizeof(int));
13122723828SAlexey Brodkin }
13222723828SAlexey Brodkin } while (++re_src < re_end);
13322723828SAlexey Brodkin
13422723828SAlexey Brodkin return 0;
13522723828SAlexey Brodkin }
136