blob: c7a21d9393d62713e25e9cf811d75ce1f5a1a275 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Simon Glass672e82c2015-08-04 12:33:51 -06002/*
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.
Simon Glass672e82c2015-08-04 12:33:51 -060010 */
11
Simon Glass672e82c2015-08-04 12:33:51 -060012#include <efi.h>
13#include <elf.h>
Simon Glass672e82c2015-08-04 12:33:51 -060014
Ivan Gorinov28bad7d2018-06-28 14:49:46 -070015efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn)
Simon Glass672e82c2015-08-04 12:33:51 -060016{
17 long relsz = 0, relent = 0;
18 Elf64_Rel *rel = 0;
19 unsigned long *addr;
20 int i;
21
22 for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
23 switch (dyn[i].d_tag) {
24 case DT_RELA:
25 rel = (Elf64_Rel *)
26 ((unsigned long)dyn[i].d_un.d_ptr + ldbase);
27 break;
28 case DT_RELASZ:
29 relsz = dyn[i].d_un.d_val;
30 break;
31 case DT_RELAENT:
32 relent = dyn[i].d_un.d_val;
33 break;
34 default:
35 break;
36 }
37 }
38
39 if (!rel && relent == 0)
40 return EFI_SUCCESS;
41
42 if (!rel || relent == 0)
43 return EFI_LOAD_ERROR;
44
45 while (relsz > 0) {
46 /* apply the relocs */
47 switch (ELF64_R_TYPE(rel->r_info)) {
48 case R_X86_64_NONE:
49 break;
50 case R_X86_64_RELATIVE:
51 addr = (unsigned long *)(ldbase + rel->r_offset);
52 *addr += ldbase;
53 break;
54 default:
55 break;
56 }
57 rel = (Elf64_Rel *)((char *)rel + relent);
58 relsz -= relent;
59 }
60
61 return EFI_SUCCESS;
62}