The a.out version of ld.so now lives in `src/libexec/ld.aout_so'

This commit is contained in:
pk 1998-12-17 11:50:58 +00:00
parent cec94710e3
commit 9f709b3e8b
6 changed files with 0 additions and 2960 deletions

View File

@ -1,43 +0,0 @@
# $NetBSD: Makefile,v 1.25 1998/08/13 07:34:06 mycroft Exp $
.if (${MACHINE_ARCH} == "i386") || \
(${MACHINE_ARCH} == "m68k") || \
(${MACHINE_ARCH} == "ns32k") || \
(${MACHINE_ARCH} == "sparc") || \
(${MACHINE_ARCH} == "vax") || \
(${MACHINE_ARCH} == "arm32")
PROG= ld.so
SRCS= mdprologue.S rtld.c malloc.c shlib.c etc.c md.c vfprintf.c
CLIB= ${.CURDIR}/../../../../lib/libc
CLIBOBJ!=cd ${CLIB}; \
printf "xxx: .MAKE\n\t@echo \$${.OBJDIR}\n" | ${MAKE} -s -f-
PICFLAG=-fpic -fno-function-cse
CPPFLAGS+=$(PICFLAG) -DRTLD -DLIBC_SCCS -I${CLIB}/include
.if defined(DEBUG)
CPPFLAGS+=-DDEBUG
.endif
ASFLAGS+=-k
LDFLAGS+=-Bshareable -Bsymbolic -assert nosymbolic -L${CLIBOBJ}
.if defined(DESTDIR)
LDFLAGS+= -nostdlib -L${DESTDIR}/usr/lib
.endif
LDADD+= -lc_pic
DPADD+= ${LIBC_PIC}
BINDIR= /usr/libexec
HDRS= link.h
INCS=${HDRS}
INCSDIR=/usr/include
.PATH: ${CLIB}/stdio
$(PROG):
$(LD) -o $(PROG) $(LDFLAGS) $(OBJS) $(LDADD)
.S.o:
${CPP} ${.IMPSRC} | ${AS} ${ASFLAGS} -o ${.TARGET} -
.endif
MAN= rtld.1
MLINKS= rtld.1 ld.so.1
.include <bsd.prog.mk>

View File

@ -1,296 +0,0 @@
/* $NetBSD: link.h,v 1.16 1998/12/15 21:28:28 pk Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* 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 the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* RRS section definitions.
*
* The layout of some data structures defined in this header file is
* such that we can provide compatibility with the SunOS 4.x shared
* library scheme.
*/
#ifndef _LINK_H_
#define _LINK_H_
#include <dlfcn.h> /* for Dl_info */
#include <a.out.h> /* for struct nlist */
/*
* A `Shared Object Descriptor' descibes a shared object that is needed
* to complete the link edit process of the object containing it.
* A list of such objects (chained through `sod_next') is pointed at
* by `sdt_sods' in the section_dispatch_table structure.
*/
struct sod { /* Shared Object Descriptor */
long sod_name; /* name (relative to load address) */
u_int sod_library : 1, /* Searched for by library rules */
sod_reserved : 31;
short sod_major; /* major version number */
short sod_minor; /* minor version number */
long sod_next; /* next sod */
};
/*
* `Shared Object Map's are used by the run-time link editor (ld.so) to
* keep track of all shared objects loaded into a process' address space.
* These structures are only used at run-time and do not occur within
* the text or data segment of an executable or shared library.
*/
struct so_map { /* Shared Object Map */
caddr_t som_addr; /* Address at which object mapped */
char *som_path; /* Path to mmap'ed file */
struct so_map *som_next; /* Next map in chain */
struct sod *som_sod; /* Sod responsible for this map */
caddr_t som_sodbase; /* Base address of this sod */
u_int som_write : 1; /* Text is currently writable */
struct _dynamic *som_dynamic; /* _dynamic structure */
caddr_t som_spd; /* Private data */
};
/*
* Symbol description with size. This is simply an `nlist' with
* one field (nz_size) added.
* Used to convey size information on items in the data segment
* of shared objects. An array of these live in the shared object's
* text segment and is addressed by the `sdt_nzlist' field.
*/
struct nzlist {
struct nlist nlist;
u_long nz_size;
#define nz_un nlist.n_un
#define nz_strx nlist.n_un.n_strx
#define nz_name nlist.n_un.n_name
#define nz_type nlist.n_type
#define nz_value nlist.n_value
#define nz_desc nlist.n_desc
#define nz_other nlist.n_other
};
#define N_AUX(p) ((p)->n_other & 0xf)
#define N_BIND(p) (((unsigned int)(p)->n_other >> 4) & 0xf)
#define N_OTHER(r, v) (((unsigned int)(r) << 4) | ((v) & 0xf))
#define AUX_OBJECT 1
#define AUX_FUNC 2
#define AUX_LABEL 3
/*#define BIND_LOCAL 0 not used */
/*#define BIND_GLOBAL 1 not used */
#define BIND_WEAK 2
/*
* The `section_dispatch_table' structure contains offsets to various data
* structures needed to do run-time relocation.
*/
struct section_dispatch_table {
struct so_map *sdt_loaded; /* List of loaded objects */
long sdt_sods; /* List of shared objects descriptors */
long sdt_paths; /* Library search paths */
long sdt_got; /* Global offset table */
long sdt_plt; /* Procedure linkage table */
long sdt_rel; /* Relocation table */
long sdt_hash; /* Symbol hash table */
long sdt_nzlist; /* Symbol table itself */
long sdt_filler2; /* Unused (was: stab_hash) */
long sdt_buckets; /* Number of hash buckets */
long sdt_strings; /* Symbol strings */
long sdt_str_sz; /* Size of symbol strings */
long sdt_text_sz; /* Size of text area */
long sdt_plt_sz; /* Size of procedure linkage table */
};
/*
* RRS symbol hash table, addressed by `sdt_hash' in section_dispatch_table.
* Used to quickly lookup symbols of the shared object by hashing
* on the symbol's name. `rh_symbolnum' is the index of the symbol
* in the shared object's symbol list (`sdt_nzlist'), `rh_next' is
* the next symbol in the hash bucket (in case of collisions).
*/
struct rrs_hash {
int rh_symbolnum; /* Symbol number */
int rh_next; /* Next hash entry */
};
/*
* `rt_symbols' is used to keep track of run-time allocated commons
* and data items copied from shared objects.
*/
struct rt_symbol {
struct nzlist *rt_sp; /* The symbol */
struct rt_symbol *rt_next; /* Next in linear list */
struct rt_symbol *rt_link; /* Next in bucket */
caddr_t rt_srcaddr; /* Address of "master" copy */
struct so_map *rt_smp; /* Originating map */
};
/*
* Debugger interface structure.
*/
struct so_debug {
int dd_version; /* Version # of interface */
int dd_in_debugger; /* Set when run by debugger */
int dd_sym_loaded; /* Run-time linking brought more
symbols into scope */
char *dd_bpt_addr; /* Address of rtld-generated bpt */
int dd_bpt_shadow; /* Original contents of bpt */
struct rt_symbol *dd_cc; /* Allocated commons/copied data */
};
/*
* Entry points into ld.so - user interface to the run-time linker.
*/
struct ld_entry {
void *(*dlopen) __P((const char *, int));
int (*dlclose) __P((void *));
void *(*dlsym) __P((void *, const char *));
int (*dlctl) __P((void *, int, void *));
void (*dlexit) __P((void));
int (*dladdr) __P((void *, Dl_info *));
void (*dlrsrvd[2]) __P((void));
};
/*
* This is the structure pointed at by the __DYNAMIC symbol if an
* executable requires the attention of the run-time link editor.
* __DYNAMIC is given the value zero if no run-time linking needs to
* be done (it is always present in shared objects).
* The union `d_un' provides for different versions of the dynamic
* linking mechanism (switched on by `d_version'). The last version
* used by Sun is 3. We leave some room here and go to version number
* 8 for NetBSD, the main difference lying in the support for the
* `nz_list' type of symbols.
*/
struct _dynamic {
int d_version; /* version # of this interface */
struct so_debug *d_debug;
union {
struct section_dispatch_table *d_sdt;
} d_un;
struct ld_entry *d_entry; /* compat - now in crt_ldso */
};
#define LD_VERSION_SUN (3)
#define LD_VERSION_BSD (8)
#define LD_VERSION_NZLIST_P(v) ((v) >= 8)
#define LD_GOT(x) ((x)->d_un.d_sdt->sdt_got)
#define LD_PLT(x) ((x)->d_un.d_sdt->sdt_plt)
#define LD_REL(x) ((x)->d_un.d_sdt->sdt_rel)
#define LD_SYMBOL(x) ((x)->d_un.d_sdt->sdt_nzlist)
#define LD_HASH(x) ((x)->d_un.d_sdt->sdt_hash)
#define LD_STRINGS(x) ((x)->d_un.d_sdt->sdt_strings)
#define LD_NEED(x) ((x)->d_un.d_sdt->sdt_sods)
#define LD_BUCKETS(x) ((x)->d_un.d_sdt->sdt_buckets)
#define LD_PATHS(x) ((x)->d_un.d_sdt->sdt_paths)
#define LD_GOTSZ(x) ((x)->d_un.d_sdt->sdt_plt - (x)->d_un.d_sdt->sdt_got)
#define LD_RELSZ(x) ((x)->d_un.d_sdt->sdt_hash - (x)->d_un.d_sdt->sdt_rel)
#define LD_HASHSZ(x) ((x)->d_un.d_sdt->sdt_nzlist - (x)->d_un.d_sdt->sdt_hash)
#define LD_STABSZ(x) ((x)->d_un.d_sdt->sdt_strings - (x)->d_un.d_sdt->sdt_nzlist)
#define LD_PLTSZ(x) ((x)->d_un.d_sdt->sdt_plt_sz)
#define LD_STRSZ(x) ((x)->d_un.d_sdt->sdt_str_sz)
#define LD_TEXTSZ(x) ((x)->d_un.d_sdt->sdt_text_sz)
/*
* Interface to ld.so
*/
struct crt_ldso {
int crt_ba; /* Base address of ld.so */
int crt_dzfd; /* "/dev/zero" file decriptor (SunOS) */
int crt_ldfd; /* ld.so file descriptor */
struct _dynamic *crt_dp; /* Main's __DYNAMIC */
char **crt_ep; /* environment strings */
caddr_t crt_bp; /* Breakpoint if run from debugger */
char *crt_prog; /* Program name (v3) */
char *crt_ldso; /* Link editor name (v4) */
struct ld_entry *crt_ldentry; /* dl*() access (v4) */
};
/*
* Version passed from crt0 to ld.so (1st argument to _rtld()).
*/
#define CRT_VERSION_SUN 1
#define CRT_VERSION_BSD_2 2
#define CRT_VERSION_BSD_3 3
#define CRT_VERSION_BSD_4 4
/*
* Maximum number of recognized shared object version numbers.
*/
#define MAXDEWEY 8
/*
* Header of the hints file.
*/
struct hints_header {
long hh_magic;
#define HH_MAGIC 011421044151
long hh_version; /* Interface version number */
#define LD_HINTS_VERSION_1 1
#define LD_HINTS_VERSION_2 2
long hh_hashtab; /* Location of hash table */
long hh_nbucket; /* Number of buckets in hashtab */
long hh_strtab; /* Location of strings */
long hh_strtab_sz; /* Size of strings */
long hh_ehints; /* End of hints (max offset in file) */
long hh_dirlist; /* Colon-separated list of srch dirs */
};
#define HH_BADMAG(hdr) ((hdr).hh_magic != HH_MAGIC)
/*
* Hash table element in hints file.
*/
struct hints_bucket {
/* namex and pathx are indices into the string table */
int hi_namex; /* Library name */
int hi_pathx; /* Full path */
int hi_dewey[MAXDEWEY]; /* The versions */
int hi_ndewey; /* Number of version numbers */
#define hi_major hi_dewey[0]
#define hi_minor hi_dewey[1]
int hi_next; /* Next in this bucket */
};
#define _PATH_LD_HINTS "/var/run/ld.so.hints"
#endif /* _LINK_H_ */

View File

@ -1,483 +0,0 @@
/* $NetBSD: malloc.c,v 1.5 1998/12/15 21:33:00 pk Exp $ */
/*
* Copyright (c) 1983 Regents of the University of California.
* 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 the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";
#else
__RCSID("$NetBSD: malloc.c,v 1.5 1998/12/15 21:33:00 pk Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
/*
* malloc.c (Caltech) 2/21/82
* Chris Kingsley, kingsley@cit-20.
*
* This is a very fast storage allocator. It allocates blocks of a small
* number of different sizes, and keeps free lists of each size. Blocks that
* don't exactly fit are passed up to the next larger size. In this
* implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
* This is designed for use in a virtual memory environment.
*/
#include <sys/types.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/mman.h>
#ifndef BSD
#define MAP_COPY MAP_PRIVATE
#define MAP_FILE 0
#define MAP_ANON 0
#endif
#ifndef BSD /* Need do better than this */
#define NEED_DEV_ZERO 1
#endif
#define NULL 0
/*
* The overhead on a block is at least 4 bytes. When free, this space
* contains a pointer to the next free block, and the bottom two bits must
* be zero. When in use, the first byte is set to MAGIC, and the second
* byte is the size index. The remaining bytes are for alignment.
* If range checking is enabled then a second word holds the size of the
* requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
* The order of elements is critical: ov_magic must overlay the low order
* bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
*/
union overhead {
union overhead *ov_next; /* when free */
struct {
u_char ovu_magic; /* magic number */
u_char ovu_index; /* bucket # */
#ifdef RCHECK
u_short ovu_rmagic; /* range magic number */
u_int ovu_size; /* actual block size */
#endif
} ovu;
#define ov_magic ovu.ovu_magic
#define ov_index ovu.ovu_index
#define ov_rmagic ovu.ovu_rmagic
#define ov_size ovu.ovu_size
};
#define MAGIC 0xef /* magic # on accounting info */
#define RMAGIC 0x5555 /* magic # on range info */
#ifdef RCHECK
#define RSLOP sizeof (u_short)
#else
#define RSLOP 0
#endif
/*
* Pre-allocate mmap'ed pages
*/
#define NPOOLPAGES (32*1024/pagesz)
static caddr_t pagepool_start, pagepool_end;
static int morepages __P((int));
static void morecore __P((int));
static int findbucket __P((union overhead *, int));
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
* smallest allocatable block is 8 bytes. The overhead information
* precedes the data area returned to the user.
*/
#define NBUCKETS 30
static union overhead *nextf[NBUCKETS];
static int pagesz; /* page size */
static int pagebucket; /* page size bucket */
#ifdef MSTATS
/*
* nmalloc[i] is the difference between the number of mallocs and frees
* for a given block size.
*/
static u_int nmalloc[NBUCKETS];
#include <stdio.h>
#endif
#if defined(DEBUG) || defined(RCHECK)
#define ASSERT(p) if (!(p)) botch("p")
#include <stdio.h>
static
botch(s)
char *s;
{
fprintf(stderr, "\r\nassertion botched: %s\r\n", s);
(void) fflush(stderr); /* just in case user buffered it */
abort();
}
#else
#define ASSERT(p)
#endif
void *
malloc(nbytes)
size_t nbytes;
{
register union overhead *op;
register int bucket, n;
register unsigned amt;
/*
* First time malloc is called, setup page size and
* align break pointer so all data will be page aligned.
*/
if (pagesz == 0) {
pagesz = n = getpagesize();
if (morepages(NPOOLPAGES) == 0)
return NULL;
op = (union overhead *)(pagepool_start);
n = n - sizeof (*op) - ((int)op & (n - 1));
if (n < 0)
n += pagesz;
if (n) {
pagepool_start += n;
}
bucket = 0;
amt = 8;
while (pagesz > amt) {
amt <<= 1;
bucket++;
}
pagebucket = bucket;
}
/*
* Convert amount of memory requested into closest block size
* stored in hash buckets which satisfies request.
* Account for space used per block for accounting.
*/
if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
#ifndef RCHECK
amt = 8; /* size of first bucket */
bucket = 0;
#else
amt = 16; /* size of first bucket */
bucket = 1;
#endif
n = -(sizeof (*op) + RSLOP);
} else {
amt = pagesz;
bucket = pagebucket;
}
while (nbytes > amt + n) {
amt <<= 1;
if (amt == 0)
return (NULL);
bucket++;
}
/*
* If nothing in hash bucket right now,
* request more memory from the system.
*/
if ((op = nextf[bucket]) == NULL) {
morecore(bucket);
if ((op = nextf[bucket]) == NULL)
return (NULL);
}
/* remove from linked list */
nextf[bucket] = op->ov_next;
op->ov_magic = MAGIC;
op->ov_index = bucket;
#ifdef MSTATS
nmalloc[bucket]++;
#endif
#ifdef RCHECK
/*
* Record allocated size of block and
* bound space with magic numbers.
*/
op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
op->ov_rmagic = RMAGIC;
*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
return ((char *)(op + 1));
}
/*
* Allocate more memory to the indicated bucket.
*/
static void
morecore(bucket)
int bucket;
{
register union overhead *op;
register int sz; /* size of desired block */
int amt; /* amount to allocate */
int nblks; /* how many blocks we get */
/* Map bucket number to size */
sz = 1 << (bucket + 3);
#ifdef DEBUG
ASSERT(sz > 0);
#else
if (sz <= 0)
return;
#endif
if (sz < pagesz) {
amt = pagesz;
nblks = amt / sz;
} else {
amt = sz + pagesz;
nblks = 1;
}
if (amt > pagepool_end - pagepool_start)
if (morepages(amt/pagesz + NPOOLPAGES) == 0)
return;
op = (union overhead *)pagepool_start;
pagepool_start += amt;
/*
* Add new memory allocated to that on
* free list for this hash bucket.
*/
nextf[bucket] = op;
while (--nblks > 0) {
op->ov_next = (union overhead *)((caddr_t)op + sz);
op = (union overhead *)((caddr_t)op + sz);
}
}
void
free(cp)
void *cp;
{
register int size;
register union overhead *op;
if (cp == NULL)
return;
op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
#ifdef DEBUG
ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
#else
if (op->ov_magic != MAGIC)
return; /* sanity */
#endif
#ifdef RCHECK
ASSERT(op->ov_rmagic == RMAGIC);
ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
#endif
size = op->ov_index;
ASSERT(size < NBUCKETS);
op->ov_next = nextf[size]; /* also clobbers ov_magic */
nextf[size] = op;
#ifdef MSTATS
nmalloc[size]--;
#endif
}
/*
* When a program attempts "storage compaction" as mentioned in the
* old malloc man page, it realloc's an already freed block. Usually
* this is the last block it freed; occasionally it might be farther
* back. We have to search all the free lists for the block in order
* to determine its bucket: 1st we make one pass thru the lists
* checking only the first block in each; if that fails we search
* ``realloc_srchlen'' blocks in each list for a match (the variable
* is extern so the caller can modify it). If that fails we just copy
* however many bytes was given to realloc() and hope it's not huge.
*/
int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
void *
realloc(cp, nbytes)
void *cp;
size_t nbytes;
{
register u_int onb;
register int i;
union overhead *op;
char *res;
int was_alloced = 0;
if (cp == NULL)
return (malloc(nbytes));
op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
if (op->ov_magic == MAGIC) {
was_alloced++;
i = op->ov_index;
} else {
/*
* Already free, doing "compaction".
*
* Search for the old block of memory on the
* free list. First, check the most common
* case (last element free'd), then (this failing)
* the last ``realloc_srchlen'' items free'd.
* If all lookups fail, then assume the size of
* the memory block being realloc'd is the
* largest possible (so that all "nbytes" of new
* memory are copied into). Note that this could cause
* a memory fault if the old area was tiny, and the moon
* is gibbous. However, that is very unlikely.
*/
if ((i = findbucket(op, 1)) < 0 &&
(i = findbucket(op, realloc_srchlen)) < 0)
i = NBUCKETS;
}
onb = 1 << (i + 3);
if (onb < pagesz)
onb -= sizeof (*op) + RSLOP;
else
onb += pagesz - sizeof (*op) - RSLOP;
/* avoid the copy if same size block */
if (was_alloced) {
if (i) {
i = 1 << (i + 2);
if (i < pagesz)
i -= sizeof (*op) + RSLOP;
else
i += pagesz - sizeof (*op) - RSLOP;
}
if (nbytes <= onb && nbytes > i) {
#ifdef RCHECK
op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
return(cp);
} else
free(cp);
}
if ((res = malloc(nbytes)) == NULL)
return (NULL);
if (cp != res) /* common optimization if "compacting" */
bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
return (res);
}
/*
* Search ``srchlen'' elements of each free list for a block whose
* header starts at ``freep''. If srchlen is -1 search the whole list.
* Return bucket number, or -1 if not found.
*/
static int
findbucket(freep, srchlen)
union overhead *freep;
int srchlen;
{
register union overhead *p;
register int i, j;
for (i = 0; i < NBUCKETS; i++) {
j = 0;
for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
if (p == freep)
return (i);
j++;
}
}
return (-1);
}
#ifdef MSTATS
/*
* mstats - print out statistics about malloc
*
* Prints two lines of numbers, one showing the length of the free list
* for each size category, the second showing the number of mallocs -
* frees for each size category.
*/
mstats(s)
char *s;
{
register int i, j;
register union overhead *p;
int totfree = 0,
totused = 0;
fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
for (i = 0; i < NBUCKETS; i++) {
for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
;
fprintf(stderr, " %d", j);
totfree += j * (1 << (i + 3));
}
fprintf(stderr, "\nused:\t");
for (i = 0; i < NBUCKETS; i++) {
fprintf(stderr, " %d", nmalloc[i]);
totused += nmalloc[i] * (1 << (i + 3));
}
fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
totused, totfree);
}
#endif
static int
morepages(n)
int n;
{
int fd = -1;
int offset;
#ifdef NEED_DEV_ZERO
fd = open("/dev/zero", O_RDWR, 0);
if (fd == -1)
perror("/dev/zero");
#endif
if (pagepool_end - pagepool_start > pagesz) {
caddr_t addr = (caddr_t)
(((int)pagepool_start + pagesz - 1) & ~(pagesz - 1));
if (munmap(addr, pagepool_end - addr) != 0)
warn("morepages: munmap %p", addr);
}
offset = (int)pagepool_start - ((int)pagepool_start & ~(pagesz - 1));
if ((pagepool_start = mmap(0, n * pagesz,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) {
char *str = "ld.so: malloc: cannot map pages\n";
(void)write(2, str, strlen(str));
return 0;
}
pagepool_end = pagepool_start + n * pagesz;
pagepool_start += offset;
#ifdef NEED_DEV_ZERO
close(fd);
#endif
return n;
}

View File

@ -1,41 +0,0 @@
/* $NetBSD: md-prologue.c,v 1.2 1996/07/03 03:31:56 thorpej Exp $ */
/*
* rtld entry pseudo code - turn into assembler and tweak it
*/
#include <sys/types.h>
#include <sys/types.h>
#include <a.out.h>
#include "link.h"
#include "md.h"
extern long _GOT_[];
extern void (*rtld)();
extern void (*binder())();
void
rtld_entry(version, crtp)
int version;
struct crt *crtp;
{
register struct link_dynamic *dp;
register void (*f)();
/* __DYNAMIC is first entry in GOT */
dp = (struct link_dynamic *) (_GOT_[0]+crtp->crt_ba);
f = (void (*)())((long)rtld + crtp->crt_ba);
(*f)(version, crtp, dp);
}
void
binder_entry()
{
extern int PC;
struct jmpslot *sp;
void (*func)();
func = binder(PC, sp->reloc_index & 0x003fffff);
(*func)();
}

View File

@ -1,221 +0,0 @@
.\" $NetBSD: rtld.1,v 1.7 1998/09/05 13:08:41 pk Exp $
.\"
.\" Copyright (c) 1998 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Paul Kranenburg.
.\"
.\" 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 the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 27, 1995
.Dt RTLD 1
.Os NetBSD
.Sh NAME
.Nm ld.so
.Nd run-time link-editor
.Sh DESCRIPTION
.Nm
is a self-contained, position independent program image providing run-time
support for loading and link-editing shared objects into a process'
address space. It uses the data structures
.Po
see
.Xr link 5
.Pc
contained within dynamically linked programs to determine which shared
libraries are needed and loads them at a convenient virtual address
using the
.Xr mmap 2
system call.
.Pp
After all shared libraries have been succesfully loaded,
.Nm
proceeds to resolve external references from both the main program and
all objects loaded. A mechanism is provided for initialisation routines
to be called, on a per-object basis, giving a shared object an opportunity
to perform any extra set-up, before execution of the program proper begins.
.Nm
looks for a symbol named
.Em .init
in each object's symbol table. If present, this symbol is assumed to
represent a C-function declared as
.Ft void
.Fn .init "void" ,
which is then called. Similarly, a
.Ft void
.Fn .fini "void"
function is called just before an object is unloaded from the process
address space as a result of calling
.Xr dlclose 3 .
Note that while an object's
.Em .init
is always called, whether the object is loaded automatically at program
startup or programatically by using
.Xr dlopen 3 ,
the
.Em .fini
function is called only on
.Sq last Xr dlclose 3 .
.Pp
This mechanism is exploited by the system-supplied C++ constructor
initialization code located in
.Pa /usr/lib/c++rt.o .
This file should be included in the list of object-code files passed to
.Xr ld 1
when building a shared C++ library.
.Pp
.Nm
is itself a shared object that is initially loaded by the startup module
.Em crt0 .
Since
.Xr a.out 5
formats do not provide easy access to the file header from within a running
process,
.Em crt0
uses the special symbol
.Va _DYNAMIC
to determine whether a program is in fact dynamically linked or not. Whenever
the linker
.Xr ld 1
has relocated this symbol to a location other than 0,
.Em crt0
assumes the services of
.Nm
are needed
.Po
see
.Xr link 5
for details
.Pc Ns \&.
.Em crt0
passes control to
.Nm rtld Ns 's
entry point before the program's
.Fn main
routine is called. Thus,
.Nm
can complete the link-editing process before the dynamic program calls upon
services of any dynamic library.
.Pp
To quickly locate the required shared objects in the filesystem,
.Nm
may use a
.Dq hints
file, prepared by the
.Xr ldconfig 8
utility, in which the full path specification of the shared objects can be
looked up by hashing on the 3-tuple
.Ao
library-name, major-version-number, minor-version-number
.Ac Ns \&.
.Pp
.Nm
recognises a number of environment variables that can be used to modify
its behaviour as follows:
.Pp
.Bl -tag -width "LD_TRACE_LOADED_OBJECTS_PROGNAME"
.It Ev LD_LIBRARY_PATH
A colon separated list of directories, overriding the default search path
for shared libraries.
.It Ev LD_WARN_NON_PURE_CODE
When set, issue a warning whenever a link-editing operation requires
modification of the text segment of some loaded object. This is usually
indicative of an incorrectly built library.
.It Ev LD_SUPPRESS_WARNINGS
When set, no warning messages of any kind are issued. Normally, a warning
is given if satisfactorily versioned library could not be found.
.It Ev LD_TRACE_LOADED_OBJECTS
When set, causes
.Nm
to exit after loading the shared objects and printing a summary which includes
the absolute pathnames of all objects, to standard output.
.It Ev LD_TRACE_LOADED_OBJECTS_FMT1
.It Ev LD_TRACE_LOADED_OBJECTS_FMT2
When set, these variables are interpreted as format strings a la
.Xr printf 3
to customize the trace output and are used by
.Xr ldd 1 's
.Fl f
option and allows
.Xr ldd 1
to be operated as a filter more conveniently.
The following conversions can be used:
.Bl -tag -indent "LD_TRACE_LOADED_OBJECTS_FMT1 " -width "xxxx"
.It \&%a
The main program's name
.Po also known as
.Dq __progname
.Pc .
.It \&%A
The value of the environment variable
.Ev LD_TRACE_LOADED_OBJECTS_PROGNAME
.It \&%o
The libary name.
.It \&%m
The library's major version number.
.It \&%n
The library's minor version number.
.It \&%p
The full pathname as determined by
.Nm rtld Ns 's
library search rules.
.It \&%x
The library's load address.
.El
.Pp
Additionally,
.Sy \en
and
.Sy \et
are recognised and have their usual meaning.
.It Ev LD_NO_INTERN_SEARCH
When set,
.Nm
does not process any internal search paths that were recorded in the
executable.
.It Ev LD_NOSTD_PATH
When set, do not include a set of built-in standard directory paths for
searching. This might be useful when running on a system with a completely
non-standard filesystem layout.
.El
.Pp
.Sh FILES
.Bl -tag -width /var/run/ld.so.hintsXXX -compact
.It Pa /var/run/ld.so.hints
library location hints built by
.Xr ldconfig 8
.El
.Pp
.Sh SEE ALSO
.Xr ld 1 ,
.Xr link 5 ,
.Xr ldconfig 8
.Sh HISTORY
The shared library model employed first appeared in SunOS 4.0.

File diff suppressed because it is too large Load Diff