2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
32 #include <sys/imgact.h>
33 #include <sys/malloc.h>
35 #include <sys/namei.h>
36 #include <sys/fcntl.h>
37 #include <sys/vnode.h>
38 #include <sys/linker.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/syscall.h>
42 #include <sys/signalvar.h>
45 #include <vm/vm_param.h>
47 #include <machine/elf.h>
48 #include <machine/frame.h>
49 #include <machine/md_var.h>
50 #include <machine/unwind.h>
52 Elf_Addr link_elf_get_gp(linker_file_t);
54 extern Elf_Addr fptr_storage[];
56 struct sysentvec elf64_freebsd_sysvec = {
65 __elfN(freebsd_fixup),
68 NULL, /* &szsigcode */
79 VM_PROT_READ|VM_PROT_WRITE,
85 static Elf64_Brandinfo freebsd_brand_info = {
90 "/libexec/ld-elf.so.1",
91 &elf64_freebsd_sysvec,
94 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
95 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
97 static Elf64_Brandinfo freebsd_brand_oinfo = {
102 "/usr/libexec/ld-elf.so.1",
103 &elf64_freebsd_sysvec,
106 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
107 (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
111 elf64_dump_thread(struct thread *td, void *dst, size_t *off __unused)
114 /* Flush the dirty registers onto the backingstore. */
116 ia64_flush_dirty(td, &td->td_frame->tf_special);
121 lookup_fdesc(linker_file_t lf, Elf_Size symidx, elf_lookup_fn lookup)
129 addr = lookup(lf, symidx, 0);
132 symname = elf_get_symname(top, symidx);
133 for (i = 0; i < top->ndeps; i++) {
135 addr = (Elf_Addr)linker_file_lookup_symbol(lf,
148 * Lookup and/or construct OPD
150 for (i = 0; i < 8192; i += 2) {
151 if (fptr_storage[i] == addr)
152 return (Elf_Addr)(fptr_storage + i);
154 if (fptr_storage[i] == 0) {
155 fptr_storage[i] = addr;
156 fptr_storage[i+1] = link_elf_get_gp(lf);
157 return (Elf_Addr)(fptr_storage + i);
161 printf("%s: fptr table full\n", __func__);
167 /* Process one elf relocation with addend. */
169 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
170 int type, int local, elf_lookup_fn lookup)
173 Elf_Addr addend, addr;
174 Elf_Size rtype, symidx;
176 const Elf_Rela *rela;
180 rel = (const Elf_Rel *)data;
181 where = (Elf_Addr *)(relocbase + rel->r_offset);
182 rtype = ELF_R_TYPE(rel->r_info);
183 symidx = ELF_R_SYM(rel->r_info);
185 case R_IA_64_DIR64LSB:
186 case R_IA_64_FPTR64LSB:
187 case R_IA_64_REL64LSB:
196 rela = (const Elf_Rela *)data;
197 where = (Elf_Addr *)(relocbase + rela->r_offset);
198 rtype = ELF_R_TYPE(rela->r_info);
199 symidx = ELF_R_SYM(rela->r_info);
200 addend = rela->r_addend;
203 panic("%s: invalid ELF relocation (0x%x)\n", __func__, type);
207 if (rtype == R_IA_64_REL64LSB)
208 *where = relocbase + addend;
215 case R_IA_64_DIR64LSB: /* word64 LSB S + A */
216 addr = lookup(lf, symidx, 1);
219 *where = addr + addend;
221 case R_IA_64_FPTR64LSB: /* word64 LSB @fptr(S + A) */
223 printf("%s: addend ignored for OPD relocation\n",
226 addr = lookup_fdesc(lf, symidx, lookup);
231 case R_IA_64_REL64LSB: /* word64 LSB BD + A */
233 case R_IA_64_IPLTLSB:
234 addr = lookup_fdesc(lf, symidx, lookup);
237 where[0] = *((Elf_Addr*)addr) + addend;
238 where[1] = *((Elf_Addr*)addr + 1);
241 printf("%s: unknown relocation (0x%x)\n", __func__,
250 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
251 elf_lookup_fn lookup)
254 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
258 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
259 int type, elf_lookup_fn lookup)
262 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
266 elf_cpu_load_file(linker_file_t lf)
269 Elf_Phdr *ph, *phlim;
270 Elf_Addr reloc, vaddr;
272 hdr = (Elf_Ehdr *)(lf->address);
274 printf("Missing or corrupted ELF header at %p\n", hdr);
279 * Iterate over the segments and register the unwind table if
282 ph = (Elf_Phdr *)(lf->address + hdr->e_phoff);
283 phlim = ph + hdr->e_phnum;
286 if (ph->p_type == PT_LOAD && reloc == ~0ULL)
287 reloc = (Elf_Addr)lf->address - ph->p_vaddr;
289 if (ph->p_type == PT_IA_64_UNWIND) {
290 vaddr = ph->p_vaddr + reloc;
291 unw_table_add((vm_offset_t)lf->address, vaddr,
292 vaddr + ph->p_memsz);
301 elf_cpu_unload_file(linker_file_t lf)
304 unw_table_remove((vm_offset_t)lf->address);