Home | History | Annotate | Line # | Download | only in ld.elf_so
      1 /*	$NetBSD: compat.c,v 1.1 2018/10/17 23:36:58 joerg 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.1 2018/10/17 23:36:58 joerg 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 #ifdef RTLD_OBJ_DLOPEN_OFFSET
     64 const uintptr_t _rtld_compat_obj[] = {
     65 #  ifdef _LP64
     66 #    if BYTE_ORDER == BIG_ENDIAN
     67 	[0] = (((uint64_t)RTLD_MAGIC) << 32) | RTLD_VERSION,
     68 #    else
     69 	[0] = (((uint64_t)RTLD_VERSION) << 32) | RTLD_MAGIC,
     70 #    endif
     71 #  else
     72 	[0] = RTLD_MAGIC,
     73 	[1] = RTLD_VERSION,
     74 #  endif
     75 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 0] = (uintptr_t)dlopen,
     76 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 1] = (uintptr_t)dlsym,
     77 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 2] = (uintptr_t)dlerror,
     78 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 3] = (uintptr_t)dlclose,
     79 	[(RTLD_OBJ_DLOPEN_OFFSET / sizeof(uintptr_t)) + 4] = (uintptr_t)dladdr,
     80 };
     81 #else
     82 const uintptr_t _rtld_compat_obj[] = {
     83 #  ifdef _LP64
     84 #    if BYTE_ORDER == BIG_ENDIAN
     85 	[0] = (((uint64_t)RTLD_MAGIC) << 32) | RTLD_VERSION,
     86 #    else
     87 	[0] = (((uint64_t)RTLD_VERSION) << 32) | RTLD_MAGIC,
     88 #    endif
     89 #  else
     90 	[0] = RTLD_MAGIC,
     91 	[1] = RTLD_VERSION,
     92 #  endif
     93 };
     94 #endif /* RTLD_OBJ_DLOPEN_OFFSET */
     95