Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.c revision 1.22
      1 /*	$NetBSD: rtld.c,v 1.22 1999/08/01 19:47:07 kleink Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/param.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include <ctype.h>
     53 
     54 #include <dlfcn.h>
     55 #include "debug.h"
     56 #include "rtld.h"
     57 
     58 #if !defined(lint)
     59 #include "sysident.h"
     60 #endif
     61 
     62 /*
     63  * Debugging support.
     64  */
     65 
     66 typedef void    (*funcptr) __P((void));
     67 
     68 /*
     69  * Function declarations.
     70  */
     71 static void     _rtld_init __P((caddr_t));
     72 static void     _rtld_exit __P((void));
     73 
     74 Elf_Addr        _rtld __P((Elf_Word *));
     75 
     76 
     77 /*
     78  * Data declarations.
     79  */
     80 static char    *error_message;	/* Message for dlopen(), or NULL */
     81 
     82 struct r_debug  _rtld_debug;	/* for GDB; */
     83 bool            _rtld_trust;	/* False for setuid and setgid programs */
     84 Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
     85 Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
     86 Obj_Entry      *_rtld_objmain;	/* The main program shared object */
     87 Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
     88 char            _rtld_path[] = _PATH_RTLD;
     89 #ifdef	VARPSZ
     90 int		_rtld_pagesz;	/* Page size, as provided by kernel */
     91 #endif
     92 
     93 Search_Path    *_rtld_default_paths;
     94 Search_Path    *_rtld_paths;
     95 /*
     96  * Global declarations normally provided by crt0.
     97  */
     98 char           *__progname;
     99 char          **environ;
    100 
    101 #ifdef OLD_GOT
    102 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    103 #else
    104 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    105 extern Elf_Dyn  _DYNAMIC;
    106 #endif
    107 
    108 static void _rtld_call_fini_functions __P((Obj_Entry *));
    109 static void _rtld_call_init_functions __P((Obj_Entry *));
    110 static Obj_Entry *_rtld_dlcheck __P((void *));
    111 static void _rtld_unref_object_dag __P((Obj_Entry *));
    112 
    113 static void
    114 _rtld_call_fini_functions(first)
    115 	Obj_Entry *first;
    116 {
    117 	Obj_Entry *obj;
    118 
    119 	for (obj = first; obj != NULL; obj = obj->next)
    120 		if (obj->fini != NULL)
    121 			(*obj->fini)();
    122 }
    123 
    124 static void
    125 _rtld_call_init_functions(first)
    126 	Obj_Entry *first;
    127 {
    128 	if (first != NULL) {
    129 		_rtld_call_init_functions(first->next);
    130 		if (first->init != NULL)
    131 			(*first->init)();
    132 	}
    133 }
    134 
    135 /*
    136  * Initialize the dynamic linker.  The argument is the address at which
    137  * the dynamic linker has been mapped into memory.  The primary task of
    138  * this function is to relocate the dynamic linker.
    139  */
    140 static void
    141 _rtld_init(mapbase)
    142 	caddr_t mapbase;
    143 {
    144 	Obj_Entry objself;/* The dynamic linker shared object */
    145 #ifdef RTLD_RELOCATE_SELF
    146 	int dodebug = false;
    147 #else
    148 	int dodebug = true;
    149 #endif
    150 
    151 	memset(&objself, 0, sizeof objself);
    152 
    153 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
    154 	objself.path = NULL;
    155 	objself.rtld = true;
    156 	objself.mapbase = mapbase;
    157 
    158 #if defined(__mips__)
    159 	/*
    160 	* mips and ld.so currently linked at load address,
    161 	* so no relocation needed
    162 	*/
    163 	objself.relocbase = 0;
    164 #else
    165 	objself.relocbase = mapbase;
    166 #endif
    167 
    168 	objself.pltgot = NULL;
    169 
    170 #ifdef OLD_GOT
    171 	objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
    172 #else
    173 	objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
    174 #endif
    175 
    176 #ifdef RTLD_RELOCATE_SELF
    177 	/* We have not been relocated yet, so fix the dynamic address */
    178 	objself.dynamic = (Elf_Dyn *)
    179 		((u_long) mapbase + (char *) objself.dynamic);
    180 #endif				/* RTLD_RELOCATE_SELF */
    181 
    182 	_rtld_digest_dynamic(&objself);
    183 
    184 #ifdef __alpha__
    185 	/* XXX XXX XXX */
    186 	objself.pltgot = NULL;
    187 #endif
    188 	assert(objself.needed == NULL);
    189 
    190 #if !defined(__mips__) && !defined(__i386__)
    191 	/* no relocation for mips/i386 */
    192 	assert(!objself.textrel);
    193 #endif
    194 
    195 	_rtld_relocate_objects(&objself, true, dodebug);
    196 
    197 	/*
    198 	 * Now that we relocated ourselves, we can use globals.
    199 	 */
    200 	_rtld_objself = objself;
    201 
    202 	_rtld_objself.path = _rtld_path;
    203 	_rtld_add_paths(&_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
    204 
    205 	/*
    206 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
    207 	 */
    208 	_rtld_objlist = &_rtld_objself;
    209 
    210 	/* Make the object list empty again. */
    211 	_rtld_objlist = NULL;
    212 	_rtld_objtail = &_rtld_objlist;
    213 
    214 	_rtld_debug.r_brk = _rtld_debug_state;
    215 	_rtld_debug.r_state = RT_CONSISTENT;
    216 }
    217 
    218 /*
    219  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
    220  * before the process exits.
    221  */
    222 static void
    223 _rtld_exit()
    224 {
    225 	dbg(("rtld_exit()"));
    226 
    227 	_rtld_call_fini_functions(_rtld_objlist->next);
    228 }
    229 
    230 /*
    231  * Main entry point for dynamic linking.  The argument is the stack
    232  * pointer.  The stack is expected to be laid out as described in the
    233  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
    234  * the stack pointer points to a word containing ARGC.  Following that
    235  * in the stack is a null-terminated sequence of pointers to argument
    236  * strings.  Then comes a null-terminated sequence of pointers to
    237  * environment strings.  Finally, there is a sequence of "auxiliary
    238  * vector" entries.
    239  *
    240  * This function returns the entry point for the main program, the dynamic
    241  * linker's exit procedure in sp[0], and a pointer to the main object in
    242  * sp[1].
    243  */
    244 Elf_Addr
    245 _rtld(sp)
    246 	Elf_Word *sp;
    247 {
    248 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
    249 	               *pAUX_phent, *pAUX_phnum;
    250 #ifdef	VARPSZ
    251 	const AuxInfo  *pAUX_pagesz;
    252 #endif
    253 	char          **env;
    254 	const AuxInfo  *aux;
    255 	const AuxInfo  *auxp;
    256 	Elf_Word       *const osp = sp;
    257 	bool            bind_now = 0;
    258 	const char     *ld_bind_now;
    259 	const char    **argv;
    260 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    261 	int             i = 0;
    262 #endif
    263 
    264 	/*
    265          * On entry, the dynamic linker itself has not been relocated yet.
    266          * Be very careful not to reference any global data until after
    267          * _rtld_init has returned.  It is OK to reference file-scope statics
    268          * and string constants, and to call static and global functions.
    269          */
    270 	/* Find the auxiliary vector on the stack. */
    271 	/* first Elf_Word reserved to address of exit routine */
    272 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    273 	dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
    274 	     &sp[3], (char *) sp[3]));
    275 	dbg(("got is at %p, dynamic is at %p\n",
    276 	    _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
    277 	debug = 1;
    278 	dbg(("_ctype_ is %p\n", _ctype_));
    279 #endif
    280 
    281 	sp += 2;		/* skip over return argument space */
    282 	argv = (const char **) &sp[1];
    283 	sp += sp[0] + 2;	/* Skip over argc, arguments, and NULL
    284 				 * terminator */
    285 	env = (char **) sp;
    286 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
    287 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    288 		dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
    289 #endif
    290 	}
    291 	aux = (const AuxInfo *) sp;
    292 
    293 	/* Digest the auxiliary vector. */
    294 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
    295 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
    296 #ifdef	VARPSZ
    297 	pAUX_pagesz = NULL;
    298 #endif
    299 	for (auxp = aux; auxp->au_id != AUX_null; ++auxp) {
    300 		switch (auxp->au_id) {
    301 		case AUX_base:
    302 			pAUX_base = auxp;
    303 			break;
    304 		case AUX_entry:
    305 			pAUX_entry = auxp;
    306 			break;
    307 		case AUX_execfd:
    308 			pAUX_execfd = auxp;
    309 			break;
    310 		case AUX_phdr:
    311 			pAUX_phdr = auxp;
    312 			break;
    313 		case AUX_phent:
    314 			pAUX_phent = auxp;
    315 			break;
    316 		case AUX_phnum:
    317 			pAUX_phnum = auxp;
    318 			break;
    319 #ifdef	VARPSZ
    320 		case AUX_pagesz:
    321 			pAUX_pagesz = auxp;
    322 			break;
    323 #endif
    324 		}
    325 	}
    326 
    327 	/* Initialize and relocate ourselves. */
    328 	assert(pAUX_base != NULL);
    329 	_rtld_init((caddr_t) pAUX_base->au_v);
    330 
    331 #ifdef	VARPSZ
    332 	assert(pAUX_pagesz != NULL);
    333 	_rtld_pagesz = (int)pAUX_pagesz->au_v;
    334 #endif
    335 
    336 #ifdef RTLD_DEBUG
    337 	dbg(("_ctype_ is %p\n", _ctype_));
    338 #endif
    339 
    340 	__progname = _rtld_objself.path;
    341 	environ = env;
    342 
    343 	_rtld_trust = geteuid() == getuid() && getegid() == getgid();
    344 
    345 	ld_bind_now = getenv("LD_BIND_NOW");
    346 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
    347 		bind_now = true;
    348 	if (_rtld_trust) {
    349 #ifdef DEBUG
    350 		const char     *ld_debug = getenv("LD_DEBUG");
    351 		if (ld_debug != NULL && *ld_debug != '\0')
    352 			debug = 1;
    353 #endif
    354 		_rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
    355 	}
    356 	dbg(("%s is initialized, base address = %p", __progname,
    357 	     (void *) pAUX_base->au_v));
    358 
    359 	/*
    360          * Load the main program, or process its program header if it is
    361          * already loaded.
    362          */
    363 	if (pAUX_execfd != NULL) {	/* Load the main program. */
    364 		int             fd = pAUX_execfd->au_v;
    365 		dbg(("loading main program"));
    366 		_rtld_objmain = _rtld_map_object(argv[0], fd);
    367 		close(fd);
    368 		if (_rtld_objmain == NULL)
    369 			_rtld_die();
    370 	} else {		/* Main program already loaded. */
    371 		const Elf_Phdr *phdr;
    372 		int             phnum;
    373 		caddr_t         entry;
    374 
    375 		dbg(("processing main program's program header"));
    376 		assert(pAUX_phdr != NULL);
    377 		phdr = (const Elf_Phdr *) pAUX_phdr->au_v;
    378 		assert(pAUX_phnum != NULL);
    379 		phnum = pAUX_phnum->au_v;
    380 		assert(pAUX_phent != NULL);
    381 		assert(pAUX_phent->au_v == sizeof(Elf_Phdr));
    382 		assert(pAUX_entry != NULL);
    383 		entry = (caddr_t) pAUX_entry->au_v;
    384 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
    385 	}
    386 
    387 	_rtld_objmain->path = xstrdup("main program");
    388 	_rtld_objmain->mainprog = true;
    389 	_rtld_digest_dynamic(_rtld_objmain);
    390 
    391 	_rtld_linkmap_add(_rtld_objmain);
    392 	_rtld_linkmap_add(&_rtld_objself);
    393 
    394 	/* Link the main program into the list of objects. */
    395 	*_rtld_objtail = _rtld_objmain;
    396 	_rtld_objtail = &_rtld_objmain->next;
    397 	++_rtld_objmain->refcount;
    398 
    399 	/*
    400 	 * Pre-load user-specified objects after the main program but before
    401 	 * any shared object dependencies.
    402 	 */
    403 	dbg(("preloading objects"));
    404 	if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
    405 		_rtld_die();
    406 
    407 	dbg(("loading needed objects"));
    408 	if (_rtld_load_needed_objects(_rtld_objmain) == -1)
    409 		_rtld_die();
    410 
    411 	dbg(("relocating objects"));
    412 	if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
    413 		_rtld_die();
    414 
    415 	dbg(("doing copy relocations"));
    416 	if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
    417 		_rtld_die();
    418 
    419 	dbg(("calling _init functions"));
    420 	_rtld_call_init_functions(_rtld_objmain->next);
    421 
    422 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
    423 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
    424 
    425 	/*
    426 	 * Return with the entry point and the exit procedure in at the top
    427 	 * of stack.
    428 	 */
    429 
    430 	_rtld_debug_state();	/* say hello to gdb! */
    431 
    432 	((void **) osp)[0] = _rtld_exit;
    433 	((void **) osp)[1] = _rtld_objmain;
    434 	return (Elf_Addr) _rtld_objmain->entry;
    435 }
    436 
    437 void
    438 _rtld_die()
    439 {
    440 	const char *msg = _rtld_dlerror();
    441 
    442 	if (msg == NULL)
    443 		msg = "Fatal error";
    444 	xerrx(1, "%s\n", msg);
    445 }
    446 
    447 static Obj_Entry *
    448 _rtld_dlcheck(handle)
    449 	void *handle;
    450 {
    451 	Obj_Entry *obj;
    452 
    453 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
    454 		if (obj == (Obj_Entry *) handle)
    455 			break;
    456 
    457 	if (obj == NULL || obj->dl_refcount == 0) {
    458 		xwarnx("Invalid shared object handle %p", handle);
    459 		return NULL;
    460 	}
    461 	return obj;
    462 }
    463 
    464 static void
    465 _rtld_unref_object_dag(root)
    466 	Obj_Entry *root;
    467 {
    468 	assert(root->refcount != 0);
    469 	--root->refcount;
    470 	if (root->refcount == 0) {
    471 		const Needed_Entry *needed;
    472 
    473 		for (needed = root->needed; needed != NULL;
    474 		    needed = needed->next)
    475 			_rtld_unref_object_dag(needed->obj);
    476 	}
    477 }
    478 
    479 int
    480 _rtld_dlclose(handle)
    481 	void *handle;
    482 {
    483 	Obj_Entry *root = _rtld_dlcheck(handle);
    484 
    485 	if (root == NULL)
    486 		return -1;
    487 
    488 	_rtld_debug.r_state = RT_DELETE;
    489 	_rtld_debug_state();
    490 
    491 	--root->dl_refcount;
    492 	_rtld_unref_object_dag(root);
    493 	if (root->refcount == 0) {	/* We are finished with some objects. */
    494 		Obj_Entry      *obj;
    495 		Obj_Entry     **linkp;
    496 
    497 		/* Finalize objects that are about to be unmapped. */
    498 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
    499 			if (obj->refcount == 0 && obj->fini != NULL)
    500 				(*obj->fini) ();
    501 
    502 		/* Unmap all objects that are no longer referenced. */
    503 		linkp = &_rtld_objlist->next;
    504 		while ((obj = *linkp) != NULL) {
    505 			if (obj->refcount == 0) {
    506 				munmap(obj->mapbase, obj->mapsize);
    507 				free(obj->path);
    508 				while (obj->needed != NULL) {
    509 					Needed_Entry   *needed = obj->needed;
    510 					obj->needed = needed->next;
    511 					free(needed);
    512 				}
    513 				_rtld_linkmap_delete(obj);
    514 				*linkp = obj->next;
    515 				if (obj->next == NULL)
    516 					_rtld_objtail = linkp;
    517 				free(obj);
    518 			} else
    519 				linkp = &obj->next;
    520 		}
    521 	}
    522 	_rtld_debug.r_state = RT_CONSISTENT;
    523 	_rtld_debug_state();
    524 
    525 	return 0;
    526 }
    527 
    528 char *
    529 _rtld_dlerror()
    530 {
    531 	char *msg = error_message;
    532 	error_message = NULL;
    533 	return msg;
    534 }
    535 
    536 void *
    537 _rtld_dlopen(name, mode)
    538 	const char *name;
    539 	int mode;
    540 {
    541 	Obj_Entry **old_obj_tail = _rtld_objtail;
    542 	Obj_Entry *obj = NULL;
    543 
    544 	_rtld_debug.r_state = RT_ADD;
    545 	_rtld_debug_state();
    546 
    547 	if (name == NULL) {
    548 		obj = _rtld_objmain;
    549 	} else {
    550 		char *path = _rtld_find_library(name, _rtld_objmain);
    551 		if (path != NULL)
    552 			obj = _rtld_load_object(path, true);
    553 	}
    554 
    555 	if (obj != NULL) {
    556 		++obj->dl_refcount;
    557 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
    558 			assert(*old_obj_tail == obj);
    559 
    560 			/* FIXME - Clean up properly after an error. */
    561 			if (_rtld_load_needed_objects(obj) == -1) {
    562 				--obj->dl_refcount;
    563 				obj = NULL;
    564 			} else if (_rtld_relocate_objects(obj,
    565 			    (mode & 3) == RTLD_NOW, true) == -1) {
    566 				--obj->dl_refcount;
    567 				obj = NULL;
    568 			} else {
    569 				_rtld_call_init_functions(obj);
    570 			}
    571 		}
    572 	}
    573 	_rtld_debug.r_state = RT_CONSISTENT;
    574 	_rtld_debug_state();
    575 
    576 	return obj;
    577 }
    578 
    579 void *
    580 _rtld_dlsym(handle, name)
    581 	void *handle;
    582 	const char *name;
    583 {
    584 	const Obj_Entry *obj = _rtld_dlcheck(handle);
    585 	const Elf_Sym  *def;
    586 	const Obj_Entry *defobj;
    587 
    588 	if (obj == NULL)
    589 		return NULL;
    590 
    591 	/*
    592          * FIXME - This isn't correct.  The search should include the whole
    593          * DAG rooted at the given object.
    594          */
    595 	def = _rtld_find_symdef(_rtld_objlist, 0, name, obj, &defobj, false);
    596 	if (def != NULL)
    597 		return defobj->relocbase + def->st_value;
    598 
    599 	_rtld_error("Undefined symbol \"%s\"", name);
    600 	return NULL;
    601 }
    602 
    603 /*
    604  * Error reporting function.  Use it like printf.  If formats the message
    605  * into a buffer, and sets things up so that the next call to dlerror()
    606  * will return the message.
    607  */
    608 void
    609 #ifdef __STDC__
    610 _rtld_error(const char *fmt,...)
    611 #else
    612 _rtld_error(va_alist)
    613 	va_dcl
    614 #endif
    615 {
    616 	static char     buf[512];
    617 	va_list         ap;
    618 #ifdef __STDC__
    619 	va_start(ap, fmt);
    620 #else
    621 	const char *fmt;
    622 
    623 	va_start(ap);
    624 	fmt = va_arg(ap, const char *);
    625 #endif
    626 	xvsnprintf(buf, sizeof buf, fmt, ap);
    627 	error_message = buf;
    628 	va_end(ap);
    629 }
    630 
    631 void
    632 _rtld_debug_state()
    633 {
    634 	/* do nothing */
    635 }
    636 
    637 void
    638 _rtld_linkmap_add(obj)
    639 	Obj_Entry *obj;
    640 {
    641 	struct link_map *l = &obj->linkmap;
    642 	struct link_map *prev;
    643 
    644 	obj->linkmap.l_name = obj->path;
    645 	obj->linkmap.l_addr = obj->mapbase;
    646 	obj->linkmap.l_ld = obj->dynamic;
    647 #ifdef __mips__
    648 	/* GDB needs load offset on MIPS to use the symbols */
    649 	obj->linkmap.l_offs = obj->relocbase;
    650 #endif
    651 
    652 	if (_rtld_debug.r_map == NULL) {
    653 		_rtld_debug.r_map = l;
    654 		return;
    655 	}
    656 	for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
    657 	l->l_prev = prev;
    658 	prev->l_next = l;
    659 	l->l_next = NULL;
    660 }
    661 
    662 void
    663 _rtld_linkmap_delete(obj)
    664 	Obj_Entry *obj;
    665 {
    666 	struct link_map *l = &obj->linkmap;
    667 
    668 	if (l->l_prev == NULL) {
    669 		if ((_rtld_debug.r_map = l->l_next) != NULL)
    670 			l->l_next->l_prev = NULL;
    671 		return;
    672 	}
    673 	if ((l->l_prev->l_next = l->l_next) != NULL)
    674 		l->l_next->l_prev = l->l_prev;
    675 }
    676