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