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