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: releng/5.0/sys/i386/i386/elf_machdep.c 105469 2002-10-19 19:16:03Z marcel $
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/linker.h>
34 #include <sys/sysent.h>
35 #include <sys/imgact_elf.h>
36 #include <sys/syscall.h>
37 #include <sys/signalvar.h>
38 #include <sys/vnode.h>
39
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_param.h>
43
44 #include <machine/elf.h>
45 #include <machine/md_var.h>
46
47 struct sysentvec elf32_freebsd_sysvec = {
48 SYS_MAXSYSCALL,
49 sysent,
50 0,
51 0,
52 NULL,
53 0,
54 NULL,
55 NULL,
56 __elfN(freebsd_fixup),
57 sendsig,
58 sigcode,
59 &szsigcode,
60 NULL,
61 "FreeBSD ELF32",
62 __elfN(coredump),
63 NULL,
64 MINSIGSTKSZ,
65 PAGE_SIZE,
66 VM_MIN_ADDRESS,
67 VM_MAXUSER_ADDRESS,
68 USRSTACK,
69 PS_STRINGS,
70 VM_PROT_ALL,
71 exec_copyout_strings,
72 exec_setregs
73 };
74
75 static Elf32_Brandinfo freebsd_brand_info = {
76 ELFOSABI_FREEBSD,
77 EM_386,
78 "FreeBSD",
79 "",
80 "/usr/libexec/ld-elf.so.1",
81 &elf32_freebsd_sysvec
82 };
83
84 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
85 (sysinit_cfunc_t) elf32_insert_brand_entry,
86 &freebsd_brand_info);
87
88 /* Process one elf relocation with addend. */
89 int
90 elf_reloc(linker_file_t lf, const void *data, int type)
91 {
92 Elf_Addr relocbase = (Elf_Addr) lf->address;
93 Elf_Addr *where;
94 Elf_Addr addr;
95 Elf_Addr addend;
96 Elf_Word rtype, symidx;
97 const Elf_Rel *rel;
98 const Elf_Rela *rela;
99
100 switch (type) {
101 case ELF_RELOC_REL:
102 rel = (const Elf_Rel *)data;
103 where = (Elf_Addr *) (relocbase + rel->r_offset);
104 addend = *where;
105 rtype = ELF_R_TYPE(rel->r_info);
106 symidx = ELF_R_SYM(rel->r_info);
107 break;
108 case ELF_RELOC_RELA:
109 rela = (const Elf_Rela *)data;
110 where = (Elf_Addr *) (relocbase + rela->r_offset);
111 addend = rela->r_addend;
112 rtype = ELF_R_TYPE(rela->r_info);
113 symidx = ELF_R_SYM(rela->r_info);
114 break;
115 default:
116 panic("unknown reloc type %d\n", type);
117 }
118
119 switch (rtype) {
120
121 case R_386_NONE: /* none */
122 break;
123
124 case R_386_32: /* S + A */
125 addr = elf_lookup(lf, symidx, 1);
126 if (addr == 0)
127 return -1;
128 addr += addend;
129 if (*where != addr)
130 *where = addr;
131 break;
132
133 case R_386_PC32: /* S + A - P */
134 addr = elf_lookup(lf, symidx, 1);
135 if (addr == 0)
136 return -1;
137 addr += addend - (Elf_Addr)where;
138 if (*where != addr)
139 *where = addr;
140 break;
141
142 case R_386_COPY: /* none */
143 /*
144 * There shouldn't be copy relocations in kernel
145 * objects.
146 */
147 printf("kldload: unexpected R_COPY relocation\n");
148 return -1;
149 break;
150
151 case R_386_GLOB_DAT: /* S */
152 addr = elf_lookup(lf, symidx, 1);
153 if (addr == 0)
154 return -1;
155 if (*where != addr)
156 *where = addr;
157 break;
158
159 case R_386_RELATIVE: /* B + A */
160 addr = relocbase + addend;
161 if (*where != addr)
162 *where = addr;
163 break;
164
165 default:
166 printf("kldload: unexpected relocation type %d\n",
167 rtype);
168 return -1;
169 }
170 return(0);
171 }
172
173 int
174 elf_cpu_load_file(linker_file_t lf __unused)
175 {
176
177 return (0);
178 }
179
180 int
181 elf_cpu_unload_file(linker_file_t lf __unused)
182 {
183
184 return (0);
185 }
Cache object: 002c9c0254c8903459a55b5e143f4144
|