kernel ELF: Reimplement Elf Branding, .note.ABI-tag
[dragonfly.git] / sys / cpu / i386 / misc / elf_machdep.c
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: src/sys/i386/i386/elf_machdep.c,v 1.8 1999/12/21 11:14:02 eivind Exp $
26  */
27
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/linker.h>
32 #include <sys/sysent.h>
33 #include <sys/imgact_elf.h>
34 #include <sys/syscall.h>
35 #include <sys/signalvar.h>
36 #include <sys/vnode.h>
37 #include <machine/elf.h>
38 #include <machine/md_var.h>
39
40 static struct sysentvec elf32_dragonfly_sysvec = {
41         .sv_size        = SYS_MAXSYSCALL,
42         .sv_table       = sysent,
43         .sv_mask        = -1,
44         .sv_sigsize     = 0,
45         .sv_sigtbl      = NULL,
46         .sv_errsize     = 0,
47         .sv_errtbl      = NULL,
48         .sv_transtrap   = NULL,
49         .sv_fixup       = __elfN(dragonfly_fixup),
50         .sv_sendsig     = sendsig,
51         .sv_sigcode     = sigcode,
52         .sv_szsigcode   = &szsigcode,
53         .sv_prepsyscall = NULL,
54         .sv_name        = "DragonFly ELF32",
55         .sv_coredump    = __elfN(coredump),
56         .sv_imgact_try  = NULL,
57         .sv_minsigstksz = MINSIGSTKSZ,
58 };
59
60 static Elf32_Brandinfo dragonfly_brand_info = {
61         .brand          = ELFOSABI_NONE,
62         .machine        = EM_386,
63         .compat_3_brand = "DragonFly",
64         .emul_path      = NULL,
65         .interp_path    = "/usr/libexec/ld-elf.so.2",
66         .sysvec         = &elf32_dragonfly_sysvec,
67         .interp_newpath = NULL,
68         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
69         .brand_note     = &elf32_dragonfly_brandnote,
70 };
71
72 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
73         (sysinit_cfunc_t) elf32_insert_brand_entry,
74         &dragonfly_brand_info);
75
76 /* Process one elf relocation with addend. */
77 static int
78 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
79     int type, int local, elf_lookup_fn lookup)
80 {
81         Elf_Addr *where;
82         Elf_Addr addr;
83         Elf_Addr addend;
84         Elf_Word rtype, symidx;
85         const Elf_Rel *rel;
86         const Elf_Rela *rela;
87
88         switch (type) {
89         case ELF_RELOC_REL:
90                 rel = (const Elf_Rel *)data;
91                 where = (Elf_Addr *) (relocbase + rel->r_offset);
92                 addend = *where;
93                 rtype = ELF_R_TYPE(rel->r_info);
94                 symidx = ELF_R_SYM(rel->r_info);
95                 break;
96         case ELF_RELOC_RELA:
97                 rela = (const Elf_Rela *)data;
98                 where = (Elf_Addr *) (relocbase + rela->r_offset);
99                 addend = rela->r_addend;
100                 rtype = ELF_R_TYPE(rela->r_info);
101                 symidx = ELF_R_SYM(rela->r_info);
102                 break;
103         default:
104                 panic("unknown reloc type %d\n", type);
105         }
106
107         if (local) {
108                 if (rtype == R_386_RELATIVE) {  /* A + B */
109                         addr = elf_relocaddr(lf, relocbase + addend);
110                         if (*where != addr)
111                                 *where = addr;
112                 }
113                 return (0);
114         }
115
116         switch (rtype) {
117
118                 case R_386_NONE:        /* none */
119                         break;
120
121                 case R_386_32:          /* S + A */
122                         if (lookup(lf, symidx, 1, &addr))
123                                 return -1;
124                         addr += addend;
125                         if (*where != addr)
126                                 *where = addr;
127                         break;
128
129                 case R_386_PC32:        /* S + A - P */
130                         if (lookup(lf, symidx, 1, &addr))
131                                 return -1;
132                         addr += addend - (Elf_Addr)where;
133                         if (*where != addr)
134                                 *where = addr;
135                         break;
136
137                 case R_386_COPY:        /* none */
138                         /*
139                          * There shouldn't be copy relocations in kernel
140                          * objects.
141                          */
142                         kprintf("kldload: unexpected R_COPY relocation\n");
143                         return -1;
144                         break;
145
146                 case R_386_GLOB_DAT:    /* S */
147                         if (lookup(lf, symidx, 1, &addr))
148                                 return -1;
149                         if (*where != addr)
150                                 *where = addr;
151                         break;
152
153                 case R_386_RELATIVE:
154                         break;
155
156                 default:
157                         kprintf("kldload: unexpected relocation type %d\n",
158                                rtype);
159                         return -1;
160         }
161         return(0);
162 }
163
164 int
165 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
166     elf_lookup_fn lookup)
167 {
168
169         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
170 }
171
172 int
173 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
174     int type, elf_lookup_fn lookup)
175 {
176
177         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
178 }