OSDN Git Service

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