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