xref: /netbsd-src/libexec/ld.elf_so/compat.c (revision 4312beab4fdae46cf5564cc8dc9860677b7db593)
1*4312beabSjoerg /*	$NetBSD: compat.c,v 1.1 2018/10/17 23:36:58 joerg Exp $	*/
2*4312beabSjoerg /*-
3*4312beabSjoerg  * Copyright (c) 2018 The NetBSD Foundation, Inc.
4*4312beabSjoerg  * All rights reserved.
5*4312beabSjoerg  *
6*4312beabSjoerg  * This code is derived from software contributed to The NetBSD Foundation
7*4312beabSjoerg  * by Joerg Sonnenberger.
8*4312beabSjoerg  *
9*4312beabSjoerg  * Redistribution and use in source and binary forms, with or without
10*4312beabSjoerg  * modification, are permitted provided that the following conditions
11*4312beabSjoerg  * are met:
12*4312beabSjoerg  * 1. Redistributions of source code must retain the above copyright
13*4312beabSjoerg  *    notice, this list of conditions and the following disclaimer.
14*4312beabSjoerg  * 2. Redistributions in binary form must reproduce the above copyright
15*4312beabSjoerg  *    notice, this list of conditions and the following disclaimer in the
16*4312beabSjoerg  *    documentation and/or other materials provided with the distribution.
17*4312beabSjoerg  *
18*4312beabSjoerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*4312beabSjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*4312beabSjoerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*4312beabSjoerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*4312beabSjoerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*4312beabSjoerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*4312beabSjoerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*4312beabSjoerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*4312beabSjoerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*4312beabSjoerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*4312beabSjoerg  * POSSIBILITY OF SUCH DAMAGE.
29*4312beabSjoerg  */
30*4312beabSjoerg 
31*4312beabSjoerg /*
32*4312beabSjoerg  * Early ELF support in NetBSD up to April 2nd, 2000 exposed dlopen and
33*4312beabSjoerg  * friends via function pointers in the main object that is passed to the
34*4312beabSjoerg  * startup code in crt0.o. Later versions kept the magic and version number
35*4312beabSjoerg  * checks, but depend on normal symbol interposition to get the symbols from
36*4312beabSjoerg  * rtld. The compatibility object build here contains just enough fields to
37*4312beabSjoerg  * make either routine happy without polluting the rest of rtld.
38*4312beabSjoerg  */
39*4312beabSjoerg 
40*4312beabSjoerg #include <sys/cdefs.h>
41*4312beabSjoerg __RCSID("$NetBSD: compat.c,v 1.1 2018/10/17 23:36:58 joerg Exp $");
42*4312beabSjoerg 
43*4312beabSjoerg #include "rtld.h"
44*4312beabSjoerg 
45*4312beabSjoerg #if defined(__alpha__)
46*4312beabSjoerg #define RTLD_OBJ_DLOPEN_OFFSET 264
47*4312beabSjoerg #elif defined(__i386__)
48*4312beabSjoerg #define RTLD_OBJ_DLOPEN_OFFSET 140
49*4312beabSjoerg #elif defined(__mips) && defined(_ABIO32)
50*4312beabSjoerg /* The MIPS legacy support is required for O32 only, N32 is not affected. */
51*4312beabSjoerg #define RTLD_OBJ_DLOPEN_OFFSET 152
52*4312beabSjoerg #elif defined(__powerpc__) && !defined(__powerpc64__)
53*4312beabSjoerg /* Only 32bit PowerPC is affected. */
54*4312beabSjoerg #define RTLD_OBJ_DLOPEN_OFFSET 140
55*4312beabSjoerg #elif defined(__sparc) && !defined(__sparc64__)
56*4312beabSjoerg /* Only 32bit SPARC is affected, 64bit SPARC ELF support was incomplete. */
57*4312beabSjoerg #define RTLD_OBJ_DLOPEN_OFFSET 140
58*4312beabSjoerg #endif
59*4312beabSjoerg 
60*4312beabSjoerg #define RTLD_MAGIC	0xd550b87a
61*4312beabSjoerg #define RTLD_VERSION	1
62*4312beabSjoerg 
63*4312beabSjoerg #ifdef RTLD_OBJ_DLOPEN_OFFSET
64*4312beabSjoerg const uintptr_t _rtld_compat_obj[] = {
65*4312beabSjoerg #  ifdef _LP64
66*4312beabSjoerg #    if BYTE_ORDER == BIG_ENDIAN
67*4312beabSjoerg 	[0] = (((uint64_t)RTLD_MAGIC) << 32) | RTLD_VERSION,
68*4312beabSjoerg #    else
69*4312beabSjoerg 	[0] = (((uint64_t)RTLD_VERSION) << 32) | RTLD_MAGIC,
70*4312beabSjoerg #    endif
71*4312beabSjoerg #  else
72*4312beabSjoerg 	[0] = RTLD_MAGIC,
73*4312beabSjoerg 	[1] = RTLD_VERSION,
74*4312beabSjoerg #  endif
75*4312beabSjoerg 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 0] = (uintptr_t)dlopen,
76*4312beabSjoerg 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 1] = (uintptr_t)dlsym,
77*4312beabSjoerg 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 2] = (uintptr_t)dlerror,
78*4312beabSjoerg 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 3] = (uintptr_t)dlclose,
79*4312beabSjoerg 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 4] = (uintptr_t)dladdr,
80*4312beabSjoerg };
81*4312beabSjoerg #else
82*4312beabSjoerg const uintptr_t _rtld_compat_obj[] = {
83*4312beabSjoerg #  ifdef _LP64
84*4312beabSjoerg #    if BYTE_ORDER == BIG_ENDIAN
85*4312beabSjoerg 	[0] = (((uint64_t)RTLD_MAGIC) << 32) | RTLD_VERSION,
86*4312beabSjoerg #    else
87*4312beabSjoerg 	[0] = (((uint64_t)RTLD_VERSION) << 32) | RTLD_MAGIC,
88*4312beabSjoerg #    endif
89*4312beabSjoerg #  else
90*4312beabSjoerg 	[0] = RTLD_MAGIC,
91*4312beabSjoerg 	[1] = RTLD_VERSION,
92*4312beabSjoerg #  endif
93*4312beabSjoerg };
94*4312beabSjoerg #endif /* RTLD_OBJ_DLOPEN_OFFSET */
95