Home | History | Annotate | Line # | Download | only in arm32
kobj_machdep.c revision 1.8
      1 /*	$NetBSD: kobj_machdep.c,v 1.8 2013/08/27 06:39:43 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*-
     30  * Copyright 1996-1998 John D. Polstra.
     31  * All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  *
     42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     43  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     45  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     46  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     48  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     49  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     51  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.8 2013/08/27 06:39:43 skrll Exp $");
     56 
     57 #define	ELFSIZE		ARCH_ELFSIZE
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/kobj.h>
     62 #include <sys/exec.h>
     63 #include <sys/exec_elf.h>
     64 
     65 #include <arm/cpufunc.h>
     66 
     67 int
     68 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
     69 	   bool isrela, bool local)
     70 {
     71 	Elf_Addr *where;
     72 	Elf_Addr addr;
     73 	Elf_Addr addend;
     74 	Elf_Word rtype, symidx;
     75 	const Elf_Rel *rel;
     76 	const Elf_Rela *rela;
     77 
     78 	if (isrela) {
     79 		rela = (const Elf_Rela *)data;
     80 		where = (Elf_Addr *) (relocbase + rela->r_offset);
     81 		addend = rela->r_addend;
     82 		rtype = ELF_R_TYPE(rela->r_info);
     83 		symidx = ELF_R_SYM(rela->r_info);
     84 	} else {
     85 		rel = (const Elf_Rel *)data;
     86 		where = (Elf_Addr *) (relocbase + rel->r_offset);
     87 		addend = *where;
     88 		rtype = ELF_R_TYPE(rel->r_info);
     89 		symidx = ELF_R_SYM(rel->r_info);
     90 	}
     91 
     92 	switch (rtype) {
     93 	case R_ARM_NONE:	/* none */
     94 	case R_ARM_V4BX:	/* none */
     95 		return 0;
     96 
     97 	case R_ARM_ABS32:
     98 		addr = kobj_sym_lookup(ko, symidx);
     99 		if (addr == 0)
    100 			break;
    101 		*where = addr + addend;
    102 		return 0;
    103 
    104 	case R_ARM_COPY:	/* none */
    105 		/* There shouldn't be copy relocations in kernel objects. */
    106 		break;
    107 
    108 	case R_ARM_JUMP_SLOT:
    109 		addr = kobj_sym_lookup(ko, symidx);
    110 		if (addr == 0)
    111 			break;
    112 		*where = addr;
    113 		return 0;
    114 
    115 	case R_ARM_RELATIVE:	/* A + B */
    116 		addr = relocbase + addend;
    117 		if (*where != addr)
    118 			*where = addr;
    119 		return 0;
    120 
    121 	case R_ARM_MOVW_ABS_NC:	/* (S + A) | T */
    122 	case R_ARM_MOVT_ABS:
    123 		if ((*where & 0x0fb00000) != 0x03000000)
    124 			break;
    125 		addr = kobj_sym_lookup(ko, symidx);
    126 		if (addr == 0)
    127 			break;
    128 		if (rtype == R_ARM_MOVT_ABS)
    129 			addr >>= 16;
    130 		*where = (*where & 0xfff0f000)
    131 		    | ((addr << 4) & 0x000f0000) | (addr & 0x00000fff);
    132 		return 0;
    133 
    134 	case R_ARM_CALL:	/* ((S + A) | T) -  P */
    135 	case R_ARM_JUMP24:
    136 	case R_ARM_PC24:	/* Deprecated */
    137 		if (local && (*where & 0x00ffffff) != 0x00fffffe)
    138 			return 0;
    139 
    140 		/* Remove the instruction from the 24 bit offset */
    141 		addend &= 0x00ffffff;
    142 
    143 		/* Sign extend if necessary */
    144 		if (addend & 0x00800000)
    145 			addend |= 0xff000000;
    146 
    147 		addend <<= 2;
    148 
    149 		addr = kobj_sym_lookup(ko, symidx);
    150 		if (addr == 0)
    151 			break;
    152 
    153 		addend += (uintptr_t)addr - (uintptr_t)where;
    154 
    155 		if (addend & 3) {
    156 			printf ("Relocation %x unaligned @ %p\n", addend, where);
    157 			return -1;
    158 		}
    159 
    160 		if ((addend & 0xfe000000) != 0x00000000 &&
    161 		    (addend & 0xfe000000) != 0xfe000000) {
    162 			printf ("Relocation %x too far @ %p\n", addend, where);
    163 			return -1;
    164 		}
    165 		*where = (*where & 0xff000000) | ((addend >> 2) & 0x00ffffff);
    166 		return 0;
    167 
    168 	case R_ARM_REL32:	/* ((S + A) | T) -  P */
    169 		/* T = 0 for now */
    170 		addr = kobj_sym_lookup(ko, symidx);
    171 		if (addr == 0)
    172 			break;
    173 
    174 		addend += (uintptr_t)addr - (uintptr_t)where;
    175 		*where = addend;
    176 		return 0;
    177 
    178 	case R_ARM_PREL31:	/* ((S + A) | T) -  P */
    179 		/* 42 R_ARM_PREL31 4 sign_extend(P[30:0]) 31 - bit 2's complement */
    180 		/* Sign extend if necessary */
    181 		if (addend & 0x40000000)
    182 			addend |= 0xc0000000;
    183 		/* T = 0 for now */
    184 		addr = kobj_sym_lookup(ko, symidx);
    185 		if (addr == 0)
    186 			break;
    187 
    188 		addend += (uintptr_t)addr - (uintptr_t)where;
    189 
    190 		if ((addend & 0x80000000) != 0x00000000 &&
    191 		    (addend & 0x80000000) != 0x80000000) {
    192 			printf ("Relocation %x too far @ %p\n", addend, where);
    193 			return -1;
    194 		}
    195 
    196 		*where = (*where & 0x80000000) | (addend & 0x7fffffff);
    197 
    198 	default:
    199 		break;
    200 	}
    201 
    202 	printf("kobj_reloc: unexpected/invalid relocation type %d @ %p symidx %u\n",
    203 	    rtype, where, symidx);
    204 	return -1;
    205 }
    206 
    207 int
    208 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
    209 {
    210 
    211 	if (load) {
    212 #ifndef _RUMPKERNEL
    213 		cpu_idcache_wbinv_range((vaddr_t)base, size);
    214 		cpu_tlb_flushID();
    215 #endif
    216 	}
    217 
    218 	return 0;
    219 }
    220