xref: /openbmc/u-boot/arch/arm/lib/reloc_arm_efi.c (revision 72719d2f)
1 /*
2  * reloc_arm.c - position-independent ARM ELF shared object relocator
3  *
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  *
8  * All rights reserved.
9  *
10  * SPDX-License-Identifier:	BSD-3-Clause
11  *
12  * This file is taken and modified from the gnu-efi project.
13  */
14 
15 #include <efi.h>
16 #include <elf.h>
17 
18 efi_status_t _relocate(long ldbase, Elf32_Dyn *dyn, efi_handle_t image,
19 		       struct efi_system_table *systab)
20 {
21 	long relsz = 0, relent = 0;
22 	Elf32_Rel *rel = 0;
23 	ulong *addr;
24 	int i;
25 
26 	for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
27 		switch (dyn[i].d_tag) {
28 		case DT_REL:
29 			rel = (Elf32_Rel *)((ulong)dyn[i].d_un.d_ptr
30 					+ ldbase);
31 			break;
32 		case DT_RELSZ:
33 			relsz = dyn[i].d_un.d_val;
34 			break;
35 		case DT_RELENT:
36 			relent = dyn[i].d_un.d_val;
37 			break;
38 		default:
39 			break;
40 		}
41 	}
42 
43 	if (!rel && relent == 0)
44 		return EFI_SUCCESS;
45 
46 	if (!rel || relent == 0)
47 		return EFI_LOAD_ERROR;
48 
49 	while (relsz > 0) {
50 		/* apply the relocs */
51 		switch (ELF32_R_TYPE(rel->r_info)) {
52 		case R_ARM_NONE:
53 			break;
54 		case R_ARM_RELATIVE:
55 			addr = (ulong *)(ldbase + rel->r_offset);
56 			*addr += ldbase;
57 			break;
58 		default:
59 			break;
60 		}
61 		rel = (Elf32_Rel *)((char *)rel + relent);
62 		relsz -= relent;
63 	}
64 
65 	return EFI_SUCCESS;
66 }
67