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