Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
Simon Glass | 672e82c | 2015-08-04 12:33:51 -0600 | [diff] [blame] | 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. |
Simon Glass | 672e82c | 2015-08-04 12:33:51 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
Simon Glass | 672e82c | 2015-08-04 12:33:51 -0600 | [diff] [blame] | 12 | #include <efi.h> |
| 13 | #include <elf.h> |
Simon Glass | 672e82c | 2015-08-04 12:33:51 -0600 | [diff] [blame] | 14 | |
Ivan Gorinov | 28bad7d | 2018-06-28 14:49:46 -0700 | [diff] [blame] | 15 | efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn) |
Simon Glass | 672e82c | 2015-08-04 12:33:51 -0600 | [diff] [blame] | 16 | { |
| 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 | } |