Home | History | Annotate | Line # | Download | only in arm32
kobj_machdep.c revision 1.14
      1 /*	$NetBSD: kobj_machdep.c,v 1.14 2020/06/20 07:10:36 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.14 2020/06/20 07:10:36 skrll Exp $");
     56 
     57 #define	ELFSIZE		ARCH_ELFSIZE
     58 
     59 #include <sys/param.h>
     60 
     61 #include <sys/exec.h>
     62 #include <sys/exec_elf.h>
     63 #include <sys/kmem.h>
     64 #include <sys/kobj.h>
     65 #include <sys/kobj_impl.h>
     66 #include <sys/ksyms.h>
     67 #include <sys/systm.h>
     68 
     69 #include <arm/cpufunc.h>
     70 #include <arm/locore.h>
     71 
     72 int
     73 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
     74 	   bool isrela, bool local)
     75 {
     76 	Elf_Addr *where;
     77 	Elf_Addr addr;
     78 	Elf_Addr addend;
     79 	Elf_Word rtype, symidx;
     80 	const Elf_Rel *rel;
     81 	const Elf_Rela *rela;
     82 	int error;
     83 
     84 	if (isrela) {
     85 		rela = (const Elf_Rela *)data;
     86 		where = (Elf_Addr *) (relocbase + rela->r_offset);
     87 		addend = rela->r_addend;
     88 		rtype = ELF_R_TYPE(rela->r_info);
     89 		symidx = ELF_R_SYM(rela->r_info);
     90 	} else {
     91 		rel = (const Elf_Rel *)data;
     92 		where = (Elf_Addr *) (relocbase + rel->r_offset);
     93 		addend = *where;
     94 		rtype = ELF_R_TYPE(rel->r_info);
     95 		symidx = ELF_R_SYM(rel->r_info);
     96 	}
     97 
     98 	switch (rtype) {
     99 	case R_ARM_NONE:	/* none */
    100 	case R_ARM_V4BX:	/* none */
    101 		return 0;
    102 
    103 	case R_ARM_ABS32:
    104 		error = kobj_sym_lookup(ko, symidx, &addr);
    105 		if (error)
    106 			break;
    107 		*where = addr + addend;
    108 		return 0;
    109 
    110 	case R_ARM_COPY:	/* none */
    111 		/* There shouldn't be copy relocations in kernel objects. */
    112 		break;
    113 
    114 	case R_ARM_JUMP_SLOT:
    115 		error = kobj_sym_lookup(ko, symidx, &addr);
    116 		if (error)
    117 			break;
    118 		*where = addr;
    119 		return 0;
    120 
    121 	case R_ARM_RELATIVE:	/* A + B */
    122 		addr = relocbase + addend;
    123 		if (*where != addr)
    124 			*where = addr;
    125 		return 0;
    126 
    127 	case R_ARM_MOVW_ABS_NC:	/* (S + A) | T */
    128 	case R_ARM_MOVT_ABS:
    129 		if ((*where & 0x0fb00000) != 0x03000000)
    130 			break;
    131 		error = kobj_sym_lookup(ko, symidx, &addr);
    132 		if (error)
    133 			break;
    134 		if (rtype == R_ARM_MOVT_ABS)
    135 			addr >>= 16;
    136 		*where = (*where & 0xfff0f000)
    137 		    | ((addr << 4) & 0x000f0000) | (addr & 0x00000fff);
    138 		return 0;
    139 
    140 	case R_ARM_CALL:	/* ((S + A) | T) -  P */
    141 	case R_ARM_JUMP24:
    142 	case R_ARM_PC24:	/* Deprecated */
    143 		if (local && (*where & 0x00ffffff) != 0x00fffffe)
    144 			return 0;
    145 
    146 		/* Remove the instruction from the 24 bit offset */
    147 		addend &= 0x00ffffff;
    148 
    149 		/* Sign extend if necessary */
    150 		if (addend & 0x00800000)
    151 			addend |= 0xff000000;
    152 
    153 		addend <<= 2;
    154 
    155 		error = kobj_sym_lookup(ko, symidx, &addr);
    156 		if (error)
    157 			break;
    158 
    159 		addend += (uintptr_t)addr - (uintptr_t)where;
    160 
    161 		if (addend & 3) {
    162 			printf ("Relocation %x unaligned @ %p\n", addend, where);
    163 			return -1;
    164 		}
    165 
    166 		if ((addend & 0xfe000000) != 0x00000000 &&
    167 		    (addend & 0xfe000000) != 0xfe000000) {
    168 			printf ("Relocation %x too far @ %p\n", addend, where);
    169 			return -1;
    170 		}
    171 		*where = (*where & 0xff000000) | ((addend >> 2) & 0x00ffffff);
    172 		return 0;
    173 
    174 	case R_ARM_REL32:	/* ((S + A) | T) -  P */
    175 		/* T = 0 for now */
    176 		error = kobj_sym_lookup(ko, symidx, &addr);
    177 		if (error)
    178 			break;
    179 
    180 		addend += (uintptr_t)addr - (uintptr_t)where;
    181 		*where = addend;
    182 		return 0;
    183 
    184 	case R_ARM_PREL31:	/* ((S + A) | T) -  P */
    185 		/* Sign extend if necessary */
    186 		if (addend & 0x40000000)
    187 			addend |= 0xc0000000;
    188 		/* T = 0 for now */
    189 		error = kobj_sym_lookup(ko, symidx, &addr);
    190 		if (error)
    191 			break;
    192 
    193 		addend += (uintptr_t)addr - (uintptr_t)where;
    194 
    195 		if ((addend & 0x80000000) != 0x00000000 &&
    196 		    (addend & 0x80000000) != 0x80000000) {
    197 			printf ("Relocation %x too far @ %p\n", addend, where);
    198 			return -1;
    199 		}
    200 
    201 		*where = (*where & 0x80000000) | (addend & 0x7fffffff);
    202 
    203 	default:
    204 		break;
    205 	}
    206 
    207 	printf("kobj_reloc: unexpected/invalid relocation type %d @ %p symidx %u\n",
    208 	    rtype, where, symidx);
    209 	return -1;
    210 }
    211 
    212 #if __ARMEB__
    213 
    214 enum be8_magic_sym_type {
    215 	Other, ArmStart, ThumbStart, DataStart
    216 };
    217 
    218 struct be8_marker {
    219 	enum be8_magic_sym_type type;
    220 	void *addr;
    221 };
    222 
    223 struct be8_marker_list {
    224 	size_t cnt;
    225 	struct be8_marker *markers;
    226 };
    227 
    228 /*
    229  * See ELF for the ARM Architecture, Section 4.5.5: Mapping Symbols
    230  * ARM reserves $a/$d/$t (and variants like $a.2) to mark start of
    231  * arm/thumb code sections to allow conversion from ARM32-EB to -BE8
    232  * format.
    233  */
    234 static enum be8_magic_sym_type
    235 be8_sym_type(const char *name, int info)
    236 {
    237 	if (ELF_ST_BIND(info) != STB_LOCAL)
    238 		return Other;
    239 	if (ELF_ST_TYPE(info) != STT_NOTYPE)
    240 		return Other;
    241 	if (name[0] != '$' || name[1] == '\0' ||
    242 	    (name[2] != '\0' && name[2] != '.'))
    243 		return Other;
    244 
    245 	switch (name[1]) {
    246 	case 'a':
    247 		return ArmStart;
    248 	case 'd':
    249 		return DataStart;
    250 	case 't':
    251 		return ThumbStart;
    252 	default:
    253 		return Other;
    254 	}
    255 }
    256 
    257 static int
    258 be8_ksym_count(const char *name, int symindex, void *value, uint32_t size,
    259 	int info, void *cookie)
    260 {
    261 	size_t *res = cookie;
    262 	enum be8_magic_sym_type t = be8_sym_type(name, info);
    263 
    264 	if (t != Other)
    265 		(*res)++;
    266 	return 0;
    267 }
    268 
    269 static int
    270 be8_ksym_add(const char *name, int symindex, void *value, uint32_t size,
    271 	int info, void *cookie)
    272 {
    273 	size_t ndx;
    274 	struct be8_marker_list *list = cookie;
    275 	enum be8_magic_sym_type t = be8_sym_type(name, info);
    276 
    277 	if (t == Other)
    278 		return 0;
    279 
    280 	ndx = list->cnt++;
    281 	list->markers[ndx].type = t;
    282 	list->markers[ndx].addr = value;
    283 
    284 	return 0;
    285 }
    286 
    287 static int
    288 be8_ksym_comp(const void *a, const void *b)
    289 {
    290 	const struct be8_marker *ma = a, *mb = b;
    291 	uintptr_t va = (uintptr_t)ma->addr, vb = (uintptr_t)mb->addr;
    292 
    293 	if (va == vb)
    294 		return 0;
    295 	if (va < vb)
    296 		return -1;
    297 	return 1;
    298 }
    299 
    300 static void
    301 be8_ksym_swap(void *start, size_t size, const struct be8_marker_list *list)
    302 {
    303 	uintptr_t va_end = (uintptr_t)start + size;
    304 	size_t i;
    305 	uint32_t *p32, *p32_end, v32;
    306 	uint16_t *p16, *p16_end, v16;
    307 
    308 	/* find first relevant list entry */
    309 	for (i = 0; i < list->cnt; i++)
    310 		if (start <= list->markers[i].addr)
    311 			break;
    312 
    313 	/* swap all arm and thumb code parts of this section */
    314 	for ( ; i < list->cnt; i++) {
    315 		switch (list->markers[i].type) {
    316 		case ArmStart:
    317 			p32 = (uint32_t*)list->markers[i].addr;
    318 			p32_end = (uint32_t*)va_end;
    319 			if (i+1 < list->cnt) {
    320 				if ((uintptr_t)list->markers[i+1].addr
    321 				    < va_end)
    322 					p32_end = (uint32_t*)
    323 						list->markers[i+1].addr;
    324 			}
    325 			while (p32 < p32_end) {
    326 				v32 = bswap32(*p32);
    327 				*p32++ = v32;
    328 			}
    329 			break;
    330 		case ThumbStart:
    331 			p16 = (uint16_t*)list->markers[i].addr;
    332 			p16_end = (uint16_t*)va_end;
    333 			if (i+1 < list->cnt) {
    334 				if ((uintptr_t)list->markers[i+1].addr
    335 				    < va_end)
    336 					p16_end = (uint16_t*)
    337 						list->markers[i+1].addr;
    338 			}
    339 			while (p16 < p16_end) {
    340 				v16 = bswap16(*p16);
    341 				*p16++ = v16;
    342 			}
    343 			break;
    344 		default:
    345 			break;
    346 		}
    347 	}
    348 }
    349 
    350 static void
    351 kobj_be8_fixup(kobj_t ko)
    352 {
    353 	size_t relsym_cnt = 0, i, msize;
    354 	struct be8_marker_list list;
    355 	struct be8_marker tmp;
    356 
    357 	/*
    358 	 * Count all special relocations symbols
    359 	 */
    360 	ksyms_mod_foreach(ko->ko_name, be8_ksym_count, &relsym_cnt);
    361 
    362 	/*
    363 	 * Provide storage for the address list and add the symbols
    364 	 */
    365 	list.cnt = 0;
    366 	msize = relsym_cnt*sizeof(*list.markers);
    367 	list.markers = kmem_alloc(msize, KM_SLEEP);
    368 	ksyms_mod_foreach(ko->ko_name, be8_ksym_add, &list);
    369 	KASSERT(list.cnt == relsym_cnt);
    370 
    371 	/*
    372 	 * Sort symbols by ascending address
    373 	 */
    374 	if (kheapsort(list.markers, relsym_cnt, sizeof(*list.markers),
    375 	    be8_ksym_comp, &tmp) != 0)
    376 		panic("could not sort be8 marker symbols");
    377 
    378 	/*
    379 	 * Apply swaps to the .text section (XXX we do not have the
    380 	 * section header available any more, it has been jetisoned
    381 	 * already, so we can not check for all PROGBIT sections).
    382 	 */
    383 	for (i = 0; i < ko->ko_nprogtab; i++) {
    384 		if (strcmp(ko->ko_progtab[i].name, ".text") != 0)
    385 			continue;
    386 		be8_ksym_swap(ko->ko_progtab[i].addr,
    387 		    (size_t)ko->ko_progtab[i].size,
    388 		    &list);
    389 	}
    390 
    391 	/*
    392 	 * Done, free list
    393 	 */
    394 	kmem_free(list.markers, msize);
    395 }
    396 #endif
    397 
    398 int
    399 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
    400 {
    401 
    402 	if (load) {
    403 #if __ARMEB__
    404 		if (CPU_IS_ARMV7_P() && base == (void*)ko->ko_text_address)
    405 			kobj_be8_fixup(ko);
    406 #endif
    407 #ifndef _RUMPKERNEL
    408 		cpu_idcache_wbinv_range((vaddr_t)base, size);
    409 		cpu_tlb_flushID();
    410 #endif
    411 	}
    412 
    413 	return 0;
    414 }
    415