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