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