OSDN Git Service

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