char *key;
const char *key;
static struct rt_symbol	*enter_rts __P((char *, long, int, caddr_t,						long, struct so_map *));static void		maphints __P((void));static void		unmaphints __P((void));static void		preload __P((char *));static void		ld_trace __P((struct so_map *));static inline intstrcmp (register const char *s1, register const char *s2){	while (*s1 == *s2++)		if (*s1++ == 0)			return (0);	return (*(unsigned char *)s1 - *(unsigned char *)--s2);}#include "md-static-funcs.c"/* * Called from assembler stub that has set up crtp (passed from crt0) * and dp (our __DYNAMIC). */intrtld(version, crtp, dp)	int			version;	struct crt_ldso		*crtp;	struct _dynamic		*dp;{	int			n;	int			nreloc;		/* # of ld.so relocations */	struct relocation_info	*reloc;	struct so_debug		*ddp;	struct so_map		*smp;	/* Check version */	if (		version != CRT_VERSION_BSD_2 &&			version != CRT_VERSION_BSD_3 &&			version != CRT_VERSION_BSD_4 &&			version != CRT_VERSION_SUN)		return -1;	/* Fixup __DYNAMIC structure */	(long)dp->d_un.d_sdt += crtp->crt_ba;	/* Divide by hand to avoid possible use of library division routine */	for (	nreloc = 0, n = LD_RELSZ(dp);		n > 0;		n -= sizeof(struct relocation_info) ) nreloc++;		/* Relocate ourselves */	for (reloc = (struct relocation_info *)(LD_REL(dp) + crtp->crt_ba);	    nreloc; nreloc--, reloc++) {		register long	addr = reloc->r_address + crtp->crt_ba;		md_relocate_simple(reloc, crtp->crt_ba, addr);	}	if (version >= CRT_VERSION_BSD_4)		__progname = crtp->crt_ldso;	if (version >= CRT_VERSION_BSD_3)		main_progname = crtp->crt_prog;	/* 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);	if (careful) {		unsetenv("LD_LIBRARY_PATH");		unsetenv("LD_PRELOAD");	}	/* Setup directory search */	ld_library_path = getenv("LD_LIBRARY_PATH");	add_search_path(ld_library_path);	if (getenv("LD_NOSTD_PATH") == NULL)		std_search_path();	ld_suppress_warnings = getenv("LD_SUPPRESS_WARNINGS") != NULL;	ld_warn_non_pure_code = getenv("LD_WARN_NON_PURE_CODE") != NULL;	no_intern_search = careful || getenv("LD_NO_INTERN_SEARCH") != 0;	anon_open();	/*	 * Init object administration. We start off with a map description	 * for `main' and `rtld'.	 */	smp = alloc_link_map(main_progname, (struct sod *)0, (struct so_map *)0,					(caddr_t)0, 0, crtp->crt_dp);	LM_PRIVATE(smp)->spd_refcount++;	LM_PRIVATE(smp)->spd_flags |= RTLD_MAIN;	smp = alloc_link_map(us, (struct sod *)0, (struct so_map *)0,					(caddr_t)crtp->crt_ba, 0, dp);	LM_PRIVATE(smp)->spd_refcount++;	LM_PRIVATE(smp)->spd_flags |= RTLD_RTLD;	/* Handle LD_PRELOAD's here */	ld_preload_path = getenv("LD_PRELOAD");	if (ld_preload_path != NULL)		preload(ld_preload_path);	/* Load subsidiary objects into the process address space */	ld_tracing = (int)getenv("LD_TRACE_LOADED_OBJECTS");	load_subs(link_map_head);	if (ld_tracing) {		ld_trace(link_map_head);		exit(0);	}	init_maps(link_map_head);	/* Fill in some field in main's __DYNAMIC structure */	if (version >= CRT_VERSION_BSD_4)		crtp->crt_ldentry = &ld_entry;	else		crtp->crt_dp->d_entry = &ld_entry;	crtp->crt_dp->d_un.d_sdt->sdt_loaded = link_map_head->som_next;	ddp = crtp->crt_dp->d_debug;	ddp->dd_cc = rt_symbol_head;	if (ddp->dd_in_debugger) {		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) {			err(1, "Cannot set breakpoint (%s)", main_progname);		}		md_set_breakpoint((long)crtp->crt_bp, (long *)&ddp->dd_bpt_shadow);		if (mprotect(addr, PAGSIZ, PROT_READ|PROT_EXEC) == -1) {			err(1, "Cannot re-protect breakpoint (%s)",				main_progname);		}		ddp->dd_bpt_addr = crtp->crt_bp;		if (link_map_head)			ddp->dd_sym_loaded = 1;	}	/* Close the hints file */	unmaphints();	/* Close our file descriptor */	(void)close(crtp->crt_ldfd);	anon_close();	return 0;}static intload_subs(smp)	struct so_map	*smp;{	for (; smp; smp = smp->som_next) {		struct sod	*sodp;		long		next = 0;		if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)			continue;		if (smp->som_dynamic)			next = LD_NEED(smp->som_dynamic);		while (next) {			struct so_map	*newmap;			sodp = (struct sod *)(LM_LDBASE(smp) + next);			if ((newmap = map_object(sodp, smp)) == NULL) {				if (!ld_tracing) {					char *fmt = sodp->sod_library ?						"%s: lib%s.so.%d.%d" :						"%s: %s";					err(1, fmt, main_progname,						sodp->sod_name+LM_LDBASE(smp),						sodp->sod_major,						sodp->sod_minor);				}				newmap = alloc_link_map(NULL, sodp, smp,				    0, 0, 0);			}			LM_PRIVATE(newmap)->spd_refcount++;			next = sodp->sod_next;		}	}	return 0;}voidld_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;		}	}}/* * Allocate a new link map for shared object NAME loaded at ADDR as a * result of the presence of link object LOP in the link map PARENT. */static struct so_map *alloc_link_map(path, sodp, parent, addr, size, dp)	char		*path;	struct sod	*sodp;	struct so_map	*parent;	caddr_t		addr;	size_t		size;	struct _dynamic	*dp;{	struct so_map		*smp;	struct somap_private	*smpp;	smpp = (struct somap_private *)xmalloc(sizeof(struct somap_private));	smp = (struct so_map *)xmalloc(sizeof(struct so_map));	smp->som_next = NULL;	*link_map_tail = smp;	link_map_tail = &smp->som_next;	/*smp->som_sodbase = 0; NOT USED */	smp->som_write = 0;	smp->som_addr = addr;	smp->som_path = path?strdup(path):NULL;	smp->som_sod = sodp;	smp->som_dynamic = dp;	smp->som_spd = (caddr_t)smpp;/*XXX*/	if (addr == 0) main_map = smp;	smpp->spd_refcount = 0;	smpp->spd_flags = 0;	smpp->spd_parent = parent;	smpp->spd_size = size;#ifdef SUN_COMPAT	smpp->spd_offset =		(addr==0 && dp && dp->d_version==LD_VERSION_SUN) ? PAGSIZ : 0;#endif	return smp;}/* * Free the link map for an object being unmapped.  The link map * has already been removed from the link map list, so it can't be used * after it's been unmapped. */static voidfree_link_map(smp)	struct so_map   *smp;{	if ((LM_PRIVATE(smp)->spd_flags & RTLD_DL) != 0) {		/* free synthetic sod structure allocated in __dlopen() */		free((char *)smp->som_sod->sod_name);		free(smp->som_sod);	}	/* free the link map structure. */	free(smp->som_spd);	if (smp->som_path != NULL)		free(smp->som_path);	free(smp);}/* * Map object identified by link object SODP which was found * in link map SMP. */static struct so_map *map_object(sodp, smp)	struct sod	*sodp;	struct so_map	*smp;{	char		*name;	struct _dynamic	*dp;	char		*path, *ipath;	int		fd;	caddr_t		addr;	struct exec	hdr;	int		usehints = 0;	struct so_map	*p;	name = (char *)sodp->sod_name;	if (smp)		name += (long)LM_LDBASE(smp);	if (sodp->sod_library) {		usehints = 1;again:		if (smp == NULL || no_intern_search ||		    LD_PATHS(smp->som_dynamic) == 0) {			ipath = NULL;		} else {			ipath = LM_PATHS(smp);			add_search_path(ipath);		}		path = rtfindlib(name, sodp->sod_major,				 sodp->sod_minor, &usehints, ipath);		if (ipath)			remove_search_path(ipath);		if (path == NULL) {			errno = ENOENT;			return NULL;		}	} else {		if (careful && *name != '/') {			errno = EACCES;			return NULL;		}		path = name;	}	/* Check if already loaded */	for (p = link_map_head; p; p = p->som_next)		if (p->som_path && strcmp(p->som_path, path) == 0)			break;	if (p != NULL)		return p;	if ((fd = open(path, O_RDONLY, 0)) == -1) {		if (usehints) {			usehints = 0;			goto again;		}		return NULL;	}	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {		(void)close(fd);		/*errno = x;*/		return NULL;	}	if (N_BADMAG(hdr)) {		(void)close(fd);		errno = EFTYPE;		return NULL;	}	if ((addr = mmap(0, hdr.a_text + hdr.a_data + hdr.a_bss,	         PROT_READ|PROT_EXEC,	         MAP_COPY, fd, 0)) == (caddr_t)-1) {		(void)close(fd);		return NULL;	}#if DEBUG	xprintf("map1: 0x%x for 0x%x\n", addr, hdr.a_text + hdr.a_data + hdr.a_bss);#endif	if (mprotect(addr + hdr.a_text, hdr.a_data,	    PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {		(void)close(fd);		return NULL;	}	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) {		(void)close(fd);		return NULL;	}	(void)close(fd);	/* 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;	return alloc_link_map(path, sodp, smp, addr,	    hdr.a_text + hdr.a_data + hdr.a_bss, dp);}/* * Unmap a mapped object. */static voidunmap_object(smp)	struct so_map	*smp;{	struct so_map *p, **pp;	/* remove from link map list */	pp = &link_map_head;	while ((p = *pp) != NULL) {		if (p == smp)			break;		pp = &p->som_next;	}	if (p == NULL) {		warnx("warning: link map entry for %s not on link map list!",		    smp->som_path);		return;	}	*pp = smp->som_next;                    /* make list skip it */	if (link_map_tail == &smp->som_next)    /* and readjust tail pointer */		link_map_tail = pp;	/* unmap from address space */	(void)munmap(smp->som_addr, LM_PRIVATE(smp)->spd_size);}voidinit_maps(head)	struct so_map	*head;{	struct so_map	*smp;	/* Relocate all loaded objects according to their RRS segments */	for (smp = head; smp; smp = smp->som_next) {		if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)			continue;		reloc_map(smp);	}	/* Copy any relocated initialized data. */	for (smp = head; smp; smp = smp->som_next) {		if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)			continue;		reloc_copy(smp);	}	/* Call any object initialization routines. */	for (smp = head; smp; smp = smp->som_next) {		if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)			continue;		call_map(smp, ".init");		call_map(smp, "__init");	}}static inline voidcheck_text_reloc(r, smp, addr)	struct relocation_info	*r;	struct so_map		*smp;	caddr_t			addr;{	char	*sym;	if (addr >= LM_ETEXT(smp))		return;	if (RELOC_EXTERN_P(r))		sym = LM_STRINGS(smp) +				LM_SYMBOL(smp, RELOC_SYMBOL(r))->nz_strx;	else		sym = "";	if (ld_warn_non_pure_code && !ld_suppress_warnings)		warnx("warning: non pure code in %s at %x (%s)",				smp->som_path, r->r_address, sym);	if (smp->som_write == 0 &&		mprotect(smp->som_addr + LM_TXTADDR(smp),				LD_TEXTSZ(smp->som_dynamic),				PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {		err(1, "Cannot enable writes to %s:%s",					main_progname, smp->som_path);	}	smp->som_write = 1;}static voidreloc_map(smp)	struct so_map		*smp;{	struct _dynamic		*dp = smp->som_dynamic;	struct relocation_info	*r = LM_REL(smp);	struct relocation_info	*rend = r + LD_RELSZ(dp)/sizeof(*r);	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);	if (LD_PLTSZ(dp))		md_fix_jmpslot(LM_PLT(smp),				(long)LM_PLT(smp), (long)binder_entry);	for (; r < rend; r++) {		char	*sym;		caddr_t	addr = smp->som_addr + r->r_address;		check_text_reloc(r, smp, addr);		if (RELOC_EXTERN_P(r)) {			struct so_map	*src_map = NULL;			struct nzlist	*p, *np;			long	relocation = md_get_addend(r, addr);			if (RELOC_LAZY_P(r))				continue;			p = (struct nzlist *)				(symbolbase + symsize * RELOC_SYMBOL(r));			if (p->nz_type == (N_SETV + N_EXT))				src_map = smp;			sym = stringbase + p->nz_strx;			np = lookup(sym, &src_map, 0/*XXX-jumpslots!*/);			if (np == NULL)				errx(1, "Undefined symbol \"%s\" in %s:%s",					sym, main_progname, smp->som_path);			/*			 * Found symbol definition.			 * If it's in a link map, adjust value			 * according to the load address of that map.			 * Otherwise it's a run-time allocated common			 * whose value is already up-to-date.			 */			relocation += np->nz_value;			if (src_map)				relocation += (long)src_map->som_addr;			if (RELOC_PCREL_P(r))				relocation -= (long)smp->som_addr;			if (RELOC_COPY_P(r) && src_map) {				(void)enter_rts(sym,					(long)addr,					N_DATA + N_EXT,					src_map->som_addr + np->nz_value,					np->nz_size, src_map);				continue;			}			md_relocate(r, relocation, addr, 0);		} else {			md_relocate(r,#ifdef SUN_COMPAT				md_get_rt_segment_addend(r, addr)#else				md_get_addend(r, addr)#endif					+ (long)smp->som_addr, addr, 0);		}	}	if (smp->som_write) {		if (mprotect(smp->som_addr + LM_TXTADDR(smp),				LD_TEXTSZ(smp->som_dynamic),				PROT_READ|PROT_EXEC) == -1) {			err(1, "Cannot disable writes to %s:%s",						main_progname, smp->som_path);		}		smp->som_write = 0;	}}static voidreloc_copy(smp)	struct so_map		*smp;{	struct rt_symbol	*rtsp;	for (rtsp = rt_symbol_head; rtsp; rtsp = rtsp->rt_next)		if ((rtsp->rt_smp == NULL || rtsp->rt_smp == smp) &&				rtsp->rt_sp->nz_type == N_DATA + N_EXT) {			bcopy(rtsp->rt_srcaddr, (caddr_t)rtsp->rt_sp->nz_value,							rtsp->rt_sp->nz_size);		}}static voidcall_map(smp, sym)	struct so_map		*smp;	char			*sym;{	struct so_map		*src_map = smp;	struct nzlist		*np;	np = lookup(sym, &src_map, 1);	if (np)		(*(void (*) __P((void)))(src_map->som_addr + np->nz_value))();}/* * Run-time common symbol table. */#define RTC_TABSIZE		57static struct rt_symbol 	*rt_symtab[RTC_TABSIZE];/* * Compute hash value for run-time symbol table */static inline inthash_string(key)	char *key;{	register char *cp;	register int k;	cp = key;	k = 0;	while (*cp)		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;	return k;}/* * Lookup KEY in the run-time common symbol table. */static inline struct rt_symbol *lookup_rts(key)	char *key;{	register int			hashval;	register struct rt_symbol	*rtsp;	/* Determine which bucket.  */	hashval = hash_string(key) % RTC_TABSIZE;	/* Search the bucket.  */	for (rtsp = rt_symtab[hashval]; rtsp; rtsp = rtsp->rt_link)		if (strcmp(key, rtsp->rt_sp->nz_name) == 0)			return rtsp;	return NULL;}static struct rt_symbol *enter_rts(name, value, type, srcaddr, size, smp)	char		*name;	long		value;	int		type;	caddr_t		srcaddr;	long		size;	struct so_map	*smp;{	register int			hashval;	register struct rt_symbol	*rtsp, **rpp;	/* Determine which bucket */	hashval = hash_string(name) % RTC_TABSIZE;	/* Find end of bucket */	for (rpp = &rt_symtab[hashval]; *rpp; rpp = &(*rpp)->rt_link)		;	/* Allocate new common symbol */	rtsp = (struct rt_symbol *)malloc(sizeof(struct rt_symbol));	rtsp->rt_sp = (struct nzlist *)malloc(sizeof(struct nzlist));	rtsp->rt_sp->nz_name = strdup(name);	rtsp->rt_sp->nz_value = value;	rtsp->rt_sp->nz_type = type;	rtsp->rt_sp->nz_size = size;	rtsp->rt_srcaddr = srcaddr;	rtsp->rt_smp = smp;	rtsp->rt_link = NULL;	/* Link onto linear list as well */	rtsp->rt_next = rt_symbol_head;	rt_symbol_head = rtsp;	*rpp = rtsp;	return rtsp;}