xref: /OK3568_Linux_fs/u-boot/arch/arc/lib/relocate.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <elf.h>
9*4882a593Smuzhiyun #include <asm-generic/sections.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun extern ulong __image_copy_start;
12*4882a593Smuzhiyun extern ulong __ivt_end;
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
15*4882a593Smuzhiyun 
copy_uboot_to_ram(void)16*4882a593Smuzhiyun int copy_uboot_to_ram(void)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	size_t len = (size_t)&__image_copy_end - (size_t)&__image_copy_start;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun 	memcpy((void *)gd->relocaddr, (void *)&__image_copy_start, len);
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 	return 0;
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun 
clear_bss(void)25*4882a593Smuzhiyun int clear_bss(void)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
28*4882a593Smuzhiyun 	size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	memset((void *)dst_addr, 0x00, len);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	return 0;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Base functionality is taken from x86 version with added ARC-specifics
37*4882a593Smuzhiyun  */
do_elf_reloc_fixups(void)38*4882a593Smuzhiyun int do_elf_reloc_fixups(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	Elf32_Rela *re_src = (Elf32_Rela *)(&__rel_dyn_start);
41*4882a593Smuzhiyun 	Elf32_Rela *re_end = (Elf32_Rela *)(&__rel_dyn_end);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	debug("Section .rela.dyn is located at %08x-%08x\n",
44*4882a593Smuzhiyun 	      (unsigned int)re_src, (unsigned int)re_end);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	Elf32_Addr *offset_ptr_rom, *last_offset = NULL;
47*4882a593Smuzhiyun 	Elf32_Addr *offset_ptr_ram;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	do {
50*4882a593Smuzhiyun 		/* Get the location from the relocation entry */
51*4882a593Smuzhiyun 		offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 		/* Check that the location of the relocation is in .text */
54*4882a593Smuzhiyun 		if (offset_ptr_rom >= (Elf32_Addr *)&__image_copy_start &&
55*4882a593Smuzhiyun 		    offset_ptr_rom > last_offset) {
56*4882a593Smuzhiyun 			unsigned int val;
57*4882a593Smuzhiyun 			/* Switch to the in-RAM version */
58*4882a593Smuzhiyun 			offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
59*4882a593Smuzhiyun 							gd->reloc_off);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 			debug("Patching value @ %08x (relocated to %08x)\n",
62*4882a593Smuzhiyun 			      (unsigned int)offset_ptr_rom,
63*4882a593Smuzhiyun 			      (unsigned int)offset_ptr_ram);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 			/*
66*4882a593Smuzhiyun 			 * Use "memcpy" because target location might be
67*4882a593Smuzhiyun 			 * 16-bit aligned on ARC so we may need to read
68*4882a593Smuzhiyun 			 * byte-by-byte. On attempt to read entire word by
69*4882a593Smuzhiyun 			 * CPU throws an exception
70*4882a593Smuzhiyun 			 */
71*4882a593Smuzhiyun 			memcpy(&val, offset_ptr_ram, sizeof(int));
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #ifdef __LITTLE_ENDIAN__
74*4882a593Smuzhiyun 			/* If location in ".text" section swap value */
75*4882a593Smuzhiyun 			if ((unsigned int)offset_ptr_rom <
76*4882a593Smuzhiyun 			    (unsigned int)&__ivt_end)
77*4882a593Smuzhiyun 				val = (val << 16) | (val >> 16);
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 			/* Check that the target points into executable */
81*4882a593Smuzhiyun 			if (val >= (unsigned int)&__image_copy_start && val <=
82*4882a593Smuzhiyun 			    (unsigned int)&__image_copy_end) {
83*4882a593Smuzhiyun 				val += gd->reloc_off;
84*4882a593Smuzhiyun #ifdef __LITTLE_ENDIAN__
85*4882a593Smuzhiyun 				/* If location in ".text" section swap value */
86*4882a593Smuzhiyun 				if ((unsigned int)offset_ptr_rom <
87*4882a593Smuzhiyun 				    (unsigned int)&__ivt_end)
88*4882a593Smuzhiyun 					val = (val << 16) | (val >> 16);
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun 				memcpy(offset_ptr_ram, &val, sizeof(int));
91*4882a593Smuzhiyun 			}
92*4882a593Smuzhiyun 		}
93*4882a593Smuzhiyun 		last_offset = offset_ptr_rom;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	} while (++re_src < re_end);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	return 0;
98*4882a593Smuzhiyun }
99