Home | History | Annotate | Download | only in lib
      1 // SPDX-License-Identifier: GPL-2.0+
      2 /* reloc_riscv.c - position independent ELF shared object relocator
      3    Copyright (C) 2018 Alexander Graf <agraf (at) suse.de>
      4    Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel (at) linaro.org>
      5    Copyright (C) 1999 Hewlett-Packard Co.
      6 	Contributed by David Mosberger <davidm (at) 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 
     53 efi_status_t _relocate(long ldbase, Elf_Dyn *dyn, efi_handle_t image,
     54 		       struct efi_system_table *systab)
     55 {
     56 	long relsz = 0, relent = 0;
     57 	Elf_Rela *rel = 0;
     58 	unsigned long *addr;
     59 	int i;
     60 
     61 	for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
     62 		switch (dyn[i].d_tag) {
     63 		case DT_RELA:
     64 			rel = (Elf_Rela *)((ulong)dyn[i].d_un.d_ptr + ldbase);
     65 			break;
     66 		case DT_RELASZ:
     67 			relsz = dyn[i].d_un.d_val;
     68 			break;
     69 		case DT_RELAENT:
     70 			relent = dyn[i].d_un.d_val;
     71 			break;
     72 		default:
     73 			break;
     74 		}
     75 	}
     76 
     77 	if (!rel && relent == 0)
     78 		return EFI_SUCCESS;
     79 
     80 	if (!rel || relent == 0)
     81 		return EFI_LOAD_ERROR;
     82 
     83 	while (relsz > 0) {
     84 		/* apply the relocs */
     85 		switch (ELF_R_TYPE(rel->r_info)) {
     86 		case R_RISCV_RELATIVE:
     87 			addr = (ulong *)(ldbase + rel->r_offset);
     88 			*addr = ldbase + rel->r_addend;
     89 			break;
     90 		default:
     91 			/* Panic */
     92 			while (1) ;
     93 		}
     94 		rel = (Elf_Rela *)((char *)rel + relent);
     95 		relsz -= relent;
     96 	}
     97 	return EFI_SUCCESS;
     98 }
     99