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