Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.c revision 1.21
      1 /*	$NetBSD: rtld.c,v 1.21 1999/07/22 00:18:19 thorpej 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_paths;
     94 /*
     95  * Global declarations normally provided by crt0.
     96  */
     97 char           *__progname;
     98 char          **environ;
     99 
    100 #ifdef OLD_GOT
    101 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    102 #else
    103 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    104 extern Elf_Dyn  _DYNAMIC;
    105 #endif
    106 
    107 static void _rtld_call_fini_functions __P((Obj_Entry *));
    108 static void _rtld_call_init_functions __P((Obj_Entry *));
    109 static Obj_Entry *_rtld_dlcheck __P((void *));
    110 static void _rtld_unref_object_dag __P((Obj_Entry *));
    111 
    112 static void
    113 _rtld_call_fini_functions(first)
    114 	Obj_Entry *first;
    115 {
    116 	Obj_Entry *obj;
    117 
    118 	for (obj = first; obj != NULL; obj = obj->next)
    119 		if (obj->fini != NULL)
    120 			(*obj->fini)();
    121 }
    122 
    123 static void
    124 _rtld_call_init_functions(first)
    125 	Obj_Entry *first;
    126 {
    127 	if (first != NULL) {
    128 		_rtld_call_init_functions(first->next);
    129 		if (first->init != NULL)
    130 			(*first->init)();
    131 	}
    132 }
    133 
    134 /*
    135  * Initialize the dynamic linker.  The argument is the address at which
    136  * the dynamic linker has been mapped into memory.  The primary task of
    137  * this function is to relocate the dynamic linker.
    138  */
    139 static void
    140 _rtld_init(mapbase)
    141 	caddr_t mapbase;
    142 {
    143 	Obj_Entry objself;/* The dynamic linker shared object */
    144 #ifdef RTLD_RELOCATE_SELF
    145 	int dodebug = false;
    146 #else
    147 	int dodebug = true;
    148 #endif
    149 
    150 	memset(&objself, 0, sizeof objself);
    151 
    152 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
    153 	objself.path = NULL;
    154 	objself.rtld = true;
    155 	objself.mapbase = mapbase;
    156 
    157 #if defined(__mips__)
    158 	/*
    159 	* mips and ld.so currently linked at load address,
    160 	* so no relocation needed
    161 	*/
    162 	objself.relocbase = 0;
    163 #else
    164 	objself.relocbase = mapbase;
    165 #endif
    166 
    167 	objself.pltgot = NULL;
    168 
    169 #ifdef OLD_GOT
    170 	objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
    171 #else
    172 	objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
    173 #endif
    174 
    175 #ifdef RTLD_RELOCATE_SELF
    176 	/* We have not been relocated yet, so fix the dynamic address */
    177 	objself.dynamic = (Elf_Dyn *)
    178 		((u_long) mapbase + (char *) objself.dynamic);
    179 #endif				/* RTLD_RELOCATE_SELF */
    180 
    181 	_rtld_digest_dynamic(&objself);
    182 
    183 #ifdef __alpha__
    184 	/* XXX XXX XXX */
    185 	objself.pltgot = NULL;
    186 #endif
    187 	assert(objself.needed == NULL);
    188 
    189 #if !defined(__mips__) && !defined(__i386__)
    190 	/* no relocation for mips/i386 */
    191 	assert(!objself.textrel);
    192 #endif
    193 
    194 	_rtld_relocate_objects(&objself, true, dodebug);
    195 
    196 	/*
    197 	 * Now that we relocated ourselves, we can use globals.
    198 	 */
    199 	_rtld_objself = objself;
    200 
    201 	_rtld_objself.path = _rtld_path;
    202 	_rtld_add_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
    203 
    204 	/*
    205 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
    206 	 */
    207 	_rtld_objlist = &_rtld_objself;
    208 
    209 	/* Make the object list empty again. */
    210 	_rtld_objlist = NULL;
    211 	_rtld_objtail = &_rtld_objlist;
    212 
    213 	_rtld_debug.r_brk = _rtld_debug_state;
    214 	_rtld_debug.r_state = RT_CONSISTENT;
    215 }
    216 
    217 /*
    218  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
    219  * before the process exits.
    220  */
    221 static void
    222 _rtld_exit()
    223 {
    224 	dbg(("rtld_exit()"));
    225 
    226 	_rtld_call_fini_functions(_rtld_objlist->next);
    227 }
    228 
    229 /*
    230  * Main entry point for dynamic linking.  The argument is the stack
    231  * pointer.  The stack is expected to be laid out as described in the
    232  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
    233  * the stack pointer points to a word containing ARGC.  Following that
    234  * in the stack is a null-terminated sequence of pointers to argument
    235  * strings.  Then comes a null-terminated sequence of pointers to
    236  * environment strings.  Finally, there is a sequence of "auxiliary
    237  * vector" entries.
    238  *
    239  * This function returns the entry point for the main program, the dynamic
    240  * linker's exit procedure in sp[0], and a pointer to the main object in
    241  * sp[1].
    242  */
    243 Elf_Addr
    244 _rtld(sp)
    245 	Elf_Word *sp;
    246 {
    247 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
    248 	               *pAUX_phent, *pAUX_phnum;
    249 #ifdef	VARPSZ
    250 	const AuxInfo  *pAUX_pagesz;
    251 #endif
    252 	char          **env;
    253 	const AuxInfo  *aux;
    254 	const AuxInfo  *auxp;
    255 	Elf_Word       *const osp = sp;
    256 	bool            bind_now = 0;
    257 	const char     *ld_bind_now;
    258 	const char    **argv;
    259 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    260 	int             i = 0;
    261 #endif
    262 
    263 	/*
    264          * On entry, the dynamic linker itself has not been relocated yet.
    265          * Be very careful not to reference any global data until after
    266          * _rtld_init has returned.  It is OK to reference file-scope statics
    267          * and string constants, and to call static and global functions.
    268          */
    269 	/* Find the auxiliary vector on the stack. */
    270 	/* first Elf_Word reserved to address of exit routine */
    271 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    272 	dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
    273 	     &sp[3], (char *) sp[3]));
    274 	dbg(("got is at %p, dynamic is at %p\n",
    275 	    _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
    276 	debug = 1;
    277 	dbg(("_ctype_ is %p\n", _ctype_));
    278 #endif
    279 
    280 	sp += 2;		/* skip over return argument space */
    281 	argv = (const char **) &sp[1];
    282 	sp += sp[0] + 2;	/* Skip over argc, arguments, and NULL
    283 				 * terminator */
    284 	env = (char **) sp;
    285 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
    286 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    287 		dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
    288 #endif
    289 	}
    290 	aux = (const AuxInfo *) sp;
    291 
    292 	/* Digest the auxiliary vector. */
    293 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
    294 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
    295 #ifdef	VARPSZ
    296 	pAUX_pagesz = NULL;
    297 #endif
    298 	for (auxp = aux; auxp->au_id != AUX_null; ++auxp) {
    299 		switch (auxp->au_id) {
    300 		case AUX_base:
    301 			pAUX_base = auxp;
    302 			break;
    303 		case AUX_entry:
    304 			pAUX_entry = auxp;
    305 			break;
    306 		case AUX_execfd:
    307 			pAUX_execfd = auxp;
    308 			break;
    309 		case AUX_phdr:
    310 			pAUX_phdr = auxp;
    311 			break;
    312 		case AUX_phent:
    313 			pAUX_phent = auxp;
    314 			break;
    315 		case AUX_phnum:
    316 			pAUX_phnum = auxp;
    317 			break;
    318 #ifdef	VARPSZ
    319 		case AUX_pagesz:
    320 			pAUX_pagesz = auxp;
    321 			break;
    322 #endif
    323 		}
    324 	}
    325 
    326 	/* Initialize and relocate ourselves. */
    327 	assert(pAUX_base != NULL);
    328 	_rtld_init((caddr_t) pAUX_base->au_v);
    329 
    330 #ifdef	VARPSZ
    331 	assert(pAUX_pagesz != NULL);
    332 	_rtld_pagesz = (int)pAUX_pagesz->au_v;
    333 #endif
    334 
    335 #ifdef RTLD_DEBUG
    336 	dbg(("_ctype_ is %p\n", _ctype_));
    337 #endif
    338 
    339 	__progname = _rtld_objself.path;
    340 	environ = env;
    341 
    342 	_rtld_trust = geteuid() == getuid() && getegid() == getgid();
    343 
    344 	ld_bind_now = getenv("LD_BIND_NOW");
    345 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
    346 		bind_now = true;
    347 	if (_rtld_trust) {
    348 #ifdef DEBUG
    349 		const char     *ld_debug = getenv("LD_DEBUG");
    350 		if (ld_debug != NULL && *ld_debug != '\0')
    351 			debug = 1;
    352 #endif
    353 		_rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
    354 	}
    355 	dbg(("%s is initialized, base address = %p", __progname,
    356 	     (void *) pAUX_base->au_v));
    357 
    358 	/*
    359          * Load the main program, or process its program header if it is
    360          * already loaded.
    361          */
    362 	if (pAUX_execfd != NULL) {	/* Load the main program. */
    363 		int             fd = pAUX_execfd->au_v;
    364 		dbg(("loading main program"));
    365 		_rtld_objmain = _rtld_map_object(argv[0], fd);
    366 		close(fd);
    367 		if (_rtld_objmain == NULL)
    368 			_rtld_die();
    369 	} else {		/* Main program already loaded. */
    370 		const Elf_Phdr *phdr;
    371 		int             phnum;
    372 		caddr_t         entry;
    373 
    374 		dbg(("processing main program's program header"));
    375 		assert(pAUX_phdr != NULL);
    376 		phdr = (const Elf_Phdr *) pAUX_phdr->au_v;
    377 		assert(pAUX_phnum != NULL);
    378 		phnum = pAUX_phnum->au_v;
    379 		assert(pAUX_phent != NULL);
    380 		assert(pAUX_phent->au_v == sizeof(Elf_Phdr));
    381 		assert(pAUX_entry != NULL);
    382 		entry = (caddr_t) pAUX_entry->au_v;
    383 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
    384 	}
    385 
    386 	_rtld_objmain->path = xstrdup("main program");
    387 	_rtld_objmain->mainprog = true;
    388 	_rtld_digest_dynamic(_rtld_objmain);
    389 
    390 	_rtld_linkmap_add(_rtld_objmain);
    391 	_rtld_linkmap_add(&_rtld_objself);
    392 
    393 	/* Link the main program into the list of objects. */
    394 	*_rtld_objtail = _rtld_objmain;
    395 	_rtld_objtail = &_rtld_objmain->next;
    396 	++_rtld_objmain->refcount;
    397 
    398 	/*
    399 	 * Pre-load user-specified objects after the main program but before
    400 	 * any shared object dependencies.
    401 	 */
    402 	dbg(("preloading objects"));
    403 	if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
    404 		_rtld_die();
    405 
    406 	dbg(("loading needed objects"));
    407 	if (_rtld_load_needed_objects(_rtld_objmain) == -1)
    408 		_rtld_die();
    409 
    410 	dbg(("relocating objects"));
    411 	if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
    412 		_rtld_die();
    413 
    414 	dbg(("doing copy relocations"));
    415 	if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
    416 		_rtld_die();
    417 
    418 	dbg(("calling _init functions"));
    419 	_rtld_call_init_functions(_rtld_objmain->next);
    420 
    421 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
    422 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
    423 
    424 	/*
    425 	 * Return with the entry point and the exit procedure in at the top
    426 	 * of stack.
    427 	 */
    428 
    429 	_rtld_debug_state();	/* say hello to gdb! */
    430 
    431 	((void **) osp)[0] = _rtld_exit;
    432 	((void **) osp)[1] = _rtld_objmain;
    433 	return (Elf_Addr) _rtld_objmain->entry;
    434 }
    435 
    436 void
    437 _rtld_die()
    438 {
    439 	const char *msg = _rtld_dlerror();
    440 
    441 	if (msg == NULL)
    442 		msg = "Fatal error";
    443 	xerrx(1, "%s\n", msg);
    444 }
    445 
    446 static Obj_Entry *
    447 _rtld_dlcheck(handle)
    448 	void *handle;
    449 {
    450 	Obj_Entry *obj;
    451 
    452 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
    453 		if (obj == (Obj_Entry *) handle)
    454 			break;
    455 
    456 	if (obj == NULL || obj->dl_refcount == 0) {
    457 		xwarnx("Invalid shared object handle %p", handle);
    458 		return NULL;
    459 	}
    460 	return obj;
    461 }
    462 
    463 static void
    464 _rtld_unref_object_dag(root)
    465 	Obj_Entry *root;
    466 {
    467 	assert(root->refcount != 0);
    468 	--root->refcount;
    469 	if (root->refcount == 0) {
    470 		const Needed_Entry *needed;
    471 
    472 		for (needed = root->needed; needed != NULL;
    473 		    needed = needed->next)
    474 			_rtld_unref_object_dag(needed->obj);
    475 	}
    476 }
    477 
    478 int
    479 _rtld_dlclose(handle)
    480 	void *handle;
    481 {
    482 	Obj_Entry *root = _rtld_dlcheck(handle);
    483 
    484 	if (root == NULL)
    485 		return -1;
    486 
    487 	_rtld_debug.r_state = RT_DELETE;
    488 	_rtld_debug_state();
    489 
    490 	--root->dl_refcount;
    491 	_rtld_unref_object_dag(root);
    492 	if (root->refcount == 0) {	/* We are finished with some objects. */
    493 		Obj_Entry      *obj;
    494 		Obj_Entry     **linkp;
    495 
    496 		/* Finalize objects that are about to be unmapped. */
    497 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
    498 			if (obj->refcount == 0 && obj->fini != NULL)
    499 				(*obj->fini) ();
    500 
    501 		/* Unmap all objects that are no longer referenced. */
    502 		linkp = &_rtld_objlist->next;
    503 		while ((obj = *linkp) != NULL) {
    504 			if (obj->refcount == 0) {
    505 				munmap(obj->mapbase, obj->mapsize);
    506 				free(obj->path);
    507 				while (obj->needed != NULL) {
    508 					Needed_Entry   *needed = obj->needed;
    509 					obj->needed = needed->next;
    510 					free(needed);
    511 				}
    512 				_rtld_linkmap_delete(obj);
    513 				*linkp = obj->next;
    514 				if (obj->next == NULL)
    515 					_rtld_objtail = linkp;
    516 				free(obj);
    517 			} else
    518 				linkp = &obj->next;
    519 		}
    520 	}
    521 	_rtld_debug.r_state = RT_CONSISTENT;
    522 	_rtld_debug_state();
    523 
    524 	return 0;
    525 }
    526 
    527 char *
    528 _rtld_dlerror()
    529 {
    530 	char *msg = error_message;
    531 	error_message = NULL;
    532 	return msg;
    533 }
    534 
    535 void *
    536 _rtld_dlopen(name, mode)
    537 	const char *name;
    538 	int mode;
    539 {
    540 	Obj_Entry **old_obj_tail = _rtld_objtail;
    541 	Obj_Entry *obj = NULL;
    542 
    543 	_rtld_debug.r_state = RT_ADD;
    544 	_rtld_debug_state();
    545 
    546 	if (name == NULL) {
    547 		obj = _rtld_objmain;
    548 	} else {
    549 		char *path = _rtld_find_library(name, _rtld_objmain);
    550 		if (path != NULL)
    551 			obj = _rtld_load_object(path, true);
    552 	}
    553 
    554 	if (obj != NULL) {
    555 		++obj->dl_refcount;
    556 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
    557 			assert(*old_obj_tail == obj);
    558 
    559 			/* FIXME - Clean up properly after an error. */
    560 			if (_rtld_load_needed_objects(obj) == -1) {
    561 				--obj->dl_refcount;
    562 				obj = NULL;
    563 			} else if (_rtld_relocate_objects(obj,
    564 			    (mode & 3) == RTLD_NOW, true) == -1) {
    565 				--obj->dl_refcount;
    566 				obj = NULL;
    567 			} else {
    568 				_rtld_call_init_functions(obj);
    569 			}
    570 		}
    571 	}
    572 	_rtld_debug.r_state = RT_CONSISTENT;
    573 	_rtld_debug_state();
    574 
    575 	return obj;
    576 }
    577 
    578 void *
    579 _rtld_dlsym(handle, name)
    580 	void *handle;
    581 	const char *name;
    582 {
    583 	const Obj_Entry *obj = _rtld_dlcheck(handle);
    584 	const Elf_Sym  *def;
    585 	const Obj_Entry *defobj;
    586 
    587 	if (obj == NULL)
    588 		return NULL;
    589 
    590 	/*
    591          * FIXME - This isn't correct.  The search should include the whole
    592          * DAG rooted at the given object.
    593          */
    594 	def = _rtld_find_symdef(_rtld_objlist, 0, name, obj, &defobj, false);
    595 	if (def != NULL)
    596 		return defobj->relocbase + def->st_value;
    597 
    598 	_rtld_error("Undefined symbol \"%s\"", name);
    599 	return NULL;
    600 }
    601 
    602 /*
    603  * Error reporting function.  Use it like printf.  If formats the message
    604  * into a buffer, and sets things up so that the next call to dlerror()
    605  * will return the message.
    606  */
    607 void
    608 #ifdef __STDC__
    609 _rtld_error(const char *fmt,...)
    610 #else
    611 _rtld_error(va_alist)
    612 	va_dcl
    613 #endif
    614 {
    615 	static char     buf[512];
    616 	va_list         ap;
    617 #ifdef __STDC__
    618 	va_start(ap, fmt);
    619 #else
    620 	const char *fmt;
    621 
    622 	va_start(ap);
    623 	fmt = va_arg(ap, const char *);
    624 #endif
    625 	xvsnprintf(buf, sizeof buf, fmt, ap);
    626 	error_message = buf;
    627 	va_end(ap);
    628 }
    629 
    630 void
    631 _rtld_debug_state()
    632 {
    633 	/* do nothing */
    634 }
    635 
    636 void
    637 _rtld_linkmap_add(obj)
    638 	Obj_Entry *obj;
    639 {
    640 	struct link_map *l = &obj->linkmap;
    641 	struct link_map *prev;
    642 
    643 	obj->linkmap.l_name = obj->path;
    644 	obj->linkmap.l_addr = obj->mapbase;
    645 	obj->linkmap.l_ld = obj->dynamic;
    646 #ifdef __mips__
    647 	/* GDB needs load offset on MIPS to use the symbols */
    648 	obj->linkmap.l_offs = obj->relocbase;
    649 #endif
    650 
    651 	if (_rtld_debug.r_map == NULL) {
    652 		_rtld_debug.r_map = l;
    653 		return;
    654 	}
    655 	for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
    656 	l->l_prev = prev;
    657 	prev->l_next = l;
    658 	l->l_next = NULL;
    659 }
    660 
    661 void
    662 _rtld_linkmap_delete(obj)
    663 	Obj_Entry *obj;
    664 {
    665 	struct link_map *l = &obj->linkmap;
    666 
    667 	if (l->l_prev == NULL) {
    668 		if ((_rtld_debug.r_map = l->l_next) != NULL)
    669 			l->l_next->l_prev = NULL;
    670 		return;
    671 	}
    672 	if ((l->l_prev->l_next = l->l_next) != NULL)
    673 		l->l_next->l_prev = l->l_prev;
    674 }
    675