OSDN Git Service

make RUNPATH/RPATH support configurable
[uclinux-h8/uClibc.git] / ldso / ldso / dl-elf.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * This file contains the helper routines to load an ELF shared
4  * library into memory and add the symbol table info to the chain.
5  *
6  * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
7  * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8  *                              David Engel, Hongjiu Lu and Mitch D'Souza
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the above contributors may not be
16  *    used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32
33 #include "ldso.h"
34
35 #ifdef __LDSO_CACHE_SUPPORT__
36
37 static caddr_t _dl_cache_addr = NULL;
38 static size_t _dl_cache_size = 0;
39
40 int _dl_map_cache(void)
41 {
42         int fd;
43         struct stat st;
44         header_t *header;
45         libentry_t *libent;
46         int i, strtabsize;
47
48         if (_dl_cache_addr == (caddr_t) - 1)
49                 return -1;
50         else if (_dl_cache_addr != NULL)
51                 return 0;
52
53         if (_dl_stat(LDSO_CACHE, &st)
54             || (fd = _dl_open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
55                 _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
56                 return -1;
57         }
58
59         _dl_cache_size = st.st_size;
60         _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
61         _dl_close(fd);
62         if (_dl_mmap_check_error(_dl_cache_addr)) {
63                 _dl_dprintf(2, "%s: can't map cache '%s'\n",
64                                 _dl_progname, LDSO_CACHE);
65                 return -1;
66         }
67
68         header = (header_t *) _dl_cache_addr;
69
70         if (_dl_cache_size < sizeof(header_t) ||
71                         _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
72                         || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
73                         || _dl_cache_size <
74                         (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
75                         || _dl_cache_addr[_dl_cache_size - 1] != '\0')
76         {
77                 _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
78                                 LDSO_CACHE);
79                 goto fail;
80         }
81
82         strtabsize = _dl_cache_size - sizeof(header_t) -
83                 header->nlibs * sizeof(libentry_t);
84         libent = (libentry_t *) & header[1];
85
86         for (i = 0; i < header->nlibs; i++) {
87                 if (libent[i].sooffset >= strtabsize ||
88                                 libent[i].liboffset >= strtabsize)
89                 {
90                         _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
91                         goto fail;
92                 }
93         }
94
95         return 0;
96
97 fail:
98         _dl_munmap(_dl_cache_addr, _dl_cache_size);
99         _dl_cache_addr = (caddr_t) - 1;
100         return -1;
101 }
102
103 int _dl_unmap_cache(void)
104 {
105         if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
106                 return -1;
107
108 #if 1
109         _dl_munmap(_dl_cache_addr, _dl_cache_size);
110         _dl_cache_addr = NULL;
111 #endif
112
113         return 0;
114 }
115 #endif
116
117
118 void 
119 _dl_protect_relro (struct elf_resolve *l)
120 {
121         ElfW(Addr) start = ((l->loadaddr + l->relro_addr)
122                             & ~(_dl_pagesize - 1));
123         ElfW(Addr) end = ((l->loadaddr + l->relro_addr + l->relro_size)
124                           & ~(_dl_pagesize - 1));
125 #if defined (__SUPPORT_LD_DEBUG__)
126         if (_dl_debug)
127                 _dl_dprintf(2, "RELRO protecting %s:  start:%x, end:%x\n", l->libname, start, end);
128 #endif
129         if (start != end &&
130             _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
131                 _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
132                 _dl_exit(0);
133         }
134 }
135
136 /* This function's behavior must exactly match that
137  * in uClibc/ldso/util/ldd.c */
138 static struct elf_resolve *
139 search_for_named_library(const char *name, int secure, const char *path_list,
140         struct dyn_elf **rpnt)
141 {
142         char *path, *path_n;
143         char mylibname[2050];
144         struct elf_resolve *tpnt;
145         int done = 0;
146
147         if (path_list==NULL)
148                 return NULL;
149
150         /* We need a writable copy of this string */
151         path = _dl_strdup(path_list);
152         if (!path) {
153                 _dl_dprintf(2, "Out of memory!\n");
154                 _dl_exit(0);
155         }
156
157         /* Unlike ldd.c, don't bother to eliminate double //s */
158
159         /* Replace colons with zeros in path_list */
160         /* : at the beginning or end of path maps to CWD */
161         /* :: anywhere maps CWD */
162         /* "" maps to CWD */ 
163         path_n = path;
164         do {
165                 if (*path == 0) {
166                         *path = ':';
167                         done = 1;
168                 }
169                 if (*path == ':') {
170                         *path = 0;
171                         if (*path_n)
172                                 _dl_strcpy(mylibname, path_n);
173                         else
174                                 _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
175                         _dl_strcat(mylibname, "/");
176                         _dl_strcat(mylibname, name);
177                         if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
178                                 return tpnt;
179                         path_n = path+1;
180                 }
181                 path++;
182         } while (!done);
183         return NULL;
184 }
185
186 /* Check if the named library is already loaded... */
187 struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
188                 int trace_loaded_objects)
189 {
190         const char *pnt, *pnt1;
191         struct elf_resolve *tpnt1;
192         const char *libname, *libname2;
193         static const char libc[] = "libc.so.";
194         static const char aborted_wrong_lib[] = "%s: aborted attempt to load %s!\n";
195
196         pnt = libname = full_libname;
197
198         _dl_if_debug_dprint("Checking if '%s' is already loaded\n", full_libname);
199         /* quick hack to ensure mylibname buffer doesn't overflow.  don't
200            allow full_libname or any directory to be longer than 1024. */
201         if (_dl_strlen(full_libname) > 1024)
202                 return NULL;
203
204         /* Skip over any initial initial './' and '/' stuff to
205          * get the short form libname with no path garbage */
206         pnt1 = _dl_strrchr(pnt, '/');
207         if (pnt1) {
208                 libname = pnt1 + 1;
209         }
210
211         /* Make sure they are not trying to load the wrong C library!
212          * This sometimes happens esp with shared libraries when the
213          * library path is somehow wrong! */
214 #define isdigit(c)  (c >= '0' && c <= '9')
215         if ((_dl_strncmp(libname, libc, 8) == 0) &&  _dl_strlen(libname) >=8 &&
216                         isdigit(libname[8]))
217         {
218                 /* Abort attempts to load glibc, libc5, etc */
219                 if ( libname[8]!='0') {
220                         if (!trace_loaded_objects) {
221                                 _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
222                                 _dl_exit(1);
223                         }
224                         return NULL;
225                 }
226         }
227
228         /* Critical step!  Weed out duplicates early to avoid
229          * function aliasing, which wastes memory, and causes
230          * really bad things to happen with weaks and globals. */
231         for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
232
233                 /* Skip over any initial initial './' and '/' stuff to
234                  * get the short form libname with no path garbage */
235                 libname2 = tpnt1->libname;
236                 pnt1 = _dl_strrchr(libname2, '/');
237                 if (pnt1) {
238                         libname2 = pnt1 + 1;
239                 }
240
241                 if (_dl_strcmp(libname2, libname) == 0) {
242                         /* Well, that was certainly easy */
243                         return tpnt1;
244                 }
245         }
246
247         return NULL;
248 }
249
250
251 /* Used to return error codes back to dlopen et. al.  */
252 unsigned long _dl_error_number;
253 unsigned long _dl_internal_error_number;
254
255 struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
256         struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
257 {
258         char *pnt;
259         struct elf_resolve *tpnt1;
260         char *libname;
261
262         _dl_internal_error_number = 0;
263         libname = full_libname;
264
265         /* quick hack to ensure mylibname buffer doesn't overflow.  don't
266            allow full_libname or any directory to be longer than 1024. */
267         if (_dl_strlen(full_libname) > 1024)
268                 goto goof;
269
270         /* Skip over any initial initial './' and '/' stuff to
271          * get the short form libname with no path garbage */
272         pnt = _dl_strrchr(libname, '/');
273         if (pnt) {
274                 libname = pnt + 1;
275         }
276
277         /* Critical step!  Weed out duplicates early to avoid
278          * function aliasing, which wastes memory, and causes
279          * really bad things to happen with weaks and globals. */
280         if ((tpnt1=_dl_check_if_named_library_is_loaded(libname, trace_loaded_objects))!=NULL) {
281                 tpnt1->usage_count++;
282                 return tpnt1;
283         }
284
285         _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
286         /* If the filename has any '/', try it straight and leave it at that.
287            For IBCS2 compatibility under linux, we substitute the string
288            /usr/i486-sysv4/lib for /usr/lib in library names. */
289
290         if (libname != full_libname) {
291                 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
292                 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
293                 if (tpnt1) {
294                         return tpnt1;
295                 }
296                 //goto goof;
297         }
298
299         /*
300          * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
301          * the default path of /usr/lib.  Check in rpath directories.
302          */
303 #ifdef __LDSO_RUNPATH__
304         pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
305         if (pnt) {
306                 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
307                 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
308                 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
309                         return tpnt1;
310         }
311 #endif
312
313         /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
314         if (_dl_library_path) {
315                 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
316                 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
317                 {
318                         return tpnt1;
319                 }
320         }
321
322         /*
323          * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
324          */
325 #ifdef __LDSO_RUNPATH__
326         pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
327         if (pnt) {
328                 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
329                 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
330                 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
331                         return tpnt1;
332         }
333 #endif
334
335         /*
336          * Where should the cache be searched?  There is no such concept in the
337          * ABI, so we have some flexibility here.  For now, search it before
338          * the hard coded paths that follow (i.e before /lib and /usr/lib).
339          */
340 #ifdef __LDSO_CACHE_SUPPORT__
341         if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
342                 int i;
343                 header_t *header = (header_t *) _dl_cache_addr;
344                 libentry_t *libent = (libentry_t *) & header[1];
345                 char *strs = (char *) &libent[header->nlibs];
346
347                 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
348                 for (i = 0; i < header->nlibs; i++) {
349                         if ((libent[i].flags == LIB_ELF ||
350                                                 libent[i].flags == LIB_ELF_LIBC0 ||
351                                                 libent[i].flags == LIB_ELF_LIBC5) &&
352                                         _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
353                                         (tpnt1 = _dl_load_elf_shared_library(secure,
354                                                                                                                  rpnt, strs + libent[i].liboffset)))
355                                 return tpnt1;
356                 }
357         }
358 #endif
359
360         /* Look for libraries wherever the shared library loader
361          * was installed */
362         _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
363         if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
364         {
365                 return tpnt1;
366         }
367
368
369         /* Lastly, search the standard list of paths for the library.
370            This list must exactly match the list in uClibc/ldso/util/ldd.c */
371         _dl_if_debug_dprint("\tsearching full lib path list\n");
372         if ((tpnt1 = search_for_named_library(libname, secure,
373                                         UCLIBC_RUNTIME_PREFIX "lib:"
374                                         UCLIBC_RUNTIME_PREFIX "usr/lib"
375 #ifndef __LDSO_CACHE_SUPPORT__
376                                         ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
377 #endif
378                                         , rpnt)
379                 ) != NULL)
380         {
381                 return tpnt1;
382         }
383
384 goof:
385         /* Well, we shot our wad on that one.  All we can do now is punt */
386         if (_dl_internal_error_number)
387                 _dl_error_number = _dl_internal_error_number;
388         else
389                 _dl_error_number = LD_ERROR_NOFILE;
390 #if defined (__SUPPORT_LD_DEBUG__)
391         if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
392 #endif
393         return NULL;
394 }
395
396
397 /*
398  * Read one ELF library into memory, mmap it into the correct locations and
399  * add the symbol info to the symbol chain.  Perform any relocations that
400  * are required.
401  */
402
403 struct elf_resolve *_dl_load_elf_shared_library(int secure,
404         struct dyn_elf **rpnt, char *libname)
405 {
406         ElfW(Ehdr) *epnt;
407         unsigned long dynamic_addr = 0;
408         ElfW(Dyn) *dpnt;
409         struct elf_resolve *tpnt;
410         ElfW(Phdr) *ppnt;
411         char *status, *header;
412         unsigned long dynamic_info[DYNAMIC_SIZE];
413         unsigned long *lpnt;
414         unsigned long libaddr;
415         unsigned long minvma = 0xffffffff, maxvma = 0;
416         int i, flags, piclib, infile;
417         ElfW(Addr) relro_addr = 0;
418         size_t relro_size = 0;
419
420         /* If this file is already loaded, skip this step */
421         tpnt = _dl_check_hashed_files(libname);
422         if (tpnt) {
423                 if (*rpnt) {
424                         (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
425                         _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
426                         (*rpnt)->next->prev = (*rpnt);
427                         *rpnt = (*rpnt)->next;
428                         (*rpnt)->dyn = tpnt;
429                         tpnt->symbol_scope = _dl_symbol_tables;
430                 }
431                 tpnt->usage_count++;
432                 tpnt->libtype = elf_lib;
433 #if defined (__SUPPORT_LD_DEBUG__)
434                 if(_dl_debug) _dl_dprintf(2, "file='%s';  already loaded\n", libname);
435 #endif
436                 return tpnt;
437         }
438
439         /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
440            we don't load the library if it isn't setuid. */
441
442         if (secure) {
443                 struct stat st;
444
445                 if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
446                         return NULL;
447         }
448
449         libaddr = 0;
450         infile = _dl_open(libname, O_RDONLY, 0);
451         if (infile < 0) {
452 #if 0
453                 /*
454                  * NO!  When we open shared libraries we may search several paths.
455                  * it is inappropriate to generate an error here.
456                  */
457                 _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
458 #endif
459                 _dl_internal_error_number = LD_ERROR_NOFILE;
460                 return NULL;
461         }
462
463         header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
464                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
465         if (_dl_mmap_check_error(header)) {
466                 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
467                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
468                 _dl_close(infile);
469                 return NULL;
470         };
471
472         _dl_read(infile, header, _dl_pagesize);
473         epnt = (ElfW(Ehdr) *) (intptr_t) header;
474         if (epnt->e_ident[0] != 0x7f ||
475                         epnt->e_ident[1] != 'E' ||
476                         epnt->e_ident[2] != 'L' ||
477                         epnt->e_ident[3] != 'F')
478         {
479                 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
480                                 libname);
481                 _dl_internal_error_number = LD_ERROR_NOTELF;
482                 _dl_close(infile);
483                 _dl_munmap(header, _dl_pagesize);
484                 return NULL;
485         };
486
487         if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
488 #ifdef MAGIC2
489                                 && epnt->e_machine != MAGIC2
490 #endif
491                                 ))
492         {
493                 _dl_internal_error_number =
494                         (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
495                 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
496                                 "\n", _dl_progname, libname);
497                 _dl_close(infile);
498                 _dl_munmap(header, _dl_pagesize);
499                 return NULL;
500         };
501
502         ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
503
504         piclib = 1;
505         for (i = 0; i < epnt->e_phnum; i++) {
506
507                 if (ppnt->p_type == PT_DYNAMIC) {
508                         if (dynamic_addr)
509                                 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
510                                                 _dl_progname, libname);
511                         dynamic_addr = ppnt->p_vaddr;
512                 };
513
514                 if (ppnt->p_type == PT_LOAD) {
515                         /* See if this is a PIC library. */
516                         if (i == 0 && ppnt->p_vaddr > 0x1000000) {
517                                 piclib = 0;
518                                 minvma = ppnt->p_vaddr;
519                         }
520                         if (piclib && ppnt->p_vaddr < minvma) {
521                                 minvma = ppnt->p_vaddr;
522                         }
523                         if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
524                                 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
525                         }
526                 }
527                 ppnt++;
528         };
529
530         maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
531         minvma = minvma & ~0xffffU;
532
533         flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
534         if (!piclib)
535                 flags |= MAP_FIXED;
536
537         status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
538                         maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
539         if (_dl_mmap_check_error(status)) {
540                 _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
541                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
542                 _dl_close(infile);
543                 _dl_munmap(header, _dl_pagesize);
544                 return NULL;
545         };
546         libaddr = (unsigned long) status;
547         flags |= MAP_FIXED;
548
549         /* Get the memory to store the library */
550         ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
551
552         for (i = 0; i < epnt->e_phnum; i++) {
553                 if (ppnt->p_type == PT_GNU_RELRO) {
554                         relro_addr = ppnt->p_vaddr;
555                         relro_size = ppnt->p_memsz;
556                 }
557                 if (ppnt->p_type == PT_LOAD) {
558
559                         /* See if this is a PIC library. */
560                         if (i == 0 && ppnt->p_vaddr > 0x1000000) {
561                                 piclib = 0;
562                                 /* flags |= MAP_FIXED; */
563                         }
564
565
566
567                         if (ppnt->p_flags & PF_W) {
568                                 unsigned long map_size;
569                                 char *cpnt;
570
571                                 status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
572                                                         (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
573                                                 + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
574                                                 ppnt->p_offset & OFFS_ALIGN);
575
576                                 if (_dl_mmap_check_error(status)) {
577                                         _dl_dprintf(2, "%s: can't map '%s'\n",
578                                                         _dl_progname, libname);
579                                         _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
580                                         _dl_munmap((char *) libaddr, maxvma - minvma);
581                                         _dl_close(infile);
582                                         _dl_munmap(header, _dl_pagesize);
583                                         return NULL;
584                                 };
585
586                                 /* Pad the last page with zeroes. */
587                                 cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
588                                                 ppnt->p_filesz);
589                                 while (((unsigned long) cpnt) & ADDR_ALIGN)
590                                         *cpnt++ = 0;
591
592                                 /* I am not quite sure if this is completely
593                                  * correct to do or not, but the basic way that
594                                  * we handle bss segments is that we mmap
595                                  * /dev/zero if there are any pages left over
596                                  * that are not mapped as part of the file */
597
598                                 map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
599
600                                 if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
601                                         status = (char *) _dl_mmap((char *) map_size +
602                                                         (piclib ? libaddr : 0),
603                                                         ppnt->p_vaddr + ppnt->p_memsz - map_size,
604                                                         LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
605                         } else
606                                 status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
607                                                 + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
608                                                 ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
609                                                 infile, ppnt->p_offset & OFFS_ALIGN);
610                         if (_dl_mmap_check_error(status)) {
611                                 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
612                                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
613                                 _dl_munmap((char *) libaddr, maxvma - minvma);
614                                 _dl_close(infile);
615                                 _dl_munmap(header, _dl_pagesize);
616                                 return NULL;
617                         };
618
619                         /* if(libaddr == 0 && piclib) {
620                            libaddr = (unsigned long) status;
621                            flags |= MAP_FIXED;
622                            }; */
623                 };
624                 ppnt++;
625         };
626         _dl_close(infile);
627
628         /* For a non-PIC library, the addresses are all absolute */
629         if (piclib) {
630                 dynamic_addr += (unsigned long) libaddr;
631         }
632
633         /*
634          * OK, the ELF library is now loaded into VM in the correct locations
635          * The next step is to go through and do the dynamic linking (if needed).
636          */
637
638         /* Start by scanning the dynamic section to get all of the pointers */
639
640         if (!dynamic_addr) {
641                 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
642                 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
643                                 _dl_progname, libname);
644                 _dl_munmap(header, _dl_pagesize);
645                 return NULL;
646         }
647
648         dpnt = (ElfW(Dyn) *) dynamic_addr;
649         _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
650         _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, libaddr);
651         /* If the TEXTREL is set, this means that we need to make the pages
652            writable before we perform relocations.  Do this now. They get set
653            back again later. */
654
655         if (dynamic_info[DT_TEXTREL]) {
656 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
657                 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
658                 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
659                         if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
660                                 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
661                                                         (ppnt->p_vaddr & PAGE_ALIGN)),
662                                                 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
663                                                 PROT_READ | PROT_WRITE | PROT_EXEC);
664                 }
665 #else
666                 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
667                 _dl_exit(1);
668 #endif
669         }
670
671         tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
672                         dynamic_addr, 0);
673         tpnt->relro_addr = relro_addr;
674         tpnt->relro_size = relro_size;
675         tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
676         tpnt->n_phent = epnt->e_phnum;
677
678         /*
679          * Add this object into the symbol chain
680          */
681         if (*rpnt) {
682                 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
683                 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
684                 (*rpnt)->next->prev = (*rpnt);
685                 *rpnt = (*rpnt)->next;
686                 (*rpnt)->dyn = tpnt;
687                 tpnt->symbol_scope = _dl_symbol_tables;
688         }
689         tpnt->usage_count++;
690         tpnt->libtype = elf_lib;
691
692         /*
693          * OK, the next thing we need to do is to insert the dynamic linker into
694          * the proper entry in the GOT so that the PLT symbols can be properly
695          * resolved.
696          */
697
698         lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
699
700         if (lpnt) {
701                 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
702                 INIT_GOT(lpnt, tpnt);
703         };
704
705 #if defined (__SUPPORT_LD_DEBUG__)
706         if(_dl_debug) {
707                 _dl_dprintf(2, "\n\tfile='%s';  generating link map\n", libname);
708                 _dl_dprintf(2, "\t\tdynamic: %x  base: %x\n",
709                                 dynamic_addr, libaddr);
710                 _dl_dprintf(2, "\t\t  entry: %x  phdr: %x  phnum: %x\n\n",
711                                 epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
712
713         }
714 #endif
715         _dl_munmap(header, _dl_pagesize);
716
717         return tpnt;
718 }
719 /* now_flag must be RTLD_NOW or zero */
720 int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
721 {
722         int goof = 0;
723         struct elf_resolve *tpnt;
724         ElfW(Word) reloc_size, reloc_addr, relative_count;
725
726         if (rpnt->next)
727                 goof += _dl_fixup(rpnt->next, now_flag);
728         tpnt = rpnt->dyn;
729
730 #if defined (__SUPPORT_LD_DEBUG__)
731         if(!(tpnt->init_flag & RELOCS_DONE)) 
732                 _dl_if_debug_dprint("\nrelocation processing: %s\n", tpnt->libname);
733 #endif
734
735         if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
736                 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
737                                 _dl_progname, UNSUPPORTED_RELOC_STR);
738                 goof++;
739                 return goof;
740         }
741
742         reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
743 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
744    range.  Note that according to the ELF spec, this is completely legal! */
745 #ifdef ELF_MACHINE_PLTREL_OVERLAP
746         reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
747 #endif
748         if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
749             !(tpnt->init_flag & RELOCS_DONE)) {
750                 tpnt->init_flag |= RELOCS_DONE;
751                 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
752                 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
753                 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
754                         reloc_size -= relative_count * sizeof(ELF_RELOC);
755                         elf_machine_relative (tpnt->loadaddr, reloc_addr, relative_count);
756                         reloc_addr += relative_count * sizeof(ELF_RELOC);
757                 }
758                 goof += _dl_parse_relocation_information(rpnt,
759                                 reloc_addr,
760                                 reloc_size);
761         }
762         if (tpnt->dynamic_info[DT_BIND_NOW])
763                 now_flag = RTLD_NOW;
764         if (tpnt->dynamic_info[DT_JMPREL] &&
765             (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
766              (now_flag && !(tpnt->rtld_flags & now_flag)))) {
767                 tpnt->rtld_flags |= now_flag; 
768                 tpnt->init_flag |= JMP_RELOCS_DONE;
769                 if (!(tpnt->rtld_flags & RTLD_NOW)) {
770                         _dl_parse_lazy_relocation_information(rpnt,
771                                         tpnt->dynamic_info[DT_JMPREL],
772                                         tpnt->dynamic_info [DT_PLTRELSZ]);
773                 } else {
774                         goof += _dl_parse_relocation_information(rpnt,
775                                         tpnt->dynamic_info[DT_JMPREL],
776                                         tpnt->dynamic_info[DT_PLTRELSZ]);
777                 }
778         }
779         return goof;
780 }
781
782 /* Minimal printf which handles only %s, %d, and %x */
783 void _dl_dprintf(int fd, const char *fmt, ...)
784 {
785         int num;
786         va_list args;
787         char *start, *ptr, *string;
788         static char *buf;
789
790         buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
791                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
792         if (_dl_mmap_check_error(buf)) {
793                 _dl_write(fd, "mmap of a spare page failed!\n", 29);
794                 _dl_exit(20);
795         }
796
797         start = ptr = buf;
798
799         if (!fmt)
800                 return;
801
802         if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
803                 _dl_write(fd, "overflow\n", 11);
804                 _dl_exit(20);
805         }
806
807         _dl_strcpy(buf, fmt);
808         va_start(args, fmt);
809
810         while (start) {
811                 while (*ptr != '%' && *ptr) {
812                         ptr++;
813                 }
814
815                 if (*ptr == '%') {
816                         *ptr++ = '\0';
817                         _dl_write(fd, start, _dl_strlen(start));
818
819                         switch (*ptr++) {
820                                 case 's':
821                                         string = va_arg(args, char *);
822
823                                         if (!string)
824                                                 _dl_write(fd, "(null)", 6);
825                                         else
826                                                 _dl_write(fd, string, _dl_strlen(string));
827                                         break;
828
829                                 case 'i':
830                                 case 'd':
831                                         {
832                                                 char tmp[22];
833                                                 num = va_arg(args, int);
834
835                                                 string = _dl_simple_ltoa(tmp, num);
836                                                 _dl_write(fd, string, _dl_strlen(string));
837                                                 break;
838                                         }
839                                 case 'x':
840                                 case 'X':
841                                         {
842                                                 char tmp[22];
843                                                 num = va_arg(args, int);
844
845                                                 string = _dl_simple_ltoahex(tmp, num);
846                                                 _dl_write(fd, string, _dl_strlen(string));
847                                                 break;
848                                         }
849                                 default:
850                                         _dl_write(fd, "(null)", 6);
851                                         break;
852                         }
853
854                         start = ptr;
855                 } else {
856                         _dl_write(fd, start, _dl_strlen(start));
857                         start = NULL;
858                 }
859         }
860         _dl_munmap(buf, _dl_pagesize);
861         return;
862 }
863
864 char *_dl_strdup(const char *string)
865 {
866         char *retval;
867         int len;
868
869         len = _dl_strlen(string);
870         retval = _dl_malloc(len + 1);
871         _dl_strcpy(retval, string);
872         return retval;
873 }
874
875 void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, ElfW(Addr) load_off)
876 {
877         __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
878 }
879 #ifdef __USE_GNU
880 #if ! defined LIBDL || (! defined PIC && ! defined __PIC__)
881 int
882 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
883 {
884         struct elf_resolve *l;
885         struct dl_phdr_info info;
886         int ret = 0;
887
888         for (l = _dl_loaded_modules; l != NULL; l = l->next) {
889                 info.dlpi_addr = l->loadaddr;
890                 info.dlpi_name = l->libname;
891                 info.dlpi_phdr = l->ppnt;
892                 info.dlpi_phnum = l->n_phent;
893                 ret = callback (&info, sizeof (struct dl_phdr_info), data);
894                 if (ret)
895                         break;
896         }
897         return ret;
898 }
899 strong_alias(__dl_iterate_phdr, dl_iterate_phdr);
900 #endif
901 #endif