elf_machdep.c revision 102808
180708Sjake/*-
280708Sjake * Copyright (c) 2001 Jake Burkholder.
385586Sjake * Copyright (c) 2000 Eduardo Horvath.
485586Sjake * Copyright (c) 1999 The NetBSD Foundation, Inc.
580708Sjake * All rights reserved.
680708Sjake *
785586Sjake * This code is derived from software contributed to The NetBSD Foundation
885586Sjake * by Paul Kranenburg.
985586Sjake *
1080708Sjake * Redistribution and use in source and binary forms, with or without
1180708Sjake * modification, are permitted provided that the following conditions
1280708Sjake * are met:
1380708Sjake * 1. Redistributions of source code must retain the above copyright
1480708Sjake *    notice, this list of conditions and the following disclaimer.
1580708Sjake * 2. Redistributions in binary form must reproduce the above copyright
1680708Sjake *    notice, this list of conditions and the following disclaimer in the
1780708Sjake *    documentation and/or other materials provided with the distribution.
1885586Sjake * 3. All advertising materials mentioning features or use of this software
1985586Sjake *    must display the following acknowledgement:
2085586Sjake *        This product includes software developed by the NetBSD
2185586Sjake *        Foundation, Inc. and its contributors.
2285586Sjake * 4. Neither the name of The NetBSD Foundation nor the names of its
2385586Sjake *    contributors may be used to endorse or promote products derived
2485586Sjake *    from this software without specific prior written permission.
2580708Sjake *
2685586Sjake * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2785586Sjake * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2885586Sjake * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2985586Sjake * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3085586Sjake * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3185586Sjake * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3285586Sjake * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3385586Sjake * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3485586Sjake * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3585586Sjake * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3685586Sjake * POSSIBILITY OF SUCH DAMAGE.
3780708Sjake *
3885586Sjake *	from: NetBSD: mdreloc.c,v 1.5 2001/04/25 12:24:51 kleink Exp
3980708Sjake * $FreeBSD: head/sys/sparc64/sparc64/elf_machdep.c 102808 2002-09-01 21:41:24Z jake $
4080708Sjake */
4180708Sjake
4280708Sjake#include <sys/param.h>
43100384Speter#include <sys/kernel.h>
4480708Sjake#include <sys/systm.h>
45102808Sjake#include <sys/exec.h>
46102808Sjake#include <sys/imgact.h>
4780708Sjake#include <sys/linker.h>
48100384Speter#include <sys/sysent.h>
49100384Speter#include <sys/imgact_elf.h>
50100384Speter#include <sys/syscall.h>
51100384Speter#include <sys/signalvar.h>
52100384Speter#include <sys/vnode.h>
53102808Sjake
54102808Sjake#include <vm/vm.h>
55102808Sjake#include <vm/vm_param.h>
56102808Sjake
5780708Sjake#include <machine/elf.h>
5880708Sjake
5985586Sjake#include "linker_if.h"
6085586Sjake
61100384Speterstruct sysentvec elf64_freebsd_sysvec = {
62100384Speter	SYS_MAXSYSCALL,
63100384Speter	sysent,
64100384Speter	0,
65100384Speter	0,
66102808Sjake	NULL,
67100384Speter	0,
68102808Sjake	NULL,
69102808Sjake	NULL,
70102808Sjake	__elfN(freebsd_fixup),
71100384Speter	sendsig,
72102555Sjake	NULL,
73102555Sjake	NULL,
74102808Sjake	NULL,
75100384Speter	"FreeBSD ELF64",
76100384Speter	__elfN(coredump),
77100384Speter	NULL,
78102808Sjake	MINSIGSTKSZ,
79102808Sjake	PAGE_SIZE,
80102808Sjake	VM_MIN_ADDRESS,
81102808Sjake	VM_MAXUSER_ADDRESS,
82102808Sjake	USRSTACK,
83102808Sjake	PS_STRINGS,
84102808Sjake	VM_PROT_READ | VM_PROT_WRITE,
85102808Sjake	exec_copyout_strings,
86102808Sjake	exec_setregs
87100384Speter};
88100384Speter
89100384Speterstatic Elf64_Brandinfo freebsd_brand_info = {
90100384Speter						ELFOSABI_FREEBSD,
91100384Speter						EM_SPARCV9,
92100384Speter						"FreeBSD",
93100384Speter						"",
94100384Speter						"/usr/libexec/ld-elf.so.1",
95100384Speter						&elf64_freebsd_sysvec
96100384Speter					  };
97100384Speter
98100384SpeterSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
99100384Speter	(sysinit_cfunc_t) elf64_insert_brand_entry,
100100384Speter	&freebsd_brand_info);
101100384Speter
10285586Sjake/*
10385586Sjake * The following table holds for each relocation type:
10485586Sjake *	- the width in bits of the memory location the relocation
10585586Sjake *	  applies to (not currently used)
10685586Sjake *	- the number of bits the relocation value must be shifted to the
10785586Sjake *	  right (i.e. discard least significant bits) to fit into
10885586Sjake *	  the appropriate field in the instruction word.
10985586Sjake *	- flags indicating whether
11085586Sjake *		* the relocation involves a symbol
11185586Sjake *		* the relocation is relative to the current position
11285586Sjake *		* the relocation is for a GOT entry
11385586Sjake *		* the relocation is relative to the load address
11485586Sjake *
11585586Sjake */
11685586Sjake#define _RF_S		0x80000000		/* Resolve symbol */
11785586Sjake#define _RF_A		0x40000000		/* Use addend */
11885586Sjake#define _RF_P		0x20000000		/* Location relative */
11985586Sjake#define _RF_G		0x10000000		/* GOT offset */
12085586Sjake#define _RF_B		0x08000000		/* Load address relative */
12185586Sjake#define _RF_U		0x04000000		/* Unaligned */
12285586Sjake#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
12385586Sjake#define _RF_RS(s)	( (s) & 0xff)		/* right shift */
12485586Sjakestatic int reloc_target_flags[] = {
12585586Sjake	0,							/* NONE */
12685586Sjake	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
12785586Sjake	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
12885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
12985586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
13085586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
13185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
13285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
13385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
13485586Sjake	      _RF_A|	  _RF_B|_RF_SZ(32) | _RF_RS(10),	/* HI22 */
13585586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
13685586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
13785586Sjake	      _RF_A|	  _RF_B|_RF_SZ(32) | _RF_RS(0),		/* LO10 */
13885586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
13985586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
14085586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
14185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
14285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
14385586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
14485586Sjake				_RF_SZ(32) | _RF_RS(0),		/* COPY */
14585586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
14685586Sjake				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
14785586Sjake	      _RF_A|	  _RF_B|_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
14885586Sjake	_RF_S|_RF_A|	  _RF_U|_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
14985586Sjake
15085586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
15185586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
15285586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
15385586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
15485586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
15585586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
15685586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
15785586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
15885586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
15985586Sjake	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
16085586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
16185586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
16285586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
16385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
16485586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
16585586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
16685586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
16785586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
16885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
16985586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
17085586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
17185586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
17285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
17385586Sjake	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
17485586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
17585586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
17685586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
17785586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
17885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
17985586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
18085586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
18185586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
18285586Sjake};
18385586Sjake
18485586Sjake#define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
18585586Sjake#define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
18685586Sjake#define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
18785586Sjake#define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
18885586Sjake#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
18985586Sjake#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
19085586Sjake#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
19185586Sjake
19285586Sjakestatic long reloc_target_bitmask[] = {
19385586Sjake#define _BM(x)	(~(-(1ULL << (x))))
19485586Sjake	0,				/* NONE */
19585586Sjake	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
19685586Sjake	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
19785586Sjake	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
19885586Sjake	_BM(22), _BM(22),		/* HI22, _22 */
19985586Sjake	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
20085586Sjake	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
20185586Sjake	_BM(10), _BM(22),		/* _PC10, _PC22 */
20285586Sjake	_BM(30), 0,			/* _WPLT30, _COPY */
20385586Sjake	_BM(32), _BM(32), _BM(32),	/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
20485586Sjake	_BM(32), _BM(32),		/* _UA32, PLT32 */
20585586Sjake	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
20685586Sjake	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
20785586Sjake	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
20885586Sjake	_BM(10), _BM(22),		/* _OLO10, _HH22 */
20985586Sjake	_BM(10), _BM(22),		/* _HM10, _LM22 */
21085586Sjake	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
21185586Sjake	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
21285586Sjake	-1,				/* GLOB_JMP */
21385586Sjake	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
21485586Sjake	-1, -1,				/* DISP64, PLT64 */
21585586Sjake	_BM(22), _BM(13),		/* HIX22, LOX10 */
21685586Sjake	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
21785586Sjake	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
21885586Sjake#undef _BM
21985586Sjake};
22085586Sjake#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
22185586Sjake
22285586Sjake/* Process one elf relocation with addend. */
22380708Sjakeint
22495410Smarcelelf_reloc(linker_file_t lf, const void *data, int type)
22580708Sjake{
22685586Sjake	const Elf_Rela *rela;
22785586Sjake	Elf_Addr relocbase;
22885586Sjake	Elf_Half *where32;
22985586Sjake	Elf_Addr *where;
23095410Smarcel	Elf_Word rtype, symidx;
23185586Sjake	Elf_Addr value;
23285586Sjake	Elf_Addr mask;
23398635Smux	Elf_Addr addr;
23485586Sjake
23585586Sjake	if (type != ELF_RELOC_RELA)
23685586Sjake		return (-1);
23785586Sjake
23885586Sjake	relocbase = (Elf_Addr)lf->address;
23985586Sjake	rela = (const Elf_Rela *)data;
24085586Sjake	where = (Elf_Addr *)(relocbase + rela->r_offset);
24185586Sjake	where32 = (Elf_Half *)where;
24285586Sjake	rtype = ELF_R_TYPE(rela->r_info);
24395410Smarcel	symidx = ELF_R_SYM(rela->r_info);
24485586Sjake
24585586Sjake	if (rtype == R_SPARC_NONE)
24685586Sjake		return (0);
24785586Sjake
24885586Sjake	if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
24985586Sjake	    rtype > R_SPARC_UA16)
25085586Sjake		return (-1);
25185586Sjake
25285586Sjake	if (RELOC_UNALIGNED(rtype))
25385586Sjake		return (-1);
25485586Sjake
25585586Sjake	value = rela->r_addend;
25685586Sjake
25785586Sjake	if (RELOC_RESOLVE_SYMBOL(rtype)) {
25895410Smarcel		addr = elf_lookup(lf, symidx, 1);
25985586Sjake		if (addr == 0)
26085586Sjake			return (-1);
26198635Smux		value += addr;
26285586Sjake	}
26385586Sjake
26485586Sjake	if (RELOC_PC_RELATIVE(rtype))
26585586Sjake		value -= (Elf_Addr)where;
26685586Sjake
26785586Sjake	if (RELOC_BASE_RELATIVE(rtype))
26898635Smux		value += relocbase;
26985586Sjake
27085586Sjake	mask = RELOC_VALUE_BITMASK(rtype);
27185586Sjake	value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
27285586Sjake	value &= mask;
27385586Sjake
27485586Sjake	if (RELOC_TARGET_SIZE(rtype) > 32) {
27585586Sjake		*where &= ~mask;
27685586Sjake		*where |= value;
27785586Sjake	} else {
27885586Sjake		*where32 &= ~mask;
27985586Sjake		*where32 |= value;
28085586Sjake	}
28185586Sjake
28280708Sjake	return (0);
28380708Sjake}
284