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