elf32_machdep.c revision 102808
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 102808 2002-09-01 21:41:24Z jake $
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};
77
78static Elf32_Brandinfo freebsd_brand_info = {
79						ELFOSABI_FREEBSD,
80						EM_PPC,
81						"FreeBSD",
82						"",
83						"/usr/libexec/ld-elf.so.1",
84						&elf32_freebsd_sysvec
85					  };
86
87SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
88	(sysinit_cfunc_t) elf32_insert_brand_entry,
89	&freebsd_brand_info);
90
91/* Process one elf relocation with addend. */
92int
93elf_reloc(linker_file_t lf, const void *data, int type)
94{
95	Elf_Addr relocbase = (Elf_Addr) lf->address;
96	Elf_Addr *where;
97	Elf_Addr addr;
98	Elf_Addr addend;
99	Elf_Word rtype, symidx;
100	const Elf_Rel *rel;
101	const Elf_Rela *rela;
102
103	switch (type) {
104	case ELF_RELOC_REL:
105		rel = (const Elf_Rel *)data;
106		where = (Elf_Addr *) (relocbase + rel->r_offset);
107		addend = *where;
108		rtype = ELF_R_TYPE(rel->r_info);
109		symidx = ELF_R_SYM(rel->r_info);
110		break;
111	case ELF_RELOC_RELA:
112		rela = (const Elf_Rela *)data;
113		where = (Elf_Addr *) (relocbase + rela->r_offset);
114		addend = rela->r_addend;
115		rtype = ELF_R_TYPE(rela->r_info);
116		symidx = ELF_R_SYM(rela->r_info);
117		break;
118	default:
119		panic("elf_reloc: unknown relocation mode %d\n", type);
120	}
121
122	switch (rtype) {
123
124		case R_PPC_NONE:
125			break;
126
127		case R_PPC_GLOB_DAT:
128			addr = elf_lookup(lf, symidx, 1);
129			if (addr == 0)
130				return -1;
131			addr += addend;
132			if (*where != addr)
133				*where = addr;
134			break;
135
136		case R_PPC_JMP_SLOT:
137			/* No point in lazy binding for kernel modules. */
138			addr = elf_lookup(lf, symidx, 1);
139			if (addr == 0)
140				return -1;
141			if (*where != addr)
142				*where = addr;
143			break;
144
145		case R_PPC_RELATIVE:
146			addr = relocbase + addend + *where;
147			if (*where != addr)
148				*where = addr;
149			break;
150
151		case R_PPC_COPY:
152			/*
153			 * There shouldn't be copy relocations in kernel
154			 * objects.
155			 */
156			printf("kldload: unexpected R_COPY relocation\n");
157			return -1;
158
159		default:
160			printf("kldload: unexpected relocation type %d\n",
161			       (int) rtype);
162			return -1;
163	}
164	return(0);
165}
166