1999-06-18 01:09:47 +04:00
|
|
|
/* $NetBSD: rtld.c,v 1.75 1999/06/17 21:11:42 thorpej Exp $ */
|
1998-01-06 01:00:34 +03:00
|
|
|
|
1998-09-05 17:08:38 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
1993-10-17 00:54:08 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-09-05 17:08:38 +04:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Paul Kranenburg.
|
|
|
|
*
|
1993-10-17 00:54:08 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1998-09-05 17:08:38 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
1993-10-17 00:54:08 +03:00
|
|
|
*
|
1998-09-05 17:08:38 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1993-10-17 00:54:08 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/time.h>
|
1994-01-29 00:01:20 +03:00
|
|
|
#include <sys/errno.h>
|
1993-10-17 00:54:08 +03:00
|
|
|
#include <sys/mman.h>
|
1994-06-10 19:16:32 +04:00
|
|
|
#include <err.h>
|
1995-06-05 04:08:38 +04:00
|
|
|
#include <dlfcn.h>
|
1993-10-17 00:54:08 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <a.out.h>
|
1994-06-10 19:16:32 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1993-10-17 00:54:08 +03:00
|
|
|
#include <string.h>
|
1994-06-10 19:16:32 +04:00
|
|
|
#include <unistd.h>
|
1993-10-26 22:30:13 +03:00
|
|
|
#if __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-12-17 14:34:10 +03:00
|
|
|
#include "shlib.h"
|
1993-10-17 00:54:08 +03:00
|
|
|
#include "ld.h"
|
|
|
|
|
1996-09-11 02:17:51 +04:00
|
|
|
#ifdef __m68k__
|
|
|
|
/*
|
|
|
|
* This is a slight hack to allow the same loader to be used on
|
|
|
|
* 4k and 8k __LDPGSZ executables.
|
|
|
|
*/
|
|
|
|
static int page_size = 0x2000;
|
|
|
|
#undef PAGSIZ
|
|
|
|
#define PAGSIZ page_size
|
|
|
|
#endif /* __m68k__ */
|
|
|
|
|
1994-12-18 19:05:49 +03:00
|
|
|
#ifndef MAP_ANON
|
|
|
|
#define MAP_ANON 0
|
|
|
|
#define anon_open() do { \
|
|
|
|
if ((anon_fd = open("/dev/zero", O_RDWR, 0)) == -1) \
|
|
|
|
err("open: %s", "/dev/zero"); \
|
|
|
|
} while (0)
|
|
|
|
#define anon_close() do { \
|
|
|
|
(void)close(anon_fd); \
|
|
|
|
anon_fd = -1; \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define anon_open()
|
|
|
|
#define anon_close()
|
1993-10-23 00:18:58 +03:00
|
|
|
#endif
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
/*
|
1994-01-29 00:01:20 +03:00
|
|
|
* Loader private data, hung off <so_map>->som_spd
|
1993-10-17 00:54:08 +03:00
|
|
|
*/
|
1994-01-29 00:01:20 +03:00
|
|
|
struct somap_private {
|
|
|
|
int spd_version;
|
|
|
|
struct so_map *spd_parent;
|
|
|
|
int spd_refcount;
|
|
|
|
int spd_flags;
|
1998-03-16 02:10:21 +03:00
|
|
|
#define _RTLD_MAIN 1 /* Marks the main program */
|
|
|
|
#define _RTLD_RTLD 2 /* Marks the run-time linker */
|
|
|
|
#define _RTLD_DL 4 /* A dlopen'ed object */
|
|
|
|
#define _RTLD_GLOBAL 8 /* Map is open to global search */
|
1997-06-27 00:52:06 +04:00
|
|
|
size_t spd_size;
|
1998-05-19 16:15:38 +04:00
|
|
|
int spd_symbolsize;
|
|
|
|
long spd_symbolbase;
|
|
|
|
char *spd_stringbase;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
#ifdef SUN_COMPAT
|
1994-01-29 00:01:20 +03:00
|
|
|
long spd_offset; /* Correction for Sun main programs */
|
1993-10-17 00:54:08 +03:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_PRIVATE(smp) ((struct somap_private *)(smp)->som_spd)
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
#ifdef SUN_COMPAT
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_OFFSET(smp) (LM_PRIVATE(smp)->spd_offset)
|
1993-10-17 00:54:08 +03:00
|
|
|
#else
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_OFFSET(smp) (0)
|
1993-10-17 00:54:08 +03:00
|
|
|
#endif
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Base address for section_dispatch_table entries */
|
|
|
|
#define LM_LDBASE(smp) (smp->som_addr + LM_OFFSET(smp))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* Start of text segment */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_TXTADDR(smp) (smp->som_addr == (caddr_t)0 ? PAGSIZ : 0)
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* Start of run-time relocation_info */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_REL(smp) ((struct relocation_info *) \
|
|
|
|
(smp->som_addr + LM_OFFSET(smp) + LD_REL((smp)->som_dynamic)))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* Start of symbols */
|
1998-05-19 16:15:38 +04:00
|
|
|
#define LM_SYMBOLBASE(smp) ((long) \
|
|
|
|
(smp->som_addr + LM_OFFSET(smp) + LD_SYMBOL((smp)->som_dynamic)))
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_SYMBOL(smp, i) ((struct nzlist *) \
|
1998-05-19 16:15:38 +04:00
|
|
|
(LM_PRIVATE(smp)->spd_symbolbase + \
|
|
|
|
(i) * LM_PRIVATE(smp)->spd_symbolsize))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* Start of hash table */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_HASH(smp) ((struct rrs_hash *) \
|
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_HASH((smp)->som_dynamic)))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* Start of strings */
|
1998-05-19 16:15:38 +04:00
|
|
|
#define LM_STRINGBASE(smp) ((char *) \
|
1994-01-29 00:01:20 +03:00
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_STRINGS((smp)->som_dynamic)))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-06-05 01:56:22 +04:00
|
|
|
/* Start of search paths */
|
|
|
|
#define LM_PATHS(smp) ((char *) \
|
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_PATHS((smp)->som_dynamic)))
|
|
|
|
|
1993-10-21 03:00:09 +03:00
|
|
|
/* End of text */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_ETEXT(smp) ((char *) \
|
|
|
|
((smp)->som_addr + LM_TXTADDR(smp) + LD_TEXTSZ((smp)->som_dynamic)))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* PLT is in data segment, so don't use LM_OFFSET here */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_PLT(smp) ((jmpslot_t *) \
|
|
|
|
((smp)->som_addr + LD_PLT((smp)->som_dynamic)))
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1993-10-23 00:18:58 +03:00
|
|
|
/* Parent of link map */
|
1994-01-29 00:01:20 +03:00
|
|
|
#define LM_PARENT(smp) (LM_PRIVATE(smp)->spd_parent)
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static char __main_progname[] = "main";
|
|
|
|
static char *main_progname = __main_progname;
|
|
|
|
static char us[] = "/usr/libexec/ld.so";
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
char **environ;
|
1995-09-24 02:46:24 +04:00
|
|
|
char *__progname = us;
|
1993-10-17 18:40:11 +03:00
|
|
|
int errno;
|
1994-06-10 19:16:32 +04:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
static uid_t uid, euid;
|
|
|
|
static gid_t gid, egid;
|
|
|
|
static int careful;
|
1994-12-18 19:05:49 +03:00
|
|
|
static int anon_fd = -1;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *link_map_head, *main_map;
|
|
|
|
struct so_map **link_map_tail = &link_map_head;
|
1993-10-17 00:54:08 +03:00
|
|
|
struct rt_symbol *rt_symbol_head;
|
|
|
|
|
1995-06-05 01:56:22 +04:00
|
|
|
static char *ld_library_path;
|
1995-10-09 02:43:28 +03:00
|
|
|
static char *ld_preload_path;
|
1995-06-05 01:56:22 +04:00
|
|
|
static int no_intern_search;
|
1995-06-05 03:21:35 +04:00
|
|
|
static int ld_suppress_warnings;
|
|
|
|
static int ld_warn_non_pure_code;
|
1995-06-05 01:56:22 +04:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
static int ld_tracing;
|
|
|
|
|
1997-01-04 01:37:58 +03:00
|
|
|
static void *__dlopen __P((const char *, int));
|
1994-05-24 14:44:06 +04:00
|
|
|
static int __dlclose __P((void *));
|
1997-01-04 01:37:58 +03:00
|
|
|
static void *__dlsym __P((void *, const char *));
|
1994-05-24 14:44:06 +04:00
|
|
|
static int __dlctl __P((void *, int, void *));
|
1995-09-24 02:46:24 +04:00
|
|
|
static void __dlexit __P((void));
|
1998-05-13 01:22:28 +04:00
|
|
|
static int __dladdr __P((void *, Dl_info *));
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
static struct ld_entry ld_entry = {
|
1998-05-13 01:22:28 +04:00
|
|
|
__dlopen, __dlclose, __dlsym, __dlctl, __dlexit, __dladdr
|
1993-10-17 00:54:08 +03:00
|
|
|
};
|
|
|
|
|
1993-12-08 13:28:05 +03:00
|
|
|
void xprintf __P((char *, ...));
|
1995-06-05 04:08:38 +04:00
|
|
|
int rtld __P((int, struct crt_ldso *, struct _dynamic *));
|
|
|
|
void binder_entry __P((void));
|
|
|
|
long binder __P((jmpslot_t *));
|
1995-10-09 02:43:28 +03:00
|
|
|
static int load_subs __P((struct so_map *));
|
1995-12-28 20:57:32 +03:00
|
|
|
static struct so_map *map_object __P((struct sod *, struct so_map *));
|
1997-06-27 00:52:06 +04:00
|
|
|
static void unmap_object __P((struct so_map *));
|
1994-01-29 00:01:20 +03:00
|
|
|
static struct so_map *alloc_link_map __P(( char *, struct sod *,
|
|
|
|
struct so_map *, caddr_t,
|
1997-06-27 00:52:06 +04:00
|
|
|
size_t, struct _dynamic *));
|
|
|
|
static void free_link_map __P((struct so_map *));
|
1994-06-10 19:16:32 +04:00
|
|
|
static inline void check_text_reloc __P(( struct relocation_info *,
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *,
|
1993-10-17 00:54:08 +03:00
|
|
|
caddr_t));
|
1995-10-09 02:43:28 +03:00
|
|
|
static void init_maps __P((struct so_map *));
|
1994-01-29 00:01:20 +03:00
|
|
|
static void reloc_map __P((struct so_map *));
|
|
|
|
static void reloc_copy __P((struct so_map *));
|
1995-10-09 02:43:28 +03:00
|
|
|
static void call_map __P((struct so_map *, char *));
|
1995-06-05 01:56:22 +04:00
|
|
|
static char *rtfindlib __P((char *, int, int, int *, char *));
|
1998-03-27 02:33:12 +03:00
|
|
|
static struct nzlist *lookup __P(( const char *, struct so_map *,
|
|
|
|
struct so_map **, int));
|
1997-01-04 01:37:58 +03:00
|
|
|
static inline struct rt_symbol *lookup_rts __P((const char *));
|
|
|
|
static struct rt_symbol *enter_rts __P((const char *, long, int, caddr_t,
|
1994-01-29 00:01:20 +03:00
|
|
|
long, struct so_map *));
|
1994-10-26 23:21:42 +03:00
|
|
|
static void maphints __P((void));
|
|
|
|
static void unmaphints __P((void));
|
1998-12-16 00:35:17 +03:00
|
|
|
static int hash_string __P((const char *));
|
|
|
|
static int hinthash __P((char *, int, int));
|
|
|
|
static char *findhint __P((char *, int, int, char *));
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
static void preload __P((char *));
|
|
|
|
static void ld_trace __P((struct so_map *));
|
1997-05-03 12:00:02 +04:00
|
|
|
static void build_sod __P((const char *, struct sod *));
|
1995-03-06 23:51:24 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
static inline int
|
|
|
|
strcmp (register const char *s1, register const char *s2)
|
|
|
|
{
|
|
|
|
while (*s1 == *s2++)
|
|
|
|
if (*s1++ == 0)
|
|
|
|
return (0);
|
|
|
|
return (*(unsigned char *)s1 - *(unsigned char *)--s2);
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
/* `md-static-funcs.c' implements these functions: */
|
|
|
|
static void md_relocate_simple __P((struct relocation_info *,
|
|
|
|
long, char *));
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
#include "md-static-funcs.c"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from assembler stub that has set up crtp (passed from crt0)
|
|
|
|
* and dp (our __DYNAMIC).
|
|
|
|
*/
|
1994-01-29 00:01:20 +03:00
|
|
|
int
|
1993-10-17 00:54:08 +03:00
|
|
|
rtld(version, crtp, dp)
|
1995-06-05 04:08:38 +04:00
|
|
|
int version;
|
|
|
|
struct crt_ldso *crtp;
|
|
|
|
struct _dynamic *dp;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int nreloc; /* # of ld.so relocations */
|
|
|
|
struct relocation_info *reloc;
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_debug *ddp;
|
|
|
|
struct so_map *smp;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Check version */
|
1994-01-29 00:01:20 +03:00
|
|
|
if ( version != CRT_VERSION_BSD_2 &&
|
|
|
|
version != CRT_VERSION_BSD_3 &&
|
1995-09-24 02:46:24 +04:00
|
|
|
version != CRT_VERSION_BSD_4 &&
|
1994-01-29 00:01:20 +03:00
|
|
|
version != CRT_VERSION_SUN)
|
|
|
|
return -1;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Fixup __DYNAMIC structure */
|
1994-01-29 00:01:20 +03:00
|
|
|
(long)dp->d_un.d_sdt += crtp->crt_ba;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Divide by hand to avoid possible use of library division routine */
|
1993-10-17 00:54:08 +03:00
|
|
|
for ( nreloc = 0, n = LD_RELSZ(dp);
|
|
|
|
n > 0;
|
|
|
|
n -= sizeof(struct relocation_info) ) nreloc++;
|
|
|
|
|
|
|
|
|
|
|
|
/* Relocate ourselves */
|
1994-01-29 00:01:20 +03:00
|
|
|
for ( reloc = (struct relocation_info *)(LD_REL(dp) + crtp->crt_ba);
|
1993-10-17 00:54:08 +03:00
|
|
|
nreloc;
|
|
|
|
nreloc--, reloc++) {
|
|
|
|
|
|
|
|
register long addr = reloc->r_address + crtp->crt_ba;
|
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
md_relocate_simple(reloc, crtp->crt_ba, (char *)addr);
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1996-09-11 02:17:51 +04:00
|
|
|
/* Now (and NOT BEFORE this point) we can call externals. */
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
if (version >= CRT_VERSION_BSD_4)
|
|
|
|
__progname = crtp->crt_ldso;
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (version >= CRT_VERSION_BSD_3)
|
|
|
|
main_progname = crtp->crt_prog;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Setup out (private) environ variable */
|
|
|
|
environ = crtp->crt_ep;
|
|
|
|
|
1996-09-11 02:17:51 +04:00
|
|
|
/*
|
|
|
|
* Make sure we do not allow the library search path
|
|
|
|
* to be modified through the environment if we are
|
|
|
|
* running either setuid or setgid.
|
|
|
|
*/
|
1993-10-23 00:18:58 +03:00
|
|
|
uid = getuid(); euid = geteuid();
|
|
|
|
gid = getgid(); egid = getegid();
|
|
|
|
careful = (uid != euid) || (gid != egid);
|
|
|
|
if (careful) {
|
|
|
|
unsetenv("LD_LIBRARY_PATH");
|
|
|
|
unsetenv("LD_PRELOAD");
|
|
|
|
}
|
|
|
|
|
1996-09-11 02:17:51 +04:00
|
|
|
#ifdef __m68k__
|
|
|
|
/*
|
|
|
|
* Locate the a.out header and check the machine ID.
|
|
|
|
* If we see MID_M68K4K, we alter the page size we
|
|
|
|
* use for address calculations. This allows the
|
|
|
|
* same loader to be used for 4k and 8k executables
|
|
|
|
* and libraries. Trust me; the world is a better
|
|
|
|
* place because this.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
register long textaddr;
|
|
|
|
register struct exec *eh;
|
|
|
|
|
|
|
|
/* XXX Assume this is near the start of text. */
|
|
|
|
textaddr = ((long)crtp->crt_bp) & ~0xFFF;
|
|
|
|
eh = (struct exec *) textaddr;
|
|
|
|
#if 0
|
|
|
|
xprintf("%s: textaddr is 0x%x\n", us, textaddr);
|
|
|
|
xprintf("\t magic=0x%x\n", eh->a_midmag);
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Use this rather than N_PAGSIZ(), to be
|
|
|
|
* safe...
|
|
|
|
*/
|
|
|
|
if (N_GETMID((*eh)) == MID_M68K4K)
|
|
|
|
page_size = 0x1000;
|
|
|
|
#if 0
|
|
|
|
xprintf("\t page_size=%d\n", page_size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* __m68k__ */
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
/* Setup directory search */
|
1995-06-05 01:56:22 +04:00
|
|
|
ld_library_path = getenv("LD_LIBRARY_PATH");
|
|
|
|
add_search_path(ld_library_path);
|
1994-06-10 19:16:32 +04:00
|
|
|
if (getenv("LD_NOSTD_PATH") == NULL)
|
|
|
|
std_search_path();
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-06-05 03:21:35 +04:00
|
|
|
ld_suppress_warnings = getenv("LD_SUPPRESS_WARNINGS") != NULL;
|
|
|
|
ld_warn_non_pure_code = getenv("LD_WARN_NON_PURE_CODE") != NULL;
|
|
|
|
|
1999-04-23 23:29:30 +04:00
|
|
|
no_intern_search = getenv("LD_NO_INTERN_SEARCH") != 0;
|
1995-06-05 01:56:22 +04:00
|
|
|
|
1994-12-18 19:05:49 +03:00
|
|
|
anon_open();
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
/*
|
|
|
|
* Init object administration. We start off with a map description
|
|
|
|
* for `main' and `rtld'.
|
|
|
|
*/
|
|
|
|
smp = alloc_link_map(main_progname, (struct sod *)0, (struct so_map *)0,
|
1997-06-27 00:52:06 +04:00
|
|
|
(caddr_t)0, 0, crtp->crt_dp);
|
1995-10-09 02:43:28 +03:00
|
|
|
LM_PRIVATE(smp)->spd_refcount++;
|
1998-03-16 02:10:21 +03:00
|
|
|
LM_PRIVATE(smp)->spd_flags |= _RTLD_MAIN | _RTLD_GLOBAL;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
smp = alloc_link_map(us, (struct sod *)0, (struct so_map *)0,
|
1997-06-27 00:52:06 +04:00
|
|
|
(caddr_t)crtp->crt_ba, 0, dp);
|
1995-10-09 02:43:28 +03:00
|
|
|
LM_PRIVATE(smp)->spd_refcount++;
|
1998-03-16 02:10:21 +03:00
|
|
|
LM_PRIVATE(smp)->spd_flags |= _RTLD_RTLD;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1999-01-29 02:58:49 +03:00
|
|
|
/* Fill in some field in main's __DYNAMIC structure */
|
|
|
|
if (version >= CRT_VERSION_BSD_4)
|
|
|
|
crtp->crt_ldentry = &ld_entry;
|
|
|
|
else
|
|
|
|
crtp->crt_dp->d_entry = &ld_entry;
|
|
|
|
|
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
/* Handle LD_PRELOAD's here */
|
|
|
|
ld_preload_path = getenv("LD_PRELOAD");
|
|
|
|
if (ld_preload_path != NULL)
|
|
|
|
preload(ld_preload_path);
|
|
|
|
|
|
|
|
/* Load subsidiary objects into the process address space */
|
|
|
|
ld_tracing = (int)getenv("LD_TRACE_LOADED_OBJECTS");
|
|
|
|
load_subs(link_map_head);
|
|
|
|
if (ld_tracing) {
|
|
|
|
ld_trace(link_map_head);
|
|
|
|
exit(0);
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
init_maps(link_map_head);
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
crtp->crt_dp->d_un.d_sdt->sdt_loaded = link_map_head->som_next;
|
1993-11-11 00:37:39 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
ddp = crtp->crt_dp->d_debug;
|
|
|
|
ddp->dd_cc = rt_symbol_head;
|
|
|
|
if (ddp->dd_in_debugger) {
|
1993-11-11 00:37:39 +03:00
|
|
|
caddr_t addr = (caddr_t)((long)crtp->crt_bp & (~(PAGSIZ - 1)));
|
|
|
|
|
|
|
|
/* Set breakpoint for the benefit of debuggers */
|
|
|
|
if (mprotect(addr, PAGSIZ,
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
|
1994-06-10 19:16:32 +04:00
|
|
|
err(1, "Cannot set breakpoint (%s)", main_progname);
|
1993-11-11 00:37:39 +03:00
|
|
|
}
|
1994-06-10 19:16:32 +04:00
|
|
|
md_set_breakpoint((long)crtp->crt_bp, (long *)&ddp->dd_bpt_shadow);
|
1993-11-11 00:37:39 +03:00
|
|
|
if (mprotect(addr, PAGSIZ, PROT_READ|PROT_EXEC) == -1) {
|
1994-06-10 19:16:32 +04:00
|
|
|
err(1, "Cannot re-protect breakpoint (%s)",
|
|
|
|
main_progname);
|
1993-11-11 00:37:39 +03:00
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
ddp->dd_bpt_addr = crtp->crt_bp;
|
1993-11-11 00:37:39 +03:00
|
|
|
if (link_map_head)
|
1994-01-29 00:01:20 +03:00
|
|
|
ddp->dd_sym_loaded = 1;
|
1993-11-11 00:37:39 +03:00
|
|
|
}
|
1993-12-08 13:28:05 +03:00
|
|
|
|
1994-10-26 23:21:42 +03:00
|
|
|
/* Close the hints file */
|
|
|
|
unmaphints();
|
|
|
|
|
1993-12-08 13:28:05 +03:00
|
|
|
/* Close our file descriptor */
|
|
|
|
(void)close(crtp->crt_ldfd);
|
1994-12-18 19:05:49 +03:00
|
|
|
anon_close();
|
1994-01-29 00:01:20 +03:00
|
|
|
return 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
static int
|
|
|
|
load_subs(smp)
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *smp;
|
1995-10-09 02:43:28 +03:00
|
|
|
{
|
1998-03-16 02:10:21 +03:00
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
/* Propagate _RTLD_GLOBAL parent flag */
|
|
|
|
flag |= (LM_PRIVATE(smp)->spd_flags & _RTLD_GLOBAL);
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
for (; smp; smp = smp->som_next) {
|
1994-01-29 00:01:20 +03:00
|
|
|
struct sod *sodp;
|
|
|
|
long next = 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1994-01-29 00:01:20 +03:00
|
|
|
continue;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (smp->som_dynamic)
|
|
|
|
next = LD_NEED(smp->som_dynamic);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
while (next) {
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *newmap;
|
|
|
|
|
|
|
|
sodp = (struct sod *)(LM_LDBASE(smp) + next);
|
1995-10-09 02:43:28 +03:00
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
if ((newmap = map_object(sodp, smp)) == NULL) {
|
1995-10-09 02:43:28 +03:00
|
|
|
if (!ld_tracing) {
|
1994-01-29 00:01:20 +03:00
|
|
|
char *fmt = sodp->sod_library ?
|
1994-06-10 19:16:32 +04:00
|
|
|
"%s: lib%s.so.%d.%d" :
|
|
|
|
"%s: %s";
|
1995-12-28 20:57:32 +03:00
|
|
|
err(1, fmt, main_progname,
|
|
|
|
sodp->sod_name+LM_LDBASE(smp),
|
1994-01-29 00:01:20 +03:00
|
|
|
sodp->sod_major,
|
1994-06-10 19:16:32 +04:00
|
|
|
sodp->sod_minor);
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
1997-06-27 00:52:06 +04:00
|
|
|
newmap = alloc_link_map(NULL, sodp, smp, 0, 0, 0);
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
|
|
|
LM_PRIVATE(newmap)->spd_refcount++;
|
1998-03-16 02:10:21 +03:00
|
|
|
/* Note: existing maps also acquire the new flag */
|
|
|
|
LM_PRIVATE(newmap)->spd_flags |= flag;
|
1994-01-29 00:01:20 +03:00
|
|
|
next = sodp->sod_next;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
1995-10-09 02:43:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
void
|
|
|
|
ld_trace(smp)
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
char *fmt1, *fmt2, *fmt, *main_local;
|
|
|
|
int c;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
if ((main_local = getenv("LD_TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
|
|
|
|
main_local = "";
|
|
|
|
|
|
|
|
if ((fmt1 = getenv("LD_TRACE_LOADED_OBJECTS_FMT1")) == NULL)
|
|
|
|
fmt1 = "\t-l%o.%m => %p (%x)\n";
|
|
|
|
|
|
|
|
if ((fmt2 = getenv("LD_TRACE_LOADED_OBJECTS_FMT2")) == NULL)
|
|
|
|
fmt2 = "\t%o (%x)\n";
|
|
|
|
|
|
|
|
for (; smp; smp = smp->som_next) {
|
1994-01-29 00:01:20 +03:00
|
|
|
struct sod *sodp;
|
|
|
|
char *name, *path;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if ((sodp = smp->som_sod) == NULL)
|
1993-10-17 00:54:08 +03:00
|
|
|
continue;
|
1995-10-09 02:43:28 +03:00
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
name = (char *)sodp->sod_name;
|
|
|
|
if (LM_PARENT(smp))
|
|
|
|
name += (long)LM_LDBASE(LM_PARENT(smp));
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if ((path = smp->som_path) == NULL)
|
1993-10-17 00:54:08 +03:00
|
|
|
path = "not found";
|
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
fmt = sodp->sod_library ? fmt1 : fmt2;
|
|
|
|
while ((c = *fmt++) != '\0') {
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
putchar(c);
|
|
|
|
continue;
|
|
|
|
case '\\':
|
|
|
|
switch (c = *fmt) {
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
case 'n':
|
|
|
|
putchar('\n');
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
putchar('\t');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
switch (c = *fmt) {
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
case '%':
|
|
|
|
default:
|
|
|
|
putchar(c);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
printf("%s", main_local);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
printf("%s", main_progname);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
printf("%s", name);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
printf("%d", sodp->sod_major);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
printf("%d", sodp->sod_minor);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
printf("%s", path);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
printf("%p", smp->som_addr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-23 00:18:58 +03:00
|
|
|
/*
|
1994-05-24 14:44:06 +04:00
|
|
|
* Allocate a new link map for shared object NAME loaded at ADDR as a
|
1993-10-23 00:18:58 +03:00
|
|
|
* result of the presence of link object LOP in the link map PARENT.
|
|
|
|
*/
|
1995-09-24 02:46:24 +04:00
|
|
|
static struct so_map *
|
1997-06-27 00:52:06 +04:00
|
|
|
alloc_link_map(path, sodp, parent, addr, size, dp)
|
1994-01-29 00:01:20 +03:00
|
|
|
char *path;
|
|
|
|
struct sod *sodp;
|
|
|
|
struct so_map *parent;
|
|
|
|
caddr_t addr;
|
1997-06-27 00:52:06 +04:00
|
|
|
size_t size;
|
1994-01-29 00:01:20 +03:00
|
|
|
struct _dynamic *dp;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *smp;
|
|
|
|
struct somap_private *smpp;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
smpp = (struct somap_private *)xmalloc(sizeof(struct somap_private));
|
|
|
|
smp = (struct so_map *)xmalloc(sizeof(struct so_map));
|
|
|
|
smp->som_next = NULL;
|
|
|
|
*link_map_tail = smp;
|
|
|
|
link_map_tail = &smp->som_next;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-06-21 03:07:03 +04:00
|
|
|
/*smp->som_sodbase = 0; NOT USED */
|
|
|
|
smp->som_write = 0;
|
1994-01-29 00:01:20 +03:00
|
|
|
smp->som_addr = addr;
|
1994-06-24 17:31:34 +04:00
|
|
|
smp->som_path = path?strdup(path):NULL;
|
1994-01-29 00:01:20 +03:00
|
|
|
smp->som_sod = sodp;
|
|
|
|
smp->som_dynamic = dp;
|
|
|
|
smp->som_spd = (caddr_t)smpp;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
smpp->spd_refcount = 0;
|
|
|
|
smpp->spd_flags = 0;
|
|
|
|
smpp->spd_parent = parent;
|
1997-06-27 00:52:06 +04:00
|
|
|
smpp->spd_size = size;
|
1998-09-12 19:03:19 +04:00
|
|
|
if (dp == NULL)
|
|
|
|
return (smp);
|
|
|
|
|
|
|
|
/*XXX*/ if (addr == 0) main_map = smp;
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
#ifdef SUN_COMPAT
|
1994-01-29 00:01:20 +03:00
|
|
|
smpp->spd_offset =
|
1998-09-12 19:03:19 +04:00
|
|
|
(addr==0 && dp->d_version==LD_VERSION_SUN) ? PAGSIZ : 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
#endif
|
1998-05-19 16:15:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre-compute the location of symbol and string tables;
|
|
|
|
* they do not change while this object is loaded.
|
|
|
|
*/
|
|
|
|
smpp->spd_symbolsize = LD_VERSION_NZLIST_P(dp->d_version)
|
|
|
|
? sizeof(struct nzlist)
|
|
|
|
: sizeof(struct nlist);
|
|
|
|
smpp->spd_symbolbase = LM_SYMBOLBASE(smp);
|
|
|
|
smpp->spd_stringbase = LM_STRINGBASE(smp);
|
|
|
|
|
1998-09-12 19:03:19 +04:00
|
|
|
return (smp);
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1997-06-27 00:52:06 +04:00
|
|
|
/*
|
|
|
|
* Free the link map for an object being unmapped. The link map
|
|
|
|
* has already been removed from the link map list, so it can't be used
|
|
|
|
* after it's been unmapped.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
free_link_map(smp)
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
if ((LM_PRIVATE(smp)->spd_flags & _RTLD_DL) != 0) {
|
1997-07-01 02:10:06 +04:00
|
|
|
/* free synthetic sod structure allocated in __dlopen() */
|
|
|
|
free((char *)smp->som_sod->sod_name);
|
|
|
|
free(smp->som_sod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the link map structure. */
|
1997-06-27 00:52:06 +04:00
|
|
|
free(smp->som_spd);
|
|
|
|
if (smp->som_path != NULL)
|
1997-07-01 00:49:40 +04:00
|
|
|
free(smp->som_path);
|
1997-06-27 00:52:06 +04:00
|
|
|
free(smp);
|
|
|
|
}
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
/*
|
1995-10-09 02:43:28 +03:00
|
|
|
* Map object identified by link object SODP which was found
|
|
|
|
* in link map SMP.
|
1993-10-17 00:54:08 +03:00
|
|
|
*/
|
1995-09-24 02:46:24 +04:00
|
|
|
static struct so_map *
|
1995-12-28 20:57:32 +03:00
|
|
|
map_object(sodp, smp)
|
1994-01-29 00:01:20 +03:00
|
|
|
struct sod *sodp;
|
|
|
|
struct so_map *smp;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1995-12-28 20:57:32 +03:00
|
|
|
char *name;
|
1994-01-29 00:01:20 +03:00
|
|
|
struct _dynamic *dp;
|
1995-10-09 02:43:28 +03:00
|
|
|
char *path, *ipath;
|
1994-12-18 19:05:49 +03:00
|
|
|
int fd;
|
1993-10-17 00:54:08 +03:00
|
|
|
caddr_t addr;
|
|
|
|
struct exec hdr;
|
1993-10-23 00:18:58 +03:00
|
|
|
int usehints = 0;
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *p;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
name = (char *)sodp->sod_name;
|
|
|
|
if (smp)
|
|
|
|
name += (long)LM_LDBASE(smp);
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (sodp->sod_library) {
|
1993-10-23 00:18:58 +03:00
|
|
|
usehints = 1;
|
|
|
|
again:
|
1995-10-09 02:43:28 +03:00
|
|
|
if (smp == NULL || no_intern_search ||
|
|
|
|
LD_PATHS(smp->som_dynamic) == 0) {
|
1995-06-05 01:56:22 +04:00
|
|
|
ipath = NULL;
|
|
|
|
} else {
|
1995-12-28 20:57:32 +03:00
|
|
|
ipath = LM_PATHS(smp);
|
1995-06-05 01:56:22 +04:00
|
|
|
add_search_path(ipath);
|
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
path = rtfindlib(name, sodp->sod_major,
|
1995-06-05 01:56:22 +04:00
|
|
|
sodp->sod_minor, &usehints, ipath);
|
|
|
|
if (ipath)
|
|
|
|
remove_search_path(ipath);
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (path == NULL) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
} else {
|
1994-01-29 00:01:20 +03:00
|
|
|
if (careful && *name != '/') {
|
|
|
|
errno = EACCES;
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
path = name;
|
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Check if already loaded */
|
|
|
|
for (p = link_map_head; p; p = p->som_next)
|
|
|
|
if (p->som_path && strcmp(p->som_path, path) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
return p;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY, 0)) == -1) {
|
1993-10-23 00:18:58 +03:00
|
|
|
if (usehints) {
|
|
|
|
usehints = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
1994-01-29 00:01:20 +03:00
|
|
|
return NULL;
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
|
1994-01-29 00:01:20 +03:00
|
|
|
(void)close(fd);
|
|
|
|
/*errno = x;*/
|
|
|
|
return NULL;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (N_BADMAG(hdr)) {
|
|
|
|
(void)close(fd);
|
|
|
|
errno = EFTYPE;
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-12-07 23:30:53 +03:00
|
|
|
if ((addr = mmap(0, hdr.a_text + hdr.a_data + hdr.a_bss,
|
1995-10-09 02:43:28 +03:00
|
|
|
PROT_READ|PROT_EXEC,
|
1999-06-18 01:09:47 +04:00
|
|
|
MAP_FILE|MAP_PRIVATE, fd, 0)) == (caddr_t)-1) {
|
1994-01-29 00:01:20 +03:00
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-06-27 00:52:06 +04:00
|
|
|
#if DEBUG
|
|
|
|
xprintf("map1: 0x%x for 0x%x\n", addr, hdr.a_text + hdr.a_data + hdr.a_bss);
|
|
|
|
#endif
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-12-19 15:21:21 +03:00
|
|
|
if (mprotect(addr + hdr.a_text, hdr.a_data,
|
1995-10-09 02:43:28 +03:00
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
|
1994-06-10 19:16:32 +04:00
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-12-18 18:38:55 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
if (mmap(addr + hdr.a_text + hdr.a_data, hdr.a_bss,
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC,
|
1999-06-18 01:09:47 +04:00
|
|
|
MAP_ANON|MAP_PRIVATE|MAP_FIXED,
|
1995-10-09 02:43:28 +03:00
|
|
|
anon_fd, 0) == (caddr_t)-1) {
|
1994-01-29 00:01:20 +03:00
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
(void)close(fd);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Assume _DYNAMIC is the first data item */
|
1994-01-29 00:01:20 +03:00
|
|
|
dp = (struct _dynamic *)(addr+hdr.a_text);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Fixup __DYNAMIC structure */
|
1994-01-29 00:01:20 +03:00
|
|
|
(long)dp->d_un.d_sdt += (long)addr;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1997-06-27 00:52:06 +04:00
|
|
|
return alloc_link_map(path, sodp, smp, addr,
|
|
|
|
hdr.a_text + hdr.a_data + hdr.a_bss, dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmap a mapped object.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
unmap_object(smp)
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
struct so_map *p, **pp;
|
|
|
|
|
|
|
|
/* remove from link map list */
|
|
|
|
pp = &link_map_head;
|
|
|
|
while ((p = *pp) != NULL) {
|
|
|
|
if (p == smp)
|
|
|
|
break;
|
|
|
|
pp = &p->som_next;
|
|
|
|
}
|
|
|
|
if (p == NULL) {
|
|
|
|
warnx("warning: link map entry for %s not on link map list!",
|
|
|
|
smp->som_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pp = smp->som_next; /* make list skip it */
|
|
|
|
if (link_map_tail == &smp->som_next) /* and readjust tail pointer */
|
|
|
|
link_map_tail = pp;
|
|
|
|
|
|
|
|
/* unmap from address space */
|
|
|
|
(void)munmap(smp->som_addr, LM_PRIVATE(smp)->spd_size);
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
void
|
|
|
|
init_maps(head)
|
|
|
|
struct so_map *head;
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
|
|
|
|
/* Relocate all loaded objects according to their RRS segments */
|
|
|
|
for (smp = head; smp; smp = smp->som_next) {
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1995-10-09 02:43:28 +03:00
|
|
|
continue;
|
|
|
|
reloc_map(smp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy any relocated initialized data. */
|
|
|
|
for (smp = head; smp; smp = smp->som_next) {
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1995-10-09 02:43:28 +03:00
|
|
|
continue;
|
|
|
|
reloc_copy(smp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call any object initialization routines. */
|
|
|
|
for (smp = head; smp; smp = smp->som_next) {
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1995-10-09 02:43:28 +03:00
|
|
|
continue;
|
|
|
|
call_map(smp, ".init");
|
|
|
|
call_map(smp, "__init");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-06-10 19:16:32 +04:00
|
|
|
static inline void
|
1994-01-29 00:01:20 +03:00
|
|
|
check_text_reloc(r, smp, addr)
|
1995-06-05 04:08:38 +04:00
|
|
|
struct relocation_info *r;
|
|
|
|
struct so_map *smp;
|
|
|
|
caddr_t addr;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
char *sym;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (addr >= LM_ETEXT(smp))
|
|
|
|
return;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (RELOC_EXTERN_P(r))
|
1998-05-19 16:15:38 +04:00
|
|
|
sym = LM_PRIVATE(smp)->spd_stringbase +
|
|
|
|
LM_SYMBOL(smp, RELOC_SYMBOL(r))->nz_strx;
|
1994-01-29 00:01:20 +03:00
|
|
|
else
|
|
|
|
sym = "";
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1995-06-05 03:21:35 +04:00
|
|
|
if (ld_warn_non_pure_code && !ld_suppress_warnings)
|
1994-12-07 23:30:53 +03:00
|
|
|
warnx("warning: non pure code in %s at %x (%s)",
|
1994-01-29 00:01:20 +03:00
|
|
|
smp->som_path, r->r_address, sym);
|
1993-12-21 01:44:35 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (smp->som_write == 0 &&
|
|
|
|
mprotect(smp->som_addr + LM_TXTADDR(smp),
|
|
|
|
LD_TEXTSZ(smp->som_dynamic),
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-06-10 19:16:32 +04:00
|
|
|
err(1, "Cannot enable writes to %s:%s",
|
1994-01-29 00:01:20 +03:00
|
|
|
main_progname, smp->som_path);
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
smp->som_write = 1;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
static void
|
|
|
|
reloc_map(smp)
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
struct _dynamic *dp = smp->som_dynamic;
|
|
|
|
struct relocation_info *r = LM_REL(smp);
|
|
|
|
struct relocation_info *rend = r + LD_RELSZ(dp)/sizeof(*r);
|
1998-05-19 16:15:38 +04:00
|
|
|
long symbolbase = LM_PRIVATE(smp)->spd_symbolbase;
|
|
|
|
char *stringbase = LM_PRIVATE(smp)->spd_stringbase;
|
|
|
|
int symsize = LM_PRIVATE(smp)->spd_symbolsize;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
|
|
|
if (LD_PLTSZ(dp))
|
|
|
|
md_fix_jmpslot(LM_PLT(smp),
|
1998-10-19 07:09:31 +04:00
|
|
|
(long)LM_PLT(smp), (long)binder_entry, 1);
|
1994-01-29 00:01:20 +03:00
|
|
|
|
|
|
|
for (; r < rend; r++) {
|
|
|
|
char *sym;
|
|
|
|
caddr_t addr = smp->som_addr + r->r_address;
|
|
|
|
|
|
|
|
check_text_reloc(r, smp, addr);
|
|
|
|
|
|
|
|
if (RELOC_EXTERN_P(r)) {
|
|
|
|
struct so_map *src_map = NULL;
|
|
|
|
struct nzlist *p, *np;
|
|
|
|
long relocation = md_get_addend(r, addr);
|
|
|
|
|
|
|
|
if (RELOC_LAZY_P(r))
|
|
|
|
continue;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
p = (struct nzlist *)
|
|
|
|
(symbolbase + symsize * RELOC_SYMBOL(r));
|
|
|
|
|
|
|
|
if (p->nz_type == (N_SETV + N_EXT))
|
|
|
|
src_map = smp;
|
|
|
|
|
|
|
|
sym = stringbase + p->nz_strx;
|
|
|
|
|
1999-02-27 06:34:05 +03:00
|
|
|
#if defined(__arm32__) && 1 /* XXX MAGIC! */
|
|
|
|
if (r->r_baserel && r->r_length == 2)
|
|
|
|
relocation = 0;
|
|
|
|
#endif
|
1998-03-27 02:33:12 +03:00
|
|
|
np = lookup(sym, smp, &src_map, 0/*XXX-jumpslots!*/);
|
1994-01-29 00:01:20 +03:00
|
|
|
if (np == NULL)
|
1994-06-10 19:16:32 +04:00
|
|
|
errx(1, "Undefined symbol \"%s\" in %s:%s\n",
|
1994-01-29 00:01:20 +03:00
|
|
|
sym, main_progname, smp->som_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Found symbol definition.
|
|
|
|
* If it's in a link map, adjust value
|
|
|
|
* according to the load address of that map.
|
|
|
|
* Otherwise it's a run-time allocated common
|
|
|
|
* whose value is already up-to-date.
|
|
|
|
*/
|
|
|
|
relocation += np->nz_value;
|
|
|
|
if (src_map)
|
|
|
|
relocation += (long)src_map->som_addr;
|
|
|
|
|
|
|
|
if (RELOC_PCREL_P(r))
|
|
|
|
relocation -= (long)smp->som_addr;
|
|
|
|
|
|
|
|
if (RELOC_COPY_P(r) && src_map) {
|
1998-02-18 02:07:33 +03:00
|
|
|
if (p->nz_size != np->nz_size)
|
1998-12-16 00:35:17 +03:00
|
|
|
warnx("symbol %s at %p in %s changed "
|
|
|
|
"size: expected %ld, actual %ld",
|
1998-02-18 02:07:33 +03:00
|
|
|
sym,
|
|
|
|
src_map->som_addr + np->nz_value,
|
|
|
|
src_map->som_path,
|
|
|
|
p->nz_size, np->nz_size);
|
1994-01-29 00:01:20 +03:00
|
|
|
(void)enter_rts(sym,
|
|
|
|
(long)addr,
|
|
|
|
N_DATA + N_EXT,
|
|
|
|
src_map->som_addr + np->nz_value,
|
1998-02-18 02:07:33 +03:00
|
|
|
p->nz_size, src_map);
|
1994-01-29 00:01:20 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
md_relocate(r, relocation, addr, 0);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
} else {
|
|
|
|
md_relocate(r,
|
1993-10-17 00:54:08 +03:00
|
|
|
#ifdef SUN_COMPAT
|
1994-01-29 00:01:20 +03:00
|
|
|
md_get_rt_segment_addend(r, addr)
|
1993-10-17 00:54:08 +03:00
|
|
|
#else
|
1994-01-29 00:01:20 +03:00
|
|
|
md_get_addend(r, addr)
|
1993-10-17 00:54:08 +03:00
|
|
|
#endif
|
1994-01-29 00:01:20 +03:00
|
|
|
+ (long)smp->som_addr, addr, 0);
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smp->som_write) {
|
|
|
|
if (mprotect(smp->som_addr + LM_TXTADDR(smp),
|
|
|
|
LD_TEXTSZ(smp->som_dynamic),
|
1993-11-11 00:37:39 +03:00
|
|
|
PROT_READ|PROT_EXEC) == -1) {
|
|
|
|
|
1994-06-10 19:16:32 +04:00
|
|
|
err(1, "Cannot disable writes to %s:%s\n",
|
1994-01-29 00:01:20 +03:00
|
|
|
main_progname, smp->som_path);
|
1993-11-11 00:37:39 +03:00
|
|
|
}
|
1994-01-29 00:01:20 +03:00
|
|
|
smp->som_write = 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-23 00:18:58 +03:00
|
|
|
static void
|
1994-01-29 00:01:20 +03:00
|
|
|
reloc_copy(smp)
|
|
|
|
struct so_map *smp;
|
1993-10-23 00:18:58 +03:00
|
|
|
{
|
|
|
|
struct rt_symbol *rtsp;
|
|
|
|
|
|
|
|
for (rtsp = rt_symbol_head; rtsp; rtsp = rtsp->rt_next)
|
1994-01-29 00:01:20 +03:00
|
|
|
if ((rtsp->rt_smp == NULL || rtsp->rt_smp == smp) &&
|
|
|
|
rtsp->rt_sp->nz_type == N_DATA + N_EXT) {
|
1993-10-23 00:18:58 +03:00
|
|
|
bcopy(rtsp->rt_srcaddr, (caddr_t)rtsp->rt_sp->nz_value,
|
|
|
|
rtsp->rt_sp->nz_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
static void
|
1995-10-09 02:43:28 +03:00
|
|
|
call_map(smp, sym)
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *smp;
|
|
|
|
char *sym;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *src_map = smp;
|
|
|
|
struct nzlist *np;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-03-27 02:33:12 +03:00
|
|
|
np = lookup(sym, smp, &src_map, 1);
|
1998-08-06 04:03:28 +04:00
|
|
|
if (np) {
|
|
|
|
#if DEBUG
|
1998-08-13 11:34:05 +04:00
|
|
|
xprintf("call_map: %s at %#x+%#x enter\n", sym, src_map->som_addr, np->nz_value);
|
1998-08-06 04:03:28 +04:00
|
|
|
#endif
|
1999-02-27 06:34:05 +03:00
|
|
|
#if defined(__arm32__) && 1 /* XXX MAGIC! */
|
|
|
|
(*(void (*) __P((u_int)))(src_map->som_addr + np->nz_value))((u_int)src_map->som_addr);
|
|
|
|
#else
|
1995-06-05 04:08:38 +04:00
|
|
|
(*(void (*) __P((void)))(src_map->som_addr + np->nz_value))();
|
1999-02-27 06:34:05 +03:00
|
|
|
#endif
|
1998-08-06 04:03:28 +04:00
|
|
|
#if DEBUG
|
1998-08-13 11:34:05 +04:00
|
|
|
xprintf("call_map: %s at %#x+%#x exit\n", sym, src_map->som_addr, np->nz_value);
|
1998-08-06 04:03:28 +04:00
|
|
|
#endif
|
|
|
|
}
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/*
|
|
|
|
* Run-time common symbol table.
|
|
|
|
*/
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
#define RTC_TABSIZE 57
|
|
|
|
static struct rt_symbol *rt_symtab[RTC_TABSIZE];
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/*
|
|
|
|
* Compute hash value for run-time symbol table
|
|
|
|
*/
|
1995-09-24 02:46:24 +04:00
|
|
|
static inline int
|
1994-01-29 00:01:20 +03:00
|
|
|
hash_string(key)
|
1998-12-16 00:35:17 +03:00
|
|
|
const char *key;
|
1994-01-29 00:01:20 +03:00
|
|
|
{
|
1998-12-16 00:35:17 +03:00
|
|
|
const char *cp;
|
|
|
|
int k;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
cp = key;
|
|
|
|
k = 0;
|
|
|
|
while (*cp)
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
return k;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/*
|
|
|
|
* Lookup KEY in the run-time common symbol table.
|
|
|
|
*/
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static inline struct rt_symbol *
|
1994-01-29 00:01:20 +03:00
|
|
|
lookup_rts(key)
|
1997-01-04 01:37:58 +03:00
|
|
|
const char *key;
|
1993-12-21 01:44:35 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
register int hashval;
|
|
|
|
register struct rt_symbol *rtsp;
|
1993-12-21 01:44:35 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Determine which bucket. */
|
|
|
|
|
|
|
|
hashval = hash_string(key) % RTC_TABSIZE;
|
|
|
|
|
|
|
|
/* Search the bucket. */
|
|
|
|
|
|
|
|
for (rtsp = rt_symtab[hashval]; rtsp; rtsp = rtsp->rt_link)
|
|
|
|
if (strcmp(key, rtsp->rt_sp->nz_name) == 0)
|
|
|
|
return rtsp;
|
|
|
|
|
|
|
|
return NULL;
|
1993-12-21 01:44:35 +03:00
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static struct rt_symbol *
|
1994-01-29 00:01:20 +03:00
|
|
|
enter_rts(name, value, type, srcaddr, size, smp)
|
1997-01-04 01:37:58 +03:00
|
|
|
const char *name;
|
1994-01-29 00:01:20 +03:00
|
|
|
long value;
|
|
|
|
int type;
|
|
|
|
caddr_t srcaddr;
|
|
|
|
long size;
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
register int hashval;
|
|
|
|
register struct rt_symbol *rtsp, **rpp;
|
|
|
|
|
|
|
|
/* Determine which bucket */
|
|
|
|
hashval = hash_string(name) % RTC_TABSIZE;
|
|
|
|
|
|
|
|
/* Find end of bucket */
|
|
|
|
for (rpp = &rt_symtab[hashval]; *rpp; rpp = &(*rpp)->rt_link)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Allocate new common symbol */
|
|
|
|
rtsp = (struct rt_symbol *)malloc(sizeof(struct rt_symbol));
|
|
|
|
rtsp->rt_sp = (struct nzlist *)malloc(sizeof(struct nzlist));
|
|
|
|
rtsp->rt_sp->nz_name = strdup(name);
|
|
|
|
rtsp->rt_sp->nz_value = value;
|
|
|
|
rtsp->rt_sp->nz_type = type;
|
|
|
|
rtsp->rt_sp->nz_size = size;
|
|
|
|
rtsp->rt_srcaddr = srcaddr;
|
|
|
|
rtsp->rt_smp = smp;
|
|
|
|
rtsp->rt_link = NULL;
|
|
|
|
|
|
|
|
/* Link onto linear list as well */
|
|
|
|
rtsp->rt_next = rt_symbol_head;
|
|
|
|
rt_symbol_head = rtsp;
|
|
|
|
|
|
|
|
*rpp = rtsp;
|
|
|
|
|
|
|
|
return rtsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-11 00:37:39 +03:00
|
|
|
/*
|
|
|
|
* Lookup NAME in the link maps. The link map producing a definition
|
1993-12-21 01:44:35 +03:00
|
|
|
* is returned in SRC_MAP. If SRC_MAP is not NULL on entry the search is
|
|
|
|
* confined to that map. If STRONG is set, the symbol returned must
|
1993-11-11 00:37:39 +03:00
|
|
|
* have a proper type (used by binder()).
|
|
|
|
*/
|
1995-09-24 02:46:24 +04:00
|
|
|
static struct nzlist *
|
1998-03-27 02:33:12 +03:00
|
|
|
lookup(name, ref_map, src_map, strong)
|
1997-01-04 01:37:58 +03:00
|
|
|
const char *name;
|
1998-03-27 02:33:12 +03:00
|
|
|
struct so_map *ref_map; /* map cont. the reference */
|
|
|
|
struct so_map **src_map; /* map cont. the definition IN/OUT */
|
1994-01-29 00:01:20 +03:00
|
|
|
int strong;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
|
|
|
long common_size = 0;
|
1996-12-22 00:53:16 +03:00
|
|
|
struct so_map *smp, *weak_smp;
|
1993-10-17 00:54:08 +03:00
|
|
|
struct rt_symbol *rtsp;
|
1996-12-22 00:53:16 +03:00
|
|
|
struct nzlist *weak_np = 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
if ((rtsp = lookup_rts(name)) != NULL)
|
1998-05-19 16:15:38 +04:00
|
|
|
return (rtsp->rt_sp);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
weak_smp = NULL; /* XXX - gcc! */
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
/*
|
|
|
|
* Search all maps for a definition of NAME
|
|
|
|
*/
|
1994-01-29 00:01:20 +03:00
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
1994-12-18 18:38:55 +03:00
|
|
|
int buckets;
|
1994-05-24 14:44:06 +04:00
|
|
|
long hashval;
|
1993-10-17 00:54:08 +03:00
|
|
|
struct rrs_hash *hp;
|
|
|
|
char *cp;
|
|
|
|
struct nzlist *np;
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Some local caching */
|
|
|
|
long symbolbase;
|
|
|
|
struct rrs_hash *hashbase;
|
|
|
|
char *stringbase;
|
|
|
|
int symsize;
|
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
np = NULL; /* XXX - gcc! */
|
|
|
|
|
1994-12-18 18:38:55 +03:00
|
|
|
if (*src_map && smp != *src_map)
|
1994-01-29 00:01:20 +03:00
|
|
|
continue;
|
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
/*
|
|
|
|
* When doing a global search, consider only maps
|
|
|
|
* marked for global lookup.
|
|
|
|
*/
|
1998-03-27 02:33:12 +03:00
|
|
|
if (*src_map == NULL && smp != ref_map &&
|
1998-03-16 02:10:21 +03:00
|
|
|
(LM_PRIVATE(smp)->spd_flags & _RTLD_GLOBAL) == 0)
|
|
|
|
continue;
|
|
|
|
|
1994-12-18 18:38:55 +03:00
|
|
|
if ((buckets = LD_BUCKETS(smp->som_dynamic)) == 0)
|
|
|
|
continue;
|
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1993-12-21 01:44:35 +03:00
|
|
|
continue;
|
|
|
|
|
1994-05-24 14:44:06 +04:00
|
|
|
restart:
|
1993-10-17 00:54:08 +03:00
|
|
|
/*
|
|
|
|
* Compute bucket in which the symbol might be found.
|
|
|
|
*/
|
1997-01-04 01:37:58 +03:00
|
|
|
for (hashval = 0, cp = (char *) name; *cp; cp++)
|
1993-10-17 00:54:08 +03:00
|
|
|
hashval = (hashval << 1) + *cp;
|
|
|
|
|
|
|
|
hashval = (hashval & 0x7fffffff) % buckets;
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
hashbase = LM_HASH(smp);
|
|
|
|
hp = hashbase + hashval;
|
1993-10-17 00:54:08 +03:00
|
|
|
if (hp->rh_symbolnum == -1)
|
|
|
|
/* Nothing in this bucket */
|
|
|
|
continue;
|
|
|
|
|
1998-05-19 16:15:38 +04:00
|
|
|
symbolbase = LM_PRIVATE(smp)->spd_symbolbase;
|
|
|
|
stringbase = LM_PRIVATE(smp)->spd_stringbase;
|
|
|
|
symsize = LM_PRIVATE(smp)->spd_symbolsize;
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
while (hp) {
|
1994-01-29 00:01:20 +03:00
|
|
|
np = (struct nzlist *)
|
|
|
|
(symbolbase + hp->rh_symbolnum * symsize);
|
|
|
|
cp = stringbase + np->nz_strx;
|
1993-10-17 00:54:08 +03:00
|
|
|
if (strcmp(cp, name) == 0)
|
|
|
|
break;
|
|
|
|
if (hp->rh_next == 0)
|
|
|
|
hp = NULL;
|
|
|
|
else
|
1994-01-29 00:01:20 +03:00
|
|
|
hp = hashbase + hp->rh_next;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
if (hp == NULL)
|
|
|
|
/* Nothing in this bucket */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have a symbol with the name we're looking for.
|
|
|
|
*/
|
1994-05-24 14:44:06 +04:00
|
|
|
if (np->nz_type == N_INDR+N_EXT) {
|
|
|
|
/*
|
|
|
|
* Next symbol gives the aliased name. Restart
|
|
|
|
* search with new name and confine to this map.
|
|
|
|
*/
|
|
|
|
name = stringbase + (++np)->nz_strx;
|
|
|
|
*src_map = smp;
|
|
|
|
goto restart;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
if (np->nz_value == 0)
|
|
|
|
/* It's not a definition */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
|
1996-12-22 00:53:16 +03:00
|
|
|
if (N_AUX(&np->nlist) == AUX_FUNC) {
|
|
|
|
/* It's a `weak' function definition */
|
1993-11-11 00:37:39 +03:00
|
|
|
if (strong)
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* It's a common, note value and continue search */
|
|
|
|
if (common_size < np->nz_value)
|
|
|
|
common_size = np->nz_value;
|
|
|
|
continue;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
1996-12-22 00:53:16 +03:00
|
|
|
if (N_BIND(&np->nlist) == BIND_WEAK && weak_np == 0) {
|
|
|
|
weak_np = np;
|
|
|
|
weak_smp = smp;
|
|
|
|
continue;
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
*src_map = smp;
|
1993-10-17 00:54:08 +03:00
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
1996-12-22 00:53:16 +03:00
|
|
|
if (weak_np) {
|
|
|
|
*src_map = weak_smp;
|
|
|
|
return weak_np;
|
|
|
|
}
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
if (common_size == 0)
|
|
|
|
/* Not found */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's a common, enter into run-time common symbol table.
|
|
|
|
*/
|
|
|
|
rtsp = enter_rts(name, (long)calloc(1, common_size),
|
1994-01-29 00:01:20 +03:00
|
|
|
N_UNDF + N_EXT, 0, common_size, NULL);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
xprintf("Allocating common: %s size %d at %#x\n", name, common_size, rtsp->rt_sp->nz_value);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rtsp->rt_sp;
|
|
|
|
}
|
|
|
|
|
1998-05-13 01:22:28 +04:00
|
|
|
/*
|
|
|
|
* Find the symbol in any mapped object that is closest to, but not
|
|
|
|
* exceeds, the given address.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
__dladdr(addr, dli)
|
|
|
|
void *addr;
|
|
|
|
Dl_info *dli;
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
struct rt_symbol *rtsp;
|
|
|
|
int i, found = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First look for an exact match in the commons table.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < RTC_TABSIZE; i++) {
|
|
|
|
for (rtsp = rt_symtab[i]; rtsp; rtsp = rtsp->rt_link)
|
|
|
|
if (rtsp->rt_sp->nz_value == (long)addr) {
|
|
|
|
dli->dli_fname = 0;
|
|
|
|
dli->dli_fbase = 0;
|
|
|
|
dli->dli_sname = rtsp->rt_sp->nz_name;
|
|
|
|
dli->dli_saddr = addr;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search all symbols tables for a ADDR
|
|
|
|
*/
|
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
|
|
|
struct so_map *src_map = smp;
|
|
|
|
int buckets;
|
|
|
|
struct rrs_hash *hp;
|
|
|
|
struct nzlist *np;
|
|
|
|
caddr_t cur = 0;
|
|
|
|
|
|
|
|
/* Some local caching */
|
|
|
|
long symbolbase;
|
|
|
|
struct rrs_hash *hashbase;
|
|
|
|
char *stringbase;
|
|
|
|
int symsize;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if ((LM_PRIVATE(smp)->spd_flags & _RTLD_GLOBAL) == 0)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((caddr_t)addr < smp->som_addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
np = lookup("_end", smp, &src_map, 1);
|
|
|
|
if ((caddr_t)addr > np->nz_value + smp->som_addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((buckets = LD_BUCKETS(smp->som_dynamic)) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the entire symbol table of this object
|
|
|
|
* in search for the nearest symbol.
|
|
|
|
*/
|
|
|
|
hashbase = LM_HASH(smp);
|
1998-05-19 16:15:38 +04:00
|
|
|
symbolbase = LM_PRIVATE(smp)->spd_symbolbase;
|
|
|
|
stringbase = LM_PRIVATE(smp)->spd_stringbase;
|
|
|
|
symsize = LM_PRIVATE(smp)->spd_symbolsize;
|
1998-05-13 01:22:28 +04:00
|
|
|
|
|
|
|
for (i = 0; i < buckets; i++) {
|
|
|
|
hp = hashbase + i;
|
|
|
|
if (hp->rh_symbolnum == -1)
|
|
|
|
/* Nothing in this bucket */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (hp) {
|
|
|
|
caddr_t v;
|
|
|
|
np = (struct nzlist *)
|
|
|
|
(symbolbase + hp->rh_symbolnum * symsize);
|
|
|
|
|
|
|
|
hp = (hp->rh_next == 0)
|
|
|
|
? NULL
|
|
|
|
: hashbase + hp->rh_next;
|
|
|
|
|
|
|
|
/* Skip any undefined symbol */
|
|
|
|
if (np->nz_type == N_UNDF+N_EXT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Compute absolute address */
|
|
|
|
v = np->nz_value + src_map->som_addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make this symbol the new candidate if it
|
|
|
|
* is closer to, but not exceeding, ADDR.
|
|
|
|
*/
|
|
|
|
if (v >= cur && v <= (caddr_t)addr) {
|
|
|
|
dli->dli_fname = smp->som_path;
|
|
|
|
dli->dli_fbase = LM_LDBASE(smp);
|
|
|
|
dli->dli_sname = stringbase +
|
|
|
|
np->nz_strx;
|
|
|
|
dli->dli_saddr = v;
|
|
|
|
if (v == addr)
|
|
|
|
return (1);
|
|
|
|
cur = v;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called from the jumptable to resolve
|
|
|
|
* procedure calls to shared objects.
|
|
|
|
*/
|
1995-09-24 02:46:24 +04:00
|
|
|
long
|
1993-10-17 00:54:08 +03:00
|
|
|
binder(jsp)
|
1994-01-29 00:01:20 +03:00
|
|
|
jmpslot_t *jsp;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
struct so_map *smp, *src_map = NULL;
|
1993-10-17 00:54:08 +03:00
|
|
|
long addr;
|
|
|
|
char *sym;
|
|
|
|
struct nzlist *np;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the PLT map that contains JSP.
|
|
|
|
*/
|
1994-01-29 00:01:20 +03:00
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
|
|
|
if (LM_PLT(smp) < jsp &&
|
1998-05-19 16:15:38 +04:00
|
|
|
jsp < LM_PLT(smp) + LD_PLTSZ(smp->som_dynamic)/sizeof(*jsp))
|
1993-10-17 00:54:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
if (smp == NULL)
|
1998-12-16 00:35:17 +03:00
|
|
|
errx(1, "Call to binder from unknown location: %p\n", jsp);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
index = jsp->reloc_index & JMPSLOT_RELOC_MASK;
|
|
|
|
|
|
|
|
/* Get the local symbol this jmpslot refers to */
|
1998-05-19 16:15:38 +04:00
|
|
|
sym = LM_PRIVATE(smp)->spd_stringbase +
|
|
|
|
LM_SYMBOL(smp,RELOC_SYMBOL(&LM_REL(smp)[index]))->nz_strx;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-03-27 02:33:12 +03:00
|
|
|
np = lookup(sym, smp, &src_map, 1);
|
1993-10-17 00:54:08 +03:00
|
|
|
if (np == NULL)
|
1998-12-16 00:35:17 +03:00
|
|
|
errx(1, "Undefined symbol \"%s\" called from %s:%s at %p",
|
1994-01-29 00:01:20 +03:00
|
|
|
sym, main_progname, smp->som_path, jsp);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
/* Fixup jmpslot so future calls transfer directly to target */
|
|
|
|
addr = np->nz_value;
|
|
|
|
if (src_map)
|
1994-01-29 00:01:20 +03:00
|
|
|
addr += (long)src_map->som_addr;
|
1993-10-17 00:54:08 +03:00
|
|
|
|
1998-10-19 07:09:31 +04:00
|
|
|
md_fix_jmpslot(jsp, (long)jsp, addr, 0);
|
1993-10-17 00:54:08 +03:00
|
|
|
|
|
|
|
#if DEBUG
|
1998-10-23 04:44:44 +04:00
|
|
|
xprintf(" BINDER: %s located at %d(%p) = %#x in %s\n", sym, index, jsp, addr, src_map->som_path);
|
1993-10-17 00:54:08 +03:00
|
|
|
#endif
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-10-26 23:21:42 +03:00
|
|
|
static int hfd;
|
1998-03-19 02:21:46 +03:00
|
|
|
static size_t hsize;
|
1993-10-23 00:18:58 +03:00
|
|
|
static struct hints_header *hheader;
|
|
|
|
static struct hints_bucket *hbuckets;
|
|
|
|
static char *hstrtab;
|
1996-01-14 03:35:17 +03:00
|
|
|
static char *hint_search_path = "";
|
1993-10-23 00:18:58 +03:00
|
|
|
|
|
|
|
#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static void
|
1993-10-23 00:18:58 +03:00
|
|
|
maphints()
|
|
|
|
{
|
|
|
|
caddr_t addr;
|
1998-03-16 00:24:27 +03:00
|
|
|
struct stat statbuf;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1998-03-19 02:21:46 +03:00
|
|
|
if ((hfd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1)
|
|
|
|
goto nohints;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1998-03-16 00:24:27 +03:00
|
|
|
if (fstat(hfd, &statbuf) != 0 ||
|
1998-03-19 02:21:46 +03:00
|
|
|
(hsize = (size_t)statbuf.st_size) < sizeof(struct hints_header))
|
|
|
|
goto nohints;
|
1998-03-16 00:24:27 +03:00
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
hsize = (hsize + PAGSIZ - 1) & -PAGSIZ;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1999-06-18 01:09:47 +04:00
|
|
|
addr = mmap(0, hsize, PROT_READ, MAP_FILE|MAP_PRIVATE, hfd, 0);
|
1998-03-19 02:21:46 +03:00
|
|
|
if (addr == (caddr_t)-1)
|
|
|
|
goto nohints;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
|
|
|
hheader = (struct hints_header *)addr;
|
1998-03-19 02:21:46 +03:00
|
|
|
if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize) {
|
1994-10-26 23:21:42 +03:00
|
|
|
munmap(addr, hsize);
|
1998-03-19 02:21:46 +03:00
|
|
|
goto nohints;
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
|
|
|
|
1996-01-09 03:02:28 +03:00
|
|
|
if (hheader->hh_version != LD_HINTS_VERSION_1 &&
|
|
|
|
hheader->hh_version != LD_HINTS_VERSION_2) {
|
1994-10-26 23:21:42 +03:00
|
|
|
munmap(addr, hsize);
|
1998-03-19 02:21:46 +03:00
|
|
|
goto nohints;
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
|
|
|
|
hstrtab = (char *)(addr + hheader->hh_strtab);
|
1996-01-14 03:35:17 +03:00
|
|
|
if (hheader->hh_version >= LD_HINTS_VERSION_2)
|
|
|
|
hint_search_path = hstrtab + hheader->hh_dirlist;
|
1998-03-19 02:21:46 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
nohints:
|
|
|
|
if (hfd >= 0)
|
|
|
|
close(hfd);
|
|
|
|
hheader = (struct hints_header *)-1;
|
|
|
|
return;
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static void
|
1994-10-26 23:21:42 +03:00
|
|
|
unmaphints()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (HINTS_VALID) {
|
|
|
|
munmap((caddr_t)hheader, hsize);
|
|
|
|
close(hfd);
|
|
|
|
hheader = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
int
|
1993-10-23 00:18:58 +03:00
|
|
|
hinthash(cp, vmajor, vminor)
|
1994-01-29 00:01:20 +03:00
|
|
|
char *cp;
|
|
|
|
int vmajor, vminor;
|
1993-10-23 00:18:58 +03:00
|
|
|
{
|
|
|
|
int k = 0;
|
|
|
|
|
|
|
|
while (*cp)
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
|
|
|
|
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
|
1996-01-09 03:02:28 +03:00
|
|
|
if (hheader->hh_version == LD_HINTS_VERSION_1)
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
1993-10-17 00:54:08 +03:00
|
|
|
#undef major
|
|
|
|
#undef minor
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static char *
|
1995-06-21 03:07:03 +04:00
|
|
|
findhint(name, major, minor, prefered_path)
|
1994-01-29 00:01:20 +03:00
|
|
|
char *name;
|
|
|
|
int major, minor;
|
1995-06-21 03:07:03 +04:00
|
|
|
char *prefered_path;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1993-10-23 00:18:58 +03:00
|
|
|
struct hints_bucket *bp;
|
|
|
|
|
|
|
|
bp = hbuckets + (hinthash(name, major, minor) % hheader->hh_nbucket);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Sanity check */
|
|
|
|
if (bp->hi_namex >= hheader->hh_strtab_sz) {
|
1995-03-06 23:51:24 +03:00
|
|
|
warnx("Bad name index: %#x\n", bp->hi_namex);
|
1993-10-23 00:18:58 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bp->hi_pathx >= hheader->hh_strtab_sz) {
|
1995-03-06 23:51:24 +03:00
|
|
|
warnx("Bad path index: %#x\n", bp->hi_pathx);
|
1993-10-23 00:18:58 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, hstrtab + bp->hi_namex) == 0) {
|
|
|
|
/* It's `name', check version numbers */
|
|
|
|
if (bp->hi_major == major &&
|
1996-01-14 03:35:17 +03:00
|
|
|
(bp->hi_ndewey < 2 || bp->hi_minor >= minor)) {
|
1995-06-21 03:07:03 +04:00
|
|
|
if (prefered_path == NULL ||
|
|
|
|
strncmp(prefered_path,
|
|
|
|
hstrtab + bp->hi_pathx,
|
|
|
|
strlen(prefered_path)) == 0) {
|
1993-10-23 00:18:58 +03:00
|
|
|
return hstrtab + bp->hi_pathx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bp->hi_next == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Move on to next in bucket */
|
|
|
|
bp = &hbuckets[bp->hi_next];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No hints available for name */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static char *
|
1995-06-05 01:56:22 +04:00
|
|
|
rtfindlib(name, major, minor, usehints, ipath)
|
1994-01-29 00:01:20 +03:00
|
|
|
char *name;
|
|
|
|
int major, minor;
|
|
|
|
int *usehints;
|
1995-06-05 01:56:22 +04:00
|
|
|
char *ipath;
|
1993-10-23 00:18:58 +03:00
|
|
|
{
|
1995-06-05 01:56:22 +04:00
|
|
|
register char *cp;
|
|
|
|
int realminor;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
|
|
|
if (hheader == NULL)
|
|
|
|
maphints();
|
|
|
|
|
1994-08-07 14:34:40 +04:00
|
|
|
if (!HINTS_VALID || !(*usehints))
|
|
|
|
goto lose;
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1995-06-05 01:56:22 +04:00
|
|
|
/* NOTE: `ipath' may reside in a piece of read-only memory */
|
1993-10-23 00:18:58 +03:00
|
|
|
|
1995-06-05 01:56:22 +04:00
|
|
|
if (ld_library_path || ipath) {
|
|
|
|
/* Prefer paths from some explicit LD_LIBRARY_PATH */
|
|
|
|
register char *lpath;
|
|
|
|
char *dp;
|
1995-06-21 03:07:03 +04:00
|
|
|
|
|
|
|
dp = lpath = concat(ld_library_path ? ld_library_path : "",
|
|
|
|
(ld_library_path && ipath) ? ":" : "",
|
|
|
|
ipath ? ipath : "");
|
|
|
|
|
1995-06-05 01:56:22 +04:00
|
|
|
while ((cp = strsep(&dp, ":")) != NULL) {
|
1994-08-07 14:34:40 +04:00
|
|
|
cp = findhint(name, major, minor, cp);
|
1995-06-21 03:07:03 +04:00
|
|
|
if (cp) {
|
|
|
|
free(lpath);
|
1994-08-07 14:34:40 +04:00
|
|
|
return cp;
|
1995-06-21 03:07:03 +04:00
|
|
|
}
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
1995-06-05 01:56:22 +04:00
|
|
|
free(lpath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Not found in hints; try directory search now, before
|
|
|
|
* we get a spurious hint match below (i.e. a match not
|
1995-06-21 03:07:03 +04:00
|
|
|
* on one of the paths we're supposed to search first.
|
1995-06-05 01:56:22 +04:00
|
|
|
*/
|
1994-08-07 14:34:40 +04:00
|
|
|
realminor = -1;
|
|
|
|
cp = (char *)findshlib(name, &major, &realminor, 0);
|
|
|
|
if (cp && realminor >= minor)
|
|
|
|
return cp;
|
1993-10-23 00:18:58 +03:00
|
|
|
}
|
|
|
|
|
1994-02-16 01:51:23 +03:00
|
|
|
/* No LD_LIBRARY_PATH or lib not found in there; check default */
|
1994-08-07 14:34:40 +04:00
|
|
|
cp = findhint(name, major, minor, NULL);
|
|
|
|
if (cp)
|
|
|
|
return cp;
|
1994-02-16 01:51:23 +03:00
|
|
|
|
1994-08-07 14:34:40 +04:00
|
|
|
lose:
|
1993-10-23 00:18:58 +03:00
|
|
|
/* No hints available for name */
|
|
|
|
*usehints = 0;
|
1994-08-07 14:34:40 +04:00
|
|
|
realminor = -1;
|
1996-01-14 03:35:17 +03:00
|
|
|
add_search_path(hint_search_path);
|
1994-08-07 14:34:40 +04:00
|
|
|
cp = (char *)findshlib(name, &major, &realminor, 0);
|
1996-01-14 03:35:17 +03:00
|
|
|
remove_search_path(hint_search_path);
|
1994-08-07 14:34:40 +04:00
|
|
|
if (cp) {
|
1995-06-05 03:21:35 +04:00
|
|
|
if (realminor < minor && !ld_suppress_warnings)
|
1994-08-07 14:34:40 +04:00
|
|
|
warnx("warning: lib%s.so.%d.%d: "
|
|
|
|
"minor version >= %d expected, using it anyway",
|
|
|
|
name, major, realminor, minor);
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
return NULL;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
void
|
|
|
|
preload(paths)
|
|
|
|
char *paths;
|
|
|
|
{
|
|
|
|
struct so_map *nsmp;
|
|
|
|
struct sod *sodp;
|
|
|
|
char *cp, *dp;
|
|
|
|
|
|
|
|
dp = paths = strdup(paths);
|
|
|
|
if (dp == NULL) {
|
|
|
|
errx(1, "preload: out of memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((cp = strsep(&dp, ":")) != NULL) {
|
|
|
|
if ((sodp = (struct sod *)malloc(sizeof(struct sod))) == NULL) {
|
|
|
|
errx(1, "preload: %s: out of memory", cp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sodp->sod_name = (long)strdup(cp);
|
|
|
|
sodp->sod_library = 0;
|
|
|
|
sodp->sod_major = sodp->sod_minor = 0;
|
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
if ((nsmp = map_object(sodp, 0)) == NULL) {
|
1995-10-09 02:43:28 +03:00
|
|
|
errx(1, "preload: %s: cannot map object", cp);
|
|
|
|
}
|
|
|
|
LM_PRIVATE(nsmp)->spd_refcount++;
|
1998-03-16 02:10:21 +03:00
|
|
|
LM_PRIVATE(nsmp)->spd_flags |= _RTLD_GLOBAL;
|
1995-10-09 02:43:28 +03:00
|
|
|
}
|
|
|
|
free(paths);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-12-16 00:35:17 +03:00
|
|
|
#if 0
|
1994-01-29 00:01:20 +03:00
|
|
|
static struct somap_private dlmap_private = {
|
|
|
|
0,
|
|
|
|
(struct so_map *)0,
|
|
|
|
0,
|
|
|
|
#ifdef SUN_COMPAT
|
|
|
|
0,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct so_map dlmap = {
|
|
|
|
(caddr_t)0,
|
|
|
|
"internal",
|
|
|
|
(struct so_map *)0,
|
|
|
|
(struct sod *)0,
|
|
|
|
(caddr_t)0,
|
|
|
|
(u_int)0,
|
|
|
|
(struct _dynamic *)0,
|
|
|
|
(caddr_t)&dlmap_private
|
|
|
|
};
|
1998-12-16 00:35:17 +03:00
|
|
|
#endif
|
1994-01-29 00:01:20 +03:00
|
|
|
static int dlerrno;
|
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
/*
|
1997-07-01 02:10:06 +04:00
|
|
|
* Populate sod struct for dlopen's call to map_object
|
1995-12-28 20:57:32 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
build_sod(name, sodp)
|
1997-05-03 12:00:02 +04:00
|
|
|
const char *name;
|
1995-12-28 20:57:32 +03:00
|
|
|
struct sod *sodp;
|
|
|
|
{
|
|
|
|
unsigned int tuplet;
|
|
|
|
int major, minor;
|
1998-12-16 00:35:17 +03:00
|
|
|
char *realname = NULL, *tok, *etok, *cp;
|
1995-12-28 20:57:32 +03:00
|
|
|
|
|
|
|
/* default is an absolute or relative path */
|
|
|
|
sodp->sod_name = (long)strdup(name); /* strtok is destructive */
|
|
|
|
sodp->sod_library = 0;
|
|
|
|
sodp->sod_major = sodp->sod_minor = 0;
|
|
|
|
|
|
|
|
/* asking for lookup? */
|
|
|
|
if (strncmp((char *)sodp->sod_name, "lib", 3) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* skip over 'lib' */
|
|
|
|
cp = (char *)sodp->sod_name + 3;
|
|
|
|
|
|
|
|
/* dot guardian */
|
|
|
|
if ((strchr(cp, '.') == NULL) || (*(cp+strlen(cp)-1) == '.'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* default */
|
|
|
|
major = minor = -1;
|
|
|
|
|
|
|
|
/* loop through name - parse skipping name */
|
|
|
|
for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) {
|
|
|
|
switch (tuplet) {
|
|
|
|
case 0:
|
|
|
|
/* removed 'lib' and extensions from name */
|
|
|
|
realname = tok;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* 'so' extension */
|
|
|
|
if (strcmp(tok, "so") != 0)
|
|
|
|
goto backout;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* major version extension */
|
|
|
|
major = strtol(tok, &etok, 10);
|
|
|
|
if (*tok == '\0' || *etok != '\0')
|
|
|
|
goto backout;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
/* minor version extension */
|
|
|
|
minor = strtol(tok, &etok, 10);
|
|
|
|
if (*tok == '\0' || *etok != '\0')
|
|
|
|
goto backout;
|
|
|
|
break;
|
|
|
|
/* if we get here, it must be weird */
|
|
|
|
default:
|
|
|
|
goto backout;
|
|
|
|
}
|
|
|
|
}
|
1998-12-16 00:35:17 +03:00
|
|
|
if (realname == NULL)
|
|
|
|
goto backout;
|
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
cp = (char *)sodp->sod_name;
|
|
|
|
sodp->sod_name = (long)strdup(realname);
|
|
|
|
free(cp);
|
|
|
|
sodp->sod_library = 1;
|
|
|
|
sodp->sod_major = major;
|
|
|
|
sodp->sod_minor = minor;
|
|
|
|
return;
|
|
|
|
|
|
|
|
backout:
|
|
|
|
free((char *)sodp->sod_name);
|
|
|
|
sodp->sod_name = (long)strdup(name);
|
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static void *
|
1994-05-24 14:44:06 +04:00
|
|
|
__dlopen(name, mode)
|
1997-01-04 01:37:58 +03:00
|
|
|
const char *name;
|
|
|
|
int mode;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1994-01-29 00:01:20 +03:00
|
|
|
struct sod *sodp;
|
|
|
|
struct so_map *smp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A NULL argument returns the current set of mapped objects.
|
|
|
|
*/
|
1995-10-09 02:43:28 +03:00
|
|
|
if (name == NULL) {
|
|
|
|
LM_PRIVATE(link_map_head)->spd_refcount++;
|
1997-05-03 12:00:02 +04:00
|
|
|
/* Return magic token */
|
|
|
|
return (void *)1;
|
1995-10-09 02:43:28 +03:00
|
|
|
}
|
1994-01-29 00:01:20 +03:00
|
|
|
|
|
|
|
if ((sodp = (struct sod *)malloc(sizeof(struct sod))) == NULL) {
|
|
|
|
dlerrno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
build_sod(name, sodp);
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1995-12-28 20:57:32 +03:00
|
|
|
if ((smp = map_object(sodp, 0)) == NULL) {
|
1994-01-29 00:01:20 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
xprintf("%s: %s\n", name, strerror(errno));
|
|
|
|
#endif
|
|
|
|
dlerrno = errno;
|
1997-07-01 02:10:06 +04:00
|
|
|
free((char *)sodp->sod_name);
|
|
|
|
free(sodp);
|
1994-01-29 00:01:20 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-07-15 02:18:36 +04:00
|
|
|
if ((mode & RTLD_LOCAL) == 0)
|
1998-03-16 02:10:21 +03:00
|
|
|
LM_PRIVATE(smp)->spd_flags |= _RTLD_GLOBAL;
|
1995-03-06 23:51:24 +03:00
|
|
|
|
1997-07-01 02:10:06 +04:00
|
|
|
if (LM_PRIVATE(smp)->spd_refcount++ > 0) {
|
|
|
|
free((char *)sodp->sod_name);
|
|
|
|
free(sodp);
|
1995-10-09 02:43:28 +03:00
|
|
|
return smp;
|
1997-07-01 02:10:06 +04:00
|
|
|
}
|
1995-03-06 23:51:24 +03:00
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
LM_PRIVATE(smp)->spd_flags |= _RTLD_DL;
|
1997-07-01 02:10:06 +04:00
|
|
|
|
|
|
|
if (load_subs(smp) != 0) {
|
|
|
|
if (--LM_PRIVATE(smp)->spd_refcount == 0) {
|
|
|
|
unmap_object(smp);
|
|
|
|
free_link_map(smp);
|
|
|
|
}
|
1995-10-09 02:43:28 +03:00
|
|
|
return NULL;
|
1997-07-01 02:10:06 +04:00
|
|
|
}
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1995-10-09 02:43:28 +03:00
|
|
|
init_maps(smp);
|
1994-01-29 00:01:20 +03:00
|
|
|
return smp;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static int
|
1994-05-24 14:44:06 +04:00
|
|
|
__dlclose(fd)
|
1994-01-29 00:01:20 +03:00
|
|
|
void *fd;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1997-05-03 12:00:02 +04:00
|
|
|
struct so_map *smp;
|
|
|
|
|
|
|
|
if (fd == (void *)1)
|
|
|
|
return 0;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1997-05-03 12:00:02 +04:00
|
|
|
smp = (struct so_map *)fd;
|
1994-01-29 00:01:20 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
xprintf("dlclose(%s): refcount = %d\n", smp->som_path, LM_PRIVATE(smp)->spd_refcount);
|
|
|
|
#endif
|
|
|
|
if (--LM_PRIVATE(smp)->spd_refcount != 0)
|
|
|
|
return 0;
|
|
|
|
|
1998-03-16 02:10:21 +03:00
|
|
|
if ((LM_PRIVATE(smp)->spd_flags & _RTLD_DL) == 0)
|
1997-05-03 12:00:02 +04:00
|
|
|
return 0;
|
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
/* Dismantle shared object map and descriptor */
|
1995-10-09 02:43:28 +03:00
|
|
|
call_map(smp, "__fini");
|
1994-01-29 00:01:20 +03:00
|
|
|
#if 0
|
1997-06-27 00:52:06 +04:00
|
|
|
unload_subs(smp); /* XXX should unload implied objects */
|
|
|
|
#endif
|
1994-01-29 00:01:20 +03:00
|
|
|
unmap_object(smp);
|
1997-07-01 02:10:06 +04:00
|
|
|
free_link_map(smp);
|
1994-01-29 00:01:20 +03:00
|
|
|
return 0;
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static void *
|
1994-05-24 14:44:06 +04:00
|
|
|
__dlsym(fd, sym)
|
1997-01-04 01:37:58 +03:00
|
|
|
void *fd;
|
|
|
|
const char *sym;
|
1993-10-17 00:54:08 +03:00
|
|
|
{
|
1997-05-03 12:00:02 +04:00
|
|
|
struct so_map *smp, *src_map = NULL;
|
1994-01-29 00:01:20 +03:00
|
|
|
struct nzlist *np;
|
|
|
|
long addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restrict search to passed map if dlopen()ed.
|
|
|
|
*/
|
1997-05-03 12:00:02 +04:00
|
|
|
if (fd == (void *)1)
|
|
|
|
smp = link_map_head;
|
|
|
|
else
|
|
|
|
src_map = smp = (struct so_map *)fd;
|
1994-01-29 00:01:20 +03:00
|
|
|
|
1998-03-27 02:33:12 +03:00
|
|
|
np = lookup(sym, smp, &src_map, 1);
|
1994-01-29 00:01:20 +03:00
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Fixup jmpslot so future calls transfer directly to target */
|
|
|
|
addr = np->nz_value;
|
|
|
|
if (src_map)
|
|
|
|
addr += (long)src_map->som_addr;
|
|
|
|
|
|
|
|
return (void *)addr;
|
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static int
|
1994-05-24 14:44:06 +04:00
|
|
|
__dlctl(fd, cmd, arg)
|
1998-05-13 01:22:28 +04:00
|
|
|
void *fd;
|
|
|
|
int cmd;
|
|
|
|
void *arg;
|
1994-01-29 00:01:20 +03:00
|
|
|
{
|
1998-05-13 01:22:28 +04:00
|
|
|
|
1994-01-29 00:01:20 +03:00
|
|
|
switch (cmd) {
|
|
|
|
case DL_GETERRNO:
|
|
|
|
*(int *)arg = dlerrno;
|
1998-05-30 22:28:31 +04:00
|
|
|
dlerrno = 0;
|
1998-05-13 01:22:28 +04:00
|
|
|
return (0);
|
1994-01-29 00:01:20 +03:00
|
|
|
default:
|
|
|
|
dlerrno = EOPNOTSUPP;
|
1998-05-13 01:22:28 +04:00
|
|
|
return (-1);
|
1994-01-29 00:01:20 +03:00
|
|
|
}
|
1998-05-13 01:22:28 +04:00
|
|
|
return (-1);
|
1993-10-17 00:54:08 +03:00
|
|
|
}
|
|
|
|
|
1995-09-24 02:46:24 +04:00
|
|
|
static void
|
|
|
|
__dlexit()
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
|
|
|
|
/* Call any object initialization routines. */
|
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
1998-03-16 02:10:21 +03:00
|
|
|
if (LM_PRIVATE(smp)->spd_flags & _RTLD_RTLD)
|
1995-09-24 02:46:24 +04:00
|
|
|
continue;
|
1995-10-09 02:43:28 +03:00
|
|
|
call_map(smp, ".fini");
|
1995-09-24 02:46:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-11-11 00:37:39 +03:00
|
|
|
void
|
|
|
|
#if __STDC__
|
|
|
|
xprintf(char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xprintf(fmt, va_alist)
|
|
|
|
char *fmt;
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
va_list ap;
|
|
|
|
#if __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
va_start(ap);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vsprintf(buf, fmt, ap);
|
|
|
|
(void)write(1, buf, strlen(buf));
|
|
|
|
va_end(ap);
|
|
|
|
}
|