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