elf32_machdep.c revision 153741
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 153741 2005-12-26 21:23:57Z sobomax $
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						0,
89					  };
90
91SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
92	(sysinit_cfunc_t) elf32_insert_brand_entry,
93	&freebsd_brand_info);
94
95static Elf32_Brandinfo freebsd_brand_oinfo = {
96						ELFOSABI_FREEBSD,
97						EM_PPC,
98						"FreeBSD",
99						NULL,
100						"/usr/libexec/ld-elf.so.1",
101						&elf32_freebsd_sysvec,
102						NULL,
103						0,
104					  };
105
106SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
107	(sysinit_cfunc_t) elf32_insert_brand_entry,
108	&freebsd_brand_oinfo);
109
110
111void
112elf32_dump_thread(struct thread *td __unused, void *dst __unused,
113    size_t *off __unused)
114{
115}
116
117
118/* Process one elf relocation with addend. */
119static int
120elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
121    int type, int local, elf_lookup_fn lookup)
122{
123	Elf_Addr *where;
124	Elf_Half *hwhere;
125	Elf_Addr addr;
126	Elf_Addr addend;
127	Elf_Word rtype, symidx;
128	const Elf_Rela *rela;
129
130	switch (type) {
131	case ELF_RELOC_REL:
132		panic("PPC only supports RELA relocations");
133		break;
134	case ELF_RELOC_RELA:
135		rela = (const Elf_Rela *)data;
136		where = (Elf_Addr *) (relocbase + rela->r_offset);
137		hwhere = (Elf_Half *) (relocbase + rela->r_offset);
138		addend = rela->r_addend;
139		rtype = ELF_R_TYPE(rela->r_info);
140		symidx = ELF_R_SYM(rela->r_info);
141		break;
142	default:
143		panic("elf_reloc: unknown relocation mode %d\n", type);
144	}
145
146	switch (rtype) {
147
148       	case R_PPC_NONE:
149	       	break;
150
151	case R_PPC_ADDR32: /* word32 S + A */
152       		addr = lookup(lf, symidx, 1);
153	       	if (addr == 0)
154	       		return -1;
155		addr += addend;
156	       	*where = addr;
157	       	break;
158
159       	case R_PPC_ADDR16_LO: /* #lo(S) */
160		addr = lookup(lf, symidx, 1);
161		if (addr == 0)
162			return -1;
163		/*
164		 * addend values are sometimes relative to sections
165		 * (i.e. .rodata) in rela, where in reality they
166		 * are relative to relocbase. Detect this condition.
167		 */
168		if (addr > relocbase && addr <= (relocbase + addend))
169			addr = relocbase + addend;
170		else
171			addr += addend;
172		*hwhere = addr & 0xffff;
173		break;
174
175	case R_PPC_ADDR16_HA: /* #ha(S) */
176		addr = lookup(lf, symidx, 1);
177		if (addr == 0)
178			return -1;
179		/*
180		 * addend values are sometimes relative to sections
181		 * (i.e. .rodata) in rela, where in reality they
182		 * are relative to relocbase. Detect this condition.
183		 */
184		if (addr > relocbase && addr <= (relocbase + addend))
185			addr = relocbase + addend;
186		else
187			addr += addend;
188	       	*hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
189		    & 0xffff;
190		break;
191
192	case R_PPC_RELATIVE: /* word32 B + A */
193       		*where = relocbase + addend;
194	       	break;
195
196	default:
197       		printf("kldload: unexpected relocation type %d\n",
198	       	    (int) rtype);
199		return -1;
200	}
201	return(0);
202}
203
204int
205elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
206    elf_lookup_fn lookup)
207{
208
209	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
210}
211
212int
213elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
214    int type, elf_lookup_fn lookup)
215{
216
217	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
218}
219
220int
221elf_cpu_load_file(linker_file_t lf)
222{
223	/* Only sync the cache for non-kernel modules */
224	if (lf->id != 1)
225		__syncicache(lf->address, lf->size);
226	return (0);
227}
228
229int
230elf_cpu_unload_file(linker_file_t lf __unused)
231{
232
233	return (0);
234}
235