Thu Nov 2 19:24:37 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / elf / dlopen.c
blobde65a20d97169202a5468e09ddd2f9586564b2cb
1 /* dlopen -- Load a shared object at run time.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <stddef.h>
21 #include <link.h>
22 #include <dlfcn.h>
24 void *
25 dlopen (const char *file, dl_open_mode mode)
27 struct link_map *new, *l;
29 void doit (void)
31 Elf32_Addr init;
33 new = _dl_map_object (_dl_loaded, file);
35 /* Map in any dependencies. */
36 for (l = new; l; l = l->l_next)
37 if (! l->l_deps_loaded)
39 if (l->l_info[DT_NEEDED])
41 const char *strtab
42 = (void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr;
43 const Elf32_Dyn *d;
44 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
45 if (d->d_tag == DT_NEEDED)
46 _dl_map_object (l, strtab + d->d_un.d_val);
48 l->l_deps_loaded = 1;
51 /* Relocate the objects loaded. */
52 for (l = new; l; l = l->l_next)
53 if (! l->l_relocated)
54 _dl_relocate_object (l, mode == RTLD_LAZY);
56 /* Run the initializer functions of new objects. */
57 while (init = _dl_init_next ())
58 (*(void (*) (void)) init) ();
61 return _dlerror_run (doit) ? NULL : new;