xref: /netbsd-src/sys/external/bsd/gnu-efi/dist/gnuefi/reloc_mips64el.c (revision d1b935f8e85510a16a1b49122bd2ccf5ad7e104c)
1 /*	$NetBSD: reloc_mips64el.c,v 1.1.1.1 2018/08/16 18:17:47 jmcneill Exp $	*/
2 
3 /* reloc_mips64el.c - position independent MIPS64 ELF shared object relocator
4    Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
5    Copyright (C) 1999 Hewlett-Packard Co.
6 	Contributed by David Mosberger <davidm@hpl.hp.com>.
7    Copyright (C) 2017 Lemote Co.
8 	Contributed by Heiher <r@hev.cc>
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 #include <efilib.h>
43 
44 #include <elf.h>
45 
_relocate(long ldbase,Elf64_Dyn * dyn,EFI_HANDLE image EFI_UNUSED,EFI_SYSTEM_TABLE * systab EFI_UNUSED)46 EFI_STATUS _relocate (long ldbase, Elf64_Dyn *dyn,
47 		      EFI_HANDLE image EFI_UNUSED,
48 		      EFI_SYSTEM_TABLE *systab EFI_UNUSED)
49 {
50 	long relsz = 0, relent = 0, gotsz = 0;
51 	Elf64_Rel *rel = 0;
52 	unsigned long *addr = 0;
53 	int i;
54 
55 	for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
56 		switch (dyn[i].d_tag) {
57 			case DT_REL:
58 				rel = (Elf64_Rel*)
59 					((unsigned long)dyn[i].d_un.d_ptr
60 					 + ldbase);
61 				break;
62 
63 			case DT_RELSZ:
64 				relsz = dyn[i].d_un.d_val;
65 				break;
66 
67 			case DT_RELENT:
68 				relent = dyn[i].d_un.d_val;
69 				break;
70 
71 			case DT_PLTGOT:
72 				addr = (unsigned long *)
73 					((unsigned long)dyn[i].d_un.d_ptr
74 					 + ldbase);
75 				break;
76 
77 			case DT_MIPS_LOCAL_GOTNO:
78 				gotsz = dyn[i].d_un.d_val;
79 				break;
80 
81 			default:
82 				break;
83 		}
84 	}
85 
86 	if ((!rel && relent == 0) && (!addr && gotsz == 0))
87 		return EFI_SUCCESS;
88 
89 	if ((!rel && relent != 0) || (!addr && gotsz != 0))
90 		return EFI_LOAD_ERROR;
91 
92 	while (gotsz > 0) {
93 		*addr += ldbase;
94 		addr += 1;
95 		gotsz --;
96 	}
97 
98 	while (relsz > 0) {
99 		/* apply the relocs */
100 		switch (ELF64_R_TYPE (swap_uint64 (rel->r_info))) {
101 			case R_MIPS_NONE:
102 				break;
103 
104 			case (R_MIPS_64 << 8) | R_MIPS_REL32:
105 				addr = (unsigned long *)
106 					(ldbase + rel->r_offset);
107 				*addr += ldbase;
108 				break;
109 
110 			default:
111 				break;
112 		}
113 		rel = (Elf64_Rel*) ((char *) rel + relent);
114 		relsz -= relent;
115 	}
116 	return EFI_SUCCESS;
117 }
118