Home | History | Annotate | Line # | Download | only in gnuefi
      1 /*	$NetBSD: reloc_arm.c,v 1.2 2019/03/30 12:46:16 jmcneill Exp $	*/
      2 
      3 /* reloc_arm.c - position independent x86 ELF shared object relocator
      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 #include <efilib.h>
     41 
     42 #ifdef __NetBSD__
     43 #include <sys/types.h>
     44 #include <sys/exec_elf.h>
     45 #else
     46 #include <elf.h>
     47 #endif
     48 
     49 EFI_STATUS _relocate (long, Elf32_Dyn *, EFI_HANDLE, EFI_SYSTEM_TABLE *);
     50 
     51 EFI_STATUS _relocate (long ldbase, Elf32_Dyn *dyn,
     52 		      EFI_HANDLE image EFI_UNUSED,
     53 		      EFI_SYSTEM_TABLE *systab EFI_UNUSED)
     54 {
     55 	long relsz = 0, relent = 0;
     56 	Elf32_Rel *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_REL:
     63 				rel = (Elf32_Rel*)
     64 					((unsigned long)dyn[i].d_un.d_ptr
     65 					 + ldbase);
     66 				break;
     67 
     68 			case DT_RELSZ:
     69 				relsz = dyn[i].d_un.d_val;
     70 				break;
     71 
     72 			case DT_RELENT:
     73 				relent = dyn[i].d_un.d_val;
     74 				break;
     75 
     76 			default:
     77 				break;
     78 		}
     79 	}
     80 
     81 	if (!rel && relent == 0)
     82 		return EFI_SUCCESS;
     83 
     84 	if (!rel || relent == 0)
     85 		return EFI_LOAD_ERROR;
     86 
     87 	while (relsz > 0) {
     88 		/* apply the relocs */
     89 		switch (ELF32_R_TYPE (rel->r_info)) {
     90 			case R_ARM_NONE:
     91 				break;
     92 
     93 			case R_ARM_RELATIVE:
     94 				addr = (unsigned long *)
     95 					(ldbase + rel->r_offset);
     96 				*addr += ldbase;
     97 				break;
     98 
     99 			default:
    100 				break;
    101 		}
    102 		rel = (Elf32_Rel*) ((char *) rel + relent);
    103 		relsz -= relent;
    104 	}
    105 	return EFI_SUCCESS;
    106 }
    107