elf32_machdep.c revision 132428
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 132428 2004-07-20 02:40:57Z grehan $
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/cpu.h>
48#include <machine/elf.h>
49#include <machine/md_var.h>
50
51struct sysentvec elf32_freebsd_sysvec = {
52	SYS_MAXSYSCALL,
53	sysent,
54	0,
55	0,
56	NULL,
57	0,
58	NULL,
59	NULL,
60	__elfN(freebsd_fixup),
61	sendsig,
62	sigcode,
63	&szsigcode,
64	NULL,
65	"FreeBSD ELF32",
66	__elfN(coredump),
67	NULL,
68	MINSIGSTKSZ,
69	PAGE_SIZE,
70	VM_MIN_ADDRESS,
71	VM_MAXUSER_ADDRESS,
72	USRSTACK,
73	PS_STRINGS,
74	VM_PROT_ALL,
75	exec_copyout_strings,
76	exec_setregs,
77	NULL
78};
79
80static Elf32_Brandinfo freebsd_brand_info = {
81						ELFOSABI_FREEBSD,
82						EM_PPC,
83						"FreeBSD",
84						NULL,
85						"/libexec/ld-elf.so.1",
86						&elf32_freebsd_sysvec,
87						NULL,
88					  };
89
90SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
91	(sysinit_cfunc_t) elf32_insert_brand_entry,
92	&freebsd_brand_info);
93
94static Elf32_Brandinfo freebsd_brand_oinfo = {
95						ELFOSABI_FREEBSD,
96						EM_PPC,
97						"FreeBSD",
98						NULL,
99						"/usr/libexec/ld-elf.so.1",
100						&elf32_freebsd_sysvec,
101						NULL,
102					  };
103
104SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
105	(sysinit_cfunc_t) elf32_insert_brand_entry,
106	&freebsd_brand_oinfo);
107
108/* Process one elf relocation with addend. */
109static int
110elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
111    int type, int local, elf_lookup_fn lookup)
112{
113	Elf_Addr *where;
114	Elf_Half *hwhere;
115	Elf_Addr addr;
116	Elf_Addr addend;
117	Elf_Word rtype, symidx;
118	const Elf_Rela *rela;
119
120	switch (type) {
121	case ELF_RELOC_REL:
122		panic("PPC only supports RELA relocations");
123		break;
124	case ELF_RELOC_RELA:
125		rela = (const Elf_Rela *)data;
126		where = (Elf_Addr *) (relocbase + rela->r_offset);
127		hwhere = (Elf_Half *) (relocbase + rela->r_offset);
128		addend = rela->r_addend;
129		rtype = ELF_R_TYPE(rela->r_info);
130		symidx = ELF_R_SYM(rela->r_info);
131		break;
132	default:
133		panic("elf_reloc: unknown relocation mode %d\n", type);
134	}
135
136	switch (rtype) {
137
138       	case R_PPC_NONE:
139	       	break;
140
141	case R_PPC_ADDR32: /* word32 S + A */
142       		addr = lookup(lf, symidx, 1);
143	       	if (addr == 0)
144	       		return -1;
145		addr += addend;
146	       	*where = addr;
147	       	break;
148
149       	case R_PPC_ADDR16_LO: /* #lo(S) */
150       		if (addend != 0) {
151	       		addr = relocbase + addend;
152		} else {
153	      		addr = lookup(lf, symidx, 1);
154			if (addr == 0)
155				return -1;
156		}
157		*hwhere = addr & 0xffff;
158		break;
159
160	case R_PPC_ADDR16_HA: /* #ha(S) */
161       		if (addend != 0) {
162	       		addr = relocbase + addend;
163		} else {
164	       		addr = lookup(lf, symidx, 1);
165		       	if (addr == 0)
166		       		return -1;
167		}
168	       	*hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
169		    & 0xffff;
170		break;
171
172	case R_PPC_RELATIVE: /* word32 B + A */
173       		*where = relocbase + addend;
174	       	break;
175
176	default:
177       		printf("kldload: unexpected relocation type %d\n",
178	       	    (int) rtype);
179		return -1;
180	}
181	return(0);
182}
183
184int
185elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
186    elf_lookup_fn lookup)
187{
188
189	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
190}
191
192int
193elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
194    int type, elf_lookup_fn lookup)
195{
196
197	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
198}
199
200int
201elf_cpu_load_file(linker_file_t lf)
202{
203	/* Only sync the cache for non-kernel modules */
204	if (lf->id != 1)
205		__syncicache(lf->address, lf->size);
206	return (0);
207}
208
209int
210elf_cpu_unload_file(linker_file_t lf __unused)
211{
212
213	return (0);
214}
215