elf32_machdep.c revision 129282
1/*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: head/sys/powerpc/powerpc/elf_machdep.c 129282 2004-05-16 20:00:28Z peter $
26 */
27
28#include <sys/param.h>
29#include <sys/kernel.h>
30#include <sys/systm.h>
31#include <sys/exec.h>
32#include <sys/imgact.h>
33#include <sys/malloc.h>
34#include <sys/proc.h>
35#include <sys/namei.h>
36#include <sys/fcntl.h>
37#include <sys/sysent.h>
38#include <sys/imgact_elf.h>
39#include <sys/syscall.h>
40#include <sys/signalvar.h>
41#include <sys/vnode.h>
42#include <sys/linker.h>
43
44#include <vm/vm.h>
45#include <vm/vm_param.h>
46
47#include <machine/elf.h>
48#include <machine/md_var.h>
49
50struct sysentvec elf32_freebsd_sysvec = {
51	SYS_MAXSYSCALL,
52	sysent,
53	0,
54	0,
55	NULL,
56	0,
57	NULL,
58	NULL,
59	__elfN(freebsd_fixup),
60	sendsig,
61	sigcode,
62	&szsigcode,
63	NULL,
64	"FreeBSD ELF32",
65	__elfN(coredump),
66	NULL,
67	MINSIGSTKSZ,
68	PAGE_SIZE,
69	VM_MIN_ADDRESS,
70	VM_MAXUSER_ADDRESS,
71	USRSTACK,
72	PS_STRINGS,
73	VM_PROT_ALL,
74	exec_copyout_strings,
75	exec_setregs,
76	NULL
77};
78
79static Elf32_Brandinfo freebsd_brand_info = {
80						ELFOSABI_FREEBSD,
81						EM_PPC,
82						"FreeBSD",
83						NULL,
84						"/libexec/ld-elf.so.1",
85						&elf32_freebsd_sysvec,
86						NULL,
87					  };
88
89SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
90	(sysinit_cfunc_t) elf32_insert_brand_entry,
91	&freebsd_brand_info);
92
93static Elf32_Brandinfo freebsd_brand_oinfo = {
94						ELFOSABI_FREEBSD,
95						EM_PPC,
96						"FreeBSD",
97						NULL,
98						"/usr/libexec/ld-elf.so.1",
99						&elf32_freebsd_sysvec,
100						NULL,
101					  };
102
103SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
104	(sysinit_cfunc_t) elf32_insert_brand_entry,
105	&freebsd_brand_oinfo);
106
107/* Process one elf relocation with addend. */
108static int
109elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
110    int type, int local, elf_lookup_fn lookup)
111{
112	Elf_Addr *where;
113	Elf_Addr addr;
114	Elf_Addr addend;
115	Elf_Word rtype, symidx;
116	const Elf_Rel *rel;
117	const Elf_Rela *rela;
118
119	switch (type) {
120	case ELF_RELOC_REL:
121		rel = (const Elf_Rel *)data;
122		where = (Elf_Addr *) (relocbase + rel->r_offset);
123		addend = *where;
124		rtype = ELF_R_TYPE(rel->r_info);
125		symidx = ELF_R_SYM(rel->r_info);
126		break;
127	case ELF_RELOC_RELA:
128		rela = (const Elf_Rela *)data;
129		where = (Elf_Addr *) (relocbase + rela->r_offset);
130		addend = rela->r_addend;
131		rtype = ELF_R_TYPE(rela->r_info);
132		symidx = ELF_R_SYM(rela->r_info);
133		break;
134	default:
135		panic("elf_reloc: unknown relocation mode %d\n", type);
136	}
137
138	switch (rtype) {
139
140		case R_PPC_NONE:
141			break;
142
143		case R_PPC_GLOB_DAT:
144			addr = lookup(lf, symidx, 1);
145			if (addr == 0)
146				return -1;
147			addr += addend;
148			if (*where != addr)
149				*where = addr;
150			break;
151
152		case R_PPC_JMP_SLOT:
153			/* No point in lazy binding for kernel modules. */
154			addr = lookup(lf, symidx, 1);
155			if (addr == 0)
156				return -1;
157			if (*where != addr)
158				*where = addr;
159			break;
160
161		case R_PPC_RELATIVE:
162			addr = relocbase + addend + *where;
163			if (*where != addr)
164				*where = addr;
165			break;
166
167		case R_PPC_COPY:
168			/*
169			 * There shouldn't be copy relocations in kernel
170			 * objects.
171			 */
172			printf("kldload: unexpected R_COPY relocation\n");
173			return -1;
174
175		default:
176			printf("kldload: unexpected relocation type %d\n",
177			       (int) rtype);
178			return -1;
179	}
180	return(0);
181}
182
183int
184elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
185    elf_lookup_fn lookup)
186{
187
188	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
189}
190
191int
192elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
193    int type, elf_lookup_fn lookup)
194{
195
196	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
197}
198
199int
200elf_cpu_load_file(linker_file_t lf __unused)
201{
202
203	return (0);
204}
205
206int
207elf_cpu_unload_file(linker_file_t lf __unused)
208{
209
210	return (0);
211}
212