Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved. |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 4 | */ |
| 5 | |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 6 | #include <elf.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Shiji Yang | bb11234 | 2023-08-03 09:47:16 +0800 | [diff] [blame] | 8 | #include <asm/sections.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Alexey Brodkin | 2f78eec | 2016-08-03 20:44:39 +0300 | [diff] [blame] | 10 | |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
Alexey Brodkin | 913e9f0 | 2015-02-24 19:40:36 +0300 | [diff] [blame] | 13 | int copy_uboot_to_ram(void) |
| 14 | { |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 15 | size_t len = (size_t)__image_copy_end - (size_t)__image_copy_start; |
Alexey Brodkin | 913e9f0 | 2015-02-24 19:40:36 +0300 | [diff] [blame] | 16 | |
Alexey Brodkin | c157ab9 | 2015-12-16 19:24:10 +0300 | [diff] [blame] | 17 | if (gd->flags & GD_FLG_SKIP_RELOC) |
| 18 | return 0; |
| 19 | |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 20 | memcpy((void *)gd->relocaddr, (void *)__image_copy_start, len); |
Alexey Brodkin | 913e9f0 | 2015-02-24 19:40:36 +0300 | [diff] [blame] | 21 | |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | int clear_bss(void) |
| 26 | { |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 27 | ulong dst_addr = (ulong)__bss_start + gd->reloc_off; |
| 28 | size_t len = (size_t)__bss_end - (size_t)__bss_start; |
Alexey Brodkin | 913e9f0 | 2015-02-24 19:40:36 +0300 | [diff] [blame] | 29 | |
| 30 | memset((void *)dst_addr, 0x00, len); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 35 | /* |
| 36 | * Base functionality is taken from x86 version with added ARC-specifics |
| 37 | */ |
| 38 | int do_elf_reloc_fixups(void) |
| 39 | { |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 40 | Elf32_Rela *re_src = (Elf32_Rela *)__rel_dyn_start; |
| 41 | Elf32_Rela *re_end = (Elf32_Rela *)__rel_dyn_end; |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 42 | |
Alexey Brodkin | c157ab9 | 2015-12-16 19:24:10 +0300 | [diff] [blame] | 43 | if (gd->flags & GD_FLG_SKIP_RELOC) |
| 44 | return 0; |
| 45 | |
Alexey Brodkin | 0347812 | 2016-08-03 20:45:22 +0300 | [diff] [blame] | 46 | debug("Section .rela.dyn is located at %08x-%08x\n", |
| 47 | (unsigned int)re_src, (unsigned int)re_end); |
| 48 | |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 49 | Elf32_Addr *offset_ptr_rom; |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 50 | Elf32_Addr *offset_ptr_ram; |
| 51 | |
| 52 | do { |
| 53 | /* Get the location from the relocation entry */ |
| 54 | offset_ptr_rom = (Elf32_Addr *)re_src->r_offset; |
| 55 | |
| 56 | /* Check that the location of the relocation is in .text */ |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 57 | if (offset_ptr_rom >= (Elf32_Addr *)__image_copy_start && |
| 58 | offset_ptr_rom < (Elf32_Addr *)__image_copy_end) { |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 59 | unsigned int val, do_swap = 0; |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 60 | /* Switch to the in-RAM version */ |
| 61 | offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom + |
| 62 | gd->reloc_off); |
| 63 | |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 64 | #ifdef __LITTLE_ENDIAN__ |
| 65 | /* If location in ".text" section swap value */ |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 66 | if (((u32)offset_ptr_rom >= (u32)__text_start && |
| 67 | (u32)offset_ptr_rom <= (u32)__text_end) |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 68 | #if defined(__ARC700__) || defined(__ARC600__) |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 69 | || ((u32)offset_ptr_rom >= (u32)__ivt_start && |
| 70 | (u32)offset_ptr_rom <= (u32)__ivt_end) |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 71 | #endif |
| 72 | ) |
| 73 | do_swap = 1; |
| 74 | #endif |
| 75 | |
| 76 | debug("Patching value @ %08x (relocated to %08x)%s\n", |
Alexey Brodkin | 0347812 | 2016-08-03 20:45:22 +0300 | [diff] [blame] | 77 | (unsigned int)offset_ptr_rom, |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 78 | (unsigned int)offset_ptr_ram, |
| 79 | do_swap ? ", middle-endian encoded" : ""); |
Alexey Brodkin | 0347812 | 2016-08-03 20:45:22 +0300 | [diff] [blame] | 80 | |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 81 | /* |
| 82 | * Use "memcpy" because target location might be |
| 83 | * 16-bit aligned on ARC so we may need to read |
| 84 | * byte-by-byte. On attempt to read entire word by |
| 85 | * CPU throws an exception |
| 86 | */ |
| 87 | memcpy(&val, offset_ptr_ram, sizeof(int)); |
| 88 | |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 89 | if (do_swap) |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 90 | val = (val << 16) | (val >> 16); |
| 91 | |
Alexey Brodkin | b4ee139 | 2014-12-26 19:36:30 +0300 | [diff] [blame] | 92 | /* Check that the target points into executable */ |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 93 | if (val < (unsigned int)__image_copy_start || |
| 94 | val > (unsigned int)__image_copy_end) { |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 95 | /* TODO: Use panic() instead of debug() |
| 96 | * |
| 97 | * For some reason GCC might generate |
| 98 | * fake relocation even for LD/SC of constant |
| 99 | * inderectly. See an example below: |
| 100 | * ----------------------->8-------------------- |
| 101 | * static int setup_mon_len(void) |
| 102 | * { |
Shiji Yang | eff11fa | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 103 | * gd->mon_len = (ulong)__bss_end - CONFIG_SYS_MONITOR_BASE; |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 104 | * return 0; |
| 105 | * } |
| 106 | * ----------------------->8-------------------- |
| 107 | * |
| 108 | * And that's what we get in the binary: |
| 109 | * ----------------------->8-------------------- |
| 110 | * 10005cb4 <setup_mon_len>: |
| 111 | * 10005cb4: 193c 3f80 0003 2f80 st 0x32f80,[r25,60] |
| 112 | * 10005cb8: R_ARC_RELATIVE *ABS*-0x10000000 |
| 113 | * 10005cbc: 7fe0 j_s.d [blink] |
| 114 | * 10005cbe: 700c mov_s r0,0 |
| 115 | * ----------------------->8-------------------- |
| 116 | */ |
| 117 | debug("Relocation target %08x points outside of image\n", |
| 118 | val); |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 119 | } |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 120 | |
Alexey Brodkin | f711e54 | 2018-05-29 18:09:55 +0300 | [diff] [blame] | 121 | val += gd->reloc_off; |
| 122 | |
| 123 | if (do_swap) |
| 124 | val = (val << 16) | (val >> 16); |
| 125 | |
| 126 | memcpy(offset_ptr_ram, &val, sizeof(int)); |
| 127 | } |
Alexey Brodkin | b628c01 | 2014-02-04 12:56:15 +0400 | [diff] [blame] | 128 | } while (++re_src < re_end); |
| 129 | |
| 130 | return 0; |
| 131 | } |