xref: /openbmc/u-boot/arch/x86/lib/reloc_x86_64_efi.c (revision 95963679)
1 // SPDX-License-Identifier: BSD-3-Clause
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 
12 #include <common.h>
13 #include <efi.h>
14 #include <elf.h>
15 #include <asm/elf.h>
16 
17 efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn)
18 {
19 	long relsz = 0, relent = 0;
20 	Elf64_Rel *rel = 0;
21 	unsigned long *addr;
22 	int i;
23 
24 	for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
25 		switch (dyn[i].d_tag) {
26 		case DT_RELA:
27 			rel = (Elf64_Rel *)
28 				((unsigned long)dyn[i].d_un.d_ptr + ldbase);
29 			break;
30 		case DT_RELASZ:
31 			relsz = dyn[i].d_un.d_val;
32 			break;
33 		case DT_RELAENT:
34 			relent = dyn[i].d_un.d_val;
35 			break;
36 		default:
37 			break;
38 		}
39 	}
40 
41 	if (!rel && relent == 0)
42 		return EFI_SUCCESS;
43 
44 	if (!rel || relent == 0)
45 		return EFI_LOAD_ERROR;
46 
47 	while (relsz > 0) {
48 		/* apply the relocs */
49 		switch (ELF64_R_TYPE(rel->r_info)) {
50 		case R_X86_64_NONE:
51 			break;
52 		case R_X86_64_RELATIVE:
53 			addr = (unsigned long *)(ldbase + rel->r_offset);
54 			*addr += ldbase;
55 			break;
56 		default:
57 			break;
58 		}
59 		rel = (Elf64_Rel *)((char *)rel + relent);
60 		relsz -= relent;
61 	}
62 
63 	return EFI_SUCCESS;
64 }
65