2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1996-1998 John D. Polstra.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * from: src/sys/i386/i386/elf_machdep.c,v 1.20 2004/08/11 02:35:05 marcel
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
37 #include <sys/imgact.h>
38 #include <sys/linker.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
42 #include <sys/syscall.h>
43 #include <sys/signalvar.h>
44 #include <sys/vnode.h>
48 #include <vm/vm_param.h>
50 #include <machine/elf.h>
51 #include <machine/md_var.h>
52 #include <machine/cache.h>
54 static struct sysentvec elf_freebsd_sysvec = {
55 .sv_size = SYS_MAXSYSCALL,
58 .sv_fixup = __elfN(freebsd_fixup),
59 .sv_sendsig = sendsig,
60 .sv_sigcode = sigcode,
61 .sv_szsigcode = &szsigcode,
63 .sv_name = "FreeBSD ELF64",
65 .sv_name = "FreeBSD ELF32",
67 .sv_coredump = __elfN(coredump),
68 .sv_imgact_try = NULL,
69 .sv_minsigstksz = MINSIGSTKSZ,
70 .sv_minuser = VM_MIN_ADDRESS,
71 .sv_maxuser = VM_MAXUSER_ADDRESS,
72 .sv_usrstack = USRSTACK,
73 .sv_psstrings = PS_STRINGS,
74 .sv_stackprot = VM_PROT_ALL,
75 .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
76 .sv_copyout_strings = exec_copyout_strings,
77 .sv_setregs = exec_setregs,
81 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_ASLR,
83 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_ASLR,
85 .sv_set_syscall_retval = cpu_set_syscall_retval,
86 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
87 .sv_syscallnames = syscallnames,
89 .sv_thread_detach = NULL,
93 static __ElfN(Brandinfo) freebsd_brand_info = {
94 .brand = ELFOSABI_FREEBSD,
96 .compat_3_brand = "FreeBSD",
98 .interp_path = "/libexec/ld-elf.so.1",
99 .sysvec = &elf_freebsd_sysvec,
100 .interp_newpath = NULL,
101 .brand_note = &__elfN(freebsd_brandnote),
102 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
105 SYSINIT(elf, SI_SUB_EXEC, SI_ORDER_ANY,
106 (sysinit_cfunc_t) __elfN(insert_brand_entry),
107 &freebsd_brand_info);
110 __elfN(dump_thread)(struct thread *td __unused, void *dst __unused,
111 size_t *off __unused)
116 * The following MIPS relocation code for tracking multiple
117 * consecutive HI32/LO32 entries is because of the following:
119 * https://dmz-portal.mips.com/wiki/MIPS_relocation_types
125 * An R_MIPS_HI16 must be followed eventually by an associated R_MIPS_LO16
126 * relocation record in the same SHT_REL section. The contents of the two
127 * fields to be relocated are combined to form a full 32-bit addend AHL.
128 * An R_MIPS_LO16 entry which does not immediately follow a R_MIPS_HI16 is
129 * combined with the most recent one encountered, i.e. multiple R_MIPS_LO16
130 * entries may be associated with a single R_MIPS_HI16. Use of these
131 * relocation types in a SHT_REL section is discouraged and may be
132 * forbidden to avoid this complication.
134 * A GNU extension allows multiple R_MIPS_HI16 records to share the same
135 * R_MIPS_LO16 relocation record(s). The association works like this within
136 * a single relocation section:
138 * + From the beginning of the section moving to the end of the section,
139 * until R_MIPS_LO16 is not found each found R_MIPS_HI16 relocation will
140 * be associated with the first R_MIPS_LO16.
142 * + Until another R_MIPS_HI16 record is found all found R_MIPS_LO16
143 * relocations found are associated with the last R_MIPS_HI16.
147 * This is so gcc can do dead code detection/removal without having to
148 * generate HI/LO pairs even if one of them would be deleted.
150 * So, the summary is:
152 * + A HI16 entry must occur before any LO16 entries;
153 * + Multiple consecutive HI16 RELA entries need to be buffered until the
154 * first LO16 RELA entry occurs - and then all HI16 RELA relocations use
155 * the offset in the LOW16 RELA for calculating their offsets;
156 * + The last HI16 RELA entry before a LO16 RELA entry is used (the AHL)
157 * for the first subsequent LO16 calculation;
158 * + If multiple consecutive LO16 RELA entries occur, only the first
159 * LO16 RELA entry triggers an update of buffered HI16 RELA entries;
160 * any subsequent LO16 RELA entry before another HI16 RELA entry will
161 * not cause any further updates to the HI16 RELA entries.
163 * Additionally, flush out any outstanding HI16 entries that don't have
164 * a LO16 entry in case some garbage entries are left in the file.
167 struct mips_tmp_reloc;
168 struct mips_tmp_reloc {
169 struct mips_tmp_reloc *next;
172 Elf32_Addr *where_hi16;
175 static struct mips_tmp_reloc *ml = NULL;
178 * Add a temporary relocation (ie, a HI16 reloc type.)
181 mips_tmp_reloc_add(Elf_Addr ahl, Elf32_Addr *where_hi16)
183 struct mips_tmp_reloc *r;
185 r = malloc(sizeof(struct mips_tmp_reloc), M_TEMP, M_NOWAIT);
187 printf("%s: failed to malloc\n", __func__);
192 r->where_hi16 = where_hi16;
200 * Flush the temporary relocation list.
202 * This should be done after a file is completely loaded
203 * so no stale relocations exist to confuse the next
207 mips_tmp_reloc_flush(void)
209 struct mips_tmp_reloc *r, *rn;
221 * Get an entry from the reloc list; or NULL if we've run out.
223 static struct mips_tmp_reloc *
224 mips_tmp_reloc_get(void)
226 struct mips_tmp_reloc *r;
236 * Free a relocation entry.
239 mips_tmp_reloc_free(struct mips_tmp_reloc *r)
246 elf_is_ifunc_reloc(Elf_Size r_info __unused)
252 /* Process one elf relocation with addend. */
254 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
255 int type, int local, elf_lookup_fn lookup)
257 Elf32_Addr *where = (Elf32_Addr *)NULL;
259 Elf_Addr addend = (Elf_Addr)0;
260 Elf_Word rtype = (Elf_Word)0, symidx;
261 struct mips_tmp_reloc *r;
262 const Elf_Rel *rel = NULL;
263 const Elf_Rela *rela = NULL;
266 /* Store the last seen ahl from a HI16 for LO16 processing */
267 static Elf_Addr last_ahl;
271 rel = (const Elf_Rel *)data;
272 where = (Elf32_Addr *) (relocbase + rel->r_offset);
273 rtype = ELF_R_TYPE(rel->r_info);
274 symidx = ELF_R_SYM(rel->r_info);
277 addend = *(Elf64_Addr *)where;
286 rela = (const Elf_Rela *)data;
287 where = (Elf32_Addr *) (relocbase + rela->r_offset);
288 addend = rela->r_addend;
289 rtype = ELF_R_TYPE(rela->r_info);
290 symidx = ELF_R_SYM(rela->r_info);
293 panic("unknown reloc type %d\n", type);
297 case R_MIPS_NONE: /* none */
300 case R_MIPS_32: /* S + A */
301 error = lookup(lf, symidx, 1, &addr);
306 *where = (Elf32_Addr)addr;
309 case R_MIPS_26: /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
310 error = lookup(lf, symidx, 1, &addr);
314 addend &= 0x03ffffff;
316 * Addendum for .rela R_MIPS_26 is not shifted right
321 addr += ((Elf_Addr)where & 0xf0000000) | addend;
324 *where &= ~0x03ffffff;
325 *where |= addr & 0x03ffffff;
328 case R_MIPS_64: /* S + A */
329 error = lookup(lf, symidx, 1, &addr);
333 if (*(Elf64_Addr*)where != addr)
334 *(Elf64_Addr*)where = addr;
338 * Handle the two GNU extension cases:
340 * + Multiple HI16s followed by a LO16, and
341 * + A HI16 followed by multiple LO16s.
343 * The former is tricky - the HI16 relocations need
344 * to be buffered until a LO16 occurs, at which point
345 * each HI16 is replayed against the LO16 relocation entry
346 * (with the relevant overflow information.)
348 * The latter should be easy to handle - when the
349 * first LO16 is seen, write out and flush the
350 * HI16 buffer. Any subsequent LO16 entries will
351 * find a blank relocation buffer.
355 case R_MIPS_HI16: /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
357 error = lookup(lf, symidx, 1, &addr);
361 *where &= 0xffff0000;
362 *where |= ((((long long) addr + 0x8000LL) >> 16) & 0xffff);
365 * Add a temporary relocation to the list;
366 * will pop it off / free the list when
367 * we've found a suitable HI16.
369 if (mips_tmp_reloc_add(addend << 16, where) == 0)
372 * Track the last seen HI16 AHL for use by
373 * the first LO16 AHL calculation.
375 * The assumption is any intermediary deleted
376 * LO16's were optimised out, so the last
377 * HI16 before the LO16 is the "true" relocation
378 * entry to use for that LO16 write.
380 last_ahl = addend << 16;
384 case R_MIPS_LO16: /* AHL + S */
386 error = lookup(lf, symidx, 1, &addr);
390 *where &= 0xffff0000;
391 *where |= addr & 0xffff;
396 tmp_ahl = last_ahl + (int16_t) addend;
397 error = lookup(lf, symidx, 1, &addr);
401 tmp_addend = addend & 0xffff0000;
403 /* Use the last seen ahl for calculating addend */
404 tmp_addend |= (uint16_t)(tmp_ahl + addr);
408 * This logic implements the "we saw multiple HI16
409 * before a LO16" assignment /and/ "we saw multiple
412 * Multiple LO16s will be handled as a blank
415 * Multple HI16's are iterated over here.
417 while ((r = mips_tmp_reloc_get()) != NULL) {
421 * We have the ahl from the HI16 entry, so
422 * offset it by the 16 bits of the low ahl.
425 rahl += (int16_t) addend;
427 tmp_addend = *(r->where_hi16);
428 tmp_addend &= 0xffff0000;
429 tmp_addend |= ((rahl + addr) -
430 (int16_t)(rahl + addr)) >> 16;
431 *(r->where_hi16) = tmp_addend;
432 mips_tmp_reloc_free(r);
438 case R_MIPS_HIGHER: /* %higher(A+S) */
439 error = lookup(lf, symidx, 1, &addr);
443 *where &= 0xffff0000;
444 *where |= (((long long)addr + 0x80008000LL) >> 32) & 0xffff;
447 case R_MIPS_HIGHEST: /* %highest(A+S) */
448 error = lookup(lf, symidx, 1, &addr);
452 *where &= 0xffff0000;
453 *where |= (((long long)addr + 0x800080008000LL) >> 48) & 0xffff;
457 printf("kldload: unexpected relocation type %d\n",
466 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
467 elf_lookup_fn lookup)
470 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
474 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
475 int type, elf_lookup_fn lookup)
478 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
482 elf_cpu_load_file(linker_file_t lf __unused)
486 * Sync the I and D caches to make sure our relocations are visible.
488 mips_icache_sync_all();
490 /* Flush outstanding relocations */
491 mips_tmp_reloc_flush();
497 elf_cpu_unload_file(linker_file_t lf __unused)
504 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)