elf_machdep.c revision 100384
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 100384 2002-07-20 02:56:12Z peter $
4080708Sjake */
4180708Sjake
4280708Sjake#include <sys/param.h>
43100384Speter#include <sys/kernel.h>
4480708Sjake#include <sys/systm.h>
4580708Sjake#include <sys/linker.h>
46100384Speter#include <sys/sysent.h>
47100384Speter#include <sys/imgact_elf.h>
48100384Speter#include <sys/syscall.h>
49100384Speter#include <sys/signalvar.h>
50100384Speter#include <sys/vnode.h>
5180708Sjake#include <machine/elf.h>
52100384Speter#include <machine/md_var.h>
5380708Sjake
5485586Sjake#include "linker_if.h"
5585586Sjake
56100384Speterstruct sysentvec elf64_freebsd_sysvec = {
57100384Speter	SYS_MAXSYSCALL,
58100384Speter	sysent,
59100384Speter	0,
60100384Speter	0,
61100384Speter	0,
62100384Speter	0,
63100384Speter	0,
64100384Speter	0,
65100384Speter	elf64_freebsd_fixup,
66100384Speter	sendsig,
67100384Speter	sigcode,
68100384Speter	&szsigcode,
69100384Speter	0,
70100384Speter	"FreeBSD ELF64",
71100384Speter	__elfN(coredump),
72100384Speter	NULL,
73100384Speter	MINSIGSTKSZ
74100384Speter};
75100384Speter
76100384Speterstatic Elf64_Brandinfo freebsd_brand_info = {
77100384Speter						ELFOSABI_FREEBSD,
78100384Speter						EM_SPARCV9,
79100384Speter						"FreeBSD",
80100384Speter						"",
81100384Speter						"/usr/libexec/ld-elf.so.1",
82100384Speter						&elf64_freebsd_sysvec
83100384Speter					  };
84100384Speter
85100384SpeterSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
86100384Speter	(sysinit_cfunc_t) elf64_insert_brand_entry,
87100384Speter	&freebsd_brand_info);
88100384Speter
8985586Sjake/*
9085586Sjake * The following table holds for each relocation type:
9185586Sjake *	- the width in bits of the memory location the relocation
9285586Sjake *	  applies to (not currently used)
9385586Sjake *	- the number of bits the relocation value must be shifted to the
9485586Sjake *	  right (i.e. discard least significant bits) to fit into
9585586Sjake *	  the appropriate field in the instruction word.
9685586Sjake *	- flags indicating whether
9785586Sjake *		* the relocation involves a symbol
9885586Sjake *		* the relocation is relative to the current position
9985586Sjake *		* the relocation is for a GOT entry
10085586Sjake *		* the relocation is relative to the load address
10185586Sjake *
10285586Sjake */
10385586Sjake#define _RF_S		0x80000000		/* Resolve symbol */
10485586Sjake#define _RF_A		0x40000000		/* Use addend */
10585586Sjake#define _RF_P		0x20000000		/* Location relative */
10685586Sjake#define _RF_G		0x10000000		/* GOT offset */
10785586Sjake#define _RF_B		0x08000000		/* Load address relative */
10885586Sjake#define _RF_U		0x04000000		/* Unaligned */
10985586Sjake#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
11085586Sjake#define _RF_RS(s)	( (s) & 0xff)		/* right shift */
11185586Sjakestatic int reloc_target_flags[] = {
11285586Sjake	0,							/* NONE */
11385586Sjake	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* RELOC_8 */
11485586Sjake	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
11585586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32 */
11685586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
11785586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
11885586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
11985586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
12085586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
12185586Sjake	      _RF_A|	  _RF_B|_RF_SZ(32) | _RF_RS(10),	/* HI22 */
12285586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 22 */
12385586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 13 */
12485586Sjake	      _RF_A|	  _RF_B|_RF_SZ(32) | _RF_RS(0),		/* LO10 */
12585586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
12685586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
12785586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
12885586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
12985586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
13085586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
13185586Sjake				_RF_SZ(32) | _RF_RS(0),		/* COPY */
13285586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
13385586Sjake				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
13485586Sjake	      _RF_A|	  _RF_B|_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
13585586Sjake	_RF_S|_RF_A|	  _RF_U|_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
13685586Sjake
13785586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
13885586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
13985586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
14085586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
14185586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
14285586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
14385586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 */
14485586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 */
14585586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* 64 */
14685586Sjake	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
14785586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(42),	/* HH22 */
14885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(32),	/* HM10 */
14985586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LM22 */
15085586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
15185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
15285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
15385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
15485586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
15585586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
15685586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 7 */
15785586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 5 */
15885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 6 */
15985586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
16085586Sjake	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
16185586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
16285586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
16385586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(22),	/* H44 */
16485586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(12),	/* M44 */
16585586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* L44 */
16685586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
16785586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
16885586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
16985586Sjake};
17085586Sjake
17185586Sjake#define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
17285586Sjake#define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
17385586Sjake#define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
17485586Sjake#define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
17585586Sjake#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
17685586Sjake#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
17785586Sjake#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
17885586Sjake
17985586Sjakestatic long reloc_target_bitmask[] = {
18085586Sjake#define _BM(x)	(~(-(1ULL << (x))))
18185586Sjake	0,				/* NONE */
18285586Sjake	_BM(8), _BM(16), _BM(32),	/* RELOC_8, _16, _32 */
18385586Sjake	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
18485586Sjake	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
18585586Sjake	_BM(22), _BM(22),		/* HI22, _22 */
18685586Sjake	_BM(13), _BM(10),		/* RELOC_13, _LO10 */
18785586Sjake	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
18885586Sjake	_BM(10), _BM(22),		/* _PC10, _PC22 */
18985586Sjake	_BM(30), 0,			/* _WPLT30, _COPY */
19085586Sjake	_BM(32), _BM(32), _BM(32),	/* _GLOB_DAT, JMP_SLOT, _RELATIVE */
19185586Sjake	_BM(32), _BM(32),		/* _UA32, PLT32 */
19285586Sjake	_BM(22), _BM(10),		/* _HIPLT22, LOPLT10 */
19385586Sjake	_BM(32), _BM(22), _BM(10),	/* _PCPLT32, _PCPLT22, _PCPLT10 */
19485586Sjake	_BM(10), _BM(11), -1,		/* _10, _11, _64 */
19585586Sjake	_BM(10), _BM(22),		/* _OLO10, _HH22 */
19685586Sjake	_BM(10), _BM(22),		/* _HM10, _LM22 */
19785586Sjake	_BM(22), _BM(10), _BM(22),	/* _PC_HH22, _PC_HM10, _PC_LM22 */
19885586Sjake	_BM(16), _BM(19),		/* _WDISP16, _WDISP19 */
19985586Sjake	-1,				/* GLOB_JMP */
20085586Sjake	_BM(7), _BM(5), _BM(6)		/* _7, _5, _6 */
20185586Sjake	-1, -1,				/* DISP64, PLT64 */
20285586Sjake	_BM(22), _BM(13),		/* HIX22, LOX10 */
20385586Sjake	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
20485586Sjake	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
20585586Sjake#undef _BM
20685586Sjake};
20785586Sjake#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
20885586Sjake
20985586Sjake/* Process one elf relocation with addend. */
21080708Sjakeint
21195410Smarcelelf_reloc(linker_file_t lf, const void *data, int type)
21280708Sjake{
21385586Sjake	const Elf_Rela *rela;
21485586Sjake	Elf_Addr relocbase;
21585586Sjake	Elf_Half *where32;
21685586Sjake	Elf_Addr *where;
21795410Smarcel	Elf_Word rtype, symidx;
21885586Sjake	Elf_Addr value;
21985586Sjake	Elf_Addr mask;
22098635Smux	Elf_Addr addr;
22185586Sjake
22285586Sjake	if (type != ELF_RELOC_RELA)
22385586Sjake		return (-1);
22485586Sjake
22585586Sjake	relocbase = (Elf_Addr)lf->address;
22685586Sjake	rela = (const Elf_Rela *)data;
22785586Sjake	where = (Elf_Addr *)(relocbase + rela->r_offset);
22885586Sjake	where32 = (Elf_Half *)where;
22985586Sjake	rtype = ELF_R_TYPE(rela->r_info);
23095410Smarcel	symidx = ELF_R_SYM(rela->r_info);
23185586Sjake
23285586Sjake	if (rtype == R_SPARC_NONE)
23385586Sjake		return (0);
23485586Sjake
23585586Sjake	if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
23685586Sjake	    rtype > R_SPARC_UA16)
23785586Sjake		return (-1);
23885586Sjake
23985586Sjake	if (RELOC_UNALIGNED(rtype))
24085586Sjake		return (-1);
24185586Sjake
24285586Sjake	value = rela->r_addend;
24385586Sjake
24485586Sjake	if (RELOC_RESOLVE_SYMBOL(rtype)) {
24595410Smarcel		addr = elf_lookup(lf, symidx, 1);
24685586Sjake		if (addr == 0)
24785586Sjake			return (-1);
24898635Smux		value += addr;
24985586Sjake	}
25085586Sjake
25185586Sjake	if (RELOC_PC_RELATIVE(rtype))
25285586Sjake		value -= (Elf_Addr)where;
25385586Sjake
25485586Sjake	if (RELOC_BASE_RELATIVE(rtype))
25598635Smux		value += relocbase;
25685586Sjake
25785586Sjake	mask = RELOC_VALUE_BITMASK(rtype);
25885586Sjake	value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
25985586Sjake	value &= mask;
26085586Sjake
26185586Sjake	if (RELOC_TARGET_SIZE(rtype) > 32) {
26285586Sjake		*where &= ~mask;
26385586Sjake		*where |= value;
26485586Sjake	} else {
26585586Sjake		*where32 &= ~mask;
26685586Sjake		*where32 |= value;
26785586Sjake	}
26885586Sjake
26980708Sjake	return (0);
27080708Sjake}
271