1993-11-03 23:41:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 Paul Kranenburg
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* This product includes software developed by Paul Kranenburg.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
1994-02-13 20:43:13 +00:00
|
|
|
* derived from this software without specific prior written permission
|
1993-11-03 23:41:59 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
1994-02-13 20:43:13 +00:00
|
|
|
#include <sys/errno.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <sys/mman.h>
|
1995-03-04 17:49:20 +00:00
|
|
|
#ifndef MAP_COPY
|
1993-11-03 23:41:59 +00:00
|
|
|
#define MAP_COPY MAP_PRIVATE
|
|
|
|
#endif
|
1997-01-12 19:59:26 +00:00
|
|
|
#include <dlfcn.h>
|
1994-06-15 22:41:19 +00:00
|
|
|
#include <err.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <a.out.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
#include <paths.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <stab.h>
|
1994-06-15 22:41:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <string.h>
|
1994-06-15 22:41:19 +00:00
|
|
|
#include <unistd.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
#include <link.h>
|
|
|
|
|
|
|
|
#include "md.h"
|
|
|
|
#include "shlib.h"
|
|
|
|
#include "support.h"
|
|
|
|
#include "dynamic.h"
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1995-03-04 17:49:20 +00:00
|
|
|
#ifndef MAP_ANON
|
|
|
|
#define MAP_ANON 0
|
|
|
|
#define anon_open() do { \
|
2000-12-09 09:35:55 +00:00
|
|
|
if ((anon_fd = open(_PATH_DEVZERO, O_RDWR, 0)) == -1) \
|
|
|
|
err("open: %s", _PATH_DEVZERO); \
|
1995-03-04 17:49:20 +00:00
|
|
|
} while (0)
|
|
|
|
#define anon_close() do { \
|
|
|
|
(void)close(anon_fd); \
|
|
|
|
anon_fd = -1; \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define anon_open()
|
|
|
|
#define anon_close()
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Structure for building a list of shared objects.
|
|
|
|
*/
|
|
|
|
struct so_list {
|
|
|
|
struct so_map *sol_map; /* Link map for shared object */
|
|
|
|
struct so_list *sol_next; /* Next entry in the list */
|
|
|
|
};
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/*
|
1994-02-13 20:43:13 +00:00
|
|
|
* Loader private data, hung off <so_map>->som_spd
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1994-02-13 20:43:13 +00:00
|
|
|
struct somap_private {
|
|
|
|
int spd_version;
|
|
|
|
struct so_map *spd_parent;
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_list *spd_children;
|
|
|
|
struct so_map *spd_prev;
|
1996-05-22 06:34:12 +00:00
|
|
|
dev_t spd_dev;
|
|
|
|
ino_t spd_ino;
|
1994-02-13 20:43:13 +00:00
|
|
|
int spd_refcount;
|
|
|
|
int spd_flags;
|
1996-01-13 00:15:25 +00:00
|
|
|
#define RTLD_MAIN 0x01
|
|
|
|
#define RTLD_RTLD 0x02
|
|
|
|
#define RTLD_DL 0x04
|
|
|
|
#define RTLD_INIT 0x08
|
1995-02-07 13:33:42 +00:00
|
|
|
unsigned long a_text; /* text size, if known */
|
|
|
|
unsigned long a_data; /* initialized data size */
|
|
|
|
unsigned long a_bss; /* uninitialized data size */
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
#ifdef SUN_COMPAT
|
1994-02-13 20:43:13 +00:00
|
|
|
long spd_offset; /* Correction for Sun main programs */
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_PRIVATE(smp) ((struct somap_private *)(smp)->som_spd)
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
#ifdef SUN_COMPAT
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_OFFSET(smp) (LM_PRIVATE(smp)->spd_offset)
|
1993-11-03 23:41:59 +00:00
|
|
|
#else
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_OFFSET(smp) (0)
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
/* Base address for section_dispatch_table entries */
|
|
|
|
#define LM_LDBASE(smp) (smp->som_addr + LM_OFFSET(smp))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Start of text segment */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_TXTADDR(smp) (smp->som_addr == (caddr_t)0 ? PAGSIZ : 0)
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Start of run-time relocation_info */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_REL(smp) ((struct relocation_info *) \
|
|
|
|
(smp->som_addr + LM_OFFSET(smp) + LD_REL((smp)->som_dynamic)))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Start of symbols */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_SYMBOL(smp, i) ((struct nzlist *) \
|
|
|
|
(smp->som_addr + LM_OFFSET(smp) + LD_SYMBOL((smp)->som_dynamic) + \
|
|
|
|
i * (LD_VERSION_NZLIST_P(smp->som_dynamic->d_version) ? \
|
|
|
|
sizeof(struct nzlist) : sizeof(struct nlist))))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Start of hash table */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_HASH(smp) ((struct rrs_hash *) \
|
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_HASH((smp)->som_dynamic)))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Start of strings */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_STRINGS(smp) ((char *) \
|
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_STRINGS((smp)->som_dynamic)))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
/* Start of search paths */
|
|
|
|
#define LM_PATHS(smp) ((char *) \
|
|
|
|
((smp)->som_addr + LM_OFFSET(smp) + LD_PATHS((smp)->som_dynamic)))
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/* End of text */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_ETEXT(smp) ((char *) \
|
|
|
|
((smp)->som_addr + LM_TXTADDR(smp) + LD_TEXTSZ((smp)->som_dynamic)))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1995-04-21 04:57:50 +00:00
|
|
|
/* Needed shared objects */
|
|
|
|
#define LM_NEED(smp) ((struct sod *) \
|
|
|
|
((smp)->som_addr + LM_TXTADDR(smp) + LD_NEED((smp)->som_dynamic)))
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/* PLT is in data segment, so don't use LM_OFFSET here */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_PLT(smp) ((jmpslot_t *) \
|
|
|
|
((smp)->som_addr + LD_PLT((smp)->som_dynamic)))
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Parent of link map */
|
1994-02-13 20:43:13 +00:00
|
|
|
#define LM_PARENT(smp) (LM_PRIVATE(smp)->spd_parent)
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-10-01 11:54:38 +00:00
|
|
|
#ifndef RELOC_EXTERN_P
|
|
|
|
#define RELOC_EXTERN_P(s) ((s)->r_extern)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RELOC_SYMBOL
|
|
|
|
#define RELOC_SYMBOL(s) ((s)->r_symbolnum)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RELOC_PCREL_P
|
|
|
|
#define RELOC_PCREL_P(s) ((s)->r_pcrel)
|
|
|
|
#endif
|
|
|
|
|
1998-02-06 16:46:46 +00:00
|
|
|
#define END_SYM "_end"
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
static char __main_progname[] = "main";
|
|
|
|
static char *main_progname = __main_progname;
|
|
|
|
static char us[] = "/usr/libexec/ld.so";
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
char **environ;
|
1994-06-15 22:41:19 +00:00
|
|
|
char *__progname;
|
1993-11-03 23:41:59 +00:00
|
|
|
int errno;
|
1994-06-15 22:41:19 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
static uid_t uid, euid;
|
|
|
|
static gid_t gid, egid;
|
|
|
|
static int careful;
|
1995-03-04 17:49:20 +00:00
|
|
|
static int anon_fd = -1;
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
static char *ld_bind_now;
|
1997-01-17 20:22:18 +00:00
|
|
|
static char *ld_ignore_missing_objects;
|
1995-11-02 18:48:15 +00:00
|
|
|
static char *ld_library_path;
|
1996-04-20 18:29:50 +00:00
|
|
|
static char *ld_preload;
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
static char *ld_tracing;
|
|
|
|
static char *ld_suppress_warnings;
|
|
|
|
static char *ld_warn_non_pure_code;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *link_map_head;
|
|
|
|
struct so_map *link_map_tail;
|
1993-11-03 23:41:59 +00:00
|
|
|
struct rt_symbol *rt_symbol_head;
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *__dlopen __P((const char *, int));
|
1994-06-15 22:41:19 +00:00
|
|
|
static int __dlclose __P((void *));
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *__dlsym __P((void *, const char *));
|
|
|
|
static const char *__dlerror __P((void));
|
1995-06-27 09:53:27 +00:00
|
|
|
static void __dlexit __P((void));
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *__dlsym3 __P((void *, const char *, void *));
|
1998-02-06 16:46:46 +00:00
|
|
|
static int __dladdr __P((const void *, Dl_info *));
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
static struct ld_entry ld_entry = {
|
1998-02-06 16:46:46 +00:00
|
|
|
__dlopen, __dlclose, __dlsym, __dlerror, __dlexit, __dlsym3, __dladdr
|
1993-11-03 23:41:59 +00:00
|
|
|
};
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
void xprintf __P((char *, ...));
|
1997-11-29 03:32:48 +00:00
|
|
|
static struct so_map *map_object __P(( const char *,
|
1996-01-13 00:15:25 +00:00
|
|
|
struct sod *,
|
|
|
|
struct so_map *));
|
1996-04-20 18:29:50 +00:00
|
|
|
static int map_preload __P((void));
|
1996-01-13 00:15:25 +00:00
|
|
|
static int map_sods __P((struct so_map *));
|
1997-11-29 03:32:48 +00:00
|
|
|
static int reloc_dag __P((struct so_map *, int));
|
1996-01-13 00:15:25 +00:00
|
|
|
static void unmap_object __P((struct so_map *, int));
|
1997-11-29 03:32:48 +00:00
|
|
|
static struct so_map *alloc_link_map __P(( const char *, struct sod *,
|
|
|
|
struct so_map *, caddr_t,
|
|
|
|
struct _dynamic *));
|
|
|
|
static void init_link_map __P(( struct so_map *,
|
|
|
|
struct somap_private *,
|
|
|
|
const char *, struct sod *,
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *, caddr_t,
|
|
|
|
struct _dynamic *));
|
1996-01-13 00:15:25 +00:00
|
|
|
static void free_link_map __P((struct so_map *));
|
1995-02-07 13:33:42 +00:00
|
|
|
static inline int check_text_reloc __P(( struct relocation_info *,
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *,
|
1993-11-03 23:41:59 +00:00
|
|
|
caddr_t));
|
1997-01-12 00:16:36 +00:00
|
|
|
static int reloc_map __P((struct so_map *, int));
|
1994-02-13 20:43:13 +00:00
|
|
|
static void reloc_copy __P((struct so_map *));
|
1997-11-29 03:32:48 +00:00
|
|
|
static void init_dag __P((struct so_map *));
|
1996-01-13 00:15:25 +00:00
|
|
|
static void init_sods __P((struct so_list *));
|
1997-12-05 02:06:37 +00:00
|
|
|
static void init_internal_malloc __P((void));
|
1997-11-29 03:32:48 +00:00
|
|
|
static void init_external_malloc __P((void));
|
1996-01-13 00:15:25 +00:00
|
|
|
static int call_map __P((struct so_map *, char *));
|
|
|
|
static char *findhint __P((char *, int, int *));
|
1998-11-07 01:59:39 +00:00
|
|
|
static char *rtfindlib __P((char *, int, int, int));
|
1998-06-07 03:53:08 +00:00
|
|
|
static char *rtfindfile __P((const char *));
|
1993-11-03 23:41:59 +00:00
|
|
|
void binder_entry __P((void));
|
|
|
|
long binder __P((jmpslot_t *));
|
1994-02-13 20:43:13 +00:00
|
|
|
static struct nzlist *lookup __P((char *, struct so_map **, int));
|
1997-11-29 03:32:48 +00:00
|
|
|
static inline struct rt_symbol *lookup_rts __P((char *, unsigned long));
|
|
|
|
static struct nzlist *lookup_in_obj __P((char *, unsigned long,
|
|
|
|
struct so_map *, int));
|
|
|
|
static struct rt_symbol *enter_rts __P((char *, unsigned long, long, int,
|
|
|
|
caddr_t, long, struct so_map *));
|
|
|
|
static void *sym_addr __P((char *));
|
1998-06-21 14:22:29 +00:00
|
|
|
static struct nzlist * lookup_errno_hack(char *, struct so_map **, int);
|
1996-10-24 16:24:19 +00:00
|
|
|
static void die __P((void));
|
1995-02-07 13:33:42 +00:00
|
|
|
static void generror __P((char *, ...));
|
1996-01-13 00:15:25 +00:00
|
|
|
static int maphints __P((void));
|
1995-03-04 17:49:20 +00:00
|
|
|
static void unmaphints __P((void));
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
static void ld_trace __P((struct so_map *));
|
|
|
|
static void rt_readenv __P((void));
|
|
|
|
static int hinthash __P((char *, int));
|
|
|
|
int rtld __P((int, struct crt_ldso *, struct _dynamic *));
|
1995-03-04 17:49:20 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Compute a hash value for symbol tables. Don't change this -- the
|
|
|
|
* algorithm is dictated by the way the linker builds the symbol
|
|
|
|
* tables in the shared objects.
|
|
|
|
*/
|
|
|
|
static inline unsigned long
|
|
|
|
sym_hash(s)
|
|
|
|
const char *s;
|
|
|
|
{
|
|
|
|
unsigned long h;
|
|
|
|
|
|
|
|
h = 0;
|
|
|
|
while (*s != '\0')
|
|
|
|
h = (h << 1) + *s++;
|
|
|
|
return h & 0x7fffffffUL;
|
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00: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-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
#include "md-static-funcs.c"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from assembler stub that has set up crtp (passed from crt0)
|
|
|
|
* and dp (our __DYNAMIC).
|
|
|
|
*/
|
1994-02-13 20:43:13 +00:00
|
|
|
int
|
1993-11-03 23:41:59 +00:00
|
|
|
rtld(version, crtp, dp)
|
|
|
|
int version;
|
|
|
|
struct crt_ldso *crtp;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct _dynamic *dp;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
|
|
|
struct relocation_info *reloc;
|
1995-11-02 18:48:15 +00:00
|
|
|
struct relocation_info *reloc_limit; /* End+1 of relocation */
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_debug *ddp;
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *main_map;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
char *add_paths;
|
1998-02-06 16:46:46 +00:00
|
|
|
char *main_path;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Check version */
|
1995-02-07 13:33:42 +00:00
|
|
|
if (version != CRT_VERSION_BSD_2 &&
|
|
|
|
version != CRT_VERSION_BSD_3 &&
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
version != CRT_VERSION_BSD_4 &&
|
1998-02-06 16:46:46 +00:00
|
|
|
version != CRT_VERSION_BSD_5 &&
|
1995-02-07 13:33:42 +00:00
|
|
|
version != CRT_VERSION_SUN)
|
1994-02-13 20:43:13 +00:00
|
|
|
return -1;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Fixup __DYNAMIC structure */
|
1994-02-13 20:43:13 +00:00
|
|
|
(long)dp->d_un.d_sdt += crtp->crt_ba;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Relocate ourselves */
|
1995-11-02 18:48:15 +00:00
|
|
|
reloc = (struct relocation_info *) (LD_REL(dp) + crtp->crt_ba);
|
|
|
|
reloc_limit =
|
|
|
|
(struct relocation_info *) ((char *) reloc + LD_RELSZ(dp));
|
|
|
|
while(reloc < reloc_limit) {
|
|
|
|
/*
|
|
|
|
* Objects linked with "-Bsymbolic" (in particular, ld.so
|
|
|
|
* itself) can end up having unused relocation entries at
|
|
|
|
* the end. These can be detected by the fact that they
|
|
|
|
* have an address of 0.
|
|
|
|
*/
|
|
|
|
if(reloc->r_address == 0) /* We're done */
|
|
|
|
break;
|
|
|
|
md_relocate_simple(reloc, crtp->crt_ba,
|
|
|
|
reloc->r_address + crtp->crt_ba);
|
|
|
|
++reloc;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
if (version >= CRT_VERSION_BSD_4)
|
|
|
|
__progname = crtp->crt_ldso;
|
1994-02-13 20:43:13 +00:00
|
|
|
if (version >= CRT_VERSION_BSD_3)
|
|
|
|
main_progname = crtp->crt_prog;
|
1998-02-06 16:46:46 +00:00
|
|
|
main_path = version >= CRT_VERSION_BSD_5 ? crtp->crt_argv[0] :
|
|
|
|
main_progname;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
/* Some buggy versions of crt0.o have crt_ldso filled in as NULL. */
|
|
|
|
if (__progname == NULL)
|
|
|
|
__progname = us;
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
/* Fill in some fields in _DYNAMIC or crt structure */
|
|
|
|
if (version >= CRT_VERSION_BSD_4)
|
|
|
|
crtp->crt_ldentry = &ld_entry; /* crt */
|
|
|
|
else
|
|
|
|
crtp->crt_dp->d_entry = &ld_entry; /* _DYNAMIC */
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/* Initialize our internal malloc package. */
|
1997-12-05 02:06:37 +00:00
|
|
|
init_internal_malloc();
|
1997-11-29 03:32:48 +00:00
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/* Setup out (private) environ variable */
|
|
|
|
environ = crtp->crt_ep;
|
|
|
|
|
|
|
|
/* Get user and group identifiers */
|
|
|
|
uid = getuid(); euid = geteuid();
|
|
|
|
gid = getgid(); egid = getegid();
|
|
|
|
|
|
|
|
careful = (uid != euid) || (gid != egid);
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
rt_readenv();
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1995-03-04 17:49:20 +00:00
|
|
|
anon_open();
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
/* Make a link map entry for the main program */
|
1998-02-06 16:46:46 +00:00
|
|
|
main_map = alloc_link_map(main_path,
|
1996-01-13 00:15:25 +00:00
|
|
|
(struct sod *) NULL, (struct so_map *) NULL,
|
|
|
|
(caddr_t) 0, crtp->crt_dp);
|
|
|
|
LM_PRIVATE(main_map)->spd_refcount++;
|
|
|
|
LM_PRIVATE(main_map)->spd_flags |= RTLD_MAIN;
|
|
|
|
|
|
|
|
/* Make a link map entry for ourselves */
|
|
|
|
smp = alloc_link_map(us,
|
|
|
|
(struct sod *) NULL, (struct so_map *) NULL,
|
|
|
|
(caddr_t) crtp->crt_ba, dp);
|
|
|
|
LM_PRIVATE(smp)->spd_refcount++;
|
|
|
|
LM_PRIVATE(smp)->spd_flags |= RTLD_RTLD;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
/*
|
|
|
|
* Setup the executable's run path
|
|
|
|
*/
|
|
|
|
if (version >= CRT_VERSION_BSD_4) {
|
|
|
|
add_paths = LM_PATHS(main_map);
|
|
|
|
if (add_paths)
|
|
|
|
add_search_path(add_paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup the directory search list for findshlib. We use only
|
|
|
|
* the standard search path. Any extra directories from
|
|
|
|
* LD_LIBRARY_PATH are searched explicitly, in rtfindlib.
|
|
|
|
*/
|
|
|
|
std_search_path();
|
1995-09-27 23:14:08 +00:00
|
|
|
|
1996-04-20 18:29:50 +00:00
|
|
|
/* Map in LD_PRELOADs before the main program's shared objects so we
|
|
|
|
can intercept those calls */
|
1997-01-12 00:16:36 +00:00
|
|
|
if (ld_preload != NULL) {
|
1996-04-20 18:29:50 +00:00
|
|
|
if(map_preload() == -1) /* Failed */
|
1996-10-24 16:24:19 +00:00
|
|
|
die();
|
1996-04-20 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Map all the shared objects that the main program depends upon */
|
|
|
|
if(map_sods(main_map) == -1)
|
1996-10-24 16:24:19 +00:00
|
|
|
die();
|
1994-02-13 20:43:13 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
if(ld_tracing) { /* We're done */
|
|
|
|
ld_trace(link_map_head);
|
1996-01-13 00:15:25 +00:00
|
|
|
exit(0);
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crtp->crt_dp->d_un.d_sdt->sdt_loaded = link_map_head->som_next;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/* Relocate all mapped objects. */
|
|
|
|
if(reloc_dag(main_map, ld_bind_now != NULL) == -1) /* Failed */
|
1996-10-24 16:24:19 +00:00
|
|
|
die();
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Switch to the same malloc that the program uses. We do
|
|
|
|
* this before initializing the loaded objects, because their
|
|
|
|
* initialization functions may well call malloc, and it won't
|
|
|
|
* work right until we have set it up.
|
|
|
|
*/
|
|
|
|
init_external_malloc();
|
|
|
|
|
|
|
|
/* Initialize all mapped objects. */
|
|
|
|
init_dag(main_map);
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
ddp = crtp->crt_dp->d_debug;
|
|
|
|
ddp->dd_cc = rt_symbol_head;
|
|
|
|
if (ddp->dd_in_debugger) {
|
1993-11-22 19:05:31 +00: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-15 22:41:19 +00:00
|
|
|
err(1, "Cannot set breakpoint (%s)", main_progname);
|
1993-11-22 19:05:31 +00:00
|
|
|
}
|
1994-06-15 22:41:19 +00:00
|
|
|
md_set_breakpoint((long)crtp->crt_bp, (long *)&ddp->dd_bpt_shadow);
|
1993-11-22 19:05:31 +00:00
|
|
|
if (mprotect(addr, PAGSIZ, PROT_READ|PROT_EXEC) == -1) {
|
1994-06-15 22:41:19 +00:00
|
|
|
err(1, "Cannot re-protect breakpoint (%s)",
|
|
|
|
main_progname);
|
1993-11-22 19:05:31 +00:00
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
ddp->dd_bpt_addr = crtp->crt_bp;
|
1993-11-22 19:05:31 +00:00
|
|
|
if (link_map_head)
|
1994-02-13 20:43:13 +00:00
|
|
|
ddp->dd_sym_loaded = 1;
|
1993-11-22 19:05:31 +00:00
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1995-03-04 17:49:20 +00:00
|
|
|
/* Close the hints file */
|
1994-09-15 20:48:55 +00:00
|
|
|
unmaphints();
|
|
|
|
|
1993-11-30 20:47:54 +00:00
|
|
|
/* Close our file descriptor */
|
|
|
|
(void)close(crtp->crt_ldfd);
|
1995-03-04 17:49:20 +00:00
|
|
|
anon_close();
|
1995-10-25 16:16:35 +00:00
|
|
|
|
1998-02-06 16:46:46 +00:00
|
|
|
return LDSO_VERSION_HAS_DLADDR;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
void
|
|
|
|
ld_trace(smp)
|
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
char *fmt1, *fmt2, *fmt, *main_local;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
struct sod *sodp;
|
|
|
|
char *name, *path;
|
|
|
|
|
|
|
|
if ((sodp = smp->som_sod) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
name = (char *)sodp->sod_name;
|
|
|
|
if (LM_PARENT(smp))
|
|
|
|
name += (long)LM_LDBASE(LM_PARENT(smp));
|
|
|
|
|
|
|
|
if ((path = smp->som_path) == NULL)
|
|
|
|
path = "not found";
|
|
|
|
|
|
|
|
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-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/*
|
1996-01-13 00:15:25 +00:00
|
|
|
* Allocate a new link map and return a pointer to it.
|
|
|
|
*
|
|
|
|
* PATH is the pathname of the shared object.
|
|
|
|
*
|
|
|
|
* SODP is a pointer to the shared object dependency structure responsible
|
|
|
|
* for causing the new object to be loaded. PARENT is the shared object
|
|
|
|
* into which SODP points. Both can be NULL if the new object is not
|
|
|
|
* being loaded as a result of a shared object dependency.
|
|
|
|
*
|
|
|
|
* ADDR is the address at which the object has been mapped. DP is a pointer
|
|
|
|
* to its _dynamic structure.
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static struct so_map *
|
1994-02-13 20:43:13 +00:00
|
|
|
alloc_link_map(path, sodp, parent, addr, dp)
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *path;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct sod *sodp;
|
|
|
|
struct so_map *parent;
|
|
|
|
caddr_t addr;
|
|
|
|
struct _dynamic *dp;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
|
|
|
struct somap_private *smpp;
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
#ifdef DEBUG /* { */
|
|
|
|
xprintf("alloc_link_map: \"%s\" at %p\n", path, addr);
|
|
|
|
#endif /* } */
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
smp = (struct so_map *)xmalloc(sizeof(struct so_map));
|
|
|
|
smpp = (struct somap_private *)xmalloc(sizeof(struct somap_private));
|
|
|
|
init_link_map(smp, smpp, path, sodp, parent, addr, dp);
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
/* Link the new entry into the list of link maps */
|
|
|
|
smpp->spd_prev = link_map_tail;
|
|
|
|
if(link_map_tail == NULL) /* First link map entered into list */
|
|
|
|
link_map_head = link_map_tail = smp;
|
|
|
|
else { /* Append to end of list */
|
|
|
|
link_map_tail->som_next = smp;
|
|
|
|
link_map_tail = smp;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
return smp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a link map entry that has already been allocated.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_link_map(smp, smpp, path, sodp, parent, addr, dp)
|
|
|
|
struct so_map *smp;
|
|
|
|
struct somap_private *smpp;
|
|
|
|
const char *path;
|
|
|
|
struct sod *sodp;
|
|
|
|
struct so_map *parent;
|
|
|
|
caddr_t addr;
|
|
|
|
struct _dynamic *dp;
|
|
|
|
{
|
|
|
|
memset(smp, 0, sizeof *smp);
|
|
|
|
memset(smpp, 0, sizeof *smpp);
|
|
|
|
smp->som_spd = (caddr_t)smpp;
|
1994-02-13 20:43:13 +00:00
|
|
|
smp->som_addr = addr;
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
smp->som_path = path ? strdup(path) : NULL;
|
1994-02-13 20:43:13 +00:00
|
|
|
smp->som_sod = sodp;
|
|
|
|
smp->som_dynamic = dp;
|
|
|
|
smpp->spd_parent = parent;
|
1993-11-03 23:41:59 +00:00
|
|
|
#ifdef SUN_COMPAT
|
1994-02-13 20:43:13 +00:00
|
|
|
smpp->spd_offset =
|
|
|
|
(addr==0 && dp && dp->d_version==LD_VERSION_SUN) ? PAGSIZ : 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Remove the specified link map entry from the list of link maps, and free
|
|
|
|
* the associated storage.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1996-01-13 00:15:25 +00:00
|
|
|
free_link_map(smp)
|
1995-06-27 09:53:27 +00:00
|
|
|
struct so_map *smp;
|
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct somap_private *smpp = LM_PRIVATE(smp);
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
#ifdef DEBUG /* { */
|
|
|
|
xprintf("free_link_map: \"%s\"\n", smp->som_path);
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
if(smpp->spd_prev == NULL) /* Removing first entry in list */
|
|
|
|
link_map_head = smp->som_next;
|
|
|
|
else /* Update link of previous entry */
|
|
|
|
smpp->spd_prev->som_next = smp->som_next;
|
|
|
|
|
|
|
|
if(smp->som_next == NULL) /* Removing last entry in list */
|
|
|
|
link_map_tail = smpp->spd_prev;
|
|
|
|
else /* Update back link of next entry */
|
|
|
|
LM_PRIVATE(smp->som_next)->spd_prev = smpp->spd_prev;
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
if (smp->som_path != NULL)
|
|
|
|
free(smp->som_path);
|
|
|
|
free(smpp);
|
1996-01-13 00:15:25 +00:00
|
|
|
free(smp);
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/*
|
1996-01-13 00:15:25 +00:00
|
|
|
* Map the shared object specified by PATH into memory, if it is not
|
|
|
|
* already mapped. Increment the object's reference count, and return a
|
|
|
|
* pointer to its link map.
|
|
|
|
*
|
|
|
|
* As a special case, if PATH is NULL, it is taken to refer to the main
|
|
|
|
* program.
|
1995-09-27 23:17:33 +00:00
|
|
|
*
|
1996-01-13 00:15:25 +00:00
|
|
|
* SODP is a pointer to the shared object dependency structure that caused
|
|
|
|
* this object to be requested. PARENT is a pointer to the link map of
|
|
|
|
* the shared object containing that structure. For a shared object not
|
|
|
|
* being mapped as a result of a shared object dependency, these pointers
|
|
|
|
* should be NULL. An example of this is a shared object that is explicitly
|
|
|
|
* loaded via dlopen().
|
|
|
|
*
|
|
|
|
* The return value is a pointer to the link map for the requested object.
|
|
|
|
* If the operation failed, the return value is NULL. In that case, an
|
|
|
|
* error message can be retrieved by calling dlerror().
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static struct so_map *
|
1996-01-13 00:15:25 +00:00
|
|
|
map_object(path, sodp, parent)
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *path;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct sod *sodp;
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *parent;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *smp;
|
1996-05-22 06:34:12 +00:00
|
|
|
struct stat statbuf;
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
if(path == NULL) /* Special case for the main program itself */
|
|
|
|
smp = link_map_head;
|
|
|
|
else {
|
1996-05-22 06:34:12 +00:00
|
|
|
/*
|
|
|
|
* Check whether the shared object is already mapped.
|
|
|
|
* We check first for an exact match by pathname. That
|
|
|
|
* will detect the usual case. If no match is found by
|
|
|
|
* pathname, then stat the file, and check for a match by
|
|
|
|
* device and inode. That will detect the less common case
|
|
|
|
* involving multiple links to the same library.
|
|
|
|
*/
|
1996-01-13 00:15:25 +00:00
|
|
|
for(smp = link_map_head; smp != NULL; smp = smp->som_next) {
|
|
|
|
if(!(LM_PRIVATE(smp)->spd_flags & (RTLD_MAIN|RTLD_RTLD))
|
|
|
|
&& smp->som_path != NULL
|
|
|
|
&& strcmp(smp->som_path, path) == 0)
|
|
|
|
break;
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1996-05-22 06:34:12 +00:00
|
|
|
if(smp == NULL) { /* Check for a match by device and inode */
|
|
|
|
if (stat(path, &statbuf) == -1) {
|
|
|
|
generror ("cannot stat \"%s\" : %s",
|
|
|
|
path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (smp = link_map_head; smp != NULL;
|
|
|
|
smp = smp->som_next) {
|
|
|
|
struct somap_private *smpp = LM_PRIVATE(smp);
|
|
|
|
|
|
|
|
if (!(smpp->spd_flags & (RTLD_MAIN | RTLD_RTLD))
|
|
|
|
&& smpp->spd_ino == statbuf.st_ino
|
|
|
|
&& smpp->spd_dev == statbuf.st_dev)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smp == NULL) { /* We must map the object */
|
|
|
|
struct _dynamic *dp;
|
|
|
|
int fd;
|
|
|
|
caddr_t addr;
|
|
|
|
struct exec hdr;
|
|
|
|
struct somap_private *smpp;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY, 0)) == -1) {
|
|
|
|
generror ("open failed for \"%s\" : %s",
|
|
|
|
path, strerror (errno));
|
1994-02-13 20:43:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
|
|
|
|
generror ("header read failed for \"%s\"", path);
|
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if (N_BADMAG(hdr)) {
|
|
|
|
generror ("bad magic number in \"%s\"", path);
|
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Map the entire address space of the object. It is
|
|
|
|
* tempting to map just the text segment at first, in
|
|
|
|
* order to avoid having to use mprotect to change the
|
|
|
|
* protections of the data segment. But that would not
|
|
|
|
* be correct. Mmap might find a group of free pages
|
|
|
|
* large enough to hold the text segment, but not large
|
|
|
|
* enough for the entire object. When we then mapped
|
|
|
|
* in the data and BSS segments, they would either be
|
|
|
|
* non-contiguous with the text segment (if we didn't
|
|
|
|
* specify MAP_FIXED), or they would map over some
|
|
|
|
* previously mapped region (if we did use MAP_FIXED).
|
|
|
|
* The only way we can be sure of getting a contigous
|
|
|
|
* region that is large enough is to map the entire
|
|
|
|
* region at once.
|
|
|
|
*/
|
|
|
|
if ((addr = mmap(0, hdr.a_text + hdr.a_data + hdr.a_bss,
|
|
|
|
PROT_READ|PROT_EXEC,
|
|
|
|
MAP_COPY, fd, 0)) == (caddr_t)-1) {
|
|
|
|
generror ("mmap failed for \"%s\" : %s",
|
|
|
|
path, strerror (errno));
|
|
|
|
(void)close(fd);
|
|
|
|
return NULL;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
(void)close(fd);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Change the data segment to writable */
|
|
|
|
if (mprotect(addr + hdr.a_text, hdr.a_data,
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
|
|
|
|
generror ("mprotect failed for \"%s\" : %s",
|
|
|
|
path, strerror (errno));
|
|
|
|
(void)munmap(addr, hdr.a_text + hdr.a_data + hdr.a_bss);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map in pages of zeros for the BSS segment */
|
|
|
|
if (mmap(addr + hdr.a_text + hdr.a_data, hdr.a_bss,
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC,
|
|
|
|
MAP_ANON|MAP_COPY|MAP_FIXED,
|
|
|
|
anon_fd, 0) == (caddr_t)-1) {
|
|
|
|
generror ("mmap failed for \"%s\" : %s",
|
|
|
|
path, strerror (errno));
|
|
|
|
(void)munmap(addr, hdr.a_text + hdr.a_data + hdr.a_bss);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assume _DYNAMIC is the first data item */
|
|
|
|
dp = (struct _dynamic *)(addr+hdr.a_text);
|
|
|
|
|
|
|
|
/* Fixup __DYNAMIC structure */
|
|
|
|
(long)dp->d_un.d_sdt += (long)addr;
|
|
|
|
|
|
|
|
smp = alloc_link_map(path, sodp, parent, addr, dp);
|
|
|
|
|
|
|
|
/* save segment sizes for unmap. */
|
|
|
|
smpp = LM_PRIVATE(smp);
|
|
|
|
smpp->a_text = hdr.a_text;
|
|
|
|
smpp->a_data = hdr.a_data;
|
|
|
|
smpp->a_bss = hdr.a_bss;
|
1996-05-22 06:34:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the device and inode, so we can detect multiple links
|
|
|
|
* to the same library. Note, if we reach this point, then
|
|
|
|
* statbuf is guaranteed to have been filled in.
|
|
|
|
*/
|
|
|
|
smpp->spd_dev = statbuf.st_dev;
|
|
|
|
smpp->spd_ino = statbuf.st_ino;
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
LM_PRIVATE(smp)->spd_refcount++;
|
|
|
|
if(LM_PRIVATE(smp)->spd_refcount == 1) { /* First use of object */
|
|
|
|
/*
|
|
|
|
* Recursively map all of the shared objects that this
|
|
|
|
* one depends upon.
|
|
|
|
*/
|
|
|
|
if(map_sods(smp) == -1) { /* Failed */
|
|
|
|
unmap_object(smp, 0); /* Clean up */
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
return smp;
|
|
|
|
}
|
|
|
|
|
1996-04-20 18:29:50 +00:00
|
|
|
/*
|
|
|
|
* Map all the shared libraries named in the LD_PRELOAD environment
|
|
|
|
* variable.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure. On failure, an error message can
|
|
|
|
* be gotten via dlerror().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
map_preload __P((void)) {
|
|
|
|
char *ld_name = ld_preload;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
while ((name = strsep(&ld_name, ":")) != NULL) {
|
|
|
|
char *path = NULL;
|
|
|
|
struct so_map *smp = NULL;
|
|
|
|
|
|
|
|
if (*name != '\0') {
|
|
|
|
path = (strchr(name, '/') != NULL) ? strdup(name) :
|
|
|
|
rtfindfile(name);
|
|
|
|
}
|
|
|
|
if (path == NULL) {
|
|
|
|
generror("Can't find LD_PRELOAD shared"
|
|
|
|
" library \"%s\"", name);
|
|
|
|
} else {
|
|
|
|
smp = map_object(path, (struct sod *) NULL,
|
|
|
|
(struct so_map *) NULL);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
if (ld_name != NULL)
|
|
|
|
*(ld_name - 1) = ':';
|
|
|
|
if (smp == NULL) {
|
|
|
|
/*
|
|
|
|
* We don't bother to unmap already-loaded libraries
|
|
|
|
* on failure, because in that case the program is
|
|
|
|
* about to die anyway.
|
|
|
|
*/
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Map all of the shared objects that a given object depends upon. PARENT is
|
|
|
|
* a pointer to the link map for the shared object whose dependencies are
|
|
|
|
* to be mapped.
|
|
|
|
*
|
|
|
|
* Returns 0 on success. Returns -1 on failure. In that case, an error
|
|
|
|
* message can be retrieved by calling dlerror().
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static int
|
1996-01-13 00:15:25 +00:00
|
|
|
map_sods(parent)
|
|
|
|
struct so_map *parent;
|
|
|
|
{
|
|
|
|
struct somap_private *parpp = LM_PRIVATE(parent);
|
|
|
|
struct so_list **soltail = &parpp->spd_children;
|
|
|
|
long next = LD_NEED(parent->som_dynamic);
|
|
|
|
|
|
|
|
while(next != 0) {
|
|
|
|
struct sod *sodp =
|
|
|
|
(struct sod *) (LM_LDBASE(parent) + next);
|
|
|
|
char *name =
|
|
|
|
(char *) (LM_LDBASE(parent) + sodp->sod_name);
|
|
|
|
char *path = NULL;
|
|
|
|
struct so_map *smp = NULL;
|
|
|
|
|
|
|
|
if(sodp->sod_library) {
|
1998-11-07 01:59:39 +00:00
|
|
|
/*
|
|
|
|
* First try for a match with an adequate minor
|
|
|
|
* number.
|
|
|
|
*/
|
1996-01-13 00:15:25 +00:00
|
|
|
path = rtfindlib(name, sodp->sod_major,
|
1998-11-07 01:59:39 +00:00
|
|
|
sodp->sod_minor, 1);
|
|
|
|
/*
|
|
|
|
* If none was found, try for just a major version
|
|
|
|
* match. A warning is issued by rtfindlib in
|
|
|
|
* this case, since the minor version number isn't
|
|
|
|
* really high enough.
|
|
|
|
*/
|
|
|
|
if (path == NULL)
|
|
|
|
path = rtfindlib(name, sodp->sod_major,
|
|
|
|
sodp->sod_minor, 0);
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
if(path == NULL && !ld_tracing) {
|
1996-01-13 00:15:25 +00:00
|
|
|
generror ("Can't find shared library"
|
|
|
|
" \"lib%s.so.%d.%d\"", name,
|
|
|
|
sodp->sod_major, sodp->sod_minor);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(careful && name[0] != '/') {
|
|
|
|
generror("Shared library path must start"
|
|
|
|
" with \"/\" for \"%s\"", name);
|
|
|
|
} else
|
|
|
|
path = strdup(name);
|
|
|
|
}
|
|
|
|
|
1996-10-24 16:24:19 +00:00
|
|
|
if(path != NULL) {
|
1996-01-13 00:15:25 +00:00
|
|
|
smp = map_object(path, sodp, parent);
|
|
|
|
free(path);
|
1996-10-24 16:24:19 +00:00
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
if(smp != NULL) {
|
|
|
|
struct so_list *solp = (struct so_list *)
|
|
|
|
xmalloc(sizeof(struct so_list));
|
|
|
|
solp->sol_map = smp;
|
|
|
|
solp->sol_next = NULL;
|
|
|
|
*soltail = solp;
|
|
|
|
soltail = &solp->sol_next;
|
1996-10-24 16:24:19 +00:00
|
|
|
} else if(ld_tracing) {
|
|
|
|
/*
|
|
|
|
* Allocate a dummy map entry so that we will get the
|
|
|
|
* "not found" message.
|
|
|
|
*/
|
|
|
|
(void)alloc_link_map(NULL, sodp, parent, 0, 0);
|
1997-01-17 20:22:18 +00:00
|
|
|
} else if (ld_ignore_missing_objects) {
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *msg;
|
1997-01-17 20:22:18 +00:00
|
|
|
/*
|
|
|
|
* Call __dlerror() even it we're not going to use
|
|
|
|
* the message, in order to clear the saved message.
|
|
|
|
*/
|
|
|
|
msg = __dlerror(); /* Should never be NULL */
|
|
|
|
if (!ld_suppress_warnings)
|
|
|
|
warnx("warning: %s", msg);
|
1996-10-24 16:24:19 +00:00
|
|
|
} else /* Give up */
|
1996-01-13 00:15:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
next = sodp->sod_next;
|
1994-06-15 22:41:19 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if(next != 0) {
|
|
|
|
/*
|
|
|
|
* Oh drat, we have to clean up a mess.
|
|
|
|
*
|
|
|
|
* We failed to load a shared object that we depend upon.
|
|
|
|
* So now we have to unload any dependencies that we had
|
|
|
|
* already successfully loaded prior to the error.
|
|
|
|
*
|
|
|
|
* Cleaning up doesn't matter so much for the initial
|
|
|
|
* loading of the program, since any failure is going to
|
|
|
|
* terminate the program anyway. But it is very important
|
|
|
|
* to clean up properly when something is being loaded
|
|
|
|
* via dlopen().
|
|
|
|
*/
|
|
|
|
struct so_list *solp;
|
|
|
|
|
|
|
|
while((solp = parpp->spd_children) != NULL) {
|
|
|
|
unmap_object(solp->sol_map, 0);
|
|
|
|
parpp->spd_children = solp->sol_next;
|
|
|
|
free(solp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
1995-03-04 17:49:20 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
1997-11-29 03:32:48 +00:00
|
|
|
* Relocate the DAG of shared objects rooted at the given link map
|
|
|
|
* entry. Returns 0 on success, or -1 on failure. On failure, an
|
|
|
|
* error message can be retrieved via dlerror().
|
1996-01-13 00:15:25 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static int
|
|
|
|
reloc_dag(root, bind_now)
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *root;
|
1997-01-12 00:16:36 +00:00
|
|
|
int bind_now;
|
1996-01-13 00:15:25 +00:00
|
|
|
{
|
|
|
|
struct so_map *smp;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Relocate all newly-loaded objects. We avoid recursion for this
|
|
|
|
* step by taking advantage of a few facts. This function is called
|
|
|
|
* only when there are in fact some newly-loaded objects to process.
|
|
|
|
* Furthermore, all newly-loaded objects will have their link map
|
|
|
|
* entries at the end of the link map list. And, the root of the
|
|
|
|
* tree of objects just loaded will have been the first to be loaded
|
|
|
|
* and therefore the first new object in the link map list. Finally,
|
|
|
|
* we take advantage of the fact that we can relocate the newly-loaded
|
|
|
|
* objects in any order.
|
|
|
|
*
|
|
|
|
* All these facts conspire to let us simply loop over the tail
|
|
|
|
* portion of the link map list, relocating each object so
|
|
|
|
* encountered.
|
|
|
|
*/
|
|
|
|
for(smp = root; smp != NULL; smp = smp->som_next) {
|
|
|
|
if(!(LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)) {
|
1997-01-12 00:16:36 +00:00
|
|
|
if(reloc_map(smp, bind_now) < 0)
|
1996-01-13 00:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Copy any relocated initialized data. Again, we can just loop
|
|
|
|
* over the appropriate portion of the link map list.
|
|
|
|
*/
|
|
|
|
for(smp = root; smp != NULL; smp = smp->som_next) {
|
|
|
|
if(!(LM_PRIVATE(smp)->spd_flags & RTLD_RTLD))
|
|
|
|
reloc_copy(smp);
|
|
|
|
}
|
1995-05-30 05:05:38 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
return 0;
|
1995-02-07 13:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-01-13 00:15:25 +00:00
|
|
|
* Remove a reference to the shared object specified by SMP. If no
|
|
|
|
* references remain, unmap the object and, recursively, its descendents.
|
|
|
|
* This function also takes care of calling the finalization routines for
|
|
|
|
* objects that are removed.
|
|
|
|
*
|
|
|
|
* If KEEP is true, then the actual calls to munmap() are skipped,
|
|
|
|
* and the object is kept in memory. That is used only for finalization,
|
|
|
|
* from dlexit(), when the program is exiting. There are two reasons
|
|
|
|
* for it. First, the program is exiting and there is no point in
|
|
|
|
* spending the time to explicitly unmap its shared objects. Second,
|
|
|
|
* even after dlexit() has been called, there are still a couple of
|
|
|
|
* calls that are made to functions in libc. (This is really a bug
|
|
|
|
* in crt0.) So libc and the main program, at least, must remain
|
|
|
|
* mapped in that situation.
|
|
|
|
*
|
|
|
|
* Under no reasonable circumstances should this function fail. If
|
|
|
|
* anything goes wrong, we consider it an internal error, and report
|
|
|
|
* it with err().
|
1995-02-07 13:33:42 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1996-01-13 00:15:25 +00:00
|
|
|
unmap_object(smp, keep)
|
1995-02-07 13:33:42 +00:00
|
|
|
struct so_map *smp;
|
1996-01-13 00:15:25 +00:00
|
|
|
int keep;
|
1995-02-07 13:33:42 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct somap_private *smpp = LM_PRIVATE(smp);
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
smpp->spd_refcount--;
|
|
|
|
if(smpp->spd_refcount == 0) { /* Finished with this object */
|
|
|
|
struct so_list *solp;
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if(smpp->spd_flags & RTLD_INIT) { /* Was initialized */
|
|
|
|
/*
|
|
|
|
* Call the object's finalization routine. For
|
|
|
|
* backward compatibility, we first try to call
|
|
|
|
* ".fini". If that does not exist, we call
|
|
|
|
* "__fini".
|
|
|
|
*/
|
|
|
|
if(call_map(smp, ".fini") == -1)
|
|
|
|
call_map(smp, "__fini");
|
|
|
|
}
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Recursively unreference the object's descendents */
|
|
|
|
while((solp = smpp->spd_children) != NULL) {
|
|
|
|
unmap_object(solp->sol_map, keep);
|
|
|
|
smpp->spd_children = solp->sol_next;
|
|
|
|
free(solp);
|
|
|
|
}
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if(!keep) { /* Unmap the object from memory */
|
|
|
|
if(munmap(smp->som_addr,
|
|
|
|
smpp->a_text + smpp->a_data + smpp->a_bss) < 0)
|
|
|
|
err(1, "internal error 1: munmap failed");
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Unlink and free the object's link map entry */
|
|
|
|
free_link_map(smp);
|
|
|
|
}
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
static inline int
|
1994-02-13 20:43:13 +00:00
|
|
|
check_text_reloc(r, smp, addr)
|
|
|
|
struct relocation_info *r;
|
|
|
|
struct so_map *smp;
|
|
|
|
caddr_t addr;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
char *sym;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
if (addr >= LM_ETEXT(smp))
|
1995-02-07 13:33:42 +00:00
|
|
|
return 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
if (RELOC_EXTERN_P(r))
|
|
|
|
sym = LM_STRINGS(smp) +
|
|
|
|
LM_SYMBOL(smp, RELOC_SYMBOL(r))->nz_strx;
|
|
|
|
else
|
|
|
|
sym = "";
|
1993-11-03 23:41:59 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
if (!ld_suppress_warnings && ld_warn_non_pure_code)
|
1995-03-04 17:49:20 +00:00
|
|
|
warnx("warning: non pure code in %s at %x (%s)",
|
1994-02-13 20:43:13 +00:00
|
|
|
smp->som_path, r->r_address, sym);
|
1993-12-22 23:28:35 +00:00
|
|
|
|
1994-02-13 20:43:13 +00: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) {
|
1995-02-07 13:33:42 +00:00
|
|
|
generror ("mprotect failed for \"%s\" : %s",
|
|
|
|
smp->som_path, strerror (errno));
|
|
|
|
return -1;
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
smp->som_write = 1;
|
1995-02-07 13:33:42 +00:00
|
|
|
return 0;
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
static int
|
1997-01-12 00:16:36 +00:00
|
|
|
reloc_map(smp, bind_now)
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
1997-01-12 00:16:36 +00:00
|
|
|
int bind_now;
|
1994-02-13 20:43:13 +00:00
|
|
|
{
|
1995-11-02 18:48:15 +00:00
|
|
|
/*
|
|
|
|
* Caching structure for reducing the number of calls to
|
|
|
|
* lookup() during relocation.
|
|
|
|
*
|
|
|
|
* While relocating a given shared object, the dynamic linker
|
|
|
|
* maintains a caching vector that is directly indexed by
|
|
|
|
* the symbol number in the relocation entry. The first time
|
|
|
|
* a given symbol is looked up, the caching vector is
|
|
|
|
* filled in with a pointer to the symbol table entry, and
|
|
|
|
* a pointer to the so_map of the shared object in which the
|
|
|
|
* symbol was defined. On subsequent uses of the same symbol,
|
|
|
|
* that information is retrieved directly from the caching
|
|
|
|
* vector, without calling lookup() again.
|
|
|
|
*
|
|
|
|
* A symbol that is referenced in a relocation entry is
|
|
|
|
* typically referenced in many relocation entries, so this
|
|
|
|
* caching reduces the number of calls to lookup()
|
|
|
|
* dramatically. The overall improvement in the speed of
|
|
|
|
* dynamic linking is also dramatic -- as much as a factor
|
|
|
|
* of three for programs that use many shared libaries.
|
|
|
|
*/
|
|
|
|
struct cacheent {
|
|
|
|
struct nzlist *np; /* Pointer to symbol entry */
|
|
|
|
struct so_map *src_map; /* Shared object that defined symbol */
|
|
|
|
};
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
struct _dynamic *dp = smp->som_dynamic;
|
|
|
|
struct relocation_info *r = LM_REL(smp);
|
|
|
|
struct relocation_info *rend = r + LD_RELSZ(dp)/sizeof(*r);
|
|
|
|
long symbolbase = (long)LM_SYMBOL(smp, 0);
|
|
|
|
char *stringbase = LM_STRINGS(smp);
|
|
|
|
int symsize = LD_VERSION_NZLIST_P(dp->d_version) ?
|
|
|
|
sizeof(struct nzlist) :
|
|
|
|
sizeof(struct nlist);
|
1995-11-02 18:48:15 +00:00
|
|
|
long numsyms = LD_STABSZ(dp) / symsize;
|
|
|
|
size_t cachebytes = numsyms * sizeof(struct cacheent);
|
|
|
|
struct cacheent *symcache =
|
|
|
|
(struct cacheent *) alloca(cachebytes);
|
|
|
|
|
|
|
|
if(symcache == NULL) {
|
|
|
|
generror("Cannot allocate symbol caching vector for %s",
|
|
|
|
smp->som_path);
|
|
|
|
return -1;
|
1995-10-25 16:16:35 +00:00
|
|
|
}
|
1995-11-02 18:48:15 +00:00
|
|
|
bzero(symcache, cachebytes);
|
1995-10-25 16:16:35 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
if (LD_PLTSZ(dp))
|
|
|
|
md_fix_jmpslot(LM_PLT(smp),
|
|
|
|
(long)LM_PLT(smp), (long)binder_entry);
|
|
|
|
|
|
|
|
for (; r < rend; r++) {
|
|
|
|
char *sym;
|
1995-11-02 18:48:15 +00:00
|
|
|
caddr_t addr;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1995-11-02 18:48:15 +00:00
|
|
|
/*
|
|
|
|
* Objects linked with "-Bsymbolic" can end up having unused
|
|
|
|
* relocation entries at the end. These can be detected by
|
|
|
|
* the fact that they have an address of 0.
|
|
|
|
*/
|
|
|
|
if(r->r_address == 0) /* Finished relocating this object */
|
|
|
|
break;
|
|
|
|
|
|
|
|
addr = smp->som_addr + r->r_address;
|
1995-02-07 13:33:42 +00:00
|
|
|
if (check_text_reloc(r, smp, addr) < 0)
|
|
|
|
return -1;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
|
|
|
if (RELOC_EXTERN_P(r)) {
|
|
|
|
struct so_map *src_map = NULL;
|
|
|
|
struct nzlist *p, *np;
|
1995-10-25 16:16:35 +00:00
|
|
|
long relocation;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
if (RELOC_JMPTAB_P(r) && !bind_now)
|
1994-02-13 20:43:13 +00:00
|
|
|
continue;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00: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;
|
|
|
|
|
1995-11-02 18:48:15 +00:00
|
|
|
/*
|
|
|
|
* Look up the symbol, checking the caching
|
|
|
|
* vector first.
|
|
|
|
*/
|
|
|
|
np = symcache[RELOC_SYMBOL(r)].np;
|
|
|
|
if(np != NULL) /* Symbol already cached */
|
|
|
|
src_map = symcache[RELOC_SYMBOL(r)].src_map;
|
|
|
|
else { /* Symbol not cached yet */
|
1997-01-12 00:16:36 +00:00
|
|
|
np = lookup(sym, &src_map, RELOC_JMPTAB_P(r));
|
1995-11-02 18:48:15 +00:00
|
|
|
/*
|
|
|
|
* Record the needed information about
|
|
|
|
* the symbol in the caching vector,
|
|
|
|
* so that we won't have to call
|
|
|
|
* lookup the next time we encounter
|
|
|
|
* the symbol.
|
|
|
|
*/
|
|
|
|
symcache[RELOC_SYMBOL(r)].np = np;
|
|
|
|
symcache[RELOC_SYMBOL(r)].src_map = src_map;
|
|
|
|
}
|
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
if (np == NULL) {
|
|
|
|
generror ("Undefined symbol \"%s\" in %s:%s",
|
1994-02-13 20:43:13 +00:00
|
|
|
sym, main_progname, smp->som_path);
|
1995-02-07 13:33:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1995-11-02 18:48:15 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1997-01-12 00:16:36 +00:00
|
|
|
relocation = np->nz_value;
|
1994-02-13 20:43:13 +00:00
|
|
|
if (src_map)
|
|
|
|
relocation += (long)src_map->som_addr;
|
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
if (RELOC_JMPTAB_P(r)) {
|
|
|
|
md_bind_jmpslot(relocation, addr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
relocation += md_get_addend(r, addr);
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
if (RELOC_PCREL_P(r))
|
|
|
|
relocation -= (long)smp->som_addr;
|
|
|
|
|
|
|
|
if (RELOC_COPY_P(r) && src_map) {
|
1997-11-29 03:32:48 +00:00
|
|
|
(void)enter_rts(sym, sym_hash(sym),
|
1994-02-13 20:43:13 +00:00
|
|
|
(long)addr,
|
|
|
|
N_DATA + N_EXT,
|
|
|
|
src_map->som_addr + np->nz_value,
|
|
|
|
np->nz_size, src_map);
|
|
|
|
continue;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
md_relocate(r, relocation, addr, 0);
|
1994-02-13 20:43:13 +00:00
|
|
|
} else {
|
|
|
|
md_relocate(r,
|
1993-11-03 23:41:59 +00:00
|
|
|
#ifdef SUN_COMPAT
|
1994-02-13 20:43:13 +00:00
|
|
|
md_get_rt_segment_addend(r, addr)
|
1993-11-03 23:41:59 +00:00
|
|
|
#else
|
1994-02-13 20:43:13 +00:00
|
|
|
md_get_addend(r, addr)
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
1994-02-13 20:43:13 +00:00
|
|
|
+ (long)smp->som_addr, addr, 0);
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smp->som_write) {
|
|
|
|
if (mprotect(smp->som_addr + LM_TXTADDR(smp),
|
|
|
|
LD_TEXTSZ(smp->som_dynamic),
|
1993-11-22 19:05:31 +00:00
|
|
|
PROT_READ|PROT_EXEC) == -1) {
|
1995-02-07 13:33:42 +00:00
|
|
|
generror ("mprotect failed for \"%s\" : %s",
|
|
|
|
smp->som_path, strerror (errno));
|
|
|
|
return -1;
|
1993-11-22 19:05:31 +00:00
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
smp->som_write = 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
1995-02-07 13:33:42 +00:00
|
|
|
return 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1994-02-13 20:43:13 +00:00
|
|
|
reloc_copy(smp)
|
|
|
|
struct so_map *smp;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
|
|
|
struct rt_symbol *rtsp;
|
|
|
|
|
|
|
|
for (rtsp = rt_symbol_head; rtsp; rtsp = rtsp->rt_next)
|
1994-02-13 20:43:13 +00:00
|
|
|
if ((rtsp->rt_smp == NULL || rtsp->rt_smp == smp) &&
|
|
|
|
rtsp->rt_sp->nz_type == N_DATA + N_EXT) {
|
1993-11-03 23:41:59 +00:00
|
|
|
bcopy(rtsp->rt_srcaddr, (caddr_t)rtsp->rt_sp->nz_value,
|
|
|
|
rtsp->rt_sp->nz_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Initialize the DAG of shared objects rooted at the given object.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_dag(smp)
|
1995-06-27 09:53:27 +00:00
|
|
|
struct so_map *smp;
|
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct somap_private *smpp = LM_PRIVATE(smp);
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if(!(smpp->spd_flags & RTLD_INIT)) { /* Not initialized yet */
|
|
|
|
smpp->spd_flags |= RTLD_INIT;
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Make sure all the children are initialized */
|
|
|
|
if(smpp->spd_children != NULL)
|
|
|
|
init_sods(smpp->spd_children);
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if(call_map(smp, ".init") == -1)
|
|
|
|
call_map(smp, "__init");
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
}
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1996-01-13 00:15:25 +00:00
|
|
|
init_sods(solp)
|
|
|
|
struct so_list *solp;
|
|
|
|
{
|
|
|
|
/* Recursively initialize the rest of the list */
|
|
|
|
if(solp->sol_next != NULL)
|
|
|
|
init_sods(solp->sol_next);
|
|
|
|
|
|
|
|
/* Initialize the first element of the list */
|
1997-11-29 03:32:48 +00:00
|
|
|
init_dag(solp->sol_map);
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call a function in a given shared object. SMP is the shared object, and
|
|
|
|
* SYM is the name of the function.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or -1 if the symbol was not found. Failure is not
|
|
|
|
* necessarily an error condition, so no error message is generated.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static int
|
1995-06-27 09:53:27 +00:00
|
|
|
call_map(smp, sym)
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
|
|
|
char *sym;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *src_map = smp;
|
|
|
|
struct nzlist *np;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
np = lookup(sym, &src_map, 1);
|
1996-01-13 00:15:25 +00:00
|
|
|
if (np) {
|
1994-02-13 20:43:13 +00:00
|
|
|
(*(void (*)())(src_map->som_addr + np->nz_value))();
|
1996-01-13 00:15:25 +00:00
|
|
|
return 0;
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
/*
|
|
|
|
* Run-time common symbol table.
|
|
|
|
*/
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
#define RTC_TABSIZE 57
|
|
|
|
static struct rt_symbol *rt_symtab[RTC_TABSIZE];
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
/*
|
1997-11-29 03:32:48 +00:00
|
|
|
* Look up a symbol in the run-time common symbol table. For efficiency,
|
|
|
|
* the symbol's hash value must be passed in too.
|
1994-02-13 20:43:13 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static inline struct rt_symbol *
|
|
|
|
lookup_rts(name, hash)
|
|
|
|
char *name;
|
|
|
|
unsigned long hash;
|
1993-12-22 23:28:35 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
register struct rt_symbol *rtsp;
|
1993-12-22 23:28:35 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
for (rtsp = rt_symtab[hash % RTC_TABSIZE]; rtsp; rtsp = rtsp->rt_link)
|
|
|
|
if (strcmp(name, rtsp->rt_sp->nz_name) == 0)
|
1994-02-13 20:43:13 +00:00
|
|
|
return rtsp;
|
|
|
|
|
|
|
|
return NULL;
|
1993-12-22 23:28:35 +00:00
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Enter a symbol into the run-time common symbol table. For efficiency,
|
|
|
|
* the symbol's hash value must be passed in too.
|
|
|
|
*/
|
|
|
|
static struct rt_symbol *
|
|
|
|
enter_rts(name, hash, value, type, srcaddr, size, smp)
|
1994-02-13 20:43:13 +00:00
|
|
|
char *name;
|
1997-11-29 03:32:48 +00:00
|
|
|
unsigned long hash;
|
|
|
|
long value;
|
|
|
|
int type;
|
|
|
|
caddr_t srcaddr;
|
|
|
|
long size;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
|
|
|
{
|
|
|
|
register struct rt_symbol *rtsp, **rpp;
|
|
|
|
|
|
|
|
/* Find end of bucket */
|
1997-11-29 03:32:48 +00:00
|
|
|
for (rpp = &rt_symtab[hash % RTC_TABSIZE]; *rpp; rpp = &(*rpp)->rt_link)
|
1995-02-07 13:33:42 +00:00
|
|
|
continue;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
|
|
|
/* Allocate new common symbol */
|
1997-11-29 03:32:48 +00:00
|
|
|
rtsp = (struct rt_symbol *)xmalloc(sizeof(struct rt_symbol));
|
|
|
|
rtsp->rt_sp = (struct nzlist *)xmalloc(sizeof(struct nzlist));
|
1994-02-13 20:43:13 +00:00
|
|
|
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-22 19:05:31 +00:00
|
|
|
/*
|
|
|
|
* Lookup NAME in the link maps. The link map producing a definition
|
1997-11-29 03:32:48 +00:00
|
|
|
* is returned in SRC_MAP. If SRC_MAP is not NULL on entry the search
|
|
|
|
* is confined to that map.
|
|
|
|
*
|
|
|
|
* REAL_DEF_ONLY is a boolean which specifies whether certain special
|
|
|
|
* symbols for functions should satisfy the lookup or not. The
|
|
|
|
* reasons behind it are somewhat complicated. They are motivated
|
|
|
|
* by the scenario in which the address of a single function is
|
|
|
|
* taken from several shared objects. The address should come out
|
|
|
|
* the same in all cases, because the application code might decide
|
|
|
|
* to use it in comparisons. To make this work, the linker creates
|
|
|
|
* a symbol entry for the function in the main executable, with a
|
|
|
|
* type of N_UNDF+N_EXT, an N_AUX of AUX_FUNC, and a value that
|
|
|
|
* refers to the PLT entry for the function in the main executable.
|
|
|
|
* If REAL_DEF_ONLY is false, then this kind of special symbol is
|
|
|
|
* considered a "definition" when lookup up the symbol. Since the
|
|
|
|
* main executable is at the beginning of the shared object search
|
|
|
|
* list, the result is that references from all shared objects will
|
|
|
|
* resolve to the main program's PLT entry, and thus the function
|
|
|
|
* addresses will compare equal as they should.
|
|
|
|
*
|
|
|
|
* When relocating the PLT entry itself, we obviously must match
|
|
|
|
* only the true defining symbol for the function. In that case, we
|
|
|
|
* set REAL_DEF_ONLY to true, which disables matching the special
|
|
|
|
* N_UNDF+N_EXT entries.
|
|
|
|
*
|
|
|
|
* It is not so clear how to set this flag for a lookup done from
|
|
|
|
* dlsym. If the lookup specifies a particular shared object other
|
|
|
|
* than the main executable, the flag makes no difference -- only the
|
|
|
|
* true definition will be matched. (That is because the special
|
|
|
|
* symbols are only present in the main executable, which will not
|
|
|
|
* be searched.) But when the lookup is over all the shared objects
|
|
|
|
* (i.e., dlsym's "fd" parameter is NULL), then the flag does have an
|
|
|
|
* effect. We elect to match only the true definition even in that
|
|
|
|
* case.
|
|
|
|
*
|
|
|
|
* The upshot of all this is the following rule of thumb: Set
|
|
|
|
* REAL_DEF_ONLY in all cases except when processing a non-PLT
|
|
|
|
* relocation.
|
1993-11-22 19:05:31 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static struct nzlist *
|
|
|
|
lookup(name, src_map, real_def_only)
|
1994-02-13 20:43:13 +00:00
|
|
|
char *name;
|
|
|
|
struct so_map **src_map; /* IN/OUT */
|
1997-11-29 03:32:48 +00:00
|
|
|
int real_def_only;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1997-11-29 03:32:48 +00:00
|
|
|
unsigned long hash;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
hash = sym_hash(name);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
if (*src_map != NULL) /* Look in just one specific object */
|
|
|
|
return lookup_in_obj(name, hash, *src_map, real_def_only);
|
|
|
|
else { /* Search runtime symbols and all loaded objects */
|
|
|
|
unsigned long common_size;
|
|
|
|
struct so_map *smp;
|
|
|
|
struct rt_symbol *rtsp;
|
|
|
|
struct nzlist *np;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
if ((rtsp = lookup_rts(name, hash)) != NULL)
|
|
|
|
return rtsp->rt_sp;
|
1993-12-22 23:28:35 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
common_size = 0;
|
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
|
|
|
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
|
|
|
|
continue;
|
|
|
|
np = lookup_in_obj(name, hash, smp, real_def_only);
|
|
|
|
if (np == NULL)
|
|
|
|
continue;
|
|
|
|
/* We know that np->nz_value > 0 at this point. */
|
|
|
|
if (np->nz_type == N_UNDF+N_EXT &&
|
|
|
|
N_AUX(&np->nlist) != AUX_FUNC) { /* Common */
|
1993-11-22 19:05:31 +00:00
|
|
|
if (common_size < np->nz_value)
|
|
|
|
common_size = np->nz_value;
|
|
|
|
continue;
|
|
|
|
}
|
1997-11-29 03:32:48 +00:00
|
|
|
|
|
|
|
/* We found the symbol definition. */
|
|
|
|
*src_map = smp;
|
|
|
|
return np;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
1997-11-29 03:32:48 +00:00
|
|
|
if (common_size > 0) { /* It is a common symbol. */
|
|
|
|
void *mem;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
mem = memset(xmalloc(common_size), 0, common_size);
|
|
|
|
rtsp = enter_rts(name, hash, (long)mem, N_UNDF + N_EXT,
|
|
|
|
0, common_size, NULL);
|
|
|
|
return rtsp->rt_sp;
|
|
|
|
}
|
|
|
|
|
1998-06-21 14:22:29 +00:00
|
|
|
/*
|
|
|
|
* Just before giving up, check for the __error() hack.
|
|
|
|
*/
|
|
|
|
np = lookup_errno_hack(name, src_map, real_def_only);
|
|
|
|
if (np != NULL)
|
|
|
|
return np;
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/* No definition was found for the symbol. */
|
|
|
|
return NULL;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
1997-11-29 03:32:48 +00:00
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Lookup a symbol in one specific shared object. The hash
|
|
|
|
* value is passed in for efficiency. For an explanation of the
|
|
|
|
* "real_def_only" flag, see the comment preceding the "lookup"
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
static struct nzlist *
|
|
|
|
lookup_in_obj(name, hash, smp, real_def_only)
|
|
|
|
char *name;
|
|
|
|
unsigned long hash;
|
|
|
|
struct so_map *smp;
|
|
|
|
int real_def_only;
|
|
|
|
{
|
|
|
|
unsigned long buckets;
|
|
|
|
struct rrs_hash *hp;
|
|
|
|
char *cp;
|
|
|
|
struct nzlist *np;
|
|
|
|
char *symbolbase;
|
|
|
|
struct rrs_hash *hashbase;
|
|
|
|
char *stringbase;
|
|
|
|
size_t symsize;
|
|
|
|
|
|
|
|
if ((buckets = LD_BUCKETS(smp->som_dynamic)) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hashbase = LM_HASH(smp);
|
|
|
|
|
|
|
|
restart:
|
|
|
|
hp = &hashbase[hash % buckets];
|
|
|
|
if (hp->rh_symbolnum == -1)
|
1993-11-03 23:41:59 +00:00
|
|
|
return NULL;
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
symbolbase = (char *)LM_SYMBOL(smp, 0);
|
|
|
|
stringbase = LM_STRINGS(smp);
|
|
|
|
symsize = LD_VERSION_NZLIST_P(smp->som_dynamic->d_version)?
|
|
|
|
sizeof(struct nzlist) : sizeof(struct nlist);
|
|
|
|
for ( ; ; ) {
|
|
|
|
np = (struct nzlist *)(symbolbase + hp->rh_symbolnum*symsize);
|
|
|
|
cp = stringbase + np->nz_strx;
|
|
|
|
if (strcmp(cp, name) == 0)
|
|
|
|
break;
|
|
|
|
if (hp->rh_next == 0) /* End of hash chain */
|
|
|
|
return NULL;
|
|
|
|
hp = hashbase + hp->rh_next;
|
|
|
|
}
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/*
|
1997-11-29 03:32:48 +00:00
|
|
|
* We have a symbol with the name we're looking for.
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
if (np->nz_type == N_INDR+N_EXT) {
|
|
|
|
/*
|
|
|
|
* Next symbol gives the aliased name. Restart
|
|
|
|
* search with new name.
|
|
|
|
*/
|
|
|
|
name = stringbase + (++np)->nz_strx;
|
|
|
|
hash = sym_hash(name);
|
|
|
|
goto restart;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
if (np->nz_value == 0) /* It's not a definition */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (real_def_only) /* Don't match special function symbols. */
|
|
|
|
if (np->nz_type == N_UNDF+N_EXT &&
|
|
|
|
N_AUX(&np->nlist) == AUX_FUNC)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return np;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
/*
|
|
|
|
* Return the value of a symbol in the user's program. This is used
|
|
|
|
* internally for a few symbols which must exist. If the requested
|
|
|
|
* symbol is not found, this simply exits with a fatal error.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
sym_addr(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
struct nzlist *np;
|
|
|
|
|
|
|
|
smp = NULL;
|
|
|
|
np = lookup(name, &smp, 1);
|
|
|
|
if (np == NULL)
|
|
|
|
errx(1, "Program has no symbol \"%s\"", name);
|
|
|
|
return ((smp == NULL) ? NULL : smp->som_addr) + np->nz_value;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1998-06-21 14:22:29 +00:00
|
|
|
|
1998-08-22 15:51:41 +00:00
|
|
|
static int *p_errno; /* Pointer to errno variable in main program. */
|
|
|
|
|
1998-06-21 14:22:29 +00:00
|
|
|
/*
|
|
|
|
* Help old a.out binaries that are broken by the new errno macro. They
|
|
|
|
* can be missing __error() through no fault of their own. In particular,
|
|
|
|
* old a.out binaries can link against an old libc that does not contain
|
|
|
|
* __error(), yet still require __error() because of other libraries that
|
|
|
|
* have been recompiled since the errno change. This locates the backward
|
|
|
|
* compatible work-alike we have hidden here in ld.so.
|
|
|
|
*/
|
|
|
|
static struct nzlist *
|
|
|
|
lookup_errno_hack(sym, src_map, real_def_only)
|
|
|
|
char *sym;
|
|
|
|
struct so_map **src_map;
|
|
|
|
int real_def_only;
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
struct nzlist *np;
|
|
|
|
|
|
|
|
if (strcmp(sym, "___error") != 0)
|
|
|
|
return NULL;
|
|
|
|
|
1998-08-22 15:51:41 +00:00
|
|
|
/*
|
|
|
|
* Locate errno in the main program. If it's not there, NULL
|
|
|
|
* will be returned by our __error() substitute, and a core dump
|
|
|
|
* will follow. That's impossible, of course, since crt0.o always
|
|
|
|
* supplies errno.
|
|
|
|
*/
|
|
|
|
smp = NULL;
|
|
|
|
np = lookup("_errno", &smp, 1);
|
|
|
|
if (np != NULL && smp != NULL) {
|
|
|
|
p_errno = (int *)(smp->som_addr + np->nz_value);
|
|
|
|
#ifdef DEBUG
|
|
|
|
xprintf(" HACK: _errno at %p in %s\n", p_errno, smp->som_path);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-06-21 14:22:29 +00:00
|
|
|
/*
|
|
|
|
* Specifically find the ld.so link map because most routines
|
|
|
|
* skip over it during normal operation.
|
|
|
|
*/
|
|
|
|
for (smp = link_map_head; ; smp = smp->som_next)
|
|
|
|
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
1998-08-22 15:51:41 +00:00
|
|
|
* Find our __error() substitute stashed here in ld.so.
|
1998-06-21 14:22:29 +00:00
|
|
|
*/
|
|
|
|
np = lookup("___error_unthreaded_hack", &smp, real_def_only);
|
|
|
|
if (np != NULL)
|
|
|
|
*src_map = smp;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (np == NULL)
|
|
|
|
xprintf(" HACK: %s fudge not found, oops\n", sym);
|
|
|
|
else
|
|
|
|
xprintf(" HACK: %s fudge in %s\n", sym, smp->som_path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just like __error_unthreaded(), but for those poor orphaned a.out
|
|
|
|
* binaries from way back that are bamboozled by the new errno macro.
|
|
|
|
*/
|
|
|
|
int *
|
|
|
|
__error_unthreaded_hack()
|
|
|
|
{
|
1998-08-22 15:51:41 +00:00
|
|
|
return p_errno;
|
1998-06-21 14:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-03 23:41:59 +00:00
|
|
|
/*
|
|
|
|
* This routine is called from the jumptable to resolve
|
|
|
|
* procedure calls to shared objects.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
long
|
1993-11-03 23:41:59 +00:00
|
|
|
binder(jsp)
|
1994-02-13 20:43:13 +00:00
|
|
|
jmpslot_t *jsp;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp, *src_map = NULL;
|
1993-11-03 23:41:59 +00:00
|
|
|
long addr;
|
|
|
|
char *sym;
|
|
|
|
struct nzlist *np;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the PLT map that contains JSP.
|
|
|
|
*/
|
1994-02-13 20:43:13 +00:00
|
|
|
for (smp = link_map_head; smp; smp = smp->som_next) {
|
|
|
|
if (LM_PLT(smp) < jsp &&
|
|
|
|
jsp < LM_PLT(smp) + LD_PLTSZ(smp->som_dynamic)/sizeof(*jsp))
|
1993-11-03 23:41:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
if (smp == NULL)
|
1997-11-29 03:32:48 +00:00
|
|
|
errx(1, "Call to binder from unknown location: %p\n", jsp);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
index = jsp->reloc_index & JMPSLOT_RELOC_MASK;
|
|
|
|
|
|
|
|
/* Get the local symbol this jmpslot refers to */
|
1994-02-13 20:43:13 +00:00
|
|
|
sym = LM_STRINGS(smp) +
|
|
|
|
LM_SYMBOL(smp,RELOC_SYMBOL(&LM_REL(smp)[index]))->nz_strx;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1993-11-22 19:05:31 +00:00
|
|
|
np = lookup(sym, &src_map, 1);
|
1993-11-03 23:41:59 +00:00
|
|
|
if (np == NULL)
|
1997-11-29 03:32:48 +00:00
|
|
|
errx(1, "Undefined symbol \"%s\" called from %s:%s at %p",
|
1994-02-13 20:43:13 +00:00
|
|
|
sym, main_progname, smp->som_path, jsp);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/* Fixup jmpslot so future calls transfer directly to target */
|
|
|
|
addr = np->nz_value;
|
|
|
|
if (src_map)
|
1994-02-13 20:43:13 +00:00
|
|
|
addr += (long)src_map->som_addr;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
md_fix_jmpslot(jsp, (long)jsp, addr);
|
|
|
|
|
|
|
|
#if DEBUG
|
1995-02-07 13:33:42 +00:00
|
|
|
xprintf(" BINDER: %s located at = %#x in %s\n", sym, addr,
|
|
|
|
src_map->som_path);
|
1993-11-03 23:41:59 +00:00
|
|
|
#endif
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
static struct hints_header *hheader; /* NULL means not mapped */
|
1993-11-03 23:41:59 +00:00
|
|
|
static struct hints_bucket *hbuckets;
|
|
|
|
static char *hstrtab;
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Map the hints file into memory, if it is not already mapped. Returns
|
|
|
|
* 0 on success, or -1 on failure.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static int
|
1995-02-07 13:33:42 +00:00
|
|
|
maphints __P((void))
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
static int hints_bad; /* TRUE if hints are unusable */
|
1996-10-10 23:16:50 +00:00
|
|
|
static int paths_added;
|
1996-01-13 00:15:25 +00:00
|
|
|
int hfd;
|
|
|
|
struct hints_header hdr;
|
|
|
|
caddr_t addr;
|
|
|
|
|
|
|
|
if (hheader != NULL) /* Already mapped */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (hints_bad) /* Known to be corrupt or unavailable */
|
|
|
|
return -1;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1995-03-04 17:49:20 +00:00
|
|
|
if ((hfd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
|
1996-01-13 00:15:25 +00:00
|
|
|
hints_bad = 1;
|
|
|
|
return -1;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Read the header and check it */
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if (read(hfd, &hdr, sizeof hdr) != sizeof hdr ||
|
|
|
|
HH_BADMAG(hdr) ||
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
(hdr.hh_version != LD_HINTS_VERSION_1 &&
|
|
|
|
hdr.hh_version != LD_HINTS_VERSION_2)) {
|
1995-03-04 17:49:20 +00:00
|
|
|
close(hfd);
|
1996-01-13 00:15:25 +00:00
|
|
|
hints_bad = 1;
|
|
|
|
return -1;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Map the hints into memory */
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
addr = mmap(0, hdr.hh_ehints, PROT_READ, MAP_SHARED, hfd, 0);
|
|
|
|
if (addr == (caddr_t)-1) {
|
1995-03-04 17:49:20 +00:00
|
|
|
close(hfd);
|
1996-01-13 00:15:25 +00:00
|
|
|
hints_bad = 1;
|
|
|
|
return -1;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
close(hfd);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
hheader = (struct hints_header *)addr;
|
1993-11-03 23:41:59 +00:00
|
|
|
hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
|
|
|
|
hstrtab = (char *)(addr + hheader->hh_strtab);
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
/* pluck out the system ldconfig path */
|
1996-10-10 23:16:50 +00:00
|
|
|
if (hheader->hh_version >= LD_HINTS_VERSION_2 && !paths_added) {
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
add_search_path(hstrtab + hheader->hh_dirlist);
|
1996-10-10 23:16:50 +00:00
|
|
|
paths_added = 1;
|
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Unmap the hints file, if it is currently mapped.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1994-09-15 20:48:55 +00:00
|
|
|
unmaphints()
|
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
if (hheader != NULL) {
|
|
|
|
munmap((caddr_t)hheader, hheader->hh_ehints);
|
1994-09-15 20:48:55 +00:00
|
|
|
hheader = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
int
|
1995-01-12 19:12:29 +00:00
|
|
|
hinthash(cp, vmajor)
|
1994-02-13 20:43:13 +00:00
|
|
|
char *cp;
|
1995-01-12 19:12:29 +00:00
|
|
|
int vmajor;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
|
|
|
int k = 0;
|
|
|
|
|
|
|
|
while (*cp)
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
|
|
|
|
|
|
|
|
k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef major
|
|
|
|
#undef minor
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Search for a library in the hints generated by ldconfig. On success,
|
|
|
|
* returns the full pathname of the matching library. This string is
|
|
|
|
* always dynamically allocated on the heap.
|
|
|
|
*
|
1998-11-07 01:59:39 +00:00
|
|
|
* MINORP is an in/out parameter. If the incoming value of *MINORP is
|
|
|
|
* >= 0, then no library will be considered a match unless its minor
|
|
|
|
* version number is at least that large. Otherwise, only the major
|
|
|
|
* version number is checked. In any case, the minor number of the
|
|
|
|
* matching library is stored into *MINORP.
|
1996-01-13 00:15:25 +00:00
|
|
|
*
|
|
|
|
* Returns NULL if the library cannot be found.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static char *
|
1996-01-13 00:15:25 +00:00
|
|
|
findhint(name, major, minorp)
|
1994-02-13 20:43:13 +00:00
|
|
|
char *name;
|
1996-01-13 00:15:25 +00:00
|
|
|
int major;
|
|
|
|
int *minorp;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct hints_bucket *bp =
|
|
|
|
hbuckets + (hinthash(name, major) % hheader->hh_nbucket);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Sanity check */
|
|
|
|
if (bp->hi_namex >= hheader->hh_strtab_sz) {
|
1995-04-21 04:57:50 +00:00
|
|
|
warnx("Bad name index: %#x\n", bp->hi_namex);
|
1993-11-03 23:41:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bp->hi_pathx >= hheader->hh_strtab_sz) {
|
1995-04-21 04:57:50 +00:00
|
|
|
warnx("Bad path index: %#x\n", bp->hi_pathx);
|
1993-11-03 23:41:59 +00:00
|
|
|
break;
|
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
1998-11-07 01:59:39 +00:00
|
|
|
* For a given major number, the hints file has only one
|
|
|
|
* entry -- namely, the one with the highest minor number.
|
|
|
|
* If we find an entry with a matching major number, we
|
|
|
|
* know it is the best one.
|
1996-01-13 00:15:25 +00:00
|
|
|
*/
|
|
|
|
if (strcmp(name, hstrtab + bp->hi_namex) == 0 &&
|
|
|
|
bp->hi_major == major) {
|
1997-01-14 17:53:55 +00:00
|
|
|
struct stat s;
|
1998-11-07 01:59:39 +00:00
|
|
|
int realminor;
|
1997-01-14 17:53:55 +00:00
|
|
|
|
1998-11-07 01:59:39 +00:00
|
|
|
realminor = bp->hi_ndewey >= 2 ? bp->hi_minor : 0;
|
|
|
|
if (realminor < *minorp) /* Not good enough */
|
|
|
|
return NULL;
|
1997-01-14 17:53:55 +00:00
|
|
|
if (stat(hstrtab + bp->hi_pathx, &s) == -1)
|
|
|
|
return NULL; /* Doesn't actually exist */
|
1998-11-07 01:59:39 +00:00
|
|
|
*minorp = realminor;
|
1996-01-13 00:15:25 +00:00
|
|
|
return strdup(hstrtab + bp->hi_pathx);
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bp->hi_next == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Move on to next in bucket */
|
|
|
|
bp = &hbuckets[bp->hi_next];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No hints available for name */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/*
|
|
|
|
* Search for the given shared library. On success, returns a string
|
|
|
|
* containing the full pathname for the library. This string is always
|
|
|
|
* dynamically allocated on the heap.
|
|
|
|
*
|
|
|
|
* Returns NULL if the library cannot be found.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static char *
|
1998-11-07 01:59:39 +00:00
|
|
|
rtfindlib(name, major, minor, strictminor)
|
1994-02-13 20:43:13 +00:00
|
|
|
char *name;
|
|
|
|
int major, minor;
|
1998-11-07 01:59:39 +00:00
|
|
|
int strictminor;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
char *ld_path = ld_library_path;
|
|
|
|
char *path = NULL;
|
1998-11-07 01:59:39 +00:00
|
|
|
int realminor;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1998-11-07 01:59:39 +00:00
|
|
|
realminor = strictminor ? minor : -1;
|
1996-01-13 00:15:25 +00:00
|
|
|
if (ld_path != NULL) { /* First, search the directories in ld_path */
|
|
|
|
/*
|
|
|
|
* There is no point in trying to use the hints file for this.
|
|
|
|
*/
|
|
|
|
char *dir;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
while (path == NULL && (dir = strsep(&ld_path, ":")) != NULL) {
|
|
|
|
path = search_lib_dir(dir, name, &major, &realminor, 0);
|
1996-04-20 18:29:50 +00:00
|
|
|
if (ld_path != NULL)
|
|
|
|
*(ld_path - 1) = ':';
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if (path == NULL && maphints() == 0) /* Search the hints file */
|
|
|
|
path = findhint(name, major, &realminor);
|
1994-06-15 22:41:19 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
if (path == NULL) /* Search the standard directories */
|
|
|
|
path = findshlib(name, &major, &realminor, 0);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
if (path != NULL && realminor < minor && !ld_suppress_warnings) {
|
1996-01-13 00:15:25 +00:00
|
|
|
warnx("warning: %s: minor version %d"
|
|
|
|
" older than expected %d, using it anyway",
|
|
|
|
path, realminor, minor);
|
|
|
|
}
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
return path;
|
|
|
|
}
|
1995-02-07 13:33:42 +00:00
|
|
|
|
1996-04-20 18:29:50 +00:00
|
|
|
/*
|
|
|
|
* Search for the given shared library file. This is similar to rtfindlib,
|
|
|
|
* except that the argument is the actual name of the desired library file.
|
|
|
|
* Thus there is no need to worry about version numbers. The return value
|
|
|
|
* is a string containing the full pathname for the library. This string
|
|
|
|
* is always dynamically allocated on the heap.
|
|
|
|
*
|
|
|
|
* Returns NULL if the library cannot be found.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static char *
|
1996-04-20 18:29:50 +00:00
|
|
|
rtfindfile(name)
|
1998-06-07 03:53:08 +00:00
|
|
|
const char *name;
|
1996-04-20 18:29:50 +00:00
|
|
|
{
|
|
|
|
char *ld_path = ld_library_path;
|
|
|
|
char *path = NULL;
|
|
|
|
|
|
|
|
if (ld_path != NULL) { /* First, search the directories in ld_path */
|
|
|
|
char *dir;
|
|
|
|
|
|
|
|
while (path == NULL && (dir = strsep(&ld_path, ":")) != NULL) {
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
path = concat(dir, "/", name);
|
|
|
|
if (lstat(path, &sb) == -1) { /* Does not exist */
|
|
|
|
free(path);
|
|
|
|
path = NULL;
|
|
|
|
}
|
|
|
|
if (ld_path != NULL)
|
|
|
|
*(ld_path - 1) = ':';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't search the hints file. It is organized around major
|
|
|
|
* and minor version numbers, so it is not suitable for finding
|
|
|
|
* a specific file name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (path == NULL) /* Search the standard directories */
|
|
|
|
path = find_lib_file(name);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
/*
|
|
|
|
* Buffer for error messages and a pointer that is set to point to the buffer
|
|
|
|
* when a error occurs. It acts as a last error flag, being set to NULL
|
|
|
|
* after an error is returned.
|
|
|
|
*/
|
|
|
|
#define DLERROR_BUF_SIZE 512
|
|
|
|
static char dlerror_buf [DLERROR_BUF_SIZE];
|
|
|
|
static char *dlerror_msg = NULL;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *
|
1996-01-13 00:15:25 +00:00
|
|
|
__dlopen(path, mode)
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *path;
|
|
|
|
int mode;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *old_tail = link_map_tail;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp;
|
1997-01-12 19:59:26 +00:00
|
|
|
int bind_now = mode == RTLD_NOW;
|
1998-06-07 03:53:08 +00:00
|
|
|
char *name;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
/*
|
|
|
|
* path == NULL is handled by map_object()
|
|
|
|
*/
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
anon_open();
|
1994-02-13 20:43:13 +00:00
|
|
|
|
1998-06-07 03:53:08 +00:00
|
|
|
name = (path && strchr(path, '/') == NULL) ? rtfindfile(path) : (char *)path;
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Map the object, and the objects on which it depends */
|
1998-06-07 03:53:08 +00:00
|
|
|
smp = map_object(name, (struct sod *) NULL, (struct so_map *) NULL);
|
|
|
|
if (name != path)
|
|
|
|
free(name);
|
1996-01-13 00:15:25 +00:00
|
|
|
if(smp == NULL) /* Failed */
|
1994-02-13 20:43:13 +00:00
|
|
|
return NULL;
|
1996-01-13 00:15:25 +00:00
|
|
|
LM_PRIVATE(smp)->spd_flags |= RTLD_DL;
|
|
|
|
|
|
|
|
/* Relocate and initialize all newly-mapped objects */
|
|
|
|
if(link_map_tail != old_tail) { /* We have mapped some new objects */
|
1997-11-29 03:32:48 +00:00
|
|
|
if(reloc_dag(smp, bind_now) == -1) /* Failed */
|
1996-01-13 00:15:25 +00:00
|
|
|
return NULL;
|
1997-11-29 03:32:48 +00:00
|
|
|
init_dag(smp);
|
1994-02-13 20:43:13 +00:00
|
|
|
}
|
1995-04-21 04:57:50 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
unmaphints();
|
|
|
|
anon_close();
|
1994-02-13 20:43:13 +00:00
|
|
|
|
|
|
|
return smp;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static int
|
1994-06-15 22:41:19 +00:00
|
|
|
__dlclose(fd)
|
1994-02-13 20:43:13 +00:00
|
|
|
void *fd;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-02-13 20:43:13 +00:00
|
|
|
struct so_map *smp = (struct so_map *)fd;
|
1996-01-13 00:15:25 +00:00
|
|
|
struct so_map *scanp;
|
1994-02-13 20:43:13 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
1995-02-07 13:33:42 +00:00
|
|
|
xprintf("dlclose(%s): refcount = %d\n", smp->som_path,
|
|
|
|
LM_PRIVATE(smp)->spd_refcount);
|
1994-02-13 20:43:13 +00:00
|
|
|
#endif
|
1996-01-13 00:15:25 +00:00
|
|
|
/* Check the argument for validity */
|
|
|
|
for(scanp = link_map_head; scanp != NULL; scanp = scanp->som_next)
|
|
|
|
if(scanp == smp) /* We found the map in the list */
|
|
|
|
break;
|
|
|
|
if(scanp == NULL || !(LM_PRIVATE(smp)->spd_flags & RTLD_DL)) {
|
|
|
|
generror("Invalid argument to dlclose");
|
|
|
|
return -1;
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
unmap_object(smp, 0);
|
1994-02-13 20:43:13 +00:00
|
|
|
|
|
|
|
return 0;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
1997-08-02 04:56:44 +00:00
|
|
|
/*
|
|
|
|
* This form of dlsym is obsolete. Current versions of crt0 don't call
|
|
|
|
* it. It can still be called by old executables that were linked with
|
|
|
|
* old versions of crt0.
|
|
|
|
*/
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *
|
1994-06-15 22:41:19 +00:00
|
|
|
__dlsym(fd, sym)
|
1997-11-29 03:32:48 +00:00
|
|
|
void *fd;
|
|
|
|
const char *sym;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1997-08-02 04:56:44 +00:00
|
|
|
if (fd == RTLD_NEXT) {
|
|
|
|
generror("RTLD_NEXT not supported by this version of"
|
1998-05-26 20:12:56 +00:00
|
|
|
" crt0.o");
|
1997-08-02 04:56:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return __dlsym3(fd, sym, NULL);
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *
|
1997-08-19 23:33:45 +00:00
|
|
|
resolvesym(fd, sym, retaddr)
|
1997-08-02 04:56:44 +00:00
|
|
|
void *fd;
|
|
|
|
char *sym;
|
|
|
|
void *retaddr;
|
|
|
|
{
|
|
|
|
struct so_map *smp;
|
|
|
|
struct so_map *src_map;
|
1994-02-13 20:43:13 +00:00
|
|
|
struct nzlist *np;
|
|
|
|
long addr;
|
|
|
|
|
1997-08-02 04:56:44 +00:00
|
|
|
if (fd == RTLD_NEXT) {
|
|
|
|
/* Find the shared object that contains the caller. */
|
|
|
|
for (smp = link_map_head; smp != NULL; smp = smp->som_next) {
|
|
|
|
void *textbase = smp->som_addr + LM_TXTADDR(smp);
|
|
|
|
void *textlimit = LM_ETEXT(smp);
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
|
1997-08-02 04:56:44 +00:00
|
|
|
if (textbase <= retaddr && retaddr < textlimit)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (smp == NULL) {
|
|
|
|
generror("Cannot determine caller's shared object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
smp = smp->som_next;
|
|
|
|
if (smp != NULL && LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
|
|
|
|
smp = smp->som_next;
|
|
|
|
if (smp == NULL) {
|
|
|
|
generror("No next shared object for RTLD_NEXT");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
src_map = smp;
|
|
|
|
np = lookup(sym, &src_map, 1);
|
|
|
|
} while (np == NULL && (smp = smp->som_next) != NULL);
|
|
|
|
} else {
|
|
|
|
smp = (struct so_map *)fd;
|
|
|
|
src_map = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restrict search to passed map if dlopen()ed.
|
|
|
|
*/
|
|
|
|
if (smp != NULL && LM_PRIVATE(smp)->spd_flags & RTLD_DL)
|
|
|
|
src_map = smp;
|
|
|
|
|
|
|
|
np = lookup(sym, &src_map, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (np == NULL) {
|
|
|
|
generror("Undefined symbol");
|
1994-02-13 20:43:13 +00:00
|
|
|
return NULL;
|
1997-08-02 04:56:44 +00:00
|
|
|
}
|
1996-01-13 00:15:25 +00:00
|
|
|
|
1994-02-13 20:43:13 +00:00
|
|
|
addr = np->nz_value;
|
|
|
|
if (src_map)
|
|
|
|
addr += (long)src_map->som_addr;
|
|
|
|
|
|
|
|
return (void *)addr;
|
|
|
|
}
|
|
|
|
|
1998-02-06 16:46:46 +00:00
|
|
|
static int
|
|
|
|
__dladdr(addr, dlip)
|
|
|
|
const void *addr;
|
|
|
|
Dl_info *dlip;
|
|
|
|
{
|
|
|
|
struct _dynamic *dp;
|
|
|
|
struct so_map *smp;
|
|
|
|
char *stringbase;
|
|
|
|
long numsyms;
|
|
|
|
int symsize;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Find the shared object that contains the address. */
|
|
|
|
for (smp = link_map_head; smp != NULL; smp = smp->som_next) {
|
|
|
|
struct so_map *src_map;
|
|
|
|
struct somap_private *smpp;
|
|
|
|
struct nzlist *np;
|
|
|
|
|
|
|
|
smpp = LM_PRIVATE(smp);
|
|
|
|
if (smpp->spd_flags & RTLD_RTLD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((void *)smp->som_addr > addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
src_map = smp;
|
|
|
|
if ((np = lookup(END_SYM, &src_map, 1)) == NULL)
|
|
|
|
continue; /* No "_end" symbol?! */
|
|
|
|
if (addr < (void *)(smp->som_addr + np->nz_value))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (smp == NULL) {
|
|
|
|
generror("No shared object contains address");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dlip->dli_fname = smp->som_path;
|
|
|
|
dlip->dli_fbase = smp->som_addr;
|
|
|
|
dlip->dli_saddr = (void *) 0;
|
|
|
|
dlip->dli_sname = NULL;
|
|
|
|
|
|
|
|
dp = smp->som_dynamic;
|
|
|
|
symsize = LD_VERSION_NZLIST_P(dp->d_version) ?
|
|
|
|
sizeof(struct nzlist) : sizeof(struct nlist);
|
|
|
|
numsyms = LD_STABSZ(dp) / symsize;
|
|
|
|
stringbase = LM_STRINGS(smp);
|
|
|
|
|
|
|
|
for (i = 0; i < numsyms; i++) {
|
|
|
|
struct nzlist *symp = LM_SYMBOL(smp, i);
|
|
|
|
unsigned long value;
|
|
|
|
|
|
|
|
/* Reject all except definitions. */
|
|
|
|
if (symp->nz_type != N_EXT + N_ABS &&
|
|
|
|
symp->nz_type != N_EXT + N_TEXT &&
|
|
|
|
symp->nz_type != N_EXT + N_DATA &&
|
|
|
|
symp->nz_type != N_EXT + N_BSS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the symbol is greater than the specified address, or
|
|
|
|
* if it is further away from addr than the current nearest
|
|
|
|
* symbol, then reject it.
|
|
|
|
*/
|
|
|
|
value = (unsigned long) (smp->som_addr + symp->nz_value);
|
|
|
|
if (value > (unsigned long) addr ||
|
|
|
|
value < (unsigned long) dlip->dli_saddr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Update our idea of the nearest symbol. */
|
|
|
|
dlip->dli_sname = stringbase + symp->nz_strx;
|
|
|
|
dlip->dli_saddr = (void *) value;
|
|
|
|
|
|
|
|
if (dlip->dli_saddr == addr) /* Can't get any closer. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Remove any leading underscore from the symbol name, to hide
|
|
|
|
* our a.out-ness.
|
|
|
|
*/
|
|
|
|
if (dlip->dli_sname != NULL && dlip->dli_sname[0] == '_')
|
|
|
|
dlip->dli_sname++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void *
|
1997-08-19 23:33:45 +00:00
|
|
|
__dlsym3(fd, sym, retaddr)
|
1997-11-29 03:32:48 +00:00
|
|
|
void *fd;
|
|
|
|
const char *sym;
|
|
|
|
void *retaddr;
|
1997-08-19 23:33:45 +00:00
|
|
|
{
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
result = resolvesym(fd, sym, retaddr);
|
|
|
|
/*
|
|
|
|
* XXX - Ugly, but it makes the least impact on the run-time loader
|
|
|
|
* sources. We assume that most of the time the error is a
|
|
|
|
* undefined symbol error from above, so we try again. If it's
|
|
|
|
* not an undefined symbol we end up getting the same error twice,
|
|
|
|
* but that's acceptable.
|
|
|
|
*/
|
|
|
|
if (result == NULL) {
|
|
|
|
/* Prepend an underscore and try again */
|
1997-11-29 03:32:48 +00:00
|
|
|
char *newsym = xmalloc(strlen(sym) + 2);
|
1997-08-19 23:33:45 +00:00
|
|
|
|
|
|
|
newsym[0] = '_';
|
|
|
|
strcpy(&newsym[1], sym);
|
|
|
|
result = resolvesym(fd, newsym, retaddr);
|
|
|
|
free(newsym);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static const char *
|
1995-02-07 13:33:42 +00:00
|
|
|
__dlerror __P((void))
|
1994-02-13 20:43:13 +00:00
|
|
|
{
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *err;
|
1995-02-07 13:33:42 +00:00
|
|
|
|
|
|
|
err = dlerror_msg;
|
|
|
|
dlerror_msg = NULL; /* Next call will return NULL */
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1997-11-29 03:32:48 +00:00
|
|
|
static void
|
1995-06-27 09:53:27 +00:00
|
|
|
__dlexit __P((void))
|
|
|
|
{
|
1996-01-13 00:15:25 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
xprintf("__dlexit called\n");
|
|
|
|
#endif
|
1995-06-27 09:53:27 +00:00
|
|
|
|
1996-01-13 00:15:25 +00:00
|
|
|
unmap_object(link_map_head, 1);
|
1995-06-27 09:53:27 +00:00
|
|
|
}
|
|
|
|
|
1996-10-24 16:24:19 +00:00
|
|
|
/*
|
|
|
|
* Print the current error message and exit with failure status.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
die __P((void))
|
|
|
|
{
|
1997-11-29 03:32:48 +00:00
|
|
|
const char *msg;
|
1996-10-24 16:24:19 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "ld.so failed");
|
|
|
|
if ((msg = __dlerror()) != NULL)
|
|
|
|
fprintf(stderr, ": %s", msg);
|
|
|
|
putc('\n', stderr);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
/*
|
|
|
|
* Generate an error message that can be later be retrieved via dlerror.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
generror(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2002-05-28 18:57:20 +00:00
|
|
|
|
1995-02-07 13:33:42 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf (dlerror_buf, DLERROR_BUF_SIZE, fmt, ap);
|
|
|
|
dlerror_msg = dlerror_buf;
|
|
|
|
|
|
|
|
va_end(ap);
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xprintf(char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
va_list ap;
|
|
|
|
|
2002-05-28 18:57:20 +00:00
|
|
|
va_start(ap, fmt);
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
2001-07-26 11:02:39 +00:00
|
|
|
(void)write(STDOUT_FILENO, buf, strlen(buf));
|
1993-11-03 23:41:59 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* rt_readenv() etc.
|
|
|
|
*
|
|
|
|
* Do a sweep over the environment once only, pick up what
|
|
|
|
* looks interesting.
|
|
|
|
*
|
|
|
|
* This is pretty obscure, but is relatively simple. Simply
|
|
|
|
* look at each environment variable, if it starts with "LD_" then
|
|
|
|
* look closer at it. If it's in our table, set the variable
|
|
|
|
* listed. effectively, this is like:
|
|
|
|
* ld_preload = careful ? NULL : getenv("LD_PRELOAD");
|
|
|
|
* except that the environment is scanned once only to pick up all
|
|
|
|
* known variables, rather than scanned multiple times for each
|
|
|
|
* variable.
|
1997-01-12 00:16:36 +00:00
|
|
|
*
|
|
|
|
* If an environment variable of interest is set to the empty string, we
|
|
|
|
* treat it as if it were unset.
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define L(n, u, v) { n, sizeof(n) - 1, u, v },
|
|
|
|
struct env_scan_tab {
|
|
|
|
char *name;
|
|
|
|
int len;
|
|
|
|
int unsafe;
|
|
|
|
char **value;
|
|
|
|
} scan_tab[] = {
|
|
|
|
L("LD_LIBRARY_PATH=", 1, &ld_library_path)
|
|
|
|
L("LD_PRELOAD=", 1, &ld_preload)
|
1997-01-17 20:22:18 +00:00
|
|
|
L("LD_IGNORE_MISSING_OBJECTS=", 1, &ld_ignore_missing_objects)
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
L("LD_TRACE_LOADED_OBJECTS=", 0, &ld_tracing)
|
1997-01-12 00:16:36 +00:00
|
|
|
L("LD_BIND_NOW=", 0, &ld_bind_now)
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
L("LD_SUPPRESS_WARNINGS=", 0, &ld_suppress_warnings)
|
|
|
|
L("LD_WARN_NON_PURE_CODE=", 0, &ld_warn_non_pure_code)
|
1997-09-18 14:08:40 +00:00
|
|
|
{ NULL, 0, 0, NULL }
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
};
|
|
|
|
#undef L
|
|
|
|
|
1997-01-12 00:16:36 +00:00
|
|
|
static void
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
rt_readenv()
|
|
|
|
{
|
|
|
|
char **p = environ;
|
|
|
|
char *v;
|
|
|
|
struct env_scan_tab *t;
|
|
|
|
|
|
|
|
/* for each string in the environment... */
|
|
|
|
while ((v = *p++)) {
|
|
|
|
|
|
|
|
/* check for LD_xxx */
|
|
|
|
if (v[0] != 'L' || v[1] != 'D' || v[2] != '_')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (t = scan_tab; t->name; t++) {
|
|
|
|
if (careful && t->unsafe)
|
|
|
|
continue; /* skip for set[ug]id */
|
|
|
|
if (strncmp(t->name, v, t->len) == 0) {
|
1997-01-12 00:16:36 +00:00
|
|
|
if (*(v + t->len) != '\0') /* Not empty */
|
|
|
|
*t->value = v + t->len;
|
Update to handle new version ld.so.hints and info in executable for
configurable fallback search paths, as well as new crt interface version.
Also:
- even faster getenv(), get all environment variable settings in a single
pass.
- ldd printf-like format specifications
- minor code cleanups, one vsprintf -> vsnprintf (harmless)
The library search sequence is a little more complete now. Before,
it'd search $LD_LIBRARY_PATH (by opendir/readdir/closedir), then read
the hints file, then read /usr/lib (again by scanning thr directory). It
would then fail if there was no "found" library.
Now, it does LD_LIBRARY_PATH and the hints file the same, but then uses
a longer fallback path. The -R path is fetched from the executable if
specified at build time, the ldconfig path is appended, and /usr/lib is
appended to that. Duplicates are suppressed. This means that simply
placing a new library in /usr/local/lib will work (the same as it did in
/usr/lib) without needing ldconfig -m. It will find it quicker if the
ldconfig is run though.
Similar changes have been made to the NetBSD ld.so, but ours is rather
different now due to John Polstra's speedups and fixes from a while back.
The ldd printf-like format support came direct from NetBSD.
Reviewed by: nate, jdp
1996-10-01 01:52:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-11-29 03:32:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Malloc implementation for use within the dynamic linker. At first
|
|
|
|
* we do a simple allocation using sbrk. After the user's program
|
|
|
|
* has been loaded, we switch to using whatever malloc functions are
|
|
|
|
* defined there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Symbols related to the sbrk and brk implementations. */
|
|
|
|
#define CURBRK_SYM "curbrk"
|
|
|
|
#define MINBRK_SYM "minbrk"
|
|
|
|
#define END_SYM "_end"
|
|
|
|
|
|
|
|
/* Symbols related to malloc. */
|
|
|
|
#define FREE_SYM "_free"
|
|
|
|
#define MALLOC_SYM "_malloc"
|
|
|
|
#define REALLOC_SYM "_realloc"
|
|
|
|
|
|
|
|
/* Hooks into the implementation of sbrk and brk. */
|
|
|
|
extern char *curbrk __asm__(CURBRK_SYM);
|
|
|
|
extern char *minbrk __asm__(MINBRK_SYM);
|
|
|
|
|
|
|
|
/* Pointers to the user program's malloc functions. */
|
|
|
|
static void *(*p_malloc) __P((size_t));
|
|
|
|
static void *(*p_realloc) __P((void *, size_t));
|
|
|
|
static void (*p_free) __P((void *));
|
|
|
|
|
|
|
|
/* Upper limit of the memory allocated by our internal malloc. */
|
|
|
|
static char *rtld_alloc_lev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the internal malloc so that it will take its memory from the
|
|
|
|
* main program's sbrk arena.
|
|
|
|
*/
|
|
|
|
static void
|
1997-12-05 02:06:37 +00:00
|
|
|
init_internal_malloc __P((void))
|
1997-11-29 03:32:48 +00:00
|
|
|
{
|
1997-12-05 02:06:37 +00:00
|
|
|
const struct exec *hdr;
|
1997-11-29 03:32:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Before anything calls sbrk or brk, we have to initialize
|
1997-12-05 02:06:37 +00:00
|
|
|
* its idea of the current break level to just beyond the main
|
|
|
|
* program's address space. Strictly speaking, the right
|
|
|
|
* way to do that is to look up the value of "_end" in the
|
|
|
|
* application's run time symbol table.
|
|
|
|
*
|
|
|
|
* That is what we used to do, and it works correctly for
|
|
|
|
* every valid program. Unfortunately, it doesn't work right
|
|
|
|
* for "unexec"ed versions of emacs. They are incorrectly
|
|
|
|
* generated with a wrong value for "_end". (xemacs gets it
|
|
|
|
* right.)
|
|
|
|
*
|
|
|
|
* To work around this, we peek at the exec header to get the
|
|
|
|
* sizes of the text, data, and bss segments. Luckily, the
|
|
|
|
* header is in memory at the start of the first mapped page.
|
|
|
|
* From the segment sizes, we can calculate a proper initial
|
|
|
|
* value for the break level.
|
1997-11-29 03:32:48 +00:00
|
|
|
*/
|
1997-12-05 02:06:37 +00:00
|
|
|
hdr = (const struct exec *)PAGSIZ;
|
|
|
|
if (N_BADMAG(*hdr)) /* Sanity check */
|
|
|
|
errx(1, "Cannot find program's a.out header");
|
|
|
|
rtld_alloc_lev = curbrk = minbrk =
|
|
|
|
(char *)hdr + hdr->a_text + hdr->a_data + hdr->a_bss;
|
1997-11-29 03:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set things up so that the dynamic linker can use the program's
|
|
|
|
* malloc functions.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_external_malloc __P((void))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Patch the program's idea of the current break address to
|
|
|
|
* what it really is as a result of the allocations we have
|
|
|
|
* already done.
|
|
|
|
*/
|
|
|
|
*(char **)(sym_addr(CURBRK_SYM)) = curbrk;
|
1997-12-05 02:06:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the minimum break level too. Otherwise, "unexec"ed
|
|
|
|
* emacs sets the break too low and wipes out our tables of
|
|
|
|
* shared objects.
|
|
|
|
*/
|
|
|
|
*(char **)(sym_addr(MINBRK_SYM)) = curbrk;
|
1997-11-29 03:32:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up pointers to the program's allocation functions, so
|
|
|
|
* that we can use them from now on.
|
|
|
|
*/
|
|
|
|
p_malloc = (void *(*)(size_t))(sym_addr(MALLOC_SYM));
|
|
|
|
p_free = (void (*)(void *))(sym_addr(FREE_SYM));
|
|
|
|
p_realloc = (void *(*)(void *, size_t))(sym_addr(REALLOC_SYM));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
malloc(size)
|
|
|
|
size_t size;
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* If we are far enough along, we can use the system malloc. */
|
|
|
|
if (p_malloc != NULL)
|
|
|
|
return (*p_malloc)(size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise we use our simple built-in malloc. We get the
|
|
|
|
* memory from brk() in increments of one page. We store the
|
|
|
|
* allocated size in the first word, so that realloc can be
|
|
|
|
* made to work.
|
|
|
|
*/
|
|
|
|
if (rtld_alloc_lev == NULL)
|
|
|
|
errx(1, "Internal error: internal malloc called before"
|
|
|
|
" being initialized");
|
|
|
|
|
|
|
|
p = (char *)ALIGN(rtld_alloc_lev);
|
|
|
|
rtld_alloc_lev = p + sizeof(size_t) + size;
|
|
|
|
|
|
|
|
if (rtld_alloc_lev > curbrk) { /* Get memory from system */
|
|
|
|
char *newbrk;
|
|
|
|
|
|
|
|
newbrk = (char *)
|
|
|
|
roundup2((unsigned long)rtld_alloc_lev, PAGSIZ);
|
2002-01-24 12:11:31 +00:00
|
|
|
if (brk(newbrk) == -1)
|
1997-11-29 03:32:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(size_t *)p = size;
|
|
|
|
return p + sizeof(size_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
realloc(ptr, size)
|
|
|
|
void *ptr;
|
|
|
|
size_t size;
|
|
|
|
{
|
|
|
|
size_t old_size;
|
|
|
|
void *new_ptr;
|
|
|
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
return malloc(size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are far enough along, and if the memory originally came
|
|
|
|
* from the system malloc, we can use the system realloc.
|
|
|
|
*/
|
|
|
|
if (p_realloc != NULL && (char *)ptr >= rtld_alloc_lev)
|
|
|
|
return (*p_realloc)(ptr, size);
|
|
|
|
|
|
|
|
old_size = *((size_t *)ptr - 1);
|
|
|
|
if (old_size >= size) /* Not expanding the region */
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
new_ptr = malloc(size);
|
|
|
|
if (new_ptr != NULL)
|
|
|
|
memcpy(new_ptr, ptr, old_size);
|
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free(ptr)
|
|
|
|
void *ptr;
|
|
|
|
{
|
|
|
|
if (ptr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are far enough along, and if the memory originally came
|
|
|
|
* from the system malloc, we can use the system free. Otherwise
|
|
|
|
* we can't free the memory and we just let it go to waste.
|
|
|
|
*/
|
|
|
|
if (p_free != NULL && (char *)ptr >= rtld_alloc_lev)
|
|
|
|
(*p_free)(ptr);
|
|
|
|
}
|