Home | History | Annotate | Line # | Download | only in aarch64
kobj_machdep.c revision 1.1
      1  1.1  ryo /*	$NetBSD: kobj_machdep.c,v 1.1 2018/08/15 11:10:45 ryo Exp $	*/
      2  1.1  ryo 
      3  1.1  ryo /*
      4  1.1  ryo  * Copyright (c) 2018 Ryo Shimizu <ryo (at) nerv.org>
      5  1.1  ryo  * All rights reserved.
      6  1.1  ryo  *
      7  1.1  ryo  * Redistribution and use in source and binary forms, with or without
      8  1.1  ryo  * modification, are permitted provided that the following conditions
      9  1.1  ryo  * are met:
     10  1.1  ryo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ryo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ryo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ryo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ryo  *    documentation and/or other materials provided with the distribution.
     15  1.1  ryo  *
     16  1.1  ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  ryo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1  ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  1.1  ryo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  1.1  ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1  ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1  ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1  ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.1  ryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  ryo  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  ryo  */
     28  1.1  ryo 
     29  1.1  ryo #include <sys/cdefs.h>
     30  1.1  ryo __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.1 2018/08/15 11:10:45 ryo Exp $");
     31  1.1  ryo 
     32  1.1  ryo #define ELFSIZE		ARCH_ELFSIZE
     33  1.1  ryo 
     34  1.1  ryo #include "opt_ddb.h"
     35  1.1  ryo 
     36  1.1  ryo #include <sys/param.h>
     37  1.1  ryo #include <sys/kobj.h>
     38  1.1  ryo #include <sys/exec.h>
     39  1.1  ryo #include <sys/exec_elf.h>
     40  1.1  ryo #include <sys/errno.h>
     41  1.1  ryo #include <sys/queue.h>
     42  1.1  ryo #include <sys/tree.h>
     43  1.1  ryo 
     44  1.1  ryo #include <aarch64/cpufunc.h>
     45  1.1  ryo 
     46  1.1  ryo /* #define KOBJ_MACHDEP_DEBUG */
     47  1.1  ryo 
     48  1.1  ryo #ifdef KOBJ_MACHDEP_DEBUG
     49  1.1  ryo #ifdef DDB
     50  1.1  ryo #include <aarch64/db_machdep.h>	/* for strdisasm() */
     51  1.1  ryo #endif
     52  1.1  ryo 
     53  1.1  ryo struct rtypeinfo {
     54  1.1  ryo 	Elf_Word rtype;
     55  1.1  ryo 	const char *name;
     56  1.1  ryo };
     57  1.1  ryo 
     58  1.1  ryo static const struct rtypeinfo rtypetbl[] = {
     59  1.1  ryo 	{ R_AARCH64_ABS64,		"R_AARCH64_ABS64"		},
     60  1.1  ryo 	{ R_AARCH64_ADD_ABS_LO12_NC,	"R_AARCH64_ADD_ABS_LO12_NC"	},
     61  1.1  ryo 	{ R_AARCH_LDST64_ABS_LO12_NC,	"R_AARCH64_LDST64_ABS_LO12_NC"	},
     62  1.1  ryo 	{ R_AARCH_LDST32_ABS_LO12_NC,	"R_AARCH64_LDST32_ABS_LO12_NC"	},
     63  1.1  ryo 	{ R_AARCH_LDST16_ABS_LO12_NC,	"R_AARCH64_LDST16_ABS_LO12_NC"	},
     64  1.1  ryo 	{ R_AARCH64_LDST8_ABS_LO12_NC,	"R_AARCH64_LDST8_ABS_LO12_NC"	},
     65  1.1  ryo 	{ R_AARCH64_ADR_PREL_PG_HI21_NC, "R_AARCH64_ADR_PREL_PG_HI21_NC"},
     66  1.1  ryo 	{ R_AARCH64_ADR_PREL_PG_HI21,	"R_AARCH64_ADR_PREL_PG_HI21"	},
     67  1.1  ryo 	{ R_AARCH_JUMP26,		"R_AARCH64_JUMP26"		},
     68  1.1  ryo 	{ R_AARCH_CALL26,		"R_AARCH64_CALL26"		},
     69  1.1  ryo 	{ R_AARCH64_PREL32,		"R_AARCH64_PREL32"		},
     70  1.1  ryo 	{ R_AARCH64_PREL16,		"R_AARCH64_PREL16"		}
     71  1.1  ryo };
     72  1.1  ryo 
     73  1.1  ryo static const char *
     74  1.1  ryo strrtype(Elf_Word rtype)
     75  1.1  ryo {
     76  1.1  ryo 	int i;
     77  1.1  ryo 	static char buf[64];
     78  1.1  ryo 
     79  1.1  ryo 	for (i = 0; i < __arraycount(rtypetbl); i++) {
     80  1.1  ryo 		if (rtypetbl[i].rtype == rtype)
     81  1.1  ryo 			return rtypetbl[i].name;
     82  1.1  ryo 	}
     83  1.1  ryo 	snprintf(buf, sizeof(buf), "RELOCATION-TYPE-%d", rtype);
     84  1.1  ryo 	return buf;
     85  1.1  ryo }
     86  1.1  ryo #endif /* KOBJ_MACHDEP_DEBUG */
     87  1.1  ryo 
     88  1.1  ryo static inline bool
     89  1.1  ryo checkalign(Elf_Addr addr, int alignbyte, void *where, Elf64_Addr off)
     90  1.1  ryo {
     91  1.1  ryo 	if ((addr & (alignbyte - 1)) != 0) {
     92  1.1  ryo 		printf("kobj_reloc: Relocation 0x%jx unaligned at %p"
     93  1.1  ryo 		    " (base+0x%jx). must be aligned %d\n",
     94  1.1  ryo 		    (uintptr_t)addr, where, off, alignbyte);
     95  1.1  ryo 		return true;
     96  1.1  ryo 	}
     97  1.1  ryo 	return false;
     98  1.1  ryo }
     99  1.1  ryo 
    100  1.1  ryo static inline bool
    101  1.1  ryo checkoverflow(Elf_Addr addr, int bitwidth, Elf_Addr targetaddr,
    102  1.1  ryo     const char *bitscale, void *where, Elf64_Addr off)
    103  1.1  ryo {
    104  1.1  ryo 	const Elf_Addr mask = ~__BITS(bitwidth - 1, 0);
    105  1.1  ryo 
    106  1.1  ryo 	if (((addr & mask) != 0) && ((addr & mask) != mask)) {
    107  1.1  ryo 		printf("kobj_reloc: Relocation 0x%jx too far from %p"
    108  1.1  ryo 		    " (base+0x%jx) for %dbit%s\n",
    109  1.1  ryo 		    (uintptr_t)targetaddr, where, off, bitwidth, bitscale);
    110  1.1  ryo 		return true;
    111  1.1  ryo 	}
    112  1.1  ryo 	return false;
    113  1.1  ryo }
    114  1.1  ryo 
    115  1.1  ryo #define WIDTHMASK(w)	(0xffffffffffffffffUL >> (64 - (w)))
    116  1.1  ryo 
    117  1.1  ryo int
    118  1.1  ryo kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
    119  1.1  ryo     bool isrela, bool local)
    120  1.1  ryo {
    121  1.1  ryo 	Elf_Addr saddr, addend, raddr, val;
    122  1.1  ryo 	Elf64_Addr off, *where;
    123  1.1  ryo 	Elf32_Addr *where32;
    124  1.1  ryo 	uint16_t *where16;
    125  1.1  ryo 	Elf_Word rtype, symidx;
    126  1.1  ryo 	const Elf_Rela *rela;
    127  1.1  ryo 	int error;
    128  1.1  ryo 	uint32_t *insn, immhi, immlo, shift;
    129  1.1  ryo 	bool nc = false;
    130  1.1  ryo #ifdef KOBJ_MACHDEP_DEBUG
    131  1.1  ryo #ifdef DDB
    132  1.1  ryo 	char disasmbuf[256];
    133  1.1  ryo #endif
    134  1.1  ryo 	Elf_Addr old;
    135  1.1  ryo #endif /* KOBJ_MACHDEP_DEBUG */
    136  1.1  ryo 
    137  1.1  ryo 
    138  1.1  ryo #ifdef KOBJ_MACHDEP_DEBUG
    139  1.1  ryo 	printf("%s:%d: ko=%p, relocbase=0x%jx, data=%p"
    140  1.1  ryo 	    ", isrela=%d, local=%d\n", __func__, __LINE__,
    141  1.1  ryo 	    ko, relocbase, data, isrela, local);
    142  1.1  ryo #endif /* KOBJ_MACHDEP_DEBUG */
    143  1.1  ryo 
    144  1.1  ryo 	if (!isrela) {
    145  1.1  ryo 		printf("kobj_reloc: REL relocations not supported");
    146  1.1  ryo 		error = 1;
    147  1.1  ryo 		goto done;
    148  1.1  ryo 	}
    149  1.1  ryo 
    150  1.1  ryo 	rela = (const Elf_Rela *)data;
    151  1.1  ryo 	addend = rela->r_addend;
    152  1.1  ryo 	rtype = ELF_R_TYPE(rela->r_info);
    153  1.1  ryo 	symidx = ELF_R_SYM(rela->r_info);
    154  1.1  ryo 	off = rela->r_offset;
    155  1.1  ryo 	where = (Elf_Addr *)(relocbase + off);
    156  1.1  ryo 
    157  1.1  ryo 	/* pointer to 32bit, 16bit, and instruction */
    158  1.1  ryo 	where32 = (void *)where;
    159  1.1  ryo 	where16 = (void *)where;
    160  1.1  ryo 	insn = (uint32_t *)where;
    161  1.1  ryo 
    162  1.1  ryo 	/* no need to lookup any symbols */
    163  1.1  ryo 	switch (rtype) {
    164  1.1  ryo 	case R_AARCH64_NONE:
    165  1.1  ryo 	case R_AARCH64_NONE2:
    166  1.1  ryo 		return 0;
    167  1.1  ryo 	}
    168  1.1  ryo 
    169  1.1  ryo 	error = kobj_sym_lookup(ko, symidx, &saddr);
    170  1.1  ryo 	if (error != 0) {
    171  1.1  ryo 		printf("kobj_reloc: symidx %d lookup failure."
    172  1.1  ryo 		    " relocation type %d at %p (base+0x%jx)\n",
    173  1.1  ryo 		    symidx, rtype, where, off);
    174  1.1  ryo 		goto done;
    175  1.1  ryo 	}
    176  1.1  ryo 
    177  1.1  ryo #ifdef KOBJ_MACHDEP_DEBUG
    178  1.1  ryo 	printf("%s:%d: symidx=%d, saddr=0x%jx, addend=0x%jx\n",
    179  1.1  ryo 	    __func__, __LINE__, symidx, (uintptr_t)saddr, (uintptr_t)addend);
    180  1.1  ryo 	printf("%s:%d: rtype=%s, where=%p (base+0x%jx)\n",
    181  1.1  ryo 	    __func__, __LINE__, strrtype(rtype), where, off);
    182  1.1  ryo 	old = *where;
    183  1.1  ryo #ifdef DDB
    184  1.1  ryo 	snprintf(disasmbuf, sizeof(disasmbuf), "%08x %s",
    185  1.1  ryo 	    *insn, strdisasm((vaddr_t)insn));
    186  1.1  ryo #endif
    187  1.1  ryo #endif /* KOBJ_MACHDEP_DEBUG */
    188  1.1  ryo 
    189  1.1  ryo 	switch (rtype) {
    190  1.1  ryo 	case R_AARCH64_ABS64:
    191  1.1  ryo 		/*
    192  1.1  ryo 		 * S + A
    193  1.1  ryo 		 *  e.g.) .quad <sym>+addend
    194  1.1  ryo 		 */
    195  1.1  ryo 		*where = saddr + addend;
    196  1.1  ryo 		break;
    197  1.1  ryo 	case R_AARCH64_ABS32:
    198  1.1  ryo 		/*
    199  1.1  ryo 		 * S + A
    200  1.1  ryo 		 *  e.g.) .word <sym>+addend
    201  1.1  ryo 		 */
    202  1.1  ryo 		*where32 = saddr + addend;
    203  1.1  ryo 		break;
    204  1.1  ryo 	case R_AARCH64_ABS16:
    205  1.1  ryo 		/*
    206  1.1  ryo 		 * S + A
    207  1.1  ryo 		 *  e.g.) .short <sym>+addend
    208  1.1  ryo 		 */
    209  1.1  ryo 		*where16 = saddr + addend;
    210  1.1  ryo 		break;
    211  1.1  ryo 	case R_AARCH64_ADD_ABS_LO12_NC:
    212  1.1  ryo 	case R_AARCH64_LDST8_ABS_LO12_NC:
    213  1.1  ryo 	case R_AARCH_LDST16_ABS_LO12_NC:
    214  1.1  ryo 	case R_AARCH_LDST32_ABS_LO12_NC:
    215  1.1  ryo 	case R_AARCH_LDST64_ABS_LO12_NC:
    216  1.1  ryo 		switch (rtype) {
    217  1.1  ryo 		case R_AARCH64_ADD_ABS_LO12_NC:
    218  1.1  ryo 		case R_AARCH64_LDST8_ABS_LO12_NC:
    219  1.1  ryo 			shift = 0;
    220  1.1  ryo 			break;
    221  1.1  ryo 		case R_AARCH_LDST16_ABS_LO12_NC:
    222  1.1  ryo 			shift = 1;
    223  1.1  ryo 			break;
    224  1.1  ryo 		case R_AARCH_LDST32_ABS_LO12_NC:
    225  1.1  ryo 			shift = 2;
    226  1.1  ryo 			break;
    227  1.1  ryo 		case R_AARCH_LDST64_ABS_LO12_NC:
    228  1.1  ryo 			shift = 3;
    229  1.1  ryo 			break;
    230  1.1  ryo 		default:
    231  1.1  ryo 			panic("illegal rtype: %d\n", rtype);
    232  1.1  ryo 		}
    233  1.1  ryo 		/*
    234  1.1  ryo 		 * S + A
    235  1.1  ryo 		 *  e.g.) add  x0,x0,#:lo12:<sym>+<addend>
    236  1.1  ryo 		 *        ldrb w0,[x0,#:lo12:<sym>+<addend>]
    237  1.1  ryo 		 *        ldrh w0,[x0,#:lo12:<sym>+<addend>]
    238  1.1  ryo 		 *        ldr  w0,[x0,#:lo12:<sym>+<addend>]
    239  1.1  ryo 		 *        ldr  x0,[x0,#:lo12:<sym>+<addend>]
    240  1.1  ryo 		 */
    241  1.1  ryo 		val = saddr + addend;
    242  1.1  ryo 		if (checkalign(val, 1 << shift, where, off)) {
    243  1.1  ryo 			error = 1;
    244  1.1  ryo 			break;
    245  1.1  ryo 		}
    246  1.1  ryo 		val &= WIDTHMASK(12);
    247  1.1  ryo 		val >>= shift;
    248  1.1  ryo 		*insn = (*insn & ~__BITS(21,10)) | (val << 10);
    249  1.1  ryo 		break;
    250  1.1  ryo 
    251  1.1  ryo 	case R_AARCH64_ADR_PREL_PG_HI21_NC:
    252  1.1  ryo 		nc = true;
    253  1.1  ryo 		/* FALLTHRU */
    254  1.1  ryo 	case R_AARCH64_ADR_PREL_PG_HI21:
    255  1.1  ryo 		/*
    256  1.1  ryo 		 * Page(S + A) - Page(P)
    257  1.1  ryo 		 *  e.g.) adrp x0,<sym>+<addend>
    258  1.1  ryo 		 */
    259  1.1  ryo 		val = saddr + addend;
    260  1.1  ryo 		val = val >> 12;
    261  1.1  ryo 		raddr = val << 12;
    262  1.1  ryo 		val -= (uintptr_t)where >> 12;
    263  1.1  ryo 		if (!nc && checkoverflow(val, 21, val, " x 4k", where, off)) {
    264  1.1  ryo 			error = 1;
    265  1.1  ryo 			break;
    266  1.1  ryo 		}
    267  1.1  ryo 		immlo = val & WIDTHMASK(2);
    268  1.1  ryo 		immhi = (val >> 2) & WIDTHMASK(19);
    269  1.1  ryo 		*insn = (*insn & ~(__BITS(30,29) | __BITS(23,5))) |
    270  1.1  ryo 		    (immlo << 29) | (immhi << 5);
    271  1.1  ryo 		break;
    272  1.1  ryo 
    273  1.1  ryo 	case R_AARCH_JUMP26:
    274  1.1  ryo 	case R_AARCH_CALL26:
    275  1.1  ryo 		/*
    276  1.1  ryo 		 * S + A - P
    277  1.1  ryo 		 *  e.g.) b <sym>+<addend>
    278  1.1  ryo 		 *        bl <sym>+<addend>
    279  1.1  ryo 		 */
    280  1.1  ryo 		raddr = saddr + addend;
    281  1.1  ryo 		val = raddr - (uintptr_t)where;
    282  1.1  ryo 		if (checkalign(val, 4, where, off)) {
    283  1.1  ryo 			error = 1;
    284  1.1  ryo 			break;
    285  1.1  ryo 		}
    286  1.1  ryo 		val = (intptr_t)val >> 2;
    287  1.1  ryo 		if (checkoverflow(val, 26, val, " word", where, off)) {
    288  1.1  ryo 			error = 1;
    289  1.1  ryo 			break;
    290  1.1  ryo 		}
    291  1.1  ryo 		val &= WIDTHMASK(26);
    292  1.1  ryo 		*insn = (*insn & ~__BITS(25,0)) | val;
    293  1.1  ryo 		break;
    294  1.1  ryo 
    295  1.1  ryo 	case R_AARCH64_PREL64:
    296  1.1  ryo 		/*
    297  1.1  ryo 		 * S + A - P
    298  1.1  ryo 		 *  e.g.) 1: .quad <sym>+<addend>-1b
    299  1.1  ryo 		 */
    300  1.1  ryo 		raddr = saddr + addend;
    301  1.1  ryo 		val = raddr - (uintptr_t)where;
    302  1.1  ryo 		if (checkoverflow(val, 64, val, "", where, off)) {
    303  1.1  ryo 			error = 1;
    304  1.1  ryo 			break;
    305  1.1  ryo 		}
    306  1.1  ryo 		*where = val;
    307  1.1  ryo 		break;
    308  1.1  ryo 	case R_AARCH64_PREL32:
    309  1.1  ryo 		/*
    310  1.1  ryo 		 * S + A - P
    311  1.1  ryo 		 *  e.g.) 1: .word <sym>+<addend>-1b
    312  1.1  ryo 		 */
    313  1.1  ryo 		raddr = saddr + addend;
    314  1.1  ryo 		val = raddr - (uintptr_t)where;
    315  1.1  ryo 		if (checkoverflow(val, 32, val, "", where, off)) {
    316  1.1  ryo 			error = 1;
    317  1.1  ryo 			break;
    318  1.1  ryo 		}
    319  1.1  ryo 		*where32 = val;
    320  1.1  ryo 		break;
    321  1.1  ryo 	case R_AARCH64_PREL16:
    322  1.1  ryo 		/*
    323  1.1  ryo 		 * S + A - P
    324  1.1  ryo 		 *  e.g.) 1: .short <sym>+<addend>-1b
    325  1.1  ryo 		 */
    326  1.1  ryo 		raddr = saddr + addend;
    327  1.1  ryo 		val = raddr - (uintptr_t)where;
    328  1.1  ryo 		if (checkoverflow(val, 16, val, "", where, off)) {
    329  1.1  ryo 			error = 1;
    330  1.1  ryo 			break;
    331  1.1  ryo 		}
    332  1.1  ryo 		*where16 = val;
    333  1.1  ryo 		break;
    334  1.1  ryo 	default:
    335  1.1  ryo 		printf("kobj_reloc: unsupported relocation type %d"
    336  1.1  ryo 		    " at %p (base+0x%jx) symidx %u\n",
    337  1.1  ryo 		    rtype, where, off, symidx);
    338  1.1  ryo 		error = 1;
    339  1.1  ryo 		break;
    340  1.1  ryo 	}
    341  1.1  ryo 
    342  1.1  ryo #ifdef KOBJ_MACHDEP_DEBUG
    343  1.1  ryo 	printf("%s: reloc\n", __func__);
    344  1.1  ryo 	printf("%s:  *where %016jx\n", __func__, (uintptr_t)old);
    345  1.1  ryo 	printf("%s:      -> %016jx\n", __func__, (uintptr_t)*where);
    346  1.1  ryo #ifdef DDB
    347  1.1  ryo 	printf("%s:    insn %s\n", __func__, disasmbuf);
    348  1.1  ryo 	printf("%s:      -> %08x %s\n", __func__,
    349  1.1  ryo 	    *insn, strdisasm((vaddr_t)insn));
    350  1.1  ryo #endif
    351  1.1  ryo 	printf("\n");
    352  1.1  ryo #endif /* KOBJ_MACHDEP_DEBUG */
    353  1.1  ryo 
    354  1.1  ryo  done:
    355  1.1  ryo 	if (error != 0)
    356  1.1  ryo 		return -1;
    357  1.1  ryo 	return 0;
    358  1.1  ryo }
    359  1.1  ryo 
    360  1.1  ryo int
    361  1.1  ryo kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
    362  1.1  ryo {
    363  1.1  ryo 	return 0;
    364  1.1  ryo }
    365