Rick Chen | 9dbb973 | 2018-05-29 14:10:06 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 2 | /* reloc_riscv.c - position independent ELF shared object relocator |
| 3 | Copyright (C) 2018 Alexander Graf <agraf@suse.de> |
| 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 | Redistribution and use in source and binary forms, with or without |
| 11 | modification, are permitted provided that the following conditions |
| 12 | are met: |
| 13 | |
| 14 | * Redistributions of source code must retain the above copyright |
| 15 | notice, this list of conditions and the following disclaimer. |
| 16 | * Redistributions in binary form must reproduce the above |
| 17 | copyright notice, this list of conditions and the following |
| 18 | disclaimer in the documentation and/or other materials |
| 19 | provided with the distribution. |
| 20 | * Neither the name of Hewlett-Packard Co. nor the names of its |
| 21 | contributors may be used to endorse or promote products derived |
| 22 | from this software without specific prior written permission. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 25 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 26 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 27 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 29 | BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 30 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 34 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 35 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <efi.h> |
| 40 | |
| 41 | #include <elf.h> |
| 42 | |
| 43 | #if __riscv_xlen == 64 |
| 44 | #define Elf_Dyn Elf64_Dyn |
| 45 | #define Elf_Rela Elf64_Rela |
| 46 | #define ELF_R_TYPE ELF64_R_TYPE |
| 47 | #else |
| 48 | #define Elf_Dyn Elf32_Dyn |
| 49 | #define Elf_Rela Elf32_Rela |
| 50 | #define ELF_R_TYPE ELF32_R_TYPE |
| 51 | #endif |
| 52 | |
Ivan Gorinov | b908d2e | 2018-06-28 14:50:17 -0700 | [diff] [blame] | 53 | efi_status_t EFIAPI _relocate(long ldbase, Elf_Dyn *dyn) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 54 | { |
| 55 | long relsz = 0, relent = 0; |
| 56 | Elf_Rela *rel = 0; |
| 57 | unsigned long *addr; |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; dyn[i].d_tag != DT_NULL; ++i) { |
| 61 | switch (dyn[i].d_tag) { |
| 62 | case DT_RELA: |
| 63 | rel = (Elf_Rela *)((ulong)dyn[i].d_un.d_ptr + ldbase); |
| 64 | break; |
| 65 | case DT_RELASZ: |
| 66 | relsz = dyn[i].d_un.d_val; |
| 67 | break; |
| 68 | case DT_RELAENT: |
| 69 | relent = dyn[i].d_un.d_val; |
| 70 | break; |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (!rel && relent == 0) |
| 77 | return EFI_SUCCESS; |
| 78 | |
| 79 | if (!rel || relent == 0) |
| 80 | return EFI_LOAD_ERROR; |
| 81 | |
| 82 | while (relsz > 0) { |
| 83 | /* apply the relocs */ |
| 84 | switch (ELF_R_TYPE(rel->r_info)) { |
| 85 | case R_RISCV_RELATIVE: |
| 86 | addr = (ulong *)(ldbase + rel->r_offset); |
| 87 | *addr = ldbase + rel->r_addend; |
| 88 | break; |
| 89 | default: |
| 90 | /* Panic */ |
| 91 | while (1) ; |
| 92 | } |
| 93 | rel = (Elf_Rela *)((char *)rel + relent); |
| 94 | relsz -= relent; |
| 95 | } |
| 96 | return EFI_SUCCESS; |
| 97 | } |