]> git.sur5r.net Git - u-boot/blob - arch/riscv/lib/reloc_riscv_efi.c
d80ffc975c446d770c9c9ea40c781f38b141ba92
[u-boot] / arch / riscv / lib / reloc_riscv_efi.c
1 /* reloc_riscv.c - position independent ELF shared object relocator
2    Copyright (C) 2018 Alexander Graf <agraf@suse.de>
3    Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
4    Copyright (C) 1999 Hewlett-Packard Co.
5         Contributed by David Mosberger <davidm@hpl.hp.com>.
6
7     All rights reserved.
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12
13     * Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15     * Redistributions in binary form must reproduce the above
16       copyright notice, this list of conditions and the following
17       disclaimer in the documentation and/or other materials
18       provided with the distribution.
19     * Neither the name of Hewlett-Packard Co. nor the names of its
20       contributors may be used to endorse or promote products derived
21       from this software without specific prior written permission.
22
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
28     BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29     OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35     SUCH DAMAGE.
36 */
37
38 #include <efi.h>
39
40 #include <elf.h>
41
42 #if __riscv_xlen == 64
43 #define Elf_Dyn         Elf64_Dyn
44 #define Elf_Rela        Elf64_Rela
45 #define ELF_R_TYPE      ELF64_R_TYPE
46 #else
47 #define Elf_Dyn         Elf32_Dyn
48 #define Elf_Rela        Elf32_Rela
49 #define ELF_R_TYPE      ELF32_R_TYPE
50 #endif
51
52 efi_status_t _relocate(long ldbase, Elf_Dyn *dyn, efi_handle_t image,
53                        struct efi_system_table *systab)
54 {
55         long relsz = 0, relent = 0;
56         Elf_Rela *rel = 0;
57         unsigned long *addr;
58         int i;
59
60         for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
61                 switch (dyn[i].d_tag) {
62                 case DT_RELA:
63                         rel = (Elf_Rela *)((ulong)dyn[i].d_un.d_ptr + ldbase);
64                         break;
65                 case DT_RELASZ:
66                         relsz = dyn[i].d_un.d_val;
67                         break;
68                 case DT_RELAENT:
69                         relent = dyn[i].d_un.d_val;
70                         break;
71                 default:
72                         break;
73                 }
74         }
75
76         if (!rel && relent == 0)
77                 return EFI_SUCCESS;
78
79         if (!rel || relent == 0)
80                 return EFI_LOAD_ERROR;
81
82         while (relsz > 0) {
83                 /* apply the relocs */
84                 switch (ELF_R_TYPE(rel->r_info)) {
85                 case R_RISCV_RELATIVE:
86                         addr = (ulong *)(ldbase + rel->r_offset);
87                         *addr = ldbase + rel->r_addend;
88                         break;
89                 default:
90                         /* Panic */
91                         while (1) ;
92                 }
93                 rel = (Elf_Rela *)((char *)rel + relent);
94                 relsz -= relent;
95         }
96         return EFI_SUCCESS;
97 }